From 765309d5a611ea02be842ead0ab1a2828196fae9 Mon Sep 17 00:00:00 2001 From: martinauyeung-oai Date: Thu, 18 Jun 2026 14:02:54 -0700 Subject: [PATCH 01/10] Emit Trusted MCP App Identity on Tool-Call Items (#27132) ## Summary - Add optional `appContext` to app-server MCP tool-call items with trusted `connectorId`, `linkId`, and `mcpAppResourceUri` metadata. - Preserve that context across tool-call events, persisted history, reconnects, and thread resume. - Keep the deprecated top-level `mcpAppResourceUri` temporarily for client migration. The consumer contract is `{ appContext: { connectorId, linkId, mcpAppResourceUri }, tool }`. ## Validation - Full GitHub Actions suite passes, including CLA, Bazel tests, clippy, release builds, and argument-comment lint. --------- Co-authored-by: martinauyeung-oai <280153141+martinauyeung-oai@users.noreply.github.com> --- .../analytics/src/analytics_client_tests.rs | 1 + .../schema/json/ServerNotification.json | 34 +++++++++++ .../codex_app_server_protocol.schemas.json | 34 +++++++++++ .../codex_app_server_protocol.v2.schemas.json | 34 +++++++++++ .../json/v2/ItemCompletedNotification.json | 34 +++++++++++ .../json/v2/ItemStartedNotification.json | 34 +++++++++++ .../schema/json/v2/ReviewStartResponse.json | 34 +++++++++++ .../schema/json/v2/ThreadForkResponse.json | 34 +++++++++++ .../schema/json/v2/ThreadListResponse.json | 34 +++++++++++ .../json/v2/ThreadMetadataUpdateResponse.json | 34 +++++++++++ .../schema/json/v2/ThreadReadResponse.json | 34 +++++++++++ .../schema/json/v2/ThreadResumeResponse.json | 34 +++++++++++ .../json/v2/ThreadRollbackResponse.json | 34 +++++++++++ .../schema/json/v2/ThreadStartResponse.json | 34 +++++++++++ .../json/v2/ThreadStartedNotification.json | 34 +++++++++++ .../json/v2/ThreadUnarchiveResponse.json | 34 +++++++++++ .../json/v2/TurnCompletedNotification.json | 34 +++++++++++ .../schema/json/v2/TurnStartResponse.json | 34 +++++++++++ .../json/v2/TurnStartedNotification.json | 34 +++++++++++ .../typescript/v2/McpToolCallAppContext.ts | 5 ++ .../schema/typescript/v2/ThreadItem.ts | 7 ++- .../schema/typescript/v2/index.ts | 1 + .../src/protocol/thread_history.rs | 28 +++++++++- .../src/protocol/v2/item.rs | 16 ++++++ .../src/protocol/v2/tests.rs | 53 ++++++++++++++++++ codex-rs/app-server/README.md | 2 +- .../thread_resume_redaction.rs | 13 +++++ .../app-server/tests/suite/v2/mcp_tool.rs | 1 + .../tests/suite/v2/thread_resume.rs | 25 ++++++++- codex-rs/core/src/mcp_tool_call.rs | 44 ++++++++++++--- codex-rs/core/src/mcp_tool_call_tests.rs | 56 ++++++++++++++++++- .../core/src/tools/handlers/mcp_resource.rs | 4 ++ .../core/tests/common/apps_test_server.rs | 4 ++ codex-rs/core/tests/suite/rmcp_client.rs | 2 + codex-rs/core/tests/suite/search_tool.rs | 3 + ...event_processor_with_jsonl_output_tests.rs | 1 + .../tests/event_processor_with_json_output.rs | 5 ++ codex-rs/protocol/src/items.rs | 10 ++++ codex-rs/protocol/src/protocol.rs | 20 +++++++ .../src/chatwidget/tests/history_replay.rs | 1 + 40 files changed, 899 insertions(+), 15 deletions(-) create mode 100644 codex-rs/app-server-protocol/schema/typescript/v2/McpToolCallAppContext.ts diff --git a/codex-rs/analytics/src/analytics_client_tests.rs b/codex-rs/analytics/src/analytics_client_tests.rs index fcdd97af8e..dac4829ab3 100644 --- a/codex-rs/analytics/src/analytics_client_tests.rs +++ b/codex-rs/analytics/src/analytics_client_tests.rs @@ -4121,6 +4121,7 @@ async fn turn_event_counts_completed_tool_items() { tool: "search".to_string(), status, arguments: json!({}), + app_context: None, mcp_app_resource_uri: None, plugin_id: Some("sample@test".to_string()), result: None, diff --git a/codex-rs/app-server-protocol/schema/json/ServerNotification.json b/codex-rs/app-server-protocol/schema/json/ServerNotification.json index bfafd5483b..8509a1c69d 100644 --- a/codex-rs/app-server-protocol/schema/json/ServerNotification.json +++ b/codex-rs/app-server-protocol/schema/json/ServerNotification.json @@ -2396,6 +2396,29 @@ ], "type": "object" }, + "McpToolCallAppContext": { + "properties": { + "connectorId": { + "type": "string" + }, + "linkId": { + "type": [ + "string", + "null" + ] + }, + "resourceUri": { + "type": [ + "string", + "null" + ] + } + }, + "required": [ + "connectorId" + ], + "type": "object" + }, "McpToolCallError": { "properties": { "message": { @@ -4066,6 +4089,16 @@ }, { "properties": { + "appContext": { + "anyOf": [ + { + "$ref": "#/definitions/McpToolCallAppContext" + }, + { + "type": "null" + } + ] + }, "arguments": true, "durationMs": { "description": "The duration of the MCP tool call in milliseconds.", @@ -4089,6 +4122,7 @@ "type": "string" }, "mcpAppResourceUri": { + "description": "Deprecated: use `appContext.resourceUri` instead.", "type": [ "string", "null" 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 8ef80b7b54..e9611c67e1 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 @@ -12119,6 +12119,29 @@ "title": "McpServerToolCallResponse", "type": "object" }, + "McpToolCallAppContext": { + "properties": { + "connectorId": { + "type": "string" + }, + "linkId": { + "type": [ + "string", + "null" + ] + }, + "resourceUri": { + "type": [ + "string", + "null" + ] + } + }, + "required": [ + "connectorId" + ], + "type": "object" + }, "McpToolCallError": { "properties": { "message": { @@ -17511,6 +17534,16 @@ }, { "properties": { + "appContext": { + "anyOf": [ + { + "$ref": "#/definitions/v2/McpToolCallAppContext" + }, + { + "type": "null" + } + ] + }, "arguments": true, "durationMs": { "description": "The duration of the MCP tool call in milliseconds.", @@ -17534,6 +17567,7 @@ "type": "string" }, "mcpAppResourceUri": { + "description": "Deprecated: use `appContext.resourceUri` instead.", "type": [ "string", "null" 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 00d7a4bbdf..bac9a48e70 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 @@ -8543,6 +8543,29 @@ "title": "McpServerToolCallResponse", "type": "object" }, + "McpToolCallAppContext": { + "properties": { + "connectorId": { + "type": "string" + }, + "linkId": { + "type": [ + "string", + "null" + ] + }, + "resourceUri": { + "type": [ + "string", + "null" + ] + } + }, + "required": [ + "connectorId" + ], + "type": "object" + }, "McpToolCallError": { "properties": { "message": { @@ -15290,6 +15313,16 @@ }, { "properties": { + "appContext": { + "anyOf": [ + { + "$ref": "#/definitions/McpToolCallAppContext" + }, + { + "type": "null" + } + ] + }, "arguments": true, "durationMs": { "description": "The duration of the MCP tool call in milliseconds.", @@ -15313,6 +15346,7 @@ "type": "string" }, "mcpAppResourceUri": { + "description": "Deprecated: use `appContext.resourceUri` instead.", "type": [ "string", "null" 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 027edd0367..d7bfb3827e 100644 --- a/codex-rs/app-server-protocol/schema/json/v2/ItemCompletedNotification.json +++ b/codex-rs/app-server-protocol/schema/json/v2/ItemCompletedNotification.json @@ -297,6 +297,29 @@ "LegacyAppPathString": { "type": "string" }, + "McpToolCallAppContext": { + "properties": { + "connectorId": { + "type": "string" + }, + "linkId": { + "type": [ + "string", + "null" + ] + }, + "resourceUri": { + "type": [ + "string", + "null" + ] + } + }, + "required": [ + "connectorId" + ], + "type": "object" + }, "McpToolCallError": { "properties": { "message": { @@ -784,6 +807,16 @@ }, { "properties": { + "appContext": { + "anyOf": [ + { + "$ref": "#/definitions/McpToolCallAppContext" + }, + { + "type": "null" + } + ] + }, "arguments": true, "durationMs": { "description": "The duration of the MCP tool call in milliseconds.", @@ -807,6 +840,7 @@ "type": "string" }, "mcpAppResourceUri": { + "description": "Deprecated: use `appContext.resourceUri` instead.", "type": [ "string", "null" 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 64152e9cf4..770fc8bd4c 100644 --- a/codex-rs/app-server-protocol/schema/json/v2/ItemStartedNotification.json +++ b/codex-rs/app-server-protocol/schema/json/v2/ItemStartedNotification.json @@ -297,6 +297,29 @@ "LegacyAppPathString": { "type": "string" }, + "McpToolCallAppContext": { + "properties": { + "connectorId": { + "type": "string" + }, + "linkId": { + "type": [ + "string", + "null" + ] + }, + "resourceUri": { + "type": [ + "string", + "null" + ] + } + }, + "required": [ + "connectorId" + ], + "type": "object" + }, "McpToolCallError": { "properties": { "message": { @@ -784,6 +807,16 @@ }, { "properties": { + "appContext": { + "anyOf": [ + { + "$ref": "#/definitions/McpToolCallAppContext" + }, + { + "type": "null" + } + ] + }, "arguments": true, "durationMs": { "description": "The duration of the MCP tool call in milliseconds.", @@ -807,6 +840,7 @@ "type": "string" }, "mcpAppResourceUri": { + "description": "Deprecated: use `appContext.resourceUri` instead.", "type": [ "string", "null" 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 61f61e74dc..545ae64d4a 100644 --- a/codex-rs/app-server-protocol/schema/json/v2/ReviewStartResponse.json +++ b/codex-rs/app-server-protocol/schema/json/v2/ReviewStartResponse.json @@ -434,6 +434,29 @@ "LegacyAppPathString": { "type": "string" }, + "McpToolCallAppContext": { + "properties": { + "connectorId": { + "type": "string" + }, + "linkId": { + "type": [ + "string", + "null" + ] + }, + "resourceUri": { + "type": [ + "string", + "null" + ] + } + }, + "required": [ + "connectorId" + ], + "type": "object" + }, "McpToolCallError": { "properties": { "message": { @@ -928,6 +951,16 @@ }, { "properties": { + "appContext": { + "anyOf": [ + { + "$ref": "#/definitions/McpToolCallAppContext" + }, + { + "type": "null" + } + ] + }, "arguments": true, "durationMs": { "description": "The duration of the MCP tool call in milliseconds.", @@ -951,6 +984,7 @@ "type": "string" }, "mcpAppResourceUri": { + "description": "Deprecated: use `appContext.resourceUri` instead.", "type": [ "string", "null" 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 43a76f145a..9ced571cfa 100644 --- a/codex-rs/app-server-protocol/schema/json/v2/ThreadForkResponse.json +++ b/codex-rs/app-server-protocol/schema/json/v2/ThreadForkResponse.json @@ -539,6 +539,29 @@ "LegacyAppPathString": { "type": "string" }, + "McpToolCallAppContext": { + "properties": { + "connectorId": { + "type": "string" + }, + "linkId": { + "type": [ + "string", + "null" + ] + }, + "resourceUri": { + "type": [ + "string", + "null" + ] + } + }, + "required": [ + "connectorId" + ], + "type": "object" + }, "McpToolCallError": { "properties": { "message": { @@ -1420,6 +1443,16 @@ }, { "properties": { + "appContext": { + "anyOf": [ + { + "$ref": "#/definitions/McpToolCallAppContext" + }, + { + "type": "null" + } + ] + }, "arguments": true, "durationMs": { "description": "The duration of the MCP tool call in milliseconds.", @@ -1443,6 +1476,7 @@ "type": "string" }, "mcpAppResourceUri": { + "description": "Deprecated: use `appContext.resourceUri` instead.", "type": [ "string", "null" 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 1e1504b738..9925c5411e 100644 --- a/codex-rs/app-server-protocol/schema/json/v2/ThreadListResponse.json +++ b/codex-rs/app-server-protocol/schema/json/v2/ThreadListResponse.json @@ -460,6 +460,29 @@ "LegacyAppPathString": { "type": "string" }, + "McpToolCallAppContext": { + "properties": { + "connectorId": { + "type": "string" + }, + "linkId": { + "type": [ + "string", + "null" + ] + }, + "resourceUri": { + "type": [ + "string", + "null" + ] + } + }, + "required": [ + "connectorId" + ], + "type": "object" + }, "McpToolCallError": { "properties": { "message": { @@ -1235,6 +1258,16 @@ }, { "properties": { + "appContext": { + "anyOf": [ + { + "$ref": "#/definitions/McpToolCallAppContext" + }, + { + "type": "null" + } + ] + }, "arguments": true, "durationMs": { "description": "The duration of the MCP tool call in milliseconds.", @@ -1258,6 +1291,7 @@ "type": "string" }, "mcpAppResourceUri": { + "description": "Deprecated: use `appContext.resourceUri` instead.", "type": [ "string", "null" 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 6e62eae14b..1e142947ed 100644 --- a/codex-rs/app-server-protocol/schema/json/v2/ThreadMetadataUpdateResponse.json +++ b/codex-rs/app-server-protocol/schema/json/v2/ThreadMetadataUpdateResponse.json @@ -460,6 +460,29 @@ "LegacyAppPathString": { "type": "string" }, + "McpToolCallAppContext": { + "properties": { + "connectorId": { + "type": "string" + }, + "linkId": { + "type": [ + "string", + "null" + ] + }, + "resourceUri": { + "type": [ + "string", + "null" + ] + } + }, + "required": [ + "connectorId" + ], + "type": "object" + }, "McpToolCallError": { "properties": { "message": { @@ -1235,6 +1258,16 @@ }, { "properties": { + "appContext": { + "anyOf": [ + { + "$ref": "#/definitions/McpToolCallAppContext" + }, + { + "type": "null" + } + ] + }, "arguments": true, "durationMs": { "description": "The duration of the MCP tool call in milliseconds.", @@ -1258,6 +1291,7 @@ "type": "string" }, "mcpAppResourceUri": { + "description": "Deprecated: use `appContext.resourceUri` instead.", "type": [ "string", "null" 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 ff70a017b4..532a712807 100644 --- a/codex-rs/app-server-protocol/schema/json/v2/ThreadReadResponse.json +++ b/codex-rs/app-server-protocol/schema/json/v2/ThreadReadResponse.json @@ -460,6 +460,29 @@ "LegacyAppPathString": { "type": "string" }, + "McpToolCallAppContext": { + "properties": { + "connectorId": { + "type": "string" + }, + "linkId": { + "type": [ + "string", + "null" + ] + }, + "resourceUri": { + "type": [ + "string", + "null" + ] + } + }, + "required": [ + "connectorId" + ], + "type": "object" + }, "McpToolCallError": { "properties": { "message": { @@ -1235,6 +1258,16 @@ }, { "properties": { + "appContext": { + "anyOf": [ + { + "$ref": "#/definitions/McpToolCallAppContext" + }, + { + "type": "null" + } + ] + }, "arguments": true, "durationMs": { "description": "The duration of the MCP tool call in milliseconds.", @@ -1258,6 +1291,7 @@ "type": "string" }, "mcpAppResourceUri": { + "description": "Deprecated: use `appContext.resourceUri` instead.", "type": [ "string", "null" 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 f9a65cdd8b..28a5f5b24a 100644 --- a/codex-rs/app-server-protocol/schema/json/v2/ThreadResumeResponse.json +++ b/codex-rs/app-server-protocol/schema/json/v2/ThreadResumeResponse.json @@ -539,6 +539,29 @@ "LegacyAppPathString": { "type": "string" }, + "McpToolCallAppContext": { + "properties": { + "connectorId": { + "type": "string" + }, + "linkId": { + "type": [ + "string", + "null" + ] + }, + "resourceUri": { + "type": [ + "string", + "null" + ] + } + }, + "required": [ + "connectorId" + ], + "type": "object" + }, "McpToolCallError": { "properties": { "message": { @@ -1420,6 +1443,16 @@ }, { "properties": { + "appContext": { + "anyOf": [ + { + "$ref": "#/definitions/McpToolCallAppContext" + }, + { + "type": "null" + } + ] + }, "arguments": true, "durationMs": { "description": "The duration of the MCP tool call in milliseconds.", @@ -1443,6 +1476,7 @@ "type": "string" }, "mcpAppResourceUri": { + "description": "Deprecated: use `appContext.resourceUri` instead.", "type": [ "string", "null" 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 f455f6dfff..9f394b31cf 100644 --- a/codex-rs/app-server-protocol/schema/json/v2/ThreadRollbackResponse.json +++ b/codex-rs/app-server-protocol/schema/json/v2/ThreadRollbackResponse.json @@ -460,6 +460,29 @@ "LegacyAppPathString": { "type": "string" }, + "McpToolCallAppContext": { + "properties": { + "connectorId": { + "type": "string" + }, + "linkId": { + "type": [ + "string", + "null" + ] + }, + "resourceUri": { + "type": [ + "string", + "null" + ] + } + }, + "required": [ + "connectorId" + ], + "type": "object" + }, "McpToolCallError": { "properties": { "message": { @@ -1235,6 +1258,16 @@ }, { "properties": { + "appContext": { + "anyOf": [ + { + "$ref": "#/definitions/McpToolCallAppContext" + }, + { + "type": "null" + } + ] + }, "arguments": true, "durationMs": { "description": "The duration of the MCP tool call in milliseconds.", @@ -1258,6 +1291,7 @@ "type": "string" }, "mcpAppResourceUri": { + "description": "Deprecated: use `appContext.resourceUri` instead.", "type": [ "string", "null" 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 0c3eac4de5..1baf6e4bdc 100644 --- a/codex-rs/app-server-protocol/schema/json/v2/ThreadStartResponse.json +++ b/codex-rs/app-server-protocol/schema/json/v2/ThreadStartResponse.json @@ -539,6 +539,29 @@ "LegacyAppPathString": { "type": "string" }, + "McpToolCallAppContext": { + "properties": { + "connectorId": { + "type": "string" + }, + "linkId": { + "type": [ + "string", + "null" + ] + }, + "resourceUri": { + "type": [ + "string", + "null" + ] + } + }, + "required": [ + "connectorId" + ], + "type": "object" + }, "McpToolCallError": { "properties": { "message": { @@ -1420,6 +1443,16 @@ }, { "properties": { + "appContext": { + "anyOf": [ + { + "$ref": "#/definitions/McpToolCallAppContext" + }, + { + "type": "null" + } + ] + }, "arguments": true, "durationMs": { "description": "The duration of the MCP tool call in milliseconds.", @@ -1443,6 +1476,7 @@ "type": "string" }, "mcpAppResourceUri": { + "description": "Deprecated: use `appContext.resourceUri` instead.", "type": [ "string", "null" 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 0e44cfb258..22801809cd 100644 --- a/codex-rs/app-server-protocol/schema/json/v2/ThreadStartedNotification.json +++ b/codex-rs/app-server-protocol/schema/json/v2/ThreadStartedNotification.json @@ -460,6 +460,29 @@ "LegacyAppPathString": { "type": "string" }, + "McpToolCallAppContext": { + "properties": { + "connectorId": { + "type": "string" + }, + "linkId": { + "type": [ + "string", + "null" + ] + }, + "resourceUri": { + "type": [ + "string", + "null" + ] + } + }, + "required": [ + "connectorId" + ], + "type": "object" + }, "McpToolCallError": { "properties": { "message": { @@ -1235,6 +1258,16 @@ }, { "properties": { + "appContext": { + "anyOf": [ + { + "$ref": "#/definitions/McpToolCallAppContext" + }, + { + "type": "null" + } + ] + }, "arguments": true, "durationMs": { "description": "The duration of the MCP tool call in milliseconds.", @@ -1258,6 +1291,7 @@ "type": "string" }, "mcpAppResourceUri": { + "description": "Deprecated: use `appContext.resourceUri` instead.", "type": [ "string", "null" 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 f88a1fae59..3009e86e70 100644 --- a/codex-rs/app-server-protocol/schema/json/v2/ThreadUnarchiveResponse.json +++ b/codex-rs/app-server-protocol/schema/json/v2/ThreadUnarchiveResponse.json @@ -460,6 +460,29 @@ "LegacyAppPathString": { "type": "string" }, + "McpToolCallAppContext": { + "properties": { + "connectorId": { + "type": "string" + }, + "linkId": { + "type": [ + "string", + "null" + ] + }, + "resourceUri": { + "type": [ + "string", + "null" + ] + } + }, + "required": [ + "connectorId" + ], + "type": "object" + }, "McpToolCallError": { "properties": { "message": { @@ -1235,6 +1258,16 @@ }, { "properties": { + "appContext": { + "anyOf": [ + { + "$ref": "#/definitions/McpToolCallAppContext" + }, + { + "type": "null" + } + ] + }, "arguments": true, "durationMs": { "description": "The duration of the MCP tool call in milliseconds.", @@ -1258,6 +1291,7 @@ "type": "string" }, "mcpAppResourceUri": { + "description": "Deprecated: use `appContext.resourceUri` instead.", "type": [ "string", "null" 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 7e6cef2c37..b9a940157c 100644 --- a/codex-rs/app-server-protocol/schema/json/v2/TurnCompletedNotification.json +++ b/codex-rs/app-server-protocol/schema/json/v2/TurnCompletedNotification.json @@ -434,6 +434,29 @@ "LegacyAppPathString": { "type": "string" }, + "McpToolCallAppContext": { + "properties": { + "connectorId": { + "type": "string" + }, + "linkId": { + "type": [ + "string", + "null" + ] + }, + "resourceUri": { + "type": [ + "string", + "null" + ] + } + }, + "required": [ + "connectorId" + ], + "type": "object" + }, "McpToolCallError": { "properties": { "message": { @@ -928,6 +951,16 @@ }, { "properties": { + "appContext": { + "anyOf": [ + { + "$ref": "#/definitions/McpToolCallAppContext" + }, + { + "type": "null" + } + ] + }, "arguments": true, "durationMs": { "description": "The duration of the MCP tool call in milliseconds.", @@ -951,6 +984,7 @@ "type": "string" }, "mcpAppResourceUri": { + "description": "Deprecated: use `appContext.resourceUri` instead.", "type": [ "string", "null" 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 083767a6cf..da10713dd1 100644 --- a/codex-rs/app-server-protocol/schema/json/v2/TurnStartResponse.json +++ b/codex-rs/app-server-protocol/schema/json/v2/TurnStartResponse.json @@ -434,6 +434,29 @@ "LegacyAppPathString": { "type": "string" }, + "McpToolCallAppContext": { + "properties": { + "connectorId": { + "type": "string" + }, + "linkId": { + "type": [ + "string", + "null" + ] + }, + "resourceUri": { + "type": [ + "string", + "null" + ] + } + }, + "required": [ + "connectorId" + ], + "type": "object" + }, "McpToolCallError": { "properties": { "message": { @@ -928,6 +951,16 @@ }, { "properties": { + "appContext": { + "anyOf": [ + { + "$ref": "#/definitions/McpToolCallAppContext" + }, + { + "type": "null" + } + ] + }, "arguments": true, "durationMs": { "description": "The duration of the MCP tool call in milliseconds.", @@ -951,6 +984,7 @@ "type": "string" }, "mcpAppResourceUri": { + "description": "Deprecated: use `appContext.resourceUri` instead.", "type": [ "string", "null" 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 3a43d7d3fb..6b93498221 100644 --- a/codex-rs/app-server-protocol/schema/json/v2/TurnStartedNotification.json +++ b/codex-rs/app-server-protocol/schema/json/v2/TurnStartedNotification.json @@ -434,6 +434,29 @@ "LegacyAppPathString": { "type": "string" }, + "McpToolCallAppContext": { + "properties": { + "connectorId": { + "type": "string" + }, + "linkId": { + "type": [ + "string", + "null" + ] + }, + "resourceUri": { + "type": [ + "string", + "null" + ] + } + }, + "required": [ + "connectorId" + ], + "type": "object" + }, "McpToolCallError": { "properties": { "message": { @@ -928,6 +951,16 @@ }, { "properties": { + "appContext": { + "anyOf": [ + { + "$ref": "#/definitions/McpToolCallAppContext" + }, + { + "type": "null" + } + ] + }, "arguments": true, "durationMs": { "description": "The duration of the MCP tool call in milliseconds.", @@ -951,6 +984,7 @@ "type": "string" }, "mcpAppResourceUri": { + "description": "Deprecated: use `appContext.resourceUri` instead.", "type": [ "string", "null" diff --git a/codex-rs/app-server-protocol/schema/typescript/v2/McpToolCallAppContext.ts b/codex-rs/app-server-protocol/schema/typescript/v2/McpToolCallAppContext.ts new file mode 100644 index 0000000000..0a8343428d --- /dev/null +++ b/codex-rs/app-server-protocol/schema/typescript/v2/McpToolCallAppContext.ts @@ -0,0 +1,5 @@ +// 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. + +export type McpToolCallAppContext = { connectorId: string, linkId: string | null, resourceUri: string | null, }; diff --git a/codex-rs/app-server-protocol/schema/typescript/v2/ThreadItem.ts b/codex-rs/app-server-protocol/schema/typescript/v2/ThreadItem.ts index 1cdac99c64..eb8c6486b8 100644 --- a/codex-rs/app-server-protocol/schema/typescript/v2/ThreadItem.ts +++ b/codex-rs/app-server-protocol/schema/typescript/v2/ThreadItem.ts @@ -16,6 +16,7 @@ import type { DynamicToolCallOutputContentItem } from "./DynamicToolCallOutputCo import type { DynamicToolCallStatus } from "./DynamicToolCallStatus"; import type { FileUpdateChange } from "./FileUpdateChange"; import type { HookPromptFragment } from "./HookPromptFragment"; +import type { McpToolCallAppContext } from "./McpToolCallAppContext"; import type { McpToolCallError } from "./McpToolCallError"; import type { McpToolCallResult } from "./McpToolCallResult"; import type { McpToolCallStatus } from "./McpToolCallStatus"; @@ -55,7 +56,11 @@ exitCode: number | null, /** * The duration of the command execution in milliseconds. */ -durationMs: number | null, } | { "type": "fileChange", id: string, changes: Array, status: PatchApplyStatus, } | { "type": "mcpToolCall", id: string, server: string, tool: string, status: McpToolCallStatus, arguments: JsonValue, mcpAppResourceUri?: string, pluginId: string | null, result: McpToolCallResult | null, error: McpToolCallError | null, +durationMs: number | null, } | { "type": "fileChange", id: string, changes: Array, status: PatchApplyStatus, } | { "type": "mcpToolCall", id: string, server: string, tool: string, status: McpToolCallStatus, arguments: JsonValue, appContext: McpToolCallAppContext | null, +/** + * Deprecated: use `appContext.resourceUri` instead. + */ +mcpAppResourceUri?: string, pluginId: string | null, result: McpToolCallResult | null, error: McpToolCallError | null, /** * The duration of the MCP tool call in milliseconds. */ diff --git a/codex-rs/app-server-protocol/schema/typescript/v2/index.ts b/codex-rs/app-server-protocol/schema/typescript/v2/index.ts index 2e59b0fe19..9b1bd3b6e8 100644 --- a/codex-rs/app-server-protocol/schema/typescript/v2/index.ts +++ b/codex-rs/app-server-protocol/schema/typescript/v2/index.ts @@ -243,6 +243,7 @@ export type { McpServerStatusDetail } from "./McpServerStatusDetail"; export type { McpServerStatusUpdatedNotification } from "./McpServerStatusUpdatedNotification"; export type { McpServerToolCallParams } from "./McpServerToolCallParams"; export type { McpServerToolCallResponse } from "./McpServerToolCallResponse"; +export type { McpToolCallAppContext } from "./McpToolCallAppContext"; export type { McpToolCallError } from "./McpToolCallError"; export type { McpToolCallProgressNotification } from "./McpToolCallProgressNotification"; export type { McpToolCallResult } from "./McpToolCallResult"; diff --git a/codex-rs/app-server-protocol/src/protocol/thread_history.rs b/codex-rs/app-server-protocol/src/protocol/thread_history.rs index 001fa7cf93..6b27e72d46 100644 --- a/codex-rs/app-server-protocol/src/protocol/thread_history.rs +++ b/codex-rs/app-server-protocol/src/protocol/thread_history.rs @@ -10,6 +10,7 @@ use crate::protocol::v2::CollabAgentToolCallStatus; use crate::protocol::v2::CommandExecutionStatus; use crate::protocol::v2::DynamicToolCallOutputContentItem; use crate::protocol::v2::DynamicToolCallStatus; +use crate::protocol::v2::McpToolCallAppContext; use crate::protocol::v2::McpToolCallError; use crate::protocol::v2::McpToolCallResult; use crate::protocol::v2::McpToolCallStatus; @@ -765,6 +766,14 @@ impl ThreadHistoryBuilder { .arguments .clone() .unwrap_or(serde_json::Value::Null), + app_context: payload + .connector_id + .clone() + .map(|connector_id| McpToolCallAppContext { + connector_id, + link_id: payload.link_id.clone(), + resource_uri: payload.mcp_app_resource_uri.clone(), + }), mcp_app_resource_uri: payload.mcp_app_resource_uri.clone(), plugin_id: payload.plugin_id.clone(), result: None, @@ -807,6 +816,14 @@ impl ThreadHistoryBuilder { .arguments .clone() .unwrap_or(serde_json::Value::Null), + app_context: payload + .connector_id + .clone() + .map(|connector_id| McpToolCallAppContext { + connector_id, + link_id: payload.link_id.clone(), + resource_uri: payload.mcp_app_resource_uri.clone(), + }), mcp_app_resource_uri: payload.mcp_app_resource_uri.clone(), plugin_id: payload.plugin_id.clone(), result, @@ -1567,7 +1584,6 @@ mod tests { use codex_protocol::protocol::DynamicToolCallResponseEvent; use codex_protocol::protocol::ExecCommandEndEvent; use codex_protocol::protocol::ExecCommandSource; - use codex_protocol::protocol::ItemCompletedEvent; use codex_protocol::protocol::ItemStartedEvent; use codex_protocol::protocol::McpInvocation; use codex_protocol::protocol::McpToolCallEndEvent; @@ -2397,7 +2413,9 @@ mod tests { tool: "lookup".into(), arguments: Some(serde_json::json!({"id":"123"})), }, + connector_id: None, mcp_app_resource_uri: None, + link_id: None, plugin_id: None, duration: Duration::from_millis(8), result: Err("boom".into()), @@ -2447,6 +2465,7 @@ mod tests { tool: "lookup".into(), status: McpToolCallStatus::Failed, arguments: serde_json::json!({"id":"123"}), + app_context: None, mcp_app_resource_uri: None, plugin_id: None, result: None, @@ -2475,7 +2494,9 @@ mod tests { tool: "lookup".into(), arguments: Some(serde_json::json!({"id":"123"})), }, + connector_id: Some("calendar".into()), mcp_app_resource_uri: Some("ui://widget/lookup.html".into()), + link_id: Some("link_calendar".into()), plugin_id: Some("sample@test".into()), duration: Duration::from_millis(8), result: Ok(CallToolResult { @@ -2506,6 +2527,11 @@ mod tests { tool: "lookup".into(), status: McpToolCallStatus::Completed, arguments: serde_json::json!({"id":"123"}), + app_context: Some(McpToolCallAppContext { + connector_id: "calendar".into(), + link_id: Some("link_calendar".into()), + resource_uri: Some("ui://widget/lookup.html".into()), + }), mcp_app_resource_uri: Some("ui://widget/lookup.html".into()), plugin_id: Some("sample@test".into()), result: Some(Box::new(McpToolCallResult { 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 34cfe8e136..e630de634b 100644 --- a/codex-rs/app-server-protocol/src/protocol/v2/item.rs +++ b/codex-rs/app-server-protocol/src/protocol/v2/item.rs @@ -290,8 +290,10 @@ pub enum ThreadItem { tool: String, status: McpToolCallStatus, arguments: JsonValue, + app_context: Option, #[serde(default, skip_serializing_if = "Option::is_none")] #[ts(optional)] + /// Deprecated: use `appContext.resourceUri` instead. mcp_app_resource_uri: Option, plugin_id: Option, result: Option>, @@ -384,6 +386,15 @@ pub enum ThreadItem { ContextCompaction { id: String }, } +#[derive(Serialize, Deserialize, Debug, Clone, PartialEq, Eq, JsonSchema, TS)] +#[serde(rename_all = "camelCase")] +#[ts(rename_all = "camelCase", export_to = "v2/")] +pub struct McpToolCallAppContext { + pub connector_id: String, + pub link_id: Option, + pub resource_uri: Option, +} + #[derive(Serialize, Deserialize, Debug, Clone, PartialEq, Eq, JsonSchema, TS)] #[serde(rename_all = "camelCase")] #[ts(rename_all = "camelCase", export_to = "v2/")] @@ -877,6 +888,11 @@ impl From for ThreadItem { tool: mcp.tool, status: McpToolCallStatus::from(mcp.status), arguments: mcp.arguments, + app_context: mcp.connector_id.map(|connector_id| McpToolCallAppContext { + connector_id, + link_id: mcp.link_id, + resource_uri: mcp.mcp_app_resource_uri.clone(), + }), mcp_app_resource_uri: mcp.mcp_app_resource_uri, plugin_id: mcp.plugin_id, result: mcp.result.map(McpToolCallResult::from).map(Box::new), 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 bb66aa632e..0368bf45f6 100644 --- a/codex-rs/app-server-protocol/src/protocol/v2/tests.rs +++ b/codex-rs/app-server-protocol/src/protocol/v2/tests.rs @@ -2615,7 +2615,9 @@ fn core_turn_item_into_thread_item_converts_supported_variants() { server: "server".to_string(), tool: "tool".to_string(), arguments: json!({"arg": "value"}), + connector_id: Some("calendar".to_string()), mcp_app_resource_uri: Some("app://connector".to_string()), + link_id: Some("link_calendar".to_string()), plugin_id: Some("sample@test".to_string()), status: CoreMcpToolCallStatus::InProgress, result: None, @@ -2631,6 +2633,11 @@ fn core_turn_item_into_thread_item_converts_supported_variants() { tool: "tool".to_string(), status: McpToolCallStatus::InProgress, arguments: json!({"arg": "value"}), + app_context: Some(McpToolCallAppContext { + connector_id: "calendar".to_string(), + link_id: Some("link_calendar".to_string()), + resource_uri: Some("app://connector".to_string()), + }), mcp_app_resource_uri: Some("app://connector".to_string()), plugin_id: Some("sample@test".to_string()), result: None, @@ -2644,7 +2651,9 @@ fn core_turn_item_into_thread_item_converts_supported_variants() { server: "server".to_string(), tool: "tool".to_string(), arguments: JsonValue::Null, + connector_id: None, mcp_app_resource_uri: None, + link_id: None, plugin_id: None, status: CoreMcpToolCallStatus::Completed, result: Some(CallToolResult { @@ -2665,6 +2674,7 @@ fn core_turn_item_into_thread_item_converts_supported_variants() { tool: "tool".to_string(), status: McpToolCallStatus::Completed, arguments: JsonValue::Null, + app_context: None, mcp_app_resource_uri: None, plugin_id: None, result: Some(Box::new(McpToolCallResult { @@ -2678,6 +2688,49 @@ fn core_turn_item_into_thread_item_converts_supported_variants() { ); } +#[test] +fn mcp_tool_call_app_context_serializes_connector_id() { + let item = ThreadItem::McpToolCall { + id: "mcp-1".to_string(), + server: "codex_apps".to_string(), + tool: "calendar.create_event".to_string(), + status: McpToolCallStatus::InProgress, + arguments: json!({}), + app_context: Some(McpToolCallAppContext { + connector_id: "calendar".to_string(), + link_id: Some("link_calendar".to_string()), + resource_uri: Some("app://connector".to_string()), + }), + mcp_app_resource_uri: Some("app://connector".to_string()), + plugin_id: None, + result: None, + error: None, + duration_ms: None, + }; + + assert_eq!( + serde_json::to_value(item).expect("MCP tool call should serialize"), + json!({ + "type": "mcpToolCall", + "id": "mcp-1", + "server": "codex_apps", + "tool": "calendar.create_event", + "status": "inProgress", + "arguments": {}, + "appContext": { + "connectorId": "calendar", + "linkId": "link_calendar", + "resourceUri": "app://connector", + }, + "mcpAppResourceUri": "app://connector", + "pluginId": null, + "result": null, + "error": null, + "durationMs": null, + }) + ); +} + #[test] fn user_input_into_core_preserves_image_detail() { assert_eq!( diff --git a/codex-rs/app-server/README.md b/codex-rs/app-server/README.md index b9c7899fc8..deda1c517e 100644 --- a/codex-rs/app-server/README.md +++ b/codex-rs/app-server/README.md @@ -1365,7 +1365,7 @@ Today both notifications carry an empty `items` array even when item events were - `reasoning` — `{id, summary, content}` where `summary` holds streamed reasoning summaries (applicable for most OpenAI models) and `content` holds raw reasoning blocks (applicable for e.g. open source models). - `commandExecution` — `{id, command, cwd, status, commandActions, aggregatedOutput?, exitCode?, durationMs?}` for sandboxed commands; `status` is `inProgress`, `completed`, `failed`, or `declined`. - `fileChange` — `{id, changes, status}` describing proposed edits; `changes` list `{path, kind, diff}` and `status` is `inProgress`, `completed`, `failed`, or `declined`. -- `mcpToolCall` — `{id, server, tool, status, arguments, mcpAppResourceUri?, pluginId, result?, error?}` describing MCP calls; `status` is `inProgress`, `completed`, or `failed`. +- `mcpToolCall` — `{id, server, tool, status, arguments, appContext, mcpAppResourceUri?, pluginId, result?, error?}` describing MCP calls; `appContext` is `{connectorId, linkId, resourceUri}` for calls through a trusted MCP app, where `connectorId` identifies the connector that owns the tool, `linkId` identifies the app link, and `resourceUri` points to the widget template. The top-level `mcpAppResourceUri` is deprecated and temporarily duplicated for client migration. `tool` identifies the invoked action. `status` is `inProgress`, `completed`, or `failed`. - `collabToolCall` — `{id, tool, status, senderThreadId, receiverThreadId?, newThreadId?, prompt?, agentStatus?}` describing collab tool calls (`spawn_agent`, `send_input`, `resume_agent`, `wait`, `close_agent`); `status` is `inProgress`, `completed`, or `failed`. - `webSearch` — `{id, query, action?}` for a web search request issued by the agent; `action` mirrors the Responses API web_search action payload (`search`, `open_page`, `find_in_page`) and may be omitted until completion. - `imageView` — `{id, path}` emitted when the agent invokes the image viewer tool. diff --git a/codex-rs/app-server/src/request_processors/thread_resume_redaction.rs b/codex-rs/app-server/src/request_processors/thread_resume_redaction.rs index 110d6d5cbf..b7d8640695 100644 --- a/codex-rs/app-server/src/request_processors/thread_resume_redaction.rs +++ b/codex-rs/app-server/src/request_processors/thread_resume_redaction.rs @@ -52,6 +52,7 @@ fn redacted_mcp_tool_call_result() -> McpToolCallResult { #[cfg(test)] mod tests { use super::*; + use codex_app_server_protocol::McpToolCallAppContext; use codex_app_server_protocol::McpToolCallError; use codex_app_server_protocol::McpToolCallStatus; use codex_app_server_protocol::SessionSource; @@ -78,6 +79,11 @@ mod tests { tool: "lookup".to_string(), status: McpToolCallStatus::Completed, arguments: serde_json::json!({"secret":"argument"}), + app_context: Some(McpToolCallAppContext { + connector_id: "calendar".to_string(), + link_id: Some("link_calendar".to_string()), + resource_uri: Some("ui://widget/lookup.html".to_string()), + }), mcp_app_resource_uri: Some("ui://widget/lookup.html".to_string()), plugin_id: Some("sample@test".to_string()), result: Some(Box::new(McpToolCallResult { @@ -120,6 +126,11 @@ mod tests { tool: "lookup".to_string(), status: McpToolCallStatus::Completed, arguments: JsonValue::String(REDACTED_PAYLOAD.to_string()), + app_context: Some(McpToolCallAppContext { + connector_id: "calendar".to_string(), + link_id: Some("link_calendar".to_string()), + resource_uri: Some("ui://widget/lookup.html".to_string()), + }), mcp_app_resource_uri: Some("ui://widget/lookup.html".to_string()), plugin_id: Some("sample@test".to_string()), result: Some(Box::new(redacted_mcp_tool_call_result())), @@ -137,6 +148,7 @@ mod tests { tool: "lookup".to_string(), status: McpToolCallStatus::Failed, arguments: serde_json::json!({"secret":"argument"}), + app_context: None, mcp_app_resource_uri: None, plugin_id: None, result: None, @@ -156,6 +168,7 @@ mod tests { tool: "lookup".to_string(), status: McpToolCallStatus::Failed, arguments: JsonValue::String(REDACTED_PAYLOAD.to_string()), + app_context: None, mcp_app_resource_uri: None, plugin_id: None, result: None, diff --git a/codex-rs/app-server/tests/suite/v2/mcp_tool.rs b/codex-rs/app-server/tests/suite/v2/mcp_tool.rs index 10c8a6ef04..07c2e9fed7 100644 --- a/codex-rs/app-server/tests/suite/v2/mcp_tool.rs +++ b/codex-rs/app-server/tests/suite/v2/mcp_tool.rs @@ -513,6 +513,7 @@ url = "{mcp_server_url}/mcp" tool, status, arguments: json!({ "message": LARGE_RESPONSE_MESSAGE }), + app_context: None, mcp_app_resource_uri: None, plugin_id: None, result: Some(result), diff --git a/codex-rs/app-server/tests/suite/v2/thread_resume.rs b/codex-rs/app-server/tests/suite/v2/thread_resume.rs index c191263d1c..cd508fc687 100644 --- a/codex-rs/app-server/tests/suite/v2/thread_resume.rs +++ b/codex-rs/app-server/tests/suite/v2/thread_resume.rs @@ -23,6 +23,7 @@ use codex_app_server_protocol::FileChangeRequestApprovalResponse; use codex_app_server_protocol::ItemStartedNotification; use codex_app_server_protocol::JSONRPCError; use codex_app_server_protocol::JSONRPCResponse; +use codex_app_server_protocol::McpToolCallAppContext; use codex_app_server_protocol::PatchApplyStatus; use codex_app_server_protocol::PatchChangeKind; use codex_app_server_protocol::RequestId; @@ -729,6 +730,7 @@ async fn thread_resume_redacts_payloads_for_chatgpt_remote_clients() -> Result<( .expect("remote resume should include redacted MCP item"); let ThreadItem::McpToolCall { arguments, + app_context, result, error, .. @@ -737,6 +739,14 @@ async fn thread_resume_redacts_payloads_for_chatgpt_remote_clients() -> Result<( unreachable!("matched MCP item"); }; assert_eq!(arguments, &json!("[redacted]")); + assert_eq!( + app_context, + &Some(McpToolCallAppContext { + connector_id: "calendar".to_string(), + link_id: Some("link_calendar".to_string()), + resource_uri: Some("ui://widget/lookup.html".to_string()), + }) + ); let result = result.as_ref().expect("redacted MCP result"); assert_eq!( result.content, @@ -770,12 +780,23 @@ async fn thread_resume_redacts_payloads_for_chatgpt_remote_clients() -> Result<( .find(|item| matches!(item, ThreadItem::McpToolCall { .. })) .expect("normal resume should include MCP item"); let ThreadItem::McpToolCall { - arguments, result, .. + arguments, + app_context, + result, + .. } = normal_mcp_item else { unreachable!("matched MCP item"); }; assert_eq!(arguments, &json!({"secret":"argument"})); + assert_eq!( + app_context, + &Some(McpToolCallAppContext { + connector_id: "calendar".to_string(), + link_id: Some("link_calendar".to_string()), + resource_uri: Some("ui://widget/lookup.html".to_string()), + }) + ); let result = result.as_ref().expect("normal MCP result"); assert_eq!( result.content, @@ -877,7 +898,9 @@ fn append_resume_redaction_history( tool: "lookup".to_string(), arguments: Some(json!({"secret":"argument"})), }, + connector_id: Some("calendar".to_string()), mcp_app_resource_uri: Some("ui://widget/lookup.html".to_string()), + link_id: Some("link_calendar".to_string()), plugin_id: None, duration: Duration::from_millis(8), result: Ok(CallToolResult { diff --git a/codex-rs/core/src/mcp_tool_call.rs b/codex-rs/core/src/mcp_tool_call.rs index a89951542a..56d6e678a0 100644 --- a/codex-rs/core/src/mcp_tool_call.rs +++ b/codex-rs/core/src/mcp_tool_call.rs @@ -143,14 +143,7 @@ pub(crate) async fn handle_mcp_tool_call( let metadata = lookup_mcp_tool_metadata(sess.as_ref(), turn_context.as_ref(), &server, &tool_name).await; - let item_metadata = McpToolCallItemMetadata { - mcp_app_resource_uri: metadata - .as_ref() - .and_then(|metadata| metadata.mcp_app_resource_uri.clone()), - plugin_id: metadata - .as_ref() - .and_then(|metadata| metadata.plugin_id.clone()), - }; + let item_metadata = McpToolCallItemMetadata::from_tool_metadata(&server, metadata.as_ref()); let app_tool_policy = if server == CODEX_APPS_MCP_SERVER_NAME { let annotations = metadata .as_ref() @@ -312,12 +305,32 @@ pub(crate) struct HandledMcpToolCall { pub(crate) tool_input: JsonValue, } -#[derive(Clone)] +#[derive(Clone, Debug, PartialEq, Eq)] struct McpToolCallItemMetadata { + connector_id: Option, + link_id: Option, mcp_app_resource_uri: Option, plugin_id: Option, } +impl McpToolCallItemMetadata { + fn from_tool_metadata(server: &str, metadata: Option<&McpToolApprovalMetadata>) -> Self { + let trusted_mcp_app_metadata = if server == CODEX_APPS_MCP_SERVER_NAME { + metadata + } else { + None + }; + Self { + connector_id: trusted_mcp_app_metadata + .and_then(|metadata| metadata.connector_id.clone()), + link_id: trusted_mcp_app_metadata.and_then(|metadata| metadata.link_id.clone()), + mcp_app_resource_uri: metadata + .and_then(|metadata| metadata.mcp_app_resource_uri.clone()), + plugin_id: metadata.and_then(|metadata| metadata.plugin_id.clone()), + } + } +} + async fn handle_approved_mcp_tool_call( sess: &Session, turn_context: &TurnContext, @@ -892,7 +905,9 @@ async fn notify_mcp_tool_call_started( server, tool, arguments: arguments.unwrap_or(JsonValue::Null), + connector_id: item_metadata.connector_id, mcp_app_resource_uri: item_metadata.mcp_app_resource_uri, + link_id: item_metadata.link_id, plugin_id: item_metadata.plugin_id, status: McpToolCallStatus::InProgress, result: None, @@ -932,7 +947,9 @@ async fn notify_mcp_tool_call_completed( server, tool, arguments: arguments.unwrap_or(JsonValue::Null), + connector_id: item_metadata.connector_id, mcp_app_resource_uri: item_metadata.mcp_app_resource_uri, + link_id: item_metadata.link_id, plugin_id: item_metadata.plugin_id, status, result, @@ -998,6 +1015,7 @@ enum McpToolApprovalDecision { pub(crate) struct McpToolApprovalMetadata { annotations: Option, connector_id: Option, + link_id: Option, connector_name: Option, connector_description: Option, plugin_id: Option, @@ -1010,6 +1028,7 @@ pub(crate) struct McpToolApprovalMetadata { const MCP_TOOL_OPENAI_OUTPUT_TEMPLATE_META_KEY: &str = "openai/outputTemplate"; const MCP_TOOL_UI_RESOURCE_URI_META_KEY: &str = "ui/resourceUri"; +const MCP_TOOL_LINK_ID_META_KEY: &str = "link_id"; const MCP_TOOL_PLUGIN_ID_META_KEY: &str = "plugin_id"; const MCP_TOOL_THREAD_ID_META_KEY: &str = "threadId"; @@ -1493,6 +1512,13 @@ pub(crate) async fn lookup_mcp_tool_metadata( Some(McpToolApprovalMetadata { annotations: tool_info.tool.annotations, connector_id: tool_info.connector_id, + link_id: tool_info + .tool + .meta + .as_ref() + .and_then(|meta| meta.get(MCP_TOOL_LINK_ID_META_KEY)) + .and_then(serde_json::Value::as_str) + .map(str::to_string), connector_name: tool_info.connector_name, connector_description, plugin_id, diff --git a/codex-rs/core/src/mcp_tool_call_tests.rs b/codex-rs/core/src/mcp_tool_call_tests.rs index 24abfd9e09..eb500b0f6c 100644 --- a/codex-rs/core/src/mcp_tool_call_tests.rs +++ b/codex-rs/core/src/mcp_tool_call_tests.rs @@ -78,6 +78,7 @@ fn approval_metadata( McpToolApprovalMetadata { annotations: None, connector_id: connector_id.map(str::to_string), + link_id: None, connector_name: connector_name.map(str::to_string), connector_description: connector_description.map(str::to_string), plugin_id: None, @@ -1149,8 +1150,39 @@ async fn plugin_mcp_tool_call_request_meta_includes_plugin_id() { ); } +#[test] +fn mcp_tool_call_item_metadata_only_trusts_codex_apps_identity() { + let mut metadata = approval_metadata( + Some("asdk_app_0123456789abcdef0123456789abcdef"), + /*connector_name*/ None, + /*connector_description*/ None, + /*tool_title*/ None, + /*tool_description*/ None, + ); + metadata.link_id = Some("link_fedcba9876543210fedcba9876543210".to_string()); + + assert_eq!( + McpToolCallItemMetadata::from_tool_metadata(CODEX_APPS_MCP_SERVER_NAME, Some(&metadata),), + McpToolCallItemMetadata { + connector_id: Some("asdk_app_0123456789abcdef0123456789abcdef".to_string()), + link_id: Some("link_fedcba9876543210fedcba9876543210".to_string()), + mcp_app_resource_uri: None, + plugin_id: None, + } + ); + assert_eq!( + McpToolCallItemMetadata::from_tool_metadata("custom_server", Some(&metadata)), + McpToolCallItemMetadata { + connector_id: None, + link_id: None, + mcp_app_resource_uri: None, + plugin_id: None, + } + ); +} + #[tokio::test] -async fn mcp_tool_call_item_includes_plugin_id() { +async fn mcp_tool_call_item_includes_app_identity() { let (session, turn_context, rx_event) = make_session_and_context_with_rx().await; notify_mcp_tool_call_started( @@ -1158,11 +1190,13 @@ async fn mcp_tool_call_item_includes_plugin_id() { &turn_context, "call-plugin", McpInvocation { - server: "sample".to_string(), + server: CODEX_APPS_MCP_SERVER_NAME.to_string(), tool: "echo".to_string(), arguments: None, }, McpToolCallItemMetadata { + connector_id: Some("asdk_app_0123456789abcdef0123456789abcdef".to_string()), + link_id: Some("link_fedcba9876543210fedcba9876543210".to_string()), mcp_app_resource_uri: None, plugin_id: Some("sample@test".to_string()), }, @@ -1180,6 +1214,14 @@ async fn mcp_tool_call_item_includes_plugin_id() { panic!("expected MCP tool call item"); }; + assert_eq!( + item.connector_id.as_deref(), + Some("asdk_app_0123456789abcdef0123456789abcdef") + ); + assert_eq!( + item.link_id.as_deref(), + Some("link_fedcba9876543210fedcba9876543210") + ); assert_eq!(item.plugin_id.as_deref(), Some("sample@test")); } @@ -1193,6 +1235,7 @@ async fn codex_apps_tool_call_request_meta_includes_turn_metadata_and_codex_apps let metadata = McpToolApprovalMetadata { annotations: None, connector_id: Some("calendar".to_string()), + link_id: None, connector_name: Some("Calendar".to_string()), connector_description: Some("Manage events".to_string()), plugin_id: None, @@ -1661,6 +1704,7 @@ fn guardian_mcp_review_request_includes_annotations_when_present() { let metadata = McpToolApprovalMetadata { annotations: Some(annotations(Some(false), Some(true), Some(true))), connector_id: None, + link_id: None, connector_name: None, connector_description: None, plugin_id: None, @@ -2326,6 +2370,7 @@ async fn approve_mode_skips_when_annotations_do_not_require_approval() { /*open_world*/ None, )), connector_id: None, + link_id: None, connector_name: None, connector_description: None, plugin_id: None, @@ -2400,6 +2445,7 @@ async fn guardian_mode_skips_auto_when_annotations_do_not_require_approval() { /*open_world*/ None, )), connector_id: None, + link_id: None, connector_name: None, connector_description: None, plugin_id: None, @@ -2457,6 +2503,7 @@ async fn permission_request_hook_allows_mcp_tool_call() { /*open_world*/ None, )), connector_id: None, + link_id: None, connector_name: None, connector_description: None, plugin_id: None, @@ -2593,6 +2640,7 @@ async fn permission_request_hook_runs_after_remembered_mcp_approval() { /*open_world*/ None, )), connector_id: None, + link_id: None, connector_name: None, connector_description: None, plugin_id: None, @@ -2680,6 +2728,7 @@ async fn guardian_mode_mcp_denial_returns_rationale_message() { let metadata = McpToolApprovalMetadata { annotations: Some(annotations(Some(false), Some(true), Some(true))), connector_id: None, + link_id: None, connector_name: None, connector_description: None, plugin_id: None, @@ -2734,6 +2783,7 @@ async fn prompt_mode_waits_for_approval_when_annotations_do_not_require_approval /*open_world*/ None, )), connector_id: None, + link_id: None, connector_name: None, connector_description: None, plugin_id: None, @@ -2789,6 +2839,7 @@ async fn full_access_mode_skips_mcp_tool_approval_for_all_approval_modes() { let metadata = McpToolApprovalMetadata { annotations: Some(annotations(Some(false), Some(true), Some(true))), connector_id: Some("calendar".to_string()), + link_id: None, connector_name: Some("Calendar".to_string()), connector_description: Some("Manage events".to_string()), plugin_id: None, @@ -2842,6 +2893,7 @@ async fn approve_mode_skips_guardian_in_every_permission_mode() { let metadata = McpToolApprovalMetadata { annotations: Some(annotations(Some(false), Some(true), Some(true))), connector_id: Some("calendar".to_string()), + link_id: None, connector_name: Some("Calendar".to_string()), connector_description: Some("Manage events".to_string()), plugin_id: None, diff --git a/codex-rs/core/src/tools/handlers/mcp_resource.rs b/codex-rs/core/src/tools/handlers/mcp_resource.rs index e2f83859b6..2f97c02a42 100644 --- a/codex-rs/core/src/tools/handlers/mcp_resource.rs +++ b/codex-rs/core/src/tools/handlers/mcp_resource.rs @@ -203,7 +203,9 @@ async fn emit_tool_call_begin( server, tool, arguments: arguments.unwrap_or(Value::Null), + connector_id: None, mcp_app_resource_uri: None, + link_id: None, plugin_id: None, status: McpToolCallStatus::InProgress, result: None, @@ -242,7 +244,9 @@ async fn emit_tool_call_end( server, tool, arguments: arguments.unwrap_or(Value::Null), + connector_id: None, mcp_app_resource_uri: None, + link_id: None, plugin_id: None, status, result, diff --git a/codex-rs/core/tests/common/apps_test_server.rs b/codex-rs/core/tests/common/apps_test_server.rs index 0e7141281c..8a671ea5bc 100644 --- a/codex-rs/core/tests/common/apps_test_server.rs +++ b/codex-rs/core/tests/common/apps_test_server.rs @@ -17,6 +17,7 @@ use wiremock::matchers::path; use wiremock::matchers::path_regex; const CONNECTOR_ID: &str = "calendar"; +pub const LINK_ID: &str = "link_calendar"; const CONNECTOR_NAME: &str = "Calendar"; const DISCOVERABLE_CALENDAR_ID: &str = "connector_2128aebfecb84f64a069897515042a44"; const DISCOVERABLE_GMAIL_ID: &str = "connector_68df038e0ba48191908c8434991bbac2"; @@ -352,6 +353,7 @@ impl Respond for CodexAppsJsonRpcResponder { }, "_meta": { "connector_id": CONNECTOR_ID, + "link_id": LINK_ID, "connector_name": self.connector_name.clone(), "connector_description": self.connector_description.clone(), "openai/outputTemplate": CALENDAR_CREATE_EVENT_MCP_APP_RESOURCE_URI, @@ -378,6 +380,7 @@ impl Respond for CodexAppsJsonRpcResponder { }, "_meta": { "connector_id": CONNECTOR_ID, + "link_id": LINK_ID, "connector_name": self.connector_name.clone(), "connector_description": self.connector_description.clone(), "_codex_apps": { @@ -410,6 +413,7 @@ impl Respond for CodexAppsJsonRpcResponder { }, "_meta": { "connector_id": CONNECTOR_ID, + "link_id": LINK_ID, "connector_name": self.connector_name.clone(), "connector_description": self.connector_description.clone(), "openai/fileParams": ["file"], diff --git a/codex-rs/core/tests/suite/rmcp_client.rs b/codex-rs/core/tests/suite/rmcp_client.rs index 2c64b5fba2..dde226a194 100644 --- a/codex-rs/core/tests/suite/rmcp_client.rs +++ b/codex-rs/core/tests/suite/rmcp_client.rs @@ -1337,7 +1337,9 @@ async fn stdio_image_responses_round_trip() -> anyhow::Result<()> { tool: "image".to_string(), arguments: Some(json!({})), }, + connector_id: None, mcp_app_resource_uri: None, + link_id: None, plugin_id: None, }, ); diff --git a/codex-rs/core/tests/suite/search_tool.rs b/codex-rs/core/tests/suite/search_tool.rs index 284ee49b8f..7228547725 100644 --- a/codex-rs/core/tests/suite/search_tool.rs +++ b/codex-rs/core/tests/suite/search_tool.rs @@ -25,6 +25,7 @@ use core_test_support::apps_test_server::CALENDAR_CREATE_EVENT_MCP_APP_RESOURCE_ use core_test_support::apps_test_server::CALENDAR_CREATE_EVENT_RESOURCE_URI; use core_test_support::apps_test_server::DIRECT_CALENDAR_CREATE_EVENT_TOOL as CALENDAR_CREATE_TOOL; use core_test_support::apps_test_server::DIRECT_CALENDAR_LIST_EVENTS_TOOL as CALENDAR_LIST_TOOL; +use core_test_support::apps_test_server::LINK_ID; use core_test_support::apps_test_server::SEARCH_CALENDAR_APP_ONLY_TOOL; use core_test_support::apps_test_server::SEARCH_CALENDAR_CREATE_TOOL; use core_test_support::apps_test_server::SEARCH_CALENDAR_LIST_TOOL; @@ -559,10 +560,12 @@ async fn tool_search_returns_deferred_tools_without_follow_up_tool_injection() - unreachable!("event guard guarantees McpToolCallEnd"); }; assert_eq!(end.call_id, "calendar-call-1"); + assert_eq!(end.connector_id.as_deref(), Some("calendar")); assert_eq!( end.mcp_app_resource_uri.as_deref(), Some(CALENDAR_CREATE_EVENT_MCP_APP_RESOURCE_URI) ); + assert_eq!(end.link_id.as_deref(), Some(LINK_ID)); assert_eq!( end.invocation, McpInvocation { diff --git a/codex-rs/exec/src/event_processor_with_jsonl_output_tests.rs b/codex-rs/exec/src/event_processor_with_jsonl_output_tests.rs index f8521071e3..83cd6d3c6b 100644 --- a/codex-rs/exec/src/event_processor_with_jsonl_output_tests.rs +++ b/codex-rs/exec/src/event_processor_with_jsonl_output_tests.rs @@ -98,6 +98,7 @@ fn mcp_tool_call_result_preserves_meta_in_jsonl_event() { tool: "web_run".to_string(), status: McpToolCallStatus::Completed, arguments: json!({"search_query": [{"q": "OpenAI Codex CLI documentation"}]}), + app_context: None, mcp_app_resource_uri: None, plugin_id: None, result: Some(Box::new(codex_app_server_protocol::McpToolCallResult { diff --git a/codex-rs/exec/tests/event_processor_with_json_output.rs b/codex-rs/exec/tests/event_processor_with_json_output.rs index 0f88670940..1f44ca31a8 100644 --- a/codex-rs/exec/tests/event_processor_with_json_output.rs +++ b/codex-rs/exec/tests/event_processor_with_json_output.rs @@ -478,6 +478,7 @@ fn mcp_tool_call_begin_and_end_emit_item_events() { tool: "tool_x".to_string(), status: ApiMcpToolCallStatus::InProgress, arguments: json!({ "key": "value" }), + app_context: None, mcp_app_resource_uri: None, plugin_id: None, result: None, @@ -496,6 +497,7 @@ fn mcp_tool_call_begin_and_end_emit_item_events() { tool: "tool_x".to_string(), status: ApiMcpToolCallStatus::Completed, arguments: json!({ "key": "value" }), + app_context: None, mcp_app_resource_uri: None, plugin_id: None, result: Some(Box::new(McpToolCallResult { @@ -568,6 +570,7 @@ fn mcp_tool_call_failure_sets_failed_status() { tool: "tool_y".to_string(), status: ApiMcpToolCallStatus::Failed, arguments: json!({ "param": 42 }), + app_context: None, mcp_app_resource_uri: None, plugin_id: None, result: None, @@ -617,6 +620,7 @@ fn mcp_tool_call_defaults_arguments_and_preserves_structured_content() { tool: "tool_z".to_string(), status: ApiMcpToolCallStatus::InProgress, arguments: serde_json::Value::Null, + app_context: None, mcp_app_resource_uri: None, plugin_id: None, result: None, @@ -635,6 +639,7 @@ fn mcp_tool_call_defaults_arguments_and_preserves_structured_content() { tool: "tool_z".to_string(), status: ApiMcpToolCallStatus::Completed, arguments: serde_json::Value::Null, + app_context: None, mcp_app_resource_uri: None, plugin_id: None, result: Some(Box::new(McpToolCallResult { diff --git a/codex-rs/protocol/src/items.rs b/codex-rs/protocol/src/items.rs index 01f44576ac..08d257dc3b 100644 --- a/codex-rs/protocol/src/items.rs +++ b/codex-rs/protocol/src/items.rs @@ -188,9 +188,15 @@ pub struct McpToolCallItem { pub arguments: serde_json::Value, #[serde(default, skip_serializing_if = "Option::is_none")] #[ts(optional)] + pub connector_id: Option, + #[serde(default, skip_serializing_if = "Option::is_none")] + #[ts(optional)] pub mcp_app_resource_uri: Option, #[serde(default, skip_serializing_if = "Option::is_none")] #[ts(optional)] + pub link_id: Option, + #[serde(default, skip_serializing_if = "Option::is_none")] + #[ts(optional)] pub plugin_id: Option, pub status: McpToolCallStatus, #[serde(default, skip_serializing_if = "Option::is_none")] @@ -546,7 +552,9 @@ impl McpToolCallItem { tool: self.tool.clone(), arguments: (!self.arguments.is_null()).then(|| self.arguments.clone()), }, + connector_id: self.connector_id.clone(), mcp_app_resource_uri: self.mcp_app_resource_uri.clone(), + link_id: self.link_id.clone(), plugin_id: self.plugin_id.clone(), }) } @@ -566,6 +574,8 @@ impl McpToolCallItem { arguments: (!self.arguments.is_null()).then(|| self.arguments.clone()), }, mcp_app_resource_uri: self.mcp_app_resource_uri.clone(), + connector_id: self.connector_id.clone(), + link_id: self.link_id.clone(), plugin_id: self.plugin_id.clone(), duration: self.duration?, result, diff --git a/codex-rs/protocol/src/protocol.rs b/codex-rs/protocol/src/protocol.rs index a71eb64fe4..258150aa84 100644 --- a/codex-rs/protocol/src/protocol.rs +++ b/codex-rs/protocol/src/protocol.rs @@ -2324,9 +2324,15 @@ pub struct McpToolCallBeginEvent { pub invocation: McpInvocation, #[serde(default, skip_serializing_if = "Option::is_none")] #[ts(optional)] + pub connector_id: Option, + #[serde(default, skip_serializing_if = "Option::is_none")] + #[ts(optional)] pub mcp_app_resource_uri: Option, #[serde(default, skip_serializing_if = "Option::is_none")] #[ts(optional)] + pub link_id: Option, + #[serde(default, skip_serializing_if = "Option::is_none")] + #[ts(optional)] pub plugin_id: Option, } @@ -2337,9 +2343,15 @@ pub struct McpToolCallEndEvent { pub invocation: McpInvocation, #[serde(default, skip_serializing_if = "Option::is_none")] #[ts(optional)] + pub connector_id: Option, + #[serde(default, skip_serializing_if = "Option::is_none")] + #[ts(optional)] pub mcp_app_resource_uri: Option, #[serde(default, skip_serializing_if = "Option::is_none")] #[ts(optional)] + pub link_id: Option, + #[serde(default, skip_serializing_if = "Option::is_none")] + #[ts(optional)] pub plugin_id: Option, #[ts(type = "string")] pub duration: Duration, @@ -4940,7 +4952,9 @@ mod tests { server: "server".into(), tool: "tool".into(), arguments: json!({"arg": "value"}), + connector_id: Some("connector".into()), mcp_app_resource_uri: Some("app://connector".into()), + link_id: Some("link_123".into()), plugin_id: Some("sample@test".into()), status: McpToolCallStatus::InProgress, result: None, @@ -4956,10 +4970,12 @@ mod tests { assert_eq!(event.call_id, "mcp-1"); assert_eq!(event.invocation.server, "server"); assert_eq!(event.invocation.tool, "tool"); + assert_eq!(event.connector_id.as_deref(), Some("connector")); assert_eq!( event.mcp_app_resource_uri.as_deref(), Some("app://connector") ); + assert_eq!(event.link_id.as_deref(), Some("link_123")); assert_eq!(event.plugin_id.as_deref(), Some("sample@test")); } _ => panic!("expected McpToolCallBegin event"), @@ -5047,7 +5063,9 @@ mod tests { server: "server".into(), tool: "tool".into(), arguments: json!({"arg": "value"}), + connector_id: Some("connector".into()), mcp_app_resource_uri: Some("app://connector".into()), + link_id: Some("link_123".into()), plugin_id: Some("sample@test".into()), status: McpToolCallStatus::Completed, result: Some(CallToolResult { @@ -5068,10 +5086,12 @@ mod tests { assert_eq!(event.call_id, "mcp-1"); assert_eq!(event.invocation.server, "server"); assert_eq!(event.invocation.tool, "tool"); + assert_eq!(event.connector_id.as_deref(), Some("connector")); assert_eq!( event.mcp_app_resource_uri.as_deref(), Some("app://connector") ); + assert_eq!(event.link_id.as_deref(), Some("link_123")); assert_eq!(event.plugin_id.as_deref(), Some("sample@test")); assert_eq!(event.duration, Duration::from_millis(42)); assert!(event.is_success()); diff --git a/codex-rs/tui/src/chatwidget/tests/history_replay.rs b/codex-rs/tui/src/chatwidget/tests/history_replay.rs index e6b9d4ede0..f8626dfcc0 100644 --- a/codex-rs/tui/src/chatwidget/tests/history_replay.rs +++ b/codex-rs/tui/src/chatwidget/tests/history_replay.rs @@ -973,6 +973,7 @@ async fn replayed_in_progress_mcp_tool_call_stays_active() { tool: "copilot".to_string(), status: codex_app_server_protocol::McpToolCallStatus::InProgress, arguments: json!({"action": "wait"}), + app_context: None, mcp_app_resource_uri: None, plugin_id: None, result: None, From ec848dde0e0b264ee6d703a43baeeae624bba98e Mon Sep 17 00:00:00 2001 From: Adrian <145513011+adrian-openai@users.noreply.github.com> Date: Thu, 18 Jun 2026 14:05:27 -0700 Subject: [PATCH 02/10] feat: opt ChatGPT auth into agent identity (#19049) ## Stack This is PR 2 of the simplified HAI single-run-task stack: - [#19047](https://github.com/openai/codex/pull/19047) Agent Identity assertion and task-registration primitives, including the shared run-task helper used by existing Agent Identity JWT auth. - [#19049](https://github.com/openai/codex/pull/19049) Disabled-by-default ChatGPT auth opt-in that provisions/reuses persisted Agent Identity runtime auth and its single run task. - [#19051](https://github.com/openai/codex/pull/19051) Run-scoped provider auth that uses one backend-owned task id for first-party inference and compaction requests. [#19054](https://github.com/openai/codex/pull/19054) collapsed out of the active stack because the simplified design no longer needs a separate background/control-plane task helper. ## Summary This PR adds the disabled-by-default path for normal ChatGPT-login Codex sessions to obtain Agent Identity runtime auth through the Codex backend. Existing Agent Identity JWT startup mode remains a separate path and does not require the feature flag. What changed: - adds the experimental `use_agent_identity` feature flag and config schema entry - adds an explicit `AgentIdentityAuthPolicy` so call sites choose `JwtOnly` or `ChatGptAuth` instead of passing a bare boolean - stores standalone Agent Identity JWT credentials separately from backend-registered Agent Identity records - persists the registered Agent Identity record, private key, and single run task id in `auth.json` so process restarts reuse the same identity - derives the agent/task registration base URL from ChatGPT/Codex auth config while keeping JWT JWKS lookup separate - provisions and caches ChatGPT-derived Agent Identity runtime auth when `use_agent_identity` is enabled - reuses the shared run-task registration helper from PR1 rather than adding a second task-registration path This PR intentionally does not switch model inference over to `AgentAssertion` auth. The provider-auth integration lands in the next PR. ## Testing - `just test -p codex-login` --- codex-rs/Cargo.lock | 1 + .../src/transport/remote_control/tests.rs | 3 + .../remote_control/tests/clients_tests.rs | 2 + .../remote_control/tests/pairing_tests.rs | 2 + .../src/transport/remote_control/websocket.rs | 3 + codex-rs/cli/src/doctor.rs | 4 +- codex-rs/cli/src/main.rs | 9 +- codex-rs/cloud-config/Cargo.toml | 1 + codex-rs/cloud-config/src/bundle_loader.rs | 1 + codex-rs/cloud-config/src/service_tests.rs | 52 ++ codex-rs/cloud-tasks/src/util.rs | 1 + codex-rs/core/config.schema.json | 6 + codex-rs/exec/src/lib.rs | 1 - codex-rs/features/src/lib.rs | 8 + codex-rs/login/src/auth/agent_identity.rs | 422 ++++++++++++-- codex-rs/login/src/auth/auth_tests.rs | 523 +++++++++++++++++- .../login/src/auth/bedrock_api_key_tests.rs | 3 + codex-rs/login/src/auth/manager.rs | 417 +++++++++++--- codex-rs/login/src/auth/storage.rs | 31 +- codex-rs/login/src/auth/storage_tests.rs | 37 +- codex-rs/login/src/lib.rs | 1 + codex-rs/login/tests/suite/auth_refresh.rs | 1 + codex-rs/login/tests/suite/logout.rs | 1 + codex-rs/tui/src/lib.rs | 1 - 24 files changed, 1359 insertions(+), 172 deletions(-) diff --git a/codex-rs/Cargo.lock b/codex-rs/Cargo.lock index 0fae5a41bf..55d02cd8b1 100644 --- a/codex-rs/Cargo.lock +++ b/codex-rs/Cargo.lock @@ -2418,6 +2418,7 @@ version = "0.0.0" dependencies = [ "base64 0.22.1", "chrono", + "codex-agent-identity", "codex-backend-client", "codex-config", "codex-core", diff --git a/codex-rs/app-server-transport/src/transport/remote_control/tests.rs b/codex-rs/app-server-transport/src/transport/remote_control/tests.rs index 8e75c0fc8a..6c453e6f9d 100644 --- a/codex-rs/app-server-transport/src/transport/remote_control/tests.rs +++ b/codex-rs/app-server-transport/src/transport/remote_control/tests.rs @@ -1038,6 +1038,7 @@ async fn remote_control_start_allows_missing_auth_when_enabled() { codex_home.path().to_path_buf(), /*enable_codex_api_key_env*/ false, AuthCredentialsStoreMode::File, + /*forced_chatgpt_workspace_id*/ None, /*chatgpt_base_url*/ None, AuthKeyringBackendKind::default(), ) @@ -1866,6 +1867,7 @@ async fn remote_control_waits_for_account_id_before_enrolling() { codex_home.path().to_path_buf(), /*enable_codex_api_key_env*/ false, AuthCredentialsStoreMode::File, + /*forced_chatgpt_workspace_id*/ None, /*chatgpt_base_url*/ None, AuthKeyringBackendKind::default(), ) @@ -1961,6 +1963,7 @@ async fn persisted_enable_does_not_follow_auth_to_an_account_without_a_preferenc codex_home.path().to_path_buf(), /*enable_codex_api_key_env*/ false, AuthCredentialsStoreMode::File, + /*forced_chatgpt_workspace_id*/ None, /*chatgpt_base_url*/ None, AuthKeyringBackendKind::default(), ) diff --git a/codex-rs/app-server-transport/src/transport/remote_control/tests/clients_tests.rs b/codex-rs/app-server-transport/src/transport/remote_control/tests/clients_tests.rs index 630ce8c6b4..6354e435c9 100644 --- a/codex-rs/app-server-transport/src/transport/remote_control/tests/clients_tests.rs +++ b/codex-rs/app-server-transport/src/transport/remote_control/tests/clients_tests.rs @@ -182,6 +182,7 @@ async fn list_remote_control_clients_recovers_auth_after_unauthorized() { codex_home.path().to_path_buf(), /*enable_codex_api_key_env*/ false, AuthCredentialsStoreMode::File, + /*forced_chatgpt_workspace_id*/ None, /*chatgpt_base_url*/ None, AuthKeyringBackendKind::default(), ) @@ -266,6 +267,7 @@ async fn list_remote_control_clients_retries_unauthorized_only_once() { codex_home.path().to_path_buf(), /*enable_codex_api_key_env*/ false, AuthCredentialsStoreMode::File, + /*forced_chatgpt_workspace_id*/ None, /*chatgpt_base_url*/ None, AuthKeyringBackendKind::default(), ) diff --git a/codex-rs/app-server-transport/src/transport/remote_control/tests/pairing_tests.rs b/codex-rs/app-server-transport/src/transport/remote_control/tests/pairing_tests.rs index ff146492fb..190526d720 100644 --- a/codex-rs/app-server-transport/src/transport/remote_control/tests/pairing_tests.rs +++ b/codex-rs/app-server-transport/src/transport/remote_control/tests/pairing_tests.rs @@ -536,6 +536,7 @@ async fn remote_control_handle_recovers_auth_before_refreshing_pairing() { codex_home.path().to_path_buf(), /*enable_codex_api_key_env*/ false, AuthCredentialsStoreMode::File, + /*forced_chatgpt_workspace_id*/ None, /*chatgpt_base_url*/ None, AuthKeyringBackendKind::default(), ) @@ -804,6 +805,7 @@ async fn remote_control_handle_discards_pairing_response_after_auth_change() { codex_home.path().to_path_buf(), /*enable_codex_api_key_env*/ false, AuthCredentialsStoreMode::File, + /*forced_chatgpt_workspace_id*/ None, /*chatgpt_base_url*/ None, AuthKeyringBackendKind::default(), ) diff --git a/codex-rs/app-server-transport/src/transport/remote_control/websocket.rs b/codex-rs/app-server-transport/src/transport/remote_control/websocket.rs index 3f594a1b4e..239d5d9ea7 100644 --- a/codex-rs/app-server-transport/src/transport/remote_control/websocket.rs +++ b/codex-rs/app-server-transport/src/transport/remote_control/websocket.rs @@ -2211,6 +2211,7 @@ mod tests { codex_home.path().to_path_buf(), /*enable_codex_api_key_env*/ false, AuthCredentialsStoreMode::File, + /*forced_chatgpt_workspace_id*/ None, /*chatgpt_base_url*/ None, AuthKeyringBackendKind::default(), ) @@ -2308,6 +2309,7 @@ mod tests { codex_home.path().to_path_buf(), /*enable_codex_api_key_env*/ false, AuthCredentialsStoreMode::File, + /*forced_chatgpt_workspace_id*/ None, /*chatgpt_base_url*/ None, AuthKeyringBackendKind::default(), ) @@ -2430,6 +2432,7 @@ mod tests { codex_home.path().to_path_buf(), /*enable_codex_api_key_env*/ false, AuthCredentialsStoreMode::File, + /*forced_chatgpt_workspace_id*/ None, /*chatgpt_base_url*/ None, AuthKeyringBackendKind::default(), ) diff --git a/codex-rs/cli/src/doctor.rs b/codex-rs/cli/src/doctor.rs index c0f310e700..5e5a1e94be 100644 --- a/codex-rs/cli/src/doctor.rs +++ b/codex-rs/cli/src/doctor.rs @@ -1399,8 +1399,8 @@ fn stored_auth_issues( codex_app_server_protocol::AuthMode::AgentIdentity => { if auth .agent_identity - .as_deref() - .is_none_or(|token| token.trim().is_empty()) + .as_ref() + .is_none_or(|agent_identity| !agent_identity.has_auth_material()) { issues.push("agent identity auth is missing an agent identity token"); } diff --git a/codex-rs/cli/src/main.rs b/codex-rs/cli/src/main.rs index 8b633265e5..9d278c2122 100644 --- a/codex-rs/cli/src/main.rs +++ b/codex-rs/cli/src/main.rs @@ -1738,12 +1738,9 @@ async fn load_exec_server_remote_auth_provider( let agent_identity_jwt = read_codex_access_token_from_env().ok_or_else(|| { anyhow::anyhow!("CODEX_ACCESS_TOKEN is required when --use-agent-identity-auth is set") })?; - let auth = CodexAuth::from_agent_identity_jwt( - &agent_identity_jwt, - Some(&config.chatgpt_base_url), - /*agent_identity_authapi_base_url_override*/ None, - ) - .await?; + let auth = + CodexAuth::from_agent_identity_jwt(&agent_identity_jwt, Some(&config.chatgpt_base_url)) + .await?; return Ok(codex_model_provider::auth_provider_from_auth(&auth)); } diff --git a/codex-rs/cloud-config/Cargo.toml b/codex-rs/cloud-config/Cargo.toml index 6bf58c8399..363cb21b6f 100644 --- a/codex-rs/cloud-config/Cargo.toml +++ b/codex-rs/cloud-config/Cargo.toml @@ -25,6 +25,7 @@ tokio = { workspace = true, features = ["fs", "rt", "sync", "time"] } tracing = { workspace = true } [dev-dependencies] +codex-agent-identity = { workspace = true } pretty_assertions = { workspace = true } tempfile = { workspace = true } tokio = { workspace = true, features = ["macros", "rt", "test-util", "time"] } diff --git a/codex-rs/cloud-config/src/bundle_loader.rs b/codex-rs/cloud-config/src/bundle_loader.rs index 2d76d29a69..5ec1ab260f 100644 --- a/codex-rs/cloud-config/src/bundle_loader.rs +++ b/codex-rs/cloud-config/src/bundle_loader.rs @@ -63,6 +63,7 @@ pub async fn cloud_config_bundle_loader_for_storage( codex_home.clone(), enable_codex_api_key_env, credentials_store_mode, + /*forced_chatgpt_workspace_id*/ None, Some(chatgpt_base_url.clone()), keyring_backend_kind, ) diff --git a/codex-rs/cloud-config/src/service_tests.rs b/codex-rs/cloud-config/src/service_tests.rs index d63fef4c54..96fed2f589 100644 --- a/codex-rs/cloud-config/src/service_tests.rs +++ b/codex-rs/cloud-config/src/service_tests.rs @@ -17,6 +17,8 @@ use codex_config::CloudRequirementsFragment; use codex_config::CloudRequirementsTomlBundle; use codex_config::types::AuthCredentialsStoreMode; use codex_login::AuthKeyringBackendKind; +use codex_login::auth::AgentIdentityAuth; +use codex_login::auth::AgentIdentityAuthRecord; use pretty_assertions::assert_eq; use serde_json::json; use std::collections::VecDeque; @@ -48,6 +50,7 @@ async fn auth_manager_with_api_key() -> Arc { tmp.path().to_path_buf(), /*enable_codex_api_key_env*/ false, AuthCredentialsStoreMode::File, + /*forced_chatgpt_workspace_id*/ None, /*chatgpt_base_url*/ None, AuthKeyringBackendKind::default(), ) @@ -77,6 +80,7 @@ async fn auth_manager_with_plan_and_identity( tmp.path().to_path_buf(), /*enable_codex_api_key_env*/ false, AuthCredentialsStoreMode::File, + /*forced_chatgpt_workspace_id*/ None, /*chatgpt_base_url*/ None, AuthKeyringBackendKind::default(), ) @@ -88,6 +92,28 @@ async fn auth_manager_with_plan(plan_type: &str) -> Arc { auth_manager_with_plan_and_identity(plan_type, Some("user-12345"), Some("account-12345")).await } +async fn auth_manager_with_agent_identity_business_plan() -> Arc { + let key_material = + codex_agent_identity::generate_agent_key_material().expect("generate agent key material"); + AuthManager::from_auth_for_testing(CodexAuth::AgentIdentity( + AgentIdentityAuth::from_record( + AgentIdentityAuthRecord { + agent_runtime_id: "agent-runtime-123".to_string(), + agent_private_key: key_material.private_key_pkcs8_base64, + account_id: "account-12345".to_string(), + chatgpt_user_id: "user-12345".to_string(), + email: "user@example.com".to_string(), + plan_type: PlanType::Business, + chatgpt_account_is_fedramp: false, + task_id: Some("task-123".to_string()), + }, + "https://auth.openai.com/api/accounts", + ) + .await + .expect("agent identity record should be complete"), + )) +} + fn chatgpt_auth_json( plan_type: &str, chatgpt_user_id: Option<&str>, @@ -408,6 +434,28 @@ async fn get_bundle_allows_eligible_workspace_plans_and_writes_cache() { } } +#[tokio::test] +async fn get_bundle_allows_agent_identity_business_plan() { + let bundle = test_bundle(); + let fetcher = Arc::new(StaticBundleClient::new(bundle.clone())); + let codex_home = tempdir().expect("tempdir"); + let service = CloudConfigBundleService::new( + auth_manager_with_agent_identity_business_plan().await, + fetcher.clone(), + codex_home.path().to_path_buf(), + CLOUD_CONFIG_BUNDLE_TIMEOUT, + ); + + assert_eq!(service.load_startup_bundle().await, Ok(Some(bundle))); + assert_eq!(fetcher.request_count.load(Ordering::SeqCst), 1); + assert!( + codex_home + .path() + .join(CLOUD_CONFIG_BUNDLE_CACHE_FILENAME) + .exists() + ); +} + #[tokio::test] async fn get_bundle_skips_team_like_usage_based_plan() { let fetcher = Arc::new(StaticBundleClient::new(test_bundle())); @@ -635,6 +683,7 @@ async fn get_bundle_recovers_after_unauthorized_reload() { auth_home.path().to_path_buf(), /*enable_codex_api_key_env*/ false, AuthCredentialsStoreMode::File, + /*forced_chatgpt_workspace_id*/ None, /*chatgpt_base_url*/ None, AuthKeyringBackendKind::default(), ) @@ -690,6 +739,7 @@ async fn get_bundle_recovers_after_unauthorized_reload_updates_cache_identity() auth_home.path().to_path_buf(), /*enable_codex_api_key_env*/ false, AuthCredentialsStoreMode::File, + /*forced_chatgpt_workspace_id*/ None, /*chatgpt_base_url*/ None, AuthKeyringBackendKind::default(), ) @@ -753,6 +803,7 @@ async fn get_bundle_surfaces_auth_recovery_message() { auth_home.path().to_path_buf(), /*enable_codex_api_key_env*/ false, AuthCredentialsStoreMode::File, + /*forced_chatgpt_workspace_id*/ None, /*chatgpt_base_url*/ None, AuthKeyringBackendKind::default(), ) @@ -818,6 +869,7 @@ async fn get_bundle_unauthorized_without_recovery_uses_generic_message() { auth_home.path().to_path_buf(), /*enable_codex_api_key_env*/ false, AuthCredentialsStoreMode::File, + /*forced_chatgpt_workspace_id*/ None, /*chatgpt_base_url*/ None, AuthKeyringBackendKind::default(), ) diff --git a/codex-rs/cloud-tasks/src/util.rs b/codex-rs/cloud-tasks/src/util.rs index f683c58a4b..65db955f15 100644 --- a/codex-rs/cloud-tasks/src/util.rs +++ b/codex-rs/cloud-tasks/src/util.rs @@ -49,6 +49,7 @@ pub async fn load_auth_manager(chatgpt_base_url: Option) -> Option anyhow::Result forced_login_method: config.forced_login_method, forced_chatgpt_workspace_id: config.forced_chatgpt_workspace_id.clone(), chatgpt_base_url: Some(config.chatgpt_base_url.clone()), - agent_identity_authapi_base_url: None, }) .await { diff --git a/codex-rs/features/src/lib.rs b/codex-rs/features/src/lib.rs index 58f291407a..56acb213e5 100644 --- a/codex-rs/features/src/lib.rs +++ b/codex-rs/features/src/lib.rs @@ -228,6 +228,8 @@ pub enum Feature { PreventIdleSleep, /// Enable remote compaction v2 over the normal Responses API. RemoteCompactionV2, + /// Use Agent Identity for ChatGPT-authenticated sessions. + UseAgentIdentity, /// Enable workspace dependency support. WorkspaceDependencies, @@ -1314,6 +1316,12 @@ pub const FEATURES: &[FeatureSpec] = &[ stage: Stage::Stable, default_enabled: true, }, + FeatureSpec { + id: Feature::UseAgentIdentity, + key: "use_agent_identity", + stage: Stage::UnderDevelopment, + default_enabled: false, + }, FeatureSpec { id: Feature::WorkspaceDependencies, key: "workspace_dependencies", diff --git a/codex-rs/login/src/auth/agent_identity.rs b/codex-rs/login/src/auth/agent_identity.rs index d4c4f3e513..370af883c0 100644 --- a/codex-rs/login/src/auth/agent_identity.rs +++ b/codex-rs/login/src/auth/agent_identity.rs @@ -1,83 +1,318 @@ +use std::future::Future; use std::sync::Arc; use codex_agent_identity::AgentIdentityKey; +use codex_agent_identity::ChatGptEnvironment; +use codex_agent_identity::build_abom; +use codex_agent_identity::decode_agent_identity_jwt; +use codex_agent_identity::fetch_agent_identity_jwks; +use codex_agent_identity::generate_agent_key_material; +use codex_agent_identity::is_retryable_registration_error; +use codex_agent_identity::public_key_ssh_from_private_key_pkcs8_base64; +use codex_agent_identity::register_agent_identity; use codex_agent_identity::register_agent_task; use codex_protocol::account::PlanType as AccountPlanType; +use codex_protocol::protocol::SessionSource; +use thiserror::Error; use crate::default_client::build_reqwest_client; use super::storage::AgentIdentityAuthRecord; -#[derive(Clone, Debug)] -pub struct AgentIdentityAuth { - inner: Arc, +pub(super) const MAX_AGENT_IDENTITY_BOOTSTRAP_ATTEMPTS: usize = 3; + +pub(super) fn agent_identity_authapi_base_url( + chatgpt_base_url: Option<&str>, +) -> std::io::Result { + let environment = match chatgpt_base_url { + Some(chatgpt_base_url) => ChatGptEnvironment::from_chatgpt_base_url(chatgpt_base_url) + .map_err(std::io::Error::other)?, + None => ChatGptEnvironment::default(), + }; + Ok(environment.agent_identity_authapi_base_url().to_string()) } -#[derive(Debug)] -struct AgentIdentityAuthInner { - record: AgentIdentityAuthRecord, - run_task_id: String, +pub(super) fn require_agent_identity_authapi_base_url( + agent_identity_authapi_base_url: Option<&str>, +) -> std::io::Result<&str> { + agent_identity_authapi_base_url.ok_or_else(|| { + std::io::Error::other( + "Agent Identity only supports production and staging ChatGPT environments", + ) + }) +} + +#[derive(Debug, Error)] +pub enum AgentIdentityAuthError { + #[error( + "agent identity bootstrap unavailable after {attempts} attempts during {operation}: {message}" + )] + BootstrapUnavailable { + operation: &'static str, + attempts: usize, + message: String, + }, +} + +impl AgentIdentityAuthError { + pub fn is_bootstrap_unavailable(error: &std::io::Error) -> bool { + matches!( + error + .get_ref() + .and_then(|source| source.downcast_ref::()), + Some(Self::BootstrapUnavailable { .. }) + ) + } +} + +#[derive(Debug, Error)] +#[error("retryable agent identity registration failure: {message}")] +pub(super) struct RetryableAgentIdentityRegistrationError { + message: String, +} + +impl RetryableAgentIdentityRegistrationError { + pub(super) fn new(message: String) -> Self { + Self { message } + } +} + +#[derive(Clone, Debug)] +pub struct AgentIdentityAuth { + record: Arc, +} + +#[derive(Debug, Clone, PartialEq, Eq)] +pub(super) struct ManagedChatGptAgentIdentityBinding { + pub(super) account_id: String, + pub(super) chatgpt_user_id: String, + pub(super) email: String, + pub(super) plan_type: AccountPlanType, + pub(super) chatgpt_account_is_fedramp: bool, + pub(super) access_token: String, } impl AgentIdentityAuth { - pub async fn load( - record: AgentIdentityAuthRecord, + pub async fn from_record( + mut record: AgentIdentityAuthRecord, agent_identity_authapi_base_url: &str, ) -> std::io::Result { - let run_task_id = register_agent_task( - &build_reqwest_client(), - agent_identity_authapi_base_url, - key_for_record(&record), - ) - .await - .map_err(std::io::Error::other)?; + public_key_ssh_from_private_key_pkcs8_base64(&record.agent_private_key) + .map_err(std::io::Error::other)?; + if record_needs_task_registration(&record) { + record.task_id = Some( + register_task_for_record_with_retries(&record, agent_identity_authapi_base_url) + .await?, + ); + } Ok(Self { - inner: Arc::new(AgentIdentityAuthInner { - record, - run_task_id, - }), + record: Arc::new(record), }) } + pub async fn from_jwt( + jwt: &str, + chatgpt_base_url: &str, + agent_identity_authapi_base_url: &str, + ) -> std::io::Result { + let record = verified_record_from_jwt(jwt, chatgpt_base_url).await?; + Self::from_record(record, agent_identity_authapi_base_url).await + } + #[cfg(test)] - fn from_initialized_record(record: AgentIdentityAuthRecord, run_task_id: String) -> Self { + fn from_initialized_record(mut record: AgentIdentityAuthRecord, run_task_id: String) -> Self { + record.task_id = Some(run_task_id); Self { - inner: Arc::new(AgentIdentityAuthInner { - record, - run_task_id, - }), + record: Arc::new(record), } } pub fn record(&self) -> &AgentIdentityAuthRecord { - &self.inner.record + self.record.as_ref() } pub fn run_task_id(&self) -> &str { - &self.inner.run_task_id + match self.record.task_id.as_deref() { + Some(task_id) => task_id, + None => unreachable!("AgentIdentityAuth should only be constructed with a task_id"), + } } pub fn account_id(&self) -> &str { - &self.inner.record.account_id + &self.record.account_id } pub fn chatgpt_user_id(&self) -> &str { - &self.inner.record.chatgpt_user_id + &self.record.chatgpt_user_id } pub fn email(&self) -> &str { - &self.inner.record.email + &self.record.email } pub fn plan_type(&self) -> AccountPlanType { - self.inner.record.plan_type + self.record.plan_type } pub fn is_fedramp_account(&self) -> bool { - self.inner.record.chatgpt_account_is_fedramp + self.record.chatgpt_account_is_fedramp } } +pub(super) async fn register_managed_chatgpt_agent_identity( + binding: ManagedChatGptAgentIdentityBinding, + agent_identity_authapi_base_url: &str, + session_source: SessionSource, +) -> std::io::Result { + let key_material = generate_agent_key_material().map_err(std::io::Error::other)?; + let client = build_reqwest_client(); + let runtime_id = retry_registration(|| async { + register_agent_identity( + &client, + agent_identity_authapi_base_url, + &binding.access_token, + binding.chatgpt_account_is_fedramp, + &key_material, + build_abom(session_source.clone()), + vec!["responsesapi".to_string()], + ) + .await + .map_err(|err| { + if is_retryable_registration_error(&err) { + std::io::Error::other(RetryableAgentIdentityRegistrationError::new( + err.to_string(), + )) + } else { + std::io::Error::other(err) + } + }) + }) + .await + .map_err(|err| classify_bootstrap_error("agent identity registration", err))?; + + let record = AgentIdentityAuthRecord { + agent_runtime_id: runtime_id, + agent_private_key: key_material.private_key_pkcs8_base64, + account_id: binding.account_id, + chatgpt_user_id: binding.chatgpt_user_id, + email: binding.email, + plan_type: binding.plan_type, + chatgpt_account_is_fedramp: binding.chatgpt_account_is_fedramp, + task_id: None, + }; + AgentIdentityAuth::from_record(record, agent_identity_authapi_base_url) + .await + .map_err(|err| classify_bootstrap_error("agent task registration", err)) +} + +pub(super) async fn verified_record_from_jwt( + jwt: &str, + chatgpt_base_url: &str, +) -> std::io::Result { + AgentIdentityAuthRecord::from_agent_identity_jwt(jwt)?; + let jwks = fetch_agent_identity_jwks(&build_reqwest_client(), chatgpt_base_url) + .await + .map_err(std::io::Error::other)?; + let claims = decode_agent_identity_jwt(jwt, Some(&jwks)).map_err(std::io::Error::other)?; + Ok(claims.into()) +} + +pub(super) fn record_needs_task_registration(record: &AgentIdentityAuthRecord) -> bool { + record + .task_id + .as_deref() + .is_none_or(|task_id| task_id.trim().is_empty()) +} + +pub(super) fn record_matches_managed_chatgpt_binding( + record: &AgentIdentityAuthRecord, + binding: &ManagedChatGptAgentIdentityBinding, +) -> bool { + record.account_id == binding.account_id + && record.chatgpt_user_id == binding.chatgpt_user_id + && public_key_ssh_from_private_key_pkcs8_base64(&record.agent_private_key).is_ok() +} + +pub(super) fn classify_bootstrap_error( + operation: &'static str, + err: std::io::Error, +) -> std::io::Error { + if is_retryable_io_registration_error(&err) { + std::io::Error::other(AgentIdentityAuthError::BootstrapUnavailable { + operation, + attempts: MAX_AGENT_IDENTITY_BOOTSTRAP_ATTEMPTS, + message: err.to_string(), + }) + } else { + err + } +} + +pub(super) fn is_retryable_io_registration_error(err: &std::io::Error) -> bool { + err.get_ref().is_some_and( + ::is::< + RetryableAgentIdentityRegistrationError, + >, + ) +} + +pub(super) async fn retry_registration(mut operation: F) -> std::io::Result +where + F: FnMut() -> Fut, + Fut: Future>, +{ + let mut attempt = 1; + loop { + match operation().await { + Ok(value) => return Ok(value), + Err(err) + if attempt < MAX_AGENT_IDENTITY_BOOTSTRAP_ATTEMPTS + && is_retryable_io_registration_error(&err) => + { + tracing::warn!( + attempt, + max_attempts = MAX_AGENT_IDENTITY_BOOTSTRAP_ATTEMPTS, + error = %err, + "agent identity registration attempt failed; retrying" + ); + attempt += 1; + } + Err(err) => return Err(err), + } + } +} + +async fn register_task_for_record_with_retries( + record: &AgentIdentityAuthRecord, + agent_identity_authapi_base_url: &str, +) -> std::io::Result { + retry_registration(|| async { + register_task_for_record(record, agent_identity_authapi_base_url).await + }) + .await +} + +async fn register_task_for_record( + record: &AgentIdentityAuthRecord, + agent_identity_authapi_base_url: &str, +) -> std::io::Result { + register_agent_task( + &build_reqwest_client(), + agent_identity_authapi_base_url, + key_for_record(record), + ) + .await + .map_err(|err| { + if is_retryable_registration_error(&err) { + std::io::Error::other(RetryableAgentIdentityRegistrationError::new( + err.to_string(), + )) + } else { + std::io::Error::other(err) + } + }) +} + fn key_for_record(record: &AgentIdentityAuthRecord) -> AgentIdentityKey<'_> { AgentIdentityKey { agent_runtime_id: &record.agent_runtime_id, @@ -111,6 +346,7 @@ mod tests { email: "agent@example.com".to_string(), plan_type: AccountPlanType::Plus, chatgpt_account_is_fedramp: false, + task_id: None, } } @@ -120,7 +356,7 @@ mod tests { } #[tokio::test] - async fn load_registers_run_task() -> anyhow::Result<()> { + async fn from_record_registers_task() -> anyhow::Result<()> { let server = MockServer::start().await; Mock::given(method("POST")) .and(path("/v1/agent/agent-runtime-1/task/register")) @@ -131,9 +367,11 @@ mod tests { .mount(&server) .await; - let auth = - AgentIdentityAuth::load(agent_identity_record_with_generated_key(), &server.uri()) - .await?; + let auth = AgentIdentityAuth::from_record( + agent_identity_record_with_generated_key(), + &server.uri(), + ) + .await?; assert_eq!(auth.run_task_id(), "task-run-1"); let requests = server @@ -152,6 +390,38 @@ mod tests { Ok(()) } + #[tokio::test] + async fn from_jwt_registers_task() -> anyhow::Result<()> { + let server = MockServer::start().await; + Mock::given(method("GET")) + .and(path("/backend-api/wham/agent-identities/jwks")) + .respond_with(ResponseTemplate::new(200).set_body_json(test_jwks_body())) + .expect(1) + .mount(&server) + .await; + Mock::given(method("POST")) + .and(path("/v1/agent/agent-runtime-1/task/register")) + .respond_with(ResponseTemplate::new(200).set_body_json(json!({ + "task_id": "task-run-1", + }))) + .expect(1) + .mount(&server) + .await; + + let record = agent_identity_record_with_generated_key(); + let jwt = signed_agent_identity_jwt(&record)?; + let auth = AgentIdentityAuth::from_jwt( + &jwt, + &format!("{}/backend-api", server.uri()), + &server.uri(), + ) + .await?; + + assert_eq!(auth.record().agent_runtime_id, "agent-runtime-1"); + assert_eq!(auth.run_task_id(), "task-run-1"); + Ok(()) + } + #[test] fn run_task_is_shared_across_clones() { let auth = AgentIdentityAuth::from_initialized_record( @@ -160,12 +430,12 @@ mod tests { ); let cloned = auth.clone(); - assert!(Arc::ptr_eq(&auth.inner, &cloned.inner)); + assert!(Arc::ptr_eq(&auth.record, &cloned.record)); assert_eq!(cloned.run_task_id(), "task-run-1"); } #[tokio::test] - async fn failed_run_task_registration_can_be_retried_on_next_call() -> anyhow::Result<()> { + async fn from_record_retries_transient_registration() -> anyhow::Result<()> { let server = MockServer::start().await; let request_count = Arc::new(AtomicUsize::new(0)); let response_count = Arc::clone(&request_count); @@ -183,14 +453,80 @@ mod tests { .expect(2) .mount(&server) .await; - let record = agent_identity_record_with_generated_key(); - AgentIdentityAuth::load(record.clone(), &server.uri()) - .await - .expect_err("first registration should fail"); - let auth = AgentIdentityAuth::load(record, &server.uri()).await?; + let auth = AgentIdentityAuth::from_record( + agent_identity_record_with_generated_key(), + &server.uri(), + ) + .await?; assert_eq!(request_count.load(Ordering::SeqCst), 2); assert_eq!(auth.run_task_id(), "task-run-1"); Ok(()) } + + fn signed_agent_identity_jwt( + record: &AgentIdentityAuthRecord, + ) -> jsonwebtoken::errors::Result { + let mut header = jsonwebtoken::Header::new(jsonwebtoken::Algorithm::RS256); + header.kid = Some("test-key".to_string()); + jsonwebtoken::encode( + &header, + &json!({ + "iss": "https://chatgpt.com/codex-backend/agent-identity", + "aud": "codex-app-server", + "iat": 1_700_000_000usize, + "exp": 4_000_000_000usize, + "agent_runtime_id": record.agent_runtime_id, + "agent_private_key": record.agent_private_key, + "account_id": record.account_id, + "chatgpt_user_id": record.chatgpt_user_id, + "email": record.email, + "plan_type": record.plan_type, + "chatgpt_account_is_fedramp": record.chatgpt_account_is_fedramp, + }), + &jsonwebtoken::EncodingKey::from_rsa_pem(TEST_AGENT_IDENTITY_RSA_PRIVATE_KEY_PEM)?, + ) + } + + fn test_jwks_body() -> serde_json::Value { + json!({ + "keys": [{ + "kty": "RSA", + "kid": "test-key", + "use": "sig", + "alg": "RS256", + "n": "1qQF2MqTrGAMDm7wXbjJP5sWqGA83tAGUs2ksy7iJXLJdhCg4AtwGm4SFl4f6kxhCSzlN1QdXuZjvRT2wZZiGUi9xUE28rf4WLrTxSnwqLuTy5knMP08yC0t_0YU_FGPZMcWb14hG05IvZr8UbmRaVagxSR8H4rSIymRoVwwmFSrqz068XrWGSYNIfLEASyo5GdAaqmk1JALINHgYGQJVxMxtwcvDxoVKmC7eltUNymMNBZhsv4E8sx9YNLpBoEibznfEpDU_DGzrM5eZCsQzaqbhBOlGd427ifud_Nnd9cPqzgCUc23-0FXSPfpbgksCXAwAmD0OFjQWrgqVdKL6Q", + "e": "AQAB", + }] + }) + } + + const TEST_AGENT_IDENTITY_RSA_PRIVATE_KEY_PEM: &[u8] = br#"-----BEGIN PRIVATE KEY----- +MIIEvgIBADANBgkqhkiG9w0BAQEFAASCBKgwggSkAgEAAoIBAQDWpAXYypOsYAwO +bvBduMk/mxaoYDze0AZSzaSzLuIlcsl2EKDgC3AabhIWXh/qTGEJLOU3VB1e5mO9 +FPbBlmIZSL3FQTbyt/hYutPFKfCou5PLmScw/TzILS3/RhT8UY9kxxZvXiEbTki9 +mvxRuZFpVqDFJHwfitIjKZGhXDCYVKurPTrxetYZJg0h8sQBLKjkZ0BqqaTUkAsg +0eBgZAlXEzG3By8PGhUqYLt6W1Q3KYw0FmGy/gTyzH1g0ukGgSJvOd8SkNT8MbOs +zl5kKxDNqpuEE6UZ3jbuJ+5382d31w+rOAJRzbf7QVdI9+luCSwJcDACYPQ4WNBa +uCpV0ovpAgMBAAECggEAVu84LwZdqYN9XpswX8VoPYrjMm9IODapWQBRpQFoNyK2 +1ksF3bjEPvA2Azk8U/l7k+vLKw22l6lY3EyRZPcz5GnB8xLm3ogE3mtNOp4yCyVu +RxhQ91aaN7mU17/a4BdorLi2LYVCg3zBmYociD1Q2AluNGsCmwPu+K7tfR2J0Sg8 +NjqiTbDG1XDpR/icwgC9t6vh8lZpCHDhF4tbQfLLVLeA/OdcuzXDyMCXbmdVIdBQ +rm4aIFmr2e1/2ctTbCg85S6AGFTH+pSLjrwTzyvf+F6NW5uNjLQAQLFj+EznBDxj +Xdx90cySrjsKK6PVWQF4RiTvkSW8eWL7R6B2FZbGwQKBgQDuVQRj72hWloR7mbEL +aUEEv3pIXTMXWEsoMBNczos/1L1RnAN1AI44TurznasPZAWvQj+kVbLDR+TAeZrL +iA8HIWswQUI18hFmgKzSkwIXGtubcKVrgsKeS4lMDKCM/Ef6WAYdeq6ronoY5lCN +YrJFmGp81W5zcV7lyiycgbSiGwKBgQDmjWYf6pZjrK7Z+OJ3X1AZfi2vss15SCvL +3fPgzIDbViztpGyQhc3DQZIsBNIu0xZp/veGce9TEeTds2ro9NfdJFeou8+fC7Pq +sOsM3amGFFi+ZW/9BWyjZEM88bgWWAjqLHbpfHDxjAf5CSxddqxgHlbP0Ytyb1Vg +gmPDn9YKSwKBgQDbTi3hC35WFuDHn0/zcSHcDZmnFuOZeqyFyV83yfMGhGrEuqvP +sPgtRikajJ3IZsB4WZyYSidZXEFY/0z6NjOl2xF38MTNQPbT/FmK1q1Yt2UWrlv5 +BvSwlk87RG9D7C0LZo4R+D7cPoDdgqjiwMvMEIkEX5zn641oI1ZTmWKuuwKBgQCD +KF+3unnRvHRAVoFnTZbA2fJdqMeRvogD04GhGlYX8V9f1hFY6nXTJaNlXVzA/J8c +r8ra9kgjJuPfZ+ljG58OFFW2DRohLcQtuHYPfK6rMzoFHqnl9EcIcMp7ijuionR3 +29HOJFgQYgxLFXfit9d6WugiE+BTupiEbckZif13HwKBgE/lAlkVHP6YahOO2Ljc +J1bwkqKZTB5dHolX9A58e/xXnfZ5P8f3Z83+Izap3FwqQulk7b1WO1MQcHuVg2NN +5da0D4h2rYOXnbYIg0BVu4spQbaM6ewsp66b8+MzLOBvj8SzWdt1Oyw0q/MRyQAR +8U4M2TSWCKUY/A6sT4W8+mT9 +-----END PRIVATE KEY-----"#; } diff --git a/codex-rs/login/src/auth/auth_tests.rs b/codex-rs/login/src/auth/auth_tests.rs index aa1dcd9c86..b52f03b706 100644 --- a/codex-rs/login/src/auth/auth_tests.rs +++ b/codex-rs/login/src/auth/auth_tests.rs @@ -6,6 +6,7 @@ use codex_app_server_protocol::AuthMode; use codex_protocol::account::PlanType as AccountPlanType; use codex_protocol::auth::KnownPlan as InternalKnownPlan; use codex_protocol::auth::PlanType as InternalPlanType; +use codex_protocol::protocol::SessionSource; use base64::Engine; use codex_protocol::config_types::ForcedLoginMethod; @@ -14,11 +15,14 @@ use pretty_assertions::assert_eq; use serde::Serialize; use serde_json::json; use std::sync::Arc; +use std::sync::atomic::AtomicUsize; +use std::sync::atomic::Ordering; use tempfile::TempDir; use tempfile::tempdir; use wiremock::Mock; use wiremock::MockServer; use wiremock::ResponseTemplate; +use wiremock::matchers::body_partial_json; use wiremock::matchers::header; use wiremock::matchers::method; use wiremock::matchers::path; @@ -95,7 +99,7 @@ fn login_with_api_key_overwrites_existing_auth_json() { } #[tokio::test] -async fn login_with_access_token_writes_only_token() { +async fn login_with_access_token_writes_agent_identity_jwt() { let dir = tempdir().unwrap(); let auth_path = dir.path().join("auth.json"); let record = agent_identity_record(WORKSPACE_ID_ALLOWED); @@ -108,7 +112,8 @@ async fn login_with_access_token_writes_only_token() { .expect(1) .mount(&server) .await; - let chatgpt_base_url = format!("{}/backend-api", server.uri()); + let authapi_base_url = server.uri(); + let chatgpt_base_url = format!("{authapi_base_url}/backend-api"); super::login_with_access_token( dir.path(), @@ -127,14 +132,75 @@ async fn login_with_access_token_writes_only_token() { .expect("auth.json should parse"); assert_eq!(auth.auth_mode, Some(AuthMode::AgentIdentity)); assert_eq!( - auth.agent_identity.as_deref(), - Some(agent_identity.as_str()) + auth.agent_identity, + Some(AgentIdentityStorage::Jwt(agent_identity)) ); assert!(auth.tokens.is_none(), "tokens should be cleared"); assert!(auth.openai_api_key.is_none(), "API key should be cleared"); server.verify().await; } +#[tokio::test] +#[serial(codex_auth_env)] +async fn stored_agent_identity_jwt_keeps_auth_json_unchanged() -> anyhow::Result<()> { + let _access_token_guard = remove_access_token_env_var(); + let codex_home = tempdir()?; + let record = agent_identity_record(WORKSPACE_ID_ALLOWED); + let agent_identity = + signed_agent_identity_jwt(&record, json!(record.plan_type)).expect("signed agent identity"); + let server = MockServer::start().await; + Mock::given(method("GET")) + .and(path("/backend-api/wham/agent-identities/jwks")) + .respond_with(ResponseTemplate::new(200).set_body_json(test_jwks_body())) + .expect(1) + .mount(&server) + .await; + mock_agent_task_registration(&server, "", &record.agent_runtime_id, "task-id").await; + let authapi_base_url = server.uri(); + let chatgpt_base_url = format!("{authapi_base_url}/backend-api"); + save_auth( + codex_home.path(), + &AuthDotJson { + auth_mode: Some(ApiAuthMode::AgentIdentity), + openai_api_key: None, + tokens: None, + last_refresh: None, + agent_identity: Some(AgentIdentityStorage::Jwt(agent_identity.clone())), + personal_access_token: None, + bedrock_api_key: None, + }, + AuthCredentialsStoreMode::File, + AuthKeyringBackendKind::Direct, + )?; + + let auth = super::load_auth( + codex_home.path(), + /*enable_codex_api_key_env*/ false, + AuthCredentialsStoreMode::File, + /*forced_chatgpt_workspace_id*/ None, + Some(&chatgpt_base_url), + AuthKeyringBackendKind::Direct, + Some(&authapi_base_url), + ) + .await? + .expect("auth should load"); + + let CodexAuth::AgentIdentity(agent_identity_auth) = auth else { + panic!("stored JWT should load as agent identity auth"); + }; + assert_eq!(agent_identity_auth.run_task_id(), "task-id"); + let storage = FileAuthStorage::new(codex_home.path().to_path_buf()); + let auth = storage + .try_read_auth_json(&get_auth_file(codex_home.path())) + .expect("auth.json should parse"); + assert_eq!( + auth.agent_identity, + Some(AgentIdentityStorage::Jwt(agent_identity)) + ); + server.verify().await; + Ok(()) +} + #[tokio::test] #[serial(codex_auth_env)] async fn login_with_access_token_writes_only_personal_access_token() { @@ -278,6 +344,387 @@ async fn login_with_access_token_rejects_invalid_jwt() { ); } +#[tokio::test] +#[serial(codex_auth_env)] +async fn chatgpt_auth_registers_agent_identity_when_enabled() -> anyhow::Result<()> { + let codex_home = tempdir()?; + write_auth_file( + AuthFileParams { + openai_api_key: None, + chatgpt_plan_type: Some("pro".to_string()), + chatgpt_account_id: Some("account-123".to_string()), + }, + codex_home.path(), + )?; + let auth = super::load_auth( + codex_home.path(), + /*enable_codex_api_key_env*/ false, + AuthCredentialsStoreMode::File, + /*forced_chatgpt_workspace_id*/ None, + /*chatgpt_base_url*/ None, + AuthKeyringBackendKind::Direct, + /*agent_identity_authapi_base_url*/ None, + ) + .await? + .expect("auth should load"); + + assert!( + auth.agent_identity_auth( + AgentIdentityAuthPolicy::JwtOnly, + /*agent_identity_authapi_base_url*/ None, + /*forced_chatgpt_workspace_id*/ None, + SessionSource::Cli, + ) + .await? + .is_none() + ); + + let server = MockServer::start().await; + Mock::given(method("POST")) + .and(path("/v1/agent/register")) + .and(header("authorization", "Bearer test-access-token")) + .and(body_partial_json(json!({ + "abom": { + "agent_harness_id": "codex-cli", + }, + "capabilities": ["responsesapi"], + "ttl": null, + }))) + .respond_with(ResponseTemplate::new(/*s*/ 200).set_body_json(json!({ + "agent_runtime_id": "agent-runtime-123", + }))) + .expect(/*r*/ 1) + .mount(&server) + .await; + mock_agent_task_registration(&server, "", "agent-runtime-123", "task-123").await; + + let agent_auth = auth + .agent_identity_auth( + AgentIdentityAuthPolicy::ChatGptAuth, + Some(&server.uri()), + /*forced_chatgpt_workspace_id*/ None, + SessionSource::Cli, + ) + .await? + .expect("agent identity should register"); + let reused = auth + .agent_identity_auth( + AgentIdentityAuthPolicy::ChatGptAuth, + Some(&server.uri()), + /*forced_chatgpt_workspace_id*/ None, + SessionSource::Cli, + ) + .await? + .expect("agent identity should be reused"); + + assert_eq!( + agent_auth.record().agent_runtime_id, + reused.record().agent_runtime_id + ); + assert_eq!(agent_auth.run_task_id(), "task-123"); + assert_eq!(reused.run_task_id(), "task-123"); + assert_eq!(agent_auth.record().agent_runtime_id, "agent-runtime-123"); + assert_eq!(agent_auth.record().account_id, "account-123"); + assert_eq!(agent_auth.record().chatgpt_user_id, "user-12345"); + assert_eq!(agent_auth.record().task_id.as_deref(), Some("task-123")); + assert_eq!(reused.record().task_id.as_deref(), Some("task-123")); + let persisted = auth + .stored_managed_chatgpt_agent_identity_record("account-123") + .expect("identity should persist"); + assert_eq!(persisted.agent_runtime_id, "agent-runtime-123"); + assert_eq!(persisted.task_id.as_deref(), Some("task-123")); + + let reloaded = super::load_auth( + codex_home.path(), + /*enable_codex_api_key_env*/ false, + AuthCredentialsStoreMode::File, + /*forced_chatgpt_workspace_id*/ None, + /*chatgpt_base_url*/ None, + AuthKeyringBackendKind::Direct, + /*agent_identity_authapi_base_url*/ None, + ) + .await? + .expect("auth should reload"); + let reloaded_agent_auth = reloaded + .agent_identity_auth( + AgentIdentityAuthPolicy::ChatGptAuth, + Some(&server.uri()), + /*forced_chatgpt_workspace_id*/ None, + SessionSource::Cli, + ) + .await? + .expect("agent identity should reload from storage"); + assert_eq!( + reloaded_agent_auth.record().agent_runtime_id, + "agent-runtime-123" + ); + assert_eq!(reloaded_agent_auth.run_task_id(), "task-123"); + Ok(()) +} + +#[tokio::test] +#[serial(codex_auth_env)] +async fn chatgpt_auth_retries_transient_agent_identity_registration() -> anyhow::Result<()> { + let codex_home = tempdir()?; + write_auth_file( + AuthFileParams { + openai_api_key: None, + chatgpt_plan_type: Some("pro".to_string()), + chatgpt_account_id: Some("account-123".to_string()), + }, + codex_home.path(), + )?; + let auth = super::load_auth( + codex_home.path(), + /*enable_codex_api_key_env*/ false, + AuthCredentialsStoreMode::File, + /*forced_chatgpt_workspace_id*/ None, + /*chatgpt_base_url*/ None, + AuthKeyringBackendKind::Direct, + /*agent_identity_authapi_base_url*/ None, + ) + .await? + .expect("auth should load"); + + let server = MockServer::start().await; + let registration_count = Arc::new(AtomicUsize::new(0)); + let response_count = Arc::clone(®istration_count); + Mock::given(method("POST")) + .and(path("/v1/agent/register")) + .respond_with(move |_request: &wiremock::Request| { + if response_count.fetch_add(1, Ordering::SeqCst) < 2 { + ResponseTemplate::new(/*status*/ 503) + } else { + ResponseTemplate::new(/*status*/ 200).set_body_json(json!({ + "agent_runtime_id": "agent-runtime-123", + })) + } + }) + .expect(/*requests*/ 3) + .mount(&server) + .await; + mock_agent_task_registration(&server, "", "agent-runtime-123", "task-123").await; + + let agent_auth = auth + .agent_identity_auth( + AgentIdentityAuthPolicy::ChatGptAuth, + Some(&server.uri()), + /*forced_chatgpt_workspace_id*/ None, + SessionSource::Cli, + ) + .await? + .expect("agent identity should register after retries"); + + assert_eq!(registration_count.load(Ordering::SeqCst), 3); + assert_eq!(agent_auth.record().agent_runtime_id, "agent-runtime-123"); + assert_eq!(agent_auth.record().task_id.as_deref(), Some("task-123")); + assert_eq!( + auth.stored_managed_chatgpt_agent_identity_record("account-123") + .and_then(|record| record.task_id), + Some("task-123".to_string()) + ); + Ok(()) +} + +#[tokio::test] +#[serial(codex_auth_env)] +async fn chatgpt_auth_registration_retry_exhaustion_is_fallback_eligible() -> anyhow::Result<()> { + let codex_home = tempdir()?; + write_auth_file( + AuthFileParams { + openai_api_key: None, + chatgpt_plan_type: Some("pro".to_string()), + chatgpt_account_id: Some("account-123".to_string()), + }, + codex_home.path(), + )?; + let auth = super::load_auth( + codex_home.path(), + /*enable_codex_api_key_env*/ false, + AuthCredentialsStoreMode::File, + /*forced_chatgpt_workspace_id*/ None, + /*chatgpt_base_url*/ None, + AuthKeyringBackendKind::Direct, + /*agent_identity_authapi_base_url*/ None, + ) + .await? + .expect("auth should load"); + + let server = MockServer::start().await; + Mock::given(method("POST")) + .and(path("/v1/agent/register")) + .respond_with(ResponseTemplate::new(/*status*/ 503)) + .expect(/*requests*/ 3) + .mount(&server) + .await; + + let err = auth + .agent_identity_auth( + AgentIdentityAuthPolicy::ChatGptAuth, + Some(&server.uri()), + /*forced_chatgpt_workspace_id*/ None, + SessionSource::Cli, + ) + .await + .expect_err("retry exhaustion should return an error"); + + assert!(AgentIdentityAuthError::is_bootstrap_unavailable(&err)); + assert!( + auth.stored_managed_chatgpt_agent_identity_record("account-123") + .is_none() + ); + Ok(()) +} + +#[tokio::test] +#[serial(codex_auth_env)] +async fn chatgpt_auth_task_registration_retry_exhaustion_is_fallback_eligible() -> anyhow::Result<()> +{ + let codex_home = tempdir()?; + write_auth_file( + AuthFileParams { + openai_api_key: None, + chatgpt_plan_type: Some("pro".to_string()), + chatgpt_account_id: Some("account-123".to_string()), + }, + codex_home.path(), + )?; + let mut record = agent_identity_record("account-123"); + record.chatgpt_user_id = "user-12345".to_string(); + record.email = "user@example.com".to_string(); + let storage = FileAuthStorage::new(codex_home.path().to_path_buf()); + let auth_path = get_auth_file(codex_home.path()); + let mut auth_json = storage.try_read_auth_json(&auth_path)?; + auth_json.agent_identity = Some(AgentIdentityStorage::Record(record.clone())); + storage.save(&auth_json)?; + let auth = super::load_auth( + codex_home.path(), + /*enable_codex_api_key_env*/ false, + AuthCredentialsStoreMode::File, + /*forced_chatgpt_workspace_id*/ None, + /*chatgpt_base_url*/ None, + AuthKeyringBackendKind::Direct, + /*agent_identity_authapi_base_url*/ None, + ) + .await? + .expect("auth should load"); + + let server = MockServer::start().await; + Mock::given(method("POST")) + .and(path(format!( + "/v1/agent/{}/task/register", + record.agent_runtime_id + ))) + .respond_with(ResponseTemplate::new(/*status*/ 503)) + .expect(/*requests*/ 3) + .mount(&server) + .await; + + let err = auth + .agent_identity_auth( + AgentIdentityAuthPolicy::ChatGptAuth, + Some(&server.uri()), + /*forced_chatgpt_workspace_id*/ None, + SessionSource::Cli, + ) + .await + .expect_err("task retry exhaustion should return an error"); + + assert!(AgentIdentityAuthError::is_bootstrap_unavailable(&err)); + record.task_id = None; + assert_eq!( + auth.stored_managed_chatgpt_agent_identity_record("account-123"), + Some(record) + ); + Ok(()) +} + +#[tokio::test] +#[serial(codex_auth_env)] +async fn chatgpt_auth_non_retryable_registration_error_is_hard_failure() -> anyhow::Result<()> { + let codex_home = tempdir()?; + write_auth_file( + AuthFileParams { + openai_api_key: None, + chatgpt_plan_type: Some("pro".to_string()), + chatgpt_account_id: Some("account-123".to_string()), + }, + codex_home.path(), + )?; + let auth = super::load_auth( + codex_home.path(), + /*enable_codex_api_key_env*/ false, + AuthCredentialsStoreMode::File, + /*forced_chatgpt_workspace_id*/ None, + /*chatgpt_base_url*/ None, + AuthKeyringBackendKind::Direct, + /*agent_identity_authapi_base_url*/ None, + ) + .await? + .expect("auth should load"); + + let server = MockServer::start().await; + Mock::given(method("POST")) + .and(path("/v1/agent/register")) + .respond_with(ResponseTemplate::new(/*status*/ 403)) + .expect(/*requests*/ 1) + .mount(&server) + .await; + + let err = auth + .agent_identity_auth( + AgentIdentityAuthPolicy::ChatGptAuth, + Some(&server.uri()), + /*forced_chatgpt_workspace_id*/ None, + SessionSource::Cli, + ) + .await + .expect_err("hard registration failure should return an error"); + + assert!(!AgentIdentityAuthError::is_bootstrap_unavailable(&err)); + assert!( + auth.stored_managed_chatgpt_agent_identity_record("account-123") + .is_none() + ); + Ok(()) +} + +#[tokio::test] +async fn agent_identity_jwt_task_registration_retry_exhaustion_is_strict() -> anyhow::Result<()> { + let record = agent_identity_record(WORKSPACE_ID_ALLOWED); + let agent_identity = + signed_agent_identity_jwt(&record, json!(record.plan_type)).expect("signed agent identity"); + let server = MockServer::start().await; + Mock::given(method("GET")) + .and(path("/backend-api/wham/agent-identities/jwks")) + .respond_with(ResponseTemplate::new(200).set_body_json(test_jwks_body())) + .expect(1) + .mount(&server) + .await; + Mock::given(method("POST")) + .and(path(format!( + "/v1/agent/{}/task/register", + record.agent_runtime_id + ))) + .respond_with(ResponseTemplate::new(/*status*/ 503)) + .expect(/*requests*/ 3) + .mount(&server) + .await; + let authapi_base_url = server.uri(); + let chatgpt_base_url = format!("{authapi_base_url}/backend-api"); + + let err = CodexAuth::from_agent_identity_jwt_with_authapi_base_url( + &agent_identity, + Some(&chatgpt_base_url), + &authapi_base_url, + ) + .await + .expect_err("agent identity jwt task retry exhaustion should fail"); + + assert!(!AgentIdentityAuthError::is_bootstrap_unavailable(&err)); + Ok(()) +} + #[tokio::test] async fn login_with_access_token_rejects_unsigned_jwt() { let dir = tempdir().unwrap(); @@ -290,7 +737,8 @@ async fn login_with_access_token_rejects_unsigned_jwt() { .expect(1) .mount(&server) .await; - let chatgpt_base_url = format!("{}/backend-api", server.uri()); + let authapi_base_url = server.uri(); + let chatgpt_base_url = format!("{authapi_base_url}/backend-api"); super::login_with_access_token( dir.path(), @@ -457,6 +905,7 @@ async fn unauthorized_recovery_reports_mode_and_step_names() { dir.path().to_path_buf(), /*enable_codex_api_key_env*/ false, AuthCredentialsStoreMode::File, + /*forced_chatgpt_workspace_id*/ None, /*chatgpt_base_url*/ None, AuthKeyringBackendKind::default(), ) @@ -828,7 +1277,6 @@ async fn build_config( forced_login_method, forced_chatgpt_workspace_id, chatgpt_base_url: None, - agent_identity_authapi_base_url: None, } } @@ -879,7 +1327,7 @@ fn remove_access_token_env_var() -> EnvVarGuard { #[serial(codex_auth_env)] async fn load_auth_reads_access_token_from_env() { let codex_home = tempdir().unwrap(); - let expected_record = agent_identity_record(WORKSPACE_ID_ALLOWED); + let mut expected_record = agent_identity_record(WORKSPACE_ID_ALLOWED); let agent_identity = signed_agent_identity_jwt(&expected_record, json!(expected_record.plan_type)) .expect("signed agent identity"); @@ -898,6 +1346,7 @@ async fn load_auth_reads_access_token_from_env() { .expect(1) .mount(&server) .await; + expected_record.task_id = Some("task-123".to_string()); let _access_token_guard = EnvVarGuard::set(CODEX_ACCESS_TOKEN_ENV_VAR, &agent_identity); let authapi_base_url = server.uri(); @@ -1003,11 +1452,10 @@ async fn auth_manager_rejects_env_personal_access_token_workspace_mismatch() { let _access_token_guard = EnvVarGuard::set(CODEX_ACCESS_TOKEN_ENV_VAR, "at-env-workspace-mismatch"); - let manager = AuthManager::new_with_workspace_restriction( + let manager = AuthManager::new( codex_home.path().to_path_buf(), /*enable_codex_api_key_env*/ false, AuthCredentialsStoreMode::File, - /*forced_chatgpt_workspace_id*/ Some(vec![WORKSPACE_ID_ALLOWED.to_string()]), /*chatgpt_base_url*/ None, AuthKeyringBackendKind::default(), @@ -1054,11 +1502,10 @@ async fn auth_manager_rejects_stored_personal_access_token_workspace_mismatch() .await .expect("personal access token login should succeed"); - let manager = AuthManager::new_with_workspace_restriction( + let manager = AuthManager::new( codex_home.path().to_path_buf(), /*enable_codex_api_key_env*/ false, auth_credentials_store_mode, - /*forced_chatgpt_workspace_id*/ Some(vec![WORKSPACE_ID_ALLOWED.to_string()]), /*chatgpt_base_url*/ None, AuthKeyringBackendKind::default(), @@ -1092,6 +1539,7 @@ async fn personal_access_token_does_not_offer_unauthorized_recovery() { codex_home.path().to_path_buf(), /*enable_codex_api_key_env*/ false, AuthCredentialsStoreMode::File, + /*forced_chatgpt_workspace_id*/ None, /*chatgpt_base_url*/ None, AuthKeyringBackendKind::default(), ) @@ -1233,7 +1681,6 @@ async fn enforce_login_restrictions_logs_out_for_personal_access_token_workspace forced_login_method: None, forced_chatgpt_workspace_id: Some(vec![WORKSPACE_ID_ALLOWED.to_string()]), chatgpt_base_url: None, - agent_identity_authapi_base_url: None, }; let err = super::enforce_login_restrictions(&config) @@ -1341,7 +1788,7 @@ async fn enforce_login_restrictions_logs_out_for_agent_identity_workspace_mismat openai_api_key: None, tokens: None, last_refresh: None, - agent_identity: Some(agent_identity), + agent_identity: Some(AgentIdentityStorage::Jwt(agent_identity)), personal_access_token: None, bedrock_api_key: None, }, @@ -1357,15 +1804,21 @@ async fn enforce_login_restrictions_logs_out_for_agent_identity_workspace_mismat forced_login_method: None, forced_chatgpt_workspace_id: Some(vec![WORKSPACE_ID_ALLOWED.to_string()]), chatgpt_base_url: Some(chatgpt_base_url), - agent_identity_authapi_base_url: Some(authapi_base_url), }; - let err = super::enforce_login_restrictions(&config) - .await - .expect_err("expected workspace mismatch to error"); - assert!(err.to_string().contains(&format!( - "current credentials belong to {WORKSPACE_ID_DISALLOWED}" - ))); + let err = super::enforce_login_restrictions_with_agent_identity_authapi_base_url( + &config, + Some(&authapi_base_url), + ) + .await + .expect_err("expected workspace mismatch to error"); + let message = err.to_string(); + assert!( + message.contains(&format!( + "current credentials belong to {WORKSPACE_ID_DISALLOWED}" + )), + "{message}" + ); assert!( !codex_home.path().join("auth.json").exists(), "auth.json should be removed on mismatch" @@ -1437,9 +1890,28 @@ fn agent_identity_record(account_id: &str) -> AgentIdentityAuthRecord { email: "user@example.com".to_string(), plan_type: AccountPlanType::Pro, chatgpt_account_is_fedramp: false, + task_id: None, } } +async fn mock_agent_task_registration( + server: &MockServer, + path_prefix: &str, + agent_runtime_id: &str, + task_id: &str, +) { + Mock::given(method("POST")) + .and(path(format!( + "{path_prefix}/v1/agent/{agent_runtime_id}/task/register" + ))) + .respond_with(ResponseTemplate::new(/*s*/ 200).set_body_json(json!({ + "task_id": task_id, + }))) + .expect(/*r*/ 1) + .mount(server) + .await; +} + fn fake_agent_identity_jwt(record: &AgentIdentityAuthRecord) -> std::io::Result { fake_agent_identity_jwt_with_plan_type(record, serde_json::to_value(record.plan_type)?) } @@ -1580,10 +2052,13 @@ async fn assert_agent_identity_plan_alias( .await; let authapi_base_url = server.uri(); let chatgpt_base_url = format!("{authapi_base_url}/backend-api"); - let auth = - CodexAuth::from_agent_identity_jwt(&jwt, Some(&chatgpt_base_url), Some(&authapi_base_url)) - .await - .expect("agent identity auth"); + let auth = CodexAuth::from_agent_identity_jwt_with_authapi_base_url( + &jwt, + Some(&chatgpt_base_url), + &authapi_base_url, + ) + .await + .expect("agent identity auth"); pretty_assertions::assert_eq!(auth.account_plan_type(), Some(expected_plan_type)); server.verify().await; diff --git a/codex-rs/login/src/auth/bedrock_api_key_tests.rs b/codex-rs/login/src/auth/bedrock_api_key_tests.rs index 35ace9fea8..691d8bbc5d 100644 --- a/codex-rs/login/src/auth/bedrock_api_key_tests.rs +++ b/codex-rs/login/src/auth/bedrock_api_key_tests.rs @@ -60,6 +60,7 @@ async fn login_with_bedrock_api_key_replaces_openai_auth() -> anyhow::Result<()> codex_home.path().to_path_buf(), /*enable_codex_api_key_env*/ false, AuthCredentialsStoreMode::File, + /*forced_chatgpt_workspace_id*/ None, /*chatgpt_base_url*/ None, AuthKeyringBackendKind::default(), ) @@ -107,6 +108,7 @@ async fn logout_removes_bedrock_auth() -> anyhow::Result<()> { codex_home.path().to_path_buf(), /*enable_codex_api_key_env*/ false, AuthCredentialsStoreMode::File, + /*forced_chatgpt_workspace_id*/ None, /*chatgpt_base_url*/ None, AuthKeyringBackendKind::default(), ) @@ -130,6 +132,7 @@ async fn bedrock_only_auth_storage_creates_primary_auth() -> anyhow::Result<()> codex_home.path().to_path_buf(), /*enable_codex_api_key_env*/ false, AuthCredentialsStoreMode::File, + /*forced_chatgpt_workspace_id*/ None, /*chatgpt_base_url*/ None, AuthKeyringBackendKind::default(), ) diff --git a/codex-rs/login/src/auth/manager.rs b/codex-rs/login/src/auth/manager.rs index bb548ffe78..138bdad5b9 100644 --- a/codex-rs/login/src/auth/manager.rs +++ b/codex-rs/login/src/auth/manager.rs @@ -20,8 +20,6 @@ use tokio::sync::watch; use tracing::instrument; use codex_agent_identity::ChatGptEnvironment; -use codex_agent_identity::decode_agent_identity_jwt; -use codex_agent_identity::fetch_agent_identity_jwks; use codex_app_server_protocol::AuthMode; use codex_app_server_protocol::AuthMode as ApiAuthMode; use codex_protocol::config_types::ForcedLoginMethod; @@ -29,18 +27,27 @@ use codex_protocol::config_types::ModelProviderAuthInfo; use super::access_token::CodexAccessToken; use super::access_token::classify_codex_access_token; +use super::agent_identity::ManagedChatGptAgentIdentityBinding; +use super::agent_identity::agent_identity_authapi_base_url; +use super::agent_identity::classify_bootstrap_error; +use super::agent_identity::record_matches_managed_chatgpt_binding; +use super::agent_identity::record_needs_task_registration; +use super::agent_identity::register_managed_chatgpt_agent_identity; +use super::agent_identity::require_agent_identity_authapi_base_url; +use super::agent_identity::verified_record_from_jwt; use super::external_bearer::BearerTokenRefresher; use super::revoke::revoke_auth_tokens; pub use crate::auth::agent_identity::AgentIdentityAuth; +pub use crate::auth::agent_identity::AgentIdentityAuthError; pub use crate::auth::bedrock_api_key::BedrockApiKeyAuth; pub use crate::auth::personal_access_token::PersonalAccessTokenAuth; pub use crate::auth::storage::AgentIdentityAuthRecord; +pub use crate::auth::storage::AgentIdentityStorage; pub use crate::auth::storage::AuthDotJson; pub use crate::auth::storage::AuthKeyringBackendKind; use crate::auth::storage::AuthStorageBackend; use crate::auth::storage::create_auth_storage; use crate::auth::util::try_parse_error_message; -use crate::default_client::build_reqwest_client; use crate::default_client::create_client; use crate::token_data::TokenData; use crate::token_data::parse_chatgpt_jwt_claims; @@ -51,6 +58,7 @@ use codex_protocol::account::PlanType as AccountPlanType; use codex_protocol::auth::PlanType as InternalPlanType; use codex_protocol::auth::RefreshTokenFailedError; use codex_protocol::auth::RefreshTokenFailedReason; +use codex_protocol::protocol::SessionSource; use serde_json::Value; use thiserror::Error; @@ -65,6 +73,15 @@ pub enum CodexAuth { BedrockApiKey(BedrockApiKeyAuth), } +/// Policy for resolving Agent Identity auth from a broader Codex auth snapshot. +#[derive(Debug, Clone, Copy, PartialEq, Eq)] +pub enum AgentIdentityAuthPolicy { + /// Use Agent Identity auth only when the current auth is already Agent Identity. + JwtOnly, + /// Allow managed ChatGPT auth to register or reuse Agent Identity auth. + ChatGptAuth, +} + impl PartialEq for CodexAuth { fn eq(&self, other: &Self) -> bool { match (self, other) { @@ -222,7 +239,6 @@ impl CodexAuth { agent_identity_authapi_base_url: Option<&str>, ) -> std::io::Result { let auth_mode = auth_dot_json.resolved_mode(); - let client = create_client(); if auth_mode == ApiAuthMode::ApiKey { let Some(api_key) = auth_dot_json.openai_api_key.as_deref() else { return Err(std::io::Error::other("API key auth is missing a key.")); @@ -230,17 +246,34 @@ impl CodexAuth { return Ok(Self::from_api_key(api_key)); } if auth_mode == ApiAuthMode::AgentIdentity { - let Some(agent_identity) = auth_dot_json.agent_identity else { + let Some(agent_identity) = auth_dot_json.agent_identity.clone() else { return Err(std::io::Error::other( - "agent identity auth is missing an agent identity token.", + "agent identity auth is missing agent identity auth material.", )); }; - return Self::from_agent_identity_jwt( - &agent_identity, - chatgpt_base_url, - agent_identity_authapi_base_url, - ) - .await; + let base_url = chatgpt_base_url + .unwrap_or(ChatGptEnvironment::default().chatgpt_base_url()) + .trim_end_matches('/') + .to_string(); + let agent_identity_authapi_base_url = + require_agent_identity_authapi_base_url(agent_identity_authapi_base_url)?; + match agent_identity { + AgentIdentityStorage::Jwt(jwt) => { + let auth = AgentIdentityAuth::from_jwt( + &jwt, + &base_url, + agent_identity_authapi_base_url, + ) + .await?; + return Ok(Self::AgentIdentity(auth)); + } + AgentIdentityStorage::Record(record) => { + let auth = + AgentIdentityAuth::from_record(record, agent_identity_authapi_base_url) + .await?; + return Ok(Self::AgentIdentity(auth)); + } + } } if auth_mode == ApiAuthMode::PersonalAccessToken { let Some(personal_access_token) = auth_dot_json.personal_access_token.as_deref() else { @@ -262,7 +295,7 @@ impl CodexAuth { let storage_mode = auth_dot_json.storage_mode(auth_credentials_store_mode); let state = ChatgptAuthState { auth_dot_json: Arc::new(Mutex::new(Some(auth_dot_json))), - client, + client: create_client(), }; match auth_mode { @@ -292,6 +325,8 @@ impl CodexAuth { chatgpt_base_url: Option<&str>, keyring_backend_kind: AuthKeyringBackendKind, ) -> std::io::Result> { + let agent_identity_authapi_base_url = + agent_identity_authapi_base_url(chatgpt_base_url).ok(); load_auth( codex_home, /*enable_codex_api_key_env*/ false, @@ -299,7 +334,7 @@ impl CodexAuth { /*forced_chatgpt_workspace_id*/ None, chatgpt_base_url, keyring_backend_kind, - /*agent_identity_authapi_base_url*/ None, + agent_identity_authapi_base_url.as_deref(), ) .await } @@ -307,19 +342,28 @@ impl CodexAuth { pub async fn from_agent_identity_jwt( jwt: &str, chatgpt_base_url: Option<&str>, - agent_identity_authapi_base_url_override: Option<&str>, + ) -> std::io::Result { + let agent_identity_authapi_base_url = agent_identity_authapi_base_url(chatgpt_base_url)?; + Self::from_agent_identity_jwt_with_authapi_base_url( + jwt, + chatgpt_base_url, + &agent_identity_authapi_base_url, + ) + .await + } + + async fn from_agent_identity_jwt_with_authapi_base_url( + jwt: &str, + chatgpt_base_url: Option<&str>, + agent_identity_authapi_base_url: &str, ) -> std::io::Result { let base_url = chatgpt_base_url .unwrap_or(ChatGptEnvironment::default().chatgpt_base_url()) .trim_end_matches('/') .to_string(); - let resolved_authapi_base_url = resolve_agent_identity_authapi_base_url( - Some(&base_url), - agent_identity_authapi_base_url_override, - )?; - let record = verified_agent_identity_record(jwt, &base_url).await?; - let auth = AgentIdentityAuth::load(record, &resolved_authapi_base_url).await?; - Ok(Self::AgentIdentity(auth)) + Ok(Self::AgentIdentity( + AgentIdentityAuth::from_jwt(jwt, &base_url, agent_identity_authapi_base_url).await?, + )) } pub async fn from_personal_access_token(access_token: &str) -> std::io::Result { @@ -499,6 +543,89 @@ impl CodexAuth { self.get_current_auth_json().and_then(|t| t.tokens) } + fn stored_managed_chatgpt_agent_identity_record( + &self, + account_id: &str, + ) -> Option { + self.get_current_auth_json() + .and_then(|auth| auth.agent_identity) + .and_then(|identity| identity.as_record().cloned()) + .filter(|identity| identity.account_id == account_id) + } + + fn persist_managed_chatgpt_agent_identity_record( + &self, + record: AgentIdentityAuthRecord, + ) -> std::io::Result<()> { + if let Self::Chatgpt(chatgpt_auth) = self { + chatgpt_auth.persist_agent_identity_record(record)?; + } + Ok(()) + } + + async fn agent_identity_auth( + &self, + policy: AgentIdentityAuthPolicy, + agent_identity_authapi_base_url: Option<&str>, + forced_chatgpt_workspace_id: Option>, + session_source: SessionSource, + ) -> std::io::Result> { + match self { + Self::AgentIdentity(auth) => Ok(Some(auth.clone())), + Self::ApiKey(_) + | Self::ChatgptAuthTokens(_) + | Self::PersonalAccessToken(_) + | Self::BedrockApiKey(_) => Ok(None), + Self::Chatgpt(_) => { + if policy == AgentIdentityAuthPolicy::JwtOnly { + return Ok(None); + } + self.ensure_managed_chatgpt_agent_identity( + require_agent_identity_authapi_base_url(agent_identity_authapi_base_url)?, + forced_chatgpt_workspace_id, + session_source, + ) + .await + .map(Some) + } + } + } + + async fn ensure_managed_chatgpt_agent_identity( + &self, + agent_identity_authapi_base_url: &str, + forced_chatgpt_workspace_id: Option>, + session_source: SessionSource, + ) -> std::io::Result { + let binding = + ManagedChatGptAgentIdentityBinding::from_auth(self, forced_chatgpt_workspace_id) + .ok_or_else(|| std::io::Error::other("ChatGPT auth is unavailable"))?; + + // JWT auth is loaded as CodexAuth::AgentIdentity; this path only reuses + // records created by the managed ChatGPT Agent Identity bootstrap. + if let Some(record) = self.stored_managed_chatgpt_agent_identity_record(&binding.account_id) + && record_matches_managed_chatgpt_binding(&record, &binding) + { + let should_persist = record_needs_task_registration(&record); + let auth = AgentIdentityAuth::from_record(record, agent_identity_authapi_base_url) + .await + .map_err(|err| classify_bootstrap_error("agent task registration", err))?; + if should_persist { + self.persist_managed_chatgpt_agent_identity_record(auth.record().clone())?; + } + return Ok(auth); + } + + let auth = register_managed_chatgpt_agent_identity( + binding, + agent_identity_authapi_base_url, + session_source, + ) + .await?; + self.persist_managed_chatgpt_agent_identity_record(auth.record().clone())?; + Ok(auth) + } + /// Consider this private to integration tests. pub fn create_dummy_chatgpt_auth_for_testing() -> Self { let auth_dot_json = AuthDotJson { @@ -516,10 +643,9 @@ impl CodexAuth { bedrock_api_key: None, }; - let client = create_client(); let state = ChatgptAuthState { auth_dot_json: Arc::new(Mutex::new(Some(auth_dot_json))), - client, + client: create_client(), }; let dummy_auth_id = NEXT_DUMMY_AUTH_ID.fetch_add(1, Ordering::Relaxed); let storage = create_auth_storage( @@ -537,6 +663,43 @@ impl CodexAuth { } } +impl ManagedChatGptAgentIdentityBinding { + fn from_auth(auth: &CodexAuth, forced_workspace_id: Option>) -> Option { + if !auth.is_chatgpt_auth() { + return None; + } + + let token_data = auth.get_token_data().ok()?; + let forced_workspace_id = + forced_workspace_id + .as_deref() + .and_then(|workspace_ids| match workspace_ids { + [workspace_id] if !workspace_id.is_empty() => Some(workspace_id.clone()), + _ => None, + }); + let account_id = forced_workspace_id + .or(token_data + .account_id + .clone() + .filter(|value| !value.is_empty())) + .or(token_data.id_token.chatgpt_account_id.clone())?; + let chatgpt_user_id = token_data + .id_token + .chatgpt_user_id + .clone() + .filter(|value| !value.is_empty())?; + + Some(Self { + account_id, + chatgpt_user_id, + email: token_data.id_token.email.clone().unwrap_or_default(), + plan_type: auth.account_plan_type().unwrap_or(AccountPlanType::Unknown), + chatgpt_account_is_fedramp: auth.is_fedramp_account(), + access_token: token_data.access_token, + }) + } +} + impl ChatgptAuth { fn current_auth_json(&self) -> Option { #[expect(clippy::unwrap_used)] @@ -554,6 +717,31 @@ impl ChatgptAuth { fn client(&self) -> &CodexHttpClient { &self.state.client } + + fn persist_agent_identity_record( + &self, + record: AgentIdentityAuthRecord, + ) -> std::io::Result<()> { + persist_agent_identity_record(&self.state.auth_dot_json, &self.storage, record) + } +} + +fn persist_agent_identity_record( + auth_dot_json: &Arc>>, + storage: &Arc, + record: AgentIdentityAuthRecord, +) -> std::io::Result<()> { + let mut guard = auth_dot_json + .lock() + .map_err(|_| std::io::Error::other("failed to lock auth state"))?; + let mut auth = storage + .load()? + .or_else(|| guard.clone()) + .ok_or_else(|| std::io::Error::other("auth data is not available"))?; + auth.agent_identity = Some(AgentIdentityStorage::Record(record)); + storage.save(&auth)?; + *guard = Some(auth); + Ok(()) } pub const OPENAI_API_KEY_ENV_VAR: &str = "OPENAI_API_KEY"; @@ -582,34 +770,6 @@ fn read_non_empty_env_var(key: &str) -> Option { .filter(|value| !value.is_empty()) } -async fn verified_agent_identity_record( - jwt: &str, - chatgpt_base_url: &str, -) -> std::io::Result { - AgentIdentityAuthRecord::from_agent_identity_jwt(jwt)?; - let jwks = fetch_agent_identity_jwks(&build_reqwest_client(), chatgpt_base_url) - .await - .map_err(std::io::Error::other)?; - let claims = decode_agent_identity_jwt(jwt, Some(&jwks)).map_err(std::io::Error::other)?; - Ok(claims.into()) -} - -fn resolve_agent_identity_authapi_base_url( - chatgpt_base_url: Option<&str>, - agent_identity_authapi_base_url_override: Option<&str>, -) -> std::io::Result { - if let Some(base_url) = agent_identity_authapi_base_url_override { - return Ok(base_url.trim_end_matches('/').to_string()); - } - - let environment = match chatgpt_base_url { - Some(chatgpt_base_url) => ChatGptEnvironment::from_chatgpt_base_url(chatgpt_base_url) - .map_err(std::io::Error::other)?, - None => ChatGptEnvironment::default(), - }; - Ok(environment.agent_identity_authapi_base_url().to_string()) -} - /// Delete the auth.json file inside `codex_home` if it exists. Returns `Ok(true)` /// if a file was removed, `Ok(false)` if no auth file was present. pub fn logout( @@ -705,13 +865,13 @@ pub async fn login_with_access_token( .unwrap_or(ChatGptEnvironment::default().chatgpt_base_url()) .trim_end_matches('/') .to_string(); - verified_agent_identity_record(jwt, &base_url).await?; + verified_record_from_jwt(jwt, &base_url).await?; AuthDotJson { auth_mode: Some(ApiAuthMode::AgentIdentity), openai_api_key: None, tokens: None, last_refresh: None, - agent_identity: Some(jwt.to_string()), + agent_identity: Some(AgentIdentityStorage::Jwt(jwt.to_string())), personal_access_token: None, bedrock_api_key: None, } @@ -793,11 +953,23 @@ pub struct AuthConfig { pub keyring_backend_kind: AuthKeyringBackendKind, pub forced_login_method: Option, pub chatgpt_base_url: Option, - pub agent_identity_authapi_base_url: Option, pub forced_chatgpt_workspace_id: Option>, } pub async fn enforce_login_restrictions(config: &AuthConfig) -> std::io::Result<()> { + let agent_identity_authapi_base_url = + agent_identity_authapi_base_url(config.chatgpt_base_url.as_deref()).ok(); + enforce_login_restrictions_with_agent_identity_authapi_base_url( + config, + agent_identity_authapi_base_url.as_deref(), + ) + .await +} + +async fn enforce_login_restrictions_with_agent_identity_authapi_base_url( + config: &AuthConfig, + agent_identity_authapi_base_url: Option<&str>, +) -> std::io::Result<()> { let Some(auth) = load_auth( &config.codex_home, /*enable_codex_api_key_env*/ true, @@ -805,7 +977,7 @@ pub async fn enforce_login_restrictions(config: &AuthConfig) -> std::io::Result< /*forced_chatgpt_workspace_id*/ None, config.chatgpt_base_url.as_deref(), config.keyring_backend_kind, - config.agent_identity_authapi_base_url.as_deref(), + agent_identity_authapi_base_url, ) .await? else { @@ -985,11 +1157,13 @@ async fn load_auth( ensure_personal_access_token_workspace_allowed(forced_chatgpt_workspace_id, &auth)?; Ok(Some(CodexAuth::PersonalAccessToken(auth))) } - CodexAccessToken::AgentIdentityJwt(jwt) => CodexAuth::from_agent_identity_jwt( - jwt, - chatgpt_base_url, - agent_identity_authapi_base_url, - ) + CodexAccessToken::AgentIdentityJwt(jwt) => { + CodexAuth::from_agent_identity_jwt_with_authapi_base_url( + jwt, + chatgpt_base_url, + require_agent_identity_authapi_base_url(agent_identity_authapi_base_url)?, + ) + } .await .map(Some), }; @@ -1524,7 +1698,9 @@ pub struct AuthManager { keyring_backend_kind: AuthKeyringBackendKind, forced_chatgpt_workspace_id: RwLock>>, chatgpt_base_url: Option, + agent_identity_authapi_base_url: Option, refresh_lock: Semaphore, + agent_identity_lock: Semaphore, external_auth: RwLock>>, } @@ -1572,30 +1748,16 @@ impl Debug for AuthManager { } } +fn default_agent_identity_authapi_base_url() -> Option { + agent_identity_authapi_base_url(/*chatgpt_base_url*/ None).ok() +} + impl AuthManager { /// Create a new manager loading the initial auth using the provided /// preferred auth method. Errors loading auth are swallowed; `auth()` will /// simply return `None` in that case so callers can treat it as an /// unauthenticated state. pub async fn new( - codex_home: PathBuf, - enable_codex_api_key_env: bool, - auth_credentials_store_mode: AuthCredentialsStoreMode, - chatgpt_base_url: Option, - keyring_backend_kind: AuthKeyringBackendKind, - ) -> Self { - Self::new_with_workspace_restriction( - codex_home, - enable_codex_api_key_env, - auth_credentials_store_mode, - /*forced_chatgpt_workspace_id*/ None, - chatgpt_base_url, - keyring_backend_kind, - ) - .await - } - - async fn new_with_workspace_restriction( codex_home: PathBuf, enable_codex_api_key_env: bool, auth_credentials_store_mode: AuthCredentialsStoreMode, @@ -1603,6 +1765,8 @@ impl AuthManager { chatgpt_base_url: Option, keyring_backend_kind: AuthKeyringBackendKind, ) -> Self { + let agent_identity_authapi_base_url = + agent_identity_authapi_base_url(chatgpt_base_url.as_deref()).ok(); let managed_auth = load_auth( &codex_home, enable_codex_api_key_env, @@ -1610,7 +1774,7 @@ impl AuthManager { forced_chatgpt_workspace_id.as_deref(), chatgpt_base_url.as_deref(), keyring_backend_kind, - /*agent_identity_authapi_base_url*/ None, + agent_identity_authapi_base_url.as_deref(), ) .await .ok() @@ -1628,7 +1792,9 @@ impl AuthManager { keyring_backend_kind, forced_chatgpt_workspace_id: RwLock::new(forced_chatgpt_workspace_id), chatgpt_base_url, + agent_identity_authapi_base_url, refresh_lock: Semaphore::new(/*permits*/ 1), + agent_identity_lock: Semaphore::new(/*permits*/ 1), external_auth: RwLock::new(None), } } @@ -1650,7 +1816,9 @@ impl AuthManager { keyring_backend_kind: AuthKeyringBackendKind::default(), forced_chatgpt_workspace_id: RwLock::new(None), chatgpt_base_url: None, + agent_identity_authapi_base_url: default_agent_identity_authapi_base_url(), refresh_lock: Semaphore::new(/*permits*/ 1), + agent_identity_lock: Semaphore::new(/*permits*/ 1), external_auth: RwLock::new(None), }) } @@ -1671,7 +1839,40 @@ impl AuthManager { keyring_backend_kind: AuthKeyringBackendKind::default(), forced_chatgpt_workspace_id: RwLock::new(None), chatgpt_base_url: None, + agent_identity_authapi_base_url: default_agent_identity_authapi_base_url(), refresh_lock: Semaphore::new(/*permits*/ 1), + agent_identity_lock: Semaphore::new(/*permits*/ 1), + external_auth: RwLock::new(None), + }) + } + + /// Create an AuthManager with a specific CodexAuth and Agent Identity AuthAPI base URL, for testing only. + #[doc(hidden)] + pub fn from_auth_for_testing_with_agent_identity_authapi_base_url( + auth: CodexAuth, + agent_identity_authapi_base_url: String, + ) -> Arc { + let cached = CachedAuth { + auth: Some(auth), + permanent_refresh_failure: None, + }; + let (auth_change_tx, _auth_change_rx) = watch::channel(0); + Arc::new(Self { + codex_home: PathBuf::from("non-existent"), + inner: RwLock::new(cached), + auth_change_tx, + enable_codex_api_key_env: false, + auth_credentials_store_mode: AuthCredentialsStoreMode::File, + keyring_backend_kind: AuthKeyringBackendKind::default(), + forced_chatgpt_workspace_id: RwLock::new(None), + chatgpt_base_url: None, + agent_identity_authapi_base_url: Some( + agent_identity_authapi_base_url + .trim_end_matches('/') + .to_string(), + ), + refresh_lock: Semaphore::new(/*permits*/ 1), + agent_identity_lock: Semaphore::new(/*permits*/ 1), external_auth: RwLock::new(None), }) } @@ -1690,7 +1891,9 @@ impl AuthManager { keyring_backend_kind: AuthKeyringBackendKind::default(), forced_chatgpt_workspace_id: RwLock::new(None), chatgpt_base_url: None, + agent_identity_authapi_base_url: default_agent_identity_authapi_base_url(), refresh_lock: Semaphore::new(/*permits*/ 1), + agent_identity_lock: Semaphore::new(/*permits*/ 1), external_auth: RwLock::new(Some( Arc::new(BearerTokenRefresher::new(config)) as Arc )), @@ -1736,6 +1939,38 @@ impl AuthManager { self.auth_cached() } + pub async fn agent_identity_auth( + &self, + policy: AgentIdentityAuthPolicy, + session_source: SessionSource, + ) -> std::io::Result> { + let Some(auth) = self.auth().await else { + return Ok(None); + }; + if policy == AgentIdentityAuthPolicy::ChatGptAuth && matches!(auth, CodexAuth::Chatgpt(_)) { + let _bootstrap_permit = self + .agent_identity_lock + .acquire() + .await + .map_err(std::io::Error::other)?; + return auth + .agent_identity_auth( + policy, + self.agent_identity_authapi_base_url.as_deref(), + self.forced_chatgpt_workspace_id(), + session_source, + ) + .await; + } + auth.agent_identity_auth( + policy, + self.agent_identity_authapi_base_url.as_deref(), + self.forced_chatgpt_workspace_id(), + session_source, + ) + .await + } + /// Force a reload of the auth information from auth.json. Returns /// whether the auth value changed. pub async fn reload(&self) -> bool { @@ -1838,7 +2073,7 @@ impl AuthManager { forced_chatgpt_workspace_id.as_deref(), self.chatgpt_base_url.as_deref(), self.keyring_backend_kind, - /*agent_identity_authapi_base_url*/ None, + self.agent_identity_authapi_base_url.as_deref(), ) .await .ok() @@ -1911,6 +2146,7 @@ impl AuthManager { codex_home: PathBuf, enable_codex_api_key_env: bool, auth_credentials_store_mode: AuthCredentialsStoreMode, + forced_chatgpt_workspace_id: Option>, chatgpt_base_url: Option, keyring_backend_kind: AuthKeyringBackendKind, ) -> Arc { @@ -1919,6 +2155,7 @@ impl AuthManager { codex_home, enable_codex_api_key_env, auth_credentials_store_mode, + forced_chatgpt_workspace_id, chatgpt_base_url, keyring_backend_kind, ) @@ -1931,17 +2168,15 @@ impl AuthManager { config: &impl AuthManagerConfig, enable_codex_api_key_env: bool, ) -> Arc { - Arc::new( - Self::new_with_workspace_restriction( - config.codex_home(), - enable_codex_api_key_env, - config.cli_auth_credentials_store_mode(), - config.forced_chatgpt_workspace_id(), - Some(config.chatgpt_base_url()), - config.auth_keyring_backend_kind(), - ) - .await, + Self::shared( + config.codex_home(), + enable_codex_api_key_env, + config.cli_auth_credentials_store_mode(), + config.forced_chatgpt_workspace_id(), + Some(config.chatgpt_base_url()), + config.auth_keyring_backend_kind(), ) + .await } pub fn unauthorized_recovery(self: &Arc) -> UnauthorizedRecovery { diff --git a/codex-rs/login/src/auth/storage.rs b/codex-rs/login/src/auth/storage.rs index ebec064170..19ba018f30 100644 --- a/codex-rs/login/src/auth/storage.rs +++ b/codex-rs/login/src/auth/storage.rs @@ -51,7 +51,7 @@ pub struct AuthDotJson { pub last_refresh: Option>, #[serde(default, skip_serializing_if = "Option::is_none")] - pub agent_identity: Option, + pub agent_identity: Option, #[serde(default, skip_serializing_if = "Option::is_none")] pub personal_access_token: Option, @@ -60,6 +60,32 @@ pub struct AuthDotJson { pub bedrock_api_key: Option, } +#[derive(Deserialize, Serialize, Clone, Debug, PartialEq, Eq)] +#[serde(untagged)] +pub enum AgentIdentityStorage { + Jwt(String), + Record(AgentIdentityAuthRecord), +} + +impl AgentIdentityStorage { + pub fn has_auth_material(&self) -> bool { + match self { + Self::Jwt(jwt) => !jwt.trim().is_empty(), + Self::Record(record) => { + !record.agent_runtime_id.trim().is_empty() + && !record.agent_private_key.trim().is_empty() + } + } + } + + pub(crate) fn as_record(&self) -> Option<&AgentIdentityAuthRecord> { + match self { + Self::Jwt(_) => None, + Self::Record(record) => Some(record), + } + } +} + #[derive(Deserialize, Serialize, Clone, Debug, PartialEq, Eq)] pub struct AgentIdentityAuthRecord { pub agent_runtime_id: String, @@ -69,6 +95,8 @@ pub struct AgentIdentityAuthRecord { pub email: String, pub plan_type: AccountPlanType, pub chatgpt_account_is_fedramp: bool, + #[serde(default, skip_serializing_if = "Option::is_none")] + pub task_id: Option, } impl AgentIdentityAuthRecord { @@ -90,6 +118,7 @@ impl From for AgentIdentityAuthRecord { email: claims.email, plan_type: claims.plan_type.into(), chatgpt_account_is_fedramp: claims.chatgpt_account_is_fedramp, + task_id: None, } } } diff --git a/codex-rs/login/src/auth/storage_tests.rs b/codex-rs/login/src/auth/storage_tests.rs index 72d59b6600..db51a7746e 100644 --- a/codex-rs/login/src/auth/storage_tests.rs +++ b/codex-rs/login/src/auth/storage_tests.rs @@ -81,7 +81,38 @@ async fn file_storage_round_trips_agent_identity_auth() -> anyhow::Result<()> { openai_api_key: None, tokens: None, last_refresh: None, - agent_identity: Some(agent_identity), + agent_identity: Some(AgentIdentityStorage::Jwt(agent_identity)), + personal_access_token: None, + bedrock_api_key: None, + }; + + storage.save(&auth_dot_json)?; + + let loaded = storage.load()?; + assert_eq!(Some(auth_dot_json), loaded); + Ok(()) +} + +#[tokio::test] +async fn file_storage_round_trips_registered_agent_identity_auth() -> anyhow::Result<()> { + let codex_home = tempdir()?; + let storage = FileAuthStorage::new(codex_home.path().to_path_buf()); + let record = AgentIdentityAuthRecord { + agent_runtime_id: "agent-runtime-id".to_string(), + agent_private_key: "private-key".to_string(), + account_id: "account-id".to_string(), + chatgpt_user_id: "user-id".to_string(), + email: "user@example.com".to_string(), + plan_type: AccountPlanType::Pro, + chatgpt_account_is_fedramp: false, + task_id: Some("task-id".to_string()), + }; + let auth_dot_json = AuthDotJson { + auth_mode: Some(AuthMode::Chatgpt), + openai_api_key: None, + tokens: None, + last_refresh: None, + agent_identity: Some(AgentIdentityStorage::Record(record)), personal_access_token: None, bedrock_api_key: None, }; @@ -139,8 +170,8 @@ async fn file_storage_loads_agent_identity_as_jwt() -> anyhow::Result<()> { let loaded = storage.load()?; assert_eq!( - loaded.expect("auth should load").agent_identity.as_deref(), - Some(agent_identity_jwt.as_str()) + loaded.expect("auth should load").agent_identity, + Some(AgentIdentityStorage::Jwt(agent_identity_jwt)) ); Ok(()) } diff --git a/codex-rs/login/src/lib.rs b/codex-rs/login/src/lib.rs index 367f276308..2b8e48b4a5 100644 --- a/codex-rs/login/src/lib.rs +++ b/codex-rs/login/src/lib.rs @@ -17,6 +17,7 @@ pub use server::ServerOptions; pub use server::ShutdownHandle; pub use server::run_login_server; +pub use auth::AgentIdentityAuthPolicy; pub use auth::AuthConfig; pub use auth::AuthDotJson; pub use auth::AuthKeyringBackendKind; diff --git a/codex-rs/login/tests/suite/auth_refresh.rs b/codex-rs/login/tests/suite/auth_refresh.rs index 4065722298..af892d76a0 100644 --- a/codex-rs/login/tests/suite/auth_refresh.rs +++ b/codex-rs/login/tests/suite/auth_refresh.rs @@ -1217,6 +1217,7 @@ impl RefreshTokenTestContext { codex_home.path().to_path_buf(), /*enable_codex_api_key_env*/ false, AuthCredentialsStoreMode::File, + /*forced_chatgpt_workspace_id*/ None, /*chatgpt_base_url*/ None, AuthKeyringBackendKind::default(), ) diff --git a/codex-rs/login/tests/suite/logout.rs b/codex-rs/login/tests/suite/logout.rs index 5df73e932f..3c47bc88b8 100644 --- a/codex-rs/login/tests/suite/logout.rs +++ b/codex-rs/login/tests/suite/logout.rs @@ -201,6 +201,7 @@ async fn auth_manager_logout_with_revoke_uses_cached_auth() -> Result<()> { codex_home.path().to_path_buf(), /*enable_codex_api_key_env*/ false, AuthCredentialsStoreMode::File, + /*forced_chatgpt_workspace_id*/ None, /*chatgpt_base_url*/ None, AuthKeyringBackendKind::default(), ) diff --git a/codex-rs/tui/src/lib.rs b/codex-rs/tui/src/lib.rs index 55cf893076..f189b23149 100644 --- a/codex-rs/tui/src/lib.rs +++ b/codex-rs/tui/src/lib.rs @@ -1163,7 +1163,6 @@ pub async fn run_main( forced_login_method: config.forced_login_method, forced_chatgpt_workspace_id: config.forced_chatgpt_workspace_id.clone(), chatgpt_base_url: Some(config.chatgpt_base_url.clone()), - agent_identity_authapi_base_url: None, }) .await { From 4af7762f0129e903461a351cc1e6daa79cb0cea0 Mon Sep 17 00:00:00 2001 From: Alex Daley Date: Thu, 18 Jun 2026 17:19:24 -0400 Subject: [PATCH 03/10] [connectors] Ignore synthetic links for app accessibility (#28770) Summary - Stop treating Codex Apps MCP tools with `_meta._codex_apps.synthetic_link: true` as evidence that a connector is accessible in `app/list`. - Preserve synthetic tools in the agent-facing MCP connector set so they remain available for install/auth flows. - Keep the app-list accessibility cache limited to connectors backed by at least one non-synthetic tool. - Add focused regression coverage for both sides of the boundary. Validation - `just fmt` - `just test -p codex-core synthetic_links_are_exposed_to_the_agent_but_not_accessible_in_app_list` - `git diff --check` - A crate-wide `just test -p codex-core` run completed with 2,699 passing and 51 unrelated local sandbox/state failures, primarily state DB migration races (`UNIQUE constraint failed: _sqlx_migrations.version`). --- codex-rs/core/src/connectors.rs | 27 +++++++++-- codex-rs/core/src/connectors_tests.rs | 64 +++++++++++++++++++++++++++ 2 files changed, 88 insertions(+), 3 deletions(-) diff --git a/codex-rs/core/src/connectors.rs b/codex-rs/core/src/connectors.rs index 8cc83be3d2..86e5f07f9a 100644 --- a/codex-rs/core/src/connectors.rs +++ b/codex-rs/core/src/connectors.rs @@ -32,6 +32,7 @@ use codex_features::Feature; use codex_login::AuthManager; use codex_login::CodexAuth; use codex_mcp::CODEX_APPS_MCP_SERVER_NAME; +use codex_mcp::MCP_TOOL_CODEX_APPS_META_KEY; use codex_mcp::McpConnectionManager; use codex_mcp::McpRuntimeContext; use codex_mcp::ToolInfo; @@ -154,7 +155,7 @@ pub(crate) fn refresh_accessible_connectors_cache_from_mcp_tools( } let cache_key = accessible_connectors_cache_key(config, auth); - let accessible_connectors = accessible_connectors_from_mcp_tools(mcp_tools); + let accessible_connectors = accessible_connectors_for_app_list_from_mcp_tools(mcp_tools); write_cached_accessible_connectors(cache_key, &accessible_connectors); } @@ -339,7 +340,7 @@ pub async fn list_accessible_connectors_from_mcp_tools_with_mcp_manager( cancel_token.cancel(); } - let accessible_connectors = accessible_connectors_from_mcp_tools(&tools); + let accessible_connectors = accessible_connectors_for_app_list_from_mcp_tools(&tools); if codex_apps_ready || !accessible_connectors.is_empty() { write_cached_accessible_connectors(cache_key, &accessible_connectors); } @@ -469,9 +470,15 @@ async fn cached_directory_connectors_for_tool_suggest_with_auth( } pub(crate) fn accessible_connectors_from_mcp_tools(mcp_tools: &[ToolInfo]) -> Vec { + collect_accessible_connectors_from_mcp_tools(mcp_tools.iter()) +} + +fn collect_accessible_connectors_from_mcp_tools<'a>( + mcp_tools: impl Iterator, +) -> Vec { // ToolInfo already carries plugin provenance, so app-level plugin sources // can be derived here instead of requiring a separate enrichment pass. - let tools = mcp_tools.iter().filter_map(|tool| { + let tools = mcp_tools.filter_map(|tool| { if tool.server_name != CODEX_APPS_MCP_SERVER_NAME { return None; } @@ -486,6 +493,20 @@ pub(crate) fn accessible_connectors_from_mcp_tools(mcp_tools: &[ToolInfo]) -> Ve codex_connectors::accessible::collect_accessible_connectors(tools) } +fn accessible_connectors_for_app_list_from_mcp_tools(mcp_tools: &[ToolInfo]) -> Vec { + let non_synthetic_tools = mcp_tools.iter().filter(|tool| { + tool.tool + .meta + .as_deref() + .and_then(|meta| meta.get(MCP_TOOL_CODEX_APPS_META_KEY)) + .and_then(serde_json::Value::as_object) + .and_then(|meta| meta.get("synthetic_link")) + .and_then(serde_json::Value::as_bool) + != Some(true) + }); + collect_accessible_connectors_from_mcp_tools(non_synthetic_tools) +} + pub fn with_app_enabled_state(mut connectors: Vec, config: &Config) -> Vec { let user_apps_config = apps_config_from_layer_stack(&config.config_layer_stack); let requirements_apps_config = config.config_layer_stack.requirements_toml().apps.as_ref(); diff --git a/codex-rs/core/src/connectors_tests.rs b/codex-rs/core/src/connectors_tests.rs index 4e6cb55aaa..5bbd6a1f88 100644 --- a/codex-rs/core/src/connectors_tests.rs +++ b/codex-rs/core/src/connectors_tests.rs @@ -17,6 +17,7 @@ use codex_mcp::CODEX_APPS_MCP_SERVER_NAME; use codex_mcp::ToolInfo; use pretty_assertions::assert_eq; use rmcp::model::JsonObject; +use rmcp::model::Meta; use rmcp::model::Tool; use std::collections::BTreeMap; use std::collections::HashSet; @@ -140,6 +141,69 @@ fn accessible_connectors_from_mcp_tools_carries_plugin_display_names() { ); } +#[test] +fn synthetic_links_are_exposed_to_the_agent_but_not_accessible_in_app_list() { + let mut synthetic_tool = codex_app_tool("gmail_batch_read_email", "gmail", Some("Gmail"), &[]); + synthetic_tool.tool.meta = Some(Meta( + serde_json::json!({ + "resource_name": "gmail.batch_read_email", + "_codex_apps": { + "resource_uri": "/connector/gmail/batch_read_email", + "contains_mcp_source": false, + "synthetic_link": true + } + }) + .as_object() + .expect("meta should be an object") + .clone(), + )); + let tools = vec![ + synthetic_tool, + codex_app_tool("calendar_list_events", "calendar", Some("Calendar"), &[]), + ]; + + let calendar = AppInfo { + id: "calendar".to_string(), + name: "Calendar".to_string(), + description: None, + logo_url: None, + logo_url_dark: None, + distribution_channel: None, + install_url: Some(connector_install_url("Calendar", "calendar")), + branding: None, + app_metadata: None, + labels: None, + is_accessible: true, + is_enabled: true, + plugin_display_names: Vec::new(), + }; + assert_eq!( + accessible_connectors_for_app_list_from_mcp_tools(&tools), + vec![calendar.clone()] + ); + assert_eq!( + accessible_connectors_from_mcp_tools(&tools), + vec![ + calendar, + AppInfo { + id: "gmail".to_string(), + name: "Gmail".to_string(), + description: None, + logo_url: None, + logo_url_dark: None, + distribution_channel: None, + install_url: Some(connector_install_url("Gmail", "gmail")), + branding: None, + app_metadata: None, + labels: None, + is_accessible: true, + is_enabled: true, + plugin_display_names: Vec::new(), + } + ] + ); +} + #[tokio::test] async fn refresh_accessible_connectors_cache_from_mcp_tools_writes_latest_installed_apps() { let codex_home = tempdir().expect("tempdir should succeed"); From 406062c3af8b27c8e1b4b83c485ebe1ae0df874c Mon Sep 17 00:00:00 2001 From: xl-openai Date: Thu, 18 Jun 2026 14:29:01 -0700 Subject: [PATCH 04/10] [codex] Preserve remote plugin download status errors (#28863) ## Summary - preserve the original HTTP status when a remote plugin bundle download returns a non-success response - retain at most 8 KiB of the error response body and annotate truncation or body-read failures - add regression coverage for an oversized error response ## Root cause The non-success response path reused the normal size-limited body reader. When an error response exceeded 8 KiB, that reader returned `DownloadTooLarge` before the code constructed `DownloadStatus`, masking the upstream HTTP status and response context. ## Impact Remote plugin installation failures now retain the actionable upstream HTTP status without allowing unbounded error bodies into logs. ## Validation - `just test -p codex-app-server plugin_install_preserves_status_when_remote_bundle_error_body_is_too_large` - `just fmt` - `git diff --check` --- .../tests/suite/v2/plugin_install.rs | 25 ++++++++---- codex-rs/core-plugins/src/remote_bundle.rs | 38 +++++++++++++++---- 2 files changed, 49 insertions(+), 14 deletions(-) diff --git a/codex-rs/app-server/tests/suite/v2/plugin_install.rs b/codex-rs/app-server/tests/suite/v2/plugin_install.rs index 4026bef357..3fc40d2dce 100644 --- a/codex-rs/app-server/tests/suite/v2/plugin_install.rs +++ b/codex-rs/app-server/tests/suite/v2/plugin_install.rs @@ -1010,15 +1010,12 @@ async fn plugin_install_tracks_remote_plugin_analytics_event() -> Result<()> { } #[tokio::test] -async fn plugin_install_errors_when_remote_bundle_download_fails() -> Result<()> { +async fn plugin_install_preserves_status_when_remote_bundle_error_body_is_too_large() -> Result<()> +{ let codex_home = TempDir::new()?; let server = MockServer::start().await; - let bundle_url = mount_remote_plugin_bundle( - &server, - /*status_code*/ 503, - b"bundle temporarily unavailable".to_vec(), - ) - .await; + let bundle_url = + mount_remote_plugin_bundle(&server, /*status_code*/ 503, vec![b'x'; 8 * 1024 + 1]).await; configure_remote_plugin_test(codex_home.path(), &server)?; mount_remote_plugin_detail(&server, REMOTE_PLUGIN_ID, "1.2.3", Some(&bundle_url)).await; mount_empty_remote_installed_plugins(&server).await; @@ -1041,6 +1038,20 @@ async fn plugin_install_errors_when_remote_bundle_download_fails() -> Result<()> assert_eq!(err.error.code, -32603); assert!(err.error.message.contains("failed with status 503")); + assert!( + err.error + .message + .contains("[response body truncated after 8192 bytes]") + ); + assert_eq!( + err.error + .message + .bytes() + .filter(|byte| *byte == b'x') + .count(), + 8192 + ); + assert!(!err.error.message.contains("exceeded maximum size")); wait_for_remote_plugin_request_count( &server, "GET", diff --git a/codex-rs/core-plugins/src/remote_bundle.rs b/codex-rs/core-plugins/src/remote_bundle.rs index cb60d60b77..49e6bd4138 100644 --- a/codex-rs/core-plugins/src/remote_bundle.rs +++ b/codex-rs/core-plugins/src/remote_bundle.rs @@ -293,13 +293,37 @@ async fn download_remote_plugin_bundle_with_limit( let url = final_url.to_string(); let status = response.status(); if !status.is_success() { - let body = read_response_body_with_limit( - response, - &url, - /*max_bytes*/ REMOTE_PLUGIN_BUNDLE_ERROR_BODY_MAX_BYTES, - ) - .await?; - let body = String::from_utf8_lossy(&body).to_string(); + let mut response = response; + let mut body = Vec::new(); + let mut body_truncated = false; + let mut body_read_error = None; + loop { + let chunk = match response.chunk().await { + Ok(Some(chunk)) => chunk, + Ok(None) => break, + Err(source) => { + body_read_error = Some(source); + break; + } + }; + let remaining = REMOTE_PLUGIN_BUNDLE_ERROR_BODY_MAX_BYTES as usize - body.len(); + if chunk.len() > remaining { + body.extend_from_slice(&chunk[..remaining]); + body_truncated = true; + break; + } + body.extend_from_slice(&chunk); + } + + let mut body = String::from_utf8_lossy(&body).into_owned(); + if body_truncated { + body.push_str(&format!( + "\n[response body truncated after {REMOTE_PLUGIN_BUNDLE_ERROR_BODY_MAX_BYTES} bytes]" + )); + } + if let Some(source) = body_read_error { + body.push_str(&format!("\n[failed to read response body: {source}]")); + } return Err(RemotePluginBundleInstallError::DownloadStatus { url, status, body }); } From dce673905a191744138dbba19a7d3f8d85b10d6c Mon Sep 17 00:00:00 2001 From: "Adam Perry @ OpenAI" Date: Thu, 18 Jun 2026 15:06:23 -0700 Subject: [PATCH 05/10] core: load AGENTS.md from foreign environments (#28958) ## Why Make it possible to load AGENTS.md from remote exec-servers whose OS is different than app-server. ## What - keep `AGENTS.md` discovery and provenance as `PathUri`, with root-aware parent and ancestor traversal - expose lifecycle instruction sources as legacy app-server path strings in events while retaining `PathUri` internally - preserve and test mixed POSIX and Windows paths in model context and TUI status output - cover remote Windows loading end to end by seeding the Wine prefix through host filesystem APIs - fix bug in `PathUri`'s parent() implementation that would erase Windows drive letters --- bazel/rules/testing/wine/src/lib.rs | 8 + .../codex_app_server_protocol.schemas.json | 12 +- .../codex_app_server_protocol.v2.schemas.json | 12 +- .../schema/json/v2/ThreadForkResponse.json | 4 +- .../schema/json/v2/ThreadResumeResponse.json | 4 +- .../schema/json/v2/ThreadStartResponse.json | 4 +- .../typescript/v2/ThreadForkResponse.ts | 5 +- .../typescript/v2/ThreadResumeResponse.ts | 5 +- .../typescript/v2/ThreadStartResponse.ts | 5 +- .../src/protocol/common.rs | 6 +- .../src/protocol/v2/tests.rs | 41 +++- .../src/protocol/v2/thread.rs | 52 ++++- codex-rs/app-server/README.md | 2 +- .../request_processors/thread_processor.rs | 8 +- codex-rs/app-server/src/thread_state.rs | 5 +- .../tests/suite/v2/thread_resume.rs | 6 +- .../app-server/tests/suite/v2/thread_start.rs | 6 +- codex-rs/core/src/agents_md.rs | 63 +++--- codex-rs/core/src/agents_md_tests.rs | 181 ++++++++++++++---- codex-rs/core/src/codex_thread.rs | 12 +- codex-rs/core/src/session/mod.rs | 6 +- codex-rs/core/tests/common/test_codex.rs | 16 +- .../core/tests/remote_env_windows/BUILD.bazel | 1 - .../remote_env_windows_test.rs | 37 +++- codex-rs/core/tests/suite/agents_md.rs | 62 +++--- codex-rs/core/tests/suite/compact.rs | 13 +- .../core/tests/suite/hierarchical_agents.rs | 3 - .../exec-server/testing/wine_exec_server.rs | 9 +- .../testing/wine_remote_test_runner.rs | 2 +- codex-rs/tui/Cargo.toml | 2 +- codex-rs/tui/src/app_server_session.rs | 16 +- codex-rs/tui/src/chatwidget.rs | 3 +- .../src/chatwidget/tests/slash_commands.rs | 4 +- .../chatwidget/tests/status_command_tests.rs | 23 ++- codex-rs/tui/src/session_state.rs | 3 +- codex-rs/tui/src/status/helpers.rs | 50 ++++- codex-rs/utils/path-uri/src/lib.rs | 26 ++- codex-rs/utils/path-uri/src/tests.rs | 36 +++- 38 files changed, 550 insertions(+), 203 deletions(-) diff --git a/bazel/rules/testing/wine/src/lib.rs b/bazel/rules/testing/wine/src/lib.rs index 3b99f5870b..76cb029e17 100644 --- a/bazel/rules/testing/wine/src/lib.rs +++ b/bazel/rules/testing/wine/src/lib.rs @@ -105,6 +105,14 @@ impl WineTestCommand { } impl WineTestProcess { + /// Returns the host path to this process's isolated Wine prefix. + pub fn prefix_path(&self) -> &Path { + let Some(processes) = self.processes.as_ref() else { + panic!("Wine process guard is missing"); + }; + processes.prefix.path() + } + /// Takes the piped standard output of the Wine process. /// /// This may only be called once for a process created by 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 e9611c67e1..0a18c588f3 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 @@ -16973,9 +16973,9 @@ }, "instructionSources": { "default": [], - "description": "Instruction source files currently loaded for this thread.", + "description": "Environment-native paths to instruction source files currently loaded for this thread.", "items": { - "$ref": "#/definitions/v2/AbsolutePathBuf" + "$ref": "#/definitions/v2/LegacyAppPathString" }, "type": "array" }, @@ -18688,9 +18688,9 @@ }, "instructionSources": { "default": [], - "description": "Instruction source files currently loaded for this thread.", + "description": "Environment-native paths to instruction source files currently loaded for this thread.", "items": { - "$ref": "#/definitions/v2/AbsolutePathBuf" + "$ref": "#/definitions/v2/LegacyAppPathString" }, "type": "array" }, @@ -19105,9 +19105,9 @@ }, "instructionSources": { "default": [], - "description": "Instruction source files currently loaded for this thread.", + "description": "Environment-native paths to instruction source files currently loaded for this thread.", "items": { - "$ref": "#/definitions/v2/AbsolutePathBuf" + "$ref": "#/definitions/v2/LegacyAppPathString" }, "type": "array" }, 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 bac9a48e70..0ddfdb62bd 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 @@ -14752,9 +14752,9 @@ }, "instructionSources": { "default": [], - "description": "Instruction source files currently loaded for this thread.", + "description": "Environment-native paths to instruction source files currently loaded for this thread.", "items": { - "$ref": "#/definitions/AbsolutePathBuf" + "$ref": "#/definitions/LegacyAppPathString" }, "type": "array" }, @@ -16467,9 +16467,9 @@ }, "instructionSources": { "default": [], - "description": "Instruction source files currently loaded for this thread.", + "description": "Environment-native paths to instruction source files currently loaded for this thread.", "items": { - "$ref": "#/definitions/AbsolutePathBuf" + "$ref": "#/definitions/LegacyAppPathString" }, "type": "array" }, @@ -16884,9 +16884,9 @@ }, "instructionSources": { "default": [], - "description": "Instruction source files currently loaded for this thread.", + "description": "Environment-native paths to instruction source files currently loaded for this thread.", "items": { - "$ref": "#/definitions/AbsolutePathBuf" + "$ref": "#/definitions/LegacyAppPathString" }, "type": "array" }, 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 9ced571cfa..bebd194497 100644 --- a/codex-rs/app-server-protocol/schema/json/v2/ThreadForkResponse.json +++ b/codex-rs/app-server-protocol/schema/json/v2/ThreadForkResponse.json @@ -2368,9 +2368,9 @@ }, "instructionSources": { "default": [], - "description": "Instruction source files currently loaded for this thread.", + "description": "Environment-native paths to instruction source files currently loaded for this thread.", "items": { - "$ref": "#/definitions/AbsolutePathBuf" + "$ref": "#/definitions/LegacyAppPathString" }, "type": "array" }, 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 28a5f5b24a..73e228845d 100644 --- a/codex-rs/app-server-protocol/schema/json/v2/ThreadResumeResponse.json +++ b/codex-rs/app-server-protocol/schema/json/v2/ThreadResumeResponse.json @@ -2394,9 +2394,9 @@ }, "instructionSources": { "default": [], - "description": "Instruction source files currently loaded for this thread.", + "description": "Environment-native paths to instruction source files currently loaded for this thread.", "items": { - "$ref": "#/definitions/AbsolutePathBuf" + "$ref": "#/definitions/LegacyAppPathString" }, "type": "array" }, 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 1baf6e4bdc..726088c0d9 100644 --- a/codex-rs/app-server-protocol/schema/json/v2/ThreadStartResponse.json +++ b/codex-rs/app-server-protocol/schema/json/v2/ThreadStartResponse.json @@ -2368,9 +2368,9 @@ }, "instructionSources": { "default": [], - "description": "Instruction source files currently loaded for this thread.", + "description": "Environment-native paths to instruction source files currently loaded for this thread.", "items": { - "$ref": "#/definitions/AbsolutePathBuf" + "$ref": "#/definitions/LegacyAppPathString" }, "type": "array" }, diff --git a/codex-rs/app-server-protocol/schema/typescript/v2/ThreadForkResponse.ts b/codex-rs/app-server-protocol/schema/typescript/v2/ThreadForkResponse.ts index c5b1201c26..9577562476 100644 --- a/codex-rs/app-server-protocol/schema/typescript/v2/ThreadForkResponse.ts +++ b/codex-rs/app-server-protocol/schema/typescript/v2/ThreadForkResponse.ts @@ -2,6 +2,7 @@ // 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 { LegacyAppPathString } from "../LegacyAppPathString"; import type { ReasoningEffort } from "../ReasoningEffort"; import type { ApprovalsReviewer } from "./ApprovalsReviewer"; import type { AskForApproval } from "./AskForApproval"; @@ -9,9 +10,9 @@ import type { SandboxPolicy } from "./SandboxPolicy"; import type { Thread } from "./Thread"; export type ThreadForkResponse = {thread: Thread, model: string, modelProvider: string, serviceTier: string | null, cwd: AbsolutePathBuf, /** - * Instruction source files currently loaded for this thread. + * Environment-native paths to instruction source files currently loaded for this thread. */ -instructionSources: Array, approvalPolicy: AskForApproval, /** +instructionSources: Array, approvalPolicy: AskForApproval, /** * Reviewer currently used for approval requests on this thread. */ approvalsReviewer: ApprovalsReviewer, /** diff --git a/codex-rs/app-server-protocol/schema/typescript/v2/ThreadResumeResponse.ts b/codex-rs/app-server-protocol/schema/typescript/v2/ThreadResumeResponse.ts index 7a4f90377c..e1f7d642be 100644 --- a/codex-rs/app-server-protocol/schema/typescript/v2/ThreadResumeResponse.ts +++ b/codex-rs/app-server-protocol/schema/typescript/v2/ThreadResumeResponse.ts @@ -2,6 +2,7 @@ // 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 { LegacyAppPathString } from "../LegacyAppPathString"; import type { ReasoningEffort } from "../ReasoningEffort"; import type { ApprovalsReviewer } from "./ApprovalsReviewer"; import type { AskForApproval } from "./AskForApproval"; @@ -9,9 +10,9 @@ import type { SandboxPolicy } from "./SandboxPolicy"; import type { Thread } from "./Thread"; export type ThreadResumeResponse = {thread: Thread, model: string, modelProvider: string, serviceTier: string | null, cwd: AbsolutePathBuf, /** - * Instruction source files currently loaded for this thread. + * Environment-native paths to instruction source files currently loaded for this thread. */ -instructionSources: Array, approvalPolicy: AskForApproval, /** +instructionSources: Array, approvalPolicy: AskForApproval, /** * Reviewer currently used for approval requests on this thread. */ approvalsReviewer: ApprovalsReviewer, /** diff --git a/codex-rs/app-server-protocol/schema/typescript/v2/ThreadStartResponse.ts b/codex-rs/app-server-protocol/schema/typescript/v2/ThreadStartResponse.ts index 38859a3805..992ab5dba7 100644 --- a/codex-rs/app-server-protocol/schema/typescript/v2/ThreadStartResponse.ts +++ b/codex-rs/app-server-protocol/schema/typescript/v2/ThreadStartResponse.ts @@ -2,6 +2,7 @@ // 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 { LegacyAppPathString } from "../LegacyAppPathString"; import type { ReasoningEffort } from "../ReasoningEffort"; import type { ApprovalsReviewer } from "./ApprovalsReviewer"; import type { AskForApproval } from "./AskForApproval"; @@ -9,9 +10,9 @@ import type { SandboxPolicy } from "./SandboxPolicy"; import type { Thread } from "./Thread"; export type ThreadStartResponse = {thread: Thread, model: string, modelProvider: string, serviceTier: string | null, cwd: AbsolutePathBuf, /** - * Instruction source files currently loaded for this thread. + * Environment-native paths to instruction source files currently loaded for this thread. */ -instructionSources: Array, approvalPolicy: AskForApproval, /** +instructionSources: Array, approvalPolicy: AskForApproval, /** * Reviewer currently used for approval requests on this thread. */ approvalsReviewer: ApprovalsReviewer, /** diff --git a/codex-rs/app-server-protocol/src/protocol/common.rs b/codex-rs/app-server-protocol/src/protocol/common.rs index e5a882b3b5..3336f74a63 100644 --- a/codex-rs/app-server-protocol/src/protocol/common.rs +++ b/codex-rs/app-server-protocol/src/protocol/common.rs @@ -2573,7 +2573,11 @@ mod tests { service_tier: None, cwd, runtime_workspace_roots: Vec::new(), - instruction_sources: vec![absolute_path("/tmp/AGENTS.md")], + instruction_sources: vec![ + codex_utils_path_uri::LegacyAppPathString::from_abs_path(&absolute_path( + "/tmp/AGENTS.md", + )), + ], approval_policy: v2::AskForApproval::OnFailure, approvals_reviewer: v2::ApprovalsReviewer::User, sandbox: v2::SandboxPolicy::DangerFullAccess, 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 0368bf45f6..459b11f7f2 100644 --- a/codex-rs/app-server-protocol/src/protocol/v2/tests.rs +++ b/codex-rs/app-server-protocol/src/protocol/v2/tests.rs @@ -36,6 +36,7 @@ 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::LegacyAppPathString; +use codex_utils_path_uri::PathUri; use pretty_assertions::assert_eq; use serde_json::Value as JsonValue; use serde_json::json; @@ -3685,17 +3686,49 @@ fn thread_lifecycle_responses_default_missing_optional_fields() { serde_json::from_value(response.clone()).expect("thread/start response"); let resume: ThreadResumeResponse = serde_json::from_value(response.clone()).expect("thread/resume response"); - let fork: ThreadForkResponse = serde_json::from_value(response).expect("thread/fork response"); + let fork: ThreadForkResponse = + serde_json::from_value(response.clone()).expect("thread/fork response"); - assert_eq!(start.instruction_sources, Vec::::new()); + assert_eq!(start.instruction_sources, Vec::::new()); assert_eq!(start.thread.parent_thread_id, None); assert_eq!(start.thread.recency_at, None); - assert_eq!(resume.instruction_sources, Vec::::new()); - assert_eq!(fork.instruction_sources, Vec::::new()); + assert_eq!( + resume.instruction_sources, + Vec::::new() + ); + assert_eq!(fork.instruction_sources, Vec::::new()); assert_eq!(start.active_permission_profile, None); assert_eq!(resume.active_permission_profile, None); assert_eq!(resume.initial_turns_page, None); assert_eq!(fork.active_permission_profile, None); + + let foreign_source: LegacyAppPathString = + serde_json::from_value(json!(r"C:\workspace\AGENTS.md")).expect("foreign source"); + let mut response_with_foreign_source = response; + response_with_foreign_source["instructionSources"] = json!([foreign_source.as_str()]); + let start: ThreadStartResponse = serde_json::from_value(response_with_foreign_source.clone()) + .expect("thread/start response with foreign source"); + let resume: ThreadResumeResponse = serde_json::from_value(response_with_foreign_source.clone()) + .expect("thread/resume response with foreign source"); + let fork: ThreadForkResponse = serde_json::from_value(response_with_foreign_source) + .expect("thread/fork response with foreign source"); + assert_eq!(start.instruction_sources, vec![foreign_source.clone()]); + assert_eq!(resume.instruction_sources, vec![foreign_source.clone()]); + assert_eq!(fork.instruction_sources, vec![foreign_source]); + let foreign_source_uri = + PathUri::parse("file:///C:/workspace/AGENTS.md").expect("foreign source URI"); + assert_eq!( + start.instruction_source_path_uris(), + vec![foreign_source_uri.clone()] + ); + assert_eq!( + resume.instruction_source_path_uris(), + vec![foreign_source_uri.clone()] + ); + assert_eq!( + fork.instruction_source_path_uris(), + vec![foreign_source_uri] + ); } #[test] diff --git a/codex-rs/app-server-protocol/src/protocol/v2/thread.rs b/codex-rs/app-server-protocol/src/protocol/v2/thread.rs index b5809f09b8..59adcbaa85 100644 --- a/codex-rs/app-server-protocol/src/protocol/v2/thread.rs +++ b/codex-rs/app-server-protocol/src/protocol/v2/thread.rs @@ -26,6 +26,8 @@ use codex_protocol::protocol::ThreadGoalStatus as CoreThreadGoalStatus; use codex_protocol::protocol::TokenUsage as CoreTokenUsage; use codex_protocol::protocol::TokenUsageInfo as CoreTokenUsageInfo; use codex_utils_absolute_path::AbsolutePathBuf; +use codex_utils_path_uri::LegacyAppPathString; +use codex_utils_path_uri::PathUri; use schemars::JsonSchema; use serde::Deserialize; use serde::Serialize; @@ -161,9 +163,9 @@ pub struct ThreadStartResponse { #[experimental("thread/start.runtimeWorkspaceRoots")] #[serde(default)] pub runtime_workspace_roots: Vec, - /// Instruction source files currently loaded for this thread. + /// Environment-native paths to instruction source files currently loaded for this thread. #[serde(default)] - pub instruction_sources: Vec, + pub instruction_sources: Vec, #[experimental(nested)] pub approval_policy: AskForApproval, /// Reviewer currently used for approval requests on this thread. @@ -179,6 +181,13 @@ pub struct ThreadStartResponse { pub reasoning_effort: Option, } +impl ThreadStartResponse { + /// Parses valid absolute instruction source paths and omits malformed legacy values. + pub fn instruction_source_path_uris(&self) -> Vec { + instruction_source_path_uris(&self.instruction_sources) + } +} + #[derive( Serialize, Deserialize, Debug, Default, Clone, PartialEq, JsonSchema, TS, ExperimentalApi, )] @@ -375,9 +384,9 @@ pub struct ThreadResumeResponse { #[experimental("thread/resume.runtimeWorkspaceRoots")] #[serde(default)] pub runtime_workspace_roots: Vec, - /// Instruction source files currently loaded for this thread. + /// Environment-native paths to instruction source files currently loaded for this thread. #[serde(default)] - pub instruction_sources: Vec, + pub instruction_sources: Vec, #[experimental(nested)] pub approval_policy: AskForApproval, /// Reviewer currently used for approval requests on this thread. @@ -397,6 +406,13 @@ pub struct ThreadResumeResponse { pub initial_turns_page: Option, } +impl ThreadResumeResponse { + /// Parses valid absolute instruction source paths and omits malformed legacy values. + pub fn instruction_source_path_uris(&self) -> Vec { + instruction_source_path_uris(&self.instruction_sources) + } +} + #[derive(Serialize, Deserialize, Debug, Clone, PartialEq, JsonSchema, TS)] #[serde(rename_all = "camelCase")] #[ts(export_to = "v2/")] @@ -523,9 +539,9 @@ pub struct ThreadForkResponse { #[experimental("thread/fork.runtimeWorkspaceRoots")] #[serde(default)] pub runtime_workspace_roots: Vec, - /// Instruction source files currently loaded for this thread. + /// Environment-native paths to instruction source files currently loaded for this thread. #[serde(default)] - pub instruction_sources: Vec, + pub instruction_sources: Vec, #[experimental(nested)] pub approval_policy: AskForApproval, /// Reviewer currently used for approval requests on this thread. @@ -541,6 +557,30 @@ pub struct ThreadForkResponse { pub reasoning_effort: Option, } +impl ThreadForkResponse { + /// Parses valid absolute instruction source paths and omits malformed legacy values. + pub fn instruction_source_path_uris(&self) -> Vec { + instruction_source_path_uris(&self.instruction_sources) + } +} + +fn instruction_source_path_uris(sources: &[LegacyAppPathString]) -> Vec { + // Instruction sources are advisory diagnostics. Warn and fail open so a malformed legacy + // path cannot fail thread start, resume, or fork. + sources + .iter() + .filter_map(|source| { + source.to_inferred_path_uri().or_else(|| { + tracing::warn!( + path = source.as_str(), + "ignoring invalid instruction source path from app-server" + ); + None + }) + }) + .collect() +} + #[derive(Serialize, Deserialize, Debug, Clone, PartialEq, JsonSchema, TS)] #[serde(rename_all = "camelCase")] #[ts(export_to = "v2/")] diff --git a/codex-rs/app-server/README.md b/codex-rs/app-server/README.md index deda1c517e..871f9cee79 100644 --- a/codex-rs/app-server/README.md +++ b/codex-rs/app-server/README.md @@ -140,7 +140,7 @@ Example with notification opt-out: - `thread/start` — create a new thread; emits `thread/started` (including the current `thread.status`) and auto-subscribes you to turn/item events for that thread. When the request includes a `cwd` and the resolved sandbox is `workspace-write` or full access, app-server also marks that project as trusted in the user `config.toml`. Pass `sessionStartSource: "clear"` when starting a replacement thread after clearing the current session so `SessionStart` hooks receive `source: "clear"` instead of the default `"startup"`. Experimental `runtimeWorkspaceRoots` replaces the thread-scoped runtime workspace roots used to materialize `:workspace_roots`; paths must be absolute. For permissions, prefer experimental `permissions` profile selection by id; the legacy `sandbox` shorthand is still accepted but cannot be combined with `permissions`. Experimental `environments` selects the sticky execution environments for turns on the thread; omit it to use the server default, pass `[]` to disable environments, or pass explicit environment ids with per-environment `cwd`. Experimental `selectedCapabilityRoots` selects environment-owned plugin or standalone-skill roots. Skills found below those roots are listed and read through the owning environment. Stdio MCP servers declared by selected plugins are also started in that environment; HTTP MCP declarations remain inactive. - `thread/resume` — reopen an existing thread by id so subsequent `turn/start` calls append to it. Accepts the same permission override rules as `thread/start`. - `thread/fork` — fork an existing thread into a new thread id by copying the stored history; if the source thread is currently mid-turn, the fork records the same interruption marker as `turn/interrupt` instead of inheriting an unmarked partial turn suffix. The returned `thread.forkedFromId` points at the source thread when known. Accepts `ephemeral: true` for an in-memory temporary fork, emits `thread/started` (including the current `thread.status`), and auto-subscribes you to turn/item events for the new thread. Experimental clients can pass `excludeTurns: true` when they plan to page fork history via `thread/turns/list` instead of receiving the full turn array immediately. Accepts the same permission override rules as `thread/start`. -- `thread/start`, `thread/resume`, and `thread/fork` responses include the legacy `sandbox` compatibility projection. Experimental clients can read `runtimeWorkspaceRoots` for the thread-scoped runtime roots and `activePermissionProfile` for the named or implicit built-in profile identity/provenance when known. +- `thread/start`, `thread/resume`, and `thread/fork` responses include the legacy `sandbox` compatibility projection. `instructionSources` lists loaded instruction files using each source environment's native absolute path syntax, including files loaded from remote environments. Experimental clients can read `runtimeWorkspaceRoots` for the thread-scoped runtime roots and `activePermissionProfile` for the named or implicit built-in profile identity/provenance when known. - `thread/list` — page through stored threads; supports cursor-based pagination and optional `modelProviders`, `sourceKinds`, `archived`, `cwd`, and `searchTerm` filters. Experimental clients can use `parentThreadId` to filter direct spawned children represented by persisted spawn-edge state. Review and Guardian threads are not included because they do not participate in that spawn-edge lifecycle. Each returned `thread` includes `status` (`ThreadStatus`), defaulting to `notLoaded` when the thread is not currently loaded. Subagent threads also include `parentThreadId` when the immediate parent is known. - `thread/loaded/list` — list the thread ids currently loaded in memory. - `thread/read` — read a stored thread by id without resuming it; optionally include turns via `includeTurns`. The returned `thread` includes `status` (`ThreadStatus`), defaulting to `notLoaded` when the thread is not currently loaded. diff --git a/codex-rs/app-server/src/request_processors/thread_processor.rs b/codex-rs/app-server/src/request_processors/thread_processor.rs index 63883cbdfe..1aa6e4eeac 100644 --- a/codex-rs/app-server/src/request_processors/thread_processor.rs +++ b/codex-rs/app-server/src/request_processors/thread_processor.rs @@ -1181,7 +1181,7 @@ impl ThreadRequestProcessor { ) .await?; - let instruction_sources = thread.instruction_sources().await; + let instruction_sources = thread.legacy_instruction_sources().await; let config_snapshot = thread .config_snapshot() .instrument(tracing::info_span!( @@ -2681,7 +2681,7 @@ impl ThreadRequestProcessor { self.outgoing.send_error(request_id, err).await; return Ok(()); } - let instruction_sources = codex_thread.instruction_sources().await; + let instruction_sources = codex_thread.legacy_instruction_sources().await; let SessionConfiguredEvent { rollout_path, .. } = session_configured; let Some(rollout_path) = rollout_path else { let error = @@ -2989,7 +2989,7 @@ impl ThreadRequestProcessor { /*include_turns*/ false, ); thread_summary.session_id = existing_thread.session_configured().session_id.to_string(); - let instruction_sources = existing_thread.instruction_sources().await; + let instruction_sources = existing_thread.legacy_instruction_sources().await; let listener_command_tx = { let thread_state = thread_state.lock().await; @@ -3422,7 +3422,7 @@ impl ThreadRequestProcessor { .map_err(|err| core_thread_write_error("inherit source thread name", err))?; } - let instruction_sources = forked_thread.instruction_sources().await; + let instruction_sources = forked_thread.legacy_instruction_sources().await; // Auto-attach a conversation listener when forking a thread. log_listener_attach_result( diff --git a/codex-rs/app-server/src/thread_state.rs b/codex-rs/app-server/src/thread_state.rs index 890d4da5eb..91de76b99c 100644 --- a/codex-rs/app-server/src/thread_state.rs +++ b/codex-rs/app-server/src/thread_state.rs @@ -13,7 +13,7 @@ use codex_protocol::ThreadId; use codex_protocol::protocol::EventMsg; use codex_protocol::protocol::RolloutItem; use codex_rollout::state_db::StateDbHandle; -use codex_utils_absolute_path::AbsolutePathBuf; +use codex_utils_path_uri::LegacyAppPathString; use std::collections::HashMap; use std::collections::HashSet; use std::sync::Arc; @@ -31,7 +31,7 @@ pub(crate) struct PendingThreadResumeRequest { pub(crate) request_id: ConnectionRequestId, pub(crate) history_items: Vec, pub(crate) config_snapshot: ThreadConfigSnapshot, - pub(crate) instruction_sources: Vec, + pub(crate) instruction_sources: Vec, pub(crate) thread_summary: codex_app_server_protocol::Thread, pub(crate) emit_thread_goal_update: bool, pub(crate) thread_goal_state_db: Option, @@ -200,6 +200,7 @@ mod tests { use codex_protocol::config_types::CollaborationMode; use codex_protocol::config_types::ModeKind; use codex_protocol::config_types::Settings; + use codex_utils_absolute_path::AbsolutePathBuf; use pretty_assertions::assert_eq; #[test] diff --git a/codex-rs/app-server/tests/suite/v2/thread_resume.rs b/codex-rs/app-server/tests/suite/v2/thread_resume.rs index cd508fc687..07ee66caf2 100644 --- a/codex-rs/app-server/tests/suite/v2/thread_resume.rs +++ b/codex-rs/app-server/tests/suite/v2/thread_resume.rs @@ -82,6 +82,7 @@ use codex_rollout::append_rollout_item_to_path; use codex_rollout::read_session_meta_line; use codex_state::StateRuntime; use codex_utils_absolute_path::AbsolutePathBuf; +use codex_utils_path_uri::LegacyAppPathString; use core_test_support::responses; use core_test_support::skip_if_no_network; use pretty_assertions::assert_eq; @@ -288,7 +289,8 @@ async fn thread_resume_running_thread_uses_cached_instruction_sources() -> Resul .. } = to_response::(start_resp)?; let project_agents = AbsolutePathBuf::try_from(project_agents)?; - assert_eq!(instruction_sources, vec![project_agents.clone()]); + let project_agents_source = LegacyAppPathString::from_abs_path(&project_agents); + assert_eq!(instruction_sources, vec![project_agents_source.clone()]); let turn_id = mcp .send_turn_start_request(TurnStartParams { @@ -330,7 +332,7 @@ async fn thread_resume_running_thread_uses_cached_instruction_sources() -> Resul .. } = to_response::(resume_resp)?; - assert_eq!(instruction_sources, vec![project_agents]); + assert_eq!(instruction_sources, vec![project_agents_source]); Ok(()) } diff --git a/codex-rs/app-server/tests/suite/v2/thread_start.rs b/codex-rs/app-server/tests/suite/v2/thread_start.rs index c0946ba679..dcfeb4fa94 100644 --- a/codex-rs/app-server/tests/suite/v2/thread_start.rs +++ b/codex-rs/app-server/tests/suite/v2/thread_start.rs @@ -381,7 +381,7 @@ async fn thread_start_response_includes_loaded_instruction_sources() -> Result<( let instruction_sources = instruction_sources .into_iter() - .map(normalize_path_for_comparison) + .map(|path| normalize_path_for_comparison(path.as_str())) .collect::>(); let expected_instruction_sources = vec![ std::fs::canonicalize(global_agents_path)?, @@ -428,7 +428,7 @@ async fn thread_start_response_excludes_empty_project_instruction_source() -> Re let instruction_sources = instruction_sources .into_iter() - .map(normalize_path_for_comparison) + .map(|path| normalize_path_for_comparison(path.as_str())) .collect::>(); let expected_instruction_sources = vec![normalize_path_for_comparison(std::fs::canonicalize( global_agents_path, @@ -474,7 +474,7 @@ async fn thread_start_without_selected_environment_includes_only_global_instruct assert_eq!( instruction_sources .into_iter() - .map(normalize_path_for_comparison) + .map(|path| normalize_path_for_comparison(path.as_str())) .collect::>(), vec![normalize_path_for_comparison(std::fs::canonicalize( global_agents_path, diff --git a/codex-rs/core/src/agents_md.rs b/codex-rs/core/src/agents_md.rs index 861e7b4ac7..2465cb6d55 100644 --- a/codex-rs/core/src/agents_md.rs +++ b/codex-rs/core/src/agents_md.rs @@ -53,16 +53,11 @@ pub(crate) async fn load_project_instructions( let mut loaded = LoadedAgentsMd::from_user_instructions(user_instructions); for turn_environment in &environments.turn_environments { let filesystem = turn_environment.environment.get_filesystem(); - // TODO(anp): Migrate AGENTS.md discovery to PathUri so instructions can be loaded from - // environment-native foreign working directories. - let Ok(cwd) = turn_environment.cwd().to_abs_path() else { - continue; - }; match read_agents_md( config, filesystem.as_ref(), &turn_environment.environment_id, - &cwd, + turn_environment.cwd(), ) .await { @@ -97,7 +92,7 @@ async fn read_agents_md( config: &Config, fs: &dyn ExecutorFileSystem, environment_id: &str, - cwd: &AbsolutePathBuf, + cwd: &PathUri, ) -> io::Result> { let max_total = config.project_doc_max_bytes; @@ -118,15 +113,14 @@ async fn read_agents_md( break; } - let path_uri = PathUri::from_abs_path(&p); - match fs.get_metadata(&path_uri, /*sandbox*/ None).await { + match fs.get_metadata(&p, /*sandbox*/ None).await { Ok(metadata) if !metadata.is_file => continue, Ok(_) => {} Err(err) if err.kind() == io::ErrorKind::NotFound => continue, Err(err) => return Err(err), } - let mut data = match fs.read_file(&path_uri, /*sandbox*/ None).await { + let mut data = match fs.read_file(&p, /*sandbox*/ None).await { Ok(data) => data, Err(err) if err.kind() == io::ErrorKind::NotFound => continue, Err(err) => return Err(err), @@ -139,7 +133,7 @@ async fn read_agents_md( if size > remaining { tracing::warn!( "Project doc `{}` exceeds remaining budget ({} bytes) - truncating.", - p.display(), + p.inferred_native_path_string(), remaining, ); } @@ -169,9 +163,9 @@ async fn read_agents_md( /// directory, inclusive. Symlinks are allowed. async fn agents_md_paths( config: &Config, - cwd: &AbsolutePathBuf, + cwd: &PathUri, fs: &dyn ExecutorFileSystem, -) -> io::Result> { +) -> io::Result> { let dir = cwd.clone(); let mut merged = TomlValue::Table(toml::map::Map::new()); @@ -194,18 +188,18 @@ async fn agents_md_paths( }; let mut project_root = None; if !project_root_markers.is_empty() { - for ancestor in dir.ancestors() { + for current in dir.ancestors() { for marker in &project_root_markers { - let marker_path = ancestor.join(marker); - let marker_path_uri = PathUri::from_abs_path(&marker_path); - let marker_exists = match fs.get_metadata(&marker_path_uri, /*sandbox*/ None).await - { + let marker_path = current + .join(marker) + .map_err(|err| io::Error::new(io::ErrorKind::InvalidInput, err))?; + let marker_exists = match fs.get_metadata(&marker_path, /*sandbox*/ None).await { Ok(_) => true, Err(err) if err.kind() == io::ErrorKind::NotFound => false, Err(err) => return Err(err), }; if marker_exists { - project_root = Some(ancestor.clone()); + project_root = Some(current.clone()); break; } } @@ -215,7 +209,7 @@ async fn agents_md_paths( } } - let search_dirs: Vec = if let Some(root) = project_root { + let search_dirs: Vec = if let Some(root) = project_root { let mut dirs = Vec::new(); let mut cursor = dir.clone(); loop { @@ -234,13 +228,14 @@ async fn agents_md_paths( vec![dir] }; - let mut found: Vec = Vec::new(); + let mut found: Vec = Vec::new(); let candidate_filenames = candidate_filenames(config); for d in search_dirs { for name in &candidate_filenames { - let candidate = d.join(name); - let candidate_uri = PathUri::from_abs_path(&candidate); - match fs.get_metadata(&candidate_uri, /*sandbox*/ None).await { + let candidate = d + .join(name) + .map_err(|err| io::Error::new(io::ErrorKind::InvalidInput, err))?; + match fs.get_metadata(&candidate, /*sandbox*/ None).await { Ok(md) if md.is_file => { found.push(candidate); break; @@ -371,7 +366,7 @@ impl LoadedAgentsMd { fn environment_labeled_text(&self) -> String { let mut output = String::new(); let mut has_previous = false; - let mut previous_environment: Option<(&str, &AbsolutePathBuf)> = None; + let mut previous_environment: Option<(&str, &PathUri)> = None; if let Some(instructions) = &self.user_instructions { output.push_str(&instructions.text); has_previous = true; @@ -394,7 +389,7 @@ impl LoadedAgentsMd { output.push_str(&format!( "for `{}` with root {}\n\n", environment_id, - cwd.display() + cwd.inferred_native_path_string() )); } output.push_str(&entry.contents); @@ -421,7 +416,7 @@ impl LoadedAgentsMd { None } else { self.single_project_cwd() - .map(|cwd| cwd.to_string_lossy().into_owned()) + .map(PathUri::inferred_native_path_string) }; ContextUserInstructions { directory, @@ -436,10 +431,10 @@ impl LoadedAgentsMd { } /// Returns the AGENTS.md files that supplied instruction entries. - pub fn sources(&self) -> impl Iterator { + pub fn sources(&self) -> impl Iterator + '_ { self.user_instructions .iter() - .map(|instructions| &instructions.source) + .map(|instructions| PathUri::from_abs_path(&instructions.source)) .chain( self.entries .iter() @@ -463,7 +458,7 @@ impl LoadedAgentsMd { }) } - fn single_project_cwd(&self) -> Option<&AbsolutePathBuf> { + fn single_project_cwd(&self) -> Option<&PathUri> { self.entries .iter() .find_map(|entry| match &entry.provenance { @@ -488,9 +483,9 @@ enum InstructionProvenance { /// Workspace instructions discovered from project AGENTS.md files. Project { /// Exact AGENTS.md file, distinct from the environment's selected cwd. - source_path: AbsolutePathBuf, + source_path: PathUri, environment_id: String, - cwd: AbsolutePathBuf, + cwd: PathUri, }, /// Instructions without a file source, including internally defined guidance. @@ -498,9 +493,9 @@ enum InstructionProvenance { } impl InstructionProvenance { - fn path(&self) -> Option<&AbsolutePathBuf> { + fn path(&self) -> Option { match self { - Self::Project { source_path, .. } => Some(source_path), + Self::Project { source_path, .. } => Some(source_path.clone()), Self::Internal => None, } } diff --git a/codex-rs/core/src/agents_md_tests.rs b/codex-rs/core/src/agents_md_tests.rs index 42604629a0..ab027112a9 100644 --- a/codex-rs/core/src/agents_md_tests.rs +++ b/codex-rs/core/src/agents_md_tests.rs @@ -250,8 +250,13 @@ async fn load_agents_md(config: &TestConfig) -> Option { .await } -async fn agents_md_paths(config: &TestConfig) -> std::io::Result> { - super::agents_md_paths(&config.config, &config.cwd, LOCAL_FS.as_ref()).await +async fn agents_md_paths(config: &TestConfig) -> std::io::Result> { + super::agents_md_paths( + &config.config, + &PathUri::from_abs_path(&config.cwd), + LOCAL_FS.as_ref(), + ) + .await } fn resolved_local_environments( @@ -277,12 +282,101 @@ fn resolved_local_environments( fn project_provenance(path: AbsolutePathBuf, cwd: AbsolutePathBuf) -> InstructionProvenance { InstructionProvenance::Project { - source_path: path, + source_path: PathUri::from_abs_path(&path), environment_id: "local".to_string(), - cwd, + cwd: PathUri::from_abs_path(&cwd), } } +#[test] +fn foreign_agents_md_uses_environment_native_paths() { + let (cwd, rendered_cwd) = if cfg!(windows) { + ( + PathUri::parse("file:///codex%20runtime").expect("POSIX cwd URI"), + "/codex runtime", + ) + } else { + ( + PathUri::parse("file:///C:/codex%20runtime").expect("Windows cwd URI"), + r"C:\codex runtime", + ) + }; + let source_path = cwd.join("AGENTS.md").expect("AGENTS.md URI"); + let loaded = LoadedAgentsMd { + user_instructions: None, + entries: vec![InstructionEntry { + contents: "remote instructions".to_string(), + provenance: InstructionProvenance::Project { + source_path: source_path.clone(), + environment_id: "remote".to_string(), + cwd, + }, + }], + }; + + assert_eq!( + loaded.render(), + format!( + "# AGENTS.md instructions for {rendered_cwd} + + +remote instructions +" + ) + ); + assert_eq!(loaded.sources().collect::>(), vec![source_path]); +} + +#[test] +fn multi_environment_agents_md_renders_mixed_path_conventions() { + let posix_cwd = PathUri::parse("file:///srv/project").expect("POSIX cwd URI"); + let windows_cwd = PathUri::parse("file:///C:/workspace").expect("Windows cwd URI"); + let posix_source = posix_cwd.join("AGENTS.md").expect("POSIX AGENTS.md URI"); + let windows_source = windows_cwd + .join("AGENTS.md") + .expect("Windows AGENTS.md URI"); + let loaded = LoadedAgentsMd { + user_instructions: None, + entries: vec![ + InstructionEntry { + contents: "POSIX instructions".to_string(), + provenance: InstructionProvenance::Project { + source_path: posix_source.clone(), + environment_id: "posix".to_string(), + cwd: posix_cwd, + }, + }, + InstructionEntry { + contents: "Windows instructions".to_string(), + provenance: InstructionProvenance::Project { + source_path: windows_source.clone(), + environment_id: "windows".to_string(), + cwd: windows_cwd, + }, + }, + ], + }; + + assert_eq!( + loaded.render(), + r#"# AGENTS.md instructions + + +for `posix` with root /srv/project + +POSIX instructions + +for `windows` with root C:\workspace + +Windows instructions +"# + ); + assert_eq!( + loaded.sources().collect::>(), + vec![posix_source, windows_source] + ); +} + /// Helper that returns a `Config` pointing at `root` and using `limit` as /// the maximum number of bytes to embed from AGENTS.md. The caller can /// optionally specify a custom `instructions` string – when `None` the @@ -508,7 +602,7 @@ async fn read_agents_md_propagates_metadata_errors() { }; let cwd = config.cwd.clone(); - let err = read_agents_md(&config.config, &fs, "local", &cwd) + let err = read_agents_md(&config.config, &fs, "local", &PathUri::from_abs_path(&cwd)) .await .expect_err("metadata error"); @@ -526,7 +620,7 @@ async fn read_agents_md_propagates_read_errors() { }; let cwd = config.cwd.clone(); - let err = read_agents_md(&config.config, &fs, "local", &cwd) + let err = read_agents_md(&config.config, &fs, "local", &PathUri::from_abs_path(&cwd)) .await .expect_err("read error"); @@ -544,7 +638,7 @@ async fn read_agents_md_ignores_files_removed_after_discovery() { }; let cwd = config.cwd.clone(); - let loaded = read_agents_md(&config.config, &fs, "local", &cwd) + let loaded = read_agents_md(&config.config, &fs, "local", &PathUri::from_abs_path(&cwd)) .await .expect("removed file is recoverable"); @@ -659,17 +753,18 @@ secondary doc"#, ); assert_eq!(loaded.render(), expected_fragment); assert_eq!( - loaded.sources().cloned().collect::>(), + loaded.sources().collect::>(), vec![ - config - .user_instructions - .as_ref() - .expect("global instructions") - .source - .clone(), - primary.path().join("AGENTS.md").abs(), - primary_nested.join("AGENTS.md").abs(), - secondary.path().join("AGENTS.md").abs(), + PathUri::from_abs_path( + &config + .user_instructions + .as_ref() + .expect("global instructions") + .source, + ), + PathUri::from_abs_path(&primary.path().join("AGENTS.md").abs()), + PathUri::from_abs_path(&primary_nested.join("AGENTS.md").abs()), + PathUri::from_abs_path(&secondary.path().join("AGENTS.md").abs()), ] ); } @@ -898,7 +993,10 @@ async fn concatenates_root_and_cwd_docs() { assert_eq!(loaded.text(), "root doc\n\ncrate doc"); assert_eq!( loaded.sources().collect::>(), - vec![&root_agents, &crate_agents] + vec![ + PathUri::from_abs_path(&root_agents), + PathUri::from_abs_path(&crate_agents), + ] ); } @@ -925,8 +1023,8 @@ async fn project_root_markers_are_honored_for_agents_discovery() { let expected_parent = root.path().join("AGENTS.md").abs(); let expected_child = cfg.cwd.join("AGENTS.md"); assert_eq!(discovery.len(), 2); - assert_eq!(discovery[0], expected_parent); - assert_eq!(discovery[1], expected_child); + assert_eq!(discovery[0], PathUri::from_abs_path(&expected_parent)); + assert_eq!(discovery[1], PathUri::from_abs_path(&expected_child)); let res = get_user_instructions(&cfg).await.expect("doc expected"); assert_eq!(res, "parent doc\n\nchild doc"); @@ -971,8 +1069,8 @@ async fn project_layers_do_not_override_project_root_markers() { assert_eq!( discovery, vec![ - root.path().join("AGENTS.md").abs(), - config.cwd.join("AGENTS.md"), + PathUri::from_abs_path(&root.path().join("AGENTS.md").abs()), + PathUri::from_abs_path(&config.cwd.join("AGENTS.md")), ] ); } @@ -991,7 +1089,10 @@ async fn agents_md_paths_preserve_symlinked_cwd() { cfg.cwd = linked_cwd.abs(); let discovery = agents_md_paths(&cfg).await.expect("discover paths"); - assert_eq!(discovery, vec![cfg.cwd.join("AGENTS.md")]); + assert_eq!( + discovery, + vec![PathUri::from_abs_path(&cfg.cwd.join("AGENTS.md"))] + ); let res = get_user_instructions(&cfg).await.expect("doc expected"); assert_eq!(res, "project doc"); @@ -1050,7 +1151,10 @@ async fn instruction_sources_include_global_before_agents_md_docs() { assert_eq!(loaded.user_instructions(), cfg.user_instructions.as_ref()); assert_eq!( loaded.sources().collect::>(), - vec![&global_agents, &project_agents] + vec![ + PathUri::from_abs_path(&global_agents), + PathUri::from_abs_path(&project_agents), + ] ); assert_eq!( loaded.text(), @@ -1091,7 +1195,10 @@ async fn child_agents_message_after_project_docs_is_not_an_instruction_source() assert_eq!(loaded, expected); assert_eq!( loaded.sources().collect::>(), - vec![&global_agents, &project_agents] + vec![ + PathUri::from_abs_path(&global_agents), + PathUri::from_abs_path(&project_agents), + ] ); assert_eq!( loaded.text(), @@ -1117,8 +1224,8 @@ async fn agents_local_md_preferred() { let discovery = agents_md_paths(&cfg).await.expect("discover paths"); assert_eq!(discovery.len(), 1); assert_eq!( - discovery[0].file_name().unwrap().to_string_lossy(), - LOCAL_AGENTS_MD_FILENAME + discovery[0].basename().as_deref(), + Some(LOCAL_AGENTS_MD_FILENAME) ); } @@ -1166,12 +1273,9 @@ async fn agents_md_preferred_over_fallbacks() { let discovery = agents_md_paths(&cfg).await.expect("discover paths"); assert_eq!(discovery.len(), 1); - assert!( - discovery[0] - .file_name() - .unwrap() - .to_string_lossy() - .eq(DEFAULT_AGENTS_MD_FILENAME) + assert_eq!( + discovery[0].basename().as_deref(), + Some(DEFAULT_AGENTS_MD_FILENAME) ); } @@ -1186,7 +1290,7 @@ async fn agents_md_directory_is_ignored() { assert_eq!(res, None); let discovery = agents_md_paths(&cfg).await.expect("discover paths"); - assert_eq!(discovery, Vec::::new()); + assert_eq!(discovery, Vec::::new()); } #[cfg(unix)] @@ -1209,7 +1313,7 @@ async fn agents_md_special_file_is_ignored() { assert_eq!(res, None); let discovery = agents_md_paths(&cfg).await.expect("discover paths"); - assert_eq!(discovery, Vec::::new()); + assert_eq!(discovery, Vec::::new()); } #[tokio::test] @@ -1228,11 +1332,8 @@ async fn override_directory_falls_back_to_agents_md_file() { let discovery = agents_md_paths(&cfg).await.expect("discover paths"); assert_eq!(discovery.len(), 1); assert_eq!( - discovery[0] - .file_name() - .expect("file name") - .to_string_lossy(), - DEFAULT_AGENTS_MD_FILENAME + discovery[0].basename().as_deref(), + Some(DEFAULT_AGENTS_MD_FILENAME) ); } diff --git a/codex-rs/core/src/codex_thread.rs b/codex-rs/core/src/codex_thread.rs index 8c44857297..ab46d8b3ab 100644 --- a/codex-rs/core/src/codex_thread.rs +++ b/codex-rs/core/src/codex_thread.rs @@ -42,6 +42,7 @@ use codex_thread_store::ThreadMetadataPatch; use codex_thread_store::ThreadStoreError; use codex_thread_store::ThreadStoreResult; use codex_utils_absolute_path::AbsolutePathBuf; +use codex_utils_path_uri::LegacyAppPathString; use codex_utils_path_uri::PathUri; use rmcp::model::ReadResourceRequestParams; use std::collections::BTreeMap; @@ -564,10 +565,19 @@ impl CodexThread { } /// Returns the files that supplied the thread's loaded model instructions. - pub async fn instruction_sources(&self) -> Vec { + pub async fn instruction_sources(&self) -> Vec { self.codex.instruction_sources().await } + /// Returns loaded instruction sources rendered as legacy app-server path strings. + pub async fn legacy_instruction_sources(&self) -> Vec { + self.instruction_sources() + .await + .into_iter() + .map(Into::into) + .collect() + } + pub async fn config(&self) -> Arc { self.codex.session.get_config().await } diff --git a/codex-rs/core/src/session/mod.rs b/codex-rs/core/src/session/mod.rs index b55c4b11d7..5d579f14a8 100644 --- a/codex-rs/core/src/session/mod.rs +++ b/codex-rs/core/src/session/mod.rs @@ -830,15 +830,13 @@ impl Codex { state.session_configuration.thread_config_snapshot() } - pub(crate) async fn instruction_sources(&self) -> Vec { + pub(crate) async fn instruction_sources(&self) -> Vec { let state = self.session.state.lock().await; state .session_configuration .loaded_agents_md .as_ref() - .map_or_else(Vec::new, |instructions| { - instructions.sources().cloned().collect() - }) + .map_or_else(Vec::new, |instructions| instructions.sources().collect()) } pub(crate) async fn thread_environment_selections(&self) -> Vec { diff --git a/codex-rs/core/tests/common/test_codex.rs b/codex-rs/core/tests/common/test_codex.rs index 42b9bf87ca..e0e7b20287 100644 --- a/codex-rs/core/tests/common/test_codex.rs +++ b/codex-rs/core/tests/common/test_codex.rs @@ -58,6 +58,7 @@ use tempfile::TempDir; use wiremock::MockServer; use crate::TempDirExt; +use crate::TestEnvironment; use crate::get_remote_test_env; use crate::load_default_config_for_test; use crate::load_default_config_for_test_with_cloud_config_bundle; @@ -179,7 +180,20 @@ pub async fn test_env() -> Result { /*sandbox*/ None, ) .await?; - let cwd = cwd_uri.to_abs_path()?; + let cwd = if remote_env == TestEnvironment::WineExec { + // TODO(anp): Convert `Config::cwd` to `LegacyAppPathString` and remove this + // compatibility projection. + // `Config::cwd` still requires `AbsolutePathBuf`. Preserve the test harness's + // Linux-absolute `/C:/...` compatibility spelling so converting it back to a + // `PathUri` recovers the remote Windows convention. Production conversions stay + // strict: `PathUri::to_abs_path` intentionally rejects foreign paths. + let path = cwd_uri.to_url().to_file_path().map_err(|()| { + anyhow!("remote test cwd URI cannot be projected onto the host: {cwd_uri}") + })?; + AbsolutePathBuf::try_from(path)? + } else { + cwd_uri.to_abs_path()? + }; Ok(TestEnv { environment, exec_server_url: Some(websocket_url), diff --git a/codex-rs/core/tests/remote_env_windows/BUILD.bazel b/codex-rs/core/tests/remote_env_windows/BUILD.bazel index 5e213ac14b..9b5ce52bd9 100644 --- a/codex-rs/core/tests/remote_env_windows/BUILD.bazel +++ b/codex-rs/core/tests/remote_env_windows/BUILD.bazel @@ -22,7 +22,6 @@ wine_rust_test( "//codex-rs/protocol", "//codex-rs/utils/path-uri", "@crates//:anyhow", - "@crates//:base64", "@crates//:pretty_assertions", "@crates//:serde_json", "@crates//:tempfile", diff --git a/codex-rs/core/tests/remote_env_windows/remote_env_windows_test.rs b/codex-rs/core/tests/remote_env_windows/remote_env_windows_test.rs index cff8ded739..04f8914982 100644 --- a/codex-rs/core/tests/remote_env_windows/remote_env_windows_test.rs +++ b/codex-rs/core/tests/remote_env_windows/remote_env_windows_test.rs @@ -36,11 +36,13 @@ use core_test_support::test_codex::test_codex; use core_test_support::test_codex::turn_permission_fields; use core_test_support::wait_for_event; use codex_utils_path_uri::LegacyAppPathString; +use codex_utils_path_uri::PathConvention; use codex_utils_path_uri::PathUri; use pretty_assertions::assert_eq; use serde_json::Value; use serde_json::json; use std::collections::BTreeMap; +use std::fs; use tempfile::TempDir; use tokio::time::timeout; use wine_exec_server_test_support::WineExecServer; @@ -57,7 +59,7 @@ async fn windows_exec_server_runs_with_native_shell_and_cwd() -> Result<()> { const VERIFY_COMMAND: &str = r#"$path = Join-Path (Get-Location) 'codex-apply-patch-smoke.txt'; if (-not (Test-Path $path)) { exit 1 }; if ([IO.File]::ReadAllText($path) -ne "patched through unified exec`n") { exit 2 }; Remove-Item $path; Write-Output 'PATCH_VERIFIED'"#; WineExecServer - .scope(|exec_server_url| async move { + .scope(|exec_server_url, _wine_prefix| async move { let server = start_mock_server().await; let arguments = serde_json::to_string(&json!({ "cmd": COMMAND, @@ -245,8 +247,17 @@ async fn windows_exec_server_runs_with_native_shell_and_cwd() -> Result<()> { #[tokio::test(flavor = "multi_thread", worker_threads = 2)] async fn app_server_starts_thread_with_windows_environment_native_cwd() -> Result<()> { + const AGENTS_INSTRUCTIONS: &str = "remote Windows workspace instructions"; + const NATIVE_CWD: &str = r"C:\windows"; + WineExecServer - .scope(|exec_server_url| async move { + .scope(|exec_server_url, wine_prefix| async move { + let agents_path = PathUri::parse("file:///C:/windows/AGENTS.md")?; + fs::write( + wine_prefix.join("drive_c").join("windows").join("AGENTS.md"), + AGENTS_INSTRUCTIONS, + )?; + let codex_home = TempDir::new()?; let server = create_mock_responses_server_repeating_assistant("done").await; write_mock_responses_config_toml( @@ -272,7 +283,7 @@ async fn app_server_starts_thread_with_windows_environment_native_cwd() -> Resul .send_thread_start_request(ThreadStartParams { environments: Some(vec![TurnEnvironmentParams { environment_id: REMOTE_ENVIRONMENT_ID.to_string(), - cwd: serde_json::from_value::(json!(r"C:\windows"))?, + cwd: serde_json::from_value::(json!(NATIVE_CWD))?, }]), ..Default::default() }) @@ -289,8 +300,13 @@ async fn app_server_starts_thread_with_windows_environment_native_cwd() -> Resul assert_eq!(response.cwd, host_cwd); // TODO(anp): Derive runtime workspace roots from the selected remote environment. assert_eq!(response.runtime_workspace_roots, vec![host_cwd]); - // TODO(anp): Discover and report instruction sources from the remote filesystem. - assert_eq!(response.instruction_sources, Vec::new()); + assert_eq!( + response.instruction_sources, + vec![LegacyAppPathString::from_path_uri( + &agents_path, + PathConvention::Windows, + )?] + ); // TODO(anp): Report the implicit built-in permission profile instead of None. assert_eq!(response.active_permission_profile, None); @@ -326,6 +342,17 @@ async fn app_server_starts_thread_with_windows_environment_native_cwd() -> Resul .find(|request| request.url.path().ends_with("/responses")) .context("turn should send a Responses request")?; let body = first_request.body_json::()?; + let remote_instructions = body["input"] + .as_array() + .into_iter() + .flatten() + .filter(|item| item.get("role").and_then(Value::as_str) == Some("user")) + .filter_map(|item| item.get("content").and_then(Value::as_array)) + .flatten() + .filter_map(|content| content.get("text").and_then(Value::as_str)) + .find(|text| text.contains(AGENTS_INSTRUCTIONS)) + .context("remote workspace instructions should be model visible")?; + assert!(remote_instructions.contains(r"# AGENTS.md instructions for C:\windows")); let environment_context = body["input"] .as_array() .into_iter() diff --git a/codex-rs/core/tests/suite/agents_md.rs b/codex-rs/core/tests/suite/agents_md.rs index 5b79e9f1e6..e3a08447c5 100644 --- a/codex-rs/core/tests/suite/agents_md.rs +++ b/codex-rs/core/tests/suite/agents_md.rs @@ -25,7 +25,6 @@ use core_test_support::responses::mount_sse_once; use core_test_support::responses::sse; use core_test_support::responses::start_mock_server; use core_test_support::skip_if_no_network; -use core_test_support::skip_if_wine_exec; use core_test_support::test_codex::RecordingUserInstructionsProvider; use core_test_support::test_codex::TestCodexBuilder; use core_test_support::test_codex::test_codex; @@ -88,7 +87,7 @@ fn instruction_fragments(request: &responses::ResponsesRequest) -> Vec { } fn expected_instruction_fragment(cwd: &AbsolutePathBuf, contents: &str) -> String { - let cwd = cwd.as_path().display(); + let cwd = PathUri::from_abs_path(cwd).inferred_native_path_string(); format!("# AGENTS.md instructions for {cwd}\n\n\n{contents}\n") } @@ -138,8 +137,6 @@ fn request_body_contains(request: &wiremock::Request, text: &str) -> bool { #[tokio::test(flavor = "multi_thread", worker_threads = 2)] async fn agents_override_is_preferred_over_agents_md() -> Result<()> { - // TODO(anp): Remove after instruction-source helpers use target-native paths. - skip_if_wine_exec!(Ok(()), "requires native cross-OS instruction-source paths"); let instructions = agents_instructions(test_codex().with_workspace_setup(|cwd, fs| async move { let agents_md = cwd.join("AGENTS.md"); @@ -172,8 +169,6 @@ async fn agents_override_is_preferred_over_agents_md() -> Result<()> { #[tokio::test(flavor = "multi_thread", worker_threads = 2)] async fn configured_fallback_is_used_when_agents_candidate_is_directory() -> Result<()> { - // TODO(anp): Remove after instruction-source helpers use target-native paths. - skip_if_wine_exec!(Ok(()), "requires native cross-OS instruction-source paths"); let instructions = agents_instructions( test_codex() .with_config(|config| { @@ -334,8 +329,8 @@ async fn symlinked_cwd_uses_logical_parent_for_agents_discovery() -> Result<()> assert_eq!( test.codex.instruction_sources().await, vec![ - logical_root.join("AGENTS.md"), - test.config.cwd.join("AGENTS.md") + PathUri::from_abs_path(&logical_root.join("AGENTS.md")), + PathUri::from_abs_path(&test.config.cwd.join("AGENTS.md")) ] ); @@ -355,8 +350,6 @@ async fn symlinked_cwd_uses_logical_parent_for_agents_discovery() -> Result<()> #[tokio::test(flavor = "multi_thread", worker_threads = 2)] async fn selected_environment_sources_match_model_visible_instructions() -> Result<()> { - // TODO(anp): Remove after instruction-source helpers use target-native paths. - skip_if_wine_exec!(Ok(()), "requires native cross-OS instruction-source paths"); let server = start_mock_server().await; let resp_mock = mount_sse_once( &server, @@ -385,7 +378,10 @@ async fn selected_environment_sources_match_model_visible_instructions() -> Resu assert_eq!( test.codex.instruction_sources().await, - vec![global_agents, project_agents] + vec![ + PathUri::from_abs_path(&global_agents), + PathUri::from_abs_path(&project_agents), + ] ); test.submit_turn("hello").await?; @@ -454,7 +450,7 @@ async fn loads_user_instructions_without_a_primary_environment() -> Result<()> { assert_eq!(provider.load_count(), 2); assert_eq!( no_environment_thread.thread.instruction_sources().await, - vec![global_source] + vec![PathUri::from_abs_path(&global_source)] ); no_environment_thread @@ -485,8 +481,6 @@ async fn loads_user_instructions_without_a_primary_environment() -> Result<()> { #[tokio::test(flavor = "multi_thread", worker_threads = 2)] async fn fresh_thread_composes_global_before_project_and_reports_sources() -> Result<()> { - // TODO(anp): Remove after instruction-source helpers use target-native paths. - skip_if_wine_exec!(Ok(()), "requires native cross-OS instruction-source paths"); // Set up one global source, one project source, and two ordinary model turns. let server = responses::start_mock_server().await; let response_mock = responses::mount_sse_sequence( @@ -521,7 +515,10 @@ async fn fresh_thread_composes_global_before_project_and_reports_sources() -> Re }); let test = builder.build_with_remote_env(&server).await?; let project_source = test.config.cwd.join(GLOBAL_AGENTS_FILENAME); - let creation_sources = vec![global_source.clone(), project_source.clone()]; + let creation_sources = vec![ + PathUri::from_abs_path(&global_source), + PathUri::from_abs_path(&project_source), + ]; // Confirm the thread records both creation-time sources in composition order. assert_eq!(test.codex.instruction_sources().await, creation_sources); @@ -597,8 +594,6 @@ async fn fresh_thread_composes_global_before_project_and_reports_sources() -> Re #[tokio::test(flavor = "multi_thread", worker_threads = 2)] async fn multi_environment_thread_loads_every_project_and_keeps_creation_snapshot() -> Result<()> { - // TODO(anp): Remove after instruction-source helpers use target-native paths. - skip_if_wine_exec!(Ok(()), "requires native cross-OS instruction-source paths"); skip_if_no_network!(Ok(())); let Some(_remote_env) = get_remote_test_env() else { return Ok(()); @@ -672,9 +667,9 @@ async fn multi_environment_thread_loads_every_project_and_keeps_creation_snapsho assert_eq!( thread.thread.instruction_sources().await, vec![ - global_source.clone(), - remote_source.clone(), - local_source.clone().try_into()?, + PathUri::from_abs_path(&global_source), + PathUri::from_abs_path(&remote_source), + PathUri::from_path(&local_source)?, ] ); @@ -700,7 +695,7 @@ async fn multi_environment_thread_loads_every_project_and_keeps_creation_snapsho let contents = format!( "{GLOBAL_INSTRUCTIONS}\n\nfor `{REMOTE_ENVIRONMENT_ID}` with root {}\n\nremote project instructions\n\nfor `{LOCAL_ENVIRONMENT_ID}` with root {}\n\nlocal project instructions", - test.config.cwd.display(), + PathUri::from_abs_path(&test.config.cwd).inferred_native_path_string(), local_root.path().display(), ); let expected = @@ -712,7 +707,11 @@ async fn multi_environment_thread_loads_every_project_and_keeps_creation_snapsho assert_eq!(provider.load_count(), 2); assert_eq!( thread.thread.instruction_sources().await, - vec![global_source, remote_source, local_source.try_into()?] + vec![ + PathUri::from_abs_path(&global_source), + PathUri::from_abs_path(&remote_source), + PathUri::from_path(&local_source)?, + ] ); Ok(()) @@ -741,7 +740,10 @@ async fn invalid_utf8_global_instructions_are_lossy() -> Result<()> { test.submit_turn("inspect lossy global instructions") .await?; - assert_eq!(test.codex.instruction_sources().await, vec![source.clone()]); + assert_eq!( + test.codex.instruction_sources().await, + vec![PathUri::from_abs_path(&source)] + ); let expected_fragment = expected_provider_only_instruction_fragment("global\u{FFFD}instructions"); assert_single_instruction_fragment(&response_mock.single_request(), &expected_fragment); @@ -784,7 +786,7 @@ async fn cold_resume_replays_rendered_instructions_but_reports_current_config_so // Assert the pre-resume thread reports the source used to create its snapshot. assert_eq!( initial.codex.instruction_sources().await, - vec![old_source.clone()], + vec![PathUri::from_abs_path(&old_source)], "initial thread reports the creation-time global source" ); initial.submit_turn("persist instructions").await?; @@ -814,7 +816,7 @@ async fn cold_resume_replays_rendered_instructions_but_reports_current_config_so // Assert the API reports the new source while model history replays the old structured prefix. assert_eq!( resumed.codex.instruction_sources().await, - vec![new_source], + vec![PathUri::from_abs_path(&new_source)], "resume reports sources from the newly loaded config" ); @@ -868,7 +870,7 @@ async fn fork_replays_rendered_instructions_from_shared_history() -> Result<()> // Assert the parent reports the source used to create its snapshot. assert_eq!( parent.codex.instruction_sources().await, - vec![source.clone()], + vec![PathUri::from_abs_path(&source)], "parent reports the creation-time global source" ); parent.submit_turn("persist instructions").await?; @@ -903,7 +905,7 @@ async fn fork_replays_rendered_instructions_from_shared_history() -> Result<()> // Assert the fork reports the new source before issuing its first turn. assert_eq!( forked.thread.instruction_sources().await, - vec![new_source], + vec![PathUri::from_abs_path(&new_source)], "fork config should reflect the newly loaded global source" ); @@ -1033,7 +1035,7 @@ async fn run_subagent_global_instruction_case(fork_context: bool) -> Result<()> // Assert the parent reports the creation-time source before spawning. assert_eq!( test.codex.instruction_sources().await, - vec![source.clone()], + vec![PathUri::from_abs_path(&source)], "parent reports the creation-time global source before spawning" ); test.submit_turn(SPAWN_SEED_PROMPT).await?; @@ -1076,12 +1078,12 @@ async fn run_subagent_global_instruction_case(fork_context: bool) -> Result<()> assert_single_instruction_fragment(&child_request, &expected_fragment); assert_eq!( test.codex.instruction_sources().await, - vec![source.clone()], + vec![PathUri::from_abs_path(&source)], "running parent retains the creation-time global source after spawning" ); assert_eq!( child_thread.instruction_sources().await, - vec![source], + vec![PathUri::from_abs_path(&source)], "subagent reports the parent's creation-time source" ); if fork_context { diff --git a/codex-rs/core/tests/suite/compact.rs b/codex-rs/core/tests/suite/compact.rs index 3b709d54a2..92ec66e161 100644 --- a/codex-rs/core/tests/suite/compact.rs +++ b/codex-rs/core/tests/suite/compact.rs @@ -25,6 +25,7 @@ use codex_protocol::protocol::RolloutLine; use codex_protocol::protocol::WarningEvent; use codex_protocol::user_input::UserInput; use codex_utils_absolute_path::AbsolutePathBuf; +use codex_utils_path_uri::PathUri; use core_test_support::PathBufExt; use core_test_support::context_snapshot; use core_test_support::context_snapshot::ContextSnapshotOptions; @@ -4593,7 +4594,7 @@ async fn manual_compaction_keeps_the_creation_time_global_instructions() -> Resu // Assert the pre-compaction source list points at the creation-time file. assert_eq!( test.codex.instruction_sources().await, - vec![source.clone()], + vec![PathUri::from_abs_path(&source)], "thread reports the creation-time global source before compaction" ); @@ -4623,7 +4624,7 @@ async fn manual_compaction_keeps_the_creation_time_global_instructions() -> Resu assert_single_instruction_fragment(&requests[2], &expected_fragment); assert_eq!( test.codex.instruction_sources().await, - vec![source], + vec![PathUri::from_abs_path(&source)], "thread retains the creation-time global source after compaction" ); @@ -4673,7 +4674,7 @@ async fn mid_turn_compaction_keeps_the_creation_time_global_instructions() -> Re // Assert the pre-compaction source list points at the creation-time file. assert_eq!( test.codex.instruction_sources().await, - vec![source.clone()], + vec![PathUri::from_abs_path(&source)], "thread reports the creation-time global source before mid-turn compaction" ); @@ -4695,7 +4696,7 @@ async fn mid_turn_compaction_keeps_the_creation_time_global_instructions() -> Re assert_single_instruction_fragment(&requests[2], &expected_fragment); assert_eq!( test.codex.instruction_sources().await, - vec![source], + vec![PathUri::from_abs_path(&source)], "thread retains the creation-time global source after mid-turn compaction" ); @@ -4780,7 +4781,7 @@ async fn remote_v2_compaction_keeps_creation_time_instructions_after_same_path_m ); assert_eq!( test.codex.instruction_sources().await, - vec![source.clone()], + vec![PathUri::from_abs_path(&source)], "running thread retains the selected same-path source" ); assert_eq!( @@ -4829,7 +4830,7 @@ async fn remote_v2_compaction_keeps_creation_time_instructions_after_same_path_m ); assert_eq!( resumed.codex.instruction_sources().await, - vec![source], + vec![PathUri::from_abs_path(&source)], "cold-resumed thread reports the same rewritten source path" ); diff --git a/codex-rs/core/tests/suite/hierarchical_agents.rs b/codex-rs/core/tests/suite/hierarchical_agents.rs index f017d7d42b..c8c5da94b7 100644 --- a/codex-rs/core/tests/suite/hierarchical_agents.rs +++ b/codex-rs/core/tests/suite/hierarchical_agents.rs @@ -5,7 +5,6 @@ use core_test_support::responses::ev_response_created; use core_test_support::responses::mount_sse_once; use core_test_support::responses::sse; use core_test_support::responses::start_mock_server; -use core_test_support::skip_if_wine_exec; use core_test_support::test_codex::test_codex; const HIERARCHICAL_AGENTS_SNIPPET: &str = @@ -13,8 +12,6 @@ const HIERARCHICAL_AGENTS_SNIPPET: &str = #[tokio::test(flavor = "multi_thread", worker_threads = 2)] async fn hierarchical_agents_appends_to_project_doc_in_user_instructions() { - // TODO(anp): Remove after instruction-source helpers use target-native paths. - skip_if_wine_exec!("requires native cross-OS instruction-source paths"); let server = start_mock_server().await; let resp_mock = mount_sse_once( &server, diff --git a/codex-rs/exec-server/testing/wine_exec_server.rs b/codex-rs/exec-server/testing/wine_exec_server.rs index 88857e785a..a3643f8668 100644 --- a/codex-rs/exec-server/testing/wine_exec_server.rs +++ b/codex-rs/exec-server/testing/wine_exec_server.rs @@ -1,6 +1,7 @@ //! Test support for running the Windows exec-server under Wine. use std::future::Future; +use std::path::PathBuf; use anyhow::Context; use anyhow::Result; @@ -12,16 +13,18 @@ use wine_test_support::WineTestCommand; pub struct WineExecServer; impl WineExecServer { - /// Starts the server, passes its WebSocket URL to `operation`, and tears it down afterward. + /// Starts the server, passes its WebSocket URL and Wine prefix to `operation`, and tears it + /// down afterward. pub async fn scope(self, operation: F) -> Result where - F: FnOnce(String) -> Fut, + F: FnOnce(String, PathBuf) -> Fut, Fut: Future>, { let executable = codex_utils_cargo_bin::cargo_bin("wine-windows-exec-server")?; let mut exec_server = WineTestCommand::new(executable) .env("CODEX_HOME", r"C:\codex-home") .spawn()?; + let wine_prefix = exec_server.prefix_path().to_path_buf(); let stdout = exec_server.take_stdout(); exec_server @@ -36,7 +39,7 @@ impl WineExecServer { break line; } }; - operation(exec_server_url).await + operation(exec_server_url, wine_prefix).await }) .await } diff --git a/codex-rs/exec-server/testing/wine_remote_test_runner.rs b/codex-rs/exec-server/testing/wine_remote_test_runner.rs index 2889415675..6e73e68e9b 100644 --- a/codex-rs/exec-server/testing/wine_remote_test_runner.rs +++ b/codex-rs/exec-server/testing/wine_remote_test_runner.rs @@ -33,7 +33,7 @@ async fn main() -> Result<()> { } WineExecServer - .scope(|exec_server_url| async move { + .scope(|exec_server_url, _wine_prefix| async move { let mut command = Command::new(test_binary); command .env(TEST_ENVIRONMENT_ENV_VAR, "wine-exec") diff --git a/codex-rs/tui/Cargo.toml b/codex-rs/tui/Cargo.toml index b000cba124..2a10f00b42 100644 --- a/codex-rs/tui/Cargo.toml +++ b/codex-rs/tui/Cargo.toml @@ -62,6 +62,7 @@ codex-utils-fuzzy-match = { workspace = true } codex-utils-home-dir = { workspace = true } codex-utils-oss = { workspace = true } codex-utils-path = { workspace = true } +codex-utils-path-uri = { workspace = true } codex-utils-plugins = { workspace = true } codex-utils-sandbox-summary = { workspace = true } codex-utils-sleep-inhibitor = { workspace = true } @@ -151,7 +152,6 @@ codex-cli = { workspace = true } codex-mcp = { workspace = true } core_test_support = { workspace = true } codex-utils-cargo-bin = { workspace = true } -codex-utils-path-uri = { workspace = true } assert_matches = { workspace = true } chrono = { workspace = true, features = ["serde"] } insta = { workspace = true } diff --git a/codex-rs/tui/src/app_server_session.rs b/codex-rs/tui/src/app_server_session.rs index e57b26840d..a3734ac0e2 100644 --- a/codex-rs/tui/src/app_server_session.rs +++ b/codex-rs/tui/src/app_server_session.rs @@ -120,6 +120,7 @@ use codex_protocol::openai_models::ModelServiceTier; use codex_protocol::openai_models::ModelUpgrade; use codex_protocol::openai_models::ReasoningEffortPreset; use codex_utils_absolute_path::AbsolutePathBuf; +use codex_utils_path_uri::PathUri; use color_eyre::eyre::ContextCompat; use color_eyre::eyre::Result; use color_eyre::eyre::WrapErr; @@ -1561,7 +1562,7 @@ async fn thread_session_state_from_thread_start_response( response.active_permission_profile.clone().map(Into::into), response.cwd.clone(), response.runtime_workspace_roots.clone(), - response.instruction_sources.clone(), + response.instruction_source_path_uris(), response.reasoning_effort.clone(), config, ) @@ -1602,7 +1603,7 @@ async fn thread_session_state_from_thread_resume_response( response.active_permission_profile.clone().map(Into::into), response.cwd.clone(), response.runtime_workspace_roots.clone(), - response.instruction_sources.clone(), + response.instruction_source_path_uris(), response.reasoning_effort.clone(), config, ) @@ -1634,7 +1635,7 @@ async fn thread_session_state_from_thread_fork_response( response.active_permission_profile.clone().map(Into::into), response.cwd.clone(), response.runtime_workspace_roots.clone(), - response.instruction_sources.clone(), + response.instruction_source_path_uris(), response.reasoning_effort.clone(), config, ) @@ -1673,7 +1674,7 @@ async fn thread_session_state_from_thread_response( active_permission_profile: Option, cwd: AbsolutePathBuf, runtime_workspace_roots: Vec, - instruction_source_paths: Vec, + instruction_source_paths: Vec, reasoning_effort: Option, config: &Config, ) -> Result { @@ -1759,6 +1760,7 @@ mod tests { use codex_protocol::permissions::NetworkSandboxPolicy; use codex_utils_absolute_path::test_support::PathBufExt; use codex_utils_absolute_path::test_support::test_path_buf; + use codex_utils_path_uri::LegacyAppPathString; use pretty_assertions::assert_eq; use tempfile::TempDir; @@ -2367,7 +2369,9 @@ mod tests { test_path_buf("/tmp/project").abs(), test_path_buf("/tmp/project/extra").abs(), ], - instruction_sources: vec![test_path_buf("/tmp/project/AGENTS.md").abs()], + instruction_sources: vec![LegacyAppPathString::from_abs_path( + &test_path_buf("/tmp/project/AGENTS.md").abs(), + )], approval_policy: codex_app_server_protocol::AskForApproval::Never, approvals_reviewer: codex_app_server_protocol::ApprovalsReviewer::User, sandbox: read_only_profile @@ -2393,7 +2397,7 @@ mod tests { ); assert_eq!( started.session.instruction_source_paths, - response.instruction_sources + response.instruction_source_path_uris() ); assert_eq!(started.session.permission_profile, read_only_profile); assert_eq!(started.turns.len(), 1); diff --git a/codex-rs/tui/src/chatwidget.rs b/codex-rs/tui/src/chatwidget.rs index 94cdd9e03d..fd3c140e3f 100644 --- a/codex-rs/tui/src/chatwidget.rs +++ b/codex-rs/tui/src/chatwidget.rs @@ -163,6 +163,7 @@ use codex_terminal_detection::TerminalName; use codex_terminal_detection::terminal_info; use codex_utils_absolute_path::AbsolutePathBuf; use codex_utils_cli::resume_hint; +use codex_utils_path_uri::PathUri; use codex_utils_plugins::mention_syntax::PLUGIN_TEXT_MENTION_SIGIL; use codex_utils_plugins::mention_syntax::TOOL_MENTION_SIGIL; use crossterm::event::KeyCode; @@ -687,7 +688,7 @@ pub(crate) struct ChatWidget { // App-server-backed command runner for status-line workspace metadata lookups. workspace_command_runner: Option, // Instruction source files loaded for the current session, supplied by app-server. - instruction_source_paths: Vec, + instruction_source_paths: Vec, // Runtime network proxy bind addresses from SessionConfigured. session_network_proxy: Option, // Shared latch so we only warn once about invalid status-line item IDs. diff --git a/codex-rs/tui/src/chatwidget/tests/slash_commands.rs b/codex-rs/tui/src/chatwidget/tests/slash_commands.rs index 6fe4500feb..73d033de03 100644 --- a/codex-rs/tui/src/chatwidget/tests/slash_commands.rs +++ b/codex-rs/tui/src/chatwidget/tests/slash_commands.rs @@ -589,7 +589,9 @@ async fn ctrl_d_with_modal_open_does_not_quit() { #[tokio::test] async fn slash_init_does_not_depend_on_loaded_instruction_sources() { let (mut chat, mut rx, _op_rx) = make_chatwidget_manual(/*model_override*/ None).await; - chat.instruction_source_paths = vec![chat.config.cwd.join("project-instructions.md")]; + chat.instruction_source_paths = vec![codex_utils_path_uri::PathUri::from_abs_path( + &chat.config.cwd.join("project-instructions.md"), + )]; submit_composer_text(&mut chat, "/init"); diff --git a/codex-rs/tui/src/chatwidget/tests/status_command_tests.rs b/codex-rs/tui/src/chatwidget/tests/status_command_tests.rs index c206313549..953de6b89c 100644 --- a/codex-rs/tui/src/chatwidget/tests/status_command_tests.rs +++ b/codex-rs/tui/src/chatwidget/tests/status_command_tests.rs @@ -1,5 +1,6 @@ use super::*; use assert_matches::assert_matches; +use codex_utils_path_uri::PathUri; #[tokio::test] async fn status_command_renders_immediately_and_refreshes_rate_limits_for_chatgpt_auth() { @@ -96,9 +97,23 @@ async fn status_command_uses_catalog_default_reasoning_when_config_empty() { } #[tokio::test] -async fn status_command_renders_instruction_sources_from_thread_session() { +async fn status_command_renders_native_and_foreign_instruction_sources() { let (mut chat, mut rx, _op_rx) = make_chatwidget_manual(/*model_override*/ None).await; - chat.instruction_source_paths = vec![chat.config.cwd.join("AGENTS.md")]; + let (foreign_source, foreign_display) = if cfg!(windows) { + ( + PathUri::parse("file:///remote/AGENTS.md").expect("POSIX instruction source"), + "/remote/AGENTS.md", + ) + } else { + ( + PathUri::parse("file:///C:/remote/AGENTS.md").expect("Windows instruction source"), + r"C:\remote\AGENTS.md", + ) + }; + chat.instruction_source_paths = vec![ + PathUri::from_abs_path(&chat.config.cwd.join("AGENTS.md")), + foreign_source, + ]; chat.dispatch_command(SlashCommand::Status); @@ -109,8 +124,8 @@ async fn status_command_renders_instruction_sources_from_thread_session() { other => panic!("expected status output, got {other:?}"), }; assert!( - rendered.contains("Agents.md"), - "expected /status to render app-server instruction sources, got: {rendered}" + rendered.contains(&format!("AGENTS.md, {foreign_display}")), + "expected /status to show native-relative and environment-native foreign paths, got: {rendered}" ); assert!( !rendered.contains("Agents.md "), diff --git a/codex-rs/tui/src/session_state.rs b/codex-rs/tui/src/session_state.rs index 7988ba2264..059e148a79 100644 --- a/codex-rs/tui/src/session_state.rs +++ b/codex-rs/tui/src/session_state.rs @@ -12,6 +12,7 @@ use codex_protocol::config_types::Personality; use codex_protocol::models::ActivePermissionProfile; use codex_protocol::models::PermissionProfile; use codex_utils_absolute_path::AbsolutePathBuf; +use codex_utils_path_uri::PathUri; #[derive(Debug, Clone, PartialEq)] pub(crate) struct SessionNetworkProxyRuntime { @@ -47,7 +48,7 @@ pub(crate) struct ThreadSessionState { pub(crate) active_permission_profile: Option, pub(crate) cwd: AbsolutePathBuf, pub(crate) runtime_workspace_roots: Vec, - pub(crate) instruction_source_paths: Vec, + pub(crate) instruction_source_paths: Vec, pub(crate) reasoning_effort: Option, pub(crate) collaboration_mode: Option>, pub(crate) personality: Option, diff --git a/codex-rs/tui/src/status/helpers.rs b/codex-rs/tui/src/status/helpers.rs index f3ceeb435f..f03336a4b0 100644 --- a/codex-rs/tui/src/status/helpers.rs +++ b/codex-rs/tui/src/status/helpers.rs @@ -5,7 +5,8 @@ use crate::text_formatting; use chrono::DateTime; use chrono::Local; use codex_protocol::account::PlanType; -use codex_utils_absolute_path::AbsolutePathBuf; +use codex_utils_path_uri::PathConvention; +use codex_utils_path_uri::PathUri; use std::path::Path; use unicode_width::UnicodeWidthStr; @@ -33,10 +34,20 @@ pub(crate) fn compose_model_display( (model_name.to_string(), details) } -pub(crate) fn compose_agents_summary(config: &Config, paths: &[AbsolutePathBuf]) -> String { +pub(crate) fn compose_agents_summary(config: &Config, paths: &[PathUri]) -> String { let mut rels: Vec = Vec::new(); - for p in paths { + for path in paths { + // TODO(anp): Rationalize instruction-source summaries with the TUI's broader foreign-path + // display strategy once other status surfaces can retain environment-native paths. + if path.infer_path_convention() != Some(PathConvention::native()) { + rels.push(path.inferred_native_path_string()); + continue; + } + let Ok(p) = path.to_abs_path() else { + rels.push(path.inferred_native_path_string()); + continue; + }; let p = p.as_path(); let file_name = p .file_name() @@ -229,7 +240,10 @@ mod tests { let config = test_config(&codex_home, &cwd).await; assert_eq!( - compose_agents_summary(&config, &[global_agents_path.abs()]), + compose_agents_summary( + &config, + &[PathUri::from_abs_path(&global_agents_path.abs())] + ), format_directory_display(&global_agents_path, /*max_width*/ None) ); } @@ -242,11 +256,33 @@ mod tests { let config = test_config(&codex_home, &cwd).await; assert_eq!( - compose_agents_summary(&config, &[override_path.abs()]), + compose_agents_summary(&config, &[PathUri::from_abs_path(&override_path.abs())]), format_directory_display(&override_path, /*max_width*/ None) ); } + #[tokio::test] + async fn compose_agents_summary_shows_relative_native_and_full_foreign_paths() { + let codex_home = TempDir::new().expect("temp codex home"); + let cwd = TempDir::new().expect("temp cwd"); + let config = test_config(&codex_home, &cwd).await; + let native_source = PathUri::from_abs_path(&config.cwd.join("AGENTS.md")); + let foreign_source = if cfg!(windows) { + PathUri::parse("file:///remote%20workspace/AGENTS.md") + .expect("POSIX instruction source") + } else { + PathUri::parse("file:///C:/remote%20workspace/AGENTS.md") + .expect("Windows instruction source") + }; + + let summary = compose_agents_summary(&config, &[native_source, foreign_source]); + if cfg!(windows) { + insta::assert_snapshot!(summary, @r"AGENTS.md, /remote workspace/AGENTS.md"); + } else { + insta::assert_snapshot!(summary, @r"AGENTS.md, C:\remote workspace\AGENTS.md"); + } + } + #[tokio::test] async fn compose_agents_summary_orders_global_before_project_agents() { let codex_home = TempDir::new().expect("temp codex home"); @@ -258,8 +294,8 @@ mod tests { let summary = compose_agents_summary( &config, &[ - global_agents_path.clone().abs(), - project_agents_path.clone().abs(), + PathUri::from_abs_path(&global_agents_path.clone().abs()), + PathUri::from_abs_path(&project_agents_path.clone().abs()), ], ); let mut paths = summary.split(", "); diff --git a/codex-rs/utils/path-uri/src/lib.rs b/codex-rs/utils/path-uri/src/lib.rs index 6bc80b5561..5ef1df2fa2 100644 --- a/codex-rs/utils/path-uri/src/lib.rs +++ b/codex-rs/utils/path-uri/src/lib.rs @@ -209,13 +209,28 @@ impl PathUri { PathBuf::from(self.inferred_native_path_string()) } - /// Returns the parent URI, or `None` for the URI root or an opaque fallback - /// URI created by [`Self::from_abs_path`]. + /// Returns the lexical parent without crossing the inferred native path root. + /// + /// POSIX `/`, Windows drive roots, Windows UNC share roots, and opaque fallback + /// URIs created by [`Self::from_abs_path`] have no parent. pub fn parent(&self) -> Option { - if self.encoded_path() == "/" || decode_bad_path_uri(&self.0).is_some() { + if decode_bad_path_uri(&self.0).is_some() { return None; } + let convention = self.infer_path_convention()?; + // In URI form, both a Windows drive root (`file:///C:`) and a UNC share root + // (`file://server/share`) retain one non-empty path segment. Keep that segment as the + // anchor so parent traversal cannot produce a URI that is not an absolute Windows path. + let anchor_depth = usize::from(convention == PathConvention::Windows); + let depth = self + .0 + .path_segments()? + .filter(|segment| !segment.is_empty()) + .count(); + if depth <= anchor_depth { + return None; + } let mut url = self.0.clone(); { let mut segments = match url.path_segments_mut() { @@ -227,6 +242,11 @@ impl PathUri { Some(Self(url)) } + /// Returns this URI and each lexical parent up to its inferred native path root. + pub fn ancestors(&self) -> impl Iterator { + std::iter::successors(Some(self.clone()), Self::parent) + } + /// Lexically resolves native absolute or relative path text against this URI. /// /// Path text is interpreted using the POSIX or Windows convention inferred diff --git a/codex-rs/utils/path-uri/src/tests.rs b/codex-rs/utils/path-uri/src/tests.rs index 328142dc6d..075d343d01 100644 --- a/codex-rs/utils/path-uri/src/tests.rs +++ b/codex-rs/utils/path-uri/src/tests.rs @@ -522,7 +522,7 @@ fn path_buf_uses_the_inferred_native_spelling() { } #[test] -fn parent_uses_uri_hierarchy_and_preserves_authority() { +fn parent_stops_at_posix_drive_and_unc_roots() { for (input, expected) in [ ( "file:///workspace/src/lib.rs", @@ -531,12 +531,13 @@ fn parent_uses_uri_hierarchy_and_preserves_authority() { ("file:///workspace", Some("file:///")), ("file:///", None), ("file:///C:/Users", Some("file:///C:")), - ("file:///C:/", Some("file:///")), + ("file:///C:/", None), + ("file:///C:", None), ( "file://server/share/src/main.rs", Some("file://server/share/src"), ), - ("file://server/share", Some("file://server/")), + ("file://server/share", None), ] { let uri = PathUri::parse(input).expect("valid file URI"); let expected = expected.map(|value| PathUri::parse(value).expect("valid expected URI")); @@ -544,6 +545,35 @@ fn parent_uses_uri_hierarchy_and_preserves_authority() { } } +#[test] +fn ancestors_include_self_and_stop_at_native_path_roots() { + for (input, expected) in [ + ( + "file:///workspace/src", + vec!["file:///workspace/src", "file:///workspace", "file:///"], + ), + ( + "file:///C:/workspace/src", + vec![ + "file:///C:/workspace/src", + "file:///C:/workspace", + "file:///C:", + ], + ), + ( + "file://server/share/project", + vec!["file://server/share/project", "file://server/share"], + ), + ] { + let uri = PathUri::parse(input).expect("valid file URI"); + let ancestors = uri + .ancestors() + .map(|path| path.to_string()) + .collect::>(); + assert_eq!(ancestors, expected, "ancestors for {input}"); + } +} + #[test] fn join_normalizes_relative_uri_segments() { for (base, relative, expected) in [ From 772c5c51952a8bd279dbeeedfea69a6feb837a1d Mon Sep 17 00:00:00 2001 From: charlesgong-openai Date: Thu, 18 Jun 2026 15:49:27 -0700 Subject: [PATCH 06/10] [codex] Support marketplace plugin manifest fallback (#28789) ## Summary Support marketplace plugins whose source directory does not include a discoverable plugin manifest. Metadata-rich `marketplace.json` entries now act as fallback plugin manifests for listing, local detail reads, install, and non-curated cache refresh. The fallback preserves marketplace-entry plugin fields wholesale, then adds the small Codex-facing compatibility bridge for presentation metadata. A real source `plugin.json` always wins when present. ## Details - Capture flattened marketplace-entry fields into `MarketplacePluginManifestFallback`, preserving fields such as `version`, `description`, `skills`, `mcpServers`, `apps`, `hooks`, `agents`, `commands`, `strict`, `author`, and future manifest fields without a per-field translation list. - Bridge Claude-style top-level `displayName`, `author.name`, `homepage`, and marketplace `category` into Codex's nested `interface` fields only when the nested values are absent. - Treat fallback metadata as installable only when the marketplace entry contributes metadata beyond bare `name` and `source`; existing missing-manifest behavior remains for metadata-free entries. - Read local plugin details from the already parsed fallback manifest, including fallback-declared app and MCP paths, instead of rereading only an on-disk manifest. - Pass fallback contents into `PluginStore`, which validates them and injects `.codex-plugin/plugin.json` into Store's existing atomic copy. Local marketplace source directories are never mutated, and the fallback path no longer needs an additional staging directory. - Keep Git source materialization unchanged; Git clones still use the existing marketplace source staging area before Store installation. --- codex-rs/core-plugins/src/loader.rs | 67 ++++- codex-rs/core-plugins/src/manager.rs | 63 ++++- codex-rs/core-plugins/src/manager_tests.rs | 242 ++++++++++++++++- codex-rs/core-plugins/src/marketplace.rs | 158 ++++++++++- .../core-plugins/src/marketplace_tests.rs | 254 +++++++++++++++++- codex-rs/core-plugins/src/store.rs | 155 ++++++++++- codex-rs/core-plugins/src/store_tests.rs | 5 +- 7 files changed, 897 insertions(+), 47 deletions(-) diff --git a/codex-rs/core-plugins/src/loader.rs b/codex-rs/core-plugins/src/loader.rs index 43b561b3be..279578d75e 100644 --- a/codex-rs/core-plugins/src/loader.rs +++ b/codex-rs/core-plugins/src/loader.rs @@ -7,12 +7,14 @@ use crate::manifest::PluginManifestMcpServers; use crate::manifest::PluginManifestPaths; use crate::manifest::load_plugin_manifest; use crate::marketplace::MarketplacePluginSource; +use crate::marketplace::find_marketplace_plugin; use crate::marketplace::list_marketplaces; use crate::marketplace::load_marketplace; use crate::remote::REMOTE_GLOBAL_MARKETPLACE_NAME; use crate::remote::RemoteInstalledPlugin; use crate::store::PluginStore; use crate::store::plugin_version_for_source; +use crate::store::plugin_version_for_source_with_fallback_manifest; use codex_app_server_protocol::AuthMode; use codex_config::ConfigLayerStack; use codex_config::HooksFile; @@ -460,7 +462,7 @@ fn refresh_non_curated_plugin_cache_with_mode( let store = PluginStore::try_new(codex_home.to_path_buf()).map_err(|err| err.to_string())?; let marketplace_outcome = list_marketplaces(additional_roots) .map_err(|err| format!("failed to discover marketplaces for cache refresh: {err}"))?; - let mut plugin_sources = HashMap::::new(); + let mut plugin_sources = HashMap::)>::new(); for marketplace in marketplace_outcome.marketplaces { if is_openai_curated_marketplace_name(&marketplace.name) { @@ -489,14 +491,31 @@ fn refresh_non_curated_plugin_cache_with_mode( continue; } - plugin_sources.insert(plugin_key, plugin.source); + let manifest_fallback = find_marketplace_plugin(&marketplace.path, &plugin.name) + .map(|resolved| { + resolved + .manifest_fallback + .contents_if_has_metadata() + .map(str::to_string) + }) + .unwrap_or_else(|err| { + warn!( + plugin = plugin.name, + marketplace = marketplace.name, + error = %err, + "failed to resolve marketplace plugin manifest fallback during cache refresh" + ); + None + }); + plugin_sources.insert(plugin_key, (plugin.source, manifest_fallback)); } } let mut cache_refreshed = false; for plugin_id in configured_non_curated_plugin_ids { let plugin_key = plugin_id.as_key(); - let Some(source) = plugin_sources.get(&plugin_key).cloned() else { + let Some((source, manifest_fallback_contents)) = plugin_sources.get(&plugin_key).cloned() + else { warn!( plugin = plugin_id.plugin_name, marketplace = plugin_id.marketplace_name, @@ -509,8 +528,14 @@ fn refresh_non_curated_plugin_cache_with_mode( format!("failed to materialize plugin source for {plugin_key}: {err}") })?; let source_path = materialized.path.clone(); - let plugin_version = plugin_version_for_source(source_path.as_path()) - .map_err(|err| format!("failed to read plugin version for {plugin_key}: {err}"))?; + let plugin_version = match manifest_fallback_contents.as_deref() { + Some(manifest_contents) => plugin_version_for_source_with_fallback_manifest( + source_path.as_path(), + manifest_contents, + ), + None => plugin_version_for_source(source_path.as_path()), + } + .map_err(|err| format!("failed to read plugin version for {plugin_key}: {err}"))?; if mode == NonCuratedCacheRefreshMode::IfVersionChanged && store.active_plugin_version(&plugin_id).as_deref() == Some(plugin_version.as_str()) @@ -518,9 +543,16 @@ fn refresh_non_curated_plugin_cache_with_mode( continue; } - store - .install_with_version(source_path, plugin_id.clone(), plugin_version) - .map_err(|err| format!("failed to refresh plugin cache for {plugin_key}: {err}"))?; + match manifest_fallback_contents.as_deref() { + Some(manifest_contents) => store.install_with_version_and_fallback_manifest( + source_path, + plugin_id.clone(), + plugin_version, + manifest_contents, + ), + None => store.install_with_version(source_path, plugin_id.clone(), plugin_version), + } + .map_err(|err| format!("failed to refresh plugin cache for {plugin_key}: {err}"))?; cache_refreshed = true; } @@ -901,15 +933,22 @@ fn default_mcp_config_paths(plugin_root: &Path) -> Vec { pub async fn load_plugin_apps(plugin_root: &Path) -> Vec { if let Some(manifest) = load_plugin_manifest(plugin_root) { - return load_apps_from_paths( - plugin_root, - plugin_app_config_paths(plugin_root, &manifest.paths), - ) - .await; + return load_plugin_apps_from_manifest(plugin_root, &manifest.paths).await; } load_apps_from_paths(plugin_root, default_app_config_paths(plugin_root)).await } +pub(crate) async fn load_plugin_apps_from_manifest( + plugin_root: &Path, + manifest_paths: &PluginManifestPaths, +) -> Vec { + load_apps_from_paths( + plugin_root, + plugin_app_config_paths(plugin_root, manifest_paths), + ) + .await +} + pub fn plugin_app_declarations_from_value(value: &JsonValue) -> Vec { let Ok(parsed) = serde_json::from_value::(value.clone()) else { return Vec::new(); @@ -1181,7 +1220,7 @@ async fn load_declared_plugin_mcp_servers(plugin_root: &Path) -> HashMap>, diff --git a/codex-rs/core-plugins/src/manager.rs b/codex-rs/core-plugins/src/manager.rs index c31040ebf1..713a356a03 100644 --- a/codex-rs/core-plugins/src/manager.rs +++ b/codex-rs/core-plugins/src/manager.rs @@ -7,10 +7,10 @@ use crate::loader::PluginHookLoadOutcome; use crate::loader::configured_curated_plugin_ids_from_codex_home; use crate::loader::curated_plugin_cache_version; use crate::loader::installed_plugin_telemetry_metadata; -use crate::loader::load_plugin_apps; +use crate::loader::load_plugin_apps_from_manifest; use crate::loader::load_plugin_hooks; use crate::loader::load_plugin_hooks_from_layer_stack; -use crate::loader::load_plugin_mcp_servers; +use crate::loader::load_plugin_mcp_servers_from_manifest; use crate::loader::load_plugin_skills; use crate::loader::load_plugins_from_layer_stack; use crate::loader::log_plugin_load_errors; @@ -27,6 +27,7 @@ use crate::marketplace::MarketplaceInterface; use crate::marketplace::MarketplaceListError; use crate::marketplace::MarketplaceListOutcome; use crate::marketplace::MarketplacePluginAuthPolicy; +use crate::marketplace::MarketplacePluginManifestFallback; use crate::marketplace::MarketplacePluginPolicy; use crate::marketplace::MarketplacePluginSource; use crate::marketplace::ResolvedMarketplacePlugin; @@ -315,6 +316,7 @@ pub struct ConfiguredMarketplacePlugin { pub policy: MarketplacePluginPolicy, pub interface: Option, pub keywords: Vec, + pub manifest_fallback: Option, pub installed: bool, pub enabled: bool, } @@ -1256,15 +1258,32 @@ impl PluginsManager { }; let store = self.store.clone(); let codex_home = self.codex_home.clone(); + let manifest_fallback_contents = resolved + .manifest_fallback + .contents_if_has_metadata() + .map(str::to_string); let result: StorePluginInstallResult = tokio::task::spawn_blocking(move || { let materialized = materialize_marketplace_plugin_source(codex_home.as_path(), &resolved.source) .map_err(PluginStoreError::Invalid)?; let source_path = materialized.path; - if let Some(plugin_version) = plugin_version { - store.install_with_version(source_path, resolved.plugin_id, plugin_version) - } else { - store.install(source_path, resolved.plugin_id) + match (plugin_version, manifest_fallback_contents.as_deref()) { + (Some(plugin_version), Some(manifest_contents)) => store + .install_with_version_and_fallback_manifest( + source_path, + resolved.plugin_id, + plugin_version, + manifest_contents, + ), + (Some(plugin_version), None) => { + store.install_with_version(source_path, resolved.plugin_id, plugin_version) + } + (None, Some(manifest_contents)) => store.install_with_fallback_manifest( + source_path, + resolved.plugin_id, + manifest_contents, + ), + (None, None) => store.install(source_path, resolved.plugin_id), } }) .await @@ -1394,6 +1413,7 @@ impl PluginsManager { let enabled = enabled_plugins.contains(&plugin_key); let mut interface = plugin.interface; let mut local_version = plugin.local_version; + let manifest_fallback = plugin.manifest_fallback.clone(); if installed && matches!(&plugin.source, MarketplacePluginSource::Git { .. }) && let Some(plugin_id) = plugin_id.as_ref() @@ -1424,6 +1444,7 @@ impl PluginsManager { policy: plugin.policy, keywords: plugin.keywords, interface, + manifest_fallback, }) }) .collect::>(); @@ -1491,6 +1512,10 @@ impl PluginsManager { let marketplace_name = plugin.plugin_id.marketplace_name.clone(); let plugin_key = plugin.plugin_id.as_key(); + let manifest_fallback = plugin + .manifest_fallback + .contents_if_has_metadata() + .map(|_| plugin.manifest_fallback.clone()); let (installed_plugins, enabled_plugins) = self.configured_plugin_states(config); let installed = installed_plugins.contains(&plugin_key); let installed_version = if installed { @@ -1518,6 +1543,7 @@ impl PluginsManager { .as_ref() .map(|manifest| manifest.keywords.clone()) .unwrap_or_default(), + manifest_fallback, installed, enabled: enabled_plugins.contains(&plugin_key), }, @@ -1604,9 +1630,18 @@ impl PluginsManager { "path does not exist or is not a directory".to_string(), )); } - let manifest = load_plugin_manifest(source_path.as_path()).ok_or_else(|| { - MarketplaceError::InvalidPlugin("missing or invalid plugin.json".to_string()) - })?; + let manifest = + if codex_utils_plugins::find_plugin_manifest_path(source_path.as_path()).is_some() { + load_plugin_manifest(source_path.as_path()) + } else { + plugin + .manifest_fallback + .as_ref() + .and_then(|fallback| fallback.parse_for_plugin_root(source_path.as_path())) + } + .ok_or_else(|| { + MarketplaceError::InvalidPlugin("missing or invalid plugin.json".to_string()) + })?; let description = manifest.description.clone(); let marketplace_category = plugin .interface @@ -1637,8 +1672,14 @@ impl PluginsManager { }) .collect(); let auth_mode = self.auth_mode(); - let mut app_declarations = load_plugin_apps(source_path.as_path()).await; - let mut mcp_servers = load_plugin_mcp_servers(source_path.as_path(), auth_mode).await; + let mut app_declarations = + load_plugin_apps_from_manifest(source_path.as_path(), &manifest.paths).await; + let mut mcp_servers = load_plugin_mcp_servers_from_manifest( + source_path.as_path(), + &manifest.paths, + /*plugin_policy*/ None, + ) + .await; if auth_mode.is_some() { apply_app_mcp_routing_policy( &mut app_declarations, diff --git a/codex-rs/core-plugins/src/manager_tests.rs b/codex-rs/core-plugins/src/manager_tests.rs index 1aa11c6120..a4b30a1996 100644 --- a/codex-rs/core-plugins/src/manager_tests.rs +++ b/codex-rs/core-plugins/src/manager_tests.rs @@ -2241,6 +2241,98 @@ async fn install_plugin_uses_manifest_version_for_non_curated_plugins() { ); } +#[tokio::test] +async fn install_plugin_writes_marketplace_manifest_fallback_when_missing_plugin_json() { + let tmp = tempfile::tempdir().unwrap(); + let repo_root = tmp.path().join("repo"); + let plugin_root = repo_root.join("plugins/quality-review"); + fs::create_dir_all(repo_root.join(".git")).unwrap(); + fs::create_dir_all(repo_root.join(".agents/plugins")).unwrap(); + fs::create_dir_all(plugin_root.join("skills/thermo-nuclear-code-quality-review")).unwrap(); + fs::write( + plugin_root.join("skills/thermo-nuclear-code-quality-review/SKILL.md"), + "review skill", + ) + .unwrap(); + fs::write( + repo_root.join(".agents/plugins/marketplace.json"), + r#"{ + "name": "debug", + "plugins": [ + { + "name": "quality-review", + "description": "Strict code quality review focused on maintainability.", + "source": "./plugins/quality-review", + "author": { + "name": "Byron Grogan" + }, + "skills": [ + "./skills/thermo-nuclear-code-quality-review" + ], + "category": "code-review" + } + ] +}"#, + ) + .unwrap(); + + let result = PluginsManager::new(tmp.path().to_path_buf()) + .install_plugin(PluginInstallRequest { + plugin_name: "quality-review".to_string(), + marketplace_path: AbsolutePathBuf::try_from( + repo_root.join(".agents/plugins/marketplace.json"), + ) + .unwrap(), + }) + .await + .unwrap(); + + let installed_path = tmp.path().join("plugins/cache/debug/quality-review/local"); + assert_eq!( + result, + PluginInstallOutcome { + plugin_id: PluginId::new("quality-review".to_string(), "debug".to_string()).unwrap(), + plugin_version: "local".to_string(), + installed_path: AbsolutePathBuf::try_from(installed_path.clone()).unwrap(), + auth_policy: MarketplacePluginAuthPolicy::OnInstall, + } + ); + assert!(!plugin_root.join(".codex-plugin/plugin.json").exists()); + assert!( + !tmp.path() + .join("plugins/.marketplace-plugin-source-staging") + .exists() + ); + + let manifest = crate::manifest::load_plugin_manifest(&installed_path).unwrap(); + assert_eq!(manifest.name, "quality-review"); + assert_eq!( + manifest.description.as_deref(), + Some("Strict code quality review focused on maintainability.") + ); + assert_eq!( + manifest.paths.skills, + vec![ + AbsolutePathBuf::try_from( + installed_path.join("skills/thermo-nuclear-code-quality-review") + ) + .unwrap() + ] + ); + let interface = manifest.interface.expect("fallback interface"); + assert_eq!(interface.developer_name.as_deref(), Some("Byron Grogan")); + assert_eq!(interface.category.as_deref(), Some("code-review")); + let fallback_json: serde_json::Value = serde_json::from_str( + &fs::read_to_string(installed_path.join(".codex-plugin/plugin.json")).unwrap(), + ) + .unwrap(); + assert_eq!( + fallback_json["author"], + serde_json::json!({ "name": "Byron Grogan" }) + ); + assert_eq!(fallback_json["category"], "code-review"); +} + #[tokio::test] async fn install_plugin_supports_git_subdir_marketplace_sources() { let tmp = tempfile::tempdir().unwrap(); @@ -2484,6 +2576,7 @@ enabled = false }, interface: None, keywords: Vec::new(), + manifest_fallback: None, installed: true, enabled: true, }, @@ -2503,6 +2596,7 @@ enabled = false }, interface: None, keywords: Vec::new(), + manifest_fallback: None, installed: true, enabled: false, }, @@ -2632,6 +2726,7 @@ plugins = true }, interface: None, keywords: Vec::new(), + manifest_fallback: None, installed: false, enabled: false, }] @@ -2768,6 +2863,142 @@ plugins = true assert!(api_key_outcome.plugin.apps.is_empty()); } +#[tokio::test] +async fn read_plugin_for_config_uses_marketplace_manifest_fallback_paths_for_local_source() { + let tmp = tempfile::tempdir().unwrap(); + let repo_root = tmp.path().join("repo"); + let plugin_root = repo_root.join("sample-plugin"); + fs::create_dir_all(repo_root.join(".git")).unwrap(); + fs::create_dir_all(repo_root.join(".agents/plugins")).unwrap(); + write_file( + &repo_root.join(".agents/plugins/marketplace.json"), + r#"{ + "name": "debug", + "plugins": [ + { + "name": "sample-plugin", + "source": "./sample-plugin", + "apps": "./config/custom.app.json", + "mcpServers": { + "sample-mcp": { + "command": "sample-mcp" + } + } + } + ] +}"#, + ); + write_file( + &plugin_root.join("config/custom.app.json"), + r#"{"apps":{"sample-app":{"id":"connector_sample"}}}"#, + ); + write_file( + &tmp.path().join(CONFIG_TOML_FILE), + r#"[features] +plugins = true +"#, + ); + + let config = load_config(tmp.path(), &repo_root).await; + let manager = PluginsManager::new(tmp.path().to_path_buf()); + let outcome = manager + .read_plugin_for_config( + &config, + &PluginReadRequest { + plugin_name: "sample-plugin".to_string(), + marketplace_path: AbsolutePathBuf::try_from( + repo_root.join(".agents/plugins/marketplace.json"), + ) + .unwrap(), + }, + ) + .await + .unwrap(); + + assert_eq!( + outcome.plugin.apps, + vec![AppConnectorId("connector_sample".to_string())] + ); + assert_eq!( + outcome.plugin.mcp_server_names, + vec!["sample-mcp".to_string()] + ); + + let listed_plugin = manager + .list_marketplaces_for_config( + &config, + &[AbsolutePathBuf::try_from(repo_root.clone()).unwrap()], + /*include_openai_curated*/ false, + ) + .unwrap() + .marketplaces + .into_iter() + .find(|marketplace| marketplace.name == "debug") + .unwrap() + .plugins + .into_iter() + .find(|plugin| plugin.name == "sample-plugin") + .unwrap(); + let listed_detail = manager + .read_plugin_detail_for_marketplace_plugin(&config, "debug", listed_plugin) + .await + .unwrap(); + assert_eq!( + listed_detail.apps, + vec![AppConnectorId("connector_sample".to_string())] + ); + assert_eq!( + listed_detail.mcp_server_names, + vec!["sample-mcp".to_string()] + ); +} + +#[tokio::test] +async fn read_plugin_for_config_does_not_fallback_from_invalid_plugin_manifest() { + let tmp = tempfile::tempdir().unwrap(); + let repo_root = tmp.path().join("repo"); + let plugin_root = repo_root.join("sample-plugin"); + fs::create_dir_all(repo_root.join(".git")).unwrap(); + fs::create_dir_all(repo_root.join(".agents/plugins")).unwrap(); + write_file( + &repo_root.join(".agents/plugins/marketplace.json"), + r#"{ + "name": "debug", + "plugins": [ + { + "name": "sample-plugin", + "source": "./sample-plugin", + "description": "Fallback metadata" + } + ] +}"#, + ); + write_file(&plugin_root.join(".codex-plugin/plugin.json"), "{"); + write_file( + &tmp.path().join(CONFIG_TOML_FILE), + r#"[features] +plugins = true +"#, + ); + + let config = load_config(tmp.path(), &repo_root).await; + let err = PluginsManager::new(tmp.path().to_path_buf()) + .read_plugin_for_config( + &config, + &PluginReadRequest { + plugin_name: "sample-plugin".to_string(), + marketplace_path: AbsolutePathBuf::try_from( + repo_root.join(".agents/plugins/marketplace.json"), + ) + .unwrap(), + }, + ) + .await + .unwrap_err(); + + assert_eq!(err.to_string(), "missing or invalid plugin.json"); +} + #[tokio::test] async fn read_plugin_for_config_uses_user_layer_skill_settings_only() { let tmp = tempfile::tempdir().unwrap(); @@ -3150,8 +3381,11 @@ enabled = true .find(|marketplace| marketplace.name == "debug") .expect("debug marketplace should be listed"); + let mut plugins = marketplace.plugins; + assert!(plugins[0].manifest_fallback.is_some()); + plugins[0].manifest_fallback = None; assert_eq!( - marketplace.plugins, + plugins, vec![ConfiguredMarketplacePlugin { id: "toolkit@debug".to_string(), name: "toolkit".to_string(), @@ -3186,6 +3420,7 @@ enabled = true ..Default::default() }), keywords: Vec::new(), + manifest_fallback: None, installed: true, enabled: true, }] @@ -3266,6 +3501,7 @@ plugins = true }, interface: None, keywords: Vec::new(), + manifest_fallback: None, installed: false, enabled: false, }], @@ -3388,6 +3624,7 @@ plugins = true }, interface: None, keywords: Vec::new(), + manifest_fallback: None, installed: false, enabled: false, }], @@ -3815,6 +4052,7 @@ enabled = false }, interface: None, keywords: Vec::new(), + manifest_fallback: None, installed: false, enabled: true, }] @@ -3847,6 +4085,7 @@ enabled = false }, interface: None, keywords: Vec::new(), + manifest_fallback: None, installed: false, enabled: false, }] @@ -3937,6 +4176,7 @@ enabled = true }, interface: None, keywords: Vec::new(), + manifest_fallback: None, installed: false, enabled: true, }], diff --git a/codex-rs/core-plugins/src/marketplace.rs b/codex-rs/core-plugins/src/marketplace.rs index 82cbcf1415..456bd9004c 100644 --- a/codex-rs/core-plugins/src/marketplace.rs +++ b/codex-rs/core-plugins/src/marketplace.rs @@ -8,6 +8,7 @@ use codex_plugin::PluginIdError; use codex_protocol::protocol::Product; use codex_utils_absolute_path::AbsolutePathBuf; use serde::Deserialize; +use serde_json::Map as JsonMap; use serde_json::Value as JsonValue; use std::fs; use std::io; @@ -29,6 +30,7 @@ pub struct ResolvedMarketplacePlugin { pub policy: MarketplacePluginPolicy, pub interface: Option, pub manifest: Option, + pub manifest_fallback: MarketplacePluginManifestFallback, } #[derive(Debug, Clone, PartialEq, Eq)] @@ -56,6 +58,57 @@ pub struct MarketplaceInterface { pub display_name: Option, } +#[derive(Debug, Clone, PartialEq, Eq)] +pub struct MarketplacePluginManifestFallback { + contents: String, + has_metadata: bool, +} + +impl MarketplacePluginManifestFallback { + pub fn contents(&self) -> &str { + &self.contents + } + + pub(crate) fn contents_if_has_metadata(&self) -> Option<&str> { + self.has_metadata.then_some(self.contents()) + } + + pub(crate) fn parse_for_plugin_root( + &self, + plugin_root: &Path, + ) -> Option { + crate::manifest::parse_plugin_manifest( + plugin_root, + &fallback_plugin_manifest_path(plugin_root), + &self.contents, + ) + .ok() + } + + pub(crate) fn parse_for_listing(&self) -> Option { + // Git sources have no plugin root before install. Parse against a synthetic absolute root, + // then discard path-bearing fields so listings expose metadata only. + let mut manifest = crate::manifest::parse_plugin_manifest( + Path::new("/"), + Path::new("/.codex-plugin/plugin.json"), + &self.contents, + ) + .ok()?; + manifest.paths = crate::manifest::PluginManifestPaths { + skills: Vec::new(), + mcp_servers: None, + apps: None, + hooks: None, + }; + if let Some(interface) = manifest.interface.as_mut() { + interface.composer_icon = None; + interface.logo = None; + interface.screenshots.clear(); + } + Some(manifest) + } +} + #[derive(Debug, Clone, PartialEq, Eq)] pub struct MarketplacePlugin { pub name: String, @@ -64,6 +117,7 @@ pub struct MarketplacePlugin { pub policy: MarketplacePluginPolicy, pub interface: Option, pub keywords: Vec, + pub manifest_fallback: Option, } #[derive(Debug, Clone, PartialEq, Eq, Hash)] @@ -313,6 +367,10 @@ pub fn load_marketplace(path: &AbsolutePathBuf) -> Result return Err(err), }; + let manifest_fallback = plugin + .manifest_fallback + .contents_if_has_metadata() + .map(|_| plugin.manifest_fallback.clone()); let local_version = plugin .manifest .as_ref() @@ -329,6 +387,7 @@ pub fn load_marketplace(path: &AbsolutePathBuf) -> Result load_plugin_manifest(path.as_path()), + MarketplacePluginSource::Local { path } => { + if codex_utils_plugins::find_plugin_manifest_path(path.as_path()).is_some() { + load_plugin_manifest(path.as_path()) + } else if manifest_fallback.has_metadata { + manifest_fallback.parse_for_plugin_root(path.as_path()) + } else { + None + } + } + MarketplacePluginSource::Git { .. } if manifest_fallback.has_metadata => { + manifest_fallback.parse_for_listing() + } MarketplacePluginSource::Git { .. } => None, }; let interface = plugin_interface_with_marketplace_category( @@ -462,6 +535,7 @@ fn resolve_marketplace_plugin_entry( }, interface, manifest, + manifest_fallback, })) } @@ -765,6 +839,9 @@ struct RawMarketplaceManifestPlugin { policy: RawMarketplaceManifestPluginPolicy, #[serde(default)] category: Option, + #[serde(default)] + #[serde(flatten)] + manifest_fields: JsonMap, } #[derive(Debug, Clone, Default, PartialEq, Eq, Deserialize)] @@ -822,6 +899,85 @@ fn resolve_marketplace_interface( } } +fn fallback_plugin_manifest_path(plugin_root: &Path) -> PathBuf { + plugin_root.join(".codex-plugin/plugin.json") +} + +fn marketplace_plugin_manifest_fallback( + name: &str, + category: Option<&str>, + manifest_fields: &JsonMap, +) -> MarketplacePluginManifestFallback { + let mut manifest = manifest_fields.clone(); + manifest.insert("name".to_string(), JsonValue::String(name.to_string())); + if let Some(category) = category { + manifest.insert( + "category".to_string(), + JsonValue::String(category.to_string()), + ); + } + if let Some(interface) = plugin_manifest_interface(manifest_fields, category) { + manifest.insert("interface".to_string(), interface); + } + + let contents = serde_json::to_string_pretty(&JsonValue::Object(manifest)) + .unwrap_or_else(|_| format!(r#"{{"name":"{name}"}}"#)); + MarketplacePluginManifestFallback { + contents, + has_metadata: !manifest_fields.is_empty() || category.is_some(), + } +} + +fn plugin_manifest_interface( + fields: &JsonMap, + category: Option<&str>, +) -> Option { + let mut interface = fields + .get("interface") + .and_then(JsonValue::as_object) + .cloned() + .unwrap_or_default(); + + if !interface.contains_key("displayName") + && let Some(display_name) = fields.get("displayName").and_then(JsonValue::as_str) + { + interface.insert( + "displayName".to_string(), + JsonValue::String(display_name.to_string()), + ); + } + if !interface.contains_key("developerName") + && let Some(author_name) = fields + .get("author") + .and_then(|author| author.get("name")) + .and_then(JsonValue::as_str) + .map(str::trim) + .filter(|value| !value.is_empty()) + { + interface.insert( + "developerName".to_string(), + JsonValue::String(author_name.to_string()), + ); + } + if !interface.contains_key("websiteUrl") + && !interface.contains_key("websiteURL") + && let Some(homepage) = fields.get("homepage").and_then(JsonValue::as_str) + { + interface.insert( + "websiteUrl".to_string(), + JsonValue::String(homepage.to_string()), + ); + } + if let Some(category) = category.map(str::trim).filter(|value| !value.is_empty()) { + interface.insert( + "category".to_string(), + JsonValue::String(category.to_string()), + ); + } + + (!interface.is_empty()).then_some(JsonValue::Object(interface)) +} + #[cfg(test)] #[path = "marketplace_tests.rs"] mod tests; diff --git a/codex-rs/core-plugins/src/marketplace_tests.rs b/codex-rs/core-plugins/src/marketplace_tests.rs index 61385f1db0..45aa2364e2 100644 --- a/codex-rs/core-plugins/src/marketplace_tests.rs +++ b/codex-rs/core-plugins/src/marketplace_tests.rs @@ -20,6 +20,17 @@ fn write_alternate_plugin_manifest(plugin_root: &Path, contents: &str) { fs::write(manifest_path, contents).unwrap(); } +fn minimal_manifest_fallback(name: &str) -> MarketplacePluginManifestFallback { + MarketplacePluginManifestFallback { + contents: format!( + r#"{{ + "name": "{name}" +}}"# + ), + has_metadata: false, + } +} + #[test] fn find_marketplace_plugin_finds_repo_marketplace_plugin() { let tmp = tempdir().unwrap(); @@ -65,6 +76,7 @@ fn find_marketplace_plugin_finds_repo_marketplace_plugin() { }, interface: None, manifest: None, + manifest_fallback: minimal_manifest_fallback("local-plugin"), } ); } @@ -108,6 +120,7 @@ fn find_marketplace_plugin_supports_alternate_layout_and_string_local_source() { }, interface: None, manifest: None, + manifest_fallback: minimal_manifest_fallback("string-source-plugin"), } ); } @@ -162,10 +175,219 @@ fn find_marketplace_plugin_supports_git_subdir_sources() { }, interface: None, manifest: None, + manifest_fallback: minimal_manifest_fallback("remote-plugin"), } ); } +#[test] +fn find_marketplace_plugin_builds_manifest_fallback_from_entry() { + let tmp = tempdir().unwrap(); + let repo_root = tmp.path().join("repo"); + let plugin_root = repo_root.join("plugins/quality-review"); + fs::create_dir_all(repo_root.join(".git")).unwrap(); + fs::create_dir_all(repo_root.join(".agents/plugins")).unwrap(); + fs::create_dir_all(plugin_root.join("skills/thermo-nuclear-code-quality-review")).unwrap(); + fs::create_dir_all(plugin_root.join("skills/second-review")).unwrap(); + fs::write( + repo_root.join(".agents/plugins/marketplace.json"), + r##"{ + "name": "team-marketplace", + "plugins": [ + { + "name": "quality-review", + "version": "1.2.3", + "description": "Strict code quality review focused on maintainability.", + "displayName": "Quality Review", + "source": "./plugins/quality-review", + "author": { + "name": "Byron Grogan" + }, + "homepage": "https://example.com/quality", + "repository": "https://github.com/example/quality-review", + "license": "MIT", + "skills": [ + "./skills/thermo-nuclear-code-quality-review", + "./skills/second-review" + ], + "commands": ["./commands/review.md"], + "mcpServers": { + "review": { + "type": "stdio", + "command": "review-mcp" + } + }, + "apps": "./apps/app.json", + "hooks": ["./hooks/session.json"], + "agents": [ + "./agents/thermo-nuclear-code-quality-review.md" + ], + "category": "code-review", + "keywords": ["quality", "review"], + "strict": false, + "interface": { + "shortDescription": "Interface short description.", + "longDescription": "Runs strict reviews focused on maintainability and boundaries.", + "category": "interface-category", + "capabilities": ["review", "quality"], + "privacyPolicyURL": "https://example.com/privacy", + "termsOfServiceUrl": "https://example.com/terms", + "defaultPrompt": [ + "Review this change", + "Find structural issues" + ], + "brandColor": "#00AAFF", + "composerIcon": "./assets/icon.svg", + "logo": "./assets/logo.png", + "screenshots": ["./assets/shot.png"] + } + } + ] +}"##, + ) + .unwrap(); + + let resolved = find_marketplace_plugin( + &AbsolutePathBuf::try_from(repo_root.join(".agents/plugins/marketplace.json")).unwrap(), + "quality-review", + ) + .unwrap(); + + let manifest = resolved.manifest.as_ref().expect("fallback manifest"); + assert_eq!(manifest.name, "quality-review"); + assert_eq!(manifest.version.as_deref(), Some("1.2.3")); + assert_eq!( + manifest.description.as_deref(), + Some("Strict code quality review focused on maintainability.") + ); + assert_eq!( + manifest.paths.skills, + vec![ + AbsolutePathBuf::try_from( + plugin_root.join("skills/thermo-nuclear-code-quality-review") + ) + .unwrap(), + AbsolutePathBuf::try_from(plugin_root.join("skills/second-review")).unwrap(), + ] + ); + let Some(crate::manifest::PluginManifestMcpServers::Object(mcp_servers)) = + manifest.paths.mcp_servers.as_ref() + else { + panic!("fallback mcpServers should be inline"); + }; + assert_eq!( + serde_json::from_str::(mcp_servers).unwrap(), + serde_json::json!({ + "review": { + "type": "stdio", + "command": "review-mcp" + } + }) + ); + assert_eq!( + manifest.paths.apps.as_ref(), + Some(&AbsolutePathBuf::try_from(plugin_root.join("apps/app.json")).unwrap()) + ); + assert_eq!( + manifest.paths.hooks.as_ref(), + Some(&crate::manifest::PluginManifestHooks::Paths(vec![ + AbsolutePathBuf::try_from(plugin_root.join("hooks/session.json")).unwrap() + ])) + ); + assert_eq!(manifest.keywords, vec!["quality", "review"]); + let interface = manifest.interface.as_ref().expect("fallback interface"); + assert_eq!( + interface, + &PluginManifestInterface { + display_name: Some("Quality Review".to_string()), + short_description: Some("Interface short description.".to_string()), + long_description: Some( + "Runs strict reviews focused on maintainability and boundaries.".to_string() + ), + developer_name: Some("Byron Grogan".to_string()), + category: Some("code-review".to_string()), + capabilities: vec!["review".to_string(), "quality".to_string()], + website_url: Some("https://example.com/quality".to_string()), + privacy_policy_url: Some("https://example.com/privacy".to_string()), + terms_of_service_url: Some("https://example.com/terms".to_string()), + default_prompt: Some(vec![ + "Review this change".to_string(), + "Find structural issues".to_string() + ]), + brand_color: Some("#00AAFF".to_string()), + composer_icon: Some( + AbsolutePathBuf::try_from(plugin_root.join("assets/icon.svg")).unwrap() + ), + logo: Some(AbsolutePathBuf::try_from(plugin_root.join("assets/logo.png")).unwrap()), + screenshots: vec![ + AbsolutePathBuf::try_from(plugin_root.join("assets/shot.png")).unwrap() + ], + } + ); + + let fallback_json: JsonValue = + serde_json::from_str(resolved.manifest_fallback.contents()).unwrap(); + assert_eq!( + fallback_json["skills"], + serde_json::json!([ + "./skills/thermo-nuclear-code-quality-review", + "./skills/second-review" + ]) + ); + assert_eq!( + fallback_json["mcpServers"], + serde_json::json!({ + "review": { + "type": "stdio", + "command": "review-mcp" + } + }) + ); + assert_eq!( + fallback_json["displayName"], + JsonValue::String("Quality Review".to_string()) + ); + assert_eq!( + fallback_json["interface"]["websiteUrl"], + JsonValue::String("https://example.com/quality".to_string()) + ); + assert_eq!( + fallback_json["interface"]["privacyPolicyURL"], + JsonValue::String("https://example.com/privacy".to_string()) + ); + assert!(fallback_json["interface"].get("privacyPolicyUrl").is_none()); + assert_eq!( + fallback_json["author"], + serde_json::json!({ "name": "Byron Grogan" }) + ); + assert_eq!( + fallback_json["agents"], + serde_json::json!(["./agents/thermo-nuclear-code-quality-review.md"]) + ); + assert_eq!( + fallback_json["commands"], + serde_json::json!(["./commands/review.md"]) + ); + assert_eq!(fallback_json["strict"], JsonValue::Bool(false)); + assert_eq!( + fallback_json["homepage"], + JsonValue::String("https://example.com/quality".to_string()) + ); + assert_eq!( + fallback_json["repository"], + JsonValue::String("https://github.com/example/quality-review".to_string()) + ); + assert_eq!( + fallback_json["license"], + JsonValue::String("MIT".to_string()) + ); + assert_eq!( + fallback_json["category"], + JsonValue::String("code-review".to_string()) + ); + assert!(resolved.manifest_fallback.has_metadata); +} + #[test] fn find_marketplace_plugin_normalizes_github_shorthand_with_dot_git_suffix() { let tmp = tempdir().unwrap(); @@ -415,6 +637,7 @@ fn list_marketplaces_supports_alternate_manifest_layout() { screenshots: Vec::new(), }), keywords: Vec::new(), + manifest_fallback: None, }], }] ); @@ -500,6 +723,7 @@ fn list_marketplaces_supports_repo_root_local_plugin_sources() { screenshots: Vec::new(), }), keywords: Vec::new(), + manifest_fallback: None, }], }] ); @@ -552,6 +776,7 @@ fn list_marketplaces_includes_plugins_without_discoverable_manifest() { }, interface: None, keywords: Vec::new(), + manifest_fallback: None, }], }] ); @@ -659,6 +884,7 @@ fn list_marketplaces_supports_explicit_api_marketplace_manifest_path() { }, interface: None, keywords: Vec::new(), + manifest_fallback: None, }], }] ); @@ -750,6 +976,7 @@ fn list_marketplaces_returns_home_and_repo_marketplaces() { }, interface: None, keywords: Vec::new(), + manifest_fallback: None, }, MarketplacePlugin { name: "home-only".to_string(), @@ -764,6 +991,7 @@ fn list_marketplaces_returns_home_and_repo_marketplaces() { }, interface: None, keywords: Vec::new(), + manifest_fallback: None, }, ], }, @@ -787,6 +1015,7 @@ fn list_marketplaces_returns_home_and_repo_marketplaces() { }, interface: None, keywords: Vec::new(), + manifest_fallback: None, }, MarketplacePlugin { name: "repo-only".to_string(), @@ -801,6 +1030,7 @@ fn list_marketplaces_returns_home_and_repo_marketplaces() { }, interface: None, keywords: Vec::new(), + manifest_fallback: None, }, ], }, @@ -880,6 +1110,7 @@ fn list_marketplaces_keeps_distinct_entries_for_same_name() { }, interface: None, keywords: Vec::new(), + manifest_fallback: None, }], }, Marketplace { @@ -899,6 +1130,7 @@ fn list_marketplaces_keeps_distinct_entries_for_same_name() { }, interface: None, keywords: Vec::new(), + manifest_fallback: None, }], }, ] @@ -974,6 +1206,7 @@ fn list_marketplaces_dedupes_multiple_roots_in_same_repo() { }, interface: None, keywords: Vec::new(), + manifest_fallback: None, }], }] ); @@ -1138,6 +1371,7 @@ fn list_marketplaces_skips_plugins_with_invalid_names_but_keeps_marketplace() { }, interface: None, keywords: Vec::new(), + manifest_fallback: None, }], }] ); @@ -1220,6 +1454,9 @@ fn list_marketplaces_keeps_remote_and_local_plugin_sources() { }, { "name": "git-subdir-plugin", + "version": "1.2.3", + "displayName": "Git Subdir Plugin", + "keywords": ["git", "remote"], "source": { "source": "git-subdir", "url": "owner/repo", @@ -1240,8 +1477,11 @@ fn list_marketplaces_keeps_remote_and_local_plugin_sources() { .marketplaces; assert_eq!(marketplaces.len(), 1); + let mut plugins = marketplaces[0].plugins.clone(); + assert!(plugins[2].manifest_fallback.is_some()); + plugins[2].manifest_fallback = None; assert_eq!( - marketplaces[0].plugins, + plugins, vec![ MarketplacePlugin { name: "local-plugin".to_string(), @@ -1257,6 +1497,7 @@ fn list_marketplaces_keeps_remote_and_local_plugin_sources() { }, interface: None, keywords: Vec::new(), + manifest_fallback: None, }, MarketplacePlugin { name: "url-plugin".to_string(), @@ -1274,10 +1515,11 @@ fn list_marketplaces_keeps_remote_and_local_plugin_sources() { }, interface: None, keywords: Vec::new(), + manifest_fallback: None, }, MarketplacePlugin { name: "git-subdir-plugin".to_string(), - local_version: None, + local_version: Some("1.2.3".to_string()), source: MarketplacePluginSource::Git { url: "https://github.com/owner/repo.git".to_string(), path: Some("plugins/example".to_string()), @@ -1289,8 +1531,12 @@ fn list_marketplaces_keeps_remote_and_local_plugin_sources() { authentication: MarketplacePluginAuthPolicy::OnInstall, products: None, }, - interface: None, - keywords: Vec::new(), + interface: Some(PluginManifestInterface { + display_name: Some("Git Subdir Plugin".to_string()), + ..Default::default() + }), + keywords: vec!["git".to_string(), "remote".to_string()], + manifest_fallback: None, }, ] ); diff --git a/codex-rs/core-plugins/src/store.rs b/codex-rs/core-plugins/src/store.rs index 19c3b0116f..f0c737e28d 100644 --- a/codex-rs/core-plugins/src/store.rs +++ b/codex-rs/core-plugins/src/store.rs @@ -1,5 +1,6 @@ use crate::manifest::PluginManifest; use crate::manifest::load_plugin_manifest; +use crate::manifest::parse_plugin_manifest; use codex_plugin::PluginId; use codex_plugin::validate_plugin_segment; use codex_utils_absolute_path::AbsolutePathBuf; @@ -30,6 +31,12 @@ pub struct PluginStore { data_root: AbsolutePathBuf, } +#[derive(Clone, Copy)] +enum InstallManifest<'a> { + OnDisk, + Fallback(&'a str), +} + impl PluginStore { pub fn new(codex_home: PathBuf) -> Self { Self::try_new(codex_home) @@ -104,8 +111,20 @@ impl PluginStore { source_path: AbsolutePathBuf, plugin_id: PluginId, ) -> Result { - let plugin_version = plugin_version_for_source(source_path.as_path())?; - self.install_with_version(source_path, plugin_id, plugin_version) + self.install_with_manifest(source_path, plugin_id, InstallManifest::OnDisk) + } + + pub(crate) fn install_with_fallback_manifest( + &self, + source_path: AbsolutePathBuf, + plugin_id: PluginId, + manifest_contents: &str, + ) -> Result { + self.install_with_manifest( + source_path, + plugin_id, + InstallManifest::Fallback(manifest_contents), + ) } pub fn install_with_version( @@ -113,6 +132,47 @@ impl PluginStore { source_path: AbsolutePathBuf, plugin_id: PluginId, plugin_version: String, + ) -> Result { + self.install_with_version_and_manifest( + source_path, + plugin_id, + plugin_version, + InstallManifest::OnDisk, + ) + } + + pub(crate) fn install_with_version_and_fallback_manifest( + &self, + source_path: AbsolutePathBuf, + plugin_id: PluginId, + plugin_version: String, + manifest_contents: &str, + ) -> Result { + self.install_with_version_and_manifest( + source_path, + plugin_id, + plugin_version, + InstallManifest::Fallback(manifest_contents), + ) + } + + fn install_with_manifest( + &self, + source_path: AbsolutePathBuf, + plugin_id: PluginId, + manifest: InstallManifest<'_>, + ) -> Result { + let manifest = resolve_install_manifest(source_path.as_path(), manifest); + let plugin_version = plugin_version_for_install_manifest(source_path.as_path(), manifest)?; + self.install_with_version_and_manifest(source_path, plugin_id, plugin_version, manifest) + } + + fn install_with_version_and_manifest( + &self, + source_path: AbsolutePathBuf, + plugin_id: PluginId, + plugin_version: String, + manifest: InstallManifest<'_>, ) -> Result { if !source_path.as_path().is_dir() { return Err(PluginStoreError::Invalid(format!( @@ -121,7 +181,8 @@ impl PluginStore { ))); } - let plugin_name = plugin_name_for_source(source_path.as_path())?; + let manifest = resolve_install_manifest(source_path.as_path(), manifest); + let plugin_name = plugin_name_for_source(source_path.as_path(), manifest)?; if plugin_name != plugin_id.plugin_name { return Err(PluginStoreError::Invalid(format!( "plugin.json name `{plugin_name}` does not match marketplace plugin name `{}`", @@ -134,6 +195,7 @@ impl PluginStore { source_path.as_path(), self.plugin_base_root(&plugin_id).as_path(), &plugin_version, + manifest, )?; Ok(PluginInstallResult { @@ -168,7 +230,37 @@ impl PluginStoreError { } pub fn plugin_version_for_source(source_path: &Path) -> Result { - let plugin_version = plugin_manifest_version_for_source(source_path)? + plugin_version_for_install_manifest(source_path, InstallManifest::OnDisk) +} + +pub(crate) fn plugin_version_for_source_with_fallback_manifest( + source_path: &Path, + manifest_contents: &str, +) -> Result { + let manifest = + resolve_install_manifest(source_path, InstallManifest::Fallback(manifest_contents)); + plugin_version_for_install_manifest(source_path, manifest) +} + +fn resolve_install_manifest<'a>( + source_path: &Path, + manifest: InstallManifest<'a>, +) -> InstallManifest<'a> { + // A real plugin manifest always wins. The fallback only fills the gap for marketplace + // sources that cannot be changed in place because they may be user-owned directories. + match manifest { + InstallManifest::Fallback(_) if find_plugin_manifest_path(source_path).is_some() => { + InstallManifest::OnDisk + } + manifest => manifest, + } +} + +fn plugin_version_for_install_manifest( + source_path: &Path, + manifest: InstallManifest<'_>, +) -> Result { + let plugin_version = plugin_manifest_version_for_source(source_path, manifest)? .unwrap_or_else(|| DEFAULT_PLUGIN_VERSION.to_string()); validate_plugin_version_segment(&plugin_version).map_err(PluginStoreError::Invalid)?; Ok(plugin_version) @@ -193,9 +285,20 @@ pub fn validate_plugin_version_segment(plugin_version: &str) -> Result<(), Strin Ok(()) } -fn plugin_manifest_for_source(source_path: &Path) -> Result { - load_plugin_manifest(source_path) - .ok_or_else(|| PluginStoreError::Invalid("missing or invalid plugin.json".to_string())) +fn plugin_manifest_for_source( + source_path: &Path, + manifest: InstallManifest<'_>, +) -> Result { + match manifest { + InstallManifest::OnDisk => load_plugin_manifest(source_path) + .ok_or_else(|| PluginStoreError::Invalid("missing or invalid plugin.json".to_string())), + InstallManifest::Fallback(contents) => parse_plugin_manifest( + source_path, + &source_path.join(".codex-plugin/plugin.json"), + contents, + ) + .map_err(|err| PluginStoreError::Invalid(format!("failed to parse plugin.json: {err}"))), + } } #[derive(Debug, Deserialize)] @@ -207,12 +310,17 @@ struct RawPluginManifestVersion { fn plugin_manifest_version_for_source( source_path: &Path, + manifest: InstallManifest<'_>, ) -> Result, PluginStoreError> { - let manifest_path = find_plugin_manifest_path(source_path) - .ok_or_else(|| PluginStoreError::Invalid("missing plugin.json".to_string()))?; - - let contents = fs::read_to_string(&manifest_path) - .map_err(|err| PluginStoreError::io("failed to read plugin.json", err))?; + let contents = match manifest { + InstallManifest::OnDisk => { + let manifest_path = find_plugin_manifest_path(source_path) + .ok_or_else(|| PluginStoreError::Invalid("missing plugin.json".to_string()))?; + fs::read_to_string(&manifest_path) + .map_err(|err| PluginStoreError::io("failed to read plugin.json", err))? + } + InstallManifest::Fallback(contents) => contents.to_string(), + }; let manifest: RawPluginManifestVersion = serde_json::from_str(&contents) .map_err(|err| PluginStoreError::Invalid(format!("failed to parse plugin.json: {err}")))?; let Some(version) = manifest.version else { @@ -232,8 +340,11 @@ fn plugin_manifest_version_for_source( Ok(Some(version.to_string())) } -fn plugin_name_for_source(source_path: &Path) -> Result { - let manifest = plugin_manifest_for_source(source_path)?; +fn plugin_name_for_source( + source_path: &Path, + manifest: InstallManifest<'_>, +) -> Result { + let manifest = plugin_manifest_for_source(source_path, manifest)?; let plugin_name = manifest.name; validate_plugin_segment(&plugin_name, "plugin name") @@ -261,6 +372,7 @@ fn replace_plugin_root_atomically( source: &Path, target_root: &Path, plugin_version: &str, + manifest: InstallManifest<'_>, ) -> Result<(), PluginStoreError> { let Some(parent) = target_root.parent() else { return Err(PluginStoreError::Invalid(format!( @@ -287,6 +399,21 @@ fn replace_plugin_root_atomically( let staged_root = staged_dir.path().join(plugin_dir_name); let staged_version_root = staged_root.join(plugin_version); copy_dir_recursive(source, &staged_version_root)?; + if let InstallManifest::Fallback(contents) = manifest { + // Inject the generated manifest into Store's existing atomic copy so install does not + // mutate the original source or require a second staging directory. + let manifest_path = staged_version_root.join(".codex-plugin/plugin.json"); + let Some(manifest_parent) = manifest_path.parent() else { + return Err(PluginStoreError::Invalid( + "plugin manifest path has no parent".to_string(), + )); + }; + fs::create_dir_all(manifest_parent).map_err(|err| { + PluginStoreError::io("failed to create plugin manifest directory", err) + })?; + fs::write(&manifest_path, contents) + .map_err(|err| PluginStoreError::io("failed to write fallback plugin manifest", err))?; + } let target_version_root = target_root.join(plugin_version); if target_root.exists() && !target_version_root.exists() { diff --git a/codex-rs/core-plugins/src/store_tests.rs b/codex-rs/core-plugins/src/store_tests.rs index 6bf2b93688..aa9b17cc29 100644 --- a/codex-rs/core-plugins/src/store_tests.rs +++ b/codex-rs/core-plugins/src/store_tests.rs @@ -192,7 +192,7 @@ fn install_with_version_uses_requested_cache_version() { } #[test] -fn install_uses_manifest_version_when_present() { +fn install_prefers_on_disk_manifest_version_over_fallback() { let tmp = tempdir().unwrap(); write_plugin_with_version( tmp.path(), @@ -203,9 +203,10 @@ fn install_uses_manifest_version_when_present() { let plugin_id = PluginId::new("sample-plugin".to_string(), "debug".to_string()).unwrap(); let result = PluginStore::new(tmp.path().to_path_buf()) - .install( + .install_with_fallback_manifest( AbsolutePathBuf::try_from(tmp.path().join("sample-plugin")).unwrap(), plugin_id.clone(), + r#"{"name":"sample-plugin","version":"9.9.9"}"#, ) .unwrap(); From bb72e151e50cfd4f6282ab86418188af3c83b5df Mon Sep 17 00:00:00 2001 From: pakrym-oai Date: Thu, 18 Jun 2026 16:13:07 -0700 Subject: [PATCH 07/10] [codex] Remove child AGENTS.md prompt experiment (#28993) ## Why `child_agents_md` is a disabled, under-development experiment that adds a second model-visible explanation of hierarchical `AGENTS.md` behavior. Keeping it leaves unused prompt, configuration, documentation, and test surface. ## What changed - remove the `ChildAgentsMd` feature and `child_agents_md` config schema entry - remove the hierarchical prompt asset, export, and instruction injection - remove feature-specific tests and documentation - keep the generic unstable-feature warning coverage using `apply_patch_streaming_events` Normal project `AGENTS.md` discovery and composition are unchanged. ## Testing - `just test -p codex-features` - `just test -p codex-prompts` - `just test -p codex-core agents_md` - `just test -p codex-core unstable_features_warning` --- codex-rs/core/config.schema.json | 6 -- codex-rs/core/src/agents_md.rs | 9 -- codex-rs/core/src/agents_md_tests.rs | 96 ------------------ .../core/tests/suite/hierarchical_agents.rs | 97 ------------------- codex-rs/core/tests/suite/mod.rs | 1 - .../tests/suite/unstable_features_warning.rs | 10 +- codex-rs/features/src/lib.rs | 8 -- codex-rs/features/src/tests.rs | 9 +- codex-rs/prompts/src/agents.rs | 1 - codex-rs/prompts/src/lib.rs | 2 - .../prompts/templates/agents/hierarchical.md | 7 -- docs/agents_md.md | 4 - 12 files changed, 11 insertions(+), 239 deletions(-) delete mode 100644 codex-rs/core/tests/suite/hierarchical_agents.rs delete mode 100644 codex-rs/prompts/src/agents.rs delete mode 100644 codex-rs/prompts/templates/agents/hierarchical.md diff --git a/codex-rs/core/config.schema.json b/codex-rs/core/config.schema.json index 46220b5e9e..21c7975261 100644 --- a/codex-rs/core/config.schema.json +++ b/codex-rs/core/config.schema.json @@ -446,9 +446,6 @@ "browser_use_external": { "type": "boolean" }, - "child_agents_md": { - "type": "boolean" - }, "chronicle": { "type": "boolean" }, @@ -4689,9 +4686,6 @@ "browser_use_external": { "type": "boolean" }, - "child_agents_md": { - "type": "boolean" - }, "chronicle": { "type": "boolean" }, diff --git a/codex-rs/core/src/agents_md.rs b/codex-rs/core/src/agents_md.rs index 2465cb6d55..05a1a901fa 100644 --- a/codex-rs/core/src/agents_md.rs +++ b/codex-rs/core/src/agents_md.rs @@ -26,8 +26,6 @@ use codex_config::merge_toml_values; use codex_config::project_root_markers_from_config; use codex_exec_server::ExecutorFileSystem; use codex_extension_api::UserInstructions; -use codex_features::Feature; -use codex_prompts::HIERARCHICAL_AGENTS_MESSAGE; use codex_utils_absolute_path::AbsolutePathBuf; use codex_utils_path_uri::PathUri; use std::io; @@ -72,13 +70,6 @@ pub(crate) async fn load_project_instructions( } } - if config.features.enabled(Feature::ChildAgentsMd) { - loaded.entries.push(InstructionEntry { - contents: HIERARCHICAL_AGENTS_MESSAGE.to_string(), - provenance: InstructionProvenance::Internal, - }); - } - (!loaded.is_empty()).then_some(loaded) } diff --git a/codex-rs/core/src/agents_md_tests.rs b/codex-rs/core/src/agents_md_tests.rs index ab027112a9..0e1a670fc2 100644 --- a/codex-rs/core/src/agents_md_tests.rs +++ b/codex-rs/core/src/agents_md_tests.rs @@ -909,32 +909,6 @@ async fn secondary_environment_invalid_utf8_does_not_suppress_other_docs() { assert!(loaded.text().contains("secondary\u{FFFD}doc")); } -#[tokio::test] -async fn child_agents_guidance_is_appended_once_after_environment_groups() { - let primary = tempfile::tempdir().expect("primary tempdir"); - let secondary = tempfile::tempdir().expect("secondary tempdir"); - fs::write(primary.path().join("AGENTS.md"), "primary doc").unwrap(); - fs::write(secondary.path().join("AGENTS.md"), "secondary doc").unwrap(); - let mut config = make_config(&primary, /*limit*/ 4096, /*instructions*/ None).await; - config.features.enable(Feature::ChildAgentsMd).unwrap(); - let environments = resolved_local_environments([ - ("primary", config.cwd.clone()), - ("secondary", secondary.abs()), - ]); - - let loaded = load_project_instructions( - &config.config, - /*user_instructions*/ None, - &environments, - ) - .await - .expect("instructions expected"); - let text = loaded.text(); - - assert_eq!(text.matches(HIERARCHICAL_AGENTS_MESSAGE).count(), 1); - assert!(text.ends_with(HIERARCHICAL_AGENTS_MESSAGE)); -} - /// If there are existing system instructions but AGENTS.md docs are /// missing we expect the original instructions to be returned unchanged. #[tokio::test] @@ -1098,32 +1072,6 @@ async fn agents_md_paths_preserve_symlinked_cwd() { assert_eq!(res, "project doc"); } -#[tokio::test] -async fn child_agents_message_after_global_instructions_uses_plain_separator() { - let tmp = tempfile::tempdir().expect("tempdir"); - let mut cfg = make_config(&tmp, /*limit*/ 4096, Some("global doc")).await; - cfg.features.enable(Feature::ChildAgentsMd).unwrap(); - - let loaded = load_agents_md(&cfg).await.expect("instructions expected"); - let global_agents = cfg.codex_home.join(DEFAULT_AGENTS_MD_FILENAME); - let expected = LoadedAgentsMd { - user_instructions: Some(UserInstructions { - text: "global doc".to_string(), - source: global_agents, - }), - entries: vec![InstructionEntry { - contents: HIERARCHICAL_AGENTS_MESSAGE.to_string(), - provenance: InstructionProvenance::Internal, - }], - }; - - assert_eq!(loaded, expected); - assert_eq!( - loaded.text(), - format!("global doc\n\n{HIERARCHICAL_AGENTS_MESSAGE}") - ); -} - #[tokio::test] async fn instruction_sources_include_global_before_agents_md_docs() { let tmp = tempfile::tempdir().expect("tempdir"); @@ -1162,50 +1110,6 @@ async fn instruction_sources_include_global_before_agents_md_docs() { ); } -#[tokio::test] -async fn child_agents_message_after_project_docs_is_not_an_instruction_source() { - let tmp = tempfile::tempdir().expect("tempdir"); - fs::write(tmp.path().join("AGENTS.md"), "project doc").unwrap(); - - let mut cfg = make_config(&tmp, /*limit*/ 4096, Some("global doc")).await; - cfg.features.enable(Feature::ChildAgentsMd).unwrap(); - let global_agents = cfg.codex_home.join(DEFAULT_AGENTS_MD_FILENAME); - fs::create_dir_all(&cfg.codex_home).unwrap(); - fs::write(&global_agents, "global doc").unwrap(); - - let loaded = load_agents_md(&cfg).await.expect("instructions expected"); - let project_agents = cfg.cwd.join("AGENTS.md"); - - let expected = LoadedAgentsMd { - user_instructions: Some(UserInstructions { - text: "global doc".to_string(), - source: global_agents.clone(), - }), - entries: vec![ - InstructionEntry { - contents: "project doc".to_string(), - provenance: project_provenance(project_agents.clone(), cfg.cwd.clone()), - }, - InstructionEntry { - contents: HIERARCHICAL_AGENTS_MESSAGE.to_string(), - provenance: InstructionProvenance::Internal, - }, - ], - }; - assert_eq!(loaded, expected); - assert_eq!( - loaded.sources().collect::>(), - vec![ - PathUri::from_abs_path(&global_agents), - PathUri::from_abs_path(&project_agents), - ] - ); - assert_eq!( - loaded.text(), - format!("global doc{AGENTS_MD_SEPARATOR}project doc\n\n{HIERARCHICAL_AGENTS_MESSAGE}") - ); -} - /// AGENTS.override.md is preferred over AGENTS.md when both are present. #[tokio::test] async fn agents_local_md_preferred() { diff --git a/codex-rs/core/tests/suite/hierarchical_agents.rs b/codex-rs/core/tests/suite/hierarchical_agents.rs deleted file mode 100644 index c8c5da94b7..0000000000 --- a/codex-rs/core/tests/suite/hierarchical_agents.rs +++ /dev/null @@ -1,97 +0,0 @@ -use codex_features::Feature; -use codex_utils_path_uri::PathUri; -use core_test_support::responses::ev_completed; -use core_test_support::responses::ev_response_created; -use core_test_support::responses::mount_sse_once; -use core_test_support::responses::sse; -use core_test_support::responses::start_mock_server; -use core_test_support::test_codex::test_codex; - -const HIERARCHICAL_AGENTS_SNIPPET: &str = - "Files called AGENTS.md commonly appear in many places inside a container"; - -#[tokio::test(flavor = "multi_thread", worker_threads = 2)] -async fn hierarchical_agents_appends_to_project_doc_in_user_instructions() { - let server = start_mock_server().await; - let resp_mock = mount_sse_once( - &server, - sse(vec![ev_response_created("resp1"), ev_completed("resp1")]), - ) - .await; - - let mut builder = test_codex() - .with_config(|config| { - config - .features - .enable(Feature::ChildAgentsMd) - .expect("test config should allow feature update"); - }) - .with_workspace_setup(|cwd, fs| async move { - let agents_md = cwd.join("AGENTS.md"); - let agents_md_uri = PathUri::from_path(&agents_md)?; - fs.write_file(&agents_md_uri, b"be nice".to_vec(), /*sandbox*/ None) - .await?; - Ok::<(), anyhow::Error>(()) - }); - let test = builder - .build_with_remote_env(&server) - .await - .expect("build test codex"); - - test.submit_turn("hello").await.expect("submit turn"); - - let request = resp_mock.single_request(); - let user_messages = request.message_input_texts("user"); - let instructions = user_messages - .iter() - .find(|text| text.starts_with("# AGENTS.md instructions")) - .expect("instructions message"); - assert!( - instructions.contains("be nice"), - "expected AGENTS.md text included: {instructions}" - ); - let snippet_pos = instructions - .find(HIERARCHICAL_AGENTS_SNIPPET) - .expect("expected hierarchical agents snippet"); - let base_pos = instructions - .find("be nice") - .expect("expected AGENTS.md text"); - assert!( - snippet_pos > base_pos, - "expected hierarchical agents message appended after base instructions: {instructions}" - ); -} - -#[tokio::test(flavor = "multi_thread", worker_threads = 2)] -async fn hierarchical_agents_emits_when_no_project_doc() { - let server = start_mock_server().await; - let resp_mock = mount_sse_once( - &server, - sse(vec![ev_response_created("resp1"), ev_completed("resp1")]), - ) - .await; - - let mut builder = test_codex().with_config(|config| { - config - .features - .enable(Feature::ChildAgentsMd) - .expect("test config should allow feature update"); - }); - let test = builder - .build_with_remote_env(&server) - .await - .expect("build test codex"); - - test.submit_turn("hello").await.expect("submit turn"); - - let request = resp_mock.single_request(); - let user_messages = request.message_input_texts("user"); - let instructions = user_messages - .iter() - .find(|text| text.starts_with("# AGENTS.md instructions")) - .expect("instructions message"); - assert!( - instructions.contains(HIERARCHICAL_AGENTS_SNIPPET), - "expected hierarchical agents message appended: {instructions}" - ); -} diff --git a/codex-rs/core/tests/suite/mod.rs b/codex-rs/core/tests/suite/mod.rs index 1e200ab486..f6e84f5ca2 100644 --- a/codex-rs/core/tests/suite/mod.rs +++ b/codex-rs/core/tests/suite/mod.rs @@ -57,7 +57,6 @@ mod extension_sandbox; mod fork_thread; #[cfg(not(target_os = "windows"))] mod guardian_review; -mod hierarchical_agents; #[cfg(not(target_os = "windows"))] mod hooks; #[cfg(not(target_os = "windows"))] diff --git a/codex-rs/core/tests/suite/unstable_features_warning.rs b/codex-rs/core/tests/suite/unstable_features_warning.rs index 783acc638b..a6612a20c0 100644 --- a/codex-rs/core/tests/suite/unstable_features_warning.rs +++ b/codex-rs/core/tests/suite/unstable_features_warning.rs @@ -21,14 +21,14 @@ async fn emits_warning_when_unstable_features_enabled_via_config() { let mut config = load_default_config_for_test(&home).await; config .features - .enable(Feature::ChildAgentsMd) + .enable(Feature::ApplyPatchStreamingEvents) .expect("test config should allow feature update"); let user_config_path = AbsolutePathBuf::from_absolute_path(config.codex_home.join(CONFIG_TOML_FILE)) .expect("absolute user config path"); config.config_layer_stack = config.config_layer_stack.with_user_config( &user_config_path, - toml! { features = { child_agents_md = true } }.into(), + toml! { features = { apply_patch_streaming_events = true } }.into(), ); let thread_manager = codex_core::test_support::thread_manager_with_models_provider( @@ -56,7 +56,7 @@ async fn emits_warning_when_unstable_features_enabled_via_config() { let EventMsg::Warning(WarningEvent { message }) = warning else { panic!("expected warning event"); }; - assert!(message.contains("child_agents_md")); + assert!(message.contains("apply_patch_streaming_events")); assert!(message.contains("Under-development features enabled")); assert!(message.contains("suppress_unstable_features_warning = true")); } @@ -67,7 +67,7 @@ async fn suppresses_warning_when_configured() { let mut config = load_default_config_for_test(&home).await; config .features - .enable(Feature::ChildAgentsMd) + .enable(Feature::ApplyPatchStreamingEvents) .expect("test config should allow feature update"); config.suppress_unstable_features_warning = true; let user_config_path = @@ -75,7 +75,7 @@ async fn suppresses_warning_when_configured() { .expect("absolute user config path"); config.config_layer_stack = config.config_layer_stack.with_user_config( &user_config_path, - toml! { features = { child_agents_md = true } }.into(), + toml! { features = { apply_patch_streaming_events = true } }.into(), ); let thread_manager = codex_core::test_support::thread_manager_with_models_provider( diff --git a/codex-rs/features/src/lib.rs b/codex-rs/features/src/lib.rs index 56acb213e5..aa208d1ba0 100644 --- a/codex-rs/features/src/lib.rs +++ b/codex-rs/features/src/lib.rs @@ -132,8 +132,6 @@ pub enum Feature { LocalThreadStoreCompression, /// Enable the Chronicle sidecar for passive screen-context memories. Chronicle, - /// Append additional AGENTS.md guidance to user instructions. - ChildAgentsMd, /// Compress request bodies (zstd) when sending streaming requests to codex-backend. EnableRequestCompression, /// Start the managed network proxy for sandboxed sessions. @@ -910,12 +908,6 @@ pub const FEATURES: &[FeatureSpec] = &[ stage: Stage::UnderDevelopment, default_enabled: false, }, - FeatureSpec { - id: Feature::ChildAgentsMd, - key: "child_agents_md", - stage: Stage::UnderDevelopment, - default_enabled: false, - }, FeatureSpec { id: Feature::ApplyPatchFreeform, key: "apply_patch_freeform", diff --git a/codex-rs/features/src/tests.rs b/codex-rs/features/src/tests.rs index 2e9d3203c0..7ee46f4ba2 100644 --- a/codex-rs/features/src/tests.rs +++ b/codex-rs/features/src/tests.rs @@ -707,12 +707,15 @@ fn materialize_resolved_enabled_writes_all_features_and_preserves_custom_config( #[test] fn unstable_warning_event_only_mentions_enabled_under_development_features() { let mut configured_features = Table::new(); - configured_features.insert("child_agents_md".to_string(), TomlValue::Boolean(true)); + configured_features.insert( + "apply_patch_streaming_events".to_string(), + TomlValue::Boolean(true), + ); configured_features.insert("personality".to_string(), TomlValue::Boolean(true)); configured_features.insert("unknown".to_string(), TomlValue::Boolean(true)); let mut features = Features::with_defaults(); - features.enable(Feature::ChildAgentsMd); + features.enable(Feature::ApplyPatchStreamingEvents); let warning = unstable_features_warning_event( Some(&configured_features), @@ -725,7 +728,7 @@ fn unstable_warning_event_only_mentions_enabled_under_development_features() { let EventMsg::Warning(WarningEvent { message }) = warning.msg else { panic!("expected warning event"); }; - assert!(message.contains("child_agents_md")); + assert!(message.contains("apply_patch_streaming_events")); assert!(!message.contains("personality")); assert!(message.contains("/tmp/config.toml")); } diff --git a/codex-rs/prompts/src/agents.rs b/codex-rs/prompts/src/agents.rs deleted file mode 100644 index e716d5bf94..0000000000 --- a/codex-rs/prompts/src/agents.rs +++ /dev/null @@ -1 +0,0 @@ -pub const HIERARCHICAL_AGENTS_MESSAGE: &str = include_str!("../templates/agents/hierarchical.md"); diff --git a/codex-rs/prompts/src/lib.rs b/codex-rs/prompts/src/lib.rs index 8830e445d4..5a30358393 100644 --- a/codex-rs/prompts/src/lib.rs +++ b/codex-rs/prompts/src/lib.rs @@ -1,4 +1,3 @@ -mod agents; mod apply_patch; mod compact; mod goals; @@ -7,7 +6,6 @@ mod realtime; mod review_exit; mod review_request; -pub use agents::HIERARCHICAL_AGENTS_MESSAGE; pub use apply_patch::APPLY_PATCH_TOOL_INSTRUCTIONS; pub use compact::SUMMARIZATION_PROMPT; pub use compact::SUMMARY_PREFIX; diff --git a/codex-rs/prompts/templates/agents/hierarchical.md b/codex-rs/prompts/templates/agents/hierarchical.md deleted file mode 100644 index 4f782078c8..0000000000 --- a/codex-rs/prompts/templates/agents/hierarchical.md +++ /dev/null @@ -1,7 +0,0 @@ -Files called AGENTS.md commonly appear in many places inside a container - at "/", in "~", deep within git repositories, or in any other directory; their location is not limited to version-controlled folders. - -Their purpose is to pass along human guidance to you, the agent. Such guidance can include coding standards, explanations of the project layout, steps for building or testing, and even wording that must accompany a GitHub pull-request description produced by the agent; all of it is to be followed. - -Each AGENTS.md governs the entire directory that contains it and every child directory beneath that point. Whenever you change a file, you have to comply with every AGENTS.md whose scope covers that file. Naming conventions, stylistic rules and similar directives are restricted to the code that falls inside that scope unless the document explicitly states otherwise. - -When two AGENTS.md files disagree, the one located deeper in the directory structure overrides the higher-level file, while instructions given directly in the prompt by the system, developer, or user outrank any AGENTS.md content. diff --git a/docs/agents_md.md b/docs/agents_md.md index 3df0facdf6..4fa02abd1d 100644 --- a/docs/agents_md.md +++ b/docs/agents_md.md @@ -1,7 +1,3 @@ # AGENTS.md For information about AGENTS.md, see [this documentation](https://developers.openai.com/codex/guides/agents-md). - -## Hierarchical agents message - -When the `child_agents_md` feature flag is enabled (via `[features]` in `config.toml`), Codex appends additional guidance about AGENTS.md scope and precedence to the user instructions message and emits that message even when no AGENTS.md is present. From 195c936fa2cef6b5c88d74f90d5f83840058ca27 Mon Sep 17 00:00:00 2001 From: "Adam Perry @ OpenAI" Date: Thu, 18 Jun 2026 16:16:19 -0700 Subject: [PATCH 08/10] core: log AGENTS.md paths as URIs (#28989) ## Why No need to do path contortions when it's for our own logs. ## What Follow up on a previous PR's nit and update the path-types skill for future reference. --- .codex/skills/path-types/SKILL.md | 1 + codex-rs/core/src/agents_md.rs | 6 +++--- 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/.codex/skills/path-types/SKILL.md b/.codex/skills/path-types/SKILL.md index 0062730013..87be423d58 100644 --- a/.codex/skills/path-types/SKILL.md +++ b/.codex/skills/path-types/SKILL.md @@ -34,6 +34,7 @@ Keep these requirements in mind while migrating code to conform with the above g * URIs should not yet be stored in rollouts, databases, or other persistent storage * path conversion errors: fail-closed for security-relevant paths, fail-open for UI/diagnostics * prefer small focused methods on `PathUri` or `LegacyAppPathString` over local helpers +* represent `PathUri` values as URIs in diagnostics It is OK if the conversion between paths and URIs is somewhat lossy as long as it will do the right thing for real users. diff --git a/codex-rs/core/src/agents_md.rs b/codex-rs/core/src/agents_md.rs index 05a1a901fa..d0dc7026a9 100644 --- a/codex-rs/core/src/agents_md.rs +++ b/codex-rs/core/src/agents_md.rs @@ -123,9 +123,9 @@ async fn read_agents_md( if size > remaining { tracing::warn!( - "Project doc `{}` exceeds remaining budget ({} bytes) - truncating.", - p.inferred_native_path_string(), - remaining, + path = %p, + remaining_bytes = remaining, + "project doc exceeds remaining budget; truncating" ); } From 346d2c163ff9191a37dc5f198ccd3cdd731430cc Mon Sep 17 00:00:00 2001 From: "Adam Perry @ OpenAI" Date: Thu, 18 Jun 2026 16:39:21 -0700 Subject: [PATCH 09/10] core: keep remote exec on reported shell (#28983) ## Why We need to avoid resolving shells on the app-server's host for remote environments. We might make it possible to do fancier shell resolution from remote envs but for now just require the model to produce a shell that matches the environment's default. This gets my e2e demo working for shell commands after #28854 moved shell resolution to PathUri and caused remote envs to hit the fallback shell when the shell wasn't available on the host. ## What Remote `exec_command` calls now accept only the environment's reported default shell name or exact path, and execute with that reported path. Other explicit shells return a concise error. A Wine-backed integration test covers explicit PowerShell execution in the Windows cwd. --- .../handlers/unified_exec/exec_command.rs | 25 ++++++- codex-rs/core/tests/suite/remote_env.rs | 71 +++++++++++++++++++ 2 files changed, 94 insertions(+), 2 deletions(-) diff --git a/codex-rs/core/src/tools/handlers/unified_exec/exec_command.rs b/codex-rs/core/src/tools/handlers/unified_exec/exec_command.rs index 833f7a77b9..0e341f7256 100644 --- a/codex-rs/core/src/tools/handlers/unified_exec/exec_command.rs +++ b/codex-rs/core/src/tools/handlers/unified_exec/exec_command.rs @@ -1,3 +1,4 @@ +use std::path::Path; use std::sync::Arc; use crate::function_tool::FunctionCallError; @@ -31,6 +32,7 @@ use codex_otel::TOOL_CALL_UNIFIED_EXEC_METRIC; use codex_sandboxing::SandboxManager; use codex_sandboxing::SandboxType; use codex_sandboxing::SandboxablePreference; +use codex_shell_command::shell_detect::detect_shell_type; use codex_tools::ToolName; use codex_tools::ToolSpec; use codex_utils_output_truncation::approx_token_count; @@ -172,7 +174,7 @@ impl ExecCommandHandler { ))); } }; - let args: ExecCommandArgs = match native_cwd.as_ref() { + let mut args: ExecCommandArgs = match native_cwd.as_ref() { Some(native_cwd) => { // The base path only resolves paths nested in the permissions config types. parse_arguments_with_base_path(&arguments, native_cwd)? @@ -195,7 +197,6 @@ impl ExecCommandHandler { ) .await; } - let process_id = manager.allocate_process_id().await; let shell_mode = shell_mode_for_environment(&turn.unified_exec_shell_mode, environment.as_ref()); // Remote environments may use a different OS and must build commands with their native @@ -205,6 +206,26 @@ impl ExecCommandHandler { .clone() .map(Arc::new) .unwrap_or_else(|| session.user_shell()); + // TODO(anp): Resolve requested shells in remote environments instead of restricting + // commands to the reported default shell. + if environment.is_remote() + && let Some(requested_shell) = args.shell.take() + { + let Some(remote_shell) = turn_environment.shell.as_ref() else { + return Err(FunctionCallError::RespondToModel(format!( + "environment `{}` does not report a shell", + turn_environment.environment_id + ))); + }; + if detect_shell_type(Path::new(&requested_shell)) != Some(remote_shell.shell_type) { + return Err(FunctionCallError::RespondToModel(format!( + "environment `{}` only supports `{}`", + turn_environment.environment_id, + remote_shell.name() + ))); + } + } + let process_id = manager.allocate_process_id().await; let resolved_command = get_command( &args, shell, diff --git a/codex-rs/core/tests/suite/remote_env.rs b/codex-rs/core/tests/suite/remote_env.rs index 311485c112..63bb5deeda 100644 --- a/codex-rs/core/tests/suite/remote_env.rs +++ b/codex-rs/core/tests/suite/remote_env.rs @@ -228,6 +228,77 @@ async fn remote_test_env_exposes_target_shell_to_model() -> Result<()> { Ok(()) } +#[tokio::test(flavor = "multi_thread", worker_threads = 2)] +async fn explicit_remote_shell_runs_in_remote_cwd() -> Result<()> { + const CALL_ID: &str = "remote-explicit-shell"; + + let (shell, command) = match core_test_support::test_environment() { + TestEnvironment::Docker { .. } => ( + "bash", + r#"case "$PWD" in /tmp/codex-core-test-cwd-*) ;; *) echo "unexpected cwd: $PWD" >&2; exit 1 ;; esac"#, + ), + TestEnvironment::WineExec => ( + "powershell", + r#"$cwd = (Get-Location).Path; if ($cwd -notlike 'C:\codex-core-test-cwd-*') { Write-Error "unexpected cwd: $cwd"; exit 1 }"#, + ), + TestEnvironment::Local => return Ok(()), + }; + + let server = start_mock_server().await; + let arguments = serde_json::to_string(&json!({ + "cmd": command, + "shell": shell, + "login": false, + "yield_time_ms": 10_000, + }))?; + let mut builder = test_codex().with_config(|config| { + config.use_experimental_unified_exec_tool = true; + config + .features + .enable(Feature::UnifiedExec) + .expect("test config should allow feature update"); + }); + let test = builder.build_with_remote_env(&server).await?; + let response_mock = mount_sse_sequence( + &server, + vec![ + sse(vec![ + ev_response_created("resp-1"), + ev_function_call(CALL_ID, "exec_command", &arguments), + ev_completed("resp-1"), + ]), + sse(vec![ + ev_response_created("resp-2"), + ev_assistant_message("msg-1", "done"), + ev_completed("resp-2"), + ]), + ], + ) + .await; + + test.submit_turn_with_environments( + "run the remote shell in the remote cwd", + Some(vec![TurnEnvironmentSelection { + environment_id: REMOTE_ENVIRONMENT_ID.to_string(), + cwd: PathUri::from_abs_path(&test.config.cwd), + }]), + ) + .await?; + let request = response_mock + .last_request() + .context("model should receive the command output")?; + let (output, success) = request + .function_call_output_content_and_success(CALL_ID) + .context("remote shell tool result should be present")?; + assert_ne!(success, Some(false)); + assert!( + output.is_some_and(|output| output.contains("Process exited with code 0")), + "remote shell command should exit successfully", + ); + + Ok(()) +} + fn absolute_path(path: PathBuf) -> AbsolutePathBuf { AbsolutePathBuf::try_from(path).expect("path should be absolute") } From e83b7841b0789d6f8b3252092d44e51a6e0e54ba Mon Sep 17 00:00:00 2001 From: xl-openai Date: Thu, 18 Jun 2026 16:45:58 -0700 Subject: [PATCH 10/10] [codex] Reuse parsed plugin skills during session startup (#28844) ## Summary - Preserve raw plugin skill-root snapshots in the matching loaded-plugin cache entry, keyed by the effective plugin root identity including namespace. - Pass those snapshots through `SkillsLoadInput` as an optional preload, so session startup reuses plugin parsing while ordinary skill loads pass `None`. - Keep plugin skill loading cohesive: the existing loaders accept the optional snapshots directly, and uncached or marketplace-detail paths do not create a cache. ## Why Plugin discovery already parses plugin skills to determine available capabilities. Cold session startup then scanned and parsed the same roots again while building the skills snapshot. This solves the same duplicate-work problem as #28623 while keeping ownership narrow: `PluginsManager` creates and owns `PluginSkillSnapshots` only for its loaded-plugin cache entry; `SkillsService` consumes an optional clone. Entry replacement or clearing naturally drops the snapshots, with no separate generation, capacity policy, or watcher coupling. ## Validation - `cargo clippy -p codex-core-skills --all-targets -- -D warnings` - `just test -p codex-core-plugins skills_service_reuses_skills_parsed_during_plugin_load` - `just test -p codex-core-skills namespaces_plugin_skills_using_provided_namespace` - `just fmt` --- codex-rs/app-server/src/skills_watcher.rs | 2 + codex-rs/core-plugins/src/loader.rs | 22 +- codex-rs/core-plugins/src/loader_tests.rs | 1 + codex-rs/core-plugins/src/manager.rs | 74 +++++-- codex-rs/core-plugins/src/manager_tests.rs | 61 +++++- .../core-plugins/src/tool_suggest_metadata.rs | 10 +- codex-rs/core-skills/src/lib.rs | 2 + codex-rs/core-skills/src/loader.rs | 108 ++++------ codex-rs/core-skills/src/loader_tests.rs | 194 ++++++++++-------- codex-rs/core-skills/src/root_loader.rs | 158 ++++++++++++++ codex-rs/core-skills/src/service.rs | 26 ++- codex-rs/core/src/agent/role_tests.rs | 4 +- codex-rs/core/src/session/session.rs | 4 +- codex-rs/core/src/session/tests.rs | 18 +- codex-rs/core/src/session/turn_context.rs | 10 +- codex-rs/ext/skills/src/provider/executor.rs | 19 +- .../tests/executor_file_system_authority.rs | 25 ++- 17 files changed, 527 insertions(+), 211 deletions(-) create mode 100644 codex-rs/core-skills/src/root_loader.rs diff --git a/codex-rs/app-server/src/skills_watcher.rs b/codex-rs/app-server/src/skills_watcher.rs index 086e076d4b..637fe963ea 100644 --- a/codex-rs/app-server/src/skills_watcher.rs +++ b/codex-rs/app-server/src/skills_watcher.rs @@ -114,6 +114,8 @@ impl SkillsWatcher { .skill_roots_for_config(&skills_input, Some(environment.get_filesystem())) .await .into_iter() + // Plugin roots are invalidated by plugin lifecycle operations. + .filter(|root| root.plugin_id.is_none()) .map(|root| WatchPath { path: root.path.into_path_buf(), recursive: true, diff --git a/codex-rs/core-plugins/src/loader.rs b/codex-rs/core-plugins/src/loader.rs index 279578d75e..54eb371686 100644 --- a/codex-rs/core-plugins/src/loader.rs +++ b/codex-rs/core-plugins/src/loader.rs @@ -21,6 +21,7 @@ use codex_config::HooksFile; use codex_config::types::McpServerConfig; use codex_config::types::PluginConfig; use codex_config::types::PluginMcpServerConfig; +use codex_core_skills::PluginSkillSnapshots; use codex_core_skills::SkillMetadata; use codex_core_skills::config_rules::SkillConfigRules; use codex_core_skills::config_rules::resolve_disabled_skill_paths; @@ -74,6 +75,7 @@ enum PluginLoadScope<'a> { AllCapabilities { restriction_product: Option, skill_config_rules: &'a SkillConfigRules, + plugin_skill_snapshots: Option<&'a PluginSkillSnapshots>, }, HooksOnly, } @@ -115,6 +117,7 @@ pub(crate) async fn load_plugins_from_layer_stack( config_layer_stack: &ConfigLayerStack, extra_plugins: HashMap, store: &PluginStore, + plugin_skill_snapshots: Option<&PluginSkillSnapshots>, restriction_product: Option, prefer_remote_curated_conflicts: bool, ) -> Vec> { @@ -127,6 +130,7 @@ pub(crate) async fn load_plugins_from_layer_stack( PluginLoadScope::AllCapabilities { restriction_product, skill_config_rules: &skill_config_rules, + plugin_skill_snapshots, }, ) .await @@ -745,6 +749,7 @@ async fn load_plugin( PluginLoadScope::AllCapabilities { restriction_product, skill_config_rules, + plugin_skill_snapshots, } => { loaded_plugin.manifest_name = Some(manifest.display_name().to_string()); loaded_plugin.manifest_description = manifest.description.clone(); @@ -755,6 +760,7 @@ async fn load_plugin( &manifest, *restriction_product, skill_config_rules, + *plugin_skill_snapshots, ) .await; let has_enabled_skills = resolved_skills.has_enabled_skills(); @@ -851,10 +857,17 @@ pub async fn load_plugin_skills( manifest: &PluginManifest, restriction_product: Option, skill_config_rules: &SkillConfigRules, + plugin_skill_snapshots: Option<&PluginSkillSnapshots>, ) -> ResolvedPluginSkills { - load_plugin_skill_inventory(plugin_root, plugin_id, manifest, restriction_product) - .await - .resolve(skill_config_rules) + load_plugin_skill_inventory( + plugin_root, + plugin_id, + manifest, + restriction_product, + plugin_skill_snapshots, + ) + .await + .resolve(skill_config_rules) } pub(crate) async fn load_plugin_skill_inventory( @@ -862,6 +875,7 @@ pub(crate) async fn load_plugin_skill_inventory( plugin_id: &PluginId, manifest: &PluginManifest, restriction_product: Option, + plugin_skill_snapshots: Option<&PluginSkillSnapshots>, ) -> PluginSkillInventory { let roots = plugin_skill_roots(plugin_root, &manifest.paths) .into_iter() @@ -874,7 +888,7 @@ pub(crate) async fn load_plugin_skill_inventory( plugin_root: Some(plugin_root.clone()), }) .collect::>(); - let outcome = load_skills_from_roots(roots).await; + let outcome = load_skills_from_roots(roots, plugin_skill_snapshots).await; let had_errors = !outcome.errors.is_empty(); let skills = outcome .skills diff --git a/codex-rs/core-plugins/src/loader_tests.rs b/codex-rs/core-plugins/src/loader_tests.rs index 133103b78a..ee22256fd1 100644 --- a/codex-rs/core-plugins/src/loader_tests.rs +++ b/codex-rs/core-plugins/src/loader_tests.rs @@ -160,6 +160,7 @@ enabled = true &stack, HashMap::new(), &store, + /*plugin_skill_snapshots*/ None, Some(Product::Codex), /*prefer_remote_curated_conflicts*/ false, ) diff --git a/codex-rs/core-plugins/src/manager.rs b/codex-rs/core-plugins/src/manager.rs index 713a356a03..9d6c096336 100644 --- a/codex-rs/core-plugins/src/manager.rs +++ b/codex-rs/core-plugins/src/manager.rs @@ -61,6 +61,7 @@ use codex_config::set_user_plugin_enabled; use codex_config::types::PluginConfig; use codex_config::types::ToolSuggestDisabledTool; use codex_config::types::ToolSuggestDiscoverableType; +use codex_core_skills::PluginSkillSnapshots; use codex_core_skills::SkillMetadata; use codex_core_skills::config_rules::SkillConfigRules; use codex_core_skills::config_rules::skill_config_rules_from_stack; @@ -370,6 +371,7 @@ pub struct PluginsManager { struct LoadedPluginsCacheEntry { key: PluginLoadCacheKey, plugins: Vec, + plugin_skill_snapshots: PluginSkillSnapshots, } #[derive(Default)] @@ -385,6 +387,16 @@ struct PluginLoadCacheKey { remote_plugin_enabled: bool, } +impl PluginLoadCacheKey { + fn from_config(config: &PluginsConfigInput) -> Self { + Self { + configured_plugins: configured_plugins_from_stack(&config.config_layer_stack), + skill_config_rules: skill_config_rules_from_stack(&config.config_layer_stack), + remote_plugin_enabled: config.remote_plugin_enabled, + } + } +} + impl PluginsManager { pub fn new(codex_home: PathBuf) -> Self { Self::new_with_options(codex_home, Some(Product::Codex), /*auth_mode*/ None) @@ -470,6 +482,24 @@ impl PluginsManager { .await } + /// Returns skill snapshots parsed while loading the matching plugin cache entry. + pub fn plugin_skill_snapshots_for_config( + &self, + config: &PluginsConfigInput, + ) -> Option { + if !config.plugins_enabled { + return None; + } + let key = PluginLoadCacheKey::from_config(config); + self.loaded_plugins_cache + .read() + .unwrap_or_else(std::sync::PoisonError::into_inner) + .entry + .as_ref() + .filter(|cached| cached.key == key) + .map(|cached| cached.plugin_skill_snapshots.clone()) + } + #[instrument( name = "plugins_for_config", level = "info", @@ -489,11 +519,7 @@ impl PluginsManager { return PluginLoadOutcome::default(); } - let cache_key = PluginLoadCacheKey { - configured_plugins: configured_plugins_from_stack(&config.config_layer_stack), - skill_config_rules: skill_config_rules_from_stack(&config.config_layer_stack), - remote_plugin_enabled: config.remote_plugin_enabled, - }; + let cache_key = PluginLoadCacheKey::from_config(config); if !force_reload && let Some(plugins) = self.cached_loaded_plugins(&cache_key) { return self.resolve_loaded_plugins_for_auth(plugins); } @@ -506,16 +532,23 @@ impl PluginsManager { return self.resolve_loaded_plugins_for_auth(plugins); } let cache_generation = self.loaded_plugins_cache_generation(); + let plugin_skill_snapshots = PluginSkillSnapshots::for_plugin_load(); let plugins = load_plugins_from_layer_stack( &config.config_layer_stack, self.remote_installed_plugin_configs(), &self.store, + Some(&plugin_skill_snapshots), self.restriction_product, config.remote_plugin_enabled, ) .await; log_plugin_load_errors(&plugins); - self.cache_loaded_plugins_if_current(cache_generation, cache_key, plugins.clone()); + self.cache_loaded_plugins_if_current( + cache_generation, + cache_key, + plugins.clone(), + plugin_skill_snapshots, + ); self.resolve_loaded_plugins_for_auth(plugins) } @@ -589,6 +622,7 @@ impl PluginsManager { config_layer_stack, self.remote_installed_plugin_configs(), &self.store, + /*plugin_skill_snapshots*/ None, self.restriction_product, config.remote_plugin_enabled, ) @@ -626,19 +660,13 @@ impl PluginsManager { } fn cached_loaded_plugins(&self, key: &PluginLoadCacheKey) -> Option> { - match self.loaded_plugins_cache.read() { - Ok(cache) => cache - .entry - .as_ref() - .filter(|cached| cached.key == *key) - .map(|cached| cached.plugins.clone()), - Err(err) => err - .into_inner() - .entry - .as_ref() - .filter(|cached| cached.key == *key) - .map(|cached| cached.plugins.clone()), - } + self.loaded_plugins_cache + .read() + .unwrap_or_else(std::sync::PoisonError::into_inner) + .entry + .as_ref() + .filter(|cached| cached.key == *key) + .map(|cached| cached.plugins.clone()) } fn loaded_plugins_cache_generation(&self) -> u64 { @@ -653,13 +681,18 @@ impl PluginsManager { generation: u64, key: PluginLoadCacheKey, plugins: Vec, + plugin_skill_snapshots: PluginSkillSnapshots, ) { let mut cache = match self.loaded_plugins_cache.write() { Ok(cache) => cache, Err(err) => err.into_inner(), }; if cache.generation == generation { - cache.entry = Some(LoadedPluginsCacheEntry { key, plugins }); + cache.entry = Some(LoadedPluginsCacheEntry { + key, + plugins, + plugin_skill_snapshots, + }); } } @@ -1659,6 +1692,7 @@ impl PluginsManager { &codex_core_skills::config_rules::skill_config_rules_from_stack( &config.config_layer_stack, ), + /*plugin_skill_snapshots*/ None, ) .await; let plugin_data_root = self.store.plugin_data_root(&plugin_id); diff --git a/codex-rs/core-plugins/src/manager_tests.rs b/codex-rs/core-plugins/src/manager_tests.rs index a4b30a1996..cc8ee19137 100644 --- a/codex-rs/core-plugins/src/manager_tests.rs +++ b/codex-rs/core-plugins/src/manager_tests.rs @@ -37,6 +37,9 @@ use codex_config::McpServerConfig; use codex_config::McpServerOAuthConfig; use codex_config::McpServerToolConfig; use codex_config::types::McpServerTransportConfig; +use codex_core_skills::PluginSkillSnapshots; +use codex_core_skills::SkillsLoadInput; +use codex_core_skills::SkillsService; use codex_core_skills::config_rules::SkillConfigRules; use codex_login::CodexAuth; use codex_plugin::AppDeclaration; @@ -1476,6 +1479,7 @@ async fn load_plugin_skills_dedupes_overlapping_manifest_roots() { &manifest, /*restriction_product*/ None, &SkillConfigRules::default(), + /*plugin_skill_snapshots*/ None, ) .await; @@ -2039,6 +2043,55 @@ async fn plugin_cache_ignores_unrelated_session_overrides() { assert_eq!(second.plugins()[0].mcp_servers.len(), 1); } +#[tokio::test] +async fn skills_service_reuses_skills_parsed_during_plugin_load() { + let codex_home = TempDir::new().unwrap(); + let codex_home_abs = codex_home.path().to_path_buf().abs(); + let plugin_root = codex_home + .path() + .join("plugins/cache") + .join("test/sample/local"); + write_plugin( + codex_home.path().join("plugins/cache/test").as_path(), + "sample/local", + "sample", + ); + let skill_path = plugin_root.join("skills/SKILL.md"); + write_file(&skill_path, "---\nname: search\ndescription: first\n---\n"); + write_file( + &codex_home.path().join(CONFIG_TOML_FILE), + &plugin_config_toml(/*enabled*/ true, /*plugins_feature_enabled*/ true), + ); + + let config = load_config(codex_home.path(), codex_home.path()).await; + let manager = PluginsManager::new(codex_home.path().to_path_buf()); + let plugin_outcome = manager.plugins_for_config(&config).await; + let plugin_skill_snapshots = manager.plugin_skill_snapshots_for_config(&config); + write_file(&skill_path, "---\nname: search\ndescription: second\n---\n"); + + let skills_input = SkillsLoadInput::new( + codex_home_abs.clone(), + plugin_outcome.effective_plugin_skill_roots(), + config.config_layer_stack.clone(), + /*bundled_skills_enabled*/ false, + ) + .with_plugin_skill_snapshots(plugin_skill_snapshots); + let skills_service = SkillsService::new(codex_home_abs, /*bundled_skills_enabled*/ false); + let cached = skills_service + .snapshot_for_config(&skills_input, /*fs*/ None) + .await; + + assert_eq!( + cached + .outcome() + .skills + .iter() + .map(|skill| skill.description.as_str()) + .collect::>(), + vec!["first"] + ); +} + #[test] fn loaded_plugins_cache_invalidation_rejects_stale_load_completion() { let codex_home = TempDir::new().unwrap(); @@ -2051,7 +2104,12 @@ fn loaded_plugins_cache_invalidation_rejects_stale_load_completion() { let stale_generation = manager.loaded_plugins_cache_generation(); manager.clear_loaded_plugins_cache(); - manager.cache_loaded_plugins_if_current(stale_generation, cache_key.clone(), Vec::new()); + manager.cache_loaded_plugins_if_current( + stale_generation, + cache_key.clone(), + Vec::new(), + PluginSkillSnapshots::for_plugin_load(), + ); assert_eq!(manager.cached_loaded_plugins(&cache_key), None); } @@ -5164,6 +5222,7 @@ async fn load_plugins_ignores_project_config_files() { &stack, std::collections::HashMap::new(), &PluginStore::new(codex_home.path().to_path_buf()), + /*plugin_skill_snapshots*/ None, Some(Product::Codex), /*prefer_remote_curated_conflicts*/ false, ) diff --git a/codex-rs/core-plugins/src/tool_suggest_metadata.rs b/codex-rs/core-plugins/src/tool_suggest_metadata.rs index 2a667405e8..310bec8654 100644 --- a/codex-rs/core-plugins/src/tool_suggest_metadata.rs +++ b/codex-rs/core-plugins/src/tool_suggest_metadata.rs @@ -210,8 +210,14 @@ async fn load_plugin_metadata( } let manifest = load_plugin_manifest(plugin_root.as_path()) .ok_or_else(|| "missing or invalid plugin.json".to_string())?; - let skill_inventory = - load_plugin_skill_inventory(plugin_root, &plugin_id, &manifest, restriction_product).await; + let skill_inventory = load_plugin_skill_inventory( + plugin_root, + &plugin_id, + &manifest, + restriction_product, + /*plugin_skill_snapshots*/ None, + ) + .await; let mut mcp_server_names = load_plugin_mcp_servers(plugin_root.as_path(), /*auth_mode*/ None) .await diff --git a/codex-rs/core-skills/src/lib.rs b/codex-rs/core-skills/src/lib.rs index 31e5a7c531..53ffcddd07 100644 --- a/codex-rs/core-skills/src/lib.rs +++ b/codex-rs/core-skills/src/lib.rs @@ -6,6 +6,7 @@ mod mention_counts; pub mod model; pub mod remote; pub mod render; +mod root_loader; pub mod service; mod skill_instructions; pub mod system; @@ -29,6 +30,7 @@ pub use render::SkillRenderReport; pub use render::build_available_skills; pub use render::default_skill_metadata_budget; pub use render::render_available_skills_body; +pub use root_loader::PluginSkillSnapshots; pub use service::SkillsLoadInput; pub use service::SkillsService; pub use skill_instructions::SkillInstructions; diff --git a/codex-rs/core-skills/src/loader.rs b/codex-rs/core-skills/src/loader.rs index 64861c45eb..2a3a3189be 100644 --- a/codex-rs/core-skills/src/loader.rs +++ b/codex-rs/core-skills/src/loader.rs @@ -1,6 +1,5 @@ use crate::model::SkillDependencies; use crate::model::SkillError; -use crate::model::SkillFileSystemsByPath; use crate::model::SkillInterface; use crate::model::SkillLoadOutcome; use crate::model::SkillMetadata; @@ -24,7 +23,6 @@ use codex_utils_plugins::PluginSkillRoot; use codex_utils_plugins::plugin_namespace_for_skill_path; use dirs::home_dir; use serde::Deserialize; -use std::collections::HashMap; use std::collections::HashSet; use std::collections::VecDeque; use std::error::Error; @@ -161,77 +159,51 @@ pub struct SkillRoot { pub plugin_root: Option, } -pub async fn load_skills_from_roots(roots: I) -> SkillLoadOutcome +pub async fn load_skills_from_roots( + roots: I, + plugin_skill_snapshots: Option<&crate::PluginSkillSnapshots>, +) -> SkillLoadOutcome where I: IntoIterator, { + crate::root_loader::load_and_merge_skill_roots(roots, plugin_skill_snapshots).await +} + +#[derive(Clone)] +pub(crate) struct SkillRootSnapshot { + pub(crate) root: AbsolutePathBuf, + pub(crate) skills: Vec, + pub(crate) errors: Vec, + pub(crate) file_system: Arc, +} + +pub(crate) async fn load_skill_root(root: SkillRoot) -> SkillRootSnapshot { + let SkillRoot { + path, + scope, + file_system, + plugin_id, + plugin_namespace, + plugin_root, + } = root; + let root = canonicalize_for_skill_identity(file_system.as_ref(), &path).await; let mut outcome = SkillLoadOutcome::default(); - let mut skill_roots: Vec = Vec::new(); - let mut skill_root_by_path: HashMap = HashMap::new(); - let mut file_systems_by_skill_path: HashMap> = - HashMap::new(); - for root in roots { - let fs = root.file_system; - let root_path = canonicalize_for_skill_identity(fs.as_ref(), &root.path).await; - let skills_before_root = outcome.skills.len(); - discover_skills_under_root( - fs.as_ref(), - &root_path, - root.scope, - root.plugin_id.as_deref(), - root.plugin_namespace.as_deref(), - root.plugin_root.as_ref(), - &mut outcome, - ) - .await; - for skill in &outcome.skills[skills_before_root..] { - if !skill_roots.contains(&root_path) { - skill_roots.push(root_path.clone()); - } - skill_root_by_path - .entry(skill.path_to_skills_md.clone()) - .or_insert_with(|| root_path.clone()); - file_systems_by_skill_path - .entry(skill.path_to_skills_md.clone()) - .or_insert_with(|| Arc::clone(&fs)); - } + discover_skills_under_root( + file_system.as_ref(), + &root, + scope, + plugin_id.as_deref(), + plugin_namespace.as_deref(), + plugin_root.as_ref(), + &mut outcome, + ) + .await; + SkillRootSnapshot { + root, + skills: outcome.skills, + errors: outcome.errors, + file_system, } - - let mut seen: HashSet = HashSet::new(); - outcome - .skills - .retain(|skill| seen.insert(skill.path_to_skills_md.clone())); - let retained_skill_paths: HashSet = outcome - .skills - .iter() - .map(|skill| skill.path_to_skills_md.clone()) - .collect(); - skill_root_by_path.retain(|path, _| retained_skill_paths.contains(path)); - let used_roots: HashSet = skill_root_by_path.values().cloned().collect(); - skill_roots.retain(|root| used_roots.contains(root)); - file_systems_by_skill_path.retain(|path, _| retained_skill_paths.contains(path)); - outcome.skill_roots = skill_roots; - outcome.skill_root_by_path = Arc::new(skill_root_by_path); - outcome.file_systems_by_skill_path = SkillFileSystemsByPath::new(file_systems_by_skill_path); - - fn scope_rank(scope: SkillScope) -> u8 { - // Higher-priority scopes first (matches root scan order for dedupe). - match scope { - SkillScope::Repo => 0, - SkillScope::User => 1, - SkillScope::System => 2, - SkillScope::Admin => 3, - } - } - - outcome.skills.sort_by(|a, b| { - scope_rank(a.scope) - .cmp(&scope_rank(b.scope)) - .then_with(|| a.name.cmp(&b.name)) - .then_with(|| a.path_to_skills_md.cmp(&b.path_to_skills_md)) - }); - - outcome } pub(crate) async fn skill_roots( diff --git a/codex-rs/core-skills/src/loader_tests.rs b/codex-rs/core-skills/src/loader_tests.rs index 3a6ae9b1c4..e22ed33f3d 100644 --- a/codex-rs/core-skills/src/loader_tests.rs +++ b/codex-rs/core-skills/src/loader_tests.rs @@ -128,6 +128,7 @@ async fn load_skills_for_test(config: &TestConfig) -> SkillLoadOutcome { /*home_dir*/ None, ) .await, + /*plugin_skill_snapshots*/ None, ) .await } @@ -315,7 +316,7 @@ async fn loads_skills_from_home_agents_dir_for_user_scope() -> anyhow::Result<() Some(&home_folder_abs), ) .await; - let outcome = load_skills_from_roots(roots).await; + let outcome = load_skills_from_roots(roots, /*plugin_skill_snapshots*/ None).await; assert!( outcome.errors.is_empty(), "unexpected errors: {:?}", @@ -845,14 +846,17 @@ interface: ); let plugin_root_abs = plugin_root.abs(); - let outcome = load_skills_from_roots([SkillRoot { - path: plugin_root.join("skills").abs(), - scope: SkillScope::User, - file_system: Arc::clone(&LOCAL_FS), - plugin_id: Some("twilio-developer-kit@test".to_string()), - plugin_namespace: None, - plugin_root: Some(plugin_root_abs.clone()), - }]) + let outcome = load_skills_from_roots( + [SkillRoot { + path: plugin_root.join("skills").abs(), + scope: SkillScope::User, + file_system: Arc::clone(&LOCAL_FS), + plugin_id: Some("twilio-developer-kit@test".to_string()), + plugin_namespace: None, + plugin_root: Some(plugin_root_abs.clone()), + }], + /*plugin_skill_snapshots*/ None, + ) .await; assert!( @@ -903,14 +907,17 @@ interface: "##, ); - let outcome = load_skills_from_roots([SkillRoot { - path: plugin_root.join("skills").abs(), - scope: SkillScope::User, - file_system: Arc::clone(&LOCAL_FS), - plugin_id: Some("twilio-developer-kit@test".to_string()), - plugin_namespace: None, - plugin_root: Some(plugin_root.abs()), - }]) + let outcome = load_skills_from_roots( + [SkillRoot { + path: plugin_root.join("skills").abs(), + scope: SkillScope::User, + file_system: Arc::clone(&LOCAL_FS), + plugin_id: Some("twilio-developer-kit@test".to_string()), + plugin_namespace: None, + plugin_root: Some(plugin_root.abs()), + }], + /*plugin_skill_snapshots*/ None, + ) .await; assert!( @@ -1050,14 +1057,17 @@ async fn loads_skills_via_symlinked_subdir_for_admin_scope() { fs::create_dir_all(admin_root.path()).unwrap(); symlink_dir(shared.path(), &admin_root.path().join("shared")); - let outcome = load_skills_from_roots([SkillRoot { - path: admin_root.path().abs(), - scope: SkillScope::Admin, - file_system: Arc::clone(&LOCAL_FS), - plugin_id: None, - plugin_namespace: None, - plugin_root: None, - }]) + let outcome = load_skills_from_roots( + [SkillRoot { + path: admin_root.path().abs(), + scope: SkillScope::Admin, + file_system: Arc::clone(&LOCAL_FS), + plugin_id: None, + plugin_namespace: None, + plugin_root: None, + }], + /*plugin_skill_snapshots*/ None, + ) .await; assert!( @@ -1133,14 +1143,17 @@ async fn system_scope_ignores_symlinked_subdir() { fs::create_dir_all(&system_root).unwrap(); symlink_dir(shared.path(), &system_root.join("shared")); - let outcome = load_skills_from_roots([SkillRoot { - path: system_root.abs(), - scope: SkillScope::System, - file_system: Arc::clone(&LOCAL_FS), - plugin_id: None, - plugin_namespace: None, - plugin_root: None, - }]) + let outcome = load_skills_from_roots( + [SkillRoot { + path: system_root.abs(), + scope: SkillScope::System, + file_system: Arc::clone(&LOCAL_FS), + plugin_id: None, + plugin_namespace: None, + plugin_root: None, + }], + /*plugin_skill_snapshots*/ None, + ) .await; assert!( outcome.errors.is_empty(), @@ -1168,14 +1181,17 @@ async fn respects_max_scan_depth_for_user_scope() { ); let skills_root = codex_home.path().join("skills"); - let outcome = load_skills_from_roots([SkillRoot { - path: skills_root.abs(), - scope: SkillScope::User, - file_system: Arc::clone(&LOCAL_FS), - plugin_id: None, - plugin_namespace: None, - plugin_root: None, - }]) + let outcome = load_skills_from_roots( + [SkillRoot { + path: skills_root.abs(), + scope: SkillScope::User, + file_system: Arc::clone(&LOCAL_FS), + plugin_id: None, + plugin_namespace: None, + plugin_root: None, + }], + /*plugin_skill_snapshots*/ None, + ) .await; assert!( @@ -1276,14 +1292,17 @@ async fn namespaces_plugin_skills_using_provided_namespace() { ) .unwrap(); - let outcome = load_skills_from_roots([SkillRoot { - path: plugin_root.join("skills").abs(), - scope: SkillScope::User, - file_system: Arc::clone(&LOCAL_FS), - plugin_id: Some("sample@test".to_string()), - plugin_namespace: Some("sample".to_string()), - plugin_root: Some(plugin_root.abs()), - }]) + let outcome = load_skills_from_roots( + [SkillRoot { + path: plugin_root.join("skills").abs(), + scope: SkillScope::User, + file_system: Arc::clone(&LOCAL_FS), + plugin_id: Some("sample@test".to_string()), + plugin_namespace: Some("sample".to_string()), + plugin_root: Some(plugin_root.abs()), + }], + /*plugin_skill_snapshots*/ None, + ) .await; assert!( @@ -1322,14 +1341,17 @@ async fn plugin_skill_name_length_limit_allows_max_qualified_name() { ) .unwrap(); - let outcome = load_skills_from_roots([SkillRoot { - path: plugin_root.join("skills").abs(), - scope: SkillScope::User, - file_system: Arc::clone(&LOCAL_FS), - plugin_id: Some("sample@test".to_string()), - plugin_namespace: Some(plugin_name.clone()), - plugin_root: Some(plugin_root.abs()), - }]) + let outcome = load_skills_from_roots( + [SkillRoot { + path: plugin_root.join("skills").abs(), + scope: SkillScope::User, + file_system: Arc::clone(&LOCAL_FS), + plugin_id: Some("sample@test".to_string()), + plugin_namespace: Some(plugin_name.clone()), + plugin_root: Some(plugin_root.abs()), + }], + /*plugin_skill_snapshots*/ None, + ) .await; assert!( @@ -1368,14 +1390,17 @@ async fn plugin_skill_name_length_limit_rejects_overlong_qualified_name() { ) .unwrap(); - let outcome = load_skills_from_roots([SkillRoot { - path: plugin_root.join("skills").abs(), - scope: SkillScope::User, - file_system: Arc::clone(&LOCAL_FS), - plugin_id: Some("sample@test".to_string()), - plugin_namespace: Some(plugin_name.clone()), - plugin_root: Some(plugin_root.abs()), - }]) + let outcome = load_skills_from_roots( + [SkillRoot { + path: plugin_root.join("skills").abs(), + scope: SkillScope::User, + file_system: Arc::clone(&LOCAL_FS), + plugin_id: Some("sample@test".to_string()), + plugin_namespace: Some(plugin_name.clone()), + plugin_root: Some(plugin_root.abs()), + }], + /*plugin_skill_snapshots*/ None, + ) .await; assert_eq!(outcome.skills, Vec::new()); @@ -1807,24 +1832,27 @@ async fn deduplicates_by_path_preferring_first_root() { let skill_path = write_skill_at(root.path(), "dupe", "dupe-skill", "from repo"); - let outcome = load_skills_from_roots([ - SkillRoot { - path: root.path().abs(), - scope: SkillScope::Repo, - file_system: Arc::clone(&LOCAL_FS), - plugin_id: None, - plugin_namespace: None, - plugin_root: None, - }, - SkillRoot { - path: root.path().abs(), - scope: SkillScope::User, - file_system: Arc::clone(&LOCAL_FS), - plugin_id: None, - plugin_namespace: None, - plugin_root: None, - }, - ]) + let outcome = load_skills_from_roots( + [ + SkillRoot { + path: root.path().abs(), + scope: SkillScope::Repo, + file_system: Arc::clone(&LOCAL_FS), + plugin_id: None, + plugin_namespace: None, + plugin_root: None, + }, + SkillRoot { + path: root.path().abs(), + scope: SkillScope::User, + file_system: Arc::clone(&LOCAL_FS), + plugin_id: None, + plugin_namespace: None, + plugin_root: None, + }, + ], + /*plugin_skill_snapshots*/ None, + ) .await; assert!( diff --git a/codex-rs/core-skills/src/root_loader.rs b/codex-rs/core-skills/src/root_loader.rs new file mode 100644 index 0000000000..a31c8a2753 --- /dev/null +++ b/codex-rs/core-skills/src/root_loader.rs @@ -0,0 +1,158 @@ +use std::collections::HashMap; +use std::collections::HashSet; +use std::fmt; +use std::sync::Arc; +use std::sync::Mutex; + +use codex_utils_plugins::PluginSkillRoot; + +use crate::SkillLoadOutcome; +use crate::loader::SkillRoot; +use crate::loader::SkillRootSnapshot; +use crate::loader::load_skill_root; +use crate::model::SkillFileSystemsByPath; + +/// Parsed plugin skill-root snapshots produced by one plugin load. +/// +/// Clones share the same snapshots. The plugins manager stores them with the corresponding loaded +/// plugins and passes a clone to skill loading as an optional preload. +#[derive(Clone)] +pub struct PluginSkillSnapshots { + snapshots_by_root: Arc>>, +} + +impl PluginSkillSnapshots { + /// Creates an empty snapshot collection for a plugin load to populate. + pub fn for_plugin_load() -> Self { + Self { + snapshots_by_root: Arc::new(Mutex::new(HashMap::new())), + } + } +} + +impl fmt::Debug for PluginSkillSnapshots { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + f.debug_struct("PluginSkillSnapshots") + .finish_non_exhaustive() + } +} + +pub(crate) async fn load_and_merge_skill_roots( + roots: I, + plugin_skill_snapshots: Option<&PluginSkillSnapshots>, +) -> SkillLoadOutcome +where + I: IntoIterator, +{ + let mut root_snapshots = Vec::new(); + for root in roots { + let cache_key = match ( + root.plugin_id.clone(), + root.plugin_namespace.clone(), + root.plugin_root.clone(), + ) { + (Some(plugin_id), Some(plugin_namespace), Some(plugin_root)) => Some(PluginSkillRoot { + path: root.path.clone(), + plugin_id, + plugin_namespace, + plugin_root, + }), + _ => None, + }; + let cached_snapshot = cache_key.as_ref().and_then(|cache_key| { + let plugin_skill_snapshots = plugin_skill_snapshots?; + plugin_skill_snapshots + .snapshots_by_root + .lock() + .unwrap_or_else(std::sync::PoisonError::into_inner) + .get(cache_key) + .cloned() + }); + let snapshot = match cached_snapshot { + Some(snapshot) => snapshot, + None => { + let snapshot = load_skill_root(root).await; + if let Some(plugin_skill_snapshots) = plugin_skill_snapshots + && let Some(cache_key) = cache_key + { + plugin_skill_snapshots + .snapshots_by_root + .lock() + .unwrap_or_else(std::sync::PoisonError::into_inner) + .insert(cache_key, snapshot.clone()); + } + snapshot + } + }; + root_snapshots.push(snapshot); + } + + merge_skill_root_snapshots(root_snapshots) +} + +fn merge_skill_root_snapshots(snapshots: Vec) -> SkillLoadOutcome { + fn scope_rank(scope: codex_protocol::protocol::SkillScope) -> u8 { + use codex_protocol::protocol::SkillScope; + + // Higher-priority scopes first (matches root scan order for dedupe). + match scope { + SkillScope::Repo => 0, + SkillScope::User => 1, + SkillScope::System => 2, + SkillScope::Admin => 3, + } + } + + let mut outcome = SkillLoadOutcome::default(); + let mut skill_roots = Vec::new(); + let mut skill_root_by_path = HashMap::new(); + let mut file_systems_by_skill_path = HashMap::new(); + + for snapshot in snapshots { + let SkillRootSnapshot { + root, + skills, + errors, + file_system, + } = snapshot; + if !skills.is_empty() && !skill_roots.contains(&root) { + skill_roots.push(root.clone()); + } + for skill in &skills { + skill_root_by_path + .entry(skill.path_to_skills_md.clone()) + .or_insert_with(|| root.clone()); + file_systems_by_skill_path + .entry(skill.path_to_skills_md.clone()) + .or_insert_with(|| Arc::clone(&file_system)); + } + outcome.skills.extend(skills); + outcome.errors.extend(errors); + } + + let mut seen = HashSet::new(); + outcome + .skills + .retain(|skill| seen.insert(skill.path_to_skills_md.clone())); + let retained_skill_paths = outcome + .skills + .iter() + .map(|skill| skill.path_to_skills_md.clone()) + .collect::>(); + skill_root_by_path.retain(|path, _| retained_skill_paths.contains(path)); + let used_roots = skill_root_by_path.values().cloned().collect::>(); + skill_roots.retain(|root| used_roots.contains(root)); + file_systems_by_skill_path.retain(|path, _| retained_skill_paths.contains(path)); + outcome.skill_roots = skill_roots; + outcome.skill_root_by_path = Arc::new(skill_root_by_path); + outcome.file_systems_by_skill_path = SkillFileSystemsByPath::new(file_systems_by_skill_path); + + outcome.skills.sort_by(|a, b| { + scope_rank(a.scope) + .cmp(&scope_rank(b.scope)) + .then_with(|| a.name.cmp(&b.name)) + .then_with(|| a.path_to_skills_md.cmp(&b.path_to_skills_md)) + }); + + outcome +} diff --git a/codex-rs/core-skills/src/service.rs b/codex-rs/core-skills/src/service.rs index a6a1e30f57..1c956d79b9 100644 --- a/codex-rs/core-skills/src/service.rs +++ b/codex-rs/core-skills/src/service.rs @@ -14,6 +14,7 @@ use tracing::instrument; use tracing::warn; use crate::HostSkillsSnapshot; +use crate::PluginSkillSnapshots; use crate::SkillLoadOutcome; use crate::build_implicit_skill_path_indexes; use crate::config_rules::SkillConfigRules; @@ -32,6 +33,7 @@ pub struct SkillsLoadInput { pub effective_skill_roots: Vec, pub config_layer_stack: ConfigLayerStack, pub bundled_skills_enabled: bool, + plugin_skill_snapshots: Option, } impl SkillsLoadInput { @@ -46,8 +48,18 @@ impl SkillsLoadInput { effective_skill_roots, config_layer_stack, bundled_skills_enabled, + plugin_skill_snapshots: None, } } + + /// Attaches plugin skill snapshots parsed during plugin loading, when available. + pub fn with_plugin_skill_snapshots( + mut self, + plugin_skill_snapshots: Option, + ) -> Self { + self.plugin_skill_snapshots = plugin_skill_snapshots; + self + } } /// Owns host skill discovery, immutable snapshots, cache invalidation, and extra roots. @@ -124,7 +136,8 @@ impl SkillsService { } let snapshot = HostSkillsSnapshot::new(Arc::new( - self.build_skill_outcome(roots, &skill_config_rules).await, + self.build_skill_outcome(input, roots, &skill_config_rules) + .await, )); let mut cache = self .cache_by_config @@ -180,7 +193,8 @@ impl SkillsService { } let skill_config_rules = skill_config_rules_from_stack(&input.config_layer_stack); let snapshot = HostSkillsSnapshot::new(Arc::new( - self.build_skill_outcome(roots, &skill_config_rules).await, + self.build_skill_outcome(input, roots, &skill_config_rules) + .await, )); if use_cwd_cache { let mut cache = self @@ -195,13 +209,13 @@ impl SkillsService { #[instrument(level = "trace", skip_all)] async fn build_skill_outcome( &self, + input: &SkillsLoadInput, roots: Vec, skill_config_rules: &SkillConfigRules, ) -> SkillLoadOutcome { - let outcome = crate::filter_skill_load_outcome_for_product( - load_skills_from_roots(roots).await, - self.restriction_product, - ); + let outcome = load_skills_from_roots(roots, input.plugin_skill_snapshots.as_ref()).await; + let outcome = + crate::filter_skill_load_outcome_for_product(outcome, self.restriction_product); let disabled_paths = resolve_disabled_skill_paths(&outcome.skills, skill_config_rules); finalize_skill_outcome(outcome, disabled_paths) } diff --git a/codex-rs/core/src/agent/role_tests.rs b/codex-rs/core/src/agent/role_tests.rs index e258c0ad90..b56531c90c 100644 --- a/codex-rs/core/src/agent/role_tests.rs +++ b/codex-rs/core/src/agent/role_tests.rs @@ -421,7 +421,9 @@ enabled = false let plugins_input = config.plugins_config_input(); let plugin_outcome = plugins_manager.plugins_for_config(&plugins_input).await; let effective_skill_roots = plugin_outcome.effective_plugin_skill_roots(); - let skills_input = skills_load_input_from_config(&config, effective_skill_roots); + let plugin_skill_snapshots = plugins_manager.plugin_skill_snapshots_for_config(&plugins_input); + let skills_input = skills_load_input_from_config(&config, effective_skill_roots) + .with_plugin_skill_snapshots(plugin_skill_snapshots); let snapshot = skills_service .snapshot_for_config( &skills_input, diff --git a/codex-rs/core/src/session/session.rs b/codex-rs/core/src/session/session.rs index af2d1abeb8..b7ceb165bb 100644 --- a/codex-rs/core/src/session/session.rs +++ b/codex-rs/core/src/session/session.rs @@ -445,7 +445,9 @@ async fn warm_plugins_and_skills_for_session_init( let plugins_input = config.plugins_config_input(); let plugin_outcome = plugins_manager.plugins_for_config(&plugins_input).await; let effective_skill_roots = plugin_outcome.effective_plugin_skill_roots(); - let skills_input = skills_load_input_from_config(config.as_ref(), effective_skill_roots); + let plugin_skill_snapshots = plugins_manager.plugin_skill_snapshots_for_config(&plugins_input); + let skills_input = skills_load_input_from_config(config.as_ref(), effective_skill_roots) + .with_plugin_skill_snapshots(plugin_skill_snapshots); skills_service .snapshot_for_config(&skills_input, fs) .await diff --git a/codex-rs/core/src/session/tests.rs b/codex-rs/core/src/session/tests.rs index 0b1cc045c4..93faae5483 100644 --- a/codex-rs/core/src/session/tests.rs +++ b/codex-rs/core/src/session/tests.rs @@ -5050,13 +5050,18 @@ pub(crate) async fn make_session_and_context() -> (Session, TurnContext) { turn_environments: Arc::clone(&turn_environments), }; + let plugins_input = per_turn_config.plugins_config_input(); let plugin_outcome = services .plugins_manager - .plugins_for_config(&per_turn_config.plugins_config_input()) + .plugins_for_config(&plugins_input) .await; let effective_skill_roots = plugin_outcome.effective_plugin_skill_roots(); + let plugin_skill_snapshots = services + .plugins_manager + .plugin_skill_snapshots_for_config(&plugins_input); let skills_input = - crate::skills_load_input_from_config(&per_turn_config, effective_skill_roots); + crate::skills_load_input_from_config(&per_turn_config, effective_skill_roots) + .with_plugin_skill_snapshots(plugin_skill_snapshots); let skill_fs = environment.get_filesystem(); let skills_snapshot = services .skills_service @@ -7099,13 +7104,18 @@ where turn_environments: Arc::clone(&turn_environments), }; + let plugins_input = per_turn_config.plugins_config_input(); let plugin_outcome = services .plugins_manager - .plugins_for_config(&per_turn_config.plugins_config_input()) + .plugins_for_config(&plugins_input) .await; let effective_skill_roots = plugin_outcome.effective_plugin_skill_roots(); + let plugin_skill_snapshots = services + .plugins_manager + .plugin_skill_snapshots_for_config(&plugins_input); let skills_input = - crate::skills_load_input_from_config(&per_turn_config, effective_skill_roots); + crate::skills_load_input_from_config(&per_turn_config, effective_skill_roots) + .with_plugin_skill_snapshots(plugin_skill_snapshots); let skill_fs = environment.get_filesystem(); let skills_snapshot = services .skills_service diff --git a/codex-rs/core/src/session/turn_context.rs b/codex-rs/core/src/session/turn_context.rs index 91880bdefa..c03bdab7cf 100644 --- a/codex-rs/core/src/session/turn_context.rs +++ b/codex-rs/core/src/session/turn_context.rs @@ -722,13 +722,19 @@ impl Session { .or(model_info.multi_agent_version) .unwrap_or_else(|| per_turn_config.multi_agent_version_from_features()), }; + let plugins_input = per_turn_config.plugins_config_input(); let plugin_outcome = self .services .plugins_manager - .plugins_for_config(&per_turn_config.plugins_config_input()) + .plugins_for_config(&plugins_input) .await; let effective_skill_roots = plugin_outcome.effective_plugin_skill_roots(); - let skills_input = skills_load_input_from_config(&per_turn_config, effective_skill_roots); + let plugin_skill_snapshots = self + .services + .plugins_manager + .plugin_skill_snapshots_for_config(&plugins_input); + let skills_input = skills_load_input_from_config(&per_turn_config, effective_skill_roots) + .with_plugin_skill_snapshots(plugin_skill_snapshots); let fs = primary_turn_environment .map(|turn_environment| turn_environment.environment.get_filesystem()); let skills_snapshot = self diff --git a/codex-rs/ext/skills/src/provider/executor.rs b/codex-rs/ext/skills/src/provider/executor.rs index 8da0265d09..75234dfbee 100644 --- a/codex-rs/ext/skills/src/provider/executor.rs +++ b/codex-rs/ext/skills/src/provider/executor.rs @@ -76,14 +76,17 @@ impl SkillProvider for ExecutorSkillProvider { }; let file_system = environment.get_filesystem(); let outcome = filter_skill_load_outcome_for_product( - load_skills_from_roots([SkillRoot { - path: root_path.clone(), - scope: SkillScope::User, - file_system: Arc::clone(&file_system), - plugin_id: None, - plugin_namespace: None, - plugin_root: None, - }]) + load_skills_from_roots( + [SkillRoot { + path: root_path.clone(), + scope: SkillScope::User, + file_system: Arc::clone(&file_system), + plugin_id: None, + plugin_namespace: None, + plugin_root: None, + }], + /*plugin_skill_snapshots*/ None, + ) .await, self.restriction_product, ); diff --git a/codex-rs/ext/skills/tests/executor_file_system_authority.rs b/codex-rs/ext/skills/tests/executor_file_system_authority.rs index bc2a87a740..e4b09e96a0 100644 --- a/codex-rs/ext/skills/tests/executor_file_system_authority.rs +++ b/codex-rs/ext/skills/tests/executor_file_system_authority.rs @@ -188,17 +188,20 @@ async fn skill_loading_and_reads_use_the_supplied_executor_file_system() { assert!(!alias_root.as_path().exists()); assert!(!canonical_root.as_path().exists()); - let outcome = load_skills_from_roots([SkillRoot { - path: alias_root.clone(), - scope: SkillScope::User, - file_system: Arc::new(SyntheticFileSystem { - alias_root, - canonical_root: canonical_root.clone(), - }), - plugin_id: None, - plugin_namespace: None, - plugin_root: None, - }]) + let outcome = load_skills_from_roots( + [SkillRoot { + path: alias_root.clone(), + scope: SkillScope::User, + file_system: Arc::new(SyntheticFileSystem { + alias_root, + canonical_root: canonical_root.clone(), + }), + plugin_id: None, + plugin_namespace: None, + plugin_root: None, + }], + /*plugin_skill_snapshots*/ None, + ) .await; assert_eq!(outcome.errors, Vec::new()); assert_eq!(outcome.skills.len(), 1);