@code-yeongyu/senpi
Version:
Coding agent CLI with read, bash, edit, write tools and session management
287 lines • 13.6 kB
JavaScript
import { LifecycleAttention } from "./lifecycle-attention.js";
import { readLifecycleRecord } from "./lifecycle-record.js";
import { lifecycleTerminal } from "./session-lifecycle-reducer.js";
/** Owns epoch/revision/decision ordering, separate from fact collection. */
export class LifecycleLedger {
constructor(persistence) {
this.sealed = false;
this.admissionWriteFailed = false;
this.unwritten = false;
this.writeFailed = false;
this.activityInstanceId = crypto.randomUUID();
this.runActive = false;
this.interrupted = false;
this.persistence = persistence;
let restored;
for (const entry of persistence.entries) {
if (entry.type !== "custom" || entry.customType !== "session-lifecycle")
continue;
const parsed = readLifecycleRecord(entry.data);
if (parsed?.sessionId === persistence.sessionId && (!restored || parsed.seq > restored.seq))
restored = parsed;
}
const spawnedBy = persistence.spawnedBy;
this.current = restored
? wireSnapshot(restored)
: {
state: "done",
seq: 0,
epoch: 0,
epochOrigin: "automatic",
humanInputRevision: 0,
selfSettleEligible: true,
restoring: true,
persisted: false,
runPaused: false,
since: new Date().toISOString(),
role: spawnedBy ? "child" : "root",
rootSessionId: spawnedBy?.rootSessionId ?? persistence.sessionId,
...(spawnedBy ? { parentSessionId: spawnedBy.parentSessionId, taskId: spawnedBy.taskId } : {}),
};
this.current = { ...this.current, activityRevision: { instanceId: this.activityInstanceId, revision: 0 } };
this.attention = new LifecycleAttention(persistence.sessionId, restored);
this.sealed = restored?.epochSealed ?? false;
this.runId = restored?.runId;
// A run recorded active is interrupted only when the transcript holds no end of it (senpi#7016).
this.interrupted =
restored?.interrupted === true ||
(restored?.runActive === true && !this.sealed && restored.runId !== persistence.endedRunId);
this.goalCompleted = restored?.completedGoalId;
this.runCompleted = restored?.completedRunId;
this.lastTurnEnd = restored?.lastTurnEnd;
// An unsealed restored intent cannot establish whether an unsaved human
// input overtook it. Invalidate it before releasing the restore barrier.
if (restored?.settleRequest && !this.sealed)
this.current = { ...this.current, selfSettleEligible: false };
}
get snapshot() {
return this.current;
}
custodyWriteFailed() {
this.current = { ...this.current, persisted: false, selfSettleEligible: false };
}
releaseRestoreBarrier() {
this.current = { ...this.current, restoring: false };
}
humanInput(at) {
this.request = undefined;
const { settleDecision: _decision, ...current } = this.current;
// The first revision is input preparation, not an epoch-zero completion.
const preparingFirstTurn = current.epoch === 0 && lifecycleTerminal(current.state);
const next = {
...current,
state: preparingFirstTurn ? "running" : current.state,
humanInputRevision: current.humanInputRevision + 1,
since: preparingFirstTurn ? at : current.since,
};
// Input that opens a new epoch is recorded by that epoch's opening record (senpi#3980). Input into an
// open epoch is written at once: it must supersede a standing settle request durably.
if (!this.runActive && (this.sealed || current.epoch === 0)) {
this.current = next;
this.unwritten = true;
}
else if (!this.write(next, true))
this.admissionWriteFailed = true;
}
startRun(origin) {
this.lastTurnEnd = undefined;
this.openEpoch(origin);
// The previous run's stop never describes this one (senpi#3864).
const { lastStop: _lastStop, ...current } = this.current;
this.current = current;
this.runId = crypto.randomUUID();
this.current = { ...this.current, runId: this.runId };
this.runActive = true;
this.admissionWriteFailed = false;
// A new run is unsaved state even inside an open epoch: recompute() treats records that differ only in runActive
// or runId as equal, so without this a later run in the same epoch would never be recorded (senpi#3980).
this.unwritten = true;
}
openEpoch(origin) {
if (!this.sealed && this.current.epoch > 0)
return;
this.sealed = false;
this.request = undefined;
this.goalCompleted = undefined;
this.runCompleted = undefined;
const { completionId: _id, completion: _completion, failure: _failure, settleDecision: _decision, ...current } = this.current;
this.current = {
...current,
epoch: current.epoch + 1,
epochOrigin: origin,
selfSettleEligible: !this.admissionWriteFailed,
};
this.unwritten = true;
}
markGoalCompleted(goalId) {
if (this.current.epoch === 0 || this.sealed || this.goalCompleted === goalId)
return;
this.goalCompleted = goalId;
this.checkpoint();
}
markRunCompleted(runId) {
if (this.current.epoch === 0 || this.sealed || this.runCompleted === runId)
return;
this.runCompleted = runId;
this.checkpoint();
}
requestSelfSettle() {
const base = { epoch: this.current.epoch, humanInputRevision: this.current.humanInputRevision };
if ((this.current.holds?.length ?? 0) > 0)
return { ...base, recorded: false, reason: "held" };
if (this.current.epoch === 0 || this.sealed)
return { ...base, recorded: false, reason: "no_epoch" };
if (!this.current.selfSettleEligible || this.current.role !== "root")
return { ...base, recorded: false, reason: "not_eligible" };
this.request = { epoch: this.current.epoch, revision: this.current.humanInputRevision };
if (this.write(this.current, false))
return { ...base, recorded: true };
this.request = undefined;
this.current = { ...this.current, selfSettleEligible: false };
return { ...base, recorded: false, reason: "not_eligible" };
}
update(input) {
if (input.holds.length > 0 || input.state === "failed")
this.request = undefined;
const terminal = input.state === "done" || input.state === "failed";
const { completionId: _id, completion: _completion, failure: _failure, settleDecision: _decision, lastStop: _lastStop, activity: _activity, ...base } = this.current;
const now = new Date().toISOString();
const standingRequest = this.request?.epoch === base.epoch &&
this.request.revision === base.humanInputRevision &&
base.selfSettleEligible &&
input.holds.length === 0;
const completion = input.state === "done" && base.epoch > 0
? this.goalCompleted
? { kind: "goal", at: now, goalId: this.goalCompleted }
: this.runCompleted
? { kind: "run", at: now, runId: this.runCompleted }
: { kind: standingRequest ? "agent" : "reply", at: now }
: undefined;
const next = {
...base,
state: input.state,
waitingOn: input.waits,
holds: input.holds,
runPaused: input.holds.some((hold) => hold.kind === "run_fence"),
since: input.state === this.current.state ? this.current.since : now,
...(terminal && base.epoch > 0 ? { completionId: `${this.persistence.sessionId}:${base.epoch}` } : {}),
...(completion
? {
completion: this.current.state === "done" && this.sealed
? (this.current.completion ?? completion)
: completion,
}
: {}),
...(input.state === "failed" && input.failure ? { failure: input.failure } : {}),
...(input.lastStop ? { lastStop: input.lastStop } : {}),
...(input.activity ? { activity: input.activity } : {}),
...(input.state === "done" && standingRequest
? {
settleDecision: this.current.settleDecision ?? { decisionId: crypto.randomUUID() },
}
: {}),
...(input.state === "done" && this.sealed && this.current.settleDecision
? { settleDecision: this.current.settleDecision }
: {}),
};
if (terminal && base.epoch > 0)
this.sealed = true;
if (this.current.restoring) {
// Input changes during session_start are collected, but cannot publish.
return;
}
const sameActivity = JSON.stringify(this.current.activity) === JSON.stringify(next.activity);
// An unchanged lifecycle writes nothing; an unwritten revision waits for the record that carries it (senpi#3980).
if (this.current.persisted && sameActivity && equivalent(this.current, next))
return;
// Activity alone only emits, also while custody writes fail. Pending run/epoch state and a failed
// lifecycle write retain the ordinary durable path: activity cannot consume or clear that work.
if (!sameActivity && !this.unwritten && !this.writeFailed && equivalent(this.current, next)) {
this.current = {
...next,
activityRevision: {
instanceId: this.activityInstanceId,
revision: (this.current.activityRevision?.revision ?? 0) + 1,
},
};
this.persistence.emit({
type: "session_lifecycle",
sessionId: this.persistence.sessionId,
lifecycle: this.current,
});
return;
}
// A brand-new idle session has no completed work epoch to announce.
if (terminal && base.epoch === 0) {
this.current = next;
return;
}
this.write(next, true);
}
checkpoint() {
this.write(this.current, false);
}
/** Publishes a revision or epoch that no record carries yet, once. */
publishUnwritten() {
if (this.unwritten)
this.write(this.current, true);
}
write(snapshot, emit) {
const next = {
...snapshot,
seq: this.current.seq + 1,
persisted: true,
activityRevision: { instanceId: this.activityInstanceId, revision: 0 },
};
const { activity: _activity, activityRevision: _activityRevision, ...durable } = next;
const record = {
...durable,
sessionId: this.persistence.sessionId,
attentionSeq: this.attention.attentionSeq,
synthetic: this.attention.synthetic,
epochSealed: this.sealed,
runActive: this.runActive,
interrupted: this.interrupted,
...(this.lastTurnEnd ? { lastTurnEnd: this.lastTurnEnd } : {}),
...(this.goalCompleted ? { completedGoalId: this.goalCompleted } : {}),
...(this.runCompleted ? { completedRunId: this.runCompleted } : {}),
...(this.runId ? { runId: this.runId } : {}),
...(this.request ? { settleRequest: this.request } : {}),
};
this.unwritten = false;
try {
this.persistence.append(record);
this.current = next;
this.writeFailed = false;
}
catch (error) {
if (!(error instanceof Error))
throw error;
this.current = { ...next, persisted: false, selfSettleEligible: false };
this.writeFailed = true;
}
if (emit && !this.current.restoring)
this.persistence.emit({
type: "session_lifecycle",
sessionId: this.persistence.sessionId,
lifecycle: this.current,
});
return this.current.persisted;
}
}
function equivalent(previous, next) {
return (previous.state === next.state &&
previous.epoch === next.epoch &&
previous.humanInputRevision === next.humanInputRevision &&
previous.runPaused === next.runPaused &&
previous.selfSettleEligible === next.selfSettleEligible &&
JSON.stringify(previous.waitingOn) === JSON.stringify(next.waitingOn) &&
JSON.stringify(previous.holds) === JSON.stringify(next.holds) &&
JSON.stringify(previous.failure) === JSON.stringify(next.failure) &&
JSON.stringify(previous.lastStop) === JSON.stringify(next.lastStop));
}
function wireSnapshot(record) {
const { sessionId: _sessionId, interrupted: _interrupted, completedGoalId: _goal, completedRunId: _completedRun, lastTurnEnd: _lastTurnEnd, attentionSeq: _attentionSeq, synthetic: _synthetic, epochSealed: _sealed, settleRequest: _request, runActive: _runActive, ...snapshot } = record;
return snapshot;
}
//# sourceMappingURL=lifecycle-ledger.js.map