fix(channels): keep the compaction outcome visible, gate only the start notice

Rework after review: compaction changes the context of every later turn,
so its outcome (compacted / failed / cancelled) is always delivered,
whether the user or the scheduler started it. Only the transient
"Compressing context…" start notice follows the channel's send_progress
setting, so a channel with progress off receives one notice per
compaction instead of two. The manual flag is gone.
This commit is contained in:
L4XB authored and chengyongru committed 2026-09-11 11:15:57 +08:00
1 parent 92f151ae67
commit a068de8068
6 files changed
+25 -31

No files matched your search

+2 -5
View File
@@ -1165,7 +1165,6 @@ class Consolidator:
runtime: LLMRuntime, runtime: LLMRuntime,
max_suffix: int = 0, max_suffix: int = 0,
events: EventSink = NO_EVENTS, events: EventSink = NO_EVENTS,
manual: bool = False,
) -> str | None: ) -> str | None:
"""Replace archived history with a summary checkpoint. """Replace archived history with a summary checkpoint.
@@ -1188,7 +1187,7 @@ class Consolidator:
compaction_id = uuid4().hex compaction_id = uuid4().hex
await events.emit( await events.emit(
ContextCompactionEvent(compaction_id=compaction_id, phase="started", manual=manual), ContextCompactionEvent(compaction_id=compaction_id, phase="started"),
) )
last_active = session.updated_at last_active = session.updated_at
archive_end = archive_start + len(messages_to_archive) archive_end = archive_start + len(messages_to_archive)
@@ -1208,21 +1207,19 @@ class Consolidator:
await events.emit( await events.emit(
ContextCompactionEvent( ContextCompactionEvent(
compaction_id=compaction_id, compaction_id=compaction_id,
manual=manual,
phase="cancelled" if isinstance(exc, asyncio.CancelledError) else "failed", phase="cancelled" if isinstance(exc, asyncio.CancelledError) else "failed",
), ),
) )
raise raise
if not summary: if not summary:
await events.emit( await events.emit(
ContextCompactionEvent(compaction_id=compaction_id, phase="failed", manual=manual), ContextCompactionEvent(compaction_id=compaction_id, phase="failed"),
) )
return None return None
await events.emit( await events.emit(
ContextCompactionEvent( ContextCompactionEvent(
compaction_id=compaction_id, compaction_id=compaction_id,
manual=manual,
phase="succeeded", phase="succeeded",
), ),
) )
+6 -6
View File
@@ -811,14 +811,14 @@ class ChannelManager:
): ):
continue continue
# Automatic compaction notices are maintenance chatter, so they # Compaction changes the context of every later turn, so its
# follow the channel's progress setting like other progress # outcome (compacted / failed / cancelled) is always delivered.
# text. A user-requested ``/compact`` and a failed compaction # The "Compressing context…" start notice is transient progress
# are still reported. # text and follows the channel's progress setting, so a channel
# with progress off gets one notice per compaction, not two.
if ( if (
isinstance(event, ContextCompactionEvent) isinstance(event, ContextCompactionEvent)
and not event.manual and event.phase == "started"
and event.phase != "failed"
and not self._should_send_progress(msg.channel) and not self._should_send_progress(msg.channel)
): ):
continue continue
-1
View File
@@ -357,7 +357,6 @@ async def cmd_compact(ctx: CommandContext) -> None:
ctx.key, ctx.key,
runtime=runtime, runtime=runtime,
events=delivery.events, events=delivery.events,
manual=True,
) )
except Exception: except Exception:
logger.exception("Manual context compaction failed for {}", ctx.key) logger.exception("Manual context compaction failed for {}", ctx.key)
-3
View File
@@ -17,9 +17,6 @@ class AgentEvent:
class ContextCompactionEvent(AgentEvent): class ContextCompactionEvent(AgentEvent):
compaction_id: str compaction_id: str
phase: Literal["started", "succeeded", "failed", "cancelled"] phase: Literal["started", "succeeded", "failed", "cancelled"]
# True when the user asked for the compaction (``/compact``). Automatic
# compaction is routine maintenance and its notices follow ``send_progress``.
manual: bool = False
@dataclass(frozen=True) @dataclass(frozen=True)
@@ -1,9 +1,11 @@
"""Automatic context-compaction notices follow the channel's ``send_progress``. """Context-compaction notices on channels with ``send_progress`` off.
Idle auto-compaction emits ``ContextCompactionEvent`` start/finish notices. A compaction emits ``ContextCompactionEvent`` start/finish notices. The
They are maintenance chatter, so a channel that has progress disabled must outcome (compacted / failed / cancelled) is always delivered because it
not receive them; a user-requested ``/compact`` and a failed compaction are changes the context of every later turn; the "Compressing context…" start
still delivered (#5719). notice is transient progress text and follows the channel's progress
setting, so such a channel receives one notice per compaction instead of
two (#5719).
""" """
from __future__ import annotations from __future__ import annotations
@@ -64,16 +66,16 @@ async def _dispatch_until(manager: ChannelManager, expected: int) -> None:
pass pass
def _compaction(phase: str, *, manual: bool = False): def _compaction(phase: str):
return outbound_message_for_event( return outbound_message_for_event(
channel="mock", channel="mock",
chat_id="c1", chat_id="c1",
event=ContextCompactionEvent(compaction_id="cmp-1", phase=phase, manual=manual), event=ContextCompactionEvent(compaction_id="cmp-1", phase=phase),
) )
@pytest.mark.asyncio @pytest.mark.asyncio
async def test_auto_compaction_notices_are_dropped_when_progress_is_off(manager): async def test_only_the_outcome_is_delivered_when_progress_is_off(manager):
channel = manager.channels["mock"] channel = manager.channels["mock"]
channel.send_progress = False channel.send_progress = False
await manager.bus.publish_outbound(_compaction("started")) await manager.bus.publish_outbound(_compaction("started"))
@@ -81,11 +83,12 @@ async def test_auto_compaction_notices_are_dropped_when_progress_is_off(manager)
await _dispatch_until(manager, expected=1) await _dispatch_until(manager, expected=1)
channel._send_mock.assert_not_awaited() contents = [call.args[0].content for call in channel._send_mock.await_args_list]
assert contents == ["Context compacted."]
@pytest.mark.asyncio @pytest.mark.asyncio
async def test_auto_compaction_notices_are_delivered_when_progress_is_on(manager): async def test_start_and_outcome_are_delivered_when_progress_is_on(manager):
channel = manager.channels["mock"] channel = manager.channels["mock"]
channel.send_progress = True channel.send_progress = True
await manager.bus.publish_outbound(_compaction("started")) await manager.bus.publish_outbound(_compaction("started"))
@@ -98,13 +101,14 @@ async def test_auto_compaction_notices_are_delivered_when_progress_is_on(manager
@pytest.mark.asyncio @pytest.mark.asyncio
async def test_manual_and_failed_compaction_notices_bypass_the_progress_setting(manager): async def test_failed_and_cancelled_outcomes_are_delivered_when_progress_is_off(manager):
channel = manager.channels["mock"] channel = manager.channels["mock"]
channel.send_progress = False channel.send_progress = False
await manager.bus.publish_outbound(_compaction("started", manual=True)) await manager.bus.publish_outbound(_compaction("started"))
await manager.bus.publish_outbound(_compaction("failed")) await manager.bus.publish_outbound(_compaction("failed"))
await manager.bus.publish_outbound(_compaction("cancelled"))
await _dispatch_until(manager, expected=2) await _dispatch_until(manager, expected=2)
contents = [call.args[0].content for call in channel._send_mock.await_args_list] contents = [call.args[0].content for call in channel._send_mock.await_args_list]
assert contents == ["Compressing context…", "Unable to compact context."] assert contents == ["Unable to compact context.", "Context compaction cancelled."]
-3
View File
@@ -71,9 +71,6 @@ async def test_compact_emits_one_lifecycle_and_keeps_the_session(loop, command)
assert started.phase == "started" assert started.phase == "started"
assert completed.phase == "succeeded" assert completed.phase == "succeeded"
assert started.compaction_id == completed.compaction_id assert started.compaction_id == completed.compaction_id
# A user-requested compaction is reported even on channels with progress off.
assert started.manual is True
assert completed.manual is True
loop.sessions.invalidate("cli:test") loop.sessions.invalidate("cli:test")
reloaded = loop.sessions.get_or_create("cli:test") reloaded = loop.sessions.get_or_create("cli:test")