Skip to content

[PR] Phantom over-limit meter after a successful fold (289%/564% ghost) — plain folds now stamp the #1820 rebuild anchor | 折叠成功后压力表读数鬼影(289%/564% 假超限)— 普通折叠也打 #1820 重建锚 (#3573) - #3586

Closed
ranxianglei wants to merge 3 commits into
masterfrom
2026-10-17_fold-anchor-ghost-meter

Conversation

@ranxianglei

Copy link
Copy Markdown
Owner

(ai: zhipuai-lb/glm-5.3)

一句话

巨型会话折叠成功之后,压力表(usage%)反而冲到 289% 甚至 564%,把刚折叠完的会话当「超限」处理:注入紧急压缩提示、发起预压缩马拉松,而真实上下文其实只有 2%——老会话「升级后每轮卡死」的直接推手之一。

机制链(根因)

  1. 折叠完成后,内核把「本次省下的 token 数」从会话基线里扣掉(src/stream.ts 的 Plugin path has no per-request cache usage log — post-fold 'cache drops to 0' cannot be attributed #695 折扣网络)。当省下的量远大于基线时(基线是旧的小数字:升级切换后没报过 usage、或上游从不报 usage),Math.max(0, 基线-省下) 直接钳到 0——基线被抹掉。
  2. 下一轮 prepare 时 effectiveTokenCount(src/server.ts:2193)的阶梯发现没有可用基线,一路回落到「按重发历史字符数估上界」(estimateCoreMessagesUpper)——这个口径比真实 token 数高 2-4 倍。
  3. 于是刚折叠掉 190 万 token 的会话被读成 289%(实测 rig)/564%(用户现场),点燃紧急 nudge(INJECT T1)和预压缩马拉松;真实上游 60 秒不回,usage 永远报不回来,基线永远缺失,循环到客户端断连。

为什么 #1820 没拦住

#1820 的 post-rebuild 锚只覆盖 preflight 重建(src/server.ts:3340),普通折叠(工具执行的 compress)从来不打锚——这正是本 PR 补上的洞。

修法(16 行)

折叠成功的收口处(src/stream.ts,所有工具执行折叠的唯一咽喉:插件 lane / 代理模式 / 预压缩共用)本来就在算 postCtx = 折叠前视图 − 本次压缩量(与 [acp-compress-obs] shrink 日志同口径)。现在把它打进 #1820 锚:setPostRebuildAnchor(session, postCtx)。

测试

  • 新增 tests/issue3573-fold-anchor-ghost-meter.test.ts 2/2 绿:①鬼影形状(基线被钳 0)下,折叠后读数=锚=折叠后余量(<窗口),修复前同断言红(已验证 stash 复测);②健康会话 usage 级基线仍然压过锚。
  • 亲缘套件全绿:compress-observability / issue1820-post-rebuild-anchor / host-usage-postfold / compress-recovery-ranges / cache-proof / issue1492 / issue1569 / issue1839 共 118 过 0 挂。
  • typecheck + build 干净(build 门 dist-external-zod OK)。

归属

Fixes #3573

…ld meter no longer ghosts >100% (#3573)

A fold that out-sizes the session meter (stale baseline after an upgrade
cutover, a never-reporting upstream, or the first fold of a raw-replayed
giant history) drives the #695 credit net below zero and clamps the
baseline to 0. With the baseline annihilated the next prepare falls
through the effectiveTokenCount ladder to the resent-history char-count
bound — 2-4x the real token mass — so a session that JUST folded 1.9M
tokens meters usage=289% (field report: 564%) and fires a phantom
EMERGENCY nudge / a preflight marathon into an already-folded context.

Plain folds now stamp the post-rebuild anchor with the fold's post-fold
view size (postCtx, same kernel-count space the [acp-compress-obs]
shrink line reports) — the exact #1820 post-rebuild semantics extended
from preflight rebuilds to tool-executed folds. A real usage report
supersedes it immediately (settleUsageReport clears it) and the bounded
prepare budget (3) falls back to legacy sizing for never-reporting
upstreams, so no behavior changes for healthy sessions.

Fixes #3573
@github-actions

github-actions Bot commented Oct 17, 2026 •

Copy link
Copy Markdown
Contributor

📦 Built Package Artifact

Branch: 2026-10-17_fold-anchor-ghost-meter (280186d)

Option A — Install this exact build (recommended)

npm install -g billion-context@0.0.4243-pr.3586.0db2bc3

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

Install instructions are refreshed on each push.

…review supplement)

Two review supplements on the ghost-meter fix, same failure domain:

- Degenerate full-coverage folds (#1911 marathon strategy) leave postCtx === 0;
  setPostRebuildAnchor no-ops on <=0 so no anchor was stamped and one ghost
  decision survived. Clamp to Math.max(1, postCtx) — the preflight fit gate
  stays the hard backstop.
- The field's 564% shape traveled the min(staleEst, raw) ladder branch via a
  one-turn-lagged localInputEstimate; the original suite only covered the
  est=0 blind case. Add a stale-estimate test (anchor branch outranks it) and
  a full-coverage fold test pinning the zero-edge.
@ranxianglei ranxianglei changed the title fix: 折叠成功后压力表读数鬼影(289%/564% 假超限)— 普通折叠也打 #1820 重建锚 (#3573) Phantom over-limit meter after a successful fold (289%/564% ghost) — plain folds now stamp the #1820 rebuild anchor | 折叠成功后压力表读数鬼影(289%/564% 假超限)— 普通折叠也打 #1820 重建锚 (#3573) Oct 17, 2026
@ranxianglei

Copy link
Copy Markdown
Owner Author

🤖 Powered by ework · qwen3.8-27b

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

查重: 不重复。 open+closed 检索无同机制在途项。最近邻四个均不同:#3590(同一现场的另一半——预压缩马拉松时长/切片,症状与机制都不同,#3573 已单列)、#3316(线形翻转下的折叠历史重锚定——对账侧)、#3353(拒绝覆盖回退的折叠——净收缩门)、#2725(陈旧基线活得比测量上下文久——同子系统但方向相反:基线活太久 vs 本条基线被抹零)。注意 #2725 与本 PR 都动 src/server.ts 的基线/锚面,若先后合入需留意语义交互(见回归审查第 7 条)。

审核独立性说明:以下全部是对 diff 与代码路径的独立复核,不沿用 PR 描述结论;红绿复测是我自己跑的,不是引用作者声称。另交代一点:本次唤醒的上一段会话已在 PR 分支上推了补充 commit 72bb201ec(零边界 + 现场 564% 形状测试)但未发审核回复即结束——本条审核覆盖两个 commit(cf80a1eb0 + 72bb201ec),补充 commit 的每一处我也独立验过。

① 方向关:根因判断成立,修的是生产者不是症状

逐环独立核验,三条全中:

  1. 钳零机制属实:src/stream.ts:1100 lastInputTokens = Math.max(0, lastInputTokens - r.tokensCompressed)(Plugin path has no per-request cache usage log — post-fold 'cache drops to 0' cannot be attributed #695 折扣网络),省下的量大于陈旧小基线时基线被抹到 0——这是鬼表的生产者,不是症状。
  2. 阶梯回落属实:effectiveTokenCount(src/server.ts:2193)顺序 = usage 级基线 → postRebuildAnchor → raw 字符上界 → lastUsageGradeTokens → 匿名 pfa → min(localInputEstimate, raw)。基线归零后,下一轮决策读到的正是上一轮折叠前的 raw 字符量(localInputEstimate 每轮 prepare 记录、决策发生在同轮赋值之前,天然滞后一轮)——与 v0.1.198 升级后老会话丢失压缩状态:197 折叠过的会话以 117 万 token 原文重放,预压缩超过客户端耐心,老会话看起来「炸了」 #3573 floor 10 rig 实测 12125638≈11.8MiB 字符数、用户现场 564% 同签名。
  3. 咽喉判断属实:全仓所有工具执行折叠的提交点都在 applyRanges(stream.ts:47 / stream-openai.ts:10 / stream-responses.ts:17 / loop/core.ts:360 插件管理调用 / preflight.ts:1751+2047 / external-summary-compress.ts:43,45,101)——单一收口,插件 lane 与代理模式拿到完全相同的锚语义,两种压缩模式下推理一致。postCtx 用 kernel countMessageTokens 计,与 usage 报告同量纲(sys 除外,与 post-rebuild usage estimate inflates ~3.4x after a big fold, firing an EMERGENCY T1 nudge into an over-budget context #1820 preflight 锚口径一致),修的是表的读数来源,不动 nudge 节奏(flat 50K,KDD feat: release CI (npm auto-publish on release branch) #8)与任何注入行为。

② 回归审查:触碰路径的既有行为逐项清点

既有行为 结论
[acp-compress-obs] shrink 日志行(含 ceiling 计算) 保持——锚打在日志行之前,日志内容与 ceiling 仍用裸 postCtx,字节不变
缓存台账 recordCacheFoldsFromBlocks({V, Vp}) 保持
#1911 退化折叠警告、#695/#1387 折扣网络本身 保持
preflight 重建锚(src/server.ts:3340) 保持;马拉松中间轮现在经 applyRanges 多打一次锚,收尾重建戳最后覆盖——净效果是马拉松期间表骑在每轮真实余量上,是 #1820 意图的自然延伸(有意变更,已披露)
锚生命周期(tick/clear) 保持:tick 在每次 prepare 首行(src/server/handle.ts:2187)、真 usage 一到 settleUsageReport 立即清锚(src/cache-ledger.ts:1445)、native compaction 边界清锚(session.ts:1317 区)——全部复用 #1820 既有函数,零新机制
健康会话(每轮报 usage) 零变化——usage 级基线是阶梯第一优先,先于锚返回;测试 4 钉住这条优先级且修复前就绿(证明它锁的是既有行为不是新行为)
不报 usage 的上游 锚预算耗尽(3 次 prepare)后自动回落旧 sizing——#1820 原语义,无冻结风险
#2725 若后续合入 它管「基线活太久」,本 PR 管「基线被抹零」,作用在不同分支上;唯一交点是普通折叠刷新锚预算到 3,有界,无叠加放大

高危模式逐项过:无 fail-fast 变吞错、无输出格式变化、无线形变化、无阈值漂移(唯一新常数 Math.max(1, postCtx) 的下界 1 已注释说明);缓存/重试/游标/去重不受影响。

③④ 代码与测试有效性

代码:注释讲为什么且带现场数字(289%/564%),风格与仓库一致;无死代码、无魔法数字、无新依赖;两 commit 各自标注清晰,diff 每一行都与目的相关(⑤/⑤b 全过)。

测试有牙,我独立复测了红绿:把 src/stream.ts 换回 master 版本跑该文件 → 测试 1(鬼影主形状)、2(现场 564% 的 stale localInputEstimate 滞后形状)、3(postCtx=0 退化折叠)三个 not ok,测试 4(usage 级优先)绿;换回 HEAD → 4/4 绿。断言都是精确等值(meter.tokens === anchor)+ 源等级 + 窗口内判定,删掉打锚、改阶梯顺序、改 setter no-op 语义各都会被对应测试抓住。亲缘套件本地定向跑:issue1820-post-rebuild-anchor + compress-observability + host-usage-postfold 共 49/49 绿。唯一小缺口:普通折叠路径下锚的 3-prepare 衰减没有专门用例——但衰减逻辑与 preflight 锚共用同一 tick 函数,已被 issue1820 套件覆盖,可接受。

结构化八问

  1. 是什么问题:巨型会话折叠成功之后,压力表反而读到 289%/564%,把刚折好的会话当超限,点燃紧急 nudge 和预压缩马拉松,真实上下文只有百分之几。
  2. 什么原因导致的:折叠把省下的 token 从会话基线里扣掉(Plugin path has no per-request cache usage log — post-fold 'cache drops to 0' cannot be attributed #695 折扣),当省下量大于陈旧小基线(升级切换后没报过 usage / 上游从不报)时基线被钳到 0;下一轮计量阶梯没有可用基线,回落到按重发历史字符数估的上界(高估 2-4 倍)——post-rebuild usage estimate inflates ~3.4x after a big fold, firing an EMERGENCY T1 nudge into an over-budget context #1820 的防鬼影锚只覆盖了 preflight 重建,普通折叠这个提交点从来没打过锚。
  3. 影响范围与严重程度:触发条件 = 重发历史 ≳ 窗口的巨型会话 × 基线缺失窗口(刚折叠/刚接管/不报 usage 的上游)。满足时严重:对已折好的上下文空转分钟级马拉松,客户端 ~60s 耐心断连重试循环,老会话实质不可用(用户现场即此类);不满足时(小会话、健康上报)完全无感。
  4. 怎么修复的:在所有工具执行折叠的唯一收口 applyRanges 成功路径上,把本来就在算的折叠后视图尺寸(kernel 计数同量纲)打进 post-rebuild usage estimate inflates ~3.4x after a big fold, firing an EMERGENCY T1 nudge into an over-budget context #1820 既有的 post-rebuild 锚——复用其全部生命周期语义(真 usage 立即顶替、3 次 prepare 有界自灭、损坏戳降级为无锚),零新机制;补一个退化折叠 postCtx=0 时锚 1 的边界。
  5. 是否有回归问题:按上面清单逐项审过,无回归;有意变更两处均已披露(见下)。
  6. 是否破坏缓存:不涉及——不碰缓存键、前缀、重试策略;锚存于 session.metadata 既有 key(post-rebuild usage estimate inflates ~3.4x after a big fold, firing an EMERGENCY T1 nudge into an over-budget context #1820 已有),旧文件载入无感,新版字段旧版忽略。
  7. 是否改了设计方向:不改架构/边界/数据流;是把 post-rebuild usage estimate inflates ~3.4x after a big fold, firing an EMERGENCY T1 nudge into an over-budget context #1820 已确立的「重建后骑实测尺寸」原则从 preflight 一个提交点扩到全部折叠提交点(同一原则,覆盖面变大)。
  8. 是否引入了新的配置:无——零新 env/config/默认值。

行为变更披露(old→new):

  • 无 usage 级基线的会话,普通折叠成功后的下一轮计量:old = 阶梯回落值(常为上一轮折叠前的 raw 字符上界,即鬼表;或测不到时为 0)→ new = 折叠后视图真实余量,最多骑 3 次 prepare 后回落旧 sizing。方向单调朝真实值,有界。
  • 全量覆盖的退化折叠:old = 不打锚(setter 对 ≤0 no-op),可能幸存一次鬼影决策 → new = 锚 1 token,表读 ~0%,preflight fit gate 仍是硬兜底。

CI 与判定

  • 核心矩阵(CI run 39040680825,head 72bb201):检查时在跑,6 test leg + 2 static leg;e2e 各 lane 已 success(gate-skip 形态)。cf80a1eb0 那轮的矩阵曾被 label 事件抢 concurrency 组取消、未出过完整判定,所以以本轮为准。
  • ci-gate-plugins.yml 那条 push 失败可忽略:纯手动 workflow(workflow_dispatch + secret 端点),不在 PR 阻塞集,两个 head 上同样表现。

判定:可合(待核心矩阵绿,预计几分钟;绿了我回来补一句确认,红了定向排查)。§7.4 角度:纯 fix、单模块、回归测试红绿可证、不碰 config/persistence/wire/kernel/update,干净 diff、Fixes #3573;唯一保留是人合而非自合——它扩展了 fallback sizing 的覆盖面,按仓库规则这类默认值语义扩展留给人拍板,而事实上新增面只是复用既有有界机制,风险很低。合并后 #3573 的「折叠后假超限」这一半闭环,另一半(冷启动首轮马拉松切片)走 #3590。

…ommits now persist the folded session's own artifact (#1341)
@ranxianglei

Copy link
Copy Markdown
Owner Author

🤖 Powered by ework · qwen3.8-27b

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

审核发现一处真回归(修 A 坏 B),已在 PR 分支上补一个测试修复 commit 280186d56。 上一条回复的「可合」判定更新为:待新 head 的核心矩阵绿。

回归是什么

核心矩阵(CI run 39040680825,head 72bb201)三个 shard-1 leg(ubuntu22/windows22/ubuntu24)各挂同一个测试,其余全绿:

本地 A/B 钉死是本 PR 引入:把 src/stream.ts 换回 master 版本跑该文件 → 6/6 绿;换回 PR 版本 → 恰好这一条红。不是 flake、不是 master 存量红(该断言自 #1915 合入以来一直在 shard-1 跑)。

机制链(逐环代码核验)

  1. [Feature] CCR v2: message-ID coverage in block summaries, range-level decompress, ID-returning search_context #1179 的 CCR fold-time storing:折叠提交时 adoptContentStore(src/store.ts:135)把新原文写进内存 content store 并置 contentStoreDirty = true,但只置脏标记、不触发保存——落盘等下一次 markDirty;
  2. 修复前 applyRanges 的成功路径从不调用 markDirty,所以父会话的脏标记在折叠当下是休眠的;
  3. 本 PR 新增的 setPostRebuildAnchor 内部调 markDirty → scheduleSave,而 scheduleSave 第一行就是同步执行 saveContentStore(src/persist.ts:1794/567):看见 contentStoreDirty=true 立即 tmp+rename 写出 .content-store.json;
  4. 于是 bug(ccr): fork adoption carries covered refs without adopting their content-store entries #1341 测试里 armParent 一折叠,父会话自己的 companion 文件当场落盘,与子会话 flushSync 的那个并列 → 计数 2 ≠ 1。

为什么改测试而不是改代码

我考虑过「打锚时不触发 markDirty」(锚只留在内存、搭下一轮自然保存的车),否掉的理由:

所以 #1341 那条绝对计数断言编码的是修复前的持久化时序,不是它本来要守护的东西。修复(commit 280186d,仅动测试):断言改为flush 增量——flushSync(child) 前后 companion 文件数差必须恰为 1,原意图(子会话自己的 artifact 恰好一份、重载后 retrieve 仍拿回原文)全部保留且未被削弱;注释写明为什么从绝对计数改为增量(#3586 语义)。全仓检索过:没有其他测试做同类 companion 绝对计数断言,不会二次爆红。

行为变更披露(old→new):工具执行折叠成功后,被折叠会话的 CCR companion 文件从「随下一轮自然保存落盘」变为「折叠提交时同步落盘」(envelope 本身仍是 debounce 时序不变)。方向朝耐久性,无用户可见副作用。

验证

本地定向:fork-ccr-adoption + issue3573 + issue1820 共 13/13 绿;storage-ccr + compress-observability + host-usage-postfold 共 49/49 绿。新 head 280186d 已推,核心矩阵在跑,绿了我回来补确认。

@ranxianglei

Copy link
Copy Markdown
Owner Author

🤖 Powered by ework · qwen3.8-27b

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

核心矩阵已绿,判定确认:可合。

新 head 280186d56 的 CI run 39044055497:completed/success——6 个 test leg(ubuntu22×3 / ubuntu24×1 / windows22×2)+ 2 个 static-gates + kernel-guard / lane-guard / lock-sync / config-surface-guard / docs-sync / docs-coverage / version-guard / pi-subagents-guard / test-import-graph / unused-exports 全部 success(唯一 skipped 是设计内的 red-signal)。上一轮红的三个 shard-1 leg 本轮全绿,fork-ccr #1341 断言按 flush 增量钉住后不再受折叠当场落盘影响;e2e 各 lane 亦 success。

PR 分支最终三个 commit:

  1. cf80a1eb0 fix:普通折叠打 post-rebuild usage estimate inflates ~3.4x after a big fold, firing an EMERGENCY T1 nudge into an over-budget context #1820 重建锚(鬼影主修);
  2. 72bb201ec fix:postCtx=0 退化边界锚 1 + 现场 564% 形状回归测试;
  3. 280186d56 test:fork-ccr companion 计数改钉 flush 增量(本 PR 引入的持久化时序变化所致的测试过期断言)。

审核结论维持上一条八问:根因判断成立、修的是生产者、两种压缩模式同语义、无缓存/线形/配置变更、行为变更两处已披露(old→new 见上)。合并留给人:这是 fallback sizing 覆盖面扩展,按仓库规则人拍板。合并后 #3573「折叠后假超限」一半闭环,冷启动首轮马拉松切片走 #3590。

@ranxianglei ranxianglei mentioned this pull request Oct 17, 2026
6 tasks
@ranxianglei ranxianglei changed the title Phantom over-limit meter after a successful fold (289%/564% ghost) — plain folds now stamp the #1820 rebuild anchor | 折叠成功后压力表读数鬼影(289%/564% 假超限)— 普通折叠也打 #1820 重建锚 (#3573) [PR] Phantom over-limit meter after a successful fold (289%/564% ghost) — plain folds now stamp the #1820 rebuild anchor | 折叠成功后压力表读数鬼影(289%/564% 假超限)— 普通折叠也打 #1820 重建锚 (#3573) Oct 17, 2026
@ranxianglei

Copy link
Copy Markdown
Owner Author

(ai: zhipuai-lb/glm-5.3)

陈旧票清理(第三批动作①):目标 issue #3573 已关。为避免重复劳动关闭本 PR;如判断有误请直接 reopen 并留言。

ranxianglei added a commit that referenced this pull request Oct 24, 2026
…ence wall clock (#3573)

A summarization round costs 25-44s against a real upstream regardless of
range size (#2682), while the common client timeout class (dsh ~60s) only
fits ONE such round. An over-window preflight kept admitting rounds until
the client disconnected mid-round: the attempt died, the in-flight summary
call was wasted, and the user saw an opaque 60s hang-retry loop (#3573).

Rounds that make upstream calls are now admitted only while the wall clock
leaves room for the next one (estimate: 2x the last round's measured time,
first round always admitted, digest regime exempt — CPU-only by design).
A stopped attempt keeps its completed folds (they are persisted), so the
client's retry resumes from the smaller view and the marathon converges
across attempts; the fail-fast stays a plain 502 with no dead-end cooldown,
and the tier-recovery funnel is suppressed for the stopped attempt so it
cannot sneak extra upstream calls past the budget.

Fixes #3573 (companion to #3586 — same issue, independent half).
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

1 participant