Skip to content

fix: gate dsh compaction refusal on live compression substrate (#3016) - #3030

Closed
ranxianglei wants to merge 3 commits into
masterfrom
2026-10-13_dsh-compaction-substrate-gate
Closed

ranxianglei wants to merge 3 commits into
masterfrom
2026-10-13_dsh-compaction-substrate-gate

Conversation

@ranxianglei

Copy link
Copy Markdown
Owner

Closes #3016

Problem

When a dsh native compaction call lands on the unattributed passthrough lane (x-bili-passthrough, #1117/#2709), bili refused it unconditionally with 422 dsh_compaction_refused — even when bili itself held no compression substrate (state.blocks = 0), i.e. nothing existed to protect. The same unconditional refusal applied in the pipeline guard. Consequence: exactly when bili's own nudge/decision channel was dead (#3004 deadlock), the last remaining shrink backstop (dsh's own compaction) was also disabled, pinning context at the wall until the hard limit.

Root cause

The guard's job (#1729/#2028) is to stop an unattributed dsh checkpoint from durably shadowing an existing compression substrate. It refused before ever asking whether such a substrate exists. A refusal that protects nothing is pure loss.

Fix (mechanism)

Gate the refusal on whether a substrate actually exists to protect:

  • Pipeline path (target session resolved): relay verbatim when session.state.blocks.length === 0; refuse only when the target holds substrate.
  • Passthrough lane (target unknowable — the client stamps only x-bili-passthrough, never a session id, because the call is agentless): refuse iff anyLiveSessionHasCompressionBlocks(); otherwise stand down and relay verbatim.

anyLiveSessionHasCompressionBlocks() (new, src/session.ts) scans the in-memory live-session map — fast, never walks disk.

Behavior change (old -> new)

Path Old New
Passthrough-lane dsh compaction always refused 422 refused 422 only while any live session holds substrate; otherwise relayed verbatim (dsh's checkpoint lands)
Pipeline dsh compaction always refused 422 refused 422 only while the resolved session holds substrate; otherwise relayed verbatim

This is intentional and the point of the fix. When nothing is compressed, a landed dsh checkpoint destroys nothing, so relaying restores the last shrink backstop. The protective path (substrate present) is byte-for-byte unchanged.

Residual (documented): on the passthrough lane the exact target is unknowable, so the check is conservative — it refuses whenever any live session holds substrate. In a rare multi-profile case where only other sessions hold substrate (and the true target does not), it still refuses. That is the safe direction (never allows when the target could be protected); tightening it to the exact target would need a client-stamped session id (see Design gap below).

Not changed

  • Escape hatch: allowDshCompaction / BILI_ALLOW_DSH_COMPACTION (在bili网页配置界面支持关闭和开启dsh的自动压缩功能 #2028) still short-circuits first -> always relay. No behavior change there.
  • Config surface: none added/renamed/re-semantics'd. No new env var, no new config field.
  • Kernel boundary: no change under kernel/**.
  • Refusal shape/status: still 422, same body, same retryable:false.

Design gap left in place (not fixed here)

On the unattributed lane the server cannot identify the target session (the client can't stamp one — agentless call, #1117/#2709). A precise per-target check would require either routing compactions through session-attributed routing or a single-active-session fallback. This PR ships the conservative-sound version (any-substrate gate) which fixes the reported deadlock scenario without that mechanism; the precise-target refinement remains a follow-up.

Tests

  • New tests/issue3016-compaction-substrate-gate.test.ts: passthrough relay-no-substrate (core fix, byte-exact), passthrough refuse-with-substrate, pipeline relay-blocks=0, pipeline refuse-with-substrate (+ dsh压缩与bili压缩冲突 #2490 bytes recorded).
  • Updated pre-existing refusal fixtures that used fresh sessions (they would now relay instead of refuse): issue2709-passthrough-compaction-guard.test.ts (fix: keep system prompt byte-stable for prefix cache #1, Use kernel renderTags:"text-only" + forbid tag echoing in prompt #3), issue2028-dsh-compaction-toggle.test.ts, issue2490-ledger-floor-doomed-probe.test.ts (e2e-a) — each seeds a substrate so they keep pinning the protective path.
  • Verified locally: all six dsh-compaction test files green (93 tests) + typecheck clean + build green. Full regression -> CI.

Review tier

Touches identity/session-binding logic and wire shape (both modes reasoned) -> human-review tier, no auto-merge (AGENTS.md section 7.4).

A dsh native compaction call was refused unconditionally (422
dsh_compaction_refused) even when bili held no compression substrate
(state.blocks=0) — there was nothing to protect. On the unattributed
passthrough lane (x-bili-passthrough) this meant that when bili's own
nudge/decision channel was dead (#3004), the last remaining shrink
backstop was also disabled, pinning context at the wall until the hard
limit.

Gate the refusal on whether a substrate actually exists to protect:
- pipeline path (target resolved): relay verbatim when
  session.state.blocks.length === 0.
- passthrough lane (target unknowable — only x-bili-passthrough is
  stamped): refuse iff anyLiveSessionHasCompressionBlocks(); otherwise
  stand down and relay.

No new config/env surface; the existing escape hatch
allowDshCompaction / BILI_ALLOW_DSH_COMPACTION (#2028) still short-circuits
first. Updates the pre-existing refusal fixtures (which used fresh
sessions) to seed a substrate so they keep pinning the protective path.

Closes #3016
@github-actions

github-actions Bot commented Oct 13, 2026 •

Copy link
Copy Markdown
Contributor

📦 Built Package Artifact

Branch: 2026-10-13_dsh-compaction-substrate-gate (dd05d52)

Option A — Install this exact build (recommended)

npm install -g billion-context@0.0.3431-pr.3030.675f082

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-pr3030.tgz
npm install -g package

Install instructions are refreshed on each push.

ework-daemon and others added 2 commits October 13, 2026 19:48
…#3016 review)

The passthrough gate scans only the in-memory live-session map. Since #2803
(LRU session eviction) landed on master after this PR's base, a
substrate-holding session can be flushed out of memory and become invisible
to the scan. The previous wording ('disk-only sessions are not actively being
compacted' / 'Never allows when the target could have substrate') documented
a guarantee the merged code does not provide. Comments only; no behavior
change.
@ranxianglei

Copy link
Copy Markdown
Owner Author

🤖 Powered by ework · qwen3.8-27b

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

Review 结论:代码正确,已在当前 master 上验证全绿;原 CI 红灯是继承自 master 的 golden 断供(已由 #3035 修复),与本 PR diff 无关。分支已更新到当前 master,等新 CI 跑完即可合。

八问

  1. 是什么问题:dsh 原生压缩落在不可归因 passthrough lane 时被无条件 422 拒绝——即使 bili 自己的压缩基底已完全死亡(state.blocks=0)也照拒。当 nudge 通道同时死掉(dsh上下文700K不触发压缩 #3004 死锁场景)时,这最后一道缩容兜底也被禁用,上下文钉死在墙上直到硬限报错。守卫的本职(nudge 死锁无检测:同一 session 交替收到两种结构互斥的请求流(巨型单消息 ↔ 多轮)时不告警 #1729/在bili网页配置界面支持关闭和开启dsh的自动压缩功能 #2028)是阻止不可归因 checkpoint 遮蔽已存在的基底,而不是无差别拦截一切。
  2. 什么原因导致的:拒绝判定只看「是不是 dsh compaction 调用 + 逃生阀是否关闭」,从不检查「有没有需要保护的基底」——谓词里缺了 substrate 存在性这一项。
  3. 影响范围与严重程度:需同时命中两个条件才触发:nudge 通道死(dsh上下文700K不触发压缩 #3004)+ 上下文已涨大。触发时该会话失去最后的缩容手段(只能等硬限错误);基底存在的正常会话行为不变。属「特定条件下严重、触发面窄」。
  4. 怎么修复的:把拒绝判定门控在基底存在性上——pipeline 路径(可解析出 session)直接看 state.blocks.length === 0:无基底则逐字 relay,有基底才拒;passthrough lane(目标不可知)用新增 anyLiveSessionHasCompressionBlocks()(扫内存活 session map,不落盘查):任一 session 有基底则拒,否则 stand down 逐字 relay。保护路径(refusal 分支)字节级未动,在bili网页配置界面支持关闭和开启dsh的自动压缩功能 #2028 逃生阀仍最优先短路。
  5. 回归问题:逐项清点——拒绝形状(422/body/retryable:false)不变;保护路径字节不变;dsh压缩与bili压缩冲突 #2490 ledger 记录逻辑不变(relay 分支无拒绝发生故不记 metadata);旧行为 fixture(issue2709/issue2028/issue2490)已补种基底继续钉住保护路径。rebased head 上 6 个 dsh-compaction 测试文件 44/44 绿;合并当前 master 后重跑 70/70 绿(含全部 dsh-compaction 套件 + golden-storage-render),typecheck clean。新增一个 caveat 见下。
  6. 缓存破坏:不涉及。缓存/重试/增量游标/去重键均未触碰。
  7. 设计方向:不变。只是收紧守卫的判定条件,数据流与边界未转向。
  8. 新配置:无。未新增任何 env/config 字段。

行为变更披露(旧→新):passthrough dsh compaction:原一律 422 → 现仅当内存中任一活 session 持有基底时 422,否则逐字 relay;pipeline 路径:原可解析出 session 即 422 → 现 blocks==0 时逐字 relay。原因:恢复基底死亡时的最后缩容兜底(#3004 根因之一);有基底的保护语义完整保留。

Review 中新发现的残留风险(已写入代码注释):master 在 PR base 之后落了 #2803(LRU session eviction)——持有基底的 session 可能被逐出内存,passthrough 门的内存扫描就看不见它 → 一个窄窗口内、恰好瞄准被逐出 session 的 checkpoint 可能被 relay(#1729 类遮蔽)。窗口很窄(目标须持有基底 ∧ 已被逐出 ∧ compaction 在归因轮重新解析前经不可归因 lane 到达),pipeline 路径不受影响(getSession 会从盘重载)。已把两处夸大保证的注释改准(docs-only commit bed60a1a3,无行为变化),细节会同步到 #3016 线程跟踪。

CI 红灯根因说明:原 head 的 4 个 test leg 失败 = storage-render golden 的 metadata 段漂移,根因是 #3023 落 master 时未 regen goldens(其 commit message 自述:master-push CI 跳过 test leg,所以 master 静默红了一段时间),#3035(已并入 master)regen 后的新 pin 1f78ed1b… 与我们失败 CI 的实际产出值逐字节相同。本地对照实验:纯 master 与 master+本 PR 产出的 digest 字节一致 ⇒ 本 PR 的 6 个文件对 golden 零影响。

分支处置:已把 origin/master(含 #3035)merge 进 PR 分支并 push(merge commit dd05d52e2,非 force-push;另含上述 docs commit)。当前 mergeable: true,状态 blocked 仅因新 head 的 CI 还在跑。

收工判定:审查通过、定向验证绿,可合。此 PR 触碰 session-binding/wire-shape 判定,按 §7.4 属人工 review 层——请 @ranxianglei 手动合并:#3030

@ranxianglei

Copy link
Copy Markdown
Owner Author

🤖 Powered by ework · qwen3.8-27b

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

CI 全绿,收工确认:更新后的 head dd05d52e2 上全部 34/34 check runs success(含 4 个 test 腿),PR 状态 mergeable_state: clean,无任何 blocking。

补充两件已处置的事:

  1. eviction caveat 已记录到 dsh native compaction 在未归因 passthrough lane 被一律 422 拒绝——即使 bili 自身压缩基底已死(blocks=0) #3016 线程(与本 issue 的保守近似属同一机制族,不另立项);
  2. CI 流程洞(master push 跳过 test/static-gates 腿 → [Enhancement][cache-ledger] attribute client-side mid-history insertions to a client bucket — seam forensics currently blame bili for host churn (found in #2997) #3023 红着合入、master 静默断供、本 PR 背锅)已单独立项 master 推送时 CI 跳过 test/static-gates 腿:坏掉的 master 静默存活,直到无关 PR 撞上才暴露 #3037,带完整证据链与建议,供你拍板——与合并本 PR 无关,不阻塞。

最终判定:可合,等 @ranxianglei 手动合并:#3030

RodrigoHamuy pushed a commit to RodrigoHamuy/billion-context that referenced this pull request Oct 13, 2026
…ger client-insert attribution

ranxianglei#3023 (20c4258) added client-insert attribution to session.metadata.cacheLedger
(inbound-view fingerprints, bucket fields) but shipped without regenerating the
storage-render goldens: the metadata section digest drifted on all four wires
while state/blockContents/stats stayed identical. Its PR CI was red on the test
legs and was merged anyway, and master-push CI skips the test lane entirely
(38389227456: test=skipped, static-gates=skipped), so master has silently failed
tests/golden-storage-render.test.ts since 2026-10-13 18:55 (+0800).

Drift verified metadata-only by bisect (a1dba97 green -> 20c4258 red) and by
raw metadata diff (timestamps/uuids aside, only cacheLedger changed; canonicalize
already normalizes volatile values). Regen diff: 8 digest lines, 4 files.
storage-corpus pins untouched (they pin legacy-format parsing, not live render).

Also un-blocks PR ranxianglei#3030, whose CI failure is entirely this inherited master
breakage, not its own diff (verified: ranxianglei#3030 head alone is green locally).
@ranxianglei

Copy link
Copy Markdown
Owner Author

Request changes(owner 裁定,完整理由见 #3016 评论)

取证部分保留认可:422 复现链、跨会话证据(df251671 / 5f97a1dc / 6e00b0e6)、substrate 概念本身都对。被推翻的是修法:

  1. 调查方向重定位——事故的首要缺陷不是"拒绝器过宽",而是 dsh上下文700K不触发压缩 #3004 的抑制死锁让 bili 自己 4h12m 零压缩(PR fix: break the nudge-decide × self-heal suppression deadlock (Closes #3004) #3019 修复)+ fork usage 基线死区(fix: fork 子会话 nudge 全盲死区 — 收据播种 sizing 基线(0.1.195 卡死复现) #2936)。主通道活着时,blocks=0 用户按 compact 的场景基本不存在;主通道死了,正确解法是修主通道,不是放行客户端重写来绕过病灶。

  2. 放行违反顺位契约——bili 的折叠账本/prefix-affinity/缓存边界全部以消息顺位为权威(这是解决 [Bug][claude MITM] Threads 下 bili 压缩回路在 continue 轮次全面失效:nudge 失明、mid-turn compress 400(previous_message_id already continued)、折叠只能在 create 送达 #2701/dsh anthropic-messages lane bypasses native-intercept fetch patch: anonymous pfa-* session, fold ledger orphaned, silent ~182s re-fold on cross-protocol switch #2791/post-fold mid-history cache-seam byte forks without fold attribution (message[15]/[17]) #2757 的根基)。dsh checkpoint 是客户端单方面重写历史,即使 blocks=0,refs 失效与 cached prefix 报废照样发生;boundary/reconcile 是事故吸收器,不是有意重写的通行证。一旦落地,整个会话即坏,且不可逆。拒绝可逆(allowDshCompaction),放行不可逆——不对称风险只允许一个方向。

  3. 与 CC 侧刚立的规矩保持一致([PR] fix(#2612): refuse Claude Code native compaction on the wire — bili owns compression #2849:/compact 一律 403 + 逃生门):同一产品对"客户端自带压缩"只能有一个答案。

保留项:refusal 422 文案升级为诊断指引(为什么没在压缩/逃生门/上报提示),另行处理(#3016 剩余项)。

@ranxianglei

Copy link
Copy Markdown
Owner Author

按 owner 裁定关闭:放行方向违反顺位契约(见 #3016 评论)。取证保留在 issue 侧;422 文案升级另行处理。

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.

dsh native compaction 在未归因 passthrough lane 被一律 422 拒绝——即使 bili 自身压缩基底已死(blocks=0)

1 participant