mirror of
https://github.com/openai/codex.git
synced 2026-11-27 23:31:09 +00:00
Defer resume picker and directory changes to a fresh TUI stack (#43376)
## Why Session transitions need stack headroom for embedded-server initialization and configuration loading. Running them inside event dispatch risks exhausting the TUI thread's stack. ## What changed - Queue `/resume` and valid `/cd` requests for processing after event dispatch returns. - Reject additional `/cd` requests while one is pending, and recheck the source session, working directory, idle state, and remote-environment restrictions before applying it. ## Testing Add a Unix PTY regression test that exercises `/resume`, worktree conversation forks, `/cd`, and new worktree conversations through the real TUI event loop. Update the directory-change test to verify that dispatch queues the request without immediately changing configuration. GitOrigin-RevId: 4950921931dfaba9e55e6bb04f98f7ba64b72122
This commit is contained in:
1 parent
694b6319d3
commit
5ecb3afd1b
11 files changed
+283
-5
No files matched your search
@@ -126,7 +126,8 @@ impl App {
|
||||
thread_id,
|
||||
requested_cwd,
|
||||
} => {
|
||||
if self.primary_thread_id != Some(thread_id)
|
||||
if self.pending_working_directory_change.is_some()
|
||||
|| self.primary_thread_id != Some(thread_id)
|
||||
|| !self.chat_widget.can_change_working_directory(thread_id)
|
||||
{
|
||||
self.chat_widget.add_error_message(
|
||||
@@ -148,7 +149,12 @@ impl App {
|
||||
);
|
||||
match std::fs::metadata(cwd.as_path()) {
|
||||
Ok(metadata) if metadata.is_dir() => {
|
||||
self.change_working_directory(tui, app_server, cwd).await;
|
||||
self.pending_working_directory_change =
|
||||
Some(working_directory::PendingWorkingDirectoryChange {
|
||||
source_thread_id: thread_id,
|
||||
source_cwd: self.config.cwd.clone(),
|
||||
destination: cwd,
|
||||
});
|
||||
}
|
||||
Ok(_) => self
|
||||
.chat_widget
|
||||
@@ -278,7 +284,7 @@ impl App {
|
||||
.await;
|
||||
}
|
||||
AppEvent::OpenResumePicker => {
|
||||
return Box::pin(self.open_resume_picker(tui, app_server)).await;
|
||||
self.pending_open_resume_picker = true;
|
||||
}
|
||||
AppEvent::OpenExternalAgentConfigMigration => {
|
||||
match crate::external_agent_config_migration::flow::handle_external_agent_config_migration_prompt(
|
||||
|
||||
@@ -9,8 +9,8 @@ impl App {
|
||||
tui: &mut tui::Tui,
|
||||
app_server: &mut AppServerSession,
|
||||
) -> Result<AppRunControl> {
|
||||
// This runs inside the active TUI event future. Starting a second embedded server
|
||||
// inline can exhaust that thread's stack while initializing its auth manager.
|
||||
// Keep embedded-server initialization on its own runtime task as well as opening the
|
||||
// picker on a fresh event-loop iteration; its auth manager needs more stack headroom.
|
||||
let picker_config = self.config.clone();
|
||||
let picker_target = self.app_server_target.clone();
|
||||
let picker_state_db = self.state_db.clone();
|
||||
|
||||
@@ -724,6 +724,8 @@ See the Codex keymap documentation for supported actions and examples."
|
||||
dynamic_tool_status_updates,
|
||||
dynamic_tool_tasks: HashMap::new(),
|
||||
pending_startup_thread_start,
|
||||
pending_open_resume_picker: false,
|
||||
pending_working_directory_change: None,
|
||||
pending_start_managed_worktree: None,
|
||||
pending_managed_worktree_creation: false,
|
||||
pending_managed_worktree_created: None,
|
||||
@@ -921,6 +923,21 @@ See the Codex keymap documentation for supported actions and examples."
|
||||
Ok(exit_reason)
|
||||
} else {
|
||||
loop {
|
||||
if app.pending_open_resume_picker {
|
||||
app.pending_open_resume_picker = false;
|
||||
match Box::pin(app.open_resume_picker(tui, &mut app_server)).await {
|
||||
Ok(AppRunControl::Continue) => {}
|
||||
Ok(AppRunControl::Exit(reason)) => break Ok(reason),
|
||||
Err(err) if app.recover_transport_error(&err) => {}
|
||||
Err(err) => break Err(err),
|
||||
}
|
||||
continue;
|
||||
}
|
||||
if let Some(pending) = app.pending_working_directory_change.take() {
|
||||
Box::pin(app.finish_working_directory_change(tui, &mut app_server, pending))
|
||||
.await;
|
||||
continue;
|
||||
}
|
||||
if let Some((mode, name)) = app.pending_start_managed_worktree.take() {
|
||||
Box::pin(app.start_managed_worktree(&mut app_server, mode, name)).await;
|
||||
continue;
|
||||
|
||||
@@ -81,6 +81,8 @@ pub(super) async fn make_test_app() -> App {
|
||||
dynamic_tool_status_updates: tokio::sync::broadcast::channel(/*capacity*/ 64).0,
|
||||
dynamic_tool_tasks: HashMap::new(),
|
||||
pending_startup_thread_start: false,
|
||||
pending_open_resume_picker: false,
|
||||
pending_working_directory_change: None,
|
||||
pending_start_managed_worktree: None,
|
||||
pending_managed_worktree_creation: false,
|
||||
pending_managed_worktree_created: None,
|
||||
|
||||
@@ -5854,7 +5854,9 @@ async fn make_test_app() -> App {
|
||||
dynamic_tool_status_updates: tokio::sync::broadcast::channel(/*capacity*/ 64).0,
|
||||
dynamic_tool_tasks: HashMap::new(),
|
||||
pending_startup_thread_start: false,
|
||||
pending_open_resume_picker: false,
|
||||
pending_managed_worktree_creation: false,
|
||||
pending_working_directory_change: None,
|
||||
pending_start_managed_worktree: None,
|
||||
pending_managed_worktree_created: None,
|
||||
pending_managed_worktree_transition: None,
|
||||
@@ -5946,7 +5948,9 @@ pub(super) async fn make_test_app_with_channels() -> (
|
||||
dynamic_tool_status_updates: tokio::sync::broadcast::channel(/*capacity*/ 64).0,
|
||||
dynamic_tool_tasks: HashMap::new(),
|
||||
pending_startup_thread_start: false,
|
||||
pending_open_resume_picker: false,
|
||||
pending_managed_worktree_creation: false,
|
||||
pending_working_directory_change: None,
|
||||
pending_start_managed_worktree: None,
|
||||
pending_managed_worktree_created: None,
|
||||
pending_managed_worktree_transition: None,
|
||||
|
||||
@@ -3446,6 +3446,9 @@ async fn changing_directory_preserves_project_trust_permissions_history_and_hook
|
||||
let thread_id = [original, ThreadId::new()][usize::from(kind == "stale")];
|
||||
app.handle_event(&mut tui, &mut server, change(thread_id, path))
|
||||
.await?;
|
||||
if let Some(pending) = app.pending_working_directory_change.take() {
|
||||
Box::pin(app.finish_working_directory_change(&mut tui, &mut server, pending)).await;
|
||||
}
|
||||
assert_eq!(app.chat_widget.thread_id(), Some(original));
|
||||
assert_eq!(app.config.cwd, current.clone().abs());
|
||||
assert!(app.runtime_working_directory_override.is_none());
|
||||
@@ -3526,6 +3529,13 @@ async fn changing_directory_preserves_project_trust_permissions_history_and_hook
|
||||
requests.lock().expect("request recorder lock").clear();
|
||||
app.handle_event(&mut tui, &mut server, change(original, "../trusted"))
|
||||
.await?;
|
||||
// Dispatch only queues /cd: configuration loading happens on a fresh loop iteration.
|
||||
assert_eq!(app.config.cwd, current.clone().abs());
|
||||
let pending = app
|
||||
.pending_working_directory_change
|
||||
.take()
|
||||
.expect("valid /cd queued");
|
||||
Box::pin(app.finish_working_directory_change(&mut tui, &mut server, pending)).await;
|
||||
let forked = app.chat_widget.thread_id().expect("forked thread");
|
||||
assert_ne!(forked, original);
|
||||
let forked_rollout = app.chat_widget.rollout_path().expect("forked rollout");
|
||||
|
||||
@@ -25,7 +25,42 @@ pub(super) struct ManagedWorktreeAttach {
|
||||
name_error: Option<String>,
|
||||
}
|
||||
|
||||
/// A /cd request awaiting a fresh event-loop iteration.
|
||||
pub(super) struct PendingWorkingDirectoryChange {
|
||||
pub(super) source_thread_id: ThreadId,
|
||||
pub(super) source_cwd: AbsolutePathBuf,
|
||||
pub(super) destination: AbsolutePathBuf,
|
||||
}
|
||||
|
||||
impl App {
|
||||
pub(super) async fn finish_working_directory_change(
|
||||
&mut self,
|
||||
tui: &mut tui::Tui,
|
||||
app_server: &mut AppServerSession,
|
||||
pending: PendingWorkingDirectoryChange,
|
||||
) {
|
||||
if self.primary_thread_id != Some(pending.source_thread_id)
|
||||
|| self.config.cwd != pending.source_cwd
|
||||
|| !self
|
||||
.chat_widget
|
||||
.can_change_working_directory(pending.source_thread_id)
|
||||
{
|
||||
return self.working_directory_error(
|
||||
"Changing directories requires an idle primary session without queued input.",
|
||||
);
|
||||
}
|
||||
if crate::uses_remote_workspace_or_environment(
|
||||
&self.app_server_target,
|
||||
self.environment_manager.as_ref(),
|
||||
) {
|
||||
return self.working_directory_error(
|
||||
"Changing directories is not supported for remote workspaces or remote execution environments.",
|
||||
);
|
||||
}
|
||||
self.change_working_directory(tui, app_server, pending.destination)
|
||||
.await;
|
||||
}
|
||||
|
||||
pub(super) fn working_directory_error(&mut self, message: impl Into<String>) {
|
||||
self.chat_widget.add_error_message(message.into());
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user