Skip to content

Your lightweight open-source personal agent.

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

L4XBa068de80683a
diff --git a/nanobot/agent/memory.py b/nanobot/agent/memory.py
index 851462e54..531e8bd01 100644
--- a/nanobot/agent/memory.py
+++ b/nanobot/agent/memory.py
@@ -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",
),
)
diff --git a/nanobot/channels/manager.py b/nanobot/channels/manager.py
index 281b091f8..dcff9918e 100644
--- a/nanobot/channels/manager.py
+++ b/nanobot/channels/manager.py
@@ -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
diff --git a/nanobot/command/builtin.py b/nanobot/command/builtin.py
index 5d542d44c..27021b9b5 100644
--- a/nanobot/command/builtin.py
+++ b/nanobot/command/builtin.py
@@ -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)
diff --git a/nanobot/events.py b/nanobot/events.py
index 729544feb..e4308f768 100644
--- a/nanobot/events.py
+++ b/nanobot/events.py
@@ -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)
diff --git a/tests/channels/test_channel_manager_compaction_notices.py b/tests/channels/test_channel_manager_compaction_notices.py
index 50697585e..1aaa2f1c8 100644
--- a/tests/channels/test_channel_manager_compaction_notices.py
+++ b/tests/channels/test_channel_manager_compaction_notices.py
@@ -1,9 +1,11 @@
-"""Automatic context-compaction notices follow the channel's ``send_progress``.
-
-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).
+"""Context-compaction notices on channels with ``send_progress`` off.
+
+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."]
diff --git a/tests/command/test_compact_command.py b/tests/command/test_compact_command.py
index 1c38188c2..2c36a8ffe 100644
--- a/tests/command/test_compact_command.py
+++ b/tests/command/test_compact_command.py
@@ -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")