From c37b7307db57ab8510af14426f739a23e8186679 Mon Sep 17 00:00:00 2001 From: Felipe Coury Date: Mon, 21 Sep 2026 18:18:31 +0000 Subject: [PATCH] Format analytics credit usage with consistent decimal precision (#47086) ## Why Chat analytics displayed raw credit strings, exposing values such as `0E-10` for plan-covered tasks and unnecessary trailing decimal zeros. ## What changed Format ordinary credit amounts with two decimal places and thousands separators. Round directly from decimal digits to avoid floating-point rounding errors, so `1.005` becomes `1.01`. Normalize zero to `0.00`, while preserving the original strings for nonzero sub-cent amounts and values outside the safe conversion range. ## Testing Add assertions for decimal and scientific notation, signed zero, rounding boundaries, tiny adjustments, and large values. Update the chat analytics snapshot to cover a plan-covered task showing `0.00` credits. GitOrigin-RevId: f946cb408c2c37291447f90de02ebbb557fbc662 --- .../src/analytics/consumer_refresh_tests.rs | 35 ++++++++++++++ ...er_refresh__consumer_chat_all_metrics.snap | 4 +- codex-rs/tui/src/analytics/task_panel.rs | 46 ++++++++++++++++++- 3 files changed, 82 insertions(+), 3 deletions(-) diff --git a/codex-rs/tui/src/analytics/consumer_refresh_tests.rs b/codex-rs/tui/src/analytics/consumer_refresh_tests.rs index 9861becd44..1ac1782e9b 100644 --- a/codex-rs/tui/src/analytics/consumer_refresh_tests.rs +++ b/codex-rs/tui/src/analytics/consumer_refresh_tests.rs @@ -61,6 +61,41 @@ fn consumer_chats_keep_missing_rows_and_only_offer_known_metrics() { .unwrap() .amounts .weekly_limit_percent = Some(125.0); + chats.rows.push(Chat { + title: "Plan-covered task".into(), + task: Some(serde_json::from_value(json!({ + "thread_id":"plan", "data_status":"available", "usage_source":"included_plan", + "weekly_limit_percent":12.1,"five_hour_limit_percent":null,"balance_usage_credits":"0E-10","groups":[] + })).unwrap()), + }); + for (raw, expected) in [ + ("0E-10", "0.00"), + ("0.0000000000", "0.00"), + ("-0e+10", "0.00"), + ("1e-1000", "1e-1000"), + ("-1e-1000", "-1e-1000"), + ("1e1000", "1e1000"), + ("12.3", "12.30"), + ("12.3400000000", "12.34"), + ("12.346", "12.35"), + ("2.675", "2.68"), + ("-2.675", "-2.68"), + ("1.005", "1.01"), + ("1.0049999999", "1.00"), + ("9.999", "10.00"), + ("0012.3", "12.30"), + ("1.23e2", "123.00"), + ("1234e-2", "12.34"), + ("9007199254740993", "9007199254740993"), + ("999999999999999999", "999999999999999999"), + ("141436", "141,436.00"), + ("0.01", "0.01"), + ("-0.000004", "-0.000004"), + ("1e-7", "1e-7"), + ] { + let amounts = serde_json::from_value(json!({"balance_usage_credits":raw})).unwrap(); + assert_eq!(task_panel::amount(Some(&amounts), /*metric*/ 2), expected); + } assert_eq!(view.task_metrics(), vec![0, 1, 2]); insta::assert_snapshot!( "consumer_chat_all_metrics", diff --git a/codex-rs/tui/src/analytics/snapshots/codex_tui__analytics__tests__consumer_refresh__consumer_chat_all_metrics.snap b/codex-rs/tui/src/analytics/snapshots/codex_tui__analytics__tests__consumer_refresh__consumer_chat_all_metrics.snap index 8b9652df9d..9a4263d34a 100644 --- a/codex-rs/tui/src/analytics/snapshots/codex_tui__analytics__tests__consumer_refresh__consumer_chat_all_metrics.snap +++ b/codex-rs/tui/src/analytics/snapshots/codex_tui__analytics__tests__consumer_refresh__consumer_chat_all_metrics.snap @@ -23,12 +23,12 @@ expression: "screen(&mut view, 110, 30)" " gpt-5.5 — " " high · fast · codex " " " +" Plan-covered task 12.1% — 0.00 " " Chat usage unavailable — — — " " Chat usage unavailable — — — " " " -" Showing 1–3 of 3 chats " +" Showing 1–4 of 4 chats " " Local chats · includes discovered descendants · excludes archived roots " " " -" " " ↑/↓ row · enter details · esc back " " tab/1–6 report · z dashboard · ? help · R refresh " diff --git a/codex-rs/tui/src/analytics/task_panel.rs b/codex-rs/tui/src/analytics/task_panel.rs index 98450ce2fb..248975b2a8 100644 --- a/codex-rs/tui/src/analytics/task_panel.rs +++ b/codex-rs/tui/src/analytics/task_panel.rs @@ -46,7 +46,51 @@ pub(super) fn amount(amounts: Option<&TaskUsageAmounts>, metric: usize) -> Strin amounts .balance_usage_credits .as_ref() - .map(|amount| amount.as_str().to_string()) + .map(|amount| { + let value = amount.as_str(); + let (mantissa, exponent) = value.split_once(['e', 'E']).unwrap_or((value, "0")); + let fraction = mantissa + .split_once('.') + .map_or("", |(_, fraction)| fraction); + let digits = mantissa.trim_start_matches(['+', '-']).replace('.', ""); + let digits = digits.trim_start_matches('0'); + if digits.is_empty() { + return "0.00".to_string(); + } + let cent_digits = digits.len() as i64 + + exponent.parse::().unwrap_or_default() as i64 + - fraction.len() as i64 + + 2; + // Retain tiny adjustments and bound the integer conversion before padding. + if !(1..=18).contains(¢_digits) { + return value.to_string(); + } + let mut cents = digits + .bytes() + .chain(std::iter::repeat(b'0')) + .take(cent_digits as usize) + .fold( + /*init*/ 0_i64, + |cents, digit| cents * 10 + i64::from(digit - b'0'), + ); + if digits + .as_bytes() + .get(cent_digits as usize) + .is_some_and(|digit| *digit >= b'5') + { + cents += 1; + } + cents + .checked_mul(/*rhs*/ 10_000) + .map(|micros| { + super::data::credits(if mantissa.starts_with('-') { + -micros + } else { + micros + }) + }) + .unwrap_or_else(|| value.to_string()) + }) .unwrap_or_else(|| "—".into()) } else { (if metric == 0 {