Skip to content

fix:修复了添加自定义报错反而会漏报#7113

Open
piexian wants to merge 3 commits intoAstrBotDevs:masterfrom
piexian:fix/non-streaming-custom-error-yield
Open

fix:修复了添加自定义报错反而会漏报#7113
piexian wants to merge 3 commits intoAstrBotDevs:masterfrom
piexian:fix/non-streaming-custom-error-yield

Conversation

@piexian
Copy link
Copy Markdown
Contributor

@piexian piexian commented Mar 28, 2026

当人格配置了自定义报错文案后,用户侧的报错出口并没有被完整覆盖。

具体表现为:

  • 当异常发生在 run_agent() 之前,例如 build_main_agent() 的前置处理、reset_coro、图片/引用图片解析等阶段时,异常会直接落入 InternalAgentSubStage 的外层 except,用户仍然会看到原始内部错误,或者看到空前缀错误。
  • 即使异常已经进入 Agent 执行链,run_agent()ToolLoopAgentRunnerthird_party runner 等不同路径的 fallback 文案也并不统一,有些路径仍会把底层异常文本直接暴露到消息平台。
  • 这导致“已配置自定义报错文案”这一能力不能覆盖所有用户可见错误场景,和预期行为不一致。

本次修改将用户侧错误出口统一为:

  • 有人格自定义报错文案时,优先展示自定义文案;
  • 没有自定义报错文案时,展示统一的通用报错文案;
  • 原始异常、错误类型、traceback 仅保留在日志中,不再直接发送到消息平台。

Modifications / 改动点

修改了以下核心文件:

  • astrbot/core/pipeline/process_stage/method/agent_sub_stages/internal.py
  • astrbot/core/astr_agent_run_util.py
  • astrbot/core/agent/runners/tool_loop_agent_runner.py
  • astrbot/core/pipeline/process_stage/method/agent_sub_stages/third_party.py
  • astrbot/core/persona_error_reply.py

实现内容:

  1. 将人格自定义报错文案的准备时机前移到 build_main_agent() 之前,避免前置异常绕过已有的人格报错逻辑。
  2. persona_error_reply.py 中新增统一的用户侧错误文案解析 helper,统一处理:
    • 人格自定义报错文案
    • 通用兜底文案
  3. 将以下用户可见错误出口统一改为同一规则:
    • InternalAgentSubStage 外层异常
    • run_agent() 异常分支
    • ToolLoopAgentRunnerllm_resp.role == "err" 分支
    • third-party agent runner 异常分支
  4. 将这些路径中的原始异常文本改为只写日志,并补充 exc_info=True,保留排障信息但不再直接暴露给用户。
  • This is NOT a breaking change. / 这不是一个破坏性变更。

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


Checklist / 检查清单

  • 😊 If there are new features added in the PR, I have discussed it with the authors through issues/emails, etc.
    / 如果 PR 中有新加入的功能,已经通过 Issue / 邮件等方式和作者讨论过。

  • 👀 My changes have been well-tested, and "Verification Steps" and "Screenshots" have been provided above.
    / 我的更改经过了良好的测试,并已在上方提供了“验证步骤”和“运行截图”

  • 🤓 I have ensured that no new dependencies are introduced, OR if new dependencies are introduced, they have been added to the appropriate locations in requirements.txt and pyproject.toml.
    / 我确保没有引入新依赖库,或者引入了新依赖库的同时将其添加到 requirements.txtpyproject.toml 文件相应位置。

  • 😮 My changes do not introduce malicious code.
    / 我的更改没有引入恶意代码。

Copilot AI review requested due to automatic review settings March 28, 2026 20:15
@dosubot dosubot bot added the size:XS This PR changes 0-9 lines, ignoring generated files. label Mar 28, 2026
Copy link
Copy Markdown
Contributor

@sourcery-ai sourcery-ai bot left a comment

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:

  • In the non-streaming error branch, consider yielding a consistent value (e.g., an empty MessageChain or the same MessageChain().message(err_msg)) instead of a bare yield so that downstream consumers don’t unexpectedly receive None while the streaming branch yields a message.
  • Add a short comment above the added yield explaining that it’s required to trigger the pipeline scheduler and deliver the error result to RespondStage, so future refactoring doesn’t accidentally remove it.
Prompt for AI Agents
Please address the comments from this code review:

## Overall Comments
- In the non-streaming error branch, consider yielding a consistent value (e.g., an empty `MessageChain` or the same `MessageChain().message(err_msg)`) instead of a bare `yield` so that downstream consumers don’t unexpectedly receive `None` while the streaming branch yields a message.
- Add a short comment above the added `yield` explaining that it’s required to trigger the pipeline scheduler and deliver the error result to `RespondStage`, so future refactoring doesn’t accidentally remove it.

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.

@dosubot dosubot bot added the area:core The bug / feature is about astrbot's core, backend label Mar 28, 2026
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

Fixes a pipeline delivery gap where, in non-streaming agent runs, an LLM failure would set an error result on the event but never yield, so downstream stages (notably RespondStage) wouldn’t run and the error message could be silently dropped.

Changes:

  • Add a missing yield after astr_event.set_result(...) in run_agent()’s non-streaming exception path so the scheduler can execute downstream stages and send the error to the user.

Copy link
Copy Markdown
Contributor

@gemini-code-assist gemini-code-assist bot left a comment

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 modifies the run_agent function in astrbot/core/astr_agent_run_util.py by adding a yield statement after setting the result on an event in the error handling path. This change ensures the asynchronous generator yields control back to the caller in this specific execution path. I have no feedback to provide.

@piexian piexian closed this Mar 29, 2026
@piexian piexian reopened this Mar 29, 2026
@dosubot dosubot bot added size:L This PR changes 100-499 lines, ignoring generated files. and removed size:XS This PR changes 0-9 lines, ignoring generated files. labels Mar 29, 2026
@piexian
Copy link
Copy Markdown
Contributor Author

piexian commented Mar 29, 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:L This PR changes 100-499 lines, ignoring generated files.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants