From 2fb905fdb016d354fa4938bd9fcb8e2654865998 Mon Sep 17 00:00:00 2001 From: chopratejas Date: Sat, 2 May 2026 17:05:58 -0700 Subject: [PATCH] =?UTF-8?q?fix:=20integrate=20B6+B7=20=E2=80=94=20fix=20cr?= =?UTF-8?q?oss-test=20contamination=20+=20injector=20mock=20parity?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Two follow-ups surfaced when B6 and B7 were merged onto the megamerge branch and the full suite ran: 1. tests/test_proxy_anthropic_cache_stability.py PR-B7 added `injector.scan_for_markers(optimized_messages)` to the Anthropic handler so the always-on tool-registration logic can see detected hashes for the current request. The two pre-existing `_FakeInjector` mocks (`test_ccr_system_instruction_injection_disabled_*` and `test_ccr_tool_injection_disabled_*`) didn't implement that method. Added a no-op `scan_for_markers` returning [] to both mocks — matches the real injector's contract for the not-yet-compressed request shape these tests exercise. 2. tests/test_memory_tool_mode.py::test_tool_mode_skip_emits_structured_log The B6 caplog assertion passed in isolation but failed in the full suite. Root cause: when an earlier test triggers proxy startup, `_setup_file_logging` flips `headroom.propagate=False` and attaches a RotatingFileHandler to the headroom logger. caplog captures via propagation to root, so log records stop reaching it. The conftest autouse fixture that resets `propagate=True` before every test gets shadowed by fixture-ordering edge cases. Principled fix: attach `caplog.handler` directly to `headroom.proxy.memory_handler` for the duration of the test so the capture is independent of propagation state. Restore the original level + remove the handler in `finally` to keep the test hermetic. Both B6 and B7 cherry-picks themselves are unmodified. This commit only adjusts test harness code so the pre-existing mocks/capture stay consistent with the new code paths. --- tests/test_memory_tool_mode.py | 16 +++++++++++++++- tests/test_proxy_anthropic_cache_stability.py | 6 ++++++ 2 files changed, 21 insertions(+), 1 deletion(-) diff --git a/tests/test_memory_tool_mode.py b/tests/test_memory_tool_mode.py index 6f5e86237..b87c2217f 100644 --- a/tests/test_memory_tool_mode.py +++ b/tests/test_memory_tool_mode.py @@ -108,13 +108,27 @@ def test_tool_mode_skip_emits_structured_log(caplog: Any) -> None: Realignment build constraint: every cache-affecting decision is logged in the ``event=foo key=val`` style so operators can audit routing. + + NOTE: caplog captures at the root logger via propagation. When other + tests in the suite trigger proxy startup, ``_setup_file_logging`` sets + ``headroom.propagate=False`` and attaches a file handler. The conftest + autouse reset is fragile against fixture ordering, so we attach + ``caplog.handler`` directly to the target logger here. That way the + capture works regardless of propagation state. """ handler, _backend = _build_tool_mode_handler() - with caplog.at_level(logging.INFO, logger="headroom.proxy.memory_handler"): + target_logger = logging.getLogger("headroom.proxy.memory_handler") + previous_level = target_logger.level + target_logger.setLevel(logging.INFO) + target_logger.addHandler(caplog.handler) + try: result = asyncio.run( handler.search_and_format_context("alpha", [{"role": "user", "content": "hi"}]) ) + finally: + target_logger.removeHandler(caplog.handler) + target_logger.setLevel(previous_level) assert result is None skip_records = [r for r in caplog.records if "event=memory_mode_skip" in r.getMessage()] diff --git a/tests/test_proxy_anthropic_cache_stability.py b/tests/test_proxy_anthropic_cache_stability.py index 6daa01c1b..a085c326b 100644 --- a/tests/test_proxy_anthropic_cache_stability.py +++ b/tests/test_proxy_anthropic_cache_stability.py @@ -459,6 +459,9 @@ def test_ccr_system_instruction_injection_disabled_when_prefix_frozen(monkeypatc def process_request(self, messages, tools): # noqa: ANN001 return messages, tools, False + def scan_for_markers(self, messages): # noqa: ANN001 + return [] + monkeypatch.setattr("headroom.ccr.CCRToolInjector", _FakeInjector) async def _fake_retry(method, url, headers, body, stream=False, **kwargs): # noqa: ANN001 @@ -523,6 +526,9 @@ def test_ccr_tool_injection_disabled_when_prefix_frozen(monkeypatch) -> None: def process_request(self, messages, tools): # noqa: ANN001 return messages, tools, False + def scan_for_markers(self, messages): # noqa: ANN001 + return [] + monkeypatch.setattr("headroom.ccr.CCRToolInjector", _FakeInjector) async def _fake_retry(method, url, headers, body, stream=False, **kwargs): # noqa: ANN001