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
@@ -145,6 +145,7 @@ fn turn_started_emits_turn_started_event() {
thread_id: "thread-1".to_string(),
turn: Turn {
id: "turn-1".to_string(),
root_turn_id: None,
items_view: codex_app_server_protocol::TurnItemsView::Full,
items: Vec::new(),
status: TurnStatus::InProgress,
@@ -1197,6 +1198,7 @@ fn plan_update_emits_started_then_updated_then_completed() {
thread_id: "thread-1".to_string(),
turn: Turn {
id: "turn-1".to_string(),
root_turn_id: None,
items_view: codex_app_server_protocol::TurnItemsView::Full,
items: Vec::new(),
status: TurnStatus::Completed,
@@ -1257,6 +1259,7 @@ fn plan_update_after_completion_starts_new_todo_list_with_new_id() {
thread_id: "thread-1".to_string(),
turn: Turn {
id: "turn-1".to_string(),
root_turn_id: None,
items_view: codex_app_server_protocol::TurnItemsView::Full,
items: Vec::new(),
status: TurnStatus::Completed,
@@ -1342,6 +1345,7 @@ fn token_usage_update_is_emitted_on_turn_completion() {
thread_id: "thread-1".to_string(),
turn: Turn {
id: "turn-1".to_string(),
root_turn_id: None,
items_view: codex_app_server_protocol::TurnItemsView::Full,
items: Vec::new(),
status: TurnStatus::Completed,
@@ -1378,6 +1382,7 @@ fn turn_completion_recovers_final_message_from_turn_items() {
thread_id: "thread-1".to_string(),
turn: Turn {
id: "turn-1".to_string(),
root_turn_id: None,
items_view: codex_app_server_protocol::TurnItemsView::Full,
items: vec![ThreadItem::AgentMessage {
id: "msg-1".to_string(),
@@ -1457,6 +1462,7 @@ fn turn_completion_reconciles_started_items_from_turn_items() {
thread_id: "thread-1".to_string(),
turn: Turn {
id: "turn-1".to_string(),
root_turn_id: None,
items_view: codex_app_server_protocol::TurnItemsView::Full,
items: vec![ThreadItem::CommandExecution {
model_context: None,
@@ -1531,6 +1537,7 @@ fn turn_completion_overwrites_stale_final_message_from_turn_items() {
thread_id: "thread-1".to_string(),
turn: Turn {
id: "turn-1".to_string(),
root_turn_id: None,
items_view: codex_app_server_protocol::TurnItemsView::Full,
items: vec![ThreadItem::AgentMessage {
id: "msg-1".to_string(),
@@ -1585,6 +1592,7 @@ fn turn_completion_preserves_streamed_final_message_when_turn_items_are_empty()
thread_id: "thread-1".to_string(),
turn: Turn {
id: "turn-1".to_string(),
root_turn_id: None,
items_view: codex_app_server_protocol::TurnItemsView::Full,
items: Vec::new(),
status: TurnStatus::Completed,
@@ -1636,6 +1644,7 @@ fn failed_turn_clears_stale_final_message() {
thread_id: "thread-1".to_string(),
turn: Turn {
id: "turn-1".to_string(),
root_turn_id: None,
items_view: codex_app_server_protocol::TurnItemsView::Full,
items: Vec::new(),
status: TurnStatus::Failed,
@@ -1665,6 +1674,7 @@ fn turn_completion_falls_back_to_final_plan_text() {
thread_id: "thread-1".to_string(),
turn: Turn {
id: "turn-1".to_string(),
root_turn_id: None,
items_view: codex_app_server_protocol::TurnItemsView::Full,
items: vec![ThreadItem::Plan {
id: "plan-1".to_string(),
@@ -1721,6 +1731,7 @@ fn turn_failure_prefers_structured_error_message() {
thread_id: "thread-1".to_string(),
turn: Turn {
id: "turn-1".to_string(),
root_turn_id: None,
items_view: codex_app_server_protocol::TurnItemsView::Full,
items: Vec::new(),
status: TurnStatus::Failed,