Support managed worktrees for interactive sessions and forks (#43069)

## Why

`--worktree` previously supported only `codex exec` and `codex exec fork`. Interactive sessions need the same managed checkout support, with configuration and policy resolved for the destination before starting a turn.

## What changed

- Enable `codex --worktree` and `codex fork --worktree <session-id>` behind the `worktrees` feature, restricted to local sessions. Interactive forks require an explicit session selector.
- Resolve interactive and exec worktree forks from the session's latest saved working directory unless `--cd` is supplied. Keep relative `--add-dir` paths anchored to the invocation directory.
- Load interactive destination configuration before telemetry and login policy initialization, and bind checkout ownership before the first turn.
- Reject explicitly untrusted sources, including when destination cloud policy or refreshed configuration reveals distrust. Retain unbound interactive checkouts after startup failure and report manual recovery instructions.

## Testing

Add CLI integration and TUI tests for startup and named forks, destination instructions and configuration, ownership before the first request, trust enforcement, and retained-checkout recovery. Extend exec coverage for saved fork directories, explicit `--cd`, relative writable roots, and cloud policy rejection.

GitOrigin-RevId: 371583f7feca73b218a4ff188b2b85812c8439e6
This commit is contained in:
Benjamin Carlsson authored and copyberry committed 2026-09-05 18:06:26 +00:00
1 parent 52e73e3a54
commit 3525845978
15 files changed
+1626 -41

No files matched your search

+15 -1
View File
@@ -25,6 +25,7 @@ fn spawn_startup_thread_start(
local_settings: crate::local_settings::LocalSettings,
config: Config,
app_event_tx: AppEventSender,
worktree: Option<crate::ManagedTuiWorktree>,
) {
let request_handle = app_server.request_handle();
let thread_params_mode = app_server.thread_params_mode();
@@ -39,7 +40,13 @@ fn spawn_startup_thread_start(
remote_cwd_override,
thread_tool_transport,
)
.await;
.await
.and_then(|started| {
if let Some(worktree) = worktree.as_ref() {
worktree.bind(started.session.thread_id)?;
}
Ok(started)
});
app_event_tx.send(AppEvent::StartupThreadStarted { result });
});
}
@@ -102,6 +109,7 @@ impl App {
startup_bootstrap: Option<AppServerBootstrap>,
startup_hooks_browser: Option<HooksListEntry>,
mut startup_draft: StartupDraftPump,
managed_worktree: Option<crate::ManagedTuiWorktree>,
) -> Result<AppExitInfo> {
use tokio_stream::StreamExt;
@@ -295,6 +303,7 @@ impl App {
local_settings.clone(),
config.clone(),
app_event_tx.clone(),
managed_worktree.clone(),
);
}
// Count a startup tooltip once the initial chat widget can render it.
@@ -457,6 +466,11 @@ impl App {
else {
return Ok(cancel_session_start(app_server).await);
};
if let Some(worktree) = managed_worktree.as_ref()
&& let Err(err) = worktree.bind(forked.session.thread_id)
{
return shutdown_on_startup_error(app_server, err).await;
}
let init = crate::chatwidget::ChatWidgetInit {
local_settings: local_settings.clone(),
config: config.clone(),