diff --git a/codex-rs/tui/src/app/history_completion.rs b/codex-rs/tui/src/app/history_completion.rs index 12c7d5ef06..fc39a23266 100644 --- a/codex-rs/tui/src/app/history_completion.rs +++ b/codex-rs/tui/src/app/history_completion.rs @@ -1,4 +1,5 @@ -//! Split older items at turn boundaries so each terminal row is rendered on the page that owns it. +//! Split older items at turn boundaries so each terminal row is rendered on its owning page. +//! Item IDs are unique only within their turn. use codex_app_server_protocol::ThreadItemPayload; use codex_app_server_protocol::Turn; @@ -7,30 +8,30 @@ use codex_app_server_protocol::TurnStatus; use std::collections::HashMap; pub(super) fn group_turn_items<'a>( - items: &[ThreadItemPayload], + items: &[(String, ThreadItemPayload)], turns: &'a [Turn], ) -> Vec<(std::ops::Range, Option<&'a Turn>)> { let offsets: HashMap<_, _> = items .iter() .enumerate() - .map(|(i, item)| (item.id(), i + 1)) + .map(|(i, (turn_id, item))| ((turn_id.as_str(), item.id()), i + 1)) .collect(); - let first = turns.iter().find_map(|turn| turn.items.first()); + let first = turns + .iter() + .find_map(|turn| turn.items.first().map(|item| (turn.id.as_str(), item.id()))); // Itemless interruptions follow the preceding turn, or precede the first item in history. - let mut end = first - .filter(|item| offsets.get(item.id()) == Some(&1)) - .map(|_| 0); + let mut end = first.filter(|key| offsets.get(key) == Some(&1)).map(|_| 0); let mut start = 0; let mut groups = Vec::new(); for turn in turns { if let Some(last) = turn.items.last() { - end = offsets.get(last.id()).copied(); + end = offsets.get(&(turn.id.as_str(), last.id())).copied(); } else if turn.status != TurnStatus::Interrupted || turn.items_view == TurnItemsView::NotLoaded { continue; } - if let Some(end) = end { + if let Some(end) = end.filter(|end| *end >= start) { groups.push((start..end, Some(turn))); start = end; } diff --git a/codex-rs/tui/src/app/history_completion_tests.rs b/codex-rs/tui/src/app/history_completion_tests.rs index f457a7e1e4..57cac93d42 100644 --- a/codex-rs/tui/src/app/history_completion_tests.rs +++ b/codex-rs/tui/src/app/history_completion_tests.rs @@ -1,13 +1,18 @@ //! Pagination boundaries preserve grouped item rendering and attach completion metadata once. +#![cfg(test)] + use super::*; use codex_app_server_protocol::ThreadItem; use codex_app_server_protocol::TurnItemsView; use codex_app_server_protocol::TurnStatus; use pretty_assertions::assert_eq; -fn payloads(items: &[codex_app_server_protocol::ThreadItem]) -> Vec { - items.iter().map(|entry| entry.payload.clone()).collect() +fn payloads(turn: &Turn, items: &[ThreadItem]) -> Vec<(String, ThreadItemPayload)> { + items + .iter() + .map(|entry| (turn.id.clone(), entry.payload.clone())) + .collect() } fn turn(id: &str, status: TurnStatus, item_ids: &[&str]) -> Turn { @@ -40,11 +45,11 @@ fn split_turn_only_completes_on_the_page_with_its_last_item() { &["first", "second", "last"], )]; assert_eq!( - group_turn_items(&payloads(&turns[0].items[1..]), &turns), + group_turn_items(&payloads(&turns[0], &turns[0].items[1..]), &turns), vec![(0..2, Some(&turns[0]))], ); assert_eq!( - group_turn_items(&payloads(&turns[0].items[..1]), &turns), + group_turn_items(&payloads(&turns[0], &turns[0].items[..1]), &turns), vec![(0..1, None)], ); } @@ -52,17 +57,21 @@ fn split_turn_only_completes_on_the_page_with_its_last_item() { #[test] fn multiple_turns_keep_item_groups_and_completion_order() { let turns = vec![ - turn("first", TurnStatus::Completed, &["a", "b"]), - turn("second", TurnStatus::Completed, &["c", "d"]), + turn("first", TurnStatus::Completed, &["a", "shared"]), + turn("second", TurnStatus::Completed, &["middle"]), + turn("last", TurnStatus::Completed, &["shared"]), ]; let items = turns .iter() - .flat_map(Turn::item_payloads) - .cloned() + .flat_map(|turn| payloads(turn, &turn.items)) .collect::>(); assert_eq!( group_turn_items(&items, &turns), - vec![(0..2, Some(&turns[0])), (2..4, Some(&turns[1])),], + vec![ + (0..2, Some(&turns[0])), + (2..3, Some(&turns[1])), + (3..4, Some(&turns[2])) + ], ); } @@ -75,7 +84,7 @@ fn itemless_interruptions_keep_their_position_on_the_owning_page() { turn("running", TurnStatus::InProgress, &["b"]), ]; assert_eq!( - group_turn_items(&payloads(&turns[1].items), &turns), + group_turn_items(&payloads(&turns[1], &turns[1].items), &turns), vec![ (0..0, Some(&turns[0])), (0..1, Some(&turns[1])), @@ -83,7 +92,7 @@ fn itemless_interruptions_keep_their_position_on_the_owning_page() { ], ); assert_eq!( - group_turn_items(&payloads(&turns[3].items), &turns), + group_turn_items(&payloads(&turns[3], &turns[3].items), &turns), vec![(0..1, Some(&turns[3]))], ); } diff --git a/codex-rs/tui/src/app/history_pagination.rs b/codex-rs/tui/src/app/history_pagination.rs index be9c9c74d9..a660d62192 100644 --- a/codex-rs/tui/src/app/history_pagination.rs +++ b/codex-rs/tui/src/app/history_pagination.rs @@ -162,7 +162,7 @@ impl App { &mut self, tui: &mut tui::Tui, turns: &[Turn], - hidden_item_ids: &HashSet<&str>, + hidden_item_ids: &HashSet<(&str, &str)>, thread_id: ThreadId, cwd: &AbsolutePathBuf, visibility: RawReasoningVisibility, @@ -172,38 +172,35 @@ impl App { } let user_items = turns .iter() - .flat_map(Turn::item_payloads) - .filter(|item| matches!(item, ThreadItemPayload::UserMessage { .. })) - .map(|item| (item.id().to_string(), item.clone())) + .flat_map(|turn| { + turn.item_payloads() + .map(move |item| (turn.id.as_str(), item)) + }) + .filter(|(_, item)| matches!(item, ThreadItemPayload::UserMessage { .. })) + .map(|(turn_id, item)| (hidden_item_ids.contains(&(turn_id, item.id())), item)) .collect::>(); let projected_user_cells = thread_items_to_transcript_cells( Some(thread_id), cwd, - user_items.iter().map(|(_, item)| item.clone()), + user_items.iter().map(|(_, item)| (*item).clone()), visibility, Some(&self.config), ); let persisted_user_cells = user_items .into_iter() .zip(projected_user_cells) - .map(|((item_id, _), cell)| (item_id, cell)) + .map(|((hidden, _), cell)| (hidden, cell)) .collect(); if self.chat_widget.thread_id() != Some(thread_id) && let Some(pane) = self.side_companions.get_mut(&thread_id) { - for index in - hidden_transcript_indices(&pane.cells, persisted_user_cells, hidden_item_ids) - { + for index in hidden_transcript_indices(&pane.cells, persisted_user_cells) { pane.splice_older_cells(index..index + 1, Vec::new()); pane.view.restart_search(); } return; } - let indices = hidden_transcript_indices( - &self.transcript_cells, - persisted_user_cells, - hidden_item_ids, - ); + let indices = hidden_transcript_indices(&self.transcript_cells, persisted_user_cells); if indices.is_empty() { return; } @@ -245,9 +242,9 @@ impl App { /// Project each terminal row on the page containing the end of its turn. fn project_older_history_cells( &mut self, - items: Vec, + items: Vec<(String, ThreadItemPayload)>, turns: &[Turn], - hidden_item_ids: &HashSet<&str>, + hidden_item_ids: &HashSet<(&str, &str)>, thread_id: ThreadId, cwd: &AbsolutePathBuf, visibility: RawReasoningVisibility, @@ -255,11 +252,13 @@ impl App { let mut cells = Vec::new(); for (range, turn_end) in completion::group_turn_items(&items, turns) { // Internal prompts stay invisible, but still separate adjacent tool groups. - for visible in items[range].split(|item| hidden_item_ids.contains(item.id())) { + 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().cloned(), + visible.iter().map(|(_, item)| item.clone()), visibility, Some(&self.config), )); @@ -401,7 +400,7 @@ impl App { } /// Classify internal review prompts across page boundaries before rendering any older items. -fn hidden_review_item_ids(turns: &[Turn]) -> HashSet<&str> { +fn hidden_review_item_ids(turns: &[Turn]) -> HashSet<(&str, &str)> { let mut hidden = HashSet::new(); let mut review_mode = false; for (index, turn) in turns.iter().enumerate() { @@ -418,7 +417,7 @@ fn hidden_review_item_ids(turns: &[Turn]) -> HashSet<&str> { ThreadItemPayload::UserMessage { .. } if review_mode || hidden_nested_review_turn => { - hidden.insert(item.id()); + hidden.insert((turn.id.as_str(), item.id())); } _ => {} } @@ -437,8 +436,7 @@ fn history_prefix_len(cells: &[Arc]) -> usize { /// Match repeated user messages from newest to oldest so identical visible prompts stay distinct. fn hidden_transcript_indices( cells: &[Arc], - mut persisted_user_cells: Vec<(String, Arc)>, - hidden_item_ids: &HashSet<&str>, + mut persisted_user_cells: Vec<(bool, Arc)>, ) -> Vec { let mut indices = Vec::new(); for (index, cell) in cells.iter().enumerate().rev() { @@ -458,7 +456,7 @@ fn hidden_transcript_indices( }) else { continue; }; - if hidden_item_ids.contains(persisted_user_cells[position].0.as_str()) { + if persisted_user_cells[position].0 { indices.push(index); } persisted_user_cells.truncate(position); diff --git a/codex-rs/tui/src/app/history_pagination_tests.rs b/codex-rs/tui/src/app/history_pagination_tests.rs index f16f4e13f4..1f64f80395 100644 --- a/codex-rs/tui/src/app/history_pagination_tests.rs +++ b/codex-rs/tui/src/app/history_pagination_tests.rs @@ -106,15 +106,12 @@ async fn fork_lineage_stays_before_repeated_older_history_pages() { fn repeated_prompt_matching_removes_only_the_hidden_occurrence() { let cells = vec![user_cell("repeat"), user_cell("other"), user_cell("repeat")]; let persisted = vec![ - ("unloaded".to_string(), user_cell("repeat")), - ("hidden".to_string(), user_cell("repeat")), - ("middle".to_string(), user_cell("other")), - ("visible".to_string(), user_cell("repeat")), + (true, user_cell("repeat")), + (true, user_cell("repeat")), + (false, user_cell("other")), + (false, user_cell("repeat")), ]; - assert_eq!( - hidden_transcript_indices(&cells, persisted, &HashSet::from(["hidden", "unloaded"])), - vec![0], - ); + assert_eq!(hidden_transcript_indices(&cells, persisted), vec![0],); } #[tokio::test] @@ -144,8 +141,8 @@ async fn resume_places_interruption_before_searches_split_across_pages() { canceled.items.push(search("canceled")); audit.items.insert(/*index*/ 0, search("older")); let page = vec![ - canceled.items[0].payload.clone(), - audit.items[0].payload.clone(), + (canceled.id.clone(), canceled.items[0].payload.clone()), + (audit.id.clone(), audit.items[0].payload.clone()), ]; let cwd = app.config.cwd.clone(); let older = app.project_older_history_cells( @@ -213,14 +210,16 @@ async fn hidden_last_item_keeps_turn_groups_and_completion_boundaries() { first.completed_at = Some(1_700_000_000); first.duration_ms = Some(125_000); let mut second = turn("second", TurnStatus::InProgress, &[]); - second.items = vec![computer("newer-call")]; + second.items = vec![computer("hidden")]; let turns = vec![first, second]; let hidden = hidden_review_item_ids(&turns); - assert_eq!(hidden, HashSet::from(["hidden"])); + assert_eq!(hidden, HashSet::from([("first", "hidden")])); let items = turns .iter() - .flat_map(Turn::item_payloads) - .cloned() + .flat_map(|turn| { + turn.item_payloads() + .map(|item| (turn.id.clone(), item.clone())) + }) .collect::>(); let cells = app.project_older_history_cells( @@ -247,10 +246,7 @@ async fn hidden_last_item_keeps_turn_groups_and_completion_boundaries() { assert_eq!( (groups, completions, visible_users), ( - vec![ - vec!["older-call".to_string()], - vec!["newer-call".to_string()] - ], + vec![vec!["older-call".to_string()], vec!["hidden".to_string()]], expected_completions, 0, ), 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 749f2e9f56..11a6dd305f 100644 --- a/codex-rs/tui/src/app/tests/history_hydration_tests.rs +++ b/codex-rs/tui/src/app/tests/history_hydration_tests.rs @@ -176,7 +176,7 @@ async fn history_hydration_metadata_tracks_missing_turns_and_stale_completions_k .apply_older_history_page(target.thread_id, &old_cursor, page, &mut thread.turns) .await?; assert_eq!( - items.iter().map(ThreadItemPayload::id).collect::>(), + items.iter().map(|(_, item)| item.id()).collect::>(), vec!["item-3-0", "item-5-0"] ); assert_eq!( diff --git a/codex-rs/tui/src/app/tests/pagination_completion_tests.rs b/codex-rs/tui/src/app/tests/pagination_completion_tests.rs index b444122e4c..68e206f717 100644 --- a/codex-rs/tui/src/app/tests/pagination_completion_tests.rs +++ b/codex-rs/tui/src/app/tests/pagination_completion_tests.rs @@ -201,8 +201,18 @@ async fn older_page_updates_the_parked_parent_without_moving_focus() -> Result<( #[tokio::test] async fn older_pagination_completion_footers_follow_answers_without_overlap_duplicates() -> Result<()> { - let (mut app, _codex_home, thread_id) = + let (mut app, codex_home, thread_id) = completed_history_app(&["Oldest", "Middle", "Newest"]).await?; + // Reuse an item ID across turns without changing the expected transcript. + let path = rollout_path( + codex_home.path(), + "2000-09-06T14-32-00", + &thread_id.to_string(), + ); + std::fs::write( + &path, + std::fs::read_to_string(&path)?.replace("answer-0", "prompt-1"), + )?; let (mut app_server, _requests, proxy) = start_recording_app_server( &app.config, /*blocked_thread_list*/ None, diff --git a/codex-rs/tui/src/app_server_session/history.rs b/codex-rs/tui/src/app_server_session/history.rs index 62d9950162..2b10177c96 100644 --- a/codex-rs/tui/src/app_server_session/history.rs +++ b/codex-rs/tui/src/app_server_session/history.rs @@ -243,7 +243,7 @@ impl AppServerSession { cursor: &str, page: ThreadItemsListResponse, turns: &mut Vec, - ) -> Result> { + ) -> Result> { if !self.is_older_history_page_pending(thread_id, cursor) { return Ok(Vec::new()); } @@ -304,7 +304,7 @@ impl AppServerSession { page: ThreadItemsListResponse, state: &mut ThreadHistoryPagination, turns: &mut Vec, - ) -> Result> { + ) -> Result> { state.next_item_cursor = advancing_cursor( state.next_item_cursor.as_deref(), page.next_cursor, @@ -342,7 +342,7 @@ impl AppServerSession { && !turn.items.iter().any(|item| item.id() == entry.item.id()) { let item = ThreadItem::try_from(entry)?; - items.push(item.payload.clone()); + items.push((turn.id.clone(), item.payload.clone())); turn.items.insert(/*index*/ 0, item); turn.items_view = TurnItemsView::Summary; } @@ -490,7 +490,7 @@ impl AppServerSession { fn rendered_history_rows( thread_id: ThreadId, thread: &Thread, - items: Vec, + items: Vec<(String, ThreadItemPayload)>, config: &Config, local_settings: &crate::local_settings::LocalSettings, width: u16, @@ -509,7 +509,7 @@ fn rendered_history_rows( thread_items_to_transcript_cells( Some(thread_id), &thread.cwd, - items, + items.into_iter().map(|(_, item)| item), visibility, Some(config), )