@code-yeongyu/senpi
Version:
Coding agent CLI with read, bash, edit, write tools and session management
134 lines • 6.96 kB
JavaScript
import { getAssistantCapture, SeedFrontierError } from "@earendil-works/pi-ai";
import { compactUpdatesEnabled } from "../../core/engine-socket-flag.js";
import { carryRecovery } from "../../core/session-recovery-identity.js";
import { toJsonEvent } from "../json-event.js";
import { subscribeSessionRecords } from "./session-record-stream.js";
/**
* Internal structured capture and the acceptance lane it feeds follow the rollout flag alone (RULING-112): off, every
* session binds legacy-only, in process and in a worker alike; on, a session without the lane refuses the subscription.
*/
export function structuredLaneEnabled() {
return compactUpdatesEnabled();
}
const SEEDS = { blockSeeds: 1 };
/**
* Admits a block's records only after its own seed or insert reached this subscriber, so a subscriber that attached
* mid-message gets no seed, delta or control for that message, never a wrong one. A started block whose next record
* has no capture, or that has no accepted end when the message ends, is invalid: its later records are dropped and
* the assistant `message_end` names it in `invalidatedBlocks`, so a reader rebuilds it from the terminal message.
* A terminal that rewrote blocks from `rewrittenFrom` on invalidates every block at or above that index, and a
* `message_end` with no interpretable terminal capture (cancel, idle timeout) invalidates every block of the message.
* Blocks are addressed by the capture's own index (`capture.contentIndex ?? event.contentIndex`), never a drifted public one.
*/
class SeededBlockGate {
constructor() {
this.nextOffsets = new Map();
}
accept(record) {
if (record.type === "message_block_state" || record.type === "message_block_metadata")
return this.control(record, record.type === "message_block_state" ? record.phase : undefined);
if (record.type === "message_tool_arguments_reset") {
const accepted = this.control(record);
if (accepted)
this.nextOffsets.set(record.contentIndex, record.rawArgumentPrefix.length);
return accepted;
}
if (record.type === "message_start" && record.message.role === "assistant") {
const seeded = toJsonEvent(record, SEEDS);
this.blocks = seeded && new Map();
this.nextOffsets.clear();
return seeded;
}
if (record.type === "message_end" && record.message.role === "assistant")
return this.end(record);
if (record.type !== "message_update")
return toJsonEvent(record, SEEDS);
if (!this.blocks)
return undefined;
const update = record.assistantMessageEvent;
if (!("contentIndex" in update)) {
// A nested terminal carries its capture cutoff, so the reducer drops raw spelling at and above it.
const seeded = toJsonEvent(record, SEEDS);
const rewrittenFrom = getAssistantCapture(record)?.rewrittenFrom;
return seeded && rewrittenFrom !== undefined ? { ...seeded, rewrittenFrom } : seeded;
}
const index = getAssistantCapture(record)?.contentIndex ?? update.contentIndex;
const starts = update.type === "text_start" || update.type === "thinking_start" || update.type === "toolcall_start";
if (starts && this.nextOffsets.has(index)) {
this.blocks.set(index, "invalid");
return undefined;
}
if (this.blocks.get(index) === "invalid")
return undefined;
if (!starts && this.blocks.get(index) !== "open")
return undefined;
let seeded;
try {
seeded = toJsonEvent(record, SEEDS);
}
catch (error) {
if (!(error instanceof SeedFrontierError))
throw error;
this.blocks.set(index, "invalid");
return undefined;
}
const capture = getAssistantCapture(record);
if (starts && seeded && capture?.nextOffset !== undefined)
this.nextOffsets.set(index, capture.nextOffset);
if ("delta" in update && seeded) {
if (capture?.offset !== this.nextOffsets.get(index) || capture?.offset === undefined) {
this.blocks.set(index, "invalid");
return undefined;
}
this.nextOffsets.set(index, capture.offset + update.delta.length);
}
const ends = update.type === "text_end" || update.type === "thinking_end" || update.type === "toolcall_end";
this.blocks.set(index, seeded ? (ends ? "ended" : "open") : "invalid");
return seeded;
}
control(record, phase) {
const state = this.blocks?.get(record.contentIndex);
if (!this.blocks || state === "invalid")
return undefined;
if (phase === "insert")
this.blocks.set(record.contentIndex, "open");
else if (state === undefined || (phase === "end" && state !== "open"))
return undefined;
else if (phase === "end")
this.blocks.set(record.contentIndex, "ended");
return record;
}
end(record) {
const capture = getAssistantCapture(record);
const rewrittenFrom = capture?.message === undefined ? 0 : (capture.rewrittenFrom ?? Number.POSITIVE_INFINITY);
const invalidatedBlocks = [...(this.blocks ?? [])].flatMap(([index, state]) => state === "ended" && index < rewrittenFrom ? [] : [index]);
this.blocks = undefined;
this.nextOffsets.clear();
const seeded = toJsonEvent(record, SEEDS) ?? record;
return invalidatedBlocks.length === 0
? seeded
: { ...seeded, invalidatedBlocks: invalidatedBlocks.sort((a, b) => a - b) };
}
}
/**
* The session's internal structured lane, correlated like its legacy records: seeded `toJsonEvent` projections of
* every session event plus the three block controls, in session order. Never a legacy destination.
*/
export function subscribeStructuredSessionRecords(session, output, failed) {
const gate = new SeededBlockGate();
return subscribeSessionRecords(session, output, undefined, (listener) => session.subscribeStructured((record) => {
// The session emits this before its legacy listeners: a failure anywhere on this path costs only the
// structured record, reported to `failed`, never the legacy and extension delivery of the event.
try {
const accepted = gate.accept(record);
// Correlation reads only `type`, `role` and outer fields, which seeded records and controls keep.
// The projection keeps its event's recovery carrier, which pairs it with that event's legacy record.
if (accepted)
listener(carryRecovery(record, accepted));
}
catch (cause) {
failed(cause);
}
}, failed));
}
//# sourceMappingURL=structured-session-records.js.map