UNPKG

@code-yeongyu/senpi

Version:

Coding agent CLI with read, bash, edit, write tools and session management

62 lines • 3 kB
/** How long an `interrupt` release waits for the aborted work to settle before it re-checks. */ export const RELEASE_SETTLE_MS = 10_000; export const NOTHING_DROPPED = { deliveries: [], user_messages: [] }; /** * Empties both queues. Queued deliveries leave the ledger unwritten, so their sender redelivers them to * the next owner; the user's queued text is returned in enqueue order for the caller to restore. A graceful close * takes them without aborting anything afterwards (`abortWillFollow: false`). */ export function takeQueuedInput(session, abortWillFollow = true) { const admittedBefore = session.externalAdmission.list().pending; const cleared = session.clearQueue({ abortWillFollow }); const stillAdmitted = new Set(session.externalAdmission.list().pending); return { deliveries: admittedBefore.filter((deliveryId) => !stillAdmitted.has(deliveryId)), user_messages: cleared.ordered.map((queued) => queued.text), }; } /** * Aborts the work, waits (bounded) for it to settle, then hands back the deliveries it left unwritten: their ids, for * the caller to report. Nothing is handed back while the session is still busy. */ export async function abortAndSettle(port, sessionId, session) { session.abortBash(); // A prompt still in preflight, or an admitted delivery, may start its run after the abort below. const stopStarts = session.subscribe((event) => { if (event.type === "agent_start") void session.abort(); }); let deadline; const expired = new Promise((resolve) => { deadline = setTimeout(resolve, RELEASE_SETTLE_MS); }); try { await Promise.race([ Promise.allSettled([ session.abort().then(() => session.waitForIdle()), port.otherRequestsSettled(sessionId), ...port.pendingPrompts(sessionId), ]), expired, ]); } finally { clearTimeout(deadline); stopStarts(); } return session.isIdle ? handBackUnwrittenDeliveries(session) : []; } /** * A delivery admitted while a run was busy waits in the runtime's follow-up queue, and one whose start the abort cut * short is queued again under its engine ticket. When the run is aborted either stays there for a person's next turn * and its admission stays pending, which refused every interrupting unload forever (oh-my-openagent#14169). Settled * after an interrupt, every such delivery leaves the queue, its custody and the ledger unwritten, so its sender * redelivers it to the next owner, as `takeQueuedInput` does for a release. */ function handBackUnwrittenDeliveries(session) { const admittedBefore = session.externalAdmission.list().pending; session.takeQueuedDeliveries(); const stillAdmitted = new Set(session.externalAdmission.list().pending); return admittedBefore.filter((deliveryId) => !stillAdmitted.has(deliveryId)); } //# sourceMappingURL=session-release-interrupt.js.map