mirror of
https://github.com/AgentSeal/codeburn.git
synced 2026-11-29 10:43:47 +00:00
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.
This commit is contained in:
parent
244070105b
commit
8ea9ba8966
2 changed files with 24 additions and 20 deletions
|
|
@ -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}`
|
||||
|
||||
|
|
|
|||
|
|
@ -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)
|
||||
})
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue