Repository navigation
feat: session-file GC — sweep stale small state files (#1082) - #1084
Conversation
📦 Built Package ArtifactBranch: Option A — Install from npm PR tag (recommended)npm install -g billion-context@pr-1084Each push to this PR publishes a new version under the Option B — Download artifact
tar xzf billion-context-pr1084.tgz
npm install -g packageInstall instructions are refreshed on each push. |
bd05c48 to
d05c26e
Compare
Review of #1084 (session-file GC) — rebased onto current master, all gates green. The PR base had gone stale (master moved to v0.1.133 /
Diff cleanliness: single commit, 9 files, +614/−1, every line related to the feature. No lock-file churn, no version bump, no acp-kernel pin change, no wire-protocol change. Design fidelity (verified against the settled thread in #1082):
Minor findings (non-blocking, reporting per review discipline):
Verdict: implementation matches the agreed design, rails are conservative, evidence is green on the rebased head. This is a 已将分支 rebase 到当前 master(v0.1.133)并全量复验(typecheck/build/2180 项测试全绿),实现与 #1082 讨论确定的设计一致、diff 干净,可以合并。 |
CI caught one thing — fixed directly on the branch (commit The Root cause: the new Fix: Re-verified on the fixed head: Prior review verdict stands — once CI is green the PR is ready for you to merge: #1084 CI 暴露了测试临时目录助手在 runner 上(无 TMPDIR、无 .tmp/)的 ENOENT,已直接在分支上改用全仓库通行的 os.tmpdir() 并复验全绿,功能代码不受影响。 |
CI is fully green on the fixed head Final state of #1084:
Ready for you to merge: #1084 (merge stays with you — I can't merge PRs). 修复了测试临时目录在 CI 上的 ENOENT(改用 os.tmpdir()),分支已 rebase 到最新 master,本地与 CI(含全平台测试矩阵和 codex E2E)全部通过,可以合并。 |
ranxianglei
left a comment
There was a problem hiding this comment.
审查结论:FIX-FIRST(rebase + 两处修复后重审)
骨架是好的:kill switch(BILI_SESSION_GC=0)、resident/pending 守卫(dropSessionForGc 防抖写复活)、损坏/不可读文件一律不动只计数、测试全确定性(注入 now + utimesSync,吸收了 #360 教训)。以下按严重度:
🔴 HIGH(与 #1083 的跨 PR 语义冲突):readRawFile 让 GC 对默认格式文件整体失效
src/persist.ts:489-504(本 PR)的 head-peek 只解 BILIENC1:head.equals(ENCRYPT_MAGIC) ? codec.decode(raw) : raw.toString("utf8")。#1083 一合,默认输出是 BILIZSTD1 → 走 utf8 分支 → JSON.parse 抛 → unreadable → 永不删除。GC 对所有默认格式文件静默 no-op,整个功能失效且无任何报错。 修法一行:无条件 this.codec.decode(raw)(decode 本就按 magic 分发、明文直通)。本 PR base 早于 #1083,rebase 时必须带上这个适配——这正是热文件上两个并行 PR 的潜伏断裂(§7.2)。
[Medium] rawInputTokens 只数文本,图片重的会话被误判可删
estimateRawBodyTokens(src/preflight.ts:171-187)只走字符串键;记录在 src/server.ts:1468;isGcEligible(session-gc.ts:116)在值存在时独占信任。图片为主的闲置会话(如 400K 图 token + 50K 文本)记 50K → eligible → 删除 → resume 重新灌满窗口——恰好是设计注释里说要防的事故。建议把图片 token 估计并入记录值,或对含图轮次回退 blocks+context 启发。
[Medium] 7 天默认删除需要 owner 在合并时明确签字
session-gc.ts:44-47 注释自认 "Owner decision… aggressive by design"。代价要说清:删除后 bili export 再也无法渲染该会话(proxy 侧折叠视图/块摘要是唯一记录,raw 历史从不持久化);重建成本=全新 preflight 折叠+模型压缩 token+冷 prefix cache。现在只有一行聚合日志。建议:BILI_SESSION_GC_DRY_RUN=1 + 每文件 debug 行(符合 §7.3 日志纪律)。默认开启+无 dry-run 的数据删除,按本仓库的数据丢失保守度需要显式 re-confirm。
小项
- [Low] 多实例:resident 守卫是进程内的;共存实例跑
BILI_PERSIST=0对共享BILI_SESSIONS_DIR不刷 mtime,其活会话文件会老化被扫。7d 门实际兜底,但至少文档提一句。 - [Low] TOCTOU(
session-gc.ts:186-205):扫描中到来的请求会重建会话/文件——良性(最坏冷重建),无需动作。 - [Process]
mergeable_state: behind→ rebase + 全量门禁;预期 HIGH#1 在 rebase 时暴露。
|
Fix follow-up on the earlier review (#1084 (review)): both blockers are fixed on this branch in 4c90034 (rebased over 147fcab), gates green (typecheck / 2175 tests / build). 1. 2. Remaining open item from the review (not code): the 7-day default still needs explicit owner sign-off at merge time — data deletion with no dry-run deserves a conscious yes. @ework — please re-review the current head 4c90034 in full (fresh pass over the whole diff, not just these two hunks). |
Low item from #1084 review: a co-resident instance that does not persist (e.g. BILI_PERSIST=0) never refreshes mtimes, so its live session files can age out under a shared BILI_SESSIONS_DIR. Worst case is the same bounded cold rebuild, backstopped by the age gate — documented in both languages.
4c90034 to
1bd5152
Compare
Full re-review done — both blockers verified fixed, base cleaned up, all gates green. One process note first: the pushed head Blocker 1 — Blocker 2 — image tokens in Small item fixed directly on the branch (commit Gates on final head Remaining open item (yours, not code): the 7-day default + no dry-run still needs your explicit sign-off at merge time — deleting data without a rehearsal deserves a conscious yes. If you want 两个 blocker(readRawFile 格式无关化、rawInputTokens 计入图片 token)均已按根因修复并验证,分支已 rebase 到最新 master 剔除重复的 release/revert 提交、补上多实例文档说明,本地与 CI 全绿;剩 7 天默认值/dry-run 一项等你合并时拍板。 |
|
独立审查结论:approve(需 rebase 后合并)。 我把 PR head 语义审查(通过)
|
…le audit log (#1082) Rebased onto master v0.1.139 (b2f28c6): adopt createStorageCodec({key}) in tests (codec rename from the #1083 era), union #1090 hasProcessedState beside dropSessionForGc, keep master's updated BILI_ENCRYPTION_KEY doc row (drop pre-#1083 wording). Squashed of the five prior commits.
bcd7183 to
9e503f4
Compare
…nglei#1082) Low item from ranxianglei#1084 review: a co-resident instance that does not persist (e.g. BILI_PERSIST=0) never refreshes mtimes, so its live session files can age out under a shared BILI_SESSIONS_DIR. Worst case is the same bounded cold rebuild, backstopped by the age gate — documented in both languages.
Closes #1082
What & why
Short-lived sessions leave small state files under the sessions dir that are never resumed. The kernel
StateStorenever deletes files, so they accumulate forever. This adds a conservative disk sweep plus a per-turn rebuild-cost signal.Design (as settled in-thread with owner):
metadata.rawInputTokens— token estimate of the raw wire payload as received (text-only; binary fields excluded byestimateRawBodyTokens). Latest wins: history grows monotonically and shrinks after native-compaction boundaries, which is exactly when re-sending gets cheaper. No acp-kernel bump needed (metadatais bili's free-form bag).BILI_SESSION_GC_MAX_AGE_DAYS(default 7d, judged on decodedsavedAt);BILI_SESSION_GC_MAX_TOKENS(default 1M, owner-set):rawInputTokenswhen recorded, else no active compression blocks ANDcontextTokens≤ threshold (a file with live blocks can read small yet carry huge raw history — deleting it would overflow the window on resume).BILI_SESSION_GC_INTERVAL_MS(default 1h); kill switchBILI_SESSION_GC=0; gated on persistence being enabled.Safety rails:
BILIENC1files are decoded through the store's codec before judging (newSessionStore.readRawFile, codec-aware path-keyed twin of the id-keyed loaders);lastSeennot newer thansavedAt) — then it is dropped from the map via newdropSessionForGc()so the writer cannot resurrect the file;[gc] removed N … freed X KB).Files
src/session-gc.ts(new) — config, file-view parsing (v3 envelope + legacy flat, grouped-then-flat fallbacks mirroring persist.ts readers), eligibility predicate, sweepsrc/persist.ts—SessionStore.readRawFile(file)src/server.ts— per-turnrawInputTokensrecording (afteractivePack), boot sweep + unref'd intervalsrc/session.ts—dropSessionForGc(id)tests/session-gc.test.ts(new) — 10 tests: eligibility matrix, view parsing, env config, sweep keep/delete/corrupt/encrypted/resident/pending cases, and an end-to-end recording→persistence round-trip through a real proxy+upstreamCONFIGURATION.md/.zh-CN.mdenv rows,README.md/.zh-CN.mdsectionsPre-flight
npm run typecheck✅ · fullnpm test: 2092 pass / 0 fail (2 pre-existing skips) ✅ ·npm run build✅E2E_CHECK=1, codex binary + dist + upstream probe) ✅. Full 4-phase E2E could not be completed in this environment: warmup fails identically on clean master with changes stashed (warmup exit=1, empty output) — pre-existing sandbox/upstream issue, not this change. CI's manual-dispatch e2e workflow can cover it.