mirror of
https://github.com/openai/codex.git
synced 2026-11-27 23:31:09 +00:00
Route Guardian inference through dedicated endpoints (#40892)
## What changed - Add the opt-in `features.guardianv2.free_guardian` setting to route eligible Guardian reviews to `/guardian` and asynchronous classifications to `/guardian-classifier`. - Support endpoint selection for both HTTP and WebSocket Responses clients, including endpoint-aware connection reuse. - Keep `/responses` for API-key authentication, custom provider URLs, non-Guardian models, and configurations where the option is disabled. - Omit routing hints and `service_tier` from requests sent to the dedicated Guardian endpoints. ## Testing - Cover route selection across authentication modes, provider URLs, HTTP fallback, WebSocket review sessions, and classifier sampling. GitOrigin-RevId: 9c88673c710d009e0239559b9245a4e6c4f4befc
This commit is contained in:
1 parent
10d5a603ae
commit
62fb56ee56
22 files changed
+488
-87
No files matched your search
+93
-37
@@ -50,6 +50,7 @@ use codex_api::ReqwestTransport;
|
||||
use codex_api::ResponseCreateWsRequest;
|
||||
use codex_api::ResponsesApiRequest;
|
||||
use codex_api::ResponsesClient as ApiResponsesClient;
|
||||
use codex_api::ResponsesEndpoint;
|
||||
use codex_api::ResponsesOptions as ApiResponsesOptions;
|
||||
use codex_api::ResponsesWebsocketClient as ApiWebSocketResponsesClient;
|
||||
use codex_api::ResponsesWebsocketConnection as ApiWebSocketConnection;
|
||||
@@ -161,7 +162,6 @@ const RESPONSES_WEBSOCKETS_V2_BETA_HEADER_VALUE: &str = "responses_websockets=20
|
||||
const X_OPENAI_INTERNAL_CODEX_RESPONSES_LITE_HEADER: &str =
|
||||
"x-openai-internal-codex-responses-lite";
|
||||
const REALTIME_CALLS_ENDPOINT: &str = "/realtime/calls";
|
||||
const RESPONSES_ENDPOINT: &str = "/responses";
|
||||
const RESPONSES_COMPACT_ENDPOINT: &str = "/responses/compact";
|
||||
// `/responses/compact` is unary, so the timeout covers the full response rather than one idle
|
||||
// period between stream events.
|
||||
@@ -261,6 +261,7 @@ pub struct ModelClient {
|
||||
state: Arc<ModelClientState>,
|
||||
agent_identity_policy: AgentIdentityAuthPolicy,
|
||||
prompt_cache_key_override: Option<String>,
|
||||
free_guardian_enabled: bool,
|
||||
http_client_factory: HttpClientFactory,
|
||||
}
|
||||
|
||||
@@ -302,6 +303,7 @@ struct LastResponse {
|
||||
#[derive(Debug, Default)]
|
||||
struct WebsocketSession {
|
||||
connection: Option<ApiWebSocketConnection>,
|
||||
endpoint: Option<ResponsesEndpoint>,
|
||||
last_request: Option<ResponsesApiRequest>,
|
||||
last_response_rx: Option<oneshot::Receiver<LastResponse>>,
|
||||
last_response_from_untraced_warmup: bool,
|
||||
@@ -480,10 +482,16 @@ impl ModelClient {
|
||||
}),
|
||||
agent_identity_policy,
|
||||
prompt_cache_key_override: None,
|
||||
free_guardian_enabled: false,
|
||||
http_client_factory,
|
||||
}
|
||||
}
|
||||
|
||||
pub(crate) fn with_free_guardian_enabled(mut self, free_guardian_enabled: bool) -> Self {
|
||||
self.free_guardian_enabled = free_guardian_enabled;
|
||||
self
|
||||
}
|
||||
|
||||
pub(crate) fn with_prompt_cache_key_override(
|
||||
mut self,
|
||||
prompt_cache_key_override: Option<String>,
|
||||
@@ -1020,21 +1028,37 @@ impl ModelClient {
|
||||
})
|
||||
}
|
||||
|
||||
fn responses_endpoint(&self, auth: Option<&CodexAuth>, model: &str) -> ResponsesEndpoint {
|
||||
if self.free_guardian_enabled
|
||||
&& crate::guardian::is_basic_session_source(&self.state.session_source)
|
||||
&& self.uses_codex_backend(auth)
|
||||
&& self.state.provider.info().supports_codex_backend_routes()
|
||||
&& model == self.state.provider.approval_review_preferred_model()
|
||||
{
|
||||
ResponsesEndpoint::Guardian
|
||||
} else {
|
||||
ResponsesEndpoint::Responses
|
||||
}
|
||||
}
|
||||
|
||||
fn uses_codex_backend(&self, auth: Option<&CodexAuth>) -> bool {
|
||||
let provider = self.state.provider.info();
|
||||
auth.is_some_and(CodexAuth::uses_codex_backend)
|
||||
&& provider.is_openai()
|
||||
&& provider.requires_openai_auth
|
||||
&& provider.env_key.is_none()
|
||||
&& provider.experimental_bearer_token.is_none()
|
||||
&& provider.auth.is_none()
|
||||
&& provider.aws.is_none()
|
||||
}
|
||||
|
||||
fn build_routing_hint_header(
|
||||
&self,
|
||||
auth: Option<&CodexAuth>,
|
||||
model: &str,
|
||||
service_tier: Option<&str>,
|
||||
) -> Option<HeaderValue> {
|
||||
let provider = self.state.provider.info();
|
||||
if !auth.is_some_and(CodexAuth::uses_codex_backend)
|
||||
|| !provider.is_openai()
|
||||
|| !provider.requires_openai_auth
|
||||
|| provider.env_key.is_some()
|
||||
|| provider.experimental_bearer_token.is_some()
|
||||
|| provider.auth.is_some()
|
||||
|| provider.aws.is_some()
|
||||
{
|
||||
if !self.uses_codex_backend(auth) {
|
||||
return None;
|
||||
}
|
||||
|
||||
@@ -1077,6 +1101,7 @@ impl ModelClient {
|
||||
responses_metadata: &CodexResponsesMetadata,
|
||||
auth_context: AuthRequestTelemetryContext,
|
||||
request_route_telemetry: RequestRouteTelemetry,
|
||||
endpoint: ResponsesEndpoint,
|
||||
) -> std::result::Result<ApiWebSocketConnection, ApiError> {
|
||||
let headers = self.build_websocket_headers(responses_metadata).await;
|
||||
let websocket_telemetry = ModelClientSession::build_websocket_telemetry(
|
||||
@@ -1089,13 +1114,15 @@ impl ModelClient {
|
||||
let start = Instant::now();
|
||||
let result = match tokio::time::timeout(
|
||||
websocket_connect_timeout,
|
||||
ApiWebSocketResponsesClient::new(api_provider, api_auth).connect(
|
||||
&self.http_client_factory,
|
||||
headers,
|
||||
codex_login::default_client::default_headers(),
|
||||
/*turn_state*/ None,
|
||||
Some(websocket_telemetry),
|
||||
),
|
||||
ApiWebSocketResponsesClient::new(api_provider, api_auth)
|
||||
.with_endpoint(endpoint)
|
||||
.connect(
|
||||
&self.http_client_factory,
|
||||
headers,
|
||||
codex_login::default_client::default_headers(),
|
||||
/*turn_state*/ None,
|
||||
Some(websocket_telemetry),
|
||||
),
|
||||
)
|
||||
.await
|
||||
{
|
||||
@@ -1206,6 +1233,7 @@ impl ModelClientSession {
|
||||
|
||||
fn reset_websocket_session(&mut self) {
|
||||
self.websocket_session.connection = None;
|
||||
self.websocket_session.endpoint = None;
|
||||
self.websocket_session.last_request = None;
|
||||
self.websocket_session.last_response_rx = None;
|
||||
self.websocket_session.last_response_from_untraced_warmup = false;
|
||||
@@ -1336,6 +1364,7 @@ impl ModelClientSession {
|
||||
/// This performs only connection setup; it never sends prompt payloads.
|
||||
pub async fn preconnect_websocket(
|
||||
&mut self,
|
||||
model_info: &ModelInfo,
|
||||
session_telemetry: &SessionTelemetry,
|
||||
responses_metadata: &CodexResponsesMetadata,
|
||||
) -> std::result::Result<(), ApiError> {
|
||||
@@ -1357,6 +1386,9 @@ impl ModelClientSession {
|
||||
client_setup.agent_identity_telemetry.clone(),
|
||||
PendingUnauthorizedRetry::default(),
|
||||
);
|
||||
let endpoint = self
|
||||
.client
|
||||
.responses_endpoint(client_setup.auth.as_ref(), &model_info.slug);
|
||||
let connection = self
|
||||
.client
|
||||
.connect_websocket(
|
||||
@@ -1365,10 +1397,12 @@ impl ModelClientSession {
|
||||
client_setup.api_auth,
|
||||
responses_metadata,
|
||||
auth_context,
|
||||
RequestRouteTelemetry::for_endpoint(RESPONSES_ENDPOINT),
|
||||
RequestRouteTelemetry::for_endpoint(endpoint.path()),
|
||||
endpoint,
|
||||
)
|
||||
.await?;
|
||||
self.websocket_session.connection = Some(connection);
|
||||
self.websocket_session.endpoint = Some(endpoint);
|
||||
self.websocket_session
|
||||
.set_connection_reused(/*connection_reused*/ false);
|
||||
Ok(())
|
||||
@@ -1397,16 +1431,17 @@ impl ModelClientSession {
|
||||
responses_metadata,
|
||||
auth_context,
|
||||
request_route_telemetry,
|
||||
endpoint,
|
||||
} = params;
|
||||
let needs_new = match self.websocket_session.connection.as_ref() {
|
||||
Some(conn) => conn.is_closed().await,
|
||||
Some(conn) => {
|
||||
self.websocket_session.endpoint != Some(endpoint) || conn.is_closed().await
|
||||
}
|
||||
None => true,
|
||||
};
|
||||
|
||||
if needs_new {
|
||||
self.websocket_session.last_request = None;
|
||||
self.websocket_session.last_response_rx = None;
|
||||
self.websocket_session.last_response_from_untraced_warmup = false;
|
||||
self.reset_websocket_session();
|
||||
let new_conn = match self
|
||||
.client
|
||||
.connect_websocket(
|
||||
@@ -1416,6 +1451,7 @@ impl ModelClientSession {
|
||||
responses_metadata,
|
||||
auth_context,
|
||||
request_route_telemetry,
|
||||
endpoint,
|
||||
)
|
||||
.await
|
||||
{
|
||||
@@ -1428,6 +1464,7 @@ impl ModelClientSession {
|
||||
}
|
||||
};
|
||||
self.websocket_session.connection = Some(new_conn);
|
||||
self.websocket_session.endpoint = Some(endpoint);
|
||||
self.websocket_session
|
||||
.set_connection_reused(/*connection_reused*/ false);
|
||||
} else {
|
||||
@@ -1490,9 +1527,12 @@ impl ModelClientSession {
|
||||
let mut pending_retry = PendingUnauthorizedRetry::default();
|
||||
loop {
|
||||
let client_setup = self.client.current_client_setup().await?;
|
||||
let endpoint = self
|
||||
.client
|
||||
.responses_endpoint(client_setup.auth.as_ref(), &model_info.slug);
|
||||
let transport = self
|
||||
.client
|
||||
.build_api_transport(&client_setup.api_provider, RESPONSES_ENDPOINT)?;
|
||||
.build_api_transport(&client_setup.api_provider, endpoint.path())?;
|
||||
let request_auth_context = AuthRequestTelemetryContext::new(
|
||||
client_setup.auth.as_ref().map(CodexAuth::auth_mode),
|
||||
client_setup.api_auth.as_ref(),
|
||||
@@ -1502,7 +1542,7 @@ impl ModelClientSession {
|
||||
let (request_telemetry, sse_telemetry) = Self::build_streaming_telemetry(
|
||||
session_telemetry,
|
||||
request_auth_context,
|
||||
RequestRouteTelemetry::for_endpoint(RESPONSES_ENDPOINT),
|
||||
RequestRouteTelemetry::for_endpoint(endpoint.path()),
|
||||
self.client.state.auth_env_telemetry.clone(),
|
||||
);
|
||||
let compression = self.responses_request_compression(client_setup.auth.as_ref());
|
||||
@@ -1522,11 +1562,16 @@ impl ModelClientSession {
|
||||
service_tier.clone(),
|
||||
responses_metadata,
|
||||
)?;
|
||||
if let Some(header_value) = self.client.build_routing_hint_header(
|
||||
client_setup.auth.as_ref(),
|
||||
&request.model,
|
||||
request.service_tier.as_deref(),
|
||||
) {
|
||||
if endpoint == ResponsesEndpoint::Guardian {
|
||||
request.service_tier = None;
|
||||
}
|
||||
if endpoint == ResponsesEndpoint::Responses
|
||||
&& let Some(header_value) = self.client.build_routing_hint_header(
|
||||
client_setup.auth.as_ref(),
|
||||
&request.model,
|
||||
request.service_tier.as_deref(),
|
||||
)
|
||||
{
|
||||
options
|
||||
.extra_headers
|
||||
.insert(X_CODEX_ROUTING_HINT_HEADER, header_value);
|
||||
@@ -1547,6 +1592,7 @@ impl ModelClientSession {
|
||||
client_setup.api_provider,
|
||||
client_setup.api_auth,
|
||||
)
|
||||
.with_endpoint(endpoint)
|
||||
.with_telemetry(Some(request_telemetry), Some(sse_telemetry));
|
||||
let stream_result = client.stream_request(request, options).await;
|
||||
|
||||
@@ -1639,6 +1685,9 @@ impl ModelClientSession {
|
||||
let mut pending_retry = PendingUnauthorizedRetry::default();
|
||||
loop {
|
||||
let client_setup = self.client.current_client_setup().await?;
|
||||
let endpoint = self
|
||||
.client
|
||||
.responses_endpoint(client_setup.auth.as_ref(), &model_info.slug);
|
||||
let request_auth_context = AuthRequestTelemetryContext::new(
|
||||
client_setup.auth.as_ref().map(CodexAuth::auth_mode),
|
||||
client_setup.api_auth.as_ref(),
|
||||
@@ -1653,16 +1702,23 @@ impl ModelClientSession {
|
||||
service_tier.clone(),
|
||||
responses_metadata,
|
||||
)?;
|
||||
if endpoint == ResponsesEndpoint::Guardian {
|
||||
request.service_tier = None;
|
||||
}
|
||||
request.access_programs = cyber_access_program::for_auth(
|
||||
client_setup.auth.as_ref(),
|
||||
prompt.cyber_access_program,
|
||||
);
|
||||
let mut websocket_metadata = responses_metadata.clone();
|
||||
websocket_metadata.routing_hint = self.client.build_routing_hint_header(
|
||||
client_setup.auth.as_ref(),
|
||||
&request.model,
|
||||
request.service_tier.as_deref(),
|
||||
);
|
||||
websocket_metadata.routing_hint = if endpoint == ResponsesEndpoint::Responses {
|
||||
self.client.build_routing_hint_header(
|
||||
client_setup.auth.as_ref(),
|
||||
&request.model,
|
||||
request.service_tier.as_deref(),
|
||||
)
|
||||
} else {
|
||||
None
|
||||
};
|
||||
let request_session_telemetry = if warmup {
|
||||
// `generate=false` prewarm is connection setup, not an inference request.
|
||||
session_telemetry.clone()
|
||||
@@ -1682,9 +1738,8 @@ impl ModelClientSession {
|
||||
api_auth: client_setup.api_auth,
|
||||
responses_metadata: &websocket_metadata,
|
||||
auth_context: request_auth_context,
|
||||
request_route_telemetry: RequestRouteTelemetry::for_endpoint(
|
||||
RESPONSES_ENDPOINT,
|
||||
),
|
||||
request_route_telemetry: RequestRouteTelemetry::for_endpoint(endpoint.path()),
|
||||
endpoint,
|
||||
})
|
||||
.await
|
||||
{
|
||||
@@ -2274,6 +2329,7 @@ struct WebsocketConnectParams<'a> {
|
||||
responses_metadata: &'a CodexResponsesMetadata,
|
||||
auth_context: AuthRequestTelemetryContext,
|
||||
request_route_telemetry: RequestRouteTelemetry,
|
||||
endpoint: ResponsesEndpoint,
|
||||
}
|
||||
|
||||
async fn handle_unauthorized(
|
||||
|
||||
Reference in new issue
Block a user