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
+1
View File
@@ -8406,6 +8406,7 @@ async fn prompt_edit_reverts_earlier_and_first_visible_prompts_in_place() -> Res
);
for item in [
RolloutItem::EventMsg(EventMsg::TurnStarted(TurnStartedEvent {
turn_attribution: None,
turn_id: turn_id.to_string(),
root_turn_id: None,
trace_id: None,
@@ -55,6 +55,7 @@ async fn history_fixture(
for (index, count) in item_counts.iter().enumerate() {
let turn_id = format!("turn-{index}");
let mut events = vec![EventMsg::TurnStarted(TurnStartedEvent {
turn_attribution: None,
turn_id: turn_id.clone(),
root_turn_id: None,
trace_id: None,
@@ -57,6 +57,7 @@ pub(super) async fn completed_history_app(
let turn_id = format!("turn-{index}");
let finished = completed_at.timestamp() + index as i64 * 60;
let mut events = vec![EventMsg::TurnStarted(TurnStartedEvent {
turn_attribution: None,
turn_id: turn_id.clone(),
root_turn_id: None,
trace_id: None,
@@ -2320,6 +2320,7 @@ async fn older_pagination_reconciles_review_prompts_across_page_boundaries() ->
user_item("newer-visible-prompt", "newer visible prompt"),
]);
let events = std::iter::once(EventMsg::TurnStarted(TurnStartedEvent {
turn_attribution: None,
turn_id: "cross-page-review-turn".to_string(),
root_turn_id: None,
trace_id: None,
@@ -2483,6 +2484,7 @@ async fn transcript_alt_beginning_loads_every_older_history_page() -> Result<()>
.map(serde_json::from_str::<serde_json::Value>)
.collect::<Result<Vec<_>, _>>()?;
let events = std::iter::once(EventMsg::TurnStarted(TurnStartedEvent {
turn_attribution: None,
turn_id: "multi-page-turn".to_string(),
root_turn_id: None,
trace_id: None,
@@ -2898,6 +2900,7 @@ async fn underfilled_scrollback_fetches_older_pages_without_opening_the_transcri
.map(serde_json::from_str::<serde_json::Value>)
.collect::<Result<Vec<_>, _>>()?;
let events = std::iter::once(EventMsg::TurnStarted(TurnStartedEvent {
turn_attribution: None,
turn_id: "scrollback-pagination-turn".to_string(),
root_turn_id: None,
trace_id: None,
@@ -4964,6 +4967,7 @@ async fn command_center_read_only_open_requests_and_failure_preservation() -> Re
let first_ordinal = contents.lines().count();
let events = [
EventMsg::TurnStarted(TurnStartedEvent {
turn_attribution: None,
turn_id: format!("saved-turn-{index}"),
root_turn_id: None,
trace_id: None,