UNPKG

@code-yeongyu/senpi

Version:

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

92 lines • 4.19 kB
/** * What stands between a parsed command and the session router on a multi-session host: the * conditional idle handover's own request (`begin_handover`), the admission gate it closes on new * work, the per-session in-flight count the idle judgement reads, and the runtime identity * `get_protocol_info` reports. A host without hooks routes every command exactly as before. */ import { agentDirMismatch } from "./host-handoff-agent-dir.js"; import { BEGIN_HANDOVER_COMMAND, beginHandoverResponse, handoverRefusal, isHostIdle, parseBeginHandover, withHostIdentity, } from "./host-handover-wire.js"; import { HostIdleHandover } from "./host-idle-handover.js"; export class HostCoreGate { constructor(registry, route, hooks) { this.inFlight = new Map(); this.answering = new Set(); this.route = route; this.runtimeBuildId = hooks.runtimeBuildId; this.endpoint = hooks.endpoint; this.handover = hooks.handover === undefined ? undefined : new HostIdleHandover({ ...hooks.handover, isIdle: () => !(hooks.machineWork?.busy() ?? false) && isHostIdle(registry, this.inFlight), holdNewWork: () => hooks.machineWork?.hold(), releaseNewWork: () => hooks.machineWork?.release(), }); } /** Answers a `begin_handover` record through `deliver` and returns true, or returns false for anything else. */ async intercept(parsed, deliver) { if (!isRecord(parsed) || parsed.type !== BEGIN_HANDOVER_COMMAND) return false; const answering = this.answerBegin(parsed).then(deliver); this.answering.add(answering); try { await answering; } finally { this.answering.delete(answering); } return true; } async answerBegin(parsed) { if (this.handover === undefined) { return beginHandoverResponse(parsed.id, { kind: "refused", reason: "handover_unsupported" }); } const request = parseBeginHandover(parsed); if (typeof request === "string") return beginHandoverResponse(parsed.id, request); // Settled before `begin` so its claim on the operation stays free of awaits. const detail = request.agentDir === undefined || this.endpoint === undefined ? undefined : await agentDirMismatch(this.endpoint.socket, request.agentDir, this.endpoint.agentDir); return beginHandoverResponse(parsed.id, detail === undefined ? await this.handover.begin(request) : { kind: "refused", reason: "foreign_agent_dir", detail }); } /** * Settles once every `begin_handover` being answered has its answer. A host idle at once drains * the moment its successor takes the socket, and its shutdown waits here so the caller that asked * is told the outcome instead of reading a closed connection. */ async answered() { while (this.answering.size > 0) await Promise.allSettled([...this.answering]); } async dispatch(command) { const pending = this.handover?.view(); if (pending !== undefined && this.handover?.admits(command.type) === false) { return handoverRefusal(command, pending); } const key = "sessionId" in command ? command.sessionId : undefined; this.inFlight.set(key, (this.inFlight.get(key) ?? 0) + 1); let response; try { response = await this.route(command); } finally { const remaining = (this.inFlight.get(key) ?? 1) - 1; if (remaining > 0) this.inFlight.set(key, remaining); else this.inFlight.delete(key); this.handover?.observe(); } return response === undefined ? undefined : withHostIdentity(response, this.runtimeBuildId, this.handover?.view()); } } function isRecord(value) { return typeof value === "object" && value !== null && !Array.isArray(value); } //# sourceMappingURL=host-core-gate.js.map