feat(memory): harden legacy history migration and Dream UX
This commit is contained in:
@@ -1,9 +1,10 @@
|
||||
"""Tests for the restructured MemoryStore — pure file I/O layer."""
|
||||
|
||||
from datetime import datetime
|
||||
import json
|
||||
from pathlib import Path
|
||||
|
||||
import pytest
|
||||
from pathlib import Path
|
||||
|
||||
from nanobot.agent.memory import MemoryStore
|
||||
|
||||
@@ -114,3 +115,135 @@ class TestLegacyHistoryMigration:
|
||||
entries = store.read_unprocessed_history(since_cursor=0)
|
||||
assert len(entries) == 1
|
||||
assert entries[0]["cursor"] == 1
|
||||
|
||||
def test_migrates_legacy_history_md_preserving_partial_entries(self, tmp_path):
|
||||
memory_dir = tmp_path / "memory"
|
||||
memory_dir.mkdir()
|
||||
legacy_file = memory_dir / "HISTORY.md"
|
||||
legacy_content = (
|
||||
"[2026-04-01 10:00] User prefers dark mode.\n\n"
|
||||
"[2026-04-01 10:05] [RAW] 2 messages\n"
|
||||
"[2026-04-01 10:04] USER: hello\n"
|
||||
"[2026-04-01 10:04] ASSISTANT: hi\n\n"
|
||||
"Legacy chunk without timestamp.\n"
|
||||
"Keep whatever content we can recover.\n"
|
||||
)
|
||||
legacy_file.write_text(legacy_content, encoding="utf-8")
|
||||
|
||||
store = MemoryStore(tmp_path)
|
||||
fallback_timestamp = datetime.fromtimestamp(
|
||||
(memory_dir / "HISTORY.md.bak").stat().st_mtime,
|
||||
).strftime("%Y-%m-%d %H:%M")
|
||||
|
||||
entries = store.read_unprocessed_history(since_cursor=0)
|
||||
assert [entry["cursor"] for entry in entries] == [1, 2, 3]
|
||||
assert entries[0]["timestamp"] == "2026-04-01 10:00"
|
||||
assert entries[0]["content"] == "User prefers dark mode."
|
||||
assert entries[1]["timestamp"] == "2026-04-01 10:05"
|
||||
assert entries[1]["content"].startswith("[RAW] 2 messages")
|
||||
assert "USER: hello" in entries[1]["content"]
|
||||
assert entries[2]["timestamp"] == fallback_timestamp
|
||||
assert entries[2]["content"].startswith("Legacy chunk without timestamp.")
|
||||
assert store.read_file(store._cursor_file).strip() == "3"
|
||||
assert store.read_file(store._dream_cursor_file).strip() == "3"
|
||||
assert not legacy_file.exists()
|
||||
assert (memory_dir / "HISTORY.md.bak").read_text(encoding="utf-8") == legacy_content
|
||||
|
||||
def test_migrates_consecutive_entries_without_blank_lines(self, tmp_path):
|
||||
memory_dir = tmp_path / "memory"
|
||||
memory_dir.mkdir()
|
||||
legacy_file = memory_dir / "HISTORY.md"
|
||||
legacy_content = (
|
||||
"[2026-04-01 10:00] First event.\n"
|
||||
"[2026-04-01 10:01] Second event.\n"
|
||||
"[2026-04-01 10:02] Third event.\n"
|
||||
)
|
||||
legacy_file.write_text(legacy_content, encoding="utf-8")
|
||||
|
||||
store = MemoryStore(tmp_path)
|
||||
|
||||
entries = store.read_unprocessed_history(since_cursor=0)
|
||||
assert len(entries) == 3
|
||||
assert [entry["content"] for entry in entries] == [
|
||||
"First event.",
|
||||
"Second event.",
|
||||
"Third event.",
|
||||
]
|
||||
|
||||
def test_raw_archive_stays_single_entry_while_following_events_split(self, tmp_path):
|
||||
memory_dir = tmp_path / "memory"
|
||||
memory_dir.mkdir()
|
||||
legacy_file = memory_dir / "HISTORY.md"
|
||||
legacy_content = (
|
||||
"[2026-04-01 10:05] [RAW] 2 messages\n"
|
||||
"[2026-04-01 10:04] USER: hello\n"
|
||||
"[2026-04-01 10:04] ASSISTANT: hi\n"
|
||||
"[2026-04-01 10:06] Normal event after raw block.\n"
|
||||
)
|
||||
legacy_file.write_text(legacy_content, encoding="utf-8")
|
||||
|
||||
store = MemoryStore(tmp_path)
|
||||
|
||||
entries = store.read_unprocessed_history(since_cursor=0)
|
||||
assert len(entries) == 2
|
||||
assert entries[0]["content"].startswith("[RAW] 2 messages")
|
||||
assert "USER: hello" in entries[0]["content"]
|
||||
assert entries[1]["content"] == "Normal event after raw block."
|
||||
|
||||
def test_nonstandard_date_headers_still_start_new_entries(self, tmp_path):
|
||||
memory_dir = tmp_path / "memory"
|
||||
memory_dir.mkdir()
|
||||
legacy_file = memory_dir / "HISTORY.md"
|
||||
legacy_content = (
|
||||
"[2026-03-25–2026-04-02] Multi-day summary.\n"
|
||||
"[2026-03-26/27] Cross-day summary.\n"
|
||||
)
|
||||
legacy_file.write_text(legacy_content, encoding="utf-8")
|
||||
|
||||
store = MemoryStore(tmp_path)
|
||||
fallback_timestamp = datetime.fromtimestamp(
|
||||
(memory_dir / "HISTORY.md.bak").stat().st_mtime,
|
||||
).strftime("%Y-%m-%d %H:%M")
|
||||
|
||||
entries = store.read_unprocessed_history(since_cursor=0)
|
||||
assert len(entries) == 2
|
||||
assert entries[0]["timestamp"] == fallback_timestamp
|
||||
assert entries[0]["content"] == "[2026-03-25–2026-04-02] Multi-day summary."
|
||||
assert entries[1]["timestamp"] == fallback_timestamp
|
||||
assert entries[1]["content"] == "[2026-03-26/27] Cross-day summary."
|
||||
|
||||
def test_existing_history_jsonl_skips_legacy_migration(self, tmp_path):
|
||||
memory_dir = tmp_path / "memory"
|
||||
memory_dir.mkdir()
|
||||
history_file = memory_dir / "history.jsonl"
|
||||
history_file.write_text(
|
||||
'{"cursor": 7, "timestamp": "2026-04-01 12:00", "content": "existing"}\n',
|
||||
encoding="utf-8",
|
||||
)
|
||||
legacy_file = memory_dir / "HISTORY.md"
|
||||
legacy_file.write_text("[2026-04-01 10:00] legacy\n\n", encoding="utf-8")
|
||||
|
||||
store = MemoryStore(tmp_path)
|
||||
|
||||
entries = store.read_unprocessed_history(since_cursor=0)
|
||||
assert len(entries) == 1
|
||||
assert entries[0]["cursor"] == 7
|
||||
assert entries[0]["content"] == "existing"
|
||||
assert legacy_file.exists()
|
||||
assert not (memory_dir / "HISTORY.md.bak").exists()
|
||||
|
||||
def test_migrates_legacy_history_with_invalid_utf8_bytes(self, tmp_path):
|
||||
memory_dir = tmp_path / "memory"
|
||||
memory_dir.mkdir()
|
||||
legacy_file = memory_dir / "HISTORY.md"
|
||||
legacy_file.write_bytes(
|
||||
b"[2026-04-01 10:00] Broken \xff data still needs migration.\n\n"
|
||||
)
|
||||
|
||||
store = MemoryStore(tmp_path)
|
||||
|
||||
entries = store.read_unprocessed_history(since_cursor=0)
|
||||
assert len(entries) == 1
|
||||
assert entries[0]["timestamp"] == "2026-04-01 10:00"
|
||||
assert "Broken" in entries[0]["content"]
|
||||
assert "migration." in entries[0]["content"]
|
||||
|
||||
@@ -185,6 +185,9 @@ async def test_start_creates_separate_pools_with_proxy(monkeypatch) -> None:
|
||||
assert builder.request_value is api_req
|
||||
assert builder.get_updates_request_value is poll_req
|
||||
assert any(cmd.command == "status" for cmd in app.bot.commands)
|
||||
assert any(cmd.command == "dream" for cmd in app.bot.commands)
|
||||
assert any(cmd.command == "dream-log" for cmd in app.bot.commands)
|
||||
assert any(cmd.command == "dream-restore" for cmd in app.bot.commands)
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
@@ -962,6 +965,27 @@ async def test_forward_command_does_not_inject_reply_context() -> None:
|
||||
assert handled[0]["content"] == "/new"
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_forward_command_preserves_dream_log_args_and_strips_bot_suffix() -> None:
|
||||
channel = TelegramChannel(
|
||||
TelegramConfig(enabled=True, token="123:abc", allow_from=["*"], group_policy="open"),
|
||||
MessageBus(),
|
||||
)
|
||||
channel._app = _FakeApp(lambda: None)
|
||||
handled = []
|
||||
|
||||
async def capture_handle(**kwargs) -> None:
|
||||
handled.append(kwargs)
|
||||
|
||||
channel._handle_message = capture_handle
|
||||
update = _make_telegram_update(text="/dream-log@nanobot_test deadbeef", reply_to_message=None)
|
||||
|
||||
await channel._forward_command(update, None)
|
||||
|
||||
assert len(handled) == 1
|
||||
assert handled[0]["content"] == "/dream-log deadbeef"
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_on_help_includes_restart_command() -> None:
|
||||
channel = TelegramChannel(
|
||||
@@ -977,3 +1001,6 @@ async def test_on_help_includes_restart_command() -> None:
|
||||
help_text = update.message.reply_text.await_args.args[0]
|
||||
assert "/restart" in help_text
|
||||
assert "/status" in help_text
|
||||
assert "/dream" in help_text
|
||||
assert "/dream-log" in help_text
|
||||
assert "/dream-restore" in help_text
|
||||
|
||||
@@ -0,0 +1,143 @@
|
||||
from __future__ import annotations
|
||||
|
||||
from types import SimpleNamespace
|
||||
|
||||
import pytest
|
||||
|
||||
from nanobot.bus.events import InboundMessage
|
||||
from nanobot.command.builtin import cmd_dream_log, cmd_dream_restore
|
||||
from nanobot.command.router import CommandContext
|
||||
from nanobot.utils.git_store import CommitInfo
|
||||
|
||||
|
||||
class _FakeStore:
|
||||
def __init__(self, git, last_dream_cursor: int = 1):
|
||||
self.git = git
|
||||
self._last_dream_cursor = last_dream_cursor
|
||||
|
||||
def get_last_dream_cursor(self) -> int:
|
||||
return self._last_dream_cursor
|
||||
|
||||
|
||||
class _FakeGit:
|
||||
def __init__(
|
||||
self,
|
||||
*,
|
||||
initialized: bool = True,
|
||||
commits: list[CommitInfo] | None = None,
|
||||
diff_map: dict[str, tuple[CommitInfo, str] | None] | None = None,
|
||||
revert_result: str | None = None,
|
||||
):
|
||||
self._initialized = initialized
|
||||
self._commits = commits or []
|
||||
self._diff_map = diff_map or {}
|
||||
self._revert_result = revert_result
|
||||
|
||||
def is_initialized(self) -> bool:
|
||||
return self._initialized
|
||||
|
||||
def log(self, max_entries: int = 20) -> list[CommitInfo]:
|
||||
return self._commits[:max_entries]
|
||||
|
||||
def show_commit_diff(self, sha: str, max_entries: int = 20):
|
||||
return self._diff_map.get(sha)
|
||||
|
||||
def revert(self, sha: str) -> str | None:
|
||||
return self._revert_result
|
||||
|
||||
|
||||
def _make_ctx(raw: str, git: _FakeGit, *, args: str = "", last_dream_cursor: int = 1) -> CommandContext:
|
||||
msg = InboundMessage(channel="cli", sender_id="u1", chat_id="direct", content=raw)
|
||||
store = _FakeStore(git, last_dream_cursor=last_dream_cursor)
|
||||
loop = SimpleNamespace(consolidator=SimpleNamespace(store=store))
|
||||
return CommandContext(msg=msg, session=None, key=msg.session_key, raw=raw, args=args, loop=loop)
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_dream_log_latest_is_more_user_friendly() -> None:
|
||||
commit = CommitInfo(sha="abcd1234", message="dream: 2026-04-04, 2 change(s)", timestamp="2026-04-04 12:00")
|
||||
diff = (
|
||||
"diff --git a/SOUL.md b/SOUL.md\n"
|
||||
"--- a/SOUL.md\n"
|
||||
"+++ b/SOUL.md\n"
|
||||
"@@ -1 +1 @@\n"
|
||||
"-old\n"
|
||||
"+new\n"
|
||||
)
|
||||
git = _FakeGit(commits=[commit], diff_map={commit.sha: (commit, diff)})
|
||||
|
||||
out = await cmd_dream_log(_make_ctx("/dream-log", git))
|
||||
|
||||
assert "## Dream Update" in out.content
|
||||
assert "Here is the latest Dream memory change." in out.content
|
||||
assert "- Commit: `abcd1234`" in out.content
|
||||
assert "- Changed files: `SOUL.md`" in out.content
|
||||
assert "Use `/dream-restore abcd1234` to undo this change." in out.content
|
||||
assert "```diff" in out.content
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_dream_log_missing_commit_guides_user() -> None:
|
||||
git = _FakeGit(diff_map={})
|
||||
|
||||
out = await cmd_dream_log(_make_ctx("/dream-log deadbeef", git, args="deadbeef"))
|
||||
|
||||
assert "Couldn't find Dream change `deadbeef`." in out.content
|
||||
assert "Use `/dream-restore` to list recent versions" in out.content
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_dream_log_before_first_run_is_clear() -> None:
|
||||
git = _FakeGit(initialized=False)
|
||||
|
||||
out = await cmd_dream_log(_make_ctx("/dream-log", git, last_dream_cursor=0))
|
||||
|
||||
assert "Dream has not run yet." in out.content
|
||||
assert "Run `/dream`" in out.content
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_dream_restore_lists_versions_with_next_steps() -> None:
|
||||
commits = [
|
||||
CommitInfo(sha="abcd1234", message="dream: latest", timestamp="2026-04-04 12:00"),
|
||||
CommitInfo(sha="bbbb2222", message="dream: older", timestamp="2026-04-04 08:00"),
|
||||
]
|
||||
git = _FakeGit(commits=commits)
|
||||
|
||||
out = await cmd_dream_restore(_make_ctx("/dream-restore", git))
|
||||
|
||||
assert "## Dream Restore" in out.content
|
||||
assert "Choose a Dream memory version to restore." in out.content
|
||||
assert "`abcd1234` 2026-04-04 12:00 - dream: latest" in out.content
|
||||
assert "Preview a version with `/dream-log <sha>`" in out.content
|
||||
assert "Restore a version with `/dream-restore <sha>`." in out.content
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_dream_restore_success_mentions_files_and_followup() -> None:
|
||||
commit = CommitInfo(sha="abcd1234", message="dream: latest", timestamp="2026-04-04 12:00")
|
||||
diff = (
|
||||
"diff --git a/SOUL.md b/SOUL.md\n"
|
||||
"--- a/SOUL.md\n"
|
||||
"+++ b/SOUL.md\n"
|
||||
"@@ -1 +1 @@\n"
|
||||
"-old\n"
|
||||
"+new\n"
|
||||
"diff --git a/memory/MEMORY.md b/memory/MEMORY.md\n"
|
||||
"--- a/memory/MEMORY.md\n"
|
||||
"+++ b/memory/MEMORY.md\n"
|
||||
"@@ -1 +1 @@\n"
|
||||
"-old\n"
|
||||
"+new\n"
|
||||
)
|
||||
git = _FakeGit(
|
||||
diff_map={commit.sha: (commit, diff)},
|
||||
revert_result="eeee9999",
|
||||
)
|
||||
|
||||
out = await cmd_dream_restore(_make_ctx("/dream-restore abcd1234", git, args="abcd1234"))
|
||||
|
||||
assert "Restored Dream memory to the state before `abcd1234`." in out.content
|
||||
assert "- New safety commit: `eeee9999`" in out.content
|
||||
assert "- Restored files: `SOUL.md`, `memory/MEMORY.md`" in out.content
|
||||
assert "Use `/dream-log eeee9999` to inspect the restore diff." in out.content
|
||||
Reference in New Issue
Block a user