UNPKG

@code-yeongyu/senpi

Version:

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

237 lines • 11.2 kB
import { readClientMessageIdentity } from "../../core/client-message-identity.js"; import { CLIENT_ADMISSION_ENTRY, clientAdmissionRecordSchema, } from "./client-admission-record.js"; import { admitClientInput } from "./client-admission-intake.js"; import { EFFECT_OUTCOMES, turnErrorOutcome } from "./effect-outcome-copy.js"; import { projectAdmission } from "./effect-status.js"; import { noteSessionEffect } from "./session-effect-status.js"; const sessions = new WeakMap(); export class ClientAdmissions { constructor(session) { this.records = new Map(); this.pending = new Map(); this.restoring = true; this.hasIdentities = false; this.session = session; const durableSessionId = session.sessionManager.getSessionId(); this.durableSessionId = durableSessionId; for (const entry of session.sessionManager.getEntries()) { if (entry.type !== "custom" || entry.customType !== CLIENT_ADMISSION_ENTRY) continue; // A custom entry is opaque transcript data: one that does not parse as an admission // (an extension reusing the type, a hand-edited file) is not an admission and must not // keep the session from opening. const parsed = clientAdmissionRecordSchema.safeParse(entry.data); if (parsed.success && parsed.data.durableSessionId === durableSessionId) { this.records.set(parsed.data.clientMessageId, parsed.data); } } // Only ids a client sent switch on event correlation; ids the host minted stay internal. this.hasIdentities = [...this.records.values()].some((record) => record.hostMinted !== true); // A ledger is built once per loaded session, so no turn can be live for a `running` record it // reads: that turn belonged to a process or load that is gone. Seal it before queued input is // restored, or the receipt reads `running` forever. for (const record of [...this.records.values()]) { if (record.state === "running") this.save({ ...record, state: "completed", outcome: EFFECT_OUTCOMES.inputInterruptedByReopen }); } // Agent listeners are awaited before the next provider call; a crash cannot turn a // consumed queue record back into a replayable queued admission. this.unsubscribeAgent = session.agent.subscribe((event) => { if (this.durableSessionId !== session.sessionManager.getSessionId()) return; if (event.type !== "message_start" || event.message.role !== "user") return; const identity = readClientMessageIdentity(event.message); const record = identity.clientMessageId ? this.records.get(identity.clientMessageId) : undefined; if (record?.state === "queued") this.save({ ...record, state: "running", prepared: undefined }); }); this.unsubscribeSession = session.subscribe((event) => { if (this.restoring || this.durableSessionId !== session.sessionManager.getSessionId()) return; if (event.type === "agent_start") { // A new turn: an abort noted before it belongs to nothing this turn will report. this.lastAbort = undefined; } else if (event.type === "agent_end") { if (!event.willRetry) this.turnOutcome = this.endOutcome(event.messages); } else if (event.type === "agent_idle") { const outcome = this.turnOutcome ?? { kind: "completed" }; this.turnOutcome = undefined; this.turnAbortedBy = undefined; this.lastAbort = undefined; for (const record of this.records.values()) { if (record.state === "running") this.save({ ...record, state: "completed", outcome }); } } else if (event.type === "queue_update") { const queuedIds = new Set(event.ordered.map((input) => input.clientMessageId)); const consumingId = readClientMessageIdentity(session.agent.state.streamingMessage).clientMessageId; for (const record of this.records.values()) { if (record.state === "queued" && !queuedIds.has(record.clientMessageId)) { this.save({ ...record, state: consumingId === record.clientMessageId ? "running" : "completed", prepared: undefined, }); } } } }); // Publish this ledger in the WeakMap before restoration emits queue events. this.ready = Promise.resolve().then(() => this.restore()); } /** Mark queued input cleared on a session whose ledger exists; never builds one (that would restore queued input). */ static clearQueuedFor(session) { sessions.get(session)?.clearQueued(); } /** * Whenever `runtime` replaces its session, give the outgoing session's still-queued inputs a * `dropped_by_replace` receipt. Only a ledger that already exists is touched: building one now would * restore (replay) persisted queued input into a session that is being torn down. */ static sealQueuedOnReplace(runtime) { return runtime.onBeforeSessionReplace(() => sessions.get(runtime.session)?.dropQueuedByReplace()); } static forSession(session) { let admissions = sessions.get(session); if (admissions && admissions.durableSessionId !== session.sessionManager.getSessionId()) { admissions.unsubscribeAgent(); admissions.unsubscribeSession(); admissions = undefined; } if (!admissions) { admissions = new ClientAdmissions(session); sessions.set(session, admissions); } return admissions; } /** The newest record for `clientMessageId` in this session, for effect status queries. */ record(clientMessageId) { return this.records.get(clientMessageId); } /** * An edit of a still-queued input: its stored prepared content takes the new text (images kept), so a * reopen restores what the client edited, not what it first sent. The fingerprint stays the original * send's, so a retry of that send still replays. */ editQueuedText(clientMessageId, text) { const record = this.records.get(clientMessageId); if (record?.state !== "queued") return; this.save({ ...record, prepared: { ...record.prepared, text } }); } /** Remember who aborted the running turn; the turn's receipts name them once it ends. */ /** Record who aborted; the returned record is the token `forgetAbort` takes. */ noteAbort(by, effectId) { if (this.session.isStreaming) this.turnAbortedBy = by; const abort = { connectionId: by.connectionId, ...(effectId === undefined ? {} : { effectId }) }; this.lastAbort = abort; return abort; } /** The client (and abort effect) that aborted the current turn, for `agent_end`/`session_abort.abortedBy`. */ get abortedBy() { return this.lastAbort; } /** * A client abort that reached no turn has settled: it must not name a later, unrelated abort. * Only that abort is forgotten; a newer one recorded meanwhile stays. */ forgetAbort(abort) { if (this.lastAbort === abort) this.lastAbort = undefined; } /** * The queues are about to be drained (`clear_queue`, `abort` or `unload_session` with `clearQueue`, a session * release is not one: its queued deliveries stay unwritten so the sender redelivers them): give every still-queued input an honest `cleared` receipt first, so the drain's `queue_update` cannot * read it as delivered (senpi#3412). */ clearQueued() { if (this.durableSessionId !== this.session.sessionManager.getSessionId()) return; for (const record of [...this.records.values()]) { if (record.state !== "queued") continue; this.save({ ...record, state: "completed", prepared: undefined, outcome: EFFECT_OUTCOMES.inputCleared, }); } } /** * The session is being replaced (`new_session`, `switch_session`, `fork`) and its queue is about to be * discarded with it: give every still-queued input an honest terminal receipt in its own transcript, so * a reopen of that file neither restores it nor reports it as delivered. */ dropQueuedByReplace() { if (this.durableSessionId !== this.session.sessionManager.getSessionId()) return; for (const record of [...this.records.values()]) { if (record.state !== "queued") continue; this.save({ ...record, state: "completed", prepared: undefined, outcome: EFFECT_OUTCOMES.inputDroppedByReplace, }); } } endOutcome(messages) { const abortSource = this.session.currentAbortSource; if (abortSource !== undefined) { return { kind: "aborted", reason: "aborted", detail: "the turn was aborted before it finished", abortSource, ...(this.turnAbortedBy === undefined ? {} : { abortedBy: this.turnAbortedBy }), }; } const last = messages.at(-1); if (last?.role === "assistant" && last.stopReason === "error") return turnErrorOutcome(last.errorMessage); return { kind: "completed" }; } async restore() { try { const queued = [...this.records.values()] .filter((record) => record.state === "queued") .sort((left, right) => left.prepared.enqueueOrder - right.prepared.enqueueOrder); for (const record of queued) { await this.session.restoreQueuedInput(record.prepared); } } finally { this.restoring = false; } } save(record) { this.session.sessionManager.appendCustomEntry(CLIENT_ADMISSION_ENTRY, record); this.records.set(record.clientMessageId, record); noteSessionEffect(this.session, projectAdmission(record)); } async admit(command, invoke) { const ledger = this; return admitClientInput.call({ ready: this.ready, get hasIdentities() { return ledger.hasIdentities; }, set hasIdentities(value) { ledger.hasIdentities = value; }, session: this.session, records: this.records, pending: this.pending, save: (record) => this.save(record), }, command, invoke); } } //# sourceMappingURL=client-admissions.js.map