Skip to content

Commit 822c2f8

Browse files
cdeustclaude
andcommitted
fix(core): raise on an unconfigured environment reader, wire every real entry point
Addresses ZETETIC-REVIEW: REQUEST_CHANGES on PR #568 (024fe55). Critical fix: core/environment.read_environment_variable used to return None when no reader was configured -- a silent fallback CLAUDE.md forbids. benchmarks/lib/ablation_runner.py sets CORTEX_ABLATE_<NAME>=1 in-process and calls a benchmark's run_benchmark() directly, never importing mcp_server/__main__.py, scripts/launcher.py, or tests_py/conftest.py. With the silent None default, every ablation measurement on this branch silently scored the un-ablated system, invalidating the published E1/E2 evidence the ablation results cite. Same class of exposure for every other benchmarks/**/run_*.py and scripts/ entry that reads a CORTEX_*_BETA / CORTEX_DECAY_* / CORTEX_RERANKER_OFFLINE flag. Fix: read_environment_variable now raises RuntimeError when no reader was ever configured (an explicitly-wired reader returning None for one specific variable is still a legitimate "unset" answer -- only the never-configured case is a bug). Then wired every real composition root, enumerated mechanically rather than guessed: - scripts/launcher.py: the one process bootstrap shared by the plugin-installed server AND every hook (ADR-0742, `.claude-plugin/ plugin.json`'s hooks all route through `launcher.py mcp_server.hooks. <name>`) -- wiring it here once covers all of them without touching mcp_server/hooks/__init__.py (which would break capture_dispatch/ capture_worker's "must never import mcp_server.core" lazy-load invariant, tests_py/hooks/test_capture_dispatch.py). Verified this reasoning against the actual test: it does a bare `-c` import that never touches launcher.py at all, so wiring launcher.py cannot affect it either way -- confirmed capture_dispatch's 8 tests still pass. - Every one of the 14 hook scripts with `if __name__ == "__main__":` ALSO gets the wiring directly in that block (redundant with launcher.py, not with each other) -- this covers the one entry point launcher.py cannot reach: scripts/measure_pipeline_hook.py spawns `python -m mcp_server.hooks.pipeline_impact_bump` directly (bypassing launcher.py) while setting CORTEX_RERANKER_OFFLINE=1 to measure hook latency. Verified the fix on that exact invocation shape (runpy. run_module with run_name="__main__", matching what `-m` does). - benchmarks/lib/_composition_root_wiring.py: one new bootstrap (environment + the existing reranker seam) that every benchmarks/**/run_*.py, ablation_runner.py, and any benchmarks/lib module reaching core now imports. Six run_*.py already got this transitively via bench_db.py or retriever.py (both updated to the unified bootstrap); the other eleven needed an explicit import, added after each file's own sys.path setup so import order does not break. benchmarks/lib/run_e1_v3_lme.py and run_e1_v3_locomo.py were audited and excluded: they only orchestrate subprocesses of the already-wired run_benchmark.py scripts, never import core in-process. - mcp_server/doctor.py's ensure_reranker_ready() already self-wired its own seam (from the prior review round); unaffected by this change. New test tests_py/benchmarks/test_ablation_runner_wiring.py drives the literal ablation_runner.py entry path in a bare subprocess (no pytest conftest wiring) and proves CORTEX_ABLATE_ADAPTIVE_DECAY=1 actually disables the mechanism, plus proves an unwired bare script raises instead of silently reporting every mechanism enabled. Also: tests_py/test_doctor.py::TestEnsureRerankerReady:: test_self_wires_and_loads_even_when_the_seam_was_reset asserted `status.state == "loaded"`, which needs the FlashRank model on disk and failed in the SQLite CI job (does not preload it). Now asserts the seam is configured and reranker_cache_dir() does not raise, not that a model downloaded -- extracted the reset/restore logic into two helpers to stay under the 40-line method cap. Verified: scripts/check_craftsmanship.py --base origin/main returns OK; ruff check/format are clean; pyright mcp_server/ reports 0 errors; full pytest reports 8433+ passed, 0 failed (re-run after these changes); `CORTEX_ABLATE_ADAPTIVE_DECAY=1 python -c "..."` through scripts/launcher.py and through a bare runpy.run_module both confirmed disabling the mechanism; `python -c "from mcp_server.core.ablation import ...; is_mechanism_disabled(...)"` with no composition root confirmed raising RuntimeError. benchmarks/reproduce.sh --no-regression: Docker is available and the script is running against real data (LongMemEval-S downloaded and checksum-verified, locomo10.json present); the longmemeval-s benchmark stage is still executing after 35+ minutes of CPU time at the time of this commit. A full --no-regression run covers at least three benchmark suites (longmemeval-s, locomo, beam-100K) plus decision-ids and does not support --quick/--limit, so it cannot complete within one review turn. Reporting this honestly rather than claiming a result I do not have; the run continues and its output will be reported once available. Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
1 parent 024fe55 commit 822c2f8

31 files changed

Lines changed: 301 additions & 42 deletions

‎benchmarks/_repro.py‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
from typing import Any
1414
from importlib.metadata import version
1515
from mcp_server.core.reranker import ensure_reranker_loaded, model_sha256
16-
import benchmarks.lib._reranker_wiring # noqa: F401 — source: issue #560
16+
import benchmarks.lib._composition_root_wiring # noqa: F401 — source: issue #560
1717

