Skip to content

fix: keep the full per-request cache ledger history (#1489) - #1490

Merged
ranxianglei merged 2 commits into
masterfrom
2026-09-27_cache-ledger-full-history
Sep 27, 2026
Merged

ranxianglei merged 2 commits into
masterfrom
2026-09-27_cache-ledger-full-history

Conversation

@ranxianglei

@ranxianglei ranxianglei commented Sep 27, 2026 •

Copy link
Copy Markdown
Owner

What & why

Per-session cache ledger arrays were capped at write time (SAMPLE_CAP = 512, FOLD_CAP = 256 in src/cache-ledger.ts): past the cap the OLDEST entries were spliced away at record time and are unrecoverable. Aggregates stayed exact, but all per-point history after the cap was silently destroyed — the web trajectory chart could only draw from the earliest retained sample (issue repro: session started 09-25, ~1483 requests, chart showed only 09-26+).

The mitigation already shipped in #1426/a0fc322a (cap 512→4096) only postpones this: "one session per month, billions of tokens" is the core selling point. Per owner direction in the issue (「所有数据都应该能留下来」), this removes the caps entirely.

Changes

  • Remove both caps (src/cache-ledger.ts): every sample and every fold is retained for the whole session lifetime. Same root cause for both arrays (capped array splicing oldest), so both go in one fix — fold markers/economics now also cover the full span, which the trajectory chart overlays.
  • handleAcpCache detail:"full" gets a render window: the text listing shows the newest 512 lines with the kernel's own LINE ITEMS (last N of M) header marking the windowed view (the kernel's existing maxLines rendering semantics). The data layer, aggregates, and the JSON endpoint /__bili/cache-report remain complete. Rationale: with an unbounded ledger, an uncapped detail:"full" dump would flood the calling model's context (a 10k-request session ≈ ~700KB of line text); summary mode's output stays bounded (same digest caps); its anomaly/TTL-spike scan now spans the full history instead of the retained tail for sessions past 512 samples — more accurate diagnostics, same output size.
  • Tests (tests/cache-ledger.test.ts): replaced "ring eviction keeps aggregates exact" — which pinned the truncation itself (520 samples → 512 kept, linesOmitted: 8) — with unbounded-retention tests past the old caps (600 samples / 260 folds, seq starts at 1, linesOmitted: 0, aggregates exact) plus a text-windowing test for detail:"full" and a legacy-deficit test pinning that pre-upgrade trimmed sessions keep their historical linesOmitted honest through the window.

No config surface added (option 4 from the issue deliberately not taken — nothing new to configure once there is no cap). No kernel changes needed: linesOmitted is already a first-class report field.

Behavior changes (old → new)

Surface Old New Why
Per-session sample retention newest 512 (master) unbounded (~100B/request; 10k-request session ≈ +1MB session file/RAM) acceptance goal: trajectory covers the full lifetime
Per-session fold retention newest 256 unbounded (folds are rare — one per compression) same root cause; chart fold markers cover full span
acp_cache detail:"full" text output lists all stored lines (≤512) lists newest 512 lines + explicit LINE ITEMS (last N of M) header when more exist bounds model-context flood; data intact elsewhere
acp_cache summary digest scan range last ≤512 stored lines (master) every recorded line (output still capped at 20 anomaly rows + top-3 spikes) long-session diagnostics become history-complete; output size unchanged
linesOmitted grows past 512 0 for everything recorded post-upgrade; sessions trimmed before keep their historical deficit recorded honestly field kept for truthful accounting

Sessions ≤512 samples: byte-identical behavior everywhere.

Coordination note: PR #1426 (issue #1420 web UI rewrite)

#1426's commit a0fc322 (branch wt-1420-ework-daemon) raises the SAME constants (512→4096) and adds i18n strings hardcoding "4096". If #1426 merges first, rebase this PR onto it — resolution is union of intent: keep the removal, drop the now-stale "4096" strings. If this merges first, #1426 rebases onto the removed lines. Either way the conflict is one hunk. Also for #1426's authors: trajectorySvg renders every received line as an SVG point with no client-side decimation — designed around ≤4096 points; with unbounded ledgers please decimate client-side for display (data stays complete server-side).

Pre-flight

  • npm run typecheck: clean
  • npm test: 2909 pass / 0 fail (12 skipped = gated e2e suites)
  • npm run build: success

Fixes #1489

The ledger arrays (lines/folds) were capped at write time — SAMPLE_CAP
512 / FOLD_CAP 256 spliced off the OLDEST entries, permanently destroying
per-point history: the web trajectory chart could only draw from the
earliest retained sample, silently truncating long sessions.

- Remove both caps: every sample and fold is now retained for the whole
  session lifetime. Aggregates were already exact; linesOmitted stays a
  field so sessions trimmed before this fix keep their historical deficit
  honestly recorded (it is 0 for everything recorded after the upgrade).
- handleAcpCache detail:"full": window the TEXT view to the newest 512
  lines (kernel's own maxLines rendering, "older outside window" note)
  so an unbounded ledger cannot flood the caller's context. Data layer
  and JSON endpoints (/__bili/cache-report) remain complete.
- Tests: replace the ring-eviction test (which pinned the truncation)
  with unbounded-retention tests past the old caps + text-windowing test.
@github-actions

github-actions Bot commented Sep 27, 2026 •

Copy link
Copy Markdown
Contributor

📦 Built Package Artifact

Branch: 2026-09-27_cache-ledger-full-history (cd0bbcd)

Option A — Install this exact build (recommended)

npm install -g billion-context@0.0.1416-pr.1490.5a44b6a

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-pr1490.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] 🏷 Review of PR #1490(针对 issue #1489)

