Repository navigation
残缺或孤立的 tool_calls 记录导致 DeepSeek 接口返回 400 #4843
Replies: 3 comments
|
这个定位方向成立,尤其是 建议修复时把验收边界再收紧一些:
我把 rc.7 → rc.2 → alpha.1 的生产、Turn closure、compaction、wire containment 四层边界和恢复流程合并到了现有 runbook(revision 3): |
|
If you need a fix that works without patching the harness — on a build that still has this bug, or for a session that's already been poisoned — I wrote a plugin-level safety net:
dsh plugin --profile web add dsh-messages-sanitizerSource: https://github.com/Leeminjing/dsh-messages-sanitizer · on npm: It's complementary to, not a replacement for, the harness-side fix: it works on the 如果你需要的是一个不改 harness 源码就能落地的方案——比如跑的还是带这个 bug 的版本,或会话已经被污染了——我写了一个插件层安全网:
dsh plugin --profile web add dsh-messages-sanitizer源码:https://github.com/Leeminjing/dsh-messages-sanitizer · npm: 它与 harness 侧的修复互补、而非替代:它在运行时作用于 |
|
遇到同症状但不同根因的案例,补充一份数据。 症状一个使用较久的会话(约 47 万事件、上下文 66 万 tokens,模型 几个特征:
复现(触发条件)该轮用户请求是"能不能搜抖音/社媒菜谱",模型于是写 这个字符串作为工具结果写入会话历史后,该会话此后每次请求都被 API 拒绝——因为历史是 append-only 的,坏字符清除不掉。 实测证据我取该会话里的消息序列(user + assistant tool_call + tool result)用 API 凭据直连复现: 我把那一个字符替换为 根因(四个环节)
即:存储层有意保真(便于无损回读),发送层未做 well-formed 归一化,加上历史 append-only ⇒ 一次写入即永久锁死会话。 与 #4843 的关系和 #4843 是同一症状、不同层面,建议共用一套请求发出前的兜底:
两者都让"从历史重建出的请求"永远不合法,因此都需要在请求真正发出前做一次规范化。 两个小改动建议① 发送前深度归一化( export function wellFormedDeep<T>(value: T): T {
if (typeof value === 'string') return value.toWellFormed() as T
if (Array.isArray(value)) return value.map(item => wellFormedDeep(item)) as T
if (value !== null && typeof value === 'object') {
const out: Record<string, unknown> = {}
for (const [key, item] of Object.entries(value)) out[key] = wellFormedDeep(item)
return out as T
}
return value
}
// const payload = JSON.stringify(wellFormedDeep(body))
② 暴露 provider 原始报错( } catch {
const trimmed = rawResponse.trim()
if (trimmed.length > 0) message = `${message}: ${trimmed.slice(0, 500)}`
}这一处改动就能把"数小时排查"变成"一眼定位"。 附:诊断时的一个坑检测孤立代理项必须比对原始字符串:
另外:会话文件是多帧 zstd 拼接,单次 |
Uh oh!
There was an error while loading. Please reload this page.
会话历史中存在无配对 result 或 id/name/arguments 残缺的 tool_calls 时,DeepSeek chat-completions 接口返回 400。
复现、预期与验收
insufficient tool messages following tool_calls message或tool_call_id。tool_calls合法。@deepseek-ai/dsh-llm-deepseek适配器。定位与修复建议(附 patch)
根因是三处对「残缺 tool_calls 记录」缺少容错:
packages/core/agent-loop/src/agent.ts—— 拼历史时只剥离孤儿调用,未校验空 id、空 name、非法 JSON arguments 的畸形调用;packages/llm/llm-deepseek/src/translate.ts—— provider 未下发 id 时兜底成空字符串 id(空 id 可能与其他空 id result 误配对);packages/compaction/compaction/src/tool-pairing.ts—— 裁剪边界的配对平衡按「计数」而非 callId,识别不了「数量相等但 callId 错配」。修复改动拆成 4 个 commit(含单元测试),验证结果:
agent-loop340 测试、llm-deepseek151 测试、compaction195 测试全过;完整改动见我的 fork 分支:
https://github.com/bozhang1214/deepseek-harness/tree/fix/tool-call-pairing-harden(fork 后可见);也可直接使用附件中的 patch 文件。All reactions