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.
This commit is contained in:
Resham Joshi 2026-11-06 06:26:33 -08:00
parent f89fe26e02
commit 32f0eee2a2
2 changed files with 7 additions and 5 deletions

View file

@ -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),
)

View file

@ -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)
})