① 方向关(原 issue):方向成立。已对照 master 验证:SAMPLE_CAP = 512 / FOLD_CAP = 256 确实在 recordCacheSample/pushFold 的写入路径上 splice 掉最旧条目,splice 后不可恢复——这是根因,不是「图画不出来」的表面现象。修复移除的是截断的产生者(cap+splice),属疏不属堵;两个数组同一根因(capped array splice oldest),一次修掉正确。

② 回归清单(diff 触碰的既有行为,逐项结论):

  • ≤512 样本的会话:行为逐字节不变(slice(-512) 为 no-op,linesOmitted 推导式未动)✓
  • GET /__bili/cache-report JSON 端点:走 buildSessionCacheReport(src/server.ts:5655),数据层保持完整 ✓
  • acp_cache detail:"full" 文本输出:新增最近 512 行窗口,防模型上下文洪泛——有意变更,PR 正文已披露 ✓
  • acp_cache summary 摘要:review 新发现的行为差异(原 PR 正文未覆盖)——摘要渲染器的异常/TTL 尖峰扫描遍历 report.lines;master 上长会话只扫保留的尾部 ≤512 行,本 PR 后扫全历史。输出大小不变(仍限 20 异常行 + top-3 尖峰),内容更准。已在 PR 正文行为表补一行披露。
  • linesOmitted 语义:派生值(sampleSeq - lines.length,src/cache-ledger.ts:293),升级前被裁的会话自动保留历史缺口、新会话恒 0——诚实记账,无需额外代码 ✓
  • src/web/ 无本账本的消费者(轨迹图在 feat(web): full UI rewrite — read-only overview / sessions / config / connect (#1420) #1426 分支),master 侧无渲染回归面 ✓

③ 代码审查:

  • 确认 bili 的报告对象不走 kernel 的 buildCacheReport(该函数内部有 maxLines=512,acp-kernel dist/index.js:6488);实际用的是 kernel 的 formatCacheReport 渲染器,full 模式对传入 lines 原样渲染、且仅当 linesOmitted > 0 才输出 LINE ITEMS (last N of M) 头(dist/index.js:6676-6692)——所以 bili 侧显式窗口化是必需的,不是与 kernel 重复。
  • linesOmitted: report.linesOmitted + dropped 加法语义正确:kernel 头渲染成 "last 512 of 总数",总数恰等于 sampleSeq。
  • 措辞 nit(已直接修正 PR 正文):原文称 full 模式会带 "older outside window" 注记——该字符串实际只存在于 summary 渲染器;full 模式的显式标记是 (last N of M) 头。行为本身正确。

④ 测试有效性:

  • 三个新测试有牙:600 样本 / 260 folds 越过旧上限,断言 seq 从 1 起、linesOmitted: 0、聚合精确;窗口化测试断言头 + 恰好 512 数据行 + 数据层完整(600/0)。cap 若回归全部会挂。
  • 发现缺失测试并已补:legacy 缺口(升级前被裁的会话)与窗口化的交互此前无覆盖——旧 ring-eviction 测试(断言非零 linesOmitted)正是被本 PR 替换的那个。已在 PR 分支直接补 legacy-trimmed sessions keep their historical deficit honest through the window(模拟升级前 trim 后继续记账,断言数据层 linesOmitted=88 且文本渲染 last 512 of 700)。commit: cd0bbcd3,该文件 18/18 通过,typecheck 干净。

⑤ diff 整洁度:2 文件 +51/-14,无跑题改动、无空白噪音。干净。

验证证据(本地于 PR head 6ae3e71 实跑):

  • npm run typecheck:clean;npm test:2909 pass / 0 fail / 12 skipped(与 PR 正文 pre-flight 一致);npm run build:success
  • CI:head sha 上 12 个 check-run 全部 success(test 矩阵 ubuntu/windows × node 22/24、e2e lanes dsh/opencode/pi/codex、hermetic registry e2e、version-guard、build-artifact)。API 显示 mergeable_state: blocked,但 legacy commit-status 列表为空且挂着一个 pending 旧状态——疑似分支保护配置残留而非 CI 失败;若合并仍被挡请查分支保护设置。cd0bbcd3 推送后 CI 会对新 head 重跑。

#1426 协调:仍 open(head wt-1420-ework-daemon)未合并。冲突处理按 PR 正文所述(one hunk,union of intent:保留删除、去掉过时的 "4096" 字符串);SVG 客户端降采样要求仍然有效,待 #1426 侧落实。

结论:方向正确、根因命中、回归清单完整(含一处新披露的 summary 扫描范围行为差异)、测试有牙并补了一处缺失覆盖、diff 干净、CI 全绿。可以合并(合并操作留给人)。

中文一句话总结:审了 PR #1490(移除缓存台账 512/256 上限、全量保留会话历史)——方向正确、根因命中,直接在 PR 分支补了 legacy 缺口交互测试(cd0bbcd)、并在 PR 描述里披露了 summary 模式扫描范围的行为差异,本地三项检查与 CI 全绿,可以合并。

@ranxianglei

Copy link
Copy Markdown
Owner Author

🤖 Powered by ework · qwen3.8-27b

[bot] 🏷 补充:补测 commit cd0bbcd3 推送后 CI 已对最终 head 全量重跑完毕——12/12 check-run 全部 success(test 矩阵 ubuntu/windows × node 22/24、e2e lanes dsh/opencode/pi/codex、hermetic registry e2e、version-guard、build-artifact)。PR 现共 2 commits,mergeable: true,可以合并。

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.

缓存账本在 SAMPLE_CAP 处丢弃最早的逐请求采样——轨迹图无法覆盖会话完整时间跨度

1 participant