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
+23 -16
View File
@@ -11,6 +11,7 @@ use super::ThreadId;
use crate::status::StatusHistoryHandle;
use crate::status::thread_cost_micros;
use codex_app_server_protocol::ThreadUsage;
use uuid::Uuid;
#[derive(Clone, Debug, PartialEq, Eq)]
pub(crate) enum ThreadUsageOutcome {
@@ -39,8 +40,7 @@ struct SelectedThreadUsage {
pub(super) struct ThreadUsageState {
thread_id: Option<ThreadId>,
estimate: Option<ThreadUsage>,
pending_request_id: Option<u64>,
next_request_id: u64,
pending_request_id: Option<Uuid>,
requested: bool,
feature_disabled: bool,
status_history_handles: Vec<StatusHistoryHandle>,
@@ -50,18 +50,14 @@ pub(super) struct ThreadUsageState {
settlement_attempts: usize,
settlement_baseline_credits_micros: Option<i64>,
settlement_baseline_usd_micros: Option<i64>,
settlement_request_id: Option<u64>,
settlement_request_id: Option<Uuid>,
settlement_refresh_due_at: Option<Instant>,
pub(super) replaying_turn_completion: bool,
}
impl ChatWidget {
pub(super) fn clear_thread_usage_state(&mut self) {
let next_request_id = self.thread_usage.next_request_id;
self.thread_usage = ThreadUsageState {
next_request_id,
..ThreadUsageState::default()
};
self.thread_usage = ThreadUsageState::default();
}
pub(super) fn ensure_thread_usage_requested(&mut self) {
@@ -178,16 +174,19 @@ impl ChatWidget {
self.thread_usage.settlement_refresh_due_at = None;
}
pub(crate) fn is_pending_thread_usage(&self, thread_id: ThreadId, request_id: Uuid) -> bool {
self.thread_id == Some(thread_id)
&& self.thread_usage.thread_id == Some(thread_id)
&& self.thread_usage.pending_request_id == Some(request_id)
}
pub(crate) fn finish_thread_usage_refresh(
&mut self,
thread_id: ThreadId,
request_id: u64,
request_id: Uuid,
result: Result<ThreadUsageOutcome, String>,
) -> bool {
if self.thread_id != Some(thread_id)
|| self.thread_usage.thread_id != Some(thread_id)
|| self.thread_usage.pending_request_id != Some(request_id)
{
if !self.is_pending_thread_usage(thread_id, request_id) {
return false;
}
@@ -324,6 +323,16 @@ impl ChatWidget {
true
}
pub(super) fn status_line_thread_credits_micros(&self) -> Option<i64> {
self.estimated_thread_usage()
.filter(|_| self.plan_type != Some(PlanType::Ent26))
.map(|usage| usage.estimated_usage_credits_micros)
}
pub(super) fn status_line_thread_cost_micros(&self) -> Option<i64> {
thread_cost_micros(self.estimated_thread_usage()?, self.plan_type)
}
pub(super) fn estimated_thread_usage(&self) -> Option<&ThreadUsage> {
self.thread_usage.estimate.as_ref()
}
@@ -336,9 +345,7 @@ impl ChatWidget {
return;
}
let request_id = self.thread_usage.next_request_id;
self.thread_usage.next_request_id =
self.thread_usage.next_request_id.wrapping_add(/*rhs*/ 1);
let request_id = Uuid::new_v4();
self.thread_usage.pending_request_id = Some(request_id);
self.thread_usage.requested = true;
self.thread_usage.thread_id = Some(thread_id);