Preserve turn attribution across recovery and compaction (#51402)

## Why

Recovering an interrupted turn replaced its original trigger with `retry` and restored only its root lineage from model context. Turns suspended before that context was written could lose their attribution entirely.

## What changed

- Persist regular-turn attribution in `TurnStartedEvent` and compaction resume metadata, including the trigger, parent and root turn IDs, and initiating agent path.
- Restore matching attribution during recovery, keep unknown triggers absent, and retain the model-context root fallback for older records.
- Reconstruct attribution from surviving turns across rollback and late completion or abort events, and clear inherited attribution when spawning a forked agent.

## Testing

Add coverage for repeated cold recovery, suspension before context persistence, legacy rollouts, fresh attribution on subsequent turns, compaction checkpoints, rollback, and late terminal events.

GitOrigin-RevId: 85b5763e8fbd48a1ea2c07cfda8196e63c2cd33a
This commit is contained in:
Eddie Chen
2026-10-06 15:50:27 +00:00
committed by copyberry
parent a4ebc509f4
commit 551bd409eb
50 changed files with 912 additions and 38 deletions
@@ -3089,6 +3089,7 @@ mod tests {
state.track_current_turn_event(
"turn-1",
&EventMsg::TurnStarted(codex_protocol::protocol::TurnStartedEvent {
turn_attribution: None,
turn_id: "turn-1".to_string(),
root_turn_id: None,
trace_id: None,
@@ -3125,6 +3126,7 @@ mod tests {
Event {
id: "turn-1".to_string(),
msg: EventMsg::TurnStarted(codex_protocol::protocol::TurnStartedEvent {
turn_attribution: None,
turn_id: "turn-1".to_string(),
root_turn_id: None,
trace_id: None,
@@ -3395,6 +3397,7 @@ mod tests {
state.track_current_turn_event(
&event_turn_id,
&EventMsg::TurnStarted(codex_protocol::protocol::TurnStartedEvent {
turn_attribution: None,
turn_id: event_turn_id.clone(),
root_turn_id: None,
trace_id: None,
@@ -1199,6 +1199,7 @@ mod thread_processor_behavior_tests {
state.track_current_turn_event(
"turn-1",
&EventMsg::TurnStarted(codex_protocol::protocol::TurnStartedEvent {
turn_attribution: None,
turn_id: "turn-1".to_string(),
root_turn_id: None,
trace_id: None,
@@ -87,6 +87,7 @@ async fn handle_observes_only_matching_model_provider() {
let event = Event {
id: "turn-1".to_string(),
msg: EventMsg::TurnStarted(TurnStartedEvent {
turn_attribution: None,
turn_id: "turn-1".to_string(),
root_turn_id: None,
trace_id: None,
@@ -1249,6 +1249,7 @@ async fn thread_fork_can_cut_before_unfinished_stored_turn() -> Result<()> {
append_rollout_item_to_path(
&source_path,
&RolloutItem::EventMsg(EventMsg::TurnStarted(TurnStartedEvent {
turn_attribution: None,
turn_id: unfinished_turn_id.to_string(),
root_turn_id: None,
trace_id: None,
@@ -1524,6 +1525,7 @@ async fn thread_fork_creates_reference_backed_paginated_thread() -> Result<()> {
);
for item in [
RolloutItem::EventMsg(EventMsg::TurnStarted(TurnStartedEvent {
turn_attribution: None,
turn_id: "turn-1".to_string(),
root_turn_id: None,
trace_id: None,
@@ -1854,6 +1856,7 @@ async fn assert_thread_fork_freezes_active_paginated_turn_as_interrupted(
append_rollout_item_to_path(
source_path.as_path(),
&RolloutItem::EventMsg(EventMsg::TurnStarted(TurnStartedEvent {
turn_attribution: None,
turn_id: "active-turn".to_string(),
root_turn_id: None,
trace_id: None,
@@ -2426,6 +2426,7 @@ async fn read_items_page(
fn paginated_turn_started(turn_id: &str) -> RolloutItem {
RolloutItem::EventMsg(EventMsg::TurnStarted(TurnStartedEvent {
turn_attribution: None,
turn_id: turn_id.to_string(),
root_turn_id: None,
trace_id: None,
@@ -3915,6 +3915,7 @@ async fn cold_paginated_resume_restores_usage_without_loading_turns() -> Result<
append_rollout_item_to_path(
&path,
&RolloutItem::EventMsg(EventMsg::TurnStarted(TurnStartedEvent {
turn_attribution: None,
turn_id: canonical_turn_id.to_string(),
root_turn_id: None,
trace_id: None,
@@ -4027,6 +4028,7 @@ async fn cold_paginated_resume_omits_usage_when_its_turn_is_ambiguous() -> Resul
append_rollout_item_to_path(
&path,
&RolloutItem::EventMsg(EventMsg::TurnStarted(TurnStartedEvent {
turn_attribution: None,
turn_id: interrupted_turn_id.to_string(),
root_turn_id: None,
trace_id: None,
@@ -4170,6 +4172,7 @@ async fn thread_resume_token_usage_replay_ignores_stale_interrupted_tail_turn()
"timestamp": meta_rfc3339,
"type": "event_msg",
"payload": serde_json::to_value(EventMsg::TurnStarted(TurnStartedEvent {
turn_attribution: None,
turn_id: stale_turn_id.to_string(),
root_turn_id: None,
trace_id: None,
@@ -4259,6 +4262,7 @@ async fn thread_resume_token_usage_replay_can_belong_to_interrupted_turn() -> Re
"timestamp": meta_rfc3339,
"type": "event_msg",
"payload": serde_json::to_value(EventMsg::TurnStarted(TurnStartedEvent {
turn_attribution: None,
turn_id: interrupted_turn_id.to_string(),
root_turn_id: None,
trace_id: None,
@@ -4576,6 +4580,7 @@ async fn thread_resume_and_read_interrupt_incomplete_rollout_turn_when_thread_is
"timestamp": meta_rfc3339,
"type": "event_msg",
"payload": serde_json::to_value(EventMsg::TurnStarted(TurnStartedEvent {
turn_attribution: None,
turn_id: turn_id.to_string(),
root_turn_id: None,
trace_id: None,
@@ -98,6 +98,7 @@ async fn timeline_pages_mix_items_and_resolve_the_opening_realtime_session() ->
content: RealtimeItemContent::RealtimeSessionStarted,
}),
RolloutItem::EventMsg(EventMsg::TurnStarted(TurnStartedEvent {
turn_attribution: None,
turn_id: "turn-1".to_string(),
root_turn_id: None,
trace_id: None,