@code-yeongyu/senpi
Version:
Coding agent CLI with read, bash, edit, write tools and session management
207 lines • 8.86 kB
JavaScript
import { CALL_RECORDS } from "./compact-call-model.js";
import { COMPONENT_RECORDS } from "./compact-current-components.js";
import { laneTransition } from "./compact-lane-transitions.js";
import { assertProtocolDepth, protocolClone, protocolDepthLimitOf } from "./compact-protocol-depth.js";
import { CompactReadError } from "./compact-read-lease.js";
import { CompactSessionState } from "./compact-session-state.js";
import { compactColdFlow } from "./compact-transcript-flows.js";
/**
* The lane's compact current state for one epoch (S03b-3): folded inside the lane's own acceptance step at the
* lane's sequence, accepting only that epoch's registry nonce (RULING-39). A rejected fold makes it unavailable for the
* rest of the epoch (legacy delivery is unaffected), so a cut never holds a partial or silently stale state.
*/
export class CompactLaneCurrent {
constructor() {
this.cleanup = Promise.resolve();
this.coldChain = Promise.resolve();
this.owners = new WeakMap();
this.refused = 0;
}
/**
* Records refused at the protocol depth limit over this lane's lifetime, one per session event, whether or not the
* epoch's compact state is still live: a real loss ends the state, never the count. The legacy path (`admits`) and
* the canonical path (`fail`) follow this one rule (RULING-59 C).
*/
get refusedRecords() {
return this.refused;
}
/** Open broadcast questions now; undefined once this epoch's compact state is unavailable. */
get openBroadcastDialogs() {
return this.state?.openBroadcastDialogs;
}
bootstrap(options) {
this.retire();
this.state = new CompactSessionState(options);
}
/**
* The one protocol depth check of a legacy record a compact fold would clone or serialize (the call and component
* folds, the settled terminal of a `message_end`), run before any of them (RULING-59 C). A refused record is counted
* and reported here, once, under the `refusedRecords` rule (live or retired state alike); a cycle ends the current
* state. Either way the record must change no compact component.
*/
admits(record, type) {
if (type !== "message_end" && !CALL_RECORDS.has(type) && !COMPONENT_RECORDS.has(type))
return true;
try {
assertProtocolDepth(record);
return true;
}
catch (cause) {
const limit = protocolDepthLimitOf(cause);
if (limit === undefined)
this.fail(cause);
else
this.refuse(limit);
return false;
}
}
fold(unit, sequence, turnId = null) {
this.apply(() => laneTransition(unit), sequence, turnId);
}
/** A host-origin lifecycle, run-state or dialog record, taken at the same JSON boundary as canonical records. */
foldComponent(record, sequence) {
this.apply(() => ({ type: "component", record: protocolClone(record) }), sequence);
}
/** An execution, hook or boundary record for the calls component (S06); undefined once the state is unavailable. */
foldCall(record, sequence, turnId, callKey) {
if (!this.state)
return undefined;
try {
return this.state.acceptCall(protocolClone(record), sequence, turnId, callKey);
}
catch (cause) {
this.fail(cause);
return undefined;
}
}
/** The call whose block a `toolcall_*` update streams (RULING-123); undefined once the state is unavailable. */
callOwning(messageId, contentIndex) {
return this.state?.callOwning(messageId, contentIndex);
}
/**
* The cold work of one accepted transcript transition: an owned append makes its message appended, and the flow a
* real producer committed (edit, context-edit removal, compaction, branch) maps through this epoch's settled joins.
*/
transcript(transition, settled, epoch) {
if (transition.kind === "append" && transition.owningMessageId && transition.recoveryEpoch === epoch)
this.status(transition.owningMessageId, "appended");
const flow = transition.kind === "append" || transition.kind === "leaf" ? transition.flow : undefined;
if (flow)
this.cold(async (state) => {
const cold = await compactColdFlow(flow, (entryIds) => settled.messagesOf(entryIds));
return cold && state.applyColdFlow(cold);
});
}
/** Persistence from the transcript join: a message this state never held, or one still live, keeps its status. */
status(messageId, persistence) {
this.cold((state) => state.applyCold({ type: "message_status", messageId, persistence }));
}
/**
* Cold work ordered after acceptance (RULING-33 cond. 2): steps run one at a time in acceptance order, each against
* the epoch it was accepted in; an outcome the store failed to write stops that epoch's compact state.
*/
cold(step) {
const state = this.state;
if (!state)
return;
this.coldChain = this.coldChain.then(async () => {
try {
const failed = [(await step(state)) ?? []].flat().find((outcome) => outcome.kind === "failed");
if (failed && this.state === state)
this.fail(failed.cause);
}
catch (cause) {
if (this.state === state)
this.fail(cause);
}
});
}
/** Never throws: building the transition from a worker frame's record is part of the fold that may fail. */
apply(build, sequence, turnId = null) {
if (!this.state)
return;
try {
const transition = build();
if (transition)
this.state.accept(transition, sequence, turnId);
}
catch (cause) {
this.fail(cause);
}
}
freeze() {
const state = this.state;
if (!state)
return undefined;
try {
const cut = state.freeze();
this.owners.set(cut, state);
return cut;
}
catch (cause) {
this.fail(cause);
return undefined;
}
}
release(cut) {
if (cut)
this.owners.get(cut)?.release(cut);
}
read(cut) {
return this.owned(cut, (owner, held) => owner.read(held));
}
readComponents(cut) {
return this.owned(cut, (owner, held) => owner.readComponents(held));
}
readActive(cut) {
return this.owned(cut, (owner, held) => owner.readActive(held));
}
readCalls(cut) {
return this.owned(cut, (owner, held) => owner.readCalls(held));
}
cutStorage(cut, use) {
return this.owned(cut, (owner, held) => owner.cutStorage(held, use));
}
owned(cut, read) {
const owner = cut && this.owners.get(cut);
if (!cut)
return Promise.reject(new CompactReadError("unavailable"));
if (!owner)
return Promise.reject(new CompactReadError("ownership"));
return read(owner, cut);
}
retire() {
const state = this.state;
this.state = undefined;
if (!state)
return;
this.cleanup = Promise.all([this.cleanup, state.retire()]).then(() => { });
// idle() still rejects for whoever joins this cleanup; nobody may join, so the failure is reported here (senpi#6068).
void this.cleanup.catch((cause) => process.stderr.write(`senpi rpc acceptance: compact current state retire failed: ${reasonOf(cause)}\n`));
}
idle() {
return Promise.all([this.cleanup, this.coldChain, this.state?.idle()]).then(() => { });
}
/**
* The epoch's compact component stops here: a fold failed, or a canonical record never reached this lane. A record
* refused at the protocol depth limit is the exception (RULING-59 C): it was refused before anything was cloned or
* folded, so it is counted and reported, also once the state is already gone, and a live state stays available.
*/
fail(cause) {
const limit = protocolDepthLimitOf(cause);
if (limit !== undefined)
return this.refuse(limit);
if (!this.state)
return;
process.stderr.write(`senpi rpc acceptance: compact current state unavailable: ${reasonOf(cause)}\n`);
this.retire();
}
refuse(limit) {
this.refused += 1;
process.stderr.write(`senpi rpc acceptance: compact record refused: unsupported_value (depth above ${limit})\n`);
}
}
function reasonOf(cause) {
return cause instanceof Error && "reason" in cause ? String(cause.reason) : String(cause);
}
//# sourceMappingURL=compact-lane-current.js.map