fix: integrate B6+B7 — fix cross-test contamination + injector mock parity

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.
This commit is contained in:
chopratejas 2026-05-02 17:05:58 -07:00
parent 00902b8fea
commit 2fb905fdb0
2 changed files with 21 additions and 1 deletions

View file

@ -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()]

View file

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