Skip to content

fix: apply forward threshold to send_message_to_user#9242

Closed
Last-emo-boy wants to merge 2 commits into
AstrBotDevs:masterfrom
Last-emo-boy:fix/8678-forward-threshold
Closed

fix: apply forward threshold to send_message_to_user#9242
Last-emo-boy wants to merge 2 commits into
AstrBotDevs:masterfrom
Last-emo-boy:fix/8678-forward-threshold

Conversation

@Last-emo-boy

@Last-emo-boy Last-emo-boy commented Jul 13, 2026

Copy link
Copy Markdown
Contributor

Fixes #8678.

Modifications / 改动点

  • Apply the existing platform_settings.forward_threshold rule to send_message_to_user output.

  • Limit conversion to messages sent from an aiocqhttp event through the same adapter instance, where a valid bot UIN is available.

  • Preserve plain-message behavior for cron, cross-platform, cross-instance, and non-aiocqhttp sends.

  • Keep current-session duplicate reply suppression unchanged by recording plain text before wrapping the message in a forward node.

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

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

Verification:

  • uv run pytest tests/unit/test_message_tools.py tests/unit/test_func_tool_manager.py tests/unit/test_astr_agent_tool_exec.py -q — 50 passed.
  • uv run ruff format --check . — 480 files already formatted.
  • uv run ruff check . — passed.
  • git diff --check origin/master — passed.

Regression coverage includes the strict > boundary, same-instance forwarding, current-session deduplication, and the cron/cross-platform fallback to plain messages.


Checklist / 检查清单

  • This is a bug fix and does not add a new feature.
  • The change is covered by targeted regression tests.
  • No new dependency is introduced.
  • This change does not introduce malicious code.

Summary by Sourcery

Apply QQ forward-threshold handling to SendMessageToUserTool while preserving existing plain-message behavior and current-session deduplication.

Bug Fixes:

  • Ensure long aiocqhttp tool messages are wrapped into merged-forward nodes according to platform forward_threshold settings.
  • Prevent creation of invalid merged-forward nodes for cross-platform or cross-instance sends, falling back to plain messages instead.
  • Maintain correct duplicate-reply suppression by recording the original plain text before wrapping messages in forward nodes.

Tests:

  • Add unit tests covering aiocqhttp forward-threshold application, boundary behavior at the threshold, and cross-platform behavior for SendMessageToUserTool.

@gemini-code-assist gemini-code-assist 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.

Code Review

This pull request updates message_tools.py to apply the aiocqhttp forward threshold to tool messages, wrapping long messages in a merged-forward node when they exceed the threshold. It also adds corresponding unit tests. The review feedback highlights two key improvements: first, adding defensive handling in message_tools.py for cases where get_config returns None or the threshold is not a valid integer; second, fixing a false-positive unit test where a missing mock for get_platform_name caused the threshold logic to be bypassed entirely.

Important

The consumer version of Gemini Code Assist on GitHub is being sunset. Starting June 18, 2026, new organization installations will be blocked, and all code review activity will officially cease on July 17, 2026.
For more details on the timeline and next steps, please review the Help Documentation.

Comment thread astrbot/core/tools/message_tools.py
Comment thread tests/unit/test_message_tools.py
@Last-emo-boy
Last-emo-boy force-pushed the fix/8678-forward-threshold branch from 8675f88 to 4ed9bca Compare July 13, 2026 07:55
@Last-emo-boy
Last-emo-boy force-pushed the fix/8678-forward-threshold branch from 4ed9bca to ac70a21 Compare July 14, 2026 03:27
@Last-emo-boy
Last-emo-boy marked this pull request as ready for review July 14, 2026 03:33
@gemini-code-assist

Copy link
Copy Markdown
Contributor

Warning

You have reached your daily quota limit. Please wait up to 24 hours and I will start processing your requests again!

@dosubot dosubot Bot added size:M This PR changes 30-99 lines, ignoring generated files. area:core The bug / feature is about astrbot's core, backend labels Jul 14, 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.

Hey - I've left some high level feedback:

  • The forward-threshold logic you’ve added here duplicates behavior that likely already exists in ResultDecorateStage; consider extracting this into a shared helper so the QQ forwarding rules stay consistent in one place.
  • Instead of manually iterating over message_chain.chain to compute word_cnt for Comp.Plain components, you could reuse message_chain.get_plain_text() and take its length, which would simplify the logic and reduce the risk of divergence if MessageChain behavior changes.
Prompt for AI Agents
Please address the comments from this code review:

## Overall Comments
- The forward-threshold logic you’ve added here duplicates behavior that likely already exists in `ResultDecorateStage`; consider extracting this into a shared helper so the QQ forwarding rules stay consistent in one place.
- Instead of manually iterating over `message_chain.chain` to compute `word_cnt` for `Comp.Plain` components, you could reuse `message_chain.get_plain_text()` and take its length, which would simplify the logic and reduce the risk of divergence if `MessageChain` behavior changes.

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.

@Soulter

Soulter commented Jul 18, 2026

Copy link
Copy Markdown
Member

thanks for your contribution, but we have no plan on this feature.

@Soulter Soulter closed this Jul 18, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area:core The bug / feature is about astrbot's core, backend size:M This PR changes 30-99 lines, ignoring generated files.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bug]send_message_to_user 工具调用不触发合并转发消息

2 participants