Make subagents follow the root service tier (#41308)

## What changed

- Share the root thread's current service tier across its agent tree.
- Apply that tier to each subagent request when the selected child model supports it, including remote compaction requests.
- Propagate root service-tier updates to existing and newly spawned subagents without rewriting child-owned settings.
- Remove per-spawn and role-level service-tier overrides so routing remains controlled by the root thread.

## Testing

- Cover supported and unsupported child models, full-history forks, live tier changes, remote compaction, new subagents, and reloaded subagents.

GitOrigin-RevId: 6c299e224e90b41a558788fb7aa4d5f336fa383a
This commit is contained in:
jif authored and copyberry committed 2026-08-28 09:48:13 +00:00
1 parent 94311d4475
commit dc2ccc6843
19 files changed
+521 -274

No files matched your search

@@ -91,9 +91,6 @@ async fn handle_spawn_agent(
.await;
let mut config =
build_agent_spawn_config(&session.get_base_instructions().await, turn.as_ref())?;
if let Some(service_tier) = args.service_tier.as_ref() {
config.service_tier = Some(service_tier.clone());
}
if args.fork_context {
reject_full_fork_agent_type_override(role_name)?;
}
@@ -108,13 +105,7 @@ async fn handle_spawn_agent(
if !args.fork_context {
apply_spawn_agent_role(&session, &mut config, role_name).await?;
}
apply_spawn_agent_service_tier(
&session,
&mut config,
turn.config.service_tier.as_deref(),
args.service_tier.as_deref(),
)
.await?;
apply_spawn_agent_service_tier(&session, &mut config).await?;
apply_spawn_agent_runtime_overrides(&mut config, turn.as_ref())?;
let result = Box::pin(session.services.agent_control.spawn_agent_with_metadata(
@@ -237,7 +228,6 @@ struct SpawnAgentArgs {
agent_type: Option<String>,
model: Option<String>,
reasoning_effort: Option<ReasoningEffort>,
service_tier: Option<String>,
#[serde(default)]
fork_context: bool,
}
@@ -11,6 +11,7 @@ use crate::tools::context::ToolPayload;
use codex_models_manager::manager::RefreshStrategy;
use codex_protocol::AgentPath;
use codex_protocol::ThreadId;
use codex_protocol::config_types::SERVICE_TIER_DEFAULT_REQUEST_VALUE;
use codex_protocol::error::CodexErr;
use codex_protocol::error::CodexErrorDetails;
use codex_protocol::models::BaseInstructions;
@@ -321,17 +322,14 @@ pub(crate) async fn apply_requested_spawn_agent_model_overrides(
pub(crate) async fn apply_spawn_agent_service_tier(
session: &Session,
config: &mut Config,
parent_service_tier: Option<&str>,
requested_service_tier: Option<&str>,
) -> Result<(), FunctionCallError> {
let candidate_service_tiers = [
config.service_tier.clone(),
requested_service_tier.map(str::to_string),
parent_service_tier.map(str::to_string),
];
if candidate_service_tiers.iter().all(Option::is_none) {
let Some(service_tier) = session.services.agent_control.root_service_tier() else {
config.service_tier = None;
return Ok(());
};
if service_tier == SERVICE_TIER_DEFAULT_REQUEST_VALUE {
config.service_tier = Some(service_tier);
return Ok(());
}
let model = config.model.clone().ok_or_else(|| {
@@ -345,31 +343,9 @@ pub(crate) async fn apply_spawn_agent_service_tier(
.get_model_info(model.as_str(), &config.to_models_manager_config())
.await;
if let Some(requested_service_tier) = requested_service_tier
&& !model_info.supports_service_tier(requested_service_tier)
{
let supported_service_tiers = if model_info.service_tiers.is_empty() {
"none".to_string()
} else {
model_info
.service_tiers
.iter()
.map(|tier| tier.id.as_str())
.collect::<Vec<_>>()
.join(", ")
};
return Err(FunctionCallError::RespondToModel(format!(
"Service tier `{requested_service_tier}` is not supported for model `{model}`. Supported service tiers: {supported_service_tiers}"
)));
}
config.service_tier =
candidate_service_tiers
.into_iter()
.flatten()
.find(|candidate_service_tier| {
model_info.supports_service_tier(candidate_service_tier.as_str())
});
config.service_tier = model_info
.supports_service_tier(service_tier.as_str())
.then_some(service_tier);
Ok(())
}
@@ -18,8 +18,6 @@ const SPAWN_AGENT_INHERITED_MODEL_GUIDANCE: &str = "Spawned agents inherit your
const SPAWN_AGENT_TYPE_OVERRIDE_DESCRIPTION_V1: &str = "Agent type override for the new agent. Omit to inherit the parent agent type with a full-history fork; otherwise, `default` is used.";
const SPAWN_AGENT_MODEL_OVERRIDE_DESCRIPTION: &str =
"Model override for the new agent. Omit unless an explicit override is needed.";
const SPAWN_AGENT_SERVICE_TIER_OVERRIDE_DESCRIPTION: &str =
"Service tier override for the new agent. Omit unless explicitly requested.";
const MAX_REASONING_EFFORT_CHARS_IN_SPAWN_AGENT_DESCRIPTION: usize = 64;
#[derive(Debug, Clone)]
@@ -110,9 +108,6 @@ pub fn create_spawn_agent_tool_v2(options: SpawnAgentToolOptions) -> ToolSpec {
if !options.expose_agent_type {
properties.remove("agent_type");
}
if options.hide_agent_type_model_reasoning {
properties.remove("service_tier");
}
if !options.expose_spawn_agent_model_overrides {
properties.remove("model");
properties.remove("reasoning_effort");
@@ -619,12 +614,6 @@ fn spawn_agent_common_properties_v1(agent_type_description: &str) -> BTreeMap<St
.to_string(),
)),
),
(
"service_tier".to_string(),
JsonSchema::string(Some(
SPAWN_AGENT_SERVICE_TIER_OVERRIDE_DESCRIPTION.to_string(),
)),
),
])
}
@@ -663,12 +652,6 @@ fn spawn_agent_common_properties_v2(agent_type_description: &str) -> BTreeMap<St
.to_string(),
)),
),
(
"service_tier".to_string(),
JsonSchema::string(Some(
SPAWN_AGENT_SERVICE_TIER_OVERRIDE_DESCRIPTION.to_string(),
)),
),
])
}
@@ -676,7 +659,6 @@ fn hide_spawn_agent_metadata_options(properties: &mut BTreeMap<String, JsonSchem
properties.remove("agent_type");
properties.remove("model");
properties.remove("reasoning_effort");
properties.remove("service_tier");
}
fn spawn_agent_tool_description(
@@ -115,12 +115,7 @@ fn spawn_agent_tool_v2_requires_task_name_and_lists_visible_models() {
.and_then(|schema| schema.description.as_deref()),
Some("Reasoning effort override for the new agent. Omit to inherit the parent effort.")
);
assert_eq!(
properties
.get("service_tier")
.and_then(|schema| schema.description.as_deref()),
Some(SPAWN_AGENT_SERVICE_TIER_OVERRIDE_DESCRIPTION)
);
assert!(!properties.contains_key("service_tier"));
assert_eq!(
parameters.required.as_ref(),
Some(&vec!["task_name".to_string(), "message".to_string()])
@@ -181,12 +176,7 @@ fn spawn_agent_tool_v1_keeps_legacy_fork_context_field() {
.and_then(|schema| schema.description.as_deref()),
Some(SPAWN_AGENT_MODEL_OVERRIDE_DESCRIPTION)
);
assert_eq!(
properties
.get("service_tier")
.and_then(|schema| schema.description.as_deref()),
Some(SPAWN_AGENT_SERVICE_TIER_OVERRIDE_DESCRIPTION)
);
assert!(!properties.contains_key("service_tier"));
}
#[test]
@@ -407,106 +407,31 @@ async fn multi_agent_v2_spawn_fork_turns_all_applies_agent_type_override() {
}
#[tokio::test]
async fn spawn_agent_service_tier_override_validates_the_effective_child_model() {
#[derive(Debug, Deserialize)]
struct SpawnAgentResult {
agent_id: String,
}
async fn spawn_agent_service_tier_uses_root_preference_when_root_model_cannot_support_it() {
let (_session, turn) = make_session_and_context().await;
let mut config = (*turn.config).clone();
config.model = Some("gpt-5.4-mini".to_string());
config.service_tier = Some(ServiceTier::Fast.request_value().to_string());
let manager = thread_manager();
let root = manager
.start_thread(StartThreadOptions::new(config.clone()))
.await
.expect("root thread should start");
assert_eq!(root.thread.config_snapshot().await.service_tier, None);
{
let (mut session, turn) = make_session_and_context().await;
let manager = thread_manager();
let root = manager
.start_thread(StartThreadOptions::new((*turn.config).clone()))
.await
.expect("root thread should start");
session.services.agent_control = manager.agent_control();
session.thread_id = root.thread_id;
config.model = Some("gpt-5.4".to_string());
apply_spawn_agent_service_tier(root.thread.session.as_ref(), &mut config)
.await
.expect("root preference should be resolved against the child model");
let output = SpawnAgentHandler::default()
.handle(invocation(
Arc::new(session),
Arc::new(turn),
"spawn_agent",
function_payload(json!({
"message": "inspect this repo",
"model": "gpt-5.4",
"service_tier": ServiceTier::Fast.request_value()
})),
))
.await
.expect("spawn_agent should accept a supported explicit service tier");
let (content, _) = expect_text_output(output);
let result: SpawnAgentResult =
serde_json::from_str(&content).expect("spawn_agent result should be json");
let snapshot = manager
.get_thread(parse_agent_id(&result.agent_id))
.await
.expect("spawned agent thread should exist")
.config_snapshot()
.await;
assert_eq!(
snapshot.service_tier,
Some(ServiceTier::Fast.request_value().to_string())
);
}
{
let (session, turn) = make_session_and_context().await;
let err = SpawnAgentHandler::default()
.handle(invocation(
Arc::new(session),
Arc::new(turn),
"spawn_agent",
function_payload(json!({
"message": "inspect this repo",
"model": "gpt-5.4",
"service_tier": "turbo"
})),
))
.await
.err()
.expect("unknown service tier should be rejected");
assert_eq!(
err,
FunctionCallError::RespondToModel(
"Service tier `turbo` is not supported for model `gpt-5.4`. Supported service tiers: priority"
.to_string()
)
);
}
{
let (session, turn) = make_session_and_context().await;
let err = SpawnAgentHandler::default()
.handle(invocation(
Arc::new(session),
Arc::new(turn),
"spawn_agent",
function_payload(json!({
"message": "inspect this repo",
"model": "gpt-5.4-mini",
"service_tier": ServiceTier::Fast.request_value()
})),
))
.await
.err()
.expect("tier unsupported by the final child model should be rejected");
assert_eq!(
err,
FunctionCallError::RespondToModel(
"Service tier `priority` is not supported for model `gpt-5.4-mini`. Supported service tiers: none"
.to_string()
)
);
}
assert_eq!(
config.service_tier,
Some(ServiceTier::Fast.request_value().to_string())
);
}
#[tokio::test]
async fn spawn_agent_service_tier_inheritance_preserves_supported_or_configured_tiers() {
async fn spawn_agent_service_tier_inheritance_uses_root_preference_and_child_model() {
#[derive(Debug, Deserialize)]
struct SpawnAgentResult {
agent_id: String,
@@ -525,7 +450,7 @@ async fn spawn_agent_service_tier_inheritance_preserves_supported_or_configured_
.start_thread(StartThreadOptions::new((*turn.config).clone()))
.await
.expect("root thread should start");
session.services.agent_control = manager.agent_control();
session.services.agent_control = root.thread.session.services.agent_control.clone();
session.thread_id = root.thread_id;
let output = SpawnAgentHandler::default()
@@ -566,7 +491,7 @@ async fn spawn_agent_service_tier_inheritance_preserves_supported_or_configured_
.start_thread(StartThreadOptions::new((*turn.config).clone()))
.await
.expect("root thread should start");
session.services.agent_control = manager.agent_control();
session.services.agent_control = root.thread.session.services.agent_control.clone();
session.thread_id = root.thread_id;
let output = SpawnAgentHandler::default()
@@ -643,7 +568,7 @@ service_tier = "priority"
})),
))
.await
.expect("spawn_agent should preserve the child role service tier");
.expect("spawn_agent should ignore the child role service tier");
let (content, _) = expect_text_output(output);
let result: SpawnAgentResult =
serde_json::from_str(&content).expect("spawn_agent result should be json");
@@ -654,15 +579,12 @@ service_tier = "priority"
.config_snapshot()
.await;
assert_eq!(
snapshot.service_tier,
Some(ServiceTier::Fast.request_value().to_string())
);
assert_eq!(snapshot.service_tier, None);
}
}
#[tokio::test]
async fn spawn_agent_role_service_tier_falls_back_to_supported_parent_tier() {
async fn spawn_agent_role_service_tier_cannot_override_root_preference() {
#[derive(Debug, Deserialize)]
struct SpawnAgentResult {
agent_id: String,
@@ -702,7 +624,7 @@ service_tier = "turbo"
.start_thread(StartThreadOptions::new((*turn.config).clone()))
.await
.expect("root thread should start");
session.services.agent_control = manager.agent_control();
session.services.agent_control = root.thread.session.services.agent_control.clone();
session.thread_id = root.thread_id;
let output = SpawnAgentHandler::default()
@@ -734,72 +656,25 @@ service_tier = "turbo"
}
#[tokio::test]
async fn spawn_agent_role_service_tier_does_not_hide_invalid_spawn_request() {
let (session, mut turn) = make_session_and_context().await;
tokio::fs::create_dir_all(&turn.config.codex_home)
.await
.expect("codex home should be created");
let role_config_path = turn.config.codex_home.as_path().join("tiered-role.toml");
tokio::fs::write(
&role_config_path,
r#"model = "gpt-5.4"
service_tier = "priority"
"#,
)
.await
.expect("role config should be written");
let role_name = "tiered-role".to_string();
let mut config = (*turn.config).clone();
config.agent_roles.insert(
role_name.clone(),
AgentRoleConfig {
description: Some("Role with a supported child tier".to_string()),
config_file: Some(role_config_path),
nickname_candidates: None,
},
);
turn.config = Arc::new(config);
let result = SpawnAgentHandler::default()
.handle(invocation(
Arc::new(session),
Arc::new(turn),
"spawn_agent",
function_payload(json!({
"message": "inspect this repo",
"agent_type": role_name,
"service_tier": "turbo"
})),
))
.await;
assert_eq!(
result.err(),
Some(FunctionCallError::RespondToModel(
"Service tier `turbo` is not supported for model `gpt-5.4`. Supported service tiers: priority"
.to_string()
))
);
}
#[tokio::test]
async fn spawn_agent_full_history_fork_accepts_explicit_service_tier() {
async fn spawn_agent_full_history_fork_inherits_root_service_tier() {
#[derive(Debug, Deserialize)]
struct SpawnAgentResult {
agent_id: String,
}
let (mut session, turn) = make_session_and_context().await;
let turn = turn
let mut turn = turn
.with_model("gpt-5.4".to_string(), &session.services.models_manager)
.await;
let mut config = (*turn.config).clone();
config.service_tier = Some(ServiceTier::Fast.request_value().to_string());
turn.config = Arc::new(config);
let manager = thread_manager();
let root = manager
.start_thread(StartThreadOptions::new((*turn.config).clone()))
.await
.expect("root thread should start");
session.services.agent_control = manager.agent_control();
session.services.agent_control = root.thread.session.services.agent_control.clone();
session.thread_id = root.thread_id;
let output = SpawnAgentHandler::default()
@@ -809,12 +684,11 @@ async fn spawn_agent_full_history_fork_accepts_explicit_service_tier() {
"spawn_agent",
function_payload(json!({
"message": "inspect this repo",
"fork_context": true,
"service_tier": ServiceTier::Fast.request_value()
"fork_context": true
})),
))
.await
.expect("full-history fork should accept explicit service tier");
.expect("full-history fork should inherit the root service tier");
let (content, _) = expect_text_output(output);
let result: SpawnAgentResult =
serde_json::from_str(&content).expect("spawn_agent result should be json");
@@ -832,7 +706,7 @@ async fn spawn_agent_full_history_fork_accepts_explicit_service_tier() {
}
#[tokio::test]
async fn multi_agent_v2_full_history_fork_accepts_explicit_service_tier() {
async fn multi_agent_v2_full_history_fork_inherits_root_service_tier() {
#[derive(Debug, Deserialize)]
struct SpawnAgentResult {
task_name: String,
@@ -843,6 +717,7 @@ async fn multi_agent_v2_full_history_fork_accepts_explicit_service_tier() {
.with_model("gpt-5.4".to_string(), &session.services.models_manager)
.await;
let mut config = (*turn.config).clone();
config.service_tier = Some(ServiceTier::Fast.request_value().to_string());
config
.features
.enable(Feature::MultiAgentV2)
@@ -853,7 +728,7 @@ async fn multi_agent_v2_full_history_fork_accepts_explicit_service_tier() {
.start_thread(StartThreadOptions::new((*turn.config).clone()))
.await
.expect("root thread should start");
session.services.agent_control = manager.agent_control();
session.services.agent_control = root.thread.session.services.agent_control.clone();
session.thread_id = root.thread_id;
let session = Arc::new(session);
let turn = Arc::new(turn);
@@ -865,12 +740,11 @@ async fn multi_agent_v2_full_history_fork_accepts_explicit_service_tier() {
"spawn_agent",
function_payload(json!({
"message": "inspect this repo",
"task_name": "fork_with_tier",
"service_tier": ServiceTier::Fast.request_value()
"task_name": "fork_with_tier"
})),
))
.await
.expect("multi-agent v2 full-history fork should accept explicit service tier");
.expect("multi-agent v2 full-history fork should inherit the root service tier");
let (content, _) = expect_text_output(output);
let result: SpawnAgentResult =
serde_json::from_str(&content).expect("spawn_agent result should be json");
@@ -124,9 +124,6 @@ async fn handle_spawn_agent(
let child_depth = next_thread_spawn_depth(&session_source);
let mut config =
build_agent_spawn_config(&session.get_base_instructions().await, turn.as_ref())?;
if let Some(service_tier) = args.service_tier.as_ref() {
config.service_tier = Some(service_tier.clone());
}
let is_full_history_fork = matches!(fork_mode, Some(SpawnAgentForkMode::FullHistory));
apply_requested_spawn_agent_model_overrides(
&session,
@@ -144,13 +141,7 @@ async fn handle_spawn_agent(
.clone_from(&turn.developer_instructions);
}
}
apply_spawn_agent_service_tier(
&session,
&mut config,
turn.config.service_tier.as_deref(),
args.service_tier.as_deref(),
)
.await?;
apply_spawn_agent_service_tier(&session, &mut config).await?;
apply_spawn_agent_runtime_overrides(&mut config, turn.as_ref())?;
// Remember an applied configured default so cold reload reapplies its restrictions.
@@ -288,7 +279,6 @@ struct SpawnAgentArgs {
agent_type: Option<String>,
model: Option<String>,
reasoning_effort: Option<ReasoningEffort>,
service_tier: Option<String>,
fork_turns: Option<String>,
fork_context: Option<bool>,
}