Plan: the suites hold under CPU contention

Closes: #621, #622 (U2); the per-package issues U1 files (U2, U3). Relates to: #604, #612, #616 (the same class, already fixed one at a time), #487 (a blocked event loop turns a mouse report into an Escape: the code-side case the lone-escape test guards), #281 (platform testing). Branches: test/load-harness, test/load-fixes-<package> (one per package), fix/<deadline> per code finding. Status: Done (2026-11-01) — U1 the load-test harness, on ambient load only; U2 pi-modes' suite, with #639 and #644; U3 nothing to change, luchs and panther found nothing in pi-remote or pi-ssh; U4 the deadline audit on #622, its two findings #633 and #634

Design

Why the suites fail under load

The box that validates on Windows (panther) runs the gesso fleet: six to eight headless pi workers, 80-100% CPU for hours. Under that, a package’s suite that passes alone in seconds fails in one of four ways. Counted across packages//test/.test.ts with the count tool (2026-10-31):

Shape Count What it asserts, really

Sleep then assert: await sleep(n) / settle(n) then expect

169

That a timer or a process finished within n ms. lone-escape.test.ts:109 sleeps twice ESCAPE_TIMEOUT_MS and expects the Escape; at 100% CPU the timer had not fired (#622).

Poll with a fixed deadline: until(check, ms) (defaults 5 s, 8 s, 3 s, 2 s per file), vi.waitFor at its 1 s default

184

That async work finishes within the deadline. judge-wiring-callback.test.ts waited 1 s for a real git read (#616); monitor-registry.test.ts 8 s for three serialized Git Bash spawns (#621).

A spawned process given a short budget: timeoutMs: 5_000 and the like

15

That Git Bash starts and runs in that budget. monitor-process.test.ts got timedOut: true with the right output (#622).

Cleanup racing a child still running

a handful

rmSync on a temp dir a child holds: EPERM (#612, #622 item 3).

Only 11 tests use vi.useFakeTimers. Timer logic (debounce, backoff, the Escape timeout, mouse-tail windows, title coalescing) runs against the wall clock in the rest.

A per-test timeout is the right bound for a hang; it is already 60 s on Windows and 5 s on Linux (vitest.config.ts:25). Every inner window below it is a second, tighter bet on the scheduler, and that is what fails.

The cleanup race has a second cause the harness will not show as a timing: in monitor-tools.test.ts, afterEach (lines 78-83) stops only the registries a test pushed onto registries, and the tests that go through registerMonitorTools (line 434 on) never push theirs; they stop their source with f.fire("session_shutdown") at the end of the test body (line 458). When until throws first, the source is still running when rmSync runs: EPERM (#622 item 3). That is a structural leak, fixed by registering the registry for cleanup, not by a wider window.

Principles

  • Wait on the event, not the clock. A test that needs an async outcome awaits it (a promise the stand-in settles, a close event, vi.waitFor with the per-test timeout as its bound). The per-test timeout catches a hang. #616 and #619’s tests are the pattern (hub.test.ts:108: until = vi.waitFor(…​, { timeout: 10_000, interval: 5 })).

  • Timer logic on fake timers. Code whose behaviour is "after n ms, X" is tested with vi.useFakeTimers() and vi.advanceTimersByTime(n): exact, instant, load-proof. Not where a real child process or socket is in the loop.

  • A negative window may stay short. "Nothing happens within 50 ms" can only pass falsely under load, never fail falsely. Those stay; the plan names them so nobody widens them.

  • A process budget is for a hang, so it is generous. A test that gives a child 5 s on Windows is asserting the spawn cost of Git Bash; the budget becomes the Windows per-test bound’s order (30-45 s) or the test waits on the exit event.

  • Cleanup after the child is gone. afterEach awaits the children it started (exit event, or a tree kill by pid on Windows as connection.test.ts now does) before rmSync.

  • No new gate, no new config. The harness is a scratch script under tools/, run by hand and by pi-dev on Linux; validate is unchanged. The one shared helper is a test helper, not a package surface.

Risks

  • A widened window hides a real slowdown (silent): a wait that now allows 45 s would also pass a regression that made the work take 30 s. Mitigation: a wait is widened only where the work is a process start or real git under load; timer logic goes to fake timers, which assert the exact delay; and the per-test timeout stays as it is, so a hang is still caught.

  • A negative window passes falsely under load (fail-open for the assertion, never a false red): "nothing happens within 50 ms" cannot fail because the box is slow, but under load it may pass when something would have happened at 60 ms. Mitigation: each such window is named in a comment at its site; where the behaviour is timer-driven the test moves to fake timers and the window becomes exact. No test pins this property; it is a reading of the assertion’s direction.

  • The harness leaves busy-loop workers behind (silent, and it would slow the box for hours): on an interrupt or a vitest crash. Mitigation: the workers are started with detached: false, killed in a finally, and again on SIGINT/SIGTERM; the script prints their pids at start so an operator can check with Win32_Process.

  • A fake-timer rewrite tests a different thing (silent): a test moved to fake timers no longer exercises the real event loop, which is what #487’s bug is about. Mitigation: lone-escape.test.ts keeps its real-socket case (lines 43-99) on the wall clock as an integration case with the per-test bound, and only the pure timeout assertions (lines 100-122) move.

  • The audit files issues nobody owns (silent): mitigation is in U4’s done-when: every filed issue has a named owner before U4 closes.

Every change is test-only unless U4 files it; a test-only change is test: and carries no release. Each fix’s category: a timing fix that waits on the event is a regression test for the flake it replaces (the flake’s own case, now unable to fail falsely); a fake-timer rewrite is a contract test on the delay; the cleanup fix in monitor-tools.test.ts is a concurrency fix (a child racing rmSync) and its test is the existing case run under the harness.

What the harness is

tools/load-test.mjs (new, ~80 lines): starts N busy-loop workers (node -e 'for(;;){}', default os.cpus().length), runs one package’s vitest with --coverage.enabled=false and --reporter=json --outputFile, kills the workers (in a finally, and on SIGINT/SIGTERM), and prints the failed test names and their durations. A --repeat k runs the suite k times and reports what failed in any run. On Windows the workers are the same node loops; no stress dependency. Usage: node tools/load-test.mjs <package dir> [--repeat k] [--workers n]. It is a tool for a developer’s hand, documented as one more row in local-dev.adoc’s verbs table (the table that holds `validate at line 44; there is no separate Windows section); validate does not call it.

The harness runs before and after each package’s fixes, so the fix list is the harness’s output, not a guess, and "fixed" means the suite passed --repeat 3 under full contention on panther and on luchs.

The code-side audit

The same tests exercise production deadlines. A deadline whose budget load can exceed, and whose expiry drops legitimate work, is a fix: issue of its own (Scope rule: not this plan’s diff). Candidates read from packages//.ts (2026-10-31), to be confirmed by the harness or by reading:

  • pi-modes/links.ts:21 GIT_TIMEOUT_MS = 1_000: an origin lookup for issue links; on expiry, no links. Harmless.

  • pi-modes/session-facts.ts:15 GIT_REMOTE_TIMEOUT_MS = 2000: the push’s facts for the gate; on expiry the gate asks without them. Worth a look: a 2 s git on a loaded box is plausible.

  • pi-modes/monitor-process.ts:31 KILL_GRACE_MS = 2_000: a monitor’s child gets 2 s after SIGTERM before the kill. Harmless (a kill follows).

  • pi-remote/member.ts:40-47 HANDOVER_MAX_WAIT_MS = 1_500, ATTACH_WINDOW_MS = 2_000, ATTACH_TIMEOUT_MS = 5_000: a member attaching to a hub on a loaded box. Worth a look: an attach that times out under load is a session off the phone, as #619 showed one way.

  • pi-modes/judge-wiring.ts:308 JUDGE_LATENCY_FLOOR_MS = 250: a floor, not a ceiling. Harmless.

  • The lone-Escape code path (#487, ESCAPE_TIMEOUT_MS in pi-tui): the test that flaked guards real behaviour under a blocked loop. The runtime patch stays until upstream fixes it; the test moves to fake timers.

Each is one line in the audit’s result: harmless (why), or an issue number.

Files

  • New: tools/load-test.mjs; one row in `docs/modules/ROOT/pages/local-dev.adoc’s verbs table.

  • New: packages/pi-modes/test/until.ts, exporting until(check: () ⇒ boolean, label?: string): Promise<void>: polls every 20 ms with no deadline of its own, so the per-test timeout is the bound; label names what was waited for in the timeout’s message. It replaces the three local helpers (monitor-process.test.ts:59, default 5 s; monitor-registry.test.ts:84, 8 s; monitor-tools.test.ts:424, 6 s). The existing shared file test/harness.ts is the session harness and is not where a wait helper belongs.

  • Test files, as the harness names them. From the five issues already: packages/pi-modes/test/lone-escape.test.ts, monitor-process.test.ts, monitor-registry.test.ts, monitor-tools.test.ts, judge-wiring-callback.test.ts (done), packages/pi-ssh/test/connection.test.ts (done).

  • CHANGELOG.adoc == Unreleased: one entry per package branch, under === Changed ("the suite holds under CPU contention"), and one per code fix under === Fixed.

Scope

Erratum (2026-10-31): the harness is a task verb, node tools/task.mjs load-test, implemented in tools/lib/load-test.mjs, not a standalone tools/load-test.mjs: ADR-001 makes task.mjs the single automation entrypoint. validate still does not call it.

Erratum (2026-10-31, the operator): no artificial load. The busy-loop workers are removed: the harness runs a package’s suite repeatedly under the box’s own load only, since faked contention can break tests the real load never does and it lands on other work’s measurements. Runs where others measure the box are announced on the bus beforehand, with their duration.

U1: the harness (test/load-harness)

tools/load-test.mjs as designed. Run it on panther (pi-win) for every package with tests, --repeat 3, and on luchs (pi-dev) the same. The two lists are the fix list: one issue per package the harness names, test(<package>): the suite under CPU contention, listing file, test name, which box, and which shape from the table. pi-modes' list goes on #622, which already holds three of its items. Expected: pi-modes most, pi-remote and pi-ssh some, the pure packages none.

Done when: the script runs on both platforms, local-dev.adoc has its row, and each package the harness named has its issue.

U2: pi-modes (test/load-fixes-pi-modes)

Closes #621 and #622. By shape:

  • Timer logic to fake timers: lone-escape.test.ts’s pure timeout cases (lines 100-122: the real Escape after the timeout, and the `sleep(80) at line 120 that waits for a split report to reassemble) advance a fake clock past ESCAPE_TIMEOUT_MS; its real-socket loop (lines 43-99) stays on the wall clock with the per-test bound. Any mouse-tail, title, monitor-stream case the harness names that is not already on fake timers follows.

  • Waits on the event: every until(…​) call in the three monitor files, and any other the harness names, goes through the new test/until.ts (no deadline of its own). #621’s 45 s at `monitor-registry.test.ts:230 becomes that call too.

  • Process budgets: monitor-process.test.ts:191 timeoutMs: 5_000 and its kin become 30_000 on win32 and stay on Linux (the test asserts the outcome, not the spawn cost; the per-test bound is 60 s there).

  • Cleanup: the registerMonitorTools tests in monitor-tools.test.ts (line 434 on) push the registry they create onto registries, so afterEach (line 79) stops its sources before rmSync whether or not the test body reached session_shutdown.

  • Negative windows named in a comment and left.

Done when: load-test.mjs --repeat 3 on pi-modes passes on panther at full contention and on luchs; nothing was skipped or weakened (the reflection pause’s J2); #621 and #622 closed by the MR, each with its merge SHA noted.

U3: pi-remote and pi-ssh (test/load-fixes-pi-remote, test/load-fixes-pi-ssh)

The same by shape, from the harness’s list, each package on its own branch with its own until.ts where it has more than one local helper. Known: pi-remote/test/member.test.ts:30 and hub-e2e.test.ts:77 until defaults (2 s, 3 s); pi-ssh/test/resilience.test.ts, link.test.ts, ports.test.ts default-window vi.waitFor calls (held under load on 2026-10-31 at 4 runs each; the harness decides).

Done when: as U2, per package; each package’s issue from U1 closed by its MR.

U4: the code-side audit

Read each candidate above and any deadline a harness failure points at. For each, the question is what expiry does: a dropped optional (links, facts) is fail-open and harmless if the caller says so; a dropped attach or a killed legitimate child is fail-closed and gets an issue. Result: one comment on #622 with a line per deadline (harmless, why; or fix: #N filed). Each filed issue is its own branch and MR, outside this plan.

Done when: the comment is posted and every filed issue has a branch owner (pi-win for Windows-specific, pi-dev otherwise).

Split

pi-win: U1’s script and its panther runs, U2, U4’s Windows-specific items. pi-dev: U1’s luchs runs, U3, U4’s cross-platform items; the Linux check and landing of every branch, as today. Order: U1 first (both), then U2 and U3 in parallel, U4 as the harness points.

Out of scope

A CI matrix (#281), a new validate step, changes to the per-test timeout, any production deadline (each is its own issue), and the 169 sleeps the harness does not name: a sleep that holds under load is not this plan’s.

Edit this page · latest