fix: stop sending context compaction notifications

This commit is contained in:
Arthur K. authored and Xubin Ren committed 2026-09-30 13:44:42 +08:00
1 parent fbe9b4fd72
commit 0b402d0414
31 files changed
+157 -38

No files matched your search

+2
View File
@@ -696,11 +696,13 @@ class TestCompactIdleSession:
"cli:events",
runtime=runtime,
events=EventSink(observe),
notify=True,
)
assert result == "Summary."
assert [event.phase for event in events] == ["started", "succeeded"]
assert events[0].compaction_id == events[1].compaction_id
assert all(event.notify for event in events)
@pytest.mark.asyncio
async def test_event_callback_failure_does_not_abort_compaction(
+16 -11
View File
@@ -59,11 +59,14 @@ async def test_internal_compaction_never_reaches_its_result_destination(channel,
{"message_id": "reply", "thread_id": "root", "chat_type": "group"}),
("dingtalk:group:conversation:user", "dingtalk", "group:conversation", {}),
])
async def test_idle_compaction_uses_the_session_delivery_route(
async def test_session_route_delivers_automatic_and_manual_compaction(
key, channel, chat_id, metadata, unified,
) -> None:
factory = TurnDeliveryFactory(MessageBus())
event = ContextCompactionEvent(compaction_id="compact-1", phase="started")
automatic = ContextCompactionEvent(compaction_id="auto-1", phase="started")
notified = ContextCompactionEvent(
compaction_id="notified-1", phase="started", notify=True,
)
key = "unified:default" if unified else key
msg = InboundMessage(
channel=channel, sender_id="user", chat_id=chat_id, content="hello",
@@ -78,11 +81,14 @@ async def test_idle_compaction_uses_the_session_delivery_route(
sink = factory.session_events(key, session_metadata)
assert sink.publish is not None
await sink.emit(event)
await sink.emit(automatic)
await sink.emit(notified)
outbound = factory.bus.outbound.get_nowait()
assert (outbound.channel, outbound.chat_id, outbound.metadata) == (channel, chat_id, metadata)
assert outbound.event is event
outbounds = [factory.bus.outbound.get_nowait() for _ in range(2)]
assert [(outbound.channel, outbound.chat_id, outbound.metadata) for outbound in outbounds] == [
(channel, chat_id, metadata),
] * 2
assert [outbound.event for outbound in outbounds] == [automatic, notified]
async def test_idle_compaction_keeps_its_route_when_a_unified_session_moves() -> None:
@@ -96,13 +102,13 @@ async def test_idle_compaction_keeps_its_route_when_a_unified_session_moves() ->
factory.create(original, key).remember_session_route(session_metadata)
sink = factory.session_events(key, session_metadata)
assert sink.publish is not None
await sink.emit(ContextCompactionEvent("compact-1", "started"))
await sink.emit(ContextCompactionEvent("compact-1", "started", notify=True))
latest = InboundMessage(
channel="telegram", sender_id="user", chat_id="42", content="next question",
)
factory.create(latest, key).remember_session_route(session_metadata)
await sink.emit(ContextCompactionEvent("compact-1", "succeeded"))
await sink.emit(ContextCompactionEvent("compact-1", "succeeded", notify=True))
events = [factory.bus.outbound.get_nowait() for _ in range(2)]
assert [(msg.channel, msg.chat_id, msg.metadata) for msg in events] == [
@@ -110,14 +116,13 @@ async def test_idle_compaction_keeps_its_route_when_a_unified_session_moves() ->
] * 2
async def test_idle_compaction_can_deliver_to_a_legacy_websocket_session() -> None:
async def test_automatic_compaction_reaches_legacy_websocket_session() -> None:
factory = TurnDeliveryFactory(MessageBus())
event = ContextCompactionEvent(compaction_id="compact-1", phase="succeeded")
sink = factory.session_events("websocket:chat", {})
assert sink.publish is not None
await sink.emit(event)
outbound = factory.bus.outbound.get_nowait()
assert (outbound.channel, outbound.chat_id, outbound.event) == ("websocket", "chat", event)
assert factory.bus.outbound.get_nowait().event is event
@pytest.mark.asyncio
+22 -3
View File
@@ -80,7 +80,9 @@ async def test_output_consumer_capability_survives_callback_adaptation():
await events.emit(ProgressEvent(content="working"))
await events.emit(ContextCompactionEvent("compact", "succeeded"))
assert received == ["working"]
assert bus.outbound.get_nowait().event == ContextCompactionEvent("compact", "succeeded")
assert bus.outbound.get_nowait().event == ContextCompactionEvent(
"compact", "succeeded",
)
async def test_scope_snapshots_route_and_queues_events_in_order():
@@ -123,7 +125,7 @@ async def test_new_internal_event_needs_explicit_audience(monkeypatch):
assert project_notification("chat", event) is None
async def test_background_scope_keeps_retry_quiet_but_delivers_compaction():
async def test_background_scope_keeps_retry_quiet_and_delivers_compaction():
bus = MessageBus()
factory = TurnDeliveryFactory(bus,
lambda *_: TurnRoute("websocket", "chat"))
@@ -137,6 +139,21 @@ async def test_background_scope_keeps_retry_quiet_but_delivers_compaction():
assert bus.outbound.get_nowait().event is event
@pytest.mark.parametrize("channel", ["websocket", "cli", "slack", "custom"])
async def test_automatic_and_manual_compaction_are_delivered_to_channels(channel):
bus = MessageBus()
delivery = TurnDeliveryFactory(bus).create(InboundMessage(
channel=channel, sender_id="u", chat_id="chat", content="",
), f"{channel}:chat")
await delivery.events.emit(ContextCompactionEvent("auto", "succeeded"))
assert bus.outbound.get_nowait().event == ContextCompactionEvent("auto", "succeeded")
event = ContextCompactionEvent("notified", "succeeded", notify=True)
await delivery.events.emit(event)
assert bus.outbound.get_nowait().event is event
@pytest.mark.parametrize("channel", ["websocket", "cli", "slack", "custom"])
async def test_bus_routes_arbitrary_events_and_text_through_one_queue(channel):
@dataclass(frozen=True)
@@ -168,7 +185,9 @@ async def test_bus_event_preserves_existing_text_fallback():
@pytest.mark.parametrize("phase", ["started", "succeeded", "failed", "cancelled"])
def test_compaction_durability_is_independent_of_subscribers(phase):
projection = project_notification("chat", ContextCompactionEvent("c1", phase))
projection = project_notification(
"chat", ContextCompactionEvent("c1", phase),
)
assert projection is not None
assert projection.deliver_offline
assert projection.attach_turn_metadata
@@ -1,11 +1,4 @@
"""Context-compaction lifecycle notices are delivered regardless of ``send_progress``.
Compaction changes the context of every later turn, so both the start and
the outcome of a compaction are information for the user, not progress
chatter: a channel with ``send_progress`` off still receives them. Reducing
the noise (one message updated in place) is the adapter's job; see the
Discord channel (#5719).
"""
"""Channel transports decide whether to render compaction lifecycle events."""
from __future__ import annotations
@@ -40,6 +33,8 @@ class _MockChannel(BaseChannel):
pass
async def send(self, msg):
if isinstance(msg.event, ContextCompactionEvent) and not msg.event.notify:
return
return await self._send_mock(msg)
@@ -71,12 +66,18 @@ def _sent_contents(manager: ChannelManager) -> list[str]:
@pytest.mark.asyncio
async def test_compaction_lifecycle_is_delivered_with_progress_off(manager: ChannelManager) -> None:
async def test_channel_receives_automatic_compaction_but_does_not_render_it(
manager: ChannelManager,
) -> None:
manager.channels["mock"].send_progress = False
for event in (
ProgressEvent(content="ordinary progress"),
ContextCompactionEvent(compaction_id="c1", phase="started"),
ContextCompactionEvent(compaction_id="c1", phase="succeeded"),
ContextCompactionEvent(compaction_id="auto", phase="started"),
ContextCompactionEvent(compaction_id="auto", phase="succeeded"),
ContextCompactionEvent(compaction_id="auto-failed", phase="failed"),
ContextCompactionEvent(compaction_id="auto-cancelled", phase="cancelled"),
ContextCompactionEvent(compaction_id="c1", phase="started", notify=True),
ContextCompactionEvent(compaction_id="c1", phase="succeeded", notify=True),
):
await manager.bus.publish_outbound(
outbound_message_for_event(channel="mock", chat_id="chat", event=event)
@@ -86,4 +87,4 @@ async def test_compaction_lifecycle_is_delivered_with_progress_off(manager: Chan
contents = _sent_contents(manager)
assert "ordinary progress" not in contents
assert len(contents) == 2
assert contents == ["Compressing context…", "Context compacted."]
+1 -1
View File
@@ -50,7 +50,7 @@ async def test_compaction_notices_sent_when_enabled(monkeypatch) -> None:
await channel.send(outbound_message_for_event(
channel="qq", chat_id="chat",
event=ContextCompactionEvent(compaction_id="c1", phase="started"),
event=ContextCompactionEvent(compaction_id="c1", phase="started", notify=True),
))
send_text.assert_awaited_once()
+2
View File
@@ -71,6 +71,8 @@ async def test_compact_emits_one_lifecycle_and_keeps_the_session(loop, command)
assert started.phase == "started"
assert completed.phase == "succeeded"
assert started.compaction_id == completed.compaction_id
assert started.notify is True
assert completed.notify is True
loop.sessions.invalidate("cli:test")
reloaded = loop.sessions.get_or_create("cli:test")