Skip to content

test(scaffald): bound the two loop-guard tests instead of racing a clock - #915

Merged
thinkclay merged 1 commit into
mainfrom
fix/bound-the-loop-guard-tests
Sep 26, 2026
Merged

thinkclay merged 1 commit into
mainfrom
fix/bound-the-loop-guard-tests

Conversation

@thinkclay

Copy link
Copy Markdown
Contributor

Addresses the part of #753 I could pin down.

profile-view-tracking and results-view-xp each keep a "the old shape really did loop" case beside the fix, so the guard above it cannot pass vacuously. That instinct is right — a guard test that cannot fail is worth nothing. The mechanism was the problem: render the broken component, wait a fixed 120 ms, assert the effect re-entered more than 20 times.

The loop is not slow

I measured it before changing anything:

window=60ms  calls=6143
…then: Error: Worker exited unexpectedly

6,143 iterations in 60 ms. > 20 was never the risk — the runaway was. Letting that run for a fixed wall-clock window is enough to take the vitest worker down with it, which is reported as STACK_TRACE_ERROR against whichever test happened to be in flight.

That is the #753 flake, and both suites hit it tonight on unrelated PRs, each passing on a plain re-run with no code change:

PR Test
#908 profile view tracking > the old shape really did loop
#870 results view XP > the old shape really did loop

Two sightings of the same construction in one evening is not ambient flakiness.

Why dependency PRs specifically: CI runs Test (affected), and a lockfile change makes every project affected — so apps/scaffald's suite runs when an ordinary feature PR would not. Both sightings were on dep PRs, and each initially read as a regression from the bump. That cost real time on #908 before I re-ran it.

The assertion never needed a clock

Knowing the effect re-enters itself with no terminating condition is what the test is for, and 25 iterations establish that as well as six thousand. unbounded-loop-probe.ts counts calls, stops the loop the instant it reaches LOOP_CAP, and resolves. Fast machine or slow, the work is identical and nothing runs away. The two cases went from 120 ms+ to ~11 ms.

shouldStop is the test's bound, not a fix to the shape under test — the effect still has no terminating condition of its own, and the probe only trips it once the loop has already proved itself. I'd rather that be explicit than have the looping component quietly stop being a faithful reproduction.

The guard still guards

This is the half worth reviewing. A stall is now the interesting failure: it means the old shape stopped looping, so react-query changed its identity semantics and the reasoning behind the fix needs revisiting. settled() races the loop against a 2 s deadline so that case is a readable assertion rather than a suite-level timeout.

Verified by mutation rather than by argument — swapped Looping's unstable mutation object for the stable mutate, so it no longer loops:

× the old shape really did loop — the guard above is not vacuous
  → expected 'stalled' to be 'looped'

It fails, clearly, in 2010 ms. The 2 s is only paid when the test is already about to fail.

Verification

  • The pair, six consecutive runs: 6/6 green every time.
  • scaffald-app full suite: 98 passed / 0 failed.
  • scaffald-app typecheck and lint clean; pnpm prepush green.

Scope

#753 was filed as "two different projects failed and passed on re-run within a day, with no local reproduction." This is a concrete, reproducible cause for the apps/scaffald half, so I'd keep #753 open until another clean stretch goes by — I can't claim these two tests were the only thing behind it.

🤖 Generated with Claude Code

…ock (#753)

`profile-view-tracking` and `results-view-xp` each keep a "the old shape
really did loop" case beside the fix, so the guard above it cannot pass
vacuously. Both did it by rendering the broken component, waiting a fixed
120 ms, and asserting the effect had re-entered more than 20 times.

That made the result a function of how fast the machine was, and the loop is
not slow. Measured locally it re-enters the effect **6,143 times in 60 ms**.
Letting that run for a fixed window is enough to take the vitest worker down
with it — `Error: Worker exited unexpectedly`, surfaced as
`STACK_TRACE_ERROR` against whichever test happened to be in flight.

That is the #753 flake. Both suites failed exactly that way tonight on
unrelated PRs and passed on a plain re-run:

    #908  profile view tracking > the old shape really did loop
    #870  results view XP      > the old shape really did loop

They surface on dependency PRs specifically because CI runs
`Test (affected)`, and a lockfile change makes every project affected — so
`apps/scaffald`'s suite runs when an ordinary feature PR would not. Each one
first read as a regression from the bump.

## The assertion never needed a clock

Knowing the effect re-enters itself without a terminating condition is what
the test is for, and 25 iterations establish that as well as six thousand.
`unbounded-loop-probe.ts` counts calls, stops the loop the instant it reaches
`LOOP_CAP`, and resolves. Fast machine or slow, the work is the same and
nothing runs away. The two cases now finish in ~11 ms rather than 120 ms+.

`shouldStop` is the *test's* bound, not a fix to the shape under test: the
effect still has no terminating condition of its own, and the probe only
trips it once the loop has already proved itself.

## The guard still guards

A stall is now the interesting failure — it means the old shape stopped
looping, so react-query changed its identity semantics and the reasoning
behind the fix needs revisiting. `settled()` races the loop against a 2 s
deadline so that case is a readable assertion rather than a suite timeout.

Verified by mutation: replacing `Looping`'s unstable mutation object with the
stable `mutate` makes it fail with

    AssertionError: expected 'stalled' to be 'looped'

rather than passing. The 2 s is only paid when the test is about to fail.

Ran the pair six times: 6/6 green each time. `scaffald-app` full suite 98
passed / 0 failed; typecheck and lint clean.

Co-Authored-By: Claude Opus 5 <[email protected]>
@thinkclay
thinkclay merged commit afab7e9 into main Sep 26, 2026
9 checks passed
@thinkclay
thinkclay deleted the fix/bound-the-loop-guard-tests branch September 26, 2026 20:22
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.

1 participant