Skip to content

fix(qqofficial): allocate fresh msg_seq per inbound msg_id (#2290)#2294

Open
dadachann wants to merge 1 commit into
dev/4.11.xfrom
fix/qqofficial-msg-seq-dedup
Open

fix(qqofficial): allocate fresh msg_seq per inbound msg_id (#2290)#2294
dadachann wants to merge 1 commit into
dev/4.11.xfrom
fix/qqofficial-msg-seq-dedup

Conversation

@dadachann

Copy link
Copy Markdown
Contributor

Summary

Fixes #2290. QQ Official v2 API deduplicates passive messages by (msg_id, msg_seq). The adapter reused msg_seq for multiple sends tied to the same inbound msg_id, so QQ rejected later sends with:

40054005 消息被去重,请检查请求msgseq

Three pre-existing strategies collided:

  • Text (send_private_text_msg / send_group_text_msg): no msg_seq at all → QQ defaulted it to 1, so any 2nd text reply under the same msg_id was a duplicate.
  • Rich media (_send_media_msg): used a global _msg_seq_counter — unique, but could still collide with the implicit msg_seq=1 of a text send under the same msg_id.
  • Streaming (reply_message_chunk): msg_seq pinned at 1 forever, only index advanced → every chunk reused msg_seq=1 (the most frequent trigger).

Changes

  • Add QQOfficialClient.next_reply_msg_seq(msg_id) — a per-inbound-msg_id allocator backed by a bounded OrderedDict (max 1024 keys, LRU eviction) + an asyncio.Lock for concurrency safety. No msg_id (proactive sends) falls back to the existing global counter.
  • Wire it into C2C/group text sends and rich media.
  • Streaming now advances ctx['msg_seq'] per chunk.
  • Guild channel sends (send_channle_*) are intentionally left unchanged — they hit the legacy guild API, which does not use this dedup mechanism.

Tests

Added 3 regression tests to test_qqofficial_eba_adapter.py:

  • allocator increments per msg_id, is independent across ids, falls back for missing id, and is gap-free under 50 concurrent allocations;
  • multiple text replies under one inbound msg_id carry msg_seq 1, 2, 3;
  • stream chunks emit strictly increasing, unique msg_seq.

All 12 tests in the file pass; ruff check and ruff format --check clean.

…0054005 dedup

QQ Official v2 API deduplicates passive messages by (msg_id, msg_seq).
The adapter reused msg_seq across multiple sends under the same inbound
msg_id, so multi-part text replies, rich media, and especially streaming
chunks (msg_seq pinned at 1) were rejected with:

  40054005 消息被去重,请检查请求msgseq

Add a per-inbound-msg_id sequence allocator (next_reply_msg_seq) on
QQOfficialClient, backed by a bounded OrderedDict + asyncio lock, and use
it for C2C/group text sends and rich media. Streaming now advances
ctx['msg_seq'] per chunk. Proactive sends (no msg_id) fall back to the
existing global counter.

Closes #2290
@dosubot dosubot Bot added size:M This PR changes 30-99 lines, ignoring generated files. eh: Improve enhance: 现有功能的改进 / improve current features IM: qqofficial QQ 官方 API Webhook 适配器相关 / QQ API (Webhook) adapter related labels Jun 29, 2026
@dadachann

Copy link
Copy Markdown
Contributor Author

CI 说明:当前红勾来自 base 分支 (dev/4.11.x) 的既有问题,与本 PR 无关

逐项核对:

Check 失败原因 是否本 PR 引入
Ruff Lint & Format dev/4.11.x 上已有 48 个文件未通过 ruff format --check(涉及 slack / telegram / wecombot / agent-runner / alembic 等,本 PR 均未触碰)
Unit Tests 3.11/3.12/3.13 51 个 collection error,全部为 ModuleNotFoundError: No module named 'langbot_plugin.api.entities.builtin.agent_runner',即 CI 解析到的 langbot_plugin 版本尚无该模块
E2E / Fast Integration 同上,import 阶段即失败
Frontend Lint 前端检查,与后端改动无关
Box Integration / CLA ✅ pass

验证:

  • 直接 checkout 未带本改动的干净 origin/dev/4.11.x,执行 uv run ruff format src --check 同样报 48 files would be reformatted —— 基线本身即红。
  • 本 PR 改动的 3 个文件单独跑 CI 同版本 ruff(0.14.14):All checks passed + 3 files already formatted
  • 51 个 test collection error 中无任何 qqofficial 相关项;本 PR 新增的 3 个回归测试本地全部通过(12 passed)。

结论:本 PR 代码与测试本身可通过全部相关检查。待 dev/4.11.x 的格式化债与 langbot_plugin 版本问题在 base 修复后 rerun 即可转绿。

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

Labels

eh: Improve enhance: 现有功能的改进 / improve current features IM: qqofficial QQ 官方 API Webhook 适配器相关 / QQ API (Webhook) adapter related size:M This PR changes 30-99 lines, ignoring generated files.

1 participant