fix(agent): skip subagent sessions during idle compaction (#6339)

Refs: NAN-304
This commit is contained in:
chengyongru authored and GitHub committed 2026-10-20 22:26:49 +08:00
1 parent 14d9e6ed0d
commit 688976bcb9
2 files changed
+58 -1

No files matched your search

+6 -1
View File
@@ -71,7 +71,12 @@ class AutoCompact:
active_keys = set(active_session_keys)
for info in await self.sessions.state.list_sessions():
key = info.get("key", "")
if not key or is_dream_session(key) or key in self._archiving:
if (
not key
or is_dream_session(key)
or key.startswith("subagent:")
or key in self._archiving
):
continue
if key in active_keys or not self._is_expired(info.get("updated_at"), now):
continue
+52
View File
@@ -10,6 +10,7 @@ from unittest.mock import AsyncMock, MagicMock
import pytest
from nanobot.agent.loop import AgentLoop
from nanobot.agent.subagent_status import SubagentStatus
from nanobot.agent.tools.context import current_request_context
from nanobot.agent.tools.registry import ToolRegistry
from nanobot.bus.events import InboundMessage
@@ -149,6 +150,57 @@ async def test_heartbeat_idle_compaction_persists_summary_without_channel_notice
assert loop.bus.outbound.empty()
@pytest.mark.parametrize("reopen", [False, True])
async def test_idle_compaction_skips_subagent_sessions(tmp_path, monkeypatch, reopen):
loop = _make_loop(tmp_path)
try:
parent = await loop.sessions.state.get("cli:parent")
_add_turns(parent, 1)
await loop.sessions.state.finish_turn(parent)
assert loop.subagents.sessions is not None
child_keys = []
for completed in (False, True):
status = SubagentStatus(
task_id=f"idle-{completed}",
label="Idle task",
task_description="Inspect the workspace",
started_at=time.monotonic(),
owner=parent.key,
state="done" if completed else "running",
phase="done" if completed else "running",
result="Task observation" if completed else None,
finished_at=time.monotonic() if completed else None,
completed_at=time.time() if completed else None,
)
child = await loop.subagents.sessions.create(status)
child.add_message("assistant", "Task observation")
await loop.sessions.state.finish_turn(child)
child_keys.append(child.key)
if reopen:
await loop.aclose()
loop = _make_loop(tmp_path)
clock = MagicMock(wraps=datetime)
clock.now.return_value = datetime.now() + timedelta(minutes=20)
monkeypatch.setattr("nanobot.agent.autocompact.datetime", clock)
loop.consolidator.archive_session = AsyncMock(return_value="Conversation summary.")
await loop._check_expired_sessions_if_due()
await _drain_background_tasks(loop)
loop.consolidator.archive_session.assert_awaited_once()
assert loop.consolidator.archive_session.await_args.args[0].key == parent.key
compacted_parent = await loop.sessions.state.get(parent.key)
assert compacted_parent.metadata["_last_summary"]["text"] == "Conversation summary."
for key in child_keys:
child = await loop.sessions.state.get(key)
assert len(child.messages) == 2
assert child.last_archived == 0
assert "_last_summary" not in child.metadata
finally:
await loop.aclose()
async def test_expired_unified_turn_keeps_latest_delivery_route(tmp_path, monkeypatch):
monkeypatch.setattr("nanobot.config.home._home_path", tmp_path / "home")
monkeypatch.setattr("nanobot.config.loader._current_config_path", tmp_path / "config.json")