Skip to content

Tests sleep 64s of wall-clock time on every push; durations are guessed, not derived #2679

Closed

Description

@malibio

Overview

Frontend tests wait on real wall-clock time via bare setTimeout sleeps, with durations chosen as "comfortably large" rather than derived from what is actually being awaited. Repo-wide that is 64.1 seconds of deliberate sleeping across 26 files (193 sleeps) — paid on every test:all, and therefore on every push, since the pre-push gate (ADR-047) is the sole CI here.

The padding is also what makes the suite fragile under load: a sleep sized by guesswork has no stated margin, so nobody can tell when it is close to its budget.

How this surfaced

A CSS-comment-only push failed the gate:

FAIL src/tests/services/ai-chat-occ-conflict-regression.test.ts
  > keeps the flat shape when the payload arrives nested under properties
Error: Test timed out in 5000ms.

Unhandled Error: [vitest-worker]: Timeout calling "resolveSnapshotPath"

That run took 1243s versus the usual ~85s on a 14-core machine running five parallel agent sessions in isolated worktrees, several executing their own gates (test:all + multi-core cargo build) concurrently. All 4 tests pass in isolation in 4.76s; the retry on a quiet machine passed.

Calibration: it took roughly five concurrent gates to trigger. This is "no headroom under heavy parallelism", not "flakes routinely" — the priority is the wasted minute on every push, with flake-resistance as the by-product.

The pattern, with examples

Sleeps sized by guesswork rather than by the thing awaited:

shared-node-store-coverage.test.ts:559 and :625 — a mocked updateNode sleeps 10,000ms to simulate a hang, so the test can assert that a 1000ms timeout fires:

vi.spyOn(backendAdapter, 'updateNode').mockImplementation(async () => {
  await new Promise(resolve => setTimeout(resolve, 10000)); // Longer than timeout
  return { ...mockNode, version: 2 };
});
// ...
const failed = await store.flushAllPendingSaves(1000);

The mock only has to outlive 1000ms. The extra 9s buys nothing — and the sleep never completes anyway, since the timeout resolves first and the timer is simply abandoned. That single file totals 29,450ms.

ai-chat-occ-conflict-regression.test.ts:123,180,213,236 — four 1000ms sleeps to outwait SharedNodeStore.DEBOUNCE_MS = 500 (shared-node-store.svelte.ts:101). 2x the debounce is defensible margin; four real seconds against a 5000ms per-test budget is what left no headroom.

Where the time is: 35 sleeps of ≥600ms account for 49.8s of the 64.1s. The remaining 158 sleeps contribute only 14.3s. A fix targeting those 35 captures ~78% of the cost in a small, reviewable change.

Proposed Solution

  1. Fake timers where a known delay is being awaited — vi.useFakeTimers() + await vi.advanceTimersByTimeAsync(600). Removes the wall-clock dependence entirely. Note shared-node-store.svelte.ts:123 documents that its flush path holds live setTimeout timers "without settling their promises at all", so the async variant is required, not the sync one.
  2. Where a real sleep must stay, derive it — size it from the constant it outwaits (DEBOUNCE_MS, the timeout under test) and say so in a comment, rather than picking a round number. The 10,000ms-to-beat-1000ms cases should simply shrink.
  3. Prefer waiting on the condition over the clock — vi.waitFor/polling where a state change is the real signal.

Do not raise the global testTimeout — #2293 explicitly rejected that as hiding the same class of problem everywhere else.

Acceptance Criteria

  • The 35 sleeps of ≥600ms are converted to fake timers, derived durations, or condition waits
  • Total deliberate sleep time in packages/desktop-app/src/tests is materially below the current 64.1s (measure before/after; record in the PR)
  • Any remaining real sleep carries a comment naming the constant it is sized against
  • ai-chat-occ-conflict-regression.test.ts and shared-node-store-coverage.test.ts specifically no longer depend on wall-clock duration
  • Full test:all passes with several other gates running concurrently (the reproduction condition: ~5 parallel sessions on 14 cores)
  • No behavioral change to the code under test — these are test-only edits

Technical Specifications

  • Scope: packages/desktop-app/src/tests/**/*.ts, the setTimeout(resolve, N) pattern
  • Biggest offenders by total sleep: shared-node-store-coverage (29,450ms), resync-direct-call-queued-write-regression (5,200), persistence-serial-writer-regression (4,600), ai-chat-occ-conflict-regression (4,000), shared-node-store-skip-while-editing (3,500)
  • Run with bun run test, never bun test — the latter bypasses the Happy-DOM Vitest config
  • Precedent for the fix shape: 43b52ec2 (core-plugins viewer-resolution test sits at 3.5s of a 5s timeout and fails the pre-push gate under load #2293) moved expensive work out of the measured window instead of raising the limit

Related

Prior instances of the same class, all filed and fixed individually: #2293, #2233, #2431, #2601, #2402, #2224, #1975, #2020. That this keeps recurring is the argument for fixing the pattern rather than the next instance.

Two structural follow-ups worth considering separately (not in scope here):

Notes

No product defect here — the OCC conflict behavior and the store's flush/timeout logic are correct. This is test design.

Activity

  1. changed the title [-]ai-chat OCC conflict regression test flakes under load (5x timer headroom blocks the push gate)[/-] [+]Tests sleep 64s of wall-clock time on every push; durations are guessed, not derived[/+] on Sep 17, 2026
  2. malibio commented on Sep 17, 2026

    @malibio
    CollaboratorAuthor

    Baseline (worktree issue-2679-test-sleep-budget): Frontend 246/247 files passed, 5083/5085 tests, 171.49s. The 2 failures are both in src/tests/performance/architecture-benchmarks.test.ts and are CPU-contention artifacts — the sidecar build was running concurrently. That file passes 13/13 in 1.07s on a quiet machine. Noting it here because it is the same load-sensitivity this issue is about, just in a perf assertion rather than a sleep.

  3. self-assigned this
    on Sep 17, 2026
  4. malibio commented on Sep 17, 2026

    @malibio
    CollaboratorAuthor

    PR #2691 opened. Measured results: literal sleep time in packages/desktop-app/src/tests went 64,105ms -> 11,905ms (-81%). Full frontend suite 171.49s -> 59.31s; the vitest 'tests' figure 104.04s -> 46.28s. 248/248 files and 5088/5088 tests pass. The pre-push gate (test:all + cargo build + test:e2e) ran green while three other sessions were building concurrently, which is the contention condition that produced the original failure. Approach: durations now derive from constants mirroring the private production values, with a drift-guard test that fails loudly if those mirrors ever diverge; mocks that needed to 'never settle' use a never-resolving promise instead of a long sleep; two mock sleeps that existed to order two writes are now explicit release gates; and an eslint rule fails any test sleeping >500ms on a numeric literal so this does not recur. ~153 short sleeps (<=500ms, 11.9s total) are deliberately left alone as not worth the churn.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions