mirror of
https://github.com/openai/codex.git
synced 2026-11-23 23:10:47 +00:00
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:
@@ -1210,6 +1210,7 @@ mod tests {
|
||||
},
|
||||
)),
|
||||
RolloutItem::EventMsg(EventMsg::TurnComplete(TurnCompleteEvent {
|
||||
root_turn_id: None,
|
||||
turn_id: "turn-1".to_string(),
|
||||
started_at: None,
|
||||
last_agent_message: None,
|
||||
|
||||
@@ -629,6 +629,7 @@ fn turn_started(turn_id: &str) -> RolloutItem {
|
||||
|
||||
fn turn_complete(turn_id: &str) -> RolloutItem {
|
||||
RolloutItem::EventMsg(EventMsg::TurnComplete(TurnCompleteEvent {
|
||||
root_turn_id: None,
|
||||
turn_id: turn_id.to_string(),
|
||||
last_agent_message: None,
|
||||
error: None,
|
||||
|
||||
@@ -333,6 +333,7 @@ fn turn_started(turn_id: &str) -> RolloutItem {
|
||||
|
||||
fn turn_completed(turn_id: &str) -> RolloutItem {
|
||||
RolloutItem::EventMsg(EventMsg::TurnComplete(TurnCompleteEvent {
|
||||
root_turn_id: None,
|
||||
turn_id: turn_id.to_string(),
|
||||
last_agent_message: None,
|
||||
error: None,
|
||||
|
||||
@@ -375,6 +375,7 @@ impl LegacyRolloutCanonicalizer {
|
||||
writer,
|
||||
timestamp,
|
||||
RolloutItem::EventMsg(EventMsg::TurnComplete(TurnCompleteEvent {
|
||||
root_turn_id: None,
|
||||
turn_id,
|
||||
last_agent_message: None,
|
||||
error: None,
|
||||
|
||||
@@ -256,6 +256,7 @@ fn compacted(replacement_history: Vec<ResponseItem>) -> RolloutItem {
|
||||
|
||||
fn completed(turn_id: &str) -> RolloutItem {
|
||||
RolloutItem::EventMsg(EventMsg::TurnComplete(TurnCompleteEvent {
|
||||
root_turn_id: None,
|
||||
turn_id: turn_id.to_string(),
|
||||
last_agent_message: None,
|
||||
error: None,
|
||||
|
||||
@@ -316,6 +316,7 @@ async fn apply_change_set(
|
||||
INSERT INTO thread_turns (
|
||||
thread_id,
|
||||
turn_id,
|
||||
root_turn_id,
|
||||
rollout_ordinal,
|
||||
rollout_byte_offset,
|
||||
rollout_end_ordinal,
|
||||
@@ -325,8 +326,9 @@ INSERT INTO thread_turns (
|
||||
started_at,
|
||||
completed_at,
|
||||
duration_ms
|
||||
) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
|
||||
) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
|
||||
ON CONFLICT(thread_id, turn_id) DO UPDATE SET
|
||||
root_turn_id = COALESCE(thread_turns.root_turn_id, excluded.root_turn_id),
|
||||
rollout_end_ordinal = excluded.rollout_end_ordinal,
|
||||
rollout_end_byte_offset = excluded.rollout_end_byte_offset,
|
||||
status = excluded.status,
|
||||
@@ -340,6 +342,7 @@ WHERE thread_turns.rollout_end_ordinal IS NULL
|
||||
)
|
||||
.bind(thread_id)
|
||||
.bind(turn_id.as_str())
|
||||
.bind(turn.root_turn_id)
|
||||
.bind(rollout_ordinal)
|
||||
.bind(rollout_byte_offset)
|
||||
.bind(terminal_ordinal)
|
||||
|
||||
@@ -52,6 +52,7 @@ pub(super) struct RolloutHistoryPosition {
|
||||
pub(super) struct StoredTurnRow {
|
||||
pub position: RolloutHistoryPosition,
|
||||
pub turn_id: String,
|
||||
pub root_turn_id: Option<String>,
|
||||
pub status: StoredTurnStatus,
|
||||
pub error: Option<StoredTurnError>,
|
||||
pub started_at: Option<i64>,
|
||||
@@ -141,6 +142,7 @@ pub(in crate::local) async fn list_turns(
|
||||
};
|
||||
turns.push(StoredTurn {
|
||||
turn_id: turn.turn_id,
|
||||
root_turn_id: turn.root_turn_id,
|
||||
items,
|
||||
items_view: params.items_view,
|
||||
status: turn.status,
|
||||
@@ -312,6 +314,7 @@ pub(super) fn stored_turn_row(row: sqlx::sqlite::SqliteRow) -> ThreadStoreResult
|
||||
rollout_ordinal: row.try_get("rollout_ordinal")?,
|
||||
},
|
||||
turn_id: row.try_get("turn_id")?,
|
||||
root_turn_id: row.try_get("root_turn_id")?,
|
||||
status,
|
||||
error,
|
||||
started_at: row.try_get("started_at")?,
|
||||
|
||||
@@ -68,6 +68,11 @@ async fn list_turns_pages_projected_rows_and_applies_item_views() {
|
||||
)
|
||||
.await;
|
||||
}
|
||||
sqlx::query("UPDATE thread_turns SET root_turn_id = 'root-1' WHERE thread_id = ? AND turn_id IN ('turn-1', 'turn-3')")
|
||||
.bind(thread_id.to_string())
|
||||
.execute(db)
|
||||
.await
|
||||
.expect("record known causal roots");
|
||||
for (turn_id, item_id, ordinal) in [
|
||||
("turn-1", "user-1", 11),
|
||||
("turn-1", "middle-1", 12),
|
||||
@@ -87,6 +92,14 @@ async fn list_turns_pages_projected_rows_and_applies_item_views() {
|
||||
.await
|
||||
.expect("first turns page");
|
||||
assert_eq!(turn_ids(&first_page), vec!["turn-1", "turn-2"]);
|
||||
assert_eq!(
|
||||
first_page
|
||||
.turns
|
||||
.iter()
|
||||
.map(|turn| turn.root_turn_id.as_deref())
|
||||
.collect::<Vec<_>>(),
|
||||
vec![Some("root-1"), None],
|
||||
);
|
||||
assert_eq!(
|
||||
first_page.turns[0].items,
|
||||
vec![
|
||||
@@ -113,6 +126,7 @@ async fn list_turns_pages_projected_rows_and_applies_item_views() {
|
||||
.await
|
||||
.expect("second turns page");
|
||||
assert_eq!(turn_ids(&second_page), vec!["turn-3"]);
|
||||
assert_eq!(second_page.turns[0].root_turn_id.as_deref(), Some("root-1"));
|
||||
assert_eq!(second_page.turns[0].items, Vec::new());
|
||||
assert_eq!(second_page.turns[0].status, StoredTurnStatus::InProgress);
|
||||
let backwards_page = store
|
||||
@@ -1357,6 +1371,8 @@ async fn lineage_reads_nested_forks() {
|
||||
for (thread_id, turn_id, ordinal, status, first_user_item_id) in [
|
||||
(root_id, "root", 1, "completed", None),
|
||||
(root_id, "shared", 2, "completed", Some("before-fork")),
|
||||
(root_id, "child", 4, "completed", None),
|
||||
(middle_id, "child", 3, "completed", None),
|
||||
(middle_id, "shared", 5, "interrupted", None),
|
||||
(middle_id, "middle", 6, "completed", None),
|
||||
(child_id, "child", 8, "completed", None),
|
||||
@@ -1373,6 +1389,11 @@ async fn lineage_reads_nested_forks() {
|
||||
)
|
||||
.await;
|
||||
}
|
||||
sqlx::query("UPDATE thread_turns SET root_turn_id = 'causal-root' WHERE thread_id = ? AND turn_id = 'shared'")
|
||||
.bind(middle_id.to_string())
|
||||
.execute(db)
|
||||
.await
|
||||
.expect("record terminal turn root");
|
||||
insert_item(
|
||||
db,
|
||||
root_id,
|
||||
@@ -1401,6 +1422,7 @@ async fn lineage_reads_nested_forks() {
|
||||
.await
|
||||
.expect("first nested descending page");
|
||||
assert_eq!(turn_ids(&first_descending_page), vec!["child", "middle"]);
|
||||
assert_eq!(first_descending_page.turns[0].root_turn_id, None);
|
||||
let second_descending_page = store
|
||||
.list_turns(turn_params(
|
||||
child_id,
|
||||
@@ -1412,6 +1434,10 @@ async fn lineage_reads_nested_forks() {
|
||||
.await
|
||||
.expect("second nested descending page");
|
||||
assert_eq!(turn_ids(&second_descending_page), vec!["shared", "root"]);
|
||||
assert_eq!(
|
||||
second_descending_page.turns[0].root_turn_id.as_deref(),
|
||||
Some("causal-root")
|
||||
);
|
||||
assert_eq!(
|
||||
second_descending_page.turns[0].status,
|
||||
StoredTurnStatus::Interrupted
|
||||
@@ -1467,6 +1493,10 @@ async fn lineage_reads_nested_forks() {
|
||||
.await
|
||||
.expect("navigate to effective occurrence turn");
|
||||
assert_eq!(turn_ids(&occurrence_turn), vec!["shared"]);
|
||||
assert_eq!(
|
||||
occurrence_turn.turns[0].root_turn_id.as_deref(),
|
||||
Some("causal-root")
|
||||
);
|
||||
}
|
||||
|
||||
async fn store_with_mode(history_mode: ThreadHistoryMode) -> (TempDir, LocalThreadStore, ThreadId) {
|
||||
|
||||
@@ -60,12 +60,14 @@ pub(super) async fn page_turn_rows(
|
||||
if remaining == 0 {
|
||||
break;
|
||||
}
|
||||
let mut query =
|
||||
QueryBuilder::<Sqlite>::new(if matches!(items_view, StoredTurnItemsView::Summary) {
|
||||
r#"
|
||||
WITH page_turns AS (
|
||||
SELECT
|
||||
let mut query = QueryBuilder::<Sqlite>::new("");
|
||||
if matches!(items_view, StoredTurnItemsView::Summary) {
|
||||
query.push("WITH page_turns AS (");
|
||||
}
|
||||
query.push(
|
||||
r#"SELECT
|
||||
turn_id,
|
||||
root_turn_id,
|
||||
rollout_ordinal,
|
||||
status,
|
||||
error_json,
|
||||
@@ -76,23 +78,8 @@ SELECT
|
||||
final_agent_item_id
|
||||
FROM thread_turns
|
||||
WHERE thread_id =
|
||||
"#
|
||||
} else {
|
||||
r#"
|
||||
SELECT
|
||||
turn_id,
|
||||
rollout_ordinal,
|
||||
status,
|
||||
error_json,
|
||||
started_at,
|
||||
completed_at,
|
||||
duration_ms,
|
||||
first_user_item_id,
|
||||
final_agent_item_id
|
||||
FROM thread_turns
|
||||
WHERE thread_id =
|
||||
"#
|
||||
});
|
||||
"#,
|
||||
);
|
||||
query.push_bind(segment.rollout_id().to_string());
|
||||
push_segment_range(&mut query, segment)?;
|
||||
for newer_segment in &lineage.segments()[segment_index + 1..] {
|
||||
|
||||
@@ -314,7 +314,7 @@ async fn paginated_live_append_materializes_turn_items_and_state() {
|
||||
.append_items(AppendThreadItemsParams {
|
||||
thread_id,
|
||||
items: vec![
|
||||
turn_started("turn-1"),
|
||||
turn_started_with_root("turn-1", "root-1"),
|
||||
completed_item(
|
||||
thread_id,
|
||||
"turn-1",
|
||||
@@ -370,6 +370,7 @@ async fn paginated_live_append_materializes_turn_items_and_state() {
|
||||
Option<i64>,
|
||||
Option<String>,
|
||||
Option<String>,
|
||||
Option<String>,
|
||||
),
|
||||
>(
|
||||
r#"
|
||||
@@ -383,7 +384,8 @@ SELECT
|
||||
completed_at,
|
||||
duration_ms,
|
||||
first_user_item_id,
|
||||
final_agent_item_id
|
||||
final_agent_item_id,
|
||||
root_turn_id
|
||||
FROM thread_turns
|
||||
WHERE thread_id = ? AND turn_id = ?
|
||||
"#,
|
||||
@@ -406,6 +408,7 @@ WHERE thread_id = ? AND turn_id = ?
|
||||
Some(10_000),
|
||||
Some("user-1".to_string()),
|
||||
Some("agent-1".to_string()),
|
||||
Some("root-1".to_string()),
|
||||
)
|
||||
);
|
||||
|
||||
@@ -1691,7 +1694,10 @@ async fn terminal_turn_does_not_change_after_later_records() {
|
||||
store
|
||||
.append_items(AppendThreadItemsParams {
|
||||
thread_id,
|
||||
items: vec![turn_started("turn-1"), turn_completed("turn-1")],
|
||||
items: vec![
|
||||
turn_started_with_root("turn-1", "root-1"),
|
||||
turn_completed("turn-1"),
|
||||
],
|
||||
})
|
||||
.await
|
||||
.expect("append terminal turn");
|
||||
@@ -1699,7 +1705,7 @@ async fn terminal_turn_does_not_change_after_later_records() {
|
||||
.append_items(AppendThreadItemsParams {
|
||||
thread_id,
|
||||
items: vec![
|
||||
turn_started("turn-1"),
|
||||
turn_started_with_root("turn-1", "wrong-root"),
|
||||
completed_item(
|
||||
thread_id,
|
||||
"turn-1",
|
||||
@@ -1736,6 +1742,7 @@ async fn terminal_turn_does_not_change_after_later_records() {
|
||||
Option<i64>,
|
||||
String,
|
||||
Option<String>,
|
||||
Option<String>,
|
||||
),
|
||||
>(
|
||||
r#"
|
||||
@@ -1745,7 +1752,8 @@ SELECT
|
||||
rollout_end_ordinal,
|
||||
rollout_end_byte_offset,
|
||||
status,
|
||||
first_user_item_id
|
||||
first_user_item_id,
|
||||
root_turn_id
|
||||
FROM thread_turns
|
||||
WHERE thread_id = ? AND turn_id = ?
|
||||
"#,
|
||||
@@ -1764,6 +1772,7 @@ WHERE thread_id = ? AND turn_id = ?
|
||||
Some(turn_end_byte_offset),
|
||||
"completed".to_string(),
|
||||
None,
|
||||
Some("root-1".to_string()),
|
||||
)
|
||||
);
|
||||
}
|
||||
@@ -2842,8 +2851,17 @@ fn turn_started(turn_id: &str) -> RolloutItem {
|
||||
}))
|
||||
}
|
||||
|
||||
fn turn_started_with_root(turn_id: &str, root_turn_id: &str) -> RolloutItem {
|
||||
let mut item = turn_started(turn_id);
|
||||
if let RolloutItem::EventMsg(EventMsg::TurnStarted(event)) = &mut item {
|
||||
event.root_turn_id = Some(root_turn_id.to_string());
|
||||
}
|
||||
item
|
||||
}
|
||||
|
||||
fn turn_completed(turn_id: &str) -> RolloutItem {
|
||||
RolloutItem::EventMsg(EventMsg::TurnComplete(TurnCompleteEvent {
|
||||
root_turn_id: None,
|
||||
turn_id: turn_id.to_string(),
|
||||
last_agent_message: None,
|
||||
error: None,
|
||||
|
||||
Reference in New Issue
Block a user