AstrBot icon indicating copy to clipboard operation
AstrBot copied to clipboard

[Bug] 后台任务结果未送达用户时不产生任何日志

Open silvaling opened this issue 1 month ago • 1 comments

问题描述

后台任务(子代理)完成后,框架会唤醒主 Agent 并把结果注入其上下文,同时为这一轮注册 send_message_to_user,由主 Agent 决定是否转达。子代理作为 worker 不直接对外发言、统一由主 Agent 转达,是合理的职责分离 —— 这部分不是问题。

问题在于:当主 Agent 未调用 send_message_to_user 时,结果不会送达用户,且没有任何 WARNING / ERROR,也没有兜底或重试。

该失败模式在代码中已被明确写出(astrbot/core/astr_agent_tool_exec.py:617-623):

"If you need to deliver the result to the user immediately, "
"you MUST use `send_message_to_user` tool to send the message directly to the user, "
"otherwise the user will not see the result. "

即:设计上已知「不调用该工具 = 用户看不到结果」,但未对此做任何检测。

具体表现为三点:

  1. 日志静默。 该路径只有两处相关日志——astrbot/core/astr_agent_tool_exec.py:637 的 logger.error("Failed to build main agent ..."),以及 astrbot/core/astr_agent_tool_exec.py:662 的 logger.warning("background task agent got no response")。后者仅在 llm_resp 为空时触发;模型正常返回、只是选择不发送时,不输出任何日志。

  2. 连 pipeline 的日志也没有。 该路径直接 runner.step_until_done() → get_final_llm_resp(),不经过消息 pipeline,因此不会出现 respond.stage 的 INFO 记录。

  3. 结果被写回了对话历史(astrbot/core/astr_agent_tool_exec.py:648-654,经 persist_agent_history 写入 summary_note)。对 Agent 侧不丢、只对用户侧丢——这使问题更难被发现:后续对话中它会以 [BackgroundTask] xxx finished. Result: ... 的形式出现,看起来一切正常。

实际观测:一次子代理任务已完成并返回完整结果,用户始终未收到任何通知,事后翻日志才发现该轮没有任何告警。

期望行为

当后台任务结果已生成、但本轮未通过 send_message_to_user 投递时,至少输出一条 WARNING(包含 task_id、tool_name),使该情况可被发现和排查。

是否自动重发该结果可以讨论,但当前的完全静默不是期望行为:用户既收不到结果,运维也无从得知——只能事后翻日志。

建议实现位置

astrbot/core/astr_agent_tool_exec.py:640-663。在取得 llm_resp 之后,判断本轮是否调用过 SendMessageToUserTool;若未调用且结果非空,则输出 WARNING。现有的 if not llm_resp: logger.warning(...)(:662)只覆盖了「模型完全没有响应」这一种情况。

若希望更彻底,可在同一位置将未投递的 llm_resp.completion_text 作为普通回复投递一次;但这会改变行为语义,建议先只加 WARNING。

如何复现?

  1. 触发一个后台任务(例如让主 Agent 拉起一个子代理),等待其完成。
  2. 使主 Agent 在未调用 send_message_to_user 的情况下结束唤醒轮(例如判定「本轮没有可交付的完成结果」)。
  3. 观察结果:
    • 用户侧:收不到任何消息;
    • data/logs/astrbot.log:该轮无 WARNING / ERROR,也无 respond.stage 记录;
    • 下一轮对话:结果会以 [BackgroundTask] ... finished. Result: ... 的形式出现在上下文里。

AstrBot 版本

4.29.0-beta.1

操作系统

Windows

部署方式

Windows 桌面版(AstrBotLauncher)

使用的消息平台适配器

QQ 官方机器人(qq_official);同一实例亦运行 aiocqhttp

错误日志

本问题的特征是没有错误日志。以下为一次唤醒轮的完整生命周期(Debug 级),从开始到结束没有任何 WARNING / ERROR,也没有任何向用户投递的动作:

15:10:53.650 [Core] Agent state transition: AgentState.IDLE -> AgentState.RUNNING        ← 唤醒轮开始
15:10:53.835 [Core] [DBUG] [openai._base_client:505]: Request options: ... '# BACKGROUND TASK CONTEXT\nThe following object describes the background task that completed:\n{"task_id": "9553d3db...", ...}'
15:11:01.403 [Core] [DBUG] [sources.openai_source:586]: completion: ChatCompletion(id='29e4c924...', finish_reason='tool_calls', ... tool_calls=[astrbot_execute_shell])
15:11:05.310 [Core] [DBUG] [sources.openai_source:586]: completion: ChatCompletion(id='97553b01...', finish_reason='tool_calls', ... tool_calls=[astrbot_execute_python])
15:11:10.686 [Core] [DBUG] [sources.openai_source:586]: completion: ChatCompletion(id='1be1ea85...', finish_reason='stop', ...)
15:11:10.689 [Core] Agent state transition: AgentState.RUNNING -> AgentState.DONE         ← 唤醒轮结束
15:11:10.709 [Core] hook(OnAgentDoneEvent) -> ...

该轮结束后:无 send_message_to_user 调用、无 WARNING、无 ERROR、无 respond.stage 记录。


附带发现

同一路径使用的系统提示词 BACKGROUND_TASK_RESULT_WOKE_SYSTEM_PROMPT 缺少 4 个行尾 \n,导致规则 1–4 被拼接为一个 473 字符的长行、规则 5 与 # BACKGROUND TASK CONTEXT 标题粘连(astrbot/core/astr_main_agent_resources.py:108 @ 73d1b7dd7)。修复方式为在该文件 L108 / L109 / L110 / L112 各补一个 \n(注意 L112 与 L98 文本逐字节相同,需按行号修)。

由于 Do NOT respond if no meaningful action is required. 因此直接贴在规则 2 的开头,可能加剧本问题。此处仅作记录,细节可另开一条。

检查清单

  • [x] 我已在 Issue 列表中搜索过相关问题仍无法解决,或该问题从未被报告过。
  • [x] 我已尝试过禁用所有插件,排除了可能是因插件导致的问题。
  • [x] 我报告的问题与 AstrBot 本体相关,而非在报告某一插件的问题。
  • [x] 我已阅读并同意本项目的贡献者行为准则。
  • [ ] (可选)我愿意提交 PR 以帮助修复该问题。

silvaling avatar Oct 05 '26 08:10 silvaling

PR 已提交:#10398。

机制核实无误:唤醒轮直接走 runner.step_until_done() → get_final_llm_resp(),不经过 pipeline,现有的 background task agent got no response(astr_agent_tool_exec.py:662)只在 llm_resp 为空时触发,模型正常返回、只是没调 send_message_to_user 时确实完全静默。修复在唤醒轮结束后检查 cron_event._has_send_oper:send_message_to_user 成功投递到当前会话时会置位,插件工具直接发消息也会,所以判断的是有没有送达而不是有没有调用,发送失败或发到别的会话同样会告警,告警带 tool_name 和 task_id。按 issue 的建议只加 WARNING、不自动重发(#9980 讨论过模型可能有意静默);附带发现的提示词换行 #10269 / #9398 已经在改,这里不重复处理。

niansia avatar Oct 05 '26 10:10 niansia