Preserve runtime workspace roots across thread resume (#43848)

## Why

Resuming a thread should retain its selected workspace folders, including additional roots and explicit empty selections. Resume overrides also need to survive a subsequent resume when no turn has run.

## What changed

- Persist `runtime_workspace_roots` in startup metadata and thread settings snapshots, separately from explicit environment selections and permission-profile roots.
- Restore roots from the latest snapshot owned by the resumed thread, falling back to owned startup metadata only when no snapshot exists. Honor explicit `runtimeWorkspaceRoots` overrides, retarget the old `cwd` root when `cwd` changes, deduplicate roots, and validate restored paths for the current host.
- Checkpoint effective settings on resume and restored settings after revert. Reload resume configuration if saved workspace roots change during loading.
- Normalize Windows rollout path spellings when matching thread search results, preserving selection of the correct rollout after revert, including compressed rollouts.

## Testing

Add regression coverage for workspace restoration, empty and explicit overrides, foreign paths, compaction and revert, resume checkpoints without recency changes, concurrent settings persistence, and rollout search path matching.

GitOrigin-RevId: d98d9d34dd63934d441120916c61c12b69e7f062
This commit is contained in:
rhan-oai authored and copyberry committed 2026-09-08 16:24:38 +00:00
1 parent dd9512c000
commit 6515a72db7
46 files changed
+984 -135

No files matched your search

@@ -113,9 +113,16 @@ fn map_additional_context(
.collect()
}
#[derive(Default)]
struct ThreadEnvironmentOverride {
environments: Option<TurnEnvironmentSelections>,
// Only default-environment updates replace the task's separately persisted root selection.
runtime_workspace_roots: Option<Vec<AbsolutePathBuf>>,
}
struct ThreadSettingsBuildParams {
method: &'static str,
environments: Option<TurnEnvironmentSelections>,
environment_override: ThreadEnvironmentOverride,
approval_policy: Option<codex_app_server_protocol::AskForApproval>,
approvals_reviewer: Option<codex_app_server_protocol::ApprovalsReviewer>,
sandbox_policy: Option<codex_app_server_protocol::SandboxPolicy>,
@@ -607,7 +614,7 @@ impl TurnRequestProcessor {
}
};
let cwd = resolve_request_cwd(params.cwd)?;
let environments = self
let environment_override = self
.build_environment_override(
thread.as_ref(),
cwd,
@@ -620,7 +627,7 @@ impl TurnRequestProcessor {
thread.as_ref(),
ThreadSettingsBuildParams {
method: "turn/start",
environments,
environment_override,
approval_policy: params.approval_policy,
approvals_reviewer: params.approvals_reviewer,
sandbox_policy: params.sandbox_policy,
@@ -704,9 +711,9 @@ impl TurnRequestProcessor {
cwd: Option<AbsolutePathBuf>,
workspace_roots: Option<Vec<AbsolutePathBuf>>,
environment_selections: Option<Vec<TurnEnvironmentSelection>>,
) -> Option<TurnEnvironmentSelections> {
) -> ThreadEnvironmentOverride {
if cwd.is_none() && workspace_roots.is_none() && environment_selections.is_none() {
return None;
return ThreadEnvironmentOverride::default();
}
// Explicit environment selections own their roots and pass through unchanged. Top-level
@@ -723,42 +730,37 @@ impl TurnRequestProcessor {
None => thread.config_snapshot().await.cwd().clone(),
},
};
return Some(TurnEnvironmentSelections::new(
legacy_fallback_cwd,
environment_selections,
));
return ThreadEnvironmentOverride {
environments: Some(TurnEnvironmentSelections::new(
legacy_fallback_cwd,
environment_selections,
)),
..Default::default()
};
}
let snapshot = thread.config_snapshot().await;
let current_cwd = snapshot.cwd().clone();
// Default-environment updates retain the task's fallback roots, not its active roots.
let snapshot = thread.thread_settings_snapshot().await;
let current_cwd = snapshot.cwd;
let legacy_fallback_cwd = cwd.unwrap_or_else(|| current_cwd.clone());
let workspace_roots = match workspace_roots {
Some(workspace_roots) => workspace_roots,
None => {
// Match the pre-environment partial-update behavior: a cwd-only update retargets
// the old cwd root while preserving any additional roots. Deduplicate because the
// new cwd may already be present as an additional root.
let mut retargeted_workspace_roots = Vec::new();
for root in snapshot.workspace_roots {
let root = if root == current_cwd {
legacy_fallback_cwd.clone()
} else {
root
};
if !retargeted_workspace_roots.contains(&root) {
retargeted_workspace_roots.push(root);
}
}
retargeted_workspace_roots
}
None => path_utils::replace_path_and_deduplicate(
snapshot.runtime_workspace_roots.unwrap_or_default(),
current_cwd.as_path(),
legacy_fallback_cwd.clone(),
),
};
let environment_selections = self
.thread_manager
.default_environment_selections(&legacy_fallback_cwd, &workspace_roots);
Some(TurnEnvironmentSelections::new(
legacy_fallback_cwd,
environment_selections,
))
ThreadEnvironmentOverride {
environments: Some(TurnEnvironmentSelections::new(
legacy_fallback_cwd,
environment_selections,
)),
runtime_workspace_roots: Some(workspace_roots),
}
}
async fn build_thread_settings_overrides(
@@ -768,7 +770,11 @@ impl TurnRequestProcessor {
) -> Result<codex_protocol::protocol::ThreadSettingsOverrides, JSONRPCErrorError> {
let ThreadSettingsBuildParams {
method,
environments,
environment_override:
ThreadEnvironmentOverride {
environments,
runtime_workspace_roots,
},
approval_policy,
approvals_reviewer,
sandbox_policy,
@@ -865,6 +871,7 @@ impl TurnRequestProcessor {
thread
.preview_thread_settings_overrides(CodexThreadSettingsOverrides {
environments: environments.clone(),
runtime_workspace_roots: runtime_workspace_roots.clone(),
approval_policy,
approvals_reviewer,
sandbox_policy: sandbox_policy.clone(),
@@ -887,6 +894,7 @@ impl TurnRequestProcessor {
Ok(codex_protocol::protocol::ThreadSettingsOverrides {
environments,
runtime_workspace_roots,
profile_workspace_roots,
approval_policy,
approvals_reviewer,
@@ -912,7 +920,7 @@ impl TurnRequestProcessor {
self.ensure_direct_input_allowed(request_id, thread.as_ref())
.await?;
let cwd = resolve_request_cwd(params.cwd)?;
let environments = self
let environment_override = self
.build_environment_override(
thread.as_ref(),
cwd,
@@ -925,7 +933,7 @@ impl TurnRequestProcessor {
thread.as_ref(),
ThreadSettingsBuildParams {
method: "thread/settings/update",
environments,
environment_override,
approval_policy: params.approval_policy,
approvals_reviewer: params.approvals_reviewer,
sandbox_policy: params.sandbox_policy,