feat(channels): add inherited global compaction notice policy

This commit is contained in:
Xubin Ren committed 2026-09-30 13:44:42 +08:00
1 parent 7644d845b4
commit ab6d5f1d3c
35 files changed
+287 -37

No files matched your search

@@ -33,7 +33,9 @@ class _MockChannel(BaseChannel):
pass
async def send(self, msg):
if isinstance(msg.event, ContextCompactionEvent) and not msg.event.notify:
if isinstance(msg.event, ContextCompactionEvent) and not (
msg.event.notify or self.show_compaction_notices
):
return
return await self._send_mock(msg)
@@ -88,3 +90,37 @@ async def test_channel_receives_automatic_compaction_but_does_not_render_it(
contents = _sent_contents(manager)
assert "ordinary progress" not in contents
assert contents == ["Compressing context…", "Context compacted."]
@pytest.mark.parametrize("global_value", [False, True])
@pytest.mark.parametrize("override", [None, False, True])
@pytest.mark.parametrize("key", ["show_compaction_notices", "showCompactionNotices"])
async def test_global_notice_policy_and_channel_override(manager, global_value, override, key):
manager.config.channels.show_compaction_notices = global_value
section = {} if override is None else {key: override}
channel = manager._build_channel("mock", _MockChannel, section)
assert channel.show_compaction_notices is (global_value if override is None else override)
channel.send_progress = False # Ordinary progress and compaction are independent policies.
manager.channels["mock"] = channel
for phase in ("started", "succeeded", "failed", "cancelled"):
for notify in (False, True):
await channel.send(outbound_message_for_event(
channel="mock", chat_id="chat",
event=ContextCompactionEvent("compact", phase, notify=notify),
))
assert channel._send_mock.await_count == (8 if channel.show_compaction_notices else 4)
def test_global_notice_config_round_trip_and_rebuild(manager):
assert Config().channels.show_compaction_notices is False
manager.config = Config.model_validate({"channels": {"showCompactionNotices": True}})
manager.config = Config.model_validate_json(manager.config.model_dump_json(by_alias=True))
assert manager._build_channel("mock", _MockChannel, {}).show_compaction_notices is True
# Rebuilding an adapter after a config change must resolve the new default.
manager.config.channels.show_compaction_notices = False
assert manager._build_channel("mock", _MockChannel, {}).show_compaction_notices is False
assert manager._build_channel(
"mock", _MockChannel, {"showCompactionNotices": True},
).show_compaction_notices
@@ -17,7 +17,9 @@ pytest.importorskip("botpy")
from nanobot.bus.events import OutboundMessage
from nanobot.bus.outbound_events import ContextCompactionEvent, outbound_message_for_event
from nanobot.bus.queue import MessageBus
from nanobot.channels.manager import ChannelManager
from nanobot.channels.qq.runtime import QQChannel, QQConfig
from nanobot.config.schema import Config
def _make_channel(tmp_path, **config_kwargs) -> QQChannel:
@@ -90,3 +92,23 @@ async def test_ordinary_messages_unaffected(monkeypatch, tmp_path) -> None:
def test_config_accepts_camel_case_override() -> None:
config = QQConfig.model_validate({"appId": "a", "secret": "s", "showCompactionNotices": True})
assert config.show_compaction_notices is True
@pytest.mark.parametrize("global_value", [False, True])
@pytest.mark.parametrize("override", [None, False, True])
@pytest.mark.parametrize("typed", [False, True])
@pytest.mark.parametrize("legacy_string", [False, True])
def test_qq_inherits_global_unless_legacy_override_is_explicit(
tmp_path, global_value, override, typed, legacy_string,
):
manager = ChannelManager.__new__(ChannelManager)
manager.config = Config()
manager.config.channels.show_compaction_notices = global_value
manager.bus = MessageBus()
section = {"appId": "a", "secret": "s", "mediaDir": str(tmp_path)}
if override is not None:
section["showCompactionNotices"] = str(override).lower() if legacy_string else override
if typed:
section = QQConfig.model_validate(section)
channel = manager._build_channel("qq", QQChannel, section)
assert channel.show_compaction_notices is (global_value if override is None else override)