Add Luna Reserve usage fallback to the TUI (#42372)

## Why

When ordinary included usage is exhausted, eligible users need a way to keep working with the backend-provided Luna Reserve quota and return to their previous model once ordinary usage recovers.

## What changed

- Automatically switch eligible TUI tasks to Luna Reserve after an authoritative account usage read, preserving the prior model and reasoning effort per task.
- Restrict model selection while on Reserve to its supported reasoning levels, keep queued turns on the accepted model, and restore the saved model after a fresh read confirms recovery.
- Surface Reserve usage in the composer and `/status`, and expose `normalModelSlug` in app-server rate-limit snapshots so clients can use the associated model's display metadata without changing the request model.
- Poll account limits more frequently near exhaustion and retain compatibility with app servers that only accept the legacy null request parameters.

## Testing

- Added coverage for Reserve entry, queued-turn handling, task reconstruction, recovery authorization, account changes, model selection, usage rendering, and banner dismissal.

GitOrigin-RevId: 46a435e9531051f8cec3f6a46c4b9f7cfa479747
This commit is contained in:
dhruvgupta-oai authored and copyberry committed 2026-09-02 21:50:51 +00:00
1 parent 76f47103fe
commit 5037919777
76 files changed
+1907 -135

No files matched your search

+33 -5
View File
@@ -721,7 +721,16 @@ impl App {
AppEvent::FatalExitRequest(message) => {
return Ok(AppRunControl::Exit(ExitReason::Fatal(message)));
}
AppEvent::CodexOp(op) => {
AppEvent::CodexOp(mut op) => {
if self.active_thread_id == self.chat_widget.thread_id() {
if matches!(&op, AppCommand::UserTurn { .. })
&& self.chat_widget.defer_pending_turn_for_luna_reserve()
{
return Ok(AppRunControl::Continue);
}
self.chat_widget
.apply_reserve_fallback_to_pending_turn(&mut op);
}
let is_user_turn = matches!(&op, AppCommand::UserTurn { .. });
if is_user_turn {
let screen_size = tui.terminal.last_known_screen_size;
@@ -1219,6 +1228,17 @@ impl App {
AppEvent::RefreshRateLimits { origin } => {
self.refresh_rate_limits(app_server, origin);
}
AppEvent::ApplyBackendBannerFallback { thread_id } => {
if self.active_thread_id == Some(thread_id)
&& self.chat_widget.thread_id() == Some(thread_id)
{
self.apply_backend_banner_fallback(app_server).await;
if !self.rate_limit_refresh_state.has_pending_recovery() {
self.chat_widget.finish_rate_limit_recovery();
}
self.refresh_rate_limits(app_server, RateLimitRefreshOrigin::Periodic);
}
}
AppEvent::RefreshTokenActivity { request_id } => {
self.refresh_token_activity(app_server, request_id);
}
@@ -1300,7 +1320,7 @@ impl App {
Vec::new()
};
match origin {
RateLimitRefreshOrigin::Recovery => {
RateLimitRefreshOrigin::Recovery | RateLimitRefreshOrigin::Periodic => {
for snapshot in snapshots {
self.chat_widget.on_rate_limit_snapshot(Some(snapshot));
}
@@ -1361,7 +1381,11 @@ impl App {
// 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::Recovery | RateLimitRefreshOrigin::Periodic => {
// Re-evaluate snapshot age even when the backend cannot refresh it.
// This updates display freshness without authorizing model recovery.
self.chat_widget.refresh_status_surfaces();
},
RateLimitRefreshOrigin::StartupPrefetch {
reset_hint_request_id,
} => {
@@ -1399,10 +1423,10 @@ impl App {
}
}
}
if matches!(
if (accepted || matches!(
origin,
RateLimitRefreshOrigin::Recovery | RateLimitRefreshOrigin::ResetConsume { .. }
) && !self.rate_limit_refresh_state.has_pending_recovery()
)) && !self.rate_limit_refresh_state.has_pending_recovery()
{
self.chat_widget.finish_rate_limit_recovery();
}
@@ -1541,6 +1565,10 @@ impl App {
self.sync_active_thread_reasoning_setting(app_server, effort)
.await;
}
AppEvent::UpdateLunaReserveReasoning { thread_id, effort } => {
self.update_luna_reserve_reasoning(app_server, thread_id, effort)
.await;
}
AppEvent::UpdateModel(model) => {
let model_changed = self.chat_widget.current_model() != model
|| self.chat_widget.current_collaboration_mode().model() != model;