Skip to content

fix(qqofficial): keep markdown-rejected sends as passive replies - #10451

Open
he-yufeng wants to merge 4 commits into
AstrBotDevs:masterfrom
he-yufeng:fix/qqofficial-markdown-rejected-passive-reply
Open

he-yufeng wants to merge 4 commits into
AstrBotDevs:masterfrom
he-yufeng:fix/qqofficial-markdown-rejected-passive-reply

Conversation

@he-yufeng

@he-yufeng he-yufeng commented Oct 8, 2026 •

Copy link
Copy Markdown
Contributor

Fixes #10420.

When the QQ official API refuses a native markdown payload, the current fallback in _send_with_markdown_fallback drops msg_id and resends through the active-message interface. Active messages are quota limited (304049), so a rejected markdown turns an ordinary reply into a rate-limit error and the message is lost.

This branch is based directly on current master. The earlier #10401 dependency was removed after #10489 landed the message_reference behavior in master.

Modifications / 改动点

qqofficial_message_event.py: reorder the fallback so a rejected markdown degrades to a plain content reply that keeps msg_id, staying a passive reply (msg_type reset from 2 to 0, stream trailing newline restored before the retry). The stream newline correction also runs before the active-send fallback, so that retry keeps msg_id instead of becoming a proactive message. Only unrelated errors go to the active-send fallback, which keeps the message_reference two-stage retry landed through #10489. Fallback raises are chained so the original rejection stays visible in the logs.

test_qqofficial_group_message_create.py: four tests covering the markdown-rejected passive path, the non-markdown active fallback, the stream newline retry, and markdown rejection after an active retry.

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

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

On merged head a7de68ed1c6b1149ab50948e067f0cbe070f8dd3, all eight QQOfficial test modules pass 144 tests under Python 3.12.12. The stream newline test failed before the ordering fix with KeyError: msg_id and now passes together with the active-send control. Red/green was also verified for the original markdown-rejected behaviors. Current master 841295fa938d2cd028b9e0cb832f293c5c5c0d4b is merged without a rebase or force push. ruff check, ruff format --check, and PR-scoped git diff --check pass on both touched files. No live QQ traffic or credentials were used.

