UNPKG

@code-yeongyu/senpi

Version:

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

139 lines • 7.42 kB
import { takeOver } from "./attach-takeover.js"; import { compactOpen } from "./compact-open-admission.js"; import { clearRetainedRecord } from "./session-router-residency.js"; import { hostInstanceId } from "./protocol-identity.js"; import { canonicalSessionPath } from "./session-path-key.js"; import { recordAttachment, releaseDisconnectAttachment, releaseOwnerAttachment } from "./session-router-attachments.js"; import { claimClose, finalizeClose } from "./session-router-close.js"; import { code, detail, error } from "./session-router-errors.js"; import { connectionLeft } from "./session-router-relocation.js"; function identityMatches(host, entry, durableSessionId, expectedHostInstance) { const durable = entry.runtime?.session.sessionId ?? entry.worker?.snapshot?.state.sessionId ?? entry.durableSessionId; return (typeof durableSessionId === "string" && durableSessionId.length > 0 && durableSessionId === durable && (expectedHostInstance === undefined || expectedHostInstance === (host.defaults.hostContext?.host_instance ?? hostInstanceId()))); } /** One test for every attachment command: a host that drains or is stopping owns every outstanding hold. */ function hostStopping(host) { return host.draining || host.drainExitRequested; } /** Wire contract v1.1: a draining or stopping host refuses attachment before admission with a v1 code. */ function drainingRefusal(host, command) { if (!hostStopping(host) || (command.type === "attach_session" && host.writer.currentConnection()?.startsWith("generation-route-") && command.expectedHostInstance !== undefined)) return undefined; return error(command.id, command.type, "session_busy_lifecycle", { reason: "host_draining" }); } /** Synchronous ownership admission shared by ordinary attach and atomic reclaim. */ function attach(host, owner, sessionId) { if (!host.registry.attachSession) throw new Error("live attachment is unavailable"); host.registry.attachSession(sessionId); recordAttachment(host, owner, sessionId); host.writer.status.publish(sessionId, "attach", "client"); } export function attachSession(host, command) { const draining = drainingRefusal(host, command); if (draining) return draining; const entry = host.registry.peek(command.sessionId); if (entry?.state !== "open" || !host.bindings.has(command.sessionId)) return error(command.id, command.type, "unknown_session", {}); if (!identityMatches(host, entry, command.durableSessionId, command.expectedHostInstance)) return error(command.id, command.type, "session_identity_mismatch", {}); const owner = host.writer.currentConnection(); if (owner === undefined) return error(command.id, command.type, "session_not_attached", { reason: "no_connection" }); if (command.takeover === true) return takeOver(host, command, owner, () => attach(host, owner, command.sessionId)); if (command.admissionId !== undefined && host.admissions) return compactOpen(host, command, async (accept, signal) => { signal.throwIfAborted(); attach(host, owner, command.sessionId); try { await accept({ sessionId: command.sessionId, durableSessionId: command.durableSessionId, attached: true }); } catch (cause) { releaseOwnerAttachment(host, owner, command.sessionId); await releaseDisconnectAttachment(host, command.sessionId); throw cause; } return undefined; }); try { attach(host, owner, command.sessionId); return { id: command.id, type: "response", command: command.type, success: true, data: { sessionId: command.sessionId, attached: true }, }; } catch (cause) { return error(command.id, command.type, code(cause), detail(cause) ?? {}); } } export async function releaseAttachment(host, command) { const owner = host.writer.currentConnection(); if (owner === undefined || !host.sessionsByConnection.get(owner)?.has(command.sessionId)) return error(command.id, command.type, "session_not_attached", { reason: "not_attached" }); releaseOwnerAttachment(host, owner, command.sessionId); // The drain owns every outstanding physical hold until its park; never preempt it. const deferred = !hostStopping(host) && (await releaseDisconnectAttachment(host, command.sessionId)); return { id: command.id, type: "response", command: command.type, success: true, data: { deferred } }; } export async function reclaimSession(host, command) { const draining = drainingRefusal(host, command); if (draining) return draining; if (typeof command.sessionPath !== "string" || command.sessionPath.length === 0) return error(command.id, command.type, "unknown_session", {}); const key = canonicalSessionPath(command.sessionPath); const row = host.registry .list() .find((candidate) => candidate.sessionPath !== undefined && canonicalSessionPath(candidate.sessionPath) === key); const entry = row && host.registry.peek(row.sessionId); if (!row || !entry) return error(command.id, command.type, "unknown_session", {}); if (entry.state !== "open" || !host.bindings.has(row.sessionId)) return error(command.id, command.type, "session_busy_lifecycle", {}); if (typeof command.expectedHostInstance !== "string" || !identityMatches(host, entry, command.durableSessionId, command.expectedHostInstance)) return error(command.id, command.type, "session_identity_mismatch", {}); const owner = host.writer.currentConnection(); if (owner === undefined) return error(command.id, command.type, "session_not_attached", { reason: "no_connection" }); const own = host.sessionsByConnection.get(owner)?.get(row.sessionId) ?? 0; const elsewhere = Math.max(0, entry.attachments - own); if (elsewhere > 0) return error(command.id, command.type, "attached_elsewhere", { attachments: elsewhere }); // No await between admission, the visible attach, and the claim of ALL caller holds. attach(host, owner, row.sessionId); const claim = claimClose(host, row.sessionId, { drainAttachments: true }); host.sessionsByConnection.get(owner)?.delete(row.sessionId); if (host.sessionsByConnection.get(owner)?.size === 0) host.sessionsByConnection.delete(owner); connectionLeft(host, row.sessionId, owner); if (host.handoverParkedDir !== undefined) await clearRetainedRecord(host, entry); if (claim.finalizer) { const binding = host.bindings.get(row.sessionId); binding?.cancelPendingExtensionUiRequests?.(); await finalizeClose(host, row.sessionId, binding, () => host.writer.sealLifecycle(row.sessionId, { type: "session_closed", sessionId: row.sessionId, reason: "client_close" }, true)); } host.writer.detachConnectionFromSession(owner, row.sessionId); host.writer.forgetSession(row.sessionId); return { id: command.id, type: "response", command: command.type, success: true, data: { reclaimed: true, sessionId: row.sessionId }, }; } //# sourceMappingURL=session-attachment-commands.js.map