Skip to content

[PR] fix(persist): keep the context estimate and usage anchor across restarts (#5914) - #5915

Open
ranxianglei wants to merge 2 commits into
masterfrom
2026-11-08_fix-buildsession-restore-estimate-anchor
Open

ranxianglei wants to merge 2 commits into
masterfrom
2026-11-08_fix-buildsession-restore-estimate-anchor

Conversation

@ranxianglei

Copy link
Copy Markdown
Owner

Closes #5914

What & why

buildSession's restore whitelist drops three persisted stats fields even though buildRecord writes them (via the ...session.stats spread): contextEstimateTokens + contextEstimateCalibrated (the per-send billing-caliber estimate pair, #2117) and lastUsagePayloadUpper (the usage anchor's own payload snapshot, #4805). After a restart:

The fix adds the three fields to the restore whitelist with the same validation discipline as their siblings (finite non-negative number / strict boolean; legal 0 and false preserved, corrupt values rejected). Single choke point: every loader funnels through buildSession, so legacy / shadow / full storage modes are all covered by this one change.

Behavior change (intended — it IS the fix)

After a restart, sessions with persisted values now show the restored estimate immediately instead of the upper-bound fall-through, and the two guards above are armed from the first post-restart read. Old behavior (upper-bound display + disarmed guards until the first send/settle) was the bug. No change to live-session behavior, wire format, or persistence format.

Config surface

None — no config field added, renamed, or re-semantics'd.

Verification

  • New suite tests/issue5914-restart-estimate-anchor.test.ts (7 tests): round-trip of the reporter's fixture values including display caliber; usage-grade reading still outranks the restored estimate; legal 0/false preserved; legacy records without the fields keep today's behavior; corrupt values rejected at restore; in-memory-only transients (pendingFoldUsage, lastLocalTextEstimate(+Origin)) still dropped; both decision guards armed from the first post-reload reading.
  • Red/green proven: 6/7 fail on the unfixed code (the 7th pins already-correct legacy behavior); all pass with the fix.
  • Targeted suites green on the rebased head (master @ 133f352): new suite + issue2129-restart-measurement-state + persist-stats-roundtrip; typecheck clean. Full-suite verdict = remote CI.

Note: tests/issue3157-persist-durability.test.ts shows 2 local failures (a full-session deep-equal drift around ccrReconcilePending) identically without this diff — pre-existing and unrelated to this fix; flagged for visibility, deliberately not touched here.

@ranxianglei ranxianglei added bug Something isn't working p2 scope/storage labels Nov 7, 2026
@github-actions

github-actions Bot commented Nov 7, 2026

Copy link
Copy Markdown
Contributor

📦 Built Package Artifact

Branch: 2026-11-08_fix-buildsession-restore-estimate-anchor (cf933c2)

Latest build — install this exact version

npm install -g billion-context@0.0.8573-pr.5915.909738a

This exact version is immutable and reproducible. The rolling pr tag tracks the newest test build across all PRs.

Recent builds (commit → version)

commit version build
cf933c2 billion-context@0.0.8573-pr.5915.909738a build

Download artifact

  1. Download the artifact from the Actions run
  2. Extract the tarball and install:
tar xzf billion-context-pr5915.tgz
npm install -g package

Install instructions are refreshed on each push.

@ranxianglei ranxianglei changed the title fix(persist): keep the context estimate and usage anchor across restarts (#5914) [PR] fix(persist): keep the context estimate and usage anchor across restarts (#5914) Nov 7, 2026
@ranxianglei

Copy link
Copy Markdown
Owner Author

🤖 Powered by ework · qwen3.8-27b

[bot] 🏷 (ai: vllm-qwen/qwen3.8-27b)

查重: 不重复——本线程(#5915, GitHub PR)是 #5914 的唯一在途修复 PR;已核对镜像全部 open [PR] 行,无第二个 PR 针对同一线程/主题。

标签: bug / pi / p2 / scope/storage(缺陷类;复现环境为 Pi/PiDeck;重启后显示回退+两个决策 guard 临时解除属「能用但有明显问题」→ p2;改动域为持久化恢复)。

接下来:拉取分支核对 diff 与原 issue 的验收标准、本地跑定向测试(新套件 + 相邻持久化套件)、读镜像缓存确认 CI 状态,结论按八问回复。

@ranxianglei

Copy link
Copy Markdown
Owner Author

🤖 Powered by ework · qwen3.8-27b

[bot] 🏷 (ai: vllm-qwen/qwen3.8-27b)

PR #5915 审核结论(对应 issue #5914)

代码层面:可合并。 diff 最小、方向正确、红绿自证成立;当前唯一拦路的是 master 继承性 CI 红(与本 PR 无关,已定性并报到 #4599)。

结构化八问

  1. 是什么问题:重启后 bili 丢掉三个已持久化的统计字段(contextEstimateTokens / contextEstimateCalibrated / lastUsagePayloadUpper)——磁盘上写了、恢复白名单没接。后果:上下文条从校准估算跳回字符上界(报告者实测 95,447 → 259,141,约 2.7×),且两个决策 guard 在下次发送/结算前处于解除状态。
  2. 什么原因导致的:buildRecord 用 ...session.stats spread 全量落盘,而 buildSession 的恢复白名单(Restart loses measurement state: buildSession() omits stats fields buildRecord() persists (k̂, usage anchor, baseline origin, localInputEstimate) #2129 时代引入)逐字段枚举、漏了这三个——落盘侧与恢复侧不对称。属「生产者写了、消费者没读」的经典单点遗漏,不是设计缺陷。
  3. 影响范围与严重程度:所有带持久化值的会话、每次代理重启后都可见(全车道);重启到下次发送之间显示口径虚高最多 ~2.7–3.5×,staleSmallAnchorFallback([Bug] statusInputBaseline falls back to lastUsageGradeTokens instead of the calibrated local estimate — so one 4.7k side-request sample blocks folding for 4 hours | #1595 承诺的"改用本地估算"兜底在 statusInputBaseline 里没有实现,一次 4.7k 侧请求样本即可挡住折叠 4 小时 #4383)与 effectiveTokenCount 尺寸 guard(Bypass requests overwrite the main session's usage anchor, causing long sessions to permanently stop compressing. | 旁路请求覆盖主会话 usage 锚点,导致长会话永久停止压缩 #4805)暂时失效。能用但明显不对 → p2,与 issue 原有定级一致。
  4. 怎么修复的:在唯一恢复咽喉点 buildSession 补三个字段,沿用兄弟条目的验证纪律(finite 非负数 / 严格 boolean;合法的 0 与 false 保留,脏值拒绝为 undefined);内存瞬态(pendingFoldUsage、lastLocalTextEstimate(+Origin))维持有意不恢复。单点覆盖 legacy/shadow/full 三种存储模式——我逐一核对了全部 12 个 buildSession 调用点,确无旁路。
  5. 是否有回归问题:逐项过完既有行为——兄弟恢复条目未动;legacy 记录缺字段时保持今日行为(有测试钉住);usage-grade 读数仍优先于恢复的 estimate(provenance 优先级不变);native-compaction 边界的清除逻辑(src/session.ts:1673)是活事件路径,与恢复正交;wire 格式、持久化格式、内存瞬态丢弃均不变(均有测试钉住)。本 diff 未发现回归。 但注意:head cf933c2 的 CI 矩阵红于两个 committed-golden 家族(golden-storage-render ×4 wire、storage-engine-proofs full-mode-restart ×4 wire)——我把 PR diff 回退到 master 版本本地复跑,两家族逐字同样失败,即 master 自 v0.1.213 火车(kernel 0.0.185)起 born-red,非本 PR 引入;ci-fixes 哨兵(tip d7769ca)尚未包含该修复,故未在 PR 分支 merge ci-fixes。详见我在 CI Control Center: Centralized CI Failure Handling & Status Sync (Comment all CI issues here; do not open new issues) | CI 总控: CI 失败集中处理与状态同步(所有 CI 问题在此评论, 勿开新 issue) #4599 的报告。另:原始红 run 被平台标签同步触发的两次 labeled→scope=skip run 取消顶替,分支可见 check 现显示 skipped/gate-success,是误导性表象(该系统性隐患单独留了 🔶 决策请求)。
  6. 是否破坏缓存:不涉及——纯恢复路径变更,不触碰缓存/重试/增量游标/去重键。
  7. 是否改了设计方向:不涉及。
  8. 是否引入了新的配置:不涉及(config surface 零变更,PR 已声明)。

验证记录(本地定向,非全量)

收工判定

代码就绪,等 CI。 待 ci-fixes 哨兵收敛 golden 修复后,PR 分支一次普通 push/rebase 即可触发真实 core 矩阵重跑转绿,然后由人合并。在此之前不建议加 ci:full 标签(会在已知继承红上空烧一整轮矩阵)。

@ranxianglei

Copy link
Copy Markdown
Owner Author

🤖 Powered by ework · qwen3.8-27b

🔶 需要决策

背景一句话:平台把本线程的标签同步到上游 GitHub PR 时,会触发 ci.yml 的 labeled 事件 → scope=skip run;这类 run 会取消同分支正在跑的真实矩阵 run 并顶替其结果,导致真实红判决在分支上显示为 skipped/gate-success(本 PR 的红 run 42424217159 就是这样被两次 labeled run 遮蔽的,ci.yml 注释里的 #3145/#3035/#3027「slipped red with test: skipped」是同一隐患家族)。

待决问题:标签同步触发的 skip-scoped CI run 是否应当保留?它让「分支可见 check 状态」不再可信——任何评审者(人或 agent)看 branch status 都会被误导,只能靠翻 run 历史还原真相。

选项:

  • A. 平台停止把 agent 打的标签同步到上游 GitHub PR(仅镜像内生效):最干净,CI 面零成本;代价是上游 GitHub 侧看不到 triage 标签(镜像里仍可查)。
  • B. ci.yml plan job 对「非 ci:full 且紧随 push 到达」的 labeled 事件不触发:保留双向可见性;代价是启发式脆弱(时间窗判断),且改的是 CI 热文件,本身要走一轮审核。
  • C. 维持现状,约定评审前必须人工加 ci:full 或核对 run 历史:零改动成本;代价是每个带标签变动的 PR 都踩一次这个坑,靠纪律兜底。

我的推荐:A(若上游侧确实不需要标签可见性);否则 C 作为底线 + 在 #4599 持续记录被遮蔽实例。不决定的默认行为:维持现状(C 的事实状态),我不做任何单方面的 CI/平台改动。

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

1 participant