Checklist / 检查清单

  • 😊 If there are new features added in the PR, I have discussed it with the authors through issues/emails, etc. (no new feature; the fix direction is tracked in [Bug] QQ频道消息无法回复:markdown 回退顺序有误,误用主动消息导致 304049 限频 #10420)
  • 👀 My changes have been well-tested, and "Verification Steps" and "Screenshots" have been provided above.
  • 📚 No documentation update needed: this is an internal send path with no WebUI, navigation, or doc surface change.
  • 🤓 I have ensured that no new dependencies are introduced.
  • 😮 My changes do not introduce malicious code.

Summary by Sourcery

Keep QQOfficial markdown fallback replies passive to avoid unnecessary active-message quota failures and lost messages.

Bug Fixes:

  • Preserve passive QQOfficial replies when markdown payloads are rejected by falling back to plain content without losing the message identifier.
  • Keep stream newline retries passive and retain the existing active-send fallback for unrelated errors.

Tests:

  • Add regression coverage for markdown rejection, active-send fallback behavior, stream newline retries, and markdown rejection after an active retry.

@sourcery-ai sourcery-ai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Hey - I've found 1 issue

Prompt for AI Agents
Please address the comments from this code review:

## Individual Comments

### Comment 1
<location path="astrbot/core/platform/sources/qqofficial/qqofficial_message_event.py" line_range="698-711" />
<code_context>
-                    ret = await send_func(fallback_payload)
-                    logger.info("[QQOfficial] 使用主动发送接口发送成功。")
-                    return ret
-                except _QQOFFICIAL_SEND_API_ERRORS as fallback_err:
-                    err = fallback_err
-                    payload = fallback_payload
-
-            if not isinstance(err, botpy.errors.ServerError):
-                raise
</code_context>
<issue_to_address>
**Markdown fallback is skipped**

When the passive send fails for a non-Markdown reason and the active retry is rejected for Markdown, `_send_with_markdown_fallback` propagates the active retry's Markdown error before attempting the plain-content downgrade, so the message is lost.

Route Markdown rejection from the active retry through the plain-content fallback.
</issue_to_address>

Sourcery assessment

Needs a human reviewer. 1 finding to address first, and if the fallback classification or reply-reference handling is wrong, the code can send an unintended proactive or duplicate message, or retry with an invalid reference; those messages are externally delivered and cannot be undone by reverting the change. The impact is bounded to affected sends, but correcting the code will not retract messages already sent.

Blocking findings: astrbot/core/platform/sources/qqofficial/qqofficial_message_event.py:711


Sourcery is free for open source - if you like our reviews please consider sharing them ✨

@he-yufeng

Copy link
Copy Markdown
Contributor Author

The sourcery finding was real. Traced it: a passive send failing for a non-markdown reason (expired msg_id) drops into the active retry with the markdown payload still intact, and when that retry is rejected for markdown the error propagated before any plain-content downgrade, so the message was lost.

Fixed in 0d456f6: the active retry's markdown rejection now routes through the same plain-content downgrade (markdown popped, msg_type 2 to 0, stream newline fix kept). The reference retry order is unchanged, so a valid message_reference still survives. Added a regression test that pins the three-call sequence (passive fail, active markdown rejection, downgraded send) and verified it goes red without the fix.

When the QQ official API refuses a native markdown payload, the
fallback used to drop msg_id and resend through the active-message
interface, which runs into the 304049 active-message rate limit and
loses the reply threading. Reorder the fallback so a rejected markdown
degrades to a plain content reply that keeps msg_id (passive reply,
msg_type reset from 2 to 0, stream trailing newline restored). The
stream newline repair retry also keeps msg_id. Only errors that are
not markdown rejections take the active-send fallback, and if the
active retry itself hits the markdown rejection (the passive failure
was unrelated, e.g. an expired msg_id) it downgrades to plain content
too instead of losing the message. Fallback raises are chained so the
original rejection stays visible in logs.
@he-yufeng
he-yufeng force-pushed the fix/qqofficial-markdown-rejected-passive-reply branch from 0d456f6 to c0321a2 Compare October 12, 2026 21:25
@he-yufeng

Copy link
Copy Markdown
Contributor Author

重建说明:这个 PR 原本叠在 #10401 的两个提交上。#10489 已把出站 message_reference 的填充合进 master(595f990b6),走的是 msg_idx 语义,比 #10401 的裸 id 透传更正确,所以叠放部分整个摘掉,改为在 master 上重放本修复,diff 现在只剩 fallback 重排和 4 个测试。此前那个 follow-up(主动重试也撞 markdown 拒绝时同样降级纯 content)也一并带上了。

验证:tests/test_qqofficial_group_message_create.py 41/41 过;拿 master 版 helper 跑 markdown-rejected 和 stream-newline 两个用例定败(负对照),本分支定过。

sourcery-ai[bot]
sourcery-ai Bot previously approved these changes Oct 12, 2026

@sourcery-ai sourcery-ai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Sourcery assessment

Approved.

…d of the fallback

The markdown-rejected (50056) path must keep msg_id and degrade to a plain
content passive reply. Dropping msg_id first turns a passive reply into an
active message, which burns the active quota and can fail outright (304049).
Upstream added event_id to the active-send fallback; the merge keeps that while
restoring the markdown-rejected intercept before the fallback so msg_id is not
dropped on that path. The later markdown handler upstream carries runs after the
fallback and cannot prevent the msg_id drop, so the intercept is still needed.

Validation (CPU): tests/test_qqofficial_group_message_create.py 43 passed.
@sourcery-ai
sourcery-ai Bot dismissed their stale review November 1, 2026 07:43

Sourcery withdrew this approval because the latest commits introduced blocking findings.

@Soulter Soulter moved this to In Progress in AstrBot Roadmap Nov 2, 2026
@he-yufeng

Copy link
Copy Markdown
Contributor Author

Fixed in b36222b7e. The three platform failures were the same ordering bug: the stream newline error entered the active-send fallback first, which removed msg_id before the newline repair could run. The newline correction now runs before that fallback, while unrelated errors still use the active-send path.

Reproduced locally as KeyError: msg_id before the change. The newline case and non-markdown active-send control now pass, the full QQ Official module is 47 passed, and Ruff check/format are clean. The PR body also no longer describes the branch as stacked on #10401.

@he-yufeng

Copy link
Copy Markdown
Contributor Author

@Soulter @clown145 Could you review b36222b7?

When QQOfficial rejects a Markdown send, this keeps the fallback as a passive reply instead of retrying into another active delivery path. The quoted reply IDs remain explicit rather than inferred from an earlier message.

@clown145, your recent QQOfficial event changes are the closest ownership match. Could you check that the fallback does not duplicate or reclassify the outgoing message?

@he-yufeng

Copy link
Copy Markdown
Contributor Author

@Soulter @clown145 The current-master merge is now a7de68ed1c6b1149ab50948e067f0cbe070f8dd3, with all 7 hosted checks green.

Could you review the fallback ordering? Markdown rejection and the stream-newline correction keep msg_id before any active-send retry, while unrelated errors retain the existing active fallback. All eight QQOfficial test modules pass 144 tests on this head. No live QQ traffic was used.

@sourcery-ai

sourcery-ai Bot commented Nov 23, 2026

Copy link
Copy Markdown
Contributor

Linked issues

Checked at commit a7de68e

#10420 [Bug] QQ频道消息无法回复:markdown 回退顺序有误,误用主动消息导致 304049 限频 · 2 of 2 met

# Requirement Status Notes
1 On a markdown rejection, retry as a passive plain-content reply while preserving msg_id. ✅ Met The markdown-rejection branch removes markdown and sets content from plain_text before the active-send fallback, leaving msg_id and event_id intact.
2 Reset msg_type from 2 to 0 for the plain-content fallback. ✅ Met The fallback sets msg_type to 0 when the original value is 2.

This branch has not been deployed

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

Labels

None yet

Projects

Status: In Progress

Development

Successfully merging this pull request may close these issues.

[Bug] QQ频道消息无法回复:markdown 回退顺序有误,误用主动消息导致 304049 限频

1 participant