Skip to content

[PR] fix: tool-driven folds leave SAVED at 0 — restore the tokensSaved writer (#4700) - #4723

Open
ranxianglei wants to merge 5 commits into
masterfrom
2026-10-25_4700-tokens-saved
Open

ranxianglei wants to merge 5 commits into
masterfrom
2026-10-25_4700-tokens-saved

Conversation

@ranxianglei

Copy link
Copy Markdown
Owner

Closes #4700

Root cause

session.stats.tokensSaved lost its writer at the kernel import: every fold lane stamps per-block compressedTokens, but nothing ever credited the session counter again (grep-verified: no incrementer anywhere in src/). Since #2734 the web SAVED column is max(counter, sum over ACTIVE blocks' compressedTokens) — so whenever folded blocks deactivate (client-side /compact archive, orphan-GC, tier distillation, host expand) both terms are 0 and the column collapses to 0 even though real folds happened. Tool-driven ACP folds are most exposed because that path runs right next to native compaction.

One correction to the issue report: on both v0.1.202 and master the kernel DOES stamp compressedTokens on the blocks it creates (kernel/src/compress.ts applySingleRange — identical code at the tag). The field lives inside blocks.payload_json; a plain SELECT over the table's top-level columns won't show it. Confirm with: SELECT block_id, json_extract(payload_json,'$.compressedTokens') FROM blocks WHERE session_id='…'. The actual defect is exactly what the issue's "Why" section says: neither term of the max() ever gets written by the fold path.

Fix

Credit the gross r.tokensCompressed at the single applyRanges success point in src/stream.ts shared by ALL lanes (tool-driven proxy folds, plugin-lane /__bili/plugin/tool folds, and preflight):

  • refused paths return before the success point → no credit;
  • partial folds: r.tokensCompressed counts only successful ranges;
  • in-place refolds report tokens: 0 from the kernel → no double count;
  • the counter persists across restarts (stats round-trip already handles it).

Web derivation unchanged: Math.max(counter, activeSum) keeps pre-tagging sessions from regressing, and now the floor also covers folds whose blocks deactivated afterwards. Two stale comments claiming "#2734 removed the last writer" corrected (the counter died at the kernel import; #2734 was web-only).

Behavior change (old → new)

  • SAVED column for tool-driven / post-deactivation folds: 0 → lifetime gross total (the reported bug). Example from the issue: two folds 12789 + 8214 → column shows 21003 instead of 0, including after client-side /compact deactivates both blocks.
  • Preflight folds: display value UNCHANGED while their blocks stay active (both terms carry the same gross caliber); after deactivation they keep their total instead of dropping to 0 (same mechanism as above).
  • Restored legacy sessions with a stale pre-kernel stamp: new credits accumulate ON TOP of the stored value (semantically correct — it is a lifetime total).

Config surface

None — no new/renamed/re-semantics config fields, env vars, or CLI flags.

Tests

  • NEW tests/issue4700-tokens-saved.test.ts: successful fold credits the counter with exactly the block's gross; consecutive folds accumulate (the 12789+8214 shape); refused fold credits nothing; repeat call over an already-folded range credits nothing again; SAVED survives full deactivation mirroring the web derivation.
  • tests/web-sessions.test.ts: new case — credited counter + all blocks inactive → detail AND list row keep showing the total (guards the floor semantics end-to-end).
  • Local targeted: typecheck clean; new + observability + web-sessions + 10 adjacent fold/accounting suites (compress-credit, host-usage-postfold, fingerprint-refold, loop-compress, compaction-archive, codex-compact, persist-stats-roundtrip, compress-reject-reasons, preflight-covered-ranges, proxy-orphan-gc) all green (154 tests). Full-suite verdict: remote CI.

@ranxianglei ranxianglei changed the title fix: tool-driven folds leave SAVED at 0 — restore the tokensSaved writer (#4700) [PR] fix: tool-driven folds leave SAVED at 0 — restore the tokensSaved writer (#4700) Oct 25, 2026
The cumulative savings counter lost its writer with the kernel import:
every fold lane stamps per-block compressedTokens, but nothing ever
credited session.stats.tokensSaved again. The web SAVED column derives
max(counter, sum over ACTIVE blocks' compressedTokens) since #2734, so
whenever folded blocks deactivate (client-side /compact archive,
orphan-GC, tier distillation, host expand) the column collapses to 0
even though real folds happened — tool-driven ACP folds are most
exposed because that path runs right next to native compaction.

Credit the gross r.tokensCompressed at the single applyRanges success
point shared by all lanes (tool-driven proxy/plugin folds AND
preflight): refused paths return earlier, partial folds count only
successful ranges, in-place refolds report tokens 0 — no double
counting. The counter persists across restarts (stats round-trip).

Also correct two stale comments that claimed #2734 removed the last
writer (the counter died at the kernel import; #2734 was web-only).
@ranxianglei

Copy link
Copy Markdown
Owner Author

🤖 Powered by ework · qwen3.8-27b

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

查重: 不重复 — 这是 #4700 的实现 PR(Closes #4700),open/closed 里没有第二个同主题条目(失败签名:web SAVED 列在折叠块失活后归零;机制:fold 提交点从不写 session.stats.tokensSaved)。

标签: bug + scope/kernel + p3(纯展示面缺陷——SAVED 列显示 0 而非累计值,无功能影响)。

收到。按 [PR] 流程不重做,只做核对:拉分支对代码、查 CI、处理分支落后问题。已用工具核实的事实:

  • PR 头 2026-10-25_4700-tokens-saved @ a45eb8ebb,单 commit,基于 fe5ba5912;
  • GitHub 报 mergeable=false / dirty —— 分支落后当前 master(4d37feaa9)530 个 commit,且它触碰的 src/stream.ts、src/persist.ts、src/web/sessions-data.ts 在 base 之后都有改动;
  • 独立复核:当前 master 全仓 grep 确认 tokensSaved 仍然没有任何写者(只有类型声明、stats round-trip 和 web 读取)→ 修复仍然必要,且不存在与新增写者重复记账的风险;stream.ts 的挂点(compressCreditTokens 记账行)在 master 上原样存在(src/stream.ts:1345),语义锚点未漂移;
  • diff 本身干净:src/stream.ts 加一行记账 + 两处过期注释纠正(persist.ts / sessions-data.ts)+ 新测试文件,无无关改动。

接下来:rebase 到最新 master → 解冲突 → 本地 typecheck + 定向测试 → push → 等 CI 全局判定。完成后按八问回复结论。

@ranxianglei
ranxianglei force-pushed the 2026-10-25_4700-tokens-saved branch from a45eb8e to ca67657 Compare October 25, 2026 14:43
@ranxianglei ranxianglei added bug Something isn't working p3 scope/kernel labels Oct 25, 2026
@ranxianglei

Copy link
Copy Markdown
Owner Author

🤖 Powered by ework · qwen3.8-27b

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

冲突已解决并推送:分支已 rebase 到当前 master(bb7c24972,即 #4687 opencode-inline 合入后的头),新头 ca67657cc,已用 --force-with-lease 推回。全程未改任何代码——diff 与 rebase 前逐字节一致(仍是原 5 个文件,+181/−9)。

按八问:

  1. 是什么问题:PR 分支落后 master 500+ 提交且产生文本冲突——双方在同一位置(tests/web-sessions.test.ts 的 fix(web): derive tokensSaved from active blocks; label frozen context/ledger windows #2734 测试之后)各自追加了独立的新测试,master 侧是 Compressed source text persists permanently with no deallocation path: blockContents alone accounts for 78% of archive s | 压缩原文永久驻留且无释放路径:blockContents 单会话占存档 78%,与「插件失效即膨胀」耦合(附 13.6:1 / 749 次回捞实测) #3671 的 decompress 计数测试(随 [PR] pi/opencode inline changed to manual opt-in: revert the default flip; default behavior is identical to the previous vers | pi/opencode inline 改为手动开启:收回默认翻转,默认行为与旧版完全一致 #4687 合入),本 PR 侧是 Tool-driven (ACP) folds leave the SAVED column at 0 — blocks lack compressedTokens and stats.tokensSaved is never credited #4700 的「credited tokensSaved survives full deactivation」测试。
  2. 什么原因导致的:两处追加落在同一锚点相邻行,git 无法自动合并相邻新增块;另外处理期间 master 又前进了一次([PR] pi/opencode inline changed to manual opt-in: revert the default flip; default behavior is identical to the previous vers | pi/opencode inline 改为手动开启:收回默认翻转,默认行为与旧版完全一致 #4687 合入),所以最终 rebase 到了最新头而不是第一次 rebase 时的中间头。
  3. 影响范围与严重程度:仅本 PR 分支历史;master、用户、运行时行为均零影响。PR 对 master 的代码差异保持不变。
  4. 怎么修复的:rebase 到最新 master,唯一冲突取并集——两个测试都保留(master 的 Compressed source text persists permanently with no deallocation path: blockContents alone accounts for 78% of archive s | 压缩原文永久驻留且无释放路径:blockContents 单会话占存档 78%,与「插件失效即膨胀」耦合(附 13.6:1 / 749 次回捞实测) #3671 测试在前、本 PR 的 Tool-driven (ACP) folds leave the SAVED column at 0 — blocks lack compressedTokens and stats.tokensSaved is never credited #4700 测试在后,各自独立闭合),src 三个文件均为自动合并后人工核对无误(applyRanges 的 tokensSaved += r.tokensCompressed 写入点、persist/sessions-data 注释修正均完好)。
  5. 是否有回归问题:rebase 本身无回归——git diff origin/master 恰好等于原提交的 5 文件变更集;本地 typecheck 干净,定向测试全绿(tests/web-sessions.test.ts + tests/issue4700-tokens-saved.test.ts:34 pass / 0 fail)。全量回归以远端 CI 为准,CI 会在新头上自动重跑。
  6. 是否破坏缓存:不涉及(无运行时行为变化)。
  7. 是否改了设计方向:不涉及。
  8. 是否引入了新的配置:不涉及。

收工判定:冲突解除,分支干净地坐在当前 master 上,等 CI 转绿后可由 owner 合并。

@github-actions

github-actions Bot commented Oct 25, 2026 •

Copy link
Copy Markdown
Contributor

📦 Built Package Artifact

Branch: 2026-10-25_4700-tokens-saved (580d4ae)

Latest build — install this exact version

npm install -g billion-context@0.0.6151-pr.4723.c2df808

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
580d4ae billion-context@0.0.6151-pr.4723.c2df808 build
88deec2 billion-context@0.0.6066-pr.4723.b462b76 build
cdb0f70 billion-context@0.0.5962-pr.4723.eca4fc8 build
ca67657 billion-context@0.0.5756-pr.4723.a650900 build

Download artifact

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

Install instructions are refreshed on each push.

ci-check added 4 commits October 26, 2026 10:15
master window commits 293ae65/de5e03659 bumped kernel/package.json to
0.0.165 without regenerating the lockfiles; lock-sync re-resolution then
rewrites the stale recorded versions (root 0.0.163, kernel 0.0.164) and
fails. Pure recorded-version sync via npm install --package-lock-only;
no dependency or semantic change.

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

Labels

bug Something isn't working p3 scope/kernel

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Tool-driven (ACP) folds leave the SAVED column at 0 — blocks lack compressedTokens and stats.tokensSaved is never credited

1 participant