feat(app-server): add fs/canonicalize

This commit is contained in:
Philipp Spiess
2026-06-11 16:29:21 +02:00
parent 7c2394808e
commit 3ddf358572
16 changed files with 410 additions and 5 deletions
@@ -800,6 +800,23 @@
],
"type": "object"
},
"FsCanonicalizeParams": {
"description": "Resolve an absolute path to its canonical filesystem path.",
"properties": {
"path": {
"allOf": [
{
"$ref": "#/definitions/AbsolutePathBuf"
}
],
"description": "Absolute path to canonicalize. The path must exist."
}
},
"required": [
"path"
],
"type": "object"
},
"FsCopyParams": {
"description": "Copy a file or directory tree on the host filesystem.",
"properties": {
@@ -5366,6 +5383,30 @@
"title": "Fs/createDirectoryRequest",
"type": "object"
},
{
"properties": {
"id": {
"$ref": "#/definitions/RequestId"
},
"method": {
"enum": [
"fs/canonicalize"
],
"title": "Fs/canonicalizeRequestMethod",
"type": "string"
},
"params": {
"$ref": "#/definitions/FsCanonicalizeParams"
}
},
"required": [
"id",
"method",
"params"
],
"title": "Fs/canonicalizeRequest",
"type": "object"
},
{
"properties": {
"id": {
@@ -1165,6 +1165,30 @@
"title": "Fs/createDirectoryRequest",
"type": "object"
},
{
"properties": {
"id": {
"$ref": "#/definitions/v2/RequestId"
},
"method": {
"enum": [
"fs/canonicalize"
],
"title": "Fs/canonicalizeRequestMethod",
"type": "string"
},
"params": {
"$ref": "#/definitions/v2/FsCanonicalizeParams"
}
},
"required": [
"id",
"method",
"params"
],
"title": "Fs/canonicalizeRequest",
"type": "object"
},
{
"properties": {
"id": {
@@ -9269,6 +9293,44 @@
],
"type": "string"
},
"FsCanonicalizeParams": {
"$schema": "http://json-schema.org/draft-07/schema#",
"description": "Resolve an absolute path to its canonical filesystem path.",
"properties": {
"path": {
"allOf": [
{
"$ref": "#/definitions/v2/AbsolutePathBuf"
}
],
"description": "Absolute path to canonicalize. The path must exist."
}
},
"required": [
"path"
],
"title": "FsCanonicalizeParams",
"type": "object"
},
"FsCanonicalizeResponse": {
"$schema": "http://json-schema.org/draft-07/schema#",
"description": "Canonical path returned by `fs/canonicalize`.",
"properties": {
"path": {
"allOf": [
{
"$ref": "#/definitions/v2/AbsolutePathBuf"
}
],
"description": "Canonical absolute path with symbolic links resolved."
}
},
"required": [
"path"
],
"title": "FsCanonicalizeResponse",
"type": "object"
},
"FsChangedNotification": {
"$schema": "http://json-schema.org/draft-07/schema#",
"description": "Filesystem watch notification emitted for `fs/watch` subscribers.",
@@ -2107,6 +2107,30 @@
"title": "Fs/createDirectoryRequest",
"type": "object"
},
{
"properties": {
"id": {
"$ref": "#/definitions/RequestId"
},
"method": {
"enum": [
"fs/canonicalize"
],
"title": "Fs/canonicalizeRequestMethod",
"type": "string"
},
"params": {
"$ref": "#/definitions/FsCanonicalizeParams"
}
},
"required": [
"id",
"method",
"params"
],
"title": "Fs/canonicalizeRequest",
"type": "object"
},
{
"properties": {
"id": {
@@ -5591,6 +5615,44 @@
],
"type": "string"
},
"FsCanonicalizeParams": {
"$schema": "http://json-schema.org/draft-07/schema#",
"description": "Resolve an absolute path to its canonical filesystem path.",
"properties": {
"path": {
"allOf": [
{
"$ref": "#/definitions/AbsolutePathBuf"
}
],
"description": "Absolute path to canonicalize. The path must exist."
}
},
"required": [
"path"
],
"title": "FsCanonicalizeParams",
"type": "object"
},
"FsCanonicalizeResponse": {
"$schema": "http://json-schema.org/draft-07/schema#",
"description": "Canonical path returned by `fs/canonicalize`.",
"properties": {
"path": {
"allOf": [
{
"$ref": "#/definitions/AbsolutePathBuf"
}
],
"description": "Canonical absolute path with symbolic links resolved."
}
},
"required": [
"path"
],
"title": "FsCanonicalizeResponse",
"type": "object"
},
"FsChangedNotification": {
"$schema": "http://json-schema.org/draft-07/schema#",
"description": "Filesystem watch notification emitted for `fs/watch` subscribers.",
@@ -0,0 +1,25 @@
{
"$schema": "http://json-schema.org/draft-07/schema#",
"definitions": {
"AbsolutePathBuf": {
"description": "A path that is guaranteed to be absolute and normalized (though it is not guaranteed to be canonicalized or exist on the filesystem).\n\nIMPORTANT: When deserializing an `AbsolutePathBuf`, a base path must be set using [AbsolutePathBufGuard::new]. If no base path is set, the deserialization will fail unless the path being deserialized is already absolute.",
"type": "string"
}
},
"description": "Resolve an absolute path to its canonical filesystem path.",
"properties": {
"path": {
"allOf": [
{
"$ref": "#/definitions/AbsolutePathBuf"
}
],
"description": "Absolute path to canonicalize. The path must exist."
}
},
"required": [
"path"
],
"title": "FsCanonicalizeParams",
"type": "object"
}
@@ -0,0 +1,25 @@
{
"$schema": "http://json-schema.org/draft-07/schema#",
"definitions": {
"AbsolutePathBuf": {
"description": "A path that is guaranteed to be absolute and normalized (though it is not guaranteed to be canonicalized or exist on the filesystem).\n\nIMPORTANT: When deserializing an `AbsolutePathBuf`, a base path must be set using [AbsolutePathBufGuard::new]. If no base path is set, the deserialization will fail unless the path being deserialized is already absolute.",
"type": "string"
}
},
"description": "Canonical path returned by `fs/canonicalize`.",
"properties": {
"path": {
"allOf": [
{
"$ref": "#/definitions/AbsolutePathBuf"
}
],
"description": "Canonical absolute path with symbolic links resolved."
}
},
"required": [
"path"
],
"title": "FsCanonicalizeResponse",
"type": "object"
}
File diff suppressed because one or more lines are too long
@@ -0,0 +1,13 @@
// 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";
/**
* Resolve an absolute path to its canonical filesystem path.
*/
export type FsCanonicalizeParams = {
/**
* Absolute path to canonicalize. The path must exist.
*/
path: AbsolutePathBuf, };
@@ -0,0 +1,13 @@
// 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";
/**
* Canonical path returned by `fs/canonicalize`.
*/
export type FsCanonicalizeResponse = {
/**
* Canonical absolute path with symbolic links resolved.
*/
path: AbsolutePathBuf, };
@@ -121,6 +121,8 @@ export type { FileSystemSandboxEntry } from "./FileSystemSandboxEntry";
export type { FileSystemSpecialPath } from "./FileSystemSpecialPath";
export type { FileUpdateChange } from "./FileUpdateChange";
export type { ForcedChatgptWorkspaceIds } from "./ForcedChatgptWorkspaceIds";
export type { FsCanonicalizeParams } from "./FsCanonicalizeParams";
export type { FsCanonicalizeResponse } from "./FsCanonicalizeResponse";
export type { FsChangedNotification } from "./FsChangedNotification";
export type { FsCopyParams } from "./FsCopyParams";
export type { FsCopyResponse } from "./FsCopyResponse";
@@ -739,6 +739,11 @@ client_request_definitions! {
serialization: None,
response: v2::FsCreateDirectoryResponse,
},
FsCanonicalize => "fs/canonicalize" {
params: v2::FsCanonicalizeParams,
serialization: None,
response: v2::FsCanonicalizeResponse,
},
FsGetMetadata => "fs/getMetadata" {
params: v2::FsGetMetadataParams,
serialization: None,
@@ -2848,6 +2853,27 @@ mod tests {
Ok(())
}
#[test]
fn serialize_fs_canonicalize() -> Result<()> {
let request = ClientRequest::FsCanonicalize {
request_id: RequestId::Integer(10),
params: v2::FsCanonicalizeParams {
path: absolute_path("tmp/example"),
},
};
assert_eq!(
json!({
"method": "fs/canonicalize",
"id": 10,
"params": {
"path": absolute_path_string("tmp/example")
}
}),
serde_json::to_value(&request)?,
);
Ok(())
}
#[test]
fn serialize_fs_get_metadata() -> Result<()> {
let request = ClientRequest::FsGetMetadata {
@@ -57,6 +57,24 @@ pub struct FsCreateDirectoryParams {
#[ts(export_to = "v2/")]
pub struct FsCreateDirectoryResponse {}
/// Resolve an absolute path to its canonical filesystem path.
#[derive(Serialize, Deserialize, Debug, Clone, PartialEq, Eq, JsonSchema, TS)]
#[serde(rename_all = "camelCase")]
#[ts(export_to = "v2/")]
pub struct FsCanonicalizeParams {
/// Absolute path to canonicalize. The path must exist.
pub path: AbsolutePathBuf,
}
/// Canonical path returned by `fs/canonicalize`.
#[derive(Serialize, Deserialize, Debug, Clone, PartialEq, Eq, JsonSchema, TS)]
#[serde(rename_all = "camelCase")]
#[ts(export_to = "v2/")]
pub struct FsCanonicalizeResponse {
/// Canonical absolute path with symbolic links resolved.
pub path: AbsolutePathBuf,
}
/// Request metadata for an absolute path.
#[derive(Serialize, Deserialize, Debug, Clone, PartialEq, Eq, JsonSchema, TS)]
#[serde(rename_all = "camelCase")]
+11 -4
View File
@@ -184,6 +184,7 @@ Example with notification opt-out:
- `fs/readFile` — read an absolute file path and return `{ dataBase64 }`.
- `fs/writeFile` — write an absolute file path from base64-encoded `{ dataBase64 }`; returns `{}`.
- `fs/createDirectory` — create an absolute directory path; `recursive` defaults to `true`.
- `fs/canonicalize` — resolve an existing absolute path to its canonical absolute path, including symbolic links.
- `fs/getMetadata` — return metadata for an absolute path: `isDirectory`, `isFile`, `isSymlink`, `createdAtMs`, and `modifiedAtMs`.
- `fs/readDirectory` — list direct child entries for an absolute directory path; each entry contains `fileName`, `isDirectory`, and `isFile`, and `fileName` is just the child name, not a path.
- `fs/remove` — remove an absolute file or directory tree; `recursive` and `force` default to `true`.
@@ -1189,20 +1190,26 @@ All filesystem paths in this section must be absolute.
"dataBase64": "aGVsbG8="
} }
{ "id": 41, "result": {} }
{ "method": "fs/getMetadata", "id": 42, "params": {
"path": "/tmp/example/nested/note.txt"
{ "method": "fs/canonicalize", "id": 42, "params": {
"path": "/tmp/example/nested/../nested/note.txt"
} }
{ "id": 42, "result": {
"path": "/tmp/example/nested/note.txt"
} }
{ "method": "fs/getMetadata", "id": 43, "params": {
"path": "/tmp/example/nested/note.txt"
} }
{ "id": 43, "result": {
"isDirectory": false,
"isFile": true,
"isSymlink": false,
"createdAtMs": 1730910000000,
"modifiedAtMs": 1730910000000
} }
{ "method": "fs/readFile", "id": 43, "params": {
{ "method": "fs/readFile", "id": 44, "params": {
"path": "/tmp/example/nested/note.txt"
} }
{ "id": 43, "result": {
{ "id": 44, "result": {
"dataBase64": "aGVsbG8="
} }
```
@@ -997,6 +997,11 @@ impl MessageProcessor {
.create_directory(params)
.await
.map(|response| Some(response.into())),
ClientRequest::FsCanonicalize { params, .. } => self
.fs_processor
.canonicalize(params)
.await
.map(|response| Some(response.into())),
ClientRequest::FsGetMetadata { params, .. } => self
.fs_processor
.get_metadata(params)
@@ -4,6 +4,8 @@ use crate::fs_watch::FsWatchManager;
use crate::outgoing_message::ConnectionId;
use base64::Engine;
use base64::engine::general_purpose::STANDARD;
use codex_app_server_protocol::FsCanonicalizeParams;
use codex_app_server_protocol::FsCanonicalizeResponse;
use codex_app_server_protocol::FsCopyParams;
use codex_app_server_protocol::FsCopyResponse;
use codex_app_server_protocol::FsCreateDirectoryParams;
@@ -107,6 +109,18 @@ impl FsRequestProcessor {
Ok(FsCreateDirectoryResponse {})
}
pub(crate) async fn canonicalize(
&self,
params: FsCanonicalizeParams,
) -> Result<FsCanonicalizeResponse, JSONRPCErrorError> {
let path = self
.file_system()?
.canonicalize(&params.path, /*sandbox*/ None)
.await
.map_err(map_fs_error)?;
Ok(FsCanonicalizeResponse { path })
}
pub(crate) async fn get_metadata(
&self,
params: FsGetMetadataParams,
@@ -25,6 +25,7 @@ use codex_app_server_protocol::ConfigReadParams;
use codex_app_server_protocol::ConfigValueWriteParams;
use codex_app_server_protocol::ExperimentalFeatureListParams;
use codex_app_server_protocol::FeedbackUploadParams;
use codex_app_server_protocol::FsCanonicalizeParams;
use codex_app_server_protocol::FsCopyParams;
use codex_app_server_protocol::FsCreateDirectoryParams;
use codex_app_server_protocol::FsGetMetadataParams;
@@ -1150,6 +1151,14 @@ impl TestAppServer {
self.send_request("fs/createDirectory", params).await
}
pub async fn send_fs_canonicalize_request(
&mut self,
params: FsCanonicalizeParams,
) -> anyhow::Result<i64> {
let params = Some(serde_json::to_value(params)?);
self.send_request("fs/canonicalize", params).await
}
pub async fn send_fs_get_metadata_request(
&mut self,
params: FsGetMetadataParams,
+82
View File
@@ -4,6 +4,8 @@ use app_test_support::TestAppServer;
use app_test_support::to_response;
use base64::Engine;
use base64::engine::general_purpose::STANDARD;
use codex_app_server_protocol::FsCanonicalizeParams;
use codex_app_server_protocol::FsCanonicalizeResponse;
use codex_app_server_protocol::FsChangedNotification;
use codex_app_server_protocol::FsCopyParams;
use codex_app_server_protocol::FsGetMetadataResponse;
@@ -120,6 +122,64 @@ async fn fs_get_metadata_returns_only_used_fields() -> Result<()> {
Ok(())
}
#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
async fn fs_canonicalize_resolves_an_existing_path() -> Result<()> {
let codex_home = TempDir::new()?;
let directory = codex_home.path().join("parent");
let child = directory.join("child");
std::fs::create_dir_all(&child)?;
let path = child.join("..");
let mut mcp = initialized_mcp(&codex_home).await?;
let request_id = mcp
.send_fs_canonicalize_request(FsCanonicalizeParams {
path: absolute_path(path),
})
.await?;
let response: FsCanonicalizeResponse = to_response(
timeout(
DEFAULT_READ_TIMEOUT,
mcp.read_stream_until_response_message(RequestId::Integer(request_id)),
)
.await??,
)?;
assert_eq!(
response.path,
absolute_path(std::fs::canonicalize(directory)?)
);
Ok(())
}
#[cfg(unix)]
#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
async fn fs_canonicalize_resolves_a_symlink() -> Result<()> {
let codex_home = TempDir::new()?;
let target = codex_home.path().join("target");
let alias = codex_home.path().join("alias");
std::fs::create_dir(&target)?;
symlink(&target, &alias)?;
let mut mcp = initialized_mcp(&codex_home).await?;
let request_id = mcp
.send_fs_canonicalize_request(FsCanonicalizeParams {
path: absolute_path(alias),
})
.await?;
let response: FsCanonicalizeResponse = to_response(
timeout(
DEFAULT_READ_TIMEOUT,
mcp.read_stream_until_response_message(RequestId::Integer(request_id)),
)
.await??,
)?;
assert_eq!(response.path.as_path(), std::fs::canonicalize(target)?);
Ok(())
}
#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
async fn fs_methods_return_error_when_local_environment_is_disabled() -> Result<()> {
let codex_home = TempDir::new()?;
@@ -139,6 +199,18 @@ async fn fs_methods_return_error_when_local_environment_is_disabled() -> Result<
.await?;
expect_error_message(&mut mcp, read_id, "local filesystem is not configured").await?;
let canonicalize_id = mcp
.send_fs_canonicalize_request(FsCanonicalizeParams {
path: absolute_path(codex_home.path().to_path_buf()),
})
.await?;
expect_error_message(
&mut mcp,
canonicalize_id,
"local filesystem is not configured",
)
.await?;
Ok(())
}
@@ -443,6 +515,16 @@ async fn fs_methods_reject_relative_paths() -> Result<()> {
)
.await?;
let canonicalize_id = mcp
.send_raw_request("fs/canonicalize", Some(json!({ "path": "relative-dir" })))
.await?;
expect_error_message(
&mut mcp,
canonicalize_id,
"Invalid request: AbsolutePathBuf deserialized without a base path",
)
.await?;
let get_metadata_id = mcp
.send_raw_request("fs/getMetadata", Some(json!({ "path": "relative.txt" })))
.await?;