… issue #276/#287)
tests_py/conftest.py was 501 lines — over both the repo's local 300-line
cap and the global 500-line cap (coding-standards.md §4.1) — discovered
while fixing #287 (a small, additive-only touch there would have left the
pre-existing violation unaddressed and unfiled, which §14 forbids without
a citing PR; this is that PR).
Split along the file's own pre-existing section-comment boundaries, into
three new modules with zero behavior change — closures over conftest.py's
module globals became explicit-parameter functions (an incidental DI
improvement, not the goal):
- tests_py/_pg_throwaway_db.py — per-process throwaway PG database
creation/cleanup (create_isolated_test_database, drop_isolated_database,
drop_dead_orphaned_databases, maintenance_url).
- tests_py/_pg_safety_guards.py — the two fail-closed data-safety guards
(guard_against_populated_db, the anti-537k-row-deletion guard from
incident 2026-06-10; guard_against_real_data_roots, from issue #219) plus
looks_like_test_db and _resolved_real_data_roots.
- tests_py/_domain_mapping_isolation.py — the dev-root filesystem-scan
isolation from issue #196.
conftest.py itself drops from 501 to 281 lines: the pytest hooks
(pytest_sessionfinish, the autouse _test_isolation fixture) and the
module-level bootstrap sequence stay in place, now delegating to the
extracted functions in the exact same order.
Two existing regression-guard test files hard-coded the guard's prior
location/signature (scanning conftest.py's raw source, or calling a
zero-arg closure) and broke on the move — fixed to import the real,
now-standalone modules directly, which also let them drop the old
exec-from-source-text workaround entirely. Both, plus the new tests added
for the extracted modules, then pushed test_conftest_guard.py (496 lines
pre-existing) and test_store_isolation.py (350 pre-existing) over the
same cap; split each along its natural per-function boundary rather than
leave a second freshly-introduced violation:
test_conftest_guard.py -> test_looks_like_test_db.py +
test_guard_against_populated_db.py + test_guard_blocks_populated_db.py +
test_guard_against_populated_db_structural.py
test_store_isolation.py -> test_store_isolation.py (trimmed) +
test_real_data_root_guard.py
Mutation-tested (scoped, CORTEX_TEST_ISOLATE_DB=0 to keep the per-process
throwaway-DB bootstrap itself out of the mutated call graph): 204/211
mutants killed; 7 survivors are provable-equivalent split/rsplit/maxsplit
choices (documented at each use site, verified by construction and by
property fuzzing — one is domain-scoped to the exact
cortex_test_pw<pid>_<hex> format the function is only ever called
against, not a general claim).
Full local suite: 6854 passed, 5 skipped, 116 subtests passed — no
regressions.
Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_012Yu6EnWspTfqHoGkExyS6u
Summary
tests_py/conftest.pywas 501 lines — over both the repo's local 300-linecap and the global 500-line cap (coding-standards.md §4.1) — discovered
while fixing #287, whose own PR (#308) touches this exact file (a small,
additive-only guard-wiring change). Per §14 (Boy-scout), a defect seen in
touched material must be fixed in the same PR or deferred to its OWN PR
with that PR cited in the report — this is that PR, cited in #308's Boy-scout
section.
What changed — behavior-preserving split
Split along the file's own pre-existing section-comment boundaries into
three new modules, zero behavior change: closures over conftest.py's
module globals became explicit-parameter functions (an incidental DI
improvement, not the goal — these are independently unit-testable now,
which the file's own prior state was not).
tests_py/_pg_throwaway_db.pycreate_isolated_test_database,drop_isolated_database,drop_dead_orphaned_databases,maintenance_urltests_py/_pg_safety_guards.pyguard_against_populated_db(anti-537k-row-deletion, incident 2026-06-10) andguard_against_real_data_roots(issue #219), pluslooks_like_test_dband_resolved_real_data_rootstests_py/_domain_mapping_isolation.pyconftest.pydrops from 501 to 281 lines. The pytest hooks(
pytest_sessionfinish, the autouse_test_isolationfixture) and themodule-level bootstrap sequence stay in place — pytest hooks/fixtures must
be defined in a conftest.py itself to be auto-discovered — now delegating
to the extracted functions in the exact same order as before.
A real regression this split would have caused, and how it was avoided
Two existing regression-guard test files hard-coded the guard's prior
location/signature — one scanned
conftest.py's raw source text andexec'd the extracted lines into a hand-built namespace; the other calleda zero-arg closure directly — and both broke outright on the move
(
StopIteration/AssertionErroron a stale structural check). Fixed toimport the real, now-standalone modules directly, which also let them
drop the old exec-from-source-text workaround entirely (that workaround
existed only because the functions used to be closures inside a file that
ALSO did heavy bootstrapping at import time; the extracted modules import
only
os/pytest, so a direct import is safe).Those fixes, plus the new unit tests written for the newly-extracted
functions (previously exercised only implicitly by the full test-session
bootstrap), then pushed both of those test files over the same
300-line cap (
test_conftest_guard.py: 496 lines pre-existing onorigin/main;test_store_isolation.py: 350 pre-existing) — rather thanleave a second, freshly-introduced-by-me violation, each was split along
its natural per-function boundary:
test_conftest_guard.py→test_looks_like_test_db.py+test_guard_against_populated_db.py+test_guard_blocks_populated_db.py(the anti-537k branch, kept separate as the single most safety-critical
piece) +
test_guard_against_populated_db_structural.pytest_store_isolation.py→test_store_isolation.py(trimmed) +test_real_data_root_guard.pyCompletion Ledger
_pg_throwaway_db.maintenance_url— basic + query-string-with-embedded-slash edge casetest_pg_throwaway_db.py::TestMaintenanceUrl(3 tests)_pg_throwaway_db._orphan_pid— well-formed parse, non-numeric suffix, domain-scoped split/rsplit/maxsplit equivalence documentedtest_pg_throwaway_db.py::TestOrphanPid(2 tests + in-source equivalence doc)_pg_throwaway_db._pid_is_definitely_dead— dead/alive/permission-denied, exactos.killcall args (mocked — see safety note below)test_pg_throwaway_db.py::TestPidIsDefinitelyDead(3 tests)_pg_throwaway_db.drop_dead_orphaned_databases— exact SELECT SQL, dict-row vs tuple-row, dead-pid drop with exact terminate+drop SQL/args, alive-pid no-op, continue-not-break on both guard clauses, selection-failure swallowedtest_pg_throwaway_db.py::TestDropDeadOrphanedDatabases(7 tests)_pg_throwaway_db.create_isolated_test_database/drop_isolated_database— unreachable-server None, exact connect args, orphan-reap on the same connection, db-name format regex, exact CREATE/TERMINATE/DROP SQLtest_pg_throwaway_db.py::TestCreateAndDropIsolatedDatabase(7 tests)_pg_safety_guards.looks_like_test_db— basic true/false, query-string stripped, host-vs-name trap (kills the rsplit-vs-split mutation), double-?edge case, no-slash edge case, remaining split/rsplit/maxsplit survivors documented equivalenttest_looks_like_test_db.py(6 tests)_pg_safety_guards.guard_against_populated_db— override bypass, url-name trust, PG-unreachable, empty-DB, exact connect/execute argstest_guard_against_populated_db.py(8 tests)_pg_safety_guards.guard_against_populated_db— the anti-537k block branch, exact exit messagetest_guard_blocks_populated_db.py(5 tests)_pg_safety_guards.guard_against_populated_db— structural (definition present, module-level call,returncode=2, incident date)test_guard_against_populated_db_structural.py(4 tests)_pg_safety_guards._resolved_real_data_roots— exact 4-tuple listtest_store_isolation.py::TestResolvedRealDataRoots(1 test)_pg_safety_guards.guard_against_real_data_roots— fires/silent, exact message with 2+ offenders (exercises the join separator), no-bypass-env-vartest_real_data_root_guard.py::TestIsolationGuard(4 tests)_pg_safety_guards.guard_against_real_data_roots— structural (redirect + guard call present at module level, ordering before first mcp_server import)test_real_data_root_guard.py::TestGuardStructuralIntegrity(3 tests)_domain_mapping_isolation.isolate_dev_root_scan— stub replacement, cache clearedtest_domain_mapping_isolation.py(2 tests)conftest.py's wiring itself (imports + calls survive)test_guard_against_populated_db_structural.py,test_real_data_root_guard.py::TestGuardStructuralIntegrity59 tests total in the touched/new files; full local suite
6854 passed, 5 skipped, 116 subtests passed(up from the pre-split6826baseline collected on this sameorigin/maincommit — the +33 delta is exactly the new coverage this PR adds for previously-untested extracted code).Gate output (quoted, run against this exact head)
§4 AST size check — every touched/new file, clean:
ruff:
Doc claims / badges (unaffected — issue #294 already removed the
suite-size claim from prose/JSON docs):
§12 Mutation testing (scoped run via a hand-adapted
mutation_check.shinvocation,
CORTEX_TEST_ISOLATE_DB=0to keep the live per-processthrowaway-DB bootstrap itself out of the mutated call graph — mutating a
function
pytest_sessionfinishcalls conflicts with mutmut's ownverification trampoline otherwise):
All 7 survivors are provable-equivalent mutants, documented at each use
site:
looks_like_test_db(4): split/rsplit/maxsplit choices —_PIN_RE-stylereasoning generalizes here too (
[0]fromsplit(sep,N)and[-1]fromrsplit(sep,N)are invariant to N); verified by a 300k-case property fuzzagainst arbitrary strings (structural equivalence, not domain-scoped).
_orphan_pid(3): split/rsplit/maxsplit choices, but this one ISdomain-scoped — equivalent only because every name the function is ever
called against (
cortex_test_pw<pid>_<8-hex>) has exactly one "" in theparsed segment (the hex suffix's alphabet excludes ""); verified by a
200k-case fuzz over the REAL format (an unrestricted fuzz does NOT agree,
confirming this is domain-scoped, not structural).
One additional finding along the way: a mutation to
_pid_is_definitely_dead'shardcoded signal number (
os.kill(pid, 0)→os.kill(pid, 1)/SIGHUP) wasinitially exercised by an UNMOCKED real-syscall test
(
_pid_is_definitely_dead(os.getpid())), which under that mutant would senda real SIGHUP to the pytest process running the mutation sub-run — mutmut
flagged it "suspicious" rather than cleanly killed. Removed that unmocked
test (redundant with the mocked, exact-call-args test that already covers
the same contract) rather than leave a test whose mutation-testing behavior
is a real safety hazard.
No conflict markers;
.bestpractices.jsonparses — confirmed.Boy-scout check (coding-standards.md §14)
tests_py/conftest.py's pre-existing300/500-line cap violation (discovered during PR fix(tests): guard local venv from silently under-collecting vs. CI (#287) #308's Local dev venv (macOS, reused .venv) collects 10 fewer tests than CI's hash-pinned Linux install (6572 vs 6582) #287 fix).
secondary violation this refactor itself created along the way (the two
test files pushed over cap by the new tests) was also fixed in this same
PR rather than deferred.
deferral for the conftest.py violation it saw but did not fix.
Rules compliance (coding-standards.md, evaluated against the version
quoted in this session's system prompt)
_CONFTEST_PATH/_GUARDS_PATHconstants removed fromtest_store_isolation.pyonce their sole consumers moved outHand-offs
None.
Not tied to a tracked GitHub issue by itself — this is the boy-scout
follow-up PR cited from PR #308 (issue #287), per this session's
"no new issues, cite the PR instead" instruction.