diff --git a/codex-rs/core/src/client.rs b/codex-rs/core/src/client.rs index 8d2db30d7b..d46a98ed31 100644 --- a/codex-rs/core/src/client.rs +++ b/codex-rs/core/src/client.rs @@ -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, + auth_owner_generation: Option, api_provider: ApiProvider, api_auth: SharedAuthProvider, agent_identity_telemetry: Option, @@ -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>, } @@ -289,6 +293,8 @@ struct LastResponse { struct WebsocketSession { connection: Option, endpoint: Option, + /// Owner of the cached state, including before a connection is opened. + auth_owner_generation: Option, last_request: Option, last_response_rx: Option>, 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 { + 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 { + // 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, responses_metadata: &'a CodexResponsesMetadata, auth_context: AuthRequestTelemetryContext, request_route_telemetry: RequestRouteTelemetry, diff --git a/codex-rs/core/tests/suite/client_websockets.rs b/codex-rs/core/tests/suite/client_websockets.rs index afe211268f..4d2ba3c926 100755 --- a/codex-rs/core/tests/suite/client_websockets.rs +++ b/codex-rs/core/tests/suite/client_websockets.rs @@ -12,6 +12,7 @@ use codex_core::X_RESPONSESAPI_INCLUDE_TIMING_METRICS_HEADER; use codex_core::test_support::with_parent_turn; use codex_features::Feature; use codex_http_client::OutboundProxyPolicy; +use codex_login::AuthManager; use codex_login::CodexAuth; use codex_login::auth::AgentIdentityAuthPolicy; use codex_model_provider_info::ModelProviderInfo; @@ -111,7 +112,8 @@ fn assert_request_trace_matches(body: &serde_json::Value, expected_trace: &W3cTr } struct WebsocketTestHarness { - _codex_home: TempDir, + codex_home: TempDir, + auth_manager: Arc, client: ModelClient, outbound_proxy_policy: OutboundProxyPolicy, session_id: SessionId, @@ -877,6 +879,131 @@ async fn responses_websocket_reuses_connection_after_session_drop() { server.shutdown().await; } +#[tokio::test(flavor = "multi_thread", worker_threads = 2)] +async fn responses_websocket_reconnects_after_account_switch() { + skip_if_no_network!(); + + for new_turn_on_switch in [false, true] { + // Keep the first socket open so only the account switch forces a reconnect. + let server = start_websocket_server( + [ + (1..=5, "first-account-state"), + (3..=5, "second-account-state"), + ] + .into_iter() + .map(|(responses, turn_state)| { + responses + .map(|index| { + let id = format!("resp-{index}"); + vec![ + ev_response_created(&id), + json!({ + "type": "response.metadata", + "headers": {"x-codex-turn-state": turn_state}, + }), + ev_completed(&id), + ] + }) + .collect() + }) + .collect(), + ) + .await; + let harness = websocket_harness_for_codex_backend(&server).await; + let mut client_session = harness.client.new_session(); + let mut input = Vec::new(); + + for index in 1..=5 { + if index == 3 { + let mut tokens = harness + .auth_manager + .auth_cached() + .unwrap() + .get_token_data() + .unwrap(); + tokens.id_token.raw_jwt = "e30.e30.signature".into(); + tokens.account_id = Some("second-account".into()); + tokens.access_token = "second-account-token".into(); + std::fs::write( + harness.codex_home.path().join("auth.json"), + serde_json::to_vec(&json!({ + "auth_mode": "chatgpt", + "tokens": tokens, + "last_refresh": chrono::Utc::now(), + })) + .unwrap(), + ) + .unwrap(); + harness.auth_manager.reload().await; + } + if index == 5 || (index == 3 && new_turn_on_switch) { + drop(client_session); + client_session = harness.client.new_session(); + } + input.push(message_item(&format!("request {index}"))); + stream_until_complete_with_model_info( + &mut client_session, + &harness, + &prompt_with_input(input.clone()), + &harness.model_info, + &format!("resp-{index}"), + ) + .await; + } + + assert_eq!( + server + .handshakes() + .iter() + .map(|handshake| ( + handshake.header("chatgpt-account-id"), + handshake.header("authorization"), + )) + .collect::>(), + vec![ + ( + Some("account_id".into()), + Some("Bearer Access Token".into()) + ), + ( + Some("second-account".into()), + Some("Bearer second-account-token".into()) + ), + ], + ); + assert_eq!( + server + .connections() + .iter() + .map(|requests| requests + .iter() + .map(|request| { + let body = request.body_json(); + ( + body["previous_response_id"].clone(), + body["input"].as_array().unwrap().len(), + body["client_metadata"]["x-codex-turn-state"].clone(), + ) + }) + .collect::>()) + .collect::>(), + vec![ + vec![ + (json!(null), 1, json!(null)), + (json!("resp-1"), 1, json!("first-account-state")), + ], + vec![ + (json!(null), 3, json!(null)), + (json!("resp-3"), 1, json!("second-account-state")), + (json!("resp-4"), 1, json!(null)), + ], + ], + "new_turn_on_switch={new_turn_on_switch}", + ); + server.shutdown().await; + } +} + #[tokio::test(flavor = "multi_thread", worker_threads = 2)] async fn responses_websocket_sends_responses_lite_metadata_per_request() { skip_if_no_network!(); @@ -2661,7 +2788,12 @@ async fn websocket_harness_with_provider_options_and_auth( let model_info = codex_core::test_support::construct_model_info_offline(MODEL, &config); let thread_id = ThreadId::new(); let session_id = SessionId::new(); - let client_auth_manager = auth.map(codex_core::test_support::auth_manager_from_auth); + let client_auth_manager = auth.map(|auth| { + codex_core::test_support::auth_manager_from_auth_with_home( + auth, + codex_home.path().to_path_buf(), + ) + }); let auth_manager = client_auth_manager.clone().unwrap_or_else(|| { codex_core::test_support::auth_manager_from_auth(CodexAuth::from_api_key("Test API Key")) }); @@ -2707,7 +2839,8 @@ async fn websocket_harness_with_provider_options_and_auth( ); WebsocketTestHarness { - _codex_home: codex_home, + codex_home, + auth_manager, client, outbound_proxy_policy, session_id,