@code-yeongyu/senpi
Version:
Coding agent CLI with read, bash, edit, write tools and session management
55 lines • 2.71 kB
JavaScript
/**
* The restore handlers a session's terminal persistence runs, one per durability class: each adopts the
* restored watch into the session's manifest and re-binds its stable `mon_` id to the fresh runtime id.
*/
import { readKillGuardSetting } from "../../../kill-guard/host.js";
import { createRestartableCommandHandler } from "./durable-command.js";
import { createCheckpointedFileRestoreHandler } from "./durable-file.js";
import { createCheckpointedRpcRestoreHandler } from "./durable-rpc.js";
import { reapplyPersistedMute } from "./restore.js";
export function restoreHandlers(writer, toolCtx, bundle, graceMs) {
const registry = bundle.monitors;
const adopting = (handler) => async (monitor, context) => {
const result = await handler(monitor, context);
// Only a LIVE entry is re-adopted, so the next persist keeps it and drops the rest.
if (result.outcome === "restored" || result.outcome === "muted") {
writer.adoptRestored(result.runtime === undefined ? monitor : { ...monitor, runtime: result.runtime });
registry.adoptFireWindow(monitor.monitorId, monitor.fireWindow);
}
return result;
};
let freshFileRuntimeId;
const checkpointedFile = createCheckpointedFileRestoreHandler({
registry,
writer,
bindMonitorId: (monitorId, runtimeId) => {
freshFileRuntimeId = runtimeId;
bundle.manager.bindMonitorId(monitorId, runtimeId);
},
});
return {
"restartable-command": adopting(createRestartableCommandHandler({
ctx: toolCtx,
registry,
...(graceMs !== undefined ? { graceMs } : {}),
killGuardOn: () => {
const session = toolCtx.getSessionContext?.();
return session === undefined || readKillGuardSetting(session.cwd, session.agentDir) === "on";
},
})),
"checkpointed-file": adopting(async (monitor, context) => {
freshFileRuntimeId = undefined;
const result = await checkpointedFile(monitor, context);
if (result.outcome !== "restored" || freshFileRuntimeId === undefined)
return result;
return { ...result, outcome: reapplyPersistedMute(registry, monitor, freshFileRuntimeId) };
}),
"checkpointed-rpc": adopting(createCheckpointedRpcRestoreHandler({
registry,
writer,
bindMonitorId: (monitorId, runtimeId) => bundle.manager.bindMonitorId(monitorId, runtimeId),
sessionId: () => toolCtx.getSessionContext?.()?.sessionManager?.getSessionId?.(),
})),
};
}
//# sourceMappingURL=restore-handlers.js.map