fix(loop): strip input_audio and video_url before session persistence

Code review found that _sanitize_persisted_blocks only stripped
image_url blocks, causing base64-encoded audio/video payloads to
bloat session history files.

- Extend _sanitize_persisted_blocks to replace input_audio and
  video_url blocks with text placeholders using LLMProvider._media_placeholder.
- Add tests for audio/video stripping with and without _meta.path.

All 3267 tests pass.
This commit is contained in:
chengyongru 2026-05-20 11:56:34 +08:00
parent 0e754d2591
commit d55f6d63c8
2 changed files with 48 additions and 0 deletions

View File

@ -1422,6 +1422,10 @@ class AgentLoop:
filtered.append({"type": "text", "text": image_placeholder_text(path)}) filtered.append({"type": "text", "text": image_placeholder_text(path)})
continue continue
if block.get("type") in ("input_audio", "video_url"):
filtered.append(LLMProvider._media_placeholder(block["type"], block))
continue
if block.get("type") == "text" and isinstance(block.get("text"), str): if block.get("type") == "text" and isinstance(block.get("text"), str):
text = block["text"] text = block["text"]
if should_truncate_text and len(text) > self.max_tool_result_chars: if should_truncate_text and len(text) > self.max_tool_result_chars:

View File

@ -29,3 +29,47 @@ def test_sanitize_persisted_blocks_truncate_text_shadowing_regression() -> None:
assert isinstance(out[0]["text"], str) assert isinstance(out[0]["text"], str)
assert out[0]["text"] != content[0]["text"] assert out[0]["text"] != content[0]["text"]
def test_sanitize_persisted_blocks_strips_audio_and_video() -> None:
"""Audio and video blocks with base64 payloads must be replaced with placeholders."""
from nanobot.agent.loop import AgentLoop
dummy = SimpleNamespace(max_tool_result_chars=1000)
content = [
{"type": "text", "text": "analyze this"},
{
"type": "input_audio",
"input_audio": {"data": "aGVsbG8=", "format": "wav"},
"_meta": {"path": "/tmp/voice.wav"},
},
{
"type": "video_url",
"video_url": {"url": "data:video/mp4;base64,aGVsbG8="},
"_meta": {"path": "/tmp/clip.mp4"},
},
]
out = AgentLoop._sanitize_persisted_blocks(dummy, content)
assert len(out) == 3
assert out[0] == content[0]
assert out[1] == {"type": "text", "text": "[audio: /tmp/voice.wav]"}
assert out[2] == {"type": "text", "text": "[video: /tmp/clip.mp4]"}
def test_sanitize_persisted_blocks_strips_audio_video_without_meta() -> None:
"""When _meta is absent, fallback placeholders use bare label."""
from nanobot.agent.loop import AgentLoop
dummy = SimpleNamespace(max_tool_result_chars=1000)
content = [
{"type": "input_audio", "input_audio": {"data": "aGVsbG8=", "format": "wav"}},
{"type": "video_url", "video_url": {"url": "data:video/mp4;base64,aGVsbG8="}},
]
out = AgentLoop._sanitize_persisted_blocks(dummy, content)
assert len(out) == 2
assert out[0] == {"type": "text", "text": "[audio]"}
assert out[1] == {"type": "text", "text": "[video]"}