@code-yeongyu/senpi
Version:
Coding agent CLI with read, bash, edit, write tools and session management
77 lines • 3.58 kB
JavaScript
import { takeQueuedInput } from "./session-release-interrupt.js";
const GRACEFUL_CLOSE_DEFAULT_WAIT_MS = 60_000;
const GRACEFUL_CLOSE_MAX_WAIT_MS = 600_000;
/** What `admit_external_message` refuses with (`session_closing`) while a graceful close drains. */
const GRACEFUL_ADMISSION_CLOSED = "session_closing: the session is closing on request and takes no new deliveries";
export function gracefulWaitMs(requested) {
return Math.min(requested ?? GRACEFUL_CLOSE_DEFAULT_WAIT_MS, GRACEFUL_CLOSE_MAX_WAIT_MS);
}
export function fenceForClose(session) {
session.holdForClose();
session.externalAdmission.close(GRACEFUL_ADMISSION_CLOSED);
return { dropped: takeQueuedInput(session, false), startedAt: Date.now() };
}
/** A drain that did not end in a close leaves the session open and admitting, as before the fence. */
export function reopenAfterDrain(session) {
session.releaseCloseHold();
session.externalAdmission.reopen();
}
/**
* Resolves `true` once `port.busy()` is empty, `false` when `waitMs` passes first. It wakes on every session event and
* on the settling work, re-checking each time; nothing polls. It subscribes once: `subscribe` replays standing events
* (a selected settings source, a resume slice) to the new listener at once, and a listener taken per pass would count
* that replay as a change and spin the host's loop until the deadline, so the turn it waits for could never stream.
*/
export async function drainUntilQuiet(session, port, waitMs) {
const deadline = Date.now() + waitMs;
let settlingPending = true;
let changed = Promise.withResolvers();
const unsubscribe = session.subscribe(() => changed.resolve());
try {
while (port.busy().length > 0) {
const left = deadline - Date.now();
if (left <= 0)
return false;
changed = Promise.withResolvers();
let timer;
const expired = new Promise((resolve) => {
timer = setTimeout(resolve, left);
});
const waits = [changed.promise.then(() => (settlingPending = true)), expired];
// Settled work stays settled: once it is, only a session event can change what keeps the session busy.
if (settlingPending)
waits.push(Promise.allSettled([session.waitForIdle(), ...port.settling()]).then(() => (settlingPending = false)));
try {
await Promise.race(waits);
}
finally {
clearTimeout(timer);
}
}
return true;
}
finally {
unsubscribe();
}
}
export function drainFacts(session, drain) {
return { turn: session.externalAdmission.turnEpoch, waited_ms: Date.now() - drain.startedAt };
}
/**
* The quiet session's clean stop, in the same synchronous step as the close claim that follows it: the closing hold is
* released (a reopen replays no pause), the `session-closed` record is written, and the teardown's `session_shutdown`
* will carry `closed: true`. Answers the record.
*/
export function recordGracefulClose(session, drain) {
const record = {
graceful: true,
...drainFacts(session, drain),
returned: { deliveries: [...drain.dropped.deliveries], user_messages: [...drain.dropped.user_messages] },
stopped: { wake_sources: [...session.activeWakeSources] },
at: new Date().toISOString(),
};
session.releaseCloseHold();
session.recordClosedOnRequest(record);
return record;
}
//# sourceMappingURL=session-graceful-close.js.map