feat(webui): distinguish context compaction reasons (NAN-324) (#6389)

* feat(webui): distinguish context compaction reasons (NAN-324)

* fix(webui): describe idle compaction cancellation as interruption

* docs: omit compaction notice details from configuration guide

* fix(i18n): make compaction notices read naturally

* fix(i18n): keep CJK compaction notices concise

* fix(i18n): describe idle compaction as unfinished
This commit is contained in:
chengyongru authored and GitHub committed 2026-10-22 16:13:25 +08:00
1 parent 084b6bb394
commit bc5c58d239
38 files changed
+540 -23

No files matched your search

+1
View File
@@ -101,6 +101,7 @@ class AutoCompact:
key,
runtime=runtime,
events=self._bind_events(key) if self._bind_events else NO_EVENTS,
reason="idle",
)
if summary:
session = await self.sessions.state.get(key)
+4 -2
View File
@@ -533,7 +533,7 @@ class ContextGovernor:
if response.provider_compaction_event_id is None:
response.provider_compaction_event_id = uuid4().hex
await shield_and_drain(state.events.emit(ContextCompactionEvent(
response.provider_compaction_event_id, "succeeded",
response.provider_compaction_event_id, "succeeded", reason="provider_native",
)))
if (
not response.provider_compaction_applied
@@ -585,7 +585,7 @@ class ContextGovernor:
measured, _source = pressure
compaction_id = uuid4().hex
await state.events.emit(
ContextCompactionEvent(compaction_id=compaction_id, phase="started"),
ContextCompactionEvent(compaction_id=compaction_id, phase="started", reason="context_pressure"),
)
try:
delta_messages = await self.prepare_in_background(
@@ -645,6 +645,7 @@ class ContextGovernor:
ContextCompactionEvent(
compaction_id=compaction_id,
phase="cancelled" if isinstance(exc, asyncio.CancelledError) else "failed",
reason="context_pressure",
),
)
raise
@@ -652,6 +653,7 @@ class ContextGovernor:
ContextCompactionEvent(
compaction_id=compaction_id,
phase="succeeded",
reason="context_pressure",
),
)
return prepared
+6 -3
View File
@@ -20,7 +20,7 @@ from uuid import uuid4
from loguru import logger
from nanobot.events import NO_EVENTS, ContextCompactionEvent, EventSink
from nanobot.events import NO_EVENTS, CompactionReason, ContextCompactionEvent, EventSink
from nanobot.llm_usage.context import llm_usage_source
from nanobot.providers.base import LLMResponse, ProviderConversationState
from nanobot.providers.conversation_state import ProviderConversationStateController
@@ -1222,6 +1222,7 @@ class Consolidator:
max_suffix: int = 0,
events: EventSink = NO_EVENTS,
notify: bool = False,
reason: CompactionReason | None = None,
) -> str | None:
"""Replace archived history with a summary checkpoint.
@@ -1244,7 +1245,7 @@ class Consolidator:
compaction_id = uuid4().hex
await events.emit(
ContextCompactionEvent(compaction_id=compaction_id, phase="started", notify=notify),
ContextCompactionEvent(compaction_id=compaction_id, phase="started", notify=notify, reason=reason),
)
archive_end = archive_start + len(messages_to_archive)
try:
@@ -1261,12 +1262,13 @@ class Consolidator:
compaction_id=compaction_id,
phase="cancelled" if isinstance(exc, asyncio.CancelledError) else "failed",
notify=notify,
reason=reason,
),
)
raise
if not summary:
await events.emit(
ContextCompactionEvent(compaction_id=compaction_id, phase="failed", notify=notify),
ContextCompactionEvent(compaction_id=compaction_id, phase="failed", notify=notify, reason=reason),
)
return None
@@ -1275,6 +1277,7 @@ class Consolidator:
compaction_id=compaction_id,
phase="succeeded",
notify=notify,
reason=reason,
),
)
@@ -3333,6 +3333,7 @@ async def test_context_compaction_is_structured_persisted_and_summary_free(histo
compaction_id="compact-1",
phase=phase,
notify=notify,
reason="manual",
),
))
@@ -3341,6 +3342,7 @@ async def test_context_compaction_is_structured_persisted_and_summary_free(histo
"chat_id": "chat-compaction",
"compaction_id": "compact-1",
"phase": phase,
"reason": "manual",
}
assert _sent_ws_payloads(mock_ws) == [expected]
[persisted] = history.read("websocket:chat-compaction")
+1
View File
@@ -361,6 +361,7 @@ async def cmd_compact(ctx: CommandContext) -> None:
runtime=runtime,
events=delivery.events,
notify=True,
reason="manual",
)
except Exception:
logger.exception("Manual context compaction failed for {}", ctx.key)
+4
View File
@@ -33,6 +33,9 @@ class ResponseSourceEvent(AgentEvent):
content: str | None = None
CompactionReason = Literal["idle", "context_pressure", "manual", "provider_native"]
@dataclass(frozen=True)
class ContextCompactionEvent(AgentEvent):
compaction_id: str
@@ -40,6 +43,7 @@ class ContextCompactionEvent(AgentEvent):
# Request a chat notice (e.g. /compact), not event delivery or persistence.
# Interactive clients still consume every phase when this is false.
notify: bool = False
reason: CompactionReason | None = None
@dataclass(frozen=True)
+3 -2
View File
@@ -369,7 +369,7 @@ class OpenAICodexProvider(LLMProvider):
nonlocal native_compaction_event_id
compaction_id = uuid4().hex
try:
await events.emit(ContextCompactionEvent(compaction_id, "started"))
await events.emit(ContextCompactionEvent(compaction_id, "started", reason="provider_native"))
compacted = await _send(
request,
emit_deltas=False,
@@ -380,10 +380,11 @@ class OpenAICodexProvider(LLMProvider):
await shield_and_drain(events.emit(ContextCompactionEvent(
compaction_id,
"cancelled" if isinstance(exc, asyncio.CancelledError) else "failed",
reason="provider_native",
)))
raise
native_compaction_event_id = compaction_id
await shield_and_drain(events.emit(ContextCompactionEvent(compaction_id, "succeeded")))
await shield_and_drain(events.emit(ContextCompactionEvent(compaction_id, "succeeded", reason="provider_native")))
return compacted
native_compaction_state = await self._responses.compact_before_request(
+1 -1
View File
@@ -39,7 +39,7 @@ _CONTENT_FIELDS = frozenset({
"session_message", "tool_events", "edits", "source", "response_sources", "resuming",
"merge_next", "latency_ms", "usage", "round_usages", "context_window_tokens", "goal_state",
"outcome", "failure_kind", "failure_error_kind", "failure_attempts", "failure_message",
"transcript_incomplete", "stream_id", "compaction_id", "phase",
"transcript_incomplete", "stream_id", "compaction_id", "phase", "reason",
})
+4 -1
View File
@@ -13,7 +13,7 @@ from nanobot.bus.outbound_events import (
RetryStatusEvent,
TurnEndEvent,
)
from nanobot.events import AgentEvent
from nanobot.events import AgentEvent, CompactionReason
from nanobot.webui.metadata import WEBUI_TURN_METADATA_KEY
_JSONValue: TypeAlias = (
@@ -102,6 +102,7 @@ class ContextCompactionWirePayload(_ChatWirePayload):
event: Literal["context_compaction"]
compaction_id: str
phase: Literal["started", "succeeded", "failed", "cancelled"]
reason: NotRequired[CompactionReason]
class SubagentTaskWirePayload(_ChatWirePayload):
@@ -152,6 +153,8 @@ def encode_context_compaction(
"compaction_id": event.compaction_id,
"phase": event.phase,
}
if event.reason is not None:
payload["reason"] = event.reason
return payload
+5 -1
View File
@@ -1487,12 +1487,16 @@ def _client_projection_event(
or phase not in {"started", "succeeded", "failed", "cancelled"}
):
return None
return {
projected = {
"event": "context_compaction",
**common,
"compaction_id": compaction_id,
"phase": phase,
}
reason = record.get("reason")
if isinstance(reason, str) and reason in {"idle", "context_pressure", "manual", "provider_native"}:
projected["reason"] = reason
return projected
if event == "turn_end":
projected = {"event": "turn_end", **common}