Lightweight coding agent that runs in your terminal
Rust
Fix TUI context window display before first TokenCount (#13896)
Eric Traute8d7ede83cf0
diff --git a/codex-rs/tui/src/app.rs b/codex-rs/tui/src/app.rsindex b0b7ecc75d..6fd6819d57 100644--- a/codex-rs/tui/src/app.rs+++ b/codex-rs/tui/src/app.rs@@ -3316,7 +3316,7 @@ impl App {fn handle_codex_event_now(&mut self, event: Event) {let needs_refresh = matches!(event.msg,- EventMsg::SessionConfigured(_) | EventMsg::TokenCount(_)+ EventMsg::SessionConfigured(_) | EventMsg::TurnStarted(_) | EventMsg::TokenCount(_));// This guard is only for intentional thread-switch shutdowns.// App-exit shutdowns are tracked by `pending_shutdown_exit_thread_id`@@ -4805,6 +4805,29 @@ mod tests {);}+ #[tokio::test]+ async fn live_turn_started_refreshes_status_line_with_runtime_context_window() {+ let mut app = make_test_app().await;+ app.chat_widget+ .setup_status_line(vec![crate::bottom_pane::StatusLineItem::ContextWindowSize]);++ assert_eq!(app.chat_widget.status_line_text(), None);++ app.handle_codex_event_now(Event {+ id: "turn-started".to_string(),+ msg: EventMsg::TurnStarted(TurnStartedEvent {+ turn_id: "turn-1".to_string(),+ model_context_window: Some(950_000),+ collaboration_mode_kind: Default::default(),+ }),+ });++ assert_eq!(+ app.chat_widget.status_line_text(),+ Some("950K window".into())+ );+ }+#[tokio::test]async fn open_agent_picker_keeps_missing_threads_for_replay() -> Result<()> {let mut app = make_test_app().await;diff --git a/codex-rs/tui/src/chatwidget.rs b/codex-rs/tui/src/chatwidget.rsindex c7f4bd1046..9e9d750b4a 100644--- a/codex-rs/tui/src/chatwidget.rs+++ b/codex-rs/tui/src/chatwidget.rs@@ -1711,6 +1711,27 @@ impl ChatWidget {}}+ fn apply_turn_started_context_window(&mut self, model_context_window: Option<i64>) {+ let info = match self.token_info.take() {+ Some(mut info) => {+ info.model_context_window = model_context_window;+ info+ }+ None => {+ let Some(model_context_window) = model_context_window else {+ return;+ };+ TokenUsageInfo {+ total_token_usage: TokenUsage::default(),+ last_token_usage: TokenUsage::default(),+ model_context_window: Some(model_context_window),+ }+ }+ };++ self.apply_token_info(info);+ }+fn apply_token_info(&mut self, info: TokenUsageInfo) {let percent = self.context_remaining_percent(&info);let used_tokens = self.context_used_tokens(&info, percent.is_some());@@ -4736,8 +4757,9 @@ impl ChatWidget {self.on_agent_reasoning_final();}EventMsg::AgentReasoningSectionBreak(_) => self.on_reasoning_section_break(),- EventMsg::TurnStarted(_) => {+ EventMsg::TurnStarted(event) => {if !is_resume_initial_replay {+ self.apply_turn_started_context_window(event.model_context_window);self.on_task_started();}}@@ -8440,6 +8462,11 @@ impl ChatWidget {&self.config}+ #[cfg(test)]+ pub(crate) fn status_line_text(&self) -> Option<String> {+ self.bottom_pane.status_line_text()+ }+pub(crate) fn clear_token_usage(&mut self) {self.token_info = None;}diff --git a/codex-rs/tui/src/chatwidget/tests.rs b/codex-rs/tui/src/chatwidget/tests.rsindex b0ff6f3892..7307891106 100644--- a/codex-rs/tui/src/chatwidget/tests.rs+++ b/codex-rs/tui/src/chatwidget/tests.rs@@ -1659,6 +1659,53 @@ async fn context_indicator_shows_used_tokens_when_window_unknown() {);}+#[tokio::test]+async fn turn_started_uses_runtime_context_window_before_first_token_count() {+ let (mut chat, mut rx, _ops) = make_chatwidget_manual(None).await;++ chat.config.model_context_window = Some(1_000_000);++ chat.handle_codex_event(Event {+ id: "turn-start".into(),+ msg: EventMsg::TurnStarted(TurnStartedEvent {+ turn_id: "turn-1".to_string(),+ model_context_window: Some(950_000),+ collaboration_mode_kind: ModeKind::Default,+ }),+ });++ assert_eq!(+ chat.status_line_value_for_item(&crate::bottom_pane::StatusLineItem::ContextWindowSize),+ Some("950K window".to_string())+ );+ assert_eq!(chat.bottom_pane.context_window_percent(), Some(100));++ chat.add_status_output();++ let cells = drain_insert_history(&mut rx);+ let context_line = cells+ .last()+ .expect("status output inserted")+ .iter()+ .map(|line| {+ line.spans+ .iter()+ .map(|span| span.content.as_ref())+ .collect::<String>()+ })+ .find(|line| line.contains("Context window"))+ .expect("context window line");++ assert!(+ context_line.contains("950K"),+ "expected /status to use TurnStarted context window, got: {context_line}"+ );+ assert!(+ !context_line.contains("1M"),+ "expected /status to avoid raw config context window, got: {context_line}"+ );+}+#[cfg_attr(target_os = "macos",ignore = "system configuration APIs are blocked under macOS seatbelt"@@ -1952,7 +1999,7 @@ fn lines_to_single_string(lines: &[ratatui::text::Line<'static>]) -> String {}fn status_line_text(chat: &ChatWidget) -> Option<String> {- chat.bottom_pane.status_line_text()+ chat.status_line_text()}fn make_token_info(total_tokens: i64, context_window: i64) -> TokenUsageInfo {