fix(compress): stop adding markers the caller did not ask for (#4678)

`/v1/compress` returns a body that someone else's client forwards
unchanged, so anything Headroom adds there reaches the provider. Two
things got added that the caller never asked for.

## Stale-Read retrieval hashes in marker-free modes

Read lifecycle replaces a Read that a later edit made stale with a short
notice. That notice always ended in `Retrieve original: hash=…` and
wrote the original to the CCR store, even in the default and
`lossy_inline` modes. The docs call those modes marker-free, and their
callers have no retrieval tool, so the hash pointed nowhere.

- Marker-free modes keep the notice ("re-read the file for current
content") but drop the hash and skip the store write.
- Lossless routers (proxy `--lossless`,
`ContentRouterConfig(lossless=True)`) skip read lifecycle entirely,
because the notice drops the file's bytes.
- Read maturation now recognises the hashless notice. Otherwise it would
replace the notice with a marker whose hash points at the notice itself.

## Cache breakpoints the client didn't send

On the session path, `normalize_message_cache_control` fell back to
placing `{"type": "ephemeral"}` on the last block message when the
client marked nothing. A caller that chose not to cache got billed cache
writes. `/v1/compress` now passes `place_unmarked=False`: if the client
marked no message, markers are stripped and none are placed. Proxy
handlers keep today's behaviour, and client markers in a misaligned list
still go through the old path.

## Tests

Each new test fails on main.

- `test_marker_free_modes_mark_a_stale_read_without_a_retrieval_hash`,
for the default mode and `lossy_inline`
- `test_lossless_router_keeps_a_stale_read`
- `test_session_turns_add_no_marker_the_caller_did_not_send`
- `test_respects_lifecycle_markers`, extended to the hashless notice

The related suites (gateway, transforms, read maturation, prefix
tracker, cache_control, compress endpoint and session) pass: 1371 passed
and 9 skipped before the review fixes, then 222 of 222 on the touched
files after them. Lint is clean on CI's ruff 0.17.0, and mypy is clean
on the touched modules.

## Not in this PR

- Read maturation (`handlers/anthropic.py`) is not gated on `--lossless`
or `--no-ccr` either. It's the same kind of bug on the proxy path, so it
belongs in a separate fix.
- The "lossless densify only" pipeline in `compress.py` sets
`ccr_enabled=False` without `lossless=True`, so read lifecycle still
runs there.

🤖 Generated with [Claude Code](https://claude.com/claude-code)

<!-- devin-review-badge-begin -->

---

<a href="https://app.devin.ai/review/headroomlabs-ai/headroom/pull/4678"
target="_blank"><picture><source media="(prefers-color-scheme: dark)"
srcset="https://static.devin.ai/assets/gh-devin-review-dark.svg?v=4"><img
src="https://static.devin.ai/assets/gh-devin-review-light.svg?v=4"
alt="Devin Review"></picture></a>
<!-- devin-review-badge-end -->

## Real Behavior Proof

Maintainer qualification on Windows, Python 3.12.13, using an actual
Uvicorn server bound to an owned loopback socket and HTTPX over real
HTTP. The normal `/v1/compress` endpoint and compression pipeline ran
with offline mode and Kompress disabled; no handler, compression stage,
HTTP response or store was mocked. Workspace, configuration, savings and
TOIN paths were isolated. All 243 native inputs and the reused artifact
SHA-256
`5be63a4f31d3e38e7427d28380ecb977616e3f6a3dd6a1c59af5ff6361a05978` were
verified; this was not a fresh native build.

- Exact command / steps: the committed probe is `python -m pytest -q -s
tests/test_compress_runtime_http.py`. The qualifying run used the
maintainer loopback-only launcher with an explicit verified native path
and SHA-256, followed by the four focused
session/API/maturation/lifecycle suites. All 138 tests passed. The probe
starts the actual server, GETs `/health`, and sends nine POSTs to
`/v1/compress`: a Read followed by Edit in default, lossy-inline and
lossless modes, then three session turns without caller cache markers
and three turns with a caller marker. It stops and joins the server in
`finally`.
- Observed result: health and all nine compression requests returned
HTTP 200. Default and lossy-inline emitted stale notices with no
retrieval hash or CCR hashes, saving 3,576 tokens per request. Lossless
preserved the original Read bytes. All three unmarked session turns
returned zero cache markers; all three marked controls retained exactly
one. Session compression saved 7,350 / 15,901 / 25,958 tokens. Server
shutdown joined successfully.
- Before-fix control: the same real-server requests with all six
affected production modules replaced by current main
`200c838c7393d20ae56a0c338f832ed27133c398` failed five assertions:
default and lossy-inline included retrieval hashes, and each unmarked
session turn added a cache marker. Lossless and the three marked
controls passed on main and are not claimed as reproduced regressions.
Candidate source was restored byte-for-byte.
- Not tested: provider forwarding or billing, a live model, deployment
behavior, or the separate proxy read-maturation scope already listed
above. External startup connection attempts were blocked by the
qualifying launcher; this is not a zero-attempt network claim. No author
readiness or self-review declaration is added by this maintainer
evidence.

---------

Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
Co-authored-by: JerrettDavis <mxjerrett@gmail.com>
This commit is contained in:
Tejas Chopra
2026-11-13 09:39:10 -06:00
committed by GitHub
co-authored by Claude Opus 5.5 JerrettDavis
parent 9f2cc01786
commit c21b0813bd
12 changed files with 337 additions and 69 deletions
+2 -2
View File
@@ -650,7 +650,7 @@ Keep carrying both yourself and send them upstream unchanged. Two consequences w
**`config.mode` values:**
- **unset (default)** — marker-free. Emits no `<<ccr:…>>` retrieval markers and writes nothing to the CCR store, so you can forward the returned messages straight to a provider. This is the right mode for a gateway or guardrail that just swaps `messages` and forwards.
- **unset (default)** — marker-free. Emits no `<<ccr:…>>` retrieval markers and writes nothing to the CCR store, so you can forward the returned messages straight to a provider. This is the right mode for a gateway or guardrail that just swaps `messages` and forwards. A file read that a later edit made stale is still replaced, with a note telling the model to re-read the file and no retrieval hash.
- **`ccr`** — emits CCR markers and writes to the store. Only for callers that also inject the `headroom_retrieve` tool *and* can reach `/v1/retrieve` (itself loopback-only). Markers are a dangling pointer for the model otherwise.
- **`lossy_inline`** (alias `lossless_then_lossy`) — runs the lossless byte/data fold first, then compresses the folded remainder. Marker-free.
- **`lossless`** — format-native folds only, with no markers and no retrieval. For a sidecar with no retrieval loop; a registered lossless provider still applies. Keep the mode stable within a `session_id`, because session mode replays earlier turns byte-for-byte.
@@ -743,7 +743,7 @@ Also for multi-turn callers:
#### Session mode
Pass `config.session_id` and Headroom keeps the conversation's replay state itself — the same per-session compression cache and prefix tracker the proxy path uses — so a gateway that owns routing can resend the **raw** conversation every turn and still get a byte-identical prefix back. Whatever this endpoint previously returned for the session is treated as the provider's cache contract: every already-returned message comes back unchanged, even where recompressing it would do better, and only the new tail is compressed. An explicit `config.frozen_message_count` still wins when it is larger.
Pass `config.session_id` and Headroom keeps the conversation's replay state itself — the same per-session compression cache and prefix tracker the proxy path uses — so a gateway that owns routing can resend the **raw** conversation every turn and still get a byte-identical prefix back. Whatever this endpoint previously returned for the session is treated as the provider's cache contract: every already-returned message comes back unchanged, even where recompressing it would do better, and only the new tail is compressed. Message `cache_control` markers stay where you put them. If you send none, none come back. An explicit `config.frozen_message_count` still wins when it is larger.
```json
{
+10 -1
View File
@@ -964,6 +964,8 @@ def normalize_message_cache_control(
messages: list[dict[str, Any]],
previous_forwarded_messages: list[dict[str, Any]] | None = None,
client_messages: list[dict[str, Any]] | None = None,
*,
place_unmarked: bool = True,
) -> list[dict[str, Any]]:
"""Own message-level cache_control placement so breakpoints stay bounded.
@@ -1002,6 +1004,10 @@ def normalize_message_cache_control(
with it), falls back to the legacy single-marker consolidation. Only
block-style (list) content can carry cache_control; string content is left
as-is. Returns the input unchanged when there is nothing to normalize.
``place_unmarked=False`` is for callers that hand the body back to someone
else's client (``/v1/compress``): when that client marked no message, its
caching choice stands and every message marker is stripped, none added.
"""
changed = False
out: list[dict[str, Any]] = []
@@ -1072,8 +1078,8 @@ def normalize_message_cache_control(
# prefix caches). The newest client marker keeps stable-run anchoring
# when the client placed it on its message's final block (intent: "cache
# through the end"); an explicit mid-message marker is honored verbatim.
positions = _client_marker_positions(client_messages) if client_messages is not None else []
if client_messages is not None and len(client_messages) == len(messages):
positions = _client_marker_positions(client_messages)
if positions:
placed_any = False
newest_mi, newest_bi, _ = positions[-1]
@@ -1093,6 +1099,9 @@ def normalize_message_cache_control(
return out
return messages
if not place_unmarked and client_messages is not None and not positions:
return out if changed else messages
# Legacy: re-place exactly one breakpoint on the last block-style message.
if last_block_idx >= 0:
marker = dict(last_marker) if last_marker else {"type": "ephemeral"}
+1 -1
View File
@@ -654,7 +654,7 @@ class ReadLifecycleConfig:
"""Event-driven Read lifecycle management.
Detects stale and superseded Read outputs in the conversation and replaces
them with compact markers + CCR hashes. Fresh Reads are never touched.
them with compact markers (+ CCR hashes when retrieval markers are on). Fresh Reads are never touched.
A Read is STALE when the file was subsequently edited (content is factually
wrong). A Read is SUPERSEDED when the file was subsequently re-Read (content
+4 -1
View File
@@ -11616,7 +11616,10 @@ class OpenAIHandlerMixin:
from headroom.cache.prefix_tracker import normalize_message_cache_control
final = normalize_message_cache_control(
turn.messages, prev_returned, client_messages=messages
turn.messages,
prev_returned,
client_messages=messages,
place_unmarked=False,
)
if _turn is not None:
# AFTER the replay overlay, BEFORE update_from_result /
+5 -2
View File
@@ -6380,8 +6380,9 @@ class ContentRouter(Transform):
)
)
# Pre-process: Read lifecycle management (stale/superseded detection)
if self.config.read_lifecycle.enabled:
# Pre-process: Read lifecycle management (stale/superseded detection).
# Lossless mode keeps every Read: a stale notice drops the bytes.
if self.config.read_lifecycle.enabled and not self.config.lossless:
from .read_lifecycle import ReadLifecycleManager
# is None (not truthiness) so falsy test doubles are honored;
@@ -6395,9 +6396,11 @@ class ContentRouter(Transform):
except ImportError:
pass
# Marker-free modes get the notice without a hash nobody can redeem.
lifecycle_mgr = ReadLifecycleManager(
self.config.read_lifecycle,
compression_store=injected_store,
retrieval_markers=self.config.ccr_inject_marker,
)
lifecycle_result = lifecycle_mgr.apply(
messages,
+32 -14
View File
@@ -106,6 +106,14 @@ def _rejected_before_running(content: Any) -> bool:
return isinstance(content, str) and content.lstrip().startswith("<tool_use_error>")
_NOTICE_PREFIXES = ("[Read content stale: ", "[Read content superseded: ")
def is_lifecycle_notice(content: str) -> bool:
"""True when ``content`` is a Read this pass already replaced."""
return content.startswith(_NOTICE_PREFIXES)
class ReadLifecycleManager:
"""Event-driven Read lifecycle management.
@@ -117,9 +125,15 @@ class ReadLifecycleManager:
self,
config: ReadLifecycleConfig,
compression_store: Any | None = None,
*,
retrieval_markers: bool = True,
):
"""``retrieval_markers=False`` is for callers that cannot resolve a
``Retrieve original: hash=`` pointer: a replaced Read then gets the
stale/superseded notice alone, and nothing is written to the store."""
self.config = config
self.store = compression_store
self.retrieval_markers = retrieval_markers
def apply(
self,
@@ -521,6 +535,10 @@ class ReadLifecycleManager:
if content_bytes < self.config.min_size_bytes:
return False, content, None
file_display = classification.file_path or "unknown"
if not self.retrieval_markers:
return True, self._notice(classification.state, file_display, None), None
# Best-effort CCR persistence (mirrors read_maturation.py): a store
# failure must not break compress(), and keeps the Read verbatim — a
# marker would point at an original nobody stored.
@@ -543,22 +561,22 @@ class ReadLifecycleManager:
)
return False, content, None
file_display = classification.file_path or "unknown"
return True, self._notice(classification.state, file_display, ccr_hash), ccr_hash
@staticmethod
def _notice(state: ReadState, file_display: str, ccr_hash: str | None) -> str:
# NOTE: the literal phrase "Retrieve original: hash=" is load-bearing —
# the compression-pinning checks in ContentRouter and the
# marker-preserving regex in compression_units.py match on it.
if classification.state == ReadState.STALE:
marker = (
# marker-preserving regex in compression_units.py match on it. The
# hashless form is not pinned: it stays intact because Read is in
# DEFAULT_EXCLUDE_TOOLS and the notice is far below any size floor.
retrieve = f" Retrieve original: hash={ccr_hash}" if ccr_hash else ""
if state == ReadState.STALE:
return (
f"[Read content stale: {file_display} was modified after this read — "
f"re-read the file for current content. "
f"Retrieve original: hash={ccr_hash}]"
f"re-read the file for current content.{retrieve}]"
)
else: # SUPERSEDED
marker = (
f"[Read content superseded: {file_display} was re-read later — "
f"re-read the file if needed. "
f"Retrieve original: hash={ccr_hash}]"
)
return True, marker, ccr_hash
return (
f"[Read content superseded: {file_display} was re-read later — "
f"re-read the file if needed.{retrieve}]"
)
+6 -1
View File
@@ -66,6 +66,7 @@ from dataclasses import dataclass, field
from typing import Any
from ..config import ReadMaturationConfig
from .read_lifecycle import is_lifecycle_notice
logger = logging.getLogger(__name__)
@@ -398,7 +399,11 @@ class ReadMaturationManager:
return None, False
# Lifecycle markers (stale/superseded) are already compact — and
# read_lifecycle runs first, so respect its replacement.
if "Retrieve original: hash=" in content or "Retrieve more: hash=" in content:
if (
is_lifecycle_notice(content)
or "Retrieve original: hash=" in content
or "Retrieve more: hash=" in content
):
return None, False
file_path, read_turn = activity.read_calls[tc_id]
+149
View File
@@ -0,0 +1,149 @@
"""Actual local uvicorn/HTTP exercise of the published compress endpoint."""
import hashlib
import json
import os
import socket
import threading
import time
from pathlib import Path
import pytest
def test_actual_server_compress_contract(tmp_path, monkeypatch):
for key, path in {
"HEADROOM_WORKSPACE_DIR": "workspace",
"HEADROOM_CONFIG_DIR": "config",
"HEADROOM_SAVINGS_PATH": "savings.json",
"HEADROOM_SAVINGS_EVENTS_PATH": "events.jsonl",
"HEADROOM_TOIN_PATH": "toin.json",
}.items():
monkeypatch.setenv(key, str(tmp_path / path))
for key, value in {
"HEADROOM_OFFLINE": "1",
"HEADROOM_TELEMETRY": "off",
"HEADROOM_BEACON": "off",
"HEADROOM_CC_SWITCH_RECONCILE": "0",
"HEADROOM_KOMPRESS_BACKGROUND_WARMUP": "0",
}.items():
monkeypatch.setenv(key, value)
httpx = pytest.importorskip("httpx")
uvicorn = pytest.importorskip("uvicorn")
native = pytest.importorskip("headroom._core")
from headroom.proxy.server import ProxyConfig, create_app
from tests.test_compress_session_mode import _anthropic_turns, _count_message_markers
from tests.test_proxy_compress_endpoint import TestCompressEndpointLossyInlineMode
native_path = Path(native.__file__).resolve()
expected = Path(os.environ.get("HEADROOM_REVIEW_NATIVE_PATH", str(native_path))).resolve()
assert native_path == expected
digest = hashlib.sha256(native_path.read_bytes()).hexdigest()
if expected_digest := os.environ.get("HEADROOM_REVIEW_NATIVE_SHA256"):
assert digest == expected_digest
app = create_app(
ProxyConfig(
host="127.0.0.1",
optimize=True,
offline=True,
memory_enabled=False,
cache_enabled=False,
rate_limit_enabled=False,
cost_tracking_enabled=False,
image_optimize=False,
disable_kompress=True,
subscription_tracking_enabled=False,
periodic_toin_stats_enabled=False,
periodic_malloc_trim_enabled=False,
)
)
listener = socket.socket()
listener.bind(("127.0.0.1", 0))
port = listener.getsockname()[1]
server = uvicorn.Server(uvicorn.Config(app, log_level="warning", loop="asyncio"))
worker = threading.Thread(target=server.run, kwargs={"sockets": [listener]}, daemon=True)
cases = []
health = None
worker.start()
try:
deadline = time.monotonic() + 20
while not server.started and worker.is_alive() and time.monotonic() < deadline:
time.sleep(0.05)
assert server.started and app.state.ready
with httpx.Client(
base_url=f"http://127.0.0.1:{port}", trust_env=False, timeout=60
) as client:
health = client.get("/health").status_code
assert health == 200
history = TestCompressEndpointLossyInlineMode._read_then_edit()
for mode in [None, "lossy_inline", "lossless"]:
payload = {"messages": history, "model": "gpt-4"}
if mode is not None:
payload["config"] = {"mode": mode}
response = client.post("/v1/compress", json=payload)
assert response.status_code == 200, response.text
data = response.json()
content = data["messages"][2]["content"]
ok = (
content == history[2]["content"]
if mode == "lossless"
else content.startswith("[Read content stale: /x/a.py")
and "hash=" not in content
and not data["ccr_hashes"]
)
cases.append(
{
"case": mode or "default",
"status": response.status_code,
"ok": ok,
"hashes": data["ccr_hashes"],
"read_bytes_preserved": content == history[2]["content"],
"contains_retrieval_hash": "hash=" in content,
"tokens_saved": data["tokens_saved"],
}
)
for marked in (False, True):
for turn in range(1, 4):
messages = _anthropic_turns(turn)
if not marked:
del messages[-1]["content"][-1]["cache_control"]
response = client.post(
"/v1/compress",
json={
"messages": messages,
"model": "claude-sonnet-5",
"config": {"session_id": f"actual-http-{marked}"},
},
)
assert response.status_code == 200, response.text
data = response.json()
count = _count_message_markers(data["messages"])
cases.append(
{
"case": f"session-marked-{marked}-turn-{turn}",
"status": response.status_code,
"markers": count,
"ok": count == (1 if marked else 0),
"tokens_saved": data["tokens_saved"],
}
)
finally:
server.should_exit = True
worker.join(timeout=15)
listener.close()
report = {
"native_path": str(native_path),
"native_sha256": digest,
"port": port,
"health": health,
"cases": cases,
"shutdown_joined": not worker.is_alive(),
"scope": "Actual uvicorn and loopback HTTP with normal endpoint/pipeline;no provider upstream,billing,MLmodel or productiondeployment claim",
}
Path(
os.environ.get("REVIEW4678_RUNTIME_REPORT", str(tmp_path / "runtime-report.json"))
).write_text(json.dumps(report, indent=2))
print("ACTUAL_HTTP_REPORT " + json.dumps(report))
assert not worker.is_alive()
assert any(x["tokens_saved"] > 0 for x in cases), "compression did not execute"
assert all(x["ok"] for x in cases), report
+20
View File
@@ -640,3 +640,23 @@ def test_session_turns_do_not_accumulate_cache_control_markers() -> None:
assert _count_message_markers(returned) == 1, (turn, _count_message_markers(returned))
# Precondition: compression rewrote tool results, so replay was live.
assert saved > 0
def test_session_turns_add_no_marker_the_caller_did_not_send() -> None:
# A caller that sends no breakpoint has chosen not to cache. The returned
# body is forwarded as-is, so a marker added here bills a cache write the
# caller never asked for.
with _make_client() as client:
for turn in range(1, 4):
messages = _anthropic_turns(turn)
del messages[-1]["content"][-1]["cache_control"]
resp = client.post(
"/v1/compress",
json={
"model": "claude-sonnet-5",
"messages": messages,
"config": {"session_id": "markers-none"},
},
)
assert resp.status_code == 200, resp.text
assert _count_message_markers(resp.json()["messages"]) == 0, turn
+38
View File
@@ -514,6 +514,44 @@ class TestCompressEndpointLossyInlineMode:
with TestClient(app, base_url="http://127.0.0.1", client=("127.0.0.1", 12345)) as c:
yield c
@staticmethod
def _read_then_edit():
def call(call_id, name):
return {
"role": "assistant",
"content": None,
"tool_calls": [
{
"id": call_id,
"type": "function",
"function": {"name": name, "arguments": '{"file_path": "/x/a.py"}'},
}
],
}
source = "\n".join(f"def function_{i}(): return {i}" for i in range(400))
return [
{"role": "user", "content": "Change a.py."},
call("call_read", "Read"),
{"role": "tool", "tool_call_id": "call_read", "content": source},
call("call_edit", "Edit"),
{"role": "tool", "tool_call_id": "call_edit", "content": "Edited /x/a.py"},
{"role": "user", "content": "Now what?"},
]
@pytest.mark.parametrize("mode", [None, "lossy_inline"])
def test_marker_free_modes_mark_a_stale_read_without_a_retrieval_hash(self, client, mode):
payload = {"messages": self._read_then_edit(), "model": "gpt-4"}
if mode is not None:
payload["config"] = {"mode": mode}
response = client.post("/v1/compress", json=payload)
assert response.status_code == 200
body = response.json()
stale = body["messages"][2]["content"]
assert stale.startswith("[Read content stale: /x/a.py")
assert "hash=" not in stale
assert body["ccr_hashes"] == []
def test_lossless_mode_folds_search_output_and_keeps_json_rows(self, client):
grep_output = "\n".join(
f"headroom/proxy/handlers/openai.py:{i}: value_{i} = compute(value_{i})"
+9 -2
View File
@@ -194,8 +194,15 @@ class TestActivityDecision:
assert res.newly_matured == 0
assert res.messages is msgs
def test_respects_lifecycle_markers(self):
marker = "[Read content stale: /x/foo.py ... Retrieve original: hash=abc123]" + " " * 2048
@pytest.mark.parametrize(
"notice",
[
"[Read content stale: /x/foo.py ... Retrieve original: hash=abc123]",
"[Read content stale: /x/foo.py was modified after this read]",
],
)
def test_respects_lifecycle_markers(self, notice):
marker = notice + " " * 2048
msgs = [
{"role": "user", "content": "look"},
*anthropic_read("r1", "/x/foo.py", marker),
+61 -45
View File
@@ -701,6 +701,54 @@ class TestNoFilePathHandling:
assert result.messages[1]["content"] == LARGE_CONTENT
def _read_then_edit(read_content: str) -> list[dict]:
return [
{
"role": "assistant",
"content": [
{
"type": "tool_use",
"id": "t1",
"name": "Read",
"input": {"file_path": "/tmp/foo.txt"},
}
],
},
{
"role": "user",
"content": [
{
"type": "tool_result",
"tool_use_id": "t1",
"content": read_content,
}
],
},
# Edit the same file -> the Read above becomes STALE.
{
"role": "assistant",
"content": [
{
"type": "tool_use",
"id": "t2",
"name": "Edit",
"input": {"file_path": "/tmp/foo.txt"},
}
],
},
{
"role": "user",
"content": [
{
"type": "tool_result",
"tool_use_id": "t2",
"content": "edited",
}
],
},
]
class TestContentRouterIntegration:
"""Regression: ContentRouter.transform must wire a real CCR store into
ReadLifecycleManager so STALE Read markers resolve via headroom_retrieve."""
@@ -720,51 +768,7 @@ class TestContentRouterIntegration:
reset_compression_store()
try:
large_content = "source line\n" * 500 # above read_lifecycle min_size_bytes
messages = [
{
"role": "assistant",
"content": [
{
"type": "tool_use",
"id": "t1",
"name": "Read",
"input": {"file_path": "/tmp/foo.txt"},
}
],
},
{
"role": "user",
"content": [
{
"type": "tool_result",
"tool_use_id": "t1",
"content": large_content,
}
],
},
# Edit the same file -> the Read above becomes STALE.
{
"role": "assistant",
"content": [
{
"type": "tool_use",
"id": "t2",
"name": "Edit",
"input": {"file_path": "/tmp/foo.txt"},
}
],
},
{
"role": "user",
"content": [
{
"type": "tool_result",
"tool_use_id": "t2",
"content": "edited",
}
],
},
]
messages = _read_then_edit(large_content)
result = compress(messages, model="claude-sonnet-4-5-20250929")
@@ -788,3 +792,15 @@ class TestContentRouterIntegration:
# Drop the memory-backend singleton so later tests in the suite
# see the env-driven default again.
reset_compression_store()
def test_lossless_router_keeps_a_stale_read(self):
from headroom.providers import OpenAIProvider
from headroom.tokenizer import Tokenizer
from headroom.transforms.content_router import ContentRouter, ContentRouterConfig
tokenizer = Tokenizer(OpenAIProvider().get_token_counter("gpt-4o"), "gpt-4o")
large_content = "source line\n" * 500
router = ContentRouter(ContentRouterConfig(lossless=True))
result = router.apply(_read_then_edit(large_content), tokenizer=tokenizer)
assert result.messages[1]["content"][0]["content"] == large_content