Skip to content

fix(server): redacted_thinking arms the #651 reasoning-drop gate - #3196

Merged
ranxianglei merged 4 commits into
ranxianglei:masterfrom
sorinirimies:fix/redacted-thinking-reasoning-drop-gate
Oct 16, 2026
Merged

ranxianglei merged 4 commits into
ranxianglei:masterfrom
sorinirimies:fix/redacted-thinking-reasoning-drop-gate

Conversation

@sorinirimies

Copy link
Copy Markdown
Contributor

Problem

Anthropic sessions with extended thinking intermittently fail with:
messages.N.content.M: thinkingorredacted_thinking blocks in the latest assistant message cannot be modified
Setting compress.reasoning.drop: false makes it go away, so the #651 drop is the rewriter.

Cause

withReasoningDrop (#1658 gate) only treats a reasoning message with a signature as signed thinking. redacted_thinking blocks ride as a text message with rawAnthropicBlock (kernel wire/anthropic.ts), so they never arm the gate. Dropping the sibling reasoning run before a closed compress call then rewrites the assistant message holding them.

Fix

Gate also trips on rawAnthropicBlock.type === 'redacted_thinking'. Adds a test in tests/reasoning-echo.test.ts; the file passes (59/59).

Note: I could not capture a failing wire body, so this closes the gap I found by reading the code. It may not be the only path.

sorinirimies and others added 3 commits October 15, 2026 00:27
…p gate (ranxianglei#1960)

withReasoningDrop only treated a signed reasoning message as signed thinking.
redacted_thinking rides as a text message with rawAnthropicBlock, so it never
armed the gate; dropping the sibling reasoning run rewrote the latest assistant
message -> 400 'thinking or redacted_thinking blocks ... cannot be modified'.
…eal teeth

Review fix for PR ranxianglei#3196 (external contribution, aligned per repo review policy).

The original test placed the redacted carrier AFTER the reasoning run, where
dropCompressReasoning's contiguity walk stops immediately — nothing was ever
dropped, so the test passed against the pre-fix code too (verified empirically).
Reordered so the redacted block shares the assistant message prefix of the
dropped run (the shape coreToAnthropic merges into one rewritten API message),
and added a wire-level regression case through anthropicToCore/coreToAnthropic
pinning both the kernel side-car shape and the no-rewrite guarantee.
@ranxianglei
ranxianglei force-pushed the fix/redacted-thinking-reasoning-drop-gate branch from c3c9792 to 06b5a0c Compare October 14, 2026 16:29
@ranxianglei

Copy link
Copy Markdown
Owner

🤖 Powered by ework · qwen3.8-27b

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

查重: 无 —— open+closed 的 issue/PR 里按同一错误签名(redacted_thinking ... cannot be modified + #651 drop)检索,无匹配项。

我按代码(不是按描述)审了这个 PR。结论:修法方向正确,但你原来加的那个测试是假测试——在没修的情况下它也能通过。我已直接在你的分支上把测试修好(独立标注 commit),并顺手 rebase 到最新 master。细节如下。

审核过程

  1. 核对 kernel/src/wire/anthropic.ts,两个关键事实成立:
    • redacted_thinking 解码后是一条 text 消息、带 rawAnthropicBlock,永远不会成为 reasoning 消息(:186-202);
    • 序列化时连续同角色消息合并成一条 API 消息,rawAnthropicBlock 原样重发(:212-274)。
      ⇒ 从同一条 assistant 消息内部删掉任何一条 BiliMessage,都会改写线上那条消息。你的问题分析成立。
  2. 对你原测试做了实证 A/B:把 src/server.ts 换回修复前版本跑你新增的测试——照样通过。原因:你把 redacted 载体放在了 reasoning 序列之后,而 dropCompressReasoning 只回扫紧邻 compress 调用的连续 reasoning,一碰到 text 就停,什么都不会删,断言恒真。这个测试防不住回归(同文件 :684 的 "test has teeth" 负对照就是项目标准)。
  3. 已在你的分支直接修正(外部贡献按仓库 review 政策对齐重构,不走拉锯):
    • commit 81ed5f36e:redacted 载体移到 reasoning 序列之前(这才是「删序列会改写携带 redacted 块的消息」的真实形状),并补了一个走真实 codec(anthropicToCore/coreToAnthropic)的端到端回归 case:unsigned thinking + redacted + 已关闭 compress 轮,断言解码出的 side-car 形状、gate 生效、出站 assistant 消息保留全部 3 个块。
    • 重新 A/B 验证:修复前代码 → 两个新测试都 FAIL(真复现);修复后 → 通过;整文件 60/60 绿。
    • rebase 到最新 master(审查时分支落后 15 个提交;其后至 master 对 src/server.ts 的唯一改动是 [Bug] 新会话(无 usage 基线)preflight 把字符数当 token——折叠其实成功(2263796→1111212)却 fail-fast 502,上游实际只计费 540828 #3146,与本区域无关,零冲突)。你的 ci: retrigger 空提交已保留(authorship 不变)。最终 head:06b5a0c73。
  4. 本地 typecheck 干净;全量回归交给远程 CI(已在跑的 27 个 check 上观察)。

八问结论

  1. 是什么问题:开了 extended thinking 的会话里,Protected-message exemption inflates the incompressible context floor: per-compression reasoning is never reclaimable (~83.5% of measured residual) #651「删已关闭 compress 调用前的 reasoning」优化一旦触发,可能改写携带 redacted_thinking 块的 assistant 消息,Anthropic 直接 400(...cannot be modified)。
  2. 什么原因导致的:[bug] dropCompressReasoning (#651) strips inbound thinking from compress tool_use on Anthropic wire, triggering thinking-pair-violated warning (#684) #1658 的保护 gate 只认「带签名的 reasoning 消息」为 signed thinking;而 redacted_thinking 在 kernel 解码里是 text+rawAnthropicBlock 形态,永远不触发 gate——谓词漏了一类 signed thinking-family 载体。
  3. 影响范围与严重程度:含 redacted 块的 Anthropic 系上游会话,且 compress 调用前紧邻的 reasoning 超过阈值(默认 2048 字符)时触发;严重程度=该请求失败(400),用户侧可关 compress.reasoning.drop 规避;间歇性而非必现。
  4. 怎么修复的:把 gate 谓词扩展为覆盖全部 signed thinking-family 载体(reasoning+签名,或 rawAnthropicBlock.type===redacted_thinking)——视图里出现任一即本请求禁用 drop。修在决策点上,不是流中过滤载荷。
  5. 是否有回归问题:逐项过——unsigned 纯 reasoning 会话照常删除(既有测试 "unsigned reasoning still drops" 钉住);空签名不触发(既有测试钉住);strictEcho 早退路径未动;tag-echo-filter 只作用于 prose 通道(KDD release v0.1.3 #10,thinking 家族永不改写);nudge/acp_summary 只插入新消息不修改 assistant 消息。未见无意回归。
  6. 是否破坏缓存:不涉及。只影响出站视图是否删 reasoning,不动持久化历史、block 状态与 prefix-cache 锚点。
  7. 是否改了设计方向:不涉及。只是把既有 gate 的谓词补齐到 kernel 契约口径,机制不变。
  8. 是否引入新配置:不涉及。

行为变更披露(有意):gate 触发面扩大——旧:仅视图内有带签名 reasoning 才触发;新:有 redacted_thinking 载体也触发。后果:凡出现过 redacted 块的会话不再享受 #651 节省(与 signed thinking 早已接受的权衡一致,且严格降低 400 风险)。

遗留说明(与你 note 一致):没有 failing wire body,无法排除其他改写路径;本 PR 关闭的是读码确认的这个 gap。若后续再报同类 400,需先抓实际 wire body 再定位下一条路径,不建议在无证据下扩大本 PR 范围。

判定:本地定向验证通过、rebase 干净、mergeable,CI 转绿即可由人合并:#3196

@ranxianglei

Copy link
Copy Markdown
Owner

🤖 Powered by ework · qwen3.8-27b

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

CI 结果更新(接上条):pushed head 06b5a0c73 的 30 个 check 跑完——guard/docs/build 全绿,6 个 test shard 红。我拉了全部失败日志逐一定位,结论:红的全是既有的端口/自重启 e2e flake,与本 PR diff 无关。证据链:

1. 挂了哪些测试(6 个 job 去重后共 4 个,全是真进程+端口+时序敏感 e2e):

  • zone live: marked upgrade-replacement fails fast on an occupied port instead of hopping (#3044) — tests/port-zone-live.test.ts:356
  • performSelfRestart: a replacement that survives SIGTERM is escalated loudly (#3044) — tests/auto-restart.test.ts:665(30s timeout)
  • hook e2e: an occupied stable port fails loud — never port-hops — tests/claude-native.test.ts:1196(120s)
  • hook e2e: manual bili start on the pinned port is attached, not refused (#1322/#1335/#1660) — tests/claude-native.test.ts:1565(180s)

2. 与 PR diff 无关的证据:

  • 本 PR 只动 src/server.ts 的 gate 谓词 + tests/reasoning-echo.test.ts,与端口占用/自重启/hook 逻辑零路径重叠;
  • 你 rebase 之前的原始 head(c3c9792)那次运行就挂了其中两个(ubuntu-22 shard 2/3,正是上表第 1、4 项)——失败签名早于我的 rebase 存在;
  • CI 是 3 shard × 4 OS/node 并行(最多 12 个并发 test job),这类 30-180s timeout 的真进程测试在 runner 争抢下时序易漂移;master 最近一次运行的 test lane 恰好是 skipped,没有可对比的全绿基线。

3. 处置:按「同一失败域一个 issue」的粒度,已立 #3206 跟踪这组 flake(带来源标记,含两次运行的对照证据与建议方向),不在本 PR 里混修。

更新后的判定:本地 typecheck + 定向测试(含 A/B 复现验证)全绿;CI 红灯全部归因于 #3206 的既有 flake 家族,非本 PR 引入。是否合并由你定:若接受「已知 flake 红、diff 相关 lane 全绿」可直接合;想要干净信号的话 retrigger 一次即可(注意可能再中 flake)。PR:#3196

ranxianglei#3222 restructured the ranxianglei#651 drop on master (planReasoningDrop/applyReasoningDrop),
so the ranxianglei#1960 redacted_thinking gate now lives in planReasoningDrop; the
standalone withReasoningDrop keeps composing it, tests unchanged.
@ranxianglei

Copy link
Copy Markdown
Owner

(ai: zhipuai-lb/glm-5.3)

Thanks for the fix — the gate logic is exactly right (rawAnthropicBlock.type === "redacted_thinking" is the missing arm of the #1658 signed gate).

Heads-up: master had moved under this branch (#3222 restructured planReasoningDrop's call site), so I merged master INTO this branch and resolved the collision by hand — your 3 commits are untouched (merge commit 3b2da8f). Tests here: reasoning-echo 60/60, reasoning family green, typecheck clean.

The follow-up that builds on this gate lands in #3311 (compress-reasoning flush timing, #3198) — your branch is the base of that stack, so merging this PR unblocks it.

@ranxianglei

Copy link
Copy Markdown
Owner

🤖 Powered by ework · qwen3.8-27b

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

收到,我独立复核了这次手工合入(没有只信报告),结论:合入是干净的,没有破坏任何东西。

1. 手工 merge 复核(3b2da8f)

  • 我的 3 个 commit(c3121ed 修复、81ed5f36e 测试补牙、06b5a0c73 retrigger)原样保留,作者身份未动;
  • 分支相对合入的 master 快照(74456f3)的真实差异 = 恰好两个文件:src/server.ts +15/-1、tests/reasoning-echo.test.ts +29——和我最初审核过的 diff 一字不差,冲突解决忠实;
  • [Bug] MCP compress 未接入 reasoning 清理及未完成调用保护 #3222 把 withReasoningDrop 拆成了 plan/apply 两段(id 计划先于 processTurn 计算)后,本 PR 的门控完整存活:hasSignedThinkingBlock 两个臂(signed reasoning + redacted_thinking)都在,且仍在 strictEcho 提前返回之后、drop 计算之前短路——所以 [Bug] MCP compress 未接入 reasoning 清理及未完成调用保护 #3222 新加的 mcp__bili__compress 展开同样被这道门保护;
  • 合并后 head 本地实测:reasoning-echo 60/60 全绿,typecheck 干净。

2. CI 红了 6 个 shard——已逐一定性,都不是这个 PR 的问题

3. master 又前进了 31 个 commit(含 #3266 移除 image-compression、#3080 output-budget-floor):对当前 master(6ee4a2a)冲突探测 = 0,门控区域 churn = 0 → 不需要再次同步,GitHub 可直接合并。

结论:手工合入验证通过,CI 红项均已归因到仓库级既有问题(#3319 / #3206)。此 PR 可以合,合完解锁 #3311/#3198 的后续栈。

@ranxianglei
ranxianglei merged commit a046d4e into ranxianglei:master Oct 16, 2026
37 of 43 checks passed
@github-actions

Copy link
Copy Markdown
Contributor

📦 Merged Build Published

This merge to master was published as 0.0.482-master.a046d4e (rolling master tag).

npm install -g billion-context@0.0.482-master.a046d4e  # 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.

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.

2 participants