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
@@ -0,0 +1,54 @@
|
||||
"""WebUI replay behavior for structured context-compaction events."""
|
||||
|
||||
import pytest
|
||||
|
||||
from nanobot.webui.transcript import replay_transcript_to_ui_messages
|
||||
|
||||
|
||||
@pytest.mark.parametrize("phase", ["succeeded", "cancelled"])
|
||||
def test_replay_folds_compaction_lifecycle_into_one_stable_message(phase) -> None:
|
||||
messages = replay_transcript_to_ui_messages([
|
||||
{
|
||||
"event": "context_compaction",
|
||||
"compaction_id": "compact-1",
|
||||
"phase": "started",
|
||||
"created_at_ms": 1_700_000_000_000,
|
||||
},
|
||||
{
|
||||
"event": "context_compaction",
|
||||
"compaction_id": "compact-1",
|
||||
"phase": phase,
|
||||
"created_at_ms": 1_700_000_001_000,
|
||||
},
|
||||
])
|
||||
|
||||
assert messages == [{
|
||||
"id": "compaction-compact-1",
|
||||
"role": "assistant",
|
||||
"content": "",
|
||||
"kind": "compaction",
|
||||
"createdAt": 1_700_000_000_000,
|
||||
"turnPhase": "activity",
|
||||
"compaction": {
|
||||
"id": "compact-1",
|
||||
"phase": phase,
|
||||
},
|
||||
}]
|
||||
|
||||
|
||||
def test_replay_keeps_distinct_compactions_in_one_turn() -> None:
|
||||
messages = replay_transcript_to_ui_messages([
|
||||
{
|
||||
"event": "context_compaction",
|
||||
"compaction_id": compaction_id,
|
||||
"phase": phase,
|
||||
}
|
||||
for compaction_id in ("compact-1", "compact-2")
|
||||
for phase in ("started", "succeeded")
|
||||
])
|
||||
|
||||
assert [message["id"] for message in messages] == [
|
||||
"compaction-compact-1",
|
||||
"compaction-compact-2",
|
||||
]
|
||||
assert all(message["compaction"]["phase"] == "succeeded" for message in messages)
|
||||
@@ -1,9 +1,34 @@
|
||||
from __future__ import annotations
|
||||
|
||||
from nanobot.bus.outbound_events import RecoveryStateEvent, TurnEndEvent
|
||||
from nanobot.bus.outbound_events import (
|
||||
ContextCompactionEvent,
|
||||
RecoveryStateEvent,
|
||||
TurnEndEvent,
|
||||
)
|
||||
from nanobot.providers.base import LLMUsage
|
||||
from nanobot.webui.metadata import WEBUI_TURN_METADATA_KEY
|
||||
from nanobot.webui.outbound_wire import encode_recovery_state, encode_turn_end
|
||||
from nanobot.webui.outbound_wire import (
|
||||
encode_context_compaction,
|
||||
encode_recovery_state,
|
||||
encode_turn_end,
|
||||
)
|
||||
|
||||
|
||||
def test_encode_context_compaction_projects_the_lifecycle() -> None:
|
||||
payload = encode_context_compaction(
|
||||
"chat-1",
|
||||
ContextCompactionEvent(
|
||||
compaction_id="compact-1",
|
||||
phase="succeeded",
|
||||
),
|
||||
)
|
||||
|
||||
assert payload == {
|
||||
"event": "context_compaction",
|
||||
"chat_id": "chat-1",
|
||||
"compaction_id": "compact-1",
|
||||
"phase": "succeeded",
|
||||
}
|
||||
|
||||
|
||||
def test_encode_recovery_state_omits_absent_optional_fields() -> None:
|
||||
|
||||
Reference in new issue
Block a user