@code-yeongyu/senpi
Version:
Coding agent CLI with read, bash, edit, write tools and session management
249 lines • 13.4 kB
JavaScript
/**
* `release_session`: a host hands one of its sessions to a runtime outside it - `omo daemon adopt`
* resumes it in a local terminal with `senpi --session <session_path>`.
*
* The hand-over is a teardown, not a transfer: nothing is replayed, the runtime is disposed and the
* path reservation is released (the in-process one and the cross-generation claim), so the next
* process to write the file is the one the caller starts. Before that the transcript gains one
* `session_released` entry - a `custom` bookkeeping entry the model never sees - so the file itself
* records which host let go of it and when. Clients still attached (only with `force`) receive
* `session_closed { reason: "released" }`, which tells them NOT to reopen the path here.
*
* A session is released only when it is QUIET: no agent run, no prompt still in preflight, no queued user
* input, no admitted delivery waiting to be written, no bash, compaction or barrier-held session work (the fields the
* handoff park judges by), and no other request for the session in flight on any connection. Anything
* else would write the file after the new writer took it. Busy is refused with nothing changed -
* `turn_active` when a turn is running or about to start, `session_busy` for other work, both naming
* the signals in `errorData.busy` - unless `interrupt` is set: then the run and any bash are aborted,
* the release waits (bounded) for the run to go idle and for the other requests and prompts to
* settle, and checks again. The last check and the close claim run in one synchronous step, so a
* command routed after it finds the session closing, and work started before it is seen by it.
* Also refused while clients are attached (`attached`, unless `force`) and for a session this host
* cannot hand over (`release_unsupported`: a worker isolate owns the runtime, or there is no file).
*/
import { rm } from "node:fs/promises";
import { isMoveId, isPlainId, parseMoveId } from "../../core/session-move/bundle.js";
import { assertMoveFits } from "../../core/session-move/export.js";
import { prepareOutgoingState, STATE_ATTEMPT_FIELD } from "../../core/session-move/extension-state.js";
import { SessionMovedAwayError } from "../../core/session-move/ownership.js";
import { isHandoffBusy } from "./handoff-activity.js";
import { RPC_ERROR_ATTACHED, RPC_ERROR_HOST_DRAINING, RPC_ERROR_INVALID_RELEASE_REASON, RPC_ERROR_RELEASE_FAILED, RPC_ERROR_RELEASE_UNSUPPORTED, RPC_ERROR_SESSION_BUSY, RPC_ERROR_SESSION_CLOSING, RPC_ERROR_TURN_ACTIVE, } from "./rpc-types.js";
import { abortAndSettle, NOTHING_DROPPED, takeQueuedInput, } from "./session-release-interrupt.js";
/** `customType` of the transcript entry a release appends. */
export const SESSION_RELEASED_ENTRY_TYPE = "session_released";
/** What `pi.session.admitExternalMessage` throws once a release has closed admission. */
export const RELEASED_ADMISSION_CLOSED = "release_session handed this session to another runtime; deliver there instead (admission is closed here)";
/** `errorData.hint` of a refusal whose `busy` names `queued`: only an interrupt hands that input back. */
export const RELEASE_QUEUED_HINT = "queued user input is still owed a turn; release with interrupt: true to take it out and receive it in dropped.user_messages";
export async function releaseSession(port, command) {
// Export and the receiver's ledger accept only a plain move id; a release under any other id could never finish.
const validMove = command.reason === "move" && isMoveId(command.moveId);
if (command.reason !== "takeover" && !validMove)
return refusal(command.id, RPC_ERROR_INVALID_RELEASE_REASON);
if (port.draining())
return refusal(command.id, RPC_ERROR_HOST_DRAINING);
const first = releasable(port, command, undefined);
if (!("session" in first))
return first;
// The seal records the session's own id, which the bundle and the receiver use as a folder name: a move of a session
// whose id is not a plain id is refused here, before anything is interrupted or sealed (never sanitized).
if (command.reason === "move" && !isPlainId(first.session.sessionManager.getSessionId())) {
return refusal(command.id, RPC_ERROR_RELEASE_UNSUPPORTED, { detail: "session_id_not_plain" });
}
const busy = busySignals(port, command.sessionId, first.session);
const interrupted = busy.length > 0;
if (interrupted && command.interrupt !== true) {
return refusal(command.id, busyCode(busy), { attachments: first.attachments, ...busyData(busy) });
}
// Admission closes below (for the interrupt, or at the claim). Every answer except a release reopens it
// on the session the host keeps, and a throw is answered `release_failed` rather than left unanswered.
const admission = first.session.externalAdmission;
let dropped = NOTHING_DROPPED;
let answer;
let stagedState;
let sealed = false;
try {
if (interrupted) {
admission.close(RELEASED_ADMISSION_CLOSED);
const taken = takeQueuedInput(first.session);
const handedBack = await abortAndSettle(port, command.sessionId, first.session);
dropped = { ...taken, deliveries: [...taken.deliveries, ...handedBack] };
// What the clear retired is on disk before `session_released` seals the file (senpi#3549).
await first.session.whenQueueClearRecorded();
}
// A never-written session gets its file here, before the final check: its `session_released` entry
// must reach disk, and a header write that fails is this release's failure, never a stray rejection.
// A written session awaits nothing, so its claim stays in the same turn as the request that asked.
const manager = first.session.sessionManager;
if (!manager.isTranscriptFlushed())
await manager.persistHeaderNow();
// A move takes the extensions' out-of-folder state with it; they write it while their runtime is still live.
// Each handler runs under the session_shutdown handler budget: a hung one fails the release instead of holding it.
if (command.reason === "move") {
const moveId = parseMoveId(command.moveId);
stagedState = await prepareOutgoingState(first.sessionPath, moveId);
const [failed] = await first.session.extensionRunner.emitSessionMove({
type: "session_move_export",
moveId,
dir: stagedState.dir,
});
if (failed !== undefined)
throw new Error(`${failed.extensionPath} could not write its state for the move: ${failed.error}`);
// State a bundle could not carry refuses the release here, before the seal: the session stays live.
await assertMoveFits(first.sessionPath, manager.getSessionId(), stagedState.dir, false);
}
answer = await claimAndRelease(port, command, interrupted, dropped, stagedState?.attempt, () => {
sealed = true;
});
return answer;
}
catch (cause) {
if (cause instanceof SessionMovedAwayError) {
answer = refusal(command.id, cause.code, {
detail: cause.message,
...(interrupted ? { interrupted: true, dropped } : {}),
});
return answer;
}
answer = refusal(command.id, RPC_ERROR_RELEASE_FAILED, {
detail: cause instanceof Error ? cause.message : String(cause),
...(interrupted ? { interrupted: true, dropped } : {}),
});
return answer;
}
finally {
if (answer?.success !== true) {
admission.reopen();
// A move refused before its seal is abandoned: what its export staged is never bundled, and a retry stages
// afresh. Once the seal names this attempt, its folder is the move's state and stays for export.
if (stagedState !== undefined && !sealed)
await rm(stagedState.dir, { recursive: true, force: true });
}
}
}
/**
* The final check and the release. After an interrupt every refusal carries `interrupted` and `dropped`:
* the queues were already emptied, so what they held must reach the caller whatever the answer.
*/
async function claimAndRelease(port, command, interrupted, dropped, stateAttempt, onSealed) {
const taken = interrupted ? { interrupted: true, dropped } : undefined;
const ready = releasable(port, command, taken);
if (!("session" in ready))
return ready;
const stillBusy = busySignals(port, command.sessionId, ready.session);
if (stillBusy.length > 0) {
return refusal(command.id, busyCode(stillBusy), {
attachments: ready.attachments,
...busyData(stillBusy),
interrupted,
...taken,
});
}
// From here to the close claim nothing awaits: a drain pass still running admits nothing more, so a
// delivery it had not admitted stays with its sender and reaches the next owner.
const admission = ready.session.externalAdmission;
admission.close(RELEASED_ADMISSION_CLOSED);
try {
// On disk or not at all: a failed write leaves no entry a later append could chain onto.
ready.session.sessionManager.appendCustomEntry(SESSION_RELEASED_ENTRY_TYPE, {
reason: command.reason,
...(command.reason === "move"
? {
move_id: command.moveId,
// The export bundles only this attempt's staged state: a late write from an earlier one never travels.
...(stateAttempt === undefined ? {} : { [STATE_ATTEMPT_FIELD]: stateAttempt }),
target: command.target ?? "",
session_id: ready.session.sessionManager.getSessionId(),
}
: {}),
interrupted,
attachments: ready.attachments,
host_instance: port.hostInstance ?? null,
released_at: new Date().toISOString(),
});
}
catch (cause) {
admission.reopen();
throw cause;
}
onSealed();
if (!(await port.tearDown(command.sessionId, ready.sessionPath))) {
return refusal(command.id, RPC_ERROR_SESSION_CLOSING, taken);
}
return {
id: command.id,
type: "response",
command: "release_session",
success: true,
data: { released: true, session_path: ready.sessionPath, attachments: ready.attachments, dropped },
};
}
function busySignals(port, sessionId, session) {
const activity = session.activitySnapshot;
const signals = [];
if (activity.isStreaming)
signals.push("turn");
if (port.pendingPrompts(sessionId).length > 0)
signals.push("prompt");
// Queued user input outlives its run (a steer can end the stream it was aimed at): it is still owed a turn.
if (session.pendingMessageCount > 0)
signals.push("queued");
if (session.externalAdmission.list().pending.length > 0)
signals.push("delivery");
if (activity.isBashRunning)
signals.push("bash");
if (activity.isCompacting)
signals.push("compaction");
if (activity.hasSessionWork)
signals.push("session_work");
// The handoff park's predicate decides; a source added to it later is not missed here.
if (signals.length === 0 && isHandoffBusy(activity))
signals.push("activity");
if (port.otherRequests(sessionId) > 0)
signals.push("request");
return signals;
}
function busyData(signals) {
return signals.includes("queued")
? { busy: signals, retry_with: { interrupt: true }, hint: RELEASE_QUEUED_HINT }
: { busy: signals };
}
function busyCode(signals) {
return signals.some((signal) => signal === "turn" || signal === "prompt" || signal === "queued" || signal === "delivery")
? RPC_ERROR_TURN_ACTIVE
: RPC_ERROR_SESSION_BUSY;
}
/** `taken`: what an interrupt already took out of the session, merged into any refusal's `errorData`. */
function releasable(port, command, taken) {
const refuse = (code, data) => refusal(command.id, code, data || taken ? { ...data, ...taken } : undefined);
let entry;
try {
entry = port.lookup(command.sessionId);
}
catch (cause) {
return refuse(port.code(cause));
}
if (entry.state !== "open")
return refuse(RPC_ERROR_SESSION_CLOSING);
const session = entry.runtime?.session;
if (session === undefined)
return refuse(RPC_ERROR_RELEASE_UNSUPPORTED, { detail: "worker_runtime" });
const sessionPath = entry.sessionPath ?? session.sessionFile;
if (sessionPath === undefined)
return refuse(RPC_ERROR_RELEASE_UNSUPPORTED, { detail: "no_session_file" });
const elsewhere = port.otherAttachments(command.sessionId);
if (elsewhere > 0 && command.force !== true)
return refuse(RPC_ERROR_ATTACHED, { attachments: elsewhere });
return { session, sessionPath, attachments: entry.attachments };
}
function refusal(id, code, data) {
return {
id,
type: "response",
command: "release_session",
success: false,
error: code,
...(data && { errorCode: code, errorData: data }),
};
}
//# sourceMappingURL=session-release.js.map