diff --git a/codex-rs/core/src/agent/control.rs b/codex-rs/core/src/agent/control.rs index 49bbab0f87..d44154ecd1 100644 --- a/codex-rs/core/src/agent/control.rs +++ b/codex-rs/core/src/agent/control.rs @@ -183,12 +183,31 @@ impl AgentControl { agent_id: ThreadId, communication: InterAgentCommunication, ) -> CodexResult { - let last_task_message = last_task_message_from_communication(&communication); let state = self.upgrade()?; let op = Op::InterAgentCommunication { communication }; self.ensure_execution_capacity_for_op(agent_id, &op).await?; + let Op::InterAgentCommunication { communication } = op else { + unreachable!("constructed as an inter-agent communication operation"); + }; + self.send_inter_agent_communication_after_capacity_check(agent_id, &state, communication) + .await + } + + async fn send_inter_agent_communication_after_capacity_check( + &self, + agent_id: ThreadId, + state: &Arc, + communication: InterAgentCommunication, + ) -> CodexResult { + let last_task_message = last_task_message_from_communication(&communication); let result = self - .handle_thread_request_result(agent_id, &state, state.send_op(agent_id, op).await) + .handle_thread_request_result( + agent_id, + state, + state + .send_op(agent_id, Op::InterAgentCommunication { communication }) + .await, + ) .await; if result.is_ok() { match last_task_message { diff --git a/codex-rs/core/src/agent/control/spawn.rs b/codex-rs/core/src/agent/control/spawn.rs index c4c40577ca..3e5d3cfce1 100644 --- a/codex-rs/core/src/agent/control/spawn.rs +++ b/codex-rs/core/src/agent/control/spawn.rs @@ -356,8 +356,24 @@ impl AgentControl { ) .await; - self.send_input_after_capacity_check(new_thread.thread_id, &state, initial_operation) - .await?; + match (multi_agent_version, initial_operation) { + (MultiAgentVersion::V2, Op::InterAgentCommunication { communication }) => { + self.send_inter_agent_communication_after_capacity_check( + new_thread.thread_id, + &state, + communication, + ) + .await?; + } + (_, initial_operation) => { + self.send_input_after_capacity_check( + new_thread.thread_id, + &state, + initial_operation, + ) + .await?; + } + } if multi_agent_version != MultiAgentVersion::V2 { let child_reference = agent_metadata .agent_path