From 21f7427483513b7e33d994a5e8d7ea17e8b62474 Mon Sep 17 00:00:00 2001 From: James Chu Date: Mon, 4 May 2026 12:32:57 -0400 Subject: [PATCH] [codex] Latch proactive usage warnings by threshold --- codex-rs/tui/src/app/tests.rs | 10 +++++--- codex-rs/tui/src/chatwidget.rs | 15 ++++++------ .../chatwidget/current_usage_limit_nudge.rs | 24 +++++++++++++++---- .../tests/current_usage_limit_nudge.rs | 15 ++++++++---- 4 files changed, 44 insertions(+), 20 deletions(-) diff --git a/codex-rs/tui/src/app/tests.rs b/codex-rs/tui/src/app/tests.rs index b400c4c19e..68888cbc2d 100644 --- a/codex-rs/tui/src/app/tests.rs +++ b/codex-rs/tui/src/app/tests.rs @@ -184,7 +184,7 @@ async fn stale_rate_limit_refresh_results_are_ignored() { /*refresh_generation*/ 1, RateLimitRefreshOrigin::UsageNudgePrefetch, Ok(vec![codex_rate_limit_snapshot( - /*used_percent*/ 90, None, + /*used_percent*/ 90, /*current_usage_limit_nudge*/ None, )]), ); @@ -206,7 +206,9 @@ async fn account_rate_limits_updated_prefetches_authoritative_usage_nudge_snapsh &app_server, codex_app_server_client::AppServerEvent::ServerNotification( ServerNotification::AccountRateLimitsUpdated(AccountRateLimitsUpdatedNotification { - rate_limits: codex_rate_limit_snapshot(/*used_percent*/ 75, None), + rate_limits: codex_rate_limit_snapshot( + /*used_percent*/ 75, /*current_usage_limit_nudge*/ None, + ), }), ), ) @@ -242,7 +244,9 @@ async fn account_rate_limits_updated_does_not_clear_authoritative_usage_nudge_st &app_server, codex_app_server_client::AppServerEvent::ServerNotification( ServerNotification::AccountRateLimitsUpdated(AccountRateLimitsUpdatedNotification { - rate_limits: codex_rate_limit_snapshot(/*used_percent*/ 80, None), + rate_limits: codex_rate_limit_snapshot( + /*used_percent*/ 80, /*current_usage_limit_nudge*/ None, + ), }), ), ) diff --git a/codex-rs/tui/src/chatwidget.rs b/codex-rs/tui/src/chatwidget.rs index 788661c6ca..cbd823b5df 100644 --- a/codex-rs/tui/src/chatwidget.rs +++ b/codex-rs/tui/src/chatwidget.rs @@ -2902,13 +2902,12 @@ impl ChatWidget { self.plan_type = snapshot.plan_type.or(self.plan_type); let is_codex_limit = limit_id.eq_ignore_ascii_case("codex"); - if is_codex_limit && matches!(source, RateLimitSnapshotSource::AuthoritativeRead) { - let nudge = if self.current_usage_limit_nudge_enabled() { - snapshot.current_usage_limit_nudge.clone() - } else { - None - }; - self.current_usage_limit_nudge_prompt.update(nudge); + if is_codex_limit + && matches!(source, RateLimitSnapshotSource::AuthoritativeRead) + && self.current_usage_limit_nudge_enabled() + && let Some(nudge) = snapshot.current_usage_limit_nudge.clone() + { + self.current_usage_limit_nudge_prompt.latch(nudge); } if self.current_usage_limit_nudge_prompt.is_active() { self.rate_limit_switch_prompt = RateLimitSwitchPromptState::Idle; @@ -9262,7 +9261,7 @@ impl ChatWidget { .set_turn_running(self.agent_turn_running); } if feature == Feature::CurrentUsageLimitNudge && !enabled { - self.current_usage_limit_nudge_prompt.update(None); + self.current_usage_limit_nudge_prompt.clear(); } #[cfg(target_os = "windows")] if matches!( diff --git a/codex-rs/tui/src/chatwidget/current_usage_limit_nudge.rs b/codex-rs/tui/src/chatwidget/current_usage_limit_nudge.rs index 64f33d662c..7589df2041 100644 --- a/codex-rs/tui/src/chatwidget/current_usage_limit_nudge.rs +++ b/codex-rs/tui/src/chatwidget/current_usage_limit_nudge.rs @@ -42,19 +42,33 @@ impl UsageNudgePrefetchThreshold { pub(super) struct CurrentUsageLimitNudgePromptState { active: bool, pending: Option, - has_shown: bool, + shown_thresholds: Vec, last_prefetches: Vec<(UsageNudgePrefetchKey, UsageNudgePrefetchThreshold)>, } impl CurrentUsageLimitNudgePromptState { - pub(super) fn update(&mut self, nudge: Option) { - self.active = nudge.is_some(); - self.pending = (!self.has_shown).then_some(nudge).flatten(); + pub(super) fn latch(&mut self, nudge: UsageLimitNudge) { + // Once the backend selects a warning for this process, keep that lane + // latched. If this threshold has not already been shown, retain it for + // the next prompt slot; later sparse snapshots should not retract the + // already-queued soft warning. + self.active = true; + if self.shown_thresholds.contains(&nudge.threshold) { + return; + } + self.pending = Some(nudge); + } + + pub(super) fn clear(&mut self) { + self.active = false; + self.pending = None; } pub(super) fn take_pending(&mut self) -> Option { let nudge = self.pending.take()?; - self.has_shown = true; + if !self.shown_thresholds.contains(&nudge.threshold) { + self.shown_thresholds.push(nudge.threshold); + } Some(nudge) } diff --git a/codex-rs/tui/src/chatwidget/tests/current_usage_limit_nudge.rs b/codex-rs/tui/src/chatwidget/tests/current_usage_limit_nudge.rs index 23896d154c..ce1e653313 100644 --- a/codex-rs/tui/src/chatwidget/tests/current_usage_limit_nudge.rs +++ b/codex-rs/tui/src/chatwidget/tests/current_usage_limit_nudge.rs @@ -62,7 +62,7 @@ async fn proactive_usage_prompt_renders_backend_actions() { } #[tokio::test] -async fn proactive_usage_prompt_shows_only_once_per_session() { +async fn proactive_usage_prompt_shows_once_per_threshold_per_session() { let (mut chat, _rx, _op_rx) = make_chatwidget_manual(/*model_override*/ None).await; chat.on_rate_limit_snapshot(Some(snapshot_with_nudge( @@ -70,12 +70,19 @@ async fn proactive_usage_prompt_shows_only_once_per_session() { UsageLimitNudgeAction::AddCredits, ))); assert!(chat.maybe_show_pending_current_usage_limit_nudge_prompt()); + chat.handle_key_event(KeyEvent::new(KeyCode::Char('n'), KeyModifiers::NONE)); + + chat.on_rate_limit_snapshot(Some(snapshot_with_nudge( + /*threshold*/ 75, + UsageLimitNudgeAction::Upgrade, + ))); + assert!(!chat.maybe_show_pending_current_usage_limit_nudge_prompt()); chat.on_rate_limit_snapshot(Some(snapshot_with_nudge( /*threshold*/ 90, UsageLimitNudgeAction::Upgrade, ))); - assert!(!chat.maybe_show_pending_current_usage_limit_nudge_prompt()); + assert!(chat.maybe_show_pending_current_usage_limit_nudge_prompt()); } #[tokio::test] @@ -100,7 +107,7 @@ async fn proactive_usage_prompt_live_snapshot_does_not_clear_authoritative_pendi } #[tokio::test] -async fn proactive_usage_prompt_authoritative_empty_snapshot_clears_pending_nudge() { +async fn proactive_usage_prompt_authoritative_empty_snapshot_preserves_pending_nudge() { let (mut chat, _rx, _op_rx) = make_chatwidget_manual(/*model_override*/ None).await; chat.on_rate_limit_snapshot(Some(snapshot_with_nudge( @@ -109,7 +116,7 @@ async fn proactive_usage_prompt_authoritative_empty_snapshot_clears_pending_nudg ))); chat.on_rate_limit_snapshot(Some(snapshot(/*percent*/ 75.0))); - assert!(!chat.maybe_show_pending_current_usage_limit_nudge_prompt()); + assert!(chat.maybe_show_pending_current_usage_limit_nudge_prompt()); } #[tokio::test]