Skip to content

fix(preflight): trust only upstream-evidenced baselines in the entry meter (#2313) - #2316

Closed
ranxianglei wants to merge 3 commits into
masterfrom
2026-10-07_preflight-estimate-baseline-trust
Closed

ranxianglei wants to merge 3 commits into
masterfrom
2026-10-07_preflight-estimate-baseline-trust

Conversation

@ranxianglei

Copy link
Copy Markdown
Owner

Partially addresses #2313 — the metering defect behind the 12-hour stuck-preflight incident, plus the operator-recovery-lever documentation. The core deadlock-breaker mechanism (forward-once probe vs livelock counter) is presented as a design question at the end and awaits owner direction, so this PR references #2313 without closing it.

What changed

src/preflight.ts — two provenance/scale defects at the preflightCompress entry:

  1. Ungated entry meter. The round-0 kernel-view baseline trusted lastInputTokens regardless of source, while every other decision site in the request path gates on usage/overflow-arm provenance (emergency shrink / preflight 的本地估算用错了视图:超长会话(客户端历史 127 万,压缩后 outbound 仅 ~16 万)在上游失败无 usage report 时,bili 按原始历史估算 #1492). An estimate-sourced value is a one-way ratchet written by failed turns and post-rebuild anchors. In the incident it pinned the round-0 view at ~10.3M phantom scale for 12 h while the folded view the gate actually judges drained underneath. Now gated (baselineTrusted), with the empty-input exception mirroring the per-round floor (a transform-failed outbound IS raw).
  2. Depth budgets scaled off the raw inbound list. The DSH: 切换模型后,preflight 在窗口远未满时提前触发压缩 #1933 F3 overshoot scaling measured estimateCoreMessages(messages) over the full re-sent history — which stays at full-history scale forever because prior server-side folds hide content behind blocks. Budgets are now decided at first measurement on the folded view being judged; a payload that already fits never pays for the decision.

Tests — tests/issue2313-preflight-entry-meter.test.ts: unit-level direct preflightCompress runs shaped like the incident (prior folds hiding most history + estimate-sourced ratcheted lastInputTokens). Verified against stashed old code: the two phantom-scale tests fail there, the trusted-floor test passes both sides (legacy cap preserved).

Docs — CONFIGURATION.md en+zh: compress.modelContextLimit / outputHeadroomMaxPct as recovery levers for a stuck fold loop. Fact-checked: the headroom reservation applies to pinned windows too (reserveOutputHeadroom call site in src/server.ts), so both levers are documented rather than just the pin.

Behavior changes (old → new)

  • Preflight entry meter (round-0 kernel tokenCount): raw lastInputTokens regardless of source → gated on usage/overflow-arm source (estimate-sourced reads as unknown/0). Only sessions with estimate-sourced baselines are affected; usage/overflow-arm sessions are unchanged.
  • Depth-budget decision timing: pre-loop on the raw inbound list → at first measurement on the folded view. Fitting payloads: no raise log either way (identical). Genuinely-over payloads: same formula, honest input.
  • The [preflight] payload ~N tok warn log now reports folded-view scale instead of phantom scale (log-wording change — this was the line that misdiagnosed the incident).

No wire/message-shape changes, no config schema changes, no persistence changes, no kernel/ changes.

Config surface

None added. Docs-only note about existing fields (compress.modelContextLimit, compress.outputHeadroomMaxPct).

Pre-flight

typecheck clean; targeted run of all 29 preflight-related suites: 149/149 pass; new suite 3/3 pass (and discriminates old code via the stash check). Full regression delegated to CI per local-test discipline.

Core deadlock-breaker — design question for owner

This PR removes the metering lie that made the incident's numbers uninterpretable and kept the budgets capped while the real payload was near the wall. If client patience still runs out before folding converges at honest scale, the structural fix is one of:

Plus human-gated calibration clamp widening (CALIBRATION_CLAMP_MIN 0.25, src/cache-ledger.ts) — necessary but not sufficient alone (learning needs a forward).

结构化七问(中文结论)

  1. 是什么问题:长会话的预检折叠循环卡死——入口计量器读到一个折叠永远降不下来的幻影值(12 小时钉在 ~10.3M),而真实载荷其实已在窗内附近。
  2. 什么原因导致的:lastInputTokens 是失败回合与重建锚写下的单向棘轮(source="estimate"),但 preflight 入口计量器没做来源门控(请求路径上其他所有决策点都门控,emergency shrink / preflight 的本地估算用错了视图:超长会话(客户端历史 127 万,压缩后 outbound 仅 ~16 万)在上游失败无 usage report 时,bili 按原始历史估算 #1492);DSH: 切换模型后,preflight 在窗口远未满时提前触发压缩 #1933 F3 的深度预算又按原始入站列表(永不下降的全历史尺度)缩放。两层叠加:计量说谎 + 预算按谎言缩放。
  3. 怎么修复的:入口计量按来源门控(只用 usage/overflow-arm 证据);深度预算改为在首轮测量后、按被折叠视图的真实规模决定;已适配载荷不付决策成本。
  4. 是否回归问题:逐项审过既有行为——usage/overflow-arm 会话行为逐字节不变(测试 B 双侧通过钉住);已适配载荷无新日志、零额外调用(测试 C);fail-fast 语义不变(诚实的 exhausted 仍报 exhausted);[Bug] Codex 额度中断后恢复长会话,preflight SSE 摘要为空并持续 502 #726 冷却、模型压缩输出是输入的数倍 #1841 futility 门、出现持续压缩失败,无法自动恢复 #847 子最小范围剔除均未触碰。
  5. 是否破坏缓存:不涉及——未触碰持久化格式、消息引用空间、块去重键或任何增量游标。
  6. 是否改了设计方向:不涉及架构转向;只把入口计量对齐到既有的来源门控纪律(emergency shrink / preflight 的本地估算用错了视图:超长会话(客户端历史 127 万,压缩后 outbound 仅 ~16 万)在上游失败无 usage report 时,bili 按原始历史估算 #1492)。核心死锁破除机制(探测转发 vs 活锁计数)作为设计问题留给 owner 拍板,未擅自实现。
  7. 是否引入新配置:无。仅文档化既有字段(modelContextLimit / outputHeadroomMaxPct)作为卡死恢复杠杆(en+zh)。

判定:本 PR 可合并(单模块、纯 fix+test+docs、带回归测试、全绿于当前 master head);#2313 的核心机制待 owner 选定方向后再立后续 PR。

ework-agent added 3 commits October 7, 2026 15:08
The preflightCompress entry meter trusted lastInputTokens regardless of
its source, while every other decision site in the request path gates it
on usage/overflow-arm provenance (#1492). An estimate-sourced value is a
one-way ratchet written by failed turns and post-rebuild anchors — in
incident #2313 it pinned the round-0 kernel view at ~10.3M phantom scale
for 12 h while the folded view the gate actually judges drained
underneath, and the #1933 F3 depth budgets scaled off the same phantom
(raw inbound list) instead of the view being folded.

- entry meter now gated on lastInputTokensSource (usage/overflow-arm);
  the empty-input exception mirrors the per-round floor (a
  transform-failed outbound IS raw)
- depth budgets decided at first measurement on the folded view; a
  payload that already fits never pays for the decision
Unit-level direct preflightCompress runs shaped like the incident: a
session whose prior folds hide most history behind blocks, with an
estimate-sourced ratcheted lastInputTokens. Proves the new code raises
depth budgets on honest scale (~1.x, uncapped) and fits the window,
stays silent for payloads that already fit, and keeps the legacy cap on
the trusted-floor path. Verified against stashed old code: the two
phantom-scale tests fail there, the trusted-floor test passes both sides.
)

compress.modelContextLimit / outputHeadroomMaxPct as operator levers
when a long session re-folds without converging before the client gives
up; en+zh. Fact-checked: the headroom reservation applies to pinned
windows too (reserveOutputHeadroom call site in src/server.ts), so both
levers are documented rather than just the pin.
@github-actions

github-actions Bot commented Oct 7, 2026

Copy link
Copy Markdown
Contributor

📦 Built Package Artifact

Branch: 2026-10-07_preflight-estimate-baseline-trust (1cf34e1)

Option A — Install this exact build (recommended)

npm install -g billion-context@0.0.2449-pr.2316.03efc33

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-pr2316.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

[bot] 🏷

PR Review: #2316 — provenance-gate the preflight entry meter (#2313)

验证方式:拉取分支 2026-10-07_preflight-estimate-baseline-trust @ 1cf34e171(base 74cf5246d == 当前 master,无漂移),逐行读 diff 并对照周边代码推理两种压缩模式下的行为;本地定向验证(新套件 + 全部 28 个 preflight 套件 + typecheck + build + 新旧代码 A/B);轮询 CI 至全量出结果。

结构化七问

1. 是什么问题:preflight 入口计量器(round-0 kernel 视图 currentTokens + #1933 F3 深度预算缩放)读的是幻影尺度——事故 #2313 中入口读数钉死 ~10.3M 长达 12 h,而真实折叠后载荷已贴近窗口,导致每次 preflight 调用都空转折叠窗口根本不需要的 range,直到客户端放弃。

2. 什么原因导致的(两个机制,均已在代码中核实):

3. 怎么修复的:入口基线改用与同函数内 per-round floor 相同的来源谓词门控(空输入例外镜像 floor:transform 失败的出站即是 raw);预算决策从循环前移到首次实际管线测量处,改在折叠视图(payloadEstimate/finalUpper)上计算。公式不变([base, 2×base] 封顶)。已核实的正确性细节:decisionTokens 跨轮单调不增(折叠只减、floor 调用内恒定),故 round-0 是定预算的正确最坏时刻;循环上界变异自 round 1 起可见;除"测量前 abort"外无路径绕过决策块。

4. 是否有回归问题:逐项审过 diff 触碰路径的既有行为:

5. 是否破坏缓存:不涉及。无线变化;skip-set 键仍是 content-fingerprinted refs;transient-retry 预算逻辑未动;prefix-cache 稳定性不受影响。summary-call 预算语义不变,只是输入变诚实。

6. 是否改了设计方向:不涉及架构转向。本修复是把入口计量器拉回既有的 #1492 provenance 纪律与 #1933 F3 意图("预算随实际剩余工作量缩放")。更深的死锁破局机制(forward-once probe / livelock counter / CALIBRATION_CLAMP_MIN clamp 放宽)刻意保持 out-of-scope,仍作为 #2313 的开放设计问题等 owner 拍板。

7. 是否引入了新的配置:不涉及。仅文档新增:CONFIGURATION.md en + zh-CN 的 modelContextLimit 条目下加恢复杠杆指引(看 [headroom] 日志;pin compress.modelContextLimit 和/或 outputHeadroomMaxPct=0)。无新 env/config/默认值。

测试有效性(质量关)

三个测试各钉一个独立行为:T1 estimate 级幻影不得封顶预算 + 读数落在折叠尺度(~1.2x 正则);T2 usage 级基线仍 floor(legacy 上限保留);T3 收敛载荷零成本(无 raise 日志、0 调用、0 折叠)。fixture 真实(block 隐藏 raw 历史 + 可折中段 + 保护尾部)。非恒真断言——A/B 证实它们在旧代码上恰好按预期失败。

diff 整洁度

干净:4 文件,每行都与目的相关(fix / 回归测试 / 双语档同步),无空白重排、无 lockfile、无生成物。无配置面变更。

热文件提示(§7.2)

#2310、#2311(Responses summary 提取修复)也动 src/preflight.ts——区域不同(提取路径 vs 入口计量/预算),语义冲突风险低,但后合并者请先 rebase 到当前 master。

判定

可合。 代码正确、最小化、测试有牙、行为变更全部披露;CI 除上述 master 既有红灯外全绿。建议:合并 #2316;unused-export 清理走一个带标注的一行小修(可搭 #2310 的车——它本来就动 launcher.ts,作为独立标注 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.

1 participant