Route multi-agent spawning through SpawnRequest (#47235)

## What changed

- Route both multi-agent spawn handlers through `LocalAgentControl::spawn`, accepting user input or an agent message.
- Centralize spawn message attribution and canonical path validation, and require message input to use `TriggerTurn`.
- Return the child's configuration snapshot alongside `LiveAgent`, so handlers obtain effective settings directly from spawning without a separate lookup.
- Update child test fixtures to use the shared spawn entry point.

GitOrigin-RevId: 9f9278df08041a62215f9ec3d38688c78fc5e0e2
This commit is contained in:
jif
2026-09-22 09:32:22 +00:00
committed by copyberry
parent 94174e44cb
commit f908e5a968
8 changed files with 178 additions and 177 deletions
+9 -40
View File
@@ -8,6 +8,7 @@ use crate::agent::types::SpawnAgentForkMode;
use crate::agent::types::SpawnAgentOptions;
use crate::agents_md_manager::SessionInstructions;
use crate::codex_thread::CodexThread;
use crate::codex_thread::ThreadConfigSnapshot;
use crate::config::PermissionProfileSnapshot;
use crate::context::ContextualUserFragment;
use crate::context::CurrentTimeReminder;
@@ -42,7 +43,7 @@ struct SpawnAgentThreadInheritance {
/// provide user input directly, making an uncontextualized inter-agent communication
/// unrepresentable.
#[allow(clippy::large_enum_variant)]
enum SpawnInitialInput {
pub(super) enum SpawnInitialInput {
UserInput(Vec<UserInput>),
InterAgentCommunication(InterAgentCommunication, AgentCommunicationContext),
}
@@ -257,7 +258,7 @@ impl LocalAgentControl {
initial_input: Vec<UserInput>,
session_source: Option<SessionSource>,
) -> CodexResult<ThreadId> {
let spawned_agent = Box::pin(self.spawn_agent_internal(
let (spawned_agent, _) = Box::pin(self.spawn_agent_internal(
config,
SpawnInitialInput::UserInput(initial_input),
session_source,
@@ -267,40 +268,6 @@ impl LocalAgentControl {
Ok(spawned_agent.thread_id)
}
/// Spawn an agent thread with some metadata.
pub(crate) async fn spawn_agent_with_metadata(
&self,
config: Config,
initial_input: Vec<UserInput>,
session_source: Option<SessionSource>,
options: SpawnAgentOptions, // TODO(jif) drop with new fork.
) -> CodexResult<LiveAgent> {
Box::pin(self.spawn_agent_internal(
config,
SpawnInitialInput::UserInput(initial_input),
session_source,
options,
))
.await
}
pub(crate) async fn spawn_agent_with_communication(
&self,
config: Config,
communication: InterAgentCommunication,
context: AgentCommunicationContext,
session_source: Option<SessionSource>,
options: SpawnAgentOptions,
) -> CodexResult<LiveAgent> {
Box::pin(self.spawn_agent_internal(
config,
SpawnInitialInput::InterAgentCommunication(communication, context),
session_source,
options,
))
.await
}
fn validate_loaded_v2_child(
&self,
thread: &CodexThread,
@@ -629,13 +596,13 @@ impl LocalAgentControl {
}
}
async fn spawn_agent_internal(
pub(super) async fn spawn_agent_internal(
&self,
config: Config,
initial_input: SpawnInitialInput,
session_source: Option<SessionSource>,
options: SpawnAgentOptions,
) -> CodexResult<LiveAgent> {
) -> CodexResult<(LiveAgent, ThreadConfigSnapshot)> {
let state = self.upgrade()?;
let multi_agent_version = state
.effective_multi_agent_version_for_spawn(
@@ -829,11 +796,13 @@ impl LocalAgentControl {
);
}
Ok(LiveAgent {
let agent = LiveAgent {
thread_id: new_thread.thread_id,
metadata: agent_metadata,
status: self.get_status(new_thread.thread_id).await,
})
};
let config = new_thread.thread.config_snapshot().await;
Ok((agent, config))
}
async fn spawn_forked_thread(
@@ -0,0 +1,57 @@
//! Converts captured spawn input into the existing local startup operation.
//! Agent messages keep their canonical paths and spawn attribution.
use super::LocalAgentControl;
use super::spawn::SpawnInitialInput;
use crate::agent::api::AgentInput;
use crate::agent::api::SpawnRequest;
use crate::agent::types::LiveAgent;
use crate::agent::types::MessageDeliveryMode;
use crate::agent_communication::AgentCommunicationContext;
use crate::agent_communication::AgentCommunicationKind;
use crate::codex_thread::ThreadConfigSnapshot;
use codex_protocol::AgentPath;
use codex_protocol::error::CodexErr;
use codex_protocol::error::Result as CodexResult;
impl LocalAgentControl {
pub(crate) async fn spawn(
&self,
request: SpawnRequest,
) -> CodexResult<(LiveAgent, ThreadConfigSnapshot)> {
let SpawnRequest {
caller,
config,
input,
source,
options,
} = request;
let input = match input {
AgentInput::UserInput(input) => SpawnInitialInput::UserInput(input),
AgentInput::Message { message, mode } => {
if mode != MessageDeliveryMode::TriggerTurn {
return Err(CodexErr::InvalidRequest(
"spawn input must start the child turn".to_string(),
));
}
let recipient = source.get_agent_path().ok_or_else(|| {
CodexErr::InvalidRequest(
"spawned agent is missing a canonical task name".to_string(),
)
})?;
let author = recipient
.as_str()
.rsplit_once('/')
.and_then(|(parent, _)| AgentPath::try_from(parent).ok())
.ok_or_else(|| {
CodexErr::InvalidRequest("spawn input needs a child path".to_string())
})?;
SpawnInitialInput::InterAgentCommunication(
message.into_communication(author, recipient, mode),
AgentCommunicationContext::new(AgentCommunicationKind::Spawn, caller),
)
}
};
Box::pin(self.spawn_agent_internal(config, input, Some(source), options)).await
}
}