mirror of
https://github.com/headroomlabs-ai/headroom.git
synced 2026-11-22 13:29:19 +08:00
fix(interceptors): guard shared tool content rewrites
This commit is contained in:
Vendored
+29
-14
@@ -76,14 +76,38 @@ def _extract_tool_result_content(msg: dict) -> str | None:
|
||||
return None
|
||||
|
||||
|
||||
def _can_swap_tool_result_content(msg: dict) -> bool:
|
||||
"""Whether replacing extracted text preserves the OpenAI content shape.
|
||||
|
||||
A joined replacement cannot be mapped back to multiple blocks without
|
||||
losing boundaries, metadata or media. All rewrite callers must check this
|
||||
before extraction, transformation or savings accounting. Anthropic's
|
||||
existing tool-result rewrite contract is unchanged.
|
||||
"""
|
||||
if msg.get("role") != "tool":
|
||||
return True
|
||||
content = msg.get("content")
|
||||
if isinstance(content, str):
|
||||
return True
|
||||
return (
|
||||
isinstance(content, list)
|
||||
and len(content) == 1
|
||||
and isinstance(content[0], dict)
|
||||
and content[0].get("type") == "text"
|
||||
and isinstance(content[0].get("text"), str)
|
||||
)
|
||||
|
||||
|
||||
def _swap_tool_result_content(msg: dict, new_content: str) -> dict:
|
||||
"""Deep copy msg and replace tool result content with new_content."""
|
||||
"""Deep copy and rewrite supported content; preserve unsupported layouts."""
|
||||
new_msg = copy.deepcopy(msg)
|
||||
if not _can_swap_tool_result_content(msg):
|
||||
return new_msg
|
||||
# OpenAI format
|
||||
if new_msg.get("role") == "tool":
|
||||
content = new_msg.get("content")
|
||||
if isinstance(content, list):
|
||||
# apply_cached only rewrites an unambiguous single text block.
|
||||
# The shared guard permits only an unambiguous single text block.
|
||||
# Keep its shape and metadata, including gateway cache breakpoints.
|
||||
content[0]["text"] = new_content
|
||||
else:
|
||||
@@ -347,18 +371,9 @@ class CompressionCache:
|
||||
with self._lock:
|
||||
result: list[dict] = []
|
||||
for msg in messages:
|
||||
if msg.get("role") == "tool" and isinstance(msg.get("content"), list):
|
||||
blocks = msg["content"]
|
||||
# A cached joined string cannot be mapped back to multiple
|
||||
# blocks without losing boundaries, metadata or media.
|
||||
if not (
|
||||
len(blocks) == 1
|
||||
and isinstance(blocks[0], dict)
|
||||
and blocks[0].get("type") == "text"
|
||||
and isinstance(blocks[0].get("text"), str)
|
||||
):
|
||||
result.append(msg)
|
||||
continue
|
||||
if not _can_swap_tool_result_content(msg):
|
||||
result.append(msg)
|
||||
continue
|
||||
if _is_tool_result_message(msg):
|
||||
content = _extract_tool_result_content(msg)
|
||||
if content is not None:
|
||||
|
||||
@@ -9,6 +9,7 @@ from dataclasses import dataclass
|
||||
from typing import Any, Protocol, runtime_checkable
|
||||
|
||||
from headroom.cache.compression_cache import (
|
||||
_can_swap_tool_result_content,
|
||||
_extract_tool_result_content,
|
||||
_is_tool_result_message,
|
||||
_swap_tool_result_content,
|
||||
@@ -253,6 +254,10 @@ def apply_to_messages(
|
||||
new_messages.append(msg)
|
||||
continue
|
||||
|
||||
if not _can_swap_tool_result_content(msg):
|
||||
new_messages.append(msg)
|
||||
continue
|
||||
|
||||
original = _extract_tool_result_content(msg)
|
||||
if not isinstance(original, str) or not original:
|
||||
new_messages.append(msg)
|
||||
|
||||
@@ -844,3 +844,33 @@ def test_get_compression_cache_returns_same_instance_under_contention() -> None:
|
||||
first = results[0]
|
||||
for c in results[1:]:
|
||||
assert c is first, "Concurrent _get_compression_cache returned different instances"
|
||||
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
"content",
|
||||
[
|
||||
[{"type": "text", "text": "first"}, {"type": "text", "text": "second"}],
|
||||
[
|
||||
{"type": "image_url", "image_url": {"url": "data:image/png;base64,AA=="}},
|
||||
{"type": "text", "text": "second"},
|
||||
],
|
||||
[],
|
||||
["not a block"],
|
||||
[{"type": "text"}],
|
||||
[{"type": "text", "text": 42}],
|
||||
],
|
||||
)
|
||||
def test_shared_swap_and_cache_preserve_unsupported_openai_blocks(content):
|
||||
from copy import deepcopy
|
||||
|
||||
from headroom.cache.compression_cache import _swap_tool_result_content
|
||||
|
||||
message = {"role": "tool", "content": content}
|
||||
original = deepcopy(message)
|
||||
assert _swap_tool_result_content(message, "short") == original
|
||||
cache = CompressionCache()
|
||||
cache.store_compressed(cache.content_hash("first\nsecond"), "short", tokens_saved=10)
|
||||
before = cache.get_stats().copy()
|
||||
assert cache.apply_cached([message]) == [original]
|
||||
assert cache.get_stats() == before
|
||||
assert message == original
|
||||
|
||||
@@ -528,8 +528,12 @@ class TestSwapToolResultListContent:
|
||||
from headroom.cache.compression_cache import _swap_tool_result_content
|
||||
|
||||
msg = _make_openai_tool_list_content_msg("tc1", ["original"])
|
||||
msg["content"][0]["cache_control"] = {"type": "ephemeral"}
|
||||
swapped = _swap_tool_result_content(msg, "compressed")
|
||||
assert swapped["content"] == "compressed"
|
||||
assert swapped["content"] == [
|
||||
{"type": "text", "text": "compressed", "cache_control": {"type": "ephemeral"}}
|
||||
]
|
||||
assert msg["content"][0]["text"] == "original"
|
||||
|
||||
def test_swap_does_not_mutate_original(self):
|
||||
"""_swap_tool_result_content performs a deep copy."""
|
||||
|
||||
@@ -1307,3 +1307,100 @@ def test_proxy_pipeline_excludes_interceptor_when_env_not_set(monkeypatch):
|
||||
for pipeline in (proxy.anthropic_pipeline, proxy.openai_pipeline):
|
||||
transforms = pipeline.transforms
|
||||
assert not any(isinstance(t, ToolResultInterceptorTransform) for t in transforms)
|
||||
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
"content",
|
||||
[
|
||||
[
|
||||
{"type": "text", "text": "first long block " * 20},
|
||||
{"type": "text", "text": "second long block " * 20},
|
||||
],
|
||||
[
|
||||
{"type": "image_url", "image_url": {"url": "data:image/png;base64,AA=="}},
|
||||
{"type": "text", "text": "long text " * 40},
|
||||
],
|
||||
[],
|
||||
["not a block"],
|
||||
[{"type": "text"}],
|
||||
[{"type": "text", "text": 42}],
|
||||
],
|
||||
)
|
||||
def test_unsupported_openai_blocks_skip_interceptors_and_savings(content, tokenizer, monkeypatch):
|
||||
from copy import deepcopy
|
||||
|
||||
class Shortener:
|
||||
name = "shape-regression"
|
||||
calls = 0
|
||||
|
||||
def progressive_disclosure_key(self, *_):
|
||||
self.calls += 1
|
||||
return "same-key"
|
||||
|
||||
def matches(self, *_):
|
||||
self.calls += 1
|
||||
return True
|
||||
|
||||
def transform(self, *_):
|
||||
self.calls += 1
|
||||
return "short"
|
||||
|
||||
interceptor = Shortener()
|
||||
monkeypatch.setattr("headroom.proxy.interceptors.base.INTERCEPTORS", [interceptor])
|
||||
unsupported = {"role": "tool", "tool_call_id": "1", "content": content}
|
||||
snapshot = deepcopy(unsupported)
|
||||
result = apply_to_messages([unsupported], tokenizer)
|
||||
assert result.messages == [snapshot]
|
||||
assert result.messages[0] is unsupported
|
||||
assert result.spans == []
|
||||
assert interceptor.calls == 0
|
||||
assert unsupported == snapshot
|
||||
pipeline_result = ToolResultInterceptorTransform().apply([unsupported], tokenizer)
|
||||
assert pipeline_result.messages == [snapshot]
|
||||
assert pipeline_result.transforms_applied == []
|
||||
assert pipeline_result.tokens_before == pipeline_result.tokens_after
|
||||
assert interceptor.calls == 0
|
||||
|
||||
# A skipped layout must not consume progressive-disclosure state.
|
||||
valid = {"role": "tool", "tool_call_id": "2", "content": "long output " * 40}
|
||||
result = apply_to_messages([unsupported, valid], tokenizer)
|
||||
assert result.messages[0] == snapshot
|
||||
assert result.messages[1]["content"] == "short"
|
||||
assert len(result.spans) == 1
|
||||
assert interceptor.calls == 3
|
||||
|
||||
|
||||
def test_single_openai_text_block_interceptor_preserves_metadata(tokenizer, monkeypatch):
|
||||
from copy import deepcopy
|
||||
|
||||
class Shortener:
|
||||
name = "metadata-regression"
|
||||
|
||||
def matches(self, *_):
|
||||
return True
|
||||
|
||||
def transform(self, *_):
|
||||
return "short"
|
||||
|
||||
monkeypatch.setattr("headroom.proxy.interceptors.base.INTERCEPTORS", [Shortener()])
|
||||
message = {
|
||||
"role": "tool",
|
||||
"tool_call_id": "1",
|
||||
"content": [
|
||||
{
|
||||
"type": "text",
|
||||
"text": "long output " * 40,
|
||||
"cache_control": {"type": "ephemeral", "ttl": "1h"},
|
||||
"metadata": {"nested": [1, 2]},
|
||||
}
|
||||
],
|
||||
}
|
||||
original = deepcopy(message)
|
||||
result = apply_to_messages([message], tokenizer)
|
||||
expected = deepcopy(message)
|
||||
expected["content"][0]["text"] = "short"
|
||||
assert result.messages == [expected]
|
||||
assert len(result.spans) == 1
|
||||
assert result.spans[0].tokens_saved > 0
|
||||
result.messages[0]["content"][0]["metadata"]["nested"].append(3)
|
||||
assert message == original
|
||||
|
||||
Reference in New Issue
Block a user