@code-yeongyu/senpi
Version:
Coding agent CLI with read, bash, edit, write tools and session management
44 lines • 2.31 kB
JavaScript
/**
* The one terminal finalizer of a startup attempt (senpi#4602, A-amended section 6). Every failure path
* (timeout, explicit failure, protocol fault, registration error) settles through it exactly once: it
* latches the original cause, runs the owned cleanup inside its nonrenewable allowance, and reports
* `cleanup=incomplete` when that cleanup did not finish in time or failed. The original diagnostic
* always comes first; a cleanup failure is appended after it, never instead of it.
*/
import { childStalledStopMaxMs } from "./host-stalled-evidence.js";
import { startupEnvelope, startupTimeoutDetail } from "./host-startup-policy.js";
import { monotonicStartupClock } from "./host-startup-run.js";
export function createStartupFinalizer(options) {
let settled;
return (outcome) => {
settled ??= finalize(outcome);
return settled;
};
async function finalize(outcome) {
const { clock } = options;
let timer;
const expired = new Promise((resolveExpired) => {
timer = clock.scheduleAt(clock.now() + options.cleanupBudgetMs, () => resolveExpired("expired"));
});
const ran = options.cleanup().then(() => "completed", (error) => (error instanceof Error ? error : new Error(String(error))));
const result = await Promise.race([ran, expired]);
clock.cancel(timer);
const cleanup = result === "completed" ? "completed" : "incomplete";
const original = outcome.kind === "timeout" ? startupTimeoutDetail(outcome, cleanup) : outcome.cause.message;
const appended = result instanceof Error
? `\ncleanup failed: ${result.message}`
: result === "expired"
? `\ncleanup did not finish within ${options.cleanupBudgetMs}ms`
: "";
return { outcome, cleanup, detail: `${original}${appended}` };
}
}
/** One failed `startHost` attempt, settled once: owned cleanup inside the failed-start allowance C at the resolved stall setting. */
export function finalizeFailedStart(outcome, cleanup) {
return createStartupFinalizer({
clock: monotonicStartupClock(),
cleanupBudgetMs: startupEnvelope(childStalledStopMaxMs()).cleanup,
cleanup,
})(outcome);
}
//# sourceMappingURL=host-startup-finalizer.js.map