fix(tests): stop sys.modules leaks + load AGENT_AUTH_SECRET in conftest#1381
Merged
dolho merged 1 commit intoJul 1, 2026
Merged
Conversation
Clears ~89 failures that a clean-install full-suite run surfaced as test-harness issues (not product regressions): conftest.py — load AGENT_AUTH_SECRET from .env alongside SECRET_KEY / INTERNAL_API_SECRET. start.sh auto-generates it (#1159); without it derive_agent_token() raises RuntimeError, failing every test that mints a per-agent auth token (circuit breaker, monitoring, watchdog, github_pat). test_github_pat_propagation_unit.py — the module installed fake services / database / services.docker_service into sys.modules permanently with no teardown, leaking into later files (test_whatsapp_adapter then failed importing container_get_archive from services.docker_utils). Confine the stubs to the `service` fixture via per-key save/restore (patch.dict's global sys.modules.clear() corrupts unrelated modules, so it is unusable here). Also accept the headers= kwarg the service now sends via build_agent_auth_headers() (#1159) so the httpx mock stops TypeError-ing into the catch-all (was masked by the AGENT_AUTH_SECRET error). test_monitoring_service.py — a second, independent polluter: it installed a bare fake services.docker_utils (no container_get_archive) permanently, the literal source of the WhatsApp "(unknown location)" ImportError. Confine the database / docker_service / docker_utils stubs to the module load; keep the real services.agent_client (used at test runtime). Validation: all 5 affected files (github_pat, whatsapp, watchdog, circuit_breaker, monitoring) = 168 passed, 0 failed, in both collection orderings. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
|
Resolve by running |
Contributor
Review — LGTM ✅Test-only harness fix, all checks green. Solid quality:
Improves isolation, no production code touched. Mergeable. |
dolho
approved these changes
Jul 1, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
A clean-install full-suite run surfaced 118 failures; the two largest clusters (~89 failures) were test-harness bugs, not product regressions. This PR fixes them. All are test-only changes (3 files).
Root causes & fixes
1.
AGENT_AUTH_SECRETnot loaded byconftest.pystart.shauto-generatesAGENT_AUTH_SECRETinto.env(#1159), butconftest.pyonly loadedSECRET_KEY/INTERNAL_API_SECRET. Without it,derive_agent_token()raisesRuntimeError, failing every test that mints a per-agent auth token (circuit breaker, monitoring, watchdog, github_pat propagation). → Added it to the.env-loading loop (31 failures).2.
sys.modulespollution intest_github_pat_propagation_unit.pyThe module installed fake
services/database/services.docker_serviceintosys.modulespermanently, with no teardown, leaking into later files —test_whatsapp_adapterthen failed importingcontainer_get_archivefromservices.docker_utils. → Confined the stubs to theservicefixture via per-key save/restore. (patch.dict's globalsys.modules.clear()corrupts unrelated modules, so it's unusable here.)3. Stale httpx mock in the same file (#1159 drift)
A second bug under the
AGENT_AUTHerror: the service now sendsheaders=build_agent_auth_headers(...), but the test's mock_get/_postdidn't acceptheaders→TypeErrorinto the catch-all. → Mock now accepts the kwarg.4. A second, independent
sys.modulespolluter intest_monitoring_service.pyIt installed a bare fake
services.docker_utils(nocontainer_get_archive) permanently — the literal source of the WhatsApp(unknown location)ImportError, independent of #2.monitoring + whatsappreproduced all 54 failures on its own. → Confined thedatabase/docker_service/docker_utilsstubs to the module load; kept the realservices.agent_client(needed at runtime). (Together #2–#4 = the 54-failure WhatsApp cluster.)Validation
Order-robust by construction — no permanent
sys.modulesleaks remain in these files.Scope
Test-only (
tests/). No product code touched. Remaining clean-install failures (stale assertions, unit-env hygiene, ~3 genuine product items) are out of scope and tracked separately.🤖 Generated with Claude Code