From 45964ea63a6a4e285ac2b080769463908f80dec1 Mon Sep 17 00:00:00 2001 From: Felipe Coury Date: Sat, 30 May 2026 21:26:57 -0300 Subject: [PATCH] fix(tui): sync workspace command state --- codex-rs/tui/src/app/event_dispatch.rs | 15 +++++++ codex-rs/tui/src/app/tests.rs | 40 +++++++++++++++++++ codex-rs/tui/src/app/thread_settings.rs | 30 ++++++++++++++ codex-rs/tui/src/chatwidget/settings.rs | 21 ++++++++++ codex-rs/tui/src/chatwidget/slash_dispatch.rs | 4 +- .../src/chatwidget/tests/slash_commands.rs | 27 +++++++++++++ codex-rs/tui/src/status/tests.rs | 2 +- 7 files changed, 136 insertions(+), 3 deletions(-) diff --git a/codex-rs/tui/src/app/event_dispatch.rs b/codex-rs/tui/src/app/event_dispatch.rs index 3bb6d48126..898119549c 100644 --- a/codex-rs/tui/src/app/event_dispatch.rs +++ b/codex-rs/tui/src/app/event_dispatch.rs @@ -414,6 +414,21 @@ impl App { .await { Ok(response) => { + self.config.cwd = response.cwd.clone(); + self.config.workspace_roots = response.runtime_workspace_roots.clone(); + self.config + .permissions + .set_workspace_roots(self.config.workspace_roots.clone()); + self.chat_widget.apply_runtime_workspace( + response.cwd.clone(), + response.runtime_workspace_roots.clone(), + ); + self.apply_workspace_to_cached_session( + thread_id, + &response.cwd, + &response.runtime_workspace_roots, + ) + .await; let message = match (operation, response.changed) { ( WorkspaceMutationOperation::SetWorkingDirectory, diff --git a/codex-rs/tui/src/app/tests.rs b/codex-rs/tui/src/app/tests.rs index 74e08bbe17..b078694bd6 100644 --- a/codex-rs/tui/src/app/tests.rs +++ b/codex-rs/tui/src/app/tests.rs @@ -5937,6 +5937,46 @@ async fn inactive_thread_settings_notification_updates_cached_collaboration_mode ); } +#[tokio::test] +async fn workspace_update_updates_cached_session_roots() { + let mut app = make_test_app().await; + let thread_id = ThreadId::new(); + let session = test_thread_session(thread_id, test_path_buf("/tmp/main")); + let cwd = test_absolute_path("/tmp/stack-layer"); + let roots = vec![cwd.clone(), test_absolute_path("/tmp/shared")]; + + app.primary_thread_id = Some(thread_id); + app.primary_session_configured = Some(session.clone()); + app.thread_event_channels.insert( + thread_id, + ThreadEventChannel::new_with_session(THREAD_EVENT_CHANNEL_CAPACITY, session, Vec::new()), + ); + + app.apply_workspace_to_cached_session(thread_id, &cwd, &roots) + .await; + + let cached_session = app + .thread_event_channels + .get(&thread_id) + .expect("thread channel") + .store + .lock() + .await + .session + .clone() + .expect("cached session"); + assert_eq!(cached_session.cwd, cwd); + assert_eq!(cached_session.runtime_workspace_roots, roots); + assert_eq!( + app.primary_session_configured + .as_ref() + .expect("primary session") + .runtime_workspace_roots + .as_slice(), + cached_session.runtime_workspace_roots.as_slice() + ); +} + #[tokio::test] async fn clear_only_ui_reset_preserves_chat_session_state() { let mut app = make_test_app().await; diff --git a/codex-rs/tui/src/app/thread_settings.rs b/codex-rs/tui/src/app/thread_settings.rs index 4e0f31d5bb..089752828f 100644 --- a/codex-rs/tui/src/app/thread_settings.rs +++ b/codex-rs/tui/src/app/thread_settings.rs @@ -10,6 +10,7 @@ use codex_app_server_protocol::ThreadSettingsUpdateParams; use codex_protocol::ThreadId; use codex_protocol::config_types::ModeKind; use codex_protocol::models::PermissionProfile; +use codex_utils_absolute_path::AbsolutePathBuf; impl App { pub(super) async fn sync_active_thread_model_setting( @@ -153,6 +154,26 @@ impl App { } } + pub(super) async fn apply_workspace_to_cached_session( + &mut self, + thread_id: ThreadId, + cwd: &AbsolutePathBuf, + runtime_workspace_roots: &[AbsolutePathBuf], + ) { + if self.primary_thread_id == Some(thread_id) + && let Some(session) = self.primary_session_configured.as_mut() + { + apply_workspace_to_session(session, cwd, runtime_workspace_roots); + } + + if let Some(channel) = self.thread_event_channels.get(&thread_id) { + let mut store = channel.store.lock().await; + if let Some(session) = store.session.as_mut() { + apply_workspace_to_session(session, cwd, runtime_workspace_roots); + } + } + } + async fn send_thread_settings_update( &mut self, app_server: &mut AppServerSession, @@ -197,6 +218,15 @@ fn apply_thread_settings_to_session(session: &mut ThreadSessionState, settings: session.collaboration_mode = Some(Box::new(collaboration_mode)); } +fn apply_workspace_to_session( + session: &mut ThreadSessionState, + cwd: &AbsolutePathBuf, + runtime_workspace_roots: &[AbsolutePathBuf], +) { + session.cwd.clone_from(cwd); + session.runtime_workspace_roots = runtime_workspace_roots.to_vec(); +} + fn thread_settings_update_has_changes(params: &ThreadSettingsUpdateParams) -> bool { params.cwd.is_some() || params.approval_policy.is_some() diff --git a/codex-rs/tui/src/chatwidget/settings.rs b/codex-rs/tui/src/chatwidget/settings.rs index bed42e2bb4..66d6ce3c6e 100644 --- a/codex-rs/tui/src/chatwidget/settings.rs +++ b/codex-rs/tui/src/chatwidget/settings.rs @@ -573,6 +573,27 @@ impl ChatWidget { .set_workspace_roots(self.config.workspace_roots.clone()); } + pub(crate) fn apply_runtime_workspace( + &mut self, + cwd: AbsolutePathBuf, + workspace_roots: Vec, + ) { + let cwd_changed = self.config.cwd != cwd; + self.config.cwd = cwd.clone(); + self.current_cwd = Some(cwd.to_path_buf()); + self.config.workspace_roots = workspace_roots; + self.config + .permissions + .set_workspace_roots(self.config.workspace_roots.clone()); + self.status_line_project_root_name_cache = None; + self.refresh_status_surfaces(); + if cwd_changed { + self.refresh_skills_for_current_cwd(/*force_reload*/ true); + } + self.refresh_plugin_mentions(); + self.request_redraw(); + } + pub(super) fn set_effective_collaboration_mode(&mut self, mode: CollaborationMode) { let mode_kind = mode.mode; let settings = mode.settings; diff --git a/codex-rs/tui/src/chatwidget/slash_dispatch.rs b/codex-rs/tui/src/chatwidget/slash_dispatch.rs index cf70cf15ec..990ff689b8 100644 --- a/codex-rs/tui/src/chatwidget/slash_dispatch.rs +++ b/codex-rs/tui/src/chatwidget/slash_dispatch.rs @@ -1002,8 +1002,6 @@ impl ChatWidget { match cmd { SlashCommand::Ide | SlashCommand::Status - | SlashCommand::Cwd - | SlashCommand::AddDir | SlashCommand::DebugConfig | SlashCommand::Ps | SlashCommand::Stop @@ -1021,6 +1019,8 @@ impl ChatWidget { | SlashCommand::Rename | SlashCommand::TestApproval => QueueDrain::Continue, SlashCommand::Feedback + | SlashCommand::Cwd + | SlashCommand::AddDir | SlashCommand::New | SlashCommand::Archive | SlashCommand::Delete diff --git a/codex-rs/tui/src/chatwidget/tests/slash_commands.rs b/codex-rs/tui/src/chatwidget/tests/slash_commands.rs index 7ec19bd231..0d66413835 100644 --- a/codex-rs/tui/src/chatwidget/tests/slash_commands.rs +++ b/codex-rs/tui/src/chatwidget/tests/slash_commands.rs @@ -213,6 +213,33 @@ async fn queued_slash_compact_dispatches_after_active_turn() { ); } +#[tokio::test] +async fn queued_slash_cwd_stops_before_next_queued_message() { + let (mut chat, mut rx, mut op_rx) = make_chatwidget_manual(/*model_override*/ None).await; + chat.thread_id = Some(ThreadId::new()); + handle_turn_started(&mut chat, "turn-1"); + + queue_composer_text_with_tab(&mut chat, "/cwd ../stack-layer"); + queue_composer_text_with_tab(&mut chat, "hello after cwd"); + + complete_turn_with_message(&mut chat, "turn-1", Some("done")); + + let events = std::iter::from_fn(|| rx.try_recv().ok()).collect::>(); + assert!( + events.iter().any(|event| matches!( + event, + AppEvent::UpdateThreadWorkspace { + operation: + codex_app_server_protocol::WorkspaceMutationOperation::SetWorkingDirectory, + path, + } if path == "../stack-layer" + )), + "expected queued /cwd to request a workspace update; events: {events:?}" + ); + assert_eq!(chat.input_queue.queued_user_messages.len(), 1); + assert_matches!(op_rx.try_recv(), Err(TryRecvError::Empty)); +} + #[tokio::test] async fn queued_slash_review_with_args_dispatches_after_active_turn() { let (mut chat, _rx, mut op_rx) = make_chatwidget_manual(/*model_override*/ None).await; diff --git a/codex-rs/tui/src/status/tests.rs b/codex-rs/tui/src/status/tests.rs index 2f6f60ee79..d6b63fd0a1 100644 --- a/codex-rs/tui/src/status/tests.rs +++ b/codex-rs/tui/src/status/tests.rs @@ -515,7 +515,7 @@ async fn status_permissions_workspace_roots_do_not_repeat_additional_directories .set(AskForApproval::OnRequest.to_core()) .expect("set approval policy"); let extra_root = test_path_buf("/workspace/extra").abs(); - config.workspace_roots = vec![config.cwd.clone(), extra_root]; + config.workspace_roots = vec![config.cwd.clone(), extra_root.clone()]; config .permissions .set_workspace_roots(config.workspace_roots.clone());