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,
max_suffix: int = 0,
events: EventSink = NO_EVENTS,
manual: bool = False,
) -> str | None:
"""Replace archived history with a summary checkpoint.
@@ -1188,7 +1187,7 @@ class Consolidator:
compaction_id = uuid4().hex
await events.emit(
ContextCompactionEvent(compaction_id=compaction_id, phase="started", manual=manual),
ContextCompactionEvent(compaction_id=compaction_id, phase="started"),
)
last_active = session.updated_at
archive_end = archive_start + len(messages_to_archive)
@@ -1208,21 +1207,19 @@ class Consolidator:
await events.emit(
ContextCompactionEvent(
compaction_id=compaction_id,
manual=manual,
phase="cancelled" if isinstance(exc, asyncio.CancelledError) else "failed",
),
)
raise
if not summary:
await events.emit(
ContextCompactionEvent(compaction_id=compaction_id, phase="failed", manual=manual),
ContextCompactionEvent(compaction_id=compaction_id, phase="failed"),
)
return None
await events.emit(
ContextCompactionEvent(
compaction_id=compaction_id,
manual=manual,
phase="succeeded",
),
)
+6 -6
View File
@@ -811,14 +811,14 @@ class ChannelManager:
):
continue
# Automatic compaction notices are maintenance chatter, so they
# follow the channel's progress setting like other progress
# text. A user-requested ``/compact`` and a failed compaction
# are still reported.
# Compaction changes the context of every later turn, so its
# outcome (compacted / failed / cancelled) is always delivered.
# The "Compressing context…" start notice is transient progress
# text and follows the channel's progress setting, so a channel
# with progress off gets one notice per compaction, not two.
if (
isinstance(event, ContextCompactionEvent)
and not event.manual
and event.phase != "failed"
and event.phase == "started"
and not self._should_send_progress(msg.channel)
):
continue
-1
View File
@@ -357,7 +357,6 @@ async def cmd_compact(ctx: CommandContext) -> None:
ctx.key,
runtime=runtime,
events=delivery.events,
manual=True,
)
except Exception:
logger.exception("Manual context compaction failed for {}", ctx.key)
-3
View File
@@ -17,9 +17,6 @@ class AgentEvent:
class ContextCompactionEvent(AgentEvent):
compaction_id: str
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)
@@ -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.
They are maintenance chatter, so a channel that has progress disabled must
not receive them; a user-requested ``/compact`` and a failed compaction are
still delivered (#5719).
A compaction emits ``ContextCompactionEvent`` start/finish notices. The
outcome (compacted / failed / cancelled) is always delivered because it
changes the context of every later turn; the "Compressing context…" start
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
@@ -64,16 +66,16 @@ async def _dispatch_until(manager: ChannelManager, expected: int) -> None:
pass
def _compaction(phase: str, *, manual: bool = False):
def _compaction(phase: str):
return outbound_message_for_event(
channel="mock",
chat_id="c1",
event=ContextCompactionEvent(compaction_id="cmp-1", phase=phase, manual=manual),
event=ContextCompactionEvent(compaction_id="cmp-1", phase=phase),
)
@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.send_progress = False
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)
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
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.send_progress = True
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
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.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("cancelled"))
await _dispatch_until(manager, expected=2)
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 completed.phase == "succeeded"
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")
reloaded = loop.sessions.get_or_create("cli:test")