Repository navigation
fix: count all post-fold requests in cache ledger turnsToNextFold (#1286) - #1289
Conversation
📦 Built Package ArtifactBranch: Option A — Install from npm PR tag (recommended)npm install -g billion-context@pr-1289Each push to this PR publishes a new version under the Option B — Download artifact
tar xzf billion-context-pr1289.tgz
npm install -g packageInstall instructions are refreshed on each push. |
The consumedFoldSeq gate served two roles in recordCacheSample: gating the decomposeSample pending list (compRepay attribution — must fire once per fold) AND driving the per-fold requestsAfter counter. Because each fold left pendRefs after its first post-fold sample, requestsAfter froze at 1 and pushFold's k = requestsAfter freeze capped turnsToNextFold at 1 for every fold regardless of actual cadence. The kernel batch reference path (buildCacheReport) counts ALL samples after a fold into requestsAfter and the inter-fold window into turnsToNextFold, so the incremental ledger diverged from batch for any fold followed by >= 2 requests: paidBack was systematically false (breakeven is usually >= 2) and savedSoFar/grossSaved/netTokens were understated by ~(N-1)x for every provider. Decouple the concerns: the counting loop now iterates every elapsed fold (f.at <= s.at); the consumedFoldSeq gate remains on the pending list only, so compRepay/T attribution semantics are unchanged. With counting fixed, the existing pushFold freeze of k = requestsAfter becomes exactly the batch-path turnsToNextFold window under chronological sample arrival — no new persistent field, no ledger format change. Adds a regression fixture with three full-hit samples between folds (S=12140/sigma=1000 -> breakevenTurns ~2.5): asserts field-for-field parity between incremental and kernel batch reports (turnsToNextFold, paidBack, requestsAfter, savedSoFar, economics summary), plus hand-computed values. Before the fix this fixture failed with k=1/paidBack=false vs batch k=3/paidBack=true; the existing single-sample fixture could not catch the bug because k=1 coincides on both paths there. Behavior change (disclosed): turnsToNextFold in /acp_cache fold economics goes from min(1, actual cadence) to the true inter-fold request count; paidBack flips false->true where measured cadence actually reaches breakevenTurns; savedSoFar/grossSaved/netTokens scale up accordingly. Fixes #1286
bacfbb4 to
99ef653
Compare
自审完成,结论:可以合并。以下是按 review 规范走下来的完整结论。 ① 原 issue 方向(#1286)复现已在 #1286 审核阶段实测确认(增量 k=1/paidBack=false/savedSoFar=11140 vs 批量 k=3/paidBack=true/savedSoFar=33420,breakevenTurns≈2.5009 两路一致)。根因是 ② 回归审查(既有行为清点)diff 只碰
③ 代码审查计数循环改为遍历所有 ④ 测试有效性新 fixture(fold 间 3 个全命中样本)断言增量 vs 批量 15 个 economics 字段 + 8 个 summary 键逐字段一致,外加手算期望(breakevenTurns≈2.5008976660682225、savedSoFar=33420)。我实测了反向验证:把 ⑤ Rebase + pre-flight(head
|
- ci.yml test job: run the kernel's own unit suite (npm --prefix kernel test) on every PR — AGENTS.md Kernel Boundary rule 5 requires BOTH test layers green, but only release-kernel.yml ran the kernel suite. - ci-image.yml / ci-e2e.yml: track kernel/** in the path filters (a kernel change IS a pipeline change, same #1289-class rationale the old registry-pin bump triggered on) and refresh the stale 'pinned exactly' comment wording.
Closes #1286
Root cause
recordCacheSample(src/cache-ledger.ts) usedconsumedFoldSeqfor two different jobs:decomposeSamplepending list — compRepay attribution, where each fold must fire exactly once (correct semantics);requestsAfter += 1counter — but each fold leftpendRefsafter its FIRST post-fold sample, sorequestsAfterfroze at 1 andpushFold'sk = requestsAfterfreeze cappedturnsToNextFoldat 1 for every fold.The kernel batch reference path (
buildCacheReport) counts ALL samples after a fold intorequestsAfter, and the inter-fold window intoturnsToNextFold. So for any fold followed by >= 2 requests the two paths diverged. Measured on the issue's repro timeline (cold req -> F1 S=12140 sigma=1000 -> 3 full-hit reqs -> F2):breakevenTurns was identical on both paths — pricing was never wrong; only k was capped, so
paidBackwas systematically false for ALL providers and savedSoFar/grossSaved/netTokens were understated by ~(N-1)x.Fix
Decouple the two concerns (the approach suggested by the issue):
f.at <= s.at) — no seq gate;consumedFoldSeqgate remains on the pending list only, so compRepay/T attribution semantics are unchanged;pushFoldfreezek = requestsAfterbecomes exactly the batch-path window under chronological sample arrival — no new persistent field, no ledger format change, no migration.Behavior change (disclosed, per issue requirement)
This ships as its own PR — deliberately NOT folded into #1279 / PR #1285's priceProfile pass-through. Reported values in
/acp_cachefold economics change:turnsToNextFold: oldmin(1, actual cadence)-> new = actual inter-fold request count;paidBack: flips false -> true wherever measured cadence actually reachesbreakevenTurns(this is the bug being corrected — old values were systematically false);savedSoFar/grossSaved/netTokens: scale up from the x(N-1)-understated figures to the true values.No wire-protocol, config-schema, or persistence-format change. Both compression modes unaffected (ledger math only).
Regression test
New fixture with THREE full-hit samples between folds (S=12140/sigma=1000 -> breakevenTurns ~2.5): asserts field-for-field parity between the incremental ledger and the kernel batch report across every economics field (seq/at/S/sigma/Vprime/hPct/T/requestsAfter/savedSoFar/turnsToNextFold/netTokenDelta/oneTimeCostUnits/perTurnSavingUnits/breakevenTurns/paidBack) plus the economics summary, with hand-computed expectations. Before the fix it fails with k=1/paidBack=false vs batch k=3/paidBack=true. The pre-existing single-sample fixture could not catch the bug because k=1 coincides on both paths there — this is the direct gap the issue calls out.
Pre-flight (branch head bacfbb4, base master 088bb34)
npm run typecheck— cleannpm test— 2448 pass / 3 fail; all 3 failures are pre-existing env-dependenttests/claude-native.test.tslive-process e2e tests (kill ESRCH/ 60s spawn timeouts), verified to fail identically with the two changed files reverted to their 088bb34 statenpm run build— success