UNPKG

@code-yeongyu/senpi

Version:

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

86 lines • 3.53 kB
/** * The session tool barrier (senpi#3717). A client that captures a pre-turn workspace checkpoint arms * the turn with a barrier, sends the prompt at once, and releases the barrier when its capture is * done. Until then a tool that may change the workspace waits in preflight; read-only tools do not. * The ceiling (`deadlineMs`) bounds the wait when a release never arrives. */ export const MAX_TOOL_BARRIER_ID_LENGTH = 256; export const MAX_TOOL_BARRIER_DEADLINE_MS = 600_000; /** Why `value` is not a {@link ToolBarrierSpec}, or undefined when it is one. */ export function toolBarrierSpecError(value) { if (typeof value !== "object" || value === null || Array.isArray(value)) { return "toolBarrier must be an object { id, deadlineMs }."; } const { id, deadlineMs } = value; if (typeof id !== "string" || id.length === 0 || id.length > MAX_TOOL_BARRIER_ID_LENGTH) { return `toolBarrier.id must be a non-empty string of at most ${MAX_TOOL_BARRIER_ID_LENGTH} characters.`; } if (typeof deadlineMs !== "number" || !Number.isSafeInteger(deadlineMs) || deadlineMs < 1 || deadlineMs > MAX_TOOL_BARRIER_DEADLINE_MS) { return `toolBarrier.deadlineMs must be a whole number of milliseconds in 1..${MAX_TOOL_BARRIER_DEADLINE_MS}.`; } return undefined; } export class ToolBarrier { #armed; #onExpired; constructor(onExpired) { this.#onExpired = onExpired; } /** Arm, or re-arm under a new id and ceiling; calls already waiting keep waiting. */ arm(spec) { const current = this.#armed; if (current) clearTimeout(current.timer); const { promise, resolve } = Promise.withResolvers(); const gate = current ?? { opened: promise, open: resolve }; const timer = setTimeout(() => this.#expire(spec.id), spec.deadlineMs); timer.unref?.(); this.#armed = { spec: { id: spec.id, deadlineMs: spec.deadlineMs }, timer, opened: gate.opened, open: gate.open }; } /** Open the barrier armed under `id`. False when nothing with that id is armed (a no-op). */ release(id) { if (this.#armed?.spec.id !== id) return false; this.#open(); return true; } /** The armed turn ended: open without reporting anything. */ clear() { if (this.#armed) this.#open(); } /** The wait a write tool must take now, or undefined when nothing is armed. Settles early on abort. */ wait(signal) { const armed = this.#armed; if (!armed) return undefined; if (!signal) return { id: armed.spec.id, done: armed.opened }; if (signal.aborted) return { id: armed.spec.id, done: Promise.resolve() }; const aborted = Promise.withResolvers(); const onAbort = () => aborted.resolve(); signal.addEventListener("abort", onAbort, { once: true }); const done = Promise.race([armed.opened, aborted.promise]).finally(() => signal.removeEventListener("abort", onAbort)); return { id: armed.spec.id, done }; } #expire(id) { const armed = this.#armed; if (armed?.spec.id !== id) return; this.#open(); this.#onExpired(armed.spec); } #open() { const armed = this.#armed; if (!armed) return; this.#armed = undefined; clearTimeout(armed.timer); armed.open(); } } //# sourceMappingURL=tool-barrier.js.map