Skip to content

test(core): make MCP event tests deterministic - #65868

Merged
rekram1-node merged 1 commit into
devfrom
mcp-test-flakes
Nov 23, 2026
Merged

rekram1-node merged 1 commit into
devfrom
mcp-test-flakes

Conversation

@rekram1-node

Copy link
Copy Markdown
Member

"MCP events > backs off failed opens, resets after active, and resumes with a replay gap once" fails now and then on Linux CI. In the last 400 failed check-test runs, plus the first attempts of runs that were rerun, I found 7 failures in the MCP events suite. All 7 were this test, all failed on the same line (waited(2000, secondFailure)), and all measured a gap between 1318ms and 1551ms where the test expects at least 1600ms. Example: https://github.com/anomalyco/opencode/actions/runs/45211338655

Why

The test reads the clock as soon as the client sees a failed open, and assumes the next backoff delay started at that time. The failed open is a real HTTP round trip. TestClock.adjust("1200 millis") wakes the first reopen at about 1000ms, then waits only for Effect work it can track, not for HTTP I/O. On a busy runner, the server's error reply sometimes arrived while the adjust was still running. The client then started the 2000ms delay from the earlier clock time (around 850 to 1150ms), but the test read the clock after the adjust, at 1200ms, so the gap looked up to 400ms too short.

I reproduced it locally by keeping the adjust busy right after the reopen: the old test failed 5 of 10 runs with the same errors as CI (1245ms to 1580ms).

What changed

Only the test changed. The test server now holds each failed open's reply until the test calls failOpen. The test calls it after each adjust returns, so the failure, and the backoff delay it starts, always happen at the clock time the test reads. I did not change any timeouts or add retries, and production code is unchanged.

Testing

From packages/core, with bun run script/test.ts test/mcp.test.ts:

  • -t "MCP events" --rerun-each 40: 280 of 280 passed (40 runs of each of the 7 tests).
  • 4 copies of that in parallel: 1120 of 1120 passed.
  • 10 copies of -t "backs off failed opens" --rerun-each 60 in parallel on a 10 core machine: 600 of 600 passed.
  • The whole file, --rerun-each 3, 2 copies in parallel: 684 of 684 passed.
  • With the same busy-adjust setup that made the old test fail 5 of 10 times, the new test passed 10 of 10.

bun typecheck in packages/core and bun script/lint-changed.ts origin/dev pass.

@opencodejr opencodejr Bot 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.

I reproduced the flake on the base branch. Under CPU load, 29 of 200 runs of the test failed on waited(2000, secondFailure), with gaps from 1211 to 1592ms, the same as CI. With this change and the same load, 200 of 200 passed. The whole MCP events suite also passes, and tsc -b for packages/core is clean.

The fix is sound. The failed reply is released only after each adjust returns, so the backoff starts at the clock time the test reads. The queue is unbounded, so releasing before the request arrives is safe, and failedOpens is only used by this test.

Public API is unchanged (test-only change).

No concerns.

@rekram1-node
rekram1-node merged commit 292cb5a into dev Nov 23, 2026
11 checks passed
@rekram1-node
rekram1-node deleted the mcp-test-flakes branch November 23, 2026 17:49
beyondChang pushed a commit to beyondChang/opencode that referenced this pull request Nov 24, 2026
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