From fd05a402b294fe115d1459276e7bce00e8a44d05 Mon Sep 17 00:00:00 2001 From: Felipe Coury Date: Mon, 16 Nov 2026 23:48:27 +0000 Subject: [PATCH] Track conversation files for the TUI sidebar (#61920) ## What changed Build a per-conversation file inventory from message links, completed file changes, generated images, and previously opened documents. Deduplicate destinations while preserving source locations and prioritizing recent references. Scan older conversation items while the sidebar chooser is open without advancing transcript history. Cancel stale scans when the chooser closes or the connection changes, and refresh the inventory after reconnecting. ## Testing Add coverage for inventory ordering, remote paths, directory filtering, review and voice items across page boundaries, and reopening the chooser. Verify that older-file discovery leaves the transcript history cursor unchanged and ignores stale completions. GitOrigin-RevId: a80ea4f4a4e58da8dc142a099cfd048d69276836 --- codex-rs/tui/src/app.rs | 1 + codex-rs/tui/src/app/event_dispatch.rs | 31 ++ codex-rs/tui/src/app/history_pagination.rs | 2 +- codex-rs/tui/src/app/reconnect.rs | 3 + codex-rs/tui/src/app/side_focus.rs | 16 +- codex-rs/tui/src/app/sidebar.rs | 16 +- codex-rs/tui/src/app/sidebar_document.rs | 3 + codex-rs/tui/src/app/sidebar_files.rs | 458 ++++++++++++++++++ codex-rs/tui/src/app/sidebar_files_tests.rs | 200 ++++++++ codex-rs/tui/src/app/startup.rs | 1 + .../app/tests/pagination_completion_tests.rs | 154 ++++++ codex-rs/tui/src/app/thread_routing.rs | 1 + codex-rs/tui/src/app_event.rs | 6 + .../tui/src/app_server_session/history.rs | 6 +- codex-rs/tui/src/markdown_render.rs | 21 +- codex-rs/tui/src/terminal_hyperlinks.rs | 9 + 16 files changed, 917 insertions(+), 11 deletions(-) create mode 100644 codex-rs/tui/src/app/sidebar_files.rs create mode 100644 codex-rs/tui/src/app/sidebar_files_tests.rs diff --git a/codex-rs/tui/src/app.rs b/codex-rs/tui/src/app.rs index e1804f7993..2f4d1b18e9 100644 --- a/codex-rs/tui/src/app.rs +++ b/codex-rs/tui/src/app.rs @@ -286,6 +286,7 @@ mod side_pane; mod sidebar; mod sidebar_close; mod sidebar_document; +mod sidebar_files; pub(crate) mod sidebar_loading; mod startup; pub(crate) mod startup_bootstrap; diff --git a/codex-rs/tui/src/app/event_dispatch.rs b/codex-rs/tui/src/app/event_dispatch.rs index 1bb7d1cdeb..7bd9866d57 100644 --- a/codex-rs/tui/src/app/event_dispatch.rs +++ b/codex-rs/tui/src/app/event_dispatch.rs @@ -99,6 +99,7 @@ impl App { | AppEvent::OpenSidebarDocument { .. } | AppEvent::SidebarDocumentResolved { .. } | AppEvent::SidebarDocumentLoaded { .. } + | AppEvent::SidebarFilesLoaded { .. } | AppEvent::SavedSideChatsLoaded { .. } | AppEvent::SideReplayFinished(_) | AppEvent::ResetTranscriptForThreadSwitch @@ -983,6 +984,9 @@ impl App { self.recap.seed_from_turns(&retained_turns, Instant::now()); self.retain_realtime_replay_state_before_replace(); self.forget_realtime_replay_thread(thread_id); + if let Some(pane) = self.sidebars.get_mut(&Some(thread_id)) { + pane.clear_files(); + } self.chat_widget.reset_after_prompt_revert(reverted.thread.path, &retained_turns); self.ensure_thread_channel(thread_id).store.lock().await .set_session(session, retained_turns); @@ -1017,6 +1021,11 @@ impl App { self.begin_initial_history_replay_buffer(); } AppEvent::BeginThreadSwitchHistoryReplayBuffer => { + for pane in self.sidebars.values_mut() { + if let Some(files) = &mut pane.files { + files.scan = None; + } + } self.begin_thread_switch_history_replay_buffer(); } AppEvent::ParkSideCompanion { pair, thread_id, stream, display } => { @@ -1085,6 +1094,12 @@ impl App { } tui.frame_requester().schedule_frame(); } + if finished_current_replay && self.chooser_focus().is_some() + && self.document_preview_available(tui, app_server) + { + self.refresh_sidebar_files(app_server, id).await; + tui.frame_requester().schedule_frame(); + } } AppEvent::SidePanePressAfterReplay { peer, thread_id, area, mut press, draft_cursor } => { if self.pending_thread_switch_resets == 0 @@ -3621,6 +3636,22 @@ impl App { tui.frame_requester().schedule_frame(); } } + AppEvent::SidebarFilesLoaded { backend_id, parent, request, page } => { + if backend_id == app_server.backend_id() + && let Some(pane) = self.sidebars.get_mut(&Some(parent)) + && let Some(files) = pane.files.as_mut() + && files.scan.as_ref().is_some_and(|(id, _)| *id == request) + { + files.scan = None; + if let Some(page) = page { + if files.apply_page(page) { + pane.tabs_revision += 1; + } + self.start_sidebar_files_scan(app_server, parent); + } + tui.frame_requester().schedule_frame(); + } + } AppEvent::SavedSideChatsLoaded { backend_id, parent, request, result } => { use crate::app_event::SavedSideChatData; use crate::sidebar_chooser::Discovery; diff --git a/codex-rs/tui/src/app/history_pagination.rs b/codex-rs/tui/src/app/history_pagination.rs index 8a8d11cb12..499cac8f27 100644 --- a/codex-rs/tui/src/app/history_pagination.rs +++ b/codex-rs/tui/src/app/history_pagination.rs @@ -492,7 +492,7 @@ fn split_image_insertion( } /// Classify internal review prompts across page boundaries before rendering any older items. -fn hidden_review_item_ids(turns: &[Turn]) -> HashSet<(&str, &str)> { +pub(super) 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() { diff --git a/codex-rs/tui/src/app/reconnect.rs b/codex-rs/tui/src/app/reconnect.rs index a525ee8bb4..c4606b6440 100644 --- a/codex-rs/tui/src/app/reconnect.rs +++ b/codex-rs/tui/src/app/reconnect.rs @@ -419,6 +419,9 @@ impl App { for doc in pane.documents.values_mut() { doc.connection_replaced(); } + if let Some(files) = &mut pane.files { + files.connection_replaced(); + } } // The old timer targets the discarded channel, so a new preview can be scheduled. self.config_notification_test_pending = None; diff --git a/codex-rs/tui/src/app/side_focus.rs b/codex-rs/tui/src/app/side_focus.rs index fe1f26542a..37586652eb 100644 --- a/codex-rs/tui/src/app/side_focus.rs +++ b/codex-rs/tui/src/app/side_focus.rs @@ -61,6 +61,9 @@ impl App { && let Some(parent) = parent { self.refresh_saved_chats(server, parent); + if self.document_preview_available(tui, server) { + self.refresh_sidebar_files(server, parent).await; + } } tui.frame_requester().schedule_frame(); Ok(()) @@ -149,14 +152,13 @@ impl App { let active = pane.visible_content() == Some(SidebarContent::Document(id)); pane.remove_document(id); if active { - self.zoomed_thread = None; - self.sidebar_mut() - .show(SidebarContent::Chooser, PaneFocus::Sidebar); - if let Some(previous) = self - .sidebar() - .and_then(|pane| pane.recent_tab(/*excluding*/ None)) - { + if let Some(previous) = pane.recent_tab(/*excluding*/ None) { + self.zoomed_thread = None; + self.sidebar_mut() + .show(SidebarContent::Chooser, PaneFocus::Sidebar); self.activate_sidebar_tab(tui, server, previous).await?; + } else { + self.open_sidebar_tabs(tui, server).await?; } } tui.frame_requester().schedule_frame(); diff --git a/codex-rs/tui/src/app/sidebar.rs b/codex-rs/tui/src/app/sidebar.rs index 8d7ae84866..f294dfe24b 100644 --- a/codex-rs/tui/src/app/sidebar.rs +++ b/codex-rs/tui/src/app/sidebar.rs @@ -80,6 +80,8 @@ pub(super) struct SidebarInputState { pub(super) struct SidebarPane { pub(super) creation: Option, pub(super) close: Option, + pub(super) files: Option, + pub(super) opened_files: Vec, pub(super) documents: HashMap, pub(super) focus_document_request: Option, content: SidebarContent, @@ -94,7 +96,7 @@ pub(super) struct SidebarPane { // Visit order is independent of the order in which tabs appear. visits: Vec, pub(super) choice: usize, - tabs_revision: usize, + pub(super) tabs_revision: usize, focus: Option, motion: PaneMotion, } @@ -162,10 +164,19 @@ impl SidebarPane { if content != SidebarContent::Chooser || focus == PaneFocus::Main { self.opening = None; } + if content != SidebarContent::Chooser + && let Some(files) = &mut self.files + { + files.scan = None; + } self.content = content; self.focus = Some(focus); } + pub(super) fn clear_files(&mut self) { + self.files = None; + } + pub(super) fn chat_tabs(&self) -> ChatTabs<'_> { ChatTabs { label_style: crate::thread_label::ThreadLabelStyle::Plain, @@ -372,6 +383,9 @@ impl SidebarPane { self.cancel_hidden_document_primary(/*visible*/ false); self.focus_document_request = None; self.opening = None; + if let Some(files) = &mut self.files { + files.scan = None; + } self.focus = None; } diff --git a/codex-rs/tui/src/app/sidebar_document.rs b/codex-rs/tui/src/app/sidebar_document.rs index 786eaf8bd0..ae0aa712b9 100644 --- a/codex-rs/tui/src/app/sidebar_document.rs +++ b/codex-rs/tui/src/app/sidebar_document.rs @@ -512,6 +512,9 @@ impl App { } let request = uuid::Uuid::new_v4(); let pane = self.sidebar_mut(); + if !pane.opened_files.contains(&path) { + pane.opened_files.push(path.clone()); + } pane.documents.insert( request, Document { diff --git a/codex-rs/tui/src/app/sidebar_files.rs b/codex-rs/tui/src/app/sidebar_files.rs new file mode 100644 index 0000000000..8134b3091a --- /dev/null +++ b/codex-rs/tui/src/app/sidebar_files.rs @@ -0,0 +1,458 @@ +//! Inventory files and links for the owning conversation without moving its history cursor. + +use super::thread_events::ThreadBufferedEvent; +use super::*; +use crate::app_server_session::ThreadParamsMode; +use crate::markdown_render::FileLink; +use crate::terminal_hyperlinks::ReaderLink; +use codex_app_server_protocol::PatchApplyStatus; +use codex_app_server_protocol::PatchChangeKind; +use codex_app_server_protocol::UserInput; +use codex_utils_path_uri::LegacyAppPathString; +use codex_utils_path_uri::PathUri; +use tokio_util::task::AbortOnDropHandle; + +const LINK_SCAN_WIDTH: u16 = 100; + +pub(super) struct Files { + pub(super) entries: Vec, + by_destination: HashMap, + // Cached paths and reader opens follow the portion confirmed by the current scan. + confirmed: usize, + pub(super) cwd: Option, + inspect_local_paths: bool, + pub(super) scan: Option<(uuid::Uuid, AbortOnDropHandle<()>)>, + // Advance only after a successful page so reopening the chooser can retry a failed request. + pub(super) next: Option, + needs_history_cursor: bool, + seen_cursors: HashSet, + older_turn: Vec, + review_mode: bool, +} + +impl Files { + fn new(cwd: Option, next: Option, inspect_local_paths: bool) -> Self { + Self { + entries: Vec::new(), + by_destination: HashMap::new(), + confirmed: 0, + cwd, + inspect_local_paths, + scan: None, + next, + needs_history_cursor: false, + seen_cursors: HashSet::new(), + older_turn: Vec::new(), + review_mode: false, + } + } + + pub(super) fn connection_replaced(&mut self) { + self.scan = None; + self.needs_history_cursor = true; + self.confirmed = 0; + self.seen_cursors.clear(); + self.older_turn.clear(); + self.review_mode = false; + } + + pub(super) fn apply_page( + &mut self, + page: codex_app_server_protocol::ThreadItemsListResponse, + ) -> bool { + let cursor = self.next.take(); + self.seen_cursors.extend(cursor); + self.next = page + .next_cursor + .filter(|next| !self.seen_cursors.contains(next)); + let mut entries = Vec::new(); + for item in page.data { + if self + .older_turn + .last() + .is_some_and(|last| last.turn_id != item.turn_id) + { + self.finish_older_turn(&mut entries); + } + if matches!( + item.item.payload, + ThreadItemPayload::AgentMessage { .. } + | ThreadItemPayload::UserMessage { .. } + | ThreadItemPayload::Plan { .. } + | ThreadItemPayload::FileChange { .. } + | ThreadItemPayload::ImageGeneration(_) + | ThreadItemPayload::EnteredReviewMode { .. } + | ThreadItemPayload::ExitedReviewMode { .. } + ) { + self.older_turn.push(item); + } + } + if self.next.is_none() { + self.finish_older_turn(&mut entries); + } + self.extend_page(entries) + } + + fn finish_older_turn(&mut self, entries: &mut Vec) { + let handoff = self + .older_turn + .iter() + .rposition(|entry| is_voice_handoff(&entry.item.payload)); + for (index, entry) in self.older_turn.drain(..).enumerate() { + let item = &entry.item.payload; + match item { + ThreadItemPayload::ExitedReviewMode { .. } => self.review_mode = true, + ThreadItemPayload::EnteredReviewMode { .. } => self.review_mode = false, + ThreadItemPayload::UserMessage { .. } if self.review_mode => {} + _ if handoff.is_some_and(|handoff| index < handoff) + && crate::chatwidget::is_private_realtime_agent_item(item) => {} + _ => newest_item_entries(self.cwd.as_ref(), item, entries), + } + } + } + + pub(super) fn extend(&mut self, entries: impl IntoIterator) -> bool { + let mut changed = false; + for entry in entries { + if let Some(&index) = self.by_destination.get(&entry.path) { + let old = &mut self.entries[index]; + if old.location.is_none() && entry.location.is_some() { + old.location = entry.location; + changed = true; + } + } else { + let path = Path::new(&entry.path); + // Keep unresolved links, including paths on another host. + if self.inspect_local_paths && path.is_absolute() && path.is_dir() { + continue; + } + self.by_destination + .insert(entry.path.clone(), self.entries.len()); + self.entries.push(entry); + changed = true; + } + } + changed + } + + fn promote(&mut self, entries: impl IntoIterator) -> bool { + self.merge_history(/*start*/ 0, entries) + } + + pub(super) fn extend_page(&mut self, entries: Vec) -> bool { + if self.confirmed == self.entries.len() { + let changed = self.extend(entries); + self.confirmed = self.entries.len(); + return changed; + } + self.merge_history(self.confirmed, entries) + } + + fn merge_history(&mut self, start: usize, entries: impl IntoIterator) -> bool { + let previous = self.entries.split_off(start); + let confirmed = self.confirmed - start; + self.by_destination.retain(|_, index| *index < start); + self.extend(entries); + for (offset, old) in previous.iter().enumerate() { + if offset == confirmed { + self.confirmed = self.entries.len(); + } + if let Some(&index) = self.by_destination.get(&old.path) { + if self.entries[index].location.is_none() { + self.entries[index].location = old.location; + } + } else { + self.by_destination + .insert(old.path.clone(), self.entries.len()); + self.entries.push(old.clone()); + } + } + if confirmed == previous.len() { + self.confirmed = self.entries.len(); + } + self.entries[start..] != previous + } +} + +fn resolve(cwd: Option<&PathUri>, mut link: FileLink) -> Option { + if link.path.is_empty() || link.path.len() > 4096 || link.path.chars().any(char::is_control) { + return None; + } + let Some(cwd) = cwd else { + return Some(link); + }; + let path = LegacyAppPathString::from_string(&link.path) + .resolve_against(cwd, /*user_home_dir*/ None) + .ok()?; + if path.to_url().has_host() { + return None; + } + link.path = path.inferred_native_path_string(); + (link.path.len() <= 4096).then_some(link) +} + +fn is_voice_handoff(item: &ThreadItemPayload) -> bool { + matches!(item, ThreadItemPayload::UserMessage { content, .. } + if crate::chatwidget::realtime_delegation_input(content).is_some()) +} + +fn item_entries(cwd: Option<&PathUri>, item: &ThreadItemPayload, entries: &mut Vec) { + let mut found = |entry| entries.extend(resolve(cwd, entry)); + let mut markdown = |text: &str| { + for file in crate::markdown_render::file_links(text, /*cwd*/ None) { + found(file); + } + }; + match item { + ThreadItemPayload::AgentMessage { text, .. } => markdown(text), + ThreadItemPayload::Plan { text, .. } => markdown(text), + ThreadItemPayload::UserMessage { content, .. } + if crate::chatwidget::realtime_delegation_input(content).is_none() => + { + for input in content { + if let UserInput::Text { text, .. } = input { + markdown(text); + } + } + } + ThreadItemPayload::FileChange { + changes, + status: PatchApplyStatus::Completed, + .. + } => { + for change in changes { + let path = match &change.kind { + PatchChangeKind::Delete => continue, + PatchChangeKind::Update { + move_path: Some(path), + } => path.to_str(), + PatchChangeKind::Add | PatchChangeKind::Update { move_path: None } => { + Some(change.path.as_str()) + } + }; + if let Some(path) = path { + found(FileLink { + path: path.to_owned(), + location: None, + }); + } + } + } + ThreadItemPayload::ImageGeneration(image) if image.status == "completed" => { + for path in image.saved_path.iter().filter_map(|path| path.to_str()) { + found(FileLink { + path: path.to_owned(), + location: None, + }); + } + } + _ => {} + } +} + +fn newest_item_entries( + cwd: Option<&PathUri>, + item: &ThreadItemPayload, + entries: &mut Vec, +) { + let start = entries.len(); + item_entries(cwd, item, entries); + entries[start..].reverse(); +} + +impl App { + pub(super) async fn refresh_sidebar_files( + &mut self, + server: &AppServerSession, + parent: ThreadId, + ) { + // Until replay finishes, the rendered cells can still belong to the previous thread. + if self.pending_thread_switch_resets != 0 + || self.current_displayed_thread_id() != Some(parent) + { + return; + } + let Some(channel) = self.thread_event_channels.get(&parent) else { + return; + }; + let mut entries = Vec::new(); + let (cwd, inspect_local_paths) = { + let store = channel.store.lock().await; + let Some(session) = &store.session else { + return; + }; + let inspect_local_paths = server.thread_params_mode() == ThreadParamsMode::Embedded + && session + .primary_environment_cwd + .as_ref() + .is_none_or(|environment| environment.is_server_local); + let cwd = if inspect_local_paths { + Some(PathUri::from_abs_path(&session.cwd)) + } else { + session + .primary_environment_cwd + .as_ref() + .filter(|environment| environment.is_server_local) + .and_then(|environment| environment.path.to_inferred_path_uri()) + }; + for event in store.buffer.iter().rev() { + if let ThreadBufferedEvent::Notification(event) = event + && let ServerNotification::ItemCompleted(item) = event.as_ref() + // User messages already contribute through their visible transcript cells. + && !matches!(item.item.payload, ThreadItemPayload::UserMessage { .. }) + && (!store.is_realtime_delegated_turn(&item.turn_id) + || !crate::chatwidget::is_private_realtime_agent_item(&item.item.payload)) + { + newest_item_entries(cwd.as_ref(), &item.item.payload, &mut entries); + } + } + let hidden = super::history_pagination::hidden_review_item_ids(&store.turns); + for turn in store.turns.iter().rev() { + let has_handoff = turn.item_payloads().any(is_voice_handoff); + let mut turn_entries = Vec::new(); + let mut voice = store.is_realtime_delegated_turn(&turn.id) && !has_handoff; + for item in turn.item_payloads() { + voice |= is_voice_handoff(item); + if !hidden.contains(&(turn.id.as_str(), item.id())) + && (!voice || !crate::chatwidget::is_private_realtime_agent_item(item)) + { + item_entries(cwd.as_ref(), item, &mut turn_entries); + } + } + entries.extend(turn_entries.into_iter().rev()); + } + (cwd, inspect_local_paths) + }; + let active = self + .chat_widget + .active_cell_transcript_hyperlink_lines(LINK_SCAN_WIDTH) + .unwrap_or_default(); + let visible_links = self + .transcript_cells + .iter() + // Skip tool output, which can require rendering and highlighting an entire patch. + .filter(|cell| { + let cell = cell.as_any(); + cell.is::() + || cell.is::() + || cell.is::() + || cell.is::() + || cell.is::() + || cell.is::() + }) + .flat_map(|cell| cell.transcript_hyperlink_lines(LINK_SCAN_WIDTH)) + .flat_map(|line| line.hyperlinks) + .filter_map(|link| { + let ReaderLink::Document(file) = link.sidebar_destination()? else { + return None; + }; + resolve(cwd.as_ref(), file) + }); + let active_links = active + .into_iter() + .flat_map(|line| line.hyperlinks) + .filter_map(|link| { + let ReaderLink::Document(file) = link.sidebar_destination()? else { + return None; + }; + resolve(cwd.as_ref(), file) + }); + let pane = self.sidebars.entry(Some(parent)).or_default(); + if pane.files.as_ref().is_some_and(|files| files.cwd != cwd) { + pane.clear_files(); + } + let files = pane.files.get_or_insert_with(|| { + Files::new( + cwd.clone(), + server.older_item_cursor(parent).map(str::to_owned), + inspect_local_paths, + ) + }); + if std::mem::take(&mut files.needs_history_cursor) { + // Resume may have missed files between the cached and newly hydrated windows. + files.next = server.older_item_cursor(parent).map(str::to_owned); + } + let opened = pane + .opened_files + .iter() + .map(|path| FileLink { + path: path.inferred_native_path_string(), + location: None, + }) + .collect::>(); + if files.promote(active_links.rev().chain(entries).chain(visible_links.rev())) { + pane.tabs_revision += 1; + } + if files.extend(opened) { + pane.tabs_revision += 1; + } + self.start_sidebar_files_scan(server, parent); + } + + pub(super) fn start_sidebar_files_scan(&mut self, server: &AppServerSession, parent: ThreadId) { + // Check the owner again after every page before issuing the next one. + if self.current_displayed_thread_id() != Some(parent) || self.chooser_focus().is_none() { + return; + } + let Some(files) = self + .sidebars + .get_mut(&Some(parent)) + .and_then(|pane| pane.files.as_mut()) + else { + return; + }; + if files.scan.is_some() { + return; + } + let Some(cursor) = files.next.clone() else { + return; + }; + let request = uuid::Uuid::new_v4(); + let tx = self.app_event_tx.clone(); + let mut view = server.request_view(); + let task = tokio::spawn(async move { + let page = view + .thread_items_page( + parent, + /*turn_id*/ None, + Some(cursor), + /*limit*/ 100, + ) + .await + .ok(); + tx.send(AppEvent::SidebarFilesLoaded { + backend_id: view.backend_id(), + parent, + request, + page, + }); + }); + files.scan = Some((request, AbortOnDropHandle::new(task))); + } + + pub(super) fn notice_sidebar_file_item( + &mut self, + thread: ThreadId, + notification: &ServerNotification, + ) { + if let ServerNotification::ItemCompleted(item) = notification + && matches!( + item.item.payload, + ThreadItemPayload::FileChange { .. } | ThreadItemPayload::ImageGeneration(_) + ) + && let Some(pane) = self.sidebars.get_mut(&Some(thread)) + && let Some(files) = pane.files.as_mut() + { + let mut entries = Vec::new(); + newest_item_entries(files.cwd.as_ref(), &item.item.payload, &mut entries); + if !entries.is_empty() && files.promote(entries) { + pane.tabs_revision += 1; + self.chat_widget.request_redraw(); + } + } + } +} + +#[cfg(test)] +#[path = "sidebar_files_tests.rs"] +mod tests; diff --git a/codex-rs/tui/src/app/sidebar_files_tests.rs b/codex-rs/tui/src/app/sidebar_files_tests.rs new file mode 100644 index 0000000000..ac123aa643 --- /dev/null +++ b/codex-rs/tui/src/app/sidebar_files_tests.rs @@ -0,0 +1,200 @@ +#![cfg(test)] + +use super::*; +use crate::markdown_render::SourceLocation; +use pretty_assertions::assert_eq; + +#[test] +fn files_from_missed_pages_precede_older_cached_paths_and_reader_opens() { + let link = |path: &str| FileLink { + path: path.into(), + location: None, + }; + let mut files = Files::new( + /*cwd*/ None, /*next*/ None, /*inspect_local_paths*/ false, + ); + files.promote([link("new.md"), link("old.md")]); + files.extend([link("opened.md")]); + files.connection_replaced(); + files.promote([link("new.md")]); + files.extend_page(vec![link("missing.md")]); + files.promote([link("live.md")]); + files.extend_page(vec![link("old.md"), link("oldest.md")]); + assert_eq!( + files.entries, + [ + "live.md", + "new.md", + "missing.md", + "old.md", + "oldest.md", + "opened.md" + ] + .map(link) + ); +} + +#[test] +fn sidebar_files_keeps_remote_paths_for_the_owning_reader() { + let cwd = LegacyAppPathString::from_string("/remote/workspace") + .to_inferred_path_uri() + .expect("remote working directory"); + let mut entries = Vec::new(); + item_entries( + Some(&cwd), + &ThreadItemPayload::Plan { + id: "plan".into(), + text: "[Source](./src/lib.rs#L12C4)".into(), + }, + &mut entries, + ); + let mut files = Files::new( + Some(cwd), + /*next*/ None, + /*inspect_local_paths*/ false, + ); + files.promote(entries); + let path = LegacyAppPathString::from_string("/remote/workspace/src/lib.rs") + .to_inferred_path_uri() + .expect("remote document path") + .inferred_native_path_string(); + assert!(!files.extend([FileLink { + path: path.clone(), + location: None, + }])); + assert_eq!( + files.entries, + vec![FileLink { + path, + location: Some(SourceLocation { + line: 12, + column: 4 + }), + }], + ); +} + +#[test] +fn sidebar_files_skip_local_directories_and_keep_extensionless_files() +-> color_eyre::eyre::Result<()> { + let workspace = tempfile::tempdir()?; + std::fs::create_dir(workspace.path().join("folder.with-dot"))?; + std::fs::write(workspace.path().join("Makefile"), "")?; + let cwd = PathUri::from_abs_path(&codex_utils_absolute_path::AbsolutePathBuf::try_from( + workspace.path(), + )?); + let mut entries = Vec::new(); + item_entries( + Some(&cwd), + &ThreadItemPayload::Plan { + id: "plan".into(), + text: "[Folder](./folder.with-dot) [Build](./Makefile)".into(), + }, + &mut entries, + ); + let mut files = Files::new( + Some(cwd), + /*next*/ None, + /*inspect_local_paths*/ true, + ); + files.extend(entries); + assert_eq!( + files.entries, + vec![FileLink { + path: workspace + .path() + .join("Makefile") + .to_string_lossy() + .into_owned(), + location: None, + }] + ); + Ok(()) +} + +#[test] +fn sidebar_files_respect_page_boundaries_for_typed_voice_and_review_items() { + use codex_app_server_protocol::FileUpdateChange; + use codex_app_server_protocol::ThreadItem; + use codex_app_server_protocol::ThreadItemEntry; + use codex_app_server_protocol::ThreadItemsListResponse; + use codex_protocol::models::MessagePhase; + + let entry = |payload| ThreadItemEntry { + turn_id: "turn".into(), + item: ThreadItem::untimed(payload), + output_deferred: false, + started_at_ms: None, + completed_at_ms: None, + }; + let user = |id: &str, text: &str| { + entry(ThreadItemPayload::UserMessage { + id: id.into(), + client_id: None, + content: vec![UserInput::Text { + text: text.into(), + text_elements: Vec::new(), + }], + }) + }; + let status = |id: &str| { + entry(ThreadItemPayload::AgentMessage { + id: id.into(), + text: format!("[{id}](./{id}.md)"), + phase: Some(MessagePhase::Commentary), + memory_citation: None, + moderation: None, + delivery: None, + questions: None, + }) + }; + let mut files = Files::new( + /*cwd*/ None, + Some("first".into()), + /*inspect_local_paths*/ false, + ); + assert!(!files.apply_page(ThreadItemsListResponse { + data: vec![ + entry(ThreadItemPayload::ExitedReviewMode { + id: "exit".into(), + review: String::new() + }), + user("review", "[Hidden review](./review.md)"), + status("voice"), + ], + next_cursor: Some("older".into()), + backwards_cursor: None, + })); + assert!(files.apply_page(ThreadItemsListResponse { + data: vec![ + user( + "handoff", + "Spoken follow-up" + ), + entry(ThreadItemPayload::EnteredReviewMode { + id: "enter".into(), + review: String::new() + }), + status("typed"), + entry(ThreadItemPayload::FileChange { + id: "edit".into(), + changes: vec![FileUpdateChange { + path: "src/lib.rs".into(), + kind: PatchChangeKind::Update { move_path: None }, + diff: String::new(), + }], + status: PatchApplyStatus::Completed, + }), + ], + next_cursor: None, + backwards_cursor: None, + })); + assert_eq!( + files + .entries + .iter() + .map(|file| file.path.as_str()) + .collect::>(), + ["./typed.md", "src/lib.rs"], + ); +} diff --git a/codex-rs/tui/src/app/startup.rs b/codex-rs/tui/src/app/startup.rs index 08a93b7df1..8ea64997ba 100644 --- a/codex-rs/tui/src/app/startup.rs +++ b/codex-rs/tui/src/app/startup.rs @@ -1565,6 +1565,7 @@ See the Codex keymap documentation for supported actions and examples." AppEvent::SavedSideChatsLoaded { backend_id, .. } | AppEvent::SidebarDocumentResolved { backend_id, .. } | AppEvent::SidebarDocumentLoaded { backend_id, .. } + | AppEvent::SidebarFilesLoaded { backend_id, .. } | AppEvent::StartupThreadStarted { backend_id, .. } => { backends.slot(*backend_id).unwrap_or(displayed_session) } 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 68e206f717..bd5a0018c4 100644 --- a/codex-rs/tui/src/app/tests/pagination_completion_tests.rs +++ b/codex-rs/tui/src/app/tests/pagination_completion_tests.rs @@ -625,3 +625,157 @@ async fn stale_history_completions_preserve_the_current_request_and_failure_can_ proxy.await??; Ok(()) } + +#[cfg(not(windows))] +#[tokio::test] +async fn closing_the_first_preview_populates_the_files_chooser() -> Result<()> { + let (mut app, _codex_home, thread_id) = + completed_history_app(&["[Guide](./guide.md)", "[Notes](./notes.md)"]).await?; + let mut server = crate::start_embedded_app_server_for_picker(&app.config).await?; + let started = server + .resume_thread( + &app.local_settings, + app.config.clone(), + thread_id, + crate::app_server_session::ResumeModelSettings::RestoreFromThread, + ) + .await?; + app.enqueue_primary_thread_session(started.session, started.turns) + .await?; + let mut tui = crate::tui::test_support::make_test_tui()?; + enable_side_retention(&mut app)?; + tui.set_owned_screen(/*owned*/ true)?; + + let original_path = codex_app_server_client::AppServerPath::from_app_server("notes.md"); + let path = server + .resolve_thread_file(thread_id, &original_path) + .await?; + Box::pin(app.open_resolved_document( + &mut tui, + &mut server, + thread_id, + original_path, + path, + /*location*/ None, + /*focus*/ true, + )) + .await?; + assert!(app.sidebar().and_then(|pane| pane.files.as_ref()).is_none()); + Box::pin(app.handle_tui_event(&mut tui, &mut server, TuiEvent::Key(KeyCode::Esc.into()))) + .await?; + assert_eq!(app.chooser_focus(), Some(sidebar::PaneFocus::Sidebar)); + let files = app + .sidebar() + .and_then(|pane| pane.files.as_ref()) + .expect("file inventory"); + let entries = files + .entries + .iter() + .map(|file| { + std::path::Path::new(&file.path) + .file_name() + .and_then(|name| name.to_str()) + }) + .collect::>(); + assert_eq!(entries, vec![Some("notes.md")]); + Ok(()) +} + +#[cfg(not(windows))] +#[tokio::test] +async fn sidebar_files_load_older_items_without_advancing_transcript_history() -> Result<()> { + let (mut app, _codex_home, thread_id) = + completed_history_app(&["[Earlier file](./earlier.md)", "[Latest file](./latest.md)"]) + .await?; + let mut server = crate::start_embedded_app_server_for_picker(&app.config).await?; + let started = server + .resume_thread( + &app.local_settings, + app.config.clone(), + thread_id, + crate::app_server_session::ResumeModelSettings::RestoreFromThread, + ) + .await?; + let cursor = server + .older_item_cursor(thread_id) + .expect("resume leaves an older page") + .to_owned(); + app.enqueue_primary_thread_session(started.session, started.turns) + .await?; + let (event_tx, mut events) = tokio::sync::mpsc::unbounded_channel(); + app.app_event_tx = crate::app_event_sender::AppEventSender::new(event_tx); + let mut tui = crate::tui::test_support::make_test_tui()?; + enable_side_retention(&mut app)?; + tui.set_owned_screen(/*owned*/ true)?; + app.sidebar_mut().show( + sidebar::SidebarContent::Chooser, + sidebar::PaneFocus::Sidebar, + ); + + // During a switch, the displayed identity is already the parent but the + // committed cells can still be from the previously focused side chat. + app.transcript_cells + .push(Arc::new(crate::history_cell::AgentMarkdownCell::new( + "[Child only](./child-only.md)".into(), + app.config.cwd.as_path(), + ))); + app.pending_thread_switch_resets = 1; + app.refresh_sidebar_files(&server, thread_id).await; + assert!(app.sidebar().and_then(|pane| pane.files.as_ref()).is_none()); + app.transcript_cells.clear(); + Box::pin(app.handle_event( + &mut tui, + &mut server, + AppEvent::SideReplayFinished(thread_id), + )) + .await?; + let has_earlier_file = |app: &App| { + app.sidebar() + .and_then(|pane| pane.files.as_ref()) + .is_some_and(|files| { + files + .entries + .iter() + .any(|entry| entry.path.ends_with("earlier.md")) + }) + }; + assert!(!has_earlier_file(&app)); + let event = tokio::time::timeout(Duration::from_secs(5), events.recv()) + .await? + .expect("older files completion"); + assert!(matches!(event, AppEvent::SidebarFilesLoaded { .. })); + // A completion that arrives after hiding the chooser must not advance its cursor. + app.sidebar_mut().hide(); + Box::pin(app.handle_event(&mut tui, &mut server, event)).await?; + assert!(!has_earlier_file(&app)); + assert_eq!( + app.sidebar_mut() + .files + .as_ref() + .and_then(|files| files.next.as_deref()), + Some(cursor.as_str()) + ); + app.sidebar_mut().show( + sidebar::SidebarContent::Chooser, + sidebar::PaneFocus::Sidebar, + ); + app.refresh_sidebar_files(&server, thread_id).await; + let event = tokio::time::timeout(Duration::from_secs(5), events.recv()) + .await? + .expect("older files completion after reopening"); + // Model an exhausted inventory before reconnect. The old completion must + // be discarded, then the fresh resume cursor must discover the missing file. + let files = app.sidebar_mut().files.as_mut().expect("file inventory"); + files.next = None; + files.connection_replaced(); + Box::pin(app.handle_event(&mut tui, &mut server, event)).await?; + assert!(!has_earlier_file(&app)); + app.refresh_sidebar_files(&server, thread_id).await; + let event = tokio::time::timeout(Duration::from_secs(5), events.recv()) + .await? + .expect("older files completion after reconnect"); + Box::pin(app.handle_event(&mut tui, &mut server, event)).await?; + assert!(has_earlier_file(&app)); + assert_eq!(server.older_item_cursor(thread_id), Some(cursor.as_str())); + Ok(()) +} diff --git a/codex-rs/tui/src/app/thread_routing.rs b/codex-rs/tui/src/app/thread_routing.rs index bd0c00454b..8983a86c38 100644 --- a/codex-rs/tui/src/app/thread_routing.rs +++ b/codex-rs/tui/src/app/thread_routing.rs @@ -1143,6 +1143,7 @@ impl App { return Ok(()); } self.track_side_chat_title(thread_id, ¬ification); + self.notice_sidebar_file_item(thread_id, ¬ification); if self.current_displayed_thread_id() == Some(thread_id) && let ServerNotification::TurnCompleted(notification) = ¬ification { diff --git a/codex-rs/tui/src/app_event.rs b/codex-rs/tui/src/app_event.rs index 4cca3dffa8..8a9e20297b 100644 --- a/codex-rs/tui/src/app_event.rs +++ b/codex-rs/tui/src/app_event.rs @@ -543,6 +543,12 @@ pub(crate) enum AppEvent { request: uuid::Uuid, result: Result, }, + SidebarFilesLoaded { + backend_id: crate::backend_id::BackendId, + parent: ThreadId, + request: uuid::Uuid, + page: Option, + }, SavedSideChatsLoaded { backend_id: crate::backend_id::BackendId, parent: ThreadId, diff --git a/codex-rs/tui/src/app_server_session/history.rs b/codex-rs/tui/src/app_server_session/history.rs index 15eb721475..3c8419619f 100644 --- a/codex-rs/tui/src/app_server_session/history.rs +++ b/codex-rs/tui/src/app_server_session/history.rs @@ -202,9 +202,13 @@ impl AppServerSession { } pub(crate) fn has_older_history(&self, thread_id: ThreadId) -> bool { + self.older_item_cursor(thread_id).is_some() + } + + pub(crate) fn older_item_cursor(&self, thread_id: ThreadId) -> Option<&str> { self.history_pagination .get(&thread_id) - .is_some_and(|page| page.next_item_cursor.is_some()) + .and_then(|page| page.next_item_cursor.as_deref()) } pub(crate) fn begin_older_history_page(&mut self, thread_id: ThreadId) -> Option { diff --git a/codex-rs/tui/src/markdown_render.rs b/codex-rs/tui/src/markdown_render.rs index ee5497df56..8e6cced3f0 100644 --- a/codex-rs/tui/src/markdown_render.rs +++ b/codex-rs/tui/src/markdown_render.rs @@ -84,6 +84,7 @@ mod local_links; pub(crate) use local_links::FileLink; pub(crate) use local_links::SourceLocation; pub(crate) use local_links::is_markdown_file; +pub(crate) use local_links::sidebar_file_link; mod math; mod mermaid; pub(crate) mod preferences; @@ -110,6 +111,24 @@ mod list_spacing_tests; mod task_list_tests; pub(crate) use web_links::hide_web_link_destination; +/// Find file destinations in source order, including Codex file citations. +pub(crate) fn file_links(input: &str, cwd: Option<&Path>) -> Vec { + let options = Options::ENABLE_STRIKETHROUGH | Options::ENABLE_TABLES; + let directives = InlineDirectives::new(input, options); + directives + .events( + Parser::new_ext(&directives.markdown, options).into_offset_iter(), + cwd, + ) + .filter_map(|(event, _)| match event { + Event::Start(Tag::Link { dest_url, .. } | Tag::Image { dest_url, .. }) => { + sidebar_file_link(&dest_url) + } + _ => None, + }) + .collect() +} + pub(crate) fn hide_reader_link_destination(destination: &str) -> bool { hide_web_link_destination(destination) || local_links::sidebar_file_link(destination).is_some() } @@ -2430,7 +2449,7 @@ impl<'a, 'policy> Writer<'a, 'policy> { fn push_link_target(&mut self, span: Span<'static>, destination: &str, source: usize) { let mut target = HyperlinkLine::default(); - if let Some(link) = local_links::sidebar_file_link(destination) { + if let Some(link) = sidebar_file_link(destination) { let width = span_width(&span); target.line.push_span(span); target diff --git a/codex-rs/tui/src/terminal_hyperlinks.rs b/codex-rs/tui/src/terminal_hyperlinks.rs index 682d6d503b..fcdcd2af2e 100644 --- a/codex-rs/tui/src/terminal_hyperlinks.rs +++ b/codex-rs/tui/src/terminal_hyperlinks.rs @@ -235,6 +235,15 @@ impl TerminalHyperlink { } } + pub(crate) fn sidebar_destination(&self) -> Option { + match self.destination_kind { + DestinationKind::TrustedFile => None, + DestinationKind::Document(_) | DestinationKind::Web => { + self.reader_destination(/*documents*/ true) + } + } + } + pub(crate) fn terminal_destination(&self) -> Option { match self.destination_kind { DestinationKind::Document(_) => None,