diff --git a/codex-rs/tui/src/app/event_dispatch.rs b/codex-rs/tui/src/app/event_dispatch.rs index 4f64afcf94..09276d55a5 100644 --- a/codex-rs/tui/src/app/event_dispatch.rs +++ b/codex-rs/tui/src/app/event_dispatch.rs @@ -1915,6 +1915,17 @@ impl App { } should_schedule_frame = matches!(origin, RateLimitRefreshOrigin::StartupPrefetch); + // A usage-nudge prefetch may complete just after the usual + // turn-end prompt slot. If the TUI is already idle, surface + // that newly latched prompt now instead of waiting for + // another turn. + if matches!(origin, RateLimitRefreshOrigin::UsageNudgePrefetch) + && self + .chat_widget + .maybe_show_pending_current_usage_limit_nudge_prompt_if_idle() + { + should_schedule_frame = true; + } } } Err(err) => { diff --git a/codex-rs/tui/src/app/tests.rs b/codex-rs/tui/src/app/tests.rs index 68888cbc2d..4306db52fc 100644 --- a/codex-rs/tui/src/app/tests.rs +++ b/codex-rs/tui/src/app/tests.rs @@ -194,6 +194,32 @@ async fn stale_rate_limit_refresh_results_are_ignored() { ); } +#[tokio::test] +async fn usage_nudge_prefetch_shows_prompt_when_result_arrives_while_idle() { + let mut app = make_test_app().await; + + assert!(app.handle_rate_limits_loaded( + /*refresh_generation*/ 0, + RateLimitRefreshOrigin::UsageNudgePrefetch, + Ok(vec![codex_rate_limit_snapshot( + /*used_percent*/ 75, + Some(UsageLimitNudge { + threshold: 75, + action: UsageLimitNudgeAction::AddCredits, + }), + )]), + )); + + assert!( + app.chat_widget + .has_active_current_usage_limit_nudge_for_test() + ); + assert!( + !app.chat_widget + .has_pending_current_usage_limit_nudge_for_test() + ); +} + #[tokio::test] async fn account_rate_limits_updated_prefetches_authoritative_usage_nudge_snapshot() { let (mut app, mut app_event_rx, _op_rx) = make_test_app_with_channels().await; diff --git a/codex-rs/tui/src/chatwidget.rs b/codex-rs/tui/src/chatwidget.rs index cbd823b5df..815bf2bced 100644 --- a/codex-rs/tui/src/chatwidget.rs +++ b/codex-rs/tui/src/chatwidget.rs @@ -6810,6 +6810,13 @@ impl ChatWidget { self.user_turn_pending_start || self.bottom_pane.is_task_running() } + pub(crate) fn maybe_show_pending_current_usage_limit_nudge_prompt_if_idle(&mut self) -> bool { + if self.is_user_turn_pending_or_running() { + return false; + } + self.maybe_show_pending_current_usage_limit_nudge_prompt() + } + fn only_user_shell_commands_running(&self) -> bool { self.agent_turn_running && !self.running_commands.is_empty() @@ -9284,6 +9291,11 @@ impl ChatWidget { self.current_usage_limit_nudge_prompt.is_active() } + #[cfg(test)] + pub(crate) fn has_pending_current_usage_limit_nudge_for_test(&self) -> bool { + self.current_usage_limit_nudge_prompt.has_pending() + } + pub(crate) fn set_approvals_reviewer(&mut self, policy: ApprovalsReviewer) { self.config.approvals_reviewer = policy; }