UNPKG

@code-yeongyu/senpi

Version:

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

71 lines • 2.92 kB
export class SessionRunActivity { #running = new Set(); /** Settled sessions not yet at agent_idle, by settle time (omo#11419). */ #settling = new Map(); #settleHoldMs; #now; #onSettleHoldExpired; constructor(options = {}) { this.#settleHoldMs = options.settleHoldMs ?? Number.POSITIVE_INFINITY; this.#now = options.now ?? Date.now; this.#onSettleHoldExpired = options.onSettleHoldExpired; } /** Applies one host event line; true when it is a run or session event the idle decision must re-read. */ observe(line) { const event = parseHostEvent(line); if (event === undefined) return false; if (event.type === "agent_start") { this.#running.add(event.sessionId); this.#settling.delete(event.sessionId); } else if (event.type === "agent_settled" && event.reason !== "session_closed") { // The run is over, but agent_settled reaches clients before its extension handlers finish (omo#11419): the // session holds idle exit until agent_idle, for at most the settle hold. this.#running.delete(event.sessionId); this.#settling.set(event.sessionId, this.#now()); } else if (event.type === "agent_settled" || event.type === "agent_idle" || event.type === "session_closed") { // A seal's owed settle (reason session_closed) or a closed session: nothing is left to wait for. this.#running.delete(event.sessionId); this.#settling.delete(event.sessionId); } else return false; return true; } /** Sessions with a run in progress. */ get busySessions() { return this.#running.size; } /** Settled sessions whose agent_settled handlers may still run, within the settle hold. */ get settlingSessions() { const now = this.#now(); for (const [sessionId, settledAt] of this.#settling) { const heldMs = now - settledAt; if (heldMs < this.#settleHoldMs) continue; this.#settling.delete(sessionId); this.#onSettleHoldExpired?.(sessionId, heldMs); } return this.#settling.size; } } /** An idle window that ran out still waits while a settled session's handlers are within their hold (omo#11419). */ export function holdIdleExitForSettling(decision, runs) { return decision === "exit" && runs.settlingSessions > 0 ? "idle" : decision; } function parseHostEvent(line) { let event; try { event = JSON.parse(line); } catch { return undefined; } if (typeof event !== "object" || event === null) return undefined; const { type, sessionId, reason } = event; return typeof sessionId === "string" ? { type, sessionId, reason } : undefined; } //# sourceMappingURL=host-run-activity.js.map