diff --git a/codex-rs/app-server/src/turn_cost_worker_chatgpt.rs b/codex-rs/app-server/src/turn_cost_worker_chatgpt.rs index b79fe31401..d728fee4ed 100644 --- a/codex-rs/app-server/src/turn_cost_worker_chatgpt.rs +++ b/codex-rs/app-server/src/turn_cost_worker_chatgpt.rs @@ -53,14 +53,13 @@ impl WorkerRuntime { continue; }; let (Some(micros), Some(settled_ids)) = - (cost.estimated_usage_usd_micros, cost.settled_response_ids) + (cost.visible_total_usd_micros(), cost.settled_response_ids) else { continue; }; // Missing/hidden dollars are not zero. A projection can appear before // all responses settle, so require every locally observed response. - if micros < 0 - || entry.expected_response_ids.is_empty() + if entry.expected_response_ids.is_empty() || !entry .expected_response_ids .iter() diff --git a/codex-rs/app-server/tests/suite/v2/turn_cost_otel.rs b/codex-rs/app-server/tests/suite/v2/turn_cost_otel.rs index f9a06f2e47..5f5c5f173e 100644 --- a/codex-rs/app-server/tests/suite/v2/turn_cost_otel.rs +++ b/codex-rs/app-server/tests/suite/v2/turn_cost_otel.rs @@ -64,7 +64,8 @@ async fn chatgpt_turn_cost_reaches_otlp_on_success() -> Result<()> { assert_eq!(body["include_settled_response_ids"], true); let threads: Vec<_> = body["threads"].as_array().expect("threads").iter().map(|thread| { let turns: Vec<_> = thread["turn_ids"].as_array().expect("turns").iter().map(|turn_id| json!({ - "turn_id": turn_id, "model": "mock-model", "estimated_usage_usd_micros": 1250001, + "turn_id": turn_id, "model": "mock-model", "estimated_usage_usd_micros": 250001, + "native_usage_usd_micros": 1000000, "settled_response_ids": ["resp-cost"] })).collect(); json!({"thread_id": thread["thread_id"], "turns": turns}) diff --git a/codex-rs/backend-client/src/client/chatgpt_turn_cost.rs b/codex-rs/backend-client/src/client/chatgpt_turn_cost.rs index ab3db6bb8c..82a5bc92a5 100644 --- a/codex-rs/backend-client/src/client/chatgpt_turn_cost.rs +++ b/codex-rs/backend-client/src/client/chatgpt_turn_cost.rs @@ -1,5 +1,5 @@ //! Queries SiWC per-turn estimates using the client's Codex or WHAM route and auth. -//! Always request settled response IDs and retain thread/turn association and missing amounts. +//! Always request settled response IDs and retain thread/turn association and unavailable amounts. use super::Client; use super::PathStyle; @@ -15,9 +15,29 @@ pub struct ChatgptTurnCost { pub turn_id: String, pub model: Option, pub estimated_usage_usd_micros: Option, + // Older backends report only converted credits. An explicit null means native USD is unavailable. + #[serde(default = "legacy_native_usd_micros")] + pub native_usage_usd_micros: Option, pub settled_response_ids: Option>, } +fn legacy_native_usd_micros() -> Option { + Some(0) +} + +impl ChatgptTurnCost { + /// Returns the complete visible estimate, including credit conversion and native USD. + pub fn visible_total_usd_micros(&self) -> Option { + // The backend also uses a missing converted estimate to hide dollar amounts. + let converted = self.estimated_usage_usd_micros?; + let native = self.native_usage_usd_micros?; + if converted < 0 || native < 0 { + return None; + } + converted.checked_add(native) + } +} + #[derive(Debug, Clone, PartialEq, Eq, Deserialize)] pub struct ChatgptThreadTurnCosts { pub thread_id: String, diff --git a/codex-rs/backend-client/src/client/chatgpt_turn_cost_tests.rs b/codex-rs/backend-client/src/client/chatgpt_turn_cost_tests.rs index e6c8fadb35..3ed481873c 100644 --- a/codex-rs/backend-client/src/client/chatgpt_turn_cost_tests.rs +++ b/codex-rs/backend-client/src/client/chatgpt_turn_cost_tests.rs @@ -45,11 +45,11 @@ async fn check_query(base_path: &str, endpoint: &str) { "threads": [ {"thread_id": "thread-a", "turns": [{ "turn_id": "turn-a", "model": "gpt-5.6", - "estimated_usage_usd_micros": 1250001, + "estimated_usage_usd_micros": 1250001, "native_usage_usd_micros": 750000, "settled_response_ids": ["resp-a", "resp-b"] }]}, {"thread_id": "thread-b", "turns": [ - {"turn_id": "turn-b", "estimated_usage_usd_micros": null, + {"turn_id": "turn-b", "estimated_usage_usd_micros": null, "native_usage_usd_micros": null, "settled_response_ids": null}, {"turn_id": "turn-c", "estimated_usage_usd_micros": 0} ]} @@ -82,6 +82,7 @@ async fn check_query(base_path: &str, endpoint: &str) { turn_id: "turn-a".to_string(), model: Some("gpt-5.6".to_string()), estimated_usage_usd_micros: Some(1250001), + native_usage_usd_micros: Some(750000), settled_response_ids: Some(vec!["resp-a".to_string(), "resp-b".to_string()]), }], }, @@ -92,12 +93,14 @@ async fn check_query(base_path: &str, endpoint: &str) { turn_id: "turn-b".to_string(), model: None, estimated_usage_usd_micros: None, + native_usage_usd_micros: None, settled_response_ids: None, }, ChatgptTurnCost { turn_id: "turn-c".to_string(), model: None, estimated_usage_usd_micros: Some(0), + native_usage_usd_micros: Some(0), settled_response_ids: None, }, ], @@ -106,3 +109,45 @@ async fn check_query(base_path: &str, endpoint: &str) { ); server.verify().await; } + +#[test] +fn visible_turn_cost_requires_both_dollar_components() { + for (converted, native, expected) in [ + (Some(0), Some(4_668), Some(4_668)), + (Some(120), Some(35), Some(155)), + (Some(120), Some(0), Some(120)), + (Some(0), Some(0), Some(0)), + (None, Some(35), None), + (Some(120), None, None), + (Some(-1), Some(35), None), + (Some(120), Some(-1), None), + (Some(i64::MAX), Some(1), None), + ] { + let cost: ChatgptTurnCost = serde_json::from_value(serde_json::json!({ + "turn_id": "turn-a", + "estimated_usage_usd_micros": converted, + "native_usage_usd_micros": native, + })) + .expect("valid turn cost response"); + assert_eq!( + cost.visible_total_usd_micros(), + expected, + "converted={converted:?}, native={native:?}" + ); + } +} + +#[test] +fn settled_turn_cost_distinguishes_omitted_native_usd_from_null() { + let mut turn = serde_json::json!({ + "turn_id": "turn-a", + "estimated_usage_usd_micros": 120, + "settled_response_ids": ["resp-a"], + }); + let legacy: ChatgptTurnCost = serde_json::from_value(turn.clone()).unwrap(); + turn["native_usage_usd_micros"] = serde_json::Value::Null; + let unavailable: ChatgptTurnCost = serde_json::from_value(turn).unwrap(); + + assert_eq!(legacy.visible_total_usd_micros(), Some(120)); + assert_eq!(unavailable.visible_total_usd_micros(), None); +}