Skip to content

Commit a4a31c3

Browse files
fix(pi): return zero totals for empty reports (#1674)
Keep the JSON totals shape stable when a pi report has no usage rows. Add a regression test covering the shared zero-valued totals object. Refs #832 and #1480. Co-authored-by: ryoppippi <[email protected]>
1 parent a26f517 commit a4a31c3

1 file changed

Lines changed: 20 additions & 1 deletion

File tree

‎rust/adapters/pi/src/report.rs‎

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ pub fn report_from_rows(rows: &[crate::UsageSummary], kind: AgentReportKind) ->
1414
.collect::<Vec<_>>();
1515
json!({
1616
rows_key(kind): rows_json,
17-
"totals": if rows.is_empty() { Value::Null } else { totals_json(rows) },
17+
"totals": totals_json(rows),
1818
})
1919
}
2020

@@ -72,3 +72,22 @@ fn rows_key(kind: AgentReportKind) -> &'static str {
7272
AgentReportKind::Session => "sessions",
7373
}
7474
}
75+
76+
#[cfg(test)]
77+
mod tests {
78+
use super::*;
79+
80+
#[test]
81+
fn empty_report_has_zero_totals_object() {
82+
let report = report_from_rows(&[], AgentReportKind::Daily);
83+
84+
assert_eq!(report["daily"], json!([]));
85+
assert!(report["totals"].is_object());
86+
assert_eq!(report["totals"]["inputTokens"], json!(0));
87+
assert_eq!(report["totals"]["outputTokens"], json!(0));
88+
assert_eq!(report["totals"]["cacheCreationTokens"], json!(0));
89+
assert_eq!(report["totals"]["cacheReadTokens"], json!(0));
90+
assert_eq!(report["totals"]["totalTokens"], json!(0));
91+
assert_eq!(report["totals"]["totalCost"].as_f64(), Some(0.0));
92+
}
93+
}

0 commit comments

Comments
 (0)