Expose and persist turn lineage across the app server (#51415)

## Why

Clients starting delegated work need to attribute descendant turns to the original turn and retain that attribution through completion and history reads.

## What changed

- Accept optional `parentTurnId` and `rootTurnId` in `turn/start`, and expose `rootTurnId` on returned turns and lifecycle notifications.
- Default new turns to their own root when none is supplied. Preserve the active turn's root when steering, ignoring incoming lineage fields.
- Carry root IDs through completion, interruption, recovery, and fork boundaries, and persist them for paginated history reads. Leave unknown roots null in older history.
- Update protocol schemas and generated TypeScript and Python bindings.

## Testing

Extend coverage for default and explicit roots, steering without changing lineage, interrupted and recovered turns, history reads after restart in both history modes, nested fork pagination, and compatibility with older database writers.

GitOrigin-RevId: f1a570a12bd11742929fd2e8b017e5d35aa07dcb
This commit is contained in:
Eddie Chen
2026-10-06 16:56:39 +00:00
committed by copyberry
parent ff9ab4aed9
commit cfc946f4e1
149 changed files with 896 additions and 135 deletions
@@ -44,6 +44,7 @@ fn reasoning(id: &str) -> ThreadItem {
fn turn(items: Vec<ThreadItem>) -> Turn {
Turn {
id: "turn".to_owned(),
root_turn_id: None,
items,
items_view: TurnItemsView::Full,
status: TurnStatus::Completed,
@@ -2357,6 +2357,7 @@ async fn resume_picker_round_trip_preserves_each_threads_input() -> Result<()> {
thread_id: target.thread_id.to_string(),
turn: codex_app_server_protocol::Turn {
id: "turn-with-follow-up".to_string(),
root_turn_id: None,
items_view: codex_app_server_protocol::TurnItemsView::Full,
items: Vec::new(),
status: codex_app_server_protocol::TurnStatus::InProgress,
@@ -47,6 +47,7 @@ async fn usage_notice_preserves_composer_geometry_on_recovery() -> Result<()> {
thread_id: ThreadId::new().to_string(),
turn: Turn {
id: "turn".into(),
root_turn_id: None,
items_view: TurnItemsView::Full,
items: Vec::new(),
status: TurnStatus::InProgress,
@@ -7,6 +7,7 @@ use pretty_assertions::assert_eq;
fn turn(id: &str, status: TurnStatus, item_ids: &[&str]) -> Turn {
Turn {
id: id.to_string(),
root_turn_id: None,
items: item_ids
.iter()
.map(|id| ThreadItem::UserMessage {
@@ -11,6 +11,7 @@ use pretty_assertions::assert_eq;
fn turn(id: &str, status: TurnStatus, item_ids: &[&str]) -> Turn {
Turn {
id: id.to_string(),
root_turn_id: None,
items: item_ids
.iter()
.map(|id| ThreadItem::UserMessage {
@@ -29,6 +29,7 @@ async fn follow_control_click_preserves_draft_caret_and_composer_geometry() -> R
thread_id: ThreadId::new().to_string(),
turn: codex_app_server_protocol::Turn {
id: "turn".into(),
root_turn_id: None,
items_view: codex_app_server_protocol::TurnItemsView::Full,
items: Vec::new(),
status: codex_app_server_protocol::TurnStatus::InProgress,
@@ -79,6 +79,7 @@ async fn configured_ctrl_space_submit_wins_over_transcript_selection() -> Result
thread_id: thread_id.to_string(),
turn: Turn {
id: "turn".into(),
root_turn_id: None,
items_view: TurnItemsView::Full,
items: Vec::new(),
status: TurnStatus::InProgress,
@@ -185,6 +186,7 @@ fn complete_plan_turn(app: &mut App) {
let thread_id = ThreadId::new().to_string();
let turn = Turn {
id: "plan-turn".into(),
root_turn_id: None,
items_view: TurnItemsView::Full,
items: Vec::new(),
status: TurnStatus::InProgress,
@@ -598,6 +598,7 @@ async fn owned_details_escape_interrupts_work_without_starting_backtrack() -> Re
thread_id: thread_id.to_string(),
turn: codex_app_server_protocol::Turn {
id: "active-turn".to_string(),
root_turn_id: None,
items_view: codex_app_server_protocol::TurnItemsView::Full,
items: Vec::new(),
status: codex_app_server_protocol::TurnStatus::InProgress,
@@ -675,6 +675,7 @@ mod tests {
thread_id: "thread-1".to_string(),
turn: Turn {
id: turn_id.to_string(),
root_turn_id: None,
items_view: codex_app_server_protocol::TurnItemsView::Full,
items: Vec::new(),
status: TurnStatus::Completed,
+1
View File
@@ -45,6 +45,7 @@ use uuid::Uuid;
fn turn(status: TurnStatus) -> Turn {
Turn {
id: "turn".to_string(),
root_turn_id: None,
items: Vec::new(),
items_view: Default::default(),
status,
@@ -4,6 +4,7 @@ use codex_app_server_protocol::TurnItemsView;
fn turn(id: &str, status: TurnStatus) -> Turn {
Turn {
id: id.to_string(),
root_turn_id: None,
items: Vec::new(),
items_view: TurnItemsView::Full,
status,
+4
View File
@@ -7224,6 +7224,7 @@ async fn resizing_empty_transcript_schedules_settled_size_recheck() {
fn test_turn(turn_id: &str, status: TurnStatus, items: Vec<ThreadItem>) -> Turn {
Turn {
id: turn_id.to_string(),
root_turn_id: None,
items_view: codex_app_server_protocol::TurnItemsView::Full,
items,
status,
@@ -8426,6 +8427,7 @@ async fn prompt_edit_reverts_earlier_and_first_visible_prompts_in_place() -> Res
completed_at_ms: 0,
})),
RolloutItem::EventMsg(EventMsg::TurnComplete(TurnCompleteEvent {
root_turn_id: None,
turn_id: turn_id.to_string(),
last_agent_message: None,
error: None,
@@ -8812,6 +8814,7 @@ async fn replay_thread_snapshot_replays_turn_history_in_order() {
turns: vec![
Turn {
id: "turn-1".to_string(),
root_turn_id: None,
items_view: codex_app_server_protocol::TurnItemsView::Full,
items: vec![ThreadItem::UserMessage {
id: "user-1".to_string(),
@@ -8829,6 +8832,7 @@ async fn replay_thread_snapshot_replays_turn_history_in_order() {
},
Turn {
id: "turn-2".to_string(),
root_turn_id: None,
items_view: codex_app_server_protocol::TurnItemsView::Full,
items: vec![
ThreadItem::UserMessage {
@@ -304,6 +304,7 @@ fn evicted_voice_delegation_marker_still_suppresses_private_replay() {
let mut resumed = ThreadEventStore::new(/*capacity*/ 1);
resumed.set_turns(vec![Turn {
id: "voice-turn".into(),
root_turn_id: None,
items: vec![voice_request],
items_view: codex_app_server_protocol::TurnItemsView::Summary,
status: TurnStatus::InProgress,
@@ -345,6 +346,7 @@ fn saved_voice_turn_suppresses_reasoning_without_hiding_typed_reasoning() {
};
let turn = |id: &str, items| Turn {
id: id.into(),
root_turn_id: None,
items,
items_view: codex_app_server_protocol::TurnItemsView::Full,
status: TurnStatus::Completed,
@@ -390,6 +392,7 @@ fn snapshot_keeps_typed_output_before_voice_handoff_in_the_same_turn() {
let mut store = ThreadEventStore::new(/*capacity*/ 8);
store.set_turns(vec![Turn {
id: "shared".into(),
root_turn_id: None,
items: vec![typed.clone(), marker.clone(), private.clone()],
items_view: codex_app_server_protocol::TurnItemsView::Full,
status: TurnStatus::InProgress,
@@ -80,6 +80,7 @@ async fn history_fixture(
}));
}
events.push(EventMsg::TurnComplete(TurnCompleteEvent {
root_turn_id: None,
turn_id,
last_agent_message: None,
error: None,
@@ -95,6 +95,7 @@ pub(super) async fn completed_history_app(
})
}));
events.push(EventMsg::TurnComplete(TurnCompleteEvent {
root_turn_id: None,
turn_id,
last_agent_message: Some(format!("{name} answer")),
error: None,
@@ -299,6 +299,7 @@ async fn switching_agent_threads_preserves_backend_voice_and_routes_pending_spee
thread_id: source.to_string(),
turn: Turn {
id: turn_id.into(),
root_turn_id: None,
items: vec![answer.clone()],
items_view: TurnItemsView::Summary,
status: TurnStatus::Completed,
@@ -665,6 +666,7 @@ async fn replay_reconciles_only_matching_voice_captions_one_for_one() {
.insert(source, records);
let voice = Turn {
id: "voice-turn".into(),
root_turn_id: None,
items: vec![
test_user_message(
"voice-user",
@@ -929,6 +931,7 @@ async fn unrendered_buffered_items_do_not_consume_retained_captions() {
thread_id: source.to_string(),
turn: Turn {
id: turn_id.into(),
root_turn_id: None,
items,
items_view: TurnItemsView::Summary,
status,
@@ -1212,6 +1215,7 @@ async fn rejected_realtime_speech_restores_the_delegated_final_answer() -> Resul
thread_id: thread_id.to_string(),
turn: Turn {
id: "rejected-turn".to_string(),
root_turn_id: None,
items: vec![answer],
items_view: TurnItemsView::Summary,
status: TurnStatus::Completed,
@@ -1311,6 +1315,7 @@ async fn switching_threads_retains_undelivered_voice_answer_after_replay_evictio
thread_id: original.to_string(),
turn: Turn {
id: "switched-turn".to_string(),
root_turn_id: None,
items: vec![answer],
items_view: TurnItemsView::Summary,
status: TurnStatus::Completed,
@@ -1391,6 +1396,7 @@ async fn switching_threads_retains_undelivered_voice_answer_after_replay_evictio
session: Some(test_thread_session(original, app.config.cwd.to_path_buf())),
turns: vec![Turn {
id: "replayed-turn".to_string(),
root_turn_id: None,
items: vec![replayed_item],
items_view: TurnItemsView::Summary,
status: TurnStatus::Completed,
@@ -1593,6 +1599,7 @@ async fn delegated_final_speech_reaches_app_server_once_and_stale_speech_is_reje
thread_id: thread_id.to_string(),
turn: Turn {
id: turn_id.to_string(),
root_turn_id: None,
items: vec![answer],
items_view: TurnItemsView::Summary,
status: TurnStatus::Completed,
@@ -1670,6 +1677,7 @@ async fn delegated_final_speech_reaches_app_server_once_and_stale_speech_is_reje
thread_id: thread_id.to_string(),
turn: Turn {
id: second_turn.to_string(),
root_turn_id: None,
items: vec![second_answer],
items_view: TurnItemsView::Summary,
status: TurnStatus::Completed,
@@ -18,6 +18,7 @@ fn turn_started_notification(thread_id: ThreadId, turn_id: &str) -> ServerNotifi
thread_id: thread_id.to_string(),
turn: Turn {
id: turn_id.to_string(),
root_turn_id: None,
items_view: TurnItemsView::Full,
items: Vec::new(),
status: TurnStatus::InProgress,
+1
View File
@@ -689,6 +689,7 @@ mod tests {
fn test_turn(turn_id: &str, status: TurnStatus, items: Vec<ThreadItem>) -> Turn {
Turn {
id: turn_id.to_string(),
root_turn_id: None,
items_view: codex_app_server_protocol::TurnItemsView::Full,
items,
status,
@@ -269,6 +269,7 @@ async fn check_thread_title_generation(scenario: TitleScenario) -> color_eyre::R
.turns
.push(Turn {
id: "existing-turn".to_string(),
root_turn_id: None,
items: vec![title_user_message("user-message", "Fix the login timeout")],
items_view: Default::default(),
status: TurnStatus::Completed,
@@ -83,6 +83,7 @@ fn transcript_export_excludes_hidden_review_prompts_and_nested_duplicates() {
let duplicate_two = user("duplicate-two", "duplicate review prompt");
let turn = |id: &str, items: Vec<ThreadItem>, status| Turn {
id: id.to_string(),
root_turn_id: None,
items,
items_view: TurnItemsView::Full,
status,
+1
View File
@@ -729,6 +729,7 @@ mod tests {
fn turn(turn_id: &str, status: TurnStatus, user_messages: usize) -> Turn {
Turn {
id: turn_id.to_string(),
root_turn_id: None,
items: (0..user_messages)
.map(|index| ThreadItem::UserMessage {
id: format!("user-{index}"),
+3
View File
@@ -1439,6 +1439,8 @@ impl AppServerSession {
disabled_plugin_ids: None,
thread_id: thread_id.to_string(),
turn_trigger: Some("user".to_string()),
parent_turn_id: None,
root_turn_id: None,
client_user_message_id: Some(client_user_message_id),
input: items,
tool_output: None,
@@ -4118,6 +4120,7 @@ mod tests {
name: None,
turns: vec![Turn {
id: "turn-1".to_string(),
root_turn_id: None,
items_view: codex_app_server_protocol::TurnItemsView::Full,
items: vec![
codex_app_server_protocol::ThreadItem::UserMessage {
@@ -126,6 +126,7 @@ fn finish_turn(
thread_id: thread_id.to_string(),
turn: Turn {
id: turn_id.to_string(),
root_turn_id: None,
items,
items_view: TurnItemsView::Summary,
status,
@@ -10,6 +10,7 @@ async fn replay_preserves_typed_updates_before_voice_steers_the_turn() {
chat.replay_thread_turns(
vec![Turn {
id: "typed-then-voice".into(),
root_turn_id: None,
items: vec![
user_item("Typed question"),
agent_item(
@@ -79,6 +80,7 @@ async fn in_progress_voice_replay_restores_the_late_reasoning_guard() {
chat.replay_thread_turns(
vec![Turn {
id: "saved-voice-turn".into(),
root_turn_id: None,
items: vec![voice_request.clone()],
items_view: TurnItemsView::Full,
status: TurnStatus::InProgress,
+2
View File
@@ -126,6 +126,7 @@ impl ChatWidget {
}
let Turn {
id: turn_id,
root_turn_id,
items_view: _,
items,
status,
@@ -224,6 +225,7 @@ impl ChatWidget {
thread_id: self.thread_id.map(|id| id.to_string()).unwrap_or_default(),
turn: Turn {
id: turn_id,
root_turn_id,
items_view: codex_app_server_protocol::TurnItemsView::NotLoaded,
items: Vec::new(),
status,
@@ -162,6 +162,7 @@ fn start_safety_buffering_test_turn(
thread_id: thread_id.to_string(),
turn: AppServerTurn {
id: turn_id.to_string(),
root_turn_id: None,
items_view: codex_app_server_protocol::TurnItemsView::Full,
items: Vec::new(),
status: AppServerTurnStatus::InProgress,
@@ -874,6 +875,7 @@ async fn live_app_server_turn_completed_clears_working_status_after_answer_item(
thread_id: "thread-1".to_string(),
turn: AppServerTurn {
id: "turn-1".to_string(),
root_turn_id: None,
items_view: codex_app_server_protocol::TurnItemsView::Full,
items: Vec::new(),
status: AppServerTurnStatus::InProgress,
@@ -921,6 +923,7 @@ async fn live_app_server_turn_completed_clears_working_status_after_answer_item(
thread_id: "thread-1".to_string(),
turn: AppServerTurn {
id: "turn-1".to_string(),
root_turn_id: None,
items_view: codex_app_server_protocol::TurnItemsView::Summary,
items: vec![item],
status: AppServerTurnStatus::Completed,
@@ -958,6 +961,7 @@ async fn live_app_server_turn_started_sets_feedback_turn_id() {
thread_id: "thread-1".to_string(),
turn: AppServerTurn {
id: "turn-1".to_string(),
root_turn_id: None,
items_view: codex_app_server_protocol::TurnItemsView::Full,
items: Vec::new(),
status: AppServerTurnStatus::InProgress,
@@ -1538,6 +1542,7 @@ async fn live_app_server_failed_turn_does_not_duplicate_error_history() {
thread_id: "thread-1".to_string(),
turn: AppServerTurn {
id: "turn-1".to_string(),
root_turn_id: None,
items_view: codex_app_server_protocol::TurnItemsView::Full,
items: Vec::new(),
status: AppServerTurnStatus::InProgress,
@@ -1573,6 +1578,7 @@ async fn live_app_server_failed_turn_does_not_duplicate_error_history() {
thread_id: "thread-1".to_string(),
turn: AppServerTurn {
id: "turn-1".to_string(),
root_turn_id: None,
items_view: codex_app_server_protocol::TurnItemsView::Full,
items: Vec::new(),
status: AppServerTurnStatus::Failed,
@@ -1705,6 +1711,7 @@ async fn live_app_server_stream_recovery_restores_previous_status_header() {
thread_id: "thread-1".to_string(),
turn: AppServerTurn {
id: "turn-1".to_string(),
root_turn_id: None,
items_view: codex_app_server_protocol::TurnItemsView::Full,
items: Vec::new(),
status: AppServerTurnStatus::InProgress,
@@ -1782,6 +1789,7 @@ async fn live_app_server_server_overloaded_error_renders_error() {
thread_id: "thread-1".to_string(),
turn: AppServerTurn {
id: "turn-1".to_string(),
root_turn_id: None,
items_view: codex_app_server_protocol::TurnItemsView::Full,
items: Vec::new(),
status: AppServerTurnStatus::InProgress,
@@ -1825,6 +1833,7 @@ async fn live_app_server_cyber_policy_error_renders_dedicated_notice() {
thread_id: "thread-1".to_string(),
turn: AppServerTurn {
id: "turn-1".to_string(),
root_turn_id: None,
items_view: codex_app_server_protocol::TurnItemsView::Full,
items: Vec::new(),
status: AppServerTurnStatus::InProgress,
@@ -1131,6 +1131,7 @@ pub(super) fn app_server_turn(
) -> AppServerTurn {
AppServerTurn {
id: turn_id.to_string(),
root_turn_id: None,
items_view: codex_app_server_protocol::TurnItemsView::Full,
items: Vec::new(),
status,
@@ -1140,6 +1140,7 @@ async fn replayed_retryable_app_server_error_keeps_turn_running() {
thread_id: "thread-1".to_string(),
turn: AppServerTurn {
id: "turn-1".to_string(),
root_turn_id: None,
items_view: codex_app_server_protocol::TurnItemsView::Full,
items: Vec::new(),
status: AppServerTurnStatus::InProgress,
@@ -1475,6 +1476,7 @@ async fn live_reasoning_summary_is_not_rendered_twice_when_item_completes() {
thread_id: "thread-1".to_string(),
turn: AppServerTurn {
id: "turn-1".to_string(),
root_turn_id: None,
items_view: codex_app_server_protocol::TurnItemsView::Full,
items: Vec::new(),
status: AppServerTurnStatus::InProgress,
@@ -1534,6 +1536,7 @@ async fn live_reasoning_summary_drops_empty_parts_without_losing_content() {
thread_id: "thread-1".to_string(),
turn: AppServerTurn {
id: "turn-1".to_string(),
root_turn_id: None,
items_view: codex_app_server_protocol::TurnItemsView::Full,
items: Vec::new(),
status: AppServerTurnStatus::InProgress,
@@ -810,6 +810,7 @@ async fn plan_implementation_popup_skips_replayed_turn_complete() {
chat.replay_thread_turns(
vec![AppServerTurn {
id: "turn-1".to_string(),
root_turn_id: None,
items_view: codex_app_server_protocol::TurnItemsView::Full,
items: vec![AppServerThreadItem::AgentMessage {
id: "msg-plan".to_string(),
@@ -850,6 +851,7 @@ async fn plan_implementation_popup_shows_once_when_replay_precedes_live_turn_com
chat.replay_thread_turns(
vec![AppServerTurn {
id: "turn-1".to_string(),
root_turn_id: None,
items_view: codex_app_server_protocol::TurnItemsView::Full,
items: vec![AppServerThreadItem::AgentMessage {
id: "msg-plan-replay".to_string(),
@@ -1159,6 +1161,7 @@ async fn submit_user_message_queues_while_compaction_turn_is_running() {
thread_id: thread_id.to_string(),
turn: AppServerTurn {
id: "turn-1".to_string(),
root_turn_id: None,
items_view: codex_app_server_protocol::TurnItemsView::Full,
items: Vec::new(),
status: AppServerTurnStatus::InProgress,
@@ -1204,6 +1207,7 @@ async fn submit_user_message_queues_while_compaction_turn_is_running() {
thread_id: thread_id.to_string(),
turn: AppServerTurn {
id: "turn-1".to_string(),
root_turn_id: None,
items_view: codex_app_server_protocol::TurnItemsView::Full,
items: Vec::new(),
status: AppServerTurnStatus::Completed,
@@ -1298,6 +1298,7 @@ async fn interrupted_turn_after_goal_budget_limited_uses_budget_message_snapshot
thread_id: "thread-1".to_string(),
turn: codex_app_server_protocol::Turn {
id: "turn-1".to_string(),
root_turn_id: None,
items_view: codex_app_server_protocol::TurnItemsView::Full,
items: Vec::new(),
status: codex_app_server_protocol::TurnStatus::InProgress,
@@ -1335,6 +1336,7 @@ async fn interrupted_turn_after_goal_budget_limited_uses_budget_message_snapshot
thread_id: "thread-1".to_string(),
turn: codex_app_server_protocol::Turn {
id: "turn-1".to_string(),
root_turn_id: None,
items_view: codex_app_server_protocol::TurnItemsView::Full,
items: Vec::new(),
status: codex_app_server_protocol::TurnStatus::Interrupted,
@@ -274,6 +274,7 @@ async fn replayed_turn_completions_do_not_start_live_billing_refreshes() {
chat.replay_thread_turns(
vec![Turn {
id: "replayed-turn".to_string(),
root_turn_id: None,
items: Vec::new(),
items_view: TurnItemsView::NotLoaded,
status: status.clone(),
+3
View File
@@ -6747,6 +6747,7 @@ session_picker_view = "dense"
name: None,
turns: vec![codex_app_server_protocol::Turn {
id: String::from("turn-1"),
root_turn_id: None,
items_view: codex_app_server_protocol::TurnItemsView::Full,
items: vec![
ThreadItem::UserMessage {
@@ -6834,6 +6835,7 @@ session_picker_view = "dense"
name: None,
turns: vec![codex_app_server_protocol::Turn {
id: String::from("turn-1"),
root_turn_id: None,
items_view: codex_app_server_protocol::TurnItemsView::Full,
items: vec![ThreadItem::Reasoning {
id: String::from("reasoning-1"),
@@ -6912,6 +6914,7 @@ session_picker_view = "dense"
name: None,
turns: vec![codex_app_server_protocol::Turn {
id: String::from("turn-1"),
root_turn_id: None,
items_view: codex_app_server_protocol::TurnItemsView::Full,
items: vec![ThreadItem::Reasoning {
id: String::from("reasoning-1"),
@@ -43,6 +43,7 @@ fn turn_completed_notification(turn_id: &str, status: TurnStatus) -> ServerNotif
thread_id: "thread-1".to_string(),
turn: Turn {
id: turn_id.to_string(),
root_turn_id: None,
items: Vec::new(),
items_view: Default::default(),
status,
@@ -23,6 +23,7 @@ fn computer(id: &str) -> ThreadItem {
fn turn(id: &str, items: Vec<ThreadItem>) -> Turn {
Turn {
id: id.to_string(),
root_turn_id: None,
items,
items_view: TurnItemsView::Full,
status: TurnStatus::Completed,
@@ -378,6 +378,7 @@ fn folding_a_reasoning_only_page_retains_its_find_and_copy_revision() {
};
let current = Turn {
id: "turn".to_owned(),
root_turn_id: None,
items: vec![call, reasoning],
items_view: TurnItemsView::Full,
status: TurnStatus::Completed,