UNPKG

@code-yeongyu/senpi

Version:

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

71 lines • 3.39 kB
import { FRAGMENT_MAX_BYTES } from "./compact-recovery-bytes.js"; import { WorkerPermitGate } from "./session-worker-permits.js"; import { SESSION_WORKER_LIMITS } from "./session-worker-protocol.js"; /** Code units one fragment carries: D stored bytes (R4.B3), two per UTF-16 code unit, as S12a's staging stores them. */ const FRAGMENT_UNITS = FRAGMENT_MAX_BYTES / 2; const withoutSignals = (_key, value) => value instanceof SharedArrayBuffer ? undefined : value; /** * The worker half of the payload transport (S12b; RULING-63 Q3, RULING-64). Every bootstrap, output, snapshot and * canonical message is built only after this isolate holds a host permit, and one permit pays for one physical frame. * A message whose JSON fits one fragment is posted as it is; a larger one travels as fragments of at most D stored bytes * of that JSON, each paid for by its own permit. In the ordered stream every fragment takes the next physical ordinal * and the final fragment is the commit frame: it carries the message's own ordinal, binding and signals. */ export class WorkerTransfer { constructor(post, block, stamp, fail) { /** A permit taken for a message whose builder threw: only the host adds credit, so the next message spends it. */ this.spare = false; this.post = post; this.gate = new WorkerPermitGate(block, SESSION_WORKER_LIMITS.controlMs); this.stamp = stamp; this.fail = fail; } send(build) { this.take(); let message; try { message = build(); } catch (cause) { this.spare = true; throw cause; } const text = JSON.stringify(message, withoutSignals); if (text.length <= FRAGMENT_UNITS) { this.post(message); return; } const count = Math.ceil(text.length / FRAGMENT_UNITS); const commit = "ordinal" in message && message.ordinal !== undefined ? this.stamp.defer(count - 1) : undefined; const epochSignal = "bootstrap" in message ? message.bootstrap?.epochSignal : undefined; for (let index = 0; index < count; index += 1) { if (index > 0) this.take(); const last = index === count - 1; this.post({ type: "fragment", of: message.type, length: text.length * 2, offset: index * FRAGMENT_UNITS * 2, text: text.slice(index * FRAGMENT_UNITS, (index + 1) * FRAGMENT_UNITS), ...(commit ? { ordinal: commit.ordinal - (count - 1 - index) } : {}), ...(last && commit?.firstOrdinal !== undefined ? { firstOrdinal: commit.firstOrdinal } : {}), ...(last && "signal" in message ? { signal: message.signal } : {}), ...(last && epochSignal ? { epochSignal } : {}), }); } } /** A closed host ends the isolate cleanly; a deny that never cleared, or no answer, is a typed worker failure. */ take() { if (this.spare) { this.spare = false; return; } const outcome = this.gate.acquire(); if (outcome === "closed") process.exit(0); if (outcome !== "granted") this.fail(`session_worker_permit_${outcome}`); } } //# sourceMappingURL=session-worker-transfer.js.map