Preserve root turn attribution in turn-start events (#44611)

## Why

Persisted turn-start events lack the originating root turn ID, so they cannot directly associate child turns with the root turn that initiated them.

## What changed

- Add optional `root_turn_id` to `TurnStartedEvent`. Use the inherited root turn ID when available, otherwise the turn's own ID.
- Centralize turn-start emission for regular turns, shell commands, and manual compaction so they record attribution consistently.
- Carry attribution into `ThreadHistoryTurnChange` when processing turn-start events and retain it through completion in the history builder.
- Keep older records compatible by defaulting missing attribution to `None`.

## Testing

Add coverage for root and child turn attribution, persistence when tool collisions fail a turn before sampling, history change propagation, and deserialization of older turn-start records.

GitOrigin-RevId: 54a80b828ca7d1c4c19f13d468c01d052e1b9130
This commit is contained in:
Owen Lin authored and copyberry committed 2026-09-10 18:24:13 +00:00
1 parent 242c5ce01c
commit 196964ef10
40 files changed
+252 -55

No files matched your search

+25 -7
View File
@@ -45,6 +45,7 @@ use core_test_support::submit_thread_settings;
use core_test_support::test_codex::local;
use core_test_support::test_codex::test_codex;
use core_test_support::wait_for_event;
use pretty_assertions::assert_eq;
use serde_json::Value;
use serde_json::json;
use test_case::test_case;
@@ -123,20 +124,25 @@ async fn strict_tool_collisions_fail_the_turn_before_sampling(
defer_loading: false,
})]
};
let thread = test
let codex_core::NewThread { thread, .. } = test
.thread_manager
.start_thread(StartThreadOptions {
dynamic_tools,
..StartThreadOptions::new(test.config.clone())
})
.await?
.thread;
.await?;
thread
.start_or_steer_turn(TurnInputRequest::user_input(vec![UserInput::Text {
text: "use the planning tool".to_string(),
text_elements: Vec::new(),
}]))
.start_or_steer_turn(
TurnInputRequest::user_input(vec![UserInput::Text {
text: "use the planning tool".to_string(),
text_elements: Vec::new(),
}])
.on_start(codex_core::TurnStartOptions {
root_turn_id: Some("root-turn".into()),
..Default::default()
}),
)
.await?;
let EventMsg::Error(error) =
@@ -157,6 +163,18 @@ async fn strict_tool_collisions_fail_the_turn_before_sampling(
unreachable!("event predicate guarantees turn completion");
};
assert_eq!(completed.error, Some(error));
thread.flush_rollout().await?;
let history = thread.load_history(/*include_archived*/ false).await?;
let attribution = history.items.iter().find_map(|item| match item {
codex_history::RolloutItem::EventMsg(EventMsg::TurnStarted(event))
if event.turn_id == completed.turn_id =>
{
event.root_turn_id.as_deref()
}
_ => None,
});
assert_eq!(attribution, Some("root-turn"));
assert!(
server
.received_requests()