Skip to content

Commit 6a38480

Browse files
ryoppippiaxisrow
andcommitted
fix(zcode): saturate report token arithmetic
Apply saturating arithmetic to shared, focused, and unified report accumulation and totals so extreme counters cannot panic or wrap. Add daily and weekly report regressions and document the ZCode fixture surface and its dependencies. Co-authored-by: axisrow <[email protected]>
1 parent aa4e00f commit 6a38480

8 files changed

Lines changed: 301 additions & 72 deletions

File tree

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

Lines changed: 40 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,7 @@ mod tests {
132132
}
133133

134134
#[test]
135-
fn snapshots_focused_zcode_json_reports_for_daily_monthly_and_session() {
135+
fn snapshots_focused_zcode_json_reports_for_daily_monthly_weekly_and_session() {
136136
let entries = snapshot_entries();
137137
let daily = summarize_entries(&entries, AgentReportKind::Daily).unwrap();
138138
let monthly = summarize_entries(&entries, AgentReportKind::Monthly).unwrap();
@@ -157,6 +157,45 @@ mod tests {
157157
);
158158
}
159159

160+
#[test]
161+
fn saturates_focused_daily_and_weekly_reports_for_extreme_counters() {
162+
let entries = [
163+
extreme_entry("2099-01-02", 4_070_908_800_000, u64::MAX),
164+
extreme_entry("2099-01-03", 4_071_081_600_000, 1),
165+
];
166+
let daily = summarize_entries(&entries, AgentReportKind::Daily).unwrap();
167+
let daily_report = report_from_rows(&daily, AgentReportKind::Daily);
168+
let weekly = summarize_entries(&entries, AgentReportKind::Weekly).unwrap();
169+
let weekly_report = report_from_rows(&weekly, AgentReportKind::Weekly);
170+
171+
for report in [daily_report, weekly_report] {
172+
for key in [
173+
"inputTokens",
174+
"outputTokens",
175+
"cacheCreationTokens",
176+
"cacheReadTokens",
177+
"totalTokens",
178+
] {
179+
assert_eq!(report["totals"][key], u64::MAX, "{key}");
180+
}
181+
}
182+
assert_eq!(weekly[0].model_breakdowns[0].input_tokens, u64::MAX);
183+
assert_eq!(weekly[0].model_breakdowns[0].output_tokens, u64::MAX);
184+
}
185+
186+
fn extreme_entry(date: &str, millis: i64, tokens: u64) -> LoadedEntry {
187+
let mut entry = entry("session-extreme", date, millis);
188+
entry.data.message.usage = TokenUsageRaw {
189+
input_tokens: tokens,
190+
output_tokens: tokens,
191+
cache_creation_input_tokens: tokens,
192+
cache_read_input_tokens: tokens,
193+
..TokenUsageRaw::default()
194+
};
195+
entry.extra_total_tokens = tokens;
196+
entry
197+
}
198+
160199
fn snapshot_entries() -> Vec<LoadedEntry> {
161200
[
162201
(

‎rust/crates/ccusage-adapter-all/src/report.rs‎

Lines changed: 29 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -234,11 +234,26 @@ fn agent_json(row: &AllRow) -> Value {
234234

235235
fn totals_json(rows: &[AllRow], include_sources: bool) -> Value {
236236
let mut totals = json!({
237-
"inputTokens": rows.iter().map(|row| row.input_tokens).sum::<u64>(),
238-
"outputTokens": rows.iter().map(|row| row.output_tokens).sum::<u64>(),
239-
"cacheCreationTokens": rows.iter().map(|row| row.cache_creation_tokens).sum::<u64>(),
240-
"cacheReadTokens": rows.iter().map(|row| row.cache_read_tokens).sum::<u64>(),
241-
"totalTokens": rows.iter().map(|row| row.total_tokens).sum::<u64>(),
237+
"inputTokens": rows
238+
.iter()
239+
.map(|row| row.input_tokens)
240+
.fold(0, u64::saturating_add),
241+
"outputTokens": rows
242+
.iter()
243+
.map(|row| row.output_tokens)
244+
.fold(0, u64::saturating_add),
245+
"cacheCreationTokens": rows
246+
.iter()
247+
.map(|row| row.cache_creation_tokens)
248+
.fold(0, u64::saturating_add),
249+
"cacheReadTokens": rows
250+
.iter()
251+
.map(|row| row.cache_read_tokens)
252+
.fold(0, u64::saturating_add),
253+
"totalTokens": rows
254+
.iter()
255+
.map(|row| row.total_tokens)
256+
.fold(0, u64::saturating_add),
242257
"totalCost": json_float(rows.iter().map(|row| row.total_cost).sum::<f64>()),
243258
});
244259
if include_sources {
@@ -356,7 +371,10 @@ pub(super) fn print_table_with_options(
356371
}
357372
table.separator();
358373
let totals = totals_json(rows, include_sources);
359-
let table_total_tokens = rows.iter().map(table_total_tokens).sum::<u64>();
374+
let table_total_tokens = rows
375+
.iter()
376+
.map(table_total_tokens)
377+
.fold(0, u64::saturating_add);
360378
if compact {
361379
let mut total_row = vec![
362380
color(shared, "Total", Color::Yellow),
@@ -721,8 +739,11 @@ fn push_model_breakdown_rows(
721739
shared: &SharedArgs,
722740
) {
723741
for b in breakdowns {
724-
let total =
725-
b.input_tokens + b.output_tokens + b.cache_creation_tokens + b.cache_read_tokens;
742+
let total = b
743+
.input_tokens
744+
.saturating_add(b.output_tokens)
745+
.saturating_add(b.cache_creation_tokens)
746+
.saturating_add(b.cache_read_tokens);
726747
let model = color(
727748
shared,
728749
format!("- {}", short_model_name(&b.model_name)),

‎rust/crates/ccusage-adapter-all/src/tests.rs‎

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,87 @@ fn aggregates_daily_agent_rows_by_period() {
137137
assert_eq!(breakdowns[1].agent, "codex");
138138
}
139139

140+
#[test]
141+
fn saturates_unified_daily_and_weekly_reports_for_extreme_counters() {
142+
let rows = vec![
143+
extreme_all_row("2099-01-02", u64::MAX),
144+
extreme_all_row("2099-01-03", 1),
145+
];
146+
let daily_rows = aggregate_rows(rows.clone(), AgentReportKind::Daily);
147+
let daily_report = report_json_with_options(&daily_rows, AgentReportKind::Daily, false, true);
148+
let weekly_rows = aggregate_rows(rows, AgentReportKind::Weekly);
149+
let weekly_report =
150+
report_json_with_options(&weekly_rows, AgentReportKind::Weekly, false, true);
151+
152+
for report in [daily_report, weekly_report] {
153+
for key in [
154+
"inputTokens",
155+
"outputTokens",
156+
"cacheCreationTokens",
157+
"cacheReadTokens",
158+
"totalTokens",
159+
] {
160+
assert_eq!(report["totals"][key], u64::MAX, "{key}");
161+
}
162+
assert_eq!(
163+
report["totals"]["sourceBreakdowns"][0]["inputTokens"],
164+
u64::MAX
165+
);
166+
assert_eq!(
167+
report["totals"]["sourceBreakdowns"][0]["modelBreakdowns"][0]["inputTokens"],
168+
u64::MAX
169+
);
170+
}
171+
assert_eq!(weekly_rows[0].model_breakdowns[0].input_tokens, u64::MAX);
172+
assert_eq!(weekly_rows[0].model_breakdowns[0].output_tokens, u64::MAX);
173+
}
174+
175+
fn extreme_all_row(period: &str, tokens: u64) -> AllRow {
176+
AllRow {
177+
period: period.to_string(),
178+
agent: "codex",
179+
models_used: vec!["gpt-5".to_string()],
180+
input_tokens: tokens,
181+
output_tokens: tokens,
182+
cache_creation_tokens: tokens,
183+
cache_read_tokens: tokens,
184+
total_tokens: tokens,
185+
total_cost: 0.0,
186+
metadata: Some(json!({
187+
"sourceBreakdowns": [{
188+
"source": "Responses API",
189+
"modelsUsed": ["gpt-5"],
190+
"inputTokens": tokens,
191+
"outputTokens": tokens,
192+
"cacheCreationTokens": tokens,
193+
"cacheReadTokens": tokens,
194+
"reasoningOutputTokens": tokens,
195+
"totalTokens": tokens,
196+
"totalCost": 0.0,
197+
"modelBreakdowns": [{
198+
"modelName": "gpt-5",
199+
"inputTokens": tokens,
200+
"outputTokens": tokens,
201+
"cacheCreationTokens": tokens,
202+
"cacheReadTokens": tokens,
203+
"cost": 0.0
204+
}]
205+
}]
206+
})),
207+
metadata_agents: Some(vec!["codex"]),
208+
agent_breakdowns: None,
209+
model_breakdowns: vec![ModelBreakdown {
210+
model_name: "gpt-5".to_string(),
211+
input_tokens: tokens,
212+
output_tokens: tokens,
213+
cache_creation_tokens: tokens,
214+
cache_read_tokens: tokens,
215+
extra_total_tokens: tokens,
216+
..ModelBreakdown::default()
217+
}],
218+
}
219+
}
220+
140221
#[test]
141222
fn merges_same_agent_daily_rows_into_one_monthly_breakdown() {
142223
let rows = aggregate_rows(

‎rust/crates/ccusage-adapter-all/src/types.rs‎

Lines changed: 31 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -79,11 +79,13 @@ pub(super) struct AllAccumulator {
7979

8080
impl AllAccumulator {
8181
pub(super) fn add(&mut self, row: AllRow) {
82-
self.input_tokens += row.input_tokens;
83-
self.output_tokens += row.output_tokens;
84-
self.cache_creation_tokens += row.cache_creation_tokens;
85-
self.cache_read_tokens += row.cache_read_tokens;
86-
self.total_tokens += row.total_tokens;
82+
self.input_tokens = self.input_tokens.saturating_add(row.input_tokens);
83+
self.output_tokens = self.output_tokens.saturating_add(row.output_tokens);
84+
self.cache_creation_tokens = self
85+
.cache_creation_tokens
86+
.saturating_add(row.cache_creation_tokens);
87+
self.cache_read_tokens = self.cache_read_tokens.saturating_add(row.cache_read_tokens);
88+
self.total_tokens = self.total_tokens.saturating_add(row.total_tokens);
8789
self.total_cost += row.total_cost;
8890
self.models.extend(row.models_used.iter().cloned());
8991
if let Some(agents) = row.metadata_agents.as_ref() {
@@ -132,11 +134,15 @@ impl AllAccumulator {
132134
}
133135

134136
fn merge_agent_breakdown(target: &mut AllRow, source: AllRow) {
135-
target.input_tokens += source.input_tokens;
136-
target.output_tokens += source.output_tokens;
137-
target.cache_creation_tokens += source.cache_creation_tokens;
138-
target.cache_read_tokens += source.cache_read_tokens;
139-
target.total_tokens += source.total_tokens;
137+
target.input_tokens = target.input_tokens.saturating_add(source.input_tokens);
138+
target.output_tokens = target.output_tokens.saturating_add(source.output_tokens);
139+
target.cache_creation_tokens = target
140+
.cache_creation_tokens
141+
.saturating_add(source.cache_creation_tokens);
142+
target.cache_read_tokens = target
143+
.cache_read_tokens
144+
.saturating_add(source.cache_read_tokens);
145+
target.total_tokens = target.total_tokens.saturating_add(source.total_tokens);
140146
target.total_cost += source.total_cost;
141147
let mut models: BTreeSet<String> = target.models_used.drain(..).collect();
142148
models.extend(source.models_used);
@@ -209,7 +215,7 @@ fn add_json_number(target: &mut Map<String, Value>, source: &Map<String, Value>,
209215
return;
210216
};
211217
let value = match (target_value.as_u64(), source_value.as_u64()) {
212-
(Some(target), Some(source)) => json!(target + source),
218+
(Some(target), Some(source)) => json!(target.saturating_add(source)),
213219
_ => {
214220
json_float(target_value.as_f64().unwrap_or(0.0) + source_value.as_f64().unwrap_or(0.0))
215221
}
@@ -301,11 +307,13 @@ fn merge_model_breakdowns(
301307
i
302308
});
303309
let b = &mut breakdowns[index];
304-
b.input_tokens += item.input_tokens;
305-
b.output_tokens += item.output_tokens;
306-
b.cache_creation_tokens += item.cache_creation_tokens;
307-
b.cache_read_tokens += item.cache_read_tokens;
308-
b.extra_total_tokens += item.extra_total_tokens;
310+
b.input_tokens = b.input_tokens.saturating_add(item.input_tokens);
311+
b.output_tokens = b.output_tokens.saturating_add(item.output_tokens);
312+
b.cache_creation_tokens = b
313+
.cache_creation_tokens
314+
.saturating_add(item.cache_creation_tokens);
315+
b.cache_read_tokens = b.cache_read_tokens.saturating_add(item.cache_read_tokens);
316+
b.extra_total_tokens = b.extra_total_tokens.saturating_add(item.extra_total_tokens);
309317
b.cost += item.cost;
310318
b.missing_pricing |= item.missing_pricing;
311319
}
@@ -327,11 +335,13 @@ fn aggregate_model_breakdowns(rows: &[AllRow]) -> Vec<ModelBreakdown> {
327335
i
328336
});
329337
let b = &mut breakdowns[index];
330-
b.input_tokens += item.input_tokens;
331-
b.output_tokens += item.output_tokens;
332-
b.cache_creation_tokens += item.cache_creation_tokens;
333-
b.cache_read_tokens += item.cache_read_tokens;
334-
b.extra_total_tokens += item.extra_total_tokens;
338+
b.input_tokens = b.input_tokens.saturating_add(item.input_tokens);
339+
b.output_tokens = b.output_tokens.saturating_add(item.output_tokens);
340+
b.cache_creation_tokens = b
341+
.cache_creation_tokens
342+
.saturating_add(item.cache_creation_tokens);
343+
b.cache_read_tokens = b.cache_read_tokens.saturating_add(item.cache_read_tokens);
344+
b.extra_total_tokens = b.extra_total_tokens.saturating_add(item.extra_total_tokens);
335345
b.cost += item.cost;
336346
b.missing_pricing |= item.missing_pricing;
337347
}

‎rust/crates/ccusage-core/src/output.rs‎

Lines changed: 33 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -77,20 +77,36 @@ pub fn session_summary_json(row: &UsageSummary) -> Value {
7777
}
7878

7979
pub fn totals_json(rows: &[UsageSummary]) -> Value {
80-
let input = rows.iter().map(|row| row.input_tokens).sum::<u64>();
81-
let output = rows.iter().map(|row| row.output_tokens).sum::<u64>();
80+
let input = rows
81+
.iter()
82+
.map(|row| row.input_tokens)
83+
.fold(0, u64::saturating_add);
84+
let output = rows
85+
.iter()
86+
.map(|row| row.output_tokens)
87+
.fold(0, u64::saturating_add);
8288
let cache_create = rows
8389
.iter()
8490
.map(|row| row.cache_creation_tokens)
85-
.sum::<u64>();
86-
let cache_read = rows.iter().map(|row| row.cache_read_tokens).sum::<u64>();
87-
let extra = rows.iter().map(|row| row.extra_total_tokens).sum::<u64>();
91+
.fold(0, u64::saturating_add);
92+
let cache_read = rows
93+
.iter()
94+
.map(|row| row.cache_read_tokens)
95+
.fold(0, u64::saturating_add);
96+
let extra = rows
97+
.iter()
98+
.map(|row| row.extra_total_tokens)
99+
.fold(0, u64::saturating_add);
88100
let mut value = json!({
89101
"inputTokens": input,
90102
"outputTokens": output,
91103
"cacheCreationTokens": cache_create,
92104
"cacheReadTokens": cache_read,
93-
"totalTokens": input + output + cache_create + cache_read + extra,
105+
"totalTokens": input
106+
.saturating_add(output)
107+
.saturating_add(cache_create)
108+
.saturating_add(cache_read)
109+
.saturating_add(extra),
94110
"totalCost": rows.iter().map(|row| row.total_cost).sum::<f64>(),
95111
});
96112
let credits = rows.iter().filter_map(|row| row.credits).sum::<f64>();
@@ -299,10 +315,12 @@ pub fn print_usage_table_with_options(
299315
.get("totalCost")
300316
.and_then(Value::as_f64)
301317
.unwrap_or_default();
302-
let total_tokens = totals
303-
.get("totalTokens")
304-
.and_then(Value::as_u64)
305-
.unwrap_or(input + output + cache_create + cache_read);
318+
let total_tokens = totals.get("totalTokens").and_then(Value::as_u64).unwrap_or(
319+
input
320+
.saturating_add(output)
321+
.saturating_add(cache_create)
322+
.saturating_add(cache_read),
323+
);
306324
table.separator();
307325
let mut total_row = vec![
308326
color(shared, "Total", Color::Yellow),
@@ -437,10 +455,11 @@ fn push_breakdown_rows(
437455
shared: &SharedArgs,
438456
) {
439457
for breakdown in &row.model_breakdowns {
440-
let total = breakdown.input_tokens
441-
+ breakdown.output_tokens
442-
+ breakdown.cache_creation_tokens
443-
+ breakdown.cache_read_tokens;
458+
let total = breakdown
459+
.input_tokens
460+
.saturating_add(breakdown.output_tokens)
461+
.saturating_add(breakdown.cache_creation_tokens)
462+
.saturating_add(breakdown.cache_read_tokens);
444463
let mut values = vec![
445464
color(
446465
shared,

0 commit comments

Comments
 (0)