Add root turn IDs to turn and tool analytics (#40486)

## Why

Analytics need to associate subagent activity with the top-level turn that
caused it, while avoiding a stale association when steering makes that origin
ambiguous.

## What changed

- Add `root_turn_id` to turn and tool-item analytics events.
- Read the root from live turn metadata when each event is built, reporting
  `null` when the root is unknown or ambiguous.
- Propagate the metadata through control-tool and code-mode completion facts so
  correlated and background tool events retain the correct root.

## Testing

- Cover root IDs for top-level turns, subagent turns, correlated code-mode
  calls, and tool events emitted after turn completion.
- Verify that queued and subsequent events report `null` after the live root is
  invalidated.

GitOrigin-RevId: 42fd76541c3c716cec74787d47b50adb2d22e6d2
This commit is contained in:
Eddie Chen authored and copyberry committed 2026-08-24 22:30:59 +00:00
1 parent 0280d73c1e
commit 49880081fe
13 files changed
+439 -30

No files matched your search

+17 -2
View File
@@ -778,6 +778,7 @@ impl AnalyticsReducer {
CodeModeToolCallFact::Completed {
thread_id,
turn_id,
turn_metadata,
call_id,
cell_id,
tool_name,
@@ -842,6 +843,7 @@ impl AnalyticsReducer {
self.record_tool_event(
&thread_id,
&turn_id,
turn_metadata.root_turn_id(),
event,
ToolEventEmission::AwaitResponse,
out,
@@ -858,6 +860,7 @@ impl AnalyticsReducer {
let ControlToolCallFact {
thread_id,
turn_id,
turn_metadata,
call_id,
cell_id,
tool_name,
@@ -921,6 +924,7 @@ impl AnalyticsReducer {
self.record_tool_event(
&thread_id,
&turn_id,
turn_metadata.root_turn_id(),
event,
ToolEventEmission::AwaitResponse,
out,
@@ -979,14 +983,16 @@ impl AnalyticsReducer {
&mut self,
thread_id: &str,
turn_id: &str,
root_turn_id: Option<String>,
mut event: TrackEventRequest,
emission: ToolEventEmission,
out: &mut Vec<TrackEventRequest>,
) {
if tool_event_base_mut(&mut event).is_none() {
let Some(base) = tool_event_base_mut(&mut event) else {
out.push(event);
return;
}
};
base.root_turn_id = root_turn_id;
let state = self
.tool_response_states
.entry((thread_id.to_string(), turn_id.to_string()))
@@ -1873,9 +1879,15 @@ impl AnalyticsReducer {
thread_metadata,
review_summary: self.item_review_summaries.get(&key),
}) {
let root_turn_id = self
.turns
.get(&notification.turn_id)
.and_then(|turn| turn.resolved_config.as_ref())
.and_then(|config| config.turn_metadata.root_turn_id());
self.record_tool_event(
&notification.thread_id,
&notification.turn_id,
root_turn_id,
event,
ToolEventEmission::ImmediateUnlessCorrelated,
out,
@@ -2888,6 +2900,7 @@ fn tool_item_base(
thread_id: thread_id.to_string(),
session_id: thread_metadata.session_id.clone(),
turn_id: turn_id.to_string(),
root_turn_id: None,
item_id,
cell_id: None,
parent_call_id: None,
@@ -3342,6 +3355,7 @@ fn codex_turn_event_params(
let TurnResolvedConfigFact {
turn_id: _resolved_turn_id,
thread_id: _resolved_thread_id,
turn_metadata,
num_input_images: _resolved_num_input_images,
submission_type,
ephemeral,
@@ -3377,6 +3391,7 @@ fn codex_turn_event_params(
thread_id,
session_id: thread_metadata.session_id.clone(),
turn_id,
root_turn_id: turn_metadata.root_turn_id(),
app_server_client,
runtime,
submission_type,