Skip to content

fix(qqofficial): require explicit ids for quoted replies - #10842

Open
he-yufeng wants to merge 3 commits into
AstrBotDevs:masterfrom
he-yufeng:fix/qqofficial-reference-only-with-id
Open

he-yufeng wants to merge 3 commits into
AstrBotDevs:masterfrom
he-yufeng:fix/qqofficial-reference-only-with-id

Conversation

@he-yufeng

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

Copy link
Copy Markdown
Contributor

Refs #10840.

Problem

A passive reply can arrive with message_type=103 and the previous message in msg_elements even when the user did not quote it. Treating the presence of quoted text as sufficient to create a Reply causes quote-preferring plugins to process the wrong message.

Fix

  • Create a Reply only when message_reference.message_id or the quoted element's own id/message_id is present.
  • Keep structured quoted attachments on the existing attachment path.
  • Preserve attachment-looking quoted text literally. The earlier regex recovery has been removed: content alone cannot distinguish QQ-generated metadata from a user-authored line with the same spelling. No image is inferred without structured attachment provenance.

This narrows the change to identifiable quotes and avoids altering ambiguous user text. Recovering flattened images needs an upstream provenance signal and is not claimed by this PR.

Verification

  • Regressions cover a passive echo without an ID, an explicit element-level quote ID, and two attachment-looking text forms that must stay literal. Existing structured-image coverage remains unchanged.
  • On merged head 878f40dc6cbcb88a08a3099112d555faae572d8c, tests/test_qqofficial_group_message_create.py passes 50 tests, and all eight QQOfficial test modules pass 144 tests under Python 3.12.12. This includes the passive-echo, explicit quote ID, structured attachment, literal text, ordered mention, and webhook cases.
  • Changed-file Ruff check, Ruff format check, and PR-scoped git diff --check pass. The full staged merge diff also exposes pre-existing whitespace in unrelated upstream documentation; those files are not changed by this PR.
  • The previous head f790a032d118390f3dc2e24c3e009f7e05fcfe70 passed all 23 hosted checks. Current master 841295fa938d2cd028b9e0cb832f293c5c5c0d4b is merged without a rebase or force push. No live QQ traffic, credentials, or network path was exercised locally.

Checklist

  • I have performed a self-review of my own code.
  • I have formatted my code.
  • I have added regression coverage and described the validation level above.
  • Hosted full-suite validation passes on all three platforms.
  • No WebUI, navigation, or setting changed, so no corresponding docs update is needed.

AI Assistance Disclosure

  • AI tools were used.

Claude Code assisted with investigating the issue, implementing the fix, and running the regression checks.

Summary by Sourcery

Require explicit quote identifiers in QQOfficial messages to prevent false replies and preserve ambiguous quoted content.

Bug Fixes:

  • Only create QQOfficial quoted-reply components when an explicit message or quoted-element ID is available, preventing passive reply echoes from referencing the wrong message.
  • Preserve attachment-like quoted text as literal text unless structured attachment data is present.

Tests:

  • Add regression coverage for passive reply echoes, element-level quote IDs, and attachment-like quoted text.

…lattened quoted images

QQ tags a passive reply with message_type 103 and puts the previous message
into msg_elements even when the user only @-mentioned the bot. The adapter
built a Reply whenever that echo produced any text, so quote-preferring
plugins started verifying a message nobody had quoted.

A quoted message id is what distinguishes a real quote from the echo, so the
Reply is now built only when message_reference.message_id or the quoted
element's own id is present. The echo text is dropped rather than presented as
a quote.

QQ also pre-renders a quoted attachment into the quoted text as an
[附件N] 类型:图片 ... URL:<url> line and leaves attachments empty, so quoted
images reached plugins as prose. Those lines are lifted back into Image
components and removed from the text. A populated attachments list still wins,
so the existing path is unchanged.

@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_platform_adapter.py" line_range="723-726" />
<code_context>
+    #: QQ renders a quoted attachment into the quoted text as
+    #: ``[附件N] 类型:图片 ... URL:<url>`` and leaves ``attachments`` empty, so the
+    #: image would otherwise reach plugins as unstructured prose.
+    _FLATTENED_IMAGE_LINE_RE = re.compile(
+        r"^\[附件\d+\]\s*类型:图片\b[^\n]*?URL:(\S+)[^\n]*\n?",
+        re.MULTILINE,
+    )
+
+    @staticmethod
</code_context>
<issue_to_address>
**Matching prose becomes an image**

When a quoted message contains literal text matching the attachment-line pattern, `_extract_flattened_quoted_images` treats any quoted-content line matching this pattern as QQ attachment metadata, removes it from the text, and returns its URL as an image. A user-authored line that happens to match the format is therefore misrepresented as an image instead of quoted literally.

Only lift lines when their attachment-metadata origin can be distinguished from ordinary quoted text; otherwise preserve ambiguous lines as text.
</issue_to_address>

Sourcery assessment

Approval pending. 1 finding to address first.

Blocking findings: astrbot/core/platform/sources/qqofficial/qqofficial_platform_adapter.py:726


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

@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.

@he-yufeng he-yufeng changed the title fix(qqofficial): stop inventing Reply on passive echoes and recover flattened quoted images fix(qqofficial): require explicit ids for quoted replies Nov 17, 2026
@codecov-commenter

Copy link
Copy Markdown

⚠️ Please install the 'codecov app svg image' to ensure uploads and comments are reliably processed by Codecov.

Codecov Report

❌ Patch coverage is 0% with 1 line in your changes missing coverage. Please review.

Files with missing lines Patch % Lines
.../sources/qqofficial/qqofficial_platform_adapter.py 0.00% 1 Missing ⚠️

📢 Thoughts on this report? Let us know!

@he-yufeng

Copy link
Copy Markdown
Contributor Author

@Soulter @clown145 Current master is merged in 878f40dc6cbcb88a08a3099112d555faae572d8c, and all 7 hosted checks pass.

Could you review the narrowed quote rule? A passive message_type=103 echo without an explicit reference or element ID no longer creates a Reply, while structured attachments and literal attachment-looking text keep their existing behavior. The eight QQOfficial test modules pass 144 tests on this merged head. No live QQ path was exercised.

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.

2 participants