From d4e23dec12c60a4540cb7bb8ec3edb45e42ab6c2 Mon Sep 17 00:00:00 2001 From: dajiaohuang Date: Thu, 13 Aug 2026 21:32:07 +0800 Subject: [PATCH] fix(memory): preserve full consolidation input --- nanobot/agent/memory.py | 25 +++++++++++++++++++++---- tests/agent/test_consolidator.py | 8 ++++---- 2 files changed, 25 insertions(+), 8 deletions(-) diff --git a/nanobot/agent/memory.py b/nanobot/agent/memory.py index 0e738e311..a381d13f7 100644 --- a/nanobot/agent/memory.py +++ b/nanobot/agent/memory.py @@ -669,11 +669,28 @@ class MemoryStore: max_chars: int | None = None, session_key: str | None = None, ) -> str: - """Persist and return a bounded raw checkpoint when summarization degrades.""" - checkpoint = self._build_raw_checkpoint(messages, max_chars=max_chars) - self.append_history(checkpoint, session_key=session_key) + """Persist raw messages in bounded chunks and return a checkpoint.""" + limit = max_chars if max_chars is not None else _RAW_ARCHIVE_MAX_CHARS + chunk_size = min(max(1, limit), _HISTORY_ENTRY_HARD_CAP - 1_000) + formatted = self._format_messages(public_history_messages(messages)) + chunks = [ + formatted[start:start + chunk_size] + for start in range(0, len(formatted), chunk_size) + ] or [""] + for part, chunk in enumerate(chunks, start=1): + suffix = f" (part {part}/{len(chunks)})" if len(chunks) > 1 else "" + self.append_history( + f"[RAW] {len(messages)} messages{suffix}\n{chunk}", + session_key=session_key, + ) logger.warning( - "Memory consolidation degraded: raw-archived {} messages", len(messages) + "Memory consolidation degraded: raw-archived {} messages in {} entries", + len(messages), + len(chunks), + ) + checkpoint = self._normalize_history_entry( + f"[RAW] {len(messages)} messages\n{formatted}", + max_chars=limit, ) return checkpoint diff --git a/tests/agent/test_consolidator.py b/tests/agent/test_consolidator.py index 587d0becd..05a217b85 100644 --- a/tests/agent/test_consolidator.py +++ b/tests/agent/test_consolidator.py @@ -1333,7 +1333,6 @@ class TestCompactIdleSession: assert mock_provider.chat_stream_with_retry.await_count == 2 entries = store.read_unprocessed_history(since_cursor=0) assert len(entries) == 1 - assert entries[0]["content"].startswith("[RAW] ") assert "important answer" in entries[0]["content"] assert sessions.get_or_create("cli:repeated-tool").last_archived == 2 @@ -1391,8 +1390,9 @@ class TestCompactIdleSession: assert "[RAW]" in result mock_provider.chat_stream_with_retry.assert_not_awaited() entries = store.read_unprocessed_history(since_cursor=0) - assert len(entries) == 1 - assert entries[0]["content"].startswith("[RAW] ") + assert len(entries) > 1 + assert all(entry["content"].startswith("[RAW] ") for entry in entries) + assert "x" * 100_000 in "".join(entry["content"].split("\n", 1)[1] for entry in entries) assert sessions.get_or_create("sdk:oversized").last_archived == 1 @pytest.mark.asyncio @@ -1523,7 +1523,7 @@ class TestRawArchiveTruncation: messages = [{"role": "user", "content": big}] store.raw_archive(messages) entries = store.read_unprocessed_history(since_cursor=0) - assert len(entries) == 1 + assert len(entries) > 1 assert len(entries[0]["content"]) < 50_000 assert "[RAW]" in entries[0]["content"]