From 8ea9ba8966a4c613e4dbe7a5c3083f959d337773 Mon Sep 17 00:00:00 2001 From: Resham Joshi <65915470+iamtoruk@users.noreply.github.com> Date: Fri, 6 Nov 2026 06:20:07 -0800 Subject: [PATCH] fix(antigravity): let the output total win whenever the split disagrees Rule: ModelUsageStats' output total (field 3 / outputTokens, documented as thinking + response) is authoritative whenever it is set. The existing reconciliation already trusted it over a disagreeing split, but skipped the case where thinking alone exceeded the total, so outputTokens 100 with thinking 200 and response 50 kept 250. That case now caps thinking at the total and leaves response at 0. One shared helper applies the rule on the RPC and SQLite paths; the unreleased rpc-output-total-v1 bump already re-parses cached cascades. --- src/providers/antigravity.ts | 39 +++++++++++++++-------------- tests/providers/antigravity.test.ts | 5 +++- 2 files changed, 24 insertions(+), 20 deletions(-) diff --git a/src/providers/antigravity.ts b/src/providers/antigravity.ts index 5cc53751..76687ee7 100644 --- a/src/providers/antigravity.ts +++ b/src/providers/antigravity.ts @@ -1010,6 +1010,16 @@ function decodePackedVarints(data: Uint8Array): number[] { return values } +// ModelUsageStats' output total (field 3, thinking + response) wins over its +// split whenever it is set: 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, is cut back to it. Returns [response, thinking]. +function reconcileOutputSplit(total: number, thinking: number, response: number): [number, number] { + if (total <= 0 || response + thinking === total) return [response, thinking] + const kept = Math.min(thinking, total) + return [total - kept, kept] +} + function buildCallFromSqliteGenMetadataRow( cascadeId: string, row: AntigravityGenMetadataRow, @@ -1029,15 +1039,11 @@ function buildCallFromSqliteGenMetadataRow( const totalOutputTokens = protoFieldPositiveInteger(firstProtoField(usageFields, 3)) const cacheWriteTokens = protoFieldPositiveInteger(firstProtoField(usageFields, 4)) const cacheReadTokens = protoFieldPositiveInteger(firstProtoField(usageFields, 5)) - let thinkingTokens = protoFieldPositiveInteger(firstProtoField(usageFields, 9)) - let responseTokens = protoFieldPositiveInteger(firstProtoField(usageFields, 10)) - - if (responseTokens === 0 && thinkingTokens === 0) { - responseTokens = totalOutputTokens - } else if (totalOutputTokens > 0 && responseTokens + thinkingTokens !== totalOutputTokens) { - const adjustedResponseTokens = totalOutputTokens - thinkingTokens - if (adjustedResponseTokens >= 0) responseTokens = adjustedResponseTokens - } + const [responseTokens, thinkingTokens] = reconcileOutputSplit( + totalOutputTokens, + protoFieldPositiveInteger(firstProtoField(usageFields, 9)), + protoFieldPositiveInteger(firstProtoField(usageFields, 10)), + ) if (inputTokens === 0 && totalOutputTokens === 0) return null @@ -1325,21 +1331,16 @@ export function buildCallsFromGeneratorMetadata( const inputTokens = parseInt(usage.inputTokens ?? '0', 10) const outputTokens = parseInt(usage.outputTokens ?? '0', 10) - const thinkingTokens = parseInt(usage.thinkingOutputTokens ?? '0', 10) - let responseTokens = parseInt(usage.responseOutputTokens ?? '0', 10) + const [responseTokens, thinkingTokens] = reconcileOutputSplit( + outputTokens, + parseInt(usage.thinkingOutputTokens ?? '0', 10), + parseInt(usage.responseOutputTokens ?? '0', 10), + ) const cacheReadTokens = parseInt(usage.cacheReadTokens ?? '0', 10) const cacheWriteTokens = parseInt(usage.cacheWriteTokens ?? '0', 10) if (inputTokens === 0 && outputTokens === 0) continue - // Proto3 JSON omits unset fields, so the response/thinking split can be - // missing while outputTokens holds the total. Same rule as the SQLite path. - if (responseTokens === 0 && thinkingTokens === 0) { - responseTokens = outputTokens - } else if (outputTokens > 0 && responseTokens + thinkingTokens !== outputTokens && outputTokens - thinkingTokens >= 0) { - responseTokens = outputTokens - thinkingTokens - } - const responseId = usage.responseId || String(i) const dedupKey = `antigravity:${cascadeId}:${responseId}` diff --git a/tests/providers/antigravity.test.ts b/tests/providers/antigravity.test.ts index 37c319f4..3ad33df4 100644 --- a/tests/providers/antigravity.test.ts +++ b/tests/providers/antigravity.test.ts @@ -880,8 +880,11 @@ describe('antigravity provider helpers', () => { const calls = buildCallsFromGeneratorMetadata('split-less', [ { chatModel: { model: 'gemini-3-pro', usage: { model: 'gemini-3-pro', inputTokens: '1000', outputTokens: '4000', responseId: 'r1' } } }, { 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' } } }, ], {}) - expect(calls.map(c => [c.outputTokens, c.reasoningTokens])).toEqual([[4000, 0], [3000, 1000]]) + // The output total wins over a split that disagrees, even when thinking + // alone is larger than the total. + expect(calls.map(c => [c.outputTokens, c.reasoningTokens])).toEqual([[4000, 0], [3000, 1000], [0, 100]]) expect(calls[0]!.costUSD).toBeCloseTo(calls[1]!.costUSD, 12) expect(calls[0]!.costUSD).toBeGreaterThan(0.04) })