Add OpenTelemetry for subagent lifecycles (#62567)

## Why

Correlating a subagent's creation, turn attempts, and stop requests
helps explain what happened when those actions span workers.

## What changed

- Emit structured events and counters for successful spawns and accepted
  `close_agent` and `interrupt_agent` calls. Count accepted stops even
  when the target is already idle or unloaded.
- Track initial, follow-up, and resumed turn attempts for spawned
  subagents. Record each start and at most one outcome per attempt:
  completed, failed, interrupted, or suspended.
- Use thread, turn, and call IDs to correlate structured events while
  keeping those IDs out of metric labels.

## Testing

Add coverage for lifecycle counts, duplicate outcomes, stop handling,
and event correlation across workers. Improve the Windows elevated
sandbox test for an approved `git pull` by provisioning the sandbox
before the turn and reporting setup or launch failures with diagnostics.

GitOrigin-RevId: bc1190302e1dffbdd79c7ac984b6debe1dac4a65
This commit is contained in:
Bryan Ashley
2026-11-19 18:39:32 +00:00
committed by copyberry
parent 617ad20df0
commit 65454c7b99
18 changed files with 779 additions and 35 deletions
+1
View File
@@ -2742,6 +2742,7 @@ impl Session {
/// Persist the event to rollout and send it to clients.
pub(crate) async fn send_event(&self, turn_context: &TurnContext, mut msg: EventMsg) {
crate::agent::turn_metrics::record_terminal_event(turn_context, &msg);
if let Some((item_id, started_at_ms)) = msg.command_approval_start() {
// The client can see the command before execution. Keep its first start
// for the canonical lifecycle events that will be saved in history.
+5
View File
@@ -1083,6 +1083,11 @@ impl Session {
thread_id.to_string(),
thread_extension_init,
);
crate::agent::turn_metrics::register_thread(
&thread_extension_data,
&session_source,
&initial_history,
);
if let Some(required) = &required_cyber_access_programs {
thread_extension_data.insert(
crate::cyber_access_program::RequiredCyberAccessProgramsSnapshot(Arc::clone(
@@ -112,6 +112,7 @@ pub(super) async fn suspend_turn_and_shutdown(
CodexErr::Fatal(format!("close suspended root turn writer failed: {error}"))
})?;
recorded?;
crate::agent::turn_metrics::record_suspension(&task.turn_context);
// Announce completion only after extension cleanup and writer closure so a
// replacement worker cannot write the same thread concurrently.
session