@code-yeongyu/senpi
Version:
Coding agent CLI with read, bash, edit, write tools and session management
115 lines • 4.83 kB
JavaScript
function liveStatus(runtime, fanout) {
// A question is asked from inside a running turn, and compaction can run inside one too: the narrower
// state wins, so a supervisor sees "waiting on the user" rather than a turn that merely looks busy.
// An unknown fanout count is no evidence either way: the runtime's own count decides.
if ((fanout.pendingQuestions ?? 0) > 0 || runtime.pendingQuestions > 0)
return "waiting_input";
if (runtime.isCompacting)
return "compacting";
if (runtime.isStreaming)
return "running";
return "idle";
}
function statusOf(entry, runtime, fanout, terminal) {
if (terminal !== undefined)
return terminal.status;
switch (entry.state) {
case "opening":
return "opening";
case "closing":
case "quarantined":
case "closed":
return "closed";
case "open":
return runtime === undefined ? "opening" : liveStatus(runtime, fanout);
default: {
const exhaustive = entry.state;
throw new Error(`unexpected session state ${String(exhaustive)}`);
}
}
}
/**
* Derive a session's status. `terminal` is the seal the writer recorded (park, close, release); `cause`
* is the change being published, used only for the `reason` of a run that an abort ended.
*/
export function deriveSessionStatus(entry, runtime, fanout, terminal, cause) {
const status = statusOf(entry, runtime, fanout, terminal);
const reason = terminal?.reason ?? (status === "idle" && cause === "abort" ? "aborted" : undefined);
return {
status,
...(reason !== undefined ? { reason } : {}),
kind: entry.kind,
cwd: entry.cwd,
attachments: Math.max(0, entry.attachments),
...(entry.durableSessionId !== undefined ? { durableSessionId: entry.durableSessionId } : {}),
...(entry.sessionPath !== undefined ? { sessionPath: entry.sessionPath } : {}),
...(entry.name !== undefined ? { name: entry.name } : {}),
archived: entry.archived === true,
...(entry.settled ?? { settled: false }),
...(runtime?.model !== undefined ? { model: { provider: runtime.model.provider, id: runtime.model.id } } : {}),
...(runtime?.thinkingLevel !== undefined ? { thinkingLevel: runtime.thinkingLevel } : {}),
pendingQueue: { steering: runtime?.steering ?? 0, followUp: runtime?.followUp ?? 0 },
...(runtime?.waiting !== undefined && status === "running" ? { waiting: runtime.waiting } : {}),
context: entry.context,
};
}
/**
* The terminal a lifecycle record seals a session with. A park and a handoff park leave a session that
* reopens by path (`unloaded`); every other close ends it (`closed`) with today's `session_closed.reason`.
*/
export function terminalOfLifecycle(record) {
const reason = typeof record.reason === "string" ? record.reason : undefined;
if (record.type === "session_parked")
return {
status: "unloaded",
reason: reason === "unloaded" ? "unload_session" : reason === "host_shutdown" ? "host_shutdown" : "idle",
};
if (record.type !== "session_closed")
return undefined;
if (reason === "handoff_parked")
return { status: "unloaded", reason: "handoff" };
return reason === undefined ? { status: "closed" } : { status: "closed", reason };
}
/** The status change a session record publishes, or undefined when the record changes no status. */
export function causeOfRecord(record) {
switch (record.type) {
case "agent_start":
return "prompt";
case "agent_end":
return "agent_end";
case "agent_idle":
return "agent_idle";
case "session_abort":
return "abort";
case "queue_update":
return "queue";
case "model_changed":
case "thinking_level_changed":
return "set_model";
case "session_replaced":
return "replace";
case "session_info_changed":
return "rename";
case "session_parked":
return "park";
case "session_closed":
return "close";
case "session_archived_changed":
return "archive";
case "session_settled_changed":
return "settle";
case "question_resolved":
return "question";
case "auto_retry_start":
case "auto_retry_end":
return "retry";
case "extension_ui_request":
return record.method === "question" ? "question" : undefined;
case "compaction_start":
case "compaction_end":
return "compaction";
default:
return undefined;
}
}
//# sourceMappingURL=session-status.js.map