Preserve the owned Pi runner across controller-only E2E restart

Separate durable runner preservation from final process retirement. Bind
restart preservation to the public run identity and observed ancestry, and
drain every retained owner after the replacement controller shuts down.

Co-authored-by: Paperclip <noreply@paperclip.ing>
This commit is contained in:
DottaandPaperclip committed 2026-10-02 09:43:06 -05:00
1 parent beddbab87d
commit 59bd71dff2
11 files changed
+419 -35

No files matched your search

+52
View File
@@ -5,6 +5,20 @@ import {
type ObservedProcessGroup, type ProcessObservation,
} from "./process-tree.js";
/** Supplied by the company-scoped run API, never by process-name discovery. */
export interface RestartRunnerIdentity {
processPid: number;
processGroupId: number;
processStartedAt: string;
}
export function parseRestartRunnerIdentity(value: unknown): RestartRunnerIdentity {
const v = value as RestartRunnerIdentity | null;
if (!v || !Number.isSafeInteger(v.processPid) || v.processPid <= 1
|| v.processGroupId !== v.processPid || typeof v.processStartedAt !== "string"
|| !Number.isFinite(Date.parse(v.processStartedAt))) throw new Error("Invalid restart runner identity");
return { processPid: v.processPid, processGroupId: v.processGroupId, processStartedAt: v.processStartedAt };
}
export function createProcessTreeOwner(root: ChildProcess, options: {
readTable?: () => Promise<ProcessObservation[] | null>;
signalGroup?: (pid: number, signal: NodeJS.Signals) => void;
@@ -12,6 +26,8 @@ export function createProcessTreeOwner(root: ChildProcess, options: {
let groups: ObservedProcessGroup[] = [];
let table: ProcessObservation[] = [];
let rootStarted: string | undefined;
let preserved: ObservedProcessGroup[] = [];
let restartRunner: ProcessObservation | undefined;
let observing: Promise<void> | undefined;
const covered = new Set<number>();
const readTable = options.readTable ?? readProcessTable;
@@ -47,6 +63,14 @@ export function createProcessTreeOwner(root: ChildProcess, options: {
}
}
groups = [...byGroup.values()];
// Follow only continuously owned members of the admitted daemon tree.
// Orphaned descendants stay owned, but unrelated controller children do not.
const retainedPreserved = refreshContinuouslyLiveProcessGroups(revalidateObservedProcessGroups(preserved, next), next);
const preservedIds = new Set(retainedPreserved.map(group => group.processGroupId));
for (const member of retainedPreserved.flatMap(group => group.members)) {
for (const group of observeDescendantProcessTree(next, member.pid).groups) preservedIds.add(group.processGroupId);
}
preserved = groups.filter(group => preservedIds.has(group.processGroupId));
table = next;
})().finally(() => { observing = undefined; });
return observing;
@@ -60,6 +84,33 @@ export function createProcessTreeOwner(root: ChildProcess, options: {
function liveGroups() {
return groups.filter(group => table.some(row => row.processGroupId === group.processGroupId && running(row)));
}
async function preserveRunnerForRestart(identity: RestartRunnerIdentity) {
const expected = parseRestartRunnerIdentity(identity);
await observe();
if (restartRunner || exited()) throw new Error("Restart runner admission must precede controller exit");
const tree = observeDescendantProcessTree(table, root.pid!);
const candidate = tree.members.map(member => member.process).find(row => row.pid === expected.processPid);
if (!candidate || !running(candidate) || candidate.kind !== "paperclip-runnerd"
|| candidate.processGroupId !== expected.processGroupId
|| Date.parse(candidate.started) !== Date.parse(expected.processStartedAt)
|| candidate.processGroupId === root.pid) throw new Error("Restart runner is not the exact owned durable daemon");
const subtree = observeDescendantProcessTree(table, candidate.pid);
const members = new Set(subtree.members.map(member => member.process.pid));
const ids = new Set(subtree.groups.map(group => group.processGroupId));
if (table.some(row => ids.has(row.processGroupId) && !members.has(row.pid) && running(row))) {
throw new Error("Restart runner shares a process group with an unrelated process");
}
restartRunner = { ...candidate };
preserved = groups.filter(group => ids.has(group.processGroupId));
}
function assertRestartRunnerAlive() {
if (!restartRunner || !table.some(row => row.pid === restartRunner!.pid && row.started === restartRunner!.started
&& row.processGroupId === restartRunner!.processGroupId && running(row))) throw new Error("Admitted restart runner did not survive controller restart");
}
function restartCleanupGroups() {
const ids = new Set(preserved.map(group => group.processGroupId));
return new Set(liveGroups().filter(group => !ids.has(group.processGroupId)).map(group => group.processGroupId));
}
function signalSnapshot(signal: NodeJS.Signals, selected?: ReadonlySet<number>) {
const currentProcessGroupId = table.find(row => row.pid === process.pid)?.processGroupId ?? null;
if (currentProcessGroupId === null) throw new Error("Cleanup caller group identity is unavailable");
@@ -118,6 +169,7 @@ export function createProcessTreeOwner(root: ChildProcess, options: {
graceComplete = selected.size > 0 && delivered.length === selected.size;
}
return { observe, signal, liveGroups, gracefulRoots, signalGracefully,
preserveRunnerForRestart, assertRestartRunnerAlive, restartCleanupGroups,
directGraceDelivered: () => directGraceDelivered, stopObserving: () => { if (timer) clearInterval(timer); } };
}