mirror of
https://github.com/agentscope-ai/ReMe.git
synced 2026-08-28 05:25:04 +00:00
* fix(auto-memory): preserve message timestamps * fix(auto-memory): infer daily date from messages * feat(file_io): add strict date parsing and improve daily date handling - Add new parse_daily_date function for strict YYYY-MM-DD validation - Replace extract_daily_date with parse_daily_date for explicit date validation - Change _messages_day to use max date instead of min for historical imports - Reorder imports to maintain consistent module ordering - Move session message saving after date validation in auto_memory - Add comprehensive tests for invalid date rejection before saving - Add tests for strict YYYY-MM-DD date format validation - Update test names to reflect latest date behavior --------- Co-authored-by: Ziyang Guo <121015044+RunMarshal@users.noreply.github.com> Co-authored-by: jinli.yl <jinli.yl@alibaba-inc.com>
138 lines
4.5 KiB
Python
138 lines
4.5 KiB
Python
"""Tests for shared evolve helpers."""
|
|
|
|
# pylint: disable=protected-access
|
|
|
|
from agentscope.message import Msg
|
|
|
|
from reme.steps.evolve._evolve import agent_reply_result_text, format_history
|
|
from reme.steps.evolve.auto_memory import AutoMemoryStep, _sanitize_msg_for_save
|
|
|
|
|
|
def test_agent_reply_result_text_uses_last_text_block():
|
|
"""Agent reply text should be the last text block in the final message."""
|
|
reply = {
|
|
"result": "intermediate text\nfinal text",
|
|
"last_message": {
|
|
"content": [
|
|
{"type": "thinking", "thinking": "hidden"},
|
|
{"type": "text", "text": "intermediate text"},
|
|
{"type": "tool_call", "name": "write"},
|
|
{"type": "text", "text": "final text"},
|
|
],
|
|
},
|
|
}
|
|
|
|
assert agent_reply_result_text(reply) == "final text"
|
|
|
|
|
|
def test_agent_reply_result_text_falls_back_to_result():
|
|
"""Agent reply text falls back to result when no text block exists."""
|
|
reply = {"result": " fallback text \n", "last_message": {"content": [{"type": "thinking"}]}}
|
|
|
|
assert agent_reply_result_text(reply) == "fallback text"
|
|
|
|
|
|
def test_sanitize_msg_for_save_drops_tool_results_and_base64_data():
|
|
"""Saved history should keep conversation context without tool output pollution."""
|
|
msg = Msg.model_validate(
|
|
{
|
|
"name": "assistant",
|
|
"role": "assistant",
|
|
"content": [
|
|
{"type": "text", "text": "real conversation"},
|
|
{
|
|
"type": "tool_call",
|
|
"id": "call-1",
|
|
"name": "memory_search",
|
|
"input": "{}",
|
|
},
|
|
{
|
|
"type": "tool_result",
|
|
"id": "call-1",
|
|
"name": "memory_search",
|
|
"output": "retrieved memory that should not become conversation history",
|
|
},
|
|
{
|
|
"type": "data",
|
|
"source": {
|
|
"type": "base64",
|
|
"media_type": "image/png",
|
|
"data": "abc",
|
|
},
|
|
},
|
|
],
|
|
},
|
|
)
|
|
|
|
sanitized = _sanitize_msg_for_save(msg)
|
|
|
|
assert [block.type for block in sanitized.content] == ["text", "tool_call"]
|
|
assert sanitized.content[0].text == "real conversation"
|
|
assert sanitized.content[1].name == "memory_search"
|
|
|
|
|
|
def test_auto_memory_accepts_message_timestamp_aliases():
|
|
"""AutoMemoryStep preserves historical message timestamps from common benchmark fields."""
|
|
top_level = AutoMemoryStep._to_msg(
|
|
{
|
|
"name": "user",
|
|
"role": "user",
|
|
"content": "first event",
|
|
"time_created": "2023-01-19T08:00:00",
|
|
},
|
|
)
|
|
nested = AutoMemoryStep._to_msg(
|
|
{
|
|
"name": "assistant",
|
|
"role": "assistant",
|
|
"content": "second event",
|
|
"metadata": {"timestamp": "2023-01-20T09:30:00"},
|
|
},
|
|
)
|
|
|
|
history = format_history([top_level, nested])
|
|
|
|
assert top_level.created_at == "2023-01-19T08:00:00"
|
|
assert nested.created_at == "2023-01-20T09:30:00"
|
|
assert "[user @ 2023-01-19T08:00:00]" in history
|
|
assert "[assistant @ 2023-01-20T09:30:00]" in history
|
|
|
|
|
|
def test_auto_memory_created_at_takes_precedence_over_aliases():
|
|
"""Explicit AgentScope ``created_at`` values are not overwritten by compatibility aliases."""
|
|
msg = AutoMemoryStep._to_msg(
|
|
{
|
|
"name": "user",
|
|
"role": "user",
|
|
"content": "event",
|
|
"created_at": "2023-02-01T00:00:00",
|
|
"time_created": "2023-01-19T08:00:00",
|
|
"metadata": {"timestamp": "2023-01-20T09:30:00"},
|
|
},
|
|
)
|
|
|
|
assert msg.created_at == "2023-02-01T00:00:00"
|
|
|
|
|
|
def test_auto_memory_derives_latest_day_from_message_timestamps():
|
|
"""Historical imports should anchor the daily note date to the latest message time."""
|
|
messages = [
|
|
AutoMemoryStep._to_msg(
|
|
{
|
|
"name": "assistant",
|
|
"role": "assistant",
|
|
"content": "second event",
|
|
"created_at": "2023-01-20T09:30:00",
|
|
},
|
|
),
|
|
AutoMemoryStep._to_msg(
|
|
{
|
|
"name": "user",
|
|
"role": "user",
|
|
"content": "first event",
|
|
"metadata": {"timestamp": "2023-01-19T08:00:00"},
|
|
},
|
|
),
|
|
]
|
|
|
|
assert AutoMemoryStep._messages_day(messages) == "2023-01-20"
|