// @effect-diagnostics-next-line nodeBuiltinImport:off - Effect's File.Info has no ctime, which the cache fingerprint needs. import * as NodeFSP from "node:fs/promises"; import { pipe } from "effect/Function"; import type { UsageRecord } from "@t3tools/provider-core/server/usage"; import * as NodeSqliteClient from "@t3tools/shared/nodeSqliteClient"; import * as Effect from "effect/Effect"; import * as Exit from "effect/Exit"; import * as FileSystem from "effect/FileSystem"; import * as Path from "effect/Path"; import * as Schema from "effect/Schema"; import * as SqlClient from "effect/sql/SqlClient"; type FieldValue = number | bigint | Uint8Array; type Fields = Map; function fields(bytes: Uint8Array): Fields { let offset = 0; const result: Fields = new Map(); const varint = () => { let value = 0n; for (let shift = 0n; shift < 70n; shift += 7n) { const byte = bytes[offset++]; if (byte === undefined || (shift === 63n && byte > 1)) { throw new Error("Invalid Antigravity protobuf varint"); } value |= BigInt(byte & 127) << shift; if (byte < 128) { return value > BigInt(Number.MAX_SAFE_INTEGER) ? value : Number(value); } } throw new Error("Invalid Antigravity protobuf varint"); }; while (offset < bytes.length) { const tag = varint(); if (typeof tag !== "number") throw new Error("Invalid protobuf field"); const number = Math.floor(tag / 8); const wire = tag % 8; if (number === 0) throw new Error("Invalid protobuf field"); let value: FieldValue; if (wire === 0) { value = varint(); } else if (wire === 1 || wire === 5 || wire === 2) { const length = wire === 2 ? varint() : wire === 1 ? 8 : 4; if (typeof length !== "number") throw new Error("Invalid protobuf field length"); if (length > bytes.length - offset) throw new Error("Truncated protobuf field"); value = bytes.subarray(offset, offset + length); offset += length; if (wire !== 2) continue; } else { throw new Error("Unsupported protobuf wire type"); } const entries = result.get(number) ?? []; entries.push(value); result.set(number, entries); } return result; } const numberAt = (value: Fields, key: number) => { const entry = value.get(key)?.[0]; return typeof entry === "number" ? entry : 0; }; const bytesAt = (value: Fields, key: number) => { const entry = value.get(key)?.[0]; return entry instanceof Uint8Array ? entry : undefined; }; const nested = (value: Fields, key: number) => { const bytes = bytesAt(value, key); return bytes === undefined ? new Map() : fields(bytes); }; const textAt = (value: Fields, key: number) => { const bytes = bytesAt(value, key); return bytes === undefined ? "" : new TextDecoder("utf-8", { fatal: true }).decode(bytes).trim(); }; const timestamp = (value: Fields) => { const seconds = numberAt(value, 1); return seconds > 0 ? seconds * 1000 + Math.floor(numberAt(value, 2) / 1_000_000) : null; }; // Offline fallback for records without names. New names are learned from the scanned metadata. // Cross-checked against ccusage's Antigravity parser and mrexodia/agent-cost-dashboard. const MODEL_IDS: Record = { 246: "gemini-2.5-pro", 312: "gemini-2.5-flash", 313: "gemini-2.5-flash-thinking", 329: "gemini-2.5-flash-thinking", 330: "gemini-2.5-flash-lite", 281: "claude-sonnet-4", 282: "claude-sonnet-4", 290: "claude-opus-4", 291: "claude-opus-4", 333: "claude-sonnet-4-5", 334: "claude-sonnet-4-5", 340: "claude-haiku-4-5", 341: "claude-haiku-4-5", 342: "gpt-oss-120b-medium", 1026: "claude-opus-4-6", 1035: "claude-sonnet-4-6", 1016: "gemini-3.1-pro", 1036: "gemini-3.1-pro", 1037: "gemini-3.1-pro", 1018: "gemini-3-flash-preview", 1084: "gemini-3-flash-preview", 1047: "gemini-3-flash-preview", 1020: "gemini-3.5-flash-medium", 1021: "gemini-3.1-flash-image", 1050: "gemini-3.1-flash-lite", 1071: "gemini-3.6-flash-high", 1072: "gemini-3.6-flash-medium", 1073: "gemini-3.6-flash-low", 1132: "gemini-3.5-flash-high", 1133: "gemini-3.5-flash-high", 1187: "gemini-3.5-flash-extra-low", 1196: "gemini-3.6-flash", 1266: "gemini-3.6-flash", 1298: "gemini-3.7-flash-high", 1299: "gemini-3.7-flash-medium", 1300: "gemini-3.7-flash-low", 1301: "gemini-3.7-flash-tiered", 1318: "gemini-3.8-flash-high", 1319: "gemini-3.8-flash-medium", 1320: "gemini-3.8-flash-low", 1322: "gemini-3.8-flash-tiered", 1402: "claude-opus-5-5", 1417: "gemini-4-argon", 1418: "gemini-4-argon", 1419: "gemini-4-argon", }; // Effort and routing variants share the base model's price. function withRateModel(record: UsageRecord): UsageRecord { const base = record.model.replace( /-(?:extra-low|low|medium|high|tiered|control|thinking|agent)$/, "", ); const rateModel = /^gemini-3-flash-(?:agent(?:-[ab])?|[ab])$/.test(record.model) ? "gemini-3.5-flash" : record.model === "gemini-3.7-flash-safety-le" ? "gemini-3.7-flash" : base === "gemini-3-flash" ? "gemini-3-flash-preview" : base; return rateModel === record.model ? record : { ...record, rateModel }; } function modelName(name: string): string { if (name) { const normalized = name .toLowerCase() .replace(/\s*\(([^)]*)\)\s*$/, "-$1") .replace(/\s+/g, "-"); if (normalized.startsWith("claude-")) { return normalized .replace(/^claude-(\d+(?:\.\d+)?)-(sonnet|opus|haiku)/, "claude-$2-$1") .replaceAll(".", "-"); } return normalized; } return ""; } function isConcreteModel(model: string): boolean { return ( /^(?:gemini-\d|claude-(?:sonnet|opus|haiku)-\d|gpt-)/.test(model) && !/^gemini-3-flash-(?:agent(?:-[ab])?|[ab])$/.test(model) ); } function rememberModel(models: Map, id: number, model: string | null) { const previous = models.get(id); models.set(id, previous === undefined || previous === model ? model : null); } interface Metadata { model: string; modelId: number; timestampMs: number | null; usage: Fields | undefined; stepIndex: number | undefined; } // Usage in fields 4 and 9 sums every attempt; the per-attempt lists in fields 17 and 28 repeat it. function metadata(bytes: Uint8Array, step: boolean): Metadata { const root = fields(bytes); if (!step && bytesAt(root, 1) === undefined) { throw new Error("Missing Antigravity generation metadata"); } const data = step ? root : nested(root, 1); const model = step ? nested(data, 24) : data; const usageBytes = bytesAt(data, step ? 9 : 4); // Copy the usage bytes: a subarray would hold the row's whole blob alive. const usage = usageBytes === undefined ? undefined : fields(usageBytes.slice()); const names = [textAt(model, step ? 12 : 19), textAt(model, step ? 8 : 21)].map(modelName); return { model: names.find(isConcreteModel) ?? names.find((name) => name && !/^model[_-]/.test(name)) ?? "", modelId: numberAt(model, step ? 1 : 3) || (usage === undefined ? 0 : numberAt(usage, 1)), timestampMs: step ? (timestamp(nested(data, 8)) ?? timestamp(nested(data, 1))) : timestamp(nested(nested(data, 9), 4)), usage, stepIndex: step ? undefined : linkedStepIndex(data), }; } // A generation's usage is repeated on the step after its last_step_index. The link only merges that // copy, so a malformed entry leaves the generation unlinked instead of failing the whole database. function linkedStepIndex(data: Fields): number | undefined { for (const entry of data.get(20) ?? []) { try { const pair = fields(blob(entry)); if (textAt(pair, 1) !== "last_step_index") continue; const index = textAt(pair, 2); return /^\d+$/.test(index) ? Number(index) + 1 : undefined; } catch { continue; } } return undefined; } function blob(value: unknown): Uint8Array { if (!(value instanceof Uint8Array)) throw new Error("Invalid Antigravity metadata blob"); return value; } interface UsageCandidate { record: UsageRecord; modelId: number; modelQuality: number; keys: readonly string[]; timestampQuality: number; } class AntigravityDatabaseDecodeError extends Schema.TaggedError()( "AntigravityDatabaseDecodeError", { path: Schema.String, cause: Schema.optional(Schema.Defect()) }, ) { override get message(): string { return `Antigravity usage could not be decoded from ${this.path}.`; } } /** Generation rows can hold megabytes of context; read one at a time to bound blocking statement.all(). */ const GENERATION_PAGE_ROWS = 1; const STEP_PAGE_ROWS = 64; const readDatabase = Effect.fn("readDatabase")(function* ( path: string, sessionId: string, fallbackTimestamp: number, ) { const sql = yield* SqlClient.SqlClient; const decode = (evaluate: () => A) => Effect.try({ try: evaluate, catch: (cause) => new AntigravityDatabaseDecodeError({ path, cause }), }); yield* sql.unsafe("PRAGMA busy_timeout = 100"); return yield* Effect.gen(function* () { const tables = new Set( (yield* sql.unsafe<{ readonly name: unknown }>( "SELECT name FROM sqlite_master WHERE type = 'table'", )).map((row) => row.name), ); if (!tables.has("gen_metadata") && !tables.has("steps")) { return yield* new AntigravityDatabaseDecodeError({ path }); } // Seek by rowid regardless of indexes; idx normally aliases rowid, but sort for layouts where it does not. const readMetadata = Effect.fnUntraced(function* ( query: string, pageRows: number, step: boolean, ) { const entries: Array<{ idx: number; entry: Metadata }> = []; let after = -Infinity; while (true) { const rows = yield* sql .unsafe<{ readonly row_id: number; readonly idx: unknown; readonly data: unknown; }>(query, [after, pageRows]) .pipe(Effect.withTracerEnabled(false)); for (const row of rows) { // Filter outside SQL to bound scans to one page, including rows without metadata. if (step && row.data === null) continue; const idx = row.idx; if (typeof idx !== "number") { return yield* new AntigravityDatabaseDecodeError({ path }); } entries.push({ idx, entry: yield* decode(() => metadata(blob(row.data), step)) }); } if (rows.length < pageRows) break; after = rows[rows.length - 1]!.row_id; yield* Effect.yieldNow; } return entries.sort((a, b) => a.idx - b.idx); }); const generations = tables.has("gen_metadata") ? yield* readMetadata( "SELECT rowid AS row_id, idx, data FROM gen_metadata WHERE rowid > ? ORDER BY rowid LIMIT ?", GENERATION_PAGE_ROWS, false, ) : []; let trajectoryTimestamp: number | null = null; if (tables.has("trajectory_metadata_blob")) { for (const row of yield* sql.unsafe<{ readonly data: unknown }>( "SELECT data FROM trajectory_metadata_blob", )) { trajectoryTimestamp ??= yield* decode(() => pipe(blob(row.data), fields, (message) => nested(message, 2), timestamp), ); } } const steps = tables.has("steps") ? yield* readMetadata( "SELECT rowid AS row_id, idx, metadata AS data FROM steps WHERE rowid > ? ORDER BY rowid LIMIT ?", STEP_PAGE_ROWS, true, ) : []; return yield* decode(() => { const models = new Map(); for (const { entry } of [...generations, ...steps]) { if (entry.modelId > 0 && isConcreteModel(entry.model)) rememberModel(models, entry.modelId, entry.model); } return { candidates: usageCandidates( sessionId, generations, steps, trajectoryTimestamp, fallbackTimestamp, ), models, }; }); }).pipe(sql.withTransaction); }); function usageCandidates( sessionId: string, generations: ReadonlyArray<{ idx: number; entry: Metadata }>, steps: ReadonlyArray<{ idx: number; entry: Metadata }>, trajectoryTimestamp: number | null, fallbackTimestamp: number, ): UsageCandidate[] { const records: UsageCandidate[] = []; const generationsByStep = new Map( generations.flatMap(({ entry }) => entry.stepIndex === undefined ? [] : [[entry.stepIndex, entry] as const], ), ); for (const [source, entries] of [ ["step", steps], ["generation", generations], ] as const) { for (const [index, { idx, entry }] of entries.entries()) { const usage = entry.usage; if (usage === undefined) continue; const modelId = numberAt(usage, 1) || entry.modelId; const generation = source === "step" ? generationsByStep.get(idx) : undefined; const matches = (metadata: Metadata) => modelId === 0 || metadata.modelId === modelId; const model = matches(entry) && entry.model ? entry.model : generation && matches(generation) ? generation.model : ""; const outputTokens = Math.max(numberAt(usage, 3), numberAt(usage, 9) + numberAt(usage, 10)); const totals = { uncachedInputTokens: numberAt(usage, 2), cachedInputTokens: numberAt(usage, 5), cacheCreationTokens: numberAt(usage, 4), outputTokens, reasoningTokens: Math.min(outputTokens, numberAt(usage, 9)), }; if ( totals.uncachedInputTokens + totals.cachedInputTokens + totals.cacheCreationTokens + outputTokens === 0 ) continue; const stepIndex = source === "step" ? idx : entry.stepIndex; const responseKeys = ([11, 12, 7] as const).flatMap((key) => { const id = textAt(usage, key); return id ? [`antigravity:${key}:${id}`] : []; }); const identity = stepIndex === undefined ? `antigravity:${sessionId}:generation:${index}` : `antigravity:${sessionId}:step:${stepIndex}`; const record: UsageRecord = { provider: "antigravity", sessionId, timestampMs: entry.timestampMs ?? trajectoryTimestamp ?? fallbackTimestamp, model, totals, reportedCostUsd: null, speed: "standard", dedupeKey: responseKeys[0] ?? identity, }; records.push({ record, modelId, modelQuality: isConcreteModel(model) ? 4 : model ? 1 : 0, keys: [...responseKeys, identity], timestampQuality: entry.timestampMs !== null ? 2 : trajectoryTimestamp !== null ? 1 : 0, }); } } return records; } interface CachedDatabase { readonly fingerprint: string; readonly data: Effect.Success>; } /** Fingerprint canonical DB/WAL paths by size, mtime and ctime; WAL holds new rows, and ctime catches restored mtimes. */ export const makeAntigravityUsageCache = () => new Map(); export const readAntigravityUsage = Effect.fn("readAntigravityUsage")(function* ( conversationsDirectories: string | readonly string[], sinceMs: number, cache?: Map, ) { const fileSystem = yield* FileSystem.FileSystem; const pathService = yield* Path.Path; const roots = typeof conversationsDirectories === "string" ? [conversationsDirectories] : conversationsDirectories; const files: Array<{ root: string; path: string; records: UsageRecord[] }> = []; const errors: string[] = []; const models = new Map(); const pending: Array<{ data: CachedDatabase["data"]; fileIndex: number }> = []; const identities = new Map(); const groups: Array< UsageCandidate & { parent: number; size: number; owner: number; fileIndex: number } > = []; const find = (index: number): number => { let root = index; while (groups[root]!.parent !== root) root = groups[root]!.parent; while (index !== root) { const parent = groups[index]!.parent; groups[index]!.parent = root; index = parent; } return root; }; const merge = (left: number, right: number): number => { let a = find(left); let b = find(right); if (a === b) return a; if (groups[a]!.size < groups[b]!.size) [a, b] = [b, a]; const target = groups[a]!; const source = groups[b]!; const first = target.owner < source.owner ? target : source; const bestModel = source.modelQuality === target.modelQuality ? first : source.modelQuality > target.modelQuality ? source : target; const bestTime = source.timestampQuality > target.timestampQuality || (source.timestampQuality === target.timestampQuality && source.record.timestampMs < target.record.timestampMs) ? source : target; const x = target.record.totals; const y = source.record.totals; target.record = { ...first.record, model: bestModel.record.model, timestampMs: bestTime.record.timestampMs, totals: { uncachedInputTokens: Math.max(x.uncachedInputTokens, y.uncachedInputTokens), cachedInputTokens: Math.max(x.cachedInputTokens, y.cachedInputTokens), cacheCreationTokens: Math.max(x.cacheCreationTokens, y.cacheCreationTokens), outputTokens: Math.max(x.outputTokens, y.outputTokens), reasoningTokens: Math.max(x.reasoningTokens, y.reasoningTokens), }, }; target.timestampQuality = bestTime.timestampQuality; target.modelQuality = bestModel.modelQuality; target.owner = first.owner; target.fileIndex = first.fileIndex; target.size += source.size; source.parent = a; return a; }; const append = (candidate: UsageCandidate, fileIndex: number) => { const index = groups.length; groups.push({ ...candidate, parent: index, size: 1, owner: index, fileIndex }); for (const key of candidate.keys) { const existing = identities.get(key); if (existing !== undefined) merge(index, existing); identities.set(key, index); } }; const visited = new Set(); const readFile = Effect.fn("readAntigravityUsage.readFile")(function* (path: string) { const canonical = yield* fileSystem.realPath(path); if (visited.has(canonical)) return null; visited.add(canonical); const fingerprintOf = (file: string) => Effect.tryPromise({ try: () => NodeFSP.stat(file), catch: (cause) => new AntigravityDatabaseDecodeError({ path: file, cause }), }).pipe( Effect.map((stat) => ({ stat, part: `${stat.size}:${stat.mtimeMs}:${stat.ctimeMs}` })), ); const { stat, part } = yield* fingerprintOf(path); const wal = yield* fingerprintOf(`${path}-wal`).pipe(Effect.option); const fingerprint = `${part}/${wal._tag === "Some" ? wal.value.part : "-"}`; const cached = cache?.get(canonical); if (cached?.fingerprint === fingerprint) return cached.data; const data = yield* readDatabase(path, pathService.basename(path, ".db"), stat.mtimeMs).pipe( Effect.provide(NodeSqliteClient.layer({ filename: path, readonly: true })), ); cache?.set(canonical, { fingerprint, data }); return data; }); const walk = Effect.fn("antigravityUsageReader.walk")(function* ( directory: string, root: string, ): Effect.fn.Return { const names = yield* fileSystem.readDirectory(directory).pipe( Effect.catchTags({ PlatformError: (cause) => { if (cause.reason._tag !== "NotFound") errors.push(directory); return Effect.succeed(null); }, }), ); if (names === null) return; names.sort((a, b) => a.localeCompare(b)); for (const name of names) { const path = pathService.join(directory, name); // Like a directory listing's entry types: symlinks are neither files nor directories. const isLink = Exit.isSuccess(yield* Effect.exit(fileSystem.readLink(path))); const type = isLink ? "SymbolicLink" : yield* fileSystem.stat(path).pipe( Effect.map((info) => info.type), Effect.orElseSucceed(() => null), ); if (type === "Directory") { yield* walk(path, root); } else if ((type === "File" || type === null) && name.endsWith(".db")) { const data = yield* readFile(path).pipe( Effect.catch(() => { errors.push(path); return Effect.succeed(null); }), ); if (data === null) continue; for (const [id, model] of data.models) { rememberModel(models, id, model); } const fileIndex = files.length; files.push({ root, path, records: [] }); pending.push({ data, fileIndex }); } } }); for (const root of roots) yield* walk(root, root); if (cache !== undefined) { for (const key of cache.keys()) if (!visited.has(key)) cache.delete(key); } for (const { data, fileIndex } of pending) { for (const [index, candidate] of data.candidates.entries()) { const learned = models.get(candidate.modelId); const fallback = learned === null ? undefined : MODEL_IDS[candidate.modelId]; const model = (candidate.modelQuality === 4 ? candidate.record.model : "") || learned || fallback || candidate.record.model || (candidate.modelId > 0 ? `antigravity-model-${candidate.modelId}` : "antigravity-unknown"); append( { ...candidate, record: { ...candidate.record, model }, modelQuality: candidate.modelQuality === 4 ? 4 : learned ? 3 : fallback ? 2 : candidate.modelQuality, }, fileIndex, ); if (index % 256 === 255) yield* Effect.yieldNow; } } for (const [index, group] of groups.entries()) { if (group.parent === index && group.record.timestampMs >= sinceMs) { files[group.fileIndex]!.records.push(withRateModel(group.record)); } } return { files, errors }; });