Skip to content

Commit 3f0e87a

Browse files
committed
fix(usage): retry a failed Antigravity cache write on the next scan
A failed write restored the dirty flag, but a write was only scheduled when a scan changed the cache, so an unchanged history stayed unsaved. A write is now scheduled whenever the cache is dirty.
1 parent e74c68d commit 3f0e87a

2 files changed

Lines changed: 41 additions & 4 deletions

File tree

‎apps/server/src/provider/Drivers/AntigravityUsage.ts‎

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -175,10 +175,9 @@ const make = Effect.gen(function* () {
175175
Effect.provideService(FileSystem.FileSystem, fileSystem),
176176
Effect.provideService(Path.Path, path),
177177
);
178-
if (result.cacheChanged) {
179-
cacheDirty = true;
180-
yield* schedulePersist;
181-
}
178+
// Also after an unchanged scan, so a failed write is retried.
179+
if (result.cacheChanged) cacheDirty = true;
180+
if (cacheDirty) yield* schedulePersist;
182181
const scanned: ProviderUsageScan[] = [];
183182
for (const dir of conversationDirs) {
184183
const exists = yield* fileSystem

‎apps/server/src/usage/UsageService.test.ts‎

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -935,6 +935,44 @@ describe("UsageService", () => {
935935
}).pipe(Effect.scoped),
936936
);
937937

938+
it.live("retries a failed Antigravity cache write on the next scan", () =>
939+
Effect.gen(function* () {
940+
const { settings, home } = yield* setup;
941+
const conversations = NodePath.join(home, "antigravity", "conversations");
942+
yield* Effect.promise(async () => {
943+
await NodeFSP.mkdir(conversations, { recursive: true });
944+
const db = new NodeSqlite.DatabaseSync(NodePath.join(conversations, "session-1.db"));
945+
db.exec("CREATE TABLE gen_metadata (idx INTEGER, data BLOB)");
946+
db.prepare("INSERT INTO gen_metadata VALUES (?, ?)").run(
947+
0,
948+
antigravityGeneration("r-1", 40),
949+
);
950+
db.close();
951+
});
952+
yield* Effect.gen(function* () {
953+
const { stateDir } = yield* ServerConfig.ServerConfig;
954+
const cachePath = NodePath.join(stateDir, "usage-antigravity-cache-v1.json");
955+
// A directory where the cache file goes makes the first write fail.
956+
yield* Effect.promise(() => NodeFSP.mkdir(cachePath, { recursive: true }));
957+
const service = yield* makeWithAntigravity;
958+
yield* service.readSummary(WINDOW);
959+
yield* service.awaitPersisted;
960+
assert.isTrue((yield* Effect.promise(() => NodeFSP.stat(cachePath))).isDirectory());
961+
962+
// The history is unchanged, but the failed write is still owed.
963+
yield* Effect.promise(() => NodeFSP.rm(cachePath, { recursive: true }));
964+
yield* service.readSummary(WINDOW);
965+
yield* service.awaitPersisted;
966+
const saved = JSON.parse(
967+
yield* Effect.promise(() => NodeFSP.readFile(cachePath, "utf8")),
968+
) as { files: Record<string, unknown> };
969+
assert.strictEqual(Object.keys(saved.files).length, 1);
970+
}).pipe(
971+
Effect.provide(layerService({ prefix: "usage-service-antigravity-retry", home, settings })),
972+
);
973+
}).pipe(Effect.scoped),
974+
);
975+
938976
it.live("restores the Cursor account cache after a restart", () =>
939977
Effect.gen(function* () {
940978
const { settings, home } = yield* setup;

0 commit comments

Comments
 (0)