Reset cached WebSocket state when auth ownership changes (#44489)

## Why

Switching accounts could reuse a Responses WebSocket connection and incremental response state belonging to the previous account.

## What changed

Track auth ownership for cached WebSocket sessions and reconnect when it changes, including across turns. Clear incremental response state and `x-codex-turn-state` so the next request sends the full input with fresh routing state. Apply the same connection handling to prewarm requests and build request metadata after checking ownership.

## Testing

Add a regression test for account switches within a turn and between turns. Verify new account credentials on reconnect, full input without `previous_response_id` or stale routing state after the switch, and subsequent connection and incremental response reuse.

GitOrigin-RevId: 83b0636c0afec8b21b5d837a9937db32fb448237
This commit is contained in:
jif authored and copyberry committed 2026-09-10 10:23:20 +00:00
1 parent ee93abb690
commit 537278c65f
2 files changed
+193 -27

No files matched your search

+57 -24
View File
@@ -11,6 +11,8 @@
//! A [`ModelClientSession`] is created per turn and is used to stream one or more Responses API
//! requests during that turn. It caches a Responses WebSocket connection (opened lazily) and stores
//! per-turn state such as the `x-codex-turn-state` token used for sticky routing.
//! Cached connections, incremental response state, and turn routing are discarded when auth
//! ownership changes.
//!
//! WebSocket prewarm is a v2-only `response.create` with `generate=false`; it waits for completion
//! so the next request can reuse the same connection and `previous_response_id`.
@@ -213,6 +215,7 @@ struct ModelClientState {
/// share the same auth/provider setup flow.
struct CurrentClientSetup {
auth: Option<CodexAuth>,
auth_owner_generation: Option<u64>,
api_provider: ApiProvider,
api_auth: SharedAuthProvider,
agent_identity_telemetry: Option<AgentIdentityTelemetry>,
@@ -276,6 +279,7 @@ pub struct ModelClientSession {
/// This is a contract between the client and server: we receive it at turn start,
/// keep sending it unchanged between turn requests (e.g., for retries, incremental
/// appends, or continuation requests), and must not send it between different turns.
/// An auth ownership change clears it so the new owner gets fresh routing state.
turn_state: Arc<OnceLock<String>>,
}
@@ -289,6 +293,8 @@ struct LastResponse {
struct WebsocketSession {
connection: Option<ApiWebSocketConnection>,
endpoint: Option<ResponsesEndpoint>,
/// Owner of the cached state, including before a connection is opened.
auth_owner_generation: Option<u64>,
last_request: Option<ResponsesApiRequest>,
last_response_rx: Option<oneshot::Receiver<LastResponse>>,
last_response_from_untraced_warmup: bool,
@@ -507,9 +513,18 @@ impl ModelClient {
/// This constructor does not perform network I/O itself; the session opens a websocket lazily
/// when the first stream request is issued.
pub fn new_session(&self) -> ModelClientSession {
let auth_owner_generation = self.auth_owner_generation();
let mut websocket_session = self.take_cached_websocket_session();
if websocket_session.auth_owner_generation != auth_owner_generation {
// Drop the old owner's cache before this turn can establish fresh routing state.
websocket_session = WebsocketSession {
auth_owner_generation,
..Default::default()
};
}
ModelClientSession {
client: self.clone(),
websocket_session: self.take_cached_websocket_session(),
websocket_session,
turn_state: Arc::new(OnceLock::new()),
}
}
@@ -518,6 +533,15 @@ impl ModelClient {
self.state.provider.auth_manager()
}
fn auth_owner_generation(&self) -> Option<u64> {
self.auth_manager().map(|manager| {
manager
.auth_change_state_receiver()
.borrow()
.owner_generation
})
}
fn take_cached_websocket_session(&self) -> WebsocketSession {
let mut cached_websocket_session = self
.state
@@ -914,6 +938,9 @@ impl ModelClient {
/// This centralizes setup used by both prewarm and normal request paths so they stay in
/// lockstep when auth/provider resolution changes.
async fn current_client_setup(&self) -> Result<CurrentClientSetup> {
// Capture before resolving credentials so an account switch during setup cannot label
// an old connection with the new owner's revision.
let auth_owner_generation = self.auth_owner_generation();
let auth = self.state.provider.auth().await;
let api_provider = self.state.provider.api_provider().await?;
let resolved_auth = self
@@ -927,6 +954,7 @@ impl ModelClient {
.await?;
Ok(CurrentClientSetup {
auth,
auth_owner_generation,
api_provider,
api_auth: resolved_auth.auth,
agent_identity_telemetry: resolved_auth.agent_identity_telemetry,
@@ -1325,22 +1353,17 @@ impl ModelClientSession {
let endpoint = self
.client
.responses_endpoint(client_setup.auth.as_ref(), &model_info.slug);
let connection = self
.client
.connect_websocket(
session_telemetry,
client_setup.api_provider,
client_setup.api_auth,
responses_metadata,
auth_context,
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);
self.websocket_connection(WebsocketConnectParams {
session_telemetry,
api_provider: client_setup.api_provider,
api_auth: client_setup.api_auth,
auth_owner_generation: client_setup.auth_owner_generation,
responses_metadata,
auth_context,
request_route_telemetry: RequestRouteTelemetry::for_endpoint(endpoint.path()),
endpoint,
})
.await?;
Ok(())
}
/// Returns a websocket connection for this turn.
@@ -1364,6 +1387,7 @@ impl ModelClientSession {
session_telemetry,
api_provider,
api_auth,
auth_owner_generation,
responses_metadata,
auth_context,
request_route_telemetry,
@@ -1375,8 +1399,14 @@ impl ModelClientSession {
}
None => true,
};
// Resolving an external auth provider can change ownership during client setup.
let owner_changed = self.websocket_session.auth_owner_generation != auth_owner_generation
|| self.client.auth_owner_generation() != auth_owner_generation;
if owner_changed {
self.turn_state = Arc::new(OnceLock::new());
}
if needs_new {
if needs_new || owner_changed {
self.reset_websocket_session();
let new_conn = match self
.client
@@ -1401,6 +1431,7 @@ impl ModelClientSession {
};
self.websocket_session.connection = Some(new_conn);
self.websocket_session.endpoint = Some(endpoint);
self.websocket_session.auth_owner_generation = auth_owner_generation;
self.websocket_session
.set_connection_reused(/*connection_reused*/ false);
} else {
@@ -1682,17 +1713,12 @@ impl ModelClientSession {
} else {
session_telemetry_for_request(session_telemetry, &request)
};
let mut client_metadata = self
.client
.build_ws_client_metadata(responses_metadata, model_info.use_responses_lite);
if let Some(turn_state) = self.turn_state.get() {
client_metadata.insert(X_CODEX_TURN_STATE_HEADER.to_string(), turn_state.clone());
}
match self
.websocket_connection(WebsocketConnectParams {
session_telemetry,
api_provider: client_setup.api_provider,
api_auth: client_setup.api_auth,
auth_owner_generation: client_setup.auth_owner_generation,
responses_metadata: &websocket_metadata,
auth_context: request_auth_context,
request_route_telemetry: RequestRouteTelemetry::for_endpoint(endpoint.path()),
@@ -1732,6 +1758,12 @@ impl ModelClientSession {
{
crate::guardian::observe_guardian_request(session_telemetry, &request);
}
let mut client_metadata = self
.client
.build_ws_client_metadata(responses_metadata, model_info.use_responses_lite);
if let Some(turn_state) = self.turn_state.get() {
client_metadata.insert(X_CODEX_TURN_STATE_HEADER.to_string(), turn_state.clone());
}
let (incremental_request, previous_response_id_from_untraced_warmup) =
self.prepare_websocket_request(&request);
let inference_trace_attempt = if warmup {
@@ -2299,6 +2331,7 @@ struct WebsocketConnectParams<'a> {
session_telemetry: &'a SessionTelemetry,
api_provider: codex_api::Provider,
api_auth: SharedAuthProvider,
auth_owner_generation: Option<u64>,
responses_metadata: &'a CodexResponsesMetadata,
auth_context: AuthRequestTelemetryContext,
request_route_telemetry: RequestRouteTelemetry,