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') + }) +})