Skip to content

Commit ba4be79

Browse files
committed
fix(antigravity): keep distinct retry attempts out of the turn fallback identity
The step/generator positional fallback identity identifies the primary turn, but it was also attached to every unkeyed retry_infos entry. Because deduplicate_events merges by taking the max of each token field, a real failed attempt (e.g. same input, fewer output tokens) was folded into the successful attempt and its usage silently disappeared. retry_infos lists every attempt, including the one already reported as the primary usage. So the only retry that duplicates the primary is that one mirror entry, whatever the attempt count. Drop exactly that entry (not only when there is a single retry, which still left 2x for multi-attempt turns), and leave the remaining retries without the turn's positional identity so they are counted as their own usage.
1 parent 3f1e52d commit ba4be79

2 files changed

Lines changed: 69 additions & 15 deletions

File tree

‎rust/adapters/antigravity/src/loader.rs‎

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -610,6 +610,61 @@ mod tests {
610610
assert_eq!(entries[0].data.message.usage.output_tokens, 50);
611611
}
612612

613+
#[test]
614+
fn keeps_unkeyed_failed_attempt_when_retries_mirror_primary() {
615+
let fixture = Fixture::new();
616+
let success = UsageFixture {
617+
input_tokens: 100,
618+
total_output_tokens: 50,
619+
visible_output_tokens: 50,
620+
..UsageFixture::default()
621+
};
622+
let failed = UsageFixture {
623+
input_tokens: 100,
624+
total_output_tokens: 10,
625+
visible_output_tokens: 10,
626+
..UsageFixture::default()
627+
};
628+
create_database(
629+
&fixture.path("conversations/session.db"),
630+
&[(
631+
0,
632+
metadata_blob_with_steps(
633+
Some("gemini-3-pro"),
634+
Some(success),
635+
Some((1_778_100_001, 0)),
636+
&[failed, success],
637+
&[1],
638+
),
639+
)],
640+
&[(
641+
1,
642+
step_metadata_blob(
643+
Some("gemini-3-pro"),
644+
Some(success),
645+
Some((1_778_100_000, 0)),
646+
&[],
647+
None,
648+
),
649+
)],
650+
&[],
651+
);
652+
653+
let entries = load_from_fixture(&fixture, true);
654+
655+
let mut usages = entries
656+
.iter()
657+
.map(|entry| {
658+
(
659+
entry.data.message.usage.input_tokens,
660+
entry.data.message.usage.output_tokens,
661+
)
662+
})
663+
.collect::<Vec<_>>();
664+
usages.sort_unstable();
665+
assert_eq!(usages, vec![(100, 10), (100, 50)]);
666+
}
667+
613668
#[test]
614669
fn attributes_step_and_retry_usage_to_their_token_models() {
615670
let fixture = Fixture::new();

‎rust/adapters/antigravity/src/parser.rs‎

Lines changed: 14 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -239,7 +239,7 @@ pub(super) fn parse_sqlite_file(path: &Path) -> Result<Option<Vec<AntigravityUsa
239239
trajectory_timestamp,
240240
fallback_timestamp,
241241
session_id: &session_id,
242-
fallback_identities: &fallback_identities,
242+
fallback_identities: &[],
243243
},
244244
);
245245
}
@@ -306,7 +306,7 @@ pub(super) fn parse_sqlite_file(path: &Path) -> Result<Option<Vec<AntigravityUsa
306306
trajectory_timestamp,
307307
fallback_timestamp,
308308
session_id: &session_id,
309-
fallback_identities: &fallback_identities,
309+
fallback_identities: &[],
310310
},
311311
);
312312
}
@@ -591,14 +591,7 @@ fn parse_generator_metadata(blob: &[u8]) -> ProtoResult<GeneratorMetadata> {
591591
.into_iter()
592592
.flatten()
593593
.collect::<Vec<_>>();
594-
// `retry_infos[0]` mirrors the primary `usage` when only a single attempt
595-
// was made. Emitting both would double-count unkeyed Gemini turns, so drop
596-
// the mirror copy and let the primary carry the step fallback identity.
597-
if let (Some(primary), [retry]) = (usage.as_ref(), retry_usages.as_slice())
598-
&& retry.is_mirror_of(primary)
599-
{
600-
retry_usages.clear();
601-
}
594+
drop_primary_mirror(usage.as_ref(), &mut retry_usages);
602595
let timestamp = field_bytes(&chat_model_fields, 9)
603596
.map(parse_generation_info_timestamp)
604597
.transpose()?
@@ -618,6 +611,16 @@ fn parse_generator_metadata(blob: &[u8]) -> ProtoResult<GeneratorMetadata> {
618611
})
619612
}
620613

614+
// `retry_infos` lists every attempt, including the one already reported as the
615+
// primary `usage`, so exactly one entry may be its copy.
616+
fn drop_primary_mirror(primary: Option<&ModelUsage>, retries: &mut Vec<ModelUsage>) {
617+
if let Some(primary) = primary
618+
&& let Some(position) = retries.iter().position(|retry| retry.is_mirror_of(primary))
619+
{
620+
retries.remove(position);
621+
}
622+
}
623+
621624
fn parse_step_indices(root: &[ProtoField<'_>]) -> Vec<i64> {
622625
let mut indices = Vec::new();
623626
for field in root {
@@ -661,11 +664,7 @@ fn parse_step_metadata(blob: &[u8]) -> ProtoResult<StepMetadata> {
661664
.into_iter()
662665
.flatten()
663666
.collect::<Vec<_>>();
664-
if let (Some(primary), [retry]) = (usage.as_ref(), retry_usages.as_slice())
665-
&& retry.is_mirror_of(primary)
666-
{
667-
retry_usages.clear();
668-
}
667+
drop_primary_mirror(usage.as_ref(), &mut retry_usages);
669668
let model_info = field_bytes(&fields, 24)
670669
.map(parse_model_info)
671670
.transpose()?

0 commit comments

Comments
 (0)