Repository navigation
fix: chain-detection content fallback exempts this instance's own sessions (#1086) - #1090
Conversation
📦 Built Package ArtifactBranch: Option A — Install from npm PR tag (recommended)npm install -g billion-context@pr-1090Each push to this PR publishes a new version under the Option B — Download artifact
tar xzf billion-context-pr1090.tgz
npm install -g packageInstall instructions are refreshed on each push. |
[bot] 🏷 Re-reviewed #1090 after pulling the branch. Verdict: mergeable — the code matches the #1086 spec and every check is green; I fixed one small docs defect directly on the branch. Code review (against the #1086 repro) — correct and complete across all four layers:
Verification:
Small defect fixed on the branch (review discipline: fix small problems directly, don't ping-pong): the two newly-added env-var table rows in 中文摘要:修复了 #1086 中链检测把本实例自身注入的 ACP 产物误判为 bili→bili 链、导致压缩内核整段停摆的问题——按已解析的会话身份区分"本实例自有会话"(正常压缩)与"外部接入请求"(原样透传+一次性警告),并保留 Per repo rules the merge itself is human-only — please merge when ready: #1090 |
[bot] 🏷 Follow-up: CI on the new docs-only head |
ranxianglei
left a comment
There was a problem hiding this comment.
审查结论:MERGE 级(人审合并;建议 F1/F2 各开 follow-up issue)
完整深审(代码逐层 + v0.1.133 旧检测器对拍 + 全部 7 个审查问题直答)后结论:可以合。要点:
已验证正确的核心
- 4 层保真齐全,层(b)(artifact 结构性检测)比 PR body 承诺的 count-heuristic 更强;wire 纯读不改(
chain-artifacts.ts建 Set、链式路径bodyBuffer逐字节透传prepared:null,T3 断言 sha 一致)。 - TOCTOU 安全方向正确:false-"chain" 只导致 passthrough,永不丢数据;state 只由 processed 请求创建。
- 免疫不变量在代码里成立:hop 标记只在
prepared !== null时打(src/server.ts:3785→buildForwardTarget:3324),stats.requests只在 processed 路径递增 → 内层 bili 转发体必带 hop,外层无 state → content fallback 正确触发。 - 测试可证伪:T2/T1 对
a40fe47:src/server/chain-sig.ts:58-63(v0.1.133 全文 substring 匹配、无豁免)复现了原 bug。diff 卫生:7 文件 2 提交、无 version bump、hex 转义、zh/en 同步。
发现(按严重度)
- F1 [Medium]
BILI_PERSIST=0重启即永久链式误判(src/session.ts:340-341if (!store.enabled) return false):持久化关闭的实例重启后唯一自证证据被清空,tag 历史(plugin 客户端会回放)被永久 chain-flag,且 passthrough 不建 state、永不自愈——正是 #1086 的无界增长症状,只是范围缩小到 persistence-off 重启。默认 persistence-ON 大多数用户安全。建议 follow-up:store 禁用时内容 artifact 不作为决定性证据(跳过 content fallback,hop 保留)。 - F2 [Medium] 磁盘分支零覆盖:4 个集成测试全部
_setStoreForTest({enabled:false})——store.loadSync重启路径(注释自称覆盖 auto-update restart)、BILI_CHAIN_CONTENTenv 解析、warn-set FIFO 淘汰都没测。建议 fast-follow:persist→flushAllSessions→_resetSessionsForTest→重放 artifact body→必须 process。 - F3 [Medium 固有取舍] 迁移/换代理误报:bili-A 死亡后带 tag 历史迁到 bili-B 不可区分于剥头链;首请求就含用户粘贴的真形 tag(#1039 先例)同样误判 → 永久 passthrough、压缩静默关闭。缓解已到位(严格 regex + 每会话一次 warn 带 knob 提示 + 逃生阀),README troubleshooting 值得加一行。
- F4 [Low 已知边界] 首请求剥 hop 且无 artifact 的真链会被外层 processed → 双压缩;0.1.132 hop-only 同洞,非回归。
- F5 [Low] 请求路径同步 IO:仅 byte-seed 命中+内存 miss 后触发;外会话两次 ENOENT stat。可接受。
- F6 [Nit]
loadSync在 #408 负 token 钳制时scheduleSave——"只读"探针带写副作用;沿袭既有行为,无害。
按 AGENTS §7.4 本 PR 改 wire 行为(chain 判定)须人审合并——与计划一致。建议合并后把 F1、F2 各开 issue。
|
@ework — please re-review the current head f3cdf65 in full. Additional context since your last pass: an independent deep review is at #1090 (review) (verdict: merge-grade, F1–F6 findings; F1/F2 filed as #1100 / #1101). Re-verify against those findings and the follow-up issues before human merge. |
[bot] 🏷 Full re-review of head Base & gates
Independent verification of each finding
Core invariants re-confirmed by reading the code: identity resolved before the verdict ( Recommendation 中文摘要:逐条对照独立深审的 F1–F6 并亲自读取被引用的代码复核——F1( Per repo rules the merge itself is human-only: #1090 |
…sions (#1086) v0.1.133's content fallback judged bili's own injected ACP artifacts (render tags / acp_status+search_context tool surface) as bili->bili chain evidence and passed every turn through verbatim, so the compression kernel never ran again on single-instance setups (#1086). Re-introduces the fallback correctly after #1087's revert: - structural detector: declarations alone are not artifacts; historical tool calls (both names, all four wire shapes) or wire-escaped render tags are; prose never counts - self-state exemption: processed local state for the session (memory or persisted store) means the artifacts are self-produced, process normally - foreign artifacts without local state: byte-identical passthrough, one warn per session, no session record created - escape valve: chainContentDetection:false / BILI_CHAIN_CONTENT=0
…1100) The chain-detection content fallback (#1086) decides "no local processed state => foreign bili->bili chain => passthrough". That inference is only sound when persistence can prove ownership across a restart: with BILI_PERSIST=0 an instance cannot recover its session state after a restart, so "no local state" is ambiguous with our OWN replayed session. Treating it as decisive made a persistence-off instance permanently pass-through every own session after a restart — compression silently off and unbounded growth, i.e. the #1086 symptom again, scoped to persistence-off + restart + replayed history. Gate the fallback's byte pre-filter on getStore().enabled: when the store is disabled the whole content fallback is skipped and the request is processed normally. The x-bili-hop marker path is untouched and still catches real chains. hasProcessedState already returned false in exactly this case, but that boolean cannot express "decline to make a verdict", so the decision belongs at the point where the fallback is gated. Tests: T3 (foreign => passthrough) now runs an ENABLED store over an empty temp dir so "no local state" is a sound foreignness signal; new T6 asserts the BILI_PERSIST=0 + restart replay is processed, not passed through. Closes #1100
…et FIFO eviction (#1101)
ec384a2 to
60cfaa7
Compare
|
Rebased onto current master ( Rebase notes:
Gates on the rebased head: typecheck ✅ · full suite 2230 pass / 0 fail / 2 skipped (E2E gates) ✅ · build ✅. |
[bot] 🏷 Re-reviewed the rebased head Rebase hygiene (§7.2's #1 rework driver — checked first)
Folded commit #1100 (
Folded commit #1101 (
Gates on
中文摘要:对 rebase 后的 head Per repo rules the merge itself is human-only: #1090 |
…le audit log (#1082) Rebased onto master v0.1.139 (b2f28c6): adopt createStorageCodec({key}) in tests (codec rename from the #1083 era), union #1090 hasProcessedState beside dropSessionForGc, keep master's updated BILI_ENCRYPTION_KEY doc row (drop pre-#1083 wording). Squashed of the five prior commits.
Closes #1086, #1100
What happened
v0.1.133 (#1079) added a content fallback for bili→bili chain detection: when the body carries ACP artifacts (render tags or the ACP tool names), treat the request as an already-compressed chain payload and forward it verbatim — meant for middleboxes that strip
x-bili-hop. Regression: those artifacts are exactly what bili itself injects/surfaces for its own managed clients (the opencode-native plugin registersacp_status/search_contextin the client tool surface; clients re-send render tags verbatim). Single-instance setups therefore judged every turn a "chain", passed everything through, and the compression kernel never ran again → long sessions hit the upstream token limit with HTTP 400s. #1087 (v0.1.134) reverted the pair wholesale; this PR re-introduces the fallback correctly.Design (4 layers)
src/server/chain-artifacts.ts, new): allocation-free byte pre-filter, then structural verdict — tool declarations alone are NOT artifacts (that is the normal shape of a bili-managed client); historical tool calls of both names (all four wire shapes: OpenAItool_calls[].function.name, Anthropictool_useblocks, Responsesfunction_callitems, GeminifunctionCallparts) or wire-escaped render tags ARE artifacts; prose never counts (JSON escaping defeats even the byte seed).src/session.ts:hasProcessedState): if THIS instance holds processed compression state for the session (memory first, then the persisted store — covers auto-update restarts:stats.requests>0 || nextBlockId>1 || blocks>0), the artifacts are self-produced ⇒ process normally. This is what v0.1.133 lacked: it judged before binding the session identity.x-bili-hoppath. Applies only when the store is enabled (see below); with persistence off the fallback is skipped entirely.chainContentDetection: falsein the config file /BILI_CHAIN_CONTENT=0env (env wins), documented in CONFIGURATION.md / CONFIGURATION.zh-CN.md. Thex-bili-hopsignal is unaffected by this switch.The decision site is protocol-neutral (shared path before per-wire processing), so all four wire protocols get identical behavior.
Persistence-off instances (#1100)
The foreign verdict's "no local state ⇒ foreign" inference is only sound when persistence can prove ownership across a restart (
loadSyncrestores disk state). WithBILI_PERSIST=0an instance cannot recover its sessions after a restart, so "no local state" is ambiguous with our OWN replayed history — and a decisive passthrough there would permanently disable compression for every own session after a restart (the #1086 symptom again, scoped to persistence-off + restart + replayed history). The fallback's byte pre-filter is therefore gated ongetStore().enabled: when the store is disabled the whole content fallback is skipped and the request is processed normally. Thex-bili-hopmarker path is untouched and still catches real chains. (hasProcessedStatealready returnedfalsein exactly this case, but that boolean cannot express "decline to make a verdict", so the decision belongs where the fallback is gated.)Why not just keep the revert
Without the content fallback, a middlebox that strips
x-bili-hopfrom a chained request silently re-enables double compression (the #292-class corruption the header exists to prevent). The protection stays; the misjudgment is fixed at its root (session identity + processed-state check + ownership-provability gate), not by removing the signal.Residual corners (documented, bounded)
loadSyncreturns null ⇒ treated as foreign ⇒ passed through (warn emitted). Narrower than before — the deterministic persistence-disabled case is now handled above ([Bug] BILI_PERSIST=0 重启后自己的会话被永久判为 bili→bili 链(#1086 残留,#1090 随附) #1100); only an actively-GC'd live record remains.Evidence
npm run typecheckclean;npm test: 2171 pass / 0 fail / 2 skipped (e2e-gated);npm run buildOK.tests/issue1086-chain-self-exemption.test.ts(9 tests): detector units across all four wire shapes + declaration-only (group E repro) + placeholder/prose negatives; T1 self-exemption under incident-shaped traffic (kernel rebuilt the body, zero[chain]warns); T2 fresh plugin-mode session processed; T3 foreign session byte-identical passthrough + single throttled warn + no session trace (now runs an enabled store over an empty temp dir, so "no local state" is a sound foreignness signal); T4 escape valve; T5 liveness guard — 60-turn openai-chat SSE loop against a relay that demands compress once context grows, asserting ≥1 kernel compress round-trip executed, bounded upstream bytes, and zero chain warns (the exact failure mode of the issue can no longer regress silently); T6 ([Bug] BILI_PERSIST=0 重启后自己的会话被永久判为 bili→bili 链(#1086 残留,#1090 随附) #1100) —BILI_PERSIST=0+ cleared memory (post-restart shape) replaying incident-shaped artifacts asserts the request is PROCESSED (kernel rebuilt the body, session created, stats advanced) NOT passed through verbatim, and that no chain warning fires (ownership unprovable ≠ confirmed-foreign). T6 fails on the pre-fix tree (passthrough) and passes post-fix.resolve()verified read-only apart from TTL expiry), forward() contract parity with the existing opencode v1: keep opencode-acp legacy sessions working after switching to bili #920 early-return bypass, first-request concurrency, per-request cost, config precedence vs sibling flags, style/compliance greps (hex escapes, noas any, version field untouched, no lockfile churn).Auto-merge gate
Not auto-merge eligible: adds a config schema key and changes wire pass-through behavior (both compression modes). Human merge required.
修了什么问题:链检测的内容回退(#1079/v0.1.133)把 bili 自己注入的 ACP 产物(渲染标签、acp_status/search_context 工具面)误判为 bili→bili 链,导致单实例部署压缩内核永久跳过、长会话撞上游 1,048,576 上限报 400(#1086)。本 PR 在 #1087 整体回退的基础上正确重新引入该回退:结构性产物判定(声明≠产物)+ 本实例已处理状态豁免(内存/持久化存储)+ 外部载荷字节级透传且每会话仅告警一次 + 逃生阀
BILI_CHAIN_CONTENT=0。另修复深审发现的 F1(#1100):BILI_PERSIST=0的实例重启后无法自证会话所有权,原逻辑会把回放历史的自有会话永久判为链而透传、压缩静默关闭——现将内容回退整体门控在getStore().enabled上,持久化关闭时直接走正常处理(x-bili-hop路径不受影响)。回归测试含 60 轮长会话活性守卫(T5)与持久化关闭重启场景(T6)。可以合并。