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 //! 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 //! 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. //! 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 //! 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`. //! 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. /// share the same auth/provider setup flow.
struct CurrentClientSetup { struct CurrentClientSetup {
auth: Option<CodexAuth>, auth: Option<CodexAuth>,
auth_owner_generation: Option<u64>,
api_provider: ApiProvider, api_provider: ApiProvider,
api_auth: SharedAuthProvider, api_auth: SharedAuthProvider,
agent_identity_telemetry: Option<AgentIdentityTelemetry>, 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, /// 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 /// keep sending it unchanged between turn requests (e.g., for retries, incremental
/// appends, or continuation requests), and must not send it between different turns. /// 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>>, turn_state: Arc<OnceLock<String>>,
} }
@@ -289,6 +293,8 @@ struct LastResponse {
struct WebsocketSession { struct WebsocketSession {
connection: Option<ApiWebSocketConnection>, connection: Option<ApiWebSocketConnection>,
endpoint: Option<ResponsesEndpoint>, endpoint: Option<ResponsesEndpoint>,
/// Owner of the cached state, including before a connection is opened.
auth_owner_generation: Option<u64>,
last_request: Option<ResponsesApiRequest>, last_request: Option<ResponsesApiRequest>,
last_response_rx: Option<oneshot::Receiver<LastResponse>>, last_response_rx: Option<oneshot::Receiver<LastResponse>>,
last_response_from_untraced_warmup: bool, 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 /// This constructor does not perform network I/O itself; the session opens a websocket lazily
/// when the first stream request is issued. /// when the first stream request is issued.
pub fn new_session(&self) -> ModelClientSession { 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 { ModelClientSession {
client: self.clone(), client: self.clone(),
websocket_session: self.take_cached_websocket_session(), websocket_session,
turn_state: Arc::new(OnceLock::new()), turn_state: Arc::new(OnceLock::new()),
} }
} }
@@ -518,6 +533,15 @@ impl ModelClient {
self.state.provider.auth_manager() 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 { fn take_cached_websocket_session(&self) -> WebsocketSession {
let mut cached_websocket_session = self let mut cached_websocket_session = self
.state .state
@@ -914,6 +938,9 @@ impl ModelClient {
/// This centralizes setup used by both prewarm and normal request paths so they stay in /// This centralizes setup used by both prewarm and normal request paths so they stay in
/// lockstep when auth/provider resolution changes. /// lockstep when auth/provider resolution changes.
async fn current_client_setup(&self) -> Result<CurrentClientSetup> { 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 auth = self.state.provider.auth().await;
let api_provider = self.state.provider.api_provider().await?; let api_provider = self.state.provider.api_provider().await?;
let resolved_auth = self let resolved_auth = self
@@ -927,6 +954,7 @@ impl ModelClient {
.await?; .await?;
Ok(CurrentClientSetup { Ok(CurrentClientSetup {
auth, auth,
auth_owner_generation,
api_provider, api_provider,
api_auth: resolved_auth.auth, api_auth: resolved_auth.auth,
agent_identity_telemetry: resolved_auth.agent_identity_telemetry, agent_identity_telemetry: resolved_auth.agent_identity_telemetry,
@@ -1325,22 +1353,17 @@ impl ModelClientSession {
let endpoint = self let endpoint = self
.client .client
.responses_endpoint(client_setup.auth.as_ref(), &model_info.slug); .responses_endpoint(client_setup.auth.as_ref(), &model_info.slug);
let connection = self self.websocket_connection(WebsocketConnectParams {
.client session_telemetry,
.connect_websocket( api_provider: client_setup.api_provider,
session_telemetry, api_auth: client_setup.api_auth,
client_setup.api_provider, auth_owner_generation: client_setup.auth_owner_generation,
client_setup.api_auth, responses_metadata,
responses_metadata, auth_context,
auth_context, request_route_telemetry: RequestRouteTelemetry::for_endpoint(endpoint.path()),
RequestRouteTelemetry::for_endpoint(endpoint.path()), endpoint,
endpoint, })
) .await?;
.await?;
self.websocket_session.connection = Some(connection);
self.websocket_session.endpoint = Some(endpoint);
self.websocket_session
.set_connection_reused(/*connection_reused*/ false);
Ok(()) Ok(())
} }
/// Returns a websocket connection for this turn. /// Returns a websocket connection for this turn.
@@ -1364,6 +1387,7 @@ impl ModelClientSession {
session_telemetry, session_telemetry,
api_provider, api_provider,
api_auth, api_auth,
auth_owner_generation,
responses_metadata, responses_metadata,
auth_context, auth_context,
request_route_telemetry, request_route_telemetry,
@@ -1375,8 +1399,14 @@ impl ModelClientSession {
} }
None => true, 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(); self.reset_websocket_session();
let new_conn = match self let new_conn = match self
.client .client
@@ -1401,6 +1431,7 @@ impl ModelClientSession {
}; };
self.websocket_session.connection = Some(new_conn); self.websocket_session.connection = Some(new_conn);
self.websocket_session.endpoint = Some(endpoint); self.websocket_session.endpoint = Some(endpoint);
self.websocket_session.auth_owner_generation = auth_owner_generation;
self.websocket_session self.websocket_session
.set_connection_reused(/*connection_reused*/ false); .set_connection_reused(/*connection_reused*/ false);
} else { } else {
@@ -1682,17 +1713,12 @@ impl ModelClientSession {
} else { } else {
session_telemetry_for_request(session_telemetry, &request) 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 match self
.websocket_connection(WebsocketConnectParams { .websocket_connection(WebsocketConnectParams {
session_telemetry, session_telemetry,
api_provider: client_setup.api_provider, api_provider: client_setup.api_provider,
api_auth: client_setup.api_auth, api_auth: client_setup.api_auth,
auth_owner_generation: client_setup.auth_owner_generation,
responses_metadata: &websocket_metadata, responses_metadata: &websocket_metadata,
auth_context: request_auth_context, auth_context: request_auth_context,
request_route_telemetry: RequestRouteTelemetry::for_endpoint(endpoint.path()), request_route_telemetry: RequestRouteTelemetry::for_endpoint(endpoint.path()),
@@ -1732,6 +1758,12 @@ impl ModelClientSession {
{ {
crate::guardian::observe_guardian_request(session_telemetry, &request); 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) = let (incremental_request, previous_response_id_from_untraced_warmup) =
self.prepare_websocket_request(&request); self.prepare_websocket_request(&request);
let inference_trace_attempt = if warmup { let inference_trace_attempt = if warmup {
@@ -2299,6 +2331,7 @@ struct WebsocketConnectParams<'a> {
session_telemetry: &'a SessionTelemetry, session_telemetry: &'a SessionTelemetry,
api_provider: codex_api::Provider, api_provider: codex_api::Provider,
api_auth: SharedAuthProvider, api_auth: SharedAuthProvider,
auth_owner_generation: Option<u64>,
responses_metadata: &'a CodexResponsesMetadata, responses_metadata: &'a CodexResponsesMetadata,
auth_context: AuthRequestTelemetryContext, auth_context: AuthRequestTelemetryContext,
request_route_telemetry: RequestRouteTelemetry, request_route_telemetry: RequestRouteTelemetry,
+136 -3
View File
@@ -12,6 +12,7 @@ use codex_core::X_RESPONSESAPI_INCLUDE_TIMING_METRICS_HEADER;
use codex_core::test_support::with_parent_turn; use codex_core::test_support::with_parent_turn;
use codex_features::Feature; use codex_features::Feature;
use codex_http_client::OutboundProxyPolicy; use codex_http_client::OutboundProxyPolicy;
use codex_login::AuthManager;
use codex_login::CodexAuth; use codex_login::CodexAuth;
use codex_login::auth::AgentIdentityAuthPolicy; use codex_login::auth::AgentIdentityAuthPolicy;
use codex_model_provider_info::ModelProviderInfo; use codex_model_provider_info::ModelProviderInfo;
@@ -111,7 +112,8 @@ fn assert_request_trace_matches(body: &serde_json::Value, expected_trace: &W3cTr
} }
struct WebsocketTestHarness { struct WebsocketTestHarness {
_codex_home: TempDir, codex_home: TempDir,
auth_manager: Arc<AuthManager>,
client: ModelClient, client: ModelClient,
outbound_proxy_policy: OutboundProxyPolicy, outbound_proxy_policy: OutboundProxyPolicy,
session_id: SessionId, session_id: SessionId,
@@ -877,6 +879,131 @@ async fn responses_websocket_reuses_connection_after_session_drop() {
server.shutdown().await; 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<_>>(),
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::<Vec<_>>())
.collect::<Vec<_>>(),
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)] #[tokio::test(flavor = "multi_thread", worker_threads = 2)]
async fn responses_websocket_sends_responses_lite_metadata_per_request() { async fn responses_websocket_sends_responses_lite_metadata_per_request() {
skip_if_no_network!(); 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 model_info = codex_core::test_support::construct_model_info_offline(MODEL, &config);
let thread_id = ThreadId::new(); let thread_id = ThreadId::new();
let session_id = SessionId::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(|| { 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")) 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 { WebsocketTestHarness {
_codex_home: codex_home, codex_home,
auth_manager,
client, client,
outbound_proxy_policy, outbound_proxy_policy,
session_id, session_id,