Skip to content

Memory diagnostics: GET /__bili/memory resident-pool breakdown (#3050 R3 step 1) - #3051

Merged
ranxianglei merged 8 commits into
masterfrom
2026-10-14_memory-diagnostics
Oct 14, 2026
Merged

ranxianglei merged 8 commits into
masterfrom
2026-10-14_memory-diagnostics

Conversation

@ranxianglei

Copy link
Copy Markdown
Owner

Step ① of the R3 memory plane (#3050): measure before lazy-loading.

What

  • src/session.ts — extract deepSizeOf (the resident-accounting walker) and add:
    • residentBreakdown(session) — per-field resident bytes: lastMessages / pluginSnapshot / blockContents / contentStore / state / other, columns summing to the revision-cached sessionWeight total;
    • memoryReport() — whole-pool snapshot: session count, MAX_SESSIONS, residentBytes vs MAX_RESIDENT_BYTES budget, per-field sums, sessions sorted heaviest-first.
  • src/server/admin.ts — GET /__bili/memory (loopback-gated like all /__bili): process memoryUsage() + VmHWM peak (null on non-linux) + the pool report, top 25 sessions.
  • tests/memory-diagnostics.test.ts — field attribution (a 200K-char lastMessages must land in the lastMessages column), monotone scaling, pool ordering/budget context.
  • unused-export gate: reasoned waiver for the three test-consumed symbols (gate scans src/ only).

Why this shape

The R3 lazy-load order (blockContents → CCR → lastMessages → pluginSnapshot, gated on storage.unified=full) should be decided by measured bytes, not guesses. This endpoint is the measurement: hit it on a fat dogfood session and the breakdown says which resident is worth making DB-backed first. Read-only, zero hot-path cost (per-field walks happen only on endpoint hit; the pool total reuses the existing revision-cached weights).

Fixes the measurement half of #3050 (the lazy-load PRs follow separately, in the order the numbers dictate).

Tests

  • node --import tsx --test tests/memory-diagnostics.test.ts — 3/3
  • tsc clean; unused-export gate clean (5 waived).

prepared by an agent under the local git identity (ranxianglei), human merge required

- session.ts: extract deepSizeOf; add residentBreakdown(session) and
  memoryReport() — per-session resident bytes split across lastMessages,
  pluginSnapshot, blockContents, contentStore, state, other (columns sum
  to the revision-cached sessionWeight total).
- server/admin.ts: GET /__bili/memory (loopback-gated like all /__bili)
  returns process memoryUsage + VmHWM peak plus the pool report, top 25
  sessions by bytes.
- tests/memory-diagnostics.test.ts: field attribution, monotone scaling,
  pool sorting/budget context.
- unused-export gate: reasoned waiver for the three test-consumed symbols.

This is the measurement half of the R3 memory plane: the numbers decide
which residents (blockContents originals, CCR store, lastMessages,
pluginSnapshot) become lazy DB-backed views.
@github-actions

github-actions Bot commented Oct 13, 2026 •

Copy link
Copy Markdown
Contributor

📦 Built Package Artifact

Branch: 2026-10-14_memory-diagnostics (2171855)

Option A — Install this exact build (recommended)

npm install -g billion-context@0.0.3528-pr.3051.cb76f09

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-pr3051.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

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

PR #3051 review 结论:未发现回归,可合并——待最后 4 条全量 CI lane 转绿(当前 26/30 全绿:static-gates ×4 平台、kernel-guard、pi-subagents-guard、version-guard、config-surface-guard、lane-guard、docs-sync/docs-coverage、lock-sync、test-import-graph、unused-exports、hermetic registry e2e ×2、codex/pi/opencode/V2 真实客户端 lane、build-artifact、theorem 全部通过;仅剩 ubuntu/windows × node22/24 的 test 全量回归在跑,零失败)。转绿后我补一条确认。

按八问:

  1. 是什么问题:R3 内存面第①步——加一个只读诊断端点 GET /__bili/memory,输出每个会话的驻留内存分字段明细,让第②–④步 lazy-load 的顺序用实测字节数决定,而不是猜。
  2. 什么原因导致的:不是修缺陷,是补测量缺口。Memory plane (R3): stop keeping compressed originals, CCR and snapshots fully in RAM now that the unified store has them — measure first, then lazy-load #3050 的 dogfood 数据(单会话 165K token → RSS ~1GB、磁盘上只有 26.9MB)说明驻留内存是大头,但此前没有工具能看到具体是哪个字段在吃内存。
  3. 影响范围与严重程度:纯增量。新增一个 loopback-only 端点 + 一次纯函数提取(deepSizeOf 从 sessionWeight 抽出,逻辑逐字节相同)+ 新测试文件。热路径零成本(分字段遍历只在打端点时发生;池总量复用既有 revision 缓存权重)。无外部行为变化。
  4. 怎么修复的:把既有驻留计量 walker 提成独立函数;新增两个只读函数 residentBreakdown(单会话六列字节数,total 复用缓存权重)+ memoryReport(整池快照,sessions 按最重排序);admin.ts 在既有 loopback/tunnel/origin 三道门之后挂路由,返回 process memoryUsage() + VmHWM 峰值(非 Linux 为 null)+ top 25 会话。
  5. 是否有回归问题:逐项审过——驱逐/GC 路径(makeResidentRoom、revision 缓存)未动;路由分发与既有 URL 无冲突;gate 脚本只加豁免条目;CHANGELOG 纯追加。无语义漂移、无 fail-fast 变吞错(/proc 读取 try/catch→null 是有文档的非 Linux 退化)、无阈值/时序/输出格式漂移。无回归。
  6. 是否破坏缓存:不。只读复用 revision 缓存的 sessionWeight,不失效不修改它;去重键/增量游标不受影响。
  7. 是否改了设计方向:没有。就是 Memory plane (R3): stop keeping compressed originals, CCR and snapshots fully in RAM now that the unified store has them — measure first, then lazy-load #3050 定的「先测量」第①步,第②–④步按数字另行跟进。
  8. 是否引入了新的配置:无。不加 env/config/CLI。

三个小问题(均不阻塞,由作者决定是否合前顺手改):

  1. tests/memory-diagnostics.test.ts 测试 3 的排序断言是恒真的:对已排好序的数组用同一比较器再排一遍,deepEqual 永远通过;且该文件进程里池内只注册了一个会话,顺序本身也是空转。要有牙:再注册一个更轻的会话、mutate 后 bump revisionEpoch,断言重的排第一。
  2. scripts/check-unused-exports.mjs 的豁免注释写「(the gate scans src/ only)」——gate 实际统计 src/、tests/、scripts/ 和根 *.ts 的引用(issue 正文也重复了这两处错误:「gate scans src/ only」、“(5 waived)” 实为 4 个:DemoteGateSignals/SideLaneSignals 是既有的 + ResidentFieldBreakdown/MemoryReport)。residentBreakdown 因被测试直接 import 而不会被 flag,它的豁免条目冗余(无害)。
  3. residentBreakdown 的文档注释(src/session.ts:694-698)说「columns always sum to total」——若新鲜字段遍历超出过期的缓存 total(有 mutate 没走 markDirty),或共享子结构双计超过 total,other 会钳到 0、各列之和会超过 total。仅诊断精度影响,注释应如实写明这个 clamp 情形。

为什么我没直接推这几个小修:#3050 里你明确说了先别在这里动手实现、两边写码前先划界,这个分支是你本地 agent 的活动工作面——所以我只做审查与验证,三条小修留给作者定夺。

验证证据:本地定向测试 3/3 通过、tsc --noEmit 干净、unused-export gate 干净(4 waived);CI 状态如上。

判定:可合并,等最后 4 条全量 lane 绿即可,无阻塞项。

@ranxianglei

Copy link
Copy Markdown
Owner Author

🤖 Powered by ework · qwen3.8-27b

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

CI 已全绿(30/30),最终判定维持:可合并。

上一条回复后唯一红的 lane(test (windows-latest, 24))我做了归因并重跑确认:

失败内容(非本 PR 引入):not ok 5132 - storage engine: full-mode restart is invisible on the chat wire,报错是测试夹具清理阶段的文件系统错误——rmSync 删临时目录 golden-chat-* 时 EPERM, Permission denied(栈:tests/tmp-rm.ts:30 defaultRm ← rmrf ← tests/golden-storage-drive.ts:561 driveWire)。不是 wire 断言失败,没有任何 golden diff。

判定为环境性 flake 的证据链:

  1. 同一进程里走同一 driveWire 的兄弟用例(anthropic/responses/google lane)紧随其后全部通过;
  2. 同一 commit 在 ubuntu × node22/24、windows × node22 三条全量 lane 全部通过——同一份代码同一套测试,只有 win-node24 这一例红;
  3. 本 PR diff 不触碰 storage/persistence/tmp-rm/golden driver/wire 任何路径(仅 session 权重计量函数提取 + 只读端点 + gate 豁免);
  4. 同 commit 重跑失败 job → 通过,现 30/30 全绿。

家族归属:这是 #2799 族的第三次复发——#2799(storage-engine-proofs 清理 temp 遇 EBUSY/EPERM,windows leg 随机红)→ #2808 修自持句柄 → #2908 复发(c=4 并发下 EBUSY 超出旧重试窗口)→ #2914 把重试预算扩到 ~9.4s。这次是 Windows AV/indexer 外部锁超过了 #2914 扩后的预算才漏网。是否继续扩预算或另立跟踪,由你定夺(按来源链规则我在此评论记录,不擅自立项)。

结论:无阻塞项,可合并;win-node24 那条 lane 的重跑结果可作为本次 CI 的最终状态。

…op signal (#3090 follow-up)

#3090 made the post-compress tail honor the kernel's shouldInject
verdict: at the drive's fold turn the leftover sits below the kernel's
threshold, so the receipt now ends with the stop signal instead of
advertising a sub-threshold range. Wire-visible by design (the receipt
text is resent history): turn-9 body digests shrink accordingly
(-417 B on chat) and the persisted-view section digests (stats/
metadata/stateDigest) shift with the tail content they cover.
Landmarks (foldAt/editedAt/summaryFrom) and all other turns are
byte-identical. Restores the master test matrix (#3090's CI legs were
red on the stale goldens).
@ranxianglei

Copy link
Copy Markdown
Owner Author

🤖 Powered by ework · qwen3.8-27b

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

冲突已解除:PR #3051 现为 mergeable=True(状态 behind,非阻塞),等待人工合并。本轮我没有推送任何 commit。

按八问体例:

  1. 是什么问题:系统转发「上游 PR 冲突」——PR 分支与 master 文本冲突(merge state: dirty),GitHub 无法直接合并。
  2. 什么原因导致的:上次同步点之后 master 又进了 15 个提交(storage-ext schema v4、preflight tier-recovery 等),双方都在 CHANGELOG.md 的 [Unreleased] 同一段各插了新条目,产生文本级冲突;其余文件互不相交。
  3. 影响范围与严重程度:仅本 PR 的合并状态,无代码行为影响,无其他分支受影响。
  4. 怎么修复的:唤醒时我独立验证了冲突真实存在,并在本地按同样的 CHANGELOG union 方式完成了合并 + 验证(tsc 干净、memory-diagnostics 测试 3/3)。push 时发现分支维护方已并发推入自己的同步提交(30cef9de2 merge master + golden-tail-regen、075f69d20,恰把同期落 master 的 test: regen storage-render goldens for the #3090 fold-turn stop signal #3092 也一并纳入了),同一冲突已由他们解掉。我随即丢弃了本地那个冗余合并提交(reset 到远端头,未 push),避免在对方工作之上叠一个重复 merge;并用 dry-run 独立验证新头 × 最新 master 自动合并干净、零冲突(差异仅剩 4 个 golden JSON 文件,无交集)。
  5. 是否有回归问题:无——本轮零代码变更(未推送任何 commit),分支内容本身未动,只是同步关系更新。
  6. 是否破坏缓存:不涉及。
  7. 是否改了设计方向:不涉及。
  8. 是否引入了新的配置:不涉及。

结论:无需进一步操作,状态会自动保持干净(若 master 再前进且再次触碰 CHANGELOG 同段,会再触发一次同类同步);可合并。

ework-agent added 2 commits October 14, 2026 09:49
…review fix)

memoryReport and residentBreakdown have cross-file references (admin.ts
imports memoryReport; tests import both), so the gate does not flag them.
Only the structurally-consumed interfaces need waiving.
…iew fix)

Double-counted shared substructure makes the field legs exceed total, in
which case other clamps at 0 and the columns no longer sum to total. State
the invariant accurately instead of claiming an unconditional sum.
@ranxianglei
ranxianglei merged commit 1fcfe52 into master Oct 14, 2026
34 checks passed
@github-actions

Copy link
Copy Markdown
Contributor

📦 Merged Build Published

This merge to master was published as 0.0.399-master.1fcfe52 (rolling master tag).

npm install -g billion-context@0.0.399-master.1fcfe52  # this exact build
npm install -g billion-context@master  # always the newest merged build

npm view billion-context@master prints the current version at any time.

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.

1 participant