persisting credits if new snapshot does not contain credit info (#7490)

in response to incoming changes to responses headers where the header
may sometimes not contain credits info (no longer forcing a credit
check)
This commit is contained in:
zhao-oai
2025-12-02 16:23:24 -05:00
committed by GitHub
parent f6a7da4ac3
commit 5ebdc9af1b
4 changed files with 149 additions and 2 deletions

View File

@@ -18,6 +18,7 @@ use codex_core::protocol::AgentReasoningDeltaEvent;
use codex_core::protocol::AgentReasoningEvent;
use codex_core::protocol::ApplyPatchApprovalRequestEvent;
use codex_core::protocol::BackgroundEventEvent;
use codex_core::protocol::CreditsSnapshot;
use codex_core::protocol::Event;
use codex_core::protocol::EventMsg;
use codex_core::protocol::ExecApprovalRequestEvent;
@@ -521,6 +522,53 @@ fn test_rate_limit_warnings_monthly() {
);
}
#[test]
fn rate_limit_snapshot_keeps_prior_credits_when_missing_from_headers() {
let (mut chat, _rx, _op_rx) = make_chatwidget_manual();
chat.on_rate_limit_snapshot(Some(RateLimitSnapshot {
primary: None,
secondary: None,
credits: Some(CreditsSnapshot {
has_credits: true,
unlimited: false,
balance: Some("17.5".to_string()),
}),
}));
let initial_balance = chat
.rate_limit_snapshot
.as_ref()
.and_then(|snapshot| snapshot.credits.as_ref())
.and_then(|credits| credits.balance.as_deref());
assert_eq!(initial_balance, Some("17.5"));
chat.on_rate_limit_snapshot(Some(RateLimitSnapshot {
primary: Some(RateLimitWindow {
used_percent: 80.0,
window_minutes: Some(60),
resets_at: Some(123),
}),
secondary: None,
credits: None,
}));
let display = chat
.rate_limit_snapshot
.as_ref()
.expect("rate limits should be cached");
let credits = display
.credits
.as_ref()
.expect("credits should persist when headers omit them");
assert_eq!(credits.balance.as_deref(), Some("17.5"));
assert!(!credits.unlimited);
assert_eq!(
display.primary.as_ref().map(|window| window.used_percent),
Some(80.0)
);
}
#[test]
fn rate_limit_switch_prompt_skips_when_on_lower_cost_model() {
let (mut chat, _, _) = make_chatwidget_manual();