mirror of
https://github.com/HKUDS/nanobot.git
synced 2026-09-04 02:01:48 +03:00
fix(memory): avoid duplicate summary injection
This commit is contained in:
@@ -724,6 +724,10 @@ class TestAutoCompactIntegration:
|
||||
async def test_full_lifecycle(self, tmp_path):
|
||||
loop = _make_loop(tmp_path, session_ttl_minutes=15)
|
||||
session = loop.sessions.get_or_create("cli:test")
|
||||
overview = (
|
||||
"IDLE_OVERVIEW_MARKER: User is learning English past tense. "
|
||||
"Example: 'I walked to the store yesterday.'"
|
||||
)
|
||||
|
||||
# Phase 1: User has a conversation longer than the retained recent suffix
|
||||
session.add_message("user", "I'm learning English, teach me past tense")
|
||||
@@ -745,7 +749,7 @@ class TestAutoCompactIntegration:
|
||||
# Phase 3: User returns with a new message
|
||||
loop.provider.chat_with_retry = AsyncMock(
|
||||
return_value=LLMResponse(
|
||||
content="User is learning English past tense. Example: 'I walked to the store yesterday.'",
|
||||
content=overview,
|
||||
tool_calls=[],
|
||||
)
|
||||
)
|
||||
@@ -759,6 +763,9 @@ class TestAutoCompactIntegration:
|
||||
|
||||
# Phase 4: Verify
|
||||
session_after = loop.sessions.get_or_create("cli:test")
|
||||
resumed_system_prompt = loop.provider.chat_with_retry.await_args_list[-1].kwargs[
|
||||
"messages"
|
||||
][0]["content"]
|
||||
|
||||
assert any(
|
||||
"past tense is used" in str(m.get("content", "")).lower()
|
||||
@@ -773,6 +780,7 @@ class TestAutoCompactIntegration:
|
||||
assert not any(
|
||||
"[Resumed Session]" in str(m.get("content", "")) for m in session_after.messages
|
||||
)
|
||||
assert resumed_system_prompt.count(overview) == 1
|
||||
# Runtime context end marker should NOT be persisted
|
||||
assert not any(
|
||||
"[/Runtime Context]" in str(m.get("content", "")) for m in session_after.messages
|
||||
|
||||
@@ -112,6 +112,35 @@ def test_recent_history_injection_is_session_scoped(tmp_path) -> None:
|
||||
assert "legacy entry without session" not in prompt
|
||||
|
||||
|
||||
def test_session_summary_replaces_matching_recent_history_entry(tmp_path) -> None:
|
||||
workspace = _make_workspace(tmp_path)
|
||||
builder = ContextBuilder(workspace)
|
||||
session_key = "telegram:chat-1"
|
||||
overview = "CURRENT_SESSION_OVERVIEW_MARKER"
|
||||
|
||||
builder.memory.append_history("another session event", session_key=session_key)
|
||||
summary_cursor = builder.memory.append_history(overview, session_key=session_key)
|
||||
summary = f"Previous conversation summary (last active 2026-08-19T10:00:00):\n{overview}"
|
||||
|
||||
prompt = builder.build_system_prompt(
|
||||
session_key=session_key,
|
||||
session_summary=summary,
|
||||
)
|
||||
|
||||
assert "# Recent History" in prompt
|
||||
assert "another session event" in prompt
|
||||
assert "[Archived Context Summary]" in prompt
|
||||
assert prompt.count(overview) == 1
|
||||
|
||||
builder.memory.set_last_dream_cursor(summary_cursor)
|
||||
processed_prompt = builder.build_system_prompt(
|
||||
session_key=session_key,
|
||||
session_summary=summary,
|
||||
)
|
||||
assert "# Recent History" not in processed_prompt
|
||||
assert processed_prompt.count(overview) == 1
|
||||
|
||||
|
||||
def test_recent_history_injection_unified_excludes_cron_internals(tmp_path) -> None:
|
||||
workspace = _make_workspace(tmp_path)
|
||||
builder = ContextBuilder(workspace)
|
||||
|
||||
Reference in New Issue
Block a user