99 * scans only reparse files that changed, and a file that merely grew resumes
1010 * from its cached parse position so only the appended bytes are read.
1111 * OpenCode's SQLite reader queries the live database each scan so WAL writes
12- * remain visible. Antigravity databases are memoised in memory while the
13- * database and its WAL keep the same `(size, mtime, ctime)`.
12+ * remain visible. Antigravity databases are memoised while the database and its
13+ * WAL keep the same `(size, mtime, ctime)`, and the memo is persisted so an
14+ * unchanged history is not decoded again after a restart.
1415 *
1516 * Cursor's account API is slow, so its source answers from a cache and marks
1617 * itself `refreshing` while a background refresh runs; `awaitRefresh` waits
@@ -60,7 +61,13 @@ import { resolveCodexHomeLayout } from "../provider/Drivers/CodexHomeLayout.ts";
6061import { resolveAntigravityInstanceDirectories } from "../provider/antigravityAuthSupport.ts" ;
6162import { mergeProviderInstanceEnvironment } from "@t3tools/provider-core/server/instanceEnvironment" ;
6263import { readOpenCodeUsage } from "./opencodeUsageReader.ts" ;
63- import { makeAntigravityUsageCache , readAntigravityUsage } from "./antigravityUsageReader.ts" ;
64+ import {
65+ ANTIGRAVITY_USAGE_CACHE_FILE_NAME ,
66+ decodeAntigravityUsageCache ,
67+ makeAntigravityUsageCache ,
68+ makeAntigravityUsageCacheWriter ,
69+ readAntigravityUsage ,
70+ } from "./antigravityUsageReader.ts" ;
6471import {
6572 CURSOR_ACCOUNT_CACHE_FILE_NAME ,
6673 CURSOR_ACCOUNT_TTL_MS ,
@@ -226,6 +233,7 @@ export const make = Effect.gen(function* () {
226233 /** Cursor account caches by credential source. */
227234 const cursorCaches = new Map < string , CursorAccountCache > ( ) ;
228235 let cursorCacheDirty = false ;
236+ let antigravityCacheDirty = false ;
229237 /**
230238 * The last failed refresh per credential source, standing for a TTL so a
231239 * client refetching a broken login does not refetch Cursor each time. A
@@ -246,6 +254,7 @@ export const make = Effect.gen(function* () {
246254 const scanCachePath = path . join ( config . stateDir , SCAN_CACHE_FILE_NAME ) ;
247255 const legacyScanCachePath = path . join ( config . stateDir , LEGACY_SCAN_CACHE_FILE_NAME ) ;
248256 const cursorCachePath = path . join ( config . stateDir , CURSOR_ACCOUNT_CACHE_FILE_NAME ) ;
257+ const antigravityCachePath = path . join ( config . stateDir , ANTIGRAVITY_USAGE_CACHE_FILE_NAME ) ;
249258 const writeCacheFile = ( filePath : string , contents : string ) =>
250259 writeFileStringAtomically ( { filePath, contents } ) . pipe (
251260 Effect . provideService ( FileSystem . FileSystem , fileSystem ) ,
@@ -449,6 +458,10 @@ export const make = Effect.gen(function* () {
449458 for ( const [ key , cache ] of decodeCursorAccountCaches ( yield * readDocument ( cursorCachePath ) ) ) {
450459 cursorCaches . set ( key , cache ) ;
451460 }
461+ const antigravityDocument = yield * readDocument ( antigravityCachePath ) ;
462+ for ( const [ key , entry ] of decodeAntigravityUsageCache ( antigravityDocument ) ) {
463+ antigravityCache . set ( key , entry ) ;
464+ }
452465 let document = yield * readDocument ( scanCachePath ) ;
453466 if ( document === null ) {
454467 document = yield * readDocument ( legacyScanCachePath ) ;
@@ -466,6 +479,7 @@ export const make = Effect.gen(function* () {
466479 ) ;
467480
468481 const writeScanCache = makeScanCacheWriter ( ) ;
482+ const writeAntigravityCache = makeAntigravityUsageCacheWriter ( ) ;
469483 // Scans with different windows can finish together; serializing the writes
470484 // keeps an older snapshot from landing after a newer one.
471485 const persistLock = yield * Semaphore . make ( 1 ) ;
@@ -488,6 +502,17 @@ export const make = Effect.gen(function* () {
488502 ) ,
489503 ) ;
490504 }
505+ if ( antigravityCacheDirty ) {
506+ antigravityCacheDirty = false ;
507+ yield * Effect . sync ( ( ) => writeAntigravityCache ( antigravityCache ) ) . pipe (
508+ Effect . flatMap ( ( contents ) => writeCacheFile ( antigravityCachePath , contents ) ) ,
509+ Effect . catchCause ( ( ) =>
510+ Effect . sync ( ( ) => {
511+ antigravityCacheDirty = true ;
512+ } ) ,
513+ ) ,
514+ ) ;
515+ }
491516 if ( cursorCacheDirty ) {
492517 cursorCacheDirty = false ;
493518 yield * Effect . sync ( ( ) => encodeCursorAccountCaches ( cursorCaches ) ) . pipe (
@@ -909,6 +934,7 @@ export const make = Effect.gen(function* () {
909934 const result = yield * Effect . promise ( ( ) =>
910935 readAntigravityUsage ( [ ...antigravityDirs ] , windowStartMs , antigravityCache ) ,
911936 ) ;
937+ if ( result . cacheChanged ) antigravityCacheDirty = true ;
912938 const scanned : ScannedDir [ ] = [ ] ;
913939 for ( const dir of antigravityDirs ) {
914940 const exists = yield * fileSystem
0 commit comments