Skip to content

test: run two trace budget tests on the virtual clock so a slow runner cannot flake them - #59

Merged
gshost1 merged 2 commits into
mainfrom
test/trace-budget-windows-flake
Nov 8, 2026
Merged

gshost1 merged 2 commits into
mainfrom
test/trace-budget-windows-flake

Conversation

@gshost1

@gshost1 gshost1 commented Nov 8, 2026

Copy link
Copy Markdown
Owner

What and why

Two core unit tests failed intermittently on "Test (Windows)" (x86_64-pc-windows-msvc) on PRs that do not touch crates/core/src/trace/ (PR #6 head 6f4306d; PR #54 head 4e5e0a5, run 42498487874, job 128785895630). Both pass on Windows ARM64, macOS and Linux. Test-only change; no product code touched.

Both tests are now deterministic on the existing VirtualClock (crates/core/src/wait/deadline.rs), the same clock contended_artifact_lock_preserves_dispatch_budget already uses.

Timing analysis

slow_pre_capture_preserves_dispatch_budget (TIMEOUT "Target did not become actionable within the wait budget").

  • Action budget: 500 ms of real clock. The pre-capture deadline is that budget less the 100 ms dispatch reserve, capped at 1 s, so 400 ms.
  • The adapter slept 50 ms of real time. The nominal spend before dispatch is 50 ms, so the margin was 500 - 50 - 100 = 350 ms, and everything else on the path (session and trace dir creation, the poll loop, the lease, lock file I/O) also ran against that 350 ms in real time.
  • A loaded Windows runner (2 test threads, slow file creation, oversleeping timers) can spend more than 350 ms there, so the action deadline expires before dispatch and the poll loop reports the timeout. That is the runner's speed, not the property.
  • Property guarded: a slow pre capture must not starve the dispatch (the capture deadline is capped, leaving the reserve).

artifact_capture_budget_supports_deadline_bound_screenshot_backends (left: 1, right: 2).

  • Budgets: action 5 s (Deadline::standard), pre and post captures capped at 1 s, the backend needs at least 250 ms remaining, and the artifact lock wait is a fixed 50 ms (ARTIFACT_LOCK_WAIT_MS).
  • FileLock::acquire opens the lock file first and then checks deadline.is_expired(), so the 50 ms real-time window also covers opening the lock file. On a slow Windows filesystem that open can exceed 50 ms, the capture is skipped with lock_failed, and one of the two screenshots is lost. The 50 ms wait is deliberate in the product (see the comment in trace_artifact_budget.rs) and is left alone.
  • Property guarded: the capture budgets leave a deadline-bound backend at least 250 ms.

Changes

  • trace_artifacts_dispatch_budget_tests.rs: freeze the virtual clock; the first screenshot calls crate::wait::deadline::sleep(deadline.remaining()), so it consumes its whole capped deadline (400 ms, virtual). New assertions, which are stricter than before: the dispatch receives exactly the 100 ms reserve, and exactly 400 ms of virtual time elapsed. The old was_capped branch with a real 50 ms sleep is gone.
  • trace_artifacts_more_tests.rs: freeze the virtual clock in the one test, so the lock wait cannot expire and remaining budgets are exact.

How tested

All through serial_cargo.sh, one command at a time, macOS arm64.

  • Each changed test, 200 runs in a loop with cargo test -p agent-desktop-core --lib -- --exact <path>, RUST_TEST_THREADS=2: 200/200 and 200/200.
  • Same loops under load: 16 yes CPU burners at normal priority on 8 cores, load average about 50: 200/200 and 200/200.
  • cargo build --tests -p agent-desktop-core, then cargo test -p agent-desktop-core --lib: 1827 passed, 0 failed.
  • cargo clippy -p agent-desktop-core --all-targets -- -D warnings: clean. cargo fmt --all -- --check: clean.
  • cargo check -p agent-desktop-core --all-targets --target x86_64-pc-windows-msvc: clean.
  • bash .githooks/pre-commit over staged changes: passed. Committed with the hook bypassed afterwards (--no-verify) because it had just run.

Invert-verify (product code temporarily broken, test watched fail, restored):

  • ref_action/action_artifacts.rs: pre_capture_deadline returns Some(parent) instead of parent.capped(capture_budget). slow_pre_capture_preserves_dispatch_budget fails with Timeout "Operation exceeded its deadline".
  • TRACE_CAPTURE_BUDGET_MS changed from 1000 to 200. artifact_capture_budget_supports_deadline_bound_screenshot_backends fails with left: 0, right: 2.

Risks

  • Not reproduced here: I could not make the old tests fail on macOS, even with 100 runs of each under the same CPU load (100/100 pass). The Windows failure comes from slow file I/O and timer behavior that CPU load alone does not recreate, so the analysis above is from reading the code against the failure messages. The fix removes the dependence on real time rather than relying on a reproduction. Please re-run Test (Windows) on this PR a few times.
  • The first test now consumes the full capped deadline instead of a short sleep, so it exercises a harsher case than before.

…r cannot flake them

Both tests asserted budgets that real time could spend. On the Windows CI
runner the real-time budget was consumed by file I/O and oversleeping, not by
the property under test.

slow_pre_capture_preserves_dispatch_budget gave the whole action 500 ms of
wall clock and slept 50 ms for real inside the pre capture. It now freezes the
virtual clock, makes the capture consume its entire capped deadline (400 ms)
and asserts the dispatch receives exactly the 100 ms reserve, which is
stricter than before.

artifact_capture_budget_supports_deadline_bound_screenshot_backends lost a
capture when opening the artifact lock file took longer than the fixed 50 ms
lock wait, which is real time on a slow runner. It now runs on the virtual
clock, so the lock wait cannot expire and the 250 ms backend minimum is
checked against exact remaining budgets.
@gshost1
gshost1 marked this pull request as ready for review November 8, 2026 19:51
@gshost1
gshost1 merged commit 7921e3f into main Nov 8, 2026
13 checks passed
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