feat: make context compaction visible in channels (#5656)
* feat: surface context compaction lifecycle * fix: complete and simplify context compaction notifications
This commit is contained in:
1 parent
455533169d
commit
5229989f28
52 files changed
+1849
-140
No files matched your search
+56
-16
@@ -17,9 +17,15 @@ from contextlib import suppress
|
||||
from datetime import datetime
|
||||
from pathlib import Path
|
||||
from typing import TYPE_CHECKING, Any, Callable, Iterator, cast
|
||||
from uuid import uuid4
|
||||
|
||||
from loguru import logger
|
||||
|
||||
from nanobot.bus.outbound_events import (
|
||||
ContextCompactionCallback,
|
||||
ContextCompactionEvent,
|
||||
emit_context_compaction,
|
||||
)
|
||||
from nanobot.llm_usage.context import llm_usage_source
|
||||
from nanobot.providers.base import ProviderCallContext, ProviderConversationState
|
||||
from nanobot.runtime_context import public_history_messages
|
||||
@@ -930,9 +936,8 @@ class MemoryArchiver:
|
||||
if not summary:
|
||||
logger.warning("Memory archive provider summary was not safe to replay, raw-dumping")
|
||||
return raw_fallback()
|
||||
if summary == "(nothing)":
|
||||
return "(nothing)"
|
||||
self.store.append_history(summary, session_key=session_key)
|
||||
if summary != "(nothing)":
|
||||
self.store.append_history(summary, session_key=session_key)
|
||||
return summary
|
||||
|
||||
async def archive_session(
|
||||
@@ -944,7 +949,10 @@ class MemoryArchiver:
|
||||
input_token_budget: int,
|
||||
) -> str | None:
|
||||
"""Archive a captured session prefix without mutating the session."""
|
||||
messages = list(session.messages[session.last_archived:archive_end])
|
||||
messages = [
|
||||
message for message in session.messages[session.last_archived:archive_end]
|
||||
if not message.get("_command")
|
||||
]
|
||||
if not messages:
|
||||
return None
|
||||
session_summary = session_summary_from_metadata(
|
||||
@@ -1184,6 +1192,7 @@ class Consolidator:
|
||||
*,
|
||||
runtime: LLMRuntime,
|
||||
max_suffix: int = MIN_COMPACTED_REPLAY_MESSAGES,
|
||||
on_compaction: ContextCompactionCallback | None = None,
|
||||
) -> str | None:
|
||||
"""Archive the full idle tail while keeping recent messages replayable.
|
||||
|
||||
@@ -1205,25 +1214,56 @@ class Consolidator:
|
||||
|
||||
archive_start = session.last_archived
|
||||
messages_to_archive = list(session.messages[archive_start:])
|
||||
if not messages_to_archive:
|
||||
if not any(not message.get("_command") for message in messages_to_archive):
|
||||
return ""
|
||||
|
||||
compaction_id = uuid4().hex
|
||||
await emit_context_compaction(
|
||||
on_compaction,
|
||||
ContextCompactionEvent(compaction_id=compaction_id, phase="started"),
|
||||
)
|
||||
last_active = session.updated_at
|
||||
archive_end = archive_start + len(messages_to_archive)
|
||||
summary = await self.archive_session(
|
||||
session,
|
||||
archive_end=archive_end,
|
||||
runtime=runtime,
|
||||
)
|
||||
try:
|
||||
summary = await self.archive_session(
|
||||
session,
|
||||
archive_end=archive_end,
|
||||
runtime=runtime,
|
||||
)
|
||||
if summary is not None:
|
||||
self._set_last_summary(
|
||||
session,
|
||||
summary,
|
||||
last_active=last_active,
|
||||
)
|
||||
|
||||
# A turn can append while the provider call is in flight. Advance only
|
||||
# through the captured batch so new messages remain eligible next time.
|
||||
session.last_archived = archive_end
|
||||
self.sessions.save(session)
|
||||
except (Exception, asyncio.CancelledError) as exc:
|
||||
await emit_context_compaction(
|
||||
on_compaction,
|
||||
ContextCompactionEvent(
|
||||
compaction_id=compaction_id,
|
||||
phase="cancelled" if isinstance(exc, asyncio.CancelledError) else "failed",
|
||||
),
|
||||
)
|
||||
raise
|
||||
if summary is None:
|
||||
await emit_context_compaction(
|
||||
on_compaction,
|
||||
ContextCompactionEvent(compaction_id=compaction_id, phase="failed"),
|
||||
)
|
||||
return None
|
||||
|
||||
self._set_last_summary(session, summary, last_active=last_active)
|
||||
|
||||
# A turn can append while the provider call is in flight. Advance only
|
||||
# through the captured batch so new messages remain eligible next time.
|
||||
session.last_archived = archive_end
|
||||
self.sessions.save(session)
|
||||
await emit_context_compaction(
|
||||
on_compaction,
|
||||
ContextCompactionEvent(
|
||||
compaction_id=compaction_id,
|
||||
phase="succeeded",
|
||||
),
|
||||
)
|
||||
|
||||
visible = session.get_history(
|
||||
max_messages=MIN_COMPACTED_REPLAY_MESSAGES,
|
||||
|
||||
Reference in new issue
Block a user