mirror of
https://github.com/headroomlabs-ai/headroom.git
synced 2026-11-22 13:29:19 +08:00
fix(router): compress tool_result blocks in mutable window with trailing messages
Allow tool_result blocks in the mutable turn suffix to be compressed under prefix_replay_guaranteed even when trailing messages are present in the turn, while preserving byte-exact prefix caching for earlier turns. Co-authored-by: MartinMayday <MartinMayday@users.noreply.github.com>
This commit is contained in:
co-authored by
MartinMayday
parent
bb8d18a56a
commit
fc59555493
@@ -6559,6 +6559,14 @@ class ContentRouter(Transform):
|
||||
),
|
||||
num_messages,
|
||||
)
|
||||
last_assistant_index = max(
|
||||
(
|
||||
idx
|
||||
for idx, msg in enumerate(messages)
|
||||
if isinstance(msg, dict) and msg.get("role") == "assistant"
|
||||
),
|
||||
default=-1,
|
||||
)
|
||||
model_limit = kwargs.get("model_limit", 0)
|
||||
|
||||
# Adaptive Read protection: protect a fraction of recent messages
|
||||
@@ -6668,6 +6676,7 @@ class ContentRouter(Transform):
|
||||
analysis_intent = self._detect_analysis_intent(messages)
|
||||
|
||||
frozen_message_count = kwargs.get("frozen_message_count", 0)
|
||||
mutable_turn_start = max(frozen_message_count, last_assistant_index + 1)
|
||||
|
||||
# ------------------------------------------------------------------
|
||||
# Two-pass parallel compression.
|
||||
@@ -6823,6 +6832,7 @@ class ContentRouter(Transform):
|
||||
compress_assistant_text_blocks=compress_assistant_text_blocks,
|
||||
prefix_replay_guaranteed=prefix_replay_guaranteed,
|
||||
protect_prompt_text=prompt_turn,
|
||||
in_mutable_window=(i >= mutable_turn_start),
|
||||
)
|
||||
result_slots[i] = transformed_message
|
||||
route_counts["content_blocks"] += 1
|
||||
@@ -7885,6 +7895,7 @@ class ContentRouter(Transform):
|
||||
compress_assistant_text_blocks: bool = False,
|
||||
prefix_replay_guaranteed: bool = False,
|
||||
protect_prompt_text: bool = False,
|
||||
in_mutable_window: bool | None = None,
|
||||
) -> dict[str, Any]:
|
||||
"""Process content blocks (Anthropic format) for compression.
|
||||
|
||||
@@ -7987,14 +7998,13 @@ class ContentRouter(Transform):
|
||||
else:
|
||||
protect_text_blocks = True
|
||||
|
||||
# The final user/tool message is this turn's fresh content: not yet
|
||||
# forwarded, so its cache_control marker names a key the provider has
|
||||
# not written. Everything earlier may already be cached under its
|
||||
# marker and stays byte-exact (contract 1). Only tool_result blocks
|
||||
# are released; a marked text block is the user's prompt.
|
||||
fresh_turn = (
|
||||
prefix_replay_guaranteed and messages_from_end == 1 and role in ("user", "tool")
|
||||
)
|
||||
# User/tool messages in the mutable turn suffix are this turn's
|
||||
# fresh content: not yet forwarded, so a cache_control marker names a key
|
||||
# the provider has not written. Everything earlier may already be cached
|
||||
# under its marker and stays byte-exact (contract 1). Only tool_result
|
||||
# blocks are released; a marked text block is the user's prompt.
|
||||
is_fresh = in_mutable_window if in_mutable_window is not None else (messages_from_end == 1)
|
||||
fresh_turn = prefix_replay_guaranteed and is_fresh and role in ("user", "tool")
|
||||
|
||||
for block in content_blocks:
|
||||
if not isinstance(block, dict):
|
||||
|
||||
@@ -0,0 +1,119 @@
|
||||
from __future__ import annotations
|
||||
|
||||
from types import SimpleNamespace
|
||||
|
||||
import pytest
|
||||
|
||||
from headroom.transforms.content_router import (
|
||||
CompressionStrategy,
|
||||
ContentRouter,
|
||||
ContentRouterConfig,
|
||||
)
|
||||
|
||||
|
||||
class _TestTokenizer:
|
||||
def count_tokens(self, text: str) -> int:
|
||||
return max(1, len(text) // 4)
|
||||
|
||||
def count_text(self, text: str) -> int:
|
||||
return max(1, len(text) // 4)
|
||||
|
||||
def count_message_tokens(self, msg: dict) -> int:
|
||||
return 10
|
||||
|
||||
|
||||
def test_tool_result_compressed_with_trailing_messages_in_mutable_window(
|
||||
monkeypatch: pytest.MonkeyPatch,
|
||||
) -> None:
|
||||
router = ContentRouter(ContentRouterConfig())
|
||||
|
||||
def fake_compress(content, context="", bias=1.0, precomputed_detection=None):
|
||||
return SimpleNamespace(
|
||||
compressed=content[: len(content) // 2] + "[compressed]",
|
||||
compression_ratio=0.5,
|
||||
strategy_used=CompressionStrategy.LOG,
|
||||
)
|
||||
|
||||
monkeypatch.setattr(router, "compress", fake_compress)
|
||||
tool_output = "tool output line " * 200
|
||||
|
||||
# Issue #4708: Anthropic tool_result block followed by trailing message in the mutable turn.
|
||||
messages = [
|
||||
{"role": "user", "content": "read the module"},
|
||||
{
|
||||
"role": "assistant",
|
||||
"content": [{"type": "tool_use", "id": "t1", "name": "bash", "input": {}}],
|
||||
},
|
||||
{
|
||||
"role": "user",
|
||||
"content": [
|
||||
{
|
||||
"type": "tool_result",
|
||||
"tool_use_id": "t1",
|
||||
"content": tool_output,
|
||||
"cache_control": {"type": "ephemeral"},
|
||||
}
|
||||
],
|
||||
},
|
||||
{"role": "user", "content": "what failed in the test above?"},
|
||||
]
|
||||
|
||||
result = router.apply(
|
||||
messages,
|
||||
_TestTokenizer(),
|
||||
model_limit=100_000,
|
||||
prefix_replay_guaranteed=True,
|
||||
)
|
||||
compressed_block = result.messages[2]["content"][0]
|
||||
assert compressed_block["content"].endswith("[compressed]"), (
|
||||
"tool_result followed by trailing message must be compressed in mutable turn"
|
||||
)
|
||||
assert compressed_block["cache_control"] == {"type": "ephemeral"}
|
||||
|
||||
|
||||
def test_tool_result_in_frozen_prefix_not_mutated(
|
||||
monkeypatch: pytest.MonkeyPatch,
|
||||
) -> None:
|
||||
router = ContentRouter(ContentRouterConfig())
|
||||
|
||||
def fake_compress(content, context="", bias=1.0, precomputed_detection=None):
|
||||
return SimpleNamespace(
|
||||
compressed=content[: len(content) // 2] + "[compressed]",
|
||||
compression_ratio=0.5,
|
||||
strategy_used=CompressionStrategy.LOG,
|
||||
)
|
||||
|
||||
monkeypatch.setattr(router, "compress", fake_compress)
|
||||
tool_output = "tool output line " * 200
|
||||
|
||||
# Turn 1 tool_result is part of frozen prefix (frozen_message_count=3).
|
||||
messages = [
|
||||
{"role": "user", "content": "read the module"},
|
||||
{
|
||||
"role": "assistant",
|
||||
"content": [{"type": "tool_use", "id": "t1", "name": "bash", "input": {}}],
|
||||
},
|
||||
{
|
||||
"role": "user",
|
||||
"content": [
|
||||
{
|
||||
"type": "tool_result",
|
||||
"tool_use_id": "t1",
|
||||
"content": tool_output,
|
||||
"cache_control": {"type": "ephemeral"},
|
||||
}
|
||||
],
|
||||
},
|
||||
{"role": "assistant", "content": "tests failed with error"},
|
||||
{"role": "user", "content": "now fix it"},
|
||||
]
|
||||
|
||||
result = router.apply(
|
||||
messages,
|
||||
_TestTokenizer(),
|
||||
model_limit=100_000,
|
||||
frozen_message_count=3,
|
||||
prefix_replay_guaranteed=True,
|
||||
)
|
||||
# Message 2 is frozen (index 2 < frozen_message_count=3), must not be mutated
|
||||
assert result.messages[2]["content"][0]["content"] == tool_output
|
||||
Reference in New Issue
Block a user