mirror of
https://github.com/openai/codex.git
synced 2026-11-18 22:20:48 +00:00
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:
@@ -119,7 +119,6 @@ use codex_protocol::protocol::ReasoningContentDeltaEvent;
|
||||
use codex_protocol::protocol::ReasoningRawContentDeltaEvent;
|
||||
use codex_protocol::protocol::SafetyBufferingEvent;
|
||||
use codex_protocol::protocol::SessionSource;
|
||||
use codex_protocol::protocol::TurnDiffEvent;
|
||||
use codex_protocol::protocol::WarningEvent;
|
||||
use codex_protocol::user_input::UserInput;
|
||||
use codex_skills::ToolMentionKind;
|
||||
@@ -523,11 +522,14 @@ pub(crate) async fn run_turn(
|
||||
|
||||
let mut last_agent_message: Option<String> = None;
|
||||
let mut stop_hook_active = false;
|
||||
// Although from the perspective of codex.rs, TurnDiffTracker has the lifecycle of a Task which contains
|
||||
// many turns, from the perspective of the user, it is a single turn.
|
||||
let turn_diff_tracker = Arc::new(tokio::sync::Mutex::new(
|
||||
TurnDiffTracker::with_environment_display_roots(display_roots),
|
||||
));
|
||||
// A steer can reopen sampling within the same turn. Keep its aggregate until the turn ends.
|
||||
let turn_diff_tracker = turn_context
|
||||
.extension_data
|
||||
.get_or_init(|| tokio::sync::Mutex::new(TurnDiffTracker::new()));
|
||||
turn_diff_tracker
|
||||
.lock()
|
||||
.await
|
||||
.update_environment_display_roots(display_roots);
|
||||
|
||||
// `ModelClientSession` is turn-scoped and caches WebSocket + sticky routing state, so we reuse
|
||||
// one instance across retries within this turn.
|
||||
@@ -1974,7 +1976,7 @@ async fn run_sampling_request(
|
||||
let _code_mode_worker = sess.services.code_mode_service.start_turn_worker(
|
||||
&sess,
|
||||
Arc::clone(&step_context),
|
||||
Arc::clone(&turn_diff_tracker),
|
||||
turn_diff_tracker,
|
||||
);
|
||||
let max_retries = turn_context.provider.info().stream_max_retries();
|
||||
let incremental_tools = sess
|
||||
@@ -2072,7 +2074,6 @@ async fn run_sampling_request(
|
||||
Arc::clone(&turn_store),
|
||||
client_session,
|
||||
&responses_metadata,
|
||||
Arc::clone(&turn_diff_tracker),
|
||||
prompt,
|
||||
pending_audio,
|
||||
input_item_count,
|
||||
@@ -2944,7 +2945,6 @@ async fn try_run_sampling_request(
|
||||
turn_store: Arc<codex_extension_api::ExtensionData>,
|
||||
client_session: &mut ModelClientSession,
|
||||
responses_metadata: &CodexResponsesMetadata,
|
||||
turn_diff_tracker: SharedTurnDiffTracker,
|
||||
prompt: Prompt,
|
||||
mut pending_audio: Option<codex_history::AcceptedFileAudio>,
|
||||
input_item_count: usize,
|
||||
@@ -3063,7 +3063,6 @@ async fn try_run_sampling_request(
|
||||
String,
|
||||
Box<dyn ToolArgumentDiffConsumer>,
|
||||
)> = None;
|
||||
let mut should_emit_turn_diff = false;
|
||||
let mut should_emit_token_count = false;
|
||||
const MAX_ANALYTICS_TOOL_CALL_IDS_PER_RESPONSE: usize = 256;
|
||||
let mut analytics_tool_call_ids = Vec::new();
|
||||
@@ -3616,7 +3615,6 @@ async fn try_run_sampling_request(
|
||||
)
|
||||
.await;
|
||||
should_emit_token_count = true;
|
||||
should_emit_turn_diff = true;
|
||||
if let Err(err) = budget_result {
|
||||
break Err(err);
|
||||
}
|
||||
@@ -3832,17 +3830,6 @@ async fn try_run_sampling_request(
|
||||
return Err(CodexErr::TurnAborted);
|
||||
}
|
||||
|
||||
if should_emit_turn_diff {
|
||||
let unified_diff = {
|
||||
let tracker = turn_diff_tracker.lock().await;
|
||||
tracker.get_unified_diff()
|
||||
};
|
||||
if let Some(unified_diff) = unified_diff {
|
||||
let msg = EventMsg::TurnDiff(TurnDiffEvent { unified_diff });
|
||||
sess.clone().send_event(&turn_context, msg).await;
|
||||
}
|
||||
}
|
||||
|
||||
outcome
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user