Skip to content

agentV2 点击停止后经常无法停止 #7665

Description

@goactiongo

4.16.0

agentV2 点击停止后经常无法停止,F12 STOP如下:

Image

有时可以停止,F12 STOP如下

Image

分析

Redis 标志删除与 MongoDB 状态更新的竞态

时序问题:

工作流的 finally 块(dispatch/index.ts:388)先删除 Redis 停止标志
Stop API 的 waitForWorkflowComplete 检测到标志删除,立即读取 MongoDB 的 chatGenerateStatus
但 updateChatGenerateStatus 或 finalizeChatRound 是在工作流返回到 v2/chat/completions.ts:453 之后才调用的
此时 MongoDB 中的状态仍然是 generating (0),还没被更新为 done (1)
关键代码路径:

dispatch/index.ts finally 块:
  → delAgentRuntimeStopSign()  // 删除 Redis 标志
  
v2/chat/completions.ts:
  → await dispatchWorkFlow()   // 等待工作流返回
  → ... 后续处理 ...
  → updateChatGenerateStatus({ status: done })  // 更新 MongoDB 状态(在 Redis 删除之后!)
Stop API 在 Redis 标志删除后立即读取 MongoDB,但此时 chatGenerateStatus 还没更新,所以返回 completed: false。

工具执行阶段不检查停止信号

Agent Loop 的停止检查点:

LLM 流式输出时(stream.ts:44,128,202):每个 chunk 都检查 isAborted?.(),能快速响应
工具执行返回后(base.ts:651,679,685):只在工具完成后检查
循环迭代之间(base.ts:685):每轮 LLM 调用后检查
问题: 当 Agent 调用工具(如 HTTP 请求、沙盒执行、子应用、知识库搜索)时,工具执行期间不检查停止信号。如果工具执行时间 > 5 秒:
Stop API 超时返回 completed: false
工作流继续运行直到工具完成
用户看到"停止失败"

代码证据(base.ts:532):

const { response, ... } = await onRunTool({  // 这里 await 工具完成,期间不检查 stop
  call: tool,
  messages: cloneRequestMessages,
  assistantMessage: llmAssistantMessage
});

Stop API 超时返回 completed: false
工作流继续运行直到工具完成
用户看到"停止失败"
代码证据(base.ts:532):

const { response, ... } = await onRunTool({ // 这里 await 工具完成,期间不检查 stop
call: tool,
messages: cloneRequestMessages,
assistantMessage: llmAssistantMessage
});

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions