Skip to content

fix: remove the timing races behind three intermittently failing tests - #214

Merged
laithalsaadoon merged 1 commit into
mainfrom
fix/test-flakes
Sep 23, 2026
Merged

laithalsaadoon merged 1 commit into
mainfrom
fix/test-flakes

Conversation

@laithalsaadoon

Copy link
Copy Markdown
Contributor

Three tests failed intermittently on CI or on a loaded box while the same tree passed alone. Each failure was a race between the test and something it did not control, and none was a wrong answer from the code under test. This PR removes the races.

Test What raced Fix
apps/cli/tests/integrations.test.ts > integrations doctor > completes the live MCP handshake The handshake probe (packages/integrations/src/doctor.ts) had a 5 s bound for strictly more work than the 10 s version probe: the spawned server must load the CLI's module graph and open and migrate the sandbox store's database before it can answer initialize. Under the full mise run check on a 2-vCPU runner that cold start exceeded 5 s (2026-09-14 twice locally, CI attempt 1 of #207 on 2026-09-22). HANDSHAKE_TIMEOUT_MS 5 s -> 20 s with the reasoning in its doc comment; the test's vitest timeout 30 s -> 45 s so a slow box fails on the doctor's own row rather than on vitest's clock. A moved entry script, a missing binary, an exiting server or a wrong serverInfo.name still fail immediately through the error/exit/name branches.
packages/store/tests/store.test.ts > compensates an INTERRUPTED archive The test used Effect.timeout(200) while its comment said the interruption lands at the hanging commit. On a slow runner 200 ms landed inside git mv or git add; the abort killed that child, Effect.onError started restore at once, and its first step git reset raced the dying child for .git/index.lock, lost with exit 128, and the whole restore was abandoned with the renamed file still on disk (CI attempt 1 of #211, 2026-09-23: ENOENT ... areas/inbox/a-remembered-fact.html). The fake commit succeeds a Deferred when reached and then hangs. The test forks the archive with Effect.forkDetach, awaits the Deferred, interrupts the fiber with Fiber.interrupt, and asserts Exit.hasInterrupts on its exit. The interruption now lands at the commit on every box, which is the production case the comment describes (client timeout after the rename is staged). The four original assertions (bytes restored, no archived file, clean tree, no new commit) are unchanged.
apps/consolidator/tests/mount.test.ts > removes the temp parent it created countSnapshots counted every memhtml-corpus-snapshot-* entry in tmpdir(), and reachability.test.ts creates and removes directories under the same prefix in a parallel vitest worker (5 before, 3 after on 2026-09-08). The test records basename(parent) for each of its two cycles and asserts none of those names remains in tmpdir(). The mutation the comment names (dropping rm(parent) from release) still leaves both names and fails.

Each test was run alone on this head and passes; the full local gate result is in the first comment.

One latent hazard the store flake exposed is left for a separate PR: in production an MCP client timeout that lands mid-git mv produces the same uncompensated half-archive, because the interrupt does not wait for the aborted git child to exit before restore spawns git reset. Returning a cleanup effect from spawnGit's Effect.callback that waits for the child's close would close that window; it needs its own test and a check of rc.115's cleanup semantics, so it is not folded in here.

Authority: Laith's 2026-09-23 grant in #bonk-place (bring deps to latest aggressively and address flakes) covers merging on green.

Each of these failed on CI or on a loaded box while the same tree passed alone;
none was a wrong answer from the code under test.

- integrations doctor "completes the live MCP handshake": the handshake probe
  had a 5 s bound for strictly more work than the 10 s version probe (the
  server must load the CLI's module graph and open and migrate the store's
  database before it can answer initialize). HANDSHAKE_TIMEOUT_MS is now 20 s
  and the test's own vitest timeout 45 s, above the sum of both probe bounds.
  Failed 2026-09-14 (twice, locally under a11y/Lighthouse load) and on CI for
  #207 on 2026-09-22.

- store "compensates an INTERRUPTED archive": the test interrupted on a 200 ms
  wall clock while claiming to interrupt at the hanging commit. On a slow
  runner 200 ms landed inside `git mv` or `git add`; the aborted child still
  held .git/index.lock when the compensation's `git reset` ran, so restore
  failed as a whole and the moved file was never put back (CI for #211,
  2026-09-23: ENOENT on the first readFile). The fake commit now signals a
  Deferred when reached and hangs; the test forks the archive, waits for the
  signal, interrupts the fiber, and asserts the exit carries an interrupt.
  The interruption lands at the commit on every box, which is the production
  case the comment names.

- consolidator "removes the temp parent it created": the census counted every
  memhtml-corpus-snapshot-* entry in tmpdir(), which reachability.test.ts
  creates and removes in a parallel worker (5 before, 3 after on 2026-09-08).
  The census now reads only the two names this case created; dropping the
  rm(parent) from release still leaves both and fails the assertion.

Merged under the standing authority Laith granted on 2026-09-23 for addressing
flakes.
@laithalsaadoon

Copy link
Copy Markdown
Contributor Author

Local gate on this head (fd59693): mise run check green, 68 of 68 tasks in 214 s. Each of the three tests was also run alone on this head and passed (handshake 3.7 s, INTERRUPTED archive 0.9 s, temp parent 0.5 s). biome check clean on the four files. Commit signature verified by the GitHub API.

Copilot AI lite review requested due to automatic review settings September 23, 2026 15:00

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot was unable to review this pull request because the user who requested the review has reached their quota limit.

@laithalsaadoon
laithalsaadoon merged commit 3b2a068 into main Sep 23, 2026
11 checks passed
@laithalsaadoon
laithalsaadoon deleted the fix/test-flakes branch September 23, 2026 15:07
laithalsaadoon added a commit that referenced this pull request Oct 20, 2026
…nalizers run (#216)

The abort signal handed to execFile sends SIGTERM when the fiber is
interrupted, but the child is still alive for a moment afterwards, and if it
is `git mv` or `git add` it still holds .git/index.lock. Interruption then
proceeded straight to the caller's finalizers, and store.ts's compensation
opened with `git reset -q -- <paths>` against a lock that had not been
released: exit 128, the whole restore abandoned, and the half-done mv left on
disk with nothing to put the memory back. An MCP client timeout landing
mid-archive is exactly this path in production; the same race surfaced as a
CI flake in store.test.ts on 2026-09-23 (fixed on the test side in #214).

spawnGit's Effect.callback register now returns a cleanup effect that resolves
on the child's exit (or close, for a spawn that never started), which
Effect.callback runs and awaits on an interrupting cause before the cause
propagates (effect 4.0.0-rc.115 internal/effect.js asyncFinalizer). The
signal stays, so a git that ignores SIGTERM is still killed.

Test: blocks a real `git commit -a` on a hand-made refs/heads/<branch>.lock
with core.filesRefLockTimeout=-1, interrupts the fiber, and asserts the pid is
gone and index.lock absent by the time Fiber.interrupt returns, then that the
compensation's `git reset` succeeds. Removing the wait fails it 3 of 3.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants