@code-yeongyu/senpi
Version:
Coding agent CLI with read, bash, edit, write tools and session management
73 lines • 3.15 kB
JavaScript
import { recoveryCarrier } from "../../core/session-recovery-identity.js";
import { protocolClone, protocolDepthLimitOf } from "./compact-protocol-depth.js";
const CONTROLS = new Set(["message_block_state", "message_block_metadata", "message_tool_arguments_reset"]);
const carried = new WeakMap();
const carriedRefusals = new WeakSet();
/**
* Joins the two projections of one session event into one acceptance unit (RULING-26 Q2). The structured lane emits
* before the legacy listeners, so an event's canonical record waits here, keyed by the per-event recovery carrier both
* projections share, until its legacy record arrives; a block control has no legacy record and is a unit by itself.
* Canonical records take JSON wire semantics at this boundary: `undefined` properties are dropped (#4425 LOW 4), and a
* record deeper than the protocol depth limit is refused typed before its clone and never pairs (RULING-59 C). The
* caller reports that refusal; `refused` then tells the event's legacy record, so the event is counted once.
*/
export class CanonicalPairing {
constructor() {
this.pending = new WeakMap();
this.refusals = new WeakSet();
}
structured(record) {
const carrier = recoveryCarrier(record);
if (!carrier)
return undefined;
let canonical;
try {
canonical = protocolClone(record);
}
catch (cause) {
if (protocolDepthLimitOf(cause) !== undefined)
this.refusals.add(carrier);
throw cause;
}
if (CONTROLS.has(canonical.type))
return { record: canonical, carrier };
this.pending.set(carrier, canonical);
return undefined;
}
take(legacy) {
const carrier = recoveryCarrier(legacy);
const canonical = carrier && this.pending.get(carrier);
if (carrier)
this.pending.delete(carrier);
return canonical;
}
/** Whether the canonical record of `legacy`'s event was refused at the protocol depth limit (and reported). */
refused(legacy) {
const carrier = recoveryCarrier(legacy);
return carrier !== undefined && this.refusals.delete(carrier);
}
}
/**
* A worker binding hands the paired canonical record, or the fact that the pairing refused it at the depth limit, to
* its frame writer beside the unchanged legacy record.
*/
export function carryCanonical(legacy, canonical, refused) {
if (canonical)
carried.set(legacy, canonical);
if (refused)
carriedRefusals.add(legacy);
}
export function carriedCanonical(legacy) {
return carried.get(legacy);
}
export function carriedRefusal(legacy) {
return carriedRefusals.has(legacy);
}
/** The canonical unit a worker frame carries, when it carries one. */
export function frameUnit(frame) {
const { ordinal, firstOrdinal, recovery, canonical } = frame;
return canonical && recovery && ordinal !== undefined && firstOrdinal !== undefined
? { record: canonical, carrier: recovery, ordinal, firstOrdinal }
: undefined;
}
//# sourceMappingURL=compact-canonical-pairing.js.map