diff --git a/src/providers/antigravity.ts b/src/providers/antigravity.ts index 4bf9fbda..ed424c42 100644 --- a/src/providers/antigravity.ts +++ b/src/providers/antigravity.ts @@ -1011,12 +1011,12 @@ function decodePackedVarints(data: Uint8Array): number[] { } // ModelUsageStats' output total (field 3, thinking + response) wins over its -// split: an absent split (proto3 omits unset fields) takes the total as -// response, and a split that disagrees, even one whose thinking alone exceeds -// the total or one beside a total of 0, is cut back to it. Returns -// [response, thinking]. -function reconcileOutputSplit(total: number, thinking: number, response: number): [number, number] { - if (response + thinking === total) return [response, thinking] +// split when it is present: an absent split (proto3 omits unset fields) takes +// the total as response, and a split that disagrees, even one whose thinking +// alone exceeds the total or one beside an explicit 0, is cut back to it. An +// absent total (undefined) keeps the split. Returns [response, thinking]. +function reconcileOutputSplit(total: number | undefined, thinking: number, response: number): [number, number] { + if (total === undefined || response + thinking === total) return [response, thinking] const kept = Math.min(thinking, total) return [total - kept, kept] } @@ -1041,7 +1041,7 @@ function buildCallFromSqliteGenMetadataRow( const cacheWriteTokens = protoFieldPositiveInteger(firstProtoField(usageFields, 4)) const cacheReadTokens = protoFieldPositiveInteger(firstProtoField(usageFields, 5)) const [responseTokens, thinkingTokens] = reconcileOutputSplit( - totalOutputTokens, + firstProtoField(usageFields, 3) ? totalOutputTokens : undefined, protoFieldPositiveInteger(firstProtoField(usageFields, 9)), protoFieldPositiveInteger(firstProtoField(usageFields, 10)), ) @@ -1333,7 +1333,7 @@ export function buildCallsFromGeneratorMetadata( const inputTokens = parseInt(usage.inputTokens ?? '0', 10) const outputTokens = parseInt(usage.outputTokens ?? '0', 10) const [responseTokens, thinkingTokens] = reconcileOutputSplit( - outputTokens, + usage.outputTokens === undefined ? undefined : outputTokens, parseInt(usage.thinkingOutputTokens ?? '0', 10), parseInt(usage.responseOutputTokens ?? '0', 10), ) diff --git a/tests/providers/antigravity.test.ts b/tests/providers/antigravity.test.ts index 0f514c76..bb586065 100644 --- a/tests/providers/antigravity.test.ts +++ b/tests/providers/antigravity.test.ts @@ -882,14 +882,38 @@ describe('antigravity provider helpers', () => { { chatModel: { model: 'gemini-3-pro', usage: { model: 'gemini-3-pro', inputTokens: '1000', outputTokens: '4000', thinkingOutputTokens: '1000', responseId: 'r2' } } }, { chatModel: { model: 'gemini-3-pro', usage: { model: 'gemini-3-pro', inputTokens: '1000', outputTokens: '100', thinkingOutputTokens: '200', responseOutputTokens: '50', responseId: 'r3' } } }, { chatModel: { model: 'gemini-3-pro', usage: { model: 'gemini-3-pro', inputTokens: '1000', outputTokens: '0', thinkingOutputTokens: '100', responseOutputTokens: '50', responseId: 'r4' } } }, + { chatModel: { model: 'gemini-3-pro', usage: { model: 'gemini-3-pro', inputTokens: '1000', thinkingOutputTokens: '100', responseOutputTokens: '50', responseId: 'r5' } } }, ], {}) // The output total wins over a split that disagrees, even when thinking - // alone is larger than the total, or the total is 0. - expect(calls.map(c => [c.outputTokens, c.reasoningTokens])).toEqual([[4000, 0], [3000, 1000], [0, 100], [0, 0]]) + // alone is larger than the total, or the total is an explicit 0. With no + // total at all the split stands. + expect(calls.map(c => [c.outputTokens, c.reasoningTokens])).toEqual([[4000, 0], [3000, 1000], [0, 100], [0, 0], [50, 100]]) expect(calls[0]!.costUSD).toBeCloseTo(calls[1]!.costUSD, 12) expect(calls[0]!.costUSD).toBeGreaterThan(0.04) }) + it('keeps a SQLite split when the output total is absent, and caps it at an explicit 0', async () => { + if (!isSqliteAvailable()) return + + await withTempAntigravityHome('codeburn-antigravity-split-', async (tempHome) => { + const varint = (n: number): number[] => { const out: number[] = []; while (n > 127) { out.push((n & 127) | 128); n >>>= 7 } out.push(n); return out } + const num = (field: number, value: number) => [...varint(field << 3), ...varint(value)] + const msg = (field: number, data: number[]) => [...varint((field << 3) | 2), ...varint(data.length), ...data] + const row = (idx: number, usage: number[]) => ({ idx, hex: Buffer.from(msg(1, msg(4, usage))).toString('hex') }) + const split = [...num(2, 1000), ...num(9, 100), ...num(10, 50)] + const conversationsDir = join(tempHome, '.gemini', 'antigravity-cli', 'conversations') + await mkdir(conversationsDir, { recursive: true }) + const dbPath = join(conversationsDir, 'split-shapes.db') + createCurrentAntigravityCliDb(dbPath, { + conversationId: 'split-shapes', + rows: [row(0, split), row(1, [...num(3, 0), ...split])], + }) + + const calls = await collectAntigravityCalls({ path: dbPath, project: 'antigravity-cli', provider: 'antigravity' }) + expect(calls.map(c => [c.outputTokens, c.reasoningTokens])).toEqual([[50, 100], [0, 0]]) + }) + }) + it('stamps file mtime as fallback timestamp for SQLite-parsed calls', async () => { if (!isSqliteAvailable()) return