From 32f0eee2a25a0e99f0e0313f728e3ee5ca3aa024 Mon Sep 17 00:00:00 2001 From: Resham Joshi <65915470+iamtoruk@users.noreply.github.com> Date: Fri, 6 Nov 2026 06:26:33 -0800 Subject: [PATCH] fix(antigravity): treat an unreadable output total as absent An RPC usage entry whose outputTokens is present but not a number (an empty string) was reconciled as NaN and produced NaN output. A total that does not parse to a finite number now counts as absent and the split stands. On the SQLite path a varint cannot be non-numeric; a field 3 of the wrong wire type (no varint value) is likewise treated as absent. --- src/providers/antigravity.ts | 7 ++++--- tests/providers/antigravity.test.ts | 5 +++-- 2 files changed, 7 insertions(+), 5 deletions(-) diff --git a/src/providers/antigravity.ts b/src/providers/antigravity.ts index ed424c42..275d87de 100644 --- a/src/providers/antigravity.ts +++ b/src/providers/antigravity.ts @@ -1014,7 +1014,8 @@ function decodePackedVarints(data: Uint8Array): number[] { // 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]. +// absent or unreadable 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) @@ -1041,7 +1042,7 @@ function buildCallFromSqliteGenMetadataRow( const cacheWriteTokens = protoFieldPositiveInteger(firstProtoField(usageFields, 4)) const cacheReadTokens = protoFieldPositiveInteger(firstProtoField(usageFields, 5)) const [responseTokens, thinkingTokens] = reconcileOutputSplit( - firstProtoField(usageFields, 3) ? totalOutputTokens : undefined, + firstProtoField(usageFields, 3)?.value !== undefined ? totalOutputTokens : undefined, protoFieldPositiveInteger(firstProtoField(usageFields, 9)), protoFieldPositiveInteger(firstProtoField(usageFields, 10)), ) @@ -1333,7 +1334,7 @@ export function buildCallsFromGeneratorMetadata( const inputTokens = parseInt(usage.inputTokens ?? '0', 10) const outputTokens = parseInt(usage.outputTokens ?? '0', 10) const [responseTokens, thinkingTokens] = reconcileOutputSplit( - usage.outputTokens === undefined ? undefined : outputTokens, + usage.outputTokens !== undefined && Number.isFinite(outputTokens) ? outputTokens : undefined, 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 bb586065..e8c76d6c 100644 --- a/tests/providers/antigravity.test.ts +++ b/tests/providers/antigravity.test.ts @@ -883,11 +883,12 @@ describe('antigravity provider helpers', () => { { 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' } } }, + { chatModel: { model: 'gemini-3-pro', usage: { model: 'gemini-3-pro', inputTokens: '1000', outputTokens: '', thinkingOutputTokens: '100', responseOutputTokens: '50', responseId: 'r6' } } }, ], {}) // The output total wins over a split that disagrees, even when thinking // 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]]) + // total at all, or one that is not a number, the split stands. + expect(calls.map(c => [c.outputTokens, c.reasoningTokens])).toEqual([[4000, 0], [3000, 1000], [0, 100], [0, 0], [50, 100], [50, 100]]) expect(calls[0]!.costUSD).toBeCloseTo(calls[1]!.costUSD, 12) expect(calls[0]!.costUSD).toBeGreaterThan(0.04) })