mirror of
https://github.com/openai/codex.git
synced 2026-11-29 00:00:54 +00:00
codex: address PR review feedback (#29725)
This commit is contained in:
@@ -1,4 +1,5 @@
|
||||
use std::sync::Arc;
|
||||
use std::time::Duration;
|
||||
|
||||
use codex_core::ForkSnapshot;
|
||||
use codex_core::NewThread;
|
||||
@@ -10,6 +11,8 @@ use codex_protocol::protocol::Op;
|
||||
use codex_protocol::protocol::ResumedHistory;
|
||||
use codex_protocol::protocol::RolloutItem;
|
||||
use codex_protocol::protocol::RolloutLine;
|
||||
use codex_protocol::protocol::TurnAbortReason;
|
||||
use codex_protocol::protocol::TurnAbortedEvent;
|
||||
use codex_protocol::user_input::UserInput;
|
||||
use core_test_support::responses::ev_completed;
|
||||
use core_test_support::responses::ev_response_created;
|
||||
@@ -150,6 +153,123 @@ async fn fork_thread_twice_drops_to_first_message() {
|
||||
);
|
||||
}
|
||||
|
||||
#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
|
||||
async fn interrupted_fork_closes_persisted_in_progress_turn() {
|
||||
skip_if_no_network!();
|
||||
|
||||
let server = MockServer::start().await;
|
||||
Mock::given(method("POST"))
|
||||
.and(path("/v1/responses"))
|
||||
.respond_with(
|
||||
ResponseTemplate::new(200)
|
||||
.insert_header("content-type", "text/event-stream")
|
||||
.set_body_raw(
|
||||
sse(vec![
|
||||
ev_response_created("delayed-response"),
|
||||
ev_completed("delayed-response"),
|
||||
]),
|
||||
"text/event-stream",
|
||||
)
|
||||
.set_delay(Duration::from_secs(/*secs*/ 60)),
|
||||
)
|
||||
.mount(&server)
|
||||
.await;
|
||||
|
||||
let mut builder = test_codex();
|
||||
let test = builder
|
||||
.build_with_auto_env(&server)
|
||||
.await
|
||||
.expect("create conversation");
|
||||
let codex = test.codex.clone();
|
||||
|
||||
codex
|
||||
.submit(Op::UserInput {
|
||||
items: vec![UserInput::Text {
|
||||
text: "leave this turn in progress".to_string(),
|
||||
text_elements: Vec::new(),
|
||||
}],
|
||||
final_output_json_schema: None,
|
||||
responsesapi_client_metadata: None,
|
||||
additional_context: Default::default(),
|
||||
thread_settings: Default::default(),
|
||||
})
|
||||
.await
|
||||
.expect("submit in-progress turn");
|
||||
let started = wait_for_event(&codex, |event| matches!(event, EventMsg::TurnStarted(_))).await;
|
||||
let EventMsg::TurnStarted(started) = started else {
|
||||
unreachable!("wait predicate only accepts TurnStarted")
|
||||
};
|
||||
let turn_id = started.turn_id;
|
||||
|
||||
codex.flush_rollout().await.expect("flush source rollout");
|
||||
let source_path = codex.rollout_path().expect("source rollout path");
|
||||
let source_items = read_rollout_items(&source_path);
|
||||
let NewThread {
|
||||
thread: forked_thread,
|
||||
..
|
||||
} = test
|
||||
.thread_manager
|
||||
.fork_thread(
|
||||
ForkSnapshot::Interrupted,
|
||||
test.config.clone(),
|
||||
source_path,
|
||||
/*thread_source*/ None,
|
||||
/*parent_trace*/ None,
|
||||
)
|
||||
.await
|
||||
.expect("fork interrupted snapshot");
|
||||
let forked_path = forked_thread.rollout_path().expect("forked rollout path");
|
||||
let forked_items = read_rollout_items(&forked_path);
|
||||
|
||||
codex
|
||||
.submit(Op::Interrupt)
|
||||
.await
|
||||
.expect("interrupt source turn");
|
||||
wait_for_event(&codex, |event| {
|
||||
matches!(
|
||||
event,
|
||||
EventMsg::TurnAborted(event)
|
||||
if event.turn_id.as_deref() == Some(turn_id.as_str())
|
||||
)
|
||||
})
|
||||
.await;
|
||||
|
||||
let source_values = source_items
|
||||
.iter()
|
||||
.map(|item| serde_json::to_value(item).expect("serialize source rollout item"))
|
||||
.collect::<Vec<_>>();
|
||||
let forked_values = forked_items
|
||||
.iter()
|
||||
.map(|item| serde_json::to_value(item).expect("serialize forked rollout item"))
|
||||
.collect::<Vec<_>>();
|
||||
assert!(
|
||||
forked_values.starts_with(&source_values),
|
||||
"forked history should preserve the persisted in-progress source prefix"
|
||||
);
|
||||
|
||||
let aborts = forked_items
|
||||
.iter()
|
||||
.filter_map(|item| match item {
|
||||
RolloutItem::EventMsg(EventMsg::TurnAborted(event)) => {
|
||||
Some(serde_json::to_value(event).expect("serialize persisted abort"))
|
||||
}
|
||||
_ => None,
|
||||
})
|
||||
.collect::<Vec<_>>();
|
||||
pretty_assertions::assert_eq!(
|
||||
aborts,
|
||||
vec![
|
||||
serde_json::to_value(TurnAbortedEvent {
|
||||
turn_id: Some(turn_id),
|
||||
reason: TurnAbortReason::Interrupted,
|
||||
completed_at: None,
|
||||
duration_ms: None,
|
||||
})
|
||||
.expect("serialize expected abort")
|
||||
]
|
||||
);
|
||||
}
|
||||
|
||||
#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
|
||||
async fn fork_thread_from_history_does_not_require_source_rollout_path() {
|
||||
skip_if_no_network!();
|
||||
|
||||
@@ -1,5 +1,7 @@
|
||||
use codex_protocol::items::parse_hook_prompt_message;
|
||||
use codex_protocol::protocol::EventMsg;
|
||||
use codex_protocol::protocol::GuardianAssessmentAction;
|
||||
use codex_protocol::protocol::GuardianAssessmentStatus;
|
||||
use codex_protocol::protocol::RolloutItem;
|
||||
|
||||
/// Whether the current explicit turn is still running or has reached a terminal state.
|
||||
@@ -97,13 +99,37 @@ impl RolloutTurnLifecycleTracker {
|
||||
EventMsg::PatchApplyEnd(event) if event.turn_id.is_empty() => {
|
||||
self.materialize_implicit_turn();
|
||||
}
|
||||
EventMsg::DynamicToolCallRequest(event) if event.turn_id.is_empty() => {
|
||||
self.materialize_implicit_turn();
|
||||
}
|
||||
EventMsg::DynamicToolCallResponse(event) if event.turn_id.is_empty() => {
|
||||
self.materialize_implicit_turn();
|
||||
}
|
||||
EventMsg::GuardianAssessment(event)
|
||||
if event.turn_id.is_empty()
|
||||
&& event.status != GuardianAssessmentStatus::Approved
|
||||
&& event.target_item_id.is_some()
|
||||
&& matches!(
|
||||
&event.action,
|
||||
GuardianAssessmentAction::Command { .. }
|
||||
| GuardianAssessmentAction::Execve { .. }
|
||||
) =>
|
||||
{
|
||||
self.materialize_implicit_turn();
|
||||
}
|
||||
EventMsg::ContextCompacted(_)
|
||||
| EventMsg::EnteredReviewMode(_)
|
||||
| EventMsg::ExitedReviewMode(_)
|
||||
| EventMsg::McpToolCallEnd(_)
|
||||
| EventMsg::WebSearchEnd(_)
|
||||
| EventMsg::ImageGenerationEnd(_)
|
||||
| EventMsg::SubAgentActivity(_) => self.materialize_implicit_turn(),
|
||||
| EventMsg::SubAgentActivity(_)
|
||||
| EventMsg::ViewImageToolCall(_)
|
||||
| EventMsg::CollabAgentSpawnEnd(_)
|
||||
| EventMsg::CollabAgentInteractionEnd(_)
|
||||
| EventMsg::CollabWaitingEnd(_)
|
||||
| EventMsg::CollabCloseEnd(_)
|
||||
| EventMsg::CollabResumeEnd(_) => self.materialize_implicit_turn(),
|
||||
EventMsg::TurnStarted(event) => {
|
||||
self.finish_current_turn();
|
||||
self.current_turn = Some(CurrentTurn::Explicit(CurrentExplicitTurn {
|
||||
|
||||
@@ -1,3 +1,4 @@
|
||||
use codex_protocol::dynamic_tools::DynamicToolCallRequest;
|
||||
use codex_protocol::items::HookPromptFragment;
|
||||
use codex_protocol::items::build_hook_prompt_message;
|
||||
use codex_protocol::models::ResponseItem;
|
||||
@@ -14,7 +15,6 @@ use codex_protocol::protocol::TurnStartedEvent;
|
||||
use codex_protocol::protocol::UserMessageEvent;
|
||||
use pretty_assertions::assert_eq;
|
||||
|
||||
use super::CurrentExplicitTurn;
|
||||
use super::ExplicitTurnState;
|
||||
use super::RolloutTurnLifecycleTracker;
|
||||
|
||||
@@ -93,6 +93,11 @@ fn observe(tracker: &mut RolloutTurnLifecycleTracker, items: &[RolloutItem]) {
|
||||
}
|
||||
}
|
||||
|
||||
fn current_turn(tracker: &RolloutTurnLifecycleTracker) -> Option<(&str, usize, ExplicitTurnState)> {
|
||||
tracker
|
||||
.current_explicit_turn()
|
||||
.map(|turn| (turn.turn_id.as_str(), turn.rollout_start_index, turn.state))
|
||||
}
|
||||
#[test]
|
||||
fn records_raw_rollout_index_for_explicit_turn_start() {
|
||||
let mut tracker = RolloutTurnLifecycleTracker::new();
|
||||
@@ -106,12 +111,8 @@ fn records_raw_rollout_index_for_explicit_turn_start() {
|
||||
);
|
||||
|
||||
assert_eq!(
|
||||
tracker.current_explicit_turn(),
|
||||
Some(&CurrentExplicitTurn {
|
||||
turn_id: "turn-a".to_string(),
|
||||
rollout_start_index: 2,
|
||||
state: ExplicitTurnState::InProgress,
|
||||
})
|
||||
current_turn(&tracker),
|
||||
Some(("turn-a", 2, ExplicitTurnState::InProgress))
|
||||
);
|
||||
}
|
||||
|
||||
@@ -123,12 +124,8 @@ fn complete_closes_while_abort_and_error_retain_terminal_current_turn() {
|
||||
&[turn_started("turn-a"), turn_aborted(Some("turn-a"))],
|
||||
);
|
||||
assert_eq!(
|
||||
tracker.current_explicit_turn(),
|
||||
Some(&CurrentExplicitTurn {
|
||||
turn_id: "turn-a".to_string(),
|
||||
rollout_start_index: 0,
|
||||
state: ExplicitTurnState::Terminal,
|
||||
})
|
||||
current_turn(&tracker),
|
||||
Some(("turn-a", 0, ExplicitTurnState::Terminal))
|
||||
);
|
||||
|
||||
tracker.handle_rollout_item(&turn_complete("turn-a"));
|
||||
@@ -140,12 +137,8 @@ fn complete_closes_while_abort_and_error_retain_terminal_current_turn() {
|
||||
codex_error_info: None,
|
||||
})));
|
||||
assert_eq!(
|
||||
tracker.current_explicit_turn(),
|
||||
Some(&CurrentExplicitTurn {
|
||||
turn_id: "turn-b".to_string(),
|
||||
rollout_start_index: 3,
|
||||
state: ExplicitTurnState::Terminal,
|
||||
})
|
||||
current_turn(&tracker),
|
||||
Some(("turn-b", 3, ExplicitTurnState::Terminal))
|
||||
);
|
||||
}
|
||||
|
||||
@@ -158,12 +151,8 @@ fn second_start_finishes_and_replaces_current_turn() {
|
||||
);
|
||||
|
||||
assert_eq!(
|
||||
tracker.current_explicit_turn(),
|
||||
Some(&CurrentExplicitTurn {
|
||||
turn_id: "turn-b".to_string(),
|
||||
rollout_start_index: 1,
|
||||
state: ExplicitTurnState::InProgress,
|
||||
})
|
||||
current_turn(&tracker),
|
||||
Some(("turn-b", 1, ExplicitTurnState::InProgress))
|
||||
);
|
||||
}
|
||||
|
||||
@@ -181,22 +170,14 @@ fn late_historical_ids_do_not_affect_current_but_unknown_ids_do() {
|
||||
],
|
||||
);
|
||||
assert_eq!(
|
||||
tracker.current_explicit_turn(),
|
||||
Some(&CurrentExplicitTurn {
|
||||
turn_id: "turn-b".to_string(),
|
||||
rollout_start_index: 2,
|
||||
state: ExplicitTurnState::InProgress,
|
||||
})
|
||||
current_turn(&tracker),
|
||||
Some(("turn-b", 2, ExplicitTurnState::InProgress))
|
||||
);
|
||||
|
||||
tracker.handle_rollout_item(&turn_aborted(Some("unknown")));
|
||||
assert_eq!(
|
||||
tracker.current_explicit_turn(),
|
||||
Some(&CurrentExplicitTurn {
|
||||
turn_id: "turn-b".to_string(),
|
||||
rollout_start_index: 2,
|
||||
state: ExplicitTurnState::Terminal,
|
||||
})
|
||||
current_turn(&tracker),
|
||||
Some(("turn-b", 2, ExplicitTurnState::Terminal))
|
||||
);
|
||||
|
||||
tracker.handle_rollout_item(&turn_complete("unknown"));
|
||||
@@ -216,12 +197,8 @@ fn rollback_zero_finishes_current_and_rolled_back_ids_become_unknown() {
|
||||
],
|
||||
);
|
||||
assert_eq!(
|
||||
tracker.current_explicit_turn(),
|
||||
Some(&CurrentExplicitTurn {
|
||||
turn_id: "turn-b".to_string(),
|
||||
rollout_start_index: 2,
|
||||
state: ExplicitTurnState::InProgress,
|
||||
})
|
||||
current_turn(&tracker),
|
||||
Some(("turn-b", 2, ExplicitTurnState::InProgress))
|
||||
);
|
||||
|
||||
observe(
|
||||
@@ -250,12 +227,8 @@ fn implicit_turn_placeholders_keep_rollback_late_id_matching_aligned() {
|
||||
],
|
||||
);
|
||||
assert_eq!(
|
||||
tracker.current_explicit_turn(),
|
||||
Some(&CurrentExplicitTurn {
|
||||
turn_id: "turn-b".to_string(),
|
||||
rollout_start_index: 4,
|
||||
state: ExplicitTurnState::InProgress,
|
||||
})
|
||||
current_turn(&tracker),
|
||||
Some(("turn-b", 4, ExplicitTurnState::InProgress))
|
||||
);
|
||||
|
||||
let mut tracker = RolloutTurnLifecycleTracker::new();
|
||||
@@ -289,15 +262,51 @@ fn non_user_materialized_turn_adds_an_implicit_rollback_placeholder() {
|
||||
);
|
||||
|
||||
assert_eq!(
|
||||
tracker.current_explicit_turn(),
|
||||
Some(&CurrentExplicitTurn {
|
||||
turn_id: "turn-b".to_string(),
|
||||
rollout_start_index: 4,
|
||||
state: ExplicitTurnState::InProgress,
|
||||
})
|
||||
current_turn(&tracker),
|
||||
Some(("turn-b", 4, ExplicitTurnState::InProgress))
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn legacy_current_turn_events_add_implicit_rollback_placeholders() {
|
||||
let events = [
|
||||
EventMsg::ViewImageToolCall(
|
||||
serde_json::from_value(serde_json::json!({
|
||||
"call_id": "image-1",
|
||||
"path": "file:///tmp/image.png",
|
||||
}))
|
||||
.expect("valid view image event"),
|
||||
),
|
||||
EventMsg::DynamicToolCallRequest(DynamicToolCallRequest {
|
||||
call_id: "dynamic-1".to_string(),
|
||||
turn_id: String::new(),
|
||||
started_at_ms: 0,
|
||||
namespace: None,
|
||||
tool: "lookup".to_string(),
|
||||
arguments: serde_json::json!({}),
|
||||
}),
|
||||
];
|
||||
|
||||
for event in events {
|
||||
let mut tracker = RolloutTurnLifecycleTracker::new();
|
||||
observe(
|
||||
&mut tracker,
|
||||
&[
|
||||
turn_started("turn-a"),
|
||||
turn_complete("turn-a"),
|
||||
RolloutItem::EventMsg(event),
|
||||
rollback(/*num_turns*/ 1),
|
||||
turn_started("turn-b"),
|
||||
turn_complete("turn-a"),
|
||||
],
|
||||
);
|
||||
assert_eq!(
|
||||
current_turn(&tracker),
|
||||
Some(("turn-b", 4, ExplicitTurnState::InProgress))
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn first_user_message_reuses_compaction_only_turn_and_second_starts_another() {
|
||||
let mut tracker = RolloutTurnLifecycleTracker::new();
|
||||
@@ -334,12 +343,8 @@ fn hook_prompt_adds_an_implicit_rollback_placeholder() {
|
||||
);
|
||||
|
||||
assert_eq!(
|
||||
tracker.current_explicit_turn(),
|
||||
Some(&CurrentExplicitTurn {
|
||||
turn_id: "turn-b".to_string(),
|
||||
rollout_start_index: 4,
|
||||
state: ExplicitTurnState::InProgress,
|
||||
})
|
||||
current_turn(&tracker),
|
||||
Some(("turn-b", 4, ExplicitTurnState::InProgress))
|
||||
);
|
||||
}
|
||||
|
||||
@@ -364,12 +369,8 @@ fn hook_prompt_materializes_compaction_slot_before_user_starts_another() {
|
||||
],
|
||||
);
|
||||
assert_eq!(
|
||||
tracker.current_explicit_turn(),
|
||||
Some(&CurrentExplicitTurn {
|
||||
turn_id: "turn-b".to_string(),
|
||||
rollout_start_index: 6,
|
||||
state: ExplicitTurnState::InProgress,
|
||||
})
|
||||
current_turn(&tracker),
|
||||
Some(("turn-b", 6, ExplicitTurnState::InProgress))
|
||||
);
|
||||
|
||||
let mut tracker = RolloutTurnLifecycleTracker::new();
|
||||
|
||||
Reference in New Issue
Block a user