Files
codex/codex-rs/memories/write/src/guard_tests.rs
T
dhruvgupta-oai 5037919777 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
2026-09-02 21:50:51 +00:00

82 lines
2.2 KiB
Rust

use super::*;
use codex_protocol::protocol::RateLimitReachedType;
fn snapshot(
primary_used_percent: Option<f64>,
secondary_used_percent: Option<f64>,
) -> RateLimitSnapshot {
RateLimitSnapshot {
limit_id: Some(crate::guard_limits::CODEX_LIMIT_ID.to_string()),
limit_name: None,
normal_model_slug: None,
primary: primary_used_percent.map(window),
secondary: secondary_used_percent.map(window),
credits: None,
individual_limit: None,
spend_control_reached: None,
plan_type: None,
rate_limit_reached_type: None,
}
}
fn window(used_percent: f64) -> RateLimitWindow {
RateLimitWindow {
used_percent,
window_minutes: None,
resets_at: None,
}
}
#[test]
fn startup_check_uses_configured_remaining_threshold() {
let snapshot = snapshot(
/*primary_used_percent*/ Some(89.9),
/*secondary_used_percent*/ Some(50.0),
);
assert!(snapshot_allows_startup(
&snapshot, /*min_remaining_percent*/ 10
));
assert!(!snapshot_allows_startup(
&snapshot, /*min_remaining_percent*/ 11
));
}
#[test]
fn startup_check_skips_when_primary_or_secondary_is_too_low() {
assert!(!snapshot_allows_startup(
&snapshot(
/*primary_used_percent*/ Some(75.1),
/*secondary_used_percent*/ Some(10.0),
),
/*min_remaining_percent*/ 25,
));
assert!(!snapshot_allows_startup(
&snapshot(
/*primary_used_percent*/ Some(10.0),
/*secondary_used_percent*/ Some(75.1),
),
/*min_remaining_percent*/ 25,
));
assert!(snapshot_allows_startup(
&snapshot(
/*primary_used_percent*/ Some(74.9),
/*secondary_used_percent*/ Some(74.9),
),
/*min_remaining_percent*/ 25,
));
}
#[test]
fn startup_check_skips_when_limit_is_reached() {
let mut snapshot = snapshot(
/*primary_used_percent*/ Some(10.0),
/*secondary_used_percent*/ Some(10.0),
);
snapshot.rate_limit_reached_type = Some(RateLimitReachedType::RateLimitReached);
assert!(!snapshot_allows_startup(
&snapshot, /*min_remaining_percent*/ 25,
));
}