Repository navigation
Judge fresh-session preflight fit on the calibrated estimate, not raw char count (#3146) - #3155
Conversation
📦 Built Package ArtifactBranch: Option A — Install this exact build (recommended)npm install -g billion-context@0.0.3605-pr.3155.619bd8eThis exact version is immutable and reproducible. The rolling Option B — Download artifact
tar xzf billion-context-pr3155.tgz
npm install -g packageInstall instructions are refreshed on each push. |
71a655f to
a40e05d
Compare
…t raw char count (#3146) An unknown-baseline session (no upstream usage yet — e.g. an anonymous/fork session with lastInputTokens=0) had its preflight "does the payload fit the window?" decided on the character count (estimateCoreMessagesUpper), not the calibrated token estimate. That is a unit mismatch: a CHARACTER count compared against a TOKEN window. The bound does fall as you fold (it is recomputed each round from the post-fold view), but at character rate — ~2x the real token rate for this content — so bili destroyed roughly twice the history needed to bring the number under the window, hit the raised round cap (16 -> 32) while the real token count was already well under, and then fail-fast 502'd. Incident #3146: 32 ranges folded, 2263796 -> 1111212 chars, yet 502; upstream actually billed 540828 of an 872000 window. The char-count bound remains the TRIGGER arm only (where erring early is harmless). Every fit decision now judges on the calibrated CJK-aware estimate (payloadEstimate): the per-round exit, both relax gates, the final fitsWindow, and the caller's early no-compressible-ranges gate. A genuinely over-window fresh session degrades gracefully instead of false-fail-fasting: it forwards once for the upstream to arbitrate size (#496/#1839/#2313 pattern), or fails fast 413 when the post-fold byte mass is mathematically doomed (#2490 byte math, hoisted module-scoped so the probe path shares it). Estimate-only fail-fast messages now say the size rests on bili's local estimate, not fact.
…rebase (#3146/#2952) Rebasing #3146's fix (fitsWindow now reads result.payloadEstimate for every regime) onto master invalidated #2952's comment claiming 'result.fitsWindow reads finalUpper'. finalUpper/currentTokens are still recomputed in the recovery path to keep processTurn's tokenCount and the over-window continue/failure signals fresh; the comment now says so.
a40e05d to
da09302
Compare
(ai: vllm-qwen/qwen3.8-27b) 独立审完这个 PR(不复读 issue 结论,自己重读了 diff、逐路径 trace 了合并后代码、本地定向跑过测试)。先说动作:分支之前落后 master 75 个提交,而 master 恰好在这两个热文件上又改了 +263(preflight)/+69(server)行,所以我把它 rebase 到当前 master(3aeddc1)、逐 regime 核过语义、本地 typecheck + 定向测试全绿后 push 了新头 da09302(现已触发新一轮完整 CI)。下面是八问。 1. 是什么问题:全新会话(还没有任何上游 usage 基线, 2. 什么原因导致的: 3. 影响范围与严重程度:只影响无基线/全新会话(匿名/fork)。已知基线会话一直用校准估算,完全不受影响。严重度:特定条件下功能不可用——新会话真实体积其实放得进时会被误 502 并白烧整轮折叠;不是全局崩溃,但会卡住这类会话的首次请求。 4. 怎么修复的:把字符上界降级为只作触发臂,所有 fit 判定统一改用校准的 CJK-aware 估算( 5. 是否有回归问题(逐项核过):
6. 是否破坏缓存:不涉及。不改前缀缓存、重试游标、去重键或增量策略;dead-end 冷却键(#726,基于客户端请求体 hash)未动。 7. 是否改了设计方向:没有转向架构/边界/数据流。只是把「误用的字符上界」退回它该在的位置(触发臂),fit 判据统一到已有的校准估算——让现有机制各归其位,不是新方向。 8. 是否引入了新的配置:无。没有新增 config 字段/env/CLI flag,也没有改名或改语义任何现有字段(PR 描述亦声明 Config surface: None)。 判定:可合。 方向正确、无回归、测试有效(重写后的 |
📦 Merged Build PublishedThis merge to master was published as npm install -g billion-context@0.0.431-master.b2868ca # this exact build
npm install -g billion-context@master # always the newest merged build
|
Closes #3146
What this fixes
A brand-new session with no upstream usage baseline yet (an unknown-baseline session — e.g. an anonymous/fork session where
lastInputTokens == 0) had its preflight "does the payload fit the window?" question answered using the raw character count instead of bili's calibrated token estimate. That is a unit mismatch: a CHARACTER count compared against a TOKEN window. The bound does fall as you fold (it is recomputed each round from the post-fold view), but at character rate — roughly 2× the real token rate for this content — so bili destroyed about twice the history needed to bring the number under the window, hit the raised round cap while the real token count was already well under, and only then fail-fasted 502.That is exactly the incident in #3146: 32 ranges folded, 2263796 → 1111212 chars, yet bili returned 502; the upstream actually billed only 540828 tokens against an 872000 window, and the next hop re-anchored fine.
Root cause
estimateCoreMessagesUpper()counts characters, not tokens. For an unknown-baseline session that value was used both to trigger compression (fine — erring early is harmless) AND as the reject-forward gate (wrong). Comparing char count to a token window inflated "how much must be destroyed to pass" by ~2×, so the loop ran to the round cap having over-folded, andfitsWindow(char < window) stayed false even though the real payload fit. The predicate was satisfiable all along — the cost was just mis-priced in the wrong unit.What changed
The char-count bound is now the trigger arm only. All fit decisions judge on the calibrated, CJK-aware estimate (
payloadEstimate):src/preflight.ts: the per-round exit, both soft-protection relax gates, and the finalfitsWindowall compareresult.payloadEstimateagainst the window. The char bound still pinscurrentTokensso the kernel keeps marking ranges compressible while folding, but it no longer boostsdecisionTokens— so the loop stops folding once the real size fits, instead of running to the cap on the inflated char figure.src/server.ts: the early "no compressible ranges" gate judges fit on the calibrated estimate for every regime (a fitting fresh session forwards instead of fail-fast 502'ing); the old char-countfailFast(502, "nothing left to fold")short-circuit for fresh sessions is removed in favor of falling through to the graceful path below.Note on the doomed gate: it is applied to the post-fold payload, deliberately not pre-fold. A pre-fold byte gate would false-positive on #3146 itself — the inbound (≈9.8 MiB) looks byte-doomed, but the payload folds down to 540828 tokens, which fits the 872000 window. Only a residual that stays over after maximal folding is genuinely doomed.
Behavior change (old → new)
Known-baseline sessions are unchanged (they always judged on the calibrated estimate). Residual: a fresh payload far denser than any observed live route (~6.8 B/token worst case) may 400 once and recover via the overflow-shrink arm rather than slow-compress-to-fit — the same tradeoff chosen for #3146.
Config surface
None. No new config fields, env vars, or CLI flags; no existing field renamed or re-semantics'd.
Testing
tests/preflight-fork-estimate.test.ts: the mock now enforces the window on a realistic tokenizer matching bili's own CJK-aware estimator (the old mock counted every character as a token — the pathological regime [Bug] 新会话(无 usage 基线)preflight 把字符数当 token——折叠其实成功(2263796→1111212)却 fail-fast 502,上游实际只计费 540828 #3146 deliberately stops defending via slow compress-to-fit). Asserts a fitting fresh session forwards 200 with zero wasted summarization rounds.tests/preflight-covered-ranges.test.tsunknown-baseline case to genuinely overflow the window so it still pins the relax path.preflight-*,issue*preflight*,issue*doomed*,issue*probe*,fork-nudge-*, plus the nobaseline/anonymous-anchor seams. Typecheck + production build clean. Full-suite verdict comes from CI on the pushed head.Review note
Not auto-merge eligible (§7.4): this changes wire-judgment semantics and a fallback/default decision path in the hot file
src/server.ts, and touches the preflight fit contract — human review requested.