core: render remote environment cwd natively

This commit is contained in:
Adam Perry
2026-06-16 16:32:36 +00:00
parent e4daf1d585
commit 54304e5472
3 changed files with 134 additions and 46 deletions
@@ -10,6 +10,8 @@ use codex_protocol::permissions::FileSystemSpecialPath;
use codex_protocol::protocol::TurnContextItem;
use codex_protocol::protocol::TurnContextNetworkItem;
use codex_utils_absolute_path::AbsolutePathBuf;
use codex_utils_path_uri::ApiPathString;
use codex_utils_path_uri::PathUri;
use std::collections::HashSet;
use std::path::PathBuf;
@@ -28,12 +30,12 @@ pub(crate) struct EnvironmentContext {
#[derive(Debug, Clone, PartialEq, Eq)]
pub(crate) struct EnvironmentContextEnvironment {
pub(crate) id: String,
pub(crate) cwd: AbsolutePathBuf,
pub(crate) cwd: PathUri,
pub(crate) shell: String,
}
impl EnvironmentContextEnvironment {
fn legacy(cwd: AbsolutePathBuf, shell: String) -> Self {
fn legacy(cwd: PathUri, shell: String) -> Self {
Self {
id: String::new(),
cwd,
@@ -44,18 +46,14 @@ impl EnvironmentContextEnvironment {
fn from_turn_environments(environments: &[TurnEnvironment], shell: &Shell) -> Vec<Self> {
environments
.iter()
.filter_map(|environment| {
// TODO(anp): Migrate EnvironmentContextEnvironment to PathUri so foreign
// environments remain visible in model context.
Some(Self {
id: environment.environment_id.clone(),
cwd: environment.cwd().to_abs_path().ok()?,
shell: environment
.shell
.as_ref()
.map(|shell| shell.name().to_string())
.unwrap_or_else(|| shell.name().to_string()),
})
.map(|environment| Self {
id: environment.environment_id.clone(),
cwd: environment.cwd().clone(),
shell: environment
.shell
.as_ref()
.map(|shell| shell.name().to_string())
.unwrap_or_else(|| shell.name().to_string()),
})
.collect()
}
@@ -388,7 +386,7 @@ impl EnvironmentContext {
let before_filesystem = Self::filesystem_from_turn_context_item(before);
let environments = match &after.environments {
EnvironmentContextEnvironments::Single(environment) => {
if before.cwd.as_path() != environment.cwd.as_path() {
if turn_context_item_cwd_uri(before) != environment.cwd {
EnvironmentContextEnvironments::Single(EnvironmentContextEnvironment::legacy(
environment.cwd.clone(),
environment.shell.clone(),
@@ -444,13 +442,10 @@ impl EnvironmentContext {
turn_context_item: &TurnContextItem,
shell: String,
) -> Self {
let cwd = match AbsolutePathBuf::try_from(turn_context_item.cwd.clone()) {
Ok(cwd) => cwd,
Err(_) => AbsolutePathBuf::resolve_path_against_base(&turn_context_item.cwd, "/"),
};
Self::new_with_environments(
EnvironmentContextEnvironments::from_vec(vec![EnvironmentContextEnvironment::legacy(
cwd, shell,
turn_context_item_cwd_uri(turn_context_item),
shell,
)]),
turn_context_item.current_date.clone(),
turn_context_item.timezone.clone(),
@@ -547,20 +542,16 @@ impl ContextualUserFragment for EnvironmentContext {
let mut lines = Vec::new();
match &self.environments {
EnvironmentContextEnvironments::Single(environment) => {
lines.push(format!(
" <cwd>{}</cwd>",
environment.cwd.to_string_lossy()
));
let cwd = render_environment_cwd(&environment.cwd);
lines.push(format!(" <cwd>{cwd}</cwd>"));
lines.push(format!(" <shell>{}</shell>", environment.shell));
}
EnvironmentContextEnvironments::Multiple(environments) => {
lines.push(" <environments>".to_string());
for environment in environments {
lines.push(format!(" <environment id=\"{}\">", environment.id));
lines.push(format!(
" <cwd>{}</cwd>",
environment.cwd.to_string_lossy()
));
let cwd = render_environment_cwd(&environment.cwd);
lines.push(format!(" <cwd>{cwd}</cwd>"));
lines.push(format!(" <shell>{}</shell>", environment.shell));
lines.push(" </environment>".to_string());
}
@@ -595,6 +586,23 @@ impl ContextualUserFragment for EnvironmentContext {
}
}
fn turn_context_item_cwd_uri(turn_context_item: &TurnContextItem) -> PathUri {
let cwd = match AbsolutePathBuf::try_from(turn_context_item.cwd.clone()) {
Ok(cwd) => cwd,
Err(_) => AbsolutePathBuf::resolve_path_against_base(&turn_context_item.cwd, "/"),
};
PathUri::from_abs_path(&cwd)
}
fn render_environment_cwd(cwd: &PathUri) -> String {
let Some(convention) = cwd.infer_path_convention() else {
return cwd.to_string();
};
ApiPathString::from_path_uri(cwd, convention)
.map(ApiPathString::into_string)
.unwrap_or_else(|_| cwd.to_string())
}
#[cfg(test)]
#[path = "environment_context_tests.rs"]
mod tests;
@@ -36,7 +36,7 @@ fn serialize_workspace_write_environment_context() {
let context = EnvironmentContext::new(
vec![EnvironmentContextEnvironment {
id: "local".to_string(),
cwd: cwd.abs(),
cwd: PathUri::from_abs_path(&cwd.abs()),
shell: fake_shell_name(),
}],
Some("2026-02-26".to_string()),
@@ -58,6 +58,29 @@ fn serialize_workspace_write_environment_context() {
assert_eq!(context.render(), expected);
}
#[test]
fn serialize_environment_context_with_foreign_windows_cwd() {
let context = EnvironmentContext::new(
vec![EnvironmentContextEnvironment {
id: "remote".to_string(),
cwd: PathUri::parse("file:///C:/windows").expect("Windows cwd URI"),
shell: "powershell".to_string(),
}],
/*current_date*/ None,
/*timezone*/ None,
/*network*/ None,
/*subagents*/ None,
);
assert_eq!(
context.render(),
r#"<environment_context>
<cwd>C:\windows</cwd>
<shell>powershell</shell>
</environment_context>"#
);
}
#[test]
fn serialize_environment_context_with_network() {
let network = NetworkContext::new(
@@ -67,7 +90,7 @@ fn serialize_environment_context_with_network() {
let context = EnvironmentContext::new(
vec![EnvironmentContextEnvironment {
id: "local".to_string(),
cwd: test_path_buf("/repo").abs(),
cwd: PathUri::from_abs_path(&test_abs_path("/repo")),
shell: fake_shell_name(),
}],
Some("2026-02-26".to_string()),
@@ -129,7 +152,7 @@ fn serialize_environment_context_with_full_filesystem_profile() {
let mut context = EnvironmentContext::new(
vec![EnvironmentContextEnvironment {
id: "local".to_string(),
cwd: test_path_buf("/repo").abs(),
cwd: PathUri::from_abs_path(&test_abs_path("/repo")),
shell: fake_shell_name(),
}],
/*current_date*/ None,
@@ -211,6 +234,53 @@ fn turn_context_item_filesystem_uses_workspace_roots_instead_of_cwd() {
);
}
#[cfg(unix)]
#[test]
fn legacy_projected_windows_cwd_matches_environment_uri() {
let item = TurnContextItem {
turn_id: None,
cwd: PathBuf::from("/C:/windows"),
workspace_roots: None,
current_date: None,
timezone: None,
approval_policy: AskForApproval::Never,
sandbox_policy: SandboxPolicy::DangerFullAccess,
permission_profile: Some(PermissionProfile::Disabled),
network: None,
file_system_sandbox_policy: None,
model: "gpt-5".to_string(),
comp_hash: None,
personality: None,
collaboration_mode: None,
multi_agent_version: None,
realtime_active: None,
effort: None,
summary: codex_protocol::config_types::ReasoningSummary::Auto,
};
let current = EnvironmentContext::new(
vec![EnvironmentContextEnvironment {
id: "remote".to_string(),
cwd: PathUri::parse("file:///C:/windows").expect("Windows cwd URI"),
shell: "powershell".to_string(),
}],
/*current_date*/ None,
/*timezone*/ None,
/*network*/ None,
/*subagents*/ None,
);
assert_eq!(
EnvironmentContext::diff_from_turn_context_item(&item, &current),
EnvironmentContext::new(
Vec::new(),
/*current_date*/ None,
/*timezone*/ None,
/*network*/ None,
/*subagents*/ None,
)
);
}
#[test]
fn serialize_read_only_environment_context() {
let context = EnvironmentContext::new(
@@ -234,7 +304,7 @@ fn equals_except_shell_compares_cwd() {
let context1 = EnvironmentContext::new(
vec![EnvironmentContextEnvironment {
id: "local".to_string(),
cwd: test_abs_path("/repo"),
cwd: PathUri::from_abs_path(&test_abs_path("/repo")),
shell: fake_shell_name(),
}],
/*current_date*/ None,
@@ -245,7 +315,7 @@ fn equals_except_shell_compares_cwd() {
let context2 = EnvironmentContext::new(
vec![EnvironmentContextEnvironment {
id: "local".to_string(),
cwd: test_abs_path("/repo"),
cwd: PathUri::from_abs_path(&test_abs_path("/repo")),
shell: fake_shell_name(),
}],
/*current_date*/ None,
@@ -261,7 +331,7 @@ fn equals_except_shell_compares_cwd_differences() {
let context1 = EnvironmentContext::new(
vec![EnvironmentContextEnvironment {
id: "local".to_string(),
cwd: test_abs_path("/repo1"),
cwd: PathUri::from_abs_path(&test_abs_path("/repo1")),
shell: fake_shell_name(),
}],
/*current_date*/ None,
@@ -272,7 +342,7 @@ fn equals_except_shell_compares_cwd_differences() {
let context2 = EnvironmentContext::new(
vec![EnvironmentContextEnvironment {
id: "local".to_string(),
cwd: test_abs_path("/repo2"),
cwd: PathUri::from_abs_path(&test_abs_path("/repo2")),
shell: fake_shell_name(),
}],
/*current_date*/ None,
@@ -289,7 +359,7 @@ fn equals_except_shell_ignores_shell() {
let context1 = EnvironmentContext::new(
vec![EnvironmentContextEnvironment {
id: "local".to_string(),
cwd: test_abs_path("/repo"),
cwd: PathUri::from_abs_path(&test_abs_path("/repo")),
shell: "bash".to_string(),
}],
/*current_date*/ None,
@@ -300,7 +370,7 @@ fn equals_except_shell_ignores_shell() {
let context2 = EnvironmentContext::new(
vec![EnvironmentContextEnvironment {
id: "other".to_string(),
cwd: test_abs_path("/repo"),
cwd: PathUri::from_abs_path(&test_abs_path("/repo")),
shell: "zsh".to_string(),
}],
/*current_date*/ None,
@@ -317,7 +387,7 @@ fn serialize_environment_context_with_subagents() {
let context = EnvironmentContext::new(
vec![EnvironmentContextEnvironment {
id: "local".to_string(),
cwd: test_path_buf("/repo").abs(),
cwd: PathUri::from_abs_path(&test_abs_path("/repo")),
shell: fake_shell_name(),
}],
Some("2026-02-26".to_string()),
@@ -351,12 +421,12 @@ fn serialize_environment_context_with_multiple_selected_environments() {
vec![
EnvironmentContextEnvironment {
id: "local".to_string(),
cwd: local_cwd.abs(),
cwd: PathUri::from_abs_path(&local_cwd.abs()),
shell: "bash".to_string(),
},
EnvironmentContextEnvironment {
id: "remote".to_string(),
cwd: remote_cwd.abs(),
cwd: PathUri::from_abs_path(&remote_cwd.abs()),
shell: "bash".to_string(),
},
],
@@ -396,12 +466,12 @@ fn serialize_environment_context_prefers_environment_shell_when_present() {
vec![
EnvironmentContextEnvironment {
id: "local".to_string(),
cwd: local_cwd.abs(),
cwd: PathUri::from_abs_path(&local_cwd.abs()),
shell: "powershell".to_string(),
},
EnvironmentContextEnvironment {
id: "remote".to_string(),
cwd: remote_cwd.abs(),
cwd: PathUri::from_abs_path(&remote_cwd.abs()),
shell: "cmd".to_string(),
},
],
@@ -347,6 +347,7 @@ async fn app_server_starts_thread_with_windows_environment_native_cwd() -> Resul
.contains(AGENTS_INSTRUCTIONS);
// TODO(anp): Load remote workspace instructions into the model context.
assert!(!model_request_includes_remote_instructions);
let first_request = requests
.iter()
.find(|request| request.url.path().ends_with("/responses"))
@@ -367,17 +368,26 @@ async fn app_server_starts_thread_with_windows_environment_native_cwd() -> Resul
assert_eq!(
environment_context
.lines()
.find(|line| line.trim_start().starts_with("<shell>")),
Some(" <shell>powershell</shell>"),
.find(|line| line.trim_start().starts_with("<shell>"))
.map(str::trim),
Some("<shell>powershell</shell>"),
);
// The model should see cwd using the remote environment's native path convention, not
// the Linux app-server's host path convention.
assert_eq!(
environment_context
.lines()
.find(|line| line.trim_start().starts_with("<cwd>")),
Some(r" <cwd>C:\windows</cwd>"),
.find(|line| line.trim_start().starts_with("<cwd>"))
.map(str::trim),
Some(r"<cwd>C:\windows</cwd>"),
);
let host_workspace_roots = format!(
"<workspace_roots><root>{}</root></workspace_roots>",
codex_home.path().display()
);
// TODO(anp): Derive model-visible workspace roots from the selected remote environment
// and render them using its native path convention.
assert!(environment_context.contains(&host_workspace_roots));
Ok(())
})