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

perf: batch test child-process attribution to speed Git suites (#13506)

Adam Eivyf1d9e3cac012
diff --git a/scripts/lib/testProcessAttribution.js b/scripts/lib/testProcessAttribution.js
index 5ad0498ee2..213b7246a1 100644
--- a/scripts/lib/testProcessAttribution.js
+++ b/scripts/lib/testProcessAttribution.js
@@ -33,6 +33,8 @@ export function observeTestProcesses(getSuite, { root = process.env.PORTOS_TEST_
const directory = join(root, PROCESS_OWNERS_DIR);
const path = join(directory, `${randomUUID()}.json`);
const children = new Map();
+ const pending = new Set();
+ let inspectionTimer;
const worker = workerIdentity(process.env.VITEST_WORKER_ID);
let phase = 'suite-active';
let capped = false;
@@ -44,20 +46,45 @@ export function observeTestProcesses(getSuite, { root = process.env.PORTOS_TEST_
renameSync(`${path}.tmp`, path);
} catch { /* missing evidence is explicitly unavailable at containment */ }
};
+ const inspectPending = () => {
+ clearTimeout(inspectionTimer);
+ inspectionTimer = undefined;
+ const candidates = [...pending];
+ pending.clear();
+ if (!candidates.length) return;
+ try {
+ const records = read(candidates.map(child => child.pid));
+ for (const child of candidates) {
+ const item = children.get(child);
+ const record = records?.find(record => record.pid === child.pid);
+ if (item && record?.start && !record.terminal && record.ppid === process.pid) item.row.identity = processIdentity(record);
+ }
+ } catch { /* attribution remains unavailable, never guess by PID */ }
+ persist();
+ };
const listener = ({ process: child }) => {
if (children.size >= limit) { capped = true; persist(); return; }
let suite = 'unavailable';
try { suite = suiteIdentity(getSuite()); } catch { /* diagnostic context is optional */ }
const row = { identity: null, suite, worker, phase, lifecycle: 'constructed' };
const onSpawn = () => {
- try {
- const record = read([child.pid])?.find(item => item.pid === child.pid);
- if (record?.start && !record.terminal) row.identity = processIdentity(record);
- } catch { /* attribution remains unavailable, never guess by PID */ }
- row.lifecycle = 'spawned'; persist();
+ row.lifecycle = 'spawned';
+ pending.add(child);
+ // Most Git children close within milliseconds. Inspect survivors together
+ // rather than starting a synchronous OS reader for every short command.
+ inspectionTimer ??= setTimeout(inspectPending, 250).unref();
+ };
+ const onExit = () => {
+ pending.delete(child);
+ row.lifecycle = 'exit-observed';
+ if (row.identity) persist();
+ };
+ const onClose = () => {
+ pending.delete(child);
+ children.delete(child);
+ if (row.identity) persist();
+ if (!pending.size) { clearTimeout(inspectionTimer); inspectionTimer = undefined; }
};
- const onExit = () => { row.lifecycle = 'exit-observed'; persist(); };
- const onClose = () => { children.delete(child); persist(); };
children.set(child, { row, onSpawn, onExit, onClose });
child.once('spawn', onSpawn);
child.once('exit', onExit);
@@ -66,6 +93,9 @@ export function observeTestProcesses(getSuite, { root = process.env.PORTOS_TEST_
spawned.subscribe(listener);
return () => {
spawned.unsubscribe(listener);
+ // Teardown cannot leave a timer or lose a live child born just before it.
+ if (pending.size) inspectPending();
+ clearTimeout(inspectionTimer);
phase = 'suite-ended';
for (const [child, item] of children) {
item.row.phase = phase;
diff --git a/scripts/testProcessAttribution.test.js b/scripts/testProcessAttribution.test.js
index b87b15db40..3cf6ca77eb 100644
--- a/scripts/testProcessAttribution.test.js
+++ b/scripts/testProcessAttribution.test.js
@@ -15,7 +15,7 @@ vi.mock('node:fs', async importOriginal => {
});
const originalExit = process.exitCode;
-afterEach(() => { process.exitCode = originalExit; vi.restoreAllMocks(); });
+afterEach(() => { process.exitCode = originalExit; vi.useRealTimers(); vi.restoreAllMocks(); });
// Regression: a suite-ended child must be attributed before the root disappears,
// while a successful signal cannot remove that root until terminal identity exists.
@@ -91,3 +91,65 @@ it.each(['persisted', 'write-failed'])('pool-close preserves a %s pending identi
expect(verifyPendingTestProcesses(root, { read: () => [], probePid: () => false })).toBe(true);
} finally { rmSync(host, { recursive: true, force: true }); }
});
+
+// Regression: short Git commands must not each start an OS reader, while a
+// surviving child still retains its original suite and terminal lifecycle.
+it('batches surviving children and flushes recent spawns at teardown without inspecting closed commands', () => {
+ vi.useFakeTimers();
+ const root = mkdtempSync(join(tmpdir(), 'process-batched-'));
+ const records = [900001, 900002, 900003].map(pid => ({ pid, ppid: process.pid, start: `birth-${pid}` }));
+ const read = vi.fn(pids => records.filter(record => pids.includes(record.pid)));
+ let suite = fileURLToPath(import.meta.url);
+ const dispose = observeTestProcesses(() => suite, { root, read });
+ const launch = pid => {
+ const child = Object.assign(new EventEmitter(), { pid });
+ channel('child_process').publish({ process: child });
+ child.emit('spawn');
+ return child;
+ };
+ try {
+ for (let i = 0; i < 100; i++) {
+ const child = launch(910000 + i);
+ child.emit('exit');
+ child.emit('close');
+ }
+ vi.advanceTimersByTime(250);
+ expect(read).not.toHaveBeenCalled();
+ const first = launch(records[0].pid);
+ launch(records[1].pid);
+ suite = '/example/another.test.js';
+ vi.advanceTimersByTime(249);
+ expect(read).not.toHaveBeenCalled();
+ vi.advanceTimersByTime(1);
+ expect(read.mock.calls).toEqual([[[records[0].pid, records[1].pid]]]);
+ expect(processAttribution(root, processIdentity(records[0]))).toMatchObject({ suite: 'scripts/testProcessAttribution.test.js', phase: 'suite-active', lifecycle: 'spawned' });
+ first.emit('exit');
+ expect(processAttribution(root, processIdentity(records[0])).lifecycle).toBe('exit-observed');
+ first.emit('close');
+ expect(processAttribution(root, processIdentity(records[0])).attribution).toBe('unavailable');
+ launch(records[2].pid);
+ dispose();
+ expect(read.mock.calls).toEqual([[[records[0].pid, records[1].pid]], [[records[2].pid]]]);
+ expect(processAttribution(root, processIdentity(records[1]))).toMatchObject({ suite: 'scripts/testProcessAttribution.test.js', phase: 'suite-ended', lifecycle: 'spawned' });
+ expect(processAttribution(root, processIdentity(records[2]))).toMatchObject({ phase: 'suite-ended', lifecycle: 'spawned' });
+ vi.advanceTimersByTime(1000);
+ expect(read).toHaveBeenCalledTimes(2);
+ } finally { dispose(); rmSync(root, { recursive: true, force: true }); }
+});
+
+it.each(['missing', 'terminal', 'different-parent', 'unreadable'])('does not attribute a deferred %s process identity', state => {
+ const root = mkdtempSync(join(tmpdir(), 'process-identity-gap-'));
+ const record = { pid: 900001, ppid: process.pid, start: 'original' };
+ const read = () => {
+ if (state === 'unreadable') throw Error('synthetic unavailable reader');
+ return state === 'missing' ? [] : [{ ...record, terminal: state === 'terminal', ppid: state === 'different-parent' ? 1 : process.pid }];
+ };
+ const dispose = observeTestProcesses(() => fileURLToPath(import.meta.url), { root, read });
+ try {
+ const child = Object.assign(new EventEmitter(), { pid: record.pid });
+ channel('child_process').publish({ process: child });
+ child.emit('spawn');
+ dispose();
+ expect(processAttribution(root, processIdentity(record)).attribution).toBe('unavailable');
+ } finally { dispose(); rmSync(root, { recursive: true, force: true }); }
+});