mirror of
https://github.com/openai/codex.git
synced 2026-11-19 22:30:42 +00:00
Fix persisted stale plan cleanup
This commit is contained in:
@@ -1226,8 +1226,6 @@ impl Session {
|
||||
let mut state = self.state.lock().await;
|
||||
state.set_token_info(Some(info));
|
||||
}
|
||||
self.hydrate_latest_plan_update_from_rollout(&rollout_items)
|
||||
.await;
|
||||
|
||||
// Defer seeding the session's initial context until the first turn starts so
|
||||
// turn/start overrides can be merged before we write to the rollout.
|
||||
@@ -1245,8 +1243,6 @@ impl Session {
|
||||
let mut state = self.state.lock().await;
|
||||
state.set_token_info(Some(info));
|
||||
}
|
||||
self.hydrate_latest_plan_update_from_rollout(&rollout_items)
|
||||
.await;
|
||||
|
||||
// If persisting, persist all rollout items as-is (the store filters).
|
||||
if !rollout_items.is_empty() {
|
||||
@@ -1278,6 +1274,10 @@ impl Session {
|
||||
reconstructed_rollout.reference_context_item,
|
||||
)
|
||||
.await;
|
||||
{
|
||||
let mut state = self.state.lock().await;
|
||||
state.set_latest_plan_update(reconstructed_rollout.latest_plan_update);
|
||||
}
|
||||
self.set_previous_turn_settings(previous_turn_settings.clone())
|
||||
.await;
|
||||
previous_turn_settings
|
||||
@@ -1290,19 +1290,6 @@ impl Session {
|
||||
})
|
||||
}
|
||||
|
||||
fn last_plan_update_from_rollout(rollout_items: &[RolloutItem]) -> Option<UpdatePlanArgs> {
|
||||
rollout_items.iter().rev().find_map(|item| match item {
|
||||
RolloutItem::EventMsg(EventMsg::PlanUpdate(update)) => Some(update.clone()),
|
||||
_ => None,
|
||||
})
|
||||
}
|
||||
|
||||
async fn hydrate_latest_plan_update_from_rollout(&self, rollout_items: &[RolloutItem]) {
|
||||
let latest_plan_update = Self::last_plan_update_from_rollout(rollout_items);
|
||||
let mut state = self.state.lock().await;
|
||||
state.set_latest_plan_update(latest_plan_update);
|
||||
}
|
||||
|
||||
async fn previous_turn_settings(&self) -> Option<PreviousTurnSettings> {
|
||||
let state = self.state.lock().await;
|
||||
state.previous_turn_settings()
|
||||
@@ -1641,8 +1628,9 @@ impl Session {
|
||||
pub(crate) async fn reset_in_progress_plan_steps_for_terminal_turn(
|
||||
&self,
|
||||
turn_context: &TurnContext,
|
||||
preserve_active_goals: bool,
|
||||
) {
|
||||
if self.has_active_thread_goal_for_plan_cleanup().await {
|
||||
if preserve_active_goals && self.has_active_thread_goal_for_plan_cleanup().await {
|
||||
return;
|
||||
}
|
||||
|
||||
|
||||
@@ -8,6 +8,7 @@ pub(super) struct RolloutReconstruction {
|
||||
pub(super) history: Vec<ResponseItem>,
|
||||
pub(super) previous_turn_settings: Option<PreviousTurnSettings>,
|
||||
pub(super) reference_context_item: Option<TurnContextItem>,
|
||||
pub(super) latest_plan_update: Option<UpdatePlanArgs>,
|
||||
}
|
||||
|
||||
#[derive(Debug, Default)]
|
||||
@@ -33,6 +34,7 @@ struct ActiveReplaySegment<'a> {
|
||||
previous_turn_settings: Option<PreviousTurnSettings>,
|
||||
reference_context_item: TurnReferenceContextItem,
|
||||
base_replacement_history: Option<&'a [ResponseItem]>,
|
||||
latest_plan_update: Option<UpdatePlanArgs>,
|
||||
}
|
||||
|
||||
fn turn_ids_are_compatible(active_turn_id: Option<&str>, item_turn_id: Option<&str>) -> bool {
|
||||
@@ -45,6 +47,7 @@ fn finalize_active_segment<'a>(
|
||||
base_replacement_history: &mut Option<&'a [ResponseItem]>,
|
||||
previous_turn_settings: &mut Option<PreviousTurnSettings>,
|
||||
reference_context_item: &mut TurnReferenceContextItem,
|
||||
latest_plan_update: &mut Option<UpdatePlanArgs>,
|
||||
pending_rollback_turns: &mut usize,
|
||||
) {
|
||||
// Thread rollback drops the newest surviving real user-message boundaries. In replay, that
|
||||
@@ -81,6 +84,14 @@ fn finalize_active_segment<'a>(
|
||||
{
|
||||
*reference_context_item = active_segment.reference_context_item;
|
||||
}
|
||||
|
||||
// Plan updates follow turn rollback semantics too. The newest surviving reverse-replay
|
||||
// segment that contains one becomes the plan snapshot to hydrate into live session state.
|
||||
if latest_plan_update.is_none()
|
||||
&& let Some(segment_latest_plan_update) = active_segment.latest_plan_update
|
||||
{
|
||||
*latest_plan_update = Some(segment_latest_plan_update);
|
||||
}
|
||||
}
|
||||
|
||||
impl Session {
|
||||
@@ -97,6 +108,7 @@ impl Session {
|
||||
let mut base_replacement_history: Option<&[ResponseItem]> = None;
|
||||
let mut previous_turn_settings = None;
|
||||
let mut reference_context_item = TurnReferenceContextItem::NeverSet;
|
||||
let mut latest_plan_update = None;
|
||||
// Rollback is "drop the newest N user turns". While scanning in reverse, that becomes
|
||||
// "skip the next N user-turn segments we finalize".
|
||||
let mut pending_rollback_turns = 0usize;
|
||||
@@ -198,10 +210,18 @@ impl Session {
|
||||
&mut base_replacement_history,
|
||||
&mut previous_turn_settings,
|
||||
&mut reference_context_item,
|
||||
&mut latest_plan_update,
|
||||
&mut pending_rollback_turns,
|
||||
);
|
||||
}
|
||||
}
|
||||
RolloutItem::EventMsg(EventMsg::PlanUpdate(update)) => {
|
||||
let active_segment =
|
||||
active_segment.get_or_insert_with(ActiveReplaySegment::default);
|
||||
if active_segment.latest_plan_update.is_none() {
|
||||
active_segment.latest_plan_update = Some(update.clone());
|
||||
}
|
||||
}
|
||||
RolloutItem::ResponseItem(response_item) => {
|
||||
let active_segment =
|
||||
active_segment.get_or_insert_with(ActiveReplaySegment::default);
|
||||
@@ -213,6 +233,7 @@ impl Session {
|
||||
if base_replacement_history.is_some()
|
||||
&& previous_turn_settings.is_some()
|
||||
&& !matches!(reference_context_item, TurnReferenceContextItem::NeverSet)
|
||||
&& latest_plan_update.is_some()
|
||||
{
|
||||
// At this point we have both eager resume metadata values and the replacement-
|
||||
// history base for the surviving tail, so older rollout items cannot affect this
|
||||
@@ -227,6 +248,7 @@ impl Session {
|
||||
&mut base_replacement_history,
|
||||
&mut previous_turn_settings,
|
||||
&mut reference_context_item,
|
||||
&mut latest_plan_update,
|
||||
&mut pending_rollback_turns,
|
||||
);
|
||||
}
|
||||
@@ -296,6 +318,7 @@ impl Session {
|
||||
history: history.raw_items().to_vec(),
|
||||
previous_turn_settings,
|
||||
reference_context_item,
|
||||
latest_plan_update,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -2622,6 +2622,93 @@ async fn thread_rollback_recomputes_previous_turn_settings_and_reference_context
|
||||
);
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn thread_rollback_refreshes_latest_plan_update_from_surviving_history() {
|
||||
let (mut sess, _tc, rx) = make_session_and_context_with_rx().await;
|
||||
attach_thread_persistence(
|
||||
Arc::get_mut(&mut sess).expect("session should not have additional references"),
|
||||
)
|
||||
.await;
|
||||
|
||||
let surviving_plan = UpdatePlanArgs {
|
||||
explanation: Some("surviving plan".to_string()),
|
||||
plan: vec![PlanItemArg {
|
||||
step: "keep this".to_string(),
|
||||
status: StepStatus::Pending,
|
||||
}],
|
||||
};
|
||||
let rolled_back_plan = UpdatePlanArgs {
|
||||
explanation: Some("rolled back plan".to_string()),
|
||||
plan: vec![PlanItemArg {
|
||||
step: "drop this".to_string(),
|
||||
status: StepStatus::InProgress,
|
||||
}],
|
||||
};
|
||||
|
||||
sess.persist_rollout_items(&[
|
||||
RolloutItem::EventMsg(EventMsg::TurnStarted(TurnStartedEvent {
|
||||
turn_id: "turn-1".to_string(),
|
||||
started_at: None,
|
||||
model_context_window: Some(128_000),
|
||||
collaboration_mode_kind: ModeKind::Default,
|
||||
})),
|
||||
RolloutItem::EventMsg(EventMsg::UserMessage(UserMessageEvent {
|
||||
message: "turn 1 user".to_string(),
|
||||
images: None,
|
||||
local_images: Vec::new(),
|
||||
text_elements: Vec::new(),
|
||||
})),
|
||||
RolloutItem::ResponseItem(user_message("turn 1 user")),
|
||||
RolloutItem::EventMsg(EventMsg::PlanUpdate(surviving_plan.clone())),
|
||||
RolloutItem::EventMsg(EventMsg::TurnComplete(TurnCompleteEvent {
|
||||
turn_id: "turn-1".to_string(),
|
||||
last_agent_message: None,
|
||||
completed_at: None,
|
||||
duration_ms: None,
|
||||
time_to_first_token_ms: None,
|
||||
})),
|
||||
RolloutItem::EventMsg(EventMsg::TurnStarted(TurnStartedEvent {
|
||||
turn_id: "turn-2".to_string(),
|
||||
started_at: None,
|
||||
model_context_window: Some(128_000),
|
||||
collaboration_mode_kind: ModeKind::Default,
|
||||
})),
|
||||
RolloutItem::EventMsg(EventMsg::UserMessage(UserMessageEvent {
|
||||
message: "turn 2 user".to_string(),
|
||||
images: None,
|
||||
local_images: Vec::new(),
|
||||
text_elements: Vec::new(),
|
||||
})),
|
||||
RolloutItem::ResponseItem(user_message("turn 2 user")),
|
||||
RolloutItem::EventMsg(EventMsg::PlanUpdate(rolled_back_plan.clone())),
|
||||
RolloutItem::EventMsg(EventMsg::TurnComplete(TurnCompleteEvent {
|
||||
turn_id: "turn-2".to_string(),
|
||||
last_agent_message: None,
|
||||
completed_at: None,
|
||||
duration_ms: None,
|
||||
time_to_first_token_ms: None,
|
||||
})),
|
||||
])
|
||||
.await;
|
||||
{
|
||||
let mut state = sess.state.lock().await;
|
||||
state.set_latest_plan_update(Some(rolled_back_plan));
|
||||
}
|
||||
|
||||
handlers::thread_rollback(&sess, "sub-1".to_string(), /*num_turns*/ 1).await;
|
||||
let rollback_event = wait_for_thread_rolled_back(&rx).await;
|
||||
assert_eq!(rollback_event.num_turns, 1);
|
||||
|
||||
let latest_plan_update = {
|
||||
let state = sess.state.lock().await;
|
||||
state.latest_plan_update()
|
||||
};
|
||||
assert_eq!(
|
||||
serde_json::to_value(latest_plan_update).expect("serialize refreshed plan update"),
|
||||
serde_json::to_value(Some(surviving_plan)).expect("serialize surviving plan update")
|
||||
);
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn thread_rollback_restores_cleared_reference_context_item_after_compaction() {
|
||||
let (mut sess, tc, rx) = make_session_and_context_with_rx().await;
|
||||
@@ -8159,6 +8246,74 @@ async fn interrupt_accounts_active_goal_before_pausing() -> anyhow::Result<()> {
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn interrupted_active_goal_resets_plan_progress_before_turn_abort() -> anyhow::Result<()> {
|
||||
let (sess, tc, rx, _codex_home) = make_goal_session_and_context_with_rx().await;
|
||||
sess.set_thread_goal(
|
||||
tc.as_ref(),
|
||||
SetGoalRequest {
|
||||
objective: Some("Pause this goal on interrupt".to_string()),
|
||||
status: Some(ThreadGoalStatus::Active),
|
||||
token_budget: None,
|
||||
},
|
||||
)
|
||||
.await?;
|
||||
while rx.try_recv().is_ok() {}
|
||||
|
||||
sess.send_event(
|
||||
tc.as_ref(),
|
||||
EventMsg::PlanUpdate(UpdatePlanArgs {
|
||||
explanation: Some("interrupt should reset this plan".to_string()),
|
||||
plan: vec![PlanItemArg {
|
||||
step: "continue work".to_string(),
|
||||
status: StepStatus::InProgress,
|
||||
}],
|
||||
}),
|
||||
)
|
||||
.await;
|
||||
while let Ok(event) = rx.recv().await {
|
||||
if matches!(event.msg, EventMsg::PlanUpdate(_)) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
sess.spawn_task(
|
||||
Arc::clone(&tc),
|
||||
Vec::new(),
|
||||
NeverEndingTask {
|
||||
kind: TaskKind::Regular,
|
||||
listen_to_cancellation_token: false,
|
||||
},
|
||||
)
|
||||
.await;
|
||||
sess.abort_all_tasks(TurnAbortReason::Interrupted).await;
|
||||
|
||||
let mut saw_cleanup_before_abort = false;
|
||||
while let Ok(event) = rx.recv().await {
|
||||
match event.msg {
|
||||
EventMsg::PlanUpdate(UpdatePlanArgs { plan, .. }) => {
|
||||
saw_cleanup_before_abort |= plan
|
||||
.iter()
|
||||
.any(|item| matches!(item.status, StepStatus::Pending));
|
||||
}
|
||||
EventMsg::TurnAborted(_) => break,
|
||||
_ => {}
|
||||
}
|
||||
}
|
||||
assert!(
|
||||
saw_cleanup_before_abort,
|
||||
"interrupt cleanup should reset in-progress plan items before TurnAborted"
|
||||
);
|
||||
|
||||
let goal = sess
|
||||
.get_thread_goal()
|
||||
.await?
|
||||
.expect("goal should remain persisted after interrupt");
|
||||
assert_eq!(ThreadGoalStatus::Paused, goal.status);
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn terminal_plan_cleanup_skips_active_goal() -> anyhow::Result<()> {
|
||||
let (sess, tc, rx, _codex_home) = make_goal_session_and_context_with_rx().await;
|
||||
@@ -8192,8 +8347,11 @@ async fn terminal_plan_cleanup_skips_active_goal() -> anyhow::Result<()> {
|
||||
}
|
||||
}
|
||||
|
||||
sess.reset_in_progress_plan_steps_for_terminal_turn(tc.as_ref())
|
||||
.await;
|
||||
sess.reset_in_progress_plan_steps_for_terminal_turn(
|
||||
tc.as_ref(),
|
||||
/*preserve_active_goals*/ true,
|
||||
)
|
||||
.await;
|
||||
|
||||
let emitted_cleanup = std::iter::from_fn(|| rx.try_recv().ok())
|
||||
.any(|event| matches!(event.msg, EventMsg::PlanUpdate(UpdatePlanArgs { .. })));
|
||||
|
||||
@@ -768,8 +768,11 @@ impl Session {
|
||||
warn!("failed to apply goal runtime turn-finished event: {err}");
|
||||
}
|
||||
if should_clear_active_turn {
|
||||
self.reset_in_progress_plan_steps_for_terminal_turn(turn_context.as_ref())
|
||||
.await;
|
||||
self.reset_in_progress_plan_steps_for_terminal_turn(
|
||||
turn_context.as_ref(),
|
||||
/*preserve_active_goals*/ true,
|
||||
)
|
||||
.await;
|
||||
}
|
||||
let event = EventMsg::TurnComplete(TurnCompleteEvent {
|
||||
turn_id: turn_context.sub_id.clone(),
|
||||
@@ -871,8 +874,11 @@ impl Session {
|
||||
}
|
||||
}
|
||||
|
||||
self.reset_in_progress_plan_steps_for_terminal_turn(task.turn_context.as_ref())
|
||||
.await;
|
||||
self.reset_in_progress_plan_steps_for_terminal_turn(
|
||||
task.turn_context.as_ref(),
|
||||
reason != TurnAbortReason::Interrupted,
|
||||
)
|
||||
.await;
|
||||
let (completed_at, duration_ms) = task
|
||||
.turn_context
|
||||
.turn_timing_state
|
||||
|
||||
@@ -197,9 +197,9 @@ fn event_msg_persistence_mode(ev: &EventMsg) -> Option<EventPersistenceMode> {
|
||||
| EventMsg::RealtimeConversationListVoicesResponse(_)
|
||||
| EventMsg::McpStartupUpdate(_)
|
||||
| EventMsg::McpStartupComplete(_)
|
||||
| EventMsg::WebSearchBegin(_)
|
||||
| EventMsg::PlanUpdate(_)
|
||||
| EventMsg::ShutdownComplete
|
||||
| EventMsg::WebSearchBegin(_) => None,
|
||||
EventMsg::PlanUpdate(_) => Some(EventPersistenceMode::Limited),
|
||||
EventMsg::ShutdownComplete
|
||||
| EventMsg::DeprecationNotice(_)
|
||||
| EventMsg::ItemStarted(_)
|
||||
| EventMsg::HookStarted(_)
|
||||
|
||||
@@ -18,8 +18,10 @@ use time::format_description::FormatItem;
|
||||
use time::macros::format_description;
|
||||
use uuid::Uuid;
|
||||
|
||||
use crate::EventPersistenceMode;
|
||||
use crate::INTERACTIVE_SESSION_SOURCES;
|
||||
use crate::find_thread_path_by_id_str;
|
||||
use crate::is_persisted_rollout_item;
|
||||
use crate::list::Cursor;
|
||||
use crate::list::ThreadItem;
|
||||
use crate::list::ThreadSortKey;
|
||||
@@ -31,6 +33,9 @@ use anyhow::Result;
|
||||
use codex_protocol::ThreadId;
|
||||
use codex_protocol::models::ContentItem;
|
||||
use codex_protocol::models::ResponseItem;
|
||||
use codex_protocol::plan_tool::PlanItemArg;
|
||||
use codex_protocol::plan_tool::StepStatus;
|
||||
use codex_protocol::plan_tool::UpdatePlanArgs;
|
||||
use codex_protocol::protocol::EventMsg;
|
||||
use codex_protocol::protocol::RolloutItem;
|
||||
use codex_protocol::protocol::RolloutLine;
|
||||
@@ -45,6 +50,22 @@ use codex_protocol::protocol::UserMessageEvent;
|
||||
const NO_SOURCE_FILTER: &[SessionSource] = &[];
|
||||
const TEST_PROVIDER: &str = "test-provider";
|
||||
|
||||
#[test]
|
||||
fn plan_updates_are_persisted_in_limited_rollouts() {
|
||||
let item = RolloutItem::EventMsg(EventMsg::PlanUpdate(UpdatePlanArgs {
|
||||
explanation: Some("keep plan state durable".to_string()),
|
||||
plan: vec![PlanItemArg {
|
||||
step: "finish persistence".to_string(),
|
||||
status: StepStatus::InProgress,
|
||||
}],
|
||||
}));
|
||||
|
||||
assert!(is_persisted_rollout_item(
|
||||
&item,
|
||||
EventPersistenceMode::Limited
|
||||
));
|
||||
}
|
||||
|
||||
fn provider_vec(providers: &[&str]) -> Vec<String> {
|
||||
providers
|
||||
.iter()
|
||||
|
||||
Reference in New Issue
Block a user