Skip to content

fix(tests): stop sys.modules leaks + load AGENT_AUTH_SECRET in conftest#1381

Merged
dolho merged 1 commit into
devfrom
fix/test-harness-sysmodules-leaks-and-agent-auth-secret
Jul 1, 2026
Merged

fix(tests): stop sys.modules leaks + load AGENT_AUTH_SECRET in conftest#1381
dolho merged 1 commit into
devfrom
fix/test-harness-sysmodules-leaks-and-agent-auth-secret

Conversation

@vybe

@vybe vybe commented Jun 30, 2026

Copy link
Copy Markdown
Contributor

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_SECRET not loaded by conftest.py
start.sh auto-generates AGENT_AUTH_SECRET into .env (#1159), but conftest.py only loaded SECRET_KEY / INTERNAL_API_SECRET. Without it, derive_agent_token() raises RuntimeError, 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.modules pollution in 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. → Confined the stubs to the service fixture via per-key save/restore. (patch.dict's global sys.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_AUTH error: the service now sends headers=build_agent_auth_headers(...), but the test's mock _get/_post didn't accept headersTypeError into the catch-all. → Mock now accepts the kwarg.

4. A second, independent sys.modules polluter in test_monitoring_service.py
It installed a bare fake services.docker_utils (no container_get_archive) permanently — the literal source of the WhatsApp (unknown location) ImportError, independent of #2. monitoring + whatsapp reproduced all 54 failures on its own. → Confined the database / docker_service / docker_utils stubs to the module load; kept the real services.agent_client (needed at runtime). (Together #2#4 = the 54-failure WhatsApp cluster.)

Validation

Scenario Result
github_pat alone 11 passed
whatsapp alone 67 passed
monitoring alone 13 passed
circuit_breaker + monitoring (Redis reachable) 47 passed
All 5 affected files 168 passed, 0 failed
All 5, reversed collection order 168 passed, 0 failed

Order-robust by construction — no permanent sys.modules leaks 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

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>
@github-actions

Copy link
Copy Markdown

⚠️ Nightly unit-suite check skipped — merge conflict against dev.

Resolve by running git merge dev locally and pushing the result. The next nightly run will re-test once the conflict is gone.

@dolho

dolho commented Jul 1, 2026

Copy link
Copy Markdown
Contributor

Review — LGTM ✅

Test-only harness fix, all checks green. Solid quality:

Improves isolation, no production code touched. Mergeable.

@dolho
dolho merged commit 3324fae into dev Jul 1, 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.

2 participants