From a8a3031b583ec306c39677e5b0cf98d925cef461 Mon Sep 17 00:00:00 2001 From: iamtoruk Date: Wed, 2 Sep 2026 07:32:02 -0700 Subject: [PATCH] fix(cache): DAILY_CACHE_VERSION 30 collides with open PR #1132 Two open branches claim daily-cache version 30, so a v30 file on disk carries unknown accounting. Bump this branch to 31 and move the Hermes pending-rederive contract version with it, so a v30 file written under #1132's accounting is treated as a foreign prior version: carried forward per slice, never adopted as the finalized base. The #946 copilot fixture seeded itself at DAILY_CACHE_VERSION - 5, a moving target that crossed the copilot contract boundary (26) at this bump and silently stopped granting the entitlement those tests exist to check. Pinned to 25, which is the fixed historical version the fixture actually means. Disclosure: the bump re-derives every day whose sources still survive, so finalized history shifts. On the reviewer's machine Claude moved -0.78 percent. That is a corrected figure replacing a stale cached one, not a loss, but it is a visible change to numbers users have already seen. --- src/daily-cache.ts | 9 ++-- tests/daily-cache-carry-forward.test.ts | 9 +++- .../daily-cache-version-rederivation.test.ts | 49 +++++++++++++++++++ 3 files changed, 62 insertions(+), 5 deletions(-) diff --git a/src/daily-cache.ts b/src/daily-cache.ts index f66ae102..c2119a92 100644 --- a/src/daily-cache.ts +++ b/src/daily-cache.ts @@ -174,10 +174,11 @@ import type { DateRange, ProjectSummary } from './types.js' // but optimize added reasoning again. Re-derive so report matches the live parse. // v29: #1118 OrcaRouter route pricing (fusion aliases + peel; auto stays unpriced). // v28 on main already shipped #1115. -// v30: Hermes cost provenance. Subscription-included sessions stay $0, +// v31: Hermes cost provenance. Subscription-included sessions stay $0, // explicit estimates retain their status, and surviving Hermes sources replace // v29 slices produced by the old API-equivalent fallback. -export const DAILY_CACHE_VERSION = 30 +// 31: #1234 Hermes cost contract; 30 is claimed by #1132. +export const DAILY_CACHE_VERSION = 31 const MIN_SUPPORTED_VERSION = 28 /// Providers whose per-day CALL COUNT means something different at @@ -202,7 +203,9 @@ const MIN_SUPPORTED_VERSION = 28 /// untouched, in both directions, and every other provider keeps the guard. const PENDING_REDERIVE_PROVIDER_VERSIONS: Readonly> = { copilot: 26, - hermes: 30, + // Tracks DAILY_CACHE_VERSION: a v30 file may have been written by #1132's + // accounting, which never carried the Hermes cost contract. + hermes: 31, } function providersPendingRederiveFrom(fromVersion: number): string[] { diff --git a/tests/daily-cache-carry-forward.test.ts b/tests/daily-cache-carry-forward.test.ts index 894bc799..3d3ea756 100644 --- a/tests/daily-cache-carry-forward.test.ts +++ b/tests/daily-cache-carry-forward.test.ts @@ -736,11 +736,16 @@ describe('#946: a migration re-derives copilot instead of carrying it', () => { /// Seeds an older-version daily cache (what a 0.9.20 install leaves behind) /// so `loadDailyCache` takes the adoption path. + /// Pinned, not derived from DAILY_CACHE_VERSION: this fixture is the + /// pre-copilot-contract era, which is a fixed point in history. + /// `DAILY_CACHE_VERSION - 5` drifted across the copilot boundary (26) on the + /// next bump and silently stopped seeding the entitlement these tests check. + const PRE_COPILOT_CONTRACT_VERSION = 25 const seedOlderCache = async (days: DailyEntry[]) => { await writeFile( - join(TMP_CACHE_ROOT, `daily-cache.v${DAILY_CACHE_VERSION - 5}.json`), + join(TMP_CACHE_ROOT, `daily-cache.v${PRE_COPILOT_CONTRACT_VERSION}.json`), JSON.stringify({ - version: DAILY_CACHE_VERSION - 5, + version: PRE_COPILOT_CONTRACT_VERSION, savingsConfigHash: 'cfg-A', tzKey: currentTzKey(), lastComputedDate: daysAgoStr(1), diff --git a/tests/daily-cache-version-rederivation.test.ts b/tests/daily-cache-version-rederivation.test.ts index 2a3173aa..f40e3fc0 100644 --- a/tests/daily-cache-version-rederivation.test.ts +++ b/tests/daily-cache-version-rederivation.test.ts @@ -4,8 +4,10 @@ import { join } from 'path' import { tmpdir } from 'os' import { + DAILY_CACHE_VERSION, currentTzKey, ensureCacheHydrated, + loadDailyCache, toDateString, type DailyEntry, } from '../src/daily-cache.js' @@ -107,3 +109,50 @@ describe('daily-cache re-derivation on a DAILY_CACHE_VERSION bump', () => { expect(JSON.parse(await readFile(oldPath, 'utf8'))).toEqual(oldCache) }) }) + +// v30 is claimed by two open branches at once: this one and #1132. A v30 file +// on disk therefore carries UNKNOWN accounting, so adopting it as the +// finalized base would serve #1132's numbers under this branch's contract. +describe('daily-cache adoption of a v30 file written under a different accounting', () => { + it('carries its days forward but never adopts it as the finalized base', async () => { + const date = toDateString(new Date(Date.now() - 7 * 24 * 60 * 60 * 1000)) + const yesterday = toDateString(new Date(Date.now() - 24 * 60 * 60 * 1000)) + const foreign = day(date, 42) + foreign.providers = { + hermes: { + calls: 1, + cost: 42, + savingsUSD: 0, + sessions: 1, + inputTokens: 100, + outputTokens: 20, + cacheReadTokens: 30, + cacheWriteTokens: 0, + }, + } + await writeFile( + join(cacheRoot, 'daily-cache.v30.json'), + JSON.stringify({ + version: 30, + savingsConfigHash: 'cfg', + tzKey: currentTzKey(), + lastComputedDate: yesterday, + days: [foreign], + complete: true, + watermarkTrusted: true, + }), + ) + + const loaded = await loadDailyCache() + + expect(DAILY_CACHE_VERSION).toBe(31) + expect(loaded.version).toBe(31) + // A v30 candidate must not satisfy the same-version adoption at + // adoptOlderDailyCaches, so its trust markers cannot survive. + expect(loaded.complete).toBe(false) + expect(loaded.watermarkTrusted).toBe(false) + const carried = loaded.days.find(entry => entry.date === date) + expect(carried?.carried).toBe(true) + expect(loaded.pendingRederive).toContain('hermes') + }) +})