fix(transforms): keep exception text out of compression-failure warnings

Compressor and store exceptions can quote tool output, so each WARNING now names only the
exception type and counts; the full detail moves to a DEBUG line with exc_info.

Adds a parametrized test over all five failure paths: one WARNING without the exception text,
and the detail only at DEBUG.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Assisted-by: claude-code:claude-opus-5-5
Headline-Session: 6e77a807-8dd4-4a77-a2fa-ddce6dc2515e
Headline-AI-Share: 78% strict / 78% weighted
This commit is contained in:
Tejas Chopra
2026-10-09 18:29:49 -07:00
co-authored by Claude Opus 5.5
parent 24e6116db7
commit f255772374
7 changed files with 190 additions and 10 deletions
+4 -2
View File
@@ -258,11 +258,13 @@ def compress_batch_with_router(
bias=batch.entries[0].routed.unit.bias,
)
except Exception as e:
# The exception text can quote tool output, so it goes to DEBUG only.
logger.warning(
"Batch compression failed; passing %d units through uncompressed: %s",
"Batch compression failed (%s); passing %d units through uncompressed",
type(e).__name__,
len(batch.entries),
e,
)
logger.debug("Batch compression failure detail", exc_info=True)
return _passthrough_batch_results(
batch,
tokenizer=tokenizer,
+5 -1
View File
@@ -288,9 +288,13 @@ def dedup_blocks(
return out_blocks, stats
except Exception as e: # never break the proxy
# The exception text can quote tool output, so it goes to DEBUG only.
logger.warning(
"Cross-turn dedup failed on %d blocks; leaving them as-is: %s", len(blocks), e
"Cross-turn dedup failed (%s) on %d blocks; leaving them as-is",
type(e).__name__,
len(blocks),
)
logger.debug("Cross-turn dedup failure detail", exc_info=True)
return blocks, {"spans_folded": 0, "lines_removed": 0, "chars_removed": 0, "error": True}
+5 -1
View File
@@ -1617,7 +1617,11 @@ def store_kompress_in_ccr(original: str, compressed: str, original_tokens: int)
)
return cache_key
except Exception as e:
logger.warning("Kompress CCR store write failed; output is not retrievable: %s", e)
# Store backends can echo the payload in errors, so the detail goes to DEBUG only.
logger.warning(
"Kompress CCR store write failed (%s); output is not retrievable", type(e).__name__
)
logger.debug("Kompress CCR store failure detail", exc_info=True)
return None
+4 -2
View File
@@ -537,11 +537,13 @@ def compact_lossless(content: str, kind: str) -> str:
return content
return candidate if _smaller(candidate, content) else content
except Exception as e:
# The exception text can quote the content, so it goes to DEBUG only.
logger.warning(
"Lossless compaction (%s) failed on %d chars; leaving content as-is: %s",
"Lossless compaction (%s) failed (%s) on %d chars; leaving content as-is",
kind,
type(e).__name__,
len(content),
e,
)
logger.debug("Lossless compaction failure detail", exc_info=True)
return content
return content
+6 -2
View File
@@ -1196,8 +1196,12 @@ class SmartCrusher(Transform):
"CCR mirror: invalid hash %r from rendered marker",
ccr_hash,
)
except Exception as e: # pragma: no cover - defensive
logger.warning("CCR mirror: store write failed for hash %s: %s", ccr_hash, e)
except Exception as e:
# Store backends can echo the payload in errors, so the detail goes to DEBUG only.
logger.warning(
"CCR mirror: store write failed (%s) for hash %s", type(e).__name__, ccr_hash
)
logger.debug("CCR mirror store failure detail", exc_info=True)
def _extract_context_from_messages(self, messages: list[dict[str, Any]]) -> str:
"""Build a query string from the last 5 user messages + recent
+3 -2
View File
@@ -229,8 +229,9 @@ def test_compact_lossless_logs_a_fold_that_raises(monkeypatch) -> None:
assert len(records) == 1
message = records[0].getMessage()
assert "Lossless compaction (log) failed" in message
assert "fold bug" in message
assert "Lossless compaction (log) failed (RuntimeError)" in message
# The exception text can quote content, so it stays out of the WARNING.
assert "fold bug" not in message
# --------------------------------------------------------------------------
+163
View File
@@ -0,0 +1,163 @@
"""Compression failure paths log a WARNING without the exception text.
Each path catches the failure and passes content through unchanged. The
WARNING names the exception type and counts; the exception text (which can
quote tool output or stored payloads) appears only in the DEBUG detail line.
"""
from __future__ import annotations
import logging
from collections.abc import Callable, Iterator
import pytest
import headroom.cache.compression_store as compression_store
from headroom.transforms import (
compression_batches,
cross_turn_dedup,
kompress_compressor,
lossless_compaction,
smart_crusher,
)
from headroom.transforms.compression_batches import (
CompressionBatchEntry,
build_compression_batches,
compress_batch_with_router,
)
from headroom.transforms.compression_units import CompressionUnit, RoutedCompressionUnit
SECRET = "sk-test-secret-in-tool-output"
class _Capture(logging.Handler):
def __init__(self) -> None:
super().__init__(level=logging.DEBUG)
self.records: list[logging.LogRecord] = []
def emit(self, record: logging.LogRecord) -> None:
self.records.append(record)
@pytest.fixture
def capture() -> Iterator[Callable[[logging.Logger], _Capture]]:
attached: list[tuple[logging.Logger, _Capture, int]] = []
def attach(logger: logging.Logger) -> _Capture:
handler = _Capture()
attached.append((logger, handler, logger.level))
logger.addHandler(handler)
logger.setLevel(logging.DEBUG)
return handler
yield attach
for logger, handler, level in attached:
logger.removeHandler(handler)
logger.setLevel(level)
def _boom(*_args: object, **_kwargs: object) -> object:
raise RuntimeError(f"failed while handling {SECRET}")
def _run_batch(monkeypatch: pytest.MonkeyPatch) -> None:
def entry(index: int) -> CompressionBatchEntry:
unit = CompressionUnit(
text="x" * 150,
provider="openai",
endpoint="responses",
role="tool",
item_type="local_shell_call_output",
cache_zone="live",
mutable=True,
min_bytes=512,
)
return CompressionBatchEntry(
entry_id=f"u{index}",
routed=RoutedCompressionUnit(unit=unit, slot=(index, ("output", None))),
)
class _Router:
compress = staticmethod(_boom)
class _Counter:
def count_text(self, text: str) -> int:
return len(text)
batches, _ = build_compression_batches([entry(i) for i in range(4)], min_batch_bytes=512)
results = compress_batch_with_router(batches[0], router=_Router(), tokenizer=_Counter())
assert [result.compressed for _, result in results] == ["x" * 150] * 4
def _run_dedup(monkeypatch: pytest.MonkeyPatch) -> None:
monkeypatch.setattr(cross_turn_dedup, "_longest_match", _boom)
blocks = [
cross_turn_dedup.DedupBlock(text="line a\nline b\nline c\nline d", turn=i, protected=False)
for i in range(2)
]
out, stats = cross_turn_dedup.dedup_blocks(blocks)
assert out == blocks
assert stats["error"] is True
def _run_lossless(monkeypatch: pytest.MonkeyPatch) -> None:
monkeypatch.setattr(lossless_compaction, "collapse_runs", _boom)
log = "worker 7 retrying connection to upstream\n" * 5
assert lossless_compaction.compact_lossless(log, "log") == log
def _run_kompress_store(monkeypatch: pytest.MonkeyPatch) -> None:
monkeypatch.setattr(compression_store, "get_compression_store", _boom)
assert kompress_compressor.store_kompress_in_ccr("original words", "compressed", 2) is None
def _run_crusher_mirror(monkeypatch: pytest.MonkeyPatch) -> None:
class _Store:
store = staticmethod(_boom)
class _Rust:
def ccr_get(self, _hash: str) -> str:
return "[1, 2, 3]"
monkeypatch.setattr(compression_store, "get_compression_store", lambda: _Store())
crusher = smart_crusher.SmartCrusher()
monkeypatch.setattr(crusher, "_rust", _Rust())
crusher._mirror_single_hash_to_python_store("a" * 24, "smart_crusher", "", None)
@pytest.mark.parametrize(
("logger", "run", "expected"),
[
(compression_batches.logger, _run_batch, "Batch compression failed (RuntimeError)"),
(cross_turn_dedup.logger, _run_dedup, "Cross-turn dedup failed (RuntimeError)"),
(lossless_compaction.logger, _run_lossless, "Lossless compaction (log) failed"),
(
kompress_compressor.logger,
_run_kompress_store,
"Kompress CCR store write failed (RuntimeError)",
),
(smart_crusher.logger, _run_crusher_mirror, "CCR mirror: store write failed"),
],
ids=["batch", "dedup", "lossless", "kompress-ccr", "crusher-mirror"],
)
def test_failure_logs_warning_without_exception_text(
capture: Callable[[logging.Logger], _Capture],
monkeypatch: pytest.MonkeyPatch,
logger: logging.Logger,
run: Callable[[pytest.MonkeyPatch], None],
expected: str,
) -> None:
handler = capture(logger)
run(monkeypatch)
warnings = [r for r in handler.records if r.levelno == logging.WARNING]
assert len(warnings) == 1
message = warnings[0].getMessage()
assert expected in message
assert "RuntimeError" in message
assert SECRET not in message
assert warnings[0].exc_info is None
debug = [r for r in handler.records if r.levelno == logging.DEBUG and r.exc_info]
assert len(debug) == 1
assert SECRET in str(debug[0].exc_info[1])