Skip to content

fix(agent): keep tool loops alive with gated live SSE - #154

Open
GrothKeiran wants to merge 3 commits into
Rfym21:mainfrom
GrothKeiran:codex/fix-agent-attempt-runtime
Open

fix(agent): keep tool loops alive with gated live SSE#154
GrothKeiran wants to merge 3 commits into
Rfym21:mainfrom
GrothKeiran:codex/fix-agent-attempt-runtime

Conversation

@GrothKeiran

Copy link
Copy Markdown
Contributor

概要

这是 #150#151 的后续完整修复,解决 Codex、Claude Code、OpenClaw 等 Agent 通过 OpenAI /v1/chat/completions 使用 Qwen 网页上游时,模型仅输出计划、thinking-only 或不完整工具调用便被错误映射为正常 stop,导致任务尚未完成就终止的问题。

  • 为携带工具的 OpenAI 请求增加严格三态回合门禁:有效 tool_calls、已验证完成/阻塞、显式错误
  • 裸计划、进度描述、空回合、非法/截断工具调用不会再伪装成 finish_reason=stop
  • 在同一 HTTP 回合内隔离并纠正无效上游生成,复用主回答的 response_id 继续同一个 Qwen 对话
  • 恢复 thinking 的实时 SSE:通过 delta.reasoning_content 增量发送
  • 恢复正式回复的实时 SSE:合法 agent_final / agent_blocked 包装体正文通过 delta.content 增量发送
  • 所有正文增量保持 finish_reason=null;最终仅单独发送 tool_callsstop 或真实截断原因
  • 已开始输出但最终包装损坏时发送明确 SSE 错误,不重放正文,也不发送虚假的 stop
  • 改进长 Agent 历史外置和 WAF/captcha 显式错误,保留 system/developer 指令、原始任务及最近工具进度
  • 增加 SSE/HTTP 保活与干净 EOF、传输中断、多路上游回答的边界处理

为什么不会与流式输出冲突

SSE 内容增量和 Agent 终止状态被完全分离:reasoning_content / content 可以实时显示,但客户端只有在最终独立终止帧中收到 finish_reason=tool_calls 或经过门禁验证的 finish_reason=stop。因此恢复正文流式输出不会重新引入提前停止。

验证

  • npm test
  • 55 tests passed, 0 failed
  • 已通过构建镜像在实际 Agent 长任务场景中验证,不再出现任务未完成便正常停止,同时 thinking 和正式正文均保持 SSE 流式输出。

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant