mirror of
https://github.com/openai/codex.git
synced 2026-11-15 21:50:46 +00:00
Show file destinations in TUI change approvals (#39285)
## What changed - Show a description and the destination paths affected by each file-change approval. - Include both source and target paths for moves, format cross-platform paths for display, and show `unavailable` when change details are missing. - Recover file changes from buffered events and thread snapshots so live and replayed approvals retain their destination details. ## Testing - Add coverage for active and replayed approvals, moved and cross-platform paths, and the missing-destination fallback. GitOrigin-RevId: 24f90b313fd8643f90cbead1504c788b140d7fed
This commit is contained in:
@@ -0,0 +1,68 @@
|
||||
//! Recover file-change details for live and replayed approval requests.
|
||||
|
||||
use super::*;
|
||||
|
||||
impl App {
|
||||
pub(super) async fn thread_file_change_changes(
|
||||
&self,
|
||||
thread_id: ThreadId,
|
||||
turn_id: &str,
|
||||
item_id: &str,
|
||||
) -> Option<Vec<codex_app_server_protocol::FileUpdateChange>> {
|
||||
let channel = self.thread_event_channels.get(&thread_id)?;
|
||||
let store = channel.store.lock().await;
|
||||
store.file_change_changes(turn_id, item_id)
|
||||
}
|
||||
|
||||
pub(super) async fn handle_thread_event_now_recovering_file_changes(
|
||||
&mut self,
|
||||
event: ThreadBufferedEvent,
|
||||
) {
|
||||
let changes = match (&event, self.active_thread_id) {
|
||||
(ThreadBufferedEvent::Request(request), Some(thread_id)) => match request.as_ref() {
|
||||
ServerRequest::FileChangeRequestApproval { params, .. } => {
|
||||
self.thread_file_change_changes(thread_id, ¶ms.turn_id, ¶ms.item_id)
|
||||
.await
|
||||
}
|
||||
_ => None,
|
||||
},
|
||||
_ => None,
|
||||
};
|
||||
match (changes, event) {
|
||||
(Some(changes), ThreadBufferedEvent::Request(request)) => {
|
||||
if self
|
||||
.pending_app_server_requests
|
||||
.contains_server_request(request.as_ref())
|
||||
{
|
||||
self.handle_file_change_request(*request, changes);
|
||||
}
|
||||
}
|
||||
(_, event) => self.handle_thread_event_now(event),
|
||||
}
|
||||
}
|
||||
|
||||
pub(super) fn handle_file_change_request(
|
||||
&mut self,
|
||||
request: ServerRequest,
|
||||
changes: Vec<codex_app_server_protocol::FileUpdateChange>,
|
||||
) {
|
||||
let ServerRequest::FileChangeRequestApproval { params, .. } = request else {
|
||||
return;
|
||||
};
|
||||
self.chat_widget.on_apply_patch_approval_request(
|
||||
params.item_id.clone(),
|
||||
crate::approval_events::ApplyPatchApprovalRequestEvent {
|
||||
call_id: params.item_id,
|
||||
turn_id: params.turn_id,
|
||||
changes: crate::app_server_approval_conversions::file_update_changes_to_display(
|
||||
changes,
|
||||
),
|
||||
reason: params.reason,
|
||||
grant_root: params.grant_root,
|
||||
},
|
||||
);
|
||||
if self.startup_protected_input_boundary && !self.chat_widget.has_active_view() {
|
||||
self.startup_pending_protected_request = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -3435,6 +3435,125 @@ async fn inactive_thread_file_change_approval_recovers_buffered_changes() {
|
||||
assert!(rendered.contains("1 +hello"));
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn active_thread_file_change_approval_recovers_buffered_changes() {
|
||||
let (mut app, _app_event_rx, _op_rx) = make_test_app_with_channels().await;
|
||||
let thread_id = ThreadId::new();
|
||||
app.active_thread_id = Some(thread_id);
|
||||
app.startup_protected_input_boundary = true;
|
||||
app.enqueue_thread_notification(
|
||||
thread_id,
|
||||
ServerNotification::ItemStarted(ItemStartedNotification {
|
||||
thread_id: thread_id.to_string(),
|
||||
turn_id: "turn-active-approval".to_string(),
|
||||
started_at_ms: 0,
|
||||
item: ThreadItem::FileChange {
|
||||
id: "patch-active-approval".to_string(),
|
||||
changes: vec![FileUpdateChange {
|
||||
path: "visible-target.md".to_string(),
|
||||
kind: PatchChangeKind::Add,
|
||||
diff: "hello\n".to_string(),
|
||||
}],
|
||||
status: codex_app_server_protocol::PatchApplyStatus::InProgress,
|
||||
},
|
||||
}),
|
||||
)
|
||||
.await
|
||||
.expect("enqueue file change item");
|
||||
|
||||
let request = ServerRequest::FileChangeRequestApproval {
|
||||
request_id: AppServerRequestId::Integer(10),
|
||||
params: FileChangeRequestApprovalParams {
|
||||
thread_id: thread_id.to_string(),
|
||||
turn_id: "turn-active-approval".to_string(),
|
||||
item_id: "patch-active-approval".to_string(),
|
||||
started_at_ms: 0,
|
||||
reason: None,
|
||||
grant_root: None,
|
||||
},
|
||||
};
|
||||
assert_eq!(
|
||||
app.pending_app_server_requests
|
||||
.note_server_request(&request),
|
||||
None
|
||||
);
|
||||
app.chat_widget.handle_server_notification(
|
||||
agent_message_delta_notification(thread_id, "turn-active-approval", "agent-1", "streaming"),
|
||||
/*replay_kind*/ None,
|
||||
);
|
||||
let event = ThreadBufferedEvent::Request(Box::new(request));
|
||||
app.handle_thread_event_now_recovering_file_changes(event)
|
||||
.await;
|
||||
|
||||
assert!(!app.chat_widget.has_active_view());
|
||||
assert!(app.startup_pending_protected_request);
|
||||
app.chat_widget.handle_server_notification(
|
||||
ServerNotification::ItemCompleted(codex_app_server_protocol::ItemCompletedNotification {
|
||||
thread_id: thread_id.to_string(),
|
||||
turn_id: "turn-active-approval".to_string(),
|
||||
completed_at_ms: 0,
|
||||
item: ThreadItem::AgentMessage {
|
||||
id: "agent-1".to_string(),
|
||||
text: "streaming".to_string(),
|
||||
phase: None,
|
||||
memory_citation: None,
|
||||
},
|
||||
}),
|
||||
/*replay_kind*/ None,
|
||||
);
|
||||
|
||||
let rendered = render_bottom_popup(&app.chat_widget, /*width*/ 100);
|
||||
let destination = app.chat_widget.config_ref().cwd.join("visible-target.md");
|
||||
assert!(rendered.contains("Description: Apply proposed file edits"));
|
||||
assert!(rendered.contains(&format!("Destination: {}", destination.display())));
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn replayed_file_change_approval_recovers_snapshot_changes() {
|
||||
let (mut app, _app_event_rx, _op_rx) = make_test_app_with_channels().await;
|
||||
let thread_id = ThreadId::new();
|
||||
let cwd = test_path_buf("/tmp/project").abs();
|
||||
app.replay_thread_snapshot(
|
||||
ThreadEventSnapshot {
|
||||
session: Some(test_thread_session(thread_id, cwd.clone().into_path_buf())),
|
||||
turns: vec![test_turn(
|
||||
"turn-replayed-approval",
|
||||
TurnStatus::InProgress,
|
||||
vec![ThreadItem::FileChange {
|
||||
id: "patch-replayed-approval".to_string(),
|
||||
changes: vec![FileUpdateChange {
|
||||
path: "visible-target.md".to_string(),
|
||||
kind: PatchChangeKind::Add,
|
||||
diff: "hello\n".to_string(),
|
||||
}],
|
||||
status: codex_app_server_protocol::PatchApplyStatus::InProgress,
|
||||
}],
|
||||
)],
|
||||
events: vec![ThreadBufferedEvent::Request(Box::new(
|
||||
ServerRequest::FileChangeRequestApproval {
|
||||
request_id: AppServerRequestId::Integer(11),
|
||||
params: FileChangeRequestApprovalParams {
|
||||
thread_id: thread_id.to_string(),
|
||||
turn_id: "turn-replayed-approval".to_string(),
|
||||
item_id: "patch-replayed-approval".to_string(),
|
||||
started_at_ms: 0,
|
||||
reason: None,
|
||||
grant_root: None,
|
||||
},
|
||||
},
|
||||
))],
|
||||
input_state: None,
|
||||
},
|
||||
/*resume_restored_queue*/ false,
|
||||
);
|
||||
|
||||
let rendered = render_bottom_popup(&app.chat_widget, /*width*/ 100).replace(
|
||||
&cwd.join("visible-target.md").display().to_string(),
|
||||
"/tmp/project/visible-target.md",
|
||||
);
|
||||
assert_app_snapshot!("replayed_file_change_approval", rendered);
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn inactive_thread_permissions_approval_preserves_file_system_permissions() {
|
||||
let app = make_test_app().await;
|
||||
|
||||
@@ -196,35 +196,7 @@ impl ThreadEventStore {
|
||||
turn_id: &str,
|
||||
item_id: &str,
|
||||
) -> Option<Vec<codex_app_server_protocol::FileUpdateChange>> {
|
||||
self.buffer
|
||||
.iter()
|
||||
.rev()
|
||||
.find_map(|event| match event {
|
||||
ThreadBufferedEvent::Notification(notification) => match notification.as_ref() {
|
||||
ServerNotification::ItemStarted(notification)
|
||||
if turn_id_matches(turn_id, ¬ification.turn_id) =>
|
||||
{
|
||||
file_change_item_changes(¬ification.item, item_id)
|
||||
}
|
||||
ServerNotification::ItemCompleted(notification)
|
||||
if turn_id_matches(turn_id, ¬ification.turn_id) =>
|
||||
{
|
||||
file_change_item_changes(¬ification.item, item_id)
|
||||
}
|
||||
_ => None,
|
||||
},
|
||||
ThreadBufferedEvent::Request(_)
|
||||
| ThreadBufferedEvent::HistoryEntryResponse(_)
|
||||
| ThreadBufferedEvent::FeedbackSubmission(_) => None,
|
||||
})
|
||||
.or_else(|| {
|
||||
self.turns
|
||||
.iter()
|
||||
.rev()
|
||||
.filter(|turn| turn_id_matches(turn_id, &turn.id))
|
||||
.flat_map(|turn| turn.items.iter().rev())
|
||||
.find_map(|item| file_change_item_changes(item, item_id))
|
||||
})
|
||||
file_change_changes(self.buffer.iter(), &self.turns, turn_id, item_id)
|
||||
}
|
||||
|
||||
pub(super) fn snapshot(&self) -> ThreadEventSnapshot {
|
||||
@@ -298,6 +270,38 @@ fn turn_id_matches(request_turn_id: &str, candidate_turn_id: &str) -> bool {
|
||||
request_turn_id.is_empty() || request_turn_id == candidate_turn_id
|
||||
}
|
||||
|
||||
pub(super) fn file_change_changes<'a>(
|
||||
events: impl DoubleEndedIterator<Item = &'a ThreadBufferedEvent>,
|
||||
turns: &'a [Turn],
|
||||
turn_id: &str,
|
||||
item_id: &str,
|
||||
) -> Option<Vec<codex_app_server_protocol::FileUpdateChange>> {
|
||||
let event_items = events.rev().filter_map(|event| {
|
||||
let ThreadBufferedEvent::Notification(notification) = event else {
|
||||
return None;
|
||||
};
|
||||
let (candidate_turn_id, item) = match notification.as_ref() {
|
||||
ServerNotification::ItemStarted(notification) => {
|
||||
(¬ification.turn_id, ¬ification.item)
|
||||
}
|
||||
ServerNotification::ItemCompleted(notification) => {
|
||||
(¬ification.turn_id, ¬ification.item)
|
||||
}
|
||||
_ => return None,
|
||||
};
|
||||
turn_id_matches(turn_id, candidate_turn_id).then_some(item)
|
||||
});
|
||||
event_items
|
||||
.chain(
|
||||
turns
|
||||
.iter()
|
||||
.rev()
|
||||
.filter(|turn| turn_id_matches(turn_id, &turn.id))
|
||||
.flat_map(|turn| turn.items.iter().rev()),
|
||||
)
|
||||
.find_map(|item| file_change_item_changes(item, item_id))
|
||||
}
|
||||
|
||||
fn file_change_item_changes(
|
||||
item: &ThreadItem,
|
||||
item_id: &str,
|
||||
|
||||
@@ -202,17 +202,6 @@ impl App {
|
||||
store.session.as_ref().map(|session| session.cwd.clone())
|
||||
}
|
||||
|
||||
async fn thread_file_change_changes(
|
||||
&self,
|
||||
thread_id: ThreadId,
|
||||
turn_id: &str,
|
||||
item_id: &str,
|
||||
) -> Option<Vec<codex_app_server_protocol::FileUpdateChange>> {
|
||||
let channel = self.thread_event_channels.get(&thread_id)?;
|
||||
let store = channel.store.lock().await;
|
||||
store.file_change_changes(turn_id, item_id)
|
||||
}
|
||||
|
||||
pub(super) async fn interactive_request_for_thread_request(
|
||||
&self,
|
||||
thread_id: ThreadId,
|
||||
@@ -1451,7 +1440,10 @@ impl App {
|
||||
let mut disconnected = false;
|
||||
loop {
|
||||
match rx.try_recv() {
|
||||
Ok(event) => self.handle_thread_event_now(event),
|
||||
Ok(event) => {
|
||||
self.handle_thread_event_now_recovering_file_changes(event)
|
||||
.await
|
||||
}
|
||||
Err(TryRecvError::Empty) => break,
|
||||
Err(TryRecvError::Disconnected) => {
|
||||
disconnected = true;
|
||||
@@ -1503,6 +1495,25 @@ impl App {
|
||||
snapshot: ThreadEventSnapshot,
|
||||
resume_restored_queue: bool,
|
||||
) {
|
||||
let request_changes = snapshot
|
||||
.events
|
||||
.iter()
|
||||
.map(|event| {
|
||||
let ThreadBufferedEvent::Request(request) = event else {
|
||||
return None;
|
||||
};
|
||||
let ServerRequest::FileChangeRequestApproval { params, .. } = request.as_ref()
|
||||
else {
|
||||
return None;
|
||||
};
|
||||
file_change_changes(
|
||||
snapshot.events.iter(),
|
||||
&snapshot.turns,
|
||||
¶ms.turn_id,
|
||||
¶ms.item_id,
|
||||
)
|
||||
})
|
||||
.collect::<Vec<_>>();
|
||||
self.refresh_mcp_startup_expected_servers_from_config();
|
||||
let should_buffer_replay = !snapshot.turns.is_empty() || !snapshot.events.is_empty();
|
||||
if should_buffer_replay {
|
||||
@@ -1536,11 +1547,16 @@ impl App {
|
||||
self.chat_widget
|
||||
.replay_thread_turns(snapshot.turns, ReplayKind::ThreadSnapshot);
|
||||
}
|
||||
for event in snapshot.events {
|
||||
for (event, changes) in snapshot.events.into_iter().zip(request_changes) {
|
||||
if suppress_replay_notices && replay_filter::event_is_notice(&event) {
|
||||
continue;
|
||||
}
|
||||
self.handle_thread_event_replay(event);
|
||||
match (event, changes) {
|
||||
(ThreadBufferedEvent::Request(request), Some(changes)) => {
|
||||
self.handle_file_change_request(*request, changes)
|
||||
}
|
||||
(event, _) => self.handle_thread_event_replay(event),
|
||||
}
|
||||
}
|
||||
if should_buffer_replay {
|
||||
self.app_event_tx
|
||||
@@ -1753,7 +1769,8 @@ impl App {
|
||||
self.pending_shutdown_exit_thread_id = None;
|
||||
}
|
||||
let had_active_view = self.chat_widget.has_active_view();
|
||||
self.handle_thread_event_now(event);
|
||||
self.handle_thread_event_now_recovering_file_changes(event)
|
||||
.await;
|
||||
if !had_active_view
|
||||
&& self.chat_widget.has_active_view()
|
||||
&& self.startup_protected_input_boundary
|
||||
|
||||
Reference in New Issue
Block a user