Add spawn context for MultiAgentV2 children (#16746)

This commit is contained in:
Ahmed Ibrahim
2026-04-03 19:56:59 -07:00
committed by GitHub
Unverified
parent 6edb865cc6
commit 8a19dbb177
2 changed files with 110 additions and 0 deletions
@@ -6,11 +6,16 @@ use crate::agent::next_thread_spawn_depth;
use crate::agent::role::DEFAULT_ROLE_NAME;
use crate::agent::role::apply_role_to_config;
use codex_protocol::AgentPath;
use codex_protocol::models::DeveloperInstructions;
use codex_protocol::protocol::InterAgentCommunication;
use codex_protocol::protocol::Op;
pub(crate) struct Handler;
pub(crate) const SPAWN_AGENT_DEVELOPER_INSTRUCTIONS: &str = r#"<spawned_agent_context>
You are a newly spawned agent in a team of agents collaborating to complete a task. You can spawn sub-agents to handle subtasks, and those sub-agents can spawn their own sub-agents. You are responsible for returning the response to your assigned task in the final channel. When you give your response, the contents of your response in the final channel will be immediately delivered back to your parent agent. The prior conversation history was forked from your parent agent. Treat the next user message as your assigned task, and use the forked history only as background context.
</spawned_agent_context>"#;
impl ToolHandler for Handler {
type Output = SpawnAgentResult;
@@ -78,6 +83,17 @@ impl ToolHandler for Handler {
.map_err(FunctionCallError::RespondToModel)?;
apply_spawn_agent_runtime_overrides(&mut config, turn.as_ref())?;
apply_spawn_agent_overrides(&mut config, child_depth);
config.developer_instructions = Some(
if let Some(existing_instructions) = config.developer_instructions.take() {
DeveloperInstructions::new(existing_instructions)
.concat(DeveloperInstructions::new(
SPAWN_AGENT_DEVELOPER_INSTRUCTIONS,
))
.into_text()
} else {
DeveloperInstructions::new(SPAWN_AGENT_DEVELOPER_INSTRUCTIONS).into_text()
},
);
let spawn_source = thread_spawn_source(
session.conversation_id,