Track remaining multi-agent v2 tools in analytics (#40585)

## What changed

- Record collaboration tool analytics for `send_message`, `followup_task`,
  `interrupt_agent`, and `list_agents`, including receiver attribution, duration,
  failures, and interrupted calls.
- Include these calls in per-turn subagent tool counts while keeping the existing
  `subAgentActivity` output unchanged and omitting additional public collaborator
  tool items.
- Extend the collaboration tool schemas with the new tool variants and the
  `interrupted` terminal status.

## Testing

- Add an app-server integration test covering successful and failed calls,
  receiver attribution, duration, prompt omission, activity items, and turn
  counts.

GitOrigin-RevId: e73eaa05c508261e5fc97e7d08d7881a31e04e22
This commit is contained in:
jif authored and copyberry committed 2026-08-25 10:17:03 +00:00
1 parent d52478c52e
commit 4fa6ad1730
38 files changed
+472 -45

No files matched your search

@@ -36,6 +36,7 @@ pub(crate) use send_message::Handler as SendMessageHandler;
pub(crate) use spawn::Handler as SpawnAgentHandler;
pub(crate) use wait::Handler as WaitAgentHandler;
mod analytics;
mod followup_task;
mod interrupt_agent;
mod list_agents;
@@ -0,0 +1,62 @@
use codex_analytics::AnalyticsEventsClient;
use codex_protocol::ThreadId;
use codex_protocol::items::CollabAgentTool;
use codex_protocol::items::CollabAgentToolCallItem;
use codex_protocol::items::CollabAgentToolCallStatus;
use crate::tools::context::ToolInvocation;
use crate::turn_timing::now_unix_timestamp_ms;
/// Records private collaborator analytics without emitting another public tool item.
pub(super) struct ToolCallAnalytics {
client: AnalyticsEventsClient,
turn_id: String,
item: CollabAgentToolCallItem,
started_at_ms: i64,
}
impl ToolCallAnalytics {
pub(super) fn new(invocation: &ToolInvocation, tool: CollabAgentTool) -> Self {
Self {
client: invocation.session.services.analytics_events_client.clone(),
turn_id: invocation.turn.sub_id.clone(),
item: CollabAgentToolCallItem {
// Activity and analytics must use the same ID for turn-count deduplication.
id: invocation.call_id.clone(),
tool,
status: CollabAgentToolCallStatus::Interrupted,
sender_thread_id: invocation.session.thread_id,
receiver_thread_ids: Vec::new(),
receiver_agents: Vec::new(),
prompt: None,
model: None,
reasoning_effort: None,
agents_states: Default::default(),
},
started_at_ms: now_unix_timestamp_ms(),
}
}
pub(super) fn set_receiver(&mut self, thread_id: ThreadId) {
self.item.receiver_thread_ids = vec![thread_id];
}
pub(super) fn finish<T, E>(mut self, result: &Result<T, E>) {
self.item.status = if result.is_ok() {
CollabAgentToolCallStatus::Completed
} else {
CollabAgentToolCallStatus::Failed
};
}
}
impl Drop for ToolCallAnalytics {
fn drop(&mut self) {
self.client.track_collab_tool_call(
self.turn_id.clone(),
self.item.clone(),
self.started_at_ms,
now_unix_timestamp_ms(),
);
}
}
@@ -1,3 +1,4 @@
use super::analytics::ToolCallAnalytics;
use super::message_tool::FollowupTaskArgs;
use super::message_tool::MessageDeliveryMode;
use super::message_tool::handle_message_string_tool;
@@ -17,7 +18,12 @@ impl ToolExecutor<ToolInvocation> for Handler {
}
fn handle(&self, invocation: ToolInvocation) -> codex_tools::ToolExecutorFuture<'_> {
Box::pin(self.handle_call(invocation))
Box::pin(async move {
let mut analytics = ToolCallAnalytics::new(&invocation, CollabAgentTool::FollowupTask);
let result = self.handle_call(invocation, &mut analytics).await;
analytics.finish(&result);
result
})
}
}
@@ -25,6 +31,7 @@ impl Handler {
async fn handle_call(
&self,
invocation: ToolInvocation,
analytics: &mut ToolCallAnalytics,
) -> Result<Box<dyn crate::tools::context::ToolOutput>, FunctionCallError> {
let arguments = function_arguments(invocation.payload.clone())?;
let args: FollowupTaskArgs = parse_arguments(&arguments)?;
@@ -33,6 +40,7 @@ impl Handler {
MessageDeliveryMode::TriggerTurn,
args.target,
args.message,
analytics,
)
.await
.map(boxed_tool_output)
@@ -1,3 +1,4 @@
use super::analytics::ToolCallAnalytics;
use super::*;
use crate::tools::handlers::multi_agents_spec::create_interrupt_agent_tool_v2;
use codex_protocol::error::CodexErrorDetails;
@@ -16,15 +17,18 @@ impl ToolExecutor<ToolInvocation> for Handler {
fn handle(&self, invocation: ToolInvocation) -> codex_tools::ToolExecutorFuture<'_> {
Box::pin(async move {
handle_interrupt_agent(invocation)
.await
.map(boxed_tool_output)
let mut analytics =
ToolCallAnalytics::new(&invocation, CollabAgentTool::InterruptAgent);
let result = handle_interrupt_agent(invocation, &mut analytics).await;
analytics.finish(&result);
result.map(boxed_tool_output)
})
}
}
async fn handle_interrupt_agent(
invocation: ToolInvocation,
analytics: &mut ToolCallAnalytics,
) -> Result<InterruptAgentResult, FunctionCallError> {
let ToolInvocation {
session,
@@ -36,6 +40,7 @@ async fn handle_interrupt_agent(
let arguments = function_arguments(payload)?;
let args: InterruptAgentArgs = parse_arguments(&arguments)?;
let agent_id = resolve_agent_target(&session, &turn, &args.target).await?;
analytics.set_receiver(agent_id);
let receiver_agent = session
.services
.agent_control
@@ -1,3 +1,4 @@
use super::analytics::ToolCallAnalytics;
use super::*;
use crate::agent::control::ListedAgent;
use crate::tools::handlers::multi_agents_spec::create_list_agents_tool;
@@ -15,7 +16,12 @@ impl ToolExecutor<ToolInvocation> for Handler {
}
fn handle(&self, invocation: ToolInvocation) -> codex_tools::ToolExecutorFuture<'_> {
Box::pin(self.handle_call(invocation))
Box::pin(async move {
let analytics = ToolCallAnalytics::new(&invocation, CollabAgentTool::ListAgents);
let result = self.handle_call(invocation).await;
analytics.finish(&result);
result
})
}
}
@@ -3,6 +3,7 @@
//! `send_message` and `followup_task` share the same submission path and differ only in whether the
//! resulting `InterAgentCommunication` should wake the target immediately.
use super::analytics::ToolCallAnalytics;
use super::*;
use crate::agent_communication::AgentCommunicationContext;
use crate::agent_communication::AgentCommunicationKind;
@@ -49,11 +50,12 @@ pub(super) fn message_content(message: String) -> Result<String, FunctionCallErr
}
/// Handles the shared MultiAgentV2 message flow for both `send_message` and `followup_task`.
pub(crate) async fn handle_message_string_tool(
pub(super) async fn handle_message_string_tool(
invocation: ToolInvocation,
mode: MessageDeliveryMode,
target: String,
message: String,
analytics: &mut ToolCallAnalytics,
) -> Result<FunctionToolOutput, FunctionCallError> {
let message = message_content(message)?;
let ToolInvocation {
@@ -64,6 +66,7 @@ pub(crate) async fn handle_message_string_tool(
..
} = invocation;
let receiver_thread_id = resolve_agent_target(&session, &turn, &target).await?;
analytics.set_receiver(receiver_thread_id);
let receiver_agent = session
.services
.agent_control
@@ -1,3 +1,4 @@
use super::analytics::ToolCallAnalytics;
use super::message_tool::MessageDeliveryMode;
use super::message_tool::SendMessageArgs;
use super::message_tool::handle_message_string_tool;
@@ -17,7 +18,12 @@ impl ToolExecutor<ToolInvocation> for Handler {
}
fn handle(&self, invocation: ToolInvocation) -> codex_tools::ToolExecutorFuture<'_> {
Box::pin(self.handle_call(invocation))
Box::pin(async move {
let mut analytics = ToolCallAnalytics::new(&invocation, CollabAgentTool::SendMessage);
let result = self.handle_call(invocation, &mut analytics).await;
analytics.finish(&result);
result
})
}
}
@@ -25,6 +31,7 @@ impl Handler {
async fn handle_call(
&self,
invocation: ToolInvocation,
analytics: &mut ToolCallAnalytics,
) -> Result<Box<dyn crate::tools::context::ToolOutput>, FunctionCallError> {
let arguments = function_arguments(invocation.payload.clone())?;
let args: SendMessageArgs = parse_arguments(&arguments)?;
@@ -33,6 +40,7 @@ impl Handler {
MessageDeliveryMode::QueueOnly,
args.target,
args.message,
analytics,
)
.await
.map(boxed_tool_output)