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

@@ -11,6 +11,8 @@ use super::list_selection_view::ListSelectionView;
use crate::render::Insets;
use crate::render::RectExt;
use crate::render::renderable::Renderable;
use crate::terminal_palette::best_color;
use crate::terminal_palette::default_fg;
use crate::wrapping::word_wrap_lines;
use crossterm::event::KeyCode;
use crossterm::event::KeyEvent;
@@ -18,6 +20,7 @@ use crossterm::event::KeyEventKind;
use crossterm::event::KeyModifiers;
use ratatui::buffer::Buffer;
use ratatui::layout::Rect;
use ratatui::style::Color;
use ratatui::style::Stylize;
use ratatui::text::Line;
use ratatui::widgets::Paragraph;
@@ -122,7 +125,9 @@ impl BannerContent {
impl Renderable for BannerContent {
fn render(&self, area: Rect, buf: &mut Buffer) {
let lines = self.wrapped_lines(area.width);
Widget::render(Paragraph::new(lines), area, buf);
// Explicit foreground avoids a terminal's separate bold color washing out the title.
let foreground = default_fg().map(best_color).unwrap_or(Color::Reset);
Widget::render(Paragraph::new(lines).fg(foreground), area, buf);
}
fn desired_height(&self, width: u16) -> u16 {
@@ -140,7 +145,7 @@ impl From<ActionableBanner> for SelectionViewParams {
banner
.description
.lines()
.map(|line| Line::from(line.to_owned().dim())),
.map(|line| Line::from(line.to_owned())),
)
.collect();
Self {
@@ -164,6 +169,7 @@ impl BottomPane {
let dismissal = banner.dismissal;
let has_actions = !banner.actions.is_empty();
let mut params: SelectionViewParams = banner.into();
params.header_gap = 0;
params.allow_cancel = dismissal == BannerDismissal::Dismissible;
for item in &mut params.items {
item.dismiss_on_select = false;
@@ -6,6 +6,7 @@
//! - Routing keys to the active popup (slash commands, file search, skill/apps/task mentions).
//! - Promoting typed slash commands into atomic elements when the command name is completed.
//! - Handling submit vs newline on Enter.
//! - Showing a single yellow prompt arrow while the active model is Luna Reserve.
//! - Turning raw key streams into explicit paste operations on platforms where terminals
//! don't provide reliable bracketed paste (notably Windows).
//!
@@ -518,6 +519,7 @@ pub(crate) struct ChatComposer {
effort_ignition: Option<EffortIgnition>,
effort_status_line_transition: Option<EffortStatusLineTransition>,
effort_observed: bool,
luna_reserve_active: bool,
attachments: AttachmentState,
placeholder_text: String,
blocks_direct_input: bool,
@@ -697,6 +699,7 @@ impl ChatComposer {
effort_ignition: None,
effort_status_line_transition: None,
effort_observed: false,
luna_reserve_active: false,
attachments: AttachmentState::default(),
placeholder_text,
blocks_direct_input: false,
@@ -4626,6 +4629,14 @@ impl Renderable for ChatComposer {
}
impl ChatComposer {
pub(crate) fn set_luna_reserve_active(&mut self, active: bool) -> bool {
if self.luna_reserve_active == active {
return false;
}
self.luna_reserve_active = active;
true
}
pub(crate) fn desired_height_with_textarea_right_reserve(
&self,
width: u16,
@@ -4949,6 +4960,11 @@ impl ChatComposer {
let prompt = if self.draft.input_enabled {
if self.draft.is_bash_mode {
Span::from("!").light_red().bold()
} else if self.luna_reserve_active {
// Reserve keeps one arrow at every reasoning effort; only its foreground changes.
"›"
.fg(crate::terminal_palette::best_color((246, 197, 67)))
.bold()
} else if let Some(tier) = self.effort_tier {
let charge = self
.effort_ignition
@@ -183,6 +183,8 @@ pub(crate) struct SelectionViewParams {
/// Rendered left-column width to use for auto-sized rows.
pub name_column_width: Option<usize>,
pub header: Box<dyn Renderable>,
/// Blank rows after the header; defaults to one. Inline banners use zero.
pub header_gap: u16,
pub initial_selected_idx: Option<usize>,
/// Rich content rendered beside (wide terminals) or below (narrow terminals)
@@ -234,6 +236,7 @@ impl Default for SelectionViewParams {
description_layout: SelectionDescriptionLayout::Columns,
name_column_width: None,
header: Box::new(()),
header_gap: 1,
initial_selected_idx: None,
side_content: Box::new(()),
side_content_width: SideContentWidth::default(),
@@ -275,6 +278,7 @@ pub(crate) struct ListSelectionView {
last_selected_actual_idx: Option<usize>,
rendered_item_count: std::cell::Cell<usize>,
header: Box<dyn Renderable>,
header_gap: u16,
initial_selected_idx: Option<usize>,
side_content: Box<dyn Renderable>,
side_content_width: SideContentWidth,
@@ -411,6 +415,7 @@ impl ListSelectionView {
last_selected_actual_idx: None,
rendered_item_count: std::cell::Cell::new(0),
header,
header_gap: params.header_gap,
initial_selected_idx: params.initial_selected_idx,
side_content: params.side_content,
side_content_width: params.side_content_width,
@@ -1170,7 +1175,7 @@ impl Renderable for ListSelectionView {
let tab_height = tab_bar_height(&self.tabs, self.active_tab_idx.unwrap_or(0), inner_width);
let mut height = header.desired_height(inner_width);
height = height.saturating_add(tab_height + u16::from(tab_height > 0));
height = height.saturating_add(rows_height + 3);
height = height.saturating_add(rows_height + 2 + self.header_gap);
if self.is_searchable {
height = height.saturating_add(1);
}
@@ -1265,7 +1270,7 @@ impl Renderable for ListSelectionView {
stacked_side_area,
] = Layout::vertical([
Constraint::Max(header_height),
Constraint::Max(1),
Constraint::Max(self.header_gap),
Constraint::Length(tab_height),
Constraint::Length(u16::from(tab_height > 0)),
Constraint::Length(if self.is_searchable { 1 } else { 0 }),
+6
View File
@@ -1967,6 +1967,12 @@ impl BottomPane {
}
}
pub(crate) fn set_luna_reserve_active(&mut self, active: bool) {
if self.composer.set_luna_reserve_active(active) {
self.request_redraw();
}
}
pub(crate) fn set_status_line_hyperlink(&mut self, url: Option<String>) {
if self.composer.set_status_line_hyperlink(url) {
self.request_redraw();
@@ -6,7 +6,6 @@ expression: "render_snapshot(&pane, area)"
Usage limit reached
Your included usage is depleted.
Choose an action to continue.
› 1. Open usage settings
2. View usage
3. Extra action
@@ -6,7 +6,6 @@ expression: "render_snapshot(&pane, area)"
Usage limit reached
Your included usage is depleted.
Choose an action to continue.
› 1. Open usage settings
2. Notify owner
3. Extra action
@@ -11,7 +11,6 @@ expression: "render_snapshot(&pane, area)"
A long description wraps to fit the
available terminal width.
…
› 1. Open usage settings
2. Notify owner