From 99ef653f66e841b6cc71937f085f46971a1cdd44 Mon Sep 17 00:00:00 2001 From: ework-agent Date: Fri, 25 Sep 2026 00:28:40 +0800 Subject: [PATCH] fix: count all post-fold requests in cache ledger turnsToNextFold MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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 --- src/cache-ledger.ts | 10 ++++++- tests/cache-ledger.test.ts | 54 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 63 insertions(+), 1 deletion(-) diff --git a/src/cache-ledger.ts b/src/cache-ledger.ts index b619150beb..756e10efa9 100644 --- a/src/cache-ledger.ts +++ b/src/cache-ledger.ts @@ -108,6 +108,10 @@ export function getCacheLedger(session: Session): CacheLedger { } function pushFold(led: CacheLedger, f: Omit): void { + // Freeze k = samples since each open fold (#1286): samples arrive in + // order, so at this instant the count equals the batch-path + // turnsToNextFold window (f.at < s.at <= nextFold.at); requestsAfter + // keeps growing afterward to the full post-fold total. for (const open of led.folds) { if (open.k === null) open.k = open.requestsAfter; } @@ -177,7 +181,11 @@ export function recordCacheSample(session: Session, s: { at: number; input: numb led.consumedFoldSeq = Math.max(led.consumedFoldSeq, hi); } const hitPct = s.input > 0 ? round1((s.cached / s.input) * 100) : 0; - for (const f of pendRefs) { + // #1286: turn counting is decoupled from compRepay attribution — the + // consumedFoldSeq gate above applies to the pending list only. Every + // elapsed fold counts EVERY later sample, matching buildCacheReport's + // full post-fold requestsAfter window. + for (const f of led.folds) { if (f.at <= s.at) { f.requestsAfter += 1; if (f.hPct === null) f.hPct = hitPct; diff --git a/tests/cache-ledger.test.ts b/tests/cache-ledger.test.ts index f3dba50290..7104bea999 100644 --- a/tests/cache-ledger.test.ts +++ b/tests/cache-ledger.test.ts @@ -99,6 +99,60 @@ test("incremental ledger closes the same identity as kernel batch", () => { assert.equal(f1.turnsToNextFold, 1); }); +test("incremental k spans ALL intermediate samples (parity with batch, #1286)", () => { + const session = makeSession(); + withView20(session); + recordCacheSample(session, { at: T0 + 1000, input: 10000, cached: 0 }); + // F1: S=12140, σ=1000 → breakevenTurns ≈ 2.5009 at the default {w:1,r:0.1,q:4}. + recordCacheFoldsFromBlocks(session, [block("b1", T0 + 1500, 12140, 4000, "m00010")], { V: 10000, Vp: 5000 }); + recordCacheSample(session, { at: T0 + 2000, input: 10000, cached: 10000 }); + recordCacheSample(session, { at: T0 + 2600, input: 10000, cached: 10000 }); + recordCacheSample(session, { at: T0 + 3200, input: 10000, cached: 10000 }); + recordCacheFoldsFromBlocks(session, [block("b2", T0 + 4000, 2000, 4000, "m00015")], { V: 10000, Vp: 8000 }); + + const rawSamples: CacheSample[] = [ + { at: T0 + 1000, input: 10000, cached: 0 }, + { at: T0 + 2000, input: 10000, cached: 10000 }, + { at: T0 + 2600, input: 10000, cached: 10000 }, + { at: T0 + 3200, input: 10000, cached: 10000 }, + ]; + const rawFolds: FoldEvent[] = [ + { at: T0 + 1500, tokensCompressed: 12140, summaryTokens: 1000, firstFoldStartTokens: 4500, viewBefore: 10000, viewAfter: 5000 }, + { at: T0 + 4000, tokensCompressed: 2000, summaryTokens: 1000, firstFoldStartTokens: 7000, viewBefore: 10000, viewAfter: 8000 }, + ]; + + const inc = buildSessionCacheReport(session); + const batch = buildCacheReport(rawSamples, rawFolds); + assert.equal(inc.folds.length, batch.folds.length); + for (let i = 0; i < inc.folds.length; i++) { + const a = inc.folds[i]!; + const b = batch.folds[i]!; + for (const key of ["seq", "at", "S", "sigma", "Vprime", "hPct", "T", "requestsAfter", "savedSoFar", "turnsToNextFold", "netTokenDelta", "oneTimeCostUnits", "perTurnSavingUnits", "breakevenTurns", "paidBack"] as const) { + assert.deepEqual(a[key], b[key], `fold ${i} ${key}: incremental ${String(a[key])} !== batch ${String(b[key])}`); + } + } + for (const key of ["folds", "grossSaved", "repayCost", "summaryCost", "netTokens", "paidBackCount", "notPaidBackCount", "unobservedCount"] as const) { + assert.deepEqual(inc.economics[key], batch.economics[key], `economics.${key}`); + } + + // Hand-computed expectations: before #1286 the incremental path froze + // F1.k at 1 (consumedFoldSeq gate ended its counting after one sample), + // making paidBack false even though cadence (3) passes breakeven (≈2.5). + const f1 = inc.folds.find((f) => f.seq === 1)!; + assert.equal(f1.requestsAfter, 3); + assert.equal(f1.turnsToNextFold, 3); + assert.equal(f1.hPct, 100); + assert.equal(f1.T, 0); + assert.ok(Math.abs(f1.breakevenTurns! - 2.5008976660682225) < 1e-9); + assert.equal(f1.paidBack, true); + assert.equal(f1.savedSoFar, (12140 - 1000) * 3); + const f2 = inc.folds.find((f) => f.seq === 2)!; + assert.equal(f2.turnsToNextFold, null); + assert.equal(f2.paidBack, null); + assert.equal(f2.requestsAfter, 0); + assert.equal(inc.totals.balanced, true); +}); + test("cold-start miss lands in the ttl bucket; pure append is new content", () => { const session = makeSession(); recordCacheSample(session, { at: T0 + 1000, input: 1000, cached: 0 });