mirror of
https://github.com/openai/codex.git
synced 2026-12-03 00:41:03 +00:00
## Why A model catalog lookup can use a cached snapshot or bundled fallback without sending a request. A lookup that reaches the endpoint can send multiple requests through retries. Measuring both lets readers of the metrics distinguish cache behavior from endpoint traffic. ## What changed - Record one `codex.remote_models.lookup.outcome` observation per completed catalog lookup with its purpose, result, and the source of the snapshot actually selected. Record observations without taking the exporter lock on the lookup path. - Count dispatched requests in `codex.remote_models.http_get_attempt`, including retries. Tag them by purpose and whether they match the configured endpoint when that context is available. Keep endpoint URLs out of metric labels. - Mark background refreshes as maintenance and apply metrics enablement changes to cached global handles when the process reloads telemetry settings. ## Testing Add coverage for cache and fallback attribution, retry counts, auth failures before dispatch, and metrics collection across exporter disablement and replacement. GitOrigin-RevId: b970207a69432030b4511c7e84acb6696b6f824b
301 lines
10 KiB
Rust
301 lines
10 KiB
Rust
use codex_api::AuthProvider;
|
|
use codex_api::ModelsClient;
|
|
use codex_api::Provider;
|
|
use codex_api::RetryConfig;
|
|
use codex_client::ReqwestTransport;
|
|
use codex_http_client::HttpClientBuilder;
|
|
use codex_protocol::config_types::ReasoningSummary;
|
|
use codex_protocol::openai_models::ConfigShellToolType;
|
|
use codex_protocol::openai_models::ModelInfo;
|
|
use codex_protocol::openai_models::ModelVisibility;
|
|
use codex_protocol::openai_models::ModelsResponse;
|
|
use codex_protocol::openai_models::ReasoningEffort;
|
|
use codex_protocol::openai_models::ReasoningEffortPreset;
|
|
use codex_protocol::openai_models::TruncationPolicyConfig;
|
|
use codex_protocol::openai_models::default_input_modalities;
|
|
use http::HeaderMap;
|
|
use http::Method;
|
|
use pretty_assertions::assert_eq;
|
|
use serde_json::json;
|
|
use std::sync::Arc;
|
|
use std::sync::atomic::AtomicUsize;
|
|
use std::sync::atomic::Ordering;
|
|
use wiremock::Mock;
|
|
use wiremock::MockServer;
|
|
use wiremock::ResponseTemplate;
|
|
use wiremock::matchers::method;
|
|
use wiremock::matchers::path;
|
|
|
|
#[derive(Clone, Default)]
|
|
struct DummyAuth;
|
|
|
|
impl AuthProvider for DummyAuth {
|
|
fn add_auth_headers(&self, _headers: &mut HeaderMap) -> Result<(), codex_api::AuthError> {
|
|
Ok(())
|
|
}
|
|
}
|
|
|
|
fn provider(base_url: &str) -> Provider {
|
|
Provider {
|
|
name: "test".to_string(),
|
|
base_url: base_url.to_string(),
|
|
query_params: None,
|
|
headers: HeaderMap::new(),
|
|
retry: RetryConfig {
|
|
max_attempts: 1,
|
|
base_delay: std::time::Duration::from_millis(1),
|
|
retry_429: false,
|
|
retry_5xx: true,
|
|
retry_transport: true,
|
|
},
|
|
stream_idle_timeout: std::time::Duration::from_secs(1),
|
|
}
|
|
}
|
|
|
|
#[tokio::test]
|
|
async fn invalid_models_response_reports_bounded_decode_metadata() {
|
|
let server = MockServer::start().await;
|
|
for (body, category, column_offset) in [
|
|
(
|
|
json!({ "object": "list", "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(),
|
|
/*response_body_limit_bytes*/ None,
|
|
)
|
|
.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]
|
|
async fn models_client_hits_models_endpoint() {
|
|
let server = MockServer::start().await;
|
|
let base_url = format!("{}/api/codex", server.uri());
|
|
|
|
let response = ModelsResponse {
|
|
models: vec![ModelInfo {
|
|
slug: "gpt-test".to_string(),
|
|
display_name: "gpt-test".to_string(),
|
|
description: Some("desc".to_string()),
|
|
default_reasoning_level: Some(ReasoningEffort::Medium),
|
|
supported_reasoning_levels: vec![
|
|
ReasoningEffortPreset {
|
|
effort: ReasoningEffort::Low,
|
|
description: ReasoningEffort::Low.to_string(),
|
|
},
|
|
ReasoningEffortPreset {
|
|
effort: ReasoningEffort::Medium,
|
|
description: ReasoningEffort::Medium.to_string(),
|
|
},
|
|
ReasoningEffortPreset {
|
|
effort: ReasoningEffort::High,
|
|
description: ReasoningEffort::High.to_string(),
|
|
},
|
|
],
|
|
shell_type: ConfigShellToolType::UnifiedExec,
|
|
visibility: ModelVisibility::List,
|
|
supported_in_api: true,
|
|
priority: 1,
|
|
additional_speed_tiers: Vec::new(),
|
|
service_tiers: Vec::new(),
|
|
default_service_tier: None,
|
|
available_access_programs: None,
|
|
upgrade: None,
|
|
model_messages: None,
|
|
include_skills_usage_instructions: false,
|
|
include_plugin_usage_instructions: false,
|
|
include_apps_usage_instructions: false,
|
|
supports_reasoning_summary_parameter: true,
|
|
default_reasoning_summary: ReasoningSummary::Auto,
|
|
support_verbosity: false,
|
|
default_verbosity: None,
|
|
availability_nux: None,
|
|
apply_patch_tool_type: None,
|
|
web_search_tool_type: Default::default(),
|
|
truncation_policy: TruncationPolicyConfig::bytes(/*limit*/ 10_000),
|
|
supports_image_detail_original: false,
|
|
context_window: Some(272_000),
|
|
max_context_window: None,
|
|
auto_compact_token_limit: None,
|
|
comp_hash: None,
|
|
effective_context_window_percent: 95,
|
|
experimental_supported_tools: Vec::new(),
|
|
input_modalities: default_input_modalities(),
|
|
supports_search_tool: false,
|
|
supports_experimental_context: false,
|
|
use_responses_lite: false,
|
|
supports_reasoning_effort_updates: true,
|
|
guardian: None,
|
|
node_repl_auto_review_required: true,
|
|
node_repl_disabled: true,
|
|
auto_review_model_override: None,
|
|
model_specialty: None,
|
|
tool_mode: None,
|
|
multi_agent_version: None,
|
|
multi_agent_reasoning_effort: None,
|
|
multi_agent_tools_in_code_mode: None,
|
|
}],
|
|
};
|
|
|
|
Mock::given(method("GET"))
|
|
.and(path("/api/codex/models"))
|
|
.respond_with(
|
|
ResponseTemplate::new(200)
|
|
.insert_header("content-type", "application/json")
|
|
.set_body_json(&response),
|
|
)
|
|
.mount(&server)
|
|
.await;
|
|
|
|
let transport = ReqwestTransport::from_http_client(
|
|
HttpClientBuilder::new()
|
|
.build_direct()
|
|
.expect("test HTTP client should build"),
|
|
);
|
|
let provider = provider(&base_url);
|
|
let request_url = ModelsClient::<ReqwestTransport>::request_url(&provider, "0.1.0");
|
|
let client = ModelsClient::new(transport, provider, Arc::new(DummyAuth));
|
|
|
|
let (models, _) = client
|
|
.list_models(
|
|
request_url,
|
|
HeaderMap::new(),
|
|
/*response_body_limit_bytes*/ None,
|
|
)
|
|
.await
|
|
.expect("models request should succeed");
|
|
|
|
assert_eq!(models.len(), 1);
|
|
assert_eq!(models[0].slug, "gpt-test");
|
|
assert!(models[0].node_repl_auto_review_required);
|
|
assert!(models[0].node_repl_disabled);
|
|
|
|
let received = server
|
|
.received_requests()
|
|
.await
|
|
.expect("should capture requests");
|
|
assert_eq!(received.len(), 1);
|
|
assert_eq!(received[0].method, Method::GET.as_str());
|
|
assert_eq!(received[0].url.path(), "/api/codex/models");
|
|
}
|
|
|
|
#[derive(Default)]
|
|
struct Dispatches(AtomicUsize);
|
|
|
|
impl codex_api::RequestTelemetry for Dispatches {
|
|
fn on_dispatch(&self) {
|
|
self.0.fetch_add(/*val*/ 1, Ordering::Relaxed);
|
|
}
|
|
|
|
fn on_request(
|
|
&self,
|
|
_attempt: u64,
|
|
_status: Option<http::StatusCode>,
|
|
_error: Option<&codex_api::TransportError>,
|
|
_duration: std::time::Duration,
|
|
) {
|
|
}
|
|
}
|
|
|
|
/// Each retry represents another dispatched GET for a single catalog lookup.
|
|
#[tokio::test]
|
|
async fn dispatch_telemetry_counts_retried_models_requests() {
|
|
let server = MockServer::start().await;
|
|
Mock::given(method("GET"))
|
|
.and(path("/models"))
|
|
.respond_with(ResponseTemplate::new(503))
|
|
.expect(2)
|
|
.mount(&server)
|
|
.await;
|
|
let transport =
|
|
ReqwestTransport::from_http_client(HttpClientBuilder::new().build_direct().unwrap());
|
|
let provider = provider(&server.uri());
|
|
let url = ModelsClient::<ReqwestTransport>::request_url(&provider, "0.1.0");
|
|
let dispatches = Arc::new(Dispatches::default());
|
|
let client = ModelsClient::new(transport, provider, Arc::new(DummyAuth))
|
|
.with_telemetry(Some(dispatches.clone()));
|
|
client
|
|
.list_models(
|
|
url,
|
|
HeaderMap::new(),
|
|
/*response_body_limit_bytes*/ None,
|
|
)
|
|
.await
|
|
.unwrap_err();
|
|
assert_eq!(dispatches.0.load(Ordering::Relaxed), 2);
|
|
}
|
|
|
|
#[tokio::test]
|
|
async fn auth_failure_before_transport_does_not_count_as_a_models_get_attempt() {
|
|
struct FailingAuth;
|
|
impl AuthProvider for FailingAuth {
|
|
fn add_auth_headers(&self, _headers: &mut HeaderMap) -> Result<(), codex_api::AuthError> {
|
|
Err(codex_api::AuthError::Build(
|
|
"cannot authenticate".to_owned(),
|
|
))
|
|
}
|
|
}
|
|
|
|
let server = MockServer::start().await;
|
|
let provider = provider(&server.uri());
|
|
let url = ModelsClient::<ReqwestTransport>::request_url(&provider, "0.1.0");
|
|
let transport =
|
|
ReqwestTransport::from_http_client(HttpClientBuilder::new().build_direct().unwrap());
|
|
let dispatches = Arc::new(Dispatches::default());
|
|
let client = ModelsClient::new(transport, provider, Arc::new(FailingAuth))
|
|
.with_telemetry(Some(dispatches.clone()));
|
|
let error = client
|
|
.list_models(
|
|
url,
|
|
HeaderMap::new(),
|
|
/*response_body_limit_bytes*/ None,
|
|
)
|
|
.await
|
|
.unwrap_err();
|
|
|
|
assert!(matches!(
|
|
error,
|
|
codex_api::ApiError::Transport(codex_api::TransportError::Build(_))
|
|
));
|
|
assert_eq!(dispatches.0.load(Ordering::Relaxed), 0);
|
|
assert!(server.received_requests().await.unwrap().is_empty());
|
|
}
|