Files
codex/codex-rs/core/tests/suite/quota_exceeded.rs
T
Alex Chang cc7a62e19c Report API spend limit errors by scope (#62329)
## Why

Generic quota errors hide whether the project or organization spend limit
blocked a request. Reporting the scope tells users which limit to check.

## What changed

Recognize API key, project, and organization spend limit errors across
HTTP, SSE, and WebSocket responses. Show a message that names the scope and
treat the error as non-retryable, even when the provider sends
`Retry-After`. Apply the existing usage-limit handling to goals and
compaction.

Expose distinct `CodexErrorInfo` values in the app-server protocol and
generated bindings so clients can identify the limit in live errors and
failed turn history.

## Testing

Extend the transport tests to cover all three scopes and verify that they
stop retries. Extend the app-server test to check that the error survives
`thread/read` and that a subsequent turn can succeed in the same thread.

GitOrigin-RevId: 03341f7a78daa7540e7baa519f1a29ec07e34509
2026-11-18 18:11:54 +00:00

87 lines
3.2 KiB
Rust

use anyhow::Result;
use codex_core::TurnInputRequest;
use codex_protocol::protocol::CodexErrorInfo;
use codex_protocol::protocol::EventMsg;
use codex_protocol::user_input::UserInput;
use core_test_support::responses::ev_response_created;
use core_test_support::responses::mount_sse_once;
use core_test_support::responses::sse;
use core_test_support::responses::start_mock_server;
use core_test_support::skip_if_no_network;
use core_test_support::test_codex::test_codex;
use core_test_support::wait_for_event;
use pretty_assertions::assert_eq;
use serde_json::json;
#[test_case::test_case("insufficient_quota", CodexErrorInfo::UsageLimitExceeded, "Quota exceeded. Check your plan and billing details."; "quota")]
#[test_case::test_case("credit_balance_exhausted", CodexErrorInfo::UsageLimitExceeded, "Quota exceeded. Check your plan and billing details."; "credit_balance")]
#[test_case::test_case("api_key_spend_limit_exceeded", CodexErrorInfo::ApiKeySpendLimitExceeded, "Your API key has reached its spend limit."; "api_key_spend_limit")]
#[test_case::test_case("organization_spend_limit_exceeded", CodexErrorInfo::OrganizationSpendLimitExceeded, "Your API organization has reached its spend limit."; "organization_spend_limit")]
#[test_case::test_case("project_spend_limit_exceeded", CodexErrorInfo::ProjectSpendLimitExceeded, "Your API project has reached its spend limit."; "project_spend_limit")]
#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
async fn quota_exceeded_emits_single_error_event(
code: &str,
expected: CodexErrorInfo,
expected_message: &str,
) -> Result<()> {
skip_if_no_network!(Ok(()));
let server = start_mock_server().await;
let mut builder = test_codex();
mount_sse_once(
&server,
sse(vec![
ev_response_created("resp-1"),
json!({
"type": "response.failed",
"response": {
"id": "resp-1",
"error": {
"code": code,
"message": "You exceeded your current quota, please check your plan and billing details."
}
}
}),
]),
)
.await;
let test = builder.build_with_auto_env(&server).await?;
test.codex
.start_or_steer_turn(TurnInputRequest::user_input(vec![UserInput::Text {
text: "quota?".into(),
text_elements: Vec::new(),
}]))
.await?;
let mut error_events = 0;
loop {
let event = wait_for_event(&test.codex, |_| true).await;
match event {
EventMsg::Error(err) => {
error_events += 1;
assert_eq!(err.message, expected_message);
assert_eq!(err.codex_error_info, Some(expected.clone()));
}
EventMsg::TurnComplete(_) => break,
_ => {}
}
}
assert_eq!(error_events, 1, "expected exactly one Codex:Error event");
let requests = server.received_requests().await.expect("recorded requests");
assert_eq!(
requests
.iter()
.filter(|request| request.url.path().ends_with("/responses"))
.count(),
1
);
Ok(())
}