@code-yeongyu/senpi
Version:
Coding agent CLI with read, bash, edit, write tools and session management
457 lines • 20.2 kB
JavaScript
import { effectiveWakes } from "./extensions/builtin/goal/turn-end-liveness.js";
import { LifecycleChildren } from "./lifecycle-children.js";
import { LifecycleLedger } from "./lifecycle-ledger.js";
import { projectWaitItems } from "./lifecycle-wait-items.js";
import { sameStop, stopDetail } from "./lifecycle-stop.js";
import { lifecycleState, lifecycleTerminal } from "./session-lifecycle-reducer.js";
import { SessionWorkObligations } from "./work-obligations.js";
/** One event-driven reducer per session, shared by every client surface. */
export class SessionLifecycleController {
constructor(options) {
this.idleBoundaryPassed = false;
this.pendingHuman = false;
/** A host restart continues the engine-started run it cut short (senpi#7020); cleared when that run starts. */
this.restartContinues = false;
this.admittingHuman = false;
this.runAdmitted = false;
this.goal = null;
this.evaluating = false;
this.deferred = false;
this.outstandingTools = new Map();
this.options = options;
this.ledger = new LifecycleLedger({
...options,
append: (record) => {
this.obligations.flush();
options.append(record);
},
});
this.restoredRunId = this.ledger.runId;
if (this.ledger.lastTurnEnd)
this.liveness = {
...this.ledger.lastTurnEnd,
isChildSession: this.ledger.snapshot.role === "child",
mode: "rpc",
ledgerHold: undefined,
willRetry: false,
continuationQueued: undefined,
goalStopped: false,
openWork: { kind: "none" },
expectations: [],
};
this.obligations = new SessionWorkObligations({
entries: options.entries,
append: (record) => options.appendCustody(record),
changed: () => this.recompute(),
});
this.children = new LifecycleChildren({
entries: options.entries,
append: (report) => options.appendChild(report),
changed: () => {
this.ownTask = this.children.reports.find((report) => report.childSessionId === options.sessionId && report.taskId === options.spawnedBy?.taskId);
this.recompute();
},
});
for (const report of this.children.reports) {
if (report.childSessionId === options.sessionId && report.taskId === options.spawnedBy?.taskId)
this.ownTask = report;
}
// Before crash recovery reads consumed custody as an interrupted run (senpi#4848).
this.obligations.settleEndedRuns(this.ledger.runId, this.ledger.runId !== undefined && this.ledger.runId === options.endedRunId);
}
get snapshot() {
this.recompute();
return this.ledger.snapshot;
}
get lastTurnInput() {
return this.liveness;
}
get requiresHumanContinuation() {
return this.ledger.interrupted && !this.pendingHuman && !this.restartContinues;
}
get nextRunOrigin() {
return this.pendingHuman ? "human" : "automatic";
}
/**
* The current run when this process started it, not the run the session file left current. `startRun` settles what the
* previous run consumed, so this run's consumed custody is the only custody a run of this process holds.
*/
get runStartedHere() {
const runId = this.ledger.runId;
return runId === this.restoredRunId ? undefined : runId;
}
/** The host's clean stop cut the running run short: record it interrupted, as a kill leaves it (senpi#7020). */
cutByHostShutdown() {
this.ledger.interrupted = true;
this.ledger.checkpoint();
}
/** The interrupted run was the engine's and a host restart cut it: its continuation needs no person (senpi#7020). */
continueAfterRestart() {
this.restartContinues = true;
}
restoreComplete() {
this.ledger.releaseRestoreBarrier();
this.idleBoundaryPassed = true;
this.recompute();
if (this.ledger.snapshot.seq > 0)
this.ledger.checkpoint();
}
humanInput(at) {
this.admittingHuman = true;
this.pendingHuman ||= !this.ledger.runActive;
this.ledger.humanInput(at);
}
/** A turn was admitted and its run is about to start. */
noteRunAdmitted() {
this.runAdmitted = true;
}
admissionSettled() {
this.admittingHuman = false;
if (!this.ledger.runActive && !this.ledger.interrupted && this.options.readRuntime().pendingMessages === 0)
this.pendingHuman = false;
// An admitted run publishes its own opening record when it starts (senpi#3980).
if (this.runAdmitted)
return;
this.recompute();
}
startRun(origin) {
this.outstandingTools.clear();
this.modelSince = undefined;
this.admittingHuman = false;
this.runAdmitted = false;
this.idleBoundaryPassed = false;
this.liveness = undefined;
this.stopFacts = undefined;
this.deadlineStop = undefined;
// Settle whatever the previous run consumed, however it ended: idle() settles only a run that reaches it, and a
// run cut short by crash recovery or by a provider error that auto-retry replaces never does.
const previousRun = this.ledger.runId;
this.ledger.interrupted = false;
this.ledger.startRun(origin ?? (this.pendingHuman ? "human" : "automatic"));
this.pendingHuman = false;
this.restartContinues = false;
if (previousRun)
this.obligations.settleRun(previousRun);
this.recompute();
this.ledger.publishUnwritten();
}
idle() {
this.outstandingTools.clear();
this.modelSince = undefined;
this.runAdmitted = false;
const seq = this.ledger.snapshot.seq;
this.ledger.runActive = false;
this.idleBoundaryPassed = true;
const runId = this.ledger.runId;
if (runId)
this.obligations.settleRun(runId);
this.recompute();
if (seq > 0 && this.ledger.snapshot.seq === seq)
this.ledger.checkpoint();
}
/** Synchronous core boundaries only; token/progress events never cause activity writes. */
observeActivity(event) {
if (!this.ledger.runActive)
return;
switch (event.type) {
case "tool_execution_start":
if (event.parentToolCallId !== undefined)
return;
this.outstandingTools.set(event.toolCallId, {
toolName: event.toolName,
toolCallId: event.toolCallId,
since: new Date(Date.now()).toISOString(),
});
break;
case "tool_execution_end":
if (event.parentToolCallId !== undefined)
return;
this.outstandingTools.delete(event.toolCallId);
break;
case "message_start":
if (event.message.role !== "assistant" || !event.streaming)
return;
this.modelSince = new Date(Date.now()).toISOString();
break;
case "message_end":
if (event.message.role !== "assistant")
return;
this.modelSince = undefined;
break;
case "agent_end":
this.outstandingTools.clear();
this.modelSince = undefined;
break;
default:
return;
}
this.recompute();
}
/** The turn deadline fired: captured before its durable write, for the run it stopped only. */
deadlineStopped(kind, atMs) {
const runId = this.ledger.runId;
if (this.ledger.runActive && runId)
this.deadlineStop = { runId, kind, at: new Date(atMs).toISOString() };
}
/** The deadline-stop entry was refused; the captured stop says so, and the snapshot's `persisted` is untouched. */
deadlineStopWriteFailed() {
if (this.deadlineStop)
this.deadlineStop = { ...this.deadlineStop, persisted: false };
}
goalFacts(goal, input, stop) {
this.liveness = input;
this.stopFacts = stop;
if (input)
this.ledger.lastTurnEnd = {
endedBy: input.endedBy,
budgetSpent: input.budgetSpent,
deadlineStop: input.deadlineStop,
providerRetryAtMs: input.providerRetryAtMs,
};
this.goalChanged(goal);
}
goalChanged(goal) {
if (goal?.status === "complete" && this.goal?.status !== "complete")
this.ledger.markGoalCompleted(goal.id);
this.goal = goal;
this.recompute();
}
reportChild(report) {
this.children.report(report);
}
requestSelfSettle() {
if (this.admittingHuman || this.options.readRuntime().inputAdmissionPending)
return {
recorded: false,
reason: "not_eligible",
epoch: this.ledger.snapshot.epoch,
humanInputRevision: this.ledger.snapshot.humanInputRevision,
};
this.recompute();
return this.ledger.requestSelfSettle();
}
markRunCompleted(runId) {
this.ledger.markRunCompleted(runId);
this.recompute();
}
recompute() {
if (!this.obligations.persisted)
this.ledger.custodyWriteFailed();
if (this.ledger.snapshot.restoring || this.admittingHuman)
return;
if (this.evaluating) {
this.deferred = true;
return;
}
this.evaluating = true;
try {
do {
this.deferred = false;
this.evaluate();
} while (this.deferred);
}
finally {
this.evaluating = false;
}
}
evaluate() {
const runtime = this.options.readRuntime();
const active = this.obligations.active;
const childRecords = this.children.reports
.filter((report) => report.childSessionId !== this.options.sessionId && report.terminal === undefined)
.sort((a, b) => (a.taskId < b.taskId ? -1 : a.taskId > b.taskId ? 1 : 0));
const childReports = childRecords
.slice(0, 32)
.map(({ taskId, childSessionId, runEpoch, state, label, since }) => ({
taskId,
childSessionId,
runEpoch,
state,
...(label === undefined ? {} : { label }),
...(since === undefined ? {} : { since }),
}));
const childSources = active
.filter((record) => record.kind === "children")
.map((record) => ({
source: record.source ?? record.key,
activeCount: record.activeCount ?? 1,
...projectWaitItems(record),
}));
// A delivery's own custody while its send is in flight is the session starting a turn, not a wait (senpi#6810);
// once the live run consumed it, it is that run's own work until settleRun (senpi#6814 review).
const deliveryInFlight = active.some((record) => this.obligations.isDeliveryInFlight(record));
const liveRun = this.ledger.runActive ? this.ledger.runId : undefined;
const background = active
.filter((record) => record.kind === "background" &&
!this.obligations.isDeliveryInFlight(record) &&
!this.obligations.isConsumedBy(record, liveRun))
.map((record) => ({
source: record.source ?? record.key,
activeCount: record.activeCount ?? 1,
...projectWaitItems(record),
}));
const children = Math.max(childRecords.length, active
.filter((record) => record.kind === "children")
.reduce((sum, record) => sum + (record.activeCount ?? 1), 0));
const input = this.liveness;
let continuationQueued = input?.willRetry === true || input?.continuationQueued !== undefined;
let questionToUser = false;
for (const expectation of input ? effectiveWakes(input) : []) {
switch (expectation.kind) {
case "pending-messages":
continuationQueued ||= runtime.pendingMessages > 0;
break;
// A person's queued input starts the next turn itself (#3865).
case "user-input-queued":
continuationQueued = true;
break;
case "armed-timer":
if (expectation.timer === "userGrace")
continuationQueued = true;
else
background.push({
source: expectation.timer === "providerRetry" ? "provider-retry" : "goal-monitor",
activeCount: 1,
...(expectation.timer === "providerRetry" && input?.providerRetryAtMs !== undefined
? { retryAtMs: input.providerRetryAtMs }
: {}),
});
break;
case "recovery-staged":
background.push({ source: "staged-recovery", activeCount: 1 });
break;
case "wake-source":
if (active.some((record) => record.source === expectation.source))
break;
// A channel name is not a producer-declared kind.
background.push({ source: expectation.source, activeCount: 1 });
break;
case "question-to-user":
questionToUser = true;
break;
default:
expectation;
}
}
const synthetic = [];
const root = this.ledger.snapshot.role === "root";
if (root) {
if (questionToUser && runtime.human.length === 0)
synthetic.push({ key: `question:${this.ledger.runId ?? this.ledger.snapshot.epoch}`, kind: "question" });
if (this.goal?.status === "blocked")
synthetic.push({ key: `goal:${this.goal.id}`, kind: "goal_blocked" });
else if (this.goal && (this.goal.status === "paused" || this.goal.continuationStoppedAt !== undefined))
synthetic.push({ key: `goal:${this.goal.id}`, kind: "goal_paused" });
if (this.ledger.interrupted)
synthetic.push({ key: `interrupted:${this.ledger.snapshot.epoch}`, kind: "interrupted_run" });
const noWake = children === 0 &&
background.length === 0 &&
!deliveryInFlight &&
!continuationQueued &&
runtime.pendingMessages === 0;
if (input &&
noWake &&
input.endedBy !== "provider-error" &&
input.endedBy !== "policy-rejection" &&
(input.deadlineStop ||
input.budgetSpent ||
input.endedBy === "unknown-abort" ||
input.endedBy === "system-abort" ||
input.endedBy === "provider-abort" ||
((input.openWork.kind === "active-goal"
? this.goal?.status === "active"
: input.openWork.kind !== "none") &&
!input.goalStopped)))
synthetic.push({ key: `stall:${this.ledger.snapshot.epoch}`, kind: "stalled" });
else if (noWake &&
this.goal?.status === "active" &&
!this.ledger.runActive &&
!runtime.activity.isStreaming &&
!runtime.activity.hasSessionWork &&
!runtime.activity.isCompacting &&
!runtime.activity.isBashRunning &&
this.goal.continuationStoppedAt === undefined)
synthetic.push({ key: `stall:${this.ledger.snapshot.epoch}`, kind: "stalled" });
}
const human = root ? [...runtime.human, ...this.ledger.attention.reconcile(synthetic)] : [];
const running = this.ledger.runActive ||
runtime.activity.isStreaming ||
runtime.activity.isCompacting ||
runtime.activity.isBashRunning ||
runtime.activity.hasSessionWork ||
runtime.pendingMessages > 0 ||
deliveryInFlight ||
continuationQueued;
const failure = input &&
(input.endedBy === "provider-error" || input.endedBy === "policy-rejection") &&
!continuationQueued &&
background.length === 0
? {
reason: "provider_error",
...(input.providerRetryAtMs === undefined ? {} : { retryAtMs: input.providerRetryAtMs }),
}
: this.ownTask?.terminal === "error" ||
this.ownTask?.terminal === "lost" ||
this.ownTask?.terminal === "interrupted"
? {
reason: this.ownTask.terminal === "error" ? "task_error" : this.ownTask.terminal,
taskId: this.ownTask.taskId,
runEpoch: this.ownTask.runEpoch,
}
: undefined;
const waits = {
children,
background,
human,
...(childSources.length ? { childSources } : {}),
...(childReports.length ? { childReports } : {}),
...(childRecords.length > 32 ? { childReportsOmitted: childRecords.length - 32 } : {}),
};
const lastStop = this.lastStop(input, synthetic.some((wait) => wait.kind === "stalled"));
const state = lifecycleState({
role: this.ledger.snapshot.role,
terminal: this.ownTask?.terminal,
holds: runtime.holds,
waits,
running,
failure,
});
// A terminal task record owns a child's outcome, even if its local
// producer registry has not drained after the runner stopped it.
if (lifecycleTerminal(state) &&
this.ownTask?.terminal === undefined &&
(!this.idleBoundaryPassed || active.length > 0))
return;
const oldest = this.outstandingTools.values().next().value;
const activity = state !== "running"
? undefined
: oldest
? { kind: "tool", ...oldest, outstanding: this.outstandingTools.size }
: this.modelSince
? { kind: "model", since: this.modelSince }
: undefined;
this.ledger.update({ state, waits, holds: runtime.holds, failure, lastStop, activity });
}
/**
* Derived only from the current run's own end, bound when its agent_end was emitted: an end processed after a
* later run started sets nothing, and so does a fact refresh that carries no bound turn end, even when the goal
* collector still returns the previous run's end. An unchanged stop keeps its first `at`.
*/
lastStop(input, stalled) {
const existing = this.ledger.snapshot.lastStop;
const runId = this.ledger.runId;
if (!runId || runId === this.restoredRunId)
return existing;
if (this.stopFacts?.runId !== runId)
return existing;
const deadline = input && this.deadlineStop?.runId === runId ? this.deadlineStop : undefined;
const detail = stopDetail(input, this.stopFacts, deadline, stalled);
if (!detail)
return existing;
if (existing && sameStop(existing, detail))
return existing;
const { at, ...cause } = detail;
return { ...cause, runId, epoch: this.ledger.snapshot.epoch, at: at ?? new Date().toISOString() };
}
}
//# sourceMappingURL=session-lifecycle.js.map