mirror of
https://github.com/openai/codex.git
synced 2026-11-27 23:31:09 +00:00
Bound model catalog decode errors and classify request timeouts (#45928)
## Why Model catalog decode errors can include the full response body and payload values from deserialization errors. Catalog request deadlines also report a generic timeout instead of a request timeout. ## What changed - Report only the JSON error category, line, column, and response byte count when model catalog decoding fails. - Return `CodexErr::RequestTimeout` when the model catalog refresh deadline expires. ## Testing Add regression tests for invalid response shapes, large payload values, malformed and truncated JSON, and a delayed catalog response that must return `RequestTimeout`. GitOrigin-RevId: 6fe5111a3e3b113d79942b3c213f8e94d773eb5a
This commit is contained in:
1 parent
8f38d5a877
commit
977193486d
4 files changed
+101
-3
No files matched your search
@@ -59,8 +59,11 @@ impl<T: HttpTransport> ModelsClient<T> {
|
|||||||
let ModelsResponse { models } =
|
let ModelsResponse { models } =
|
||||||
serde_json::from_slice::<ModelsResponse>(&body).map_err(|e| {
|
serde_json::from_slice::<ModelsResponse>(&body).map_err(|e| {
|
||||||
ApiError::Stream(format!(
|
ApiError::Stream(format!(
|
||||||
"failed to decode models response: {e}; body: {}",
|
"failed to decode models response: {:?} at line {} column {} (body: {} bytes)",
|
||||||
String::from_utf8_lossy(&body)
|
e.classify(),
|
||||||
|
e.line(),
|
||||||
|
e.column(),
|
||||||
|
body.len()
|
||||||
))
|
))
|
||||||
})?;
|
})?;
|
||||||
|
|
||||||
|
|||||||
@@ -15,6 +15,8 @@ use codex_protocol::openai_models::TruncationPolicyConfig;
|
|||||||
use codex_protocol::openai_models::default_input_modalities;
|
use codex_protocol::openai_models::default_input_modalities;
|
||||||
use http::HeaderMap;
|
use http::HeaderMap;
|
||||||
use http::Method;
|
use http::Method;
|
||||||
|
use pretty_assertions::assert_eq;
|
||||||
|
use serde_json::json;
|
||||||
use std::sync::Arc;
|
use std::sync::Arc;
|
||||||
use wiremock::Mock;
|
use wiremock::Mock;
|
||||||
use wiremock::MockServer;
|
use wiremock::MockServer;
|
||||||
@@ -46,6 +48,55 @@ fn provider(base_url: &str) -> Provider {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[tokio::test]
|
||||||
|
async fn invalid_models_response_reports_bounded_decode_metadata() {
|
||||||
|
let server = MockServer::start().await;
|
||||||
|
for (body, category, column_offset) in [
|
||||||
|
(
|
||||||
|
json!({ "data": [{ "id": "private-model" }] }).to_string(),
|
||||||
|
"Data",
|
||||||
|
0,
|
||||||
|
),
|
||||||
|
(
|
||||||
|
json!({ "models": "private-value".repeat(4096) }).to_string(),
|
||||||
|
"Data",
|
||||||
|
1,
|
||||||
|
),
|
||||||
|
(r#"{"models":]}"#.to_string(), "Syntax", 1),
|
||||||
|
(r#"{"models":["#.to_string(), "Eof", 0),
|
||||||
|
] {
|
||||||
|
let response = Mock::given(method("GET"))
|
||||||
|
.and(path("/models"))
|
||||||
|
.respond_with(ResponseTemplate::new(200).set_body_string(&body))
|
||||||
|
.expect(1)
|
||||||
|
.mount_as_scoped(&server)
|
||||||
|
.await;
|
||||||
|
let transport = ReqwestTransport::from_http_client(
|
||||||
|
HttpClientBuilder::new()
|
||||||
|
.build_direct()
|
||||||
|
.expect("test HTTP client should build"),
|
||||||
|
);
|
||||||
|
let provider = provider(&server.uri());
|
||||||
|
let request_url = ModelsClient::<ReqwestTransport>::request_url(&provider, "0.1.0");
|
||||||
|
let client = ModelsClient::new(transport, provider, Arc::new(DummyAuth));
|
||||||
|
|
||||||
|
let error = client
|
||||||
|
.list_models(request_url, HeaderMap::new())
|
||||||
|
.await
|
||||||
|
.expect_err("invalid catalog should fail to decode");
|
||||||
|
|
||||||
|
assert_eq!(
|
||||||
|
error.to_string(),
|
||||||
|
format!(
|
||||||
|
"stream error: failed to decode models response: {category} at line 1 column {} (body: {} bytes)",
|
||||||
|
body.len() - column_offset,
|
||||||
|
body.len()
|
||||||
|
)
|
||||||
|
);
|
||||||
|
drop(response);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
#[tokio::test]
|
#[tokio::test]
|
||||||
async fn models_client_hits_models_endpoint() {
|
async fn models_client_hits_models_endpoint() {
|
||||||
let server = MockServer::start().await;
|
let server = MockServer::start().await;
|
||||||
|
|||||||
@@ -122,7 +122,7 @@ impl OpenAiModelsEndpoint {
|
|||||||
.map_err(map_api_error)
|
.map_err(map_api_error)
|
||||||
})
|
})
|
||||||
.await
|
.await
|
||||||
.map_err(|_| CodexErr::Timeout)??;
|
.map_err(|_| CodexErr::RequestTimeout)??;
|
||||||
Ok(ModelsEndpointResponse {
|
Ok(ModelsEndpointResponse {
|
||||||
models,
|
models,
|
||||||
etag,
|
etag,
|
||||||
@@ -301,6 +301,10 @@ impl RequestTelemetry for ModelsRequestTelemetry {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[cfg(test)]
|
||||||
|
#[path = "models_endpoint_timeout_tests.rs"]
|
||||||
|
mod timeout_tests;
|
||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
mod tests {
|
mod tests {
|
||||||
use std::num::NonZeroU64;
|
use std::num::NonZeroU64;
|
||||||
|
|||||||
@@ -0,0 +1,40 @@
|
|||||||
|
use super::*;
|
||||||
|
use codex_http_client::OutboundProxyPolicy;
|
||||||
|
use codex_protocol::error::CodexErrorDetails;
|
||||||
|
use wiremock::Mock;
|
||||||
|
use wiremock::MockServer;
|
||||||
|
use wiremock::ResponseTemplate;
|
||||||
|
use wiremock::matchers::method;
|
||||||
|
use wiremock::matchers::path;
|
||||||
|
|
||||||
|
#[tokio::test]
|
||||||
|
async fn catalog_deadline_returns_request_timeout() {
|
||||||
|
let server = MockServer::start().await;
|
||||||
|
Mock::given(method("GET"))
|
||||||
|
.and(path("/models"))
|
||||||
|
.respond_with(
|
||||||
|
ResponseTemplate::new(200)
|
||||||
|
.set_body_json(serde_json::json!({ "models": [] }))
|
||||||
|
.set_delay(MODELS_REFRESH_TIMEOUT * 2),
|
||||||
|
)
|
||||||
|
.expect(1)
|
||||||
|
.mount(&server)
|
||||||
|
.await;
|
||||||
|
let endpoint = OpenAiModelsEndpoint::new(
|
||||||
|
ModelProviderInfo::create_openai_provider(Some(server.uri())),
|
||||||
|
/*auth_manager*/ None,
|
||||||
|
);
|
||||||
|
|
||||||
|
let error = endpoint
|
||||||
|
.list_models(
|
||||||
|
"0.0.0",
|
||||||
|
HttpClientFactory::new(OutboundProxyPolicy::ReqwestDefault),
|
||||||
|
)
|
||||||
|
.await
|
||||||
|
.expect_err("delayed catalog request should time out");
|
||||||
|
|
||||||
|
assert!(
|
||||||
|
matches!(error.details(), CodexErrorDetails::RequestTimeout),
|
||||||
|
"{error}"
|
||||||
|
);
|
||||||
|
}
|
||||||
Reference in new issue
Block a user