UNPKG

@code-yeongyu/senpi

Version:

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

266 lines • 13.4 kB
import { withReceipt } from "./command-authority.js"; import { normalizeJsonValue } from "./json-normal-value.js"; import { serializeJsonLine } from "./jsonl.js"; import { observeLifecycleDelivery } from "./lifecycle-delivery.js"; import { declarationReply } from "./compact-negotiation.js"; import { RecordLine, SessionEventWriterDestinations } from "./session-event-writer-destinations.js"; import { causeOfRecord } from "./session-status.js"; import { isCorrelatedReply } from "./socket-peer-recovery.js"; export { MAX_SHARED_STDIO_QUEUE_BYTES, MAX_SHARED_STDIO_QUEUE_RECORDS } from "./session-event-writer-lanes.js"; /** * Process-wide stdout scheduler for multi-session RPC mode. * * Each queue contains complete structured JSONL records for one routing handle. * Draining takes one record per queue in round-robin order and waits for stdout * backpressure before selecting the next one. A record is deliberately written * by itself: coalescing records from different sessions would obscure the * scheduling boundary and violate D9. */ export class SessionEventWriter extends SessionEventWriterDestinations { secretInputRoute(sessionId, sealed = false) { return this.secretPresentations.route(sessionId, sealed); } setSoleSecretCapabilities(capabilities) { this.secretPresentations.handshake(capabilities); } /** Queue a session record. Content events target connections attached to the session. */ enqueue(sessionId, value) { value = withReceipt(value, this.connectionContext.getStore()); if (declarationReply(value, undefined) === undefined) return true; if (this.sealedSessions.has(sessionId)) return false; const targetId = this.connectionContext.getStore(); const record = normalizeJsonValue(value).value; const isTargeted = this.isTargetedRecord(record); // A stale reply is consumed, not a session failure: the worker still gets its output credit. const current = isTargeted ? this.connectionContext.currentGuard() : undefined; if (current?.() === false) return true; const tagged = { ...observeLifecycleDelivery(record), sessionId }; const line = new RecordLine(tagged); // Observation records are per-subscription opt-ins on the subscriber's own actor FIFO: never attachments, // the attach replay, response broadcast, or the stdio lane, which cannot subscribe. if (record.type === "snapshot_marker" || record.type === "ui_request_pending" || record.type === "ui_request_settled") { const targets = record.type === "snapshot_marker" ? this.status.mirrorTargets(sessionId, "snapshot_marker").filter((id) => id === targetId) : this.status.dialogTargets(sessionId); for (const target of targets) this.fanout.get(target)?.actor.enqueue(line.text); if (targets.length > 0) this.requestFlush(); return true; } if (this.fanout.isEmpty() && this.exceedsStdioCapacity(line.text)) { this.closeSession(sessionId, { type: "response", command: "close_session", success: false, error: "session_output_overflow, resync required", }, "error"); return false; } this.openTurns.note(sessionId, record.type, record.reason); const attached = this.fanout.targets(sessionId, targetId, isTargeted, record.type); // Mirroring is an explicit per-subscription opt-in; it adds destinations, never an attachment. const mirrors = isTargeted ? [] : this.status.mirrorTargets(sessionId, String(record.type)); const targets = mirrors.length === 0 ? attached : [...new Set([...attached, ...mirrors])]; this.deliver(sessionId, record, tagged, line, targets, mirrors, isTargeted, current); // After the causing record is on every destination's FIFO, never before (ordering contract). const cause = isTargeted ? undefined : causeOfRecord(record); if (cause !== undefined) this.status.publish(sessionId, cause, this.changeActor("agent")); this.afterRecord?.(sessionId); this.requestFlush(); return true; } /** * One `loaded_surfaces_changed` per connection per inventory transition: sessions moving from the * same digest to the same new one share a single event, tagged with the first session that * reported it; a session converging from a different start still announces its own change. */ publishLoadedSurfacesChanged(sessionId, previous, digest) { if (this.fanout.isEmpty()) { this.enqueue(sessionId, { type: "loaded_surfaces_changed" }); return; } if (this.sealedSessions.has(sessionId)) return; const line = serializeJsonLine({ type: "loaded_surfaces_changed", sessionId }); for (const id of this.fanout.attachedConnections(sessionId)) { if (!this.surfaceDedupe.announce(id, sessionId, previous, digest)) continue; const held = this.fanout.attachmentOf(id, sessionId, false, "loaded_surfaces_changed"); this.fanout.get(id)?.actor.enqueue(line, undefined, undefined, undefined, false, held); } this.requestFlush(); } /** Queue one untagged host-control response for the given connection (defaults to the current one). */ enqueueControl(value, targetOverride) { const peer = targetOverride ?? this.currentConnection(); value = withReceipt(value, peer); const reply = declarationReply(value, peer === undefined ? undefined : this.compactSelection(peer)); if (reply === undefined) return Promise.resolve(); value = reply; if (this.failure !== undefined) return Promise.reject(this.failure); const targetId = targetOverride ?? this.connectionContext.getStore(); const registered = targetId === undefined ? undefined : this.fanout.get(targetId); if (registered) { registered.actor.enqueue(serializeJsonLine(value), undefined, undefined, undefined, isCorrelatedReply(value)); this.requestFlush(); return Promise.resolve(); } if (this.exceedsStdioCapacity(serializeJsonLine(value))) { if (!this.controlOverflowReported) { this.controlOverflowReported = true; this.records.append(this.controlQueue, { type: "overflow", error: "rpc_control_output_overflow, resync required", }); this.markReady(this.controlQueue); this.requestFlush(); } return Promise.reject(new Error("rpc_control_output_overflow, resync required")); } const queue = targetId === undefined ? this.controlQueue : this.connectionQueue(targetId); const completion = new Promise((resolve, reject) => { this.records.append(queue, { ...value }, undefined, resolve, reject); }); this.markReady(queue); this.requestFlush(); return completion; } /** * Queue one HOST-level lifecycle record for every registered connection, or the * shared stdio lane when none is registered. Unlike session records it is not tagged * with a routing handle by the writer: `host_stalled` carries the handle it blames, * and `host_memory_pressure` belongs to the process, not to a session. */ /** * Tell ONE opener where it sits in the open queue, before its open reaches the loop. * * Sent to the opening connection only: a queue position is about that caller's request, not * about the host. If the connection is gone the record is dropped rather than queued - a * position is worthless to a client that already left. */ sendOpenQueued(connection, record) { // Reconstruct so desktop `refresh-senpi-events.ts` sees a literal `type:` site in this file. const wire = { type: "queued", for_request: record.for_request, position: record.position, in_flight: record.in_flight, }; const target = this.fanout.get(connection); if (target === undefined) return; target.actor.enqueue(serializeJsonLine(wire)); this.requestFlush(); } broadcastHostRecord(record) { // Reconstruct so desktop `refresh-senpi-events.ts` sees literal `type:` sites in this file. let wire; switch (record.type) { case "host_superseded": wire = { type: "host_superseded", instanceId: record.instanceId, generation: record.generation, successor: record.successor, }; break; case "host_stalled": wire = { type: "host_stalled", driftMs: record.driftMs, ...(record.sessionId !== undefined ? { sessionId: record.sessionId } : {}), ...(record.tool !== undefined ? { tool: record.tool } : {}), }; break; case "host_memory_pressure": wire = { type: "host_memory_pressure", rssMb: record.rssMb, ...(record.footprintMb !== undefined ? { footprintMb: record.footprintMb } : {}), ...(record.measure !== undefined ? { measure: record.measure } : {}), sessions: record.sessions, ...(record.main !== undefined ? { main: record.main } : {}), ...(record.kernels !== undefined ? { kernels: record.kernels } : {}), }; break; case "host_trimmed": wire = { type: "host_trimmed", footprintBeforeMb: record.footprintBeforeMb, footprintAfterMb: record.footprintAfterMb, measure: record.measure, collected: record.collected, sessions: record.sessions, pauseMs: record.pauseMs, }; break; case "subscription_limits": wire = { type: "subscription_limits", provider: record.provider, account: record.account, windows: record.windows, reportedAt: record.reportedAt, ...(record.plan === undefined ? {} : { plan: record.plan }), ...(record.error === undefined ? {} : { error: record.error }), }; break; default: { const exhaustive = record; throw new Error(`unexpected host record ${exhaustive}`); } } if (this.fanout.isEmpty()) { this.records.append(this.controlQueue, wire); this.markReady(this.controlQueue); } else this.fanout.broadcast(serializeJsonLine(wire)); this.requestFlush(); } /** * Seal a session the host PARKED and publish `session_parked`. * * Parking is the idle sweep putting a RETAINED session back on disk: the routing * handle ends exactly as a close ends it, but the session survives and reopens with * `open_session { sessionPath }`, so the record a client dispatches on must say so * instead of claiming the session ended. Delivery follows the `session_closed` rule - * a worker session's record reaches only its attached connections, an interactive * session's reaches every connection. No client asked for this teardown, so there is * no close response to answer. */ parkSession(sessionId, sessionPath, reason) { this.sealLifecycle(sessionId, { type: "session_parked", sessionId, sessionPath, reason }); } /** * Seal a session `release_session` handed to a runtime outside this host and publish * `session_closed { reason: "released", sessionPath }`. Unlike a park the file must NOT be reopened * here - another process now writes it. Delivered like a park; the releasing caller's own answer is * the `release_session` response, so there is no close response either. */ releaseSession(sessionId, sessionPath) { this.sealLifecycle(sessionId, { type: "session_closed", sessionId, reason: "released", sessionPath }); } /** * Publishes the settles a seal would strand (session-open-turns.ts) while the session can still write; * false when the session is sealed already, or became sealed by a settle that overflowed the stdio lane. */ settleBeforeSeal(sessionId) { if (this.sealedSessions.has(sessionId)) return false; for (let owed = this.openTurns.take(sessionId); owed > 0; owed -= 1) { this.enqueue(sessionId, { type: "agent_settled", reason: "session_closed" }); } return !this.sealedSessions.has(sessionId); } } //# sourceMappingURL=session-event-writer.js.map