Skip to content

fix: preserve inbound qqofficial msg anchor for proactive sends#6856

Open
jueshali wants to merge 1 commit intoAstrBotDevs:masterfrom
jueshali:codex/qqofficial-inbound-msg-anchor
Open

fix: preserve inbound qqofficial msg anchor for proactive sends#6856
jueshali wants to merge 1 commit intoAstrBotDevs:masterfrom
jueshali:codex/qqofficial-inbound-msg-anchor

Conversation

@jueshali
Copy link

@jueshali jueshali commented Mar 23, 2026

修复了 qq_official 在主动私聊发送场景下 msg_id 锚点被错误覆盖的问题。

此前,适配器会在一次主动发送成功后,将机器人自己发出的消息返回 id 写回会话缓存,并在下一次主动发送时继续把它当作 msg_id 使用。在 QQ Official C2C 场景下,这会导致后续主动发送出现如下报错:

  • 请求参数msg_id无效或越权

本次修改保证主动发送始终使用“最近一次用户入站消息”的 msg_id 作为发送锚点,而不会被机器人自己的出站消息 id 覆盖。

Modifications / 改动点

  • 修改了 astrbot/core/platform/sources/qqofficial/qqofficial_platform_adapter.py

  • 将会话消息缓存拆分为两类:

    • 用户入站消息 msg_id
    • 机器人主动发送后的返回 id
  • 主动发送时仅使用入站消息 msg_id 作为发送锚点

  • 主动发送成功后,不再用机器人自己的出站消息 id 覆盖入站锚点

  • This is NOT a breaking change. / 这不是一个破坏性变更。

Screenshots or Test Results / 运行截图或测试结果

验证步骤:

  1. 使用真实 qq_official 机器人配置启动 AstrBot。
  2. 先从 QQ 私聊向机器人发送一条消息,建立有效的入站 msg_id
  3. 通过 AstrBot 的真实 send_by_session 链路触发一次主动发送。
  4. 在没有新的入站消息情况下,再次触发一次主动发送。
  5. 确认两次主动消息均成功送达,且日志中未再出现 msg_id 越权报错。

验证结果:

  • QQ 私聊入站消息接收正常。
  • 第一次主动发送成功。
  • 在没有新入站消息的情况下,第二次主动发送仍然成功。
  • QQ 侧实际收到了两条主动消息。
  • 整个验证过程中未再出现 botpy.errors.ServerError: 请求参数msg_id无效或越权

示例日志:

[default] [qq-official-test(qq_official)] 5CDE98807986347C3CE73A719F2A62DF: [At:qq_official] 11
{"status":"ok","message":null,"data":{}}
{"status":"ok","message":null,"data":{}}

## Summary by Sourcery

Bug Fixes:
- Prevent QQ Official C2C proactive sends from failing due to overwritten msg_id anchors by separating inbound and outbound message IDs in session cache.

@auto-assign auto-assign bot requested review from LIghtJUNction and anka-afk March 23, 2026 14:32
@dosubot dosubot bot added the size:S This PR changes 10-29 lines, ignoring generated files. label Mar 23, 2026
@gemini-code-assist
Copy link
Contributor

Summary of Changes

Hello, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed!

此拉取请求旨在解决 qq_official 适配器中一个关键问题,即在主动私聊发送场景下,机器人的出站消息ID会错误地覆盖用户入站消息ID,导致后续主动发送失败并出现“请求参数msg_id无效或越权”的错误。通过引入独立的入站和出站消息ID缓存机制,本变更确保了主动发送始终能正确地使用用户入站消息ID作为锚点,从而保证了消息的稳定送达。

Highlights

  • 消息ID缓存分离: 将会话消息ID缓存拆分为用户入站消息ID和机器人出站消息ID两类。
  • 主动发送锚点修正: 确保主动发送消息时始终使用最近一次用户入站消息的ID作为发送锚点。
  • 避免ID覆盖: 修复了机器人主动发送后的返回ID错误覆盖入站消息ID的问题。
Using Gemini Code Assist

The full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips.

Invoking Gemini

You can request assistance from Gemini at any point by creating a comment using either /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands on the current page.

Feature Command Description
Code Review /gemini review Performs a code review for the current pull request in its current state.
Pull Request Summary /gemini summary Provides a summary of the current pull request in its current state.
Comment @gemini-code-assist Responds in comments when explicitly tagged, both in pull request comments and review comments.
Help /gemini help Displays a list of available commands.

Customization

To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a .gemini/ folder in the base of the repository. Detailed instructions can be found here.

Limitations & Feedback

Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for GitHub and other Google products, sign up here.

Footnotes

  1. Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution.

Copy link
Contributor

@sourcery-ai sourcery-ai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey - I've left some high level feedback:

  • The new _session_last_outbound_message_id cache and remember_session_sent_message_id helper are currently written but never read; if there’s no planned usage, consider removing them to avoid confusion and keep the adapter’s state minimal.
  • remember_session_message_id now delegates to remember_session_inbound_message_id, which changes its semantics to inbound-only; if this method is part of a broader adapter interface, it may be worth adding a short comment in-code to clarify that it intentionally represents inbound anchors for proactive sends.
Prompt for AI Agents
Please address the comments from this code review:

## Overall Comments
- The new `_session_last_outbound_message_id` cache and `remember_session_sent_message_id` helper are currently written but never read; if there’s no planned usage, consider removing them to avoid confusion and keep the adapter’s state minimal.
- `remember_session_message_id` now delegates to `remember_session_inbound_message_id`, which changes its semantics to inbound-only; if this method is part of a broader adapter interface, it may be worth adding a short comment in-code to clarify that it intentionally represents inbound anchors for proactive sends.

Sourcery is free for open source - if you like our reviews please consider sharing them ✨
Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.

@dosubot dosubot bot added the area:platform The bug / feature is about IM platform adapter, such as QQ, Lark, Telegram, WebChat and so on. label Mar 23, 2026
Copy link
Contributor

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

本次 PR 修复了 qq_official 适配器在主动发送私聊消息时,错误地使用机器人自身发出的消息 ID 覆盖用户入站消息锚点的问题。通过将会话缓存中的消息 ID 分为入站和出站两类,确保了主动发送始终使用正确的用户消息锚点,解决了后续发送失败的 bug。代码修改清晰地实现了这一逻辑分离,可读性好。我提出了一个关于简化兼容性方法实现的建议,以提高代码的可维护性。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area:platform The bug / feature is about IM platform adapter, such as QQ, Lark, Telegram, WebChat and so on. size:S This PR changes 10-29 lines, ignoring generated files.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants