fix(memory): preserve full consolidation input

This commit is contained in:
dajiaohuang authored and Xubin Ren committed 2026-09-17 21:13:23 +08:00
1 parent 835cac0ae7
commit d4e23dec12
2 files changed
+25 -8

No files matched your search

+21 -4
View File
@@ -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
+4 -4
View File
@@ -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"]