dsh-toolcall-id-repair:修復 OpenAI 兼容上游空 tool_call id 導致的會話鎖死

前言

在 DeepSeek Harness(dsh)0.2.x 裏走 OpenAI 兼容網關做流式工具調用時,若上游不把 tool_calls[].id 填全,會話往往會在一輪對話中間直接「磚掉」:助手消息已落盤,卻無法再追加匹配的 tool/result,整段 session 無法繼續讀寫。上游適配層與 dsh 持久化層對空 id 的容忍度不一致,單靠換模型或重試並不能從根上避免。下面介紹社區插件 dsh-toolcall-id-repair(維護者 zyf2492313716-cloud):在 llm/stream 瀑布流側補全空 id,並對已損壞的 session 日誌提供批量修復與校驗工具。

這是什麼

dsh-toolcall-id-repair 是標準 dsh 插件包(npm 名 @dsh-local/dsh-toolcall-id-repair,當前版本 0.1.1,MIT 許可證)。定位可以概括爲兩件事:

  1. 預防:Cordis 守衛在流式階段把空 tool call id 填成非空字符串,避免髒數據進入持久化 session。
  2. 修復:對已經寫入空 id 的 session 目錄,用 Python 腳本在三個必須一致的 id 點位同步改寫;用 Node 腳本調用 harness 自帶校驗器確認結果。

插件在 SkillHub 目錄 歸類爲工作流;源碼與說明見 GitHub 倉庫。README 記載該問題在 deepseek-harness 0.2.1-alpha.5 上仍可復現;package.json 聲明 dsh 引擎 >=0.1.2,並列出與 0.2.1-rc.5、0.2.2 若干 alpha/rc 的兼容標記。

問題從哪來

當 OpenAI 兼容後端在流式 tool call 裏省略 id 時,pi-ai 側會以 toolCall.id || "" 落庫;流校驗只看 typeof chunk.id === 'string',空字符串會通過,隨後 session format v4 的不變量拒絕該 turn,典型報錯爲:

format v4 tool/result at seq N requires toolCallId matching its tool source

助手消息已提交,匹配的 tool/result 無法再追加,turn 無法自恢復。README 指出:任何會省略 tool-call id 的 OpenAI 兼容後端都可能觸發;在某聚合網關上,短 prompt 失敗率約 0.4%(8k tokens 以內),長上下文(64k+)可升到約 36%,而同網關付費大上下文路由在約 2000 次調用中報告零失敗——說明與路由/模型檔位強相關。

核心功能

倉庫內三個工具各司其職(見 README 表格):

文件 作用
tools/guard-plugin.js Cordis 插件,在 llm/stream 瀑布流補全空 id;裝一次即可防磚
tools/repair-session.py 改寫已磚 session 中三處 id;支持 dry-run、--apply、--all
tools/verify-session.mjs 調用 harness 自帶 sessionFormatCatalog.createRestore 校驗,並檢查 append 安全性

爲什麼必須改三處 id

空 id 會同時出現在:

  1. assistant/message → message.content[].id(讀回時報 tool call id requires a nonempty string)
  2. 同條消息內嵌的 data.stream[].chunk replay(下次 append 時報 content disagrees with its embedded stream)
  3. tool/call → data.callId(導致無法追加對應 tool/result)

README 強調:只修 (1) 和 (3) 很危險——日誌看似可讀,卻在下一次 append 再炸;野外曾有隻修兩處、波及五個 session 的失敗案例。修復腳本以 message.content[].id 爲權威來源,按需重寫內嵌 stream;tool/call 通過 (turn, step, name, arguments) 四元組配對,並行同名調用仍可區分。合成 id 形如確定性 call_ + 24 位 hex,重複執行應爲 no-op。

守衛插件在權限表上聲明:無文件讀寫、無網絡——純內存流歸一化(截斷前的 README 權限節)。

安裝與啓用

通過 DSH Store / Cordis 包(推薦)

倉庫按標準 dsh 插件打包。README 給出兩種方式:

  • 在 DSH Store 搜索 dsh-toolcall-id-repair 直接安裝;
  • 或手動把 bundle patch 寫入 ~/.dsh/cordis.patch.yml:
- insert:
    - id: toolcall-id-repair
      name: '@dsh-local/dsh-toolcall-id-repair'

package.json 要求 Node ^22.19 || >=24。安裝後需按你使用的 profile 重啓 dsh 進程,使 Cordis patch 生效。

