fix(tui): sync workspace command state

This commit is contained in:
Felipe Coury
2026-06-12 00:34:00 -07:00
parent a5fab8a886
commit 45964ea63a
7 changed files with 136 additions and 3 deletions
+15
View File
@@ -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,
+40
View File
@@ -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;
+30
View File
@@ -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()
+21
View File
@@ -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<AbsolutePathBuf>,
) {
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;
@@ -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
@@ -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::<Vec<_>>();
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;
+1 -1
View File
@@ -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());