Skip to content

armgit / codex

Public Imported from GitHub
0

Lightweight coding agent that runs in your terminal

Rust

Format analytics credit usage with consistent decimal precision (#47086)

Felipe Couryc37b7307db57
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::<i32>().unwrap_or_default() as i64
+ - fraction.len() as i64
+ + 2;
+ // Retain tiny adjustments and bound the integer conversion before padding.
+ if !(1..=18).contains(&cent_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 {