Skip to content

test(ui,mcp): count scanner work and pin ping deadlines instead of timing them - #5409

Merged
kovtcharov-amd merged 1 commit into
amd:mainfrom
kovtcharov:test/fix-flaky-turn-slot
Oct 20, 2026
Merged

kovtcharov-amd merged 1 commit into
amd:mainfrom
kovtcharov:test/fix-flaky-turn-slot

Conversation

@kovtcharov-amd

@kovtcharov-amd kovtcharov-amd commented Oct 20, 2026 •

Copy link
Copy Markdown
Collaborator

Two unit tests failed whenever a CI runner was busy, so unrelated PRs went red (the envelope test on #5282 and #5337; the ping test timed out on loaded machines). Before: the envelope "not quadratic" test raced a 5 s wall clock, and the ping test needed a ping to land in a 0.5 s window that the stub's own start-up ate into. After: both assert what they claim directly, so they pass or fail the same way on any machine. Under a 40-process CPU burn they went from 16/16 and 10/16 failing to 0 of 120. The third test from the report, test_turn_waiting_on_the_user_lets_other_chats_run, was already fixed on main by #5396. Both of its CI failures predate that fix, and it passed 325/325 under the same burn.

🔍 Technical details
  • Envelope test: the test now counts the characters _json_object_end examines. A 400,089-char envelope scans exactly 400,089 characters. A per-token rescan scans ~2e10 and fails the ≤ 2×len bound; I checked this on a 20 KB envelope, where a rescan was 2,500× over.
  • Ping test: the stub is now warmed with a first request so its boot time sits outside the deadline window. The test spies _write and asserts the ping reply gets the same deadline as the request. If the product gives the ping reply a fresh self.timeout, the test fails deterministically.
  • Not a product race: the remaining superlinear cost in the envelope path is self._stream_buffer += chunk copying the held buffer per token. That is microseconds per token at real generation rates.
  • Follow-ups: other tests in test_stdio_hung_server.py also fail under heavy load. test_redaction_stays_fast_on_dense_escapes and ..._linear_on_long_non_ascii_secrets use a < 0.25 s wall-clock assert. test_a_server_that_misses_the_connect_deadline_is_stopped and test_a_timed_out_request_is_cancelled_on_the_server read marker files before the stub writes them.

Test plan

  • pytest -p no:randomly --count 60 -n 8 on both fixed tests: 120/120 idle, 120/120 under a 40-process CPU burn (before: 16/16 and 10/16 failing under the same burn)
  • Adversarial: dropping the scanner's resume or giving the ping reply a fresh timeout makes the respective test fail
  • test_sse_handler.py, test_stdio_hung_server.py, test_turn_slot_reliability.py in full: 521 passed
  • python util/lint.py --black --isort

…ming them

Two tests failed whenever the CI runner was busy, turning unrelated PRs red.

- The envelope "not quadratic" test timed 100k streaming calls against a
  5 s wall clock. It now counts the characters the envelope scanner
  examines (exactly one pass: 400089 for a 400089-char envelope); a
  per-token rescan would be ~2e10 and fails the bound.
- The ping-deadline test needed the stub's ping to land in a 0.5 s window
  that the stub's own Python start-up ate into. The stub is now warmed
  with a first request, and the test asserts the ping reply is written
  with the request's own deadline rather than inferring it from elapsed
  time.
@github-actions github-actions Bot added the tests Test changes label Oct 20, 2026
@github-actions

Copy link
Copy Markdown
Contributor

Verdict: Approve

This PR fixes two unit tests that went red on busy CI runners. Both were checking timing, and now they check the behaviour directly. The envelope test counts how many characters the scanner reads instead of racing a 5 s clock. The ping test checks that the ping reply gets the request's own deadline, and it warms the stub up first so start-up time no longer eats into the window. Both new assertions still fail when the product bug they guard against comes back, so the tests lose no protection. There are no blocking issues.

Real-world evidence

N/A: this PR only changes tests, with no CLI, API, MCP or UI change. The verdict rests on static review plus the author's stress-run numbers in the test plan.

🔍 Technical details

Checked against the source

  • The monkeypatch actually takes effect. print_streaming_text's path calls _json_object_end as a module global (src/gaia/ui/sse_handler.py:311), so patching sse_module._json_object_end intercepts every call. resume[0] is the resume position (sse_handler.py:236), so len(text) - resume[0] is exactly the new work per call.
  • The recording_write signature matches _write(proc, message, deadline=None, honour_cancel=True) (src/gaia/mcp/client/transports/stdio.py:730). The ping reply is written with the request's deadline (stdio.py:890), and the request write passes the same deadline (stdio.py:698). So the equality assertion pins the invariant that matters.
  • The follow-up notifications/cancelled write lands under its own key and cannot overwrite "ping reply" or "tools/list". If the ping reply were never written, deadlines["ping reply"] raises KeyError, so that case fails loudly too.

Issues found

None blocking. 🟢 One optional point: the ping test no longer measures elapsed time at all, so a _write that ignored its deadline and waited forever would only be caught by the 15 s watchdog, not a tight bound. That trade-off is reasonable for a flake fix, and the "stopped reading" assertion still proves the write timed out.

Strengths

  • The len(scanned) > len(envelope) // 8 guard stops the bound from passing vacuously if the scanner is never called.
  • The PR description names the remaining wall-clock tests in the same file as follow-ups instead of widening this PR's scope.

@kovtcharov-amd
kovtcharov-amd added this pull request to the merge queue Oct 20, 2026
Merged via the queue into amd:main with commit 4217c62 Oct 20, 2026
26 checks passed
@kovtcharov-amd
kovtcharov-amd deleted the test/fix-flaky-turn-slot branch October 20, 2026 01:40
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

tests Test changes

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant