Skip to content

fix(proxy): regroup interleaved items out of parallel tool-call batches (native Responses) - #8825

Merged
YUZHEthefool merged 5 commits into
farion1231:mainfrom
Fansway:fix/regroup-parallel-tool-outputs
Nov 23, 2026
Merged

YUZHEthefool merged 5 commits into
farion1231:mainfrom
Fansway:fix/regroup-parallel-tool-outputs

Conversation

@Fansway

@Fansway Fansway commented Oct 24, 2026

Copy link
Copy Markdown
Contributor

fix(proxy): regroup interleaved items out of parallel tool-call batches (native Responses)

问题(#7504)

Codex Desktop 会在并行 view_image 批次的各输出之间插入 developer 提示(<image_resize_notice>)。严格的原生 Responses 网关(Kimi api.kimi.com/coding/v1/responses)要求 tool_calls 批次后紧跟全部回执,遇到插入消息就丢弃后续(图片)回执,产生孤儿 tool_call_id,该线程此后每轮重放都被 400。最小复现(绕过 cc-switch 直发上游,200/400/200 三组对照)见 #7504 评论。

改动

  • 新增 transform_codex_responses_reorder.rs:批次内 call_id 未收齐时暂存非工具条目,收齐后放行;已连续的输入逐字节不变(幂等)
  • forwarder.rs:在原生 Responses 第三方分支(prepare_native_third_party_request 之后)调用;官方 passthrough 与 Chat/Anthropic 转换不受影响
  • 与 v4.0.16 给 Chat 上游(DeepSeek「并行工具结果被插入提示拆开」)修的是同族问题,本次补上 Responses 直通路径

验证

  • 单测 3 个:插入提示被后置放行 / 连续输入不变 / 顺序对间提示保持原位
  • 实测(4.0.19 + 本补丁,真实 Kimi 上游、真实凭据):原必现 400 的完整载荷(73KB 截图 × 3 + 插入提示 + 并行调用)→ HTTP 200,模型正常基于图片内容回复

Refs #7504, #7509, #6697, openai/codex#44604

🤖 Generated with Claude Code

Codex Desktop interleaves developer notices (e.g. <image_resize_notice>)
between the outputs of a parallel view_image batch. Strict native
Responses gateways such as Kimi's api.kimi.com/coding/v1/responses drop
tool outputs that are not contiguous with their tool_calls batch, which
leaves orphan tool_call_ids and permanently 400s every later replay of
the thread (farion1231#7504).

While a batch of function_call-style items still has unanswered
call_ids, hold back non-tool items and flush them right after the batch
completes. Semantics unchanged, already-contiguous input untouched.
Same family as the v4.0.16 fix for parallel tool results split by
inserted notices on the Chat bridge.

Refs farion1231#7504, farion1231#7509, farion1231#6697

Co-Authored-By: Claude Code <noreply@anthropic.com>

@farion1231 farion1231 left a comment

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

感谢提交!三组直发对照(A/B/C)把问题钉得很清楚,本地把这个 PR 合进当前 main 后,后端全量测试和 clippy 都能通过。

[P2] cargo fmt --check 未通过,CI 后端三平台失败

CI 的 Backend Checks(macOS / Ubuntu / Windows)都停在 "Check Rust formatting" 这一步,报的是新文件测试里的一处格式:transform_codex_responses_reorder.rs#L97-L104(let types: Vec<String> = ... 那一长行和下面的 assert_eq!)。格式检查失败后,后面的 clippy 和测试步骤都没有执行。

修法:在 src-tauri 下跑一次 cargo fmt 再推上来即可,只会改这段测试代码的换行。

修好后我会尽快复审。

Only rewraps the long `types` mapping chain and `assert_eq!` in the
test module flagged by CI; no code outside #[cfg(test)] touched.

Co-Authored-By: Claude Code <noreply@anthropic.com>

Copy link
Copy Markdown

目前实际使用中仍受到这个问题影响:Codex 通过 CC Switch 使用 Kimi For Coding 的原生 Responses 接口时,会反复遇到工具调用回执校验失败,导致请求中断,影响正常使用,尤其是涉及多个 view_image 调用的场景。

脱敏后的错误摘要:

Endpoint: /responses
Upstream status: HTTP 400
Invalid request: an assistant message with 'tool_calls' must be followed
by tool messages responding to each 'tool_call_id'.
Missing responses for view_image tool calls (具体调用 ID 已省略).

看到作者已经补交 Rust 格式修复,最新 CI 的 macOS / Windows / Linux 后端检查(格式、Clippy、测试)均已通过。想请问目前是否还有阻塞合并的评审事项?是否有预计的合并时间,以及计划在哪个正式版本中发布?

这个问题目前对日常使用造成了持续阻塞,希望能尽快合入。如果暂时还不能合并,也希望能说明剩余事项,方便用户判断后续安排。

以上仅保留排查所需的接口类型、状态码及工具名称,未附模型路由标识、具体调用 ID、会话内容、图片、凭据或完整请求日志。

funkpopo
funkpopo previously approved these changes Nov 20, 2026

@funkpopo funkpopo left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

感谢补充 cargo fmt 修复(3f35d02a)!

  • 改动精准收敛在原生 Responses 第三方路由路径:仅在并行 function_call 批次未收口时暂存插入的非工具消息(如 <image_resize_notice>)并在批次完成后立即刷出,已连续的输入保持原样,解决 #7504(Kimi 等严格网关返回 HTTP 400)的根因清晰。
  • 此前维护者已验证完整测试与 clippy,当前格式问题已修复、与最新 main(bd3d38801)合并树无冲突且全平台 CI 绿灯。

LGTM,可以直接合并。

@allenxu09 allenxu09 left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

感谢提交,针对并行 view_image 输出间穿插 <image_resize_notice> 的根因定位和正常批次下的思路都很清晰。不过状态机在边界条件上有一个会破坏多轮历史顺序的问题,建议修复后再合入:

  1. src-tauri/src/proxy/providers/transform_codex_responses_reorder.rs:54-68:未闭合的 *_call 会污染后续所有轮次并导致对话消息集体后置乱序
    • 触发路径:forwarder.rs:1982 对所有非官方原生 Responses 上游无条件调用 normalize_tool_output_grouping。该函数通过 pending 集合等待匹配的 *_output,但在遇到新轮次边界(例如 type == "message" 且 role == "user")时从不刷出 deferred 或重置 pending。
    • 具体后果:一旦历史中存在一条没有对应 *_output 的 function_call / custom_tool_call / local_shell_call(例如中断的工具调用或残缺历史,第 64 行注释也预期了“Batch never completed”的情况),pending 就会一直保持非空。之后所有轮次的 user / assistant 消息和 reasoning 都会不断被压入 deferred,而后续轮次的新 *_call / *_output 却继续写入 result,最终第 66–67 行把后续各轮的全部对话消息一股脑追加到 input 最末尾。例如 [call(c0), user("stop"), call(c1), output(c1), user("next")] 会被改写成 [call(c0), call(c1), output(c1), user("stop"), user("next")],打乱上下文顺序并破坏后续轮次的 prompt 语义。此外,第 66 行在没有工具条目越过 deferred 时也会无条件把 changed 设为 true。
    • 修复建议:
      • 遇到轮次边界(至少在 type == "message" 且 role == "user" 时)先刷出 deferred 到 result 并清空 pending,再压入该消息条目,阻止残缺批次跨轮次扩散。
      • 仅在工具调用或工具输出确实越过非空 deferred 写入 result 时才置 changed = true。
      • 补充单测覆盖「第 1 轮存在未闭合调用 → 后续 user 消息 → 第 2 轮正常 call/output」场景,验证后续轮次顺序不被破坏。

allenxu09
allenxu09 previously approved these changes Nov 23, 2026
@allenxu09
allenxu09 requested a review from funkpopo November 23, 2026 05:44
allenxu09
allenxu09 previously approved these changes Nov 23, 2026

@funkpopo funkpopo left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

感谢提交与排查,#7504 的三组直发对照把根因定位得很清楚,合并 main 并在 ac192e2a 补上 user 轮次边界重置的思路也很对。

不过结合最新 main 上的原生 Responses 处理链路复核了一遍(a4a1360f),在合并前还有 3 个会破坏真实会话顺序/兼容性的边界问题需要一起收口:

1. 与 transform_codex_responses_kimi_history::repair_tool_history 管线顺序冲突,导致调用组内 assistant 旁白被倒置

  • 位置:src-tauri/src/proxy/forwarder.rs:2057-2083、src-tauri/src/proxy/providers/transform_codex_responses_reorder.rs:60-66
  • 问题:本 PR 合并最新 main 后,normalize_tool_output_grouping 排在 repair_tool_history 之前,且 _ 通配分支会把 role == "assistant" 的消息一并压入 deferred。对于 kimi_history 专门处理的跨上游历史(例如 [user, reasoning, call(a), assistant("first"), call(b), assistant("second"), output(a), output(b), assistant("done")]):
    1. normalize_tool_output_grouping 会先把 assistant("first") / assistant("second") 挪到 output(b) 之后;
    2. 随后第 2075 行的 repair_tool_history 看到 call/output 已收口,原本“将调用组内有文字的助手消息前置到调用之前(先说话再调工具)”的逻辑(transform_codex_responses_kimi_history.rs:55-60)对所有已完成批次彻底失效。
    3. 调用前说的话落到了工具结果之后(还会形成连续多条 assistant 消息;在连续工具循环 [call(a), assistant("checking a"), output(a), call(b), output(b)] 中还会错挂到下一条 call(b) 之前)。

2. 通配分支 _ 会拆散并行调用间的 reasoning 项,影响所有 !codex_official_auth_passthrough 原生 Responses 上游

  • 位置:src-tauri/src/proxy/providers/transform_codex_responses_reorder.rs:36-67
  • 问题:is_codex_official_provider 对配置了 OPENAI_API_KEY 的 OpenAI API 卡、Azure、OpenRouter / 第三方中转、xAI、火山方舟以及 Copilot Responses 均返回 false(见 codex.rs:346-354),因此它们都会走第 2047 行的 else 分支。
  • 当推理模型在单轮并行调用中输出交替的推理与调用项时:
    [user, reasoning(rs_1), function_call(c1), reasoning(rs_2), function_call(c2), function_call_output(c1), function_call_output(c2)]
    reasoning(rs_2) 在 pending == {"c1"} 时落入 _ 分支,被挪到 function_call_output(c2) 之后。OpenAI / Azure 等 Responses 网关会校验 reasoning ↔ function_call 配对顺序并直接 400(Item 'fc_...' of type 'function_call' was provided without its required 'reasoning' item: 'rs_...'),同时也会破坏其他上游的上下文顺序与缓存前缀。

3. 同一 user 轮次内的多步调用循环 / 部分缺回执批次仍会被 pending 污染(及省略 type 的 user 消息)

  • 位置:src-tauri/src/proxy/providers/transform_codex_responses_reorder.rs:32-68
  • 问题:
    1. Codex Agent 在单条 role: "user" 指令下经常连续执行多步工具调用(中间没有新的 user 消息)。如果某批次存在缺失回执的调用,例如:
      [user, call(c0), call(c1_missing), call(c2), output(c0), notice(0), output(c2), assistant("next"), reasoning(r2), call(c3), output(c3), assistant("done")]
      由于 "c1_missing" 永远不会从 pending 移除,notice(0) 不会在 output(c2) 后刷出,后续整轮的所有 assistant / reasoning 条目都会继续进入 deferred 并被集体甩到 output(c3) 末尾。
    2. OpenAI Responses EasyInputMessage 允许省略 "type": "message"(例如 {"role": "user", "content": "..."},仓库内 codex_chat_history.rs:1000 和 kimi_history.rs:82-85 均已兼容),第 55 行强依赖 "message" 会导致此类 user 消息绕过轮次重置。

建议改法

  1. 收窄延后对象:只延后 role 为 "developer" / "system" 的消息(即 Codex 插入的 <image_resize_notice> 类提示,type 为 "message" 或省略,与 #8445 Chat 桥的 hoisted_system_message 保持一致),放行 reasoning、assistant 消息、web_search_call 等模型输出项不重排。
  2. 调整调用顺序:在 forwarder.rs 中将 normalize_tool_output_grouping 放到 transform_codex_responses_kimi_history::repair_tool_history 之后。
  3. 预扫同轮实际存在回执的 call_id:按 user 轮次(兼容 type 为 None 或 Some("message"))预扫每个 *_output 的非空 call_id;只有在同一轮次后续确实存在对应 *_output 的 *_call 才加入 pending。这样孤儿/部分缺失回执的调用不会卡死 pending,部分完成的并行批次也能在最后一个实际回执后立刻刷出 notice。
  4. 顺手对齐的小点(Nit):call_id_of 过滤空字符串(.filter(|id| !id.is_empty())),并补上 tool_search_call / tool_search_output(与 codex_chat_history.rs:535-547 一致)。
  5. 补充单测:覆盖 ① 调用组内含 assistant 旁白与 repair_tool_history 联动、② 并行调用间含 reasoning 保持原位、③ 同 user 轮次内部分缺失回执批次 + 后续多步调用、④ 省略 type 的 user 消息边界。

修完这些边界后逻辑就会非常稳固,改好后我会第一时间复审。

@funkpopo funkpopo left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

感谢 @Fansway 的定位和 @allenxu09 在 2e5fda45 补齐边界修复!复核了最新改动,此前提出的 3 个阻塞项都已经干净收口:

  • 管线顺序与延后范围收敛:forwarder.rs 已将 normalize_tool_output_grouping 移到 transform_codex_responses_kimi_history::repair_tool_history 之后,且只延后 role 为 developer / system 的提示消息;reasoning、assistant 旁白及其他模型输出项保持原位,不再破坏 kimi_history 的助手旁白前置逻辑,也不会拆散 OpenAI / Azure 等原生 Responses 上游的 reasoning ↔ function_call 配对。
  • 按 user 轮次预扫有效回执:通过 split_inclusive_mut(is_user_message)(兼容省略 type 的 EasyInputMessage)与 tool_output_positions 预扫同轮后续实际存在的回执,仅对存在匹配回执的调用入 pending;单轮多步调用及部分缺回执批次不会再卡死 pending 或跨轮污染,同时 repair_tool_history 复用同一套轮次与预扫逻辑,顺带补齐了相同边界。
  • 覆盖与本地验证:已补齐 tool_search_call / tool_search_output 支持与 9 个边界单测(含与 repair_tool_history 联动、并行调用间保留 reasoning、部分缺回执后置刷出、省略 type 的 user 边界)。由于当前最新提交的 fork CI 仍待放行,已在本地基于当前 main 合并测试通过 cargo fmt --check、cargo clippy --all-targets -- -D warnings 及 transform_codex_responses_reorder / transform_codex_responses_kimi_history 全部单测。

LGTM,已批准(approve),待 CI 放行跑绿后即可合并。

@YUZHEthefool YUZHEthefool left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks!

@YUZHEthefool
YUZHEthefool merged commit 1fba4e1 into farion1231:main Nov 23, 2026
10 checks passed
@farion1231

Copy link
Copy Markdown
Owner

感谢 @Fansway 定位并修复这个问题!三组直发 Kimi 的对照实验,把「提示夹在工具结果中间才会 400」这个机制钉得很清楚,之后几轮修改也一直在跟进。也感谢 @allenxu09 按审查意见重写:现在只挪 developer/system 提示,和已有的 Kimi/DeepSeek 历史修复共用同一套按轮切分的判断,还顺手修好了那边孤儿调用会被跨轮搬移的老问题。

已合并到 main,会随下一个版本发布。@henryZhouLikeStudy 之前反馈的 view_image 400 应该会随之解决;升级后如果还遇到,欢迎新开 issue,附上错误信息。

另有一个可选的小跟进,不阻塞:normalize_tool_output_grouping 的循环结束后可以加一行 result.append(&mut deferred) 兜底。现在是靠「只把同一轮后面确实有结果的调用记为未收齐」来保证暂存的提示一定会放回去;以后改这段逻辑时,这一行能防止条目被悄悄丢掉。

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

Labels

backend Backend (Rust/Tauri) proxy Related to proxy/API forwarding

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants