fix(memory): flatten multimodal content before provider sync

Multimodal turns carry message content as a list of typed parts
({type: "text"|"image_url", ...}). _sync_external_memory_for_turn
passed that list straight into MemoryManager.sync_all, and providers
feed it to regexes — Honcho's sync_turn calls sanitize_context, where
re.sub raised 'expected string or bytes-like object, got list'. Every
turn with an attached image silently never synced.

Flatten to plain text at the boundary: text parts joined, images noted
as an [N image(s)] marker so the attachment isn't erased from recall.
Fixing here covers all providers instead of patching each plugin.
This commit is contained in:
Erosika
2026-06-11 18:53:26 -04:00
parent 2ee69d0579
commit 705bdb6ffe
4 changed files with 161 additions and 4 deletions
@@ -207,6 +207,57 @@ class TestSyncExternalMemoryForTurn:
# sync_all still happened before the prefetch blew up.
agent._memory_manager.sync_all.assert_called_once()
# --- Multimodal content flattening ----------------------------------
def test_multimodal_user_message_is_flattened(self):
"""A turn with an attached image carries the user message as a
list of typed parts. Providers feed the content to regexes
(sanitize_context), so a raw list raised ``expected string or
bytes-like object, got 'list'`` and the turn silently never
synced. The boundary must flatten to text first."""
agent = _bare_agent()
agent._sync_external_memory_for_turn(
original_user_message=[
{"type": "text", "text": "what is in this screenshot?"},
{"type": "image_url", "image_url": {"url": "data:image/png;base64,abc"}},
],
final_response="A terminal window showing a stack trace.",
interrupted=False,
)
agent._memory_manager.sync_all.assert_called_once_with(
"[1 image] what is in this screenshot?",
"A terminal window showing a stack trace.",
session_id="test_session_001",
)
agent._memory_manager.queue_prefetch_all.assert_called_once_with(
"[1 image] what is in this screenshot?",
session_id="test_session_001",
)
def test_multimodal_response_is_flattened(self):
agent = _bare_agent()
agent._sync_external_memory_for_turn(
original_user_message="describe it",
final_response=[{"type": "text", "text": "a cat"}],
interrupted=False,
)
agent._memory_manager.sync_all.assert_called_once_with(
"describe it", "a cat",
session_id="test_session_001",
)
def test_multimodal_with_no_text_at_all_skips(self):
"""Unknown-typed parts flatten to an empty string — don't sync a
turn with no recoverable text."""
agent = _bare_agent()
agent._sync_external_memory_for_turn(
original_user_message=[{"type": "audio", "data": "..."}],
final_response="noted",
interrupted=False,
)
agent._memory_manager.sync_all.assert_not_called()
agent._memory_manager.queue_prefetch_all.assert_not_called()
# --- The specific matrix the reporter asked about ------------------
@pytest.mark.parametrize("interrupted,final,user,expect_sync", [