Skip to content

fix: bound web admin session loading (paged list, summary index, lazy detail) - #1939

Merged
ranxianglei merged 5 commits into
masterfrom
2026-10-03_bounded-web-sessions-index
Oct 5, 2026
Merged

ranxianglei merged 5 commits into
masterfrom
2026-10-03_bounded-web-sessions-index

Conversation

@ranxianglei

Copy link
Copy Markdown
Owner

Closes #1937

What changed

The admin page's GET /__bili/overview and GET /__bili/sessions both funneled through a 5s-TTL memo of SessionStore.loadAll(), while the UI polls every 5s. Every poll therefore fully decoded and re-built EVERY persisted session file (JSON parse + buildSession with blockContents maps / messages / state) and kept all full Session objects resident until the next scan replaced them. Peak memory ≈ 2× the parsed corpus; with ~250 files / ~127MiB of history under a 512MiB container limit (~259MiB V8 heap), each overview request OOM-killed the proxy (Reached heap limit Allocation failed - JavaScript heap out of memory).

This PR makes admin-data reads bounded:

  • Bounded disk summary index (src/web/sessions-data.ts): stat-only directory walk; per-file summaries cached by (mtime,size); only new/changed files are decoded, sequentially, one at a time, with a GC checkpoint between files and the parsed record discarded immediately after summary extraction. Live-pool files are skipped entirely (their live summaries win anyway); corrupt, renamed (filename≠body id), and CCR content-store files are logged and skipped. List/overview resident cost is now O(#sessions × ~1KB) regardless of total corpus bytes; a single request's peak is one decoded file.
  • Lazy detail: opening a session detail decodes exactly that one file via a new read-only SessionStore.loadSessionFromFile(path, expectId) (filename↔id guard mirrors kernel loadSync; no 代理压缩不上报改写量,宿主上下文会计塌缩至 ~0(omps recordAnchoredHistoryRewrite 零调用);且持久化 lastInputTokens 恢复无钳制,旧版负值升级后复活 #408 clamp-rewrite side effect), with per-id single-flight for concurrent requests.
  • Real server-side paging: GET /__bili/sessions?q=&page=&pageSize= (pageSize clamped ≤200). Without params the response shape is unchanged ({sessions, hiddenEmpty} + additive total).
  • Overview from the same index: numbers are computed from the ~1KB-per-session summaries; aggregation logic is byte-for-byte the old code.
  • Frontend (client.ts/page.ts/i18n.ts): 50 rows/page with a Load-more button, debounced server-side search, hard 20s fetch timeout, a persistent visible error strip on data failure, poll backoff 5s→10s→…→60s cap, an in-flight guard so slow responses never stack, and background refresh that never clobbers a paged/searched view.
  • Additive exports: persist.ts (relPathFor, isValidRecord, fileNameMatchesId, SessionStore.dir, loadSessionFromFile), sessions-data.ts (buildSessionPage, _diskScanStatsForTest). No config/env/CLI surface added.

Behavior changes (old → new)

  1. /__bili/sessions without params: gains additive field total; everything else identical — older UIs unaffected.
  2. /__bili/sessions with ?q=/?page=/?pageSize=: NEW query parameters (previously ignored because the route matched exact URL). pageSize clamps to ≤200; invalid values fall back to 50/page, page 1.
  3. /__bili/overview and /__bili/sessions can now return HTTP 500 {"error": ...} when the sessions dir is unreadable AND no earlier successful scan exists (old: silently returned an empty list — the "no data" state the issue saw before the crash). When a prior index exists, stale data is served instead of erroring.
  4. Web UI session list: loads 50 rows then paginates via button (old: one request, all rows, client-side search over loaded rows). Search now re-queries the server after 300ms debounce.
  5. Web UI data requests: 20s timeout (old: none — a wedged endpoint hung forever while polling stacked requests). On failure: persistent error banner + exponential backoff 5s→60s cap (old: fixed 5s polling continued silently, one auto-dismissing toast).
  6. Memory: overview/list/detail no longer scale with total corpus bytes (the fix itself).

Version attribution note

The issue was observed on a customized v0.1.180 Docker image. The unbounded mechanism itself (TTL-5s memo of loadAll() + 5s frontend poll + retained full sessions) exists in stock master, so stock builds at the same data scale are equally exposed; the custom image may have amplified but does not create the defect.

Pre-flight

  • npm run typecheck — clean
  • Targeted suites green locally: tests/web-sessions.test.ts (12, incl. 5 new 管理页面全量加载历史会话时触发堆 OOM:需要有界分页、摘要统计与详情懒加载 #1937 regression tests: warm-scan zero decodes, detail single-file decode proof, corrupt/content-store resilience, stale-snapshot vs loud-fail on dir breakage, endpoint paging/search/clamping), tests/web-i18n.test.ts (catalog lint incl. the two new buttons), plus all 9 persist-related suites (101 pass total)
  • npm run build — clean
  • Full regression + E2E left to CI (this diff touches src/server.ts, so ci-e2e.yml will run)

ranxianglei pushed a commit that referenced this pull request Oct 2, 2026
…tmp-") parity

Review of #1939: walkSessionFiles used name.includes(".tmp-"), which is
broader than the kernel StateStore.walkJsonFiles skip (startsWith). A
canonical filename may legitimately contain ".tmp-" mid-string when the
upstream host carries such a label (hostLabel keeps dots, e.g.
api.tmp-x.example_<hash>.json): the kernel loads that session at boot but
the old check hid it from the web list/overview and 404ed its detail.
Encrypted temps (.tmp-enc-N-N) carry no .json suffix, so the endsWith
gate above already drops them. Adds a regression test pinning the parity.
ranxianglei pushed a commit that referenced this pull request Oct 2, 2026
…switch data

Review of #1939: the original PR removed assert.ok(!plainHtml.includes(
"8.0K")) from the #1535 regression test without explanation. Re-running
with the assertion restored proves it still holds deterministically on the
new summary-index render path (data-level field-omission checks at lines
502-503 do not cover the rendered HTML), so the guard is kept.
@ranxianglei

Copy link
Copy Markdown
Owner Author

🤖 Powered by ework · qwen3.8-27b

[bot] 🏷 Review 结论:方向正确、实现扎实;审核中发现 1 个真实缺陷 + 1 处未解释的测试弱化,已直接在分支上修复并本地验证(2 commits,fast-forward 推送 918c3eaa..45b722be)。代码层面可合并;CI 因仓库级调度停摆尚未报告(证据见下,已单独立项 #1941),建议 CI 转绿后再合。

结构化七问

1. 是什么问题:web admin 的 /__bili/overview 与 /__bili/sessions 把全部 session 文件经 loadAll()(全量 decode + 全量驻留内存)包在 5s TTL memo 里,而 UI 每 5s 轮询——~250 文件 / 127MiB 语料下 V8 heap 触顶 OOM(#1937)。

2. 什么原因导致的:根因是数据流设计——"整语料进内存",内存占用 = f(语料总字节),与页面大小无关;5s 轮询只是让 OOM 从"迟早"变成"必然"。修频率是堵,修数据流才是疏。

3. 怎么修复的:改为有界摘要索引——walk 只 stat 不 decode;逐文件顺序 decode 一次(文件间 yield 给 GC)生成轻量 summary 记录后丢弃原文;list/overview 从索引出(分页 q/page/pageSize≤200);detail 按需单文件 decode(single-flight 去重);前端 50 行/页 + Load more + 防抖服务端搜索 + 持久错误条 + 轮询退避 5s→60s cap + 20s fetch timeout。缓存失效从时间型(5s TTL)改为内容型(逐文件 mtime+size)。

4. 是否有回归问题(逐项审过既有行为):

  • 保持:无参 /__bili/sessions 响应形状不变(仅加性 total);detail 端点 404/403 语义不变;live session 优先级不变;无参 overview 字段不变;version 字段未动(version-guard 会通过)。
  • 审核发现的真实缺陷(已修):walkSessionFiles 用 includes(".tmp-"),比 kernel walkJsonFiles 的 startsWith(".tmp-") 更宽——当 upstream host label 含 .tmp-(如 https://api.tmp-x.example/v1,hostLabel 保留点号)时,合法 session 文件名 api.tmp-x.example_<hash>.json 会被 web 面板隐藏(list/overview 缺失且 detail 404),而 kernel 启动时正常加载该 session。已改回 startsWith + 回归测试(commit 2e78a895)。
  • 未解释的测试弱化(已恢复):原 PR 静默删除了 acp-cache同时统计切换模型掉缓存归因 #1535 回归测试的 assert.ok(!plainHtml!.includes("8.0K")) 而无说明。实验证实该断言在新渲染路径上确定性通过,已按 master 原措辞恢复(commit 45b722be)。
  • kernel 一致性核对:fileNameMatchesId 与 acp-kernel dist/persist/index.js 的 loadAll owner check(relPath/flatName/.fb.json fallback)逐行一致 ✓。
  • 小观察(不阻塞,记录在案):① loadLogs 失败现在参与全局轮询退避/错误条(原先完全静默 best-effort)——行为变更,见下;② json() 的 20s timeout 只覆盖 fetch 阶段,不覆盖 res.json() 读体;③ 搜索结果 ≤50 行时后台刷新会重复拉同一查询(良性);④ detail 失败走 502 generic 而 list/overview 走 500 specific(外观不一致);⑤ 重复记录 tie-break 与 kernel 方向不同(kernel >= 后写赢 vs 本 PR >+mtimeMs 先写赢),两者均确定、重复记录近似同内容,影响可忽略。

5. 是否破坏缓存:旧 5s TTL memo 被 mtime+size 失效取代——语义从"每 5s 必重扫"变为"文件变化才重扫"(warm scan 零 decode,有结构性测试证明)。这正是 OOM 修复的核心;无其他缓存/重试/去重机制受影响。session 磁盘格式、persist 层均未动。

6. 是否改了设计方向:是——web session 数据路径从"整语料驻留内存"转为"有界摘要索引 + 惰性单文件解码"。属 src/web + persist 边界内的数据流调整,与 #1937 floor 3 提出的方案一致,实现与方案吻合。

7. 是否引入了新的配置:无。未新增 env/config/CLI 面。

行为变更披露(old → new,PR 已披露,审核复核确认)

  1. 无参 GET /__bili/sessions:响应新增 total 字段(additive,旧客户端不受影响)。
  2. 新增 query 参数 q/page/pageSize:非法值 → page=1/pageSize=50;pageSize 硬上限 200。
  3. overview/sessions 在 sessions 目录不可读且无先前索引时可返回 500 {error}(旧行为:静默空数组);有先前索引则服务 stale snapshot。
  4. UI:客户端过滤(仅首批数据)→ 服务端分页搜索;新增持久错误条、in-flight guard、轮询退避 5s→60s cap、20s fetch timeout;loadLogs 失败从静默变为参与全局退避+错误显示。
  5. 内存峰值:从 f(语料总量) 变为有界(与语料字节数无关)。

本地验证证据(typecheck + 定向测试;全量留给 CI)

  • npm run typecheck clean(修复前后各跑一次)。
  • tests/web-sessions.test.ts 13/13 通过(含 5 个新 管理页面全量加载历史会话时触发堆 OOM:需要有界分页、摘要统计与详情懒加载 #1937 suite + 新增 parity 回归测试)。
  • 定向集(persist-* / proxy-persist / affinity-persist / web-i18n / web-routing,共 97 tests):并行批跑中 proxy-persist 出现 1 次瞬时失败,随后 branch/master 各多次单独复跑共 11 次零失败 → 环境 flake,非 PR 回归。
  • diff 整洁度:8 文件 +729/−97,每行均与本 PR 目的相关;我的 2 个 review-fix commit 独立小提交、带标注。

CI 状态(如实报告)

两个 head sha(918c3eaa、45b722be)均无任何 check suite/check run(total_count=0,17:26Z 复核)。全仓最后一次被调度的 run 停在 17:07:22Z(兄弟 PR,全绿);本 PR open(16:50:04Z)与 push(17:15:22Z)两次事件都零调度,而 16:53:48Z 的 master push CI 正常 → Actions 调度在 17:07:22Z 后疑似仓库级停摆(疑 minutes 耗尽或平台事件异常,此处无法定论)。已立项 #1941 跟踪,附排查建议(Actions tab billing 横幅 / minutes 用量 / workflow_dispatch 手动触发)。本地证据独立成立,但门禁意义上请等 CI 转绿。

收工判定:代码可合并(审核问题已修复并本地验证);合并操作 human-only,请 owner 在 CI 恢复并转绿后自行 merge: #1939

@ranxianglei

Copy link
Copy Markdown
Owner Author

这个有冲突 解决下吧

ranxianglei pushed a commit that referenced this pull request Oct 3, 2026
…tmp-") parity

Review of #1939: walkSessionFiles used name.includes(".tmp-"), which is
broader than the kernel StateStore.walkJsonFiles skip (startsWith). A
canonical filename may legitimately contain ".tmp-" mid-string when the
upstream host carries such a label (hostLabel keeps dots, e.g.
api.tmp-x.example_<hash>.json): the kernel loads that session at boot but
the old check hid it from the web list/overview and 404ed its detail.
Encrypted temps (.tmp-enc-N-N) carry no .json suffix, so the endsWith
gate above already drops them. Adds a regression test pinning the parity.
ranxianglei pushed a commit that referenced this pull request Oct 3, 2026
…switch data

Review of #1939: the original PR removed assert.ok(!plainHtml.includes(
"8.0K")) from the #1535 regression test without explanation. Re-running
with the assertion restored proves it still holds deterministically on the
new summary-index render path (data-level field-omission checks at lines
502-503 do not cover the rendered HTML), so the guard is kept.
@ranxianglei
ranxianglei force-pushed the 2026-10-03_bounded-web-sessions-index branch from 45b722b to b366a0f Compare October 3, 2026 04:57
@github-actions

github-actions Bot commented Oct 3, 2026 •

Copy link
Copy Markdown
Contributor

📦 Built Package Artifact

Branch: 2026-10-03_bounded-web-sessions-index (7b21444)

Option A — Install this exact build (recommended)

npm install -g billion-context@0.0.2222-pr.1939.3991c9b

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-pr1939.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] 🏷 冲突已解决,rebase 到最新 master(f2b18022,含 #1956 dsh-persona-fingerprint),新 head:b366a0fe。

两处冲突:

  1. 文本冲突(GitHub 可见的那处)— src/server.ts import 块:master 侧加了 image-tokens / apig-resign 两行 import,本 PR 加了 web/index 的 buildSessionList/buildSessionPage 等。按并集解决,两侧内容都保留(路由处两个函数名都在用,缺一不可)。

  2. 语义冲突(GitHub 不显示、typecheck 才暴露)— master 的 任何一轮没有拿到 usage 报告的请求,都会把上下文分母放大 12–20×(nudge 读到 >1000% usage、假 EMERGENCY、基线冻结数分钟) #1839 给 WebSessionSummary、summaryOf、Session.stats、持久化层都加了 contextTokensSource("usage"/"estimate" 来源标记),而本 PR 重写了 src/web/sessions-data.ts 的 SummarySource 内联类型没带这个字段,自动合并后 L452 读 s.stats.contextTokensSource 直接 TS2551。已修:内联类型补上该可选字段,disk 路径在 summaryFromRecord 里镜像 buildSession 的收窄逻辑(persist.ts:782,只认 grouped stats、legacy 文件缺省即无标记),保证列表行与详情行的来源标记永远一致。独立 commit b366a0fe。

验证(rebase 后全新跑过):

CI 状态: 仓库级调度停摆(#1941 跟踪的那个)已自行恢复,新 head 上全套已在跑——real-image billing / fake-codex / opencode-native / dsh-profile 四条 e2e 已绿,test 矩阵(ubuntu/windows × node 22/24)+ registry e2e 进行中。矩阵全绿后即可合并,我随后回帖确认。

@ranxianglei

Copy link
Copy Markdown
Owner Author

🤖 Powered by ework · qwen3.8-27b

[bot] 🏷 CI 全绿,收工确认:

  • head b366a0fe 上 15/15 check 全部 success:test 矩阵(ubuntu/windows × node 22/24)全量回归、hermetic registry e2e ×2(自更新链)、五条真实客户端 e2e lane(codex-fake / opencode-native / dsh-profile / pi-package-native / image-billing)、version-guard(版本号未动 ✓)、build-artifact
  • GitHub 侧 mergeable: true / state: clean(之前的 dirty 是 rebase 前的旧状态)
  • 仓库级 CI 停摆已自行恢复(CI: 17:07:22Z 起全仓无任何新 workflow 调度(PR heads 连 check suite 都不生成) #1941 可以关掉了,或留作记录都行——恢复时间约在 04:57Z 前后,无进一步证据定位停摆根因)

判定:可合并。 分支为 f2b18022(当前 master)+ 4 个线性 commit(主修复、.tmp- 前缀对齐 kernel、#1535 断言恢复、contextTokensSource 语义合并修复)。合并操作按规矩由你执行:#1939

ework-agent added 4 commits October 5, 2026 18:29
…y detail

The admin page's /__bili/overview and /__bili/sessions both funneled through
a 5s-TTL memo of SessionStore.loadAll(), while the UI polls every 5s — so
every poll fully decoded and re-built EVERY persisted session file and kept
all full Session objects (blockContents maps, messages, state) resident.
Peak memory ≈ 2x the parsed corpus; a ~127MiB history under a 512MiB
container limit OOM-killed the proxy on each overview request (#1937).

- sessions-data.ts: replace the loadAll() memo with a bounded summary index —
  stat-only directory walk, per-file summaries cached by (mtime,size),
  sequential decode of changed/new files only (parsed record discarded right
  after extraction, GC checkpoint between files), live-pool files skipped,
  corrupt/renamed/content-store files logged and skipped. List/overview cost
  is now O(#sessions x ~1KB) resident regardless of total corpus bytes.
- detail: lazy single-file decode via new read-only
  SessionStore.loadSessionFromFile (filename<->id guard mirrors kernel
  loadSync), per-id single-flight; no more full scan for one id.
- /__bili/sessions: optional ?q=/?page=/?pageSize= (clamped <=200) switch to
  server-side filtered paging; no-param response shape unchanged (+ additive
  total). Unreadable sessions dir -> HTTP 500 instead of silent empty, with
  the last known index served when one exists.
- frontend: 50 rows/page + load-more, debounced server-side search, 20s
  fetch timeout, persistent error strip, poll backoff 5s -> 60s cap,
  in-flight guard, background refresh never clobbers a paged view.
…tmp-") parity

Review of #1939: walkSessionFiles used name.includes(".tmp-"), which is
broader than the kernel StateStore.walkJsonFiles skip (startsWith). A
canonical filename may legitimately contain ".tmp-" mid-string when the
upstream host carries such a label (hostLabel keeps dots, e.g.
api.tmp-x.example_<hash>.json): the kernel loads that session at boot but
the old check hid it from the web list/overview and 404ed its detail.
Encrypted temps (.tmp-enc-N-N) carry no .json suffix, so the endsWith
gate above already drops them. Adds a regression test pinning the parity.
…switch data

Review of #1939: the original PR removed assert.ok(!plainHtml.includes(
"8.0K")) from the #1535 regression test without explanation. Re-running
with the assertion restored proves it still holds deterministically on the
new summary-index render path (data-level field-omission checks at lines
502-503 do not cover the rendered HTML), so the guard is kept.
ranxianglei added a commit that referenced this pull request Oct 5, 2026
@ranxianglei
ranxianglei force-pushed the 2026-10-03_bounded-web-sessions-index branch from b366a0f to b9bcd86 Compare October 5, 2026 11:00
@ranxianglei
ranxianglei force-pushed the 2026-10-03_bounded-web-sessions-index branch from b9bcd86 to 7b21444 Compare October 5, 2026 11:03
@ranxianglei
ranxianglei merged commit cd6cbb9 into master Oct 5, 2026
21 checks passed
@github-actions

github-actions Bot commented Oct 5, 2026

Copy link
Copy Markdown
Contributor

📦 Merged Build Published

This merge to master was published as 0.0.43-master.cd6cbb9 (rolling master tag).

npm install -g billion-context@0.0.43-master.cd6cbb9  # 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.

ranxianglei added a commit that referenced this pull request Oct 5, 2026
…dex (#2152)

#1939's stale-snapshot/500 semantics turned a not-yet-created sessions
directory into a hard failure: readdir's ENOENT propagated out of
walkSessionFiles, so /__bili/overview and /__bili/sessions answered 500 on a
fresh install, and the advisory-endpoint-parity CI lane went red whenever no
sibling test had populated the default dir first.

ENOENT at the walk root now reads as zero disk sessions — it is
indistinguishable from a vanished directory anyway, and the index self-heals
on the next refresh once the store recreates the directory. Every other walk
error keeps the existing serve-stale-snapshot-else-500 behavior.

Tests: the #1937 breakage test is rewritten to the new semantics (vanished
dir reads empty, then self-heals after the store rewrites into it; ENOTDIR
still rejects loudly) and a regression pins buildOverview on a missing dir.
ranxianglei added a commit that referenced this pull request Oct 5, 2026
…dex (#2152)

#1939's stale-snapshot/500 semantics turned a not-yet-created sessions
directory into a hard failure: readdir's ENOENT propagated out of
walkSessionFiles, so /__bili/overview and /__bili/sessions answered 500 on a
fresh install, and the advisory-endpoint-parity CI lane went red whenever no
sibling test had populated the default dir first.

ENOENT at the walk root now reads as zero disk sessions — it is
indistinguishable from a vanished directory anyway, and the index self-heals
on the next refresh once the store recreates the directory. Every other walk
error keeps the existing serve-stale-snapshot-else-500 behavior.

Tests: the #1937 breakage test is rewritten to the new semantics (vanished
dir reads empty, then self-heals after the store rewrites into it; ENOTDIR
still rejects loudly) and a regression pins buildOverview on a missing dir.
ranxianglei added a commit that referenced this pull request Oct 5, 2026
… CI red since #2153)

The test injects a DISABLED SessionStore, so its own server boot never
creates the real sessions dir, and /__bili/overview's walk deliberately
throws on a missing top-level dir (#1939) — 500 instead of 200. Passing
therefore hinged on a concurrently-running sibling file (e.g.
anthropic-cache-control.test.ts, the next name alphabetically, whose
un-injected boot mkdirs $HOME/.local/share/billion-context/sessions)
winning a cross-file race. PR runs got lucky; every master push run
since 4e3ba1e failed all four test lanes.

Pin BILI_SESSIONS_DIR to an owned empty tempdir instead.
@github-actions github-actions Bot mentioned this pull request Oct 6, 2026
@ranxianglei
ranxianglei deleted the 2026-10-03_bounded-web-sessions-index branch October 15, 2026 05:32
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.

管理页面全量加载历史会话时触发堆 OOM:需要有界分页、摘要统计与详情懒加载

1 participant