Bind runtime auth to the provider's selected credential source (#59823)

## Why

Runtime authentication must use the credential selected by the model provider. Explicit API keys and command credentials need independent ownership so cached connections and in-flight work cannot cross credential sources.

## What changed

- Register and reuse runtimes per credential source, supporting explicit provider keys and command credentials without falling back to saved login credentials.
- Track `AuthOwner` by credential manager and owner generation across WebSocket state, compaction, and scoring.
- Preserve cached identity and runtime registration state during credential-source outages or failed storage reloads. Require a successful auth reload before logout revokes credentials.
- Reject requests whose production or staging environment differs from runtime registration, including model catalog requests.

## Testing

Add regression coverage for credential selection and runtime reuse, owner isolation, source outages, failed reloads and logout recovery, and environment mismatches.

GitOrigin-RevId: 7bab55e0ea502593393df10046309fd8502edbd0
This commit is contained in:
cooper-oai
2026-11-06 04:33:14 +00:00
committed by copyberry
parent df38cb3b2a
commit faeb379646
37 changed files with 1875 additions and 477 deletions
@@ -2,6 +2,7 @@
//! Discarded requests still charge completed usage; session shutdown cancels remaining network work.
//! Snapshot capture and launch registration hold session state; prompt preparation does not.
use codex_login::AuthOwner;
use std::sync::Arc;
use crate::compact_remote_v2::BackgroundCompactionCandidate;
@@ -26,7 +27,7 @@ struct CompactionAttempt {
history_version: u64,
model_slug: String,
compaction_hash: Option<String>,
auth_owner_generation: Option<u64>,
auth_owner: Option<AuthOwner>,
}
#[derive(Default)]
@@ -57,7 +58,7 @@ pub(super) async fn maybe_start(
phase: CompactionPhase,
turn_cancellation: &CancellationToken,
token_status: &ContextWindowTokenStatus,
(turn_state, auth_owner_generation): (Option<String>, Option<u64>),
(turn_state, auth_owner): (Option<String>, Option<AuthOwner>),
) {
let turn_context = &step_context.turn;
// Let the foreground establish the backend's model pin before starting a parallel request.
@@ -89,14 +90,14 @@ pub(super) async fn maybe_start(
history_version: state.history.history_version(),
model_slug: model_info.slug.clone(),
compaction_hash: model_info.comp_hash.clone(),
auth_owner_generation,
auth_owner: auth_owner.clone(),
};
// Stop blocks new launches; already-running compaction remains session-owned.
if turn_cancellation.is_cancelled()
|| state.shutting_down
|| state.async_compaction.last_attempt.as_ref() == Some(&attempt)
|| state.history.annotated_items().is_empty()
|| sess.services.model_client.auth_owner_generation() != auth_owner_generation
|| sess.services.model_client.auth_owner() != auth_owner
{
return;
}
@@ -108,7 +109,7 @@ pub(super) async fn maybe_start(
state.history.clone(),
base_instructions,
turn_state,
auth_owner_generation,
auth_owner,
sess.current_window_from_state(&state),
);
let tasks = state.async_compaction.tasks.clone();
@@ -187,7 +188,7 @@ pub(crate) async fn wait_for_pending(
if attempt.history_version != state.history.history_version()
|| attempt.model_slug != model.slug
|| attempt.compaction_hash != model.comp_hash
|| attempt.auth_owner_generation != sess.services.model_client.auth_owner_generation()
|| attempt.auth_owner != sess.services.model_client.auth_owner()
{
return None;
}
@@ -120,7 +120,7 @@ async fn cancelled_turn_skips_launch_but_preserves_existing_work(trigger: Compac
let world_state = Arc::new(WorldState::default());
let cancelled = CancellationToken::new();
cancelled.cancel();
let auth = sess.services.model_client.auth_owner_generation();
let auth = sess.services.model_client.auth_owner();
maybe_start(
&sess,
&step,
@@ -128,7 +128,7 @@ async fn cancelled_turn_skips_launch_but_preserves_existing_work(trigger: Compac
phase,
&cancelled,
&status,
(Some("routing".to_string()), auth),
(Some("routing".to_string()), auth.clone()),
)
.await;
assert!(
@@ -149,7 +149,7 @@ async fn cancelled_turn_skips_launch_but_preserves_existing_work(trigger: Compac
phase,
&turn_cancellation,
&status,
(Some("routing".to_string()), auth),
(Some("routing".to_string()), auth.clone()),
)
.await;
turn_cancellation.cancel();
+4 -4
View File
@@ -3,6 +3,7 @@ pub(crate) mod startup;
use crate::PromptCacheKey;
use crate::WithTurnExtensionData;
use crate::runtime_auth::RuntimeAuthSlot;
use codex_login::AuthOwner;
use std::collections::BTreeMap;
use std::collections::HashMap;
use std::collections::HashSet;
@@ -477,11 +478,11 @@ pub(crate) enum ForkPersistence {
}
/// Normal appends keep this generation; compaction and rollback start a new one.
#[derive(Clone, Copy)]
#[derive(Clone)]
pub(crate) struct CompactionPrefix {
pub(crate) generation: u64,
pub(crate) len: usize,
pub(crate) auth_owner_generation: Option<u64>,
pub(crate) auth_owner: Option<AuthOwner>,
}
pub(crate) enum CompactedHistoryInstallation<'a> {
@@ -4778,8 +4779,7 @@ impl Session {
// Validate immediately before slicing and replacing the captured prefix.
if state.history.history_version() != prefix.generation
|| state.history.annotated_items().len() < prefix.len
|| self.services.model_client.auth_owner_generation()
!= prefix.auth_owner_generation
|| self.services.model_client.auth_owner() != prefix.auth_owner
{
return false;
}