UNPKG

@code-yeongyu/senpi

Version:

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

143 lines • 6 kB
/** * The server side of the reverse bridge (senpi#3566 todo 7). Each `host_call` from the child is admitted on receipt * against its job's capacity - the same one the child enforces - and run, queued FIFO, or refused with * `host_call_refused` naming its `callId`; every admitted call is answered exactly once with `host_reply`. A job that * ends signals its running calls and answers its queued calls without running them. */ import { HostCallAdmission } from "./admission.js"; import { packPayload, unpackPayload } from "./payload-wire.js"; const UNSCOPED = { exited: new Promise(() => { }), jobLive: () => true }; /** Calls `run` now, in this turn; a synchronous throw becomes a rejection like an async one. */ function settleNow(run) { try { return Promise.resolve(run()); } catch (error) { return Promise.reject(error); } } export class ServerHostCalls { #channel; #handler; #admissions = new Map(); #calls = new Map(); #contextOf; #refs; #scope; constructor(channel, handler, contextOf = () => undefined, refs, scope = UNSCOPED) { this.#refs = refs; this.#scope = scope; this.#channel = channel; this.#handler = handler; this.#contextOf = contextOf; } /** Calls running and queued for one job (0 for a job with none). */ load(jobId) { const admission = this.#admissions.get(jobId); return { active: admission?.active ?? 0, waiting: admission?.waiting ?? 0 }; } /** Takes a `host_call`; returns false for every other frame. */ handle(frame) { if (frame.type !== "host_call") return false; const decision = this.#admission(frame.jobId).submit(frame.callId); if (decision.state === "refused") { this.#forgetIdle(frame.jobId); this.#channel.send(decision.frame); return true; } this.#calls.set(frame.callId, { frame, abort: new AbortController() }); if (decision.state === "active") this.#run(frame.callId); return true; } /** The job ended: its running calls are signalled, its queued calls are answered without running. */ endJob(jobId) { for (const call of this.#calls.values()) if (call.frame.jobId === jobId) call.abort.abort(); } #admission(jobId) { let admission = this.#admissions.get(jobId); if (admission === undefined) { admission = new HostCallAdmission(); this.#admissions.set(jobId, admission); } return admission; } #forgetIdle(jobId) { const admission = this.#admissions.get(jobId); if (admission !== undefined && admission.active === 0 && admission.waiting === 0) this.#admissions.delete(jobId); } #run(callId) { const call = this.#calls.get(callId); if (call === undefined) return; const { frame } = call; const label = frame.name ?? frame.kind; const invoke = (args) => this.#handler({ callId, jobId: frame.jobId, kind: frame.kind, name: frame.name, args, signal: call.abort.signal, context: this.#contextOf(frame.jobId), generation: { exited: this.#scope.exited, jobLive: () => this.#scope.jobLive(frame.jobId) }, }); const answered = call.abort.signal.aborted ? this.#refuseArgs(frame).then(() => Promise.reject(new Error(`${label}: the job ended before this call ran`))) : frame.argsRef === undefined ? // Inline arguments start the handler in this turn, as admission promised; only a ref waits on its read. settleNow(() => invoke(frame.args)) : this.#args(frame, call.abort.signal).then(invoke); void answered.then((value) => void this.#reply(callId, value ?? null), (error) => this.#answer(callId, { type: "host_reply", callId, error: error instanceof Error ? error.message : String(error), })); } /** The call's arguments: inline, or a child-written ref accepted (acked, now owned here) and consumed. */ async #args(frame, signal) { if (frame.argsRef === undefined) return frame.args; if (this.#refs === undefined) throw new Error("host_call carried argsRef but this side reads no refs"); return unpackPayload(frame.argsRef, this.#refs.reader, signal); } /** A call whose job ended before it ran never opens its ref: the child's file is nacked and deleted by its writer. */ async #refuseArgs(frame) { if (frame.argsRef !== undefined) this.#refs?.reader.refuse(frame.argsRef, "cancelled"); } /** A reply above the inline threshold travels as a server-written ref the child acks and then owns. */ async #reply(callId, value) { try { const packed = this.#refs === undefined ? { inline: true, value } : await packPayload(value, this.#refs.writer); this.#answer(callId, packed.inline ? { type: "host_reply", callId, value: packed.value } : { type: "host_reply", callId, valueRef: packed.ref }); } catch (error) { this.#answer(callId, { type: "host_reply", callId, error: error instanceof Error ? error.message : String(error), }); } } #answer(callId, reply) { const call = this.#calls.get(callId); if (call === undefined) return; this.#calls.delete(callId); this.#channel.send(reply); const { jobId } = call.frame; const promoted = this.#admissions.get(jobId)?.settle(callId); this.#forgetIdle(jobId); if (promoted !== undefined) this.#run(promoted); } } //# sourceMappingURL=host-calls-server.js.map