Show actionable rate-limit banners in the TUI (#41742)

## What changed

- Carry optional backend-owned banner and account identity data through
  `account/rateLimits/read`, filtering banners that do not match the authenticated
  account and user.
- Render supported rate-limit notices above the composer with actions for usage,
  credits, resets, owner notifications, and plan management while preserving the
  existing fallback UI for absent or unsupported banners.
- Refresh usage after limit errors, reject stale responses, and keep queued input
  paused until recovery completes. When instructed by the banner, switch to the
  first available fallback model without changing unrelated thread settings.

## Testing

- Add protocol, banner parsing and rendering, CTA, identity filtering, refresh
  ordering, fallback-model, account-change, and queued-input recovery coverage.

GitOrigin-RevId: e03d75a82e425deb87f4b120a71cf2286c0ef6cc
This commit is contained in:
stefanstokic-oai authored and copyberry committed 2026-08-31 00:20:06 +00:00
1 parent 94cbbddafc
commit 79b04f1ab5
69 files changed
+3454 -69

No files matched your search

+50 -6
View File
@@ -3,6 +3,8 @@
//! This module contains the exhaustive `AppEvent` dispatcher and exit-mode handling. Large domain
//! actions are delegated to focused app submodules so the central match remains the routing layer.
use super::rate_limit_refresh::RateLimitReadStatus;
use super::rate_limit_refresh::RateLimitRefreshOutcome;
use super::resize_reflow::trailing_run_start;
use super::session_lifecycle::ThreadAttachPresentation;
use super::*;
@@ -1234,31 +1236,58 @@ impl App {
self.clear_thread_goal(app_server, thread_id).await;
}
AppEvent::SendAddCreditsNudgeEmail { credit_type } => {
if self
if let Some(request_id) = self
.chat_widget
.start_add_credits_nudge_email_request(credit_type)
{
self.send_add_credits_nudge_email(app_server, credit_type);
self.send_add_credits_nudge_email(app_server, request_id, credit_type);
}
}
AppEvent::AddCreditsNudgeEmailFinished { result } => {
AppEvent::AddCreditsNudgeEmailFinished { request_id, result } => {
self.chat_widget
.finish_add_credits_nudge_email_request(result);
.finish_add_credits_nudge_email_request(request_id, result);
}
AppEvent::RateLimitsLoaded {
request_id,
origin,
hard_stop_generation,
result,
} => match result {
} => {
let accepted = match self.rate_limit_refresh_state.finish(
request_id,
hard_stop_generation,
self.rate_limit_hard_stop_generation,
if result.is_ok() {
RateLimitReadStatus::Succeeded
} else {
RateLimitReadStatus::Failed
},
) {
RateLimitRefreshOutcome::Apply => true,
RateLimitRefreshOutcome::Ignore => false,
RateLimitRefreshOutcome::RefreshRecovery => {
// Start in this account's event turn; a queued refresh could cross an account change.
self.refresh_rate_limits(app_server, RateLimitRefreshOrigin::Recovery);
false
}
};
match result {
Ok(response) => {
let rate_limit_reset_credits = response.rate_limit_reset_credits.clone();
let snapshots = if hard_stop_generation == self.rate_limit_hard_stop_generation
let snapshots = if accepted
{
self.chat_widget.update_backend_banner(&response);
self.apply_backend_banner_fallback(app_server).await;
app_server_rate_limit_snapshots(response)
} else {
Vec::new()
};
match origin {
RateLimitRefreshOrigin::Recovery => {
for snapshot in snapshots {
self.chat_widget.on_rate_limit_snapshot(Some(snapshot));
}
}
RateLimitRefreshOrigin::StartupPrefetch {
reset_hint_request_id,
} => {
@@ -1312,8 +1341,10 @@ impl App {
}
}
Err(err) => {
// A failed read is not authoritative recovery. Keep the last valid banner.
tracing::warn!("account/rateLimits/read failed during TUI refresh: {err}");
match origin {
RateLimitRefreshOrigin::Recovery => {},
RateLimitRefreshOrigin::StartupPrefetch {
reset_hint_request_id,
} => {
@@ -1350,6 +1381,14 @@ impl App {
}
}
}
}
if matches!(
origin,
RateLimitRefreshOrigin::Recovery | RateLimitRefreshOrigin::ResetConsume { .. }
) && !self.rate_limit_refresh_state.has_pending_recovery()
{
self.chat_widget.finish_rate_limit_recovery();
}
},
AppEvent::OpenTokenActivity => {
self.chat_widget
@@ -1412,6 +1451,11 @@ impl App {
credit_id,
result,
) {
// Reads started before redemption must not restore the pre-reset banner.
self.rate_limit_hard_stop_generation =
self.rate_limit_hard_stop_generation.wrapping_add(1);
self.rate_limit_refresh_state.invalidate_recovery();
self.chat_widget.clear_backend_banner();
self.refresh_rate_limits(
app_server,
RateLimitRefreshOrigin::ResetConsume { request_id },