fix: isolate maintenance-session compaction notifications (#5874)
* fix: keep heartbeat compaction notices off user channels * refactor: centralize maintenance session identity
This commit is contained in:
1 parent
c8ed6a1209
commit
d149232621
7 files changed
+96
-17
No files matched your search
@@ -15,6 +15,7 @@ from nanobot.command import CommandContext
|
||||
from nanobot.config.schema import AgentDefaults, Config
|
||||
from nanobot.events import NO_EVENTS
|
||||
from nanobot.providers.base import LLMResponse
|
||||
from nanobot.session.keys import HEARTBEAT_SESSION_KEY
|
||||
|
||||
|
||||
def _make_loop(
|
||||
@@ -132,6 +133,25 @@ async def _drain_background_tasks(loop: AgentLoop) -> None:
|
||||
await asyncio.sleep(0)
|
||||
|
||||
|
||||
async def test_heartbeat_idle_compaction_persists_summary_without_channel_notices(tmp_path):
|
||||
loop = _make_loop(tmp_path)
|
||||
session = loop.sessions.get_or_create(HEARTBEAT_SESSION_KEY)
|
||||
_add_turns(session, 2)
|
||||
session.metadata["_compaction_route"] = {"channel": "telegram", "chat_id": "chat"}
|
||||
session.updated_at = datetime.now() - timedelta(minutes=20)
|
||||
loop.sessions.save(session)
|
||||
loop.consolidator.archive_session = AsyncMock(return_value="Heartbeat summary.")
|
||||
|
||||
loop.auto_compact.check_expired(loop.schedule_background, loop.runtime_for_session)
|
||||
await _drain_background_tasks(loop)
|
||||
|
||||
loop.consolidator.archive_session.assert_awaited_once()
|
||||
loop.sessions.invalidate(HEARTBEAT_SESSION_KEY)
|
||||
refreshed = loop.sessions.get_or_create(HEARTBEAT_SESSION_KEY)
|
||||
assert refreshed.metadata["_last_summary"]["text"] == "Heartbeat summary."
|
||||
assert loop.bus.outbound.empty()
|
||||
|
||||
|
||||
class TestSessionTTLConfig:
|
||||
"""Test session TTL configuration."""
|
||||
|
||||
|
||||
@@ -9,6 +9,7 @@ from nanobot.bus.queue import MessageBus
|
||||
from nanobot.bus.runtime_events import TurnCompleted
|
||||
from nanobot.events import RetryStatusEvent
|
||||
from nanobot.providers.base import LLMProvider, ProviderCallContext
|
||||
from nanobot.session.keys import HEARTBEAT_SESSION_KEY
|
||||
from nanobot.session.manager import SessionManager
|
||||
from nanobot.session.webui_turns import WebuiTurnRoutePolicy
|
||||
from nanobot.webui.metadata import (
|
||||
@@ -17,6 +18,34 @@ from nanobot.webui.metadata import (
|
||||
)
|
||||
|
||||
|
||||
@pytest.mark.parametrize("channel", ["telegram", "discord", "websocket", "cli", "custom"])
|
||||
@pytest.mark.parametrize("legacy_route", [False, True])
|
||||
@pytest.mark.parametrize("key", [HEARTBEAT_SESSION_KEY, "dream:20260602-155256"])
|
||||
async def test_internal_compaction_never_reaches_its_result_destination(channel, legacy_route, key):
|
||||
factory = TurnDeliveryFactory(MessageBus())
|
||||
msg = InboundMessage(channel=channel, sender_id="user", chat_id="chat", content="check")
|
||||
delivery = factory.create(msg, key)
|
||||
metadata = {"_compaction_route": {"channel": channel, "chat_id": "chat"}} if legacy_route else {}
|
||||
idle_events = factory.session_events(key, metadata)
|
||||
for sink in (delivery.events, idle_events):
|
||||
assert not sink.accepts(ContextCompactionEvent)
|
||||
for phase in ("started", "succeeded", "failed", "cancelled"):
|
||||
await sink.emit(ContextCompactionEvent("internal-compact", phase))
|
||||
assert factory.bus.outbound.empty()
|
||||
|
||||
delivery.remember_session_route(metadata)
|
||||
assert "_compaction_route" not in metadata
|
||||
|
||||
# The same destination still receives the user's own compaction lifecycle.
|
||||
user_delivery = factory.create(msg, f"{channel}:chat")
|
||||
user_delivery.remember_session_route(metadata)
|
||||
for sink in (user_delivery.events, factory.session_events(f"{channel}:chat", metadata)):
|
||||
await sink.emit(ContextCompactionEvent("user-compact", "succeeded"))
|
||||
assert factory.bus.outbound.get_nowait().event == ContextCompactionEvent(
|
||||
"user-compact", "succeeded",
|
||||
)
|
||||
|
||||
|
||||
@pytest.mark.parametrize("unified", [False, True])
|
||||
@pytest.mark.parametrize(("key", "channel", "chat_id", "metadata"), [
|
||||
("slack:C123:1700000000.000100", "slack", "C123",
|
||||
|
||||
Reference in new issue
Block a user