Honor the session directory when resuming after /cd (#65030)

## Why

Changing directories with `/cd` could force in-app resume to use the current
directory even when the saved preference was to use the session's directory.

## What changed

Use the `/cd` directory for the remembered Current choice. Explicit CLI and
remote directory overrides still select Current. If reloading configuration
fails, reuse the existing configuration only when it already belongs to the
resume directory.

## Testing

Update the in-app resume and agents overview cold-resume tests to cover using
the session directory after `/cd`.

GitOrigin-RevId: d87bc2ba96f7ca10d6fd0d5ec0414712d44238e6
This commit is contained in:
Felipe Coury
2026-12-01 16:41:22 +00:00
committed by copyberry
parent 791cf84282
commit dcafb4c96f
6 changed files with 28 additions and 44 deletions
-2
View File
@@ -921,10 +921,8 @@ impl App {
} else {
target_thread.cwd.to_path_buf()
};
let current_cwd = self.config.cwd.to_path_buf();
match self
.rebuild_config_for_resume_or_fallback(
&current_cwd,
config_cwd,
(&self.config, &self.local_settings),
&self.app_server_target,
@@ -2832,6 +2832,7 @@ async fn overview_cold_resume_honors_working_directory_selection() -> Result<()>
("current", false, false),
("session", true, false),
("session", true, true),
("session", false, true),
("session", false, false),
] {
// Keep the large setup and cold-resume futures off the Windows test stack.
@@ -2873,7 +2874,7 @@ async fn overview_cold_resume_honors_working_directory_selection() -> Result<()>
}
});
app.runtime_working_directory_override = runtime_cwd.then(|| chosen.to_path_buf());
let expected_cwd = if mode == "current" || cli_cwd || runtime_cwd {
let expected_cwd = if mode == "current" || cli_cwd {
chosen
} else {
test_path_buf("/").abs()
+2 -7
View File
@@ -565,7 +565,6 @@ impl App {
pub(super) async fn rebuild_config_for_resume_or_fallback(
&self,
current_cwd: &Path,
resume_cwd: PathBuf,
fallback: (&Config, &crate::local_settings::LocalSettings),
target: &AppServerTarget,
@@ -579,7 +578,7 @@ impl App {
Ok((config, local_settings))
}
Err(err) => {
if crate::session_resume::cwds_differ(current_cwd, &resume_cwd) {
if crate::session_resume::cwds_differ(fallback.0.cwd.as_path(), &resume_cwd) {
Err(err)
} else {
let resume_cwd_display = resume_cwd.display().to_string();
@@ -2187,12 +2186,10 @@ theme = "dracula"
app.config.codex_home = codex_home.path().to_path_buf().abs();
std::fs::write(codex_home.path().join("config.toml"), "[broken")?;
let current_config = app.config.clone();
let current_cwd = current_config.cwd.clone();
let resume_config = app
.rebuild_config_for_resume_or_fallback(
&current_cwd,
current_cwd.to_path_buf(),
current_config.cwd.to_path_buf(),
(&app.config, &app.local_settings),
&app.app_server_target,
)
@@ -2208,13 +2205,11 @@ theme = "dracula"
let codex_home = tempdir()?;
app.config.codex_home = codex_home.path().to_path_buf().abs();
std::fs::write(codex_home.path().join("config.toml"), "[broken")?;
let current_cwd = app.config.cwd.clone();
let next_cwd_tmp = tempdir()?;
let next_cwd = next_cwd_tmp.path().to_path_buf();
let result = app
.rebuild_config_for_resume_or_fallback(
&current_cwd,
next_cwd,
(&app.config, &app.local_settings),
&app.app_server_target,
+23 -32
View File
@@ -80,29 +80,20 @@ impl App {
(settings.target.clone(), config, local_settings)
}
};
let cwd_override = self
.runtime_working_directory_override
let remote_cwd = app_server.remote_cwd_override().map(Path::to_path_buf);
let explicit_cwd = self
.harness_overrides
.cwd
.as_deref()
.or(self.harness_overrides.cwd.as_deref())
.or_else(|| app_server.remote_cwd_override())
.map(Path::to_path_buf);
let cwd_override = cwd_override.as_deref();
.or(remote_cwd.as_deref());
let resume_cwd_mode = crate::session_resume::effective_resume_cwd_mode(
local_settings.tui.resume_cwd,
cwd_override,
explicit_cwd,
);
let remembered_current_cwd = cwd_override.unwrap_or(self.launch_cwd.as_path());
let current_cwd = if matches!(resume_cwd_mode, Some(ResumeCwdMode::Current)) {
remembered_current_cwd.to_path_buf()
} else {
current_config.cwd.to_path_buf()
};
let uses_remote_workspace_or_environment =
crate::uses_remote_workspace_or_environment(&target, &self.environment_manager);
if uses_remote_workspace_or_environment
let allow_remember_current = explicit_cwd.is_some()
|| !crate::uses_remote_workspace_or_environment(&target, &self.environment_manager);
if !allow_remember_current
&& !matches!(target, AppServerTarget::Cloud)
&& self.harness_overrides.cwd.is_none()
&& app_server.remote_cwd_override().is_none()
&& matches!(resume_cwd_mode, Some(ResumeCwdMode::Current))
{
self.add_session_picker_error(
@@ -111,8 +102,9 @@ impl App {
);
return Err(AppRunControl::Continue);
}
let current_cwd = current_config.cwd.as_path();
let resume_cwd = if target.uses_remote_workspace() {
current_cwd.clone()
current_cwd.to_path_buf()
} else {
let history_cwd = if matches!(resume_cwd_mode, Some(ResumeCwdMode::Current)) {
None
@@ -127,10 +119,14 @@ impl App {
history_cwd,
CwdPromptAction::Resume,
crate::session_resume::ResumeCwdContext {
current_cwd: &current_cwd,
remembered_current_cwd,
allow_remember_current: !uses_remote_workspace_or_environment
|| cwd_override.is_some(),
current_cwd,
// `/cd` sets the folder used by the remembered Current preference.
remembered_current_cwd: self
.runtime_working_directory_override
.as_deref()
.or(explicit_cwd)
.unwrap_or(&self.launch_cwd),
allow_remember_current,
mode: resume_cwd_mode,
},
)
@@ -144,7 +140,9 @@ impl App {
}
Ok(crate::session_resume::ResolveCwdOutcome::Continue(Some(cwd)))
| Ok(crate::session_resume::ResolveCwdOutcome::ContinueAfterPrompt(cwd)) => cwd,
Ok(crate::session_resume::ResolveCwdOutcome::Continue(None)) => current_cwd.clone(),
Ok(crate::session_resume::ResolveCwdOutcome::Continue(None)) => {
current_cwd.to_path_buf()
}
Ok(crate::session_resume::ResolveCwdOutcome::Exit) => {
self.app_event_tx.send(AppEvent::Exit(ExitMode::Immediate));
return Err(AppRunControl::Continue);
@@ -152,16 +150,9 @@ impl App {
}
};
let (config_current_cwd, config_resume_cwd) = if target.uses_remote_workspace() {
let local_config_cwd = current_config.cwd.to_path_buf();
(local_config_cwd.clone(), local_config_cwd)
} else {
(current_cwd, resume_cwd)
};
let mut resume_config = match self
.rebuild_config_for_resume_or_fallback(
&config_current_cwd,
config_resume_cwd,
resume_cwd,
(&current_config, &local_settings),
&target,
)
+1 -1
View File
@@ -11363,7 +11363,7 @@ async fn in_app_resume_uses_configured_or_explicit_cwd() -> Result<()> {
("session", true, true, false, "explicit"),
("current", false, false, true, "runtime"),
("current", true, false, true, "runtime"),
("session", false, false, true, "runtime"),
("session", false, false, true, "session"),
("session", true, false, true, "runtime"),
] {
std::fs::write(
@@ -16,7 +16,6 @@ async fn terminal_size_telemetry_respects_reloaded_exporter_and_consent() -> Res
] {
std::fs::write(app.config.codex_home.join("config.toml"), config)?;
let (config, local_settings) = Box::pin(app.rebuild_config_for_resume_or_fallback(
&cwd,
cwd.clone(),
(&app.config, &app.local_settings),
&app.app_server_target,