refactor(memory): reuse _summarize_user_message_for_log instead of forking it
The original fix added agent/memory_manager.py:flatten_message_content, but
that helper was a near-exact duplicate of
agent/codex_responses_adapter.py:_summarize_user_message_for_log — same
None/str/list dispatch, same {text,input_text,output_text}/{image_url,input_image}
part sets, the identical [N image(s)] marker, and the same str() fallback. The
only difference was the join separator (newline for memory vs space for the
log/trajectory previews the existing helper already serves), and that helper is
already imported into agent/turn_finalizer.py — the same file whose call site the
memory fix touches.
Parameterize the existing helper with sep=' ' (default preserves every current
logging/trajectory caller byte-for-byte) and call it with sep='\n' at the memory
boundary; drop the forked flatten_message_content. Repoints the unit tests to the
consolidated helper and adds a case locking the default space-join.
Single source of truth for multimodal-content flattening; no behavior change for
the fix or for existing callers.
This commit is contained in:
@@ -982,62 +982,76 @@ class TestMemoryContextFencing:
|
||||
class TestFlattenMessageContent:
|
||||
"""Multimodal message content (list of typed parts) must flatten to a
|
||||
plain string before reaching providers — a raw list crashes their regex
|
||||
sanitization with ``expected string or bytes-like object, got 'list'``."""
|
||||
sanitization with ``expected string or bytes-like object, got 'list'``.
|
||||
|
||||
The memory boundary reuses ``_summarize_user_message_for_log`` (the same
|
||||
helper logging/trajectory use) with ``sep="\\n"`` instead of a forked copy.
|
||||
"""
|
||||
|
||||
def test_string_passthrough(self):
|
||||
from agent.memory_manager import flatten_message_content
|
||||
assert flatten_message_content("hello") == "hello"
|
||||
from agent.codex_responses_adapter import _summarize_user_message_for_log
|
||||
assert _summarize_user_message_for_log("hello", sep="\n") == "hello"
|
||||
|
||||
def test_none_is_empty(self):
|
||||
from agent.memory_manager import flatten_message_content
|
||||
assert flatten_message_content(None) == ""
|
||||
from agent.codex_responses_adapter import _summarize_user_message_for_log
|
||||
assert _summarize_user_message_for_log(None, sep="\n") == ""
|
||||
|
||||
def test_text_parts_joined(self):
|
||||
from agent.memory_manager import flatten_message_content
|
||||
def test_text_parts_joined_with_sep(self):
|
||||
from agent.codex_responses_adapter import _summarize_user_message_for_log
|
||||
content = [
|
||||
{"type": "text", "text": "first"},
|
||||
{"type": "text", "text": "second"},
|
||||
]
|
||||
assert flatten_message_content(content) == "first\nsecond"
|
||||
assert _summarize_user_message_for_log(content, sep="\n") == "first\nsecond"
|
||||
|
||||
def test_default_sep_is_space(self):
|
||||
"""Logging/trajectory callers (the default) keep the space-join."""
|
||||
from agent.codex_responses_adapter import _summarize_user_message_for_log
|
||||
content = [
|
||||
{"type": "text", "text": "first"},
|
||||
{"type": "text", "text": "second"},
|
||||
]
|
||||
assert _summarize_user_message_for_log(content) == "first second"
|
||||
|
||||
def test_image_part_becomes_marker(self):
|
||||
from agent.memory_manager import flatten_message_content
|
||||
from agent.codex_responses_adapter import _summarize_user_message_for_log
|
||||
content = [
|
||||
{"type": "text", "text": "look at this"},
|
||||
{"type": "image_url", "image_url": {"url": "data:image/png;base64,xyz"}},
|
||||
]
|
||||
assert flatten_message_content(content) == "[1 image] look at this"
|
||||
assert _summarize_user_message_for_log(content, sep="\n") == "[1 image] look at this"
|
||||
|
||||
def test_image_only_message(self):
|
||||
from agent.memory_manager import flatten_message_content
|
||||
from agent.codex_responses_adapter import _summarize_user_message_for_log
|
||||
content = [
|
||||
{"type": "image_url", "image_url": {"url": "data:..."}},
|
||||
{"type": "image_url", "image_url": {"url": "data:..."}},
|
||||
]
|
||||
assert flatten_message_content(content) == "[2 images]"
|
||||
assert _summarize_user_message_for_log(content, sep="\n") == "[2 images]"
|
||||
|
||||
def test_unknown_parts_skipped(self):
|
||||
from agent.memory_manager import flatten_message_content
|
||||
from agent.codex_responses_adapter import _summarize_user_message_for_log
|
||||
content = [{"type": "audio", "data": "..."}, {"type": "text", "text": "ok"}, 42]
|
||||
assert flatten_message_content(content) == "ok"
|
||||
assert _summarize_user_message_for_log(content, sep="\n") == "ok"
|
||||
|
||||
def test_bare_strings_in_list(self):
|
||||
from agent.memory_manager import flatten_message_content
|
||||
assert flatten_message_content(["plain", "strings"]) == "plain\nstrings"
|
||||
from agent.codex_responses_adapter import _summarize_user_message_for_log
|
||||
assert _summarize_user_message_for_log(["plain", "strings"], sep="\n") == "plain\nstrings"
|
||||
|
||||
def test_scalar_fallback(self):
|
||||
from agent.memory_manager import flatten_message_content
|
||||
assert flatten_message_content(42) == "42"
|
||||
from agent.codex_responses_adapter import _summarize_user_message_for_log
|
||||
assert _summarize_user_message_for_log(42, sep="\n") == "42"
|
||||
|
||||
def test_flattened_output_is_regex_safe(self):
|
||||
"""The original failure: sanitize_context(list) raised TypeError."""
|
||||
from agent.memory_manager import flatten_message_content, sanitize_context
|
||||
from agent.codex_responses_adapter import _summarize_user_message_for_log
|
||||
from agent.memory_manager import sanitize_context
|
||||
content = [
|
||||
{"type": "text", "text": "fix this bug"},
|
||||
{"type": "image_url", "image_url": {"url": "data:..."}},
|
||||
]
|
||||
# Must not raise.
|
||||
assert sanitize_context(flatten_message_content(content))
|
||||
assert sanitize_context(_summarize_user_message_for_log(content, sep="\n"))
|
||||
|
||||
|
||||
# ---------------------------------------------------------------------------
|
||||
|
||||
Reference in New Issue
Block a user