UNPKG

@code-yeongyu/senpi

Version:

Coding agent CLI with read, bash, edit, write tools and session management

92 lines • 4.03 kB
import { isRecord } from "./work-custody-record.js"; const ABORT_SOURCES = { "user-abort": "user", "system-abort": "system", "provider-abort": "provider", "unknown-abort": null, }; /** * First match wins: the run's own deadline explains its system abort, a guard explains a refused continuation, then * the classified abort, and `no_wake` only for a stall no hold or guard owns. Provider failures keep the failed path. */ export function stopDetail(input, facts, deadline, stalled) { if (input?.endedBy === "provider-error" || input?.endedBy === "policy-rejection") return undefined; if (deadline) return { cause: "deadline", deadline: deadline.kind, at: deadline.at, ...(deadline.persisted === false ? { persisted: false } : {}), }; if (facts?.guard) return { cause: "goal_guard", guard: facts.guard.reason, at: new Date(facts.guard.at).toISOString() }; if (facts?.todo === "capped" || facts?.todo === "cap") return { cause: "todo_guard", todo: "cap" }; const abortSource = input ? ABORT_SOURCES[input.endedBy] : undefined; if (abortSource !== undefined) return { cause: "aborted", ...(abortSource === null ? {} : { abortSource }) }; if (stalled && !input?.deadlineStop && !input?.budgetSpent) return { cause: "no_wake" }; return undefined; } export function sameStop(stop, detail) { return (stop.cause === detail.cause && stop.deadline === detail.deadline && stop.guard === detail.guard && stop.todo === detail.todo && stop.abortSource === detail.abortSource && stop.persisted === detail.persisted); } /** The latest guard appended within one bound end's extension-processing window, including legacy entries. */ export function goalGuardAfter(branch, guardStartLeafId) { const start = guardStartLeafId === null ? -1 : branch.findIndex((entry) => entry.id === guardStartLeafId); if (guardStartLeafId !== null && start < 0) return undefined; for (let index = branch.length - 1; index > start; index--) { const entry = branch[index]; if (entry?.type !== "custom" || entry.customType !== "goal-continuation-stopped" || !isRecord(entry.data)) continue; const { reason, at } = entry.data; return typeof reason === "string" && typeof at === "number" ? { reason, at } : undefined; } return undefined; } /** Malformed optional detail is dropped; it never invalidates the record that carries it. */ export function readLastStop(value, runId) { if (!isRecord(value) || typeof value.runId !== "string" || value.runId !== runId || typeof value.at !== "string" || typeof value.epoch !== "number") return undefined; const base = { runId: value.runId, at: value.at, epoch: value.epoch }; switch (value.cause) { case "deadline": return value.deadline === "inactivity" || value.deadline === "max_runtime" ? { ...base, cause: value.cause, deadline: value.deadline, ...(value.persisted === false ? { persisted: false } : {}), } : undefined; case "goal_guard": return { ...base, cause: value.cause, ...(typeof value.guard === "string" ? { guard: value.guard } : {}) }; case "todo_guard": return { ...base, cause: value.cause, todo: "cap" }; case "aborted": return { ...base, cause: value.cause, ...(value.abortSource === "user" || value.abortSource === "system" || value.abortSource === "provider" ? { abortSource: value.abortSource } : {}), }; case "no_wake": return { ...base, cause: value.cause }; default: return undefined; } } //# sourceMappingURL=lifecycle-stop.js.map