Restore elapsed time for replayed in-progress turns (#54047)

## Why

Replaying an in-progress turn reset the TUI's `Working` timer to zero, hiding time already spent on the turn.

## What changed

Initialize the status timer from the turn's `started_at` timestamp when replaying turns or turn-start notifications. Keep the zero-time fallback when the timestamp is missing, and clamp future timestamps to zero elapsed time.

## Testing

Extend history replay tests to verify elapsed time for both thread snapshots and replayed in-progress turns, and normalize the timer in rendering snapshots.

GitOrigin-RevId: 36a48f43d0d7aa530f1bfe6dbefa9f0885cbd6c7
This commit is contained in:
Eric Traut
2026-10-14 17:34:44 +00:00
committed by copyberry
parent 3ee99cde70
commit 764a70fc11
5 changed files with 61 additions and 11 deletions
+5 -1
View File
@@ -102,7 +102,11 @@ impl ChatWidget {
self.last_non_retry_error = None;
if !matches!(replay_kind, Some(ReplayKind::ResumeInitialMessages)) {
self.warning_display_state.startup_complete = true;
self.on_task_started();
if from_replay {
self.on_replayed_task_started(notification.turn.started_at);
} else {
self.on_task_started();
}
}
}
ServerNotification::TurnCompleted(notification) => {
+1 -1
View File
@@ -146,7 +146,7 @@ impl ChatWidget {
self.warning_display_state.startup_complete = true;
self.turn_lifecycle.last_turn_id = Some(turn_id.clone());
self.last_non_retry_error = None;
self.on_task_started();
self.on_replayed_task_started(started_at);
}
let trailing_reasoning_id = (status == TurnStatus::InProgress)
.then(|| items.last())
@@ -1,8 +1,8 @@
---
source: tui/src/chatwidget/tests/history_replay.rs
expression: "normalize_completion_timestamps(& (render_bottom_popup(&chat, 80)))"
expression: "timer.replace_all(&normalize_snapshot_paths(render_bottom_popup(&chat, 80)),\n\"(<elapsed> •\")"
---
• Working (0s • esc to interrupt)
• Working (<elapsed> • esc to interrupt)
› Ask Codex to do anything
@@ -1631,15 +1631,38 @@ async fn live_reasoning_summary_drops_empty_parts_without_losing_content() {
async fn thread_snapshot_replayed_turn_started_marks_task_running() {
let (mut chat, mut rx, _op_rx) = make_chatwidget_manual(/*model_override*/ None).await;
replay_turn_started(&mut chat, ReplayKind::ThreadSnapshot);
let started_at = chrono::Utc::now().timestamp() - 125;
chat.handle_server_notification(
ServerNotification::TurnStarted(TurnStartedNotification {
thread_id: "thread-1".to_string(),
turn: AppServerTurn {
started_at: Some(started_at),
..app_server_turn(
"turn-1",
AppServerTurnStatus::InProgress,
/*duration_ms*/ None,
/*error*/ None,
)
},
}),
Some(ReplayKind::ThreadSnapshot),
);
drain_insert_history(&mut rx);
assert!(chat.bottom_pane.is_task_running());
let status = chat
.bottom_pane
.status_widget()
.expect("status indicator should be visible");
assert_eq!(status.header(), "Working");
let before = chrono::Utc::now().timestamp() - started_at;
let elapsed = chat.bottom_pane.status_elapsed().unwrap().as_secs() as i64;
let after = chrono::Utc::now().timestamp() - started_at;
// The wire timestamp has second precision; reads may be separated by any CI delay.
assert!(
(before - 1..=after).contains(&elapsed),
"{elapsed} not in {before}..={after}"
);
let timer = regex_lite::Regex::new(r"\(\d+(?:h \d+m \d+s|m \d+s|s) •").unwrap();
insta::assert_snapshot!(
timer.replace_all(render_bottom_first_row(&chat, /*width*/ 80).trim(), "(<elapsed> •"),
@"• Working (<elapsed> • esc to interrupt)"
);
}
#[tokio::test]
@@ -1655,9 +1678,11 @@ async fn replayed_in_progress_turn_marks_task_running() {
let (mut chat, mut rx, mut op_rx) =
make_chatwidget_manual(/*model_override*/ None).await;
chat.thread_id = Some(ThreadId::new());
let started_at = chrono::Utc::now().timestamp() - 125;
chat.replay_thread_turns(
vec![AppServerTurn {
items_view,
started_at: Some(started_at),
..app_server_turn(
"turn-1",
AppServerTurnStatus::InProgress,
@@ -1676,9 +1701,20 @@ async fn replayed_in_progress_turn_marks_task_running() {
),
(true, Some("turn-1")),
);
let before = chrono::Utc::now().timestamp() - started_at;
let elapsed = chat.bottom_pane.status_elapsed().unwrap().as_secs() as i64;
let after = chrono::Utc::now().timestamp() - started_at;
assert!(
(before - 1..=after).contains(&elapsed),
"{elapsed} not in {before}..={after}"
);
let timer = regex_lite::Regex::new(r"\(\d+(?:h \d+m \d+s|m \d+s|s) •").unwrap();
assert_chatwidget_snapshot!(
"replayed_in_progress_turn",
normalize_snapshot_paths(render_bottom_popup(&chat, /*width*/ 80)),
timer.replace_all(
&normalize_snapshot_paths(render_bottom_popup(&chat, /*width*/ 80)),
"(<elapsed> •"
),
);
chat.handle_key_event(KeyEvent::new(KeyCode::Char('c'), KeyModifiers::CONTROL));
@@ -114,6 +114,16 @@ impl ChatWidget {
self.request_redraw();
}
pub(super) fn on_replayed_task_started(&mut self, started_at: Option<i64>) {
self.on_task_started();
if let Some(started_at) = started_at {
let elapsed_secs = chrono::Utc::now().timestamp().saturating_sub(started_at);
self.bottom_pane.reset_status_timer(Duration::from_secs(
elapsed_secs.try_into().unwrap_or_default(),
));
}
}
pub(super) fn on_task_complete(
&mut self,
last_agent_message: Option<String>,