僅掛載 guard-plugin.js(手工路徑)

若你只想用 README 裏的獨立 guard-plugin.js,可複製到固定路徑,並在 Desktop profile 的 patch 文件 $DSH_HOME/profiles/desktop/cordis.patch.yml 的 insert 列表追加:

    - id: toolcall-id-guard
      name: '/absolute/path/to/guard-plugin.js'

README 特別提醒:0.2.x 下 $DSH_HOME/config.yaml 無人讀取,寫在那裏不會加載插件。重啓後可用審計日誌確認:

cat "$DSH_HOME/toolcall-id-guard.log"

出現 mounted pid=<n> 表示已掛載;每次補 id 會追加 filled 行,便於統計上游異常頻率。

典型用法

1. 修復單個已磚 session

先 dry-run 看計劃,再落盤;--apply 前會在 $DSH_HOME/session-repair-backups/ 備份。依賴 Python 3 與 zstandard。

# Dry run first: prints the plan and writes nothing.
python3 tools/repair-session.py "$DSH_HOME/sessions/<escaped-cwd>/<session-id>"

# Then apply. A backup lands in $DSH_HOME/session-repair-backups/ first.
python3 tools/repair-session.py --apply "$DSH_HOME/sessions/<escaped-cwd>/<session-id>"

# Or sweep everything:
python3 tools/repair-session.py --apply --all

2. 校驗修復結果

node tools/verify-session.mjs "$DSH_HOME/sessions/<...>/<session-id>"
node tools/verify-session.mjs --all

不要用 zlib.zstdDecompressSync 自寫掃描器:dsh 日誌是拼接的多幀 zstd,verify-session.mjs 通過 zstdcat 解碼,避免「整段 session 顯示 0 events 卻 ALL CLEAN」的誤判。

3. 安裝守衛前確認上游是否缺 id

對本地 OpenAI 兼容端點發流式 tool call,檢查原始 JSON 裏是否出現 "id":""(README 示例用 127.0.0.1:3425 與佔位 token,需替換爲你的 model 與鑑權):

curl -sN http://127.0.0.1:3425/v1/chat/completions \
  -H 'Content-Type: application/json' -H 'Authorization: Bearer magpie' \
  -d '{"model":"<your-model>","stream":true,
       "messages":[{"role":"user","content":"Call the bash tool with: echo hi"}],
       "tools":[{"type":"function","function":{"name":"bash",
         "parameters":{"type":"object","properties":{"command":{"type":"string"}},
         "required":["command"]}}}],"tool_choice":"auto"}' \
  | grep -o '"tool_calls":\[[^]]*\]'

現象具有間歇性;README 建議在長上下文下復現。磚掉的 transcript 形態也固定:該 turn 以 callId 爲 "" 的 tool/call 結束,無 tool/result,且該條 assistant 消息內所有 tool call block 共用空 id。

適用場景與注意

適合:使用 OpenAI 兼容聚合網關 / 免費檔模型、且常遇到 format v4 tool/result 報錯或 session 無法繼續 的 dsh 用戶;需要在不影響其它行字節的前提下搶救歷史 session 時,使用 repair + verify 流程。

注意:

  1. 插件隨當前 dsh 進程權限 運行;從 DSH Store 或 GitHub 安裝前,應自行查看源碼與 MIT 許可證,確認符合你的安全策略。
  2. 根治仍在 harness 本體:README 建議在 packages/llm/llm/src/assistant-stream.ts 收緊對空 id 的處理;在官方修復合併前,本插件是文檔推薦的 llm/stream 攔截點方案。
  3. SkillHub 等爲社區目錄,與 DeepSeek / 幻方無官方從屬關係;星標等元數據以目錄頁爲準(當前線索爲 0 stars)。

結尾

dsh-toolcall-id-repair 把「空 tool call id 導致 session 磚死」拆成可操作的預防(流守衛)與事後修復(三處 id 同步改寫 + harness 原生校驗),並寫明常見誤配置與校驗陷阱。需要安裝說明與完整命令時,以倉庫 README 爲準。

  • 目錄頁:https://www.skillhub.cn/plugins/zyf2492313716-cloud/dsh-toolcall-id-repair
  • GitHub:https://github.com/zyf2492313716-cloud/dsh-toolcall-id-repair
羽毛球分组比赛记分
小程序二维码

欢迎使用《羽毛球分组比赛记分》微信小程序

小夜