[codex] Latch proactive usage warnings by threshold

This commit is contained in:
James Chu
2026-05-04 23:55:22 -04:00
parent 40a6cc1bd4
commit 21f7427483
4 changed files with 44 additions and 20 deletions
+7 -3
View File
@@ -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,
),
}),
),
)
+7 -8
View File
@@ -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!(
@@ -42,19 +42,33 @@ impl UsageNudgePrefetchThreshold {
pub(super) struct CurrentUsageLimitNudgePromptState {
active: bool,
pending: Option<UsageLimitNudge>,
has_shown: bool,
shown_thresholds: Vec<u8>,
last_prefetches: Vec<(UsageNudgePrefetchKey, UsageNudgePrefetchThreshold)>,
}
impl CurrentUsageLimitNudgePromptState {
pub(super) fn update(&mut self, nudge: Option<UsageLimitNudge>) {
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<UsageLimitNudge> {
let nudge = self.pending.take()?;
self.has_shown = true;
if !self.shown_thresholds.contains(&nudge.threshold) {
self.shown_thresholds.push(nudge.threshold);
}
Some(nudge)
}
@@ -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]