@code-yeongyu/senpi
Version:
Coding agent CLI with read, bash, edit, write tools and session management
111 lines • 5.95 kB
JavaScript
/**
* The `checkpointed-file` durability class: the restore handler that makes a persistent file
* watch survive a restart. On create the monitor tool persists the registry's own identity
* tuple (dev/ino/size/mtimeMs/digest/present) as the manifest checkpoint; here that checkpoint
* is compared ONCE against the file as it is now, and any change that happened while the process
* was gone is reported as EXACTLY ONE line through the registry's normal event sink — so the
* notifier's coalescing and wake budget apply to it exactly as they do to a live watch event.
*
* `digest` is load-bearing: without it a same-size, same-mtime rewrite is invisible across a restart.
*/
import { stat } from "node:fs/promises";
import { resolve } from "node:path";
/**
* The single comparison, run once per restore. `created` and `replaced` outrank `modified`:
* an absent→present transition or a new dev/ino is a stronger statement about the same file.
*/
export function classifyDetachedChange(saved, live) {
if (!saved.present)
return live.present ? "created" : null;
if (!live.present)
return "gone";
if (live.dev !== saved.dev || live.ino !== saved.ino)
return "replaced";
if (live.mtimeMs !== saved.mtimeMs || live.size !== saved.size || live.digest !== saved.digest)
return "modified";
return null;
}
function lost(reason) {
return { outcome: "lost", reason };
}
/** Remaining lifetime of a durable watch; a watch with no recorded expiry keeps its default window. */
function remainingMs(monitor, now) {
if (monitor.expiresAt === null)
return DEFAULT_RESTORED_WATCH_MS;
return Math.max(1, monitor.expiresAt - now);
}
const DEFAULT_RESTORED_WATCH_MS = 300_000;
/**
* Build the `checkpointed-file` restore handler. It re-registers the watch under its original
* `mon_` id and approved parent (so the permission decision still holds), then reports at most
* one detached-change line. Returns `lost` when the path is gone or unreadable, `restored` otherwise.
*/
export function createCheckpointedFileRestoreHandler(deps) {
const now = deps.now ?? Date.now;
return async (monitor, context) => {
if (monitor.runtimeKind !== "file" || monitor.path === undefined || monitor.cwd === undefined) {
return lost(`monitor ${monitor.monitorId} is not a restorable file watch`);
}
const ephemeral = monitor.durabilityClass === "ephemeral";
if (ephemeral && context.remainingMs === undefined) {
return lost(`monitor ${monitor.monitorId} has no time left on its deadline`);
}
const saved = monitor.lastCheckpoint;
if (saved === null)
return lost(`monitor ${monitor.monitorId} has no checkpoint to compare against`);
// A watch whose file existed at checkpoint time but is gone or unreadable now cannot be
// resumed; probe BEFORE re-registering so a lost restore never leaves a live watch behind.
if (saved.present) {
try {
const target = await stat(resolve(monitor.cwd, monitor.path));
if (!target.isFile())
return lost(`watched path is no longer a regular file: ${monitor.path}`);
}
catch (error) {
return lost(`watched path is gone: ${monitor.path} (${error instanceof Error ? error.message : error})`);
}
}
let runtimeId;
try {
const startedAt = now();
// An ephemeral watch resumes with only the time it had left; a durable one has no deadline.
const lifetime = ephemeral && context.remainingMs !== undefined
? { persistent: false, deadlineMs: startedAt + context.remainingMs, timeoutMs: context.remainingMs }
: { persistent: true, deadlineMs: null, timeoutMs: remainingMs(monitor, startedAt) };
const registered = await deps.registry.registerFile({
description: monitor.description,
path: monitor.path,
monitorId: monitor.monitorId,
...lifetime,
event: monitor.event ?? "create",
cwd: monitor.cwd,
// Preserve the parent approved at permission time: registerFile re-checks it.
...(monitor.approvedParent !== undefined ? { approvedParent: monitor.approvedParent } : {}),
// The persisted deadline rides through verbatim; a restore never extends it.
...(monitor.expiresAt !== null ? { expiresAt: monitor.expiresAt } : {}),
});
runtimeId = registered.id;
}
catch (error) {
return lost(`cannot re-watch ${monitor.path}: ${error instanceof Error ? error.message : String(error)}`);
}
const live = deps.registry.fileCheckpoint(runtimeId);
if (live === undefined)
return lost(`monitor ${monitor.monitorId} settled before its checkpoint was compared`);
const change = classifyDetachedChange(saved, live);
if (change === "gone") {
// Lost the race with a deletion between the probe and re-registration: report lost, not a
// change, and settle the watch just registered so a lost restore leaves nothing behind.
await deps.registry.stopFile(runtimeId);
return lost(`watched path is gone: ${monitor.path}`);
}
deps.bindMonitorId?.(monitor.monitorId, runtimeId);
if (change !== null)
deps.registry.emitFileLine(runtimeId, `changed while detached: ${change} ${monitor.path}`);
// Re-checkpoint on every restore: after `replaced` this is the new identity, and otherwise
// it re-states the tuple the next restart compares against.
deps.writer?.scheduleCheckpoint(monitor.monitorId, live);
return { outcome: "restored" };
};
}
//# sourceMappingURL=durable-file.js.map