From d3e11bdbb263b8a954e3a03f06db9fa6ac428d6f Mon Sep 17 00:00:00 2001 From: Alex Castillo Date: Wed, 11 Nov 2026 11:05:52 -0400 Subject: [PATCH 1/8] fix(host-kit): keep Linux process identity stable across clock steps --- .../internal/host-process-liveness.test.ts | 60 +++++++++++++++++++ .../host-kit/src/internal/host-process.ts | 25 +++++++- 2 files changed, 84 insertions(+), 1 deletion(-) diff --git a/packages/host-kit/src/internal/host-process-liveness.test.ts b/packages/host-kit/src/internal/host-process-liveness.test.ts index f176065e5e..908e01cdba 100644 --- a/packages/host-kit/src/internal/host-process-liveness.test.ts +++ b/packages/host-kit/src/internal/host-process-liveness.test.ts @@ -16,6 +16,7 @@ import { isProcessZombie, readHostProcessIdentityObservations, readHostProcessIdentityObservationsAsync, + readProcessStartTime, readProcessIdentities, readProcessIdentityFacts, } from './host-process.ts'; @@ -266,3 +267,62 @@ test('a host with both pinned ps binaries keeps running /bin/ps', () => { assert.equal(isProcessZombie(4242), false); assert.equal(mockRunCmdSync.mock.calls[0]?.[0], '/bin/ps'); }); + +for (const probe of ['single', 'sync', 'async'] as const) { + test.skipIf(process.platform !== 'linux')( + `Linux ${probe} identity survives a wall-clock step`, + async () => { + vi.spyOn(fs, 'readFileSync').mockImplementation((path) => { + if (String(path) === '/proc/sys/kernel/random/boot_id') return 'test-boot\n'; + if (String(path) === '/proc/4242/stat') { + return `4242 (a) b) S ${Array(18).fill('0').join(' ')} 5371442 0`; + } + throw new Error('ENOENT'); + }); + const read = async () => { + if (probe === 'single') return readProcessStartTime(4242); + const rows = + probe === 'sync' + ? readHostProcessIdentityObservations([4242]) + : await readHostProcessIdentityObservationsAsync([4242]); + return rows.get(4242)?.startTime; + }; + for (const time of ['Mon Nov 9 23:54:54 2026', 'Mon Nov 9 23:54:58 2026']) { + const stdout = probe === 'single' ? time : `4242 Ss ${time}`; + psReturns(stdout); + mockRunCmd.mockResolvedValue({ stdout, stderr: '', exitCode: 0 }); + assert.equal(await read(), 'linux:test-boot:5371442'); + } + }, + ); +} + +test.skipIf(process.platform !== 'linux')( + 'Linux identities fall back to lstart when proc is unreadable or malformed', + async () => { + for (const stat of [ + null, + '4242 (broken) S 0', + `4242 (broken) S ${Array(18).fill('0').join(' ')} nope`, + ]) { + vi.spyOn(fs, 'readFileSync').mockImplementation((path) => { + if (String(path) === '/proc/sys/kernel/random/boot_id') return 'test-boot\n'; + if (stat === null) throw new Error('ENOENT'); + return stat; + }); + psReturns('original lstart'); + assert.equal(readProcessStartTime(4242), 'original lstart'); + psReturns('4242 Ss original lstart'); + mockRunCmd.mockResolvedValue({ stdout: '4242 Ss original lstart', stderr: '', exitCode: 0 }); + assert.equal( + readHostProcessIdentityObservations([4242]).get(4242)?.startTime, + 'original lstart', + ); + assert.equal( + (await readHostProcessIdentityObservationsAsync([4242])).get(4242)?.startTime, + 'original lstart', + ); + vi.restoreAllMocks(); + } + }, +); diff --git a/packages/host-kit/src/internal/host-process.ts b/packages/host-kit/src/internal/host-process.ts index 1ec84cf123..51a1a8fb34 100644 --- a/packages/host-kit/src/internal/host-process.ts +++ b/packages/host-kit/src/internal/host-process.ts @@ -218,6 +218,25 @@ function parseHostProcessCommandRows(stdout: string): Array<{ pid: number; comma })); } +function readLinuxProcessStartToken(pid: number): string | null { + if (process.platform !== 'linux') return null; + try { + const bootId = fs.readFileSync('/proc/sys/kernel/random/boot_id', 'utf8').trim(); + const stat = fs.readFileSync(`/proc/${pid}/stat`, 'utf8'); + // comm can contain spaces and closing parentheses; field 3 follows the last one. + const end = stat.lastIndexOf(')'); + if (end < 0) return null; + const startTime = stat + .slice(end + 1) + .trim() + .split(/\s+/)[19]; + if (!bootId || !startTime || !/^\d+$/.test(startTime)) return null; + return `linux:${bootId}:${startTime}`; + } catch { + return null; + } +} + export function readProcessStartTime(pid: number): string | null { return readProcessField(pid, 'lstart='); } @@ -333,7 +352,7 @@ function parseHostProcessIdentityRows(stdout: string): HostProcessIdentityRow[] if (!match) continue; const pid = Number.parseInt(match[1]!, 10); if (!Number.isInteger(pid) || pid <= 0) continue; - rows.push({ pid, state: match[2]!, startTime: match[3]! }); + rows.push({ pid, state: match[2]!, startTime: readLinuxProcessStartToken(pid) ?? match[3]! }); } return rows; } @@ -353,6 +372,10 @@ type HostProcessField = 'lstart=' | 'command=' | 'state='; function readProcessField(pid: number, field: HostProcessField): string | null { if (!Number.isInteger(pid) || pid <= 0) return null; if (isWindowsHostPlatform()) return readWindowsProcessField(pid, field); + if (field === 'lstart=') { + const token = readLinuxProcessStartToken(pid); + if (token !== null) return token; + } try { const result = runCmdSync(hostPsCommand(), ['-p', String(pid), '-o', field], { allowFailure: true, From 4e82c3daa8715b9f40d70b82a5492612c0d20fcd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C5=82=20Pierzcha=C5=82a?= Date: Fri, 13 Nov 2026 14:59:36 +0100 Subject: [PATCH 2/8] fix(host-kit): retain owners across incomparable start-time formats --- .../daemon-contracts/src/daemon-process.ts | 3 ++- .../host-kit/src/internal/host-process.ts | 12 ++++++++++- .../internal/owner-identity-liveness.test.ts | 11 +++++++++- .../src/internal/owner-identity.test.ts | 20 ++++++++++++++++++- .../host-kit/src/internal/owner-identity.ts | 5 +++-- packages/host-kit/src/process.ts | 1 + src/__tests__/daemon-exit-wait.test.ts | 20 +++++++++++++++++++ 7 files changed, 66 insertions(+), 6 deletions(-) diff --git a/packages/daemon-contracts/src/daemon-process.ts b/packages/daemon-contracts/src/daemon-process.ts index c02d9a4856..ab14469089 100644 --- a/packages/daemon-contracts/src/daemon-process.ts +++ b/packages/daemon-contracts/src/daemon-process.ts @@ -1,5 +1,6 @@ import { isProcessAlive, + processStartTimesDiffer, isProcessPid, readHostProcessIdentityObservations, readProcessCommand, @@ -83,7 +84,7 @@ export async function waitForDaemonExit( return ( Boolean( observed?.state.startsWith('Z') || - (observed?.startTime && observed.startTime !== identity.startTime), + processStartTimesDiffer(observed?.startTime, identity.startTime), ) || !isProcessAlive(identity.pid) ); }; diff --git a/packages/host-kit/src/internal/host-process.ts b/packages/host-kit/src/internal/host-process.ts index 51a1a8fb34..cdda4b5ef2 100644 --- a/packages/host-kit/src/internal/host-process.ts +++ b/packages/host-kit/src/internal/host-process.ts @@ -237,6 +237,16 @@ function readLinuxProcessStartToken(pid: number): string | null { } } +/** Positive proof of different lifetimes; unreadable or mixed-format records prove nothing. */ +export function processStartTimesDiffer( + left: string | null | undefined, + right: string | null | undefined, +): boolean { + if (!left || !right) return false; + if (left.startsWith('linux:') !== right.startsWith('linux:')) return false; + return left !== right; +} + export function readProcessStartTime(pid: number): string | null { return readProcessField(pid, 'lstart='); } @@ -767,7 +777,7 @@ export function judgeProcessIdentity( ): ProcessIdentityVerdict { if (facts.zombie) return 'exited'; const { startTime, matches } = expected; - if (startTime && facts.startTime && facts.startTime !== startTime) return 'reassigned'; + if (processStartTimesDiffer(startTime, facts.startTime)) return 'reassigned'; const commandMatches = facts.command === null || !matches ? undefined : matches(facts.command); if (!startTime && commandMatches === false) return 'reassigned'; if (!startTime && !matches) return 'unverified'; diff --git a/packages/host-kit/src/internal/owner-identity-liveness.test.ts b/packages/host-kit/src/internal/owner-identity-liveness.test.ts index 412d3e81af..ef97f9f5e3 100644 --- a/packages/host-kit/src/internal/owner-identity-liveness.test.ts +++ b/packages/host-kit/src/internal/owner-identity-liveness.test.ts @@ -7,7 +7,8 @@ const { mockIsProcessAlive, mockIsProcessZombie, mockReadProcessStartTime } = vi mockReadProcessStartTime: vi.fn(), })); -vi.mock('./host-process.ts', () => ({ +vi.mock('./host-process.ts', async (importOriginal) => ({ + ...(await importOriginal()), isProcessAlive: mockIsProcessAlive, isProcessZombie: mockIsProcessZombie, readProcessStartTime: mockReadProcessStartTime, @@ -59,6 +60,14 @@ test('a definite start-time mismatch classifies as owner-process-reused', () => ); }); +test.each([ + ['Mon Aug 10 20:00:00 2026', 'linux:11111111-1111-4111-8111-111111111111:12345'], + ['linux:11111111-1111-4111-8111-111111111111:12345', 'Mon Aug 10 20:00:00 2026'], +])('an owner with an incomparable start-time format retains its resource', (recorded, observed) => { + mockReadProcessStartTime.mockReturnValue(observed); + assert.equal(classifyOwnerLiveness({ owner: { pid: OWNER_PID, startTime: recorded } }), 'live'); +}); + test('a null-start-time owner stays fail-closed while its pid is alive', () => { // No same-clock-domain proof of birth order exists for a null-start owner: // a clock step could make a live owner look like it started after the diff --git a/packages/host-kit/src/internal/owner-identity.test.ts b/packages/host-kit/src/internal/owner-identity.test.ts index 36da646601..04bc28e075 100644 --- a/packages/host-kit/src/internal/owner-identity.test.ts +++ b/packages/host-kit/src/internal/owner-identity.test.ts @@ -22,7 +22,14 @@ test('distinguishes dead and PID-reused owners', () => { 'owner-process-dead', ); assert.equal( - classifyOwnerLiveness({ owner: { pid: process.pid, startTime: 'not-this-process' } }), + classifyOwnerLiveness({ + owner: { + pid: process.pid, + startTime: readProcessStartTime(process.pid)?.startsWith('linux:') + ? 'linux:other-boot:1' + : 'not-this-process', + }, + }), 'owner-process-reused', ); }); @@ -54,6 +61,17 @@ test('proves two owners differ only when the evidence is there', () => { assert.equal(differs({ pid: 1, startTime: 'a' }, { pid: 2, startTime: 'a' }), true); assert.equal(differs({ pid: 1, startTime: 'a' }, { pid: 1, startTime: 'b' }), true); assert.equal(differs({ pid: 1, startTime: 'a' }, { pid: 1, startTime: 'a' }), false); + assert.equal( + differs( + { pid: 1, startTime: 'Mon Aug 10 20:00:00 2026' }, + { pid: 1, startTime: 'linux:boot:1' }, + ), + false, + ); + assert.equal( + differs({ pid: 1, startTime: 'linux:boot:1' }, { pid: 1, startTime: 'linux:boot:2' }), + true, + ); // An unreadable start time on either side leaves equal pids unproven, never // different — the inverse mistake would hand a held resource to a second owner. assert.equal(differs({ pid: 1, startTime: null }, { pid: 1, startTime: 'b' }), false); diff --git a/packages/host-kit/src/internal/owner-identity.ts b/packages/host-kit/src/internal/owner-identity.ts index 9298e3e64d..c5c16e08ea 100644 --- a/packages/host-kit/src/internal/owner-identity.ts +++ b/packages/host-kit/src/internal/owner-identity.ts @@ -1,6 +1,7 @@ import fs from 'node:fs'; import { isProcessAlive, + processStartTimesDiffer, isProcessZombie, readHostProcessIdentityObservations, readHostProcessIdentityObservationsAsync, @@ -74,7 +75,7 @@ export function ownerIdentityDiffers( right: Pick, ): boolean { if (left.pid !== right.pid) return true; - return Boolean(left.startTime && right.startTime && left.startTime !== right.startTime); + return processStartTimesDiffer(left.startTime, right.startTime); } /** @@ -183,7 +184,7 @@ function provesPidReused( ): boolean { const currentStartTime = observation === undefined ? readProcessStartTime(owner.pid) : (observation?.startTime ?? null); - return currentStartTime !== null && currentStartTime !== owner.startTime; + return processStartTimesDiffer(currentStartTime, owner.startTime); } function classifyOwnerStateDirectory(stateDir: string): OwnerLiveness { diff --git a/packages/host-kit/src/process.ts b/packages/host-kit/src/process.ts index 9dddd81b2e..9a8eedfae2 100644 --- a/packages/host-kit/src/process.ts +++ b/packages/host-kit/src/process.ts @@ -34,6 +34,7 @@ export { readProcessIdentities, readProcessIdentityFacts, readProcessStartTime, + processStartTimesDiffer, signalPidsBestEffort, stopProcessAndConfirmExit, uniquePositivePids, diff --git a/src/__tests__/daemon-exit-wait.test.ts b/src/__tests__/daemon-exit-wait.test.ts index 8eff6188f7..3f933571a9 100644 --- a/src/__tests__/daemon-exit-wait.test.ts +++ b/src/__tests__/daemon-exit-wait.test.ts @@ -88,6 +88,26 @@ test('waitForDaemonExit reports a daemon that keeps its identity as not exited', expect(wait.exited).toBe(false); }); +test.each([ + [OURS, 'linux:11111111-1111-4111-8111-111111111111:12345'], + ['linux:11111111-1111-4111-8111-111111111111:12345', OURS], +])( + 'a start-time format change does not prove daemon exit or authorize a signal', + async (recorded, observed) => { + state.starts.set(PID, observed); + expect( + await waitForDaemonExit({ pid: PID, startTime: recorded }, { timeoutMs: 0 }), + ).toMatchObject({ exited: false }); + expect( + await stopDaemonProcess( + { pid: PID, startTime: recorded }, + { mode: 'force', termTimeoutMs: 0, killTimeoutMs: 0 }, + ), + ).toMatchObject({ status: 'retained', reason: 'identity-unverified' }); + expect(signals).toEqual([]); + }, +); + test('a verified zombie proves exit before its pid is reaped', async () => { state.states.set(PID, 'Z+'); state.commands.set(PID, ''); From 05f447212e2a6aede4c0a02b373588e88f2fbb6c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C5=82=20Pierzcha=C5=82a?= Date: Fri, 13 Nov 2026 15:14:59 +0100 Subject: [PATCH 3/8] fix(web): retain browser records for incomparable process lifetimes --- .../src/internal/owner-identity.test.ts | 24 ++++++++++++++++++- .../agent-browser-process-record.test.ts | 14 +++++++++++ .../src/agent-browser-process-record.ts | 10 +++++--- 3 files changed, 44 insertions(+), 4 deletions(-) diff --git a/packages/host-kit/src/internal/owner-identity.test.ts b/packages/host-kit/src/internal/owner-identity.test.ts index 04bc28e075..14ff2450a3 100644 --- a/packages/host-kit/src/internal/owner-identity.test.ts +++ b/packages/host-kit/src/internal/owner-identity.test.ts @@ -9,7 +9,12 @@ import { ownerIdentityMatches, type OwnerIdentity, } from './owner-identity.ts'; -import { readProcessStartTime } from './host-process.ts'; +import { + readProcessStartTime, + readHostProcessIdentityObservations, + readHostProcessIdentityObservationsAsync, + readProcessIdentityFacts, +} from './host-process.ts'; import { mkdtempForTestSync } from './tmp-dir.fixtures.ts'; afterEach(() => { @@ -81,3 +86,20 @@ test('proves two owners differ only when the evidence is there', () => { false, ); }); + +test.skipIf(process.platform !== 'linux')( + 'real Linux process reads share the kernel lifetime token', + async () => { + const token = readProcessStartTime(process.pid); + assert.match(token ?? '', /^linux:[0-9a-f]{8}(?:-[0-9a-f]{4}){3}-[0-9a-f]{12}:\d+$/); + assert.equal( + readHostProcessIdentityObservations([process.pid]).get(process.pid)?.startTime, + token, + ); + assert.equal( + (await readHostProcessIdentityObservationsAsync([process.pid])).get(process.pid)?.startTime, + token, + ); + assert.equal((await readProcessIdentityFacts(process.pid)).startTime, token); + }, +); diff --git a/packages/platform-web/src/__tests__/agent-browser-process-record.test.ts b/packages/platform-web/src/__tests__/agent-browser-process-record.test.ts index 2d522c4599..ea022a1b2d 100644 --- a/packages/platform-web/src/__tests__/agent-browser-process-record.test.ts +++ b/packages/platform-web/src/__tests__/agent-browser-process-record.test.ts @@ -243,3 +243,17 @@ test('an empty or undecodable record is not dead and needs no probe', async () = ); assert.equal(mockObservations.mock.calls.length, 0); }); + +test.each([ + ['Mon Aug 10 20:00:00 2026', 'linux:11111111-1111-4111-8111-111111111111:12345'], + ['linux:11111111-1111-4111-8111-111111111111:12345', 'Mon Aug 10 20:00:00 2026'], +])( + 'incomparable process lifetimes prove neither a live nor a dead browser fleet', + async (recorded, observed) => { + mockIsProcessAlive.mockReturnValue(true); + mockObservations.mockResolvedValue(new Map([[11, { state: 'Ss', startTime: observed }]])); + const store = storeReturning([daemonEntry([record(11, recorded)])]); + assert.equal(await managedWebBrowserProcessRecordIsLive(store), false); + assert.equal(await managedWebBrowserProcessRecordIsDead(store), false); + }, +); diff --git a/packages/platform-web/src/agent-browser-process-record.ts b/packages/platform-web/src/agent-browser-process-record.ts index 208f26fa83..e09f1dc4e8 100644 --- a/packages/platform-web/src/agent-browser-process-record.ts +++ b/packages/platform-web/src/agent-browser-process-record.ts @@ -10,6 +10,7 @@ import { hostProcessId, hostPlatform, isProcessAlive, + processStartTimesDiffer, listHostProcesses, readHostProcessIdentityObservationsAsync, readProcessIdentities, @@ -234,9 +235,12 @@ async function readRecordedProcessLiveness( // A pid the process table does not name is gone only when the signal probe agrees; a table // that answered nothing (a failed `ps`) leaves it unobserved. if (observation === undefined) return isProcessAlive(record.pid) ? 'unobserved' : 'not-alive'; - return !observation.state.startsWith('Z') && observation.startTime === record.startTime - ? 'alive' - : 'not-alive'; + if ( + observation.state.startsWith('Z') || + processStartTimesDiffer(observation.startTime, record.startTime) + ) + return 'not-alive'; + return observation.startTime === record.startTime ? 'alive' : 'unobserved'; }); } From 8e977aa4696a474550b74ea95592d59c3c8b62e3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C5=82=20Pierzcha=C5=82a?= Date: Fri, 13 Nov 2026 15:18:55 +0100 Subject: [PATCH 4/8] docs(web): distinguish incomparable process observations from death --- packages/platform-web/src/agent-browser-process-record.ts | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/packages/platform-web/src/agent-browser-process-record.ts b/packages/platform-web/src/agent-browser-process-record.ts index e09f1dc4e8..58b885275e 100644 --- a/packages/platform-web/src/agent-browser-process-record.ts +++ b/packages/platform-web/src/agent-browser-process-record.ts @@ -210,8 +210,9 @@ export async function managedWebBrowserProcessRecordIsLive( /** * Whether the daemon-scoped record names processes and every one of them was observed not alive: - * gone, a zombie, or under another start time than the record carries. An empty, undecodable, or - * unanswered record, or a process the probe could not observe, is unknown evidence, not dead. + * gone, a zombie, or under a comparable different start time. An empty, undecodable, or unanswered + * record, an incomparable start time, or a process the probe could not observe is unknown evidence, + * not dead. */ export async function managedWebBrowserProcessRecordIsDead( store: OwnedProcessRecordStore, From 60ecdaa0250e18f793956d1926844df1dcca6e3d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C5=82=20Pierzcha=C5=82a?= Date: Fri, 13 Nov 2026 15:57:13 +0100 Subject: [PATCH 5/8] test(host-kit): express reused lifetimes in the current host format --- .../src/__tests__/daemon-client-lifecycle.test.ts | 8 ++++++-- .../src/daemon-registration-status.test.ts | 5 ++++- packages/host-kit/src/internal/process-lock-guard.test.ts | 5 ++++- src/__tests__/daemon-registration-owner.test.ts | 8 +++++++- 4 files changed, 21 insertions(+), 5 deletions(-) diff --git a/packages/daemon-client/src/__tests__/daemon-client-lifecycle.test.ts b/packages/daemon-client/src/__tests__/daemon-client-lifecycle.test.ts index 7440725dea..d2ad20bbf3 100644 --- a/packages/daemon-client/src/__tests__/daemon-client-lifecycle.test.ts +++ b/packages/daemon-client/src/__tests__/daemon-client-lifecycle.test.ts @@ -382,9 +382,13 @@ test('daemon acquisition reclaims a proven reused owner before publication', asy const paths = resolveDaemonPaths(stateDir); const daemon = await startHttpDaemonFixture({ via: 'fresh-daemon' }); vi.stubEnv('AGENT_DEVICE_STATE_DIR', stateDir); + const staleStartTime = + process.platform === 'linux' + ? 'linux:00000000-0000-0000-0000-000000000000:0' + : 'stale-start-time'; const stale = tryAcquireProcessLock({ lockDirPath: paths.lockPath, - owner: { pid: process.pid, startTime: 'stale-start-time', acquiredAtMs: Date.now() }, + owner: { pid: process.pid, startTime: staleStartTime, acquiredAtMs: Date.now() }, }); assert.equal(stale.status, 'acquired'); installSpawnedHttpDaemon(paths, daemon.port); @@ -402,7 +406,7 @@ test('daemon acquisition reclaims a proven reused owner before publication', asy assert.deepEqual(response, { ok: true, data: { via: 'fresh-daemon' } }); assert.equal(mockRunCmdDetached.mock.calls.length, 1); assert.equal(freshLock.state, 'held'); - if (freshLock.state === 'held') assert.notEqual(freshLock.owner.startTime, 'stale-start-time'); + if (freshLock.state === 'held') assert.notEqual(freshLock.owner.startTime, staleStartTime); assert.deepEqual(daemon.seenPaths, ['GET /health', 'POST /rpc']); } finally { if (stale.status === 'acquired') await stale.acquisition.release(); diff --git a/packages/daemon-contracts/src/daemon-registration-status.test.ts b/packages/daemon-contracts/src/daemon-registration-status.test.ts index 684f2da41a..41b4fdf9e5 100644 --- a/packages/daemon-contracts/src/daemon-registration-status.test.ts +++ b/packages/daemon-contracts/src/daemon-registration-status.test.ts @@ -116,7 +116,10 @@ test('a record whose process is gone or was replaced is not a running daemon', ( token: 't', port: 1, pid: owner.pid, - processStartTime: 'Thu Jan 1 00:00:00 1970', + processStartTime: + process.platform === 'linux' + ? 'linux:00000000-0000-0000-0000-000000000000:0' + : 'Thu Jan 1 00:00:00 1970', plugins: [ACTIVE], }); expect(readDaemonStatus(resolveDaemonPaths(stateDir)).state).toBe('stopped'); diff --git a/packages/host-kit/src/internal/process-lock-guard.test.ts b/packages/host-kit/src/internal/process-lock-guard.test.ts index 23cd844144..09bcea3bf6 100644 --- a/packages/host-kit/src/internal/process-lock-guard.test.ts +++ b/packages/host-kit/src/internal/process-lock-guard.test.ts @@ -75,7 +75,10 @@ test('a long-standing guard whose holder pid now has another start time is recla const reusedHolder = { ...DEAD_GUARD_HOLDER, pid: process.ppid, - startTime: 'Thu Jan 1 00:00:00 1970', + startTime: + process.platform === 'linux' + ? 'linux:00000000-0000-0000-0000-000000000000:0' + : 'Thu Jan 1 00:00:00 1970', }; fs.writeFileSync(mutexPath, JSON.stringify(reusedHolder)); stampDirectoryAbandoned(mutexPath); diff --git a/src/__tests__/daemon-registration-owner.test.ts b/src/__tests__/daemon-registration-owner.test.ts index c18512ed21..fd44acff7b 100644 --- a/src/__tests__/daemon-registration-owner.test.ts +++ b/src/__tests__/daemon-registration-owner.test.ts @@ -232,7 +232,13 @@ test('a log removed between creation and the write-handle reopen fails startup l for (const [pid, startTime, reason] of [ [999_999_999, 'successor-start', 'replaced'], - [process.pid, 'recycled-start', 'replaced'], + [ + process.pid, + process.platform === 'linux' + ? 'linux:00000000-0000-0000-0000-000000000000:0' + : 'recycled-start', + 'replaced', + ], [process.pid, undefined, 'unproven'], [null, undefined, 'ownerless'], ] as const) { From 4de724995d7077371e3f821eb4a25d891eb5a588 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C5=82=20Pierzcha=C5=82a?= Date: Fri, 13 Nov 2026 16:27:26 +0100 Subject: [PATCH 6/8] fix(host-kit): read Linux process state and start token from one /proc read Co-Authored-By: Claude Opus 5.5 --- .../host-kit/src/internal/host-process.ts | 21 ++++++++++++------- .../agent-browser-process-record.test.ts | 2 +- src/__tests__/daemon-exit-wait.test.ts | 2 +- 3 files changed, 15 insertions(+), 10 deletions(-) diff --git a/packages/host-kit/src/internal/host-process.ts b/packages/host-kit/src/internal/host-process.ts index cdda4b5ef2..68c5097b2f 100644 --- a/packages/host-kit/src/internal/host-process.ts +++ b/packages/host-kit/src/internal/host-process.ts @@ -218,7 +218,8 @@ function parseHostProcessCommandRows(stdout: string): Array<{ pid: number; comma })); } -function readLinuxProcessStartToken(pid: number): string | null { +/** State and start token from one `/proc//stat` read, so a reused pid cannot mix lifetimes. */ +function readLinuxProcessStat(pid: number): { state: string; startTime: string } | null { if (process.platform !== 'linux') return null; try { const bootId = fs.readFileSync('/proc/sys/kernel/random/boot_id', 'utf8').trim(); @@ -226,12 +227,14 @@ function readLinuxProcessStartToken(pid: number): string | null { // comm can contain spaces and closing parentheses; field 3 follows the last one. const end = stat.lastIndexOf(')'); if (end < 0) return null; - const startTime = stat + const fields = stat .slice(end + 1) .trim() - .split(/\s+/)[19]; - if (!bootId || !startTime || !/^\d+$/.test(startTime)) return null; - return `linux:${bootId}:${startTime}`; + .split(/\s+/); + const state = fields[0]; + const startTime = fields[19]; + if (!bootId || !state || !startTime || !/^\d+$/.test(startTime)) return null; + return { state, startTime: `linux:${bootId}:${startTime}` }; } catch { return null; } @@ -362,7 +365,9 @@ function parseHostProcessIdentityRows(stdout: string): HostProcessIdentityRow[] if (!match) continue; const pid = Number.parseInt(match[1]!, 10); if (!Number.isInteger(pid) || pid <= 0) continue; - rows.push({ pid, state: match[2]!, startTime: readLinuxProcessStartToken(pid) ?? match[3]! }); + // On Linux both facts come from one /proc read; the ps row only covers other hosts. + const linux = readLinuxProcessStat(pid); + rows.push(linux ? { pid, ...linux } : { pid, state: match[2]!, startTime: match[3]! }); } return rows; } @@ -383,8 +388,8 @@ function readProcessField(pid: number, field: HostProcessField): string | null { if (!Number.isInteger(pid) || pid <= 0) return null; if (isWindowsHostPlatform()) return readWindowsProcessField(pid, field); if (field === 'lstart=') { - const token = readLinuxProcessStartToken(pid); - if (token !== null) return token; + const token = readLinuxProcessStat(pid)?.startTime; + if (token !== undefined) return token; } try { const result = runCmdSync(hostPsCommand(), ['-p', String(pid), '-o', field], { diff --git a/packages/platform-web/src/__tests__/agent-browser-process-record.test.ts b/packages/platform-web/src/__tests__/agent-browser-process-record.test.ts index ea022a1b2d..965553cfe2 100644 --- a/packages/platform-web/src/__tests__/agent-browser-process-record.test.ts +++ b/packages/platform-web/src/__tests__/agent-browser-process-record.test.ts @@ -248,7 +248,7 @@ test.each([ ['Mon Aug 10 20:00:00 2026', 'linux:11111111-1111-4111-8111-111111111111:12345'], ['linux:11111111-1111-4111-8111-111111111111:12345', 'Mon Aug 10 20:00:00 2026'], ])( - 'incomparable process lifetimes prove neither a live nor a dead browser fleet', + 'incomparable process lifetimes (recorded %s, observed %s) prove neither a live nor a dead browser fleet', async (recorded, observed) => { mockIsProcessAlive.mockReturnValue(true); mockObservations.mockResolvedValue(new Map([[11, { state: 'Ss', startTime: observed }]])); diff --git a/src/__tests__/daemon-exit-wait.test.ts b/src/__tests__/daemon-exit-wait.test.ts index 3f933571a9..d2bc489a0a 100644 --- a/src/__tests__/daemon-exit-wait.test.ts +++ b/src/__tests__/daemon-exit-wait.test.ts @@ -92,7 +92,7 @@ test.each([ [OURS, 'linux:11111111-1111-4111-8111-111111111111:12345'], ['linux:11111111-1111-4111-8111-111111111111:12345', OURS], ])( - 'a start-time format change does not prove daemon exit or authorize a signal', + 'a start-time format change (recorded %s, observed %s) does not prove daemon exit or authorize a signal', async (recorded, observed) => { state.starts.set(PID, observed); expect( From 50182d68f0c06c38692a69f784e77620dc600918 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C5=82=20Pierzcha=C5=82a?= Date: Fri, 13 Nov 2026 16:32:07 +0100 Subject: [PATCH 7/8] docs(host-kit): describe the ps fallback for identity rows Co-Authored-By: Claude Opus 5.5 --- packages/host-kit/src/internal/host-process.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/host-kit/src/internal/host-process.ts b/packages/host-kit/src/internal/host-process.ts index 68c5097b2f..f7533107ae 100644 --- a/packages/host-kit/src/internal/host-process.ts +++ b/packages/host-kit/src/internal/host-process.ts @@ -365,7 +365,7 @@ function parseHostProcessIdentityRows(stdout: string): HostProcessIdentityRow[] if (!match) continue; const pid = Number.parseInt(match[1]!, 10); if (!Number.isInteger(pid) || pid <= 0) continue; - // On Linux both facts come from one /proc read; the ps row only covers other hosts. + // On Linux both facts come from one /proc read; the ps row is the fallback when /proc is unreadable. const linux = readLinuxProcessStat(pid); rows.push(linux ? { pid, ...linux } : { pid, state: match[2]!, startTime: match[3]! }); } From b85f3ea9779a04b87350b660d05d83792701ed10 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C5=82=20Pierzcha=C5=82a?= Date: Fri, 13 Nov 2026 17:08:06 +0100 Subject: [PATCH 8/8] test(host-kit): pin Linux state to the /proc read that gave the start token Co-Authored-By: Claude Opus 5.5 --- .../internal/host-process-liveness.test.ts | 35 +++++++++++++++++++ 1 file changed, 35 insertions(+) diff --git a/packages/host-kit/src/internal/host-process-liveness.test.ts b/packages/host-kit/src/internal/host-process-liveness.test.ts index 908e01cdba..a2e6b212f8 100644 --- a/packages/host-kit/src/internal/host-process-liveness.test.ts +++ b/packages/host-kit/src/internal/host-process-liveness.test.ts @@ -297,6 +297,41 @@ for (const probe of ['single', 'sync', 'async'] as const) { ); } +for (const [psState, procState, zombie] of [ + ['Z', 'S', false], + ['S', 'Z', true], +] as const) { + test.skipIf(process.platform !== 'linux')( + `Linux state follows the /proc read that gave the start token (ps ${psState}, /proc ${procState})`, + async () => { + // A pid reused between the ps snapshot and the /proc read must not pair one lifetime's + // start token with the other lifetime's state. + vi.spyOn(fs, 'readFileSync').mockImplementation((path) => { + if (String(path) === '/proc/sys/kernel/random/boot_id') return 'test-boot\n'; + if (String(path) === '/proc/4242/stat') { + return `4242 (a) ${procState} ${Array(18).fill('0').join(' ')} 5371442 0`; + } + throw new Error('ENOENT'); + }); + const row = `4242 ${psState} Mon Nov 9 23:54:54 2026\n`; + psReturns(row); + psAnswersIdentity({ identity: row, command: '4242 y\n' }); + const expected = { state: procState, startTime: 'linux:test-boot:5371442' }; + + assert.deepEqual(readHostProcessIdentityObservations([4242]).get(4242), expected); + assert.deepEqual( + (await readHostProcessIdentityObservationsAsync([4242])).get(4242), + expected, + ); + assert.deepEqual(await readProcessIdentityFacts(4242), { + startTime: 'linux:test-boot:5371442', + command: 'y', + zombie, + }); + }, + ); +} + test.skipIf(process.platform !== 'linux')( 'Linux identities fall back to lstart when proc is unreadable or malformed', async () => {