diff --git a/codex-rs/tui/src/app/event_dispatch.rs b/codex-rs/tui/src/app/event_dispatch.rs index b5e5cd1d68..1f85a4ed0c 100644 --- a/codex-rs/tui/src/app/event_dispatch.rs +++ b/codex-rs/tui/src/app/event_dispatch.rs @@ -1227,10 +1227,13 @@ impl App { AppEvent::CorrectFlushedAgentMessage { item, cell } => { let belongs_to_item = |cell: &Arc| { cell.as_any().downcast_ref::().is_some_and(|cell| cell.interrupted_item.as_ref() == Some(&item)) + || cell.as_any().downcast_ref::().is_some_and(|cell| cell.0 == item) }; + let hidden = cell.copy_source() == Some(""); if let Some(index) = self.transcript_cells.iter().position(belongs_to_item) { - // Keep the reply where it first appeared, with intervening tools in order. - if self.transcript_cells.extract_if(index + 1.., |cell| belongs_to_item(cell)).count() > 0 { + // Remove a hidden reply entirely; otherwise retain its original position. + let remove_from = index + usize::from(!hidden); + if self.transcript_cells.extract_if(remove_from.., |cell| belongs_to_item(cell)).count() > 0 { self.native_history.retain(&self.transcript_cells); self.transcript_view.restart_search(); self.transcript_view.history_loaded(&self.transcript_cells, 0..0); @@ -1238,8 +1241,19 @@ impl App { overlay.replace_cells(self.transcript_cells.clone()); } } - self.replace_agent_message(tui, index..index + 1, cell, crate::app_event::ConsolidationScrollbackReflow::Required)?; - } else { + if hidden { + self.finish_required_stream_reflow(tui)?; + } else { + self.replace_agent_message(tui, index..index + 1, cell, crate::app_event::ConsolidationScrollbackReflow::Required)?; + } + let previous = self.transcript_cells.iter().rev().find(|cell| { + (cell.as_any().is::() && !cell.raw_lines().is_empty()) + || cell.as_any().is::() + }); + self.chat_widget.reconcile_corrected_cloud_copy( + &item, previous.map(AsRef::as_ref), + ); + } else if !hidden { self.insert_history_cell(tui, Box::new(cell)); } } diff --git a/codex-rs/tui/src/app/history_pagination.rs b/codex-rs/tui/src/app/history_pagination.rs index ce918c3456..977191fead 100644 --- a/codex-rs/tui/src/app/history_pagination.rs +++ b/codex-rs/tui/src/app/history_pagination.rs @@ -294,21 +294,67 @@ impl App { visibility: RawReasoningVisibility, relocate_split_images: bool, ) -> (TranscriptCells, TranscriptCells) { + let cloud = matches!(self.app_server_target, AppServerTarget::Cloud) + && self.chat_widget.thread_id() == Some(thread_id); + let mut unfinished = HashSet::new(); + if cloud { + for (turn_id, item) in items.iter().rev().filter(|(turn, item)| { + matches!(item, ThreadItemPayload::AgentMessage { .. }) + && !hidden_item_ids.contains(&(turn.as_str(), item.id())) + }) { + if let Some(turn) = turns.iter().find(|turn| turn.id == *turn_id) + && let Some(entry) = turn.items.iter().find(|entry| entry.id() == item.id()) + && let Some(key) = + self.chat_widget + .remember_older_cloud_reply(turn_id, entry, &turn.status) + { + unfinished.insert(key); + } + } + } let mut cells = Vec::new(); let mut split_images = Vec::new(); + let project = |items: &[(String, ThreadItemPayload)]| { + thread_items_to_transcript_cells( + Some(thread_id), + cwd, + items.iter().map(|(_, item)| item.clone()), + visibility, + Some(&self.config), + ) + }; for (range, turn_end) in completion::group_turn_items(&items, turns) { let start = cells.len(); // Internal prompts stay invisible, but still separate adjacent tool groups. for visible in items[range] .split(|(turn_id, item)| hidden_item_ids.contains(&(turn_id.as_str(), item.id()))) { - cells.extend(thread_items_to_transcript_cells( - Some(thread_id), - cwd, - visible.iter().map(|(_, item)| item.clone()), - visibility, - Some(&self.config), - )); + for chunk in visible.split_inclusive(|(turn, item)| { + unfinished.contains(&(turn.clone(), item.id().to_owned())) + }) { + let anchor = chunk.last().and_then(|(turn, item)| { + let key = (turn.clone(), item.id().to_owned()); + unfinished.get(&key).cloned() + }); + let split = chunk.len() - usize::from(anchor.is_some()); + cells.extend(project(&chunk[..split])); + if let Some(key) = anchor { + let mut cell = + project(&chunk[split..]) + .into_iter() + .next() + .unwrap_or_else(|| { + Arc::new(history_cell::PendingAgentMessageCell(key.clone())) + }); + if let Some(markdown) = Arc::get_mut(&mut cell).and_then(|cell| { + cell.as_any_mut() + .downcast_mut::() + }) { + markdown.interrupted_item = Some(key); + } + cells.push(cell); + } + } } let Some(turn) = turn_end else { if relocate_split_images { diff --git a/codex-rs/tui/src/app/tests/history_hydration_tests.rs b/codex-rs/tui/src/app/tests/history_hydration_tests.rs index 11a6dd305f..1920e21d2e 100644 --- a/codex-rs/tui/src/app/tests/history_hydration_tests.rs +++ b/codex-rs/tui/src/app/tests/history_hydration_tests.rs @@ -365,6 +365,7 @@ async fn cloud_complete_transcript_includes_error_from_an_older_zero_item_turn() codex_app_server_client::AppServerClient::Cloud(client), crate::ThreadParamsMode::Cloud, ); + let mut interactive_thread = thread.clone(); let service = tokio::spawn(async move { let mut turn_pages = Vec::new(); let mut item_pages = Vec::new(); @@ -373,6 +374,24 @@ async fn cloud_complete_transcript_includes_error_from_an_older_zero_item_turn() panic!("expected a history read") }; let response = match *request { + ClientRequest::ThreadTurnsList { .. } if item_pages.len() >= 4 => { + json!({"data": [test_turn("middle", TurnStatus::InProgress, vec![])], "nextCursor": null}) + } + ClientRequest::ThreadItemsList { params, .. } if item_pages.len() >= 4 => { + item_pages.push(params.cursor.clone()); + match params.cursor { + None => json!({"data": [{"turnId":"middle","item":{ + "type":"userMessage","id":"z","content":[{"type":"text","text":"Question"}] + },"completedAtMs":2}],"nextCursor":"interactive"}), + Some(Opaque(cursor)) if cursor == "interactive" => json!({"data":[ + {"turnId":"middle","item":{"type":"agentMessage","id":"b","text":""},"startedAtMs":1}, + {"turnId":"middle","item":{"type":"agentMessage","id":"d","text":"Visible prefix"},"startedAtMs":1}, + {"turnId":"middle","item":{"type":"agentMessage","id":"c","text":""},"startedAtMs":1}, + {"turnId":"middle","item":{"type":"agentMessage","id":"a","text":"Earlier prefix"},"startedAtMs":1} + ],"nextCursor":null}), + other => panic!("unexpected interactive cursor {other:?}"), + } + } ClientRequest::ThreadRead { .. } => json!({"thread": thread}), ClientRequest::ThreadTurnsList { params, .. } => { let page = match params.cursor.as_deref() { @@ -399,7 +418,11 @@ async fn cloud_complete_transcript_includes_error_from_an_older_zero_item_turn() other => panic!("unexpected item cursor {other:?}"), } } - _ => panic!("unexpected request"), + ClientRequest::PluginInstalled { .. } => json!({"marketplaces":[]}), + ClientRequest::ThreadBackgroundTerminalsList { .. } => { + json!({"data": [], "nextCursor": null}) + } + other => panic!("unexpected request: {other:?}"), }; command.response.send(Ok(Ok(response))).unwrap(); } @@ -427,6 +450,78 @@ async fn cloud_complete_transcript_includes_error_from_an_older_zero_item_turn() { assert!(lines_to_single_string(&cell.raw_lines()).contains(expected)); } + let (mut app, mut events, _ops) = make_test_app_with_channels().await; + app.app_server_target = AppServerTarget::Cloud; + app.local_settings.transcript_mode = TranscriptMode::Terminal; + app.local_settings.tui.terminal_resize_reflow_max_rows = Some(1); + interactive_thread.cwd = app.config.cwd.clone().into(); + server + .hydrate_initial_thread_history( + &mut interactive_thread, + /*turn_cursor*/ None, + /*item_cursor*/ None, + Some(&app.config), + Some(&app.local_settings), + HistoryHydrationScope::Initial, + ) + .await?; + app.enqueue_primary_thread_session( + test_thread_session(id, app.config.cwd.to_path_buf()), + interactive_thread.turns, + ) + .await?; + let mut tui = crate::tui::test_support::make_test_tui()?; + drain_app_events(&mut app, &mut tui, &mut server, &mut events).await?; + let cursor = server.begin_older_history_page(id).expect("older page"); + let page = server + .thread_items_page( + id, + /*turn_id*/ None, + Some(cursor.clone()), + /*limit*/ 100, + ) + .await?; + app.handle_older_history_page(&mut tui, &mut server, id, &cursor, Ok(page)) + .await?; + for (item_id, text) in [ + ("a", "Earlier final answer"), + ("c", "Previously empty answer"), + ("d", r#"::git-stage{cwd="/repo"}"#), + ("b", "Later answer"), + ] { + let payload: ThreadItemPayload = + serde_json::from_value(json!({"type":"agentMessage","id":item_id,"text":text}))?; + let completed = codex_app_server_protocol::ItemCompletedNotification::new( + id.to_string(), + "middle".into(), + payload, + /*started_at_ms*/ None, + /*completed_at_ms*/ 3, + ); + app.enqueue_thread_notification(id, ServerNotification::ItemCompleted(completed)) + .await?; + let event = app.active_thread_rx.as_mut().unwrap().recv().await.unwrap(); + app.handle_thread_event_now(event); + drain_app_events(&mut app, &mut tui, &mut server, &mut events).await?; + } + let replies = app + .transcript_cells + .iter() + .flat_map(|cell| cell.display_lines(/*width*/ 80)) + .map(|line| line.to_string()) + .filter(|line| line.starts_with("• ")) + .collect::>() + .join("\n"); + insta::assert_snapshot!(replies, @" + • Earlier final answer + • Previously empty answer + • Later answer + "); + assert_eq!( + app.chat_widget.last_agent_markdown_text(), + Some("Later answer") + ); + drop(app); drop(server); let (turn_pages, item_pages) = service.await?; assert_eq!( @@ -435,7 +530,9 @@ async fn cloud_complete_transcript_includes_error_from_an_older_zero_item_turn() None, Some(Opaque("end".into())), None, - Some(Opaque("end".into())) + Some(Opaque("end".into())), + None, + Some(Opaque("interactive".into())), ] ); assert_eq!( diff --git a/codex-rs/tui/src/app/tests/session_lifecycle_requests.rs b/codex-rs/tui/src/app/tests/session_lifecycle_requests.rs index 544e52fa74..1ecda60338 100644 --- a/codex-rs/tui/src/app/tests/session_lifecycle_requests.rs +++ b/codex-rs/tui/src/app/tests/session_lifecycle_requests.rs @@ -420,10 +420,16 @@ async fn cloud_resume_picker_reconciles_admitted_steers() -> Result<()> { TurnStatus::InProgress, vec![ commentary.clone(), + agent_item("blank-item", "", None), agent_item("live-item", "Partial response", None), + agent_item("late-directive", "::git-pu", None), + agent_item("directive-item", "::git-st", None), ], ); ongoing_turn.items[0].completed_at_ms = Some(1); + ongoing_turn.items[1].started_at_ms = Some(1); + ongoing_turn.items[3].started_at_ms = Some(1); + ongoing_turn.items[4].started_at_ms = Some(1); let mut original_thread = overview_thread( original, /*parent_thread_id*/ None, @@ -599,6 +605,14 @@ async fn cloud_resume_picker_reconciles_admitted_steers() -> Result<()> { .collect::>() .join("\n") }; + let rendered_transcript = |app: &App| { + app.transcript_cells + .iter() + .flat_map(|cell| cell.display_lines(/*width*/ 80)) + .map(|line| line.to_string()) + .collect::>() + .join("\n") + }; let mut tui = crate::tui::test_support::make_test_tui()?; while events.try_recv().is_ok() {} for thread_id in [other, original] { @@ -621,8 +635,68 @@ async fn cloud_resume_picker_reconciles_admitted_steers() -> Result<()> { • Commentary response • Partial response "); + assert!(rendered_transcript(&app).contains("::git-st")); + assert_eq!(app.chat_widget.last_agent_markdown_text(), Some("::git-st")); + let directive = agent_item("directive-item", r#"::git-stage{cwd="/repo"}"#, None); + for notification in [ + agent_message_delta_notification(other, ongoing, "directive-item", "age"), + completed(other, ongoing, directive.clone()), + ] { + app.enqueue_thread_notification(other, notification).await?; + let event = app.active_thread_rx.as_mut().unwrap().recv().await.unwrap(); + app.handle_thread_event_now(event); + } + drain_app_events(&mut app, &mut tui, &mut server, &mut events).await?; + assert!(!rendered_transcript(&app).contains("::git-st")); + assert!(app.transcript_cells.iter().all(|cell| { + !cell + .copy_source() + .is_some_and(|source| source.contains("::git-st")) + })); + assert_eq!(app.chat_widget.last_agent_markdown_text(), Some("::git-pu")); + let mut view = crate::transcript_view::TranscriptView::default(); + let area = ratatui::layout::Rect::new(0, 0, 80, 20); + view.render( + area, + &mut ratatui::buffer::Buffer::empty(area), + &app.transcript_cells, + ); + assert!(view.begin_copy_mode(&app.transcript_cells, /*guard*/ None)); + let selection = view.selected_text(&app.transcript_cells).unwrap(); + let mut copied = String::new(); + view.copy_selected_text_with( + &app.transcript_cells, + &selection, + /*clear_selection*/ false, + |text, _| { + copied = text.to_owned(); + Ok(crate::clipboard_copy::CopyStatus::Confirmed) + }, + ) + .unwrap(); + assert_eq!(copied, "::git-pu"); for notification in [ agent_message_delta_notification(other, ongoing, "live-item", "Live response "), + agent_message_delta_notification(other, ongoing, "late-directive", "sh"), + completed( + other, + ongoing, + agent_item( + "late-directive", + r#"::git-push{cwd="/repo" branch="feat"}"#, + None, + ), + ), + ] { + app.enqueue_thread_notification(other, notification).await?; + let event = app.active_thread_rx.as_mut().unwrap().recv().await.unwrap(); + app.handle_thread_event_now(event); + } + drain_app_events(&mut app, &mut tui, &mut server, &mut events).await?; + let intermediate = rendered_transcript(&app); + assert!(intermediate.contains("Live response")); + assert!(!intermediate.contains("::git-pu")); + for notification in [ agent_message_delta_notification( other, ongoing, @@ -631,6 +705,23 @@ async fn cloud_resume_picker_reconciles_admitted_steers() -> Result<()> { ), completed(other, ongoing, commentary.clone()), agent_message_delta_notification(other, ongoing, "live-item", "continues"), + completed( + other, + ongoing, + agent_item("live-item", "Canonical final response", None), + ), + completed( + other, + ongoing, + agent_item("live-item", "Canonical final response", None), + ), + completed(other, ongoing, directive), + agent_message_delta_notification(other, ongoing, "blank-item", "Earlier "), + completed( + other, + ongoing, + agent_item("blank-item", "Earlier final response", None), + ), turn_completed_notification(other, ongoing, TurnStatus::Completed), ] { app.enqueue_thread_notification(other, notification).await?; @@ -638,11 +729,37 @@ async fn cloud_resume_picker_reconciles_admitted_steers() -> Result<()> { app.handle_thread_event_now(event); } drain_app_events(&mut app, &mut tui, &mut server, &mut events).await?; - insta::assert_snapshot!(rendered_responses(&app), @" + let live_responses = rendered_responses(&app); + let live_transcript = rendered_transcript(&app); + assert!(!live_transcript.contains("::git-st")); + assert!(!live_transcript.contains("::git-pu")); + assert_eq!( + app.chat_widget.last_agent_markdown_text(), + Some("Canonical final response") + ); + insta::assert_snapshot!(&live_responses, @" • Commentary response - • Partial response - • Live response continues + • Earlier final response + • Canonical final response "); + let channel = app.ensure_thread_channel(other); + let snapshot = channel.store.lock().await.snapshot(); + app.render_thread_snapshot( + &mut tui, + &mut server, + other, + snapshot, + /*resume_restored_queue*/ false, + ) + .await?; + drain_app_events(&mut app, &mut tui, &mut server, &mut events).await?; + assert_eq!(rendered_responses(&app), live_responses); + assert!(!rendered_transcript(&app).contains("::git-st")); + assert!(!rendered_transcript(&app).contains("::git-pu")); + assert_eq!( + app.chat_widget.last_agent_markdown_text(), + Some("Canonical final response") + ); } } diff --git a/codex-rs/tui/src/chatwidget/protocol.rs b/codex-rs/tui/src/chatwidget/protocol.rs index 074bdfec3a..937c51d31c 100644 --- a/codex-rs/tui/src/chatwidget/protocol.rs +++ b/codex-rs/tui/src/chatwidget/protocol.rs @@ -1,6 +1,21 @@ use super::*; impl ChatWidget { + fn on_keyed_agent_message_delta(&mut self, key: (String, String), delta: String) { + if self.transcript.replayed_cloud_text.is_some() + && self.stream_controller.is_some() + && self + .transcript + .active_streamed_agent_message + .as_ref() + .is_some_and(|active| active != &key) + { + self.flush_answer_stream_with_separator(); + } + self.transcript.active_streamed_agent_message = Some(key); + self.on_agent_message_delta(delta); + } + pub(crate) fn handle_server_notification( &mut self, notification: ServerNotification, @@ -144,9 +159,10 @@ impl ChatWidget { ¬ification.item_id, )) { - self.transcript.active_streamed_agent_message = - Some((notification.turn_id, notification.item_id)); - self.on_agent_message_delta(notification.delta); + self.on_keyed_agent_message_delta( + (notification.turn_id, notification.item_id), + notification.delta, + ); } } ServerNotification::PlanDelta(notification) => { @@ -623,8 +639,7 @@ impl ChatWidget { if self.transcript.active_streamed_agent_message.as_ref() != Some(&key) && self.transcript.last_flushed_agent_message.as_ref() != Some(&key) { - self.transcript.active_streamed_agent_message = Some(key); - self.on_agent_message_delta(text); + self.on_keyed_agent_message_delta(key, text); } } ThreadItemPayload::Reasoning { @@ -738,6 +753,8 @@ impl ChatWidget { { return; } + let was_finished = + std::mem::replace(&mut self.transcript.recording_finished_cloud_text, true); match notification.item.payload { item @ ThreadItemPayload::CommandExecution { .. } => { self.on_command_execution_completed(item) @@ -748,6 +765,7 @@ impl ChatWidget { replay_kind.map_or(ThreadItemRenderSource::Live, ThreadItemRenderSource::Replay), ), } + self.transcript.recording_finished_cloud_text = was_finished; } } diff --git a/codex-rs/tui/src/chatwidget/replay.rs b/codex-rs/tui/src/chatwidget/replay.rs index d575997242..5cb2cae863 100644 --- a/codex-rs/tui/src/chatwidget/replay.rs +++ b/codex-rs/tui/src/chatwidget/replay.rs @@ -7,6 +7,48 @@ use super::*; impl ChatWidget { + /// Record replies newest-to-oldest before the loaded response window, without replay effects. + pub(crate) fn remember_older_cloud_reply( + &mut self, + turn_id: &str, + entry: &codex_app_server_protocol::ThreadItem, + status: &TurnStatus, + ) -> Option<(String, String)> { + let key = (turn_id.to_owned(), entry.id().to_owned()); + if self.transcript.has_replayed_cloud_text(turn_id, entry.id()) { + return None; + } + let seen = self.transcript.replayed_cloud_text.as_mut()?; + self.transcript.older_response_order -= 1; + let position = self.transcript.older_response_order; + let finished = entry.completed_at_ms.is_some() + || (entry.started_at_ms.is_none() && *status != TurnStatus::InProgress); + if finished { + if self.transcript.last_agent_source.is_none() + && let ThreadItemPayload::AgentMessage { text, .. } = &entry.payload + { + let parsed = parse_assistant_markdown(text, self.config.cwd.as_path()); + if !parsed.visible_markdown.trim().is_empty() { + let committed = self.transcript.committed_turn_response.take(); + self.transcript + .record_agent_markdown(parsed.visible_markdown, text.clone()); + self.transcript.historical_copy_order = Some(position); + self.transcript.last_agent_copy_item = Some(key); + if self.last_turn_id() != Some(turn_id) { + self.transcript.committed_turn_response = committed; + } else if let Some((order, _)) = &mut self.transcript.committed_turn_response { + *order = position; + } + } + } + return None; + } + seen.entry(turn_id.to_owned()) + .or_default() + .insert(entry.id().to_owned(), (false, position)); + Some(key) + } + /// Restores the active typed reasoning item after a thread switch or session refresh. /// Its completion carries the full summary if the bounded event buffer lost earlier deltas. pub(crate) fn restore_active_reasoning_item( @@ -202,8 +244,9 @@ impl ChatWidget { Some((summary, content)), ); } else { - self.transcript.recording_finished_cloud_text = - status != TurnStatus::InProgress || entry.completed_at_ms.is_some(); + // Item and turn pages can reflect different points in time. + self.transcript.recording_finished_cloud_text = entry.completed_at_ms.is_some() + || (entry.started_at_ms.is_none() && status != TurnStatus::InProgress); self.replay_thread_item(item, turn_id.clone(), replay_kind); self.transcript.recording_finished_cloud_text = false; } diff --git a/codex-rs/tui/src/chatwidget/session_flow.rs b/codex-rs/tui/src/chatwidget/session_flow.rs index 56b7140ec6..5bd2a515f5 100644 --- a/codex-rs/tui/src/chatwidget/session_flow.rs +++ b/codex-rs/tui/src/chatwidget/session_flow.rs @@ -282,7 +282,7 @@ impl ChatWidget { if self.daybreak_enabled && previous_thread_id != self.thread_id { self.add_info_message("Daybreak is on for new turns.".into(), /*hint*/ None); } - self.transcript.saw_copy_source_this_turn = false; + self.transcript.committed_turn_response = None; self.refresh_skills_for_current_cwd(/*force_reload*/ true); self.refresh_connector_mentions(/*force_refresh*/ false); let initial_user_message_pending = self.initial_user_message.is_some(); diff --git a/codex-rs/tui/src/chatwidget/streaming.rs b/codex-rs/tui/src/chatwidget/streaming.rs index e72d790d8d..88451040f3 100644 --- a/codex-rs/tui/src/chatwidget/streaming.rs +++ b/codex-rs/tui/src/chatwidget/streaming.rs @@ -103,8 +103,15 @@ impl ChatWidget { .active_streamed_agent_message .take() .filter(|_| completed_message.is_none()); - if let Some(item) = &interrupted_item { - self.transcript.last_flushed_agent_message = Some(item.clone()); + if let Some((turn, item)) = &interrupted_item { + self.transcript.last_flushed_agent_message = interrupted_item.clone(); + if let Some(seen) = &mut self.transcript.replayed_cloud_text { + self.transcript.response_order += 1; + seen.entry(turn.clone()) + .or_default() + .entry(item.clone()) + .or_insert((false, self.transcript.response_order)); + } } let (cell, streamed_source) = controller.finalize(); let completed_display = completed_message.map(|source| { @@ -359,6 +366,7 @@ impl ChatWidget { (text.clone(), text) }; if !plan_text.trim().is_empty() { + self.transcript.response_order += 1; self.transcript .record_agent_markdown(plan_text.clone(), source); self.transcript.latest_proposed_plan_markdown = Some(plan_text.clone()); @@ -527,6 +535,31 @@ impl ChatWidget { ); } + pub(crate) fn reconcile_corrected_cloud_copy( + &mut self, + item: &(String, String), + previous: Option<&dyn history_cell::HistoryCell>, + ) { + if self.transcript.last_agent_copy_item.as_ref() != Some(item) { + return; + } + let agent = previous.and_then(|cell| { + cell.as_any() + .downcast_ref::() + }); + let source = previous.and_then(history_cell::HistoryCell::copy_source); + self.transcript.last_agent_copy_item = agent.and_then(|cell| cell.interrupted_item.clone()); + self.transcript.last_agent_markdown = source.map(|source| { + let markdown = if agent.is_some() { + parse_assistant_markdown(source, self.config.cwd.as_path()).visible_markdown + } else { + source.to_owned() + }; + crate::markdown_render::followup_labels(&markdown).into_owned() + }); + self.transcript.last_agent_source = source.map(str::to_owned); + } + /// Handle completion of an `AgentMessage` turn item. /// /// Commentary completion sets a deferred restore flag so the status row @@ -544,7 +577,7 @@ impl ChatWidget { if self.transcript.has_replayed_cloud_text(turn_id, &item.id) { return; } - self.transcript.last_completed_agent_message = Some((turn_id.to_string(), item.id.clone())); + let key = (turn_id.to_owned(), item.id.clone()); let mut message = String::new(); for content in &item.content { match content { @@ -553,30 +586,94 @@ impl ChatWidget { } let parsed = parse_assistant_markdown(&message, self.config.cwd.as_path()); let questions = item.questions.as_deref().unwrap_or_default(); - let interrupted = self + let cloud = self.transcript.replayed_cloud_text.is_some(); + let completing = !cloud || !from_replay || self.transcript.recording_finished_cloud_text; + self.transcript.response_order += 1; + let mut order = self.transcript.response_order; + let mut correcting = self.transcript.last_flushed_agent_message.as_ref() == Some(&key); + let mut first_observation = false; + if (from_replay || completing || !parsed.visible_markdown.is_empty()) + && let Some(seen) = &mut self.transcript.replayed_cloud_text + { + let items = seen.entry(turn_id.to_owned()).or_default(); + let (finished, position) = items.entry(item.id.clone()).or_insert_with(|| { + first_observation = true; + (completing, order) + }); + correcting |= completing && !*finished; + *finished |= completing; + order = *position; + } + if cloud && from_replay && !completing && parsed.visible_markdown.is_empty() { + if first_observation { + self.prepare_assistant_message(); + self.add_to_history(history_cell::PendingAgentMessageCell(key)); + } + return; + } + let owns_stream = self.transcript.active_streamed_agent_message.as_ref() == Some(&key); + let unrelated_stream = cloud + && self.stream_controller.is_some() + && self.transcript.active_streamed_agent_message.is_some() + && !owns_stream; + let unrelated_activity = unrelated_stream + || (cloud && order < 0 && !owns_stream) + || (cloud + && correcting + && self.is_agent_turn_running() + && self.last_turn_id().is_some_and(|active| active != turn_id)); + let latest_copy_order = self .transcript - .last_flushed_agent_message - .take() - .filter(|item| Some(item) == self.transcript.last_completed_agent_message.as_ref()); - if interrupted.is_some() { + .last_agent_copy_item + .as_ref() + .and_then(|(turn, id)| { + self.transcript + .replayed_cloud_text + .as_ref()? + .get(turn)? + .get(id) + .map(|(_, position)| *position) + }) + .or_else(|| { + self.transcript + .committed_turn_response + .as_ref() + .map(|(position, _)| *position) + }) + .or(self.transcript.historical_copy_order); + let current_copy = (!correcting + || (cloud + && order < 0 + && (self.transcript.last_agent_copy_item.is_some() + || self.transcript.last_agent_source.is_none())) + || self.transcript.last_agent_copy_item.as_ref() == Some(&key) + || (!unrelated_activity + && self.transcript.last_flushed_agent_message.as_ref() == Some(&key))) + && latest_copy_order.is_none_or(|latest| order >= latest); + if !unrelated_activity && current_copy && (completing || !cloud) { + self.transcript.last_completed_agent_message = Some(key.clone()); + } + if correcting && owns_stream && !unrelated_activity { // Commit any resumed stream fragment before replacing all fragments with // the authoritative message. Markdown references can span interruptions. self.flush_answer_stream_with_separator(); } - if (from_replay || !questions.is_empty() || interrupted.is_some()) - && self.stream_controller.is_none() - && !parsed.visible_markdown.is_empty() + if (from_replay || !questions.is_empty() || correcting || unrelated_activity) + && (correcting || !owns_stream || self.stream_controller.is_none()) + && (!parsed.visible_markdown.is_empty() || correcting) { - self.prepare_assistant_message(); - self.mark_safety_buffering_agent_message_started(); - self.bottom_pane.hide_status_indicator(); + if !unrelated_activity { + self.prepare_assistant_message(); + self.mark_safety_buffering_agent_message_started(); + self.bottom_pane.hide_status_indicator(); + } let context = self.thread_id.and_then(|thread_id| { crate::inline_visualization::InlineVisualizationContext::from_config( &self.config, thread_id, ) }); - let cell = history_cell::AgentMarkdownCell::from_parsed_message( + let mut cell = history_cell::AgentMarkdownCell::from_parsed_message( parsed.visible_markdown.clone(), message.clone(), self.config.cwd.as_path(), @@ -584,29 +681,54 @@ impl ChatWidget { questions, ) .with_phase(item.phase.clone()); - if let Some(item) = interrupted { + if parsed.visible_markdown.is_empty() { + cell = cell.with_copy_source(/*source*/ None); + } + if cloud && (from_replay && !completing || order < 0) { + cell.interrupted_item = Some(key.clone()); + } + if correcting { self.app_event_tx - .send(AppEvent::CorrectFlushedAgentMessage { item, cell }); + .send(AppEvent::CorrectFlushedAgentMessage { + item: key.clone(), + cell, + }); } else { self.add_to_history(cell); } - if self.transcript.recording_finished_cloud_text - && let Some(seen) = &mut self.transcript.replayed_cloud_text - { - seen.entry(turn_id.to_owned()) - .or_default() - .insert(item.id.clone()); + if !unrelated_activity { + self.handle_stream_finished(); } - self.handle_stream_finished(); self.request_redraw(); - } else { + } else if !unrelated_activity { self.finalize_completed_assistant_message(&message, item.phase.clone()); } - self.transcript.last_flushed_agent_message = None; - if !parsed.visible_markdown.is_empty() { + if self.transcript.last_flushed_agent_message.as_ref() == Some(&key) { + self.transcript.last_flushed_agent_message = None; + } + let committed_response = self.transcript.committed_turn_response.take(); + if !parsed.visible_markdown.is_empty() && current_copy { self.transcript .record_agent_markdown(parsed.visible_markdown.clone(), message); + self.transcript.last_agent_copy_item = Some(key); } + let newer = committed_response + .as_ref() + .is_none_or(|(previous, _)| order >= *previous); + self.transcript.committed_turn_response = if completing + && !parsed.visible_markdown.is_empty() + && newer + && self + .last_turn_id() + .is_none_or(|active_turn| active_turn == turn_id) + { + Some(( + order, + crate::markdown_render::followup_labels(&parsed.visible_markdown).into_owned(), + )) + } else { + committed_response + }; if !from_replay && let Some(cwd) = parsed.last_created_branch_cwd() && let Some(thread_id) = self.thread_id @@ -627,6 +749,9 @@ impl ChatWidget { } }); } + if unrelated_activity { + return; + } self.status_state.pending_status_indicator_restore = item.questions.is_some() || match item.phase { // Models that don't support preambles only output AgentMessageItems on turn completion. diff --git a/codex-rs/tui/src/chatwidget/tests/replay_render_tests.rs b/codex-rs/tui/src/chatwidget/tests/replay_render_tests.rs index 92adbff565..31b35a89d8 100644 --- a/codex-rs/tui/src/chatwidget/tests/replay_render_tests.rs +++ b/codex-rs/tui/src/chatwidget/tests/replay_render_tests.rs @@ -42,3 +42,175 @@ async fn completed_replay_preserves_messages_and_draft_across_reconstruction() { • Second answer "); } + +#[tokio::test] +async fn hidden_cloud_reply_preserves_only_this_turns_committed_notification() { + let deliver = |chat: &mut ChatWidget, turn: &str, id: &str, text: &str, from_replay| { + let item = AgentMessageItem { + id: id.into(), + content: vec![AgentMessageContent::Text { text: text.into() }], + moderation: None, + phase: None, + memory_citation: None, + delivery: None, + questions: None, + }; + chat.on_agent_message_item_completed(item, turn, from_replay); + }; + let notification = |chat: &mut ChatWidget| { + chat.on_task_complete( + /*last_agent_message*/ None, /*completion*/ None, /*from_replay*/ false, + ); + let Some(Notification::AgentTurnComplete { response }) = chat.pending_notification.take() + else { + panic!("missing turn completion notification") + }; + response + }; + let (mut local, _, _, _) = make_chatwidget_manual_with_sender().await; + let mut turn = app_server_turn( + "current", + AppServerTurnStatus::InProgress, + /*duration_ms*/ None, + /*error*/ None, + ); + turn.items + .push(codex_app_server_protocol::ThreadItem::untimed( + AppServerThreadItemPayload::AgentMessage { + id: "legacy".into(), + text: "Legacy answer".into(), + moderation: None, + phase: Some(MessagePhase::FinalAnswer), + memory_citation: None, + delivery: None, + questions: None, + }, + )); + local.replay_backend_thread_turns( + vec![turn], + ReplayKind::ThreadSnapshot, + &crate::AppServerTarget::Embedded, + ); + assert_eq!(notification(&mut local), "Legacy answer"); + + let (mut chat, _, _, _) = make_chatwidget_manual_with_sender().await; + chat.transcript.replayed_cloud_text = Some(Default::default()); + deliver(&mut chat, "old", "reply", "Partial", true); + handle_turn_started(&mut chat, "new"); + deliver(&mut chat, "old", "reply", "Final", false); + insta::assert_snapshot!(chat.bottom_pane.status_widget().unwrap().header(), @"Working"); + + let (mut ordered, _, mut ordered_events, _) = make_chatwidget_manual_with_sender().await; + ordered.transcript.replayed_cloud_text = Some(Default::default()); + ordered.on_task_started(); + ordered.turn_lifecycle.last_turn_id = Some("current".into()); + ordered.transcript.active_cell = Some(Box::new(history_cell::PlainHistoryCell::new(vec![ + "Tool activity".into(), + ]))); + deliver(&mut ordered, "current", "blank", "", true); + let inserted = std::iter::from_fn(|| ordered_events.try_recv().ok()) + .filter_map(|event| match event { + AppEvent::InsertHistoryCell(cell) => Some(cell), + _ => None, + }) + .collect::>(); + assert_eq!(inserted.len(), 2); + assert!(inserted[0].as_any().is::()); + assert!( + inserted[1] + .as_any() + .is::() + ); + let older_entry = |id: &str, text: &str| { + let payload = serde_json::json!({"type":"agentMessage", "id":id, "text":text}); + codex_app_server_protocol::ThreadItem::untimed(serde_json::from_value(payload).unwrap()) + }; + let in_progress = AppServerTurnStatus::InProgress; + let mut later = older_entry("later", "Later answer"); + later.completed_at_ms = Some(2); + ordered.remember_older_cloud_reply("current", &later, &in_progress); + ordered.remember_older_cloud_reply("current", &older_entry("blank", ""), &in_progress); + deliver(&mut ordered, "current", "blank", "Earlier answer", false); + assert_eq!(ordered.last_agent_markdown_text(), Some("Later answer")); + assert_eq!(notification(&mut ordered), "Later answer"); + + let (mut page_only, _, _, _) = make_chatwidget_manual_with_sender().await; + page_only.transcript.replayed_cloud_text = Some(Default::default()); + page_only.on_task_started(); + page_only.turn_lifecycle.last_turn_id = Some("current".into()); + page_only.remember_older_cloud_reply("current", &older_entry("newer", ""), &in_progress); + page_only.remember_older_cloud_reply("current", &older_entry("first", ""), &in_progress); + assert_eq!(page_only.last_agent_markdown_text(), None); + deliver(&mut page_only, "current", "first", "Page final", false); + assert_eq!(page_only.last_agent_markdown_text(), Some("Page final")); + deliver(&mut page_only, "current", "newer", "Newer final", false); + assert_eq!(page_only.last_agent_markdown_text(), Some("Newer final")); + assert_eq!(notification(&mut page_only), "Newer final"); + + for same_turn in [false, true] { + for (delayed_correction, reverse) in [(false, false), (true, false), (false, true)] { + let (mut chat, _, _, _) = make_chatwidget_manual_with_sender().await; + chat.transcript + .record_agent_markdown("Earlier reply".into(), "Earlier reply".into()); + chat.on_task_started(); + chat.turn_lifecycle.last_turn_id = Some("current".into()); + chat.transcript.replayed_cloud_text = Some(Default::default()); + let earlier_turn = if same_turn { "current" } else { "older" }; + deliver(&mut chat, earlier_turn, "earlier", "Earlier par", true); + deliver(&mut chat, "current", "hidden", "::git-st", true); + assert_eq!(chat.last_agent_markdown_text(), Some("::git-st")); + if reverse { + deliver(&mut chat, "current", "hidden", "Later final", false); + } + chat.transcript.recording_finished_cloud_text = reverse; + deliver(&mut chat, earlier_turn, "earlier", "Earlier reply", reverse); + let expected_copy = if reverse { "Later final" } else { "::git-st" }; + assert_eq!(chat.last_agent_markdown_text(), Some(expected_copy)); + if reverse { + assert_eq!(notification(&mut chat), "Later final"); + continue; + } + let directive = r#"::git-stage{cwd="/repo"}"#; + deliver(&mut chat, "current", "hidden", directive, false); + let key = ("current".to_owned(), "hidden".to_owned()); + let previous = history_cell::AgentMarkdownCell::new( + "Earlier reply".into(), + chat.config.cwd.as_path(), + ); + if !delayed_correction { + chat.reconcile_corrected_cloud_copy(&key, Some(&previous)); + } + let expected = if same_turn { "Earlier reply" } else { "" }; + assert_eq!(notification(&mut chat), expected); + if delayed_correction { + chat.reconcile_corrected_cloud_copy(&key, Some(&previous)); + } + assert_eq!(chat.last_agent_markdown_text(), Some("Earlier reply")); + chat.transcript.active_streamed_agent_message = Some(("next".into(), "live".into())); + chat.on_agent_message_delta("Still streaming".into()); + deliver(&mut chat, "older", "untracked", directive, false); + assert!(chat.stream_controller.is_some()); + let transcript = &chat.transcript; + assert!(transcript.has_replayed_cloud_text("older", "untracked")); + } + } + for plan_first in [false, true] { + let (mut chat, _, _, _) = make_chatwidget_manual_with_sender().await; + chat.transcript.replayed_cloud_text = Some(Default::default()); + deliver(&mut chat, "current", "blank", "", true); + chat.transcript.last_flushed_agent_message = Some(("current".into(), "blank".into())); + deliver(&mut chat, "current", "blank", "Visible", false); + assert_eq!(chat.last_agent_markdown_text(), Some("Visible")); + if plan_first { + chat.on_plan_item_completed("Plan".into()); + } + deliver(&mut chat, "current", "agent", "Part", true); + if !plan_first { + chat.on_plan_item_completed("Plan".into()); + } + deliver(&mut chat, "current", "agent", "Final", false); + let expected = if plan_first { "Final" } else { "Plan" }; + assert_eq!(chat.last_agent_markdown_text(), Some(expected)); + assert_eq!(notification(&mut chat), expected); + } +} diff --git a/codex-rs/tui/src/chatwidget/transcript.rs b/codex-rs/tui/src/chatwidget/transcript.rs index e001eb7404..4de5379452 100644 --- a/codex-rs/tui/src/chatwidget/transcript.rs +++ b/codex-rs/tui/src/chatwidget/transcript.rs @@ -31,6 +31,9 @@ pub(super) struct StatusCopySource { pub(super) fields: Vec<(String, Arc)>, } +/// Completion status and observation order for each replayed assistant item in one turn. +type ReplayedCloudItems = std::collections::HashMap; + #[derive(Default)] pub(super) struct TranscriptState { pub(super) active_cell: Option>, @@ -45,12 +48,12 @@ pub(super) struct TranscriptState { pub(super) last_agent_markdown: Option, /// Original source of that response, before display sanitization, for exact block copying. pub(super) last_agent_source: Option, + pub(super) last_agent_copy_item: Option<(String, String)>, /// Latest `/status` card and fields, retained across copying until a later turn or command. pub(super) last_status_copy_targets: Option, pub(super) last_completed_agent_message: Option<(String, String)>, - /// Finished assistant items projected into this widget from Cloud history, keyed by turn. - pub(super) replayed_cloud_text: - Option>>, + /// Whether each assistant item projected from Cloud history was finished, keyed by turn. + pub(super) replayed_cloud_text: Option>, pub(super) recording_finished_cloud_text: bool, /// Turn and item IDs for the agent message currently supplying streamed deltas. pub(super) active_streamed_agent_message: Option<(String, String)>, @@ -58,8 +61,13 @@ pub(super) struct TranscriptState { pub(super) last_flushed_agent_message: Option<(String, String)>, /// Raw markdown of the most recently completed proposed plan. pub(super) latest_proposed_plan_markdown: Option, - /// Whether this turn already produced a copyable response. - pub(super) saw_copy_source_this_turn: bool, + /// Observation order shared by assistant replies and proposed plans. + pub(super) response_order: i64, + /// Negative positions for replies prepended by older history pages. + pub(super) older_response_order: i64, + pub(super) historical_copy_order: Option, + /// Last completed response from this turn, independent of provisional copy text. + pub(super) committed_turn_response: Option<(i64, String)>, /// Whether the current turn emitted a plan update. pub(super) saw_plan_update_this_turn: bool, /// Whether the current turn emitted a proposed plan item that has not been superseded by a @@ -85,7 +93,8 @@ impl TranscriptState { self.replayed_cloud_text .as_ref() .and_then(|turns| turns.get(turn_id)) - .is_some_and(|items| items.contains(item_id)) + .and_then(|items| items.get(item_id)) + .is_some_and(|(finished, _)| *finished) } pub(super) fn bump_active_cell_revision(&mut self) { @@ -109,18 +118,25 @@ impl TranscriptState { self.last_agent_markdown = Some(crate::markdown_render::followup_labels(&markdown).into_owned()); self.last_agent_source = Some(source); - self.saw_copy_source_this_turn = true; + self.last_agent_copy_item = None; + self.historical_copy_order = None; + self.committed_turn_response = self + .last_agent_markdown + .clone() + .map(|text| (self.response_order, text)); } pub(super) fn reset_copy_history(&mut self) { self.last_status_copy_targets = None; self.last_agent_markdown = None; self.last_agent_source = None; - self.saw_copy_source_this_turn = false; + self.last_agent_copy_item = None; + self.historical_copy_order = None; + self.committed_turn_response = None; } pub(super) fn reset_turn_flags(&mut self) { - self.saw_copy_source_this_turn = false; + self.committed_turn_response = None; self.last_completed_agent_message = None; self.active_streamed_agent_message = None; self.last_flushed_agent_message = None; diff --git a/codex-rs/tui/src/chatwidget/turn_runtime.rs b/codex-rs/tui/src/chatwidget/turn_runtime.rs index d1acdfcc5d..c27757ec2f 100644 --- a/codex-rs/tui/src/chatwidget/turn_runtime.rs +++ b/codex-rs/tui/src/chatwidget/turn_runtime.rs @@ -152,19 +152,13 @@ impl ChatWidget { }); // For desktop notifications: prefer the notification payload, fall back to // the item-level copy source if present, otherwise send an empty string. + let committed = self.transcript.committed_turn_response.take(); let notification_response = sanitized_last_agent_message .as_ref() .filter(|message| !message.is_empty()) .cloned() - .or_else(|| { - if self.transcript.saw_copy_source_this_turn { - self.transcript.last_agent_markdown.clone() - } else { - None - } - }) + .or_else(|| committed.map(|(_, text)| text)) .unwrap_or_default(); - self.transcript.saw_copy_source_this_turn = false; // If a stream is currently active, finalize it. self.flush_answer_and_plan_streams(); self.flush_interrupt_activity(); diff --git a/codex-rs/tui/src/history_cell/messages.rs b/codex-rs/tui/src/history_cell/messages.rs index 84f5a86373..7287cd321e 100644 --- a/codex-rs/tui/src/history_cell/messages.rs +++ b/codex-rs/tui/src/history_cell/messages.rs @@ -570,6 +570,24 @@ impl HistoryCell for AgentMessageCell { } } +/// An unfinished Cloud reply's position before it has any visible text. +#[derive(Debug)] +pub(crate) struct PendingAgentMessageCell(pub(crate) (String, String)); + +impl HistoryCell for PendingAgentMessageCell { + fn display_lines(&self, _width: u16) -> Vec> { + Vec::new() + } + + fn raw_lines(&self) -> Vec> { + Vec::new() + } + + fn desired_height(&self, _width: u16) -> u16 { + 0 + } +} + /// A consolidated agent message cell that stores raw markdown source and re-renders from it. /// /// After a stream finalizes, the `ConsolidateAgentMessage` handler in `App` @@ -586,7 +604,7 @@ impl HistoryCell for AgentMessageCell { /// because resolving their local file links depends on filesystem state that can change later. #[derive(Debug)] pub(crate) struct AgentMarkdownCell { - /// Identifies an interrupted reply that can still receive its authoritative completion. + /// Reply identity used for completion replacement and corrected copy ordering. pub(crate) interrupted_item: Option<(String, String)>, pub(crate) phase: Option, markdown_source: String,