Skip to content

fix(llmcore): 空 tool_use_id 的 tool_result 不再反转顺序/插到正文前 - #879

Closed
leonsong09 wants to merge 1 commit into
lsdefine:mainfrom
leonsong09:fix/tool-result-order-v2
Closed

leonsong09 wants to merge 1 commit into
lsdefine:mainfrom
leonsong09:fix/tool-result-order-v2

Conversation

@leonsong09

Copy link
Copy Markdown

本 PR 只改 llmcore.py 一个文件、一处逻辑(+13/-1),基于 upstream main 的 68d996a 生成。前一版 #878 因误用本地文件整份覆盖而关闭,此版已重做。

问题

NativeToolClient.chat() 里,tool_use_id 为空的 tool_result(文本兜底还原出来的 MockToolCall 默认 id='')走的是「前置插入裸文本」分支:

else: combined_content = [{"type": "text", "text": f'<tool_result>{content}</tool_result>'}] + combined_content

每次 prepend 会把上一次插入的挤到后面,导致:

  1. 多个空 id 结果顺序反转
  2. 与有 id 的 tool_result_blocks(append,顺序正确)语义不一致

实测

3 个结果 R1 / R2(无 id)+ R3(有 id):

修复前:

{"role": "user", "content": [
  {"type": "tool_result", "tool_use_id": "call_X", "content": "R3"},
  {"type": "text", "text": "<tool_result>R2</tool_result>"},
  {"type": "text", "text": "<tool_result>R1</tool_result>"},
  {"type": "text", "text": "USER_TEXT"}]}

修复后:

{"role": "user", "content": [
  {"type": "tool_result", "tool_use_id": "call_X", "content": "R3"},
  {"type": "text", "text": "USER_TEXT"},
  {"type": "text", "text": "<tool_result>R1</tool_result>"},
  {"type": "text", "text": "<tool_result>R2</tool_result>"}]}

改法

空 id 结果先按出现顺序收进 orphan_tails,循环结束后按序拼到 combined_content 尾部。
所有 id 齐全时的行为逐字节不变(那条路径完全没动)。

这个场景真的会发生吗

会。触发条件是模型不调用原生 tool 接口、把调用写成正文伪 XML(_parse_text_tool_calls 兜底路径),
而一次响应返回 2 个伪 XML 调用是实测见过的。

本地 24 个生产会话日志的统计:

  • 真实注入 [ERROR] Your tool call was emitted as plain XML text (...) 6 次(welfare ×5、buddy ×1),5 次当轮自愈
  • 45 处「伪 XML 收尾且无原生 tool_use」的响应,喂给真 _parse_text_tool_calls 全部可还原
    (43 处返 1 个调用,2 处返 2 个)→ 后两例就会踩到顺序反转

后果:模型拿到逆序的工具结果,多步脚本类工具(先查表结构再查数据)容易误判;
排查日志时看到 R2 在 R1 前,还会被误当成并发/竞态。

测试

新增 tests/test_tool_result_order.py:

  • test_orphan_results_keep_order —— 两个空 id 不再反转
  • test_orphan_after_user_text —— 裸文本结果排在用户正文之后
  • test_pending_ids_filled —— _pending_tool_ids 补空 content 的行为不受影响
tests/test_tool_result_order.py ....                                     [100%]
tests/test_stream_retry.py tests/test_auto_continue.py
tests/test_ask_text_params.py .......................                    [100%]
4 passed

未包含在本 PR 的两点

  1. 日志把 tool_result 记成裸文本 <tool_result>{...} 塞进 user 消息 —— 这是日志格式问题,
    改动会影响重放/审计链路,建议单独评估。
  2. textual_tool_residue() 对上述 45 处全部返回 None(即可还原),检测器工作正常,无需改动。

chat() 里 tool_use_id 为空的 tool_result(文本兜底还原出来的 MockToolCall
默认 id='')原本走「前置插入裸文本」分支:

    else: combined_content = [{"type": "text", "text": f'<tool_result>{content}</tool_result>'}] + combined_content

每次 prepend 都会把上一次插入的挤到后面,导致两个缺陷:

1. 多个空 id 结果顺序反转(实测 R1,R2 发出 R2,R1)
2. 与有 id 的 tool_result_blocks(append,顺序正确)语义不一致

改为按出现顺序收进 orphan_results,循环结束后 extend 到 combined_content 尾部。
所有 id 齐全时的行为逐字节不变。

验证(抽取该函数体 + 桩依赖,4 个用例):
- 两个空 id + 一个真 id -> R1 在 R2 前,且都在用户正文之后
- 全部有 id -> 顺序与正文位置不变
- 只有空 id -> 正文在结果之前
- _pending_tool_ids 补空 content 行为不变
@leonsong09

Copy link
Copy Markdown
Author

superseded by #880 — see #880 / #881.

Folded into #880 as commit 4 of 6, unchanged (+6/-1 in llmcore.py).

Closing to keep one open diff per change.

@leonsong09 leonsong09 closed this Nov 14, 2026
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