From c2315e236dae40f337a3bcfd59ce7bb1b1bb13f8 Mon Sep 17 00:00:00 2001 From: Alain Schlesser Date: Tue, 17 Nov 2026 09:18:43 +0000 Subject: [PATCH] fix(ci): name the starved phase in a native discovery timeout receipt (#4149) The #4149 confirmation timed out in native Vitest file discovery on a runner whose checkout and install also ran about five times slower than usual. The same two-parent candidate tree discovers in about 5 seconds in the pinned image with two CPUs, so discovery did not regress. The receipt could not show that: it recorded only wall time and the stderr tail. A failure receipt now records the discovery leader's user and system CPU time and involuntary context switches, with the runner's parallelism and load average. CPU near the deadline means slow discovery; CPU far below it means the runner withheld CPU or I/O. Reading the usage cannot replace the discovery failure. The 30-second cap, project selection, file channel, process-group ownership and pipe drains are unchanged. Co-Authored-By: Claude Opus 5.5 --- docs/decisions/ci-utility.md | 10 +++++++++- scripts/proof-inputs.ts | 16 +++++++++++++++- tests/browser-inventory-process.test.ts | 15 +++++++++++++++ 3 files changed, 39 insertions(+), 2 deletions(-) diff --git a/docs/decisions/ci-utility.md b/docs/decisions/ci-utility.md index 1259ab7613..c09676054c 100644 --- a/docs/decisions/ci-utility.md +++ b/docs/decisions/ci-utility.md @@ -1094,7 +1094,7 @@ workflow. Discovery has a fixed 30-second wall-clock maximum for the native child to exit -and both output pipes to drain (`browserInventory`, `scripts/proof-inputs.ts:55-113`), +and both output pipes to drain (`browserInventory`, `scripts/proof-inputs.ts:56-127`), in local preflight and hosted metadata. The maintainer selected option A under [#2380](https://github.com/schlessera/brain-kit/issues/2380) on 2026-10-22: #2326's cold discovery sample took 14,811 ms in the pinned offline @@ -1102,6 +1102,14 @@ image with two CPUs, so 30 seconds allows approximately twice that observed duration. Hosted metadata steps had completed in 6–11 seconds. This bounded margin does not guarantee completion under arbitrary CPU starvation; a stuck child can take up to 15 seconds longer to fail than under the previous maximum. +A failure receipt records the discovery child's CPU time and involuntary +context switches beside its wall time, with the runner's parallelism and load +average, so a timeout names its phase: CPU near the deadline is slow discovery, +CPU far below it is a runner that withheld CPU or I/O +([#4149](https://github.com/schlessera/brain-kit/issues/4149)). The #4149 +timeout came before these fields and was a starved runner: its checkout and +install also ran about five times slower than usual, and the same candidate +tree discovered in about 5 seconds in the pinned image with two CPUs. Process controls inject a short deadline and assert the default separately; the integration test allows additional time for ownership scanning and cleanup. Discovery failure still rejects unsuccessful inventories: selection then keeps diff --git a/scripts/proof-inputs.ts b/scripts/proof-inputs.ts index da583cdf90..a7952fafe9 100644 --- a/scripts/proof-inputs.ts +++ b/scripts/proof-inputs.ts @@ -2,6 +2,7 @@ * proof jobs and the merge path. */ import { createHash } from "node:crypto"; import { mkdirSync, readFileSync, rmSync, statSync } from "node:fs"; +import { availableParallelism, loadavg } from "node:os"; import { dirname, join, relative } from "node:path"; export interface Spec { project: string; file: string; } @@ -101,8 +102,21 @@ export async function browserInventory(root: string, deadlineMs = 30000): Promis } catch (error) { stopDiscovery(); await p.exited; + // The leader's CPU time against wall time names the starved phase (#4149): + // CPU near the deadline is slow discovery; far below it, the host withheld + // CPU or I/O. Involuntary switches and load average corroborate contention. + // Bun reports these as BigInt despite its `number` types; reading them never + // replaces the discovery failure this receipt explains. + let cpu: { userMs: number; systemMs: number; involuntarySwitches: number } | null = null; + try { + const usage = p.resourceUsage(); + if (usage) cpu = { userMs: Math.round(Number(usage.cpuTime.user) / 1000), systemMs: Math.round(Number(usage.cpuTime.system) / 1000), + involuntarySwitches: Number(usage.contextSwitches.involuntary) }; + } catch { cpu = null; } const receipt = { elapsedMs: Math.round(performance.now() - started), deadlineMs, timedOut, - exitCode: p.exitCode, signal: p.signalCode ?? null, ...(cleanupError ? { cleanupError } : {}), stdout, stderr, inventory: inventoryPublication(output) }; + exitCode: p.exitCode, signal: p.signalCode ?? null, ...(cleanupError ? { cleanupError } : {}), cpu, + host: { parallelism: availableParallelism(), loadavg: loadavg().map(load => Math.round(load * 100) / 100) }, + stdout, stderr, inventory: inventoryPublication(output) }; throw new Error(`Vitest file discovery failed: ${JSON.stringify(receipt)}\n${error instanceof Error ? error.message : String(error)}`); } finally { clearTimeout(timer); diff --git a/tests/browser-inventory-process.test.ts b/tests/browser-inventory-process.test.ts index 8d97601bb8..f26b576f89 100644 --- a/tests/browser-inventory-process.test.ts +++ b/tests/browser-inventory-process.test.ts @@ -63,6 +63,21 @@ test.each([false, true])("native watchdog reports publication %s and reaps the d expect(receipt.elapsedMs).toBeGreaterThanOrEqual(900); expect(receipt.elapsedMs).toBeLessThan(5000); }); +// #4149: a hosted timeout must say whether discovery was computing or the +// runner starved it, instead of leaving the cause to be inferred from stderr. +test.each([ + ["waiting", "setInterval(()=>{},1000);"], + ["computing", "for(;;){}"], +])("native watchdog receipt separates a %s child's CPU time from wall time", async (phase, script) => { + const { receipt } = diagnostics(await nativeControl(script, controlDeadlineMs)); + expect(receipt).toMatchObject({ deadlineMs: 1000, timedOut: true, signal: "SIGKILL" }); + expect(receipt.cpu).toEqual({ userMs: expect.any(Number), systemMs: expect.any(Number), involuntarySwitches: expect.any(Number) }); + const cpuMs = receipt.cpu.userMs + receipt.cpu.systemMs; + if (phase === "waiting") expect(cpuMs).toBeLessThan(receipt.elapsedMs / 2); + else expect(cpuMs).toBeGreaterThan(receipt.elapsedMs / 4); + expect(receipt.host.parallelism).toBeGreaterThanOrEqual(1); + expect(receipt.host.loadavg).toHaveLength(3); +}); test("native signal remains distinct from watchdog expiration", async () => { const { receipt } = diagnostics(await nativeControl('process.kill(process.pid,"SIGTERM");', controlDeadlineMs)); expect(receipt).toMatchObject({ deadlineMs: 1000, timedOut: false, signal: "SIGTERM", inventory: { present: false } });