mirror of
https://github.com/openai/codex.git
synced 2026-11-22 15:15:18 +00:00
Let users remember the working directory for resumed sessions (#33950)
## What changed - Add `tui.resume_cwd` with `current` and `session` modes for resume and fork flows. - Let users persist either mode directly from the working-directory prompt, while preserving one-time choices when no preference is configured. - Apply the preference consistently during startup and in-app resume, honor explicit `--cd` overrides, and reject unsupported or unavailable directory choices with a clear error. ## Testing - Cover configuration parsing and persistence, prompt behavior, startup and in-app flows, session metadata fallback, and remote workspace constraints. GitOrigin-RevId: 7bf144c4b8c55bdb868cc8a71caf397c7740451a
This commit is contained in:
1 parent
82b294c73c
commit
2895d82b5e
21 files changed
+1469
-79
No files matched your search
@@ -11,8 +11,10 @@ use std::path::PathBuf;
|
||||
use crate::cwd_prompt;
|
||||
use crate::cwd_prompt::CwdPromptAction;
|
||||
use crate::cwd_prompt::CwdPromptOutcome;
|
||||
use crate::cwd_prompt::CwdSelection;
|
||||
use crate::legacy_core::config::Config;
|
||||
use crate::resume_picker::SessionTarget;
|
||||
use crate::tui::Tui;
|
||||
use codex_config::types::ResumeCwdMode;
|
||||
use codex_protocol::ThreadId;
|
||||
use codex_rollout::open_rollout_line_reader;
|
||||
use codex_state::StateRuntime;
|
||||
@@ -46,11 +48,30 @@ struct RawRecord {
|
||||
payload: Option<Value>,
|
||||
}
|
||||
|
||||
#[derive(Debug, PartialEq, Eq)]
|
||||
pub(crate) enum ResolveCwdOutcome {
|
||||
Continue(Option<PathBuf>),
|
||||
Exit,
|
||||
}
|
||||
|
||||
pub(crate) struct ResumeCwdContext<'path> {
|
||||
pub(crate) current_cwd: &'path Path,
|
||||
pub(crate) remembered_current_cwd: &'path Path,
|
||||
pub(crate) allow_remember_current: bool,
|
||||
pub(crate) mode: Option<ResumeCwdMode>,
|
||||
}
|
||||
|
||||
pub(crate) fn effective_resume_cwd_mode(
|
||||
configured_mode: Option<ResumeCwdMode>,
|
||||
cwd_override: Option<&Path>,
|
||||
) -> Option<ResumeCwdMode> {
|
||||
if cwd_override.is_some() {
|
||||
Some(ResumeCwdMode::Current)
|
||||
} else {
|
||||
configured_mode
|
||||
}
|
||||
}
|
||||
|
||||
pub(crate) async fn resolve_session_thread_id(
|
||||
path: &Path,
|
||||
id_str_if_uuid: Option<&str>,
|
||||
@@ -85,26 +106,58 @@ pub(crate) async fn read_session_model(
|
||||
|
||||
pub(crate) async fn resolve_cwd_for_resume_or_fork(
|
||||
tui: &mut Tui,
|
||||
config: &Config,
|
||||
state_db_ctx: Option<&StateRuntime>,
|
||||
current_cwd: &Path,
|
||||
thread_id: ThreadId,
|
||||
path: Option<&Path>,
|
||||
target_session: &SessionTarget,
|
||||
action: CwdPromptAction,
|
||||
allow_prompt: bool,
|
||||
cwd_context: ResumeCwdContext<'_>,
|
||||
) -> color_eyre::Result<ResolveCwdOutcome> {
|
||||
let Some(history_cwd) = read_session_cwd(state_db_ctx, thread_id, path).await else {
|
||||
if matches!(cwd_context.mode, Some(ResumeCwdMode::Current)) {
|
||||
return Ok(ResolveCwdOutcome::Continue(Some(
|
||||
cwd_context.remembered_current_cwd.to_path_buf(),
|
||||
)));
|
||||
}
|
||||
let Some(history_cwd) = read_session_cwd(
|
||||
state_db_ctx,
|
||||
target_session.thread_id,
|
||||
target_session.path.as_deref(),
|
||||
)
|
||||
.await
|
||||
else {
|
||||
if matches!(cwd_context.mode, Some(ResumeCwdMode::Session)) {
|
||||
color_eyre::eyre::bail!(
|
||||
"failed to determine the working directory recorded for the selected session"
|
||||
);
|
||||
}
|
||||
return Ok(ResolveCwdOutcome::Continue(None));
|
||||
};
|
||||
if allow_prompt && cwds_differ(current_cwd, &history_cwd) {
|
||||
let selection_outcome =
|
||||
cwd_prompt::run_cwd_selection_prompt(tui, action, current_cwd, &history_cwd).await?;
|
||||
match cwd_context.mode {
|
||||
Some(ResumeCwdMode::Session) => {
|
||||
return Ok(ResolveCwdOutcome::Continue(Some(history_cwd)));
|
||||
}
|
||||
Some(ResumeCwdMode::Current) | None => {}
|
||||
}
|
||||
if cwds_differ(cwd_context.current_cwd, &history_cwd) {
|
||||
let selection_outcome = cwd_prompt::run_cwd_selection_prompt(
|
||||
tui,
|
||||
config,
|
||||
action,
|
||||
cwd_context.current_cwd,
|
||||
&history_cwd,
|
||||
cwd_context.remembered_current_cwd,
|
||||
cwd_context.allow_remember_current,
|
||||
)
|
||||
.await?;
|
||||
return Ok(match selection_outcome {
|
||||
CwdPromptOutcome::Selection(CwdSelection::Current) => {
|
||||
ResolveCwdOutcome::Continue(Some(current_cwd.to_path_buf()))
|
||||
}
|
||||
CwdPromptOutcome::Selection(CwdSelection::Session) => {
|
||||
ResolveCwdOutcome::Continue(Some(history_cwd))
|
||||
}
|
||||
CwdPromptOutcome::Selection(selection) => ResolveCwdOutcome::Continue(Some(
|
||||
selection
|
||||
.selected_cwd(
|
||||
cwd_context.current_cwd,
|
||||
&history_cwd,
|
||||
cwd_context.remembered_current_cwd,
|
||||
)
|
||||
.to_path_buf(),
|
||||
)),
|
||||
CwdPromptOutcome::Exit => ResolveCwdOutcome::Exit,
|
||||
});
|
||||
}
|
||||
@@ -308,4 +361,193 @@ mod tests {
|
||||
assert_eq!(state.cwd, Some(cwd));
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn rollout_resume_state_preserves_legacy_fork_child_context() -> std::io::Result<()> {
|
||||
let temp_dir = TempDir::new()?;
|
||||
let thread_id = ThreadId::from_string("aaaaaaaa-aaaa-4aaa-8aaa-aaaaaaaaaaaa")
|
||||
.expect("legacy thread id");
|
||||
let parent_thread_id = ThreadId::from_string("bbbbbbbb-bbbb-4bbb-8bbb-bbbbbbbbbbbb")
|
||||
.expect("legacy parent id");
|
||||
let child_cwd = temp_dir.path().join("child");
|
||||
let rollout_path = temp_dir.path().join("rollout.jsonl");
|
||||
write_rollout_lines(
|
||||
&rollout_path,
|
||||
&[
|
||||
rollout_line(
|
||||
"t0",
|
||||
"session_meta",
|
||||
serde_json::json!({
|
||||
"id": thread_id,
|
||||
"forked_from_id": parent_thread_id,
|
||||
"cwd": temp_dir.path().join("initial"),
|
||||
"originator": "test",
|
||||
"cli_version": "test",
|
||||
}),
|
||||
),
|
||||
rollout_line(
|
||||
"t1",
|
||||
"event_msg",
|
||||
serde_json::json!({ "type": "task_started", "turn_id": "legacy-child-turn" }),
|
||||
),
|
||||
rollout_line(
|
||||
"t2",
|
||||
"turn_context",
|
||||
serde_json::json!({ "cwd": child_cwd.clone(), "model": "child-model" }),
|
||||
),
|
||||
],
|
||||
)?;
|
||||
|
||||
let state = read_rollout_resume_state(&rollout_path).await?;
|
||||
|
||||
assert_eq!(state.thread_id, Some(thread_id));
|
||||
assert_eq!(state.cwd, Some(child_cwd));
|
||||
assert_eq!(state.model.as_deref(), Some("child-model"));
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn session_cwd_prefers_state_metadata_over_rollout_context() -> std::io::Result<()> {
|
||||
let temp_dir = TempDir::new()?;
|
||||
let thread_id = ThreadId::new();
|
||||
let session_cwd = temp_dir.path().join("child");
|
||||
let rollout_path = temp_dir.path().join("rollout.jsonl");
|
||||
write_rollout_lines(
|
||||
&rollout_path,
|
||||
&[
|
||||
rollout_line(
|
||||
"t0",
|
||||
"session_meta",
|
||||
serde_json::json!({
|
||||
"id": thread_id,
|
||||
"cwd": session_cwd,
|
||||
"originator": "test",
|
||||
"cli_version": "test",
|
||||
}),
|
||||
),
|
||||
rollout_line(
|
||||
"t1",
|
||||
"turn_context",
|
||||
serde_json::json!({
|
||||
"cwd": temp_dir.path().join("copied-parent"),
|
||||
"model": "parent-model",
|
||||
}),
|
||||
),
|
||||
],
|
||||
)?;
|
||||
let state_runtime =
|
||||
StateRuntime::init(temp_dir.path().to_path_buf(), "test-provider".to_string())
|
||||
.await
|
||||
.map_err(std::io::Error::other)?;
|
||||
let created_at = chrono::DateTime::parse_from_rfc3339("2025-01-05T12:00:00Z")
|
||||
.expect("timestamp should parse")
|
||||
.with_timezone(&chrono::Utc);
|
||||
let mut builder = codex_state::ThreadMetadataBuilder::new(
|
||||
thread_id,
|
||||
rollout_path.clone(),
|
||||
created_at,
|
||||
serde_json::from_value(serde_json::json!("cli"))
|
||||
.expect("cli session source should deserialize"),
|
||||
);
|
||||
builder.cwd = session_cwd.clone();
|
||||
state_runtime
|
||||
.upsert_thread(&builder.build("test-provider"))
|
||||
.await
|
||||
.map_err(std::io::Error::other)?;
|
||||
|
||||
let cwd =
|
||||
read_session_cwd(Some(state_runtime.as_ref()), thread_id, Some(&rollout_path)).await;
|
||||
|
||||
assert_eq!(cwd, Some(session_cwd));
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn configured_resume_cwd_skips_prompt() -> color_eyre::Result<()> {
|
||||
let temp_dir = TempDir::new()?;
|
||||
let thread_id = ThreadId::new();
|
||||
let session_cwd = temp_dir.path().join("session");
|
||||
let rollout_path = temp_dir.path().join("rollout.jsonl");
|
||||
let config = crate::legacy_core::config::ConfigBuilder::default()
|
||||
.codex_home(temp_dir.path().to_path_buf())
|
||||
.build()
|
||||
.await?;
|
||||
let current_cwd = config.cwd.to_path_buf();
|
||||
write_rollout_lines(
|
||||
&rollout_path,
|
||||
&[rollout_line(
|
||||
"t0",
|
||||
"session_meta",
|
||||
serde_json::json!({
|
||||
"id": thread_id,
|
||||
"cwd": session_cwd.clone(),
|
||||
"originator": "test",
|
||||
"cli_version": "test",
|
||||
}),
|
||||
)],
|
||||
)?;
|
||||
let mut tui = crate::tui::test_support::make_test_tui()?;
|
||||
|
||||
for (cwd_mode, expected_cwd) in [
|
||||
(ResumeCwdMode::Current, current_cwd.clone()),
|
||||
(ResumeCwdMode::Session, session_cwd),
|
||||
] {
|
||||
let outcome = resolve_cwd_for_resume_or_fork(
|
||||
&mut tui,
|
||||
&config,
|
||||
/*state_db_ctx*/ None,
|
||||
&SessionTarget {
|
||||
path: Some(rollout_path.clone()),
|
||||
thread_id,
|
||||
},
|
||||
CwdPromptAction::Fork,
|
||||
ResumeCwdContext {
|
||||
current_cwd: ¤t_cwd,
|
||||
remembered_current_cwd: ¤t_cwd,
|
||||
allow_remember_current: true,
|
||||
mode: Some(cwd_mode),
|
||||
},
|
||||
)
|
||||
.await?;
|
||||
|
||||
assert_eq!(outcome, ResolveCwdOutcome::Continue(Some(expected_cwd)));
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn configured_session_cwd_rejects_missing_metadata() -> color_eyre::Result<()> {
|
||||
let temp_dir = TempDir::new()?;
|
||||
let config = crate::legacy_core::config::ConfigBuilder::default()
|
||||
.codex_home(temp_dir.path().to_path_buf())
|
||||
.build()
|
||||
.await?;
|
||||
let current_cwd = config.cwd.to_path_buf();
|
||||
let mut tui = crate::tui::test_support::make_test_tui()?;
|
||||
|
||||
let error = resolve_cwd_for_resume_or_fork(
|
||||
&mut tui,
|
||||
&config,
|
||||
/*state_db_ctx*/ None,
|
||||
&SessionTarget {
|
||||
path: None,
|
||||
thread_id: ThreadId::new(),
|
||||
},
|
||||
CwdPromptAction::Resume,
|
||||
ResumeCwdContext {
|
||||
current_cwd: ¤t_cwd,
|
||||
remembered_current_cwd: ¤t_cwd,
|
||||
allow_remember_current: true,
|
||||
mode: Some(ResumeCwdMode::Session),
|
||||
},
|
||||
)
|
||||
.await
|
||||
.expect_err("session mode should reject unavailable metadata");
|
||||
|
||||
assert_eq!(
|
||||
error.to_string(),
|
||||
"failed to determine the working directory recorded for the selected session"
|
||||
);
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user