Skip to content

The agent that grows with you

fix(gemini): strip call ids on insert, name the realignments in the log

Yong Lif41ed09b5113
diff --git a/agent/agent_runtime_helpers.py b/agent/agent_runtime_helpers.py
index e01ffcd4348..ffd9ba61fe8 100644
--- a/agent/agent_runtime_helpers.py
+++ b/agent/agent_runtime_helpers.py
@@ -4570,15 +4570,21 @@ def sanitize_api_messages(messages: List[Dict[str, Any]]) -> List[Dict[str, Any]
# name. Runs on the per-call copy, so the stored trajectory keeps the real
# tool name for the session DB and the UI — only the wire payload changes.
# A no-op for the native Gemini path, which already resolves the same name.
+ # A result whose assistant call frame is missing entirely never reaches
+ # here — pass 1 above drops it as an orphan — so the only results this pass
+ # sees are ones whose call name is knowable.
call_names: Dict[str, str] = {}
for msg in messages:
if msg.get("role") == "assistant":
for tc in msg.get("tool_calls") or []:
- cid = _ra().AIAgent._get_tool_call_id_static(tc)
+ # Strip on insert to match the lookup below (and pass 1's
+ # ``result_call_ids``), so an id that arrives padded still
+ # pairs instead of silently skipping realignment.
+ cid = (_ra().AIAgent._get_tool_call_id_static(tc) or "").strip()
nm = _ra().AIAgent._get_tool_call_name_static(tc)
if cid and nm:
call_names[cid] = nm
- realigned = 0
+ realigned: List[Tuple[str, str]] = []
aligned: List[Dict[str, Any]] = []
for msg in messages:
if msg.get("role") == "tool":
@@ -4591,14 +4597,15 @@ def sanitize_api_messages(messages: List[Dict[str, Any]]) -> List[Dict[str, Any]
# must still pass through byte-identical for prompt caching.
if expected and current and current != expected:
msg = {**msg, "name": expected}
- realigned += 1
+ realigned.append((current, expected))
aligned.append(msg)
if realigned:
messages = aligned
_ra().logger.debug(
"Pre-call sanitizer: realigned %d tool result name(s) with their "
- "tool_call function name",
- realigned,
+ "tool_call function name (%s)",
+ len(realigned),
+ ", ".join(f"{was} -> {now}" for was, now in realigned),
)
return messages
diff --git a/tests/run_agent/test_message_sequence_repair.py b/tests/run_agent/test_message_sequence_repair.py
index 9bacbaae724..32a0ef845a4 100644
--- a/tests/run_agent/test_message_sequence_repair.py
+++ b/tests/run_agent/test_message_sequence_repair.py
@@ -1429,8 +1429,8 @@ def test_sanitize_realigns_bridged_tool_result_name_with_call_name():
assert messages[2]["name"] == "mcp__github__create_issue"
-def test_sanitize_leaves_matching_and_unpaired_tool_result_names_alone():
- """Directly-called tools already agree; an unpaired name is left as-is."""
+def test_sanitize_leaves_already_matching_tool_result_name_alone():
+ """A directly-called tool already agrees — nothing to rewrite."""
from agent.agent_runtime_helpers import sanitize_api_messages
messages = [
@@ -1462,3 +1462,35 @@ def test_sanitize_does_not_invent_a_name_on_unnamed_tool_results():
]
out = sanitize_api_messages(list(messages))
assert out == messages
+
+
+def test_sanitize_realigns_bridged_name_when_call_id_is_padded():
+ """Ids are stripped on both sides, so a padded id still pairs."""
+ from agent.agent_runtime_helpers import sanitize_api_messages
+
+ messages = [
+ {"role": "user", "content": "file an issue"},
+ {"role": "assistant", "content": "",
+ "tool_calls": [{"id": " call_1 ", "type": "function",
+ "function": {"name": "tool_call", "arguments": "{}"}}]},
+ {"role": "tool", "name": "mcp__github__create_issue",
+ "tool_call_id": "call_1", "content": "{}"},
+ ]
+ out = sanitize_api_messages(list(messages))
+ assert [m["name"] for m in out if m.get("role") == "tool"] == ["tool_call"]
+
+
+def test_sanitize_drops_bridged_result_whose_call_frame_was_pruned():
+ """A result cannot keep a stale name if it has no call frame to disagree
+ with: the orphan pass removes it before the realignment pass runs, so the
+ mismatch never reaches the provider."""
+ from agent.agent_runtime_helpers import sanitize_api_messages
+
+ messages = [
+ {"role": "user", "content": "file an issue"},
+ # assistant tool_calls frame dropped by compression
+ {"role": "tool", "name": "mcp__github__create_issue",
+ "tool_call_id": "call_1", "content": "{}"},
+ ]
+ out = sanitize_api_messages(list(messages))
+ assert [m.get("role") for m in out] == ["user"]