Skip to main content

base/telemetry/
team.rs

1//! Team / multi-agent events.
2
3use serde::Serialize;
4
5/// A sub-agent started: one the `Agent` tool runs, or a team member.
6#[derive(Debug, Clone, Serialize, schemars::JsonSchema)]
7pub struct AgentSpawnedPayload {
8    /// The sub-agent's label (`<type>#<id>`) or the team member's label.
9    pub agent_id: String,
10    /// Its agent type; `default` when none was asked for.
11    pub role: String,
12    /// The model it runs on, when the spawn site knows it.
13    #[serde(skip_serializing_if = "Option::is_none")]
14    pub model: Option<String>,
15    /// The team stage it belongs to; `None` outside a team.
16    #[serde(skip_serializing_if = "Option::is_none")]
17    pub stage_name: Option<String>,
18    /// Whether it runs in the background (`run_in_background`).
19    pub background: bool,
20    /// The parent's tool call that started it, when one did.
21    #[serde(skip_serializing_if = "Option::is_none")]
22    pub parent_tool_use_id: Option<String>,
23}
24
25/// A sub-agent finished.
26#[derive(Debug, Clone, Serialize, schemars::JsonSchema)]
27pub struct AgentCompletedPayload {
28    pub agent_id: String,
29    pub role: String,
30    pub had_error: bool,
31    pub duration_ms: u64,
32}
33
34/// Team 流水线阶段完成事件的载荷。
35#[derive(Debug, Clone, Serialize, schemars::JsonSchema)]
36pub struct TeamStageCompletePayload {
37    pub stage_name: String,
38    pub agent_count: usize,
39    pub duration_ms: u64,
40    pub success: bool,
41    pub agents_with_errors: usize,
42}
43
44#[cfg(test)]
45mod tests {
46    use super::*;
47    use crate::telemetry::TelemetryEvent;
48
49    #[test]
50    fn agent_spawned_serializes() {
51        let event = TelemetryEvent::agent_spawned(
52            "sess",
53            5,
54            None,
55            AgentSpawnedPayload {
56                agent_id: "agent_02".into(),
57                role: "researcher".into(),
58                model: Some("claude-sonnet-4-6".into()),
59                stage_name: Some("research".into()),
60                background: false,
61                parent_tool_use_id: None,
62            },
63        );
64        assert_eq!(event.kind(), "agent_spawned");
65        let v =
66            serde_json::to_value(event).expect("serialization of telemetry event should not fail");
67        assert_eq!(v["type"], "agent_spawned");
68        assert_eq!(v["role"], "researcher");
69    }
70
71    #[test]
72    fn agent_completed_serializes() {
73        let event = TelemetryEvent::agent_completed(
74            "sess",
75            8,
76            None,
77            AgentCompletedPayload {
78                agent_id: "agent_02".into(),
79                role: "researcher".into(),
80                had_error: false,
81                duration_ms: 45000,
82            },
83        );
84        assert_eq!(event.kind(), "agent_completed");
85        let v =
86            serde_json::to_value(event).expect("serialization of telemetry event should not fail");
87        assert_eq!(v["type"], "agent_completed");
88        assert_eq!(v["duration_ms"], 45000);
89    }
90
91    #[test]
92    fn team_stage_complete_serializes() {
93        let event = TelemetryEvent::team_stage_complete(
94            "sess",
95            10,
96            None,
97            TeamStageCompletePayload {
98                stage_name: "implementation".into(),
99                agent_count: 3,
100                duration_ms: 120000,
101                success: true,
102                agents_with_errors: 0,
103            },
104        );
105        assert_eq!(event.kind(), "team_stage_complete");
106        let v =
107            serde_json::to_value(event).expect("serialization of telemetry event should not fail");
108        assert_eq!(v["type"], "team_stage_complete");
109        assert_eq!(v["agent_count"], 3);
110    }
111}