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

@@ -1,27 +1,71 @@
//! Apply an accepted account usage response to the currently selected task and model.
//! Apply backend-authorized fallback and task-local Reserve settings without saving defaults.
use super::App;
use crate::app_server_session::AppServerSession;
use crate::chatwidget::AutomaticModelSwitchReason;
use crate::model_catalog::LUNA_RESERVE_MODEL;
use crate::model_catalog::model_display_name;
use crate::service_tier_resolution;
use codex_app_server_protocol::ThreadSettingsUpdateParams;
use codex_protocol::ThreadId;
use codex_protocol::config_types::ModeKind;
use codex_protocol::openai_models::ReasoningEffort;
impl App {
pub(super) async fn update_luna_reserve_reasoning(
&mut self,
app_server: &mut AppServerSession,
thread_id: ThreadId,
effort: Option<ReasoningEffort>,
) {
// A picker left open during recovery or a task switch must not affect the new model.
if self.active_thread_id != Some(thread_id)
|| self.chat_widget.thread_id() != Some(thread_id)
|| self.chat_widget.current_model() != LUNA_RESERVE_MODEL
{
return;
}
let mut mode = self.chat_widget.effective_collaboration_mode();
mode.settings.reasoning_effort = effort.clone();
let params = ThreadSettingsUpdateParams {
thread_id: thread_id.to_string(),
effort: effort.clone(),
collaboration_mode: Some(mode.clone()),
..ThreadSettingsUpdateParams::default()
};
if self.send_thread_settings_update(app_server, params).await {
self.chat_widget.set_reasoning_effort(effort.clone());
if mode.mode == ModeKind::Plan {
self.chat_widget.set_plan_mode_reasoning_effort(effort);
}
}
}
pub(super) async fn apply_backend_banner_fallback(
&mut self,
app_server: &mut AppServerSession,
) {
// Reattached tasks must wait for the read that supersedes the latest hard stop.
if self.rate_limit_refresh_state.has_pending_recovery() {
return;
}
let Some(thread_id) = self.active_thread_id else {
return;
};
if Some(thread_id) != self.chat_widget.thread_id() {
return;
}
let Some(target) = self.chat_widget.backend_banner_fallback() else {
let Some(switch) = self.chat_widget.backend_banner_fallback() else {
return;
};
let effort = self
.chat_widget
.current_reasoning_effort()
let target = switch.model;
let entering_reserve = target.model == crate::model_catalog::LUNA_RESERVE_MODEL;
if entering_reserve && !self.chat_widget.prepare_luna_reserve_return() {
self.chat_widget.show_unavailable_reserve_recovery();
return;
}
let effort = switch
.effort
.filter(|effort| {
target
.supported_reasoning_efforts
@@ -53,13 +97,24 @@ impl App {
self.chat_widget.finish_backend_banner_fallback(mode);
self.sync_active_thread_service_tier_to_cached_session()
.await;
let mut message = format!("Automatically switched to {}", target.model);
let (prefix, suffix) = match switch.reason {
AutomaticModelSwitchReason::UsageLimit => {
("Automatically switched to", " due to usage limits.")
}
AutomaticModelSwitchReason::UsageRecovered => (
"Automatically switched back to",
" because ordinary usage is available again.",
),
};
let mut message = format!("{prefix} {}", model_display_name(&target.model));
if let Some(label) = Self::reasoning_label_for(&target.model, Some(&effort)) {
message.push(' ');
message.push_str(&label);
}
message.push_str(" due to usage limits.");
message.push_str(suffix);
self.chat_widget.add_info_message(message, /*hint*/ None);
} else if entering_reserve {
self.chat_widget.show_unavailable_reserve_recovery();
}
}
}