Preserve aggregate turn diffs across steering (#61820)

## Why

A steer can restart sampling within the same turn. Recreating the diff tracker loses earlier edits, and changing the working directory can change the meaning of relative diff paths.

## What changed

Keep the diff tracker for the lifetime of the turn and retain each environment's first display root. Refresh cached diff headers when new environments are added. Emit the final aggregate `TurnDiff` before `TurnComplete` instead of at sampling completion.

## Testing

Extend regression coverage for steering during the final flush, with and without additional patches and with a changed working directory. Check final diff ordering across continued sampling and header updates when adding an environment.

GitOrigin-RevId: fbd1ebef072d8486a324190c9125fc1446510802
This commit is contained in:
jif
2026-11-16 14:18:24 +00:00
committed by copyberry
parent 2309bae1ae
commit e5839cbc59
6 changed files with 164 additions and 74 deletions
+16
View File
@@ -42,6 +42,7 @@ use crate::state::ActiveTurn;
use crate::state::RunningTask;
use crate::state::TaskKind;
use crate::state::TurnState;
use crate::turn_diff_tracker::TurnDiffTracker;
use codex_analytics::TurnProfileFact;
use codex_analytics::TurnTokenUsageFact;
use codex_context_fragments::RenderedFragment;
@@ -61,6 +62,7 @@ use codex_protocol::protocol::TokenUsage;
use codex_protocol::protocol::TurnAbortReason;
use codex_protocol::protocol::TurnAbortedEvent;
use codex_protocol::protocol::TurnCompleteEvent;
use codex_protocol::protocol::TurnDiffEvent;
use codex_protocol::protocol::WarningEvent;
use codex_protocol::turn_input::NotSubmittedReason;
use codex_protocol::turn_input::TurnInputSubmission;
@@ -1015,6 +1017,7 @@ impl Session {
};
let saved_guardian_completion =
matches!(event, EventMsg::TurnComplete(_)) && self.is_private_guardian_reviewer().await;
self.finish_turn_diff(&turn_context, &event).await;
if !saved_guardian_completion {
self.send_event(turn_context.as_ref(), event.clone()).await;
}
@@ -1050,6 +1053,18 @@ impl Session {
None
}
async fn finish_turn_diff(&self, turn: &TurnContext, terminal_event: &EventMsg) {
let tracker = turn.extension_data.remove::<Mutex<TurnDiffTracker>>();
let (EventMsg::TurnComplete(_), Some(tracker)) = (terminal_event, tracker) else {
return;
};
let unified_diff = tracker.lock().await.get_unified_diff();
if let Some(unified_diff) = unified_diff {
self.send_event(turn, EventMsg::TurnDiff(TurnDiffEvent { unified_diff }))
.await;
}
}
async fn take_active_turn(&self, reason: &TurnAbortReason) -> Option<ActiveTurn> {
let mut active = self.active_turn.lock().await;
if matches!(
@@ -1223,6 +1238,7 @@ impl Session {
completed_at,
duration_ms,
});
self.finish_turn_diff(&task.turn_context, &event).await;
self.send_event(task.turn_context.as_ref(), event).await;
// Regular items were flushed before this terminal event was appended; buffering
// thread writers may not flush it without another explicit barrier.