Repository navigation
fix(proxy): regroup interleaved items out of parallel tool-call batches (native Responses) - #8825
Conversation
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
left a comment
There was a problem hiding this comment.
感谢提交!三组直发对照(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>
|
目前实际使用中仍受到这个问题影响:Codex 通过 CC Switch 使用 Kimi For Coding 的原生 Responses 接口时,会反复遇到工具调用回执校验失败,导致请求中断,影响正常使用,尤其是涉及多个 脱敏后的错误摘要: 看到作者已经补交 Rust 格式修复,最新 CI 的 macOS / Windows / Linux 后端检查(格式、Clippy、测试)均已通过。想请问目前是否还有阻塞合并的评审事项?是否有预计的合并时间,以及计划在哪个正式版本中发布? 这个问题目前对日常使用造成了持续阻塞,希望能尽快合入。如果暂时还不能合并,也希望能说明剩余事项,方便用户判断后续安排。 以上仅保留排查所需的接口类型、状态码及工具名称,未附模型路由标识、具体调用 ID、会话内容、图片、凭据或完整请求日志。 |
funkpopo
left a comment
There was a problem hiding this comment.
感谢补充 cargo fmt 修复(3f35d02a)!
- 改动精准收敛在原生 Responses 第三方路由路径:仅在并行
function_call批次未收口时暂存插入的非工具消息(如<image_resize_notice>)并在批次完成后立即刷出,已连续的输入保持原样,解决 #7504(Kimi 等严格网关返回 HTTP 400)的根因清晰。 - 此前维护者已验证完整测试与 clippy,当前格式问题已修复、与最新
main(bd3d38801)合并树无冲突且全平台 CI 绿灯。
LGTM,可以直接合并。
allenxu09
left a comment
There was a problem hiding this comment.
感谢提交,针对并行 view_image 输出间穿插 <image_resize_notice> 的根因定位和正常批次下的思路都很清晰。不过状态机在边界条件上有一个会破坏多轮历史顺序的问题,建议修复后再合入:
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」场景,验证后续轮次顺序不被破坏。
- 遇到轮次边界(至少在
- 触发路径:
funkpopo
left a comment
There was a problem hiding this comment.
感谢提交与排查,#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")]):normalize_tool_output_grouping会先把assistant("first")/assistant("second")挪到output(b)之后;- 随后第 2075 行的
repair_tool_history看到call/output已收口,原本“将调用组内有文字的助手消息前置到调用之前(先说话再调工具)”的逻辑(transform_codex_responses_kimi_history.rs:55-60)对所有已完成批次彻底失效。 - 调用前说的话落到了工具结果之后(还会形成连续多条
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 - 问题:
- 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)末尾。 - OpenAI Responses
EasyInputMessage允许省略"type": "message"(例如{"role": "user", "content": "..."},仓库内codex_chat_history.rs:1000和kimi_history.rs:82-85均已兼容),第 55 行强依赖"message"会导致此类user消息绕过轮次重置。
- Codex Agent 在单条
建议改法
- 收窄延后对象:只延后
role为"developer"/"system"的消息(即 Codex 插入的<image_resize_notice>类提示,type为"message"或省略,与#8445Chat 桥的hoisted_system_message保持一致),放行reasoning、assistant消息、web_search_call等模型输出项不重排。 - 调整调用顺序:在
forwarder.rs中将normalize_tool_output_grouping放到transform_codex_responses_kimi_history::repair_tool_history之后。 - 预扫同轮实际存在回执的
call_id:按user轮次(兼容type为None或Some("message"))预扫每个*_output的非空call_id;只有在同一轮次后续确实存在对应*_output的*_call才加入pending。这样孤儿/部分缺失回执的调用不会卡死pending,部分完成的并行批次也能在最后一个实际回执后立刻刷出notice。 - 顺手对齐的小点(Nit):
call_id_of过滤空字符串(.filter(|id| !id.is_empty())),并补上tool_search_call/tool_search_output(与codex_chat_history.rs:535-547一致)。 - 补充单测:覆盖 ① 调用组内含
assistant旁白与repair_tool_history联动、② 并行调用间含reasoning保持原位、③ 同user轮次内部分缺失回执批次 + 后续多步调用、④ 省略type的user消息边界。
修完这些边界后逻辑就会非常稳固,改好后我会第一时间复审。
funkpopo
left a comment
There was a problem hiding this comment.
感谢 @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 放行跑绿后即可合并。
|
感谢 @Fansway 定位并修复这个问题!三组直发 Kimi 的对照实验,把「提示夹在工具结果中间才会 400」这个机制钉得很清楚,之后几轮修改也一直在跟进。也感谢 @allenxu09 按审查意见重写:现在只挪 developer/system 提示,和已有的 Kimi/DeepSeek 历史修复共用同一套按轮切分的判断,还顺手修好了那边孤儿调用会被跨轮搬移的老问题。 已合并到 main,会随下一个版本发布。@henryZhouLikeStudy 之前反馈的 另有一个可选的小跟进,不阻塞: |
fix(proxy): regroup interleaved items out of parallel tool-call batches (native Responses)
问题(#7504)
Codex Desktop 会在并行
view_image批次的各输出之间插入 developer 提示(<image_resize_notice>)。严格的原生 Responses 网关(Kimiapi.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 转换不受影响验证
Refs #7504, #7509, #6697, openai/codex#44604
🤖 Generated with Claude Code