From 1fd1f8e7ca766ec33dfb03f7f8aa3c895d4e16a1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C5=82=20Pierzcha=C5=82a?= Date: Tue, 17 Nov 2026 22:00:20 +0100 Subject: [PATCH] test(host-kit): answer the identity fixture from the double, not the runner The identity reader prefers one `/proc//stat` read over the `ps` row it parses, so a fixture pid the runner already owns answered with the host's real state and the `ps` double was ignored. The cases passed while the runner's pid space had not reached 42 or 101, and failed on a bigger runner that had. Answers the `/proc` reads with nothing, so the double is the only evidence about the fixture pid whatever its value. Co-Authored-By: Claude Opus 5.5 --- .../src/internal/host-proc.fixtures.ts | 18 ++++++++++++++++++ .../src/internal/host-process-liveness.test.ts | 9 ++------- .../internal/host-process-stop-confirm.test.ts | 9 ++++++++- .../src/internal/owned-process-record.test.ts | 4 ++++ 4 files changed, 32 insertions(+), 8 deletions(-) create mode 100644 packages/host-kit/src/internal/host-proc.fixtures.ts diff --git a/packages/host-kit/src/internal/host-proc.fixtures.ts b/packages/host-kit/src/internal/host-proc.fixtures.ts new file mode 100644 index 0000000000..cd3350abaa --- /dev/null +++ b/packages/host-kit/src/internal/host-proc.fixtures.ts @@ -0,0 +1,18 @@ +import fs from 'node:fs'; +import { vi } from 'vitest'; + +/** + * Answers the identity reader's `/proc` reads with nothing, so these files' `ps` doubles are the only + * evidence. The reader prefers one `/proc//stat` read over the `ps` row it parses, so on a runner + * whose pid space has reached a fixture pid, the host's real state answered instead of the double and + * the case saw a lifetime nobody wrote. + */ +export function mockLinuxProcessStatAbsent(): void { + const readFileSync = fs.readFileSync; + vi.spyOn(fs, 'readFileSync').mockImplementation((path, options) => { + if (String(path).startsWith('/proc/')) { + throw Object.assign(new Error('proc is unavailable in the ps fixture'), { code: 'ENOENT' }); + } + return readFileSync(path, options); + }); +} 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 034658fd49..b36030f441 100644 --- a/packages/host-kit/src/internal/host-process-liveness.test.ts +++ b/packages/host-kit/src/internal/host-process-liveness.test.ts @@ -20,6 +20,7 @@ import { readProcessIdentities, readProcessIdentityFacts, } from './host-process.ts'; +import { mockLinuxProcessStatAbsent } from './host-proc.fixtures.ts'; function psReturns(stdout: string, exitCode = 0): void { mockRunCmdSync.mockReturnValue({ stdout, stderr: '', exitCode }); @@ -42,13 +43,7 @@ function psInstalledAt(installed: readonly string[]): void { } beforeEach(() => { - const readFileSync = fs.readFileSync; - vi.spyOn(fs, 'readFileSync').mockImplementation((path, options) => { - if (String(path).startsWith('/proc/')) { - throw Object.assign(new Error('proc is unavailable in the ps fixture'), { code: 'ENOENT' }); - } - return readFileSync(path, options); - }); + mockLinuxProcessStatAbsent(); mockRunCmdSync.mockReset(); mockRunCmd.mockReset(); }); diff --git a/packages/host-kit/src/internal/host-process-stop-confirm.test.ts b/packages/host-kit/src/internal/host-process-stop-confirm.test.ts index 4d59d70012..9352a994ee 100644 --- a/packages/host-kit/src/internal/host-process-stop-confirm.test.ts +++ b/packages/host-kit/src/internal/host-process-stop-confirm.test.ts @@ -1,5 +1,6 @@ import assert from 'node:assert/strict'; -import { afterEach, test, vi } from 'vitest'; +import { afterEach, beforeEach, test, vi } from 'vitest'; +import { mockLinuxProcessStatAbsent } from './host-proc.fixtures.ts'; // Every claim the stopper makes is about what the host answers, so one double answers both syscalls it // reads: `kill(pid, signal)` for the signals and liveness probes, and `ps` for the identity fields. @@ -317,6 +318,12 @@ function targetOf(model: HostCase): ProcessStopTarget { }; } +beforeEach(() => { + // The identity reader reads `/proc//stat` before the `ps` row, so an unmocked host can answer + // for the fixture pid. See host-proc.fixtures.ts. + mockLinuxProcessStatAbsent(); +}); + afterEach(() => { vi.useRealTimers(); vi.restoreAllMocks(); diff --git a/packages/host-kit/src/internal/owned-process-record.test.ts b/packages/host-kit/src/internal/owned-process-record.test.ts index 37b35e473c..5ca396f9ac 100644 --- a/packages/host-kit/src/internal/owned-process-record.test.ts +++ b/packages/host-kit/src/internal/owned-process-record.test.ts @@ -3,6 +3,7 @@ import fs from 'node:fs'; import path from 'node:path'; import { afterEach, beforeEach, test, vi } from 'vitest'; import { mkdtempForTestSync } from './tmp-dir.fixtures.ts'; +import { mockLinuxProcessStatAbsent } from './host-proc.fixtures.ts'; // The reaper stops through the shared escalation, so the double answers the two host syscalls it // reads: `kill(pid, signal)` for signals and liveness, and `ps` for the recorded identity. @@ -56,6 +57,9 @@ import { createOwnedProcessRecordStore } from './owned-process-record.ts'; import { reapOwnedProcessRecordsAtStartup } from './owned-process-reaper.ts'; beforeEach(() => { + // The identity reader reads `/proc//stat` before the `ps` row, so an unmocked host can answer + // for the fixture pid. See host-proc.fixtures.ts. + mockLinuxProcessStatAbsent(); processState.alive.clear(); processState.starts.clear(); processState.commands.clear();