Skip to content

[PR] fix(thinking): detect OpenRouter Zod signature-missing 400 so #5998 strip fires - #6991

Open
ranxianglei wants to merge 2 commits into
masterfrom
2026-11-17_fix-openrouter-thinking-signature
Open

ranxianglei wants to merge 2 commits into
masterfrom
2026-11-17_fix-openrouter-thinking-signature

Conversation

@ranxianglei

Copy link
Copy Markdown
Owner

问题

OpenRouter + Claude Code 场景下,bili 向 OpenRouter 发送的 thinking 内容块缺少 signature 字段。OpenRouter 的 /api/v1/messages Zod 校验器要求每个 thinking 块的 signature 必须是字符串,否则整个请求返回 HTTP 400。只要对话历史里包含这样的块,Claude Code 会话每一轮都会失败。

根因

src/server/thinking-posture.ts 的 detectThinkingSignatureRejection 只认 Anthropic 原版措辞(必须含 thinking + required/missing)。OpenRouter 的 Zod 措辞(expected string, received undefined + path 以 signature 结尾)既无 thinking 也无 required/missing,所以 #5998 的 strip-and-resend 探针对 OpenRouter 永不触发,400 每轮原样转发。

修法

扩展 detectThinkingSignatureRejection,新增一个窄门: path 尾 signature + expected string。复用已上线、已验证的 #5998 机制(首 400 → 探针剥掉无签名块 → 重试成功 → 记住 strict posture → 后续轮在出站前主动剥,客户端不再见 400)。

测试

  • 3 个检测器单测(1 正例 + 2 反例:非 signature 字段 / 有 signature+expected string 但无 path 尾)
  • 1 个 OpenRouter e2e personality(镜像 strict 的 e2e:首轮 400 自愈 + 后续轮主动剥)
  • 既有 20 个测试全绿(含既有 7 个检测器单测 + 5 个 e2e)

行为变更

无。仅放宽一个 400 措辞检测器,使其能识别 OpenRouter 的 Zod 措辞;复用既有 #5998 机制,不改变出站 wire 形状、不主动补空签名。

Closes #6988

…trip fires

OpenRouter's /api/v1/messages validator rejects a signature-less thinking
block with a Zod complaint (path ends in "signature", "expected string,
received undefined") that carries NEITHER "thinking" NOR
"required"/"missing" — so detectThinkingSignatureRejection missed it and
the #5998 strip-and-resend probe never fired, forwarding the 400 verbatim
every turn (#6988).

Extend the detector with a second, narrow gate: path tail "signature" +
"expected string". Reuse the proven #5998 mechanism (first 400 → probe
strips the unsigned block → retry accepted → posture learned → later turns
drop proactively; the client never sees the 400).

Regression tests: 3 detector unit cases (positive + 2 negatives) and an
OpenRouter e2e personality mirroring the strict one.
@github-actions

github-actions Bot commented Nov 17, 2026 •

Copy link
Copy Markdown
Contributor

📦 Built Package Artifact

Branch: 2026-11-17_fix-openrouter-thinking-signature (226cb59)

Latest build — install this exact version

npm install -g billion-context@0.0.10532-pr.6991.d85e3b0

This exact version is immutable and reproducible. The rolling pr tag tracks the newest test build across all PRs.

Recent builds (commit → version)

commit version build
226cb59 billion-context@0.0.10532-pr.6991.d85e3b0 build
31b3db1 billion-context@0.0.10529-pr.6991.24883e9 build

Download artifact

  1. Download the artifact from the Actions run
  2. Extract the tarball and install:
tar xzf billion-context-pr6991.tgz
npm install -g package

Install instructions are refreshed on each push.

…R_ZOD_BODY alias

Review pass on the #6988 detector fix: the tamper/openrouter/default cases
landed indented one level deeper than their switch siblings, and
OPENROUTER_ZOD_BODY was a pure identity alias of OPENROUTER_ZOD (same value,
extra indirection). Cosmetic only — no behavior change; the 20-test file
re-runs green after the fix.
@ranxianglei

Copy link
Copy Markdown
Owner Author

🤖 Powered by ework · qwen3.8-27b

(ai: vllm-qwen/qwen3.8-27b)

查重: 不重复。 检索 open+closed 的 issue 与 PR:无其他条目处理「OpenRouter Zod signature-missing 400」;源 issue 为 #6988,本 PR 是唯一关闭它的 PR。相关但不同:#5497(DeepSeek /anthropic "must be passed back"——不同措辞、不同病理,已关闭)、#4679(round-2 unsigned thinking stream——不同机制)、#147(DeepSeek 保留 signature——不同)。镜像 open [PR] 清单中无针对本 thread 的并列 PR。

审核结论

① 原 issue(方向)——正确。

② 回归审查——无回归。 触碰路径的既有行为逐项过:

③ 代码——最小且正确。 文档注释已更新为描述两种措辞;正则 /signature\\?"\]/i 同时匹配转义(真实信封 \"signature\"])与未转义形态。e2e rig 对 OpenRouter 建模准确(仅当 body 含无签名块才 400,探针重试因此能成功)。

④ 测试有效性——真测试,有牙。

  • 3 个新 detector 单测:1 正例(真实信封)+ 2 反例(非 signature 字段 / 有 signature+expected string 但无 path 尾)——过宽过窄都抓得住。
  • 1 个新 e2e 镜像 strict:首轮 400 自愈(2 次上游命中,第二次干净,客户端见 200)+ 后续轮出站前主动剥(1 次命中,干净)。
  • 本地定向验证(PR head):20/20 全绿(16 既有 + 4 新增),tsc --noEmit clean;全量裁决在 CI。
  • 小勘误:PR 正文「既有 20 个测试全绿(含既有 7 个检测器单测 + 5 个 e2e)」——实际 20 = 16 既有 + 4 新增(既有 detector 单测是 5 个、e2e 是 5 个)。总数对,拆分口径略有出入,不阻塞。

⑤ diff 整洁度——干净。 2 文件、+91/−15,每行都与本 PR 目的相关;无跑题文件、无空行/重排洪水。

⑤b 代码整洁——发现 2 处小问题,已直接在 PR 分支修掉(commit 226cb59dbe,独立标注):(1) tamper/openrouter/default 三个 case 比同 switch 的兄弟 case 多缩进一级;(2) OPENROUTER_ZOD_BODY 是 OPENROUTER_ZOD 的纯恒等别名——删掉,直接用 OPENROUTER_ZOD。修后 20 测试文件重跑全绿。

标签——PR 已有 bug + p1 + scope/wiring + claude-code;我按本 PR 自身 diff 独立复核:全部成立(p1 = 会话完全不可用,每轮 400;scope/wiring = relay egress seam 属外部 API 边界)。不改动。

CI——运行中:旧 head 的 test 车道在最后一次镜像缓存读取时(01:25Z)为 in_progress/queued;我推的 226cb59dbe 已触发新 head 重跑。mergeable: true,blocked on checks。

八问

  1. 是什么问题:OpenRouter + Claude Code 场景下,出站 Anthropic wire 携带无签名 thinking 块,OpenRouter 的 Zod 校验器对整个请求返回 400,会话每轮失败。
  2. 什么原因导致的:detectThinkingSignatureRejection 只认 Anthropic 原版措辞(必须含 thinking + required/missing);OpenRouter 的 Zod 措辞(path 尾 signature + expected string, received undefined)两者都不含 → Violates Anthropic's constraint that "preserved thinking must be replayed verbatim." | 违反了 Anthropic「preserved thinking 必须原样重放」的约束 #5998 的 strip-and-resend 探针永不触发,400 每轮原样转发。
  3. 影响范围与严重程度:OpenRouter + Claude Code(Anthropic wire)且历史含无签名 thinking 块的会话——每轮 400,完全不可用 → p1。其他上游不受影响:detector 对既有全部措辞的判决不变。
  4. 怎么修复的:detector 增加第二道窄门(path 尾 signature + expected string);其余完整复用已验证的 Violates Anthropic's constraint that "preserved thinking must be replayed verbatim." | 违反了 Anthropic「preserved thinking 必须原样重放」的约束 #5998 机制——首 400 → 探针剥无签名块 → 重试被接受 → 会话记住 strict posture → 后续轮出站前主动剥;探针失败则原 400 原样转发、不学习任何东西。
  5. 是否有回归问题:无。逐项审过:Anthropic 措辞真值表不变;Anthropic-wire upstream without thinking signatures (DeepSeek /anthropic): compress re-request drops reasoning from the tool_use replay → 400 "content[].thinking must be passed back" #5495/[acp-loop] In-turn compress re-request edits the latest assistant message → Anthropic 400 "thinking blocks … cannot be modified" (session stays broken) #1960 排除措辞仍不触发探针(既有 e2e 钉住);strip/posture/探针调用点未变;KDD#10 签名信道未触碰;无阈值/时序/输出格式漂移;无 fail-fast 变吞错。
  6. 是否破坏缓存:否。缓存策略、重试策略、增量游标均未变;探针仍为每请求一次。
  7. 是否改了设计方向:否。复用 Violates Anthropic's constraint that "preserved thinking must be replayed verbatim." | 违反了 Anthropic「preserved thinking 必须原样重放」的约束 #5998 的 learn-on-failure 架构,只放宽 detector 的措辞识别面。
  8. 是否引入了新的配置:否。无新 env/config/默认值;posture 仍为 session-scoped metadata,无配置面。

判定:可合并(等新 head CI 转绿)。

This branch has not been deployed

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

Labels

Projects

None yet

1 participant