@code-yeongyu/senpi
Version:
Coding agent CLI with read, bash, edit, write tools and session management
165 lines • 7.84 kB
JavaScript
/**
* The `restartable-command` restore handler. It stops a verified crash-orphaned watcher first,
* re-spawns the saved command once in the saved cwd with the restore environment (stable id,
* state dir, restored flag, downtime bound), and only calls the watch restored if it is still
* running when the grace window closes: an immediate non-zero exit is lost with its exit code and
* first output line, an immediate zero exit is a completed one-shot gate. A persistent watch has
* no deadline; an ephemeral one gets exactly the time it had left. Nothing the pre-restart PTY
* produced is replayed.
*/
import { stat } from "node:fs/promises";
import { isAbsolute } from "node:path";
import { judgeCommand } from "../../../kill-guard/command.js";
import { hostKillGuardContext } from "../../../kill-guard/host.js";
import { ensureMonitorStateDir, terminalStateDir } from "./monitor-state-dir.js";
import { formatElapsedSeconds } from "./monitor-status.js";
import { reapBeforeRespawn } from "./orphan-reaper.js";
import { sanitizeTerminalOutput } from "./output-format.js";
import { reapplyPersistedMute, } from "./restore.js";
import { DEFAULT_COLS, DEFAULT_ROWS, MONITOR_ENV_DOWNTIME_MS, MONITOR_ENV_ID, MONITOR_ENV_RESTORED, MONITOR_ENV_STATE_DIR, RESTORE_GRACE_MS, } from "./shared.js";
import { spawnCommandSession } from "./tools/spawn.js";
const judgeRestored = (command) => judgeCommand(command, hostKillGuardContext({ kind: "fresh-session" }, "bash"));
const MAX_REASON_OUTPUT_CHARS = 200;
const lost = (reason, orphan) => ({
outcome: "lost",
reason,
...(orphan !== undefined ? { orphan } : {}),
});
async function defaultDirectoryExists(path) {
try {
return (await stat(path)).isDirectory();
}
catch {
return false;
}
}
function compileFilter(source) {
if (source === undefined)
return undefined;
try {
return new RegExp(source);
}
catch {
// A filter that no longer compiles must not sink the watch: restore it unfiltered.
return undefined;
}
}
function dimension(value, fallback) {
return value !== undefined && Number.isFinite(value) && value >= 1 ? Math.trunc(value) : fallback;
}
function exitsWithin(runtime, ms) {
if (runtime.exited)
return Promise.resolve(true);
return new Promise((resolve) => {
const timer = setTimeout(() => {
unsubscribe();
resolve(false);
}, ms);
const unsubscribe = runtime.session.onExit(() => {
clearTimeout(timer);
resolve(true);
});
});
}
function firstOutputLine(runtime) {
const line = sanitizeTerminalOutput(runtime.fullOutput())
.split("\n")
.map((text) => text.trim())
.find((text) => text.length > 0);
return (line ?? "").slice(0, MAX_REASON_OUTPUT_CHARS);
}
function orphanOf(runtime, reap) {
if (runtime === undefined || reap === undefined)
return undefined;
if (reap.action === "killed")
return { pid: runtime.pid, action: "killed" };
if (reap.reason !== undefined)
return { pid: runtime.pid, action: "unverified" };
return undefined;
}
async function restoreEnvironment(deps, monitor, context) {
const env = {
[MONITOR_ENV_ID]: monitor.monitorId,
[MONITOR_ENV_RESTORED]: "1",
[MONITOR_ENV_DOWNTIME_MS]: String(context.downtimeMs),
};
const terminalDir = terminalStateDir(deps.ctx.getSessionContext?.());
if (monitor.persistent && terminalDir !== undefined) {
env[MONITOR_ENV_STATE_DIR] = await ensureMonitorStateDir(terminalDir, monitor.monitorId);
}
return env;
}
export function createRestartableCommandHandler(deps) {
const spawn = deps.spawn ?? spawnCommandSession;
const directoryExists = deps.directoryExists ?? defaultDirectoryExists;
const reapOrphan = deps.reapOrphan ?? reapBeforeRespawn;
const graceMs = deps.graceMs ?? RESTORE_GRACE_MS;
const judge = deps.judge ?? judgeRestored;
return async (monitor, context) => {
const ephemeralTimeLeft = monitor.persistent ? undefined : context.remainingMs;
if (monitor.runtimeKind !== "command" || (!monitor.persistent && ephemeralTimeLeft === undefined)) {
return lost("not a restorable command watch");
}
const { command, cwd } = monitor;
if (command === undefined || command.length === 0)
return lost("no command was recorded");
if (cwd === undefined || !isAbsolute(cwd))
return lost("no absolute cwd was recorded");
if (!(await directoryExists(cwd)))
return lost(`cwd no longer exists: ${cwd}`);
const reap = monitor.runtime === undefined ? undefined : await reapOrphan(monitor.runtime, monitor.monitorId);
const orphan = orphanOf(monitor.runtime, reap);
// The manifest carries no guard state: a watch saved before the guard, or while it was off, was never judged (A5).
if (deps.killGuardOn?.() ?? true) {
const verdict = judge(command);
if (verdict.action === "refuse")
return lost(verdict.message, orphan);
}
let spawned;
try {
spawned = await spawn(deps.ctx, {
command,
cols: dimension(deps.ctx.defaultCols, DEFAULT_COLS),
rows: dimension(deps.ctx.defaultRows, DEFAULT_ROWS),
cwd,
envOverrides: await restoreEnvironment(deps, monitor, context),
...(ephemeralTimeLeft !== undefined ? { timeoutMs: ephemeralTimeLeft } : {}),
});
}
catch (error) {
return lost(`spawn failed: ${error instanceof Error ? error.message : String(error)}`, orphan);
}
deps.registry.register({
id: spawned.id,
monitorId: monitor.monitorId,
description: monitor.description,
command,
persistent: monitor.persistent,
deadlineMs: ephemeralTimeLeft !== undefined ? Date.now() + ephemeralTimeLeft : null,
runtime: spawned.runtime,
filter: compileFilter(monitor.filter),
...(monitor.humanInput === true ? { humanInput: true } : {}),
...(monitor.wakesPerHour !== undefined ? { wakesPerHour: monitor.wakesPerHour } : {}),
// The persisted deadline rides through verbatim; a restore never extends it.
...(monitor.expiresAt !== null ? { expiresAt: monitor.expiresAt } : {}),
});
deps.ctx.manager.bindMonitorId(monitor.monitorId, spawned.id);
const startedAt = Date.now();
if (await exitsWithin(spawned.runtime, graceMs)) {
const code = spawned.runtime.exitResult?.exitCode ?? null;
if (code === 0)
return { outcome: "completed", ...(orphan !== undefined ? { orphan } : {}) };
const output = firstOutputLine(spawned.runtime);
const exit = code === null ? "exited" : `exited ${code}`;
return lost(`${exit} in ${Date.now() - startedAt}ms${output.length > 0 ? `: ${output}` : ""}`, orphan);
}
deps.registry.emitLine(spawned.id, `restored after up to ${formatElapsedSeconds(context.downtimeMs / 1000)} offline; the command started fresh`);
deps.onRestored?.(monitor.monitorId, spawned.id);
// A persisted mute is re-applied by the FRESH runtime id; the registry resolves
// records by runtime id only, so the mon_ id would silently no-op here.
const outcome = reapplyPersistedMute(deps.registry, monitor, spawned.id);
const runtime = spawned.runtime.identity();
return { outcome, ...(orphan !== undefined ? { orphan } : {}), ...(runtime !== undefined ? { runtime } : {}) };
};
}
//# sourceMappingURL=durable-command.js.map