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
JavaScript ai-agentsdashboarddeveloper-toolsdigital-twinquantified-selfsecond-brainself-hostedtailscale
test: keep Chrome startup observations bound to owned processes (#9641)
Adam Eivy3733adc09c10
diff --git a/client/src/test/browserFixture.js b/client/src/test/browserFixture.jsindex 8f27094f32..7b9ec622c6 100644--- a/client/src/test/browserFixture.js+++ b/client/src/test/browserFixture.js@@ -30,8 +30,8 @@* busy, while on a GitHub runner two first launches timed out together at 15s* with Vite already up in under 100ms. The DB-suite launcher caught the same* stall in the act (#9641): Chrome alive in uninterruptible sleep, waiting on- * page faults, under heavy I/O pressure. That points to a runner whose disk- * stalls, not a launch that needs more CPU, so the launch gets two attempts: a+ * page faults, under heavy I/O pressure. Those samples do not establish a+ * causal disk fault. The existing mitigation gives the launch two attempts: a* first one killed at half the phase, and a fresh process for the rest. The* pages the first attempt did read stay in the kernel's cache, so a slow disk* loses no progress, and a child that wedged is replaced rather than waited@@ -78,14 +78,16 @@ const PHASE_NAMES = { vite: 'Vite server start', chromium: 'Chromium launch', waconst timeoutError = ms => Object.assign(new Error(`timed out after ${ms}ms`), { name: 'TimeoutError' });-// Linux-only, bounded reads: up to 64 worker threads' `children` lists and one-// 4096-byte stat prefix per child. Never read cmdline, environ or links, and-// never print a PID or process name — only counts and the shared process facts.+// Linux-only, bounded discovery: at most 64 worker threads, 256 child+// identities and 4096 bytes per read. An incomplete census is unavailable,+// never evidence that a unique child was found. No raw proc text is reported.const readPrefix = (path) => {const fd = openSync(path, 'r');try {- const bytes = Buffer.alloc(4096);- return bytes.subarray(0, readSync(fd, bytes, 0, bytes.length, 0)).toString();+ const bytes = Buffer.alloc(4097);+ const length = readSync(fd, bytes, 0, bytes.length, 0);+ if (length > 4096) throw new Error('process observation limit');+ return bytes.subarray(0, length).toString();} finally { closeSync(fd); }};const listDir = (path) => {@@ -93,54 +95,87 @@ const listDir = (path) => {const names = [];try {let entry;- for (let count = 0; count < 64 && (entry = dir.readSync()); count++) names.push(entry.name);+ while ((entry = dir.readSync())) {+ if (names.length === 64) throw new Error('process observation limit');+ names.push(entry.name);+ }} finally { dir.closeSync(); }return names;};const BROWSER_PROCESS_NAME = /^(chrome|chromium|google-chrome|headless_shell|chrome-headless)/;-/**- * Starts observing one launch attempt: the worker's browser children that- * already exist (an earlier attempt still exiting) are excluded, and the- * returned function describes the one browser child that appeared since. Only- * a direct child of this worker is ever inspected, so the PID is owned; the- * shared facts re-check that its parent is this worker.- */+// Keeping the proc directory open binds subsequent reads to that process,+// even if it exits and its numeric PID is recycled before the sample ends.+// The kernel then refuses reads rather than following the replacement PID.+const openChild = (pid) => {+ const fd = openSync(`/proc/${pid}`, 'r');+ return {+ read: suffix => readPrefix(`/proc/self/fd/${fd}/${suffix}`),+ list: suffix => listDir(`/proc/self/fd/${fd}/${suffix}`),+ close: () => closeSync(fd),+ };+};+const childIdentity = (text, workerPid) => {+ const end = text.lastIndexOf(')');+ if (end < 0) return null;+ const fields = text.slice(end + 2).trim().split(/\s+/);+ const name = text.slice(text.indexOf('(') + 1, end);+ // Field 22 (index 19 after comm) is the process start time in clock ticks.+ if (fields[1] !== String(workerPid) || !/^\d+$/.test(fields[19] ?? '')) return null;+ return { browser: BROWSER_PROCESS_NAME.test(name), started: fields[19] };+};++/** Observe only a newly discovered direct browser child, with a stable identity. */export function _observeOwnedBrowserLaunch({platform = process.platform, workerPid = process.pid, read = readPrefix, list = listDir,- processFacts = _testChromeProcessFacts,+ processFacts = _testChromeProcessFacts, open = openChild,} = {}) {if (platform !== 'linux') return () => 'os=unsupported';- // A thread or child that exits between the listing and its read is skipped,- // but no readable `children` list at all (a kernel without them) means the- // observation is unavailable — never an empty one.- const readOrNull = (path) => { try { return read(path); } catch { return null; } };const browserChildren = () => {+ const browsers = new Map();const pids = new Set();- let readable = false;- for (const tid of list(`/proc/${workerPid}/task`).filter(name => /^[1-9]\d*$/.test(name))) {- const children = readOrNull(`/proc/${workerPid}/task/${tid}/children`);- if (children === null) continue;- readable = true;- for (const pid of children.trim().split(/\s+/)) {- if (!/^[1-9]\d*$/.test(pid)) continue;- const stat = readOrNull(`/proc/${pid}/stat`) ?? '';- const name = stat.slice(stat.indexOf('(') + 1, stat.lastIndexOf(')'));- if (BROWSER_PROCESS_NAME.test(name)) pids.add(pid);+ const tids = list(`/proc/${workerPid}/task`).filter(name => /^[1-9]\d*$/.test(name));+ if (!tids.length || tids.length > 64) throw new Error('incomplete thread census');+ for (const tid of tids) {+ const children = read(`/proc/${workerPid}/task/${tid}/children`);+ if (children.length > 4096) throw new Error('process observation limit');+ for (const pid of children.trim().split(/\s+/).filter(Boolean)) {+ if (!/^[1-9]\d*$/.test(pid)) throw new Error('invalid child identity');+ pids.add(pid);+ if (pids.size > 256) throw new Error('process observation limit');}}- if (!readable) throw new Error('no readable children list');- return pids;+ for (const pid of pids) {+ const child = open(pid);+ try {+ const identity = childIdentity(child.read('stat'), workerPid);+ if (!identity) throw new Error('child ownership unavailable');+ if (identity.browser) browsers.set(pid, identity.started);+ } finally { child.close(); }+ }+ return browsers;};let earlier;try { earlier = browserChildren(); } catch { earlier = null; }return () => {if (!earlier) return 'browserChildren=unavailable';- let fresh;- try { fresh = [...browserChildren()].filter(pid => !earlier.has(pid)); }- catch { return 'browserChildren=unavailable'; }- if (fresh.length !== 1) return `browserChildren=${fresh.length}`;- return `browserChildren=1 ${processFacts({ pid: Number(fresh[0]), exitCode: null, signalCode: null }, { workerPid })}`;+ try {+ const fresh = [...browserChildren()].filter(([pid]) => !earlier.has(pid));+ if (fresh.length !== 1) return `browserChildren=${fresh.length}`;+ const [pid, started] = fresh[0];+ const child = open(pid);+ try {+ const identity = childIdentity(child.read('stat'), workerPid);+ if (!identity?.browser || identity.started !== started) return 'browserChildren=unavailable';+ const prefix = `/proc/${pid}/`;+ const facts = processFacts({ pid: Number(pid), exitCode: null, signalCode: null }, {+ platform, workerPid,+ read: path => path.startsWith(prefix) ? child.read(path.slice(prefix.length)) : read(path),+ threads: path => child.list(path.slice(prefix.length)),+ });+ return `browserChildren=1 ${facts}`;+ } finally { child.close(); }+ } catch { return 'browserChildren=unavailable'; }};}diff --git a/client/src/test/browserFixture.test.js b/client/src/test/browserFixture.test.jsindex 0bea77dbfe..2b5750ec51 100644--- a/client/src/test/browserFixture.test.js+++ b/client/src/test/browserFixture.test.js@@ -270,46 +270,116 @@ describe('startBrowserFixture', () => {});describe('_observeOwnedBrowserLaunch', () => {- // A fake /proc: `children` lists per worker thread, and each child's stat.+ const procStat = (pid, { name = 'chrome', parent = 42, started = 100 } = {}) =>+ `${pid} (${name}) S ${parent} 42 42 ${Array(15).fill(0).join(' ')} ${started}`;+ // Open handles retain the process identity even when the numeric PID changes.const fakeProc = (state) => ({list: (path) => {if (path !== '/proc/42/task') throw new Error(`unexpected ${path}`);- return ['42', '43', 'not-a-thread'];+ return state.tids ?? ['42', '43', 'not-a-thread'];},- read: (path) => {+ read: vi.fn((path) => {if (state.unreadable) throw new Error('ENOENT');const thread = path.match(/^\/proc\/42\/task\/(\d+)\/children$/);- if (thread) return `${state.children[thread[1]] ?? ''} `;- const pid = path.match(/^\/proc\/(\d+)\/stat$/)?.[1];- if (state.names[pid]) return `${pid} (${state.names[pid]}) S 42 42 42 0 -1`;- throw new Error('ENOENT');- },+ if (thread) {+ if (thread[1] === state.unreadableThread) throw new Error('EACCES');+ return `${state.children[thread[1]] ?? ''} `;+ }+ throw new Error(`unexpected unanchored read: ${path}`);+ }),+ open: vi.fn((pid) => {+ const record = state.records[pid];+ if (!record) throw new Error('ENOENT');+ const read = vi.fn(suffix => {+ if (record.exited) throw new Error('ENOENT');+ if (suffix === 'stat') return procStat(pid, record);+ if (suffix === 'wchan') return 'folio_wait_bit_common';+ throw new Error('unavailable');+ });+ const handle = { read, list: vi.fn(() => []), close: vi.fn() };+ state.handles.push(handle);+ return handle;+ }),});-- it('describes only the browser child this worker spawned for the attempt, without its PID or name', () => {- const state = { children: { 42: '1100 1200' }, names: { 1100: 'chrome', 1200: 'esbuild', 1300: 'chrome', 1400: 'node' } };- const processFacts = vi.fn(() => 'os=linux child=uninterruptible');+ const procState = () => ({ children: {}, records: {}, handles: [] });++ it('describes only the new owned browser and anchors every child read until the sample closes', () => {+ const state = { ...procState(), children: { 42: '1100 1200' },+ records: { 1100: {}, 1200: { name: 'esbuild' }, 1300: {}, 1400: { name: 'node' } } };+ const processFacts = vi.fn((proc, options) => {+ expect(proc).toEqual({ pid: 1300, exitCode: null, signalCode: null });+ expect(options.read('/proc/1300/wchan')).toBe('folio_wait_bit_common');+ expect(options.threads('/proc/1300/task')).toEqual([]);+ return 'os=linux child=uninterruptible';+ });const sample = _observeOwnedBrowserLaunch({ platform: 'linux', workerPid: 42, processFacts, ...fakeProc(state) });- // The earlier attempt's browser (1100) is still exiting; the new one is on another thread.state.children = { 42: '1100 1200', 43: '1300 1400' };expect(sample()).toBe('browserChildren=1 os=linux child=uninterruptible');- expect(processFacts).toHaveBeenCalledWith({ pid: 1300, exitCode: null, signalCode: null }, { workerPid: 42 });+ expect(processFacts).toHaveBeenCalledOnce();+ expect(state.handles.every(handle => handle.close.mock.calls.length === 1)).toBe(true);- // Two candidates cannot be told apart, and none means nothing was spawned- // or it already exited: neither is inspected.processFacts.mockClear();state.children = { 42: '1100', 43: '1300 1500' };- state.names[1500] = 'headless_shell';+ state.records[1500] = { name: 'headless_shell' };expect(sample()).toBe('browserChildren=2');state.children = { 42: '1100' };expect(sample()).toBe('browserChildren=0');expect(processFacts).not.toHaveBeenCalled();});- it('reports an unreadable process table as unavailable, never as an empty one', () => {- const state = { children: {}, names: {}, unreadable: true };- expect(_observeOwnedBrowserLaunch({ platform: 'linux', workerPid: 42, ...fakeProc(state) })()).toBe('browserChildren=unavailable');+ it.each(['foreign-parent', 'recycled-before-sample', 'reaped-during-sample'])(+ 'does not follow an unowned or recycled PID: %s', (race) => {+ const state = procState();+ const access = fakeProc(state);+ const processFacts = vi.fn((proc, { read }) => {+ // The replacement has the same numeric PID, but the open proc handle+ // must still refer to the now-dead original process.+ state.records[1300].exited = true;+ state.records[1300] = { parent: 999, started: 101 };+ expect(() => read('/proc/1300/wchan')).toThrow('ENOENT');+ return 'os=linux child=unavailable';+ });+ const sample = _observeOwnedBrowserLaunch({ platform: 'linux', workerPid: 42, processFacts, ...access });+ state.children = { 43: '1300' };+ state.records[1300] = race === 'foreign-parent' ? { parent: 999 } : {};+ if (race === 'recycled-before-sample') {+ const originalOpen = access.open.getMockImplementation();+ access.open.mockImplementation(pid => {+ if (access.open.mock.calls.length === 2) state.records[pid] = { started: 101 };+ return originalOpen(pid);+ });+ }+ expect(sample()).toBe(race === 'reaped-during-sample'+ ? 'browserChildren=1 os=linux child=unavailable' : 'browserChildren=unavailable');+ expect(processFacts).toHaveBeenCalledTimes(race === 'reaped-during-sample' ? 1 : 0);+ expect(state.handles.every(handle => handle.close.mock.calls.length === 1)).toBe(true);+ },+ );++ it.each(['all-unreadable', 'one-unreadable', 'thread-limit', 'child-limit', 'byte-limit', 'missing-child'])(+ 'fails closed on an incomplete census: %s', (failure) => {+ const state = procState();+ const processFacts = vi.fn();+ const access = fakeProc(state);+ const sample = _observeOwnedBrowserLaunch({ platform: 'linux', workerPid: 42, processFacts, ...access });+ if (failure === 'all-unreadable') state.unreadable = true;+ if (failure === 'one-unreadable') state.unreadableThread = '43';+ if (failure === 'thread-limit') state.tids = Array.from({ length: 65 }, (_, i) => String(42 + i));+ if (failure === 'child-limit') state.children = { 42: Array.from({ length: 257 }, (_, i) => String(1000 + i)).join(' ') };+ if (failure === 'byte-limit') state.children = { 42: ' '.repeat(4097) };+ if (failure === 'missing-child') state.children = { 42: '1300' };+ expect(sample()).toBe('browserChildren=unavailable');+ expect(processFacts).not.toHaveBeenCalled();+ expect(access.open.mock.calls.length).toBeLessThanOrEqual(256);+ },+ );++ it('keeps an unavailable baseline unavailable and avoids proc reads on unsupported platforms', () => {+ const state = { ...procState(), unreadable: true };+ const sample = _observeOwnedBrowserLaunch({ platform: 'linux', workerPid: 42, ...fakeProc(state) });+ state.unreadable = false;+ expect(sample()).toBe('browserChildren=unavailable');expect(_observeOwnedBrowserLaunch({ platform: 'darwin' })()).toBe('os=unsupported');});});