Clear stale turn and review state when the TUI resumes (#64236)

## Why

Persisted history can still show a turn as in progress after its task
has stopped. A review interrupted by a crash can also lack an exit
event, leaving the resumed TUI in review mode.

## What changed

- Treat in-progress turns as interrupted when legacy or anchored resume
  history reports a non-active thread. Preserve in-progress turns on
  unanchored pages, which can include a turn started after the status
  was read.
- Reconcile buffered thread status updates during resume. An idle update
  clears activity restored from history while preserving newer live
  turns and retaining elapsed time for a later completion event.
- Reset review state when replay reaches an interrupted turn and end
  review mode at the same boundary when backtracking.

## Testing

Extend resume and replay coverage for stale and live turns, buffered
idle updates, and elapsed time when completion arrives after idle.

GitOrigin-RevId: ac46b271e86960e7e7b7e81d1c45f7ef655118fa
This commit is contained in:
Eric Traut
2026-11-27 01:14:33 +00:00
committed by copyberry
parent ad0ab3d832
commit bee5dc55ac
14 changed files with 178 additions and 26 deletions
+10 -1
View File
@@ -10,10 +10,19 @@ impl ChatWidget {
replay_kind: Option<ReplayKind>,
) -> Option<history_cell::FinalMessageSeparator> {
let live = replay_kind.is_none();
let elapsed = if live {
self.turn_lifecycle
.elapsed_at_idle
.take_if(|(id, _)| *id == turn.id)
.map(|(_, elapsed)| elapsed)
.or_else(|| self.bottom_pane.status_elapsed())
} else {
None
};
history_cell::FinalMessageSeparator::from_turn_once(
turn,
&mut self.turn_lifecycle.rendered_completion_turn_ids,
self.bottom_pane.status_elapsed().filter(|_| live),
elapsed,
live.then(Local::now),
)
.filter(|cell| self.local_settings.transcript_mode.is_owned() || cell.has_timing())
+18 -1
View File
@@ -101,6 +101,24 @@ impl ChatWidget {
ServerNotification::ThreadSettingsUpdated(notification) => {
self.apply_thread_settings(notification, replay_kind);
}
ServerNotification::ThreadStatusChanged(notification) => {
// Idle has no turn ID. Only retire activity restored from history;
// a newer live turn must finish through its own completion event.
if notification.status == codex_app_server_protocol::ThreadStatus::Idle
&& self.turn_lifecycle.agent_turn_running
&& self.turn_lifecycle.running_from_history
{
self.turn_lifecycle.elapsed_at_idle = self
.turn_lifecycle
.last_turn_id
.clone()
.zip(self.bottom_pane.status_elapsed());
self.reset_review_mode();
self.turn_lifecycle.finish();
self.update_task_running_state();
self.request_redraw();
}
}
ServerNotification::TurnStarted(notification) => {
self.turn_lifecycle.has_turn_history = true;
if self
@@ -405,7 +423,6 @@ impl ChatWidget {
| ServerNotification::GatewayOAuthChanged(_)
| ServerNotification::AccountRateLimitsUpdated(_)
| ServerNotification::ThreadStarted(_)
| ServerNotification::ThreadStatusChanged(_)
| ServerNotification::ThreadReadStateChanged(_)
| ServerNotification::ThreadSectionChanged(_)
| ServerNotification::ThreadReverted(_)
+5
View File
@@ -197,6 +197,7 @@ impl ChatWidget {
self.turn_lifecycle.last_turn_id = Some(turn_id.clone());
self.last_non_retry_error = None;
self.on_replayed_task_started(started_at);
self.turn_lifecycle.running_from_history = true;
}
let trailing_reasoning_id = (status == TurnStatus::InProgress)
.then(|| items.last())
@@ -256,6 +257,10 @@ impl ChatWidget {
} else {
status
};
// A crash can leave a review start in history without the corresponding exit.
if status == TurnStatus::Interrupted {
self.reset_review_mode();
}
if status == TurnStatus::InProgress {
self.status_state.reasoning_resume_turn_id = Some(turn_id.clone());
}
@@ -1805,6 +1805,31 @@ async fn replayed_in_progress_turn_marks_task_running() {
std::iter::from_fn(|| rx.try_recv().ok())
.all(|event| !matches!(event, AppEvent::Exit(_)))
);
let input = chat.capture_thread_input_state();
chat.restore_reconnected_input(input, &[]);
replay_entered_review_mode(&mut chat, "current changes");
let idle = ServerNotification::ThreadStatusChanged(
codex_app_server_protocol::ThreadStatusChangedNotification {
thread_id: chat.thread_id.unwrap().to_string(),
status: codex_app_server_protocol::ThreadStatus::Idle,
},
);
chat.bottom_pane
.reset_status_timer(std::time::Duration::from_secs(/*secs*/ 125));
chat.handle_server_notification(idle.clone(), /*replay_kind*/ None);
assert!(!chat.bottom_pane.is_task_running());
super::helpers::handle_turn_completed(&mut chat, "turn-1", /*duration_ms*/ None);
let completion = drain_insert_history(&mut rx)
.into_iter()
.map(|lines| lines_to_single_string(&lines))
.collect::<String>();
assert!(completion.contains("Worked for 2m 5s"), "{completion}");
handle_turn_started(&mut chat, "turn-2");
chat.handle_server_notification(idle, /*replay_kind*/ None);
assert!(chat.bottom_pane.is_task_running());
}
}
}
@@ -19,6 +19,8 @@ pub(super) struct TurnLifecycleState {
pub(super) sleep_inhibitor: SleepInhibitor,
/// Tracks whether codex-core currently considers an agent turn to be in progress.
pub(super) agent_turn_running: bool,
pub(super) running_from_history: bool,
pub(super) elapsed_at_idle: Option<(String, std::time::Duration)>,
pub(super) last_turn_id: Option<String>,
pub(super) steer_hint: SteerHint,
/// A live or restored turn makes the parent available for side-chat creation.
@@ -37,6 +39,8 @@ impl TurnLifecycleState {
Self {
sleep_inhibitor: SleepInhibitor::new(prevent_idle_sleep),
agent_turn_running: false,
running_from_history: false,
elapsed_at_idle: None,
last_turn_id: None,
steer_hint: SteerHint::Hidden,
has_turn_history: false,
@@ -51,6 +55,7 @@ impl TurnLifecycleState {
pub(super) fn start(&mut self, now: Instant) {
self.steer_hint = SteerHint::Hidden;
self.agent_turn_running = true;
self.running_from_history = false;
self.goal_status_active_turn_started_at = Some(now);
self.sleep_inhibitor.set_turn_running(/*turn_running*/ true);
}
@@ -58,6 +63,7 @@ impl TurnLifecycleState {
pub(super) fn finish(&mut self) {
self.steer_hint = SteerHint::Hidden;
self.agent_turn_running = false;
self.running_from_history = false;
self.goal_status_active_turn_started_at = None;
self.sleep_inhibitor
.set_turn_running(/*turn_running*/ false);
@@ -77,6 +83,7 @@ impl TurnLifecycleState {
pub(super) fn reset_thread(&mut self) {
self.finish();
self.last_turn_id = None;
self.elapsed_at_idle = None;
self.has_turn_history = false;
self.admitted_turn_id = None;
self.budget_limited_turn_ids.clear();