feat(memory): add progressive wiki and drain Dream history (#4477)

This commit is contained in:
chengyongru authored and GitHub committed 2026-11-18 16:28:43 +08:00
1 parent 9ccf3b6214
commit 74d070f699
40 files changed
+1513 -738

No files matched your search

+76
View File
@@ -1,6 +1,9 @@
from __future__ import annotations
import asyncio
import subprocess
import sys
from pathlib import Path
from types import SimpleNamespace
from unittest.mock import AsyncMock, MagicMock, patch
@@ -109,6 +112,42 @@ async def _wait_for_background_callbacks(loop) -> None:
class TestBackgroundTaskTracking:
def test_shutdown_drains_completed_task_before_its_callback(self, tmp_path):
# A busy loop cannot be bounded by an asyncio timeout in the same loop.
script = """
import asyncio
import sys
from pathlib import Path
from unittest.mock import AsyncMock, MagicMock
from nanobot.agent.loop import AgentLoop
from nanobot.bus.queue import MessageBus
async def main():
provider = MagicMock(aclose=AsyncMock())
provider.get_default_model.return_value = "test-model"
agent = AgentLoop(bus=MessageBus(), provider=provider, workspace=Path(sys.argv[1]))
async def finish():
return
agent.schedule_background(finish())
task = next(iter(agent._background_tasks))
await asyncio.sleep(0)
assert task.done() and task in agent._background_tasks
await agent.aclose()
assert not agent._background_tasks
asyncio.run(main())
"""
result = subprocess.run(
[sys.executable, "-c", script, str(tmp_path)],
cwd=Path(__file__).resolve().parents[2],
capture_output=True, text=True, timeout=15,
creationflags=subprocess.CREATE_NO_WINDOW if sys.platform == "win32" else 0,
)
assert result.returncode == 0, result.stdout + result.stderr
@pytest.mark.asyncio
async def test_successful_background_task_is_removed(self, monkeypatch):
loop, _bus = _make_loop()
@@ -174,6 +213,43 @@ class TestBackgroundTaskTracking:
mock_logger.opt.assert_not_called()
@pytest.mark.asyncio
async def test_shutdown_drains_work_scheduled_by_background_task(self):
loop, _bus = _make_loop()
loop.subagents.close = AsyncMock()
loop._exec_session_manager.close_all = AsyncMock()
scan_started = asyncio.Event()
release_scan = asyncio.Event()
archival_started = asyncio.Event()
release_archival = asyncio.Event()
async def archive():
archival_started.set()
await release_archival.wait()
async def scan():
scan_started.set()
await release_scan.wait()
loop.schedule_background(archive())
loop.schedule_background(scan())
await scan_started.wait()
closing = asyncio.create_task(loop.aclose())
try:
await asyncio.sleep(0)
release_scan.set()
await archival_started.wait()
assert not closing.done()
loop.subagents.close.assert_not_awaited()
finally:
release_scan.set()
release_archival.set()
await closing
assert not loop._background_tasks
loop.subagents.close.assert_awaited_once()
loop._exec_session_manager.close_all.assert_awaited_once()
@pytest.mark.asyncio
async def test_shutdown_drains_background_failure_and_logs_it_once(self, monkeypatch):
loop, _bus = _make_loop()