Skip to content

fix(waking_check): guard wake-prefix check against empty message chain#8832

Open
he-yufeng wants to merge 1 commit into
AstrBotDevs:masterfrom
he-yufeng:fix/waking-check-empty-messages
Open

fix(waking_check): guard wake-prefix check against empty message chain#8832
he-yufeng wants to merge 1 commit into
AstrBotDevs:masterfrom
he-yufeng:fix/waking-check-empty-messages

Conversation

@he-yufeng

@he-yufeng he-yufeng commented Jun 16, 2026

Copy link
Copy Markdown
Contributor

问题

WakingCheckStage 在群聊里检查 wake-prefix 时,会无条件地访问 messages[0]

for wake_prefix in wake_prefixes:
    if event.message_str.startswith(wake_prefix):
        if (
            not event.is_private_chat()
            and isinstance(messages[0], At)   # messages 为空时 IndexError
            ...

messages = event.get_messages() 为空列表时,这里会抛 IndexError: list index out of range。两种常见触发:

  • wake_prefix 配成了空字符串 ""(让机器人对任何消息都唤醒),此时 message_str.startswith("") 恒为真;
  • 平台适配器给出的消息带有文本(message_str)但消息段列表为空(例如某些转发 / 系统类负载)。

只要同时满足「群聊 + message_str 命中 wake_prefix + 消息段为空」,唤醒检查就会崩在 messages[0]

修复

isinstance(messages[0], At) 前加一个 and messages 短路:消息段为空时不是「@ 了别人」,应当正常唤醒,而不是崩溃。改动只有一行,其余分支行为不变。

测试

新增 tests/unit/test_waking_check_stage.py,覆盖空 wake-prefix 和非空 wake-prefix 两种情况下消息段为空的场景。两个用例在改动前都会因 messages[0]IndexError,改动后通过;tests/unit/test_astr_message_event.py(78 个)保持全绿。

python -m pytest tests/unit/test_waking_check_stage.py -q   # 2 passed

Summary by Sourcery

Prevent group wake-prefix checking from crashing on events with an empty message chain.

Bug Fixes:

  • Guard wake-prefix @-mention checks against empty message lists to avoid IndexError in group chats.

Tests:

  • Add unit tests covering wake-prefix handling when the message text is present but the message component list is empty for both empty and non-empty prefixes.

@dosubot dosubot Bot added size:XS This PR changes 0-9 lines, ignoring generated files. area:core The bug / feature is about astrbot's core, backend labels Jun 16, 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 reviewed your changes and they look great!


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.

@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 adds a safety check to ensure that the messages list is not empty before attempting to access its first element in WakingCheckStage.process. This prevents potential IndexError crashes when processing events with empty message chains. Additionally, corresponding unit tests have been introduced to verify this fix. I have no further feedback to provide.

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.

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:XS This PR changes 0-9 lines, ignoring generated files.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant