Skip to content

[PR] fix: replay unsigned thinking when the upstream does not sign it (#5495) - #5496

Merged
ranxianglei merged 1 commit into
masterfrom
2026-11-02_unsigned-thinking-replay
Nov 2, 2026
Merged

ranxianglei merged 1 commit into
masterfrom
2026-11-02_unsigned-thinking-replay

Conversation

@ranxianglei

Copy link
Copy Markdown
Owner

Closes #5495

What was broken

On an Anthropic-wire upstream that never signs thinking blocks (DeepSeek's /anthropic compatibility endpoint), every acp-loop re-request (compress or acp_status) died with:

400 invalid_request_error: The content[].thinking in the thinking mode must be passed back to the API.

The signature gate in src/loop/core.ts keyed on protocol alone — anthropic|google ⇒ require signature on replayed thinking. DeepSeek's relay emits no signature_delta, so every round's reasoning was silently dropped from the tool_use replay, leaving the assistant message without its thinking sibling. The degraded strip-retry then compounded it: stripping the loop thinking and retrying guarantees a second 400 on a thinking-required upstream and swaps out the real error.

Fix

  1. Gate on evidence, not protocol (src/loop/core.ts): require a signature only when THIS attempt actually saw one — sawSignedThinkingThisAttempt, already tracked per-attempt for the [bug] anthropic 线盲重取(#413/#1455)把没签名的 thinking 块交给客户端:下一轮 400 "thinking.signature: Field required",Claude Code 随即永久剥掉全部历史 thinking #2501 refetch bar. Real Claude signs every round → unchanged. An upstream showing zero signing evidence is an unsigned relay → its thinking replays verbatim.
  2. Strip-retry exclusion (src/loop/core.ts): a "must be passed back" 400 now propagates instead of strip-retrying (stripping perpetuates the rejection). Neutral 400s still get the degraded retry — the exclusion is scoped, pinned by a contrast test.
  3. Learner wording (src/loop/core.ts + src/server/relay.ts): strictReasoningEcho learn-on-failure also matches DeepSeek's Anthropic-wire spelling content[].thinking.

Behavior change (old → new)

On anthropic/google-wire lanes, a round whose attempt shows zero signing evidence previously had all of its thinking blocks dropped from the compress/acp-status re-request. They are now replayed verbatim.

  • Real Claude signs every round → no change in practice. One corner case shifts: a truncated FIRST round of a fresh session (no signed history yet, signature_delta lost) now replays the block; if upstream rejects it, the existing degraded strip-retry recovers (previously: proactive drop).
  • A session that switches providers onto an unsigned Anthropic-wire relay now works immediately (previously: every compress round 400'd forever).
  • The #221 test pin "unsigned ⇒ always dropped" encoded the buggy generalization and is updated with justification; a new mixed-block test pins the RETAINED requirement when signing evidence exists in the attempt (Claude/Gemini fatality rule intact).

Tests

New tests/issue5495-unsigned-thinking-replay.test.ts (5 tests) — each verified to fail against the pre-fix code (negative-control runs for both the gate and the strip-retry exclusion):

  • unsigned thinking replays on the compress re-request (the issue repro);
  • same on the acp_status path (both proxy-tool append sites share the guard);
  • negative control: an attempt that saw a signature keeps dropping the unsigned sibling;
  • "must be passed back" 400 propagates with NO strip-retry + arms strictReasoningEcho on the new wording;
  • contrast: a neutral 400 still strip-retries.

Targeted suites green locally (209 tests across the loop/reasoning/replay/strict-echo area, incl. verify-sig-roundtrip, issue539-loop-reasoning, anthropic-truncation-unsigned-thinking); typecheck clean on the rebased head. Full-suite verdict = CI on this head. No config-surface changes, no kernel/ changes, no version bump.

An Anthropic-wire upstream that never signs thinking blocks (DeepSeek's
/anthropic endpoint) requires the unsigned block passed back on the
acp-loop re-request. The signature gate keyed on protocol alone
(anthropic|google => require signature), so every such round's reasoning
was dropped from the tool_use replay -> 400 invalid_request_error:
"The `content[].thinking` in the thinking mode must be passed back."

- Gate on per-attempt signing evidence (sawSignedThinkingThisAttempt):
  require a signature only when THIS attempt actually saw one. Real
  Claude signs every round, so its behavior is unchanged.
- Degraded strip-retry excludes "must be passed back" 400s: stripping
  perpetuates a thinking-required rejection; propagate the original
  error instead. Neutral 400s still strip-retry.
- strictReasoningEcho learn-on-failure (loop + relay) also matches
  DeepSeek's Anthropic-wire wording content[].thinking.

Behavior change (old->new): on anthropic/google-wire lanes, a round whose
attempt shows ZERO signing evidence previously had ALL its thinking
blocks dropped from the re-request; they are now replayed verbatim. The
#221 pin "unsigned => always dropped" was the buggy generalization and
is updated accordingly; a new mixed-block test pins the retained
requirement when signing evidence exists.
@github-actions

github-actions Bot commented Nov 1, 2026

Copy link
Copy Markdown
Contributor

📦 Built Package Artifact

Branch: 2026-11-02_unsigned-thinking-replay (e18bbb0)

Latest build — install this exact version

npm install -g billion-context@0.0.7809-pr.5496.31ef6eb

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
e18bbb0 billion-context@0.0.7809-pr.5496.31ef6eb build

Download artifact

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

Install instructions are refreshed on each push.

@ranxianglei ranxianglei changed the title fix: replay unsigned thinking when the upstream does not sign it (#5495) [PR] fix: replay unsigned thinking when the upstream does not sign it (#5495) Nov 1, 2026
@ranxianglei

Copy link
Copy Markdown
Owner Author

🤖 Powered by ework · qwen3.8-27b

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

查重:不重复。 这是 #5495(DeepSeek /anthropic 无签名 thinking → 压缩重请求 400)的修复 PR。独立检索 open+closed 未发现同机制的并行修复;它与 #539 是同一行 gate 的两个相反面(#539 修的是 OpenAI/DeepSeek chat/Responses 回显 reasoning_content 的镜像方向),本 PR 已在注释里把两者对齐,不是重复立项。

已审代码、回归面、测试有效性,并本地独立复跑 + 负控制。按八问作答:

1. 是什么问题
在「说 Anthropic 协议但不给 thinking 块签名」的上游(典型是 DeepSeek 的 /anthropic 兼容端点),每次 acp-loop 重请求(compress / acp_status 之后重建历史那次)都会把当轮 thinking 丢掉,上游收到一个只有 tool_use、没有 thinking 兄弟的 assistant 消息,直接 400 content[].thinking ... must be passed back;降级 strip-retry 再删一次 thinking,必然二次 400,还把真实错误换掉。

2. 什么原因导致的
根因在 src/loop/core.ts 的签名 gate 只按协议判定:protocol === anthropic|google ⇒ 重放 thinking 必须带签名。该假设对 api.anthropic.com / Gemini 成立,对不签名的 Anthropic-wire 中继不成立——于是把「合法但无签名」的 thinking 当成非法丢了。本质是「用协议代替证据」的过度泛化。

3. 影响范围与严重程度

  • 触发条件窄:仅「Anthropic-wire + 上游不签名」这一类中继(DeepSeek /anthropic 等)。真 Claude、Gemini、OpenAI/DeepSeek chat/Responses 均不受影响。
  • 命中即核心能力停摆:受影响配置下每一轮压缩都 400,压缩完全不可用 → 对该场景属 p1 级;对其他用户零影响。

4. 怎么修复的
把 gate 从「按协议」改成「按本次尝试的证据」:只有当本轮真的观测到签名(复用 #2501 已有的 per-attempt 标志 sawSignedThinkingThisAttempt)才要求签名,否则原样回放 thinking。配套两处:
(a) must be passed back 400 不再走 strip-retry(删了只会更糟),直接抛原错误;中性 400 仍走降级重试(对比测试钉住排除是「精准」而非「一刀切」)。
(b) strictReasoningEcho 学习器新增匹配 DeepSeek 的 Anthropic-wire 措辞 content[].thinking(src/loop/core.ts + src/server/relay.ts 两处对称)。
关键取舍:gate 只取 sawSignedThinkingThisAttempt、不含 historyHasSignedThinking——这是有意的:若纳入历史签名,「从签名的 Claude 切到不签名的 DeepSeek」的场景会因历史里有旧签名而继续丢 thinking,bug 就修不干净。

5. 是否有回归问题
逐项过既有行为:

  • 真 Claude / Gemini 正常轮(每轮都签名):sawSignedThinkingThisAttempt=true ⇒ gate 逐字节不变。无回归。
  • OpenAI/DeepSeek chat + Responses(非 anthropic/google 协议):前后都为 false ⇒ 不变。无回归。
  • 唯一语义漂移(PR 已披露,我认同其边界判断):真 Claude 上「被截断的当轮 thinking(没等到 signature_delta)」以前主动丢弃,现在改为原样回放;若上游拒绝,靠既有降级 strip-retry 兜底(多一次失败请求+重试,最终收敛到同样可用态)。窄角场景,可接受。
  • 高危模式逐项:fail-fast 未变吞错(反而更诚实——把原错误抛出而非换掉);输出格式未变;阈值/时序未动。KDD#10「thinking 永不改写」守住:回放路径只决定 include/exclude,thinking 文本字节逐字保留(part.text 从不改写),signature 仅在存在时附上。

6. 是否破坏缓存
否。签名路径(真 Claude/Gemini)请求体不变,前缀缓存不受影响。无签名中继此前每次都是 400、根本没有成功的前缀可比;现在带着 thinking 成功,且这段 thinking 本就在客户端历史里,回放反而让前缀更贴合客户端原始发送。不引入任何重试/游标/去重键失效。

7. 是否改了设计方向
否。无架构/数据流转向,只是把一个已有 gate 的判据从「协议」精化为「证据」,复用 #2501 已有标志,无新模块、无新依赖。

8. 是否引入了新的配置
否。无新 env/config/默认值(CI 的 config-surface-guard 已绿)。唯一运行时副作用:命中该 400 时会把会话级 strictReasoningEcho latch 置 true——既有机制、非新配置,且与该上游「必须回显 reasoning」的事实一致。

验证(我做的,非照抄 PR)

  • 本地定向跑 5 个相关套件共 43 用例全绿(含 verify-sig-roundtrip、anthropic-truncation-unsigned-thinking、loop-core)。
  • 负控制实测:把 core.ts/relay.ts 临时还原到修复前跑新套件,5 个里 4 个红(正是 gate 与 strip-retry 排除两条腿的牙),还原后全绿——证明测试真能抓这个 bug,不是恒真断言。
  • CI:已完成的 check 全绿(各 test shard / static-gates / config-surface-guard / test-import-graph / mysql-contract),其余 shard 在跑,暂无红。
  • diff 整洁度:单 commit、4 文件(core.ts +40/-、relay.ts +3/-、两个测试文件),与描述一致,无跑题、无 lockfile/生成物噪音。(注:初次看显示 387 文件是本机 master 基线过旧所致,fetch 真实 origin/master 后即为这 4 文件。)
  • scope 完整性:签名-drop gate 全仓仅此一处(core.ts:1332/1340),无并行漏网点。

判定:可合。 逻辑正确、回归面有界且已披露、测试双向钉死并有负控制实证、CI 无红。建议等 CI 全绿后由 owner 合并(我不合 PR)。

@ranxianglei

Copy link
Copy Markdown
Owner Author

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

Verdict: APPROVE

⚠️ 本 review 以评论形式投递:审核身份与 PR 作者同为 ranxianglei,GitHub 禁止对本人 PR 提交正式 APPROVE 状态。实质结论 = APPROVE(可合),独立审查见下。 — 独立审 base 0d0c0b95..head e18bbb02,4 文件(src/loop/core.ts、src/server/relay.ts + 两个测试),无 kernel/、无 version bump、无 config-surface 变更(config-surface-guard 绿)。

我独立核过什么(不是复读 issue 结论):

  • sawSignedThinkingThisAttempt 生命周期:core.ts:797 声明在 attempt 循环外、:841 每 attempt 重置、:876 仅在见到 signature_delta 时置真 → 重放点(循环后)读到的是"存活那次 attempt"的证据。真实 Claude/Gemini 每轮都签名 ⇒ 不变;零证据的 Anthropic-wire 中转 ⇒ 逐字回传。门从 protocol∈{anthropic,google} 收紧为 protocol ∧ 本轮有签名证据,正好是"按证据不按协议"。
  • 错误路径控制流(core.ts ~1648–1735):内层 strip-retry 守卫新增 isReasoningRequiredError ⇒ "must be passed back" 400 throw e 进外层 catch;外层仍跑 [bug] compress 保护区过滤在完整性调整之后挖洞,导致 assistant tool-call 消息缺 reasoning_content(DeepSeek 思考模式 400) #684 learner(新拼写 content[].thinking 触发 arm strictReasoningEcho)并向客户端 emit upstream error 400。两个方向都测到:中性 400 不命中任何 skip 条件 ⇒ 照旧走剥离重试。
  • 没有漏改第二个协议型签名门——该判定是单一定义点,compress 与 acp_status 两个 append 路径共用。
  • 偶尔模型在acp工具处停止执行 #221 pin 翻转为"无签名证据⇒逐字回传"并附理由;偶尔模型在acp工具处停止执行 #221 的致命规则(有签名证据时未签名块仍丢)由两份测试文件的混合块负控各自钉住。
  • 测试驱动的是真 runCompressLoop + 真 anthropic adapter + mock fetch(非 mock 套 mock);每条断言 pre/post 都会翻转,故都有牙。

结构化八问:

  1. 是什么问题:Anthropic-wire 上游不给 thinking 块签名(DeepSeek /anthropic 端点)时,每次 acp-loop 重发(compress/acp_status)都因缺 thinking 兄弟块被 content[].thinking ... must be passed back 400,压缩彻底不可用。
  2. 什么原因导致的:重放门只按协议判(anthropic|google ⇒ 必须带签名),把"这条线要签名"错当成"所有 anthropic/google 线都要签名";对不签名的中转,整段思考被丢弃。
  3. 影响范围与严重程度:仅命中走 anthropic/google wire 且本轮零签名证据的场景(典型=DeepSeek /anthropic 兼容端点)。严重度=该场景下压缩/acp_status 持续 400(可用性缺陷);真实 Claude/Gemini 不受影响。
  4. 怎么修复的:把"是否需要签名"从协议常量判定换成同位置的本轮证据判定;无证据⇒按不签名中转逐字回传;"must be passed back" 400 改为直接上抛而非剥离重试(剥离只会二次 400);learn-on-failure 词表补 content[].thinking 拼写(core.ts + relay.ts 两处)。
  5. 是否有回归问题:审过既有行为——真实 Claude/Gemini 每轮都签名→不变;混合块(有证据时未签名块仍丢)由负控钉住;中性 400 仍剥离重试(对比测试钉住);fail-fast 方向更严不更松;无阈值/时序/输出格式漂移。两模式都已推:改动落在 proxy 模式服务端重发路径,plugin 模式自带历史不入该分支,天然 inert。
  6. 是否破坏缓存:不涉及。剥离重试仅对中性错误保留;被排除的 "must be passed back" 本就二次 400,前缀缓存无影响。
  7. 是否改了设计方向:不涉及架构转向,仅把一个协议常量判定替换为同位置的证据判定,边界/数据流不变。
  8. 是否引入新配置:不涉及(config-surface-guard 绿)。

CI(head e18bbb0):test 全矩阵(ubuntu/windows × node 22/24 × 5 shard)、static-gates、mysql-contract、config-surface-guard 全绿,0 failed / 0 pending(skipped 为 env 门控的 e2e/theorem 车道,默认不跑)。

收工判定:可合(mergeable)。无 kernel 变更、无需决策项、无需在分支上顺手补的小修。合并仍归人(agent 不合 PR)。

@ranxianglei

Copy link
Copy Markdown
Owner Author

🤖 Powered by ework · qwen3.8-27b

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

复审完成(review-worker-r1 第二轮)— 结论:APPROVE / 可合,与 floor 4 的初审一致。

我未复读初审结论,独立重走了:sawSignedThinkingThisAttempt 生命周期(core.ts:797 声明在 attempt 循环外 / :841 每轮重置 / :876 仅见 signature_delta 置真 ⇒ 重放点读到的是存活那次 attempt 的证据)、错误路径控制流(内层 strip-retry 守卫新增 isReasoningRequiredError ⇒ "must be passed back" 400 throw 进外层 catch,外层仍 arm strictReasoningEcho 并 emit upstream error 400;中性 400 照旧剥离重试)、单一定义点无漏网门、#221 pin 翻转 + 混合块负控保住致命规则、测试驱动真 runCompressLoop+真 adapter+mock fetch 且每条断言 pre/post 均翻转。

GitHub 侧交付说明:完整八问审查已作为评论投递到 PR(comment id 6563355189)。未能提交正式 APPROVE 状态——本环境审核身份即 PR 作者 ranxianglei,GitHub 硬性禁止对本人 PR 自批(422 "Can not approve your own pull request"),此为平台约束、不予绕过,改走评论承载同等实质结论。

CI head e18bbb0 全绿(test 全矩阵 ubuntu/windows×node22/24×5 shard、static-gates、config-surface-guard、mysql-contract;0 failed/0 pending,skipped 为 env 门控 e2e/theorem 车道)。无 kernel/ 变更、无 version bump、无 config-surface 变更;无需在分支上顺手补的小修。收工判定:可合,等 owner 合并(agent 不合 PR)。

@ranxianglei
ranxianglei merged commit dec393f into master Nov 2, 2026
53 checks passed
@github-actions

github-actions Bot commented Nov 2, 2026

Copy link
Copy Markdown
Contributor

📦 Merged Build Published

This merge to master was published as 0.0.1006-master.dec393f (rolling master tag).

npm install -g billion-context@0.0.1006-master.dec393f  # this exact build
npm install -g billion-context@master  # always the newest merged build

npm view billion-context@master prints the current version at any time.

Louie0806 added a commit to Louie0806/billion-context that referenced this pull request Nov 2, 2026
…king replay + ranxianglei#5528 additional_tools reachability

Eight files; two overlap this lane and both auto-merged:
- src/loop/core.ts: master's ranxianglei#5495 replay/exposure hunks sit at :178 and
  :1317-1331, this lane's draft-dump gate at :1181 and the DEGENERATE_RETRY_NUDGE
  import at :49. Verified after the merge that both sides are present.
- src/server/relay.ts: master's hunk is at :1576, this lane's non-SSE
  pipePluginJson egress at :1976. Both present.

Post-merge: kernel build, tsc, lane suites and the four static gates re-run.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

1 participant