Skip to content

Commit 58d1e9c

Browse files
committed
fix(usage): unchanged Antigravity databases are not decoded again after a restart
The Antigravity memo from #17139 lived only in memory, so the first usage scan after every restart decoded the whole history again. It is now saved next to the other usage caches and loaded with them, keyed as before by the size, mtime and ctime of each database and its WAL, and holding candidates as read, before aliases merge and the date window applies. A malformed entry is dropped and its database read again.
1 parent c9fa136 commit 58d1e9c

4 files changed

Lines changed: 358 additions & 8 deletions

File tree

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

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import {
1616
ProviderInstanceId,
1717
UsageDay,
1818
type UsageSource,
19+
type UsageSummary,
1920
type UsageSummaryInput,
2021
} from "@t3tools/contracts";
2122
import * as Duration from "effect/Duration";
@@ -163,6 +164,40 @@ const replaceFile = (path: string, content: string) =>
163164
await NodeFSP.rename(path + ".next", path);
164165
});
165166

167+
function protoNumber(field: number, value: number): number[] {
168+
const varint = (number: number) => {
169+
const bytes: number[] = [];
170+
do {
171+
const byte = number % 128;
172+
number = Math.floor(number / 128);
173+
bytes.push(byte + (number > 0 ? 128 : 0));
174+
} while (number > 0);
175+
return bytes;
176+
};
177+
return [...varint(field * 8), ...varint(value)];
178+
}
179+
180+
function protoBytes(field: number, bytes: readonly number[]): number[] {
181+
const encoded = protoNumber(field, bytes.length);
182+
encoded[0] = encoded[0]! + 2;
183+
return [...encoded, ...bytes];
184+
}
185+
186+
/** One Antigravity generation inside `WINDOW` with `outputTokens` of output. */
187+
function antigravityGeneration(responseId: string, outputTokens: number): Uint8Array {
188+
return new Uint8Array(
189+
protoBytes(1, [
190+
...protoBytes(4, [
191+
...protoNumber(2, 100),
192+
...protoNumber(3, outputTokens),
193+
...protoBytes(11, [...Buffer.from(responseId)]),
194+
]),
195+
...protoBytes(19, [...Buffer.from("Gemini 3 Pro")]),
196+
...protoBytes(9, protoBytes(4, protoNumber(1, Date.parse("2026-08-01T10:00:00Z") / 1000))),
197+
]),
198+
);
199+
}
200+
166201
function totalOutputTokens(summary: { buckets: readonly { totals: { outputTokens: number } }[] }) {
167202
return summary.buckets.reduce((sum, bucket) => sum + bucket.totals.outputTokens, 0);
168203
}
@@ -669,6 +704,48 @@ describe("UsageService", () => {
669704
}).pipe(Effect.scoped),
670705
);
671706

707+
it.live("restores unchanged Antigravity history after a restart without reading it", () =>
708+
Effect.gen(function* () {
709+
const { settings, home } = yield* setup;
710+
const conversations = NodePath.join(home, "antigravity", "conversations");
711+
const db = yield* Effect.acquireRelease(
712+
Effect.promise(async () => {
713+
await NodeFSP.mkdir(conversations, { recursive: true });
714+
const db = new NodeSqlite.DatabaseSync(NodePath.join(conversations, "session-1.db"));
715+
db.exec("CREATE TABLE gen_metadata (idx INTEGER, data BLOB)");
716+
db.prepare("INSERT INTO gen_metadata VALUES (?, ?)").run(
717+
0,
718+
antigravityGeneration("r-1", 40),
719+
);
720+
return db;
721+
}),
722+
(db) => Effect.sync(() => db.close()),
723+
);
724+
const antigravitySource = (summary: UsageSummary) =>
725+
summary.sources.find((source) => source.fingerprint.provider === "antigravity");
726+
yield* Effect.gen(function* () {
727+
const first = yield* UsageService.make;
728+
const original = yield* first.readSummary(WINDOW);
729+
assert.strictEqual(antigravitySource(original)?.status, "ok");
730+
assert.strictEqual(totalOutputTokens(original), 40);
731+
yield* first.awaitPersisted;
732+
733+
// An exclusive lock makes any read of the database fail, so after the
734+
// restart only the saved cache can still report its usage.
735+
db.exec("BEGIN EXCLUSIVE");
736+
const restarted = yield* UsageService.make;
737+
const restored = yield* restarted.readSummary(WINDOW);
738+
db.exec("ROLLBACK");
739+
assert.strictEqual(antigravitySource(restored)?.status, "ok");
740+
assert.deepStrictEqual(restored.buckets, original.buckets);
741+
}).pipe(
742+
Effect.provide(
743+
layerService({ prefix: "usage-service-antigravity-restart", home, settings }),
744+
),
745+
);
746+
}).pipe(Effect.scoped),
747+
);
748+
672749
it.live("counts aliased OpenCode and Antigravity directories once", () =>
673750
Effect.gen(function* () {
674751
const { settings, home } = yield* setup;

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

Lines changed: 29 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,9 @@
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";
6061
import { resolveAntigravityInstanceDirectories } from "../provider/antigravityAuthSupport.ts";
6162
import { mergeProviderInstanceEnvironment } from "@t3tools/provider-core/server/instanceEnvironment";
6263
import { 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";
6471
import {
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

Comments
 (0)