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

+8 -1
View File
@@ -1388,7 +1388,14 @@ class WebSocketChannel(BaseChannel):
"""Persist as requested, frame, and fan out one encoded WebUI payload."""
conns = list(self._subs.get(chat_id, ()))
body: dict[str, Any] = dict(payload)
if persistence == "turn_complete":
if persistence == "turn_activity":
self._persist_turn_transcript_event(
chat_id,
body,
metadata=metadata,
phase="activity",
)
elif persistence == "turn_complete":
canonical_webui_turn = (metadata or {}).get("webui") is True
prior_persistence_failure = (
canonical_webui_turn
@@ -24,6 +24,7 @@ from nanobot.bus.events import (
OutboundMessage,
)
from nanobot.bus.outbound_events import (
ContextCompactionEvent,
GoalStateSyncEvent,
GoalStatusEvent,
ProgressEvent,
@@ -2805,6 +2806,68 @@ async def test_send_turn_end_emits_turn_end_event() -> None:
]
@pytest.mark.asyncio
async def test_context_compaction_started_is_live_only() -> None:
bus = MagicMock()
channel = WebSocketChannel(
{"enabled": True, "allowFrom": ["*"]},
bus,
gateway=_basic_handler(bus),
)
mock_ws = AsyncMock()
channel._attach(mock_ws, "chat-compaction-started")
await channel.send(OutboundMessage(
channel="websocket",
chat_id="chat-compaction-started",
content="Compressing context…",
event=ContextCompactionEvent(
compaction_id="compact-started",
phase="started",
),
))
assert _sent_ws_payloads(mock_ws) == [{
"event": "context_compaction",
"chat_id": "chat-compaction-started",
"compaction_id": "compact-started",
"phase": "started",
}]
assert read_transcript_lines("websocket:chat-compaction-started") == []
@pytest.mark.asyncio
async def test_context_compaction_is_structured_persisted_and_summary_free() -> None:
bus = MagicMock()
channel = WebSocketChannel(
{"enabled": True, "allowFrom": ["*"]},
bus,
gateway=_basic_handler(bus),
)
mock_ws = AsyncMock()
channel._attach(mock_ws, "chat-compaction")
await channel.send(OutboundMessage(
channel="websocket",
chat_id="chat-compaction",
content="Context compacted · LLM summary",
event=ContextCompactionEvent(
compaction_id="compact-1",
phase="succeeded",
),
))
expected = {
"event": "context_compaction",
"chat_id": "chat-compaction",
"compaction_id": "compact-1",
"phase": "succeeded",
}
assert _sent_ws_payloads(mock_ws) == [expected]
[persisted] = read_transcript_lines("websocket:chat-compaction")
assert {key: persisted[key] for key in expected} == expected
@pytest.mark.asyncio
async def test_recovery_state_is_a_structured_event_not_assistant_text() -> None:
bus = MagicMock()