From 24c41a6af0736330cd9c67b3c9d1ec9adeefdf5f Mon Sep 17 00:00:00 2001 From: AlfredChaos <48818977+AlfredChaos@users.noreply.github.com> Date: Fri, 18 Sep 2026 00:45:58 +0800 Subject: [PATCH] fix(channels): drop compaction notices on channels without an in-place affordance (#5799) * fix(channels): drop compaction notices on channels without an in-place affordance QQ's C2C/group message API has no edit or recall endpoint, so the compaction lifecycle reached the chat as two permanent standalone messages. Add a show_compaction_notices capability that defaults to true (Telegram/Discord/WebSocket keep their in-place lifecycle) and opt QQ out, with a per-channel showCompactionNotices config override. Fixes #5784 * fix(qq): keep compaction-notice suppression inside the QQ channel Per review on #5799: drop ContextCompactionEvent in QQChannel.send() based on a QQ-local QQConfig.show_compaction_notices (default false, camel alias showCompactionNotices) instead of a cross-channel BaseChannel capability plus manager-level configuration and dispatch policy. BaseChannel, ChannelManager and the manager compaction test return to their upstream shape. Also adds the missing zh-TW locale copy (plus es/id/ko/vi so all ten QQ locales carry the new setup field) and QQ-level tests covering the default drop, the enabled path, ordinary-message passthrough, and the camelCase config override. --- docs/configuration.md | 12 ++++ nanobot/channels/qq/manifest.py | 1 + nanobot/channels/qq/runtime.py | 17 +++++ nanobot/channels/qq/webui/locales/en.json | 7 ++ nanobot/channels/qq/webui/locales/es.json | 7 ++ nanobot/channels/qq/webui/locales/fr.json | 7 ++ nanobot/channels/qq/webui/locales/id.json | 7 ++ nanobot/channels/qq/webui/locales/ja.json | 7 ++ nanobot/channels/qq/webui/locales/ko.json | 7 ++ nanobot/channels/qq/webui/locales/pt-BR.json | 7 ++ nanobot/channels/qq/webui/locales/vi.json | 7 ++ nanobot/channels/qq/webui/locales/zh-CN.json | 7 ++ nanobot/channels/qq/webui/locales/zh-TW.json | 7 ++ tests/channels/test_qq_compaction_notices.py | 73 ++++++++++++++++++++ 14 files changed, 173 insertions(+) create mode 100644 tests/channels/test_qq_compaction_notices.py diff --git a/docs/configuration.md b/docs/configuration.md index 096571518..c5f433fda 100644 --- a/docs/configuration.md +++ b/docs/configuration.md @@ -1654,6 +1654,18 @@ Normal tool workspace and media access rules still apply to attachment paths. } ``` +QQ `showCompactionNotices` defaults to `false`. It controls the context-compaction lifecycle notices ("Compressing context…" / "Context compacted."). Telegram, Discord and WebSocket present that lifecycle as one in-place-updated message or status, but QQ's C2C/group message API has no edit or recall endpoint, so each phase would land as a separate permanent message; the QQ channel therefore drops the notices by default (#5784). Set `channels.qq.showCompactionNotices: true` to post them anyway: + +```json +{ + "channels": { + "qq": { + "showCompactionNotices": true + } + } +} +``` + Telegram `richMessages` defaults to `false`. Enable it only to opt in to Bot API 10.1 `sendRichMessage` rendering; leave it disabled for Telegram Web clients that show unsupported-message errors for rich messages. ### Retry Behavior diff --git a/nanobot/channels/qq/manifest.py b/nanobot/channels/qq/manifest.py index a3e6cf093..28ada5680 100644 --- a/nanobot/channels/qq/manifest.py +++ b/nanobot/channels/qq/manifest.py @@ -11,6 +11,7 @@ SETUP_SPEC = ChannelSetupSpec( "allowFrom": field("list"), "msgFormat": field("enum", choices={"plain", "markdown"}, default="plain"), "ackMessage": field(default="⏳ Processing..."), + "showCompactionNotices": field("bool", default=False), "mediaDir": field(), "downloadChunkSize": field("int", default=1024 * 256), "downloadMaxBytes": field("int", default=1024 * 1024 * 200), diff --git a/nanobot/channels/qq/runtime.py b/nanobot/channels/qq/runtime.py index 01af68675..134580d61 100644 --- a/nanobot/channels/qq/runtime.py +++ b/nanobot/channels/qq/runtime.py @@ -37,6 +37,7 @@ from loguru import logger from pydantic import Field from nanobot.bus.events import OutboundMessage +from nanobot.bus.outbound_events import ContextCompactionEvent from nanobot.bus.queue import MessageBus from nanobot.channels.base import BaseChannel from nanobot.config.schema import Base @@ -192,6 +193,11 @@ class QQConfig(Base): download_chunk_size: int = 1024 * 256 # 256KB download_max_bytes: int = 1024 * 1024 * 200 # 200MB safety limit + # QQ's C2C/group message API has no edit or recall endpoint, so compaction + # notices would land as separate permanent messages (#5784). Off by default; + # set showCompactionNotices: true to post them anyway. + show_compaction_notices: bool = False + class QQChannel(BaseChannel): """QQ channel using botpy SDK with WebSocket connection.""" @@ -298,6 +304,17 @@ class QQChannel(BaseChannel): async def send(self, msg: OutboundMessage) -> None: """Send attachments first, then text.""" + # Compaction notices assume the channel can update one message in place + # (Telegram/Discord edit their notice; WebSocket projects it as status). + # QQ's C2C/group API has no edit or recall endpoint, so by default the + # lifecycle is dropped here instead of posting two permanent messages + # (#5784); showCompactionNotices: true restores them. + if ( + isinstance(msg.event, ContextCompactionEvent) + and not self.config.show_compaction_notices + ): + return + try: if not self._client: raise RuntimeError("QQ client not initialized") diff --git a/nanobot/channels/qq/webui/locales/en.json b/nanobot/channels/qq/webui/locales/en.json index cb426f104..ecd825144 100644 --- a/nanobot/channels/qq/webui/locales/en.json +++ b/nanobot/channels/qq/webui/locales/en.json @@ -33,6 +33,13 @@ }, "downloadMaxBytes": { "label": "Maximum download size" + }, + "showCompactionNotices": { + "label": "Compaction notices", + "choices": { + "true": "On", + "false": "Off" + } } } } diff --git a/nanobot/channels/qq/webui/locales/es.json b/nanobot/channels/qq/webui/locales/es.json index 0d2f1d528..723bc52e5 100644 --- a/nanobot/channels/qq/webui/locales/es.json +++ b/nanobot/channels/qq/webui/locales/es.json @@ -33,6 +33,13 @@ }, "downloadMaxBytes": { "label": "Tamaño máximo de descarga" + }, + "showCompactionNotices": { + "label": "Avisos de compactación de contexto", + "choices": { + "true": "Activado", + "false": "Desactivado" + } } } } diff --git a/nanobot/channels/qq/webui/locales/fr.json b/nanobot/channels/qq/webui/locales/fr.json index bf9d51b36..97cc8fa4e 100644 --- a/nanobot/channels/qq/webui/locales/fr.json +++ b/nanobot/channels/qq/webui/locales/fr.json @@ -33,6 +33,13 @@ }, "downloadMaxBytes": { "label": "Taille maximale de téléchargement" + }, + "showCompactionNotices": { + "label": "Avis de compactage du contexte", + "choices": { + "true": "Activé", + "false": "Désactivé" + } } } } diff --git a/nanobot/channels/qq/webui/locales/id.json b/nanobot/channels/qq/webui/locales/id.json index 0641dc835..dbf6b84a0 100644 --- a/nanobot/channels/qq/webui/locales/id.json +++ b/nanobot/channels/qq/webui/locales/id.json @@ -33,6 +33,13 @@ }, "downloadMaxBytes": { "label": "Ukuran unduhan maksimum" + }, + "showCompactionNotices": { + "label": "Notifikasi pemadatan konteks", + "choices": { + "true": "Aktif", + "false": "Nonaktif" + } } } } diff --git a/nanobot/channels/qq/webui/locales/ja.json b/nanobot/channels/qq/webui/locales/ja.json index 2efeba43b..66b998481 100644 --- a/nanobot/channels/qq/webui/locales/ja.json +++ b/nanobot/channels/qq/webui/locales/ja.json @@ -33,6 +33,13 @@ }, "downloadMaxBytes": { "label": "最大ダウンロードサイズ" + }, + "showCompactionNotices": { + "label": "コンテキスト圧縮通知", + "choices": { + "true": "オン", + "false": "オフ" + } } } } diff --git a/nanobot/channels/qq/webui/locales/ko.json b/nanobot/channels/qq/webui/locales/ko.json index 228f604d8..1bd6ddafd 100644 --- a/nanobot/channels/qq/webui/locales/ko.json +++ b/nanobot/channels/qq/webui/locales/ko.json @@ -33,6 +33,13 @@ }, "downloadMaxBytes": { "label": "최대 다운로드 크기" + }, + "showCompactionNotices": { + "label": "컨텍스트 압축 알림", + "choices": { + "true": "켜기", + "false": "끄기" + } } } } diff --git a/nanobot/channels/qq/webui/locales/pt-BR.json b/nanobot/channels/qq/webui/locales/pt-BR.json index 442a330c8..d833f10f8 100644 --- a/nanobot/channels/qq/webui/locales/pt-BR.json +++ b/nanobot/channels/qq/webui/locales/pt-BR.json @@ -33,6 +33,13 @@ }, "downloadMaxBytes": { "label": "Tamanho máximo de download" + }, + "showCompactionNotices": { + "label": "Avisos de compactação de contexto", + "choices": { + "true": "Ativado", + "false": "Desativado" + } } } } diff --git a/nanobot/channels/qq/webui/locales/vi.json b/nanobot/channels/qq/webui/locales/vi.json index 18e84e154..d25376076 100644 --- a/nanobot/channels/qq/webui/locales/vi.json +++ b/nanobot/channels/qq/webui/locales/vi.json @@ -33,6 +33,13 @@ }, "downloadMaxBytes": { "label": "Kích thước tải xuống tối đa" + }, + "showCompactionNotices": { + "label": "Thông báo nén ngữ cảnh", + "choices": { + "true": "Bật", + "false": "Tắt" + } } } } diff --git a/nanobot/channels/qq/webui/locales/zh-CN.json b/nanobot/channels/qq/webui/locales/zh-CN.json index a1edc5f37..b2eaf71ab 100644 --- a/nanobot/channels/qq/webui/locales/zh-CN.json +++ b/nanobot/channels/qq/webui/locales/zh-CN.json @@ -33,6 +33,13 @@ }, "downloadMaxBytes": { "label": "下载大小上限" + }, + "showCompactionNotices": { + "label": "上下文压缩通知", + "choices": { + "true": "开", + "false": "关" + } } } } diff --git a/nanobot/channels/qq/webui/locales/zh-TW.json b/nanobot/channels/qq/webui/locales/zh-TW.json index c3f8a698a..ad61aafbc 100644 --- a/nanobot/channels/qq/webui/locales/zh-TW.json +++ b/nanobot/channels/qq/webui/locales/zh-TW.json @@ -33,6 +33,13 @@ }, "downloadMaxBytes": { "label": "下載大小上限" + }, + "showCompactionNotices": { + "label": "上下文壓縮通知", + "choices": { + "true": "開", + "false": "關" + } } } } diff --git a/tests/channels/test_qq_compaction_notices.py b/tests/channels/test_qq_compaction_notices.py new file mode 100644 index 000000000..5262293c8 --- /dev/null +++ b/tests/channels/test_qq_compaction_notices.py @@ -0,0 +1,73 @@ +"""QQ drops compaction notices it cannot present as one message. + +QQ's C2C/group message API has no edit or recall endpoint, so the +compaction lifecycle would land as two separate permanent messages. The +channel drops ``ContextCompactionEvent`` by default; ``showCompactionNotices: +true`` restores the notices for anyone who wants them (#5784). +""" + +from __future__ import annotations + +from unittest.mock import AsyncMock + +import pytest + +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.qq.runtime import QQChannel, QQConfig + + +def _make_channel(**config_kwargs) -> QQChannel: + config = QQConfig(app_id="test_app", secret="test_secret", **config_kwargs) + channel = QQChannel(config, MessageBus()) + channel._client = object() # truthy: pass the initialized check + return channel + + +@pytest.mark.asyncio +async def test_compaction_notices_dropped_by_default(monkeypatch) -> None: + channel = _make_channel() + send_text = AsyncMock() + monkeypatch.setattr(channel, "_send_text_only", send_text) + + for phase in ("started", "succeeded"): + await channel.send(outbound_message_for_event( + channel="qq", chat_id="chat", + event=ContextCompactionEvent(compaction_id="c1", phase=phase), + )) + + send_text.assert_not_awaited() + + +@pytest.mark.asyncio +async def test_compaction_notices_sent_when_enabled(monkeypatch) -> None: + channel = _make_channel(show_compaction_notices=True) + send_text = AsyncMock() + monkeypatch.setattr(channel, "_send_text_only", send_text) + + await channel.send(outbound_message_for_event( + channel="qq", chat_id="chat", + event=ContextCompactionEvent(compaction_id="c1", phase="started"), + )) + + send_text.assert_awaited_once() + assert send_text.await_args.kwargs["content"] == "Compressing context…" + + +@pytest.mark.asyncio +async def test_ordinary_messages_unaffected(monkeypatch) -> None: + channel = _make_channel() + send_text = AsyncMock() + monkeypatch.setattr(channel, "_send_text_only", send_text) + + await channel.send(OutboundMessage(channel="qq", chat_id="chat", content="hello")) + + send_text.assert_awaited_once() + + +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