Skip to content

Self-hosted, Self-Improving, Federated Everything App — dev management, agent orchestration, persistent mind, digital twin, second brain, and creative pipeline from a single dashboard over Tailscale to port the user into the OS

fix: reclaim dead test roots with incomplete process ancestry (#12162)

Adam Eivy810ff2bccfd6
diff --git a/scripts/lib/testOwnedProcesses.js b/scripts/lib/testOwnedProcesses.js
index f1c87c327e..11fcb360d4 100644
--- a/scripts/lib/testOwnedProcesses.js
+++ b/scripts/lib/testOwnedProcesses.js
@@ -100,11 +100,15 @@ export function testProcessRoot({ args = [], env = {} }, parent, prefix) {
export function processAncestors(records, pid = process.pid) {
const parents = new Map(records.map(record => [record.pid, record.ppid]));
+ if (!parents.has(pid)) return null;
const ancestors = new Set([1, pid, process.ppid]);
const visited = new Set();
while (pid > 1) {
if (visited.has(pid)) return null;
visited.add(pid);
+ // Readers may omit an ancestor owned by another uid. Keep every known
+ // ancestor excluded without requiring visibility all the way to init.
+ if (!parents.has(pid)) return ancestors;
const parent = parents.get(pid);
if (!Number.isSafeInteger(parent) || parent < 1 || parent === pid) return null;
if (parent === 1) return ancestors;
diff --git a/scripts/lib/vitestStaleRunRoots.js b/scripts/lib/vitestStaleRunRoots.js
index f778269a30..d6ab780a4f 100644
--- a/scripts/lib/vitestStaleRunRoots.js
+++ b/scripts/lib/vitestStaleRunRoots.js
@@ -185,9 +185,9 @@ export function sweepStaleRunRoots(tmpRoot, { prefix = 'pvt-', now = Date.now(),
if (typeof prefix !== 'string' || !/^[a-zA-Z0-9-]+-$/.test(prefix)) {
throw new Error('Owned temp prefix must be a nonempty name ending in a hyphen');
}
- // Keep ownership evidence when the platform cannot inspect processes.
+ // Dead-owner evidence is sufficient to reclaim a root even if process
+ // inspection failed. Unknown owners still require inspection before aging out.
const { inspected, preserved } = sweepOrphanedTestProcesses(tmpRoot, { prefix, now, probe, read, terminate });
- if (!inspected && process.platform !== 'win32') return;
let names;
try {
names = readdirSync(tmpRoot);
@@ -204,7 +204,7 @@ export function sweepStaleRunRoots(tmpRoot, { prefix = 'pvt-', now = Date.now(),
const owner = readOwner(path);
const alive = owner ? probe(owner) : null;
if (alive === true) continue;
- if (alive === false || now - stat.mtimeMs > STALE_ROOT_AGE_MS) {
+ if (alive === false || (inspected && now - stat.mtimeMs > STALE_ROOT_AGE_MS)) {
rmSync(path, { recursive: true, force: true });
}
} catch {
diff --git a/scripts/testOwnedProcessRecovery.test.js b/scripts/testOwnedProcessRecovery.test.js
index a7a885c55e..9a0a6bf2b9 100644
--- a/scripts/testOwnedProcessRecovery.test.js
+++ b/scripts/testOwnedProcessRecovery.test.js
@@ -1,16 +1,45 @@
import { afterEach, describe, expect, it, vi } from 'vitest';
import { spawn, spawnSync } from 'node:child_process';
-import { lstatSync, mkdtempSync, mkdirSync, rmSync, symlinkSync, writeFileSync } from 'node:fs';
+import { existsSync, lstatSync, mkdtempSync, mkdirSync, rmSync, symlinkSync, utimesSync, writeFileSync } from 'node:fs';
import { tmpdir } from 'node:os';
import { join } from 'node:path';
import { fileURLToPath } from 'node:url';
-import { killTestProcessesUnder, sweepStaleRunRoots, writeOwnerFile } from './lib/vitestStaleRunRoots.js';
-import { readTestProcesses, testProcessRoot } from './lib/testOwnedProcesses.js';
+import { STALE_ROOT_AGE_MS, killTestProcessesUnder, sweepStaleRunRoots, writeOwnerFile } from './lib/vitestStaleRunRoots.js';
+import { processAncestors, readTestProcesses, testProcessRoot } from './lib/testOwnedProcesses.js';
import { isProcessAlive } from '../server/test/processAlive.js';
afterEach(() => vi.useRealTimers());
describe('test process ownership boundaries', () => {
+ it('keeps the known ancestor chain when a listing omits a parent, but rejects cycles', () => {
+ const records = [{ pid: 900001, ppid: 900002 }, { pid: 900002, ppid: 900003 }];
+ expect(processAncestors(records, 900004)).toBeNull();
+ expect(processAncestors(records, 900001)).toEqual(new Set([1, 900001, process.ppid, 900002, 900003]));
+ expect(processAncestors([...records, { pid: 900003, ppid: 900001 }], 900001)).toBeNull();
+ });
+
+ it.each(['missing ancestor', 'unavailable reader', 'cyclic ancestry'])('reclaims dead roots and gates the age fallback with %s', mode => {
+ const host = mkdtempSync(join(tmpdir(), 'recovery-snapshot-'));
+ const dead = join(host, 'pvt-dead');
+ const live = join(host, 'pvt-live');
+ const unknown = join(host, 'pvt-unknown');
+ const records = [{ pid: process.pid, ppid: process.ppid }];
+ if (mode === 'cyclic ancestry') records.push({ pid: process.ppid, ppid: process.pid });
+ const read = () => mode === 'unavailable reader' ? null : records;
+ const aged = new Date(Date.now() - STALE_ROOT_AGE_MS - 60000);
+ try {
+ for (const [root, pid] of [[dead, 900001], [live, 900002], [unknown, 900003]]) {
+ mkdirSync(root);
+ writeFileSync(join(root, '.owner.pid'), `${pid} 0\n`);
+ utimesSync(root, aged, aged);
+ }
+ sweepStaleRunRoots(host, { read, probe: ({ pid }) => pid === 900001 ? false : pid === 900002 ? true : null });
+ expect(existsSync(dead)).toBe(false);
+ expect(existsSync(live)).toBe(true);
+ expect(existsSync(unknown)).toBe(mode !== 'missing ancestor');
+ } finally { rmSync(host, { recursive: true, force: true }); }
+ });
+
it.skipIf(process.platform === 'win32')('retains only legacy browser ownership when the optional Darwin reader is unavailable', () => {
const exec = vi.fn((command) => {
if (command !== 'ps') throw Error('reader unavailable');