Skip to content

fix: Quiesce the MCP call-log dispatcher between tests - #5036

Merged
Sinity merged 1 commit into
masterfrom
fix/test-order-pollution
Sep 13, 2026
Merged

Sinity merged 1 commit into
masterfrom
fix/test-order-pollution

Conversation

@Sinity

@Sinity Sinity commented Sep 13, 2026

Copy link
Copy Markdown
Owner

Advances polylogue-7lhl5. One confirmed order-dependent failure fixed; four
others did not reproduce and were deliberately left unfixed rather than
papered over.

Summary

Quiesce the process-global MCP call-log dispatcher between tests, so its worker
thread stops writing into a later test's tmp_path.

Why this matters now

PR #5031 made the corpus collect as one unit, so cross-test pollution —
previously invisible, because every focused selection was green — will now appear
in the campaign's final full-corpus gate, where it is indistinguishable from a
product regression.

The leak

tests/unit/storage/test_archive_tier_init.py::test_tier_prototype_is_atomically_published_and_restored_read_only
fails assert 2 == 1 on an os.replace count.

Minimal reproducer, bisected from a four-directory run → tests/unit/mcp → first
8 files → one file:

devtools test tests/unit/mcp/test_contract_evidence.py \
              tests/unit/storage/test_archive_tier_init.py -p no:randomly

Failed 3 of 4 attempts; passes alone. No seed involved — it is a thread race, not
a shuffle artifact.

Instrumenting the assertion caught the culprit: the extra os.replace was
.api-auth-token.XXXX.tmp → api-auth-token, landing inside the tier-init
test's own
tmp_path/xdg-data/polylogue/.

Owner is polylogue/mcp/call_log.py's module-level _DISPATCHER — a
process-global singleton whose daemon thread rescans every registered outbox root
every 0.25 s until deliveries are acknowledged. No test daemon acknowledges them,
so the thread outlives the MCP test that started it, and _read_spooled_delivery
calls resolve_api_auth_token(...), which resolves api_auth_token_path() /
XDG_DATA_HOME at delivery time. The tier-init test monkeypatches
os.replace globally, so the surviving worker's token mint was attributed to
prototype publication.

Production-owned, but not a product bug

The dispatcher's process-lifetime thread is correct for a long-lived MCP
server whose environment is fixed. It is only wrong for a process that re-points
XDG between units of work — which is the test process. So the production route
was not weakened.

Added _McpCallLogDispatcher.quiesce() / reset_mcp_call_log(): stops and joins
the worker, raises if it will not stop, clears roots/retry/counters/wake queue,
and leaves it restartable — unlike the existing shutdown(), which is
terminal. Called from the existing autouse singleton-reset fixture in
tests/conftest.py, beside reset_blob_store and reset_status_snapshot.

Anti-vacuity — verified, not asserted

  • test_quiesce_stops_the_worker_clears_routing_and_allows_restart goes red if
    _roots.clear() or the _stop.clear()/_thread = None restart path is
    dropped.
  • test_global_dispatcher_is_quiesced_before_every_test goes red if the conftest
    call is removed and the file runs after any file that invokes an MCP tool.
    Confirmed by commenting the call out and watching it fail, then restoring it.

polylogue/mcp/call_log.py is out of the derived schema-identity closure, so
there is no reconvergence implication.

Four cases did not reproduce — and no fix was invented for them

test_mcp_call_log.py, test_insight_export_bundles.py,
test_session_insight_rebuild_progress.py, and
test_daemon_cli.py::test_run_daemon_services_stops_live_watcher_on_failure were
green in both large combined runs: 8,620 tests
(tests/unit/mcp core storage daemon) and ~9,200 tests
(tests/unit/api archive cli context coordination plus the four targets), both
with -p no:randomly.

Worth recording for whoever runs the final gate: the test_mcp_call_log cluster
is mechanistically consistent with the leak fixed here —
test_readiness_surface_exposes_outbox_pressure asserts on _DISPATCHER's
cumulative delivery_failures/wakeups_dropped and its registered roots, all of
which the global singleton carried across tests before this commit. That is
not claimed as verified.

Verification

devtools test tests/unit/mcp        PASSED
reproducer after fix                4/4 green
anti-vacuity probe                  red with the reset removed
devtools verify --quick             exit 0, all gates ok

Receipts: 20260913T214033Z-…-1609768-bae48713 (first repro),
…T224543Z-…-1825795 (instrumented, showing the token replace),
…T225139Z-…-1954749 (fixed), …T225233Z-…-1975853 (tests/unit/mcp),
…T225912Z-…-2116330 (9k combined), …T231042Z-…-2353742 (anti-vacuity red).

🤖 Generated with Claude Code

https://claude.ai/code/session_01DYUNBGoE652b3nfmq3CtqK

The process-global `call_log._DISPATCHER` owns a daemon thread that keeps
scanning every registered outbox root until its deliveries are acknowledged.
No test daemon acknowledges them, so the thread outlives the test that started
it. Its delivery path resolves `api_auth_token_path()` -- and therefore
`XDG_DATA_HOME` -- at delivery time, so once a later test re-points XDG the
surviving worker mints an API bearer token inside that test's tmp_path.

That is a correct design for a long-lived MCP server process, whose
environment is fixed; it is wrong for a process that re-points XDG between
units of work. Rather than weaken the production route, give the dispatcher an
explicit `quiesce()` that stops the worker, clears roots/retry/counters and
leaves it restartable, and call it from the existing autouse singleton-reset
fixture beside `reset_blob_store` and `reset_status_snapshot`.

Problem: tests/unit/storage/test_archive_tier_init.py::
test_tier_prototype_is_atomically_published_and_restored_read_only counts
`os.replace` calls through a global monkeypatch, so the leaked worker's
`api-auth-token` replace was attributed to prototype publication.

Reproduction at 14e1d82:
  devtools test tests/unit/mcp/test_contract_evidence.py \
      tests/unit/storage/test_archive_tier_init.py -p no:randomly
failed 3 of 4 attempts before this change and passes 4 of 4 after.

Verification:
  devtools test tests/unit/mcp -> PASSED
  devtools test tests/unit/mcp/test_mcp_call_log.py -> PASSED

Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
Claude-Session: https://claude.ai/code/session_01DYUNBGoE652b3nfmq3CtqK
@chatgpt-codex-connector

Copy link
Copy Markdown

You have reached your Codex usage limits for code reviews. You can see your limits in the Codex usage dashboard.
To continue using code reviews, add credits to your account and enable them for code reviews in your settings.

@coderabbitai

coderabbitai Bot commented Sep 13, 2026

Copy link
Copy Markdown

Warning

Review limit reached

Next included review available in 33 minutes.

Check out review usage here.

View limit details

Limit details: You’ve used the included review currently available.

You've used all free OSS reviews for now. Wait for the free limit to reset to keep reviewing this public repository.

Learn how review limits work.

Review configuration:

⚙️ Run configuration

Configuration used: Organization UI

Review profile: ASSERTIVE

Plan: Advanced

Run ID: 4fe730db-846a-472f-9d94-5e4b934c4fe6

📥 Commits

Reviewing files that changed from the base of the PR and between a538d84 and b8e9c73.

📒 Files selected for processing (3)
  • polylogue/mcp/call_log.py
  • tests/conftest.py
  • tests/unit/mcp/test_mcp_call_log.py

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@Sinity
Sinity merged commit 7eb083d into master Sep 13, 2026
3 checks passed
@Sinity
Sinity deleted the fix/test-order-pollution branch September 13, 2026 23:46
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