Repository navigation
Stale-guard ownership fast path: skip the per-save envelope read (#3049) - #3286
Conversation
…elope read (#3049) The #405/#2401 rollback guard re-read and re-parsed the ENTIRE session envelope on every save just to compare two monotonic counters — ~320 ms and a full file read per turn on >40 MiB sessions (a large slice of the read amplification in #3049's production reports). In shadow/full mode the guardedBuild closure now records an ownership token (counters of the payload it just ingested into the unified row + its build time). The next guard proves 'no other writer committed since' without reading the file: 1. unified row counters === token counters — every sidecar instance sharing the dir shares index.db, and each commit ingests BEFORE it writes the file, so a sibling's newer commit bumps the row first; 2. envelope mtime not newer than the token's build (+1.5 s grace for our own write latency) — catches writers that never touch the index (legacy-mode sibling, manual edit); 3. incoming counters not behind the token — defers any surprise to the exact full read. Legacy-only installs keep the exact per-save full read. The rekey, #408-clamp and restoreReplica direct-write paths drop or re-stamp the token so it never describes a payload it did not ingest. Deliberate residual (narrower than an unconditional size/mtime ownership skip): a sibling whose sidecar is disabled writing within the grace window is missed for one cycle; its next write hits the exact guard again. Tests: single-instance collapse (12 saves -> <=2 full reads, counters exact), legacy exactness (one full read per save), cross-instance commit detection through the shared row, foreign file write detection through mtime, interleaved storm stays exact with the sidecar shared.
📦 Built Package ArtifactBranch: Option A — Install this exact build (recommended)npm install -g billion-context@0.0.3849-pr.3286.7d0c836This exact version is immutable and reproducible. The rolling Option B — Download artifact
tar xzf billion-context-pr3286.tgz
npm install -g packageInstall instructions are refreshed on each push. |
|
review |
[bot] 🏷 (ai: vllm-qwen/qwen3.8-27b) 收到 review 请求。查重: 不重复——本 PR 是 #3049 保存侧守卫读放大(read-amplification)的唯一在途修复;同域相邻项 #3275(写放大 pin,纯测试)与 #3020/#3250(Phase 3 / WAL reclaim)机制不同,无重叠。 开始按规范审:原 issue 方向 → 既有行为回归清点 → 代码 → 测试有效性 → diff 整洁度。PR head 已 rebase 到最新 master( |
…view) #3286 review: statEnvelope returned { size, mtimeMs } but only mtimeMs is consumed (ownershipFastPath). Dead field removed per the no-dead-code rule.
(ai: vllm-qwen/qwen3.8-27b) Review 结论:可合并(已顺手修掉一处死字段,见下)独立审核说明(不照抄 PR 描述):checkout PR head 逐段读了 guardedBuild / staleDiskPayload / loadEnvelope / statEnvelope / token 生命周期与 store schema,本地跑了定向测试套件,并拉了 CI 失败日志逐条核对。以下结论全部基于自己的验证。 结构化八问
测试有效性(质量关)5 个 pin 有牙:T1 断言磁盘计数器精确 + 全读 ≤2 次(无快路径会是 12 次);T2 断言 legacy 恰好 5/5 次全读;T3/T4/T5 分别钉住三个逃生门且带内容 + warn 断言;sleepSync 用 Atomics.wait,确定性无 flake。缺口:文档化 residual(disabled-sidecar 兄弟在 grace 内写入)没有直接 pin——可接受,属刻意残留且模拟 3-strike 代价高。 CI 状态(已拉日志核对)Run@0db928099:30 项检查 26 绿。全部 guard job(kernel-guard / pi-subagents-guard / config-surface-guard / version-guard / unused-exports / lock-sync / static-gates×4)与全部 E2E lane(real codex / fake-codex / persona / pi-codex-ws / native-dsh/pi/opencode / local-registry)绿。4 红全部是 shard-1 测试 lane(ubuntu/windows × 22/24),日志确认是同一个既有用例: 顺手小修(已推 PR 分支)e3d6d35: 主要风险判断(residual)刻意留下的残留:sidecar 被禁用的兄弟(3-strike drift)在我们构建后 ~1.5s 内写入更新的 envelope,会被漏一个周期;该兄弟存活时其下次写入命中精确守卫即自愈,若在下次写入前崩溃,其已保存的那轮 turns 会丢到我们内存推进为止。对比 option 2 无条件 size/mtime skip(尺寸不变就一直漏),这是严格更窄的取舍,且用罕见多进程场景换主流单实例路径的 320ms/turn。判定:可接受,PR 描述对它的刻画准确。 |
(ai: vllm-qwen/qwen3.8-27b) 这条是 floor 3「review」的重复投递——该请求已在上方按八问体例完整答复(结论:可合并),线程里没有新的用户输入;github-actions 的构建产物评论无需处理。补充两条最新状态:
判定维持:可合并。 |
(ai: vllm-qwen/qwen3.8-27b) CI 预测核对完毕(拉了失败 shard 日志):head e3d6d35 上首个完成的测试 shard(ubuntu 24, shard-1)2887 用例仅 1 败—— |
📦 Merged Build PublishedThis merge to master was published as npm install -g billion-context@0.0.478-master.7f8fbbb # this exact build
npm install -g billion-context@master # always the newest merged build
|
…; output-restore floor respects proven budgets (#3406 items 2+4) Item 2 — the #3286 ownership fast path judged envelope ownership by a builtAtMs+1.5s grace window, so a sibling writing inside that window (legacy mode, sidecar disabled — never touches index.db) was adopted as ours and its envelope clobbered on the next save. The token now records the stat taken right after OUR write landed (size+mtime); the fast path fires only when the file still matches that stamp byte-for-byte. Unstamped tokens (debounced kernel-internal saves, restore-replica seeds, crash before stamping) always take the full read. Residual window shrinks from 1.5s to ~µs. Item 4 — restoreOutputBudget's no-ceiling branch raised a session's recorded high-water to OUTPUT_RESTORE_ABSOLUTE_FLOOR (32768); providers hard-capping maxOutputTokens below that reject the raised value outright (400). A high-water at/above the new trust line (4096, aligned with OUTPUT_CLAMP_MIN_OUTPUT) is the provider-accepted budget and is restored as-is, never raised above; missing/below-line high-waters keep the #3071 floor (born-starved, fork rattle, death-spiral decay chains). Item 3 (label resume vs background scan race, #3214) — NOT changed: no low-risk mutex exists without a persistence-format/index change (details in the PR body); deliberately stopped per the risk gate.
Fixes the read-amplification half of #3049 (the production report's ~320 ms/turn guard cost and its share of the 1.42 TB/day I/O).
What
The #405/#2401 rollback guard re-read + re-parse of the ENTIRE envelope on every save — just to compare two monotonic counters — is now skipped whenever the saving process can PROVE nothing else committed since its last build:
index.db(#394 multi-process) — their commit bumps the row before it writes the fileAll three hold ⇒ the disk record is exactly the one our chain last wrote (or converged to) and the in-process comparison suffices. Any miss ⇒ the exact full read runs unchanged.
What it does NOT change
jsonWriter=offterritory). This PR kills the READ side of the guard only.Deliberate residual
A sibling whose sidecar is DISABLED (3-strike drift) writing a newer envelope within the ~1.5 s grace window is missed for one cycle; its next write hits the exact guard again — strictly narrower than the unconditional size/mtime ownership skip evaluated on the issue (option 2). Crash-safe by construction: a fresh process starts with an empty token map, so the next boot always takes the full read; the rekey / #408-clamp / restoreReplica direct-write paths drop or re-stamp the token.
Tests
tests/persist-guard-ownership.test.ts— 5 new pins: single-instance collapse (12 saves → ≤2 full reads, disk counters exact), legacy exactness (one full read per save), cross-instance commit detection (shared row), foreign file write detection (mtime), interleaved storm exact with the sidecar shared. Full persist/storage regression batch green locally (multi-instance 16, tail/404/json-writer-off 16, unified/replicas/memory 28, encryption/bounded/corpus/ccr 25); tsc + unused-export gates clean.