Fix TUI context window display before first TokenCount (#13896)

The TUI was showing the raw configured `model_context_window` until the
first
`TokenCount` event arrived, even though core had already emitted the
effective
runtime window on `TurnStarted`. This made the footer, status-line
context
window, and `/status` output briefly inconsistent for models/configs
where the
effective window differs from the configured value, such as the
`gpt-5.4`
1,000,000-token override reported in #13623.

Update the TUI to cache `TurnStarted.model_context_window` immediately
so
pre-token-count displays use the runtime effective window, and add
regression
coverage for the startup path.

---------

Co-authored-by: Charles Cunningham <ccunningham@openai.com>
Co-authored-by: Codex <noreply@openai.com>
This commit is contained in:
Eric Traut 2026-03-07 17:01:47 -07:00 committed by GitHub
parent 92f7541624
commit e8d7ede83c
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 100 additions and 3 deletions

View file

@ -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;

View file

@ -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;
}

View file

@ -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 {