Expose thread costs in statusline state and reject stale usage reads (#61410)

## Why

A replacement chat widget can receive a previous widget's usage result for the same thread. Statusline state also lacks the estimated costs available in the built-in status line.

## What changed

- Add optional `thread.usage.cost.total_cost_usd` and `total_credits` fields, using the built-in status line's plan-specific values. Convert micro-units to ordinary units and omit unavailable or negative values.
- Use UUIDs for thread usage requests and verify that each request is still pending before fetching usage or accepting its result.

## Testing

Add regression coverage for rejecting a previous widget's usage result and assertions for serialized costs across supported enterprise plans.

GitOrigin-RevId: 91c3f7cead0249b673979d17cc108ceae1088df2
This commit is contained in:
Joe Savona
2026-11-14 00:07:46 +00:00
committed by copyberry
parent e87cd55ed9
commit 5ca2007abe
8 changed files with 88 additions and 29 deletions
@@ -2,6 +2,7 @@ use super::THREAD_USAGE_RETRY_DELAYS;
use super::THREAD_USAGE_SETTLEMENT_DELAYS;
use super::ThreadUsageOutcome;
use crate::app_event::AppEvent;
use crate::chatwidget::ChatWidget;
use crate::chatwidget::ReplayKind;
use crate::chatwidget::tests::make_chatwidget_manual_with_sender;
use crate::status::format_estimated_usd_micros;
@@ -21,8 +22,9 @@ use pretty_assertions::assert_eq;
use std::time::Duration;
use std::time::Instant;
use tokio::sync::mpsc::UnboundedReceiver;
use uuid::Uuid;
fn thread_usage_request_id(events: &mut UnboundedReceiver<AppEvent>) -> u64 {
fn thread_usage_request_id(events: &mut UnboundedReceiver<AppEvent>) -> Uuid {
match events.try_recv() {
Ok(AppEvent::RefreshThreadUsage { request_id, .. }) => request_id,
event => panic!("expected a thread usage request, got {event:?}"),
@@ -43,6 +45,30 @@ fn thread_usage(
}
}
async fn widget_with_pending_billing_read(thread_id: ThreadId) -> (ChatWidget, Uuid) {
let (mut chat, _sender, mut events, _ops) = make_chatwidget_manual_with_sender().await;
chat.thread_id = Some(thread_id);
chat.has_chatgpt_account = true;
chat.has_codex_backend_auth = true;
chat.plan_type = Some(PlanType::Business);
chat.ensure_thread_usage_requested();
let request_id = thread_usage_request_id(&mut events);
(chat, request_id)
}
#[tokio::test]
async fn replacement_widget_rejects_a_previous_widgets_read_for_the_same_thread() {
let thread = ThreadId::new();
let (_, old_request) = widget_with_pending_billing_read(thread).await;
let (mut replacement, current) = widget_with_pending_billing_read(thread).await;
assert!(!replacement.finish_thread_usage_refresh(
thread,
old_request,
Ok(ThreadUsageOutcome::Disabled)
));
assert!(replacement.is_pending_thread_usage(thread, current));
}
#[test]
fn estimated_cost_formats_dollars_and_sub_cent_values() {
assert_eq!(
@@ -687,6 +713,13 @@ async fn billing_surfaces_render_for_every_supported_enterprise_plan() {
));
assert!(rx.try_recv().is_err(), "billing surfaces share a request");
let snapshot = serde_json::to_value(chat.statusline_state()).unwrap();
let expected_cost = if plan_type == PlanType::Ent26 {
serde_json::json!({"total_cost_usd": 0.75})
} else {
serde_json::json!({"total_cost_usd": 0.21, "total_credits": 5.2})
};
assert_eq!(snapshot["thread"]["usage"]["cost"], expected_cost);
if plan_type == PlanType::Ent26 {
insta::assert_snapshot!(chat.status_line_text().unwrap(), @"~$0.75");
insta::assert_snapshot!(chat.last_terminal_title.as_ref().unwrap(), @"~$0.75");