app-server: preserve command action paths

This commit is contained in:
Adam Perry
2026-06-16 17:47:01 +00:00
parent 7da4e6a9ae
commit a64231c138
29 changed files with 149 additions and 54 deletions
@@ -98,7 +98,7 @@
"type": "string"
},
"path": {
"$ref": "#/definitions/AbsolutePathBuf"
"$ref": "#/definitions/ApiPathString"
},
"type": {
"enum": [
@@ -782,7 +782,7 @@
"type": "string"
},
"path": {
"$ref": "#/definitions/AbsolutePathBuf"
"$ref": "#/definitions/ApiPathString"
},
"type": {
"enum": [
+1 -1
View File
@@ -167,7 +167,7 @@
"type": "string"
},
"path": {
"$ref": "#/definitions/AbsolutePathBuf"
"$ref": "#/definitions/ApiPathString"
},
"type": {
"enum": [
@@ -7234,7 +7234,7 @@
"type": "string"
},
"path": {
"$ref": "#/definitions/v2/AbsolutePathBuf"
"$ref": "#/definitions/v2/ApiPathString"
},
"type": {
"enum": [
@@ -3527,7 +3527,7 @@
"type": "string"
},
"path": {
"$ref": "#/definitions/AbsolutePathBuf"
"$ref": "#/definitions/ApiPathString"
},
"type": {
"enum": [
@@ -85,7 +85,7 @@
"type": "string"
},
"path": {
"$ref": "#/definitions/AbsolutePathBuf"
"$ref": "#/definitions/ApiPathString"
},
"type": {
"enum": [
@@ -85,7 +85,7 @@
"type": "string"
},
"path": {
"$ref": "#/definitions/AbsolutePathBuf"
"$ref": "#/definitions/ApiPathString"
},
"type": {
"enum": [
@@ -222,7 +222,7 @@
"type": "string"
},
"path": {
"$ref": "#/definitions/AbsolutePathBuf"
"$ref": "#/definitions/ApiPathString"
},
"type": {
"enum": [
@@ -304,7 +304,7 @@
"type": "string"
},
"path": {
"$ref": "#/definitions/AbsolutePathBuf"
"$ref": "#/definitions/ApiPathString"
},
"type": {
"enum": [
@@ -225,7 +225,7 @@
"type": "string"
},
"path": {
"$ref": "#/definitions/AbsolutePathBuf"
"$ref": "#/definitions/ApiPathString"
},
"type": {
"enum": [
@@ -225,7 +225,7 @@
"type": "string"
},
"path": {
"$ref": "#/definitions/AbsolutePathBuf"
"$ref": "#/definitions/ApiPathString"
},
"type": {
"enum": [
@@ -225,7 +225,7 @@
"type": "string"
},
"path": {
"$ref": "#/definitions/AbsolutePathBuf"
"$ref": "#/definitions/ApiPathString"
},
"type": {
"enum": [
@@ -304,7 +304,7 @@
"type": "string"
},
"path": {
"$ref": "#/definitions/AbsolutePathBuf"
"$ref": "#/definitions/ApiPathString"
},
"type": {
"enum": [
@@ -225,7 +225,7 @@
"type": "string"
},
"path": {
"$ref": "#/definitions/AbsolutePathBuf"
"$ref": "#/definitions/ApiPathString"
},
"type": {
"enum": [
@@ -304,7 +304,7 @@
"type": "string"
},
"path": {
"$ref": "#/definitions/AbsolutePathBuf"
"$ref": "#/definitions/ApiPathString"
},
"type": {
"enum": [
@@ -225,7 +225,7 @@
"type": "string"
},
"path": {
"$ref": "#/definitions/AbsolutePathBuf"
"$ref": "#/definitions/ApiPathString"
},
"type": {
"enum": [
@@ -225,7 +225,7 @@
"type": "string"
},
"path": {
"$ref": "#/definitions/AbsolutePathBuf"
"$ref": "#/definitions/ApiPathString"
},
"type": {
"enum": [
@@ -222,7 +222,7 @@
"type": "string"
},
"path": {
"$ref": "#/definitions/AbsolutePathBuf"
"$ref": "#/definitions/ApiPathString"
},
"type": {
"enum": [
@@ -222,7 +222,7 @@
"type": "string"
},
"path": {
"$ref": "#/definitions/AbsolutePathBuf"
"$ref": "#/definitions/ApiPathString"
},
"type": {
"enum": [
@@ -222,7 +222,7 @@
"type": "string"
},
"path": {
"$ref": "#/definitions/AbsolutePathBuf"
"$ref": "#/definitions/ApiPathString"
},
"type": {
"enum": [
@@ -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, };
@@ -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),
@@ -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<ApiPathString> {
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")]
@@ -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 [