Preserve workspace roots across client and server platforms (#62377)

## Why

A TUI connected to an app server on another platform can receive workspace
roots that its own filesystem cannot represent. For example, a Windows
drive path from the server cannot be stored as a native absolute path on
a Unix client.

## What changed

Keep `runtimeWorkspaceRoots` as native path strings on the wire for thread
start, resume, and fork. Validate absolute paths on the server and store
the returned roots as `PathUri` values in the TUI. Use the session's roots
for active remote forks, including side chats, and preserve permissions
for those roots in the TUI's permission display.

## Testing

Cover thread startup with workspace and writable roots from another
platform, remote root retention through start, turn, resume, and fork,
and server rejection of relative roots.

GitOrigin-RevId: 1e9e3bff64a6e7ddadb941f16c4952c44724af54
This commit is contained in:
Eric Traut
2026-11-18 22:40:03 +00:00
committed by copyberry
parent 7d458c8fc5
commit b967993f35
41 changed files with 492 additions and 169 deletions
@@ -1292,7 +1292,9 @@ pub(super) async fn handle_pending_thread_resume_request(
model_provider: model_provider_id,
service_tier,
cwd,
runtime_workspace_roots: workspace_roots,
runtime_workspace_roots: codex_utils_path_uri::LegacyAppPathString::from_abs_paths(
&workspace_roots,
),
instruction_sources,
approval_policy: approval_policy.into(),
approvals_reviewer: approvals_reviewer.into(),
@@ -61,6 +61,7 @@ use codex_thread_store::PersistContext;
use codex_thread_store::StoredThreadSearchResult;
use codex_thread_store::ThreadSearchObserver;
use codex_thread_store::ThreadSearchSourceKind;
use codex_utils_path_uri::LegacyAppPathString;
use std::ops::ControlFlow;
use tokio::sync::mpsc;
@@ -164,11 +165,30 @@ struct ThreadRevertRuntimeSnapshot {
client_mcp_extensions: ClientMcpExtensions,
}
fn native_runtime_workspace_roots(
roots: Vec<LegacyAppPathString>,
) -> Result<Vec<AbsolutePathBuf>, JSONRPCErrorError> {
let roots = roots
.into_iter()
.map(|root| {
if !root.as_path().is_absolute() {
return Err(invalid_request(format!(
"invalid runtimeWorkspaceRoots: path is not absolute: {}",
root.as_str(),
)));
}
AbsolutePathBuf::try_from(root)
.map_err(|err| invalid_request(format!("invalid runtimeWorkspaceRoots: {err}")))
})
.collect::<Result<_, _>>()?;
Ok(resolve_runtime_workspace_roots(roots))
}
fn collect_resume_override_mismatches(
request: &ThreadResumeParams,
config_snapshot: &ThreadConfigSnapshot,
selected_service_tier: Option<&str>,
) -> Vec<String> {
) -> Result<Vec<String>, JSONRPCErrorError> {
let mut mismatch_details = Vec::new();
if let Some(requested_model) = request.model.as_deref()
@@ -206,7 +226,8 @@ fn collect_resume_override_mismatches(
}
}
if let Some(requested_runtime_workspace_roots) = request.runtime_workspace_roots.as_ref() {
let requested_runtime_workspace_roots = requested_runtime_workspace_roots.to_vec();
let requested_runtime_workspace_roots =
native_runtime_workspace_roots(requested_runtime_workspace_roots.to_vec())?;
if requested_runtime_workspace_roots != config_snapshot.workspace_roots {
mismatch_details.push(format!(
"runtime_workspace_roots requested={requested_runtime_workspace_roots:?} active={:?}",
@@ -279,7 +300,7 @@ fn collect_resume_override_mismatches(
"developerInstructions override was provided and ignored while running".to_string(),
);
}
mismatch_details
Ok(mismatch_details)
}
fn merge_persisted_resume_metadata(
@@ -1359,7 +1380,9 @@ impl ThreadRequestProcessor {
return Err(invalid_request(format!("project not found: {project_id}")));
}
}
let runtime_workspace_roots = runtime_workspace_roots.map(resolve_runtime_workspace_roots);
let runtime_workspace_roots = runtime_workspace_roots
.map(native_runtime_workspace_roots)
.transpose()?;
let environment_requests =
resolve_turn_environment_requests(self.thread_manager.as_ref(), environments)?;
let mut typesafe_overrides = self.build_thread_config_overrides(
@@ -1790,7 +1813,9 @@ impl ThreadRequestProcessor {
model_provider: config_snapshot.model_provider_id,
service_tier: config_snapshot.service_tier,
cwd,
runtime_workspace_roots: config_snapshot.workspace_roots,
runtime_workspace_roots: LegacyAppPathString::from_abs_paths(
&config_snapshot.workspace_roots,
),
instruction_sources,
approval_policy: config_snapshot.approval_policy.into(),
approvals_reviewer: config_snapshot.approvals_reviewer.into(),
@@ -4307,8 +4332,9 @@ impl ThreadRequestProcessor {
let history_cwd = history_settings
.map(|settings| settings.cwd.to_path_buf())
.or_else(|| thread_history.session_cwd());
let mut runtime_workspace_roots =
runtime_workspace_roots.map(resolve_runtime_workspace_roots);
let mut runtime_workspace_roots = runtime_workspace_roots
.map(native_runtime_workspace_roots)
.transpose()?;
if runtime_workspace_roots.is_none() {
// A retained owned snapshot is authoritative. Only use startup metadata when
// no such snapshot exists; missing roots do not resurrect an older selection.
@@ -4694,7 +4720,9 @@ impl ThreadRequestProcessor {
model_provider: session_configured.model_provider_id,
service_tier: session_configured.service_tier,
cwd: thread_cwd_for_api(&session_configured.cwd)?,
runtime_workspace_roots: config_snapshot.workspace_roots,
runtime_workspace_roots: LegacyAppPathString::from_abs_paths(
&config_snapshot.workspace_roots,
),
instruction_sources,
approval_policy: session_configured.approval_policy.into(),
approvals_reviewer: session_configured.approvals_reviewer.into(),
@@ -4877,7 +4905,7 @@ impl ThreadRequestProcessor {
params,
&config_snapshot,
selected_service_tier.as_deref(),
);
)?;
if !mismatch_details.is_empty() {
let has_subscribers = !self
.thread_state_manager
@@ -5692,7 +5720,9 @@ impl ThreadRequestProcessor {
} else {
Some(cli_overrides)
};
let runtime_workspace_roots = runtime_workspace_roots.map(resolve_runtime_workspace_roots);
let runtime_workspace_roots = runtime_workspace_roots
.map(native_runtime_workspace_roots)
.transpose()?;
let mut typesafe_overrides = self.build_thread_config_overrides(
model,
model_provider,
@@ -6081,7 +6111,9 @@ impl ThreadRequestProcessor {
model_provider: session_configured.model_provider_id,
service_tier: config_snapshot.service_tier.clone(),
cwd: thread_cwd_for_api(&session_configured.cwd)?,
runtime_workspace_roots: config_snapshot.workspace_roots,
runtime_workspace_roots: LegacyAppPathString::from_abs_paths(
&config_snapshot.workspace_roots,
),
instruction_sources,
approval_policy: session_configured.approval_policy.into(),
approvals_reviewer: session_configured.approvals_reviewer.into(),
@@ -22,6 +22,25 @@ fn thread_cwd_rejects_invalid_utf8() {
);
}
#[test]
fn runtime_workspace_roots_reject_home_relative_paths() {
use codex_utils_absolute_path::AbsolutePathBufGuard;
use codex_utils_path_uri::LegacyAppPathString;
let home = std::env::current_dir().expect("current directory");
let result = AbsolutePathBufGuard::with_home_directory(&home, || {
super::native_runtime_workspace_roots(vec![LegacyAppPathString::from_string("~/extra")])
});
assert_eq!(
result.expect_err("runtime workspace root must be absolute"),
JSONRPCErrorError {
code: crate::error_code::INVALID_REQUEST_ERROR_CODE,
message: "invalid runtimeWorkspaceRoots: path is not absolute: ~/extra".into(),
data: None,
}
);
}
#[test]
fn storage_failure_classification_does_not_change_mutation_error_policy() {
for error in [
@@ -791,7 +810,8 @@ mod thread_processor_behavior_tests {
&request,
&config_snapshot,
/* selected_service_tier */ Some("flex"),
),
)
.unwrap(),
vec!["service_tier requested=Some(\"priority\") active=Some(\"flex\")".to_string()]
);
}
@@ -36,6 +36,7 @@ use codex_app_server_protocol::UserInput as V2UserInput;
use codex_config::types::AuthCredentialsStoreMode;
use codex_features::Feature;
use codex_utils_absolute_path::test_support::PathExt;
use codex_utils_path_uri::LegacyAppPathString;
use core_test_support::responses;
use core_test_support::skip_if_no_network;
use pretty_assertions::assert_eq;
@@ -604,7 +605,10 @@ async fn thread_compact_start_triggers_compaction_and_returns_empty_response() -
} = timeout(DEFAULT_READ_TIMEOUT, mcp.read_response(resume_id)).await??;
assert_eq!(
(cwd.as_path(), runtime_workspace_roots),
(updated_cwd.path(), updated_roots)
(
updated_cwd.path(),
LegacyAppPathString::from_abs_paths(&updated_roots)
)
);
Ok(())
@@ -1,3 +1,4 @@
use codex_utils_path_uri::LegacyAppPathString;
use std::path::Path;
use std::time::Duration;
@@ -73,7 +74,7 @@ async fn thread_start_reports_selected_environment_metadata() -> Result<()> {
(
// TODO(anp): Return the selected environment's native cwd from thread/start.
host_cwd,
selected_workspace_roots,
LegacyAppPathString::from_abs_paths(&selected_workspace_roots),
// TODO(anp): Report the implicit built-in permission profile instead of None.
None,
)
@@ -110,13 +110,8 @@ async fn thread_environments_follow_the_loaded_thread_selection() -> Result<()>
timeout(DEFAULT_READ_TIMEOUT, second.read_response(request)).await??;
let expected = Some(vec![ThreadEnvironment {
environment_id: second.auto_env_params()?.environment_id,
cwd: resumed.cwd.clone(),
runtime_workspace_roots: resumed
.runtime_workspace_roots
.iter()
.cloned()
.map(Into::into)
.collect(),
cwd: resumed.cwd,
runtime_workspace_roots: resumed.runtime_workspace_roots,
}]);
assert_eq!(resumed.thread.environments, expected);
assert_read_and_list_environments(&mut second, &thread_id, &expected).await?;
@@ -784,7 +784,7 @@ async fn thread_resume_restores_startup_and_updated_runtime_workspace_roots() ->
.send_thread_start_request(ThreadStartParams {
model: Some("gpt-5.4".to_string()),
cwd: Some(initial_cwd.to_string_lossy().into_owned()),
runtime_workspace_roots: Some(initial_roots.clone()),
runtime_workspace_roots: Some(LegacyAppPathString::from_abs_paths(&initial_roots)),
environments: Some(explicit_environments.clone()),
..Default::default()
})
@@ -828,7 +828,7 @@ async fn thread_resume_restores_startup_and_updated_runtime_workspace_roots() ->
runtime_workspace_roots,
..
} = timeout(DEFAULT_READ_TIMEOUT, mcp.read_response(resume_id)).await??;
assert_eq!(runtime_workspace_roots, vec![explicit_cwd.clone()]);
assert_eq!(runtime_workspace_roots, vec![explicit_cwd.clone().into()]);
// A cwd-only update must retain the fallback's additional folder, not the live roots.
mcp.clear_message_buffer();
@@ -901,7 +901,10 @@ async fn thread_resume_restores_startup_and_updated_runtime_workspace_roots() ->
} = timeout(DEFAULT_READ_TIMEOUT, mcp.read_response(resume_id)).await??;
assert_eq!(
(cwd, runtime_workspace_roots),
(initial_cwd.clone().into(), expected_roots)
(
initial_cwd.clone().into(),
LegacyAppPathString::from_abs_paths(&expected_roots)
)
);
Ok(())
@@ -1003,7 +1006,9 @@ sandbox = "unelevated"
.send_thread_resume_request(ThreadResumeParams {
thread_id: thread_id.clone(),
cwd: cwd_override.map(|cwd| cwd.to_string_lossy().into_owned()),
runtime_workspace_roots: roots_override,
runtime_workspace_roots: roots_override
.as_deref()
.map(LegacyAppPathString::from_abs_paths),
service_tier: (index == 2).then_some(Some("priority".to_string())),
personality: (index == 2).then_some(Personality::None),
config: (index == 1).then(|| {
@@ -1019,7 +1024,10 @@ sandbox = "unelevated"
timeout(DEFAULT_READ_TIMEOUT, mcp.read_response(resume_id)).await??;
assert_eq!(
(resumed.cwd, resumed.runtime_workspace_roots),
(expected_cwd.clone().into(), expected_roots.clone())
(
expected_cwd.clone().into(),
LegacyAppPathString::from_abs_paths(&expected_roots)
)
);
if index < 3 {
mcp.start_turn_and_wait_for_completion(TurnStartParams {
@@ -1236,7 +1244,11 @@ async fn cold_resume_workspace_roots_validate_foreign_startup_paths() -> Result<
} = timeout(DEFAULT_READ_TIMEOUT, mcp.read_response(resume_id)).await??;
assert_eq!(
(thread.id, cwd, runtime_workspace_roots),
(thread_id, local_cwd.clone().into(), expected_roots)
(
thread_id,
local_cwd.clone().into(),
LegacyAppPathString::from_abs_paths(&expected_roots)
)
);
} else {
let error = timeout(
@@ -1983,7 +1995,7 @@ async fn cold_resume_reresolves_persisted_active_permission_profile() -> Result<
);
assert_eq!(
runtime_workspace_roots,
vec![previous_workspace_root.path().abs()]
vec![previous_workspace_root.path().abs().into()]
);
}
Ok(())
@@ -45,6 +45,7 @@ use codex_protocol::protocol::MultiAgentVersion;
use codex_rollout::RolloutItem;
use codex_rollout::read_session_meta_line;
use codex_utils_absolute_path::AbsolutePathBuf;
use codex_utils_path_uri::LegacyAppPathString;
use core_test_support::load_default_config_for_test;
use core_test_support::responses;
use pretty_assertions::assert_eq;
@@ -384,7 +385,10 @@ async fn thread_revert_preserves_fork_cutoff_after_cold_resume() -> Result<()> {
.await?;
assert_eq!(
(cwd.as_path(), runtime_workspace_roots),
(saved_cwd.as_path(), saved_roots.clone())
(
saved_cwd.as_path(),
LegacyAppPathString::from_abs_paths(&saved_roots)
)
);
mcp.start_turn_and_wait_for_completion(TurnStartParams {
thread_id: child.id.clone(),
@@ -559,10 +559,26 @@ async fn thread_start_accepts_absolute_runtime_workspace_roots() -> Result<()> {
.build_initialized()
.await?;
let invalid_id = mcp
.send_thread_start_request(ThreadStartParams {
runtime_workspace_roots: Some(vec![
codex_utils_path_uri::LegacyAppPathString::from_string("relative"),
]),
..Default::default()
})
.await?;
let invalid = timeout(
DEFAULT_READ_TIMEOUT,
mcp.read_stream_until_error_message(RequestId::Integer(invalid_id)),
)
.await??;
assert_eq!(invalid.error.code, INVALID_REQUEST_ERROR_CODE);
assert!(invalid.error.message.contains("runtimeWorkspaceRoots"));
let req_id = mcp
.send_thread_start_request(ThreadStartParams {
cwd: Some(cwd.to_string_lossy().to_string()),
runtime_workspace_roots: Some(vec![extra_root.abs()]),
runtime_workspace_roots: Some(vec![extra_root.abs().into()]),
sandbox: Some(SandboxMode::WorkspaceWrite),
..Default::default()
})
@@ -576,7 +592,7 @@ async fn thread_start_accepts_absolute_runtime_workspace_roots() -> Result<()> {
} = timeout(DEFAULT_READ_TIMEOUT, mcp.read_response(req_id)).await??;
assert_eq!(response_cwd, cwd.abs().into());
assert_eq!(runtime_workspace_roots, vec![extra_root.abs()]);
assert_eq!(runtime_workspace_roots, vec![extra_root.abs().into()]);
#[cfg(windows)]
let _ = sandbox;
#[cfg(not(windows))]
@@ -596,7 +612,7 @@ async fn thread_start_accepts_absolute_runtime_workspace_roots() -> Result<()> {
environment.runtime_workspace_roots = Some(vec![environment_root.abs().into()]);
let req_id = mcp
.send_thread_start_request(ThreadStartParams {
runtime_workspace_roots: Some(vec![extra_root.abs()]),
runtime_workspace_roots: Some(vec![extra_root.abs().into()]),
environments: Some(vec![environment]),
sandbox: Some(SandboxMode::WorkspaceWrite),
..Default::default()
@@ -607,7 +623,7 @@ async fn thread_start_accepts_absolute_runtime_workspace_roots() -> Result<()> {
sandbox,
..
} = timeout(DEFAULT_READ_TIMEOUT, mcp.read_response(req_id)).await??;
assert_eq!(runtime_workspace_roots, vec![environment_root.abs()]);
assert_eq!(runtime_workspace_roots, vec![environment_root.abs().into()]);
#[cfg(windows)]
let _ = sandbox;
#[cfg(not(windows))]
@@ -656,7 +672,7 @@ async fn thread_start_excludes_profile_workspace_roots_from_runtime_workspace_ro
assert_eq!(
runtime_workspace_roots,
vec![cwd.path().to_path_buf().abs()]
vec![cwd.path().to_path_buf().abs().into()]
);
Ok(())