diff --git a/nanobot/channels/qq/runtime.py b/nanobot/channels/qq/runtime.py index 3de44ff1b..505bef2d8 100644 --- a/nanobot/channels/qq/runtime.py +++ b/nanobot/channels/qq/runtime.py @@ -707,6 +707,13 @@ class QQChannel(BaseChannel): ) return + # Resolve the quote before attachment downloads let newer messages + # evict this message's captured payload. + quoted = _quoted_content_for(message) + if quoted: + quote_tag = f"[Reply to: {quoted}]" + content = f"{quote_tag}\n{content}" if content else quote_tag + # the data used by tests don't contain attachments property # so we use getattr with a default of [] to avoid AttributeError in tests attachments = cast( @@ -715,14 +722,6 @@ class QQChannel(BaseChannel): ) media_paths, recv_lines, att_meta = await self._handle_attachments(attachments) - # Surface the quoted message to the agent. The inbound reply context - # is always on, matching Telegram; QQ needs the payload hook above - # because it exposes no way to fetch a message by id. - quoted = _quoted_content_for(message) - if quoted: - quote_tag = f"[Reply to: {quoted}]" - content = f"{quote_tag}\n{content}" if content else quote_tag - # Compose content that always contains actionable saved paths if recv_lines: tag = ( diff --git a/nanobot/channels/qq/tests/test_qq_reply_context.py b/nanobot/channels/qq/tests/test_qq_reply_context.py index ce73f32bb..dc158ff41 100644 --- a/nanobot/channels/qq/tests/test_qq_reply_context.py +++ b/nanobot/channels/qq/tests/test_qq_reply_context.py @@ -280,6 +280,42 @@ async def test_c2c_quote_is_prefixed_onto_content(captured): assert content.endswith("test reference agent") +@pytest.mark.asyncio +@requires_botpy +async def test_quote_survives_payload_eviction_during_attachment_download(captured, monkeypatch): + from botpy.message import C2CMessage + + channel = QQChannel( + QQConfig(app_id="app", secret="secret", allow_from=["user1"], ack_message=""), + MessageBus(), + ) + channel._handle_message = AsyncMock() + payload = dict( + C2C_QUOTING_BOT, + author={"user_openid": "user1"}, + attachments=[{"url": "https://example.com/image.png", "filename": "image.png"}], + ) + message = C2CMessage(api=None, event_id="quoted", data=payload) + + async def download_with_new_messages(url, filename_hint=""): + for index in range(qq_runtime.QQ_RAW_PAYLOAD_CACHE): + C2CMessage( + api=None, + event_id=f"plain-{index}", + data={"id": f"plain-{index}", "content": "plain message"}, + ) + return None + + monkeypatch.setattr(channel, "_download_to_media_dir_chunked", download_with_new_messages) + + await channel._on_message(message, is_group=False) + + content = channel._handle_message.await_args.kwargs["content"] + assert content.startswith("[Reply to: 查完了,两处都没有叫 reference 的东西。]\n") + assert "test reference agent" in content + assert "Received files:\n- image.png\n saved: [download failed]" in content + + @pytest.mark.asyncio @requires_botpy async def test_group_quote_is_prefixed_onto_content(captured):