From a64231c1381ce2d57d99da7b6260d3d44f373ec5 Mon Sep 17 00:00:00 2001 From: Adam Perry Date: Sun, 14 Jun 2026 07:37:54 +0000 Subject: [PATCH] app-server: preserve command action paths --- .../analytics/src/analytics_client_tests.rs | 3 +- ...CommandExecutionRequestApprovalParams.json | 2 +- .../schema/json/ServerNotification.json | 2 +- .../schema/json/ServerRequest.json | 2 +- .../codex_app_server_protocol.schemas.json | 2 +- .../codex_app_server_protocol.v2.schemas.json | 2 +- .../json/v2/ItemCompletedNotification.json | 2 +- .../json/v2/ItemStartedNotification.json | 2 +- .../schema/json/v2/ReviewStartResponse.json | 2 +- .../schema/json/v2/ThreadForkResponse.json | 2 +- .../schema/json/v2/ThreadListResponse.json | 2 +- .../json/v2/ThreadMetadataUpdateResponse.json | 2 +- .../schema/json/v2/ThreadReadResponse.json | 2 +- .../schema/json/v2/ThreadResumeResponse.json | 2 +- .../json/v2/ThreadRollbackResponse.json | 2 +- .../schema/json/v2/ThreadStartResponse.json | 2 +- .../json/v2/ThreadStartedNotification.json | 2 +- .../json/v2/ThreadUnarchiveResponse.json | 2 +- .../json/v2/TurnCompletedNotification.json | 2 +- .../schema/json/v2/TurnStartResponse.json | 2 +- .../json/v2/TurnStartedNotification.json | 2 +- .../schema/typescript/v2/CommandAction.ts | 4 +- .../src/protocol/item_builders.rs | 23 +++++------ .../src/protocol/v2/item.rs | 40 +++++++++++++++---- .../src/protocol/v2/tests.rs | 31 ++++++++++++++ codex-rs/app-server/README.md | 1 + .../app-server/src/bespoke_event_handling.rs | 15 +++---- .../utils/path-uri/src/api_path_string.rs | 19 +++++++++ .../path-uri/src/api_path_string_tests.rs | 27 +++++++++++++ 29 files changed, 149 insertions(+), 54 deletions(-) diff --git a/codex-rs/analytics/src/analytics_client_tests.rs b/codex-rs/analytics/src/analytics_client_tests.rs index 96756edd57..05f6adc529 100644 --- a/codex-rs/analytics/src/analytics_client_tests.rs +++ b/codex-rs/analytics/src/analytics_client_tests.rs @@ -2131,7 +2131,8 @@ async fn item_lifecycle_notifications_publish_command_execution_event() { CommandAction::Read { command: "cat README.md".to_string(), name: "README.md".to_string(), - path: test_path_buf("/tmp/README.md").abs(), + path: serde_json::from_value(json!("/tmp/README.md")) + .expect("API path should deserialize"), }, CommandAction::ListFiles { command: "ls".to_string(), diff --git a/codex-rs/app-server-protocol/schema/json/CommandExecutionRequestApprovalParams.json b/codex-rs/app-server-protocol/schema/json/CommandExecutionRequestApprovalParams.json index 80380f858f..6230e0d0f8 100644 --- a/codex-rs/app-server-protocol/schema/json/CommandExecutionRequestApprovalParams.json +++ b/codex-rs/app-server-protocol/schema/json/CommandExecutionRequestApprovalParams.json @@ -98,7 +98,7 @@ "type": "string" }, "path": { - "$ref": "#/definitions/AbsolutePathBuf" + "$ref": "#/definitions/ApiPathString" }, "type": { "enum": [ diff --git a/codex-rs/app-server-protocol/schema/json/ServerNotification.json b/codex-rs/app-server-protocol/schema/json/ServerNotification.json index 285b8ab525..5675acf129 100644 --- a/codex-rs/app-server-protocol/schema/json/ServerNotification.json +++ b/codex-rs/app-server-protocol/schema/json/ServerNotification.json @@ -782,7 +782,7 @@ "type": "string" }, "path": { - "$ref": "#/definitions/AbsolutePathBuf" + "$ref": "#/definitions/ApiPathString" }, "type": { "enum": [ diff --git a/codex-rs/app-server-protocol/schema/json/ServerRequest.json b/codex-rs/app-server-protocol/schema/json/ServerRequest.json index 76368529fe..5db185672e 100644 --- a/codex-rs/app-server-protocol/schema/json/ServerRequest.json +++ b/codex-rs/app-server-protocol/schema/json/ServerRequest.json @@ -167,7 +167,7 @@ "type": "string" }, "path": { - "$ref": "#/definitions/AbsolutePathBuf" + "$ref": "#/definitions/ApiPathString" }, "type": { "enum": [ diff --git a/codex-rs/app-server-protocol/schema/json/codex_app_server_protocol.schemas.json b/codex-rs/app-server-protocol/schema/json/codex_app_server_protocol.schemas.json index f652c4c0b8..4c8853bf00 100644 --- a/codex-rs/app-server-protocol/schema/json/codex_app_server_protocol.schemas.json +++ b/codex-rs/app-server-protocol/schema/json/codex_app_server_protocol.schemas.json @@ -7234,7 +7234,7 @@ "type": "string" }, "path": { - "$ref": "#/definitions/v2/AbsolutePathBuf" + "$ref": "#/definitions/v2/ApiPathString" }, "type": { "enum": [ diff --git a/codex-rs/app-server-protocol/schema/json/codex_app_server_protocol.v2.schemas.json b/codex-rs/app-server-protocol/schema/json/codex_app_server_protocol.v2.schemas.json index efa516cd53..3d44c7a9bb 100644 --- a/codex-rs/app-server-protocol/schema/json/codex_app_server_protocol.v2.schemas.json +++ b/codex-rs/app-server-protocol/schema/json/codex_app_server_protocol.v2.schemas.json @@ -3527,7 +3527,7 @@ "type": "string" }, "path": { - "$ref": "#/definitions/AbsolutePathBuf" + "$ref": "#/definitions/ApiPathString" }, "type": { "enum": [ diff --git a/codex-rs/app-server-protocol/schema/json/v2/ItemCompletedNotification.json b/codex-rs/app-server-protocol/schema/json/v2/ItemCompletedNotification.json index 30f094399d..d5b5b311da 100644 --- a/codex-rs/app-server-protocol/schema/json/v2/ItemCompletedNotification.json +++ b/codex-rs/app-server-protocol/schema/json/v2/ItemCompletedNotification.json @@ -85,7 +85,7 @@ "type": "string" }, "path": { - "$ref": "#/definitions/AbsolutePathBuf" + "$ref": "#/definitions/ApiPathString" }, "type": { "enum": [ diff --git a/codex-rs/app-server-protocol/schema/json/v2/ItemStartedNotification.json b/codex-rs/app-server-protocol/schema/json/v2/ItemStartedNotification.json index 02375322b4..9585ee315b 100644 --- a/codex-rs/app-server-protocol/schema/json/v2/ItemStartedNotification.json +++ b/codex-rs/app-server-protocol/schema/json/v2/ItemStartedNotification.json @@ -85,7 +85,7 @@ "type": "string" }, "path": { - "$ref": "#/definitions/AbsolutePathBuf" + "$ref": "#/definitions/ApiPathString" }, "type": { "enum": [ diff --git a/codex-rs/app-server-protocol/schema/json/v2/ReviewStartResponse.json b/codex-rs/app-server-protocol/schema/json/v2/ReviewStartResponse.json index 36e95dbad5..124f9f82f3 100644 --- a/codex-rs/app-server-protocol/schema/json/v2/ReviewStartResponse.json +++ b/codex-rs/app-server-protocol/schema/json/v2/ReviewStartResponse.json @@ -222,7 +222,7 @@ "type": "string" }, "path": { - "$ref": "#/definitions/AbsolutePathBuf" + "$ref": "#/definitions/ApiPathString" }, "type": { "enum": [ diff --git a/codex-rs/app-server-protocol/schema/json/v2/ThreadForkResponse.json b/codex-rs/app-server-protocol/schema/json/v2/ThreadForkResponse.json index 048c8eac60..164d7de103 100644 --- a/codex-rs/app-server-protocol/schema/json/v2/ThreadForkResponse.json +++ b/codex-rs/app-server-protocol/schema/json/v2/ThreadForkResponse.json @@ -304,7 +304,7 @@ "type": "string" }, "path": { - "$ref": "#/definitions/AbsolutePathBuf" + "$ref": "#/definitions/ApiPathString" }, "type": { "enum": [ diff --git a/codex-rs/app-server-protocol/schema/json/v2/ThreadListResponse.json b/codex-rs/app-server-protocol/schema/json/v2/ThreadListResponse.json index 2f943a3e41..c24ef52682 100644 --- a/codex-rs/app-server-protocol/schema/json/v2/ThreadListResponse.json +++ b/codex-rs/app-server-protocol/schema/json/v2/ThreadListResponse.json @@ -225,7 +225,7 @@ "type": "string" }, "path": { - "$ref": "#/definitions/AbsolutePathBuf" + "$ref": "#/definitions/ApiPathString" }, "type": { "enum": [ diff --git a/codex-rs/app-server-protocol/schema/json/v2/ThreadMetadataUpdateResponse.json b/codex-rs/app-server-protocol/schema/json/v2/ThreadMetadataUpdateResponse.json index ff34fc25f7..91210938f2 100644 --- a/codex-rs/app-server-protocol/schema/json/v2/ThreadMetadataUpdateResponse.json +++ b/codex-rs/app-server-protocol/schema/json/v2/ThreadMetadataUpdateResponse.json @@ -225,7 +225,7 @@ "type": "string" }, "path": { - "$ref": "#/definitions/AbsolutePathBuf" + "$ref": "#/definitions/ApiPathString" }, "type": { "enum": [ diff --git a/codex-rs/app-server-protocol/schema/json/v2/ThreadReadResponse.json b/codex-rs/app-server-protocol/schema/json/v2/ThreadReadResponse.json index cd72fdd0c7..f235b28a1d 100644 --- a/codex-rs/app-server-protocol/schema/json/v2/ThreadReadResponse.json +++ b/codex-rs/app-server-protocol/schema/json/v2/ThreadReadResponse.json @@ -225,7 +225,7 @@ "type": "string" }, "path": { - "$ref": "#/definitions/AbsolutePathBuf" + "$ref": "#/definitions/ApiPathString" }, "type": { "enum": [ diff --git a/codex-rs/app-server-protocol/schema/json/v2/ThreadResumeResponse.json b/codex-rs/app-server-protocol/schema/json/v2/ThreadResumeResponse.json index 20a2407a12..eeea70a8a2 100644 --- a/codex-rs/app-server-protocol/schema/json/v2/ThreadResumeResponse.json +++ b/codex-rs/app-server-protocol/schema/json/v2/ThreadResumeResponse.json @@ -304,7 +304,7 @@ "type": "string" }, "path": { - "$ref": "#/definitions/AbsolutePathBuf" + "$ref": "#/definitions/ApiPathString" }, "type": { "enum": [ diff --git a/codex-rs/app-server-protocol/schema/json/v2/ThreadRollbackResponse.json b/codex-rs/app-server-protocol/schema/json/v2/ThreadRollbackResponse.json index 48996c3d51..43b5724629 100644 --- a/codex-rs/app-server-protocol/schema/json/v2/ThreadRollbackResponse.json +++ b/codex-rs/app-server-protocol/schema/json/v2/ThreadRollbackResponse.json @@ -225,7 +225,7 @@ "type": "string" }, "path": { - "$ref": "#/definitions/AbsolutePathBuf" + "$ref": "#/definitions/ApiPathString" }, "type": { "enum": [ diff --git a/codex-rs/app-server-protocol/schema/json/v2/ThreadStartResponse.json b/codex-rs/app-server-protocol/schema/json/v2/ThreadStartResponse.json index 8ac19dcc9f..6f68740ef6 100644 --- a/codex-rs/app-server-protocol/schema/json/v2/ThreadStartResponse.json +++ b/codex-rs/app-server-protocol/schema/json/v2/ThreadStartResponse.json @@ -304,7 +304,7 @@ "type": "string" }, "path": { - "$ref": "#/definitions/AbsolutePathBuf" + "$ref": "#/definitions/ApiPathString" }, "type": { "enum": [ diff --git a/codex-rs/app-server-protocol/schema/json/v2/ThreadStartedNotification.json b/codex-rs/app-server-protocol/schema/json/v2/ThreadStartedNotification.json index 7514426e95..af7015dbf7 100644 --- a/codex-rs/app-server-protocol/schema/json/v2/ThreadStartedNotification.json +++ b/codex-rs/app-server-protocol/schema/json/v2/ThreadStartedNotification.json @@ -225,7 +225,7 @@ "type": "string" }, "path": { - "$ref": "#/definitions/AbsolutePathBuf" + "$ref": "#/definitions/ApiPathString" }, "type": { "enum": [ diff --git a/codex-rs/app-server-protocol/schema/json/v2/ThreadUnarchiveResponse.json b/codex-rs/app-server-protocol/schema/json/v2/ThreadUnarchiveResponse.json index d9a007b602..a023d3f05e 100644 --- a/codex-rs/app-server-protocol/schema/json/v2/ThreadUnarchiveResponse.json +++ b/codex-rs/app-server-protocol/schema/json/v2/ThreadUnarchiveResponse.json @@ -225,7 +225,7 @@ "type": "string" }, "path": { - "$ref": "#/definitions/AbsolutePathBuf" + "$ref": "#/definitions/ApiPathString" }, "type": { "enum": [ diff --git a/codex-rs/app-server-protocol/schema/json/v2/TurnCompletedNotification.json b/codex-rs/app-server-protocol/schema/json/v2/TurnCompletedNotification.json index 03499178d9..452ba30b25 100644 --- a/codex-rs/app-server-protocol/schema/json/v2/TurnCompletedNotification.json +++ b/codex-rs/app-server-protocol/schema/json/v2/TurnCompletedNotification.json @@ -222,7 +222,7 @@ "type": "string" }, "path": { - "$ref": "#/definitions/AbsolutePathBuf" + "$ref": "#/definitions/ApiPathString" }, "type": { "enum": [ diff --git a/codex-rs/app-server-protocol/schema/json/v2/TurnStartResponse.json b/codex-rs/app-server-protocol/schema/json/v2/TurnStartResponse.json index 9e2830ced2..e1a4434826 100644 --- a/codex-rs/app-server-protocol/schema/json/v2/TurnStartResponse.json +++ b/codex-rs/app-server-protocol/schema/json/v2/TurnStartResponse.json @@ -222,7 +222,7 @@ "type": "string" }, "path": { - "$ref": "#/definitions/AbsolutePathBuf" + "$ref": "#/definitions/ApiPathString" }, "type": { "enum": [ diff --git a/codex-rs/app-server-protocol/schema/json/v2/TurnStartedNotification.json b/codex-rs/app-server-protocol/schema/json/v2/TurnStartedNotification.json index 3c35174867..d82418287a 100644 --- a/codex-rs/app-server-protocol/schema/json/v2/TurnStartedNotification.json +++ b/codex-rs/app-server-protocol/schema/json/v2/TurnStartedNotification.json @@ -222,7 +222,7 @@ "type": "string" }, "path": { - "$ref": "#/definitions/AbsolutePathBuf" + "$ref": "#/definitions/ApiPathString" }, "type": { "enum": [ diff --git a/codex-rs/app-server-protocol/schema/typescript/v2/CommandAction.ts b/codex-rs/app-server-protocol/schema/typescript/v2/CommandAction.ts index a17fb06a0c..d63e2c7209 100644 --- a/codex-rs/app-server-protocol/schema/typescript/v2/CommandAction.ts +++ b/codex-rs/app-server-protocol/schema/typescript/v2/CommandAction.ts @@ -1,6 +1,6 @@ // GENERATED CODE! DO NOT MODIFY BY HAND! // This file was generated by [ts-rs](https://github.com/Aleph-Alpha/ts-rs). Do not edit this file manually. -import type { AbsolutePathBuf } from "../AbsolutePathBuf"; +import type { ApiPathString } from "../ApiPathString"; -export type CommandAction = { "type": "read", command: string, name: string, path: AbsolutePathBuf, } | { "type": "listFiles", command: string, path: string | null, } | { "type": "search", command: string, query: string | null, path: string | null, } | { "type": "unknown", command: string, }; +export type CommandAction = { "type": "read", command: string, name: string, path: ApiPathString, } | { "type": "listFiles", command: string, path: string | null, } | { "type": "search", command: string, query: string | null, path: string | null, } | { "type": "unknown", command: string, }; diff --git a/codex-rs/app-server-protocol/src/protocol/item_builders.rs b/codex-rs/app-server-protocol/src/protocol/item_builders.rs index 096072adf2..f66691df6a 100644 --- a/codex-rs/app-server-protocol/src/protocol/item_builders.rs +++ b/codex-rs/app-server-protocol/src/protocol/item_builders.rs @@ -45,17 +45,12 @@ use std::path::PathBuf; clippy::expect_used, reason = "command event paths are absolute and have an inferred convention" )] -fn command_cwd_for_api_and_actions(cwd: &PathUri) -> (ApiPathString, AbsolutePathBuf) { +fn command_cwd_for_api(cwd: &PathUri) -> ApiPathString { let convention = cwd .infer_path_convention() .expect("command cwd should have a native path convention"); - let api_cwd = ApiPathString::from_path_uri(cwd, convention) - .expect("command cwd should render using its native path convention"); - let host_cwd = ApiPathString::from_path_uri(cwd, PathConvention::native()) - .expect("command cwd should render using the app-server host path convention"); - let host_cwd = AbsolutePathBuf::try_from(host_cwd.into_string()) - .expect("projected command cwd should be absolute on the app-server host"); - (api_cwd, host_cwd) + ApiPathString::from_path_uri(cwd, convention) + .expect("command cwd should render using its native path convention") } fn api_path_from_host(cwd: &AbsolutePathBuf) -> ApiPathString { @@ -91,7 +86,7 @@ pub fn build_file_change_end_item(payload: &PatchApplyEndEvent) -> ThreadItem { pub fn build_command_execution_approval_request_item( payload: &ExecApprovalRequestEvent, ) -> ThreadItem { - let (cwd, host_cwd) = command_cwd_for_api_and_actions(&payload.cwd); + let cwd = command_cwd_for_api(&payload.cwd); ThreadItem::CommandExecution { id: payload.call_id.clone(), command: shlex_join(&payload.command), @@ -103,7 +98,7 @@ pub fn build_command_execution_approval_request_item( .parsed_cmd .iter() .cloned() - .map(|parsed| CommandAction::from_core_with_cwd(parsed, &host_cwd)) + .map(|parsed| CommandAction::from_core_with_cwd_uri(parsed, &payload.cwd)) .collect(), aggregated_output: None, exit_code: None, @@ -112,7 +107,7 @@ pub fn build_command_execution_approval_request_item( } pub fn build_command_execution_begin_item(payload: &ExecCommandBeginEvent) -> ThreadItem { - let (cwd, host_cwd) = command_cwd_for_api_and_actions(&payload.cwd); + let cwd = command_cwd_for_api(&payload.cwd); ThreadItem::CommandExecution { id: payload.call_id.clone(), command: shlex_join(&payload.command), @@ -124,7 +119,7 @@ pub fn build_command_execution_begin_item(payload: &ExecCommandBeginEvent) -> Th .parsed_cmd .iter() .cloned() - .map(|parsed| CommandAction::from_core_with_cwd(parsed, &host_cwd)) + .map(|parsed| CommandAction::from_core_with_cwd_uri(parsed, &payload.cwd)) .collect(), aggregated_output: None, exit_code: None, @@ -139,7 +134,7 @@ pub fn build_command_execution_end_item(payload: &ExecCommandEndEvent) -> Thread Some(payload.aggregated_output.clone()) }; let duration_ms = i64::try_from(payload.duration.as_millis()).unwrap_or(i64::MAX); - let (cwd, host_cwd) = command_cwd_for_api_and_actions(&payload.cwd); + let cwd = command_cwd_for_api(&payload.cwd); ThreadItem::CommandExecution { id: payload.call_id.clone(), @@ -152,7 +147,7 @@ pub fn build_command_execution_end_item(payload: &ExecCommandEndEvent) -> Thread .parsed_cmd .iter() .cloned() - .map(|parsed| CommandAction::from_core_with_cwd(parsed, &host_cwd)) + .map(|parsed| CommandAction::from_core_with_cwd_uri(parsed, &payload.cwd)) .collect(), aggregated_output, exit_code: Some(payload.exit_code), diff --git a/codex-rs/app-server-protocol/src/protocol/v2/item.rs b/codex-rs/app-server-protocol/src/protocol/v2/item.rs index 0b3798ea7f..febf7f90ba 100644 --- a/codex-rs/app-server-protocol/src/protocol/v2/item.rs +++ b/codex-rs/app-server-protocol/src/protocol/v2/item.rs @@ -32,6 +32,8 @@ use codex_protocol::protocol::ReviewDecision as CoreReviewDecision; use codex_protocol::protocol::SubAgentActivityKind as CoreSubAgentActivityKind; use codex_utils_absolute_path::AbsolutePathBuf; use codex_utils_path_uri::ApiPathString; +use codex_utils_path_uri::PathConvention; +use codex_utils_path_uri::PathUri; use schemars::JsonSchema; use serde::Deserialize; use serde::Serialize; @@ -110,7 +112,7 @@ pub enum CommandAction { Read { command: String, name: String, - path: AbsolutePathBuf, + path: ApiPathString, }, ListFiles { command: String, @@ -174,7 +176,7 @@ impl CommandAction { } => CoreParsedCommand::Read { cmd, name, - path: path.into_path_buf(), + path: PathBuf::from(path.into_string()), }, CommandAction::ListFiles { command: cmd, path } => { CoreParsedCommand::ListFiles { cmd, path } @@ -189,12 +191,21 @@ impl CommandAction { } pub fn from_core_with_cwd(value: CoreParsedCommand, cwd: &AbsolutePathBuf) -> Self { + Self::from_core_with_cwd_uri(value, &PathUri::from_abs_path(cwd)) + } + + pub fn from_core_with_cwd_uri(value: CoreParsedCommand, cwd: &PathUri) -> Self { match value { - CoreParsedCommand::Read { cmd, name, path } => CommandAction::Read { - command: cmd, - name, - path: cwd.join(path), - }, + CoreParsedCommand::Read { cmd, name, path } => { + let Some(path) = command_action_path(cwd, &path) else { + return CommandAction::Unknown { command: cmd }; + }; + CommandAction::Read { + command: cmd, + name, + path, + } + } CoreParsedCommand::ListFiles { cmd, path } => { CommandAction::ListFiles { command: cmd, path } } @@ -208,6 +219,21 @@ impl CommandAction { } } +fn command_action_path(cwd: &PathUri, path: &std::path::Path) -> Option { + let convention = cwd.infer_path_convention()?; + let path = path.to_string_lossy(); + if let Ok(path) = ApiPathString::from_native_absolute_path(&path, convention) { + return Some(path); + } + + let relative_path = match convention { + PathConvention::Posix => path.into_owned(), + PathConvention::Windows => path.replace('\\', "/"), + }; + let path = cwd.join(&relative_path).ok()?; + ApiPathString::from_path_uri(&path, convention).ok() +} + #[derive(Serialize, Deserialize, Debug, Clone, PartialEq, JsonSchema, TS)] #[serde(tag = "type", rename_all = "camelCase")] #[ts(tag = "type")] diff --git a/codex-rs/app-server-protocol/src/protocol/v2/tests.rs b/codex-rs/app-server-protocol/src/protocol/v2/tests.rs index e2ef111399..72e4575e2b 100644 --- a/codex-rs/app-server-protocol/src/protocol/v2/tests.rs +++ b/codex-rs/app-server-protocol/src/protocol/v2/tests.rs @@ -21,6 +21,7 @@ use codex_protocol::models::ImageDetail; use codex_protocol::models::MessagePhase; use codex_protocol::models::NetworkPermissions as CoreNetworkPermissions; use codex_protocol::models::WebSearchAction as CoreWebSearchAction; +use codex_protocol::parse_command::ParsedCommand as CoreParsedCommand; use codex_protocol::permissions::FileSystemAccessMode as CoreFileSystemAccessMode; use codex_protocol::permissions::FileSystemPath as CoreFileSystemPath; use codex_protocol::permissions::FileSystemSandboxEntry as CoreFileSystemSandboxEntry; @@ -36,6 +37,8 @@ use codex_utils_absolute_path::AbsolutePathBuf; use codex_utils_absolute_path::test_support::PathBufExt; use codex_utils_absolute_path::test_support::test_path_buf; use codex_utils_path_uri::ApiPathString; +use codex_utils_path_uri::PathConvention; +use codex_utils_path_uri::PathUri; use pretty_assertions::assert_eq; use serde_json::Value as JsonValue; use serde_json::json; @@ -59,6 +62,34 @@ fn test_absolute_path() -> AbsolutePathBuf { absolute_path("readable") } +#[test] +fn command_action_read_resolves_paths_using_the_environment_convention() { + let cwd = PathUri::parse("file:///C:/codex%20runtime").expect("Windows cwd URI"); + for (path, expected) in [ + (r"C:\input\file.txt", r"C:\input\file.txt"), + (r"relative\file.txt", r"C:\codex runtime\relative\file.txt"), + ] { + let action = CommandAction::from_core_with_cwd_uri( + CoreParsedCommand::Read { + cmd: "Get-Content".to_string(), + name: "file.txt".to_string(), + path: PathBuf::from(path), + }, + &cwd, + ); + + assert_eq!( + action, + CommandAction::Read { + command: "Get-Content".to_string(), + name: "file.txt".to_string(), + path: ApiPathString::from_native_absolute_path(expected, PathConvention::Windows,) + .expect("absolute Windows API path"), + } + ); + } +} + #[test] fn thread_sources_round_trip_as_scalar_labels() { for (source, label) in [ diff --git a/codex-rs/app-server/README.md b/codex-rs/app-server/README.md index 95e8595bc3..9a634ed315 100644 --- a/codex-rs/app-server/README.md +++ b/codex-rs/app-server/README.md @@ -1390,6 +1390,7 @@ There are additional item-specific events: - `item/commandExecution/outputDelta` — streams stdout/stderr for the command; append deltas in order to render live output alongside `aggregatedOutput` in the final item. Final `commandExecution` items include parsed `commandActions`, `status`, `exitCode`, and `durationMs` so the UI can summarize what ran and whether it succeeded. - `cwd` uses the selected execution environment's native path syntax. +- Read paths in `commandActions` use that same path syntax. #### fileChange diff --git a/codex-rs/app-server/src/bespoke_event_handling.rs b/codex-rs/app-server/src/bespoke_event_handling.rs index 3c1978ca31..5312146938 100644 --- a/codex-rs/app-server/src/bespoke_event_handling.rs +++ b/codex-rs/app-server/src/bespoke_event_handling.rs @@ -114,7 +114,6 @@ use codex_sandboxing::policy_transforms::intersect_permission_profiles; use codex_shell_command::parse_command::shlex_join; use codex_utils_absolute_path::AbsolutePathBuf; use codex_utils_path_uri::ApiPathString; -use codex_utils_path_uri::PathConvention; use std::collections::HashMap; use std::sync::Arc; use std::time::SystemTime; @@ -567,22 +566,17 @@ pub(crate) async fn apply_bespoke_event_handling( clippy::expect_used, reason = "command approval paths are absolute and have an inferred convention" )] - let (api_cwd, cwd) = { + let api_cwd = { let convention = cwd .infer_path_convention() .expect("command cwd should have a native path convention"); - let api_cwd = ApiPathString::from_path_uri(&cwd, convention) - .expect("command cwd should render using its native path convention"); - let host_cwd = ApiPathString::from_path_uri(&cwd, PathConvention::native()) - .expect("command cwd should render using the app-server host path convention"); - let host_cwd = AbsolutePathBuf::try_from(host_cwd.into_string()) - .expect("projected command cwd should be absolute on the app-server host"); - (api_cwd, host_cwd) + ApiPathString::from_path_uri(&cwd, convention) + .expect("command cwd should render using its native path convention") }; let command_actions = parsed_cmd .iter() .cloned() - .map(|parsed| V2ParsedCommand::from_core_with_cwd(parsed, &cwd)) + .map(|parsed| V2ParsedCommand::from_core_with_cwd_uri(parsed, &cwd)) .collect::>(); let presentation = if let Some(network_approval_context) = network_approval_context.map(V2NetworkApprovalContext::from) @@ -2218,6 +2212,7 @@ mod tests { use codex_utils_absolute_path::AbsolutePathBuf; use codex_utils_absolute_path::test_support::PathBufExt; use codex_utils_absolute_path::test_support::test_path_buf; + use codex_utils_path_uri::PathConvention; use core_test_support::load_default_config_for_test; use pretty_assertions::assert_eq; use serde_json::json; diff --git a/codex-rs/utils/path-uri/src/api_path_string.rs b/codex-rs/utils/path-uri/src/api_path_string.rs index a8675c9a73..50081f8892 100644 --- a/codex-rs/utils/path-uri/src/api_path_string.rs +++ b/codex-rs/utils/path-uri/src/api_path_string.rs @@ -38,6 +38,25 @@ impl ApiPathString { Self(path.to_string_lossy().into_owned()) } + /// Parses an absolute native path using the supplied convention. + /// + /// Unlike [`Self::from_abs_path`], this does not interpret the path using + /// the operating system running Codex, so it can construct foreign paths. + pub fn from_native_absolute_path( + path: &str, + convention: PathConvention, + ) -> Result { + let path_uri = match convention { + PathConvention::Posix => parse_posix_path(path), + PathConvention::Windows => parse_windows_path(path), + } + .ok_or_else(|| ApiPathStringError::InvalidNativePath { + path: path.to_string(), + convention, + })?; + Self::from_path_uri(&path_uri, convention) + } + /// Renders a path URI using the requested native path convention. /// /// Rendering fails when the URI shape does not match the convention, such diff --git a/codex-rs/utils/path-uri/src/api_path_string_tests.rs b/codex-rs/utils/path-uri/src/api_path_string_tests.rs index 3fbdb3dec0..12a517371e 100644 --- a/codex-rs/utils/path-uri/src/api_path_string_tests.rs +++ b/codex-rs/utils/path-uri/src/api_path_string_tests.rs @@ -449,6 +449,33 @@ fn renders_an_absolute_path_using_the_host_convention() { ); } +#[test] +fn parses_foreign_absolute_paths_using_the_explicit_convention() { + for (path, convention) in [ + ("/workspace/a file.rs", PathConvention::Posix), + (r"C:\workspace\a file.rs", PathConvention::Windows), + (r"\\server\share\a file.rs", PathConvention::Windows), + ] { + assert_eq!( + ApiPathString::from_native_absolute_path(path, convention), + Ok(ApiPathString(path.to_string())) + ); + } +} + +#[test] +fn rejects_relative_native_paths() { + for convention in [PathConvention::Posix, PathConvention::Windows] { + assert_eq!( + ApiPathString::from_native_absolute_path("relative/path", convention), + Err(ApiPathStringError::InvalidNativePath { + path: "relative/path".to_string(), + convention, + }) + ); + } +} + #[cfg(windows)] #[test] fn renders_native_non_unicode_windows_fallback_lossily() {