Add shared service-tier support for host-owned agents (#59770)

## What changed

- Expose an optional `AgentServiceTier` provider so host-owned children, including detached children, inherit the root's committed tier during spawn preparation, startup prewarm, and each inference step.
- Reject per-turn tier overrides for children using the shared provider. Root updates apply to subsequent requests without changing requests already in flight.
- Preserve independent configured tiers and role overrides when no provider is installed, including across eviction and reload. Spawned children inherit the parent's thread preference rather than its per-turn override.
- Publish the latest committed root tier only after successful runtime registration, preventing failed duplicate starts from changing shared settings.

## Testing

Add coverage for detached-child WebSocket prewarm and between-step tier changes, shared-child override rejection, independent-tier inheritance and reload, and settings updates before registration.

GitOrigin-RevId: f0f3676813a1c59bd77baf95121e2d13e0163a8e
This commit is contained in:
rhan-oai
2026-11-06 00:24:27 +00:00
committed by copyberry
parent 74417e9c6b
commit 94ca55a93c
18 changed files with 517 additions and 47 deletions
+38 -7
View File
@@ -204,6 +204,7 @@ use futures::prelude::*;
use rmcp::model::RequestId;
use serde_json::Value;
use tokio::sync::Mutex;
use tokio::sync::MutexGuard;
use tokio::sync::RwLock;
use tokio::sync::oneshot;
use tokio::sync::watch;
@@ -2206,8 +2207,9 @@ impl Session {
.update_configured_selections(&updated.environments);
}
state.session_configuration = updated;
if root_service_tier_changed {
self.services.agent_control.propagate_config_update(
if root_service_tier_changed && !state.shared_service_tier_pending {
crate::agent::host::propagate_config_update(
self,
AgentConfigUpdate::ServiceTier(
state
.session_configuration
@@ -2312,6 +2314,25 @@ impl Session {
.thread_settings_snapshot(&state.session_configuration.environments)
}
/// Keep this guard through registration and publication to order the initial tier
/// with settings commits from the already-running submission loop.
pub(crate) async fn lock_pending_service_tier(
&self,
) -> Option<(MutexGuard<'_, SessionState>, AgentConfigUpdate)> {
let state = self.state.lock().await;
if !state.shared_service_tier_pending {
return None;
}
let update = AgentConfigUpdate::ServiceTier(
state
.session_configuration
.step_settings
.service_tier
.clone(),
);
Some((state, update))
}
pub(crate) async fn restorable_thread_settings(&self) -> CodexThreadSettingsOverrides {
let state = self.state.lock().await;
state
@@ -4248,6 +4269,18 @@ impl Session {
.await
}
fn inherits_root_service_tier(&self, session_source: &SessionSource) -> bool {
match &self.agent_runtime {
crate::agent::host::AgentRuntimeServices::Controller => matches!(
session_source,
SessionSource::SubAgent(SubAgentSource::ThreadSpawn { .. })
),
crate::agent::host::AgentRuntimeServices::Host(_) => {
crate::agent::host::host_child_uses_shared_service_tier(self)
}
}
}
#[expect(
clippy::await_holding_invalid_type,
reason = "step settings and both environment snapshots must be captured under active_turn then state"
@@ -4278,11 +4311,9 @@ impl Session {
.then(|| self.services.turn_environments.configured_snapshot_now()),
)
};
if matches!(
turn_context.session_source,
SessionSource::SubAgent(SubAgentSource::ThreadSpawn { .. })
) {
let root_service_tier = self.services.agent_control.service_tier();
if self.inherits_root_service_tier(&turn_context.session_source) {
let root_service_tier =
crate::agent::host::service_tier(self, settings.selected().service_tier.clone());
if settings.selected().service_tier != root_service_tier {
let mut selected = settings.selected().clone();
selected.service_tier = root_service_tier;
+9
View File
@@ -1092,6 +1092,8 @@ impl Session {
}
}
}
let has_provided_agent_control =
matches!(&agent_control, AgentControlInit::Provided { .. });
let (agent_control, local_agent_runtime): (Arc<dyn AgentControl>, _) = match agent_control {
AgentControlInit::Local(control) => {
let control = control.with_session_id(
@@ -1878,6 +1880,13 @@ impl Session {
// This placeholder is used only for teardown, before the session is published.
Err(err) => (AgentRuntimeServices::Controller, Some(err)),
};
state.shared_service_tier_pending = parent_thread_id.is_none()
&& match &agent_runtime {
AgentRuntimeServices::Controller => has_provided_agent_control,
AgentRuntimeServices::Host(services) => {
session_id == SessionId::from(thread_id) && services.service_tier.is_some()
}
};
let executed_tool_calls =
crate::state::ExecutedToolCalls::new(&config.features, &initial_history);
let codex_responses_headers =
+7 -7
View File
@@ -29,8 +29,6 @@ use codex_otel::STARTUP_PREWARM_DURATION_METRIC;
use codex_otel::SessionTelemetry;
use codex_protocol::error::Result as CodexResult;
use codex_protocol::models::BaseInstructions;
use codex_protocol::protocol::SessionSource;
use codex_protocol::protocol::SubAgentSource;
#[derive(Clone, Copy, Debug)]
pub(crate) enum PrewarmInput {
@@ -342,12 +340,14 @@ async fn schedule_startup_prewarm_inner(
let preconnect_model_info = Arc::clone(startup_turn_context.model_info());
// Spawned subagents inherit the root's selection, with the same feature and model filtering
// that capture applies to the actual request.
let preconnect_service_tier = if matches!(
startup_turn_context.session_source,
SessionSource::SubAgent(SubAgentSource::ThreadSpawn { .. })
) {
let inherits_root_service_tier =
session.inherits_root_service_tier(&startup_turn_context.session_source);
let preconnect_service_tier = if inherits_root_service_tier {
crate::session::get_service_tier(
session.services.agent_control.service_tier(),
crate::agent::host::service_tier(
&session,
startup_turn_context.initial_settings.service_tier.clone(),
),
&session.features(),
&preconnect_model_info,
)
+8
View File
@@ -245,6 +245,14 @@ pub(super) async fn handle(
submission_id: String,
) -> CodexResult<TurnInputSubmission> {
let request = request.into();
if request.request.start.service_tier.is_some()
&& crate::agent::host::host_child_uses_shared_service_tier(session)
{
return Err(CodexErr::InvalidRequest(
"per-turn service tier overrides are unavailable for a child using the shared service tier"
.into(),
));
}
if let SubmittedTurnInput::ResponseItem(item)
| SubmittedTurnInput::AnnotatedResponseItem(AnnotatedResponseItem { item, .. }) =
&request.request.input