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)
+50
View File
@@ -1,6 +1,11 @@
from __future__ import annotations
import pytest
from nanobot.channels.contracts import channel_default_config, channel_instance_specs
from nanobot.channels.manager import ChannelManager
from nanobot.channels.registry import load_channel_plugin
from nanobot.config.loader import load_config, save_config
from nanobot.config.schema import Config
from nanobot.webui.settings_system import (
coerce_channel_value,
@@ -65,3 +70,48 @@ def test_channel_secret_is_cleared_only_by_explicit_null() -> None:
assert saved == ["channels.matrix.password"]
assert config.channels.matrix["password"] == ""
@pytest.mark.parametrize("existing_key", ["showCompactionNotices", "show_compaction_notices"])
def test_compaction_notice_webui_contract_and_config_round_trip(existing_key, tmp_path):
name = "qq"
plugin = load_channel_plugin(name)
spec = plugin.setup
field = next(item for item in spec.to_public_dict(name)["fields"]
if item["field"] == "showCompactionNotices")
assert field["kind"] == "bool"
assert field["inheritable"] is True
assert "default_value" not in field
assert channel_default_config(plugin).get("showCompactionNotices") is None
config = Config.model_validate({
"channels": {"showCompactionNotices": True, name: {existing_key: False}},
})
key = f"channels.{name}.showCompactionNotices"
manager = ChannelManager.__new__(ChannelManager)
# The generic settings route must preserve false, true and a return to inheritance.
for submitted, expected in [("false", False), ("true", True), ("", None), (None, None)]:
save_channel_config_values(
config, name, {key: submitted}, load_channel_plugin=load_channel_plugin,
)
config_path = tmp_path / "config.json"
save_config(config, config_path)
config = load_config(config_path)
assert config.channels.show_compaction_notices is True
section = getattr(config.channels, name)
[instance] = channel_instance_specs(plugin, section, enabled_only=False)
assert instance.config.get("showCompactionNotices") is expected
assert "show_compaction_notices" not in instance.config
if expected is None:
# Inheritance is persisted as absence, also readable by older QQ versions.
assert "showCompactionNotices" not in instance.config
assert manager._resolve_bool_override(
instance.config, "show_compaction_notices", True,
) is (True if expected is None else expected)
def test_saving_other_qq_settings_does_not_pin_the_global_notice_policy():
config = Config.model_validate({"channels": {"showCompactionNotices": True}})
save_channel_config_values(
config, "qq", {"appId": "a", "secret": "s"}, load_channel_plugin=load_channel_plugin,
)
assert config.channels.qq.get("showCompactionNotices") is None