Skip to content

The agent that grows with you

fix: persist a multimodal text part once and record its spill path for the stub guard

teknium1ea51325186fe
diff --git a/agent/tool_executor.py b/agent/tool_executor.py
index 8afeb67f3be..1545d932b87 100644
--- a/agent/tool_executor.py
+++ b/agent/tool_executor.py
@@ -67,7 +67,11 @@ def _tc_name(tool_call: Any) -> str:
def _record_persisted_path_for_stub(agent, tool_call_id: str, function_result) -> None:
"""Record the spillover file path so a later result-reference stub can't dangle (best-effort)."""
try:
- path = extract_persisted_path(function_result) if isinstance(function_result, str) else None
+ candidates = [function_result] if isinstance(function_result, str) else [
+ function_result.get("text_summary"),
+ *(p.get("text") for p in function_result.get("content") or [] if isinstance(p, dict)),
+ ] if _is_multimodal_tool_result(function_result) else []
+ path = next((p for p in map(extract_persisted_path, candidates) if p), None)
if path:
agent._tool_guardrails.record_persisted_result(tool_call_id, path)
except Exception as exc:
@@ -1104,22 +1108,24 @@ def _persist_multimodal_text_parts(result: dict, tool_name: str, tool_call_id: s
entirely and ride every later request inline. Image parts are left untouched (their size is
governed by the vision embed budget); a fresh dict is returned so history is never mutated."""
parts = result.get("content") or []
- bounded_parts, changed = [], False
+ bounded_parts, first_replacement = [], None
for part in parts:
text = part.get("text") if isinstance(part, dict) and part.get("type") == "text" else None
if isinstance(text, str):
replaced = maybe_persist_tool_result(content=text, tool_name=tool_name, tool_use_id=tool_call_id,
env=env, config=budget)
if replaced != text:
- part, changed = {**part, "text": replaced}, True
+ part = {**part, "text": replaced}
+ first_replacement = first_replacement or replaced
bounded_parts.append(part)
- if not changed:
+ if first_replacement is None:
return result
bounded = {**result, "content": bounded_parts}
summary = bounded.get("text_summary")
- if isinstance(summary, str):
- bounded["text_summary"] = maybe_persist_tool_result(content=summary, tool_name=tool_name,
- tool_use_id=tool_call_id, env=env, config=budget)
+ # The summary is a subset of the (already spilled) part text: reuse that bounded reference instead
+ # of a second persist under the same id, which would overwrite the spill file with the summary.
+ if isinstance(summary, str) and len(summary) > budget.resolve_threshold(tool_name):
+ bounded["text_summary"] = first_replacement
return bounded
diff --git a/tests/agent/test_multimodal_tool_result_spill.py b/tests/agent/test_multimodal_tool_result_spill.py
index 0c573bc8391..5f34e79fa4d 100644
--- a/tests/agent/test_multimodal_tool_result_spill.py
+++ b/tests/agent/test_multimodal_tool_result_spill.py
@@ -26,8 +26,9 @@ def _run_sequential(agent, function_result):
def _envelope(text: str) -> dict:
+ # Real browser_exec shape: the content text carries an extra screenshot note the summary lacks.
return {"_multimodal": True, "text_summary": text, "meta": {"screenshot_path": "/tmp/shot.png"},
- "content": [{"type": "text", "text": text},
+ "content": [{"type": "text", "text": text + "\nscreenshot captured"},
{"type": "image_url", "image_url": {"url": "data:image/jpeg;base64,QUJD"}}]}
@@ -35,20 +36,23 @@ def test_oversized_multimodal_text_part_is_spilled_and_recoverable(tmp_path, mon
monkeypatch.setenv("HERMES_HOME", str(tmp_path))
monkeypatch.setattr("hermes_constants.get_hermes_home", lambda: tmp_path, raising=False)
big = "x" * 760_396
- (tool_msg,) = _run_sequential(_make_agent(), _envelope(big))
+ agent = _make_agent()
+ (tool_msg,) = _run_sequential(agent, _envelope(big))
content = tool_msg["content"]
assert isinstance(content, list)
texts = [p["text"] for p in content if isinstance(p, dict) and p.get("type") == "text"]
assert len(texts) == 1 and PERSISTED_OUTPUT_TAG in texts[0] and len(texts[0]) < 10_000
assert any(p.get("type") == "image_url" for p in content) # image part untouched
- # The full text is recoverable from the spillover file.
- spilled = list(Path(tmp_path, "cache", "spillover").glob("*"))
- assert spilled and spilled[0].read_text() == big
+ # One spill file holding the full content text (not overwritten by a second text_summary write),
+ # and the duplicate-result stub guard knows where it lives.
+ (spilled,) = Path(tmp_path, "cache", "spillover").glob("*")
+ assert spilled.read_text() == big + "\nscreenshot captured"
+ assert agent._tool_guardrails._persisted_result_paths == {"call_browser": str(spilled)}
def test_normal_multimodal_result_is_unchanged(tmp_path, monkeypatch):
monkeypatch.setenv("HERMES_HOME", str(tmp_path))
small = "snapshot ok"
(tool_msg,) = _run_sequential(_make_agent(), _envelope(small))
- assert tool_msg["content"][0] == {"type": "text", "text": small}
+ assert tool_msg["content"][0] == {"type": "text", "text": small + "\nscreenshot captured"}
assert not Path(tmp_path, "cache", "spillover").exists()