Skip to content

The agent that grows with you

fix(anthropic): align accounting with native wire

JoaoMarcos4406bdebd04c4b
diff --git a/agent/message_sanitization.py b/agent/message_sanitization.py
index 8ea8eccabb8..4049bea51e5 100644
--- a/agent/message_sanitization.py
+++ b/agent/message_sanitization.py
@@ -659,14 +659,17 @@ def native_anthropic_accounting_projection(messages: Any) -> tuple[Any, tuple[st
projected.append(message)
continue
- shadow = dict(message)
- ordered = shadow.pop("anthropic_content_blocks", None)
- details = shadow.pop("reasoning_details", None)
- shadow.pop("_anthropic_content_blocks", None)
-
- # Native Anthropic conversion never consumes the canonical storage-only
- # reasoning field directly.
- shadow.pop("reasoning", None)
+ # Mirror _convert_assistant_message's actual inputs instead of starting
+ # from canonical storage. Context selection is allowed to return canonical
+ # rows, which can contain timestamp/finish_reason/api_content and other
+ # local metadata that native Anthropic never sees.
+ shadow = {"role": "assistant"}
+ for key in ("content", "tool_calls", "reasoning_content", "cache_control"):
+ if key in message:
+ shadow[key] = message[key]
+
+ ordered = message.get("anthropic_content_blocks")
+ details = message.get("reasoning_details")
ordered_authoritative = isinstance(ordered, list) and any(
isinstance(block, dict) and block.get("type") in replayable_ordered_types
diff --git a/tests/agent/test_anthropic_preserved_thinking_replay.py b/tests/agent/test_anthropic_preserved_thinking_replay.py
index 9995c9e8e4d..a15b2ef002b 100644
--- a/tests/agent/test_anthropic_preserved_thinking_replay.py
+++ b/tests/agent/test_anthropic_preserved_thinking_replay.py
@@ -151,6 +151,17 @@ def _carrier_message():
}
+def _non_tool_carrier_message():
+ message = _carrier_message()
+ message["anthropic_content_blocks"] = [
+ block
+ for block in message["anthropic_content_blocks"]
+ if block.get("type") != "tool_use"
+ ]
+ message.pop("tool_calls", None)
+ return message
+
+
def test_rejected_signature_is_removed_from_every_carrier_and_persists_across_resume():
from agent.anthropic_thinking_replay import (
apply_rejected_thinking_suppression,
@@ -479,7 +490,14 @@ def test_native_accounting_projection_dedupes_ordered_carrier_and_opaque_bytes()
projected_huge, readable = native_anthropic_accounting_projection([huge_opaque])
+ metadata_copy = copy.deepcopy(huge_opaque)
+ metadata_copy["timestamp"] = 1234567890.0
+ metadata_copy["finish_reason"] = "tool_calls"
+ metadata_copy["api_content"] = "storage-only replay sidecar"
+
assert "_anthropic_readable_thinking_estimate" not in repr(projected_huge)
+ projected_metadata, _ = native_anthropic_accounting_projection([metadata_copy])
+ assert projected_metadata == projected_huge
assert readable == ("x" * 8000,)
assert estimate_native_anthropic_messages_tokens_rough(
[huge_opaque]
@@ -524,12 +542,13 @@ def test_preflight_ignores_persisted_rejected_thinking(monkeypatch):
db = _ConfigDB()
first = _assembly_agent(db)
- remember_rejected_thinking(first, [copy.deepcopy(_carrier_message())])
+ carrier = _non_tool_carrier_message()
+ remember_rejected_thinking(first, [copy.deepcopy(carrier)])
resumed = _assembly_agent(db)
history = [
{"role": "user", "content": "Q"},
- _carrier_message(),
+ copy.deepcopy(carrier),
{"role": "user", "content": "continue"},
]
preflight = _preflight_request_tokens(resumed, copy.deepcopy(history), "")