fix(tools): interleave parallel tool calls with results to prevent upstream internal error - #261
Open
justhil wants to merge 1 commit into
Open
fix(tools): interleave parallel tool calls with results to prevent upstream internal error#261justhil wants to merge 1 commit into
justhil wants to merge 1 commit into
Conversation
…stream internal error
justhil
force-pushed
the
fix/interleave-parallel-tool-calls
branch
from
September 12, 2026 13:07
d252adc to
181dda9
Compare
|
独立复现确认:这个 PR 也修好了 Anthropic 复现路径(与你测试的 OpenAI/Responses 不同):pi coding agent 走 验证方式:抓取 pi 的真实请求体(58KB)后逐段二分回放——前 3 条(单 tool_use 往返)成功,加入并行调用对后确定性失败,内容全部替换为 "ok" 仍失败;合成最小用例(一轮 2 个 tool_use + 2 个结果)同样失败,1 个 tool_use 则成功。 对你补丁的实测:本地起源码实例做对照,未打补丁时上述捕获负载稳定 529,打上本 PR 的改动后同一负载返回 200 正常输出。 另外提醒一个用户侧的连带现象:已经中毒的历史会话在修复前救不回来(并行调用记录留在上下文里),客户端只能 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
改了什么 / What changed
在
src/handlers/tool-emulation.js中增加了interleaveParallelToolMessages工具函数,并在DEVIN_CONNECT的nativeStructured链路以及直接消息处理流程中接入。assistant消息携带多个tool_calls,后跟多条role: 'tool'结果)会原样或直接批量压入底层消息流,导致上游 Devin 协议状态机无法解析多工具输出,直接报错503 UPSTREAM_INTERNAL(an internal error occurred)。nativeStructured(或无工具直接历史)的多轮消息流自动按tool_call_id进行拆分与成对交错重排(assistant(tool_1) -> tool_1_result -> assistant(tool_2) -> tool_2_result),同时保留首个 tool call 上的助手正文与 reasoning 内容,使上游状态机能够正常推进。为什么 / Why
修复客户端在使用支持并发工具调用的 Agent 框架调用
swe-2-max、swe-1-7等模型时,第 2 轮带回多个工具结果时必然抛出UPSTREAM_INTERNAL (an internal error occurred)的问题。验证过程说明:
tool_calls的assistant消息 + 2 条对应的tool结果消息),上游稳定复现503 UPSTREAM_INTERNAL。call -> result -> call -> result单回合序列后,上游即可稳定返回 200 并流畅输出流式数据。nativeStructured模式及直接连接链路上生效,不影响kimi_k2等具有自身批处理 dialect 格式的纯文本仿真路径(test/tool-emulation.test.js全部 65 个测试用例无回归且 100% pass)。测试 / Testing
测试环境:Node.js
输出结果:
/v1/chat/completions流式):Status: 200, Has UPSTREAM_INTERNAL: False,首 chunk 及推理正常输出。/v1/responses流式):Status: 200, Has response.failed: False,事件流正常生成。Checklist
Co-Authored-By、Generated with等)