Skip to content

fix(tests): eliminate order-dependent sys.modules pollution (unblocks v0.8.0 release) - #1530

Merged
vybe merged 1 commit into
devfrom
fix/sys-modules-pollution-order-dependent-tests
Jul 8, 2026
Merged

vybe merged 1 commit into
devfrom
fix/sys-modules-pollution-order-dependent-tests

Conversation

@vybe

@vybe vybe commented Jul 8, 2026

Copy link
Copy Markdown
Contributor

Why

Cutting v0.8.0, the dev → main release PR (#1529) is the first time the backend-unit base/head regression matrix ran over the full payload (it only fires on PRs, not dev pushes). It surfaced 16 order-dependent failures in three new-on-dev unit files:

  • test_fork_to_own.py (9 — ImportError: … (unknown location))
  • test_73_stats_cache.py (6 — monkeypatch missed → assertion)
  • test_186_enumeration_uniformity::test_tier4_config_gets_are_access_gated (1)

All three pass in isolation but fail under full-suite ordering — classic sys.modules pollution. These are test-harness bugs, not product bugs: the product code (is_trinity_compatible, the fork-to-own owner guard, the #73 stats-cache invalidation) is intact.

The three vectors (each fix only restores real modules)

  1. test_voice_auth.py stubbed services.{docker_service,template_service,platform_audit_service} at module level and never restored them. The stubs are incomplete (no is_trinity_compatible / execute_command_in_container), so later files importing those symbols hit ImportError … (unknown location). → snapshot + restore after voice.py imports (mirrors the existing pattern in test_voice_tools.py).
  2. test_fork_to_own.py's crud_env teardown ran a manual del sys.modules['services.agent_service.*'] after patch.dict.stop() — which had already restored the pre-test snapshot. The delete left services.agent_service.stats absent, so test_73's module-level stats_mod went stale and its monkeypatch silently missed. → removed the redundant/harmful delete; patch.dict owns restoration.
  3. test_self_hosted_git_url.py's _reload_github_service() deletes+reimports services.github_service without restoring, leaking a fresh OwnerType enum; test_fork_to_own's owner guard then compares OwnerType by identity across the two module copies and skips (DID NOT RAISE HTTPException). → autouse fixture snapshots/restores services.github_service* per test.

Verification

Full unit suite green under all three CI seeds (12345 / 67890 / 99999): 3646 passed, 15 skipped each (locally on Python 3.14; CI runs 3.11 — the release PR re-run is the authoritative check).

Test-only changes (+60/-3). Merges into dev; unblocks the v0.8.0 release PR #1529.

@vybe
vybe enabled auto-merge (squash) July 8, 2026 20:15
…unit files

The base/head regression matrix (which only runs on PRs, not dev pushes) surfaced
16 order-dependent failures in test_fork_to_own / test_73_stats_cache /
test_186_enumeration_uniformity — all new-on-dev files that pass in isolation but
fail under full-suite ordering. Three distinct sys.modules-pollution vectors, all
test-harness bugs (product code is intact); each fix only restores real modules:

1. test_voice_auth.py stubbed services.{docker_service,template_service,
   platform_audit_service} at module level and never restored them. The stubs are
   incomplete (no is_trinity_compatible / execute_command_in_container), so later
   files importing those symbols hit "ImportError: ... (unknown location)". Now
   snapshots + restores them after voice.py imports (mirrors test_voice_tools.py).

2. test_fork_to_own.py's crud_env teardown ran a manual
   "del sys.modules['services.agent_service.*']" AFTER patch.dict.stop() — which
   had already restored the pre-test snapshot. The delete left
   services.agent_service.stats absent, so test_73's module-level "stats_mod"
   reference went stale and its monkeypatch missed. Removed the redundant/harmful
   delete; patch.dict owns restore.

3. test_self_hosted_git_url.py's _reload_github_service() deletes+reimports
   services.github_service without restoring, leaking a fresh OwnerType enum;
   test_fork_to_own's owner-guard then compares OwnerType by identity across the two
   modules and silently skips ("DID NOT RAISE HTTPException"). Added an autouse
   fixture that snapshots/restores services.github_service* per test.

Verified: full unit suite green under all three CI seeds (12345/67890/99999),
3646 passed / 15 skipped each (locally on py3.14; CI runs py3.11).

Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
@vybe
vybe force-pushed the fix/sys-modules-pollution-order-dependent-tests branch from 73a84e1 to 9cf6ec6 Compare July 8, 2026 20:25
@vybe
vybe merged commit 96dc8fa into dev Jul 8, 2026
17 checks passed
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