[Bug] Agent 执行期间的插话只在「工具结果边界」生效:单次长生成、后处理/发送阶段收不到,且被会话锁排队到整轮结束

Aperta
#11,133 0 commenti 0 reazioni 0 assegnatari Vedi su GitHub

I maintainer di solito rispondono entro 1 giorno

Nessuno ha ancora preso questa issue.

Valutazione

Difficoltà
5/5
Tempo stimato
Più di una settimana
Idoneità per principianti
15/100
Tipo di issue
Bug
Chiarezza
Abbastanza chiara
Stato di attività
Attiva
Stack tecnologico
python

Direzione di ricerca

The turn lock is taken in pipeline/process_stage/method/agent_sub_stages/internal.py around line 239, and follow-up capture sits in pipeline/process_stage/follow_up.py. Injection only happens in agent/runners/tool_loop_agent_runner.py at the tool-result merge points. Reading these shows where a mid-generation message could enter context and how the lock spans post-processing. The issue asks for several design decisions (step-boundary injection, lock scope, group policy), so a maintainer should settle the scope before anyone implements it.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Descrizione

现象

私聊中让 Agent 执行多步任务(连续调用工具,单轮 40–120s)。这期间用户再发消息,会出现以下情况:

  1. 模型看不到插话:消息已被适配器立刻收到(日志 18:29:52.165),但当前这一轮完全不体现它。实测某次 LLM 调用 upstream;dur=48675(48.7s),整个生成期间插话无法进入上下文。

  2. 插话被静默排队到整轮结束:新消息卡在 InternalAgentSubStage 的会话锁 session_lock_manager.acquire_lock(event.unified_msg_origin),直到上一条 turn 把 TTS、渲染、分段发送、落库全部做完才拿到锁。实测:

    18:28:41.417  用户消息 A 到达
    18:29:23.535  A 的 agent 循环结束(finish_reason=stop)
    18:29:52.227  用户消息 B 到达(A 仍在后处理)
    18:29:56.261  B 进入 internal stage("ready to request llm provider")
    18:30:03.774  A 的 TTS 超时,降级为文字
    18:30:04.025  A 的 pipeline 结束
    18:30:04.026  B 才 "acquired session lock for llm request"   ← 等待 7.77s
    

    A 的 agent 循环在 18:29:23 已结束,B 仍要等到 18:30:04。

  3. 没有反馈:被捕获或被阻塞的消息不发 typing,也没有「已收到,本轮结束后处理」的回执;核心侧没有日志(本机全量日志中 Captured follow-up 出现 0 次),用户无法区分消息丢失和消息在等待。

复现

  1. 私聊,本地 Agent(agent_runner.runner_type: local),开启工具,max_steps 默认。
  2. 发一条会触发多步工具调用、单轮 40s 以上的指令(例如「查一下 X,再根据结果写个脚本跑一遍」)。
  3. 分别尝试:
    • (a) 工具循环仍在跑时插话;
    • (b) 模型正在单次长生成时插话;
    • (c) 工具循环已结束、机器人正在 TTS 或分段发送时插话。
  4. 观察:(a) 有时有效,插话出现在下一个工具结果后面;(b) 永远无效;(c) 永远无效,且要等整轮结束。

源码证据(v4.29.4 6133541)

  1. 事件层是并发的,不是收不到 —— astrbot/core/event_bus.py:73:task = asyncio.create_task(scheduler.execute(event)),无全局队列,也没有按 UMO 入队。

  2. 排队来自核心会话锁 —— pipeline/process_stage/method/agent_sub_stages/internal.py:239:

    async with session_lock_manager.acquire_lock(event.unified_msg_origin):
    

    锁覆盖 build_main_agent、agent 循环和 _save_to_history,包含后处理与发送,所以释放时间是「整轮结束」而不是「LLM 结束」。

  3. 插话只注入到工具结果 —— agent/runners/tool_loop_agent_runner.py:737-741, 1187-1192:content=self._merge_follow_up_notice(content),只拼在 ToolCallMessageSegment 上;没有在 step 边界追加 user 消息的路径。

  4. agent 循环 DONE 后拒绝插话,但 turn 仍持锁 —— tool_loop_agent_runner.py:704:

    if self.done() or self._is_stop_requested():
        return None
    
  5. 跨发送者(群聊)不捕获 —— pipeline/process_stage/follow_up.py:186-188:active_sender_id != sender_id → return None。

  6. 捕获等待期无反馈 —— internal.py:203-225:顺序是 try_capture_follow_up() → prepare_follow_up_capture()(await ticket.resolved.wait())→ 之后才 send_typing()。

  7. /stop 只停输出,不携带新消息 —— tool_loop_agent_runner.py:117-118, 1560-1600:写死 USER_INTERRUPTION_REQUEST="Stop output." / USER_INTERRUPTION_MESSAGE="Output stopped."。

期望行为

  1. 生成期间的插话应能被模型看到:即使当前没有工具调用,也应在下一个 step 边界(必要时中止当前生成并重跑该 step)把新输入并入上下文,而不是只能等 tool result。
  2. 插话窗口应覆盖整个 turn:从进入 agent 阶段到 turn 真正结束(含渲染、TTS、发送);或把 TTS 和发送移出会话锁,让锁释放时间接近 LLM 结束时间。
  3. 用户应能感知:被接纳的插话给出 typing 或简短回执;未被接纳(跨发送者、turn 已结束)应有明确日志或回执,而不是静默排队。
  4. 群聊多发送者策略要明确:提供「按发送者排队 / 允许 steer 当前 turn」的选项。
  5. 连发多条可合并:现在按严格序号把 N 条变成 N 个后续 turn,建议支持合并成一条补充输入。

与已有实现 / issue 的关系

  • master 630d5cee 已有 AgentTurn.steer() / AgentSession.steer_turn()(agent/runtime.py:286-311、astr_agent_runtime/session.py:231-252),但注入点仍是 tool result(tool_loop_agent_runner.py:1418-1424)、done 后仍拒绝、会话锁仍在(astr_agent_runtime/session.py:60-66)。本 issue 是在已有 steer 上补边界与可观测性,不是从零做功能。
  • #9257(closed)曾以「插话功能本身正常」关闭;本 issue 区分「工具循环内的插话」与「长生成、后处理期间的插话」。
  • #10722(open)是在途分段不可打断;#8430(open)是会话锁内插件钩子超时。两者与本 issue 不同层。

版本 / 环境

  • AstrBot v4.29.4(6133541,2026-10-31);已核对 master 630d5cee(2026-11-14)
  • Linux systemd,aiocqhttp(QQ),agent_runner.runner_type: local,streaming_response=true,max_steps=128
  • 第三方插件 astrbot_plugin_private_companion 仅做 2.2–8s 有界防抖合并,不是排队原因;其 private_reply_followup.py 注释也复现了「core 只在 tool result 捕获插话」这一限制。
Lingua principale
Python
Stelle
46.9k
Fork
3.6k
Merge medio
1g 6h
PR unite (30g)
136

Preparare l'ambiente

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Altre issue di AstrBotDevs/AstrBot

Tutte le issue di AstrBotDevs/AstrBot

Issue simili

Altre issue su Python

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.