Skip to content

[PR] fix: preserve unknown anthropic top-level blocks verbatim in the anthropic wire codec - #3547

Closed
ranxianglei wants to merge 1 commit into
masterfrom
2026-10-17_issue3410-anthropic-unknown-block
Closed

ranxianglei wants to merge 1 commit into
masterfrom
2026-10-17_issue3410-anthropic-unknown-block

Conversation

@ranxianglei

Copy link
Copy Markdown
Owner

kernel-change: the anthropic wire codec's block switch has no default case — unknown top-level block types (in the wild today: document/PDF) are silently dropped inside the kernel's parse→core projection, which only the kernel owns (the drop point plus the protected-predicate semantics). Host side cannot fix it. Owner approval trail: #3487 (P0, "default mandatory fix" + repair direction "fallback passthrough for unknown blocks, at least warn before dropping") and the full implementation plan posted in the #3410 thread.

Closes #3410

What was broken

Rebuilding an anthropic request body ran every content block through a switch over the six known types (text/tool_use/tool_result/thinking/image/redacted_thinking) with no default — any other top-level block type was simply not emitted. In practice that means user-sent PDF document blocks vanished on every turn: no warning, no log, silent content loss (same severity class as fold-drop-text).

Fix (one mechanism, kernel + host)

  • kernel/src/wire/anthropic.ts: new default case rides unknown blocks on the existing rawAnthropicBlock sidecar, exactly like image/redacted_thinking. coreToAnthropic already re-emits sidecars verbatim (block-internal cache_control included), so the round trip is byte-exact — verified by golden-free deepEqual regression tests.
  • kernel/src/protected.ts: unknown anthropic blocks now count as media AND unrecoverable. Their payload exists only in the sidecar and bili cannot archive it to disk (image-restore handles images only), so folding would lose it permanently. Archivable images (建议:compress.stripImages 默认开启——否则带图消息不可压缩会形成只升不降的输入地板撑爆上下文 #2607) and redacted_thinking keep their existing behavior.
  • kernel version 0.0.130 → 0.0.131.
  • Host notice: one-time per session per type-set (warnUnknownAnthropicBlocks, same bounded FIFO as the [Bug] OpenAI 线解析/重建无条件丢弃非 text/image_url 的 content part(如 DeepSeek Files API file part)——不压缩也丢图 #1205 drop warning). It is a NOTICE, not a drop warning — nothing is dropped anymore; it tells users their client sent PDFs and that such blocks are excluded from folding.
  • Host billing (src/image-tokens.ts): document blocks are now priced like the openai file precedent (inline base64 by payload size, url refs flat 4096, bare/malformed-source flat 4096) across the cost walker, the lockstep count walker, and the raw-body probe. Before this PR the bytes were dropped so there was nothing to price; now that they are preserved, the fold gate must see them or it undercounts.

Behavior change (old → new)

Overlap with in-flight #3412 (opaque-media-reserve)

#3412 deliberately left anthropic documents unpriced in src/image-tokens.ts because the codec dropped them at the time. This PR makes them survive, so they must be priced here. If #3412 merges first, rebase takes the union of intent in the shared walker — flagged for sequencing.

Verification

  • Kernel layer: byte-exact round-trip regressions (document with base64 source + cache_control; multiple same-type blocks get distinct stable ids) + protected-predicate cases. Targeted 63/63 green; FULL kernel suite 1296/1296 green.
  • Host layer: new tests/issue3410-unknown-anthropic-block.test.ts (detector shapes, warn dedup matrix, three billing shapes, cost/count lockstep, probe admission incl. spaced JSON, image regression anchors). Targeted run with fix-1205/issue767/issue1843 siblings: 41/41 green.
  • npm run typecheck clean; npm run build green (dist-external-zod gate OK).

Kernel change — human-only review tier per the Kernel Boundary.

@github-actions

github-actions Bot commented Oct 17, 2026 •

Copy link
Copy Markdown
Contributor

📦 Built Package Artifact

Branch: 2026-10-17_issue3410-anthropic-unknown-block (4ffb236)

Option A — Install this exact build (recommended)

npm install -g billion-context@0.0.4755-pr.3547.2e08447

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

Option B — Download artifact

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

Install instructions are refreshed on each push.

@ranxianglei

Copy link
Copy Markdown
Owner Author

🤖 Powered by ework · qwen3.8-27b

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

查重:不重复。 与在途 #3412(opaque-media-reserve)有交集但互补——#3412 预留了 opaque-media 的窗口配额,但当时 anthropic codec 会丢 document 块所以没给它计价;本 PR 让 document 块存活,因此必须在计价侧补上(共享 walker 取并集,PR 正文已标 sequencing)。两者修的是不同层(#3412 = reserve 计量 / 本 PR = codec 保留 + 计价),非同一改动的重复。

审核独立性: 以下结论由我独立读 diff + 本地复现得出,未照抄 PR 描述。kernel 侧逐行读了 anthropicToCore/coreToAnthropic 与两个 protected 谓词;lock-sync 失败我在隔离 worktree 用门禁原命令复现并修复。

结构化八问

  1. 是什么问题:anthropic wire codec 的 block switch 没有 default 分支——六种已知类型(text/tool_use/tool_result/thinking/image/redacted_thinking)之外的任何顶层 block(现实里就是 PDF document)在每次请求重建时被静默丢弃,无日志无告警,用户内容每轮丢失(与 fold-drop-text 同级严重)。
  2. 什么原因导致:根因在 codec 解析侧——anthropicToCore 对 content block 做穷举 switch、缺 default,未知类型不落任何 CoreMessage,重建时消失。这是「机制缺失」而非表面现象;修法打在机制上(补 default 走 sidecar 透传),不是下游过滤症状。drop 点在 kernel 内,host 无法修,与 issue 声明一致。
  3. 影响范围与严重程度:凡经 bili 代理、且客户端会发非六类顶层 block 的 anthropic 会话(典型:发 PDF 附件)——此前每轮静默丢内容 = 数据丢失级;修复后转逐字透传 + 计入窗口 + 排除折叠 + 一次性提示。已知六类 block 字节级不变(回归锚定)。
  4. 怎么修的(策略/机制):未知 block 复用既有 rawAnthropicBlock sidecar 通道,与 image/redacted_thinking 完全同构——coreToAnthropic 对 text 类消息若带 sidecar 就原样 push 回并 break,cache_control 随 block 对象一起存活,round-trip 字节精确(我读了 coreToAnthropic 确认,不只靠测试断言)。protected 侧把未知 block 同时判为 media + unrecoverable:payload 只在 sidecar、bili 只归档 image,折叠即永久丢失,故永不折叠(与 redacted_thinking 同待遇)。host 侧:一次性 notice(复用 [Bug] OpenAI 线解析/重建无条件丢弃非 text/image_url 的 content part(如 DeepSeek Files API file part)——不压缩也丢图 #1205 的 bounded FIFO,是「提示」非「drop 告警」)+ 三个 billing walker 加性补 document 计价(base64 按 payload、url 按 costForUrl、裸/畸形源 flat REMOTE_IMAGE_TOKENS),使 fold gate 不再少计。
  5. 是否有回归:逐项过——
    • 六类已知 block:字节级不变(round-trip 测试 + billing 回归锚 url→REMOTE、sourceless→0、text→0 钉住)。
    • sidecar 载体全集 = {image, tool_result(hasNonText), redacted_thinking, +新 unknown};protected 两处排除名单恰好 = 前三者,故新判定只对 unknown 生效,不误伤正常会话。
    • hasMediaPayload 消费方仅 absorb.ts 的 mediaOnly 过滤/豁免(纯布尔判断,无归档触发点);document 骑 rawAnthropicBlock 而非 imageBase64,不进 image-restore 通道 → 无 choke。
    • 高危模式排查:无 fail-fast 变吞错、无阈值/时序/输出格式漂移;唯一行为变化即目标行为且已披露。
  6. 是否破坏缓存:不影响前缀缓存/重试/去重键;head system message 不动;新增仅 user 侧 media 计数 + 一次性 warn key(复用既有 FIFO cap 4096)。document 计入 media reserve 会让大 PDF 会话对其余可折叠内容施加更大折叠压力——这是有意的(字节现在真在上游计费,fold gate 必须看见),属行为变更非缓存破坏。
  7. 是否改设计方向:不改架构/边界/数据流。是把「未知 block」纳入既有 sidecar 透传 + protected 谓词 + billing walker 三条既有管线,零新抽象。
  8. 是否引入新配置:无新 env/config/默认值。warn 复用既有 warnedWireDropKeys FIFO;billing 复用既有 REMOTE_IMAGE_TOKENS 常量与 costForUrl/base64ImageCost。config surface 零变化(config-surface-guard 已绿佐证)。

行为变更(旧→新,披露)

代码整洁(外部提交从严)

命名达意(unknownAnthropicBlocks / warnUnknownAnthropicBlocks / WireDropReport)、无死代码、无重复(WireDropReport 抽成两 detector 共用)、无魔法数(ANTHROPIC_KNOWN_BLOCKS 具名 Set、REMOTE_IMAGE_TOKENS 具名常量)、注释讲为什么、风格跟仓库一致、零新依赖。全项过。diff 10 文件全部 on-purpose,无无关 churn / 空行噪音。

测试有效性

真测试,断言有牙:detector deepEqual 精确报告、warn 去重矩阵(同/异 type-set、跨 session、known-only 静默、null 静默、reset 后重报)、parity 断言(同字节 base64 document 与 image 计费恒等)、cost/count 符号锁步(每种 shape)、raw probe 同时吃 compact + spaced JSON、image 回归锚。非 tautology。注意:这些 host/kernel 测试此前因 lock-sync 红而全部 SKIPPED,从未在 CI 真正跑过——「41/41」「1296/1296」是作者本地值,非 CI 验证;现已解锁,正在 CI 执行。

CI 状态 + 我已清的阻塞

  • 原 head 100c5c6ef:lock-sync FAILURE → 全部 test/static-gates/e2e 腿 SKIPPED(被门禁挡死)。
  • 根因(隔离 worktree 用门禁原命令 npm install --package-lock-only + git diff --exit-code 复现钉死):PR 把 kernel/package.json 0.0.130→0.0.131 却没同步根 package-lock.json(仍记 acp-kernel 0.0.130);门禁重新生成该字段即产生 diff → exit 1。
  • 已修:在 PR 分支追加一个标注 commit 95eb5f5eb(仅 1 行:packages["kernel"].version 0.0.130→0.0.131,用门禁原命令再生成、无附带 churn),push 成功(100c5c6ef..95eb5f5eb)。
  • 新 head 95eb5f5eb CI:lock-sync = success,全部 gate 绿,test/static-gates 矩阵正在跑(此前全 SKIPPED),e2e 腿按其自身 env 门正常 skip。矩阵跑完我给最终测试结论。

判定

@ranxianglei

Copy link
Copy Markdown
Owner Author

🤖 Powered by ework · qwen3.8-27b

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

CI 跟进(新 head 95eb5f5eb,lock-sync 阻塞已清后):

  • lock-sync 已转 success(单行 lockfile 再生成修复生效),全部 gate 绿(kernel-guard / version-guard / config-surface-guard / docs-sync / docs-coverage / unused-exports / test-import-graph / lane-guard / pi-subagents-guard / build-artifact / plan)。
  • 解锁后的完整 test/static-gates 矩阵:零失败。已绿的:static-gates(win22、ubuntu24)+ test(ubuntu22 全 3 shard、win22 全 2 shard)= 7 条;仅剩 1 条慢腿 test (ubuntu-latest, 24, 1)(node24)仍在跑,非卡死、无失败信号。
  • 判定不变:代码可合。收工条件现剩两条——① 该最后一条腿落地(我会确认最终全绿/异常);② owner 对 kernel 改动人工签核(Kernel Boundary human-only tier,我不合并)。

@ranxianglei

Copy link
Copy Markdown
Owner Author

🤖 Powered by ework · qwen3.8-27b

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

CI 全绿(最终确认,head 95eb5f5eb): 完整 test/static-gates 矩阵已跑完——32 success / 0 failure / 0 pending(16 条 skip 是 hermetic e2e 腿按其自身 env 门正常跳过)。lock-sync 修复生效转绿后,整条管线无任何失败,此前被门禁挡死而从未真正执行的 host/kernel 测试现已全部在 CI 上跑过并转绿。

收工判定:

  • 代码:可合 —— 机制正确(default 走 sidecar 逐字透传,round-trip 字节精确)、回归面干净(六类已知 block 不变,新判定只对未知类型生效)、测试有牙(去重矩阵/parity/锁步/回归锚)、整洁达标。八问详见上一条。
  • CI:全绿,阻塞已清(lock-sync 单行 lockfile 再生成,commit 95eb5f5eb)。
  • 仅剩一道流程门: 这是 kernel 改动(kernel/src/** + 版本 bump),按 Kernel Boundary 属 human-only review tier,需你人工签核;我不合并、不 auto-merge。代码 + CI 均已就绪,等你的 kernel 批准即可合并。

@ranxianglei

Copy link
Copy Markdown
Owner Author

🤖 Powered by ework · qwen3.8-27b

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

协调通知:本 PR 的 kernel hunk 已被 #3961 吸收,请收窄为 host-only。

#3938/#3939 内核专项把「kernel 侧 wire 保真」收进统一节奏后,本 PR 的以下部分已逐字搬入 #3961(仅适配 master 现行 deriveCanonicalMessageId,#3642):anthropic.ts default 分支、protected.ts 两谓词、kernel/package.json bump、kernel/tests 两处新增。建议处置:

  1. rebase 到 master 后删除 kernel/ 下的 hunk(与 [PR] kernel: wire byte fidelity — anthropic unknown-block passthrough + openai/google stability pins (#3410) #3961 重复),保留 host 侧:image-tokens.ts document 计价(cost/count/probe)、wire-drop-warn.ts + server.ts + prepare-anthropic.ts 的 notice 接线、tests/issue3410-unknown-anthropic-block.test.ts;
  2. 合并顺序:[PR] kernel: wire byte fidelity — anthropic unknown-block passthrough + openai/google stability pins (#3410) #3961 先合(或并行),本 PR 随后 rebase——两侧无 hunk 重叠,rebase 应干净;
  3. rebase 时必须更新一个 pin:tests/issue3398-opaque-media-reserve.test.ts:87-106(该文件在你当前基线 e3939dc 之后才进 master)断言 anthropic 顶层 document 计价 0/0,注释称「the codec drops it」。[PR] kernel: wire byte fidelity — anthropic unknown-block passthrough + openai/google stability pins (#3410) #3961 落地后注释即失实,你的 image-tokens hunk 落地后断言变红(预期新值:cost Math.ceil(8192/4)、count 1)。这是目前无人属主的跨 PR 断言更新,挂在本 PR 名下最顺。

Closes #3410 链接保留在本 PR 上不变。

@ranxianglei ranxianglei changed the title fix: preserve unknown anthropic top-level blocks verbatim in the anthropic wire codec [PR] fix: preserve unknown anthropic top-level blocks verbatim in the anthropic wire codec Oct 20, 2026
ranxianglei pushed a commit that referenced this pull request Oct 20, 2026
anthropicToCore's block switch had no default case: unknown top-level
block types (in the wild today: document/PDF) fell out silently and their
payload vanished on every rebuild with no warning. Unknown blocks now ride
rawAnthropicBlock exactly like image/redacted_thinking — coreToAnthropic
re-emits them byte-exact (cache_control included) and never renders the
label text; protected.ts classifies them as media AND unrecoverable so a
fold can never destroy a payload the host archives nowhere.

Absorbs the kernel-side hunks of PR #3547 (adapted to master's #3642
canonical identity); #3547 keeps its host-side notice/billing surface.

kernel-change: wire保真(#3938 / #3939⑥, 吸收#3547)
ranxianglei pushed a commit that referenced this pull request Oct 20, 2026
anthropicToCore's block switch had no default case: unknown top-level
block types (in the wild today: document/PDF) fell out silently and their
payload vanished on every rebuild with no warning. Unknown blocks now ride
rawAnthropicBlock exactly like image/redacted_thinking — coreToAnthropic
re-emits them byte-exact (cache_control included) and never renders the
label text; protected.ts classifies them as media AND unrecoverable so a
fold can never destroy a payload the host archives nowhere.

Absorbs the kernel-side hunks of PR #3547 (adapted to master's #3642
canonical identity); #3547 keeps its host-side notice/billing surface.

kernel-change: wire保真(#3938 / #3939⑥, 吸收#3547)
ranxianglei pushed a commit that referenced this pull request Oct 20, 2026
anthropicToCore's block switch had no default case: unknown top-level
block types (in the wild today: document/PDF) fell out silently and their
payload vanished on every rebuild with no warning. Unknown blocks now ride
rawAnthropicBlock exactly like image/redacted_thinking — coreToAnthropic
re-emits them byte-exact (cache_control included) and never renders the
label text; protected.ts classifies them as media AND unrecoverable so a
fold can never destroy a payload the host archives nowhere.

Absorbs the kernel-side hunks of PR #3547 (adapted to master's #3642
canonical identity); #3547 keeps its host-side notice/billing surface.

kernel-change: wire保真(#3938 / #3939⑥, 吸收#3547)
ranxianglei pushed a commit that referenced this pull request Oct 20, 2026
anthropicToCore's block switch had no default case: unknown top-level
block types (in the wild today: document/PDF) fell out silently and their
payload vanished on every rebuild with no warning. Unknown blocks now ride
rawAnthropicBlock exactly like image/redacted_thinking — coreToAnthropic
re-emits them byte-exact (cache_control included) and never renders the
label text; protected.ts classifies them as media AND unrecoverable so a
fold can never destroy a payload the host archives nowhere.

Absorbs the kernel-side hunks of PR #3547 (adapted to master's #3642
canonical identity); #3547 keeps its host-side notice/billing surface.

kernel-change: wire保真(#3938 / #3939⑥, 吸收#3547)
Host-side half of #3410: price document/other unknown top-level blocks in
the billing reserve (probe + cost/count walkers) and warn once per shape
that they ride verbatim but stay out of folding. The kernel-side codec fix
(sidecar passthrough) ships separately in #3961 — merge #3961 first so
priced bytes == forwarded bytes. Pin updated: #3398 document case now
expects opaque-media pricing.
@ranxianglei
ranxianglei force-pushed the 2026-10-17_issue3410-anthropic-unknown-block branch from 95eb5f5 to 4ffb236 Compare October 20, 2026 13:39
@ranxianglei

Copy link
Copy Markdown
Owner Author

🤖 Powered by ework · qwen3.8-27b

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

收窄完成,已按协调方案重推。此前冲突 worker 停摆(分支停留在旧 base e3939dc6f),我接手完成了全部动作。

做了什么

验证:定向本地跑 51/51 绿(issue3410 新增套件、issue3398 更新后、issue1843、fix-1205、issue767 像素计费族);typecheck 干净。CI 已在 4ffb23606 上运行。

⚠️ 合并顺序:必须在 #3961 之后合入。 若本 PR 先合:① NOTICE 文案声称 "preserves … verbatim" 而 master codec 仍在丢块(honest-output 违约窗口);② 计费会对未转发的字节计价(方向安全的高估,但数字是错的)。反向顺序(#3961 先)的窗口是 document 转发但未计价——#3961 正文已带该 pin 备注,且计费缺失只是少收,风险更低。建议顺序维持:#3961 → 本 PR。

@ranxianglei

Copy link
Copy Markdown
Owner Author

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

关闭为 superseded by #3981。owner 在 #3938(2026-10-20)已把 PR-C 的 host 车道定为 #3981(stacked on #3961),本 PR 与其范围完全重合(unknownAnthropicBlocks 检测器、每-session NOTICE、计费对齐、同名测试文件)——一个 issue 只留一条 PR。

本 PR 今日收窄后的 head 4ffb23606(纯宿主侧、rebase 到当前 master、单 commit)的工作已全部被 #3981 覆盖或吸收:

合并顺序不变:#3961 → #3981。若 owner 希望保留本 PR 而非 #3981,reopen 即可,head 是干净的单 commit。

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.

anthropic wire silently drops unknown top-level blocks (incl. PDF document) when rebuilding history

1 participant