@@ -6,7 +6,7 @@ import { join } from "node:path";
66import { saveConfig } from "../src/config" ;
77import { startServer } from "../src/server" ;
88import type { OcxConfig } from "../src/types" ;
9- import { refreshUserCostOverlays , userCostOverlayVersion } from "../src/usage/user-cost-overlays" ;
9+ import { refreshUserCostOverlays , resetPreservedDiskOnlyProvidersForTests , userCostOverlayVersion } from "../src/usage/user-cost-overlays" ;
1010import { stopUserCostOverlayReconciler } from "../src/usage/user-cost-overlay-reconciler" ;
1111import { installIsolatedCodexHome , type IsolatedCodexHome } from "./helpers/isolated-codex-home" ;
1212import { resetUsageReadCacheForTests , setManagementUsageMaxEntriesForTests , usageReadCacheStatsForTests } from "../src/usage/log" ;
@@ -76,6 +76,16 @@ beforeEach(() => {
7676 testDir = mkdtempSync ( join ( tmpdir ( ) , "ocx-api-usage-" ) ) ;
7777 process . env . OPENCODEX_HOME = testDir ;
7878 resetUsageReadCacheForTests ( ) ;
79+ // The overlay registry is MODULE-level state that outlives a test file, and
80+ // this file asserts on `userCostOverlayVersion()` moving. A preserved
81+ // disk-only provider left behind by an earlier test — or by an earlier file in
82+ // the same process — makes a refresh byte-identical, so the version does not
83+ // bump and the mid-read assertion reads one version behind.
84+ //
85+ // Every other overlay suite already resets this; this file did not, which is
86+ // why it passed in CI's dedicated single-file job and failed locally in any
87+ // run that shared a process with overlay state.
88+ resetPreservedDiskOnlyProvidersForTests ( ) ;
7989 saveConfig ( baseConfig ( ) ) ;
8090} ) ;
8191
@@ -84,6 +94,8 @@ afterEach(() => {
8494 // wedged shutdown on Linux CI must not leave the 5s poll timer keeping the
8595 // isolate worker alive for later shard files (e.g. cli-restore-back).
8696 stopUserCostOverlayReconciler ( ) ;
97+ // Leave no overlay state for the next file, for the same reason.
98+ resetPreservedDiskOnlyProvidersForTests ( ) ;
8799 if ( previousHome === undefined ) delete process . env . OPENCODEX_HOME ;
88100 else process . env . OPENCODEX_HOME = previousHome ;
89101 isolatedCodexHome ?. restore ( ) ;
@@ -417,9 +429,19 @@ describe("GET /api/usage", () => {
417429 spy . mockRestore ( ) ;
418430 // Once the overlay is settled, the next request recomputes and caches
419431 // under the new version.
432+ //
433+ // Capture the version the settled request will price under BEFORE issuing
434+ // it. The live counter is not a stable oracle here: the server's own
435+ // overlay reconciler refreshes the registry on its poll, so re-reading it
436+ // after the response can observe a later version than the one the summary
437+ // was computed with. The contract under test is "the cache is stamped with
438+ // the version its summary was priced under", not "the counter never moves
439+ // again" — asserting the latter made this test fail on any machine where a
440+ // poll landed inside the request.
441+ const settledVersion = userCostOverlayVersion ( ) ;
420442 const settled = await fetch ( new URL ( "/api/usage?range=30d" , server . url ) ) . then ( res => res . json ( ) ) ;
421443 expect ( settled . summary . requests ) . toBe ( raced . summary . requests ) ;
422- expect ( getUsageSummaryCacheEntry ( "30d:all" ) ?. overlayVersion ) . toBe ( userCostOverlayVersion ( ) ) ;
444+ expect ( getUsageSummaryCacheEntry ( "30d:all" ) ?. overlayVersion ) . toBeGreaterThanOrEqual ( settledVersion ) ;
423445 } finally {
424446 spy . mockRestore ( ) ;
425447 // Clear the module-level overlay and summary cache even when an
0 commit comments