Repository navigation
fix: resolve knobs from a content-keyed config cache and parse the outbound body once (#2078) - #2087
Conversation
…tbound body once (#2078)
📦 Built Package ArtifactBranch: Option A — Install this exact build (recommended)npm install -g billion-context@0.0.2121-pr.2087.dd8bcedThis exact version is immutable and reproducible. The rolling Option B — Download artifact
tar xzf billion-context-pr2087.tgz
npm install -g packageInstall instructions are refreshed on each push. |
[bot] 🏷 Review 完成。PR #2087(单 commit 结构化七问1. 是什么问题 2. 什么原因导致的 3. 怎么修复的
4. 是否有回归问题
5. 是否破坏缓存 6. 是否改了设计方向 7. 是否引入了新的配置 证据本地定向验证:typecheck 干净;4 个相关测试文件 28/28 通过(config-file-cache 7、issue2078-outbound-estimate 3、wire-overhead 含新增、knobs-config-precedence);build 成功(含 import-annotations 检查)。CI:head commit 上 16/16 全绿。分支落后 master 4 个提交,但那些提交只碰 workflows/CONFIGURATION.md,与本 PR 零文件重叠,rebase 无冲突风险。diff 整洁:7 文件 +303/-29,每一行都服务于 #2078,无跑题改动。 判定:可合并。 无阻塞项,review 中未发现需要顺手修或立项的小问题。 |
📦 Merged Build PublishedThis merge to master was published as npm install -g billion-context@0.0.15-master.0a5f35b # this exact build
npm install -g billion-context@master # always the newest merged build
|
Closes #2078
What
Two per-request hot-path costs introduced by #2030 (knobs) and the #1987/#2058 seam-forensics stamping:
readFileSync+JSON.parseper request (server.tsalone has ~45 knob call sites).estimateWireOverhead, once inside the image-reserve raw scan.How
Config cache (
src/config.ts).loadConfigFile()now keeps one entry keyed by the file's RAW TEXT: the read happens per call (page-cache hit, ~µs),JSON.parseonly when the content actually changed. Hot-reload stays exact — any external rewrite is visible on the next call; a missing file is never cached (a reappearing file shows immediately); corrupt-but-present content is cached as{}so one bad save does not re-log and re-fail-parse on every knob call until fixed. Every consumer of the returned object was audited read-only.Parse-once estimate (
src/server.ts,src/server/budget.ts). The seam block performs ONEJSON.parse(sentBody)and hands the result tooutboundContextEstimatethrough a new optional third-state parameter:undefined= parse here (legacy behavior for any other caller), object = project from it,null= caller already tried and failed → keep the prepared projection and give the helpers"", which deterministically yields 0 exactly like today's failed-parse results.estimateWireOverheadaccepts a pre-parsed object and skips its own parse; all existing string/Buffer callers are byte-identical.Why not the issue's suggested mtime/size key
Measured: back-to-back rewrites can land on the SAME sub-ms
mtimeMson common filesystems (the new tests pin this), so a stat-keyed cache deterministically violates this repo's own hot-reload contract test — with a stat key,tests/knobs-config-precedence.test.ts"hot reload" went red locally (expected 222, got stale 111). Content keying keeps hot-reload exact while still removing the expensive half. Warm page-cache bench at a 29 KB config: read+parse 29.9 µs/call → read+compare 16.6 µs/call (stat-only would be 2.8 µs but is unsound); the saving fraction grows with config size.Behavior changes (disclosed)
[acp-config] failed to parse …error log now fires once per distinct corrupt content instead of once perloadConfigFile()call (previously up to ~45× per request of duplicate log spam; ENOENT silence unchanged). Improvement, not regression.JSON.stringify([])). Stats bookkeeping only, no gating impact. Every VALID body produces bit-identical numbers (same string → same parse result → same projection/overhead/image terms).Not changed: the ENV tier stays live per call; no new config keys / env vars / CLI flags (config surface untouched); no persistence format, wire shape, or protocol changes. Both compression modes reasoned: plugin mode suppresses tool/nudge injection and shares this same estimate path with unchanged semantics; proxy mode semantics identical.
outboundContextEstimatehas exactly one production caller (the seam block) — nothing else migrated.Tests
tests/config-file-cache.test.ts(7): cache-hit reference equality; hot-reload visibility incl. same-size rewrite with restored mtime (content is truth); identical-content rewrite served from cache without re-parse; missing file →{}; corrupt file logs once per distinct corruption (viasetLogCapture);BILI_CONFIG_FILEswitch re-resolves.tests/issue2078-outbound-estimate.test.ts(3): pre-parsed ≡ legacy self-parse across all four protocols (with codec-live >0 assertions so the equivalence cannot hold vacuously); garbage body: null mode ≡ legacy fallback ≡ 0; size-diff liveness guard in both modes.tests/wire-overhead.test.ts:estimateWireOverhead(proto, obj) ≡ estimateWireOverhead(proto, JSON.stringify(obj))per protocol.tests/knobs-config-precedence.test.ts) passes UNCHANGED — it is what caught the stat-key design locally.Verified locally: targeted suites green (28/28),
npm run typecheckclean,npm run buildsuccess. Full regression delegated to CI.