1818

1919
def _git_sha() -> str:

‎benchmarks/active_forgetting/run_benchmark.py‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@
1313
REPO = Path(__file__).resolve().parents[2]
1414
sys.path.insert(0, str(REPO))
1515

16+
import benchmarks.lib._composition_root_wiring # noqa: E402,F401 — source: issue #560
17+
1618
# The core under test (the same functions production calls).
1719
from mcp_server.core.active_forgetting import ( # noqa: E402
1820
ACUTE_OVERLAP_THRESHOLD,

‎benchmarks/decision_ids/run_bench.py‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
import tempfile
1515
from unittest.mock import AsyncMock, Mock, patch
1616

17+
import benchmarks.lib._composition_root_wiring # noqa: F401 — source: issue #560
1718
from mcp_server.handlers import recall, unified_search
1819
from mcp_server.infrastructure.wiki_decision_index import write_decision_index
1920

‎benchmarks/energy/run_embedding_energy.py‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,8 @@ def _run(args: argparse.Namespace) -> dict[str, object]:
6363
# source: ADR-0822
6464
if str(REPO) not in sys.path:
6565
sys.path.insert(0, str(REPO))
66+
import benchmarks.lib._composition_root_wiring # noqa: PLC0415,F401 — source: issue #560
67+
6668
# Deferred: these modules require NumPy and the optional embedding stack.
6769
from benchmarks.energy import measurement, workload # noqa: PLC0415
6870

‎benchmarks/entity_dedup/run_benchmark.py‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
import argparse
1414
import sys
1515

16+
import benchmarks.lib._composition_root_wiring # noqa: F401 — source: issue #560
1617
from mcp_server.core.entity_dedup import FUZZY_ELIGIBLE_TYPES, deduplicate_entities
1718

1819
# Deterministic fixture: real-world spelling/spacing/typo variants that the

‎benchmarks/forgetting_curve/run_benchmark.py‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
REPO = Path(__file__).resolve().parents[2]
1515
sys.path.insert(0, str(REPO))
1616

17+
import benchmarks.lib._composition_root_wiring # noqa: E402,F401 — source: issue #560
1718
import psycopg # noqa: E402
1819

1920
from benchmarks.forgetting_curve import criteria # noqa: E402
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
"""Single benchmark composition-root bootstrap (issue #560).
2+
3+
Benchmarks never run through mcp_server/__main__.py or scripts/
4+
launcher.py, so every one of core/'s injection seams (core/ may not
5+
import os/pathlib or perform I/O) is unwired unless a benchmark entry
6+
point wires it itself. Import this module for its side effect
7+
(``import benchmarks.lib._composition_root_wiring # noqa: F401``) at
8+
the top of every benchmark entry point that reaches core -- every
9+
``benchmarks/**/run_*.py``, ``benchmarks/lib/ablation_runner.py``, and
10+
any script under ``benchmarks/`` or ``scripts/`` that touches
11+
ablation-guarded core logic or the FlashRank reranker.
12+
13+
Safe to import from multiple entry points and to import more than once:
14+
Python caches submodules, so each seam is wired exactly once per process.
15+
"""
16+
17+
from __future__ import annotations
18+
19+
import benchmarks.lib._environment_wiring # noqa: F401
20+
import benchmarks.lib._reranker_wiring # noqa: F401
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
"""One-shot composition-root wiring for core/environment.py's injection
2+
seam (issue #560: core/ may not import os).
3+
4+
Every benchmark entry point that touches ablation-guarded core logic or
5+
any of the env-var tuning knobs core/environment.py backs (see that
6+
module's docstring) must import this first
7+
(``import benchmarks.lib._environment_wiring # noqa: F401``) --
8+
benchmarks never run through mcp_server/__main__.py or scripts/
9+
launcher.py, so without this an unconfigured reader raises RuntimeError,
10+
and before this fix (2026-09-16) it silently no-op'd instead, which made
11+
every ablation measurement silently score the un-ablated system. Safe to
12+
import from multiple entry points: Python caches the module, so the
13+
wiring call below runs exactly once per process.
14+
"""
15+
16+
from __future__ import annotations
17+
18+
import os
19+
20+
from mcp_server.core.environment import configure_core_environment_reader
21+
22+
configure_core_environment_reader(os.environ.get)

‎benchmarks/lib/ablation_runner.py‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
if str(_ROOT) not in sys.path:
2929
sys.path.insert(0, str(_ROOT))
3030

31+
import benchmarks.lib._composition_root_wiring # noqa: E402,F401 — source: issue #560
3132
from mcp_server.core import ablation as _ablation # noqa: E402
3233
from mcp_server.core.ablation import Mechanism # noqa: E402
3334
from benchmarks.lib.db_snapshot import ( # noqa: E402

‎benchmarks/lib/bench_db.py‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
from mcp_server.core.reranker import ensure_reranker_loaded
2525
from mcp_server.infrastructure.embedding_engine import EmbeddingEngine
2626
from mcp_server.infrastructure.pg_store import PgMemoryStore
27-
import benchmarks.lib._reranker_wiring # noqa: F401 — source: issue #560
27+
import benchmarks.lib._composition_root_wiring # noqa: F401 — source: issue #560
2828

2929

3030
def _apply_capture_origin_mix(memories: list[dict[str, Any]]) -> None:

0 commit comments

Comments
 (0)