@code-yeongyu/senpi
Version:
Coding agent CLI with read, bash, edit, write tools and session management
119 lines • 5.47 kB
JavaScript
import { FRAGMENT_MAX_BYTES } from "./compact-recovery-bytes.js";
import { protocolId } from "./compact-recovery-ids.js";
import { TransferStagingError, } from "./compact-transfer-staging.js";
import { HostPermits, hostWorkerPermits } from "./session-worker-permits.js";
import { acknowledge } from "./session-worker-signals.js";
/** Every frame a permit paid for: the payload messages, and the fragments larger ones travel in. */
const PAID = new Set([
"ready",
"snapshot",
"output",
"canonical",
"canonical_unavailable",
"fragment",
]);
const FRAGMENT_KIND = protocolId("worker_frame");
/**
* The host half of one worker incarnation's payload transport (S12b): its permits, its one physical ordinal sequence,
* and the staging of fragments into the lane's S12a store. A fragment is owned once staged, so its permit returns then;
* the message exists only once its final commit frame arrived and every staged leaf read back verified, and the staged
* copy is released exactly once either way. Fragment frames never reach acceptance, so they never move the high-water
* or a first-start binding (RULING-64); the final frame's ordinal is checked with the message it completes.
*/
export class WorkerIntake {
constructor(staging, fail, ledger = hostWorkerPermits) {
this.expected = 0;
this.serial = 0;
/** Messages this intake reassembled: their fragments already returned every permit they were paid with. */
this.assembled = new WeakSet();
this.staging = staging;
this.fail = fail;
this.permits = new HostPermits(ledger);
}
/** The physical ordinal this incarnation must send next: a typed diagnostic seam (carry 11). */
get nextOrdinal() {
return this.expected;
}
/** The staging refusal that ended this incarnation, if one did: never a silent worker loss. */
get refusal() {
return this.refused;
}
received(message) {
if (PAID.has(message.type) && !this.assembled.delete(message))
this.permits.returned();
}
/** A gap, duplicate or reordered physical frame is denied and ends this incarnation before any mutation. */
inOrder(message) {
if (message.ordinal === this.expected) {
this.expected += 1;
return true;
}
if (message.signal)
acknowledge(message.signal, false);
const kind = (message.ordinal ?? Number.POSITIVE_INFINITY) < this.expected ? "duplicate" : "gap";
this.fail(`session_worker_frame_${kind}`);
return false;
}
/** Stages one fragment; the final one resolves to the whole message, which then takes the ordinary receive path. */
fragment(frame) {
const end = frame.offset + frame.text.length * 2;
const final = end === frame.length;
if (frame.ordinal !== undefined && !final && !this.inOrder(frame))
return this.refuse(frame);
const fed = this.open?.fed ?? 0;
if (frame.offset !== fed)
return this.refuse(frame, `session_worker_frame_${frame.offset < fed ? "duplicate" : "gap"}`);
try {
this.open ??= {
transfer: this.staging.begin(FRAGMENT_KIND, protocolId(`t${++this.serial}`), frame.length),
fed,
};
this.open.transfer.append(frame.text);
this.open.fed = end;
}
catch (error) {
const cause = error instanceof TransferStagingError ? error.reason : "failed";
const ordinal = frame.ordinal === undefined
? undefined
: frame.ordinal + Math.ceil((frame.length - frame.offset) / FRAGMENT_MAX_BYTES) - 1;
this.refused = {
cause,
event: { type: frame.of, ...(ordinal === undefined ? {} : { ordinal }), length: frame.length },
};
const at = ordinal === undefined ? "" : ` ordinal ${ordinal}`;
return this.refuse(frame, `session_worker_transfer_${cause}: ${frame.of}${at} (${frame.length} stored bytes)`);
}
if (!final)
return undefined;
const content = this.open.transfer.commit();
this.open = undefined;
return this.whole(content, frame);
}
refuse(frame, error) {
this.open?.transfer.abort();
this.open = undefined;
if (error === undefined)
return undefined;
if (frame.signal)
acknowledge(frame.signal, false);
this.fail(error);
return undefined;
}
async whole(content, frame) {
try {
let text = "";
for await (const chunk of this.staging.chunks(content))
text += chunk;
const message = JSON.parse(text);
this.assembled.add(message);
return Object.assign(message, frame.ordinal === undefined ? {} : { ordinal: frame.ordinal }, frame.firstOrdinal === undefined ? {} : { firstOrdinal: frame.firstOrdinal }, frame.signal ? { signal: frame.signal } : {}, frame.epochSignal && "bootstrap" in message
? { bootstrap: { ...message.bootstrap, epochSignal: frame.epochSignal } }
: {});
}
finally {
// A store closed under this read already released the content: release it only while it is still staged.
this.staging.abandon(content);
}
}
}
//# sourceMappingURL=session-worker-intake.js.map