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
This commit is contained in:
Felipe Coury
2026-11-17 00:13:43 +00:00
committed by copyberry
parent a669d8ffb2
commit fd05a402b2
16 changed files with 917 additions and 11 deletions
+1
View File
@@ -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;
+31
View File
@@ -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;
+1 -1
View File
@@ -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() {
+3
View File
@@ -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;
+9 -7
View File
@@ -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();
+15 -1
View File
@@ -80,6 +80,8 @@ pub(super) struct SidebarInputState {
pub(super) struct SidebarPane {
pub(super) creation: Option<SideCreation>,
pub(super) close: Option<super::sidebar_close::Confirmation>,
pub(super) files: Option<super::sidebar_files::Files>,
pub(super) opened_files: Vec<codex_utils_path_uri::PathUri>,
pub(super) documents: HashMap<uuid::Uuid, super::sidebar_document::Document>,
pub(super) focus_document_request: Option<uuid::Uuid>,
content: SidebarContent,
@@ -94,7 +96,7 @@ pub(super) struct SidebarPane {
// Visit order is independent of the order in which tabs appear.
visits: Vec<SidebarTab>,
pub(super) choice: usize,
tabs_revision: usize,
pub(super) tabs_revision: usize,
focus: Option<PaneFocus>,
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;
}
+3
View File
@@ -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 {
+458
View File
@@ -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<FileLink>,
by_destination: HashMap<String, usize>,
// Cached paths and reader opens follow the portion confirmed by the current scan.
confirmed: usize,
pub(super) cwd: Option<PathUri>,
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<String>,
needs_history_cursor: bool,
seen_cursors: HashSet<String>,
older_turn: Vec<codex_app_server_protocol::ThreadItemEntry>,
review_mode: bool,
}
impl Files {
fn new(cwd: Option<PathUri>, next: Option<String>, 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<FileLink>) {
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<Item = FileLink>) -> 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<Item = FileLink>) -> bool {
self.merge_history(/*start*/ 0, entries)
}
pub(super) fn extend_page(&mut self, entries: Vec<FileLink>) -> 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<Item = FileLink>) -> 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<FileLink> {
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<FileLink>) {
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<FileLink>,
) {
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::<crate::history_cell::AgentMarkdownCell>()
|| cell.is::<crate::history_cell::AgentMessageCell>()
|| cell.is::<crate::history_cell::UserHistoryCell>()
|| cell.is::<crate::history_cell::ProposedPlanCell>()
|| cell.is::<crate::history_cell::ProposedPlanStreamCell>()
|| cell.is::<crate::history_cell::DelegatedMessageCell>()
})
.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::<Vec<_>>();
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;
+200
View File
@@ -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",
"<realtime_delegation><input>Spoken follow-up</input></realtime_delegation>"
),
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::<Vec<_>>(),
["./typed.md", "src/lib.rs"],
);
}
+1
View File
@@ -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)
}
@@ -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::<Vec<_>>();
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(())
}
+1
View File
@@ -1143,6 +1143,7 @@ impl App {
return Ok(());
}
self.track_side_chat_title(thread_id, &notification);
self.notice_sidebar_file_item(thread_id, &notification);
if self.current_displayed_thread_id() == Some(thread_id)
&& let ServerNotification::TurnCompleted(notification) = &notification
{
+6
View File
@@ -543,6 +543,12 @@ pub(crate) enum AppEvent {
request: uuid::Uuid,
result: Result<crate::sidebar_image::DocumentContent, String>,
},
SidebarFilesLoaded {
backend_id: crate::backend_id::BackendId,
parent: ThreadId,
request: uuid::Uuid,
page: Option<codex_app_server_protocol::ThreadItemsListResponse>,
},
SavedSideChatsLoaded {
backend_id: crate::backend_id::BackendId,
parent: ThreadId,
@@ -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<String> {
+20 -1
View File
@@ -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<FileLink> {
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
+9
View File
@@ -235,6 +235,15 @@ impl TerminalHyperlink {
}
}
pub(crate) fn sidebar_destination(&self) -> Option<ReaderLink> {
match self.destination_kind {
DestinationKind::TrustedFile => None,
DestinationKind::Document(_) | DestinationKind::Web => {
self.reader_destination(/*documents*/ true)
}
}
}
pub(crate) fn terminal_destination(&self) -> Option<String> {
match self.destination_kind {
DestinationKind::Document(_) => None,