From a6c346b9e1689ca5e49f494bf2a347f5b3503715 Mon Sep 17 00:00:00 2001 From: Odysseas Yiakoumis Date: Mon, 25 Aug 2025 22:42:22 +0400 Subject: [PATCH] avoid error when /compact response has no token_usage (#2417) (#2640) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit **Context** When running `/compact`, `drain_to_completed` would throw an error if `token_usage` was `None` in `ResponseEvent::Completed`. This made the command fail even though everything else had succeeded. **What changed** - Instead of erroring, we now just check `if let Some(token_usage)` before sending the event. - If it’s missing, we skip it and move on. **Why** This makes `AgentTask::compact()` behave in the same way as `AgentTask::spawn()`, which also doesn’t error out when `token_usage` isn’t available. Keeps things consistent and avoids unnecessary failures. **Fixes** Closes #2417 --------- Co-authored-by: Ahmed Ibrahim --- codex-rs/core/src/codex.rs | 13 ++++--------- 1 file changed, 4 insertions(+), 9 deletions(-) diff --git a/codex-rs/core/src/codex.rs b/codex-rs/core/src/codex.rs index 49d9c03c..72a13750 100644 --- a/codex-rs/core/src/codex.rs +++ b/codex-rs/core/src/codex.rs @@ -2816,15 +2816,9 @@ async fn drain_to_completed( response_id: _, token_usage, }) => { - let token_usage = match token_usage { - Some(usage) => usage, - None => { - return Err(CodexErr::Stream( - "token_usage was None in ResponseEvent::Completed".into(), - None, - )); - } - }; + // some providers don't return token usage, so we default + // TODO: consider approximate token usage + let token_usage = token_usage.unwrap_or_default(); sess.tx_event .send(Event { id: sub_id.to_string(), @@ -2832,6 +2826,7 @@ async fn drain_to_completed( }) .await .ok(); + return Ok(()); } Ok(_) => continue,