Repository navigation
Description
Summary
tests/unit/test_2638_subscription_switch_on_turn.py::TestApiKeyFallback::test_it_never_raises failed on PR #2855's pytest (head, seed 12345) leg while passing on the other two head seeds and on all three base seeds, and the regression diff job flagged it as a new failure. The PR changes no backend code and nothing the unit job collects: git diff --name-only origin/dev <head> -- tests/unit src/backend is empty, and pytest unit/ --collect-only -p randomly --randomly-seed=12345 yields byte-identical orders for the PR head and dev (16502 items, the test at position 6928 on both sides). Same tree, same order, one red and one green: the outcome depends on what else ran in that xdist worker (-n auto --dist loadfile assigns whole files to workers; the composition varies run to run).
Assertion (junit artifact junit-head-12345)
AssertionError: assert {'switched': True, 'fallback': 'api_key', 'agent_name': 'scribe', 'old_subscription': 'A', ...} is None
Why the test is fragile
The test makes db.get_setting_value raise and asserts fallback_to_api_key("scribe") returns None. But is_api_key_fallback_enabled() fails open by design (src/backend/services/subscription_auto_switch.py: "Fail-OPEN (enabled) on a read error"), so the function proceeds past the raising read. The None the test expects actually comes from the next guard, get_anthropic_api_key() being empty, a precondition the test never states. Any earlier test in the same worker that leaves a key visible to settings_service.get_anthropic_api_key() (a stored anthropic_api_key_encrypted row in the worker's shared TRINITY_DB_PATH database, an un-restored attribute on the settings-service singleton, or ANTHROPIC_API_KEY in os.environ) turns the switch on, and the test fails with exactly the dict above (old_subscription: 'A' is the test's own fixture data).
Excluded locally
Each candidate file was run before the failing test in one process with ANTHROPIC_API_KEY unset; every pair passes:
- environment writers:
test_1999_execution_env.py,test_reload_token_endpoint.py,test_2114_subscription_shadow_guard.py - stored-key writers:
test_ent435_secret_settings.py,test_2572_credentialless_adoption.py - direct
settings_service.get_anthropic_api_key = MagicMock(...)assignments (they land on stubbed modules the unit conftest restores):test_2069_gitignore_at_creation.py,test_fork_to_own.py,test_ent123_tokenless_clone.py,test_1484_create_agent_characterization.py,test_1759_local_template_not_found.py
So the polluter is not identified yet. A full-suite run with CI's exact xdist shape and seed (-n auto --dist loadfile -p randomly --randomly-seed=12345 -v, so the [gwN] prefixes name the files that shared the failing test's worker) is the next step.
Suggested fix
- Make the test state its real precondition, e.g.
monkeypatch.setattr("services.settings_service.get_anthropic_api_key", lambda: ""), soNoneno longer depends on worker history, and add the case the fail-open contract implies: read raises and a key is present ⇒ the fallback still runs. - Find and fix the polluter so the shared worker database / singleton is not left holding a key (a
yieldfixture that clearsanthropic_api_key_encrypted, ormonkeypatchinstead of direct assignment).
Evidence
- Run https://github.com/Abilityai/trinity/actions/runs/35124910789 (
regression diffjob;pytest (head, seed 12345)), on PR fix(mcp): a denied tool call is audited as a refusal, not a success (#2807) #2855. - The test was added by fix(subscriptions): a rate-limited turn completes on another subscription (#2638) #2645 (2026-09-13) and has no prior report.
Acceptance Criteria
-
test_it_never_raisespasses regardless of which files precede it in a worker, and states the precondition it relies on - The polluting test (if one exists) restores the settings-service state it changes
- The
regression diffjob on an unrelated PR no longer flips red on this test
Activity
webmixgamer commentedon Sep 16, 2026
Local reproduction attempt (one run at the PR #2855 head, CI's shape:
-m "not slow" -p randomly --randomly-seed=12345 -n auto --dist loadfile, 10 workers,ANTHROPIC_API_KEYunset):test_it_never_raisespassed (worker gw4). Consistent with the worker-history hypothesis — the composition that exposes it is the one CI happened to draw on run 35124910789's seed-12345 head leg. The CI run was re-triggered in full; if the head leg draws the same composition again the artifact will carry the second data point.webmixgamer commentedon Sep 16, 2026
Second data point: the full rerun of run 35124910789 (same seeds, same head) is green —
pytest (head, seed 12345)passed andregression diffreports no new failures. Two executions of an identical tree and identical collection order, one red and one green, is the worker-composition signature; the polluter is still to be identified (the local xdist run also passed).