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:
chengyongru authored and GitHub committed 2026-09-05 20:21:54 +08:00
1 parent 455533169d
commit 5229989f28
52 files changed
+1849 -140

No files matched your search

+11
View File
@@ -8,6 +8,7 @@ from loguru import logger
from nanobot.bus.events import OutboundMessage
from nanobot.bus.outbound_events import (
ContextCompactionEvent,
GoalStateSyncEvent,
GoalStatusEvent,
ProgressEvent,
@@ -28,6 +29,7 @@ from nanobot.webui.metadata import (
from nanobot.webui.outbound_wire import (
WebUIWirePayload,
WebUIWirePersistence,
encode_context_compaction,
encode_recovery_state,
encode_turn_end,
)
@@ -152,6 +154,7 @@ class WebUIOutboundProjector:
SessionUpdatedEvent,
GoalStatusEvent,
GoalStateSyncEvent,
ContextCompactionEvent,
)
log = (
logger.debug
@@ -187,6 +190,14 @@ class WebUIOutboundProjector:
persistence="transient",
)
return
if isinstance(event, ContextCompactionEvent):
await self._transport.send_payload(
msg.chat_id,
encode_context_compaction(msg.chat_id, event),
persistence="transient" if event.phase == "started" else "turn_activity",
metadata=msg.metadata,
)
return
if isinstance(event, GoalStateSyncEvent):
if conns:
await self._transport.send_goal_state(
+33 -3
View File
@@ -5,7 +5,11 @@ from __future__ import annotations
from collections.abc import Mapping
from typing import Any, Literal, NotRequired, TypeAlias, TypedDict
from nanobot.bus.outbound_events import RecoveryStateEvent, TurnEndEvent
from nanobot.bus.outbound_events import (
ContextCompactionEvent,
RecoveryStateEvent,
TurnEndEvent,
)
from nanobot.webui.metadata import WEBUI_TURN_METADATA_KEY
@@ -32,8 +36,20 @@ class TurnEndWirePayload(_ChatWirePayload):
context_window_tokens: NotRequired[int]
WebUIWirePayload: TypeAlias = RecoveryStateWirePayload | TurnEndWirePayload
WebUIWirePersistence: TypeAlias = Literal["transient", "turn_complete"]
class ContextCompactionWirePayload(_ChatWirePayload):
event: Literal["context_compaction"]
compaction_id: str
phase: str
WebUIWirePayload: TypeAlias = (
ContextCompactionWirePayload | RecoveryStateWirePayload | TurnEndWirePayload
)
WebUIWirePersistence: TypeAlias = Literal[
"transient",
"turn_activity",
"turn_complete",
]
def encode_recovery_state(
@@ -55,6 +71,20 @@ def encode_recovery_state(
return payload
def encode_context_compaction(
chat_id: str,
event: ContextCompactionEvent,
) -> ContextCompactionWirePayload:
"""Project one summary-free compaction transition onto its stable wire shape."""
payload: ContextCompactionWirePayload = {
"event": "context_compaction",
"chat_id": chat_id,
"compaction_id": event.compaction_id,
"phase": event.phase,
}
return payload
def encode_turn_end(
chat_id: str,
event: TurnEndEvent,
+42
View File
@@ -2321,6 +2321,48 @@ def replay_transcript_to_ui_messages(
close_reasoning(messages)
continue
if ev == "context_compaction":
compaction_id = rec.get("compaction_id")
phase = rec.get("phase")
if (
not isinstance(compaction_id, str)
or not compaction_id
or phase not in {"started", "succeeded", "failed", "cancelled"}
):
continue
compaction: dict[str, Any] = {
"id": compaction_id,
"phase": phase,
}
payload: dict[str, Any] = {
"id": f"compaction-{compaction_id}",
"role": "assistant",
"content": "",
"kind": "compaction",
"createdAt": _created_at_ms(rec, idx),
"compaction": compaction,
**_turn_fields(rec, "activity"),
}
existing = next(
(
message_index
for message_index, message in enumerate(messages)
if message.get("id") == payload["id"]
),
None,
)
if existing is None:
messages.append(payload)
else:
payload["createdAt"] = messages[existing].get(
"createdAt",
payload["createdAt"],
)
messages[existing] = payload
active_activity_segment_id = None
active_file_edit_segment_id = None
continue
if ev == "message":
if suppress_until_turn_end and rec.get("kind") in (
"tool_hint",