Skip to content

bug(tests): test_2638 TestApiKeyFallback::test_it_never_raises depends on xdist worker history — passes only while no API key is visible, reds unrelated PRs' regression diff #2856

Open

Description

@webmixgamer

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

  1. Make the test state its real precondition, e.g. monkeypatch.setattr("services.settings_service.get_anthropic_api_key", lambda: ""), so None no 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.
  2. Find and fix the polluter so the shared worker database / singleton is not left holding a key (a yield fixture that clears anthropic_api_key_encrypted, or monkeypatch instead of direct assignment).

Evidence

Acceptance Criteria

  • test_it_never_raises passes 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 diff job on an unrelated PR no longer flips red on this test

Activity

  1. added
    priority-p2
    type-bug
    theme-reliability
    complexity-low
    status-ready
    on Sep 16, 2026
  2. webmixgamer commented on Sep 16, 2026

    @webmixgamer
    ContributorAuthor

    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_KEY unset): test_it_never_raises passed (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.

  3. webmixgamer commented on Sep 16, 2026

    @webmixgamer
    ContributorAuthor

    Second data point: the full rerun of run 35124910789 (same seeds, same head) is green — pytest (head, seed 12345) passed and regression diff reports 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).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    complexity-lowpriority-p2status-readytheme-reliabilitytype-bug

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions