Skip to content

bug: test_inject_assigned_credentials leaks a stubbed services.agent_service.helpers into sys.modules, breaking runtime filtering in later tests #2140

Closed

Description

@vybe

Summary

tests/unit/test_inject_assigned_credentials.py installs stub modules directly into sys.modules (services.agent_service, services.agent_service.helpers, .read_only, .file_sharing, services.credential_encryption) at import time and never restores them. The stubs outlive the module.

services/compatibility/spec.py::applies_to_runtime() resolves its dependency lazily:

def applies_to_runtime(check, runtime):
    if not check.claude_only:
        return True
    from services.agent_service.helpers import is_claude_runtime   # <- picks up the stub
    return is_claude_runtime(runtime)

so any test that runs after test_inject_assigned_credentials gets the stubbed is_claude_runtime and loses claude_only filtering.

Reproduction

python3 -m pytest tests/unit/test_inject_assigned_credentials.py \
                  tests/unit/test_compatibility_checks.py \
                  -q -p no:randomly -k "codex_runtime_omits or inject"
E  AssertionError: assert 'C-001' not in {'A-001', ..., 'C-001', ...}
FAILED tests/unit/test_compatibility_checks.py::TestBuildReport::test_codex_runtime_omits_claude_only_checks

Each file passes on its own. Confirmed pre-existing on dev (reproduced in a clean dev worktree), and confirmed still present on the #2137 branch — it is not caused by either.

Why it matters

The repo runs pytest-randomly, so file order varies per run. This is therefore a latent CI flake, not a deterministic failure: it only fires when the polluting module happens to sort before test_compatibility_checks.py. The test it breaks (test_codex_runtime_omits_claude_only_checks) guards the #1187 multi-runtime property that Codex/Gemini agents are not flagged with false HARD CLAUDE.md findings — so the flake masquerades as a real compatibility regression.

Suggested fix

Either restore the pre-existing entries in a fixture teardown (monkeypatch.setitem(sys.modules, ...) gets this for free), or scope the stubbing to the tests that need it rather than doing it at module import.

Acceptance Criteria

  • sys.modules is restored after test_inject_assigned_credentials completes
  • The reproduction command above passes
  • A guard asserts no test leaves a stub for services.agent_service.helpers in sys.modules (or the stubbing moves to monkeypatch.setitem, which makes leakage structurally impossible)

Found while working #2137.

Activity

  1. added
    priority-p3
    type-bug
    theme-devex
    complexity-low
    status-ready
    on Aug 12, 2026
  2. vybe commented on Aug 12, 2026

    @vybe
    ContributorAuthor

    Extra context on why CI is green today despite this.

    tests/lint_sys_modules.py (the lint (sys.modules pollution check) job, #762) is baseline-ratcheted — it blocks new bare sys.modules mutations but permits pre-existing ones. test_inject_assigned_credentials.py is in that baseline, so the lint passes on it by design.

    Backend unit CI also runs pytest as a base-vs-head matrix at 3 seeds each (backend-unit-test.yml, seeds 12345/67890/99999). That is what keeps this from being a blocking red: when a seed orders the polluting module first, the failure appears on base and head alike and is correctly read as pre-existing. It also means the flake is genuinely reachable in CI, not just locally — it simply never looks like a regression.

    So the two mechanisms that should have caught it each have a principled reason not to. The fix is still the one above: restore sys.modules (or switch to monkeypatch.setitem, which makes leakage structurally impossible and would also let the file come off the #762 baseline).

  3. added
    status-in-progress
    and removed
    status-ready
    on Aug 13, 2026
  4. self-assigned this
    on Aug 13, 2026
  5. added a commit that references this issue on Aug 13, 2026
    f26e5f8
  6. added a commit that references this issue on Aug 13, 2026
  7. added
    status-in-dev
    and removed
    status-in-progress
    on Aug 13, 2026
  8. removed
    status-in-dev
    on Aug 17, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

complexity-lowpriority-p3theme-devextype-bug

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions