Port more orchestrator executor split seams

Co-authored-by: Codex <noreply@openai.com>
This commit is contained in:
starr-openai
2026-03-21 11:22:29 -07:00
co-authored by Codex
parent a11f4795e3
commit e061bbbf84
121 changed files with 7465 additions and 672 deletions
+1
View File
@@ -1015,6 +1015,7 @@ mod tests {
.request_typed::<ConfigRequirementsReadResponse>(ClientRequest::ThreadRead {
request_id: RequestId::Integer(99),
params: codex_app_server_protocol::ThreadReadParams {
environment_id: None,
thread_id: "missing-thread".to_string(),
include_turns: false,
},
@@ -361,6 +361,12 @@
},
"type": "array"
},
"environmentId": {
"type": [
"string",
"null"
]
},
"expectedVersion": {
"type": [
"string",
@@ -410,6 +416,12 @@
"null"
]
},
"environmentId": {
"type": [
"string",
"null"
]
},
"includeLayers": {
"default": false,
"type": "boolean"
@@ -419,6 +431,12 @@
},
"ConfigValueWriteParams": {
"properties": {
"environmentId": {
"type": [
"string",
"null"
]
},
"expectedVersion": {
"type": [
"string",
@@ -1326,6 +1344,19 @@
],
"type": "object"
},
"PromptListParams": {
"properties": {
"environmentId": {
"default": null,
"description": "Optional execution environment that owns the custom prompt snapshot.",
"type": [
"string",
"null"
]
}
},
"type": "object"
},
"ReadOnlyAccess": {
"oneOf": [
{
@@ -2387,6 +2418,14 @@
},
"type": "array"
},
"environmentId": {
"default": null,
"description": "Optional execution environment that owns the skill snapshot.",
"type": [
"string",
"null"
]
},
"forceReload": {
"description": "When true, bypass the skills cache and re-scan skills from disk.",
"type": "boolean"
@@ -3750,6 +3789,30 @@
"title": "Thread/readRequest",
"type": "object"
},
{
"properties": {
"id": {
"$ref": "#/definitions/RequestId"
},
"method": {
"enum": [
"prompt/list"
],
"title": "Prompt/listRequestMethod",
"type": "string"
},
"params": {
"$ref": "#/definitions/PromptListParams"
}
},
"required": [
"id",
"method",
"params"
],
"title": "Prompt/listRequest",
"type": "object"
},
{
"properties": {
"id": {
@@ -1944,7 +1944,15 @@
]
},
"SkillsChangedNotification": {
"description": "Notification emitted when watched local skill files change.\n\nTreat this as an invalidation signal and re-run `skills/list` with the client's current parameters when refreshed skill metadata is needed.",
"description": "Notification emitted when watched skill files change for an execution environment.\n\nTreat this as an invalidation signal and re-run `skills/list` with the client's current parameters when refreshed skill metadata is needed.",
"properties": {
"environmentId": {
"type": "string"
}
},
"required": [
"environmentId"
],
"type": "object"
},
"SubAgentSource": {
@@ -619,6 +619,30 @@
"title": "Thread/readRequest",
"type": "object"
},
{
"properties": {
"id": {
"$ref": "#/definitions/v2/RequestId"
},
"method": {
"enum": [
"prompt/list"
],
"title": "Prompt/listRequestMethod",
"type": "string"
},
"params": {
"$ref": "#/definitions/v2/PromptListParams"
}
},
"required": [
"id",
"method",
"params"
],
"title": "Prompt/listRequest",
"type": "object"
},
{
"properties": {
"id": {
@@ -6375,6 +6399,12 @@
},
"type": "array"
},
"environmentId": {
"type": [
"string",
"null"
]
},
"expectedVersion": {
"type": [
"string",
@@ -6620,6 +6650,12 @@
"null"
]
},
"environmentId": {
"type": [
"string",
"null"
]
},
"includeLayers": {
"default": false,
"type": "boolean"
@@ -6634,6 +6670,9 @@
"config": {
"$ref": "#/definitions/v2/Config"
},
"environmentId": {
"type": "string"
},
"layers": {
"items": {
"$ref": "#/definitions/v2/ConfigLayer"
@@ -6652,6 +6691,7 @@
},
"required": [
"config",
"environmentId",
"origins"
],
"title": "ConfigReadResponse",
@@ -6729,6 +6769,12 @@
"ConfigValueWriteParams": {
"$schema": "http://json-schema.org/draft-07/schema#",
"properties": {
"environmentId": {
"type": [
"string",
"null"
]
},
"expectedVersion": {
"type": [
"string",
@@ -6936,6 +6982,37 @@
],
"type": "object"
},
"CustomPrompt": {
"properties": {
"argument_hint": {
"type": [
"string",
"null"
]
},
"content": {
"type": "string"
},
"description": {
"type": [
"string",
"null"
]
},
"name": {
"type": "string"
},
"path": {
"type": "string"
}
},
"required": [
"content",
"name",
"path"
],
"type": "object"
},
"DeprecationNoticeNotification": {
"$schema": "http://json-schema.org/draft-07/schema#",
"properties": {
@@ -9690,6 +9767,41 @@
},
"type": "object"
},
"PromptListParams": {
"$schema": "http://json-schema.org/draft-07/schema#",
"properties": {
"environmentId": {
"default": null,
"description": "Optional execution environment that owns the custom prompt snapshot.",
"type": [
"string",
"null"
]
}
},
"title": "PromptListParams",
"type": "object"
},
"PromptListResponse": {
"$schema": "http://json-schema.org/draft-07/schema#",
"properties": {
"environmentId": {
"type": "string"
},
"prompts": {
"items": {
"$ref": "#/definitions/v2/CustomPrompt"
},
"type": "array"
}
},
"required": [
"environmentId",
"prompts"
],
"title": "PromptListResponse",
"type": "object"
},
"RateLimitSnapshot": {
"properties": {
"credits": {
@@ -11322,7 +11434,15 @@
},
"SkillsChangedNotification": {
"$schema": "http://json-schema.org/draft-07/schema#",
"description": "Notification emitted when watched local skill files change.\n\nTreat this as an invalidation signal and re-run `skills/list` with the client's current parameters when refreshed skill metadata is needed.",
"description": "Notification emitted when watched skill files change for an execution environment.\n\nTreat this as an invalidation signal and re-run `skills/list` with the client's current parameters when refreshed skill metadata is needed.",
"properties": {
"environmentId": {
"type": "string"
}
},
"required": [
"environmentId"
],
"title": "SkillsChangedNotification",
"type": "object"
},
@@ -11409,6 +11529,14 @@
},
"type": "array"
},
"environmentId": {
"default": null,
"description": "Optional execution environment that owns the skill snapshot.",
"type": [
"string",
"null"
]
},
"forceReload": {
"description": "When true, bypass the skills cache and re-scan skills from disk.",
"type": "boolean"
@@ -11436,10 +11564,14 @@
"$ref": "#/definitions/v2/SkillsListEntry"
},
"type": "array"
},
"environmentId": {
"type": "string"
}
},
"required": [
"data"
"data",
"environmentId"
],
"title": "SkillsListResponse",
"type": "object"
@@ -1146,6 +1146,30 @@
"title": "Thread/readRequest",
"type": "object"
},
{
"properties": {
"id": {
"$ref": "#/definitions/RequestId"
},
"method": {
"enum": [
"prompt/list"
],
"title": "Prompt/listRequestMethod",
"type": "string"
},
"params": {
"$ref": "#/definitions/PromptListParams"
}
},
"required": [
"id",
"method",
"params"
],
"title": "Prompt/listRequest",
"type": "object"
},
{
"properties": {
"id": {
@@ -3008,6 +3032,12 @@
},
"type": "array"
},
"environmentId": {
"type": [
"string",
"null"
]
},
"expectedVersion": {
"type": [
"string",
@@ -3253,6 +3283,12 @@
"null"
]
},
"environmentId": {
"type": [
"string",
"null"
]
},
"includeLayers": {
"default": false,
"type": "boolean"
@@ -3267,6 +3303,9 @@
"config": {
"$ref": "#/definitions/Config"
},
"environmentId": {
"type": "string"
},
"layers": {
"items": {
"$ref": "#/definitions/ConfigLayer"
@@ -3285,6 +3324,7 @@
},
"required": [
"config",
"environmentId",
"origins"
],
"title": "ConfigReadResponse",
@@ -3362,6 +3402,12 @@
"ConfigValueWriteParams": {
"$schema": "http://json-schema.org/draft-07/schema#",
"properties": {
"environmentId": {
"type": [
"string",
"null"
]
},
"expectedVersion": {
"type": [
"string",
@@ -3569,6 +3615,37 @@
],
"type": "object"
},
"CustomPrompt": {
"properties": {
"argument_hint": {
"type": [
"string",
"null"
]
},
"content": {
"type": "string"
},
"description": {
"type": [
"string",
"null"
]
},
"name": {
"type": "string"
},
"path": {
"type": "string"
}
},
"required": [
"content",
"name",
"path"
],
"type": "object"
},
"DeprecationNoticeNotification": {
"$schema": "http://json-schema.org/draft-07/schema#",
"properties": {
@@ -6478,6 +6555,41 @@
},
"type": "object"
},
"PromptListParams": {
"$schema": "http://json-schema.org/draft-07/schema#",
"properties": {
"environmentId": {
"default": null,
"description": "Optional execution environment that owns the custom prompt snapshot.",
"type": [
"string",
"null"
]
}
},
"title": "PromptListParams",
"type": "object"
},
"PromptListResponse": {
"$schema": "http://json-schema.org/draft-07/schema#",
"properties": {
"environmentId": {
"type": "string"
},
"prompts": {
"items": {
"$ref": "#/definitions/CustomPrompt"
},
"type": "array"
}
},
"required": [
"environmentId",
"prompts"
],
"title": "PromptListResponse",
"type": "object"
},
"RateLimitSnapshot": {
"properties": {
"credits": {
@@ -9082,7 +9194,15 @@
},
"SkillsChangedNotification": {
"$schema": "http://json-schema.org/draft-07/schema#",
"description": "Notification emitted when watched local skill files change.\n\nTreat this as an invalidation signal and re-run `skills/list` with the client's current parameters when refreshed skill metadata is needed.",
"description": "Notification emitted when watched skill files change for an execution environment.\n\nTreat this as an invalidation signal and re-run `skills/list` with the client's current parameters when refreshed skill metadata is needed.",
"properties": {
"environmentId": {
"type": "string"
}
},
"required": [
"environmentId"
],
"title": "SkillsChangedNotification",
"type": "object"
},
@@ -9169,6 +9289,14 @@
},
"type": "array"
},
"environmentId": {
"default": null,
"description": "Optional execution environment that owns the skill snapshot.",
"type": [
"string",
"null"
]
},
"forceReload": {
"description": "When true, bypass the skills cache and re-scan skills from disk.",
"type": "boolean"
@@ -9196,10 +9324,14 @@
"$ref": "#/definitions/SkillsListEntry"
},
"type": "array"
},
"environmentId": {
"type": "string"
}
},
"required": [
"data"
"data",
"environmentId"
],
"title": "SkillsListResponse",
"type": "object"
@@ -33,6 +33,12 @@
},
"type": "array"
},
"environmentId": {
"type": [
"string",
"null"
]
},
"expectedVersion": {
"type": [
"string",
@@ -8,6 +8,12 @@
"null"
]
},
"environmentId": {
"type": [
"string",
"null"
]
},
"includeLayers": {
"default": false,
"type": "boolean"
@@ -876,6 +876,9 @@
"config": {
"$ref": "#/definitions/Config"
},
"environmentId": {
"type": "string"
},
"layers": {
"items": {
"$ref": "#/definitions/ConfigLayer"
@@ -894,6 +897,7 @@
},
"required": [
"config",
"environmentId",
"origins"
],
"title": "ConfigReadResponse",
@@ -10,6 +10,12 @@
}
},
"properties": {
"environmentId": {
"type": [
"string",
"null"
]
},
"expectedVersion": {
"type": [
"string",
@@ -0,0 +1,15 @@
{
"$schema": "http://json-schema.org/draft-07/schema#",
"properties": {
"environmentId": {
"default": null,
"description": "Optional execution environment that owns the custom prompt snapshot.",
"type": [
"string",
"null"
]
}
},
"title": "PromptListParams",
"type": "object"
}
@@ -0,0 +1,53 @@
{
"$schema": "http://json-schema.org/draft-07/schema#",
"definitions": {
"CustomPrompt": {
"properties": {
"argument_hint": {
"type": [
"string",
"null"
]
},
"content": {
"type": "string"
},
"description": {
"type": [
"string",
"null"
]
},
"name": {
"type": "string"
},
"path": {
"type": "string"
}
},
"required": [
"content",
"name",
"path"
],
"type": "object"
}
},
"properties": {
"environmentId": {
"type": "string"
},
"prompts": {
"items": {
"$ref": "#/definitions/CustomPrompt"
},
"type": "array"
}
},
"required": [
"environmentId",
"prompts"
],
"title": "PromptListResponse",
"type": "object"
}
@@ -1,6 +1,14 @@
{
"$schema": "http://json-schema.org/draft-07/schema#",
"description": "Notification emitted when watched local skill files change.\n\nTreat this as an invalidation signal and re-run `skills/list` with the client's current parameters when refreshed skill metadata is needed.",
"description": "Notification emitted when watched skill files change for an execution environment.\n\nTreat this as an invalidation signal and re-run `skills/list` with the client's current parameters when refreshed skill metadata is needed.",
"properties": {
"environmentId": {
"type": "string"
}
},
"required": [
"environmentId"
],
"title": "SkillsChangedNotification",
"type": "object"
}
@@ -28,6 +28,14 @@
},
"type": "array"
},
"environmentId": {
"default": null,
"description": "Optional execution environment that owns the skill snapshot.",
"type": [
"string",
"null"
]
},
"forceReload": {
"description": "When true, bypass the skills cache and re-scan skills from disk.",
"type": "boolean"
@@ -205,10 +205,14 @@
"$ref": "#/definitions/SkillsListEntry"
},
"type": "array"
},
"environmentId": {
"type": "string"
}
},
"required": [
"data"
"data",
"environmentId"
],
"title": "SkillsListResponse",
"type": "object"
@@ -36,6 +36,7 @@ import type { PluginInstallParams } from "./v2/PluginInstallParams";
import type { PluginListParams } from "./v2/PluginListParams";
import type { PluginReadParams } from "./v2/PluginReadParams";
import type { PluginUninstallParams } from "./v2/PluginUninstallParams";
import type { PromptListParams } from "./v2/PromptListParams";
import type { ReviewStartParams } from "./v2/ReviewStartParams";
import type { SkillsConfigWriteParams } from "./v2/SkillsConfigWriteParams";
import type { SkillsListParams } from "./v2/SkillsListParams";
@@ -61,4 +62,4 @@ import type { WindowsSandboxSetupStartParams } from "./v2/WindowsSandboxSetupSta
/**
* Request from the client to the server.
*/
export type ClientRequest ={ "method": "initialize", id: RequestId, params: InitializeParams, } | { "method": "thread/start", id: RequestId, params: ThreadStartParams, } | { "method": "thread/resume", id: RequestId, params: ThreadResumeParams, } | { "method": "thread/fork", id: RequestId, params: ThreadForkParams, } | { "method": "thread/archive", id: RequestId, params: ThreadArchiveParams, } | { "method": "thread/unsubscribe", id: RequestId, params: ThreadUnsubscribeParams, } | { "method": "thread/name/set", id: RequestId, params: ThreadSetNameParams, } | { "method": "thread/metadata/update", id: RequestId, params: ThreadMetadataUpdateParams, } | { "method": "thread/unarchive", id: RequestId, params: ThreadUnarchiveParams, } | { "method": "thread/compact/start", id: RequestId, params: ThreadCompactStartParams, } | { "method": "thread/shellCommand", id: RequestId, params: ThreadShellCommandParams, } | { "method": "thread/rollback", id: RequestId, params: ThreadRollbackParams, } | { "method": "thread/list", id: RequestId, params: ThreadListParams, } | { "method": "thread/loaded/list", id: RequestId, params: ThreadLoadedListParams, } | { "method": "thread/read", id: RequestId, params: ThreadReadParams, } | { "method": "skills/list", id: RequestId, params: SkillsListParams, } | { "method": "plugin/list", id: RequestId, params: PluginListParams, } | { "method": "plugin/read", id: RequestId, params: PluginReadParams, } | { "method": "app/list", id: RequestId, params: AppsListParams, } | { "method": "fs/readFile", id: RequestId, params: FsReadFileParams, } | { "method": "fs/writeFile", id: RequestId, params: FsWriteFileParams, } | { "method": "fs/createDirectory", id: RequestId, params: FsCreateDirectoryParams, } | { "method": "fs/getMetadata", id: RequestId, params: FsGetMetadataParams, } | { "method": "fs/readDirectory", id: RequestId, params: FsReadDirectoryParams, } | { "method": "fs/remove", id: RequestId, params: FsRemoveParams, } | { "method": "fs/copy", id: RequestId, params: FsCopyParams, } | { "method": "skills/config/write", id: RequestId, params: SkillsConfigWriteParams, } | { "method": "plugin/install", id: RequestId, params: PluginInstallParams, } | { "method": "plugin/uninstall", id: RequestId, params: PluginUninstallParams, } | { "method": "turn/start", id: RequestId, params: TurnStartParams, } | { "method": "turn/steer", id: RequestId, params: TurnSteerParams, } | { "method": "turn/interrupt", id: RequestId, params: TurnInterruptParams, } | { "method": "review/start", id: RequestId, params: ReviewStartParams, } | { "method": "model/list", id: RequestId, params: ModelListParams, } | { "method": "experimentalFeature/list", id: RequestId, params: ExperimentalFeatureListParams, } | { "method": "mcpServer/oauth/login", id: RequestId, params: McpServerOauthLoginParams, } | { "method": "config/mcpServer/reload", id: RequestId, params: undefined, } | { "method": "mcpServerStatus/list", id: RequestId, params: ListMcpServerStatusParams, } | { "method": "windowsSandbox/setupStart", id: RequestId, params: WindowsSandboxSetupStartParams, } | { "method": "account/login/start", id: RequestId, params: LoginAccountParams, } | { "method": "account/login/cancel", id: RequestId, params: CancelLoginAccountParams, } | { "method": "account/logout", id: RequestId, params: undefined, } | { "method": "account/rateLimits/read", id: RequestId, params: undefined, } | { "method": "feedback/upload", id: RequestId, params: FeedbackUploadParams, } | { "method": "command/exec", id: RequestId, params: CommandExecParams, } | { "method": "command/exec/write", id: RequestId, params: CommandExecWriteParams, } | { "method": "command/exec/terminate", id: RequestId, params: CommandExecTerminateParams, } | { "method": "command/exec/resize", id: RequestId, params: CommandExecResizeParams, } | { "method": "config/read", id: RequestId, params: ConfigReadParams, } | { "method": "externalAgentConfig/detect", id: RequestId, params: ExternalAgentConfigDetectParams, } | { "method": "externalAgentConfig/import", id: RequestId, params: ExternalAgentConfigImportParams, } | { "method": "config/value/write", id: RequestId, params: ConfigValueWriteParams, } | { "method": "config/batchWrite", id: RequestId, params: ConfigBatchWriteParams, } | { "method": "configRequirements/read", id: RequestId, params: undefined, } | { "method": "account/read", id: RequestId, params: GetAccountParams, } | { "method": "getConversationSummary", id: RequestId, params: GetConversationSummaryParams, } | { "method": "gitDiffToRemote", id: RequestId, params: GitDiffToRemoteParams, } | { "method": "getAuthStatus", id: RequestId, params: GetAuthStatusParams, } | { "method": "fuzzyFileSearch", id: RequestId, params: FuzzyFileSearchParams, };
export type ClientRequest ={ "method": "initialize", id: RequestId, params: InitializeParams, } | { "method": "thread/start", id: RequestId, params: ThreadStartParams, } | { "method": "thread/resume", id: RequestId, params: ThreadResumeParams, } | { "method": "thread/fork", id: RequestId, params: ThreadForkParams, } | { "method": "thread/archive", id: RequestId, params: ThreadArchiveParams, } | { "method": "thread/unsubscribe", id: RequestId, params: ThreadUnsubscribeParams, } | { "method": "thread/name/set", id: RequestId, params: ThreadSetNameParams, } | { "method": "thread/metadata/update", id: RequestId, params: ThreadMetadataUpdateParams, } | { "method": "thread/unarchive", id: RequestId, params: ThreadUnarchiveParams, } | { "method": "thread/compact/start", id: RequestId, params: ThreadCompactStartParams, } | { "method": "thread/shellCommand", id: RequestId, params: ThreadShellCommandParams, } | { "method": "thread/rollback", id: RequestId, params: ThreadRollbackParams, } | { "method": "thread/list", id: RequestId, params: ThreadListParams, } | { "method": "thread/loaded/list", id: RequestId, params: ThreadLoadedListParams, } | { "method": "thread/read", id: RequestId, params: ThreadReadParams, } | { "method": "prompt/list", id: RequestId, params: PromptListParams, } | { "method": "skills/list", id: RequestId, params: SkillsListParams, } | { "method": "plugin/list", id: RequestId, params: PluginListParams, } | { "method": "plugin/read", id: RequestId, params: PluginReadParams, } | { "method": "app/list", id: RequestId, params: AppsListParams, } | { "method": "fs/readFile", id: RequestId, params: FsReadFileParams, } | { "method": "fs/writeFile", id: RequestId, params: FsWriteFileParams, } | { "method": "fs/createDirectory", id: RequestId, params: FsCreateDirectoryParams, } | { "method": "fs/getMetadata", id: RequestId, params: FsGetMetadataParams, } | { "method": "fs/readDirectory", id: RequestId, params: FsReadDirectoryParams, } | { "method": "fs/remove", id: RequestId, params: FsRemoveParams, } | { "method": "fs/copy", id: RequestId, params: FsCopyParams, } | { "method": "skills/config/write", id: RequestId, params: SkillsConfigWriteParams, } | { "method": "plugin/install", id: RequestId, params: PluginInstallParams, } | { "method": "plugin/uninstall", id: RequestId, params: PluginUninstallParams, } | { "method": "turn/start", id: RequestId, params: TurnStartParams, } | { "method": "turn/steer", id: RequestId, params: TurnSteerParams, } | { "method": "turn/interrupt", id: RequestId, params: TurnInterruptParams, } | { "method": "review/start", id: RequestId, params: ReviewStartParams, } | { "method": "model/list", id: RequestId, params: ModelListParams, } | { "method": "experimentalFeature/list", id: RequestId, params: ExperimentalFeatureListParams, } | { "method": "mcpServer/oauth/login", id: RequestId, params: McpServerOauthLoginParams, } | { "method": "config/mcpServer/reload", id: RequestId, params: undefined, } | { "method": "mcpServerStatus/list", id: RequestId, params: ListMcpServerStatusParams, } | { "method": "windowsSandbox/setupStart", id: RequestId, params: WindowsSandboxSetupStartParams, } | { "method": "account/login/start", id: RequestId, params: LoginAccountParams, } | { "method": "account/login/cancel", id: RequestId, params: CancelLoginAccountParams, } | { "method": "account/logout", id: RequestId, params: undefined, } | { "method": "account/rateLimits/read", id: RequestId, params: undefined, } | { "method": "feedback/upload", id: RequestId, params: FeedbackUploadParams, } | { "method": "command/exec", id: RequestId, params: CommandExecParams, } | { "method": "command/exec/write", id: RequestId, params: CommandExecWriteParams, } | { "method": "command/exec/terminate", id: RequestId, params: CommandExecTerminateParams, } | { "method": "command/exec/resize", id: RequestId, params: CommandExecResizeParams, } | { "method": "config/read", id: RequestId, params: ConfigReadParams, } | { "method": "externalAgentConfig/detect", id: RequestId, params: ExternalAgentConfigDetectParams, } | { "method": "externalAgentConfig/import", id: RequestId, params: ExternalAgentConfigImportParams, } | { "method": "config/value/write", id: RequestId, params: ConfigValueWriteParams, } | { "method": "config/batchWrite", id: RequestId, params: ConfigBatchWriteParams, } | { "method": "configRequirements/read", id: RequestId, params: undefined, } | { "method": "account/read", id: RequestId, params: GetAccountParams, } | { "method": "getConversationSummary", id: RequestId, params: GetConversationSummaryParams, } | { "method": "gitDiffToRemote", id: RequestId, params: GitDiffToRemoteParams, } | { "method": "getAuthStatus", id: RequestId, params: GetAuthStatusParams, } | { "method": "fuzzyFileSearch", id: RequestId, params: FuzzyFileSearchParams, };
@@ -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 CustomPrompt = { name: string, path: string, content: string, description: string | null, argument_hint: string | null, };
@@ -11,6 +11,7 @@ export type { CollaborationMode } from "./CollaborationMode";
export type { ContentItem } from "./ContentItem";
export type { ConversationGitInfo } from "./ConversationGitInfo";
export type { ConversationSummary } from "./ConversationSummary";
export type { CustomPrompt } from "./CustomPrompt";
export type { ExecCommandApprovalParams } from "./ExecCommandApprovalParams";
export type { ExecCommandApprovalResponse } from "./ExecCommandApprovalResponse";
export type { ExecPolicyAmendment } from "./ExecPolicyAmendment";
@@ -3,11 +3,11 @@
// This file was generated by [ts-rs](https://github.com/Aleph-Alpha/ts-rs). Do not edit this file manually.
import type { ConfigEdit } from "./ConfigEdit";
export type ConfigBatchWriteParams = { edits: Array<ConfigEdit>,
export type ConfigBatchWriteParams = { environmentId?: string | null, edits: Array<ConfigEdit>,
/**
* Path to the config file to write; defaults to the user's `config.toml` when omitted.
*/
filePath?: string | null, expectedVersion?: string | null,
filePath?: string | null, expectedVersion?: string | null,
/**
* When true, hot-reload the updated user config into all loaded threads after writing.
*/
@@ -2,7 +2,7 @@
// This file was generated by [ts-rs](https://github.com/Aleph-Alpha/ts-rs). Do not edit this file manually.
export type ConfigReadParams = { includeLayers: boolean,
export type ConfigReadParams = { includeLayers: boolean, environmentId?: string | null,
/**
* Optional working directory to resolve project config layers. If specified,
* return the effective config as seen from that directory (i.e., including any
@@ -5,4 +5,4 @@ import type { Config } from "./Config";
import type { ConfigLayer } from "./ConfigLayer";
import type { ConfigLayerMetadata } from "./ConfigLayerMetadata";
export type ConfigReadResponse = { config: Config, origins: { [key in string]?: ConfigLayerMetadata }, layers: Array<ConfigLayer> | null, };
export type ConfigReadResponse = { environmentId: string, config: Config, origins: { [key in string]?: ConfigLayerMetadata }, layers: Array<ConfigLayer> | null, };
@@ -4,7 +4,7 @@
import type { JsonValue } from "../serde_json/JsonValue";
import type { MergeStrategy } from "./MergeStrategy";
export type ConfigValueWriteParams = { keyPath: string, value: JsonValue, mergeStrategy: MergeStrategy,
export type ConfigValueWriteParams = { environmentId?: string | null, keyPath: string, value: JsonValue, mergeStrategy: MergeStrategy,
/**
* Path to the config file to write; defaults to the user's `config.toml` when omitted.
*/
@@ -0,0 +1,9 @@
// 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 PromptListParams = {
/**
* Optional execution environment that owns the custom prompt snapshot.
*/
environmentId?: string | null, };
@@ -0,0 +1,6 @@
// GENERATED CODE! DO NOT MODIFY BY HAND!
// This file was generated by [ts-rs](https://github.com/Aleph-Alpha/ts-rs). Do not edit this file manually.
import type { CustomPrompt } from "../CustomPrompt";
export type PromptListResponse = { environmentId: string, prompts: Array<CustomPrompt>, };
@@ -3,9 +3,9 @@
// This file was generated by [ts-rs](https://github.com/Aleph-Alpha/ts-rs). Do not edit this file manually.
/**
* Notification emitted when watched local skill files change.
* Notification emitted when watched skill files change for an execution environment.
*
* Treat this as an invalidation signal and re-run `skills/list` with the
* client's current parameters when refreshed skill metadata is needed.
*/
export type SkillsChangedNotification = Record<string, never>;
export type SkillsChangedNotification = { environmentId: string, };
@@ -3,15 +3,19 @@
// This file was generated by [ts-rs](https://github.com/Aleph-Alpha/ts-rs). Do not edit this file manually.
import type { SkillsListExtraRootsForCwd } from "./SkillsListExtraRootsForCwd";
export type SkillsListParams = {
export type SkillsListParams = {
/**
* Optional execution environment that owns the skill snapshot.
*/
environmentId?: string | null,
/**
* When empty, defaults to the current session working directory.
*/
cwds?: Array<string>,
cwds?: Array<string>,
/**
* When true, bypass the skills cache and re-scan skills from disk.
*/
forceReload?: boolean,
forceReload?: boolean,
/**
* Optional per-cwd extra roots to scan as user-scoped skills.
*/
@@ -3,4 +3,4 @@
// This file was generated by [ts-rs](https://github.com/Aleph-Alpha/ts-rs). Do not edit this file manually.
import type { SkillsListEntry } from "./SkillsListEntry";
export type SkillsListResponse = { data: Array<SkillsListEntry>, };
export type SkillsListResponse = { environmentId: string, data: Array<SkillsListEntry>, };
@@ -215,6 +215,8 @@ export type { PluginSummary } from "./PluginSummary";
export type { PluginUninstallParams } from "./PluginUninstallParams";
export type { PluginUninstallResponse } from "./PluginUninstallResponse";
export type { ProfileV2 } from "./ProfileV2";
export type { PromptListParams } from "./PromptListParams";
export type { PromptListResponse } from "./PromptListResponse";
export type { RateLimitSnapshot } from "./RateLimitSnapshot";
export type { RateLimitWindow } from "./RateLimitWindow";
export type { RawResponseItemCompletedNotification } from "./RawResponseItemCompletedNotification";
@@ -292,6 +292,10 @@ client_request_definitions! {
params: v2::ThreadReadParams,
response: v2::ThreadReadResponse,
},
PromptList => "prompt/list" {
params: v2::PromptListParams,
response: v2::PromptListResponse,
},
SkillsList => "skills/list" {
params: v2::SkillsListParams,
response: v2::SkillsListResponse,
@@ -793,6 +797,7 @@ server_request_definitions! {
#[serde(rename_all = "camelCase")]
#[ts(rename_all = "camelCase")]
pub struct FuzzyFileSearchParams {
pub environment_id: Option<String>,
pub query: String,
pub roots: Vec<String>,
// if provided, will cancel any previous request that used the same value
@@ -827,6 +832,7 @@ pub struct FuzzyFileSearchResponse {
#[serde(rename_all = "camelCase")]
#[ts(rename_all = "camelCase")]
pub struct FuzzyFileSearchSessionStartParams {
pub environment_id: Option<String>,
pub session_id: String,
pub roots: Vec<String>,
}
@@ -838,6 +844,7 @@ pub struct FuzzyFileSearchSessionStartResponse {}
#[serde(rename_all = "camelCase")]
#[ts(rename_all = "camelCase")]
pub struct FuzzyFileSearchSessionUpdateParams {
pub environment_id: Option<String>,
pub session_id: String,
pub query: String,
}
@@ -849,6 +856,7 @@ pub struct FuzzyFileSearchSessionUpdateResponse {}
#[serde(rename_all = "camelCase")]
#[ts(rename_all = "camelCase")]
pub struct FuzzyFileSearchSessionStopParams {
pub environment_id: Option<String>,
pub session_id: String,
}
@@ -1469,6 +1477,7 @@ mod tests {
let request = ClientRequest::FsGetMetadata {
request_id: RequestId::Integer(9),
params: v2::FsGetMetadataParams {
environment_id: None,
path: absolute_path("tmp/example"),
},
};
@@ -1477,6 +1486,7 @@ mod tests {
"method": "fs/getMetadata",
"id": 9,
"params": {
"environmentId": null,
"path": absolute_path_string("tmp/example")
}
}),
@@ -163,6 +163,7 @@ pub struct ExecCommandApprovalResponse {
#[derive(Serialize, Deserialize, Debug, Clone, PartialEq, JsonSchema, TS)]
#[serde(rename_all = "camelCase")]
pub struct GitDiffToRemoteParams {
pub environment_id: Option<String>,
pub cwd: PathBuf,
}
+295 -2
View File
@@ -25,6 +25,7 @@ use codex_protocol::config_types::ServiceTier;
use codex_protocol::config_types::Verbosity;
use codex_protocol::config_types::WebSearchMode;
use codex_protocol::config_types::WebSearchToolConfig;
use codex_protocol::custom_prompts::CustomPrompt;
use codex_protocol::items::AgentMessageContent as CoreAgentMessageContent;
use codex_protocol::items::TurnItem as CoreTurnItem;
use codex_protocol::mcp::Resource as McpResource;
@@ -799,6 +800,8 @@ pub enum ConfigWriteErrorCode {
pub struct ConfigReadParams {
#[serde(default)]
pub include_layers: bool,
#[ts(optional = nullable)]
pub environment_id: Option<String>,
/// Optional working directory to resolve project config layers. If specified,
/// return the effective config as seen from that directory (i.e., including any
/// project layers between `cwd` and the project/repo root).
@@ -810,6 +813,7 @@ pub struct ConfigReadParams {
#[serde(rename_all = "camelCase")]
#[ts(export_to = "v2/")]
pub struct ConfigReadResponse {
pub environment_id: String,
#[experimental(nested)]
pub config: Config,
pub origins: HashMap<String, ConfigLayerMetadata>,
@@ -925,6 +929,8 @@ pub struct ExternalAgentConfigImportResponse {}
#[serde(rename_all = "camelCase")]
#[ts(export_to = "v2/")]
pub struct ConfigValueWriteParams {
#[ts(optional = nullable)]
pub environment_id: Option<String>,
pub key_path: String,
pub value: JsonValue,
pub merge_strategy: MergeStrategy,
@@ -939,6 +945,8 @@ pub struct ConfigValueWriteParams {
#[serde(rename_all = "camelCase")]
#[ts(export_to = "v2/")]
pub struct ConfigBatchWriteParams {
#[ts(optional = nullable)]
pub environment_id: Option<String>,
pub edits: Vec<ConfigEdit>,
/// Path to the config file to write; defaults to the user's `config.toml` when omitted.
#[ts(optional = nullable)]
@@ -2123,6 +2131,8 @@ pub struct FeedbackUploadResponse {
#[serde(rename_all = "camelCase")]
#[ts(export_to = "v2/")]
pub struct FsReadFileParams {
#[ts(optional = nullable)]
pub environment_id: Option<String>,
/// Absolute path to read.
pub path: AbsolutePathBuf,
}
@@ -2141,6 +2151,8 @@ pub struct FsReadFileResponse {
#[serde(rename_all = "camelCase")]
#[ts(export_to = "v2/")]
pub struct FsWriteFileParams {
#[ts(optional = nullable)]
pub environment_id: Option<String>,
/// Absolute path to write.
pub path: AbsolutePathBuf,
/// File contents encoded as base64.
@@ -2158,6 +2170,8 @@ pub struct FsWriteFileResponse {}
#[serde(rename_all = "camelCase")]
#[ts(export_to = "v2/")]
pub struct FsCreateDirectoryParams {
#[ts(optional = nullable)]
pub environment_id: Option<String>,
/// Absolute directory path to create.
pub path: AbsolutePathBuf,
/// Whether parent directories should also be created. Defaults to `true`.
@@ -2176,6 +2190,8 @@ pub struct FsCreateDirectoryResponse {}
#[serde(rename_all = "camelCase")]
#[ts(export_to = "v2/")]
pub struct FsGetMetadataParams {
#[ts(optional = nullable)]
pub environment_id: Option<String>,
/// Absolute path to inspect.
pub path: AbsolutePathBuf,
}
@@ -2202,6 +2218,8 @@ pub struct FsGetMetadataResponse {
#[serde(rename_all = "camelCase")]
#[ts(export_to = "v2/")]
pub struct FsReadDirectoryParams {
#[ts(optional = nullable)]
pub environment_id: Option<String>,
/// Absolute directory path to read.
pub path: AbsolutePathBuf,
}
@@ -2233,6 +2251,8 @@ pub struct FsReadDirectoryResponse {
#[serde(rename_all = "camelCase")]
#[ts(export_to = "v2/")]
pub struct FsRemoveParams {
#[ts(optional = nullable)]
pub environment_id: Option<String>,
/// Absolute path to remove.
pub path: AbsolutePathBuf,
/// Whether directory removal should recurse. Defaults to `true`.
@@ -2254,6 +2274,8 @@ pub struct FsRemoveResponse {}
#[serde(rename_all = "camelCase")]
#[ts(export_to = "v2/")]
pub struct FsCopyParams {
#[ts(optional = nullable)]
pub environment_id: Option<String>,
/// Absolute source path.
pub source_path: AbsolutePathBuf,
/// Absolute destination path.
@@ -2948,6 +2970,10 @@ pub struct ThreadRollbackResponse {
#[serde(rename_all = "camelCase")]
#[ts(export_to = "v2/")]
pub struct ThreadListParams {
/// Optional execution environment that owns the thread snapshot.
#[serde(default)]
#[ts(optional = nullable)]
pub environment_id: Option<String>,
/// Opaque pagination cursor returned by a previous call.
#[ts(optional = nullable)]
pub cursor: Option<String>,
@@ -3018,6 +3044,10 @@ pub struct ThreadListResponse {
#[serde(rename_all = "camelCase")]
#[ts(export_to = "v2/")]
pub struct ThreadLoadedListParams {
/// Optional execution environment that owns the loaded thread sessions.
#[serde(default)]
#[ts(optional = nullable)]
pub environment_id: Option<String>,
/// Opaque pagination cursor returned by a previous call.
#[ts(optional = nullable)]
pub cursor: Option<String>,
@@ -3064,6 +3094,10 @@ pub enum ThreadActiveFlag {
#[serde(rename_all = "camelCase")]
#[ts(export_to = "v2/")]
pub struct ThreadReadParams {
/// Optional execution environment that owns the thread snapshot.
#[serde(default)]
#[ts(optional = nullable)]
pub environment_id: Option<String>,
pub thread_id: String,
/// When true, include turns and their items from rollout history.
#[serde(default)]
@@ -3077,6 +3111,24 @@ pub struct ThreadReadResponse {
pub thread: Thread,
}
#[derive(Serialize, Deserialize, Debug, Clone, PartialEq, JsonSchema, TS)]
#[serde(rename_all = "camelCase")]
#[ts(export_to = "v2/")]
pub struct PromptListParams {
/// Optional execution environment that owns the custom prompt snapshot.
#[serde(default)]
#[ts(optional = nullable)]
pub environment_id: Option<String>,
}
#[derive(Serialize, Deserialize, Debug, Clone, PartialEq, JsonSchema, TS)]
#[serde(rename_all = "camelCase")]
#[ts(export_to = "v2/")]
pub struct PromptListResponse {
pub environment_id: String,
pub prompts: Vec<CustomPrompt>,
}
#[derive(Serialize, Deserialize, Debug, Clone, PartialEq, JsonSchema, TS)]
#[serde(rename_all = "camelCase")]
#[ts(export_to = "v2/")]
@@ -3120,6 +3172,10 @@ pub struct SkillsListResponse {
#[serde(rename_all = "camelCase")]
#[ts(export_to = "v2/")]
pub struct PluginListParams {
/// Optional execution environment that owns plugin discovery.
#[serde(default)]
#[ts(optional = nullable)]
pub environment_id: Option<String>,
/// Optional working directories used to discover repo marketplaces. When omitted,
/// only home-scoped marketplaces and the official curated marketplace are considered.
#[ts(optional = nullable)]
@@ -3144,6 +3200,10 @@ pub struct PluginListResponse {
#[serde(rename_all = "camelCase")]
#[ts(export_to = "v2/")]
pub struct PluginReadParams {
/// Optional execution environment that owns plugin discovery.
#[serde(default)]
#[ts(optional = nullable)]
pub environment_id: Option<String>,
pub marketplace_path: AbsolutePathBuf,
pub plugin_name: String,
}
@@ -3382,6 +3442,9 @@ pub struct SkillsConfigWriteResponse {
#[serde(rename_all = "camelCase")]
#[ts(export_to = "v2/")]
pub struct PluginInstallParams {
#[serde(default)]
#[ts(optional = nullable)]
pub environment_id: Option<String>,
pub marketplace_path: AbsolutePathBuf,
pub plugin_name: String,
/// When true, apply the remote plugin change before the local install flow.
@@ -3401,6 +3464,9 @@ pub struct PluginInstallResponse {
#[serde(rename_all = "camelCase")]
#[ts(export_to = "v2/")]
pub struct PluginUninstallParams {
#[serde(default)]
#[ts(optional = nullable)]
pub environment_id: Option<String>,
pub plugin_id: String,
/// When true, apply the remote plugin change before the local uninstall flow.
#[serde(default, skip_serializing_if = "std::ops::Not::not")]
@@ -3498,6 +3564,8 @@ impl From<CoreSkillErrorInfo> for SkillErrorInfo {
#[ts(export_to = "v2/")]
pub struct Thread {
pub id: String,
/// Execution environment that owns this thread snapshot.
pub environment_id: String,
/// Usually the first user message in the thread, if available.
pub preview: String,
/// Whether the thread is ephemeral and should not be materialized on disk.
@@ -4705,11 +4773,13 @@ pub struct ThreadClosedNotification {
#[derive(Serialize, Deserialize, Debug, Clone, PartialEq, JsonSchema, TS)]
#[serde(rename_all = "camelCase")]
#[ts(export_to = "v2/")]
/// Notification emitted when watched local skill files change.
/// Notification emitted when watched skill files change for an execution environment.
///
/// Treat this as an invalidation signal and re-run `skills/list` with the
/// client's current parameters when refreshed skill metadata is needed.
pub struct SkillsChangedNotification {}
pub struct SkillsChangedNotification {
pub environment_id: String,
}
#[derive(Serialize, Deserialize, Debug, Clone, PartialEq, JsonSchema, TS)]
#[serde(rename_all = "camelCase")]
@@ -5943,6 +6013,7 @@ mod tests {
fn thread_fixture() -> Thread {
Thread {
id: "thr_123".to_string(),
environment_id: "env-1".to_string(),
preview: "Preview".to_string(),
ephemeral: false,
model_provider: "openai".to_string(),
@@ -6342,6 +6413,7 @@ mod tests {
#[test]
fn fs_read_file_params_round_trip() {
let params = FsReadFileParams {
environment_id: None,
path: absolute_path("tmp/example.txt"),
};
@@ -6349,6 +6421,7 @@ mod tests {
assert_eq!(
value,
json!({
"environmentId": null,
"path": absolute_path_string("tmp/example.txt"),
})
);
@@ -6361,6 +6434,7 @@ mod tests {
#[test]
fn fs_create_directory_params_round_trip_with_default_recursive() {
let params = FsCreateDirectoryParams {
environment_id: None,
path: absolute_path("tmp/example"),
recursive: None,
};
@@ -6369,6 +6443,7 @@ mod tests {
assert_eq!(
value,
json!({
"environmentId": null,
"path": absolute_path_string("tmp/example"),
"recursive": null,
})
@@ -6382,6 +6457,7 @@ mod tests {
#[test]
fn fs_write_file_params_round_trip_with_base64_data() {
let params = FsWriteFileParams {
environment_id: None,
path: absolute_path("tmp/example.bin"),
data_base64: "AAE=".to_string(),
};
@@ -6390,6 +6466,7 @@ mod tests {
assert_eq!(
value,
json!({
"environmentId": null,
"path": absolute_path_string("tmp/example.bin"),
"dataBase64": "AAE=",
})
@@ -6403,6 +6480,7 @@ mod tests {
#[test]
fn fs_copy_params_round_trip_with_recursive_directory_copy() {
let params = FsCopyParams {
environment_id: None,
source_path: absolute_path("tmp/source"),
destination_path: absolute_path("tmp/destination"),
recursive: true,
@@ -6412,6 +6490,7 @@ mod tests {
assert_eq!(
value,
json!({
"environmentId": null,
"sourcePath": absolute_path_string("tmp/source"),
"destinationPath": absolute_path_string("tmp/destination"),
"recursive": true,
@@ -6835,6 +6914,7 @@ mod tests {
json!({
"thread": {
"id": "thr_123",
"environmentId": "env-1",
"preview": "Preview",
"ephemeral": false,
"modelProvider": "openai",
@@ -6896,6 +6976,7 @@ mod tests {
json!({
"thread": {
"id": "thr_123",
"environmentId": "env-1",
"preview": "Preview",
"ephemeral": false,
"modelProvider": "openai",
@@ -6957,6 +7038,7 @@ mod tests {
json!({
"thread": {
"id": "thr_123",
"environmentId": "env-1",
"preview": "Preview",
"ephemeral": false,
"modelProvider": "openai",
@@ -8058,32 +8140,235 @@ mod tests {
);
}
#[test]
fn thread_list_params_round_trip_environment_id() {
let params = ThreadListParams {
environment_id: Some("env-1".to_string()),
cursor: Some("cursor-1".to_string()),
limit: Some(25),
sort_key: Some(ThreadSortKey::UpdatedAt),
model_providers: None,
source_kinds: None,
archived: Some(false),
cwd: Some("/repo".to_string()),
search_term: Some("agent".to_string()),
};
assert_eq!(
serde_json::to_value(&params).expect("serialize thread/list params"),
json!({
"environmentId": "env-1",
"cursor": "cursor-1",
"limit": 25,
"sortKey": "updated_at",
"modelProviders": null,
"sourceKinds": null,
"archived": false,
"cwd": "/repo",
"searchTerm": "agent",
}),
);
let decoded =
serde_json::from_value::<ThreadListParams>(serde_json::to_value(&params).unwrap())
.expect("deserialize thread/list params");
assert_eq!(decoded, params);
}
#[test]
fn thread_loaded_list_params_round_trip_environment_id() {
let params = ThreadLoadedListParams {
environment_id: Some("env-1".to_string()),
cursor: Some("cursor-1".to_string()),
limit: Some(10),
};
assert_eq!(
serde_json::to_value(&params).expect("serialize thread/loaded/list params"),
json!({
"environmentId": "env-1",
"cursor": "cursor-1",
"limit": 10,
}),
);
let decoded = serde_json::from_value::<ThreadLoadedListParams>(
serde_json::to_value(&params).unwrap(),
)
.expect("deserialize thread/loaded/list params");
assert_eq!(decoded, params);
}
#[test]
fn thread_read_params_round_trip_environment_id() {
let params = ThreadReadParams {
environment_id: Some("env-2".to_string()),
thread_id: "thread-1".to_string(),
include_turns: true,
};
assert_eq!(
serde_json::to_value(&params).expect("serialize thread/read params"),
json!({
"environmentId": "env-2",
"threadId": "thread-1",
"includeTurns": true,
}),
);
let decoded =
serde_json::from_value::<ThreadReadParams>(serde_json::to_value(&params).unwrap())
.expect("deserialize thread/read params");
assert_eq!(decoded, params);
}
#[test]
fn thread_serialization_includes_environment_id() {
assert_eq!(
serde_json::to_value(thread_fixture()).expect("serialize thread"),
json!({
"id": "thr_123",
"environmentId": "env-1",
"preview": "Preview",
"ephemeral": false,
"modelProvider": "openai",
"createdAt": 1,
"updatedAt": 2,
"status": {"type": "idle"},
"path": absolute_path_string("tmp/thread"),
"cwd": absolute_path_string("tmp"),
"cliVersion": "1.0.0",
"source": "exec",
"agentNickname": null,
"agentRole": null,
"gitInfo": null,
"name": "Example",
"turns": [],
}),
);
}
#[test]
fn plugin_list_params_serialization_uses_force_remote_sync() {
assert_eq!(
serde_json::to_value(PluginListParams {
environment_id: None,
cwds: None,
force_remote_sync: false,
})
.unwrap(),
json!({
"environmentId": null,
"cwds": null,
}),
);
assert_eq!(
serde_json::to_value(PluginListParams {
environment_id: Some("env-1".to_string()),
cwds: None,
force_remote_sync: true,
})
.unwrap(),
json!({
"environmentId": "env-1",
"cwds": null,
"forceRemoteSync": true,
}),
);
}
#[test]
fn plugin_read_params_round_trip_environment_id() {
let marketplace_path = absolute_path("plugins/marketplace.json");
let params = PluginReadParams {
environment_id: Some("env-2".to_string()),
marketplace_path: marketplace_path.clone(),
plugin_name: "gmail".to_string(),
};
assert_eq!(
serde_json::to_value(&params).expect("serialize plugin/read params"),
json!({
"environmentId": "env-2",
"marketplacePath": marketplace_path.as_path().display().to_string(),
"pluginName": "gmail",
}),
);
let decoded =
serde_json::from_value::<PluginReadParams>(serde_json::to_value(&params).unwrap())
.expect("deserialize plugin/read params");
assert_eq!(decoded, params);
}
#[test]
fn fs_get_metadata_params_round_trip_environment_id() {
let params = FsGetMetadataParams {
environment_id: Some("env-1".to_string()),
path: absolute_path("tmp/example.txt"),
};
assert_eq!(
serde_json::to_value(&params).expect("serialize fs/getMetadata params"),
json!({
"environmentId": "env-1",
"path": absolute_path_string("tmp/example.txt"),
}),
);
let decoded =
serde_json::from_value::<FsGetMetadataParams>(serde_json::to_value(&params).unwrap())
.expect("deserialize fs/getMetadata params");
assert_eq!(decoded, params);
}
#[test]
fn fs_read_directory_params_round_trip_environment_id() {
let params = FsReadDirectoryParams {
environment_id: Some("env-2".to_string()),
path: absolute_path("tmp/example-dir"),
};
assert_eq!(
serde_json::to_value(&params).expect("serialize fs/readDirectory params"),
json!({
"environmentId": "env-2",
"path": absolute_path_string("tmp/example-dir"),
}),
);
let decoded =
serde_json::from_value::<FsReadDirectoryParams>(serde_json::to_value(&params).unwrap())
.expect("deserialize fs/readDirectory params");
assert_eq!(decoded, params);
}
#[test]
fn fs_remove_params_round_trip_environment_id() {
let params = FsRemoveParams {
environment_id: Some("env-3".to_string()),
path: absolute_path("tmp/example-dir"),
recursive: None,
force: None,
};
assert_eq!(
serde_json::to_value(&params).expect("serialize fs/remove params"),
json!({
"environmentId": "env-3",
"path": absolute_path_string("tmp/example-dir"),
"recursive": null,
"force": null,
}),
);
let decoded =
serde_json::from_value::<FsRemoveParams>(serde_json::to_value(&params).unwrap())
.expect("deserialize fs/remove params");
assert_eq!(decoded, params);
}
#[test]
fn plugin_install_params_serialization_uses_force_remote_sync() {
let marketplace_path = if cfg!(windows) {
@@ -8095,12 +8380,14 @@ mod tests {
let marketplace_path_json = marketplace_path.as_path().display().to_string();
assert_eq!(
serde_json::to_value(PluginInstallParams {
environment_id: None,
marketplace_path: marketplace_path.clone(),
plugin_name: "gmail".to_string(),
force_remote_sync: false,
})
.unwrap(),
json!({
"environmentId": null,
"marketplacePath": marketplace_path_json,
"pluginName": "gmail",
}),
@@ -8108,12 +8395,14 @@ mod tests {
assert_eq!(
serde_json::to_value(PluginInstallParams {
environment_id: Some("env-1".to_string()),
marketplace_path,
plugin_name: "gmail".to_string(),
force_remote_sync: true,
})
.unwrap(),
json!({
"environmentId": "env-1",
"marketplacePath": marketplace_path_json,
"pluginName": "gmail",
"forceRemoteSync": true,
@@ -8125,22 +8414,26 @@ mod tests {
fn plugin_uninstall_params_serialization_uses_force_remote_sync() {
assert_eq!(
serde_json::to_value(PluginUninstallParams {
environment_id: None,
plugin_id: "gmail@openai-curated".to_string(),
force_remote_sync: false,
})
.unwrap(),
json!({
"environmentId": null,
"pluginId": "gmail@openai-curated",
}),
);
assert_eq!(
serde_json::to_value(PluginUninstallParams {
environment_id: Some("env-2".to_string()),
plugin_id: "gmail@openai-curated".to_string(),
force_remote_sync: true,
})
.unwrap(),
json!({
"environmentId": "env-2",
"pluginId": "gmail@openai-curated",
"forceRemoteSync": true,
}),
@@ -1101,6 +1101,7 @@ async fn thread_list(endpoint: &Endpoint, config_overrides: &[String], limit: u3
println!("< initialize response: {initialize:?}");
let response = client.thread_list(ThreadListParams {
environment_id: None,
cursor: None,
limit: Some(limit),
sort_key: None,
+26 -3
View File
@@ -163,10 +163,11 @@ Example with notification opt-out:
- `model/list` — list available models (set `includeHidden: true` to include entries with `hidden: true`), with reasoning effort options, optional legacy `upgrade` model ids, optional `upgradeInfo` metadata (`model`, `upgradeCopy`, `modelLink`, `migrationMarkdown`), and optional `availabilityNux` metadata.
- `experimentalFeature/list` — list feature flags with stage metadata (`beta`, `underDevelopment`, `stable`, etc.), enabled/default-enabled state, and cursor pagination. For non-beta flags, `displayName`/`description`/`announcement` are `null`.
- `collaborationMode/list` — list available collaboration mode presets (experimental, no pagination). This response omits built-in developer instructions; clients should either pass `settings.developer_instructions: null` when setting a mode to use Codex's built-in instructions, or provide their own instructions explicitly.
- `prompt/list` — list custom prompt markdown files from the resolved `$CODEX_HOME/prompts` directory for an optional `environmentId`; the response echoes the resolved `environmentId`.
- `skills/list` — list skills for one or more `cwd` values (optional `forceReload`) within an optional `environmentId`; the response echoes the resolved `environmentId`.
- `plugin/list` — list discovered plugin marketplaces and plugin state, including effective marketplace install/auth policy metadata and best-effort `featuredPluginIds` for the official curated marketplace. `interface.category` uses the marketplace category when present; otherwise it falls back to the plugin manifest category. Pass `forceRemoteSync: true` to refresh curated plugin state before listing (**under development; do not call from production clients yet**).
- `plugin/read` — read one plugin by `marketplacePath` plus `pluginName`, returning marketplace info, a list-style `summary`, manifest descriptions/interface metadata, and bundled skills/apps/MCP server names (**under development; do not call from production clients yet**).
- `skills/changed` — notification emitted when watched local skill files change.
- `skills/changed` — notification emitted when watched skill files change for a specific `environmentId`.
- `app/list` — list available apps.
- `skills/config/write` — write user-level skill config by path.
- `plugin/install` — install a plugin from a discovered marketplace entry, rejecting marketplace entries marked unavailable for install, and return the effective plugin auth policy plus any apps that still need auth (**under development; do not call from production clients yet**).
@@ -1097,7 +1098,7 @@ Use `skills/list` to fetch the available skills (optionally scoped by `cwds`, wi
You can also add `perCwdExtraUserRoots` to scan additional absolute paths as `user` scope for specific `cwd` entries.
Entries whose `cwd` is not present in `cwds` are ignored.
`skills/list` might reuse a cached skills result per `cwd`; setting `forceReload` to `true` refreshes the result from disk. The response includes the resolved `environmentId` so clients can cache and invalidate by execution environment.
The server also emits `skills/changed` notifications when watched local skill files change. Treat this as an invalidation signal and re-run `skills/list` with your current params when needed.
The server also emits `skills/changed` notifications when watched skill files change for a specific execution environment. Treat this as an invalidation signal and re-run `skills/list` with your current params when needed.
```json
{ "method": "skills/list", "id": 25, "params": {
@@ -1138,10 +1139,32 @@ The server also emits `skills/changed` notifications when watched local skill fi
```json
{
"method": "skills/changed",
"params": {}
"params": {
"environmentId": "local"
}
}
```
Use `prompt/list` to fetch custom prompt markdown files from `$CODEX_HOME/prompts` for a specific execution environment. The response echoes the resolved `environmentId` so clients can keep prompt caches aligned with the executor environment they are talking to.
```json
{ "method": "prompt/list", "id": 27, "params": {
"environmentId": "local"
} }
{ "id": 27, "result": {
"environmentId": "local",
"prompts": [
{
"name": "bug-triage",
"path": "/Users/me/.codex/prompts/bug-triage.md",
"description": "Expand a terse bug note into a triage request",
"argumentHint": "<bug-id>",
"content": "Investigate bug {{args}} and summarize likely causes."
}
]
} }
```
To enable or disable a skill by path:
```json
@@ -261,6 +261,7 @@ pub(crate) async fn apply_bespoke_event_handling(
api_version: ApiVersion,
fallback_model_provider: String,
codex_home: &Path,
default_environment_id: &str,
) {
let Event {
id: event_turn_id,
@@ -305,7 +306,9 @@ pub(crate) async fn apply_bespoke_event_handling(
if let ApiVersion::V2 = api_version {
outgoing
.send_server_notification(ServerNotification::SkillsChanged(
SkillsChangedNotification {},
SkillsChangedNotification {
environment_id: default_environment_id.to_string(),
},
))
.await;
}
@@ -1756,7 +1759,7 @@ pub(crate) async fn apply_bespoke_event_handling(
.await
{
Ok(summary) => {
let mut thread = summary_to_thread(summary);
let mut thread = summary_to_thread(summary, default_environment_id);
match read_rollout_items_from_rollout(rollout_path.as_path()).await {
Ok(items) => {
thread.turns = build_turns_from_rollout_items(&items);
File diff suppressed because it is too large Load Diff
+255 -129
View File
@@ -25,10 +25,17 @@ use codex_core::exec::ExecExpiration;
use codex_core::exec::IO_DRAIN_TIMEOUT_MS;
use codex_core::exec::SandboxType;
use codex_core::sandboxing::ExecRequest;
use codex_exec_server::Environment;
use codex_exec_server::ExecOutputStream as ExecutorOutputStream;
use codex_exec_server::ExecParams as ExecutorExecParams;
use codex_exec_server::ExecProcess;
use codex_exec_server::ExecResizeParams as ExecutorExecResizeParams;
use codex_exec_server::ExecWaitParams as ExecutorExecWaitParams;
use codex_exec_server::ReadParams as ExecutorReadParams;
use codex_exec_server::WriteParams as ExecutorWriteParams;
use codex_utils_pty::DEFAULT_OUTPUT_BYTES_CAP;
use codex_utils_pty::ProcessHandle;
use codex_utils_pty::SpawnedProcess;
use codex_utils_pty::TerminalSize;
use std::time::Instant;
use tokio::sync::Mutex;
use tokio::sync::mpsc;
use tokio::sync::oneshot;
@@ -89,6 +96,7 @@ struct CommandControlRequest {
pub(crate) struct StartCommandExecParams {
pub(crate) outgoing: Arc<OutgoingMessageSender>,
pub(crate) request_id: ConnectionRequestId,
pub(crate) experimental_exec_server_url: Option<String>,
pub(crate) environment_id: String,
pub(crate) process_id: Option<String>,
pub(crate) exec_request: ExecRequest,
@@ -100,12 +108,14 @@ pub(crate) struct StartCommandExecParams {
pub(crate) size: Option<TerminalSize>,
}
struct RunCommandParams {
struct RunExecutorCommandParams {
outgoing: Arc<OutgoingMessageSender>,
request_id: ConnectionRequestId,
process_id: Option<String>,
spawned: SpawnedProcess,
executor_process_id: String,
executor: Arc<dyn ExecProcess>,
control_rx: mpsc::Receiver<CommandControlRequest>,
tty: bool,
stream_stdin: bool,
stream_stdout_stderr: bool,
expiration: ExecExpiration,
@@ -150,6 +160,7 @@ impl CommandExecManager {
let StartCommandExecParams {
outgoing,
request_id,
experimental_exec_server_url,
environment_id,
process_id,
exec_request,
@@ -277,46 +288,75 @@ impl CommandExecManager {
process_id = %process_key.process_id.error_repr(),
"command/exec start"
);
let spawned = if tty {
codex_utils_pty::spawn_pty_process(
program,
args,
cwd.as_path(),
&env,
&arg0,
size.unwrap_or_default(),
)
.await
} else if stream_stdin {
codex_utils_pty::spawn_pipe_process(program, args, cwd.as_path(), &env, &arg0).await
} else {
codex_utils_pty::spawn_pipe_process_no_stdin(program, args, cwd.as_path(), &env, &arg0)
.await
};
let spawned = match spawned {
Ok(spawned) => spawned,
let environment = match Environment::create(experimental_exec_server_url).await {
Ok(environment) => environment,
Err(err) => {
self.sessions.lock().await.remove(&process_key);
return Err(internal_error(format!("failed to spawn command: {err}")));
return Err(internal_error(format!(
"failed to bind environment for command/exec: {err}"
)));
}
};
tokio::spawn(async move {
let _started_network_proxy = started_network_proxy;
run_command(RunCommandParams {
outgoing,
request_id: request_id.clone(),
process_id: notification_process_id,
spawned,
control_rx,
stream_stdin,
stream_stdout_stderr,
expiration,
output_bytes_cap,
let executor = environment.get_executor();
let executor_process_id = process_key.process_id.error_repr();
let start_result = executor
.start(ExecutorExecParams {
process_id: executor_process_id.clone(),
argv: {
let mut argv = Vec::with_capacity(1 + args.len());
argv.push(program.to_string());
argv.extend(args.iter().cloned());
argv
},
cwd,
env,
tty,
stdin: stream_stdin,
arg0,
})
.await;
sessions.lock().await.remove(&process_key);
});
Ok(())
let Err(err) = start_result else {
if tty
&& let Some(size) = size
&& let Err(err) = executor
.resize(ExecutorExecResizeParams {
process_id: executor_process_id.clone(),
size: codex_exec_server::ExecTerminalSize {
rows: size.rows,
cols: size.cols,
},
})
.await
{
let _ = executor.terminate(&executor_process_id).await;
self.sessions.lock().await.remove(&process_key);
return Err(internal_error(format!(
"failed to initialize command PTY size: {err}"
)));
}
let executor = Arc::clone(&executor);
tokio::spawn(async move {
let _started_network_proxy = started_network_proxy;
run_command_with_executor(RunExecutorCommandParams {
outgoing,
request_id: request_id.clone(),
process_id: notification_process_id,
executor_process_id,
executor,
control_rx,
tty,
stream_stdin,
stream_stdout_stderr,
expiration,
output_bytes_cap,
})
.await;
sessions.lock().await.remove(&process_key);
});
return Ok(());
};
self.sessions.lock().await.remove(&process_key);
return Err(internal_error(format!("failed to spawn command: {err}")));
}
pub(crate) async fn write(
@@ -484,13 +524,15 @@ impl CommandExecManager {
}
}
async fn run_command(params: RunCommandParams) {
let RunCommandParams {
async fn run_command_with_executor(params: RunExecutorCommandParams) {
let RunExecutorCommandParams {
outgoing,
request_id,
process_id,
spawned,
executor_process_id,
executor,
control_rx,
tty,
stream_stdin,
stream_stdout_stderr,
expiration,
@@ -510,104 +552,218 @@ async fn run_command(params: RunCommandParams) {
}
};
tokio::pin!(expiration);
let SpawnedProcess {
session,
stdout_rx,
stderr_rx,
exit_rx,
} = spawned;
tokio::pin!(exit_rx);
let mut after_seq = 0_u64;
let mut timed_out = false;
let (stdio_timeout_tx, stdio_timeout_rx) = watch::channel(false);
let mut exit_code = -1;
let mut exit_deadline: Option<Instant> = None;
let mut stdout = Vec::new();
let mut stderr = Vec::new();
let mut stdout_observed_num_bytes = 0_usize;
let mut stderr_observed_num_bytes = 0_usize;
let stdout_handle = spawn_process_output(SpawnProcessOutputParams {
connection_id: request_id.connection_id,
process_id: process_id.clone(),
output_rx: stdout_rx,
stdio_timeout_rx: stdio_timeout_rx.clone(),
outgoing: Arc::clone(&outgoing),
stream: CommandExecOutputStream::Stdout,
stream_output: stream_stdout_stderr,
output_bytes_cap,
});
let stderr_handle = spawn_process_output(SpawnProcessOutputParams {
connection_id: request_id.connection_id,
process_id,
output_rx: stderr_rx,
stdio_timeout_rx,
outgoing: Arc::clone(&outgoing),
stream: CommandExecOutputStream::Stderr,
stream_output: stream_stdout_stderr,
output_bytes_cap,
});
let exit_code = loop {
loop {
tokio::select! {
control = control_rx.recv(), if control_open => {
match control {
Some(CommandControlRequest { control, response_tx }) => {
let result = match control {
CommandControl::Write { delta, close_stdin } => {
handle_process_write(
&session,
handle_executor_write(
&executor,
&executor_process_id,
stream_stdin,
delta,
close_stdin,
).await
)
.await
}
CommandControl::Resize { size } => {
handle_process_resize(&session, size)
handle_executor_resize(&executor, &executor_process_id, tty, size).await
}
CommandControl::Terminate => {
session.request_terminate();
Ok(())
handle_executor_terminate(&executor, &executor_process_id).await
}
};
if let Some(response_tx) = response_tx {
let _ = response_tx.send(result);
}
},
}
None => {
control_open = false;
session.request_terminate();
let _ = handle_executor_terminate(&executor, &executor_process_id).await;
}
}
}
_ = &mut expiration, if !timed_out => {
timed_out = true;
session.request_terminate();
let _ = handle_executor_terminate(&executor, &executor_process_id).await;
}
exit = &mut exit_rx => {
if timed_out {
break EXEC_TIMEOUT_EXIT_CODE;
} else {
break exit.unwrap_or(-1);
read = executor.read(ExecutorReadParams {
process_id: executor_process_id.clone(),
after_seq: Some(after_seq),
max_bytes: None,
wait_ms: Some(50),
}) => {
let read = match read {
Ok(read) => read,
Err(err) => {
outgoing
.send_error(
request_id,
internal_error(format!("failed to read command output: {err}")),
)
.await;
return;
}
};
after_seq = read.next_seq;
for chunk in read.chunks {
let output_stream = match chunk.stream {
ExecutorOutputStream::Stdout | ExecutorOutputStream::Pty => {
CommandExecOutputStream::Stdout
}
ExecutorOutputStream::Stderr => CommandExecOutputStream::Stderr,
};
let chunk = chunk.chunk.into_inner();
let (buffer, observed_num_bytes) = match output_stream {
CommandExecOutputStream::Stdout => {
(&mut stdout, &mut stdout_observed_num_bytes)
}
CommandExecOutputStream::Stderr => {
(&mut stderr, &mut stderr_observed_num_bytes)
}
};
let capped_chunk = match output_bytes_cap {
Some(output_bytes_cap) => {
let capped_chunk_len = output_bytes_cap
.saturating_sub(*observed_num_bytes)
.min(chunk.len());
*observed_num_bytes += capped_chunk_len;
&chunk[0..capped_chunk_len]
}
None => chunk.as_slice(),
};
let cap_reached = Some(*observed_num_bytes) == output_bytes_cap;
if stream_stdout_stderr {
if let Some(process_id) = process_id.as_ref() && !capped_chunk.is_empty() {
outgoing
.send_server_notification_to_connections(
&[request_id.connection_id],
ServerNotification::CommandExecOutputDelta(
CommandExecOutputDeltaNotification {
process_id: process_id.clone(),
stream: output_stream,
delta_base64: STANDARD.encode(capped_chunk),
cap_reached,
},
),
)
.await;
}
} else {
buffer.extend_from_slice(capped_chunk);
}
}
if read.exited {
if timed_out {
exit_code = EXEC_TIMEOUT_EXIT_CODE;
} else {
exit_code = read.exit_code.unwrap_or(-1);
}
exit_deadline.get_or_insert_with(|| {
Instant::now() + Duration::from_millis(IO_DRAIN_TIMEOUT_MS)
});
}
}
}
};
let timeout_handle = tokio::spawn(async move {
tokio::time::sleep(Duration::from_millis(IO_DRAIN_TIMEOUT_MS)).await;
let _ = stdio_timeout_tx.send(true);
});
let stdout = stdout_handle.await.unwrap_or_default();
let stderr = stderr_handle.await.unwrap_or_default();
timeout_handle.abort();
if let Some(deadline) = exit_deadline
&& Instant::now() >= deadline
{
break;
}
if !control_open && exit_deadline.is_none() && !timed_out {
continue;
}
if exit_deadline.is_some() && Instant::now() < exit_deadline.expect("checked above") {
continue;
}
if exit_deadline.is_none() {
continue;
}
}
outgoing
.send_response(
request_id,
CommandExecResponse {
exit_code,
stdout,
stderr,
stdout: bytes_to_string_smart(&stdout),
stderr: bytes_to_string_smart(&stderr),
},
)
.await;
}
async fn handle_executor_write(
executor: &Arc<dyn ExecProcess>,
process_id: &str,
stream_stdin: bool,
delta: Vec<u8>,
close_stdin: bool,
) -> Result<(), JSONRPCErrorError> {
if !stream_stdin {
return Err(invalid_request(
"stdin streaming is not enabled for this command/exec".to_string(),
));
}
executor
.write(ExecutorWriteParams {
process_id: process_id.to_string(),
chunk: delta.into(),
close_stdin,
})
.await
.map_err(|err| invalid_request(format!("stdin is already closed: {err}")))?;
Ok(())
}
async fn handle_executor_resize(
executor: &Arc<dyn ExecProcess>,
process_id: &str,
tty: bool,
size: TerminalSize,
) -> Result<(), JSONRPCErrorError> {
if !tty {
return Err(invalid_request(
"command/exec resize requires tty: true".to_string(),
));
}
executor
.resize(ExecutorExecResizeParams {
process_id: process_id.to_string(),
size: codex_exec_server::ExecTerminalSize {
rows: size.rows,
cols: size.cols,
},
})
.await
.map_err(|err| invalid_request(format!("failed to resize PTY: {err}")))?;
Ok(())
}
async fn handle_executor_terminate(
executor: &Arc<dyn ExecProcess>,
process_id: &str,
) -> Result<(), JSONRPCErrorError> {
executor
.terminate(process_id)
.await
.map_err(|err| invalid_request(format!("failed to terminate command: {err}")))?;
Ok(())
}
fn spawn_process_output(params: SpawnProcessOutputParams) -> tokio::task::JoinHandle<String> {
let SpawnProcessOutputParams {
connection_id,
@@ -666,39 +822,6 @@ fn spawn_process_output(params: SpawnProcessOutputParams) -> tokio::task::JoinHa
})
}
async fn handle_process_write(
session: &ProcessHandle,
stream_stdin: bool,
delta: Vec<u8>,
close_stdin: bool,
) -> Result<(), JSONRPCErrorError> {
if !stream_stdin {
return Err(invalid_request(
"stdin streaming is not enabled for this command/exec".to_string(),
));
}
if !delta.is_empty() {
session
.writer_sender()
.send(delta)
.await
.map_err(|_| invalid_request("stdin is already closed".to_string()))?;
}
if close_stdin {
session.close_stdin();
}
Ok(())
}
fn handle_process_resize(
session: &ProcessHandle,
size: TerminalSize,
) -> Result<(), JSONRPCErrorError> {
session
.resize(size)
.map_err(|err| invalid_request(format!("failed to resize PTY: {err}")))
}
pub(crate) fn terminal_size_from_protocol(
size: CommandExecTerminalSize,
) -> Result<TerminalSize, JSONRPCErrorError> {
@@ -802,6 +925,7 @@ mod tests {
connection_id: ConnectionId(1),
request_id: codex_app_server_protocol::RequestId::Integer(42),
},
experimental_exec_server_url: None,
environment_id: "test-env".to_string(),
process_id: Some("proc-42".to_string()),
exec_request: windows_sandbox_exec_request(),
@@ -836,6 +960,7 @@ mod tests {
.start(StartCommandExecParams {
outgoing: Arc::new(OutgoingMessageSender::new(tx)),
request_id: request_id.clone(),
experimental_exec_server_url: None,
environment_id: "test-env".to_string(),
process_id: Some("proc-99".to_string()),
exec_request: windows_sandbox_exec_request(),
@@ -886,6 +1011,7 @@ mod tests {
.start(StartCommandExecParams {
outgoing: Arc::new(OutgoingMessageSender::new(tx)),
request_id: request_id.clone(),
experimental_exec_server_url: None,
environment_id: "test-env".to_string(),
process_id: Some("proc-100".to_string()),
exec_request: ExecRequest {
+93 -4
View File
@@ -24,6 +24,7 @@ use codex_core::config_loader::SandboxModeRequirement as CoreSandboxModeRequirem
use codex_core::plugins::PluginId;
use codex_core::plugins::collect_plugin_enabled_candidates;
use codex_core::plugins::installed_plugin_telemetry_metadata;
use codex_exec_server::Environment;
use codex_protocol::config_types::WebSearchMode;
use codex_protocol::protocol::Op;
use serde_json::json;
@@ -56,6 +57,8 @@ impl UserConfigReloader for ThreadManager {
#[derive(Clone)]
pub(crate) struct ConfigApi {
codex_home: PathBuf,
cwd: PathBuf,
experimental_exec_server_url: Option<String>,
cli_overrides: Vec<(String, TomlValue)>,
loader_overrides: LoaderOverrides,
cloud_requirements: Arc<RwLock<CloudRequirementsLoader>>,
@@ -66,6 +69,8 @@ pub(crate) struct ConfigApi {
impl ConfigApi {
pub(crate) fn new(
codex_home: PathBuf,
cwd: PathBuf,
experimental_exec_server_url: Option<String>,
cli_overrides: Vec<(String, TomlValue)>,
loader_overrides: LoaderOverrides,
cloud_requirements: Arc<RwLock<CloudRequirementsLoader>>,
@@ -74,6 +79,8 @@ impl ConfigApi {
) -> Self {
Self {
codex_home,
cwd,
experimental_exec_server_url,
cli_overrides,
loader_overrides,
cloud_requirements,
@@ -100,15 +107,52 @@ impl ConfigApi {
&self,
params: ConfigReadParams,
) -> Result<ConfigReadResponse, JSONRPCErrorError> {
self.config_service().read(params).await.map_err(map_error)
let configured_environment_id =
Environment::default_environment_id(self.experimental_exec_server_url.as_deref());
if let Some(environment_id) = params.environment_id.as_deref()
&& environment_id != configured_environment_id
{
return Err(JSONRPCErrorError {
code: INVALID_REQUEST_ERROR_CODE,
message: format!(
"unsupported environmentId `{environment_id}`; configured environment is `{configured_environment_id}`"
),
data: None,
});
}
let environment = Environment::create(self.experimental_exec_server_url.clone())
.await
.map_err(|err| JSONRPCErrorError {
code: INTERNAL_ERROR_CODE,
message: format!("failed to bind environment for config/read: {err}"),
data: None,
})?;
let filesystem = environment.get_filesystem();
let params = ConfigReadParams {
cwd: params.cwd.or_else(|| Some(self.cwd.display().to_string())),
..params
};
self.config_service()
.read_with_filesystem(params, &configured_environment_id, &filesystem)
.await
.map_err(map_error)
}
pub(crate) async fn config_requirements_read(
&self,
) -> Result<ConfigRequirementsReadResponse, JSONRPCErrorError> {
let environment = Environment::create(self.experimental_exec_server_url.clone())
.await
.map_err(|err| JSONRPCErrorError {
code: INTERNAL_ERROR_CODE,
message: format!("failed to bind environment for config/requirements/read: {err}"),
data: None,
})?;
let filesystem = environment.get_filesystem();
let requirements = self
.config_service()
.read_requirements()
.read_requirements_with_filesystem(&filesystem)
.await
.map_err(map_error)?
.map(map_requirements_toml_to_api);
@@ -120,11 +164,32 @@ impl ConfigApi {
&self,
params: ConfigValueWriteParams,
) -> Result<ConfigWriteResponse, JSONRPCErrorError> {
let configured_environment_id =
Environment::default_environment_id(self.experimental_exec_server_url.as_deref());
if let Some(environment_id) = params.environment_id.as_deref()
&& environment_id != configured_environment_id
{
return Err(JSONRPCErrorError {
code: INVALID_REQUEST_ERROR_CODE,
message: format!(
"unsupported environmentId `{environment_id}`; configured environment is `{configured_environment_id}`"
),
data: None,
});
}
let environment = Environment::create(self.experimental_exec_server_url.clone())
.await
.map_err(|err| JSONRPCErrorError {
code: INTERNAL_ERROR_CODE,
message: format!("failed to bind environment for config/value/write: {err}"),
data: None,
})?;
let filesystem = environment.get_filesystem();
let pending_changes =
collect_plugin_enabled_candidates([(&params.key_path, &params.value)].into_iter());
let response = self
.config_service()
.write_value(params)
.write_value_with_filesystem(params, &filesystem)
.await
.map_err(map_error)?;
self.emit_plugin_toggle_events(pending_changes);
@@ -135,6 +200,27 @@ impl ConfigApi {
&self,
params: ConfigBatchWriteParams,
) -> Result<ConfigWriteResponse, JSONRPCErrorError> {
let configured_environment_id =
Environment::default_environment_id(self.experimental_exec_server_url.as_deref());
if let Some(environment_id) = params.environment_id.as_deref()
&& environment_id != configured_environment_id
{
return Err(JSONRPCErrorError {
code: INVALID_REQUEST_ERROR_CODE,
message: format!(
"unsupported environmentId `{environment_id}`; configured environment is `{configured_environment_id}`"
),
data: None,
});
}
let environment = Environment::create(self.experimental_exec_server_url.clone())
.await
.map_err(|err| JSONRPCErrorError {
code: INTERNAL_ERROR_CODE,
message: format!("failed to bind environment for config/batchWrite: {err}"),
data: None,
})?;
let filesystem = environment.get_filesystem();
let reload_user_config = params.reload_user_config;
let pending_changes = collect_plugin_enabled_candidates(
params
@@ -144,7 +230,7 @@ impl ConfigApi {
);
let response = self
.config_service()
.batch_write(params)
.batch_write_with_filesystem(params, &filesystem)
.await
.map_err(map_error)?;
self.emit_plugin_toggle_events(pending_changes);
@@ -406,6 +492,8 @@ mod tests {
);
let config_api = ConfigApi::new(
codex_home.path().to_path_buf(),
std::env::current_dir().expect("current dir"),
None,
Vec::new(),
LoaderOverrides::default(),
Arc::new(RwLock::new(CloudRequirementsLoader::default())),
@@ -420,6 +508,7 @@ mod tests {
let response = config_api
.batch_write(ConfigBatchWriteParams {
environment_id: None,
edits: vec![codex_app_server_protocol::ConfigEdit {
key_path: "model".to_string(),
value: json!("gpt-5"),
+55 -14
View File
@@ -29,36 +29,67 @@ use std::sync::Arc;
#[derive(Clone)]
pub(crate) struct FsApi {
environment_id: String,
file_system: Arc<dyn ExecutorFileSystem>,
experimental_exec_server_url: Option<String>,
}
impl Default for FsApi {
fn default() -> Self {
Self::new(Environment::default_environment_id(None))
Self::new(Environment::default_environment_id(None), None)
}
}
impl FsApi {
pub(crate) fn new(environment_id: String) -> Self {
pub(crate) fn new(
environment_id: String,
experimental_exec_server_url: Option<String>,
) -> Self {
Self {
environment_id,
file_system: Arc::new(Environment::default().get_filesystem()),
experimental_exec_server_url,
}
}
async fn file_system(&self) -> Result<Arc<dyn ExecutorFileSystem>, JSONRPCErrorError> {
let environment = Environment::create(self.experimental_exec_server_url.clone())
.await
.map_err(|err| JSONRPCErrorError {
code: INTERNAL_ERROR_CODE,
message: format!("failed to bind environment for fs RPC: {err}"),
data: None,
})?;
Ok(environment.filesystem())
}
}
impl FsApi {
fn validate_environment_id(
&self,
environment_id: Option<&str>,
method: &str,
) -> Result<(), JSONRPCErrorError> {
if let Some(environment_id) = environment_id
&& environment_id != self.environment_id
{
return Err(invalid_request(format!(
"{method} does not support environmentId `{environment_id}`; configured environment is `{}`",
self.environment_id
)));
}
Ok(())
}
pub(crate) async fn read_file(
&self,
params: FsReadFileParams,
) -> Result<FsReadFileResponse, JSONRPCErrorError> {
self.validate_environment_id(params.environment_id.as_deref(), "fs/readFile")?;
tracing::debug!(
environment_id = %self.environment_id,
path = ?params.path,
"fs/readFile"
);
let bytes = self
.file_system
let file_system = self.file_system().await?;
let bytes = file_system
.read_file(&params.path)
.await
.map_err(map_fs_error)?;
@@ -71,6 +102,7 @@ impl FsApi {
&self,
params: FsWriteFileParams,
) -> Result<FsWriteFileResponse, JSONRPCErrorError> {
self.validate_environment_id(params.environment_id.as_deref(), "fs/writeFile")?;
tracing::debug!(
environment_id = %self.environment_id,
path = ?params.path,
@@ -81,7 +113,8 @@ impl FsApi {
"fs/writeFile requires valid base64 dataBase64: {err}"
))
})?;
self.file_system
let file_system = self.file_system().await?;
file_system
.write_file(&params.path, bytes)
.await
.map_err(map_fs_error)?;
@@ -92,12 +125,14 @@ impl FsApi {
&self,
params: FsCreateDirectoryParams,
) -> Result<FsCreateDirectoryResponse, JSONRPCErrorError> {
self.validate_environment_id(params.environment_id.as_deref(), "fs/createDirectory")?;
tracing::debug!(
environment_id = %self.environment_id,
path = ?params.path,
"fs/createDirectory"
);
self.file_system
let file_system = self.file_system().await?;
file_system
.create_directory(
&params.path,
CreateDirectoryOptions {
@@ -113,13 +148,14 @@ impl FsApi {
&self,
params: FsGetMetadataParams,
) -> Result<FsGetMetadataResponse, JSONRPCErrorError> {
self.validate_environment_id(params.environment_id.as_deref(), "fs/getMetadata")?;
tracing::debug!(
environment_id = %self.environment_id,
path = ?params.path,
"fs/getMetadata"
);
let metadata = self
.file_system
let file_system = self.file_system().await?;
let metadata = file_system
.get_metadata(&params.path)
.await
.map_err(map_fs_error)?;
@@ -135,13 +171,14 @@ impl FsApi {
&self,
params: FsReadDirectoryParams,
) -> Result<FsReadDirectoryResponse, JSONRPCErrorError> {
self.validate_environment_id(params.environment_id.as_deref(), "fs/readDirectory")?;
tracing::debug!(
environment_id = %self.environment_id,
path = ?params.path,
"fs/readDirectory"
);
let entries = self
.file_system
let file_system = self.file_system().await?;
let entries = file_system
.read_directory(&params.path)
.await
.map_err(map_fs_error)?;
@@ -161,12 +198,14 @@ impl FsApi {
&self,
params: FsRemoveParams,
) -> Result<FsRemoveResponse, JSONRPCErrorError> {
self.validate_environment_id(params.environment_id.as_deref(), "fs/remove")?;
tracing::debug!(
environment_id = %self.environment_id,
path = ?params.path,
"fs/remove"
);
self.file_system
let file_system = self.file_system().await?;
file_system
.remove(
&params.path,
RemoveOptions {
@@ -183,13 +222,15 @@ impl FsApi {
&self,
params: FsCopyParams,
) -> Result<FsCopyResponse, JSONRPCErrorError> {
self.validate_environment_id(params.environment_id.as_deref(), "fs/copy")?;
tracing::debug!(
environment_id = %self.environment_id,
source_path = ?params.source_path,
destination_path = ?params.destination_path,
"fs/copy"
);
self.file_system
let file_system = self.file_system().await?;
file_system
.copy(
&params.source_path,
&params.destination_path,
@@ -10,7 +10,10 @@ use codex_app_server_protocol::FuzzyFileSearchResult;
use codex_app_server_protocol::FuzzyFileSearchSessionCompletedNotification;
use codex_app_server_protocol::FuzzyFileSearchSessionUpdatedNotification;
use codex_app_server_protocol::ServerNotification;
use codex_exec_server::ExecutorFileSystem;
use codex_file_search as file_search;
use codex_file_search::SearchCandidate;
use codex_utils_absolute_path::AbsolutePathBuf;
use tracing::warn;
use crate::outgoing_message::OutgoingMessageSender;
@@ -90,6 +93,123 @@ pub(crate) async fn run_fuzzy_file_search(
files
}
pub(crate) async fn run_fuzzy_file_search_with_filesystem<F>(
query: String,
roots: Vec<String>,
filesystem: &F,
cancellation_flag: Arc<AtomicBool>,
) -> Vec<FuzzyFileSearchResult>
where
F: ExecutorFileSystem + ?Sized,
{
if roots.is_empty() || cancellation_flag.load(Ordering::Relaxed) {
return Vec::new();
}
#[expect(clippy::expect_used)]
let limit = NonZero::new(MATCH_LIMIT).expect("MATCH_LIMIT should be a valid non-zero usize");
let mut candidates = Vec::new();
for root in roots {
if cancellation_flag.load(Ordering::Relaxed) {
break;
}
let root_path = PathBuf::from(&root);
let Ok(root) = AbsolutePathBuf::from_absolute_path(root_path.as_path()) else {
continue;
};
collect_search_candidates(&root, filesystem, &cancellation_flag, &mut candidates).await;
}
let results = match file_search::run_with_candidates(
&query,
candidates,
file_search::FileSearchOptions {
limit,
compute_indices: true,
..Default::default()
},
Some(cancellation_flag),
) {
Ok(results) => results,
Err(err) => {
warn!("environment fuzzy-file-search failed: {err}");
return Vec::new();
}
};
let mut files = results
.matches
.into_iter()
.map(|m| {
let file_name = m.path.file_name().unwrap_or_default();
FuzzyFileSearchResult {
root: m.root.to_string_lossy().to_string(),
path: m.path.to_string_lossy().to_string(),
match_type: match m.match_type {
file_search::MatchType::File => FuzzyFileSearchMatchType::File,
file_search::MatchType::Directory => FuzzyFileSearchMatchType::Directory,
},
file_name: file_name.to_string_lossy().to_string(),
score: m.score,
indices: m.indices,
}
})
.collect::<Vec<_>>();
files.sort_by(file_search::cmp_by_score_desc_then_path_asc::<
FuzzyFileSearchResult,
_,
_,
>(|f| f.score, |f| f.path.as_str()));
files
}
async fn collect_search_candidates<F>(
root: &AbsolutePathBuf,
filesystem: &F,
cancellation_flag: &AtomicBool,
out: &mut Vec<SearchCandidate>,
) where
F: ExecutorFileSystem + ?Sized,
{
let mut pending = vec![root.clone()];
while let Some(current) = pending.pop() {
if cancellation_flag.load(Ordering::Relaxed) {
return;
}
let Ok(entries) = filesystem.read_directory(&current).await else {
continue;
};
for entry in entries {
if cancellation_flag.load(Ordering::Relaxed) {
return;
}
let path = current.as_path().join(&entry.file_name);
let Ok(path) = AbsolutePathBuf::try_from(path) else {
continue;
};
let Ok(relative_path) = path.as_path().strip_prefix(root.as_path()) else {
continue;
};
if entry.is_file || entry.is_directory {
out.push(SearchCandidate {
root: root.clone().into_path_buf(),
path: relative_path.to_path_buf(),
match_type: if entry.is_directory {
file_search::MatchType::Directory
} else {
file_search::MatchType::File
},
});
}
if entry.is_directory {
pending.push(path);
}
}
}
}
pub(crate) struct FuzzyFileSearchSession {
session: file_search::FileSearchSession,
shared: Arc<SessionShared>,
+6 -3
View File
@@ -249,6 +249,8 @@ impl MessageProcessor {
.maybe_start_curated_repo_sync_for_config(&config, auth_manager.clone());
let config_api = ConfigApi::new(
config.codex_home.clone(),
config.cwd.clone(),
config.experimental_exec_server_url.clone(),
cli_overrides,
loader_overrides,
cloud_requirements,
@@ -256,9 +258,10 @@ impl MessageProcessor {
analytics_events_client,
);
let external_agent_config_api = ExternalAgentConfigApi::new(config.codex_home.clone());
let fs_api = FsApi::new(Environment::default_environment_id(
config.experimental_exec_server_url.as_deref(),
));
let fs_api = FsApi::new(
Environment::default_environment_id(config.experimental_exec_server_url.as_deref()),
config.experimental_exec_server_url.clone(),
);
Self {
outgoing,
+1
View File
@@ -788,6 +788,7 @@ mod tests {
fn test_thread(thread_id: &str, source: codex_app_server_protocol::SessionSource) -> Thread {
Thread {
id: thread_id.to_string(),
environment_id: "local".to_string(),
preview: String::new(),
ephemeral: false,
model_provider: "mock-provider".to_string(),
@@ -50,6 +50,7 @@ use codex_app_server_protocol::PluginInstallParams;
use codex_app_server_protocol::PluginListParams;
use codex_app_server_protocol::PluginReadParams;
use codex_app_server_protocol::PluginUninstallParams;
use codex_app_server_protocol::PromptListParams;
use codex_app_server_protocol::RequestId;
use codex_app_server_protocol::ReviewStartParams;
use codex_app_server_protocol::ServerRequest;
@@ -478,6 +479,15 @@ impl McpProcess {
self.send_request("skills/list", params).await
}
/// Send a `prompt/list` JSON-RPC request.
pub async fn send_prompt_list_request(
&mut self,
params: PromptListParams,
) -> anyhow::Result<i64> {
let params = Some(serde_json::to_value(params)?);
self.send_request("prompt/list", params).await
}
/// Send a `plugin/install` JSON-RPC request.
pub async fn send_plugin_install_request(
&mut self,
@@ -832,6 +842,7 @@ impl McpProcess {
cancellation_token: Option<String>,
) -> anyhow::Result<i64> {
let mut params = serde_json::json!({
"environmentId": null,
"query": query,
"roots": roots,
});
@@ -847,6 +858,7 @@ impl McpProcess {
roots: Vec<String>,
) -> anyhow::Result<i64> {
let params = serde_json::json!({
"environmentId": null,
"sessionId": session_id,
"roots": roots,
});
@@ -872,6 +884,7 @@ impl McpProcess {
query: &str,
) -> anyhow::Result<i64> {
let params = serde_json::json!({
"environmentId": null,
"sessionId": session_id,
"query": query,
});
@@ -896,6 +909,7 @@ impl McpProcess {
session_id: &str,
) -> anyhow::Result<i64> {
let params = serde_json::json!({
"environmentId": null,
"sessionId": session_id,
});
self.send_request("fuzzyFileSearch/sessionStop", Some(params))
@@ -46,6 +46,35 @@ async fn initialized_mcp(codex_home: &TempDir) -> Result<McpProcess> {
Ok(mcp)
}
#[tokio::test]
async fn test_fuzzy_file_search_rejects_unsupported_environment_id() -> Result<()> {
let codex_home = TempDir::new()?;
let mut mcp = initialized_mcp(&codex_home).await?;
let request_id = mcp
.send_raw_request(
"fuzzyFileSearch",
Some(json!({
"environmentId": "remote",
"query": "alp",
"roots": [],
})),
)
.await?;
let err = timeout(
DEFAULT_READ_TIMEOUT,
mcp.read_stream_until_error_message(RequestId::Integer(request_id)),
)
.await??;
assert_eq!(err.error.code, -32600);
assert_eq!(
err.error.message,
"unsupported environmentId `remote`; configured environment is `local`"
);
Ok(())
}
async fn wait_for_session_updated(
mcp: &mut McpProcess,
session_id: &str,
@@ -61,6 +61,7 @@ sandbox_mode = "workspace-write"
let request_id = mcp
.send_config_read_request(ConfigReadParams {
include_layers: true,
environment_id: None,
cwd: None,
})
.await?;
@@ -73,6 +74,7 @@ sandbox_mode = "workspace-write"
config,
origins,
layers,
..
} = to_response(resp)?;
assert_eq!(config.model.as_deref(), Some("gpt-user"));
@@ -113,6 +115,7 @@ view_image = false
let request_id = mcp
.send_config_read_request(ConfigReadParams {
include_layers: true,
environment_id: None,
cwd: None,
})
.await?;
@@ -125,6 +128,7 @@ view_image = false
config,
origins,
layers,
..
} = to_response(resp)?;
let tools = config.tools.expect("tools present");
@@ -191,6 +195,7 @@ location = { country = "US", city = "New York", timezone = "America/New_York" }
let request_id = mcp
.send_config_read_request(ConfigReadParams {
include_layers: false,
environment_id: None,
cwd: None,
})
.await?;
@@ -235,6 +240,7 @@ web_search = true
let request_id = mcp
.send_config_read_request(ConfigReadParams {
include_layers: false,
environment_id: None,
cwd: None,
})
.await?;
@@ -271,6 +277,7 @@ default_tools_approval_mode = "prompt"
let request_id = mcp
.send_config_read_request(ConfigReadParams {
include_layers: true,
environment_id: None,
cwd: None,
})
.await?;
@@ -283,6 +290,7 @@ default_tools_approval_mode = "prompt"
config,
origins,
layers,
..
} = to_response(resp)?;
assert_eq!(
@@ -356,6 +364,7 @@ model_reasoning_effort = "high"
let request_id = mcp
.send_config_read_request(ConfigReadParams {
include_layers: true,
environment_id: None,
cwd: Some(workspace.path().to_string_lossy().into_owned()),
})
.await?;
@@ -433,6 +442,7 @@ writable_roots = [{}]
let request_id = mcp
.send_config_read_request(ConfigReadParams {
include_layers: true,
environment_id: None,
cwd: None,
})
.await?;
@@ -445,6 +455,7 @@ writable_roots = [{}]
config,
origins,
layers,
..
} = to_response(resp)?;
assert_eq!(config.model.as_deref(), Some("gpt-system"));
@@ -520,6 +531,7 @@ model = "gpt-old"
let read_id = mcp
.send_config_read_request(ConfigReadParams {
include_layers: false,
environment_id: None,
cwd: None,
})
.await?;
@@ -533,6 +545,7 @@ model = "gpt-old"
let write_id = mcp
.send_config_value_write_request(ConfigValueWriteParams {
environment_id: None,
file_path: None,
key_path: "model".to_string(),
value: json!("gpt-new"),
@@ -555,6 +568,7 @@ model = "gpt-old"
let verify_id = mcp
.send_config_read_request(ConfigReadParams {
include_layers: false,
environment_id: None,
cwd: None,
})
.await?;
@@ -584,6 +598,7 @@ model = "gpt-old"
let write_id = mcp
.send_config_value_write_request(ConfigValueWriteParams {
environment_id: None,
file_path: Some(codex_home.path().join("config.toml").display().to_string()),
key_path: "model".to_string(),
value: json!("gpt-new"),
@@ -620,6 +635,7 @@ async fn config_batch_write_applies_multiple_edits() -> Result<()> {
let writable_root = test_tmp_path_buf();
let batch_id = mcp
.send_config_batch_write_request(ConfigBatchWriteParams {
environment_id: None,
file_path: Some(codex_home.join("config.toml").display().to_string()),
edits: vec![
ConfigEdit {
@@ -653,6 +669,7 @@ async fn config_batch_write_applies_multiple_edits() -> Result<()> {
let read_id = mcp
.send_config_read_request(ConfigReadParams {
include_layers: false,
environment_id: None,
cwd: None,
})
.await?;
+41
View File
@@ -64,6 +64,7 @@ async fn fs_get_metadata_returns_only_used_fields() -> Result<()> {
let mut mcp = initialized_mcp(&codex_home).await?;
let request_id = mcp
.send_fs_get_metadata_request(codex_app_server_protocol::FsGetMetadataParams {
environment_id: None,
path: absolute_path(file_path.clone()),
})
.await?;
@@ -121,6 +122,7 @@ async fn fs_methods_cover_current_fs_utils_surface() -> Result<()> {
let create_directory_request_id = mcp
.send_fs_create_directory_request(codex_app_server_protocol::FsCreateDirectoryParams {
environment_id: None,
path: absolute_path(nested_dir.clone()),
recursive: None,
})
@@ -133,6 +135,7 @@ async fn fs_methods_cover_current_fs_utils_surface() -> Result<()> {
let write_request_id = mcp
.send_fs_write_file_request(FsWriteFileParams {
environment_id: None,
path: absolute_path(nested_file.clone()),
data_base64: STANDARD.encode("hello from app-server"),
})
@@ -145,6 +148,7 @@ async fn fs_methods_cover_current_fs_utils_surface() -> Result<()> {
let root_write_request_id = mcp
.send_fs_write_file_request(FsWriteFileParams {
environment_id: None,
path: absolute_path(source_file.clone()),
data_base64: STANDARD.encode("hello from source root"),
})
@@ -157,6 +161,7 @@ async fn fs_methods_cover_current_fs_utils_surface() -> Result<()> {
let read_request_id = mcp
.send_fs_read_file_request(codex_app_server_protocol::FsReadFileParams {
environment_id: None,
path: absolute_path(nested_file.clone()),
})
.await?;
@@ -176,6 +181,7 @@ async fn fs_methods_cover_current_fs_utils_surface() -> Result<()> {
let copy_file_request_id = mcp
.send_fs_copy_request(FsCopyParams {
environment_id: None,
source_path: absolute_path(nested_file.clone()),
destination_path: absolute_path(copy_file_path.clone()),
recursive: false,
@@ -193,6 +199,7 @@ async fn fs_methods_cover_current_fs_utils_surface() -> Result<()> {
let copy_dir_request_id = mcp
.send_fs_copy_request(FsCopyParams {
environment_id: None,
source_path: absolute_path(source_dir.clone()),
destination_path: absolute_path(copied_dir.clone()),
recursive: true,
@@ -210,6 +217,7 @@ async fn fs_methods_cover_current_fs_utils_surface() -> Result<()> {
let read_directory_request_id = mcp
.send_fs_read_directory_request(codex_app_server_protocol::FsReadDirectoryParams {
environment_id: None,
path: absolute_path(source_dir.clone()),
})
.await?;
@@ -240,6 +248,7 @@ async fn fs_methods_cover_current_fs_utils_surface() -> Result<()> {
let remove_request_id = mcp
.send_fs_remove_request(codex_app_server_protocol::FsRemoveParams {
environment_id: None,
path: absolute_path(copied_dir.clone()),
recursive: None,
force: None,
@@ -267,6 +276,7 @@ async fn fs_write_file_accepts_base64_bytes() -> Result<()> {
let mut mcp = initialized_mcp(&codex_home).await?;
let write_request_id = mcp
.send_fs_write_file_request(FsWriteFileParams {
environment_id: None,
path: absolute_path(file_path.clone()),
data_base64: STANDARD.encode(bytes),
})
@@ -280,6 +290,7 @@ async fn fs_write_file_accepts_base64_bytes() -> Result<()> {
let read_request_id = mcp
.send_fs_read_file_request(codex_app_server_protocol::FsReadFileParams {
environment_id: None,
path: absolute_path(file_path),
})
.await?;
@@ -308,6 +319,7 @@ async fn fs_write_file_rejects_invalid_base64() -> Result<()> {
let mut mcp = initialized_mcp(&codex_home).await?;
let request_id = mcp
.send_fs_write_file_request(FsWriteFileParams {
environment_id: None,
path: absolute_path(file_path),
data_base64: "%%%".to_string(),
})
@@ -462,6 +474,7 @@ async fn fs_copy_rejects_directory_without_recursive() -> Result<()> {
let mut mcp = initialized_mcp(&codex_home).await?;
let request_id = mcp
.send_fs_copy_request(FsCopyParams {
environment_id: None,
source_path: absolute_path(source_dir),
destination_path: absolute_path(codex_home.path().join("dest")),
recursive: false,
@@ -489,6 +502,7 @@ async fn fs_copy_rejects_copying_directory_into_descendant() -> Result<()> {
let mut mcp = initialized_mcp(&codex_home).await?;
let request_id = mcp
.send_fs_copy_request(FsCopyParams {
environment_id: None,
source_path: absolute_path(source_dir.clone()),
destination_path: absolute_path(source_dir.join("nested").join("copy")),
recursive: true,
@@ -520,6 +534,7 @@ async fn fs_copy_preserves_symlinks_in_recursive_copy() -> Result<()> {
let mut mcp = initialized_mcp(&codex_home).await?;
let request_id = mcp
.send_fs_copy_request(FsCopyParams {
environment_id: None,
source_path: absolute_path(source_dir),
destination_path: absolute_path(copied_dir.clone()),
recursive: true,
@@ -560,6 +575,7 @@ async fn fs_copy_ignores_unknown_special_files_in_recursive_copy() -> Result<()>
let mut mcp = initialized_mcp(&codex_home).await?;
let request_id = mcp
.send_fs_copy_request(FsCopyParams {
environment_id: None,
source_path: absolute_path(source_dir),
destination_path: absolute_path(copied_dir.clone()),
recursive: true,
@@ -597,6 +613,7 @@ async fn fs_copy_rejects_standalone_fifo_source() -> Result<()> {
let mut mcp = initialized_mcp(&codex_home).await?;
let request_id = mcp
.send_fs_copy_request(FsCopyParams {
environment_id: None,
source_path: absolute_path(fifo_path),
destination_path: absolute_path(codex_home.path().join("copied")),
recursive: false,
@@ -611,3 +628,27 @@ async fn fs_copy_rejects_standalone_fifo_source() -> Result<()> {
Ok(())
}
#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
async fn fs_read_file_rejects_unsupported_environment_id() -> Result<()> {
let codex_home = TempDir::new()?;
let file_path = codex_home.path().join("note.txt");
std::fs::write(&file_path, "hello")?;
let mut mcp = initialized_mcp(&codex_home).await?;
let request_id = mcp
.send_fs_read_file_request(codex_app_server_protocol::FsReadFileParams {
environment_id: Some("remote".to_string()),
path: absolute_path(file_path),
})
.await?;
expect_error_message(
&mut mcp,
request_id,
"fs/readFile does not support environmentId `remote`; configured environment is `local`",
)
.await?;
Ok(())
}
@@ -22,6 +22,7 @@ mod plugin_install;
mod plugin_list;
mod plugin_read;
mod plugin_uninstall;
mod prompt_list;
mod rate_limits;
mod realtime_conversation;
mod request_permissions;
@@ -88,6 +88,7 @@ async fn plugin_install_returns_invalid_request_for_missing_marketplace_file() -
let request_id = mcp
.send_plugin_install_request(PluginInstallParams {
environment_id: None,
marketplace_path: AbsolutePathBuf::try_from(
codex_home.path().join("missing-marketplace.json"),
)?,
@@ -129,6 +130,7 @@ async fn plugin_install_returns_invalid_request_for_not_available_plugin() -> Re
let request_id = mcp
.send_plugin_install_request(PluginInstallParams {
environment_id: None,
marketplace_path,
plugin_name: "sample-plugin".to_string(),
force_remote_sync: false,
@@ -179,6 +181,7 @@ async fn plugin_install_returns_invalid_request_for_disallowed_product_plugin()
let request_id = mcp
.send_plugin_install_request(PluginInstallParams {
environment_id: None,
marketplace_path,
plugin_name: "sample-plugin".to_string(),
force_remote_sync: false,
@@ -241,6 +244,7 @@ async fn plugin_install_force_remote_sync_enables_remote_plugin_before_local_ins
let request_id = mcp
.send_plugin_install_request(PluginInstallParams {
environment_id: None,
marketplace_path,
plugin_name: "sample-plugin".to_string(),
force_remote_sync: true,
@@ -298,6 +302,7 @@ async fn plugin_install_tracks_analytics_event() -> Result<()> {
let request_id = mcp
.send_plugin_install_request(PluginInstallParams {
environment_id: None,
marketplace_path,
plugin_name: "sample-plugin".to_string(),
force_remote_sync: false,
@@ -413,6 +418,7 @@ async fn plugin_install_returns_apps_needing_auth() -> Result<()> {
let request_id = mcp
.send_plugin_install_request(PluginInstallParams {
environment_id: None,
marketplace_path,
plugin_name: "sample-plugin".to_string(),
force_remote_sync: false,
@@ -496,6 +502,7 @@ async fn plugin_install_filters_disallowed_apps_needing_auth() -> Result<()> {
let request_id = mcp
.send_plugin_install_request(PluginInstallParams {
environment_id: None,
marketplace_path,
plugin_name: "sample-plugin".to_string(),
force_remote_sync: false,
@@ -527,6 +534,48 @@ async fn plugin_install_filters_disallowed_apps_needing_auth() -> Result<()> {
Ok(())
}
#[tokio::test]
async fn plugin_install_rejects_unsupported_environment_id() -> Result<()> {
let codex_home = TempDir::new()?;
let repo_root = TempDir::new()?;
write_plugin_marketplace(
repo_root.path(),
"debug",
"sample-plugin",
"./sample-plugin",
None,
None,
)?;
write_plugin_source(repo_root.path(), "sample-plugin", &[])?;
let marketplace_path =
AbsolutePathBuf::try_from(repo_root.path().join(".agents/plugins/marketplace.json"))?;
let mut mcp = McpProcess::new(codex_home.path()).await?;
timeout(DEFAULT_TIMEOUT, mcp.initialize()).await??;
let request_id = mcp
.send_plugin_install_request(PluginInstallParams {
environment_id: Some("remote".to_string()),
marketplace_path,
plugin_name: "sample-plugin".to_string(),
force_remote_sync: false,
})
.await?;
let err = timeout(
DEFAULT_TIMEOUT,
mcp.read_stream_until_error_message(RequestId::Integer(request_id)),
)
.await??;
assert_eq!(err.error.code, -32600);
assert_eq!(
err.error.message,
"unsupported environmentId `remote`; configured environment is `local`"
);
Ok(())
}
#[derive(Clone)]
struct AppsServerState {
response: Arc<StdMutex<serde_json::Value>>,
@@ -63,6 +63,7 @@ async fn plugin_list_skips_invalid_marketplace_file() -> Result<()> {
let request_id = mcp
.send_plugin_list_request(PluginListParams {
environment_id: None,
cwds: Some(vec![AbsolutePathBuf::try_from(repo_root.path())?]),
force_remote_sync: false,
})
@@ -147,6 +148,7 @@ async fn plugin_list_accepts_omitted_cwds() -> Result<()> {
let request_id = mcp
.send_plugin_list_request(PluginListParams {
environment_id: None,
cwds: None,
force_remote_sync: false,
})
@@ -219,6 +221,7 @@ enabled = false
let request_id = mcp
.send_plugin_list_request(PluginListParams {
environment_id: None,
cwds: Some(vec![AbsolutePathBuf::try_from(repo_root.path())?]),
force_remote_sync: false,
})
@@ -371,6 +374,7 @@ enabled = false
let request_id = mcp
.send_plugin_list_request(PluginListParams {
environment_id: None,
cwds: Some(vec![
AbsolutePathBuf::try_from(workspace_enabled.path())?,
AbsolutePathBuf::try_from(workspace_default.path())?,
@@ -458,6 +462,7 @@ async fn plugin_list_returns_plugin_interface_with_absolute_asset_paths() -> Res
let request_id = mcp
.send_plugin_list_request(PluginListParams {
environment_id: None,
cwds: Some(vec![AbsolutePathBuf::try_from(repo_root.path())?]),
force_remote_sync: false,
})
@@ -571,6 +576,7 @@ async fn plugin_list_accepts_legacy_string_default_prompt() -> Result<()> {
let request_id = mcp
.send_plugin_list_request(PluginListParams {
environment_id: None,
cwds: Some(vec![AbsolutePathBuf::try_from(repo_root.path())?]),
force_remote_sync: false,
})
@@ -611,6 +617,7 @@ async fn plugin_list_force_remote_sync_returns_remote_sync_error_on_fail_open()
let request_id = mcp
.send_plugin_list_request(PluginListParams {
environment_id: None,
cwds: None,
force_remote_sync: true,
})
@@ -691,6 +698,7 @@ async fn plugin_list_force_remote_sync_reconciles_curated_plugin_state() -> Resu
let request_id = mcp
.send_plugin_list_request(PluginListParams {
environment_id: None,
cwds: None,
force_remote_sync: true,
})
@@ -773,6 +781,7 @@ async fn plugin_list_fetches_featured_plugin_ids_without_chatgpt_auth() -> Resul
let request_id = mcp
.send_plugin_list_request(PluginListParams {
environment_id: None,
cwds: None,
force_remote_sync: false,
})
@@ -813,6 +822,7 @@ async fn plugin_list_uses_warmed_featured_plugin_ids_cache_on_first_request() ->
let request_id = mcp
.send_plugin_list_request(PluginListParams {
environment_id: None,
cwds: None,
force_remote_sync: false,
})
@@ -146,6 +146,7 @@ enabled = true
AbsolutePathBuf::try_from(repo_root.path().join(".agents/plugins/marketplace.json"))?;
let request_id = mcp
.send_plugin_read_request(PluginReadParams {
environment_id: None,
marketplace_path: marketplace_path.clone(),
plugin_name: "demo-plugin".to_string(),
})
@@ -266,6 +267,7 @@ async fn plugin_read_accepts_legacy_string_default_prompt() -> Result<()> {
let request_id = mcp
.send_plugin_read_request(PluginReadParams {
environment_id: None,
marketplace_path: AbsolutePathBuf::try_from(
repo_root.path().join(".agents/plugins/marketplace.json"),
)?,
@@ -320,6 +322,7 @@ async fn plugin_read_returns_invalid_request_when_plugin_is_missing() -> Result<
let request_id = mcp
.send_plugin_read_request(PluginReadParams {
environment_id: None,
marketplace_path: AbsolutePathBuf::try_from(
repo_root.path().join(".agents/plugins/marketplace.json"),
)?,
@@ -372,6 +375,7 @@ async fn plugin_read_returns_invalid_request_when_plugin_manifest_is_missing() -
let request_id = mcp
.send_plugin_read_request(PluginReadParams {
environment_id: None,
marketplace_path: AbsolutePathBuf::try_from(
repo_root.path().join(".agents/plugins/marketplace.json"),
)?,
@@ -43,6 +43,7 @@ enabled = true
timeout(DEFAULT_TIMEOUT, mcp.initialize()).await??;
let params = PluginUninstallParams {
environment_id: None,
plugin_id: "sample-plugin@debug".to_string(),
force_remote_sync: false,
};
@@ -77,6 +78,36 @@ enabled = true
Ok(())
}
#[tokio::test]
async fn plugin_uninstall_rejects_unsupported_environment_id() -> Result<()> {
let codex_home = TempDir::new()?;
write_installed_plugin(&codex_home, "debug", "sample-plugin")?;
let mut mcp = McpProcess::new(codex_home.path()).await?;
timeout(DEFAULT_TIMEOUT, mcp.initialize()).await??;
let request_id = mcp
.send_plugin_uninstall_request(PluginUninstallParams {
environment_id: Some("remote".to_string()),
plugin_id: "sample-plugin@debug".to_string(),
force_remote_sync: false,
})
.await?;
let err = timeout(
DEFAULT_TIMEOUT,
mcp.read_stream_until_error_message(RequestId::Integer(request_id)),
)
.await??;
assert_eq!(err.error.code, -32600);
assert_eq!(
err.error.message,
"unsupported environmentId `remote`; configured environment is `local`"
);
Ok(())
}
#[tokio::test]
async fn plugin_uninstall_force_remote_sync_calls_remote_uninstall_first() -> Result<()> {
let server = MockServer::start().await;
@@ -122,6 +153,7 @@ enabled = true
let request_id = mcp
.send_plugin_uninstall_request(PluginUninstallParams {
environment_id: None,
plugin_id: "sample-plugin@debug".to_string(),
force_remote_sync: true,
})
@@ -171,6 +203,7 @@ async fn plugin_uninstall_tracks_analytics_event() -> Result<()> {
let request_id = mcp
.send_plugin_uninstall_request(PluginUninstallParams {
environment_id: None,
plugin_id: "sample-plugin@debug".to_string(),
force_remote_sync: false,
})
@@ -0,0 +1,88 @@
use std::time::Duration;
use anyhow::Result;
use app_test_support::McpProcess;
use app_test_support::to_response;
use codex_app_server_protocol::JSONRPCResponse;
use codex_app_server_protocol::PromptListParams;
use codex_app_server_protocol::PromptListResponse;
use codex_app_server_protocol::RequestId;
use pretty_assertions::assert_eq;
use tempfile::TempDir;
use tokio::time::timeout;
const DEFAULT_TIMEOUT: Duration = Duration::from_secs(10);
fn write_prompt(root: &TempDir, name: &str, contents: &str) -> Result<()> {
let prompts_dir = root.path().join("prompts");
std::fs::create_dir_all(&prompts_dir)?;
std::fs::write(prompts_dir.join(format!("{name}.md")), contents)?;
Ok(())
}
#[tokio::test]
async fn prompt_list_returns_environment_bound_custom_prompts() -> Result<()> {
let codex_home = TempDir::new()?;
write_prompt(
&codex_home,
"draft-release-notes",
"---\ndescription: Release notes helper\nargument_hint: version\n---\n\nBody",
)?;
let mut mcp = McpProcess::new(codex_home.path()).await?;
timeout(DEFAULT_TIMEOUT, mcp.initialize()).await??;
let request_id = mcp
.send_prompt_list_request(PromptListParams {
environment_id: Some("local".to_string()),
})
.await?;
let response: JSONRPCResponse = timeout(
DEFAULT_TIMEOUT,
mcp.read_stream_until_response_message(RequestId::Integer(request_id)),
)
.await??;
let PromptListResponse {
environment_id,
prompts,
} = to_response(response)?;
assert_eq!(environment_id, "local");
assert_eq!(prompts.len(), 1);
assert_eq!(prompts[0].name, "draft-release-notes");
assert_eq!(
prompts[0].description.as_deref(),
Some("Release notes helper")
);
assert_eq!(prompts[0].argument_hint.as_deref(), Some("version"));
assert_eq!(prompts[0].content.trim(), "Body");
Ok(())
}
#[tokio::test]
async fn prompt_list_rejects_unsupported_environment_id() -> Result<()> {
let codex_home = TempDir::new()?;
let mut mcp = McpProcess::new(codex_home.path()).await?;
timeout(DEFAULT_TIMEOUT, mcp.initialize()).await??;
let request_id = mcp
.send_prompt_list_request(PromptListParams {
environment_id: Some("remote".to_string()),
})
.await?;
let err = timeout(
DEFAULT_TIMEOUT,
mcp.read_stream_until_error_message(RequestId::Integer(request_id)),
)
.await??;
assert!(
err.error
.message
.contains("unsupported environmentId `remote`"),
"unexpected error: {}",
err.error.message
);
Ok(())
}
@@ -11,6 +11,7 @@ use codex_app_server_protocol::SkillsListExtraRootsForCwd;
use codex_app_server_protocol::SkillsListParams;
use codex_app_server_protocol::SkillsListResponse;
use codex_app_server_protocol::ThreadStartParams;
use codex_exec_server::Environment;
use pretty_assertions::assert_eq;
use tempfile::TempDir;
use tokio::time::timeout;
@@ -300,6 +301,11 @@ async fn skills_changed_notification_is_emitted_after_skill_change() -> Result<(
.context("skills/changed params must be present")?;
let notification: SkillsChangedNotification = serde_json::from_value(params)?;
assert_eq!(notification, SkillsChangedNotification {});
assert_eq!(
notification,
SkillsChangedNotification {
environment_id: Environment::default_environment_id(None),
}
);
Ok(())
}
@@ -436,6 +436,7 @@ async fn thread_fork_ephemeral_remains_pathless_and_omits_listing() -> Result<()
let list_id = mcp
.send_thread_list_request(ThreadListParams {
environment_id: None,
cursor: None,
limit: Some(10),
sort_key: None,
@@ -71,6 +71,7 @@ async fn list_threads_with_sort(
) -> Result<ThreadListResponse> {
let request_id = mcp
.send_thread_list_request(codex_app_server_protocol::ThreadListParams {
environment_id: None,
cursor,
limit,
sort_key,
@@ -486,6 +487,7 @@ async fn thread_list_respects_cwd_filter() -> Result<()> {
let mut mcp = init_mcp(codex_home.path()).await?;
let request_id = mcp
.send_thread_list_request(codex_app_server_protocol::ThreadListParams {
environment_id: None,
cursor: None,
limit: Some(10),
sort_key: None,
@@ -565,6 +567,7 @@ sqlite = true
let mut mcp = init_mcp(codex_home.path()).await?;
let request_id = mcp
.send_thread_list_request(codex_app_server_protocol::ThreadListParams {
environment_id: None,
cursor: None,
limit: Some(10),
sort_key: None,
@@ -1408,6 +1411,7 @@ async fn thread_list_invalid_cursor_returns_error() -> Result<()> {
let request_id = mcp
.send_thread_list_request(codex_app_server_protocol::ThreadListParams {
environment_id: None,
cursor: Some("not-a-cursor".to_string()),
limit: Some(2),
sort_key: None,
@@ -27,7 +27,10 @@ async fn thread_loaded_list_returns_loaded_thread_ids() -> Result<()> {
let thread_id = start_thread(&mut mcp).await?;
let list_id = mcp
.send_thread_loaded_list_request(ThreadLoadedListParams::default())
.send_thread_loaded_list_request(ThreadLoadedListParams {
environment_id: Some("local".to_string()),
..Default::default()
})
.await?;
let resp: JSONRPCResponse = timeout(
DEFAULT_READ_TIMEOUT,
@@ -62,6 +65,7 @@ async fn thread_loaded_list_paginates() -> Result<()> {
let list_id = mcp
.send_thread_loaded_list_request(ThreadLoadedListParams {
environment_id: None,
cursor: None,
limit: Some(1),
})
@@ -80,6 +84,7 @@ async fn thread_loaded_list_paginates() -> Result<()> {
let list_id = mcp
.send_thread_loaded_list_request(ThreadLoadedListParams {
environment_id: None,
cursor: next_cursor,
limit: Some(1),
})
@@ -99,6 +104,37 @@ async fn thread_loaded_list_paginates() -> Result<()> {
Ok(())
}
#[tokio::test]
async fn thread_loaded_list_rejects_unsupported_environment_id() -> Result<()> {
let server = create_mock_responses_server_repeating_assistant("Done").await;
let codex_home = TempDir::new()?;
create_config_toml(codex_home.path(), &server.uri())?;
let mut mcp = McpProcess::new(codex_home.path()).await?;
timeout(DEFAULT_READ_TIMEOUT, mcp.initialize()).await??;
let _thread_id = start_thread(&mut mcp).await?;
let list_id = mcp
.send_thread_loaded_list_request(ThreadLoadedListParams {
environment_id: Some("remote".to_string()),
cursor: None,
limit: None,
})
.await?;
let err = timeout(
DEFAULT_READ_TIMEOUT,
mcp.read_stream_until_error_message(RequestId::Integer(list_id)),
)
.await??;
assert_eq!(err.error.code, -32600);
assert_eq!(
err.error.message,
"unsupported environmentId `remote`; configured environment is `local`"
);
Ok(())
}
fn create_config_toml(codex_home: &Path, server_uri: &str) -> std::io::Result<()> {
let config_toml = codex_home.join("config.toml");
std::fs::write(
@@ -100,6 +100,7 @@ async fn thread_metadata_update_patches_git_branch_and_returns_updated_thread()
let read_id = mcp
.send_thread_read_request(ThreadReadParams {
environment_id: None,
thread_id: thread.id,
include_turns: false,
})
@@ -414,6 +415,7 @@ async fn thread_metadata_update_can_clear_stored_git_fields() -> Result<()> {
let read_id = mcp
.send_thread_read_request(ThreadReadParams {
environment_id: None,
thread_id,
include_turns: false,
})
@@ -65,6 +65,7 @@ async fn thread_read_returns_summary_without_turns() -> Result<()> {
let read_id = mcp
.send_thread_read_request(ThreadReadParams {
environment_id: None,
thread_id: conversation_id.clone(),
include_turns: false,
})
@@ -120,6 +121,7 @@ async fn thread_read_can_include_turns() -> Result<()> {
let read_id = mcp
.send_thread_read_request(ThreadReadParams {
environment_id: None,
thread_id: conversation_id.clone(),
include_turns: true,
})
@@ -182,6 +184,7 @@ async fn thread_read_loaded_thread_returns_precomputed_path_before_materializati
let read_id = mcp
.send_thread_read_request(ThreadReadParams {
environment_id: None,
thread_id: thread.id.clone(),
include_turns: false,
})
@@ -249,6 +252,7 @@ async fn thread_name_set_is_reflected_in_read_list_and_resume() -> Result<()> {
// Read should now surface `thread.name`, and the wire payload must include `name`.
let read_id = mcp
.send_thread_read_request(ThreadReadParams {
environment_id: None,
thread_id: conversation_id.clone(),
include_turns: false,
})
@@ -280,6 +284,7 @@ async fn thread_name_set_is_reflected_in_read_list_and_resume() -> Result<()> {
// List should also surface the name.
let list_id = mcp
.send_thread_list_request(ThreadListParams {
environment_id: None,
cursor: None,
limit: Some(50),
sort_key: None,
@@ -388,6 +393,7 @@ async fn thread_read_include_turns_rejects_unmaterialized_loaded_thread() -> Res
let read_id = mcp
.send_thread_read_request(ThreadReadParams {
environment_id: None,
thread_id: thread.id.clone(),
include_turns: true,
})
@@ -462,6 +468,7 @@ async fn thread_read_reports_system_error_idle_flag_after_failed_turn() -> Resul
let read_id = mcp
.send_thread_read_request(ThreadReadParams {
environment_id: None,
thread_id: thread.id,
include_turns: false,
})
@@ -512,6 +512,7 @@ async fn thread_resume_and_read_interrupt_incomplete_rollout_turn_when_thread_is
let read_id = mcp
.send_thread_read_request(ThreadReadParams {
environment_id: None,
thread_id: resumed_again.id,
include_turns: true,
})
@@ -122,6 +122,7 @@ async fn thread_shell_command_runs_as_standalone_turn_and_persists_history() ->
let read_id = mcp
.send_thread_read_request(ThreadReadParams {
environment_id: None,
thread_id: thread.id,
include_turns: true,
})
@@ -292,6 +293,7 @@ async fn thread_shell_command_uses_existing_active_turn() -> Result<()> {
let read_id = mcp
.send_thread_read_request(ThreadReadParams {
environment_id: None,
thread_id: thread.id,
include_turns: true,
})
@@ -267,6 +267,7 @@ async fn thread_unsubscribe_clears_cached_status_before_resume() -> Result<()> {
let read_id = mcp
.send_thread_read_request(ThreadReadParams {
environment_id: None,
thread_id: thread_id.clone(),
include_turns: false,
})
+2
View File
@@ -963,6 +963,8 @@ async fn spawn_thread_subagent_uses_role_specific_nickname_candidates() {
AgentRoleConfig {
description: Some("Research role".to_string()),
config_file: None,
locked_model: None,
locked_reasoning_effort: None,
nickname_candidates: Some(vec!["Atlas".to_string()]),
},
);
+66 -53
View File
@@ -17,6 +17,9 @@ use crate::config_loader::ConfigLayerStackOrdering;
use crate::config_loader::resolve_relative_paths_in_config_toml;
use anyhow::anyhow;
use codex_app_server_protocol::ConfigLayerSource;
use codex_exec_server::ExecutorFileSystem;
use codex_exec_server::LocalFileSystem;
use codex_utils_absolute_path::AbsolutePathBuf;
use std::collections::BTreeMap;
use std::collections::BTreeSet;
use std::path::Path;
@@ -27,25 +30,21 @@ use toml::Value as TomlValue;
pub const DEFAULT_ROLE_NAME: &str = "default";
const AGENT_TYPE_UNAVAILABLE_ERROR: &str = "agent type is currently not available";
/// Applies a named role layer to `config` while preserving caller-owned model selection.
///
/// The role layer is inserted at session-flag precedence so it can override persisted config, but
/// the caller's current `profile` and `model_provider` remain sticky runtime choices unless the
/// role explicitly sets `profile`, explicitly sets `model_provider`, or rewrites the active
/// profile's `model_provider` in place. Rebuilding the config without those overrides would make a
/// spawned agent silently fall back to the default provider, which is the bug this preservation
/// logic avoids.
pub(crate) async fn apply_role_to_config(
pub(crate) async fn apply_role_to_config_with_filesystem<F>(
config: &mut Config,
role_name: Option<&str>,
) -> Result<(), String> {
filesystem: &F,
) -> Result<(), String>
where
F: ExecutorFileSystem + ?Sized,
{
let role_name = role_name.unwrap_or(DEFAULT_ROLE_NAME);
let role = resolve_role_config(config, role_name)
.cloned()
.ok_or_else(|| format!("unknown agent_type '{role_name}'"))?;
apply_role_to_config_inner(config, role_name, &role)
apply_role_to_config_inner_with_filesystem(config, role_name, &role, filesystem)
.await
.map_err(|err| {
tracing::warn!("failed to apply role to config: {err}");
@@ -53,16 +52,34 @@ pub(crate) async fn apply_role_to_config(
})
}
async fn apply_role_to_config_inner(
pub(crate) async fn apply_role_to_config(
config: &mut Config,
role_name: Option<&str>,
) -> Result<(), String> {
apply_role_to_config_with_filesystem(config, role_name, &LocalFileSystem).await
}
async fn apply_role_to_config_inner_with_filesystem<F>(
config: &mut Config,
role_name: &str,
role: &AgentRoleConfig,
) -> anyhow::Result<()> {
filesystem: &F,
) -> anyhow::Result<()>
where
F: ExecutorFileSystem + ?Sized,
{
let is_built_in = !config.agent_roles.contains_key(role_name);
let Some(config_file) = role.config_file.as_ref() else {
return Ok(());
};
let role_layer_toml = load_role_layer_toml(config, config_file, is_built_in, role_name).await?;
let role_layer_toml = load_role_layer_toml_with_filesystem(
config,
config_file,
is_built_in,
role_name,
filesystem,
)
.await?;
let (preserve_current_profile, preserve_current_provider) =
preservation_policy(config, &role_layer_toml);
@@ -75,20 +92,28 @@ async fn apply_role_to_config_inner(
Ok(())
}
async fn load_role_layer_toml(
async fn load_role_layer_toml_with_filesystem<F>(
config: &Config,
config_file: &Path,
is_built_in: bool,
role_name: &str,
) -> anyhow::Result<TomlValue> {
let (role_config_toml, role_config_base) = if is_built_in {
filesystem: &F,
) -> anyhow::Result<TomlValue>
where
F: ExecutorFileSystem + ?Sized,
{
let (role_config_toml, role_config_base): (TomlValue, &Path) = if is_built_in {
let role_config_contents = built_in::config_file_contents(config_file)
.map(str::to_owned)
.ok_or(anyhow!("No corresponding config content"))?;
let role_config_toml: TomlValue = toml::from_str(&role_config_contents)?;
(role_config_toml, config.codex_home.as_path())
} else {
let role_config_contents = tokio::fs::read_to_string(config_file).await?;
let absolute_path = AbsolutePathBuf::from_absolute_path(config_file)
.map_err(|err| anyhow!("role config path must be absolute: {err}"))?;
let role_config_contents =
String::from_utf8(filesystem.read_file(&absolute_path).await?)
.map_err(|err| anyhow!("role config file is not valid UTF-8: {err}"))?;
let role_config_base = config_file
.parent()
.ok_or(anyhow!("No corresponding config content"))?;
@@ -297,41 +322,23 @@ pub(crate) mod spawn_tool_spec {
fn format_role(name: &str, declaration: &AgentRoleConfig) -> String {
if let Some(description) = &declaration.description {
let locked_settings_note = declaration
.config_file
.as_ref()
.and_then(|config_file| {
built_in::config_file_contents(config_file)
.map(str::to_owned)
.or_else(|| std::fs::read_to_string(config_file).ok())
})
.and_then(|contents| toml::from_str::<TomlValue>(&contents).ok())
.map(|role_toml| {
let model = role_toml
.get("model")
.and_then(TomlValue::as_str);
let reasoning_effort = role_toml
.get("model_reasoning_effort")
.and_then(TomlValue::as_str);
match (model, reasoning_effort) {
(Some(model), Some(reasoning_effort)) => format!(
"\n- This role's model is set to `{model}` and its reasoning effort is set to `{reasoning_effort}`. These settings cannot be changed."
),
(Some(model), None) => {
format!(
"\n- This role's model is set to `{model}` and cannot be changed."
)
}
(None, Some(reasoning_effort)) => {
format!(
"\n- This role's reasoning effort is set to `{reasoning_effort}` and cannot be changed."
)
}
(None, None) => String::new(),
}
})
.unwrap_or_default();
let locked_settings_note = match (
declaration.locked_model.as_deref(),
declaration.locked_reasoning_effort.as_deref(),
) {
(Some(model), Some(reasoning_effort)) => format!(
"\n- This role's model is set to `{model}` and its reasoning effort is set to `{reasoning_effort}`. These settings cannot be changed."
),
(Some(model), None) => {
format!("\n- This role's model is set to `{model}` and cannot be changed.")
}
(None, Some(reasoning_effort)) => {
format!(
"\n- This role's reasoning effort is set to `{reasoning_effort}` and cannot be changed."
)
}
(None, None) => String::new(),
};
format!("{name}: {{\n{description}{locked_settings_note}\n}}")
} else {
format!("{name}: no description")
@@ -351,6 +358,8 @@ mod built_in {
AgentRoleConfig {
description: Some("Default agent.".to_string()),
config_file: None,
locked_model: None,
locked_reasoning_effort: None,
nickname_candidates: None,
}
),
@@ -365,6 +374,8 @@ Rules:
- You are encouraged to spawn up multiple explorers in parallel when you have multiple distinct questions to ask about the codebase that can be answered independently. This allows you to get more information faster without waiting for one question to finish before asking the next. While waiting for the explorer results, you can continue working on other local tasks that do not depend on those results. This parallelism is a key advantage of delegation, so use it whenever you have multiple questions to ask.
- Reuse existing explorers for related questions."#.to_string()),
config_file: Some("explorer.toml".to_string().parse().unwrap_or_default()),
locked_model: None,
locked_reasoning_effort: None,
nickname_candidates: None,
}
),
@@ -380,6 +391,8 @@ Rules:
- Explicitly assign **ownership** of the task (files / responsibility). When the subtask involves code changes, you should clearly specify which files or modules the worker is responsible for. This helps avoid merge conflicts and ensures accountability. For example, you can say "Worker 1 is responsible for updating the authentication module, while Worker 2 will handle the database layer." By defining clear ownership, you can delegate more effectively and reduce coordination overhead.
- Always tell workers they are **not alone in the codebase**, and they should not revert the edits made by others, and they should adjust their implementation to accommodate the changes made by others. This is important because there may be multiple workers making changes in parallel, and they need to be aware of each other's work to avoid conflicts and ensure a cohesive final product."#.to_string()),
config_file: None,
locked_model: None,
locked_reasoning_effort: None,
nickname_candidates: None,
}
),
+32
View File
@@ -91,6 +91,8 @@ async fn apply_role_returns_unavailable_for_missing_user_role_file() {
AgentRoleConfig {
description: None,
config_file: Some(PathBuf::from("/path/does/not/exist.toml")),
locked_model: None,
locked_reasoning_effort: None,
nickname_candidates: None,
},
);
@@ -111,6 +113,8 @@ async fn apply_role_returns_unavailable_for_invalid_user_role_toml() {
AgentRoleConfig {
description: None,
config_file: Some(role_path),
locked_model: None,
locked_reasoning_effort: None,
nickname_candidates: None,
},
);
@@ -142,6 +146,8 @@ model = "role-model"
AgentRoleConfig {
description: None,
config_file: Some(role_path),
locked_model: None,
locked_reasoning_effort: None,
nickname_candidates: None,
},
);
@@ -173,6 +179,8 @@ async fn apply_role_preserves_unspecified_keys() {
AgentRoleConfig {
description: None,
config_file: Some(role_path),
locked_model: None,
locked_reasoning_effort: None,
nickname_candidates: None,
},
);
@@ -232,6 +240,8 @@ model_provider = "test-provider"
AgentRoleConfig {
description: None,
config_file: Some(role_path),
locked_model: None,
locked_reasoning_effort: None,
nickname_candidates: None,
},
);
@@ -286,6 +296,8 @@ model_verbosity = "high"
AgentRoleConfig {
description: None,
config_file: Some(role_path),
locked_model: None,
locked_reasoning_effort: None,
nickname_candidates: None,
},
);
@@ -352,6 +364,8 @@ model_provider = "role-provider"
AgentRoleConfig {
description: None,
config_file: Some(role_path),
locked_model: None,
locked_reasoning_effort: None,
nickname_candidates: None,
},
);
@@ -410,6 +424,8 @@ model_provider = "base-provider"
AgentRoleConfig {
description: None,
config_file: Some(role_path),
locked_model: None,
locked_reasoning_effort: None,
nickname_candidates: None,
},
);
@@ -474,6 +490,8 @@ model_reasoning_effort = "high"
AgentRoleConfig {
description: None,
config_file: Some(role_path),
locked_model: None,
locked_reasoning_effort: None,
nickname_candidates: None,
},
);
@@ -518,6 +536,8 @@ writable_roots = ["./sandbox-root"]
AgentRoleConfig {
description: None,
config_file: Some(role_path),
locked_model: None,
locked_reasoning_effort: None,
nickname_candidates: None,
},
);
@@ -577,6 +597,8 @@ async fn apply_role_takes_precedence_over_existing_session_flags_for_same_key()
AgentRoleConfig {
description: None,
config_file: Some(role_path),
locked_model: None,
locked_reasoning_effort: None,
nickname_candidates: None,
},
);
@@ -620,6 +642,8 @@ enabled = false
AgentRoleConfig {
description: None,
config_file: Some(role_path),
locked_model: None,
locked_reasoning_effort: None,
nickname_candidates: None,
},
);
@@ -648,6 +672,8 @@ fn spawn_tool_spec_build_deduplicates_user_defined_built_in_roles() {
AgentRoleConfig {
description: Some("user override".to_string()),
config_file: None,
locked_model: None,
locked_reasoning_effort: None,
nickname_candidates: None,
},
),
@@ -669,6 +695,8 @@ fn spawn_tool_spec_lists_user_defined_roles_before_built_ins() {
AgentRoleConfig {
description: Some("first".to_string()),
config_file: None,
locked_model: None,
locked_reasoning_effort: None,
nickname_candidates: None,
},
)]);
@@ -696,6 +724,8 @@ fn spawn_tool_spec_marks_role_locked_model_and_reasoning_effort() {
AgentRoleConfig {
description: Some("Research carefully.".to_string()),
config_file: Some(role_path),
locked_model: Some("gpt-5".to_string()),
locked_reasoning_effort: Some("high".to_string()),
nickname_candidates: None,
},
)]);
@@ -721,6 +751,8 @@ fn spawn_tool_spec_marks_role_locked_reasoning_effort_only() {
AgentRoleConfig {
description: Some("Review carefully.".to_string()),
config_file: Some(role_path),
locked_model: None,
locked_reasoning_effort: Some("medium".to_string()),
nickname_candidates: None,
},
)]);
+151 -48
View File
@@ -25,6 +25,7 @@ use crate::compact::run_inline_auto_compact_task;
use crate::compact::should_use_remote_compact_task;
use crate::compact_remote::run_inline_remote_auto_compact_task;
use crate::config::ManagedFeatures;
use crate::config_loader::ConfigLayerStackOrdering;
use crate::connectors;
use crate::exec_policy::ExecPolicyManager;
use crate::features::FEATURES;
@@ -440,9 +441,42 @@ impl Codex {
let environment =
Arc::new(Environment::create(config.experimental_exec_server_url.clone()).await?);
let environment_filesystem = environment.get_filesystem();
let config_layer_stack = config.config_layer_stack.clone();
if !config_layer_stack
.get_layers(
ConfigLayerStackOrdering::LowestPrecedenceFirst,
/*include_disabled*/ false,
)
.is_empty()
{
match crate::config::agent_roles::load_agent_roles_with_filesystem(
&config_layer_stack,
&mut config.startup_warnings,
&environment_filesystem,
)
.await
{
Ok(agent_roles) => {
config.agent_roles = crate::config::agent_roles::merge_loaded_agent_roles(
&config.agent_roles,
agent_roles,
);
}
Err(err) => {
let message =
format!("Ignoring environment-backed agent role definitions: {err}");
warn!("{message}");
config.startup_warnings.push(message);
}
}
}
let loaded_skills = skills_manager
.skills_for_config_with_filesystem(&config, &environment_filesystem)
.skills_for_config_with_filesystem(
&config,
environment.environment_id(),
&environment_filesystem,
)
.await;
for err in &loaded_skills.errors {
@@ -460,7 +494,8 @@ impl Codex {
let _ = config.features.disable(Feature::Collab);
}
if config.features.enabled(Feature::JsRepl)
if environment.experimental_exec_server_url().is_none()
&& config.features.enabled(Feature::JsRepl)
&& let Err(err) = resolve_compatible_node(config.js_repl_node_path.as_deref()).await
{
let _ = config.features.disable(Feature::JsRepl);
@@ -477,7 +512,8 @@ impl Codex {
warn!("{message}");
config.startup_warnings.push(message);
}
if config.features.enabled(Feature::CodeMode)
if environment.experimental_exec_server_url().is_none()
&& config.features.enabled(Feature::CodeMode)
&& let Err(err) = resolve_compatible_node(config.js_repl_node_path.as_deref()).await
{
let message = format!(
@@ -500,9 +536,12 @@ impl Codex {
Arc::clone(exec_policy)
} else {
Arc::new(
ExecPolicyManager::load(&config.config_layer_stack)
.await
.map_err(|err| CodexErr::Fatal(format!("failed to load rules: {err}")))?,
ExecPolicyManager::load_with_filesystem(
&config.config_layer_stack,
&environment_filesystem,
)
.await
.map_err(|err| CodexErr::Fatal(format!("failed to load rules: {err}")))?,
)
};
@@ -607,6 +646,7 @@ impl Codex {
let session = Session::new(
session_configuration,
config.clone(),
environment,
auth_manager.clone(),
models_manager.clone(),
exec_policy,
@@ -1405,6 +1445,7 @@ impl Session {
async fn new(
mut session_configuration: SessionConfiguration,
config: Arc<Config>,
environment: Arc<Environment>,
auth_manager: Arc<AuthManager>,
models_manager: Arc<ModelsManager>,
exec_policy: Arc<ExecPolicyManager>,
@@ -1520,13 +1561,21 @@ impl Session {
let mcp_manager_for_mcp = Arc::clone(&mcp_manager);
let auth_and_mcp_fut = async move {
let auth = auth_manager_clone.auth().await;
let mcp_servers = mcp_manager_for_mcp.effective_servers(&config_for_mcp, auth.as_ref());
let environment =
Environment::create(config_for_mcp.experimental_exec_server_url.clone()).await?;
let mcp_servers = mcp_manager_for_mcp
.effective_servers_with_filesystem(
&config_for_mcp,
auth.as_ref(),
&environment.get_filesystem(),
)
.await;
let auth_statuses = compute_auth_statuses(
mcp_servers.iter(),
config_for_mcp.mcp_oauth_credentials_store_mode,
)
.await;
(auth, mcp_servers, auth_statuses)
Ok::<_, anyhow::Error>((auth, mcp_servers, auth_statuses))
}
.instrument(info_span!(
"session_init.auth_mcp",
@@ -1534,16 +1583,17 @@ impl Session {
));
// Join all independent futures.
let (
rollout_recorder_and_state_db,
(history_log_id, history_entry_count),
(auth, mcp_servers, auth_statuses),
) = tokio::join!(rollout_fut, history_meta_fut, auth_and_mcp_fut);
let (rollout_recorder_and_state_db, (history_log_id, history_entry_count), auth_and_mcp) =
tokio::join!(rollout_fut, history_meta_fut, auth_and_mcp_fut);
let (rollout_recorder, state_db_ctx) = rollout_recorder_and_state_db.map_err(|e| {
error!("failed to initialize rollout recorder: {e:#}");
e
})?;
let (auth, mcp_servers, auth_statuses) = auth_and_mcp.map_err(|e| {
error!("failed to initialize MCP environment: {e:#}");
e
})?;
let rollout_path = rollout_recorder
.as_ref()
.map(|rec| rec.rollout_path.clone());
@@ -1592,6 +1642,7 @@ impl Session {
let auth = auth.as_ref();
let auth_mode = auth.map(CodexAuth::auth_mode).map(TelemetryAuthMode::from);
let auth_mode_label = auth_mode.clone().map(|mode| mode.to_string());
let account_id = auth.and_then(CodexAuth::get_account_id);
let account_email = auth.and_then(CodexAuth::get_account_email);
let originator = crate::default_client::originator().value;
@@ -1621,7 +1672,7 @@ impl Session {
conversation_id: Some(conversation_id.to_string()),
app_version: Some(env!("CARGO_PKG_VERSION").to_string()),
user_account_id: account_id,
auth_mode: auth_mode.map(|mode| mode.to_string()),
auth_mode: auth_mode_label,
originator: Some(originator),
user_email: account_email,
terminal_type: Some(terminal_type),
@@ -1915,7 +1966,12 @@ impl Session {
required_mcp_servers.sort();
let enabled_mcp_server_count = mcp_servers.values().filter(|server| server.enabled).count();
let required_mcp_server_count = required_mcp_servers.len();
let tool_plugin_provenance = mcp_manager.tool_plugin_provenance(config.as_ref());
let tool_plugin_provenance = mcp_manager
.tool_plugin_provenance_with_filesystem(
config.as_ref(),
&sess.services.environment.get_filesystem(),
)
.await;
{
let mut cancel_guard = sess.services.mcp_startup_cancellation_token.lock().await;
cancel_guard.cancel();
@@ -2404,7 +2460,11 @@ impl Session {
let skills_outcome = Arc::new(
self.services
.skills_manager
.skills_for_config_with_filesystem(&per_turn_config, &environment_filesystem)
.skills_for_config_with_filesystem(
&per_turn_config,
self.services.environment.environment_id(),
&environment_filesystem,
)
.await,
);
let mut turn_context: TurnContext = Self::make_turn_context(
@@ -2491,24 +2551,7 @@ impl Session {
.codex_home
.join(CONFIG_TOML_FILE)
};
let user_config = match std::fs::read_to_string(&config_toml_path) {
Ok(contents) => match toml::from_str::<toml::Value>(&contents) {
Ok(config) => config,
Err(err) => {
warn!("failed to parse user config while reloading layer: {err}");
return;
}
},
Err(err) if err.kind() == std::io::ErrorKind::NotFound => {
toml::Value::Table(Default::default())
}
Err(err) => {
warn!("failed to read user config while reloading layer: {err}");
return;
}
};
let filesystem = self.services.environment.filesystem();
let config_toml_path = match AbsolutePathBuf::try_from(config_toml_path) {
Ok(path) => path,
Err(err) => {
@@ -2517,6 +2560,29 @@ impl Session {
}
};
let user_config = match filesystem.read_file(&config_toml_path).await {
Ok(contents) => match String::from_utf8(contents) {
Ok(contents) => match toml::from_str::<toml::Value>(&contents) {
Ok(config) => config,
Err(err) => {
warn!("failed to parse user config while reloading layer: {err}");
return;
}
},
Err(err) => {
warn!("failed to decode user config while reloading layer: {err}");
return;
}
},
Err(err) if err.kind() == std::io::ErrorKind::NotFound => {
toml::Value::Table(toml::map::Map::new())
}
Err(err) => {
warn!("failed to read user config while reloading layer: {err}");
return;
}
};
let mut state = self.state.lock().await;
let mut config = (*state.session_configuration.original_config_do_not_use).clone();
config.config_layer_stack = config
@@ -3510,7 +3576,12 @@ impl Session {
let loaded_plugins = self
.services
.plugins_manager
.plugins_for_config(&turn_context.config);
.plugins_for_config_with_filesystem(
&turn_context.config,
/*force_reload*/ false,
&self.services.environment.get_filesystem(),
)
.await;
if let Some(plugin_section) = render_plugins_section(loaded_plugins.capability_summaries())
{
developer_sections.push(plugin_section);
@@ -4038,7 +4109,11 @@ impl Session {
let tool_plugin_provenance = self
.services
.mcp_manager
.tool_plugin_provenance(config.as_ref());
.tool_plugin_provenance_with_filesystem(
config.as_ref(),
&self.services.environment.get_filesystem(),
)
.await;
let mcp_servers = with_codex_apps_mcp(
mcp_servers,
self.features.apps_enabled_for_auth(auth.as_ref()),
@@ -4408,7 +4483,6 @@ mod handlers {
use crate::tasks::UserShellCommandMode;
use crate::tasks::UserShellCommandTask;
use crate::tasks::execute_user_shell_command;
use codex_protocol::custom_prompts::CustomPrompt;
use codex_protocol::protocol::CodexErrorInfo;
use codex_protocol::protocol::ErrorEvent;
use codex_protocol::protocol::Event;
@@ -4427,6 +4501,7 @@ mod handlers {
use codex_protocol::protocol::WarningEvent;
use codex_protocol::request_permissions::RequestPermissionsResponse;
use codex_protocol::request_user_input::RequestUserInputResponse;
use codex_utils_absolute_path::AbsolutePathBuf;
use crate::context_manager::is_user_turn_boundary;
use codex_protocol::config_types::CollaborationMode;
@@ -4753,7 +4828,12 @@ mod handlers {
let mcp_servers = sess
.services
.mcp_manager
.effective_servers(config, auth.as_ref());
.effective_servers_with_filesystem(
config,
auth.as_ref(),
&sess.services.environment.get_filesystem(),
)
.await;
let snapshot = collect_mcp_snapshot_from_manager(
&mcp_connection_manager,
compute_auth_statuses(mcp_servers.iter(), config.mcp_oauth_credentials_store_mode)
@@ -4768,12 +4848,19 @@ mod handlers {
}
pub async fn list_custom_prompts(sess: &Session, sub_id: String) {
let custom_prompts: Vec<CustomPrompt> =
if let Some(dir) = crate::custom_prompts::default_prompts_dir() {
crate::custom_prompts::discover_prompts_in(&dir).await
} else {
Vec::new()
};
let codex_home = sess.codex_home().await;
let prompts_dir = crate::custom_prompts::prompts_dir(&codex_home);
let custom_prompts = match AbsolutePathBuf::try_from(prompts_dir) {
Ok(prompts_dir) => {
let filesystem = sess.services.environment.get_filesystem();
crate::custom_prompts::discover_prompts_in_with_filesystem(
&prompts_dir,
&filesystem,
)
.await
}
Err(_) => Vec::new(),
};
let event = Event {
id: sub_id,
@@ -4807,6 +4894,7 @@ mod handlers {
&cwd,
config.as_ref(),
force_reload,
sess.services.environment.environment_id(),
&environment_filesystem,
)
.await;
@@ -4821,7 +4909,10 @@ mod handlers {
let event = Event {
id: sub_id,
msg: EventMsg::ListSkillsResponse(ListSkillsResponseEvent { skills }),
msg: EventMsg::ListSkillsResponse(ListSkillsResponseEvent {
environment_id: sess.services.environment.environment_id().to_string(),
skills,
}),
};
sess.send_event_raw(event).await;
}
@@ -5412,7 +5503,12 @@ pub(crate) async fn run_turn(
let loaded_plugins = sess
.services
.plugins_manager
.plugins_for_config(&turn_context.config);
.plugins_for_config_with_filesystem(
&turn_context.config,
/*force_reload*/ false,
&sess.services.environment.get_filesystem(),
)
.await;
// Structured plugin:// mentions are resolved from the current session's
// enabled plugins, then converted into turn-scoped guidance below.
let mentioned_plugins =
@@ -6317,7 +6413,12 @@ pub(crate) async fn built_tools(
let loaded_plugins = sess
.services
.plugins_manager
.plugins_for_config(&turn_context.config);
.plugins_for_config_with_filesystem(
&turn_context.config,
/*force_reload*/ false,
&sess.services.environment.get_filesystem(),
)
.await;
let mut effective_explicitly_enabled_connectors = explicitly_enabled_connectors.clone();
effective_explicitly_enabled_connectors.extend(sess.get_connector_selection().await);
@@ -6347,10 +6448,12 @@ pub(crate) async fn built_tools(
&& turn_context.tools_config.tool_suggest
{
if let Some(accessible_connectors) = accessible_connectors_with_enabled_state.as_ref() {
match connectors::list_tool_suggest_discoverable_tools_with_auth(
let environment_filesystem = sess.services.environment.get_filesystem();
match connectors::list_tool_suggest_discoverable_tools_with_auth_with_filesystem(
&turn_context.config,
auth.as_ref(),
accessible_connectors.as_slice(),
&environment_filesystem,
)
.await
.map(|discoverable_tools| {
+8
View File
@@ -2202,6 +2202,8 @@ enabled = false
crate::config::AgentRoleConfig {
description: None,
config_file: Some(role_path),
locked_model: None,
locked_reasoning_effort: None,
nickname_candidates: None,
},
);
@@ -2344,9 +2346,15 @@ async fn session_new_fails_when_zsh_fork_enabled_without_zsh_path() {
Arc::clone(&plugins_manager),
true,
));
let environment = Arc::new(
codex_exec_server::Environment::create(None)
.await
.expect("create environment"),
);
let result = Session::new(
session_configuration,
Arc::clone(&config),
environment,
auth_manager,
models_manager,
Arc::new(ExecPolicyManager::default()),
+378
View File
@@ -4,6 +4,7 @@ use super::AgentsToml;
use super::ConfigToml;
use crate::config_loader::ConfigLayerStack;
use crate::config_loader::ConfigLayerStackOrdering;
use codex_exec_server::ExecutorFileSystem;
use codex_utils_absolute_path::AbsolutePathBuf;
use codex_utils_absolute_path::AbsolutePathBufGuard;
use serde::Deserialize;
@@ -107,6 +108,125 @@ pub(crate) fn load_agent_roles(
Ok(roles)
}
pub(crate) async fn load_agent_roles_with_filesystem<F>(
config_layer_stack: &ConfigLayerStack,
startup_warnings: &mut Vec<String>,
filesystem: &F,
) -> std::io::Result<BTreeMap<String, AgentRoleConfig>>
where
F: ExecutorFileSystem + ?Sized,
{
let layers = config_layer_stack.get_layers(
ConfigLayerStackOrdering::LowestPrecedenceFirst,
/*include_disabled*/ false,
);
if layers.is_empty() {
return Ok(BTreeMap::new());
}
let mut roles: BTreeMap<String, AgentRoleConfig> = BTreeMap::new();
for layer in layers {
let mut layer_roles: BTreeMap<String, AgentRoleConfig> = BTreeMap::new();
let mut declared_role_files = BTreeSet::new();
let agents_toml = match agents_toml_from_layer(&layer.config) {
Ok(agents_toml) => agents_toml,
Err(err) => {
push_agent_role_warning(startup_warnings, err);
None
}
};
if let Some(agents_toml) = agents_toml {
for (declared_role_name, role_toml) in &agents_toml.roles {
let (role_name, role) = match read_declared_role_with_filesystem(
declared_role_name,
role_toml,
filesystem,
)
.await
{
Ok(role) => role,
Err(err) => {
push_agent_role_warning(startup_warnings, err);
continue;
}
};
if let Some(config_file) = role.config_file.clone() {
declared_role_files.insert(config_file);
}
if layer_roles.contains_key(&role_name) {
push_agent_role_warning(
startup_warnings,
std::io::Error::new(
std::io::ErrorKind::InvalidInput,
format!(
"duplicate agent role name `{role_name}` declared in the same config layer"
),
),
);
continue;
}
layer_roles.insert(role_name, role);
}
}
if let Some(config_folder) = layer.config_folder() {
for (role_name, role) in discover_agent_roles_in_dir_with_filesystem(
config_folder.as_path().join("agents").as_path(),
&declared_role_files,
startup_warnings,
filesystem,
)
.await?
{
if layer_roles.contains_key(&role_name) {
push_agent_role_warning(
startup_warnings,
std::io::Error::new(
std::io::ErrorKind::InvalidInput,
format!(
"duplicate agent role name `{role_name}` declared in the same config layer"
),
),
);
continue;
}
layer_roles.insert(role_name, role);
}
}
for (role_name, role) in layer_roles {
let mut merged_role = role;
if let Some(existing_role) = roles.get(&role_name) {
merge_missing_role_fields(&mut merged_role, existing_role);
}
if let Err(err) = validate_required_agent_role_description(
&role_name,
merged_role.description.as_deref(),
) {
push_agent_role_warning(startup_warnings, err);
continue;
}
roles.insert(role_name, merged_role);
}
}
Ok(roles)
}
pub(crate) fn merge_loaded_agent_roles(
existing_roles: &BTreeMap<String, AgentRoleConfig>,
loaded_roles: BTreeMap<String, AgentRoleConfig>,
) -> BTreeMap<String, AgentRoleConfig> {
let mut merged_roles = existing_roles.clone();
for (role_name, mut loaded_role) in loaded_roles {
if let Some(existing_role) = merged_roles.get(&role_name) {
merge_missing_role_fields(&mut loaded_role, existing_role);
}
merged_roles.insert(role_name, loaded_role);
}
merged_roles
}
fn push_agent_role_warning(startup_warnings: &mut Vec<String>, err: std::io::Error) {
let message = format!("Ignoring malformed agent role definition: {err}");
tracing::warn!("{message}");
@@ -144,6 +264,41 @@ fn read_declared_role(
let parsed_file = read_resolved_agent_role_file(config_file, Some(declared_role_name))?;
role_name = parsed_file.role_name;
role.description = parsed_file.description.or(role.description);
role.locked_model = parsed_file.locked_model.or(role.locked_model);
role.locked_reasoning_effort = parsed_file
.locked_reasoning_effort
.or(role.locked_reasoning_effort);
role.nickname_candidates = parsed_file.nickname_candidates.or(role.nickname_candidates);
}
Ok((role_name, role))
}
async fn read_declared_role_with_filesystem<F>(
declared_role_name: &str,
role_toml: &AgentRoleToml,
filesystem: &F,
) -> std::io::Result<(String, AgentRoleConfig)>
where
F: ExecutorFileSystem + ?Sized,
{
let mut role =
agent_role_config_from_toml_with_filesystem(declared_role_name, role_toml, filesystem)
.await?;
let mut role_name = declared_role_name.to_string();
if let Some(config_file) = role.config_file.clone() {
let parsed_file = read_resolved_agent_role_file_with_filesystem(
&config_file,
Some(declared_role_name),
filesystem,
)
.await?;
role_name = parsed_file.role_name;
role.description = parsed_file.description.or(role.description);
role.locked_model = parsed_file.locked_model.or(role.locked_model);
role.locked_reasoning_effort = parsed_file
.locked_reasoning_effort
.or(role.locked_reasoning_effort);
role.nickname_candidates = parsed_file.nickname_candidates.or(role.nickname_candidates);
}
@@ -153,6 +308,11 @@ fn read_declared_role(
fn merge_missing_role_fields(role: &mut AgentRoleConfig, fallback: &AgentRoleConfig) {
role.description = role.description.clone().or(fallback.description.clone());
role.config_file = role.config_file.clone().or(fallback.config_file.clone());
role.locked_model = role.locked_model.clone().or(fallback.locked_model.clone());
role.locked_reasoning_effort = role
.locked_reasoning_effort
.clone()
.or(fallback.locked_reasoning_effort.clone());
role.nickname_candidates = role
.nickname_candidates
.clone()
@@ -189,6 +349,37 @@ fn agent_role_config_from_toml(
Ok(AgentRoleConfig {
description,
config_file,
locked_model: None,
locked_reasoning_effort: None,
nickname_candidates,
})
}
async fn agent_role_config_from_toml_with_filesystem<F>(
role_name: &str,
role: &AgentRoleToml,
filesystem: &F,
) -> std::io::Result<AgentRoleConfig>
where
F: ExecutorFileSystem + ?Sized,
{
let config_file = role.config_file.as_ref().map(AbsolutePathBuf::to_path_buf);
validate_agent_role_config_file_with_filesystem(role_name, config_file.as_deref(), filesystem)
.await?;
let description = normalize_agent_role_description(
&format!("agents.{role_name}.description"),
role.description.as_deref(),
)?;
let nickname_candidates = normalize_agent_role_nickname_candidates(
&format!("agents.{role_name}.nickname_candidates"),
role.nickname_candidates.as_deref(),
)?;
Ok(AgentRoleConfig {
description,
config_file,
locked_model: None,
locked_reasoning_effort: None,
nickname_candidates,
})
}
@@ -207,6 +398,8 @@ struct RawAgentRoleFileToml {
pub(crate) struct ResolvedAgentRoleFile {
pub(crate) role_name: String,
pub(crate) description: Option<String>,
pub(crate) locked_model: Option<String>,
pub(crate) locked_reasoning_effort: Option<String>,
pub(crate) nickname_candidates: Option<Vec<String>>,
pub(crate) config: TomlValue,
}
@@ -284,10 +477,20 @@ pub(crate) fn parse_agent_role_file_contents(
config_table.remove("name");
config_table.remove("description");
config_table.remove("nickname_candidates");
let locked_model = config
.get("model")
.and_then(TomlValue::as_str)
.map(str::to_owned);
let locked_reasoning_effort = config
.get("model_reasoning_effort")
.and_then(TomlValue::as_str)
.map(str::to_owned);
Ok(ResolvedAgentRoleFile {
role_name,
description,
locked_model,
locked_reasoning_effort,
nickname_candidates,
config,
})
@@ -306,6 +509,38 @@ fn read_resolved_agent_role_file(
)
}
async fn read_resolved_agent_role_file_with_filesystem<F>(
path: &Path,
role_name_hint: Option<&str>,
filesystem: &F,
) -> std::io::Result<ResolvedAgentRoleFile>
where
F: ExecutorFileSystem + ?Sized,
{
let absolute_path = AbsolutePathBuf::from_absolute_path(path).map_err(|err| {
std::io::Error::new(
std::io::ErrorKind::InvalidInput,
format!("agent role path must be absolute: {err}"),
)
})?;
let contents = filesystem.read_file(&absolute_path).await?;
let contents = String::from_utf8(contents).map_err(|err| {
std::io::Error::new(
std::io::ErrorKind::InvalidData,
format!(
"agent role file at {} is not valid UTF-8: {err}",
path.display()
),
)
})?;
parse_agent_role_file_contents(
&contents,
path,
path.parent().unwrap_or(path),
role_name_hint,
)
}
fn normalize_agent_role_description(
field_label: &str,
description: Option<&str>,
@@ -389,6 +624,49 @@ fn validate_agent_role_config_file(
}
}
async fn validate_agent_role_config_file_with_filesystem<F>(
role_name: &str,
config_file: Option<&Path>,
filesystem: &F,
) -> std::io::Result<()>
where
F: ExecutorFileSystem + ?Sized,
{
let Some(config_file) = config_file else {
return Ok(());
};
let absolute_path = AbsolutePathBuf::from_absolute_path(config_file).map_err(|err| {
std::io::Error::new(
std::io::ErrorKind::InvalidInput,
format!(
"agents.{role_name}.config_file must point to an absolute file path at {}: {err}",
config_file.display()
),
)
})?;
let metadata = filesystem.get_metadata(&absolute_path).await.map_err(|e| {
std::io::Error::new(
std::io::ErrorKind::InvalidInput,
format!(
"agents.{role_name}.config_file must point to an existing file at {}: {e}",
config_file.display()
),
)
})?;
if metadata.is_file {
Ok(())
} else {
Err(std::io::Error::new(
std::io::ErrorKind::InvalidInput,
format!(
"agents.{role_name}.config_file must point to a file: {}",
config_file.display()
),
))
}
}
fn normalize_agent_role_nickname_candidates(
field_label: &str,
nickname_candidates: Option<&[String]>,
@@ -479,6 +757,65 @@ fn discover_agent_roles_in_dir(
AgentRoleConfig {
description: parsed_file.description,
config_file: Some(agent_file),
locked_model: parsed_file.locked_model,
locked_reasoning_effort: parsed_file.locked_reasoning_effort,
nickname_candidates: parsed_file.nickname_candidates,
},
);
}
Ok(roles)
}
async fn discover_agent_roles_in_dir_with_filesystem<F>(
agents_dir: &Path,
declared_role_files: &BTreeSet<PathBuf>,
startup_warnings: &mut Vec<String>,
filesystem: &F,
) -> std::io::Result<BTreeMap<String, AgentRoleConfig>>
where
F: ExecutorFileSystem + ?Sized,
{
let mut roles = BTreeMap::new();
for agent_file in collect_agent_role_files_with_filesystem(agents_dir, filesystem).await? {
if declared_role_files.contains(&agent_file) {
continue;
}
let parsed_file = match read_resolved_agent_role_file_with_filesystem(
&agent_file,
/*role_name_hint*/ None,
filesystem,
)
.await
{
Ok(parsed_file) => parsed_file,
Err(err) => {
push_agent_role_warning(startup_warnings, err);
continue;
}
};
let role_name = parsed_file.role_name;
if roles.contains_key(&role_name) {
push_agent_role_warning(
startup_warnings,
std::io::Error::new(
std::io::ErrorKind::InvalidInput,
format!(
"duplicate agent role name `{role_name}` discovered in {}",
agents_dir.display()
),
),
);
continue;
}
roles.insert(
role_name,
AgentRoleConfig {
description: parsed_file.description,
config_file: Some(agent_file),
locked_model: parsed_file.locked_model,
locked_reasoning_effort: parsed_file.locked_reasoning_effort,
nickname_candidates: parsed_file.nickname_candidates,
},
);
@@ -494,6 +831,47 @@ fn collect_agent_role_files(dir: &Path) -> std::io::Result<Vec<PathBuf>> {
Ok(files)
}
async fn collect_agent_role_files_with_filesystem<F>(
dir: &Path,
filesystem: &F,
) -> std::io::Result<Vec<PathBuf>>
where
F: ExecutorFileSystem + ?Sized,
{
let mut files = Vec::new();
let mut pending_dirs = vec![dir.to_path_buf()];
while let Some(dir) = pending_dirs.pop() {
let absolute_dir = match AbsolutePathBuf::from_absolute_path(&dir) {
Ok(dir) => dir,
Err(_) => continue,
};
let entries = match filesystem.read_directory(&absolute_dir).await {
Ok(entries) => entries,
Err(err) if err.kind() == ErrorKind::NotFound => continue,
Err(err) => return Err(err),
};
for entry in entries {
let path = dir.join(&entry.file_name);
if entry.is_directory {
pending_dirs.push(path);
continue;
}
if entry.is_file
&& path
.extension()
.is_some_and(|extension| extension == "toml")
{
files.push(path);
}
}
}
files.sort();
Ok(files)
}
fn collect_agent_role_files_recursive(dir: &Path, files: &mut Vec<PathBuf>) -> std::io::Result<()> {
let read_dir = match std::fs::read_dir(dir) {
Ok(read_dir) => read_dir,
+31
View File
@@ -16,6 +16,7 @@ use crate::config_loader::RequirementSource;
use crate::features::Feature;
use assert_matches::assert_matches;
use codex_config::CONFIG_TOML_FILE;
use codex_exec_server::LocalFileSystem;
use codex_protocol::permissions::FileSystemAccessMode;
use codex_protocol::permissions::FileSystemPath;
use codex_protocol::permissions::FileSystemSandboxEntry;
@@ -5706,6 +5707,36 @@ smart_approvals = true
Ok(())
}
#[tokio::test]
async fn smart_approvals_alias_is_migrated_with_filesystem_builder() -> std::io::Result<()> {
let codex_home = TempDir::new()?;
std::fs::write(
codex_home.path().join(CONFIG_TOML_FILE),
r#"[features]
smart_approvals = true
"#,
)?;
let config = ConfigBuilder::default()
.codex_home(codex_home.path().to_path_buf())
.fallback_cwd(Some(codex_home.path().to_path_buf()))
.build_with_filesystem(&LocalFileSystem)
.await?;
assert!(config.features.enabled(Feature::GuardianApproval));
assert_eq!(
config.approvals_reviewer,
ApprovalsReviewer::GuardianSubagent
);
let serialized = tokio::fs::read_to_string(codex_home.path().join(CONFIG_TOML_FILE)).await?;
assert!(serialized.contains("guardian_approval = true"));
assert!(serialized.contains("approvals_reviewer = \"guardian_subagent\""));
assert!(!serialized.contains("smart_approvals"));
Ok(())
}
#[tokio::test]
async fn smart_approvals_alias_is_migrated_in_profiles() -> std::io::Result<()> {
let codex_home = TempDir::new()?;
+80
View File
@@ -5,10 +5,12 @@ use crate::path_utils::resolve_symlink_write_paths;
use crate::path_utils::write_atomically;
use anyhow::Context;
use codex_config::CONFIG_TOML_FILE;
use codex_exec_server::ExecutorFileSystem;
use codex_protocol::config_types::Personality;
use codex_protocol::config_types::ServiceTier;
use codex_protocol::config_types::TrustLevel;
use codex_protocol::openai_models::ReasoningEffort;
use codex_utils_absolute_path::AbsolutePathBuf;
use std::collections::BTreeMap;
use std::collections::HashMap;
use std::path::Path;
@@ -753,6 +755,40 @@ pub fn apply_blocking(
Ok(())
}
pub fn apply_to_serialized(
existing_contents: &str,
profile: Option<&str>,
edits: &[ConfigEdit],
) -> anyhow::Result<Option<String>> {
if edits.is_empty() {
return Ok(None);
}
let doc = if existing_contents.is_empty() {
DocumentMut::new()
} else {
existing_contents.parse::<DocumentMut>()?
};
let profile = profile.map(ToOwned::to_owned).or_else(|| {
doc.get("profile")
.and_then(|item| item.as_str())
.map(ToOwned::to_owned)
});
let mut document = ConfigDocument::new(doc, profile);
let mut mutated = false;
for edit in edits {
mutated |= document.apply(edit)?;
}
if !mutated {
return Ok(None);
}
Ok(Some(document.doc.to_string()))
}
/// Persist edits asynchronously by offloading the blocking writer.
pub async fn apply(
codex_home: &Path,
@@ -766,6 +802,37 @@ pub async fn apply(
.context("config persistence task panicked")?
}
pub async fn apply_with_filesystem<F>(
codex_home: &Path,
profile: Option<&str>,
edits: &[ConfigEdit],
filesystem: &F,
) -> anyhow::Result<()>
where
F: ExecutorFileSystem + ?Sized,
{
if edits.is_empty() {
return Ok(());
}
let config_path = AbsolutePathBuf::try_from(codex_home.join(CONFIG_TOML_FILE))
.context("resolve config.toml path")?;
let serialized = match filesystem.read_file(&config_path).await {
Ok(contents) => String::from_utf8(contents).context("config.toml was not valid UTF-8")?,
Err(err) if err.kind() == std::io::ErrorKind::NotFound => String::new(),
Err(err) => return Err(err.into()),
};
let Some(updated) = apply_to_serialized(&serialized, profile, edits)? else {
return Ok(());
};
filesystem
.write_file(&config_path, updated.into_bytes())
.await
.with_context(|| format!("failed to persist config.toml at {}", config_path.display()))?;
Ok(())
}
/// Fluent builder to batch config edits and apply them atomically.
#[derive(Default)]
pub struct ConfigEditsBuilder {
@@ -988,6 +1055,19 @@ impl ConfigEditsBuilder {
.await
.context("config persistence task panicked")?
}
pub async fn apply_with_filesystem<F>(self, filesystem: &F) -> anyhow::Result<()>
where
F: ExecutorFileSystem + ?Sized,
{
apply_with_filesystem(
&self.codex_home,
self.profile.as_deref(),
&self.edits,
filesystem,
)
.await
}
}
#[cfg(test)]
+379 -25
View File
@@ -1,6 +1,7 @@
use crate::auth::AuthCredentialsStoreMode;
use crate::config::edit::ConfigEdit;
use crate::config::edit::ConfigEditsBuilder;
use crate::config::edit::apply_to_serialized;
use crate::config::types::AppsConfigToml;
use crate::config::types::DEFAULT_OTEL_ENVIRONMENT;
use crate::config::types::History;
@@ -39,6 +40,8 @@ use crate::config_loader::McpServerRequirement;
use crate::config_loader::ResidencyRequirement;
use crate::config_loader::Sourced;
use crate::config_loader::load_config_layers_state;
use crate::config_loader::load_config_layers_state_with_filesystem;
use crate::config_loader::resolve_root_git_project_for_trust_with_filesystem;
use crate::features::Feature;
use crate::features::FeatureOverrides;
use crate::features::Features;
@@ -99,6 +102,7 @@ use std::path::PathBuf;
use crate::config::permissions::compile_permission_profile;
use crate::config::permissions::network_proxy_config_from_profile_network;
use crate::config::profile::ConfigProfile;
use codex_exec_server::ExecutorFileSystem;
use codex_network_proxy::NetworkProxyConfig;
use toml::Value as TomlValue;
use toml_edit::DocumentMut;
@@ -698,6 +702,141 @@ impl ConfigBuilder {
config_layer_stack,
)
}
pub async fn build_with_filesystem<F>(self, filesystem: &F) -> std::io::Result<Config>
where
F: ExecutorFileSystem + ?Sized,
{
let Self {
codex_home,
cli_overrides,
harness_overrides,
loader_overrides,
cloud_requirements,
fallback_cwd,
} = self;
let codex_home = codex_home.ok_or_else(|| {
std::io::Error::new(
std::io::ErrorKind::InvalidInput,
"ConfigBuilder::build_with_filesystem requires an explicit codex_home",
)
})?;
if let Err(err) =
maybe_migrate_smart_approvals_alias_with_filesystem(&codex_home, filesystem).await
{
tracing::warn!(error = %err, "failed to migrate smart_approvals feature alias");
}
let cli_overrides = cli_overrides.unwrap_or_default();
let mut harness_overrides = harness_overrides.unwrap_or_default();
let loader_overrides = loader_overrides.unwrap_or_default();
let cwd_override = harness_overrides.cwd.as_deref().or(fallback_cwd.as_deref());
let cwd = cwd_override.ok_or_else(|| {
std::io::Error::new(
std::io::ErrorKind::InvalidInput,
"ConfigBuilder::build_with_filesystem requires an explicit cwd",
)
})?;
let cwd = AbsolutePathBuf::try_from(cwd)?;
harness_overrides.cwd = Some(cwd.to_path_buf());
let config_layer_stack = load_config_layers_state_with_filesystem(
&codex_home,
Some(cwd.clone()),
&cli_overrides,
loader_overrides,
cloud_requirements,
filesystem,
)
.await?;
let merged_toml = config_layer_stack.effective_config();
let config_toml: ConfigToml = match merged_toml.try_into() {
Ok(config_toml) => config_toml,
Err(err) => {
if let Some(config_error) =
crate::config_loader::first_layer_config_error(&config_layer_stack).await
{
return Err(crate::config_loader::io_error_from_config_error(
std::io::ErrorKind::InvalidData,
config_error,
Some(err),
));
}
return Err(std::io::Error::new(std::io::ErrorKind::InvalidData, err));
}
};
let active_profile_name = harness_overrides
.config_profile
.as_ref()
.or(config_toml.profile.as_ref())
.cloned();
let config_profile = match active_profile_name.as_ref() {
Some(key) => config_toml
.profiles
.get(key)
.ok_or_else(|| {
std::io::Error::new(
std::io::ErrorKind::NotFound,
format!("config profile `{key}` not found"),
)
})?
.clone(),
None => ConfigProfile::default(),
};
let user_instructions =
Config::load_instructions_with_filesystem(Some(&codex_home), filesystem).await?;
let model_instructions_path = config_profile
.model_instructions_file
.as_ref()
.or(config_toml.model_instructions_file.as_ref());
let file_base_instructions = Config::try_read_non_empty_file_with_filesystem(
model_instructions_path,
"model instructions file",
filesystem,
)
.await?;
let experimental_compact_prompt_path = config_profile
.experimental_compact_prompt_file
.as_ref()
.or(config_toml.experimental_compact_prompt_file.as_ref());
let file_compact_prompt = Config::try_read_non_empty_file_with_filesystem(
experimental_compact_prompt_path,
"experimental compact prompt file",
filesystem,
)
.await?;
let active_project = config_toml
.get_active_project_with_filesystem(cwd.as_path(), filesystem)
.await?
.unwrap_or(ProjectConfig { trust_level: None });
let mut config = Config::load_config_with_layer_stack_internal(
config_toml,
harness_overrides,
codex_home,
config_layer_stack,
Some(active_project),
/*create_memories_root*/ false,
Some(user_instructions),
Some(file_base_instructions),
Some(file_compact_prompt),
)?;
let memories_root = AbsolutePathBuf::from_absolute_path(&memory_root(&config.codex_home))?;
filesystem
.create_directory(
&memories_root,
codex_exec_server::CreateDirectoryOptions { recursive: true },
)
.await?;
config.agent_roles = agent_roles::merge_loaded_agent_roles(
&config.agent_roles,
agent_roles::load_agent_roles_with_filesystem(
&config.config_layer_stack,
&mut config.startup_warnings,
filesystem,
)
.await?,
);
Ok(config)
}
}
fn config_scope_segments(scope: &[String], key: &str) -> Vec<String> {
@@ -767,6 +906,78 @@ async fn maybe_migrate_smart_approvals_alias(codex_home: &Path) -> std::io::Resu
return Ok(false);
};
let edits = smart_approvals_alias_migration_edits(&config_toml);
if edits.is_empty() {
return Ok(false);
}
ConfigEditsBuilder::new(codex_home)
.with_edits(edits)
.apply()
.await
.map_err(|err| {
std::io::Error::other(format!("failed to migrate guardian_approval alias: {err}"))
})?;
Ok(true)
}
async fn maybe_migrate_smart_approvals_alias_with_filesystem<F>(
codex_home: &Path,
filesystem: &F,
) -> std::io::Result<bool>
where
F: ExecutorFileSystem + ?Sized,
{
let config_path = AbsolutePathBuf::resolve_path_against_base(CONFIG_TOML_FILE, codex_home)?;
let config_contents = match filesystem.read_file(&config_path).await {
Ok(bytes) => match String::from_utf8(bytes) {
Ok(contents) => contents,
Err(_) => return Ok(false),
},
Err(err) if err.kind() == ErrorKind::NotFound => return Ok(false),
Err(err) => return Err(err),
};
let Ok(config_toml) = toml::from_str::<ConfigToml>(&config_contents) else {
return Ok(false);
};
let edits = smart_approvals_alias_migration_edits(&config_toml);
if edits.is_empty() {
return Ok(false);
}
let Some(updated) =
apply_to_serialized(&config_contents, /*profile*/ None, &edits).map_err(|err| {
std::io::Error::other(format!("failed to migrate guardian_approval alias: {err}"))
})?
else {
return Ok(false);
};
let parent = config_path.as_path().parent().ok_or_else(|| {
std::io::Error::new(
std::io::ErrorKind::InvalidInput,
format!(
"config path {} has no parent directory",
config_path.as_path().display()
),
)
})?;
let parent = AbsolutePathBuf::from_absolute_path(parent)?;
filesystem
.create_directory(
&parent,
codex_exec_server::CreateDirectoryOptions { recursive: true },
)
.await?;
filesystem
.write_file(&config_path, updated.into_bytes())
.await?;
Ok(true)
}
fn smart_approvals_alias_migration_edits(config_toml: &ConfigToml) -> Vec<ConfigEdit> {
let mut edits = Vec::new();
let root_scope = Vec::new();
@@ -791,18 +1002,7 @@ async fn maybe_migrate_smart_approvals_alias(codex_home: &Path) -> std::io::Resu
}
}
if edits.is_empty() {
return Ok(false);
}
ConfigEditsBuilder::new(codex_home)
.with_edits(edits)
.apply()
.await
.map_err(|err| {
std::io::Error::other(format!("failed to migrate guardian_approval alias: {err}"))
})?;
Ok(true)
edits
}
impl Config {
@@ -1699,6 +1899,10 @@ pub struct AgentRoleConfig {
pub description: Option<String>,
/// Path to a role-specific config layer.
pub config_file: Option<PathBuf>,
/// If present, the role locks the model to this value.
pub locked_model: Option<String>,
/// If present, the role locks the reasoning effort to this value.
pub locked_reasoning_effort: Option<String>,
/// Candidate nicknames for agents spawned with this role.
pub nickname_candidates: Option<Vec<String>>,
}
@@ -1750,6 +1954,24 @@ impl ConfigToml {
windows_sandbox_level: WindowsSandboxLevel,
resolved_cwd: &Path,
sandbox_policy_constraint: Option<&Constrained<SandboxPolicy>>,
) -> SandboxPolicy {
let active_project = self.get_active_project(resolved_cwd);
self.derive_sandbox_policy_for_active_project(
sandbox_mode_override,
profile_sandbox_mode,
windows_sandbox_level,
active_project.as_ref(),
sandbox_policy_constraint,
)
}
fn derive_sandbox_policy_for_active_project(
&self,
sandbox_mode_override: Option<SandboxMode>,
profile_sandbox_mode: Option<SandboxMode>,
windows_sandbox_level: WindowsSandboxLevel,
active_project: Option<&ProjectConfig>,
sandbox_policy_constraint: Option<&Constrained<SandboxPolicy>>,
) -> SandboxPolicy {
let sandbox_mode_was_explicit = sandbox_mode_override.is_some()
|| profile_sandbox_mode.is_some()
@@ -1761,7 +1983,7 @@ impl ConfigToml {
// If no sandbox_mode is set but this directory has a trust decision,
// default to workspace-write except on unsandboxed Windows where we
// default to read-only.
self.get_active_project(resolved_cwd).and_then(|p| {
active_project.and_then(|p| {
if p.is_trusted() || p.is_untrusted() {
if cfg!(target_os = "windows")
&& windows_sandbox_level
@@ -1823,6 +2045,32 @@ impl ConfigToml {
sandbox_policy
}
async fn get_active_project_with_filesystem<F>(
&self,
resolved_cwd: &Path,
filesystem: &F,
) -> std::io::Result<Option<ProjectConfig>>
where
F: ExecutorFileSystem + ?Sized,
{
let projects = self.projects.clone().unwrap_or_default();
if let Some(project_config) = projects.get(&resolved_cwd.to_string_lossy().to_string()) {
return Ok(Some(project_config.clone()));
}
let resolved_cwd = AbsolutePathBuf::from_absolute_path(resolved_cwd)?;
if let Some(repo_root) =
resolve_root_git_project_for_trust_with_filesystem(&resolved_cwd, filesystem).await?
&& let Some(project_config_for_root) =
projects.get(&repo_root.to_string_lossy().to_string())
{
return Ok(Some(project_config_for_root.clone()));
}
Ok(None)
}
/// Resolves the cwd to an existing project, or returns None if ConfigToml
/// does not contain a project corresponding to cwd or a git repo for cwd
pub fn get_active_project(&self, resolved_cwd: &Path) -> Option<ProjectConfig> {
@@ -2120,6 +2368,30 @@ impl Config {
overrides: ConfigOverrides,
codex_home: PathBuf,
config_layer_stack: ConfigLayerStack,
) -> std::io::Result<Self> {
Self::load_config_with_layer_stack_internal(
cfg,
overrides,
codex_home,
config_layer_stack,
/*active_project_override*/ None,
/*create_memories_root*/ true,
/*user_instructions_override*/ None,
/*file_base_instructions_override*/ None,
/*file_compact_prompt_override*/ None,
)
}
pub(crate) fn load_config_with_layer_stack_internal(
cfg: ConfigToml,
overrides: ConfigOverrides,
codex_home: PathBuf,
config_layer_stack: ConfigLayerStack,
active_project_override: Option<ProjectConfig>,
create_memories_root: bool,
user_instructions_override: Option<Option<String>>,
file_base_instructions_override: Option<Option<String>>,
file_compact_prompt_override: Option<Option<String>>,
) -> std::io::Result<Self> {
validate_reserved_model_provider_ids(&cfg.model_providers)
.map_err(|message| std::io::Error::new(std::io::ErrorKind::InvalidInput, message))?;
@@ -2136,7 +2408,8 @@ impl Config {
network: network_requirements,
} = config_layer_stack.requirements().clone();
let user_instructions = Self::load_instructions(Some(&codex_home));
let user_instructions = user_instructions_override
.unwrap_or_else(|| Self::load_instructions(Some(&codex_home)));
let mut startup_warnings = Vec::new();
// Destructure ConfigOverrides fully to ensure all overrides are applied.
@@ -2216,8 +2489,8 @@ impl Config {
.into_iter()
.map(|path| AbsolutePathBuf::resolve_path_against_base(path, &resolved_cwd))
.collect::<Result<Vec<_>, _>>()?;
let active_project = cfg
.get_active_project(&resolved_cwd)
let active_project = active_project_override
.or_else(|| cfg.get_active_project(&resolved_cwd))
.unwrap_or(ProjectConfig { trust_level: None });
let permission_config_syntax = resolve_permission_config_syntax(
&config_layer_stack,
@@ -2248,7 +2521,9 @@ impl Config {
None => WindowsSandboxLevel::from_features(&features),
};
let memories_root = memory_root(&codex_home);
std::fs::create_dir_all(&memories_root)?;
if create_memories_root {
std::fs::create_dir_all(&memories_root)?;
}
let memories_root = AbsolutePathBuf::from_absolute_path(&memories_root)?;
if !additional_writable_roots
.iter()
@@ -2307,11 +2582,11 @@ impl Config {
)
} else {
let configured_network_proxy_config = NetworkProxyConfig::default();
let mut sandbox_policy = cfg.derive_sandbox_policy(
let mut sandbox_policy = cfg.derive_sandbox_policy_for_active_project(
sandbox_mode,
config_profile.sandbox_mode,
windows_sandbox_level,
&resolved_cwd,
Some(&active_project),
Some(&constrained_sandbox_policy),
);
if let SandboxPolicy::WorkspaceWrite { writable_roots, .. } = &mut sandbox_policy {
@@ -2529,8 +2804,12 @@ impl Config {
.model_instructions_file
.as_ref()
.or(cfg.model_instructions_file.as_ref());
let file_base_instructions =
Self::try_read_non_empty_file(model_instructions_path, "model instructions file")?;
let file_base_instructions = match file_base_instructions_override {
Some(file_base_instructions) => file_base_instructions,
None => {
Self::try_read_non_empty_file(model_instructions_path, "model instructions file")?
}
};
let base_instructions = base_instructions.or(file_base_instructions);
let developer_instructions = developer_instructions.or(cfg.developer_instructions);
let guardian_developer_instructions = guardian_developer_instructions_from_requirements(
@@ -2549,10 +2828,13 @@ impl Config {
.experimental_compact_prompt_file
.as_ref()
.or(cfg.experimental_compact_prompt_file.as_ref());
let file_compact_prompt = Self::try_read_non_empty_file(
experimental_compact_prompt_path,
"experimental compact prompt file",
)?;
let file_compact_prompt = match file_compact_prompt_override {
Some(file_compact_prompt) => file_compact_prompt,
None => Self::try_read_non_empty_file(
experimental_compact_prompt_path,
"experimental compact prompt file",
)?,
};
let compact_prompt = compact_prompt.or(file_compact_prompt);
let js_repl_node_path = js_repl_node_path_override
.or(config_profile.js_repl_node_path.map(Into::into))
@@ -2866,6 +3148,39 @@ impl Config {
None
}
async fn load_instructions_with_filesystem<F>(
codex_dir: Option<&Path>,
filesystem: &F,
) -> std::io::Result<Option<String>>
where
F: ExecutorFileSystem + ?Sized,
{
let Some(base) = codex_dir else {
return Ok(None);
};
for candidate in [LOCAL_PROJECT_DOC_FILENAME, DEFAULT_PROJECT_DOC_FILENAME] {
let path = AbsolutePathBuf::from_absolute_path(&base.join(candidate))?;
let contents = match filesystem.read_file(&path).await {
Ok(contents) => contents,
Err(err) if err.kind() == std::io::ErrorKind::NotFound => continue,
Err(err) => return Err(err),
};
let contents = String::from_utf8(contents).map_err(|err| {
std::io::Error::new(
std::io::ErrorKind::InvalidData,
format!("failed to decode {} as UTF-8: {err}", path.display()),
)
})?;
let trimmed = contents.trim();
if !trimmed.is_empty() {
return Ok(Some(trimmed.to_string()));
}
}
Ok(None)
}
/// If `path` is `Some`, attempts to read the file at the given path and
/// returns its contents as a trimmed `String`. If the file is empty, or
/// is `Some` but cannot be read, returns an `Err`.
@@ -2895,6 +3210,45 @@ impl Config {
}
}
async fn try_read_non_empty_file_with_filesystem<F>(
path: Option<&AbsolutePathBuf>,
context: &str,
filesystem: &F,
) -> std::io::Result<Option<String>>
where
F: ExecutorFileSystem + ?Sized,
{
let Some(path) = path else {
return Ok(None);
};
let contents = filesystem.read_file(path).await.map_err(|e| {
std::io::Error::new(
e.kind(),
format!("failed to read {context} {}: {e}", path.display()),
)
})?;
let contents = String::from_utf8(contents).map_err(|err| {
std::io::Error::new(
std::io::ErrorKind::InvalidData,
format!(
"failed to decode {context} {} as UTF-8: {err}",
path.display()
),
)
})?;
let s = contents.trim().to_string();
if s.is_empty() {
Err(std::io::Error::new(
std::io::ErrorKind::InvalidData,
format!("{context} is empty: {}", path.display()),
))
} else {
Ok(Some(s))
}
}
pub fn set_windows_sandbox_enabled(&mut self, value: bool) {
self.permissions.windows_sandbox_mode = if value {
Some(WindowsSandboxModeToml::Unelevated)
+453
View File
@@ -2,6 +2,7 @@ use super::ConfigToml;
use super::deserialize_config_toml_with_base;
use crate::config::edit::ConfigEdit;
use crate::config::edit::ConfigEditsBuilder;
use crate::config::edit::apply_to_serialized;
use crate::config::managed_features::validate_explicit_feature_settings_in_config_toml;
use crate::config::managed_features::validate_feature_requirements_in_config_toml;
use crate::config_loader::CloudRequirementsLoader;
@@ -11,6 +12,7 @@ use crate::config_loader::ConfigLayerStackOrdering;
use crate::config_loader::ConfigRequirementsToml;
use crate::config_loader::LoaderOverrides;
use crate::config_loader::load_config_layers_state;
use crate::config_loader::load_config_layers_state_with_filesystem;
use crate::config_loader::merge_toml_values;
use crate::path_utils;
use crate::path_utils::SymlinkWritePaths;
@@ -29,6 +31,8 @@ use codex_app_server_protocol::MergeStrategy;
use codex_app_server_protocol::OverriddenMetadata;
use codex_app_server_protocol::WriteStatus;
use codex_config::CONFIG_TOML_FILE;
use codex_exec_server::Environment;
use codex_exec_server::ExecutorFileSystem;
use codex_utils_absolute_path::AbsolutePathBuf;
use serde_json::Value as JsonValue;
use std::borrow::Cow;
@@ -179,6 +183,68 @@ impl ConfigService {
.map_err(|err| ConfigServiceError::json("failed to deserialize configuration", err))?;
Ok(ConfigReadResponse {
environment_id: Environment::default_environment_id(None),
config,
origins: layers.origins(),
layers: params.include_layers.then(|| {
layers
.get_layers(
ConfigLayerStackOrdering::HighestPrecedenceFirst,
/*include_disabled*/ true,
)
.iter()
.map(|layer| layer.as_layer())
.collect()
}),
})
}
pub async fn read_with_filesystem<F>(
&self,
params: ConfigReadParams,
environment_id: &str,
filesystem: &F,
) -> Result<ConfigReadResponse, ConfigServiceError>
where
F: ExecutorFileSystem + ?Sized,
{
let layers = match params.cwd.as_deref() {
Some(cwd) => {
let cwd = AbsolutePathBuf::try_from(PathBuf::from(cwd)).map_err(|err| {
ConfigServiceError::io("failed to resolve config cwd to an absolute path", err)
})?;
load_config_layers_state_with_filesystem(
&self.codex_home,
Some(cwd),
&self.cli_overrides,
self.loader_overrides.clone(),
self.cloud_requirements.clone(),
filesystem,
)
.await
.map_err(|err| ConfigServiceError::io("failed to read configuration layers", err))?
}
None => self
.load_thread_agnostic_config_with_filesystem(filesystem)
.await
.map_err(|err| {
ConfigServiceError::io("failed to read configuration layers", err)
})?,
};
let effective = layers.effective_config();
let effective_config_toml: ConfigToml = effective
.try_into()
.map_err(|err| ConfigServiceError::toml("invalid configuration", err))?;
let json_value = serde_json::to_value(&effective_config_toml)
.map_err(|err| ConfigServiceError::json("failed to serialize configuration", err))?;
let config: ApiConfig = serde_json::from_value(json_value)
.map_err(|err| ConfigServiceError::json("failed to deserialize configuration", err))?;
Ok(ConfigReadResponse {
environment_id: environment_id.to_string(),
config,
origins: layers.origins(),
layers: params.include_layers.then(|| {
@@ -210,6 +276,26 @@ impl ConfigService {
}
}
pub async fn read_requirements_with_filesystem<F>(
&self,
filesystem: &F,
) -> Result<Option<ConfigRequirementsToml>, ConfigServiceError>
where
F: ExecutorFileSystem + ?Sized,
{
let layers = self
.load_thread_agnostic_config_with_filesystem(filesystem)
.await
.map_err(|err| ConfigServiceError::io("failed to read configuration layers", err))?;
let requirements = layers.requirements_toml().clone();
if requirements.is_empty() {
Ok(None)
} else {
Ok(Some(requirements))
}
}
pub async fn write_value(
&self,
params: ConfigValueWriteParams,
@@ -233,6 +319,47 @@ impl ConfigService {
.await
}
pub async fn write_value_with_filesystem<F>(
&self,
params: ConfigValueWriteParams,
filesystem: &F,
) -> Result<ConfigWriteResponse, ConfigServiceError>
where
F: ExecutorFileSystem + ?Sized,
{
let edits = vec![(params.key_path, params.value, params.merge_strategy)];
self.apply_edits_with_filesystem(
params.file_path,
params.expected_version,
edits,
filesystem,
)
.await
}
pub async fn batch_write_with_filesystem<F>(
&self,
params: ConfigBatchWriteParams,
filesystem: &F,
) -> Result<ConfigWriteResponse, ConfigServiceError>
where
F: ExecutorFileSystem + ?Sized,
{
let edits = params
.edits
.into_iter()
.map(|edit| (edit.key_path, edit.value, edit.merge_strategy))
.collect();
self.apply_edits_with_filesystem(
params.file_path,
params.expected_version,
edits,
filesystem,
)
.await
}
pub async fn load_user_saved_config(
&self,
) -> Result<codex_app_server_protocol::UserSavedConfig, ConfigServiceError> {
@@ -248,6 +375,25 @@ impl ConfigService {
Ok(cfg.into())
}
pub async fn load_user_saved_config_with_filesystem<F>(
&self,
filesystem: &F,
) -> Result<codex_app_server_protocol::UserSavedConfig, ConfigServiceError>
where
F: ExecutorFileSystem + ?Sized,
{
let layers = self
.load_thread_agnostic_config_with_filesystem(filesystem)
.await
.map_err(|err| ConfigServiceError::io("failed to load configuration", err))?;
let toml_value = layers.effective_config();
let cfg: ConfigToml = toml_value
.try_into()
.map_err(|err| ConfigServiceError::toml("failed to parse config.toml", err))?;
Ok(cfg.into())
}
async fn apply_edits(
&self,
file_path: Option<String>,
@@ -409,6 +555,175 @@ impl ConfigService {
})
}
async fn apply_edits_with_filesystem<F>(
&self,
file_path: Option<String>,
expected_version: Option<String>,
edits: Vec<(String, JsonValue, MergeStrategy)>,
filesystem: &F,
) -> Result<ConfigWriteResponse, ConfigServiceError>
where
F: ExecutorFileSystem + ?Sized,
{
let allowed_path =
AbsolutePathBuf::resolve_path_against_base(CONFIG_TOML_FILE, &self.codex_home)
.map_err(|err| ConfigServiceError::io("failed to resolve user config path", err))?;
let provided_path = match file_path {
Some(path) => AbsolutePathBuf::from_absolute_path(PathBuf::from(path))
.map_err(|err| ConfigServiceError::io("failed to resolve user config path", err))?,
None => allowed_path.clone(),
};
if !paths_match(&allowed_path, &provided_path) {
return Err(ConfigServiceError::write(
ConfigWriteErrorCode::ConfigLayerReadonly,
"Only writes to the user config are allowed",
));
}
let layers = self
.load_thread_agnostic_config_with_filesystem(filesystem)
.await
.map_err(|err| ConfigServiceError::io("failed to load configuration", err))?;
let user_layer = match layers.get_user_layer() {
Some(layer) => Cow::Borrowed(layer),
None => Cow::Owned(
create_empty_user_layer_with_filesystem(&allowed_path, filesystem).await?,
),
};
if let Some(expected) = expected_version.as_deref()
&& expected != user_layer.version
{
return Err(ConfigServiceError::write(
ConfigWriteErrorCode::ConfigVersionConflict,
"Configuration was modified since last read. Fetch latest version and retry.",
));
}
let mut user_config = user_layer.config.clone();
let mut parsed_segments = Vec::new();
let mut config_edits = Vec::new();
for (key_path, value, strategy) in edits {
let segments = parse_key_path(&key_path).map_err(|message| {
ConfigServiceError::write(ConfigWriteErrorCode::ConfigValidationError, message)
})?;
let original_value = value_at_path(&user_config, &segments).cloned();
let parsed_value = parse_value(value).map_err(|message| {
ConfigServiceError::write(ConfigWriteErrorCode::ConfigValidationError, message)
})?;
apply_merge(&mut user_config, &segments, parsed_value.as_ref(), strategy).map_err(
|err| match err {
MergeError::PathNotFound => ConfigServiceError::write(
ConfigWriteErrorCode::ConfigPathNotFound,
"Path not found",
),
MergeError::Validation(message) => ConfigServiceError::write(
ConfigWriteErrorCode::ConfigValidationError,
message,
),
},
)?;
let updated_value = value_at_path(&user_config, &segments).cloned();
if original_value != updated_value {
let edit = match updated_value {
Some(value) => ConfigEdit::SetPath {
segments: segments.clone(),
value: toml_value_to_item(&value).map_err(|err| {
ConfigServiceError::anyhow("failed to build config edits", err)
})?,
},
None => ConfigEdit::ClearPath {
segments: segments.clone(),
},
};
config_edits.push(edit);
}
parsed_segments.push(segments);
}
validate_config(&user_config).map_err(|err| {
ConfigServiceError::write(
ConfigWriteErrorCode::ConfigValidationError,
format!("Invalid configuration: {err}"),
)
})?;
let user_config_toml =
deserialize_config_toml_with_base(user_config.clone(), &self.codex_home).map_err(
|err| {
ConfigServiceError::write(
ConfigWriteErrorCode::ConfigValidationError,
format!("Invalid configuration: {err}"),
)
},
)?;
validate_explicit_feature_settings_in_config_toml(
&user_config_toml,
layers.requirements().feature_requirements.as_ref(),
)
.map_err(|err| {
ConfigServiceError::write(
ConfigWriteErrorCode::ConfigValidationError,
format!("Invalid configuration: {err}"),
)
})?;
validate_feature_requirements_in_config_toml(
&user_config_toml,
layers.requirements().feature_requirements.as_ref(),
)
.map_err(|err| {
ConfigServiceError::write(
ConfigWriteErrorCode::ConfigValidationError,
format!("Invalid configuration: {err}"),
)
})?;
let updated_layers = layers.with_user_config(&provided_path, user_config.clone());
let effective = updated_layers.effective_config();
validate_config(&effective).map_err(|err| {
ConfigServiceError::write(
ConfigWriteErrorCode::ConfigValidationError,
format!("Invalid configuration: {err}"),
)
})?;
if !config_edits.is_empty() {
write_config_with_filesystem(
&provided_path,
user_layer.raw_toml.as_deref().unwrap_or_default(),
&config_edits,
filesystem,
)
.await?;
}
let overridden = first_overridden_edit(&updated_layers, &effective, &parsed_segments);
let status = overridden
.as_ref()
.map(|_| WriteStatus::OkOverridden)
.unwrap_or(WriteStatus::Ok);
Ok(ConfigWriteResponse {
status,
version: updated_layers
.get_user_layer()
.ok_or_else(|| {
ConfigServiceError::write(
ConfigWriteErrorCode::UserLayerNotFound,
"user layer not found in updated layers",
)
})?
.version
.clone(),
file_path: provided_path,
overridden_metadata: overridden,
})
}
/// Loads a "thread-agnostic" config, which means the config layers do not
/// include any in-repo .codex/ folders because there is no cwd/project root
/// associated with this query.
@@ -423,6 +738,25 @@ impl ConfigService {
)
.await
}
async fn load_thread_agnostic_config_with_filesystem<F>(
&self,
filesystem: &F,
) -> std::io::Result<ConfigLayerStack>
where
F: ExecutorFileSystem + ?Sized,
{
let cwd: Option<AbsolutePathBuf> = None;
load_config_layers_state_with_filesystem(
&self.codex_home,
cwd,
&self.cli_overrides,
self.loader_overrides.clone(),
self.cloud_requirements.clone(),
filesystem,
)
.await
}
}
async fn create_empty_user_layer(
@@ -462,6 +796,49 @@ async fn create_empty_user_layer(
))
}
async fn create_empty_user_layer_with_filesystem<F>(
config_toml: &AbsolutePathBuf,
filesystem: &F,
) -> Result<ConfigLayerEntry, ConfigServiceError>
where
F: ExecutorFileSystem + ?Sized,
{
match filesystem.read_file(config_toml).await {
Ok(contents) => {
let toml_value = toml::from_str(&String::from_utf8(contents).map_err(|err| {
ConfigServiceError::io(
"failed to decode existing user config.toml",
std::io::Error::new(std::io::ErrorKind::InvalidData, err),
)
})?)
.map_err(|e| {
ConfigServiceError::toml("failed to parse existing user config.toml", e)
})?;
return Ok(ConfigLayerEntry::new(
ConfigLayerSource::User {
file: config_toml.clone(),
},
toml_value,
));
}
Err(err) if err.kind() == std::io::ErrorKind::NotFound => {}
Err(err) => {
return Err(ConfigServiceError::io(
"failed to read user config.toml",
err,
));
}
}
write_empty_user_config_with_filesystem(config_toml, filesystem).await?;
Ok(ConfigLayerEntry::new(
ConfigLayerSource::User {
file: config_toml.clone(),
},
TomlValue::Table(toml::map::Map::new()),
))
}
async fn write_empty_user_config(write_path: PathBuf) -> Result<(), ConfigServiceError> {
task::spawn_blocking(move || write_atomically(&write_path, ""))
.await
@@ -469,6 +846,82 @@ async fn write_empty_user_config(write_path: PathBuf) -> Result<(), ConfigServic
.map_err(|err| ConfigServiceError::io("failed to create empty user config.toml", err))
}
async fn write_empty_user_config_with_filesystem<F>(
write_path: &AbsolutePathBuf,
filesystem: &F,
) -> Result<(), ConfigServiceError>
where
F: ExecutorFileSystem + ?Sized,
{
let parent = write_path.as_path().parent().ok_or_else(|| {
ConfigServiceError::io(
"failed to create empty user config.toml",
std::io::Error::new(
std::io::ErrorKind::InvalidInput,
format!(
"path {} has no parent directory",
write_path.as_path().display()
),
),
)
})?;
let parent = AbsolutePathBuf::from_absolute_path(parent)
.map_err(|err| ConfigServiceError::io("failed to create empty user config.toml", err))?;
filesystem
.create_directory(
&parent,
codex_exec_server::CreateDirectoryOptions { recursive: true },
)
.await
.map_err(|err| ConfigServiceError::io("failed to create empty user config.toml", err))?;
filesystem
.write_file(write_path, Vec::new())
.await
.map_err(|err| ConfigServiceError::io("failed to create empty user config.toml", err))
}
async fn write_config_with_filesystem<F>(
config_path: &AbsolutePathBuf,
existing_contents: &str,
edits: &[ConfigEdit],
filesystem: &F,
) -> Result<(), ConfigServiceError>
where
F: ExecutorFileSystem + ?Sized,
{
let Some(updated) = apply_to_serialized(existing_contents, /*profile*/ None, edits)
.map_err(|err| ConfigServiceError::anyhow("failed to persist config.toml", err))?
else {
return Ok(());
};
let parent = config_path.as_path().parent().ok_or_else(|| {
ConfigServiceError::io(
"failed to persist config.toml",
std::io::Error::new(
std::io::ErrorKind::InvalidInput,
format!(
"path {} has no parent directory",
config_path.as_path().display()
),
),
)
})?;
let parent = AbsolutePathBuf::from_absolute_path(parent)
.map_err(|err| ConfigServiceError::io("failed to persist config.toml", err))?;
filesystem
.create_directory(
&parent,
codex_exec_server::CreateDirectoryOptions { recursive: true },
)
.await
.map_err(|err| ConfigServiceError::io("failed to persist config.toml", err))?;
filesystem
.write_file(config_path, updated.into_bytes())
.await
.map_err(|err| ConfigServiceError::io("failed to persist config.toml", err))
}
fn parse_value(value: JsonValue) -> Result<Option<TomlValue>, String> {
if value.is_null() {
return Ok(None);
+17
View File
@@ -77,6 +77,7 @@ unified_exec = true
let service = ConfigService::new_with_defaults(tmp.path().to_path_buf());
service
.write_value(ConfigValueWriteParams {
environment_id: None,
file_path: Some(tmp.path().join(CONFIG_TOML_FILE).display().to_string()),
key_path: "features.personality".to_string(),
value: serde_json::json!(true),
@@ -111,6 +112,7 @@ async fn write_value_supports_nested_app_paths() -> Result<()> {
let service = ConfigService::new_with_defaults(tmp.path().to_path_buf());
service
.write_value(ConfigValueWriteParams {
environment_id: None,
file_path: Some(tmp.path().join(CONFIG_TOML_FILE).display().to_string()),
key_path: "apps".to_string(),
value: serde_json::json!({
@@ -126,6 +128,7 @@ async fn write_value_supports_nested_app_paths() -> Result<()> {
service
.write_value(ConfigValueWriteParams {
environment_id: None,
file_path: Some(tmp.path().join(CONFIG_TOML_FILE).display().to_string()),
key_path: "apps.app1.default_tools_approval_mode".to_string(),
value: serde_json::json!("prompt"),
@@ -138,6 +141,7 @@ async fn write_value_supports_nested_app_paths() -> Result<()> {
let read = service
.read(ConfigReadParams {
include_layers: false,
environment_id: None,
cwd: None,
})
.await
@@ -190,6 +194,7 @@ async fn read_includes_origins_and_layers() {
let response = service
.read(ConfigReadParams {
include_layers: true,
environment_id: None,
cwd: None,
})
.await
@@ -264,6 +269,7 @@ async fn write_value_reports_override() {
let result = service
.write_value(ConfigValueWriteParams {
environment_id: None,
file_path: Some(tmp.path().join(CONFIG_TOML_FILE).display().to_string()),
key_path: "approval_policy".to_string(),
value: serde_json::json!("never"),
@@ -276,6 +282,7 @@ async fn write_value_reports_override() {
let read_after = service
.read(ConfigReadParams {
include_layers: true,
environment_id: None,
cwd: None,
})
.await
@@ -307,6 +314,7 @@ async fn version_conflict_rejected() {
let service = ConfigService::new_with_defaults(tmp.path().to_path_buf());
let error = service
.write_value(ConfigValueWriteParams {
environment_id: None,
file_path: Some(tmp.path().join(CONFIG_TOML_FILE).display().to_string()),
key_path: "model".to_string(),
value: serde_json::json!("gpt-5"),
@@ -330,6 +338,7 @@ async fn write_value_defaults_to_user_config_path() {
let service = ConfigService::new_with_defaults(tmp.path().to_path_buf());
service
.write_value(ConfigValueWriteParams {
environment_id: None,
file_path: None,
key_path: "model".to_string(),
value: serde_json::json!("gpt-new"),
@@ -368,6 +377,7 @@ async fn invalid_user_value_rejected_even_if_overridden_by_managed() {
let error = service
.write_value(ConfigValueWriteParams {
environment_id: None,
file_path: Some(tmp.path().join(CONFIG_TOML_FILE).display().to_string()),
key_path: "approval_policy".to_string(),
value: serde_json::json!("bogus"),
@@ -394,6 +404,7 @@ async fn reserved_builtin_provider_override_rejected() {
let service = ConfigService::new_with_defaults(tmp.path().to_path_buf());
let error = service
.write_value(ConfigValueWriteParams {
environment_id: None,
file_path: Some(tmp.path().join(CONFIG_TOML_FILE).display().to_string()),
key_path: "model_providers.openai.name".to_string(),
value: serde_json::json!("OpenAI Override"),
@@ -440,6 +451,7 @@ async fn write_value_rejects_feature_requirement_conflict() {
let error = service
.write_value(ConfigValueWriteParams {
environment_id: None,
file_path: Some(tmp.path().join(CONFIG_TOML_FILE).display().to_string()),
key_path: "features.personality".to_string(),
value: serde_json::json!(false),
@@ -491,6 +503,7 @@ async fn write_value_rejects_profile_feature_requirement_conflict() {
let error = service
.write_value(ConfigValueWriteParams {
environment_id: None,
file_path: Some(tmp.path().join(CONFIG_TOML_FILE).display().to_string()),
key_path: "profiles.enterprise.features.personality".to_string(),
value: serde_json::json!(false),
@@ -547,6 +560,7 @@ async fn read_reports_managed_overrides_user_and_session_flags() {
let response = service
.read(ConfigReadParams {
include_layers: true,
environment_id: None,
cwd: None,
})
.await
@@ -604,6 +618,7 @@ async fn write_value_reports_managed_override() {
let result = service
.write_value(ConfigValueWriteParams {
environment_id: None,
file_path: Some(tmp.path().join(CONFIG_TOML_FILE).display().to_string()),
key_path: "approval_policy".to_string(),
value: serde_json::json!("on-request"),
@@ -653,6 +668,7 @@ alpha = "a"
let service = ConfigService::new_with_defaults(tmp.path().to_path_buf());
service
.write_value(ConfigValueWriteParams {
environment_id: None,
file_path: Some(path.display().to_string()),
key_path: "mcp_servers.linear".to_string(),
value: overlay.clone(),
@@ -683,6 +699,7 @@ beta = "b"
service
.write_value(ConfigValueWriteParams {
environment_id: None,
file_path: Some(path.display().to_string()),
key_path: "mcp_servers.linear".to_string(),
value: overlay,
+100
View File
@@ -5,6 +5,7 @@ use super::macos::ManagedAdminConfigLayer;
use super::macos::load_managed_admin_config_layer;
use codex_config::config_error_from_toml;
use codex_config::io_error_from_config_error;
use codex_exec_server::ExecutorFileSystem;
use codex_utils_absolute_path::AbsolutePathBuf;
use std::io;
use std::path::Path;
@@ -79,6 +80,57 @@ pub(super) async fn load_config_layers_internal(
})
}
pub(super) async fn load_config_layers_internal_with_filesystem<F>(
codex_home: &Path,
overrides: LoaderOverrides,
filesystem: &F,
) -> io::Result<LoadedConfigLayers>
where
F: ExecutorFileSystem + ?Sized,
{
#[cfg(target_os = "macos")]
let LoaderOverrides {
managed_config_path,
managed_preferences_base64,
..
} = overrides;
#[cfg(not(target_os = "macos"))]
let LoaderOverrides {
managed_config_path,
..
} = overrides;
let managed_config_path = AbsolutePathBuf::from_absolute_path(
managed_config_path.unwrap_or_else(|| managed_config_default_path(codex_home)),
)?;
let managed_config = read_config_from_path_with_filesystem(
&managed_config_path,
/*log_missing_as_info*/ false,
filesystem,
)
.await?
.map(|managed_config| MangedConfigFromFile {
managed_config,
file: managed_config_path.clone(),
});
#[cfg(target_os = "macos")]
let managed_preferences =
load_managed_admin_config_layer(managed_preferences_base64.as_deref())
.await?
.map(map_managed_admin_layer);
#[cfg(not(target_os = "macos"))]
let managed_preferences = None;
Ok(LoadedConfigLayers {
managed_config,
managed_config_from_mdm: managed_preferences,
})
}
#[cfg(target_os = "macos")]
fn map_managed_admin_layer(layer: ManagedAdminConfigLayer) -> ManagedConfigFromMdm {
let ManagedAdminConfigLayer { config, raw_toml } = layer;
@@ -120,6 +172,54 @@ pub(super) async fn read_config_from_path(
}
}
pub(super) async fn read_config_from_path_with_filesystem<F>(
path: &AbsolutePathBuf,
log_missing_as_info: bool,
filesystem: &F,
) -> io::Result<Option<TomlValue>>
where
F: ExecutorFileSystem + ?Sized,
{
match filesystem.read_file(path).await {
Ok(contents) => {
let contents = String::from_utf8(contents).map_err(|err| {
io::Error::new(
io::ErrorKind::InvalidData,
format!(
"Failed to decode {} as UTF-8: {err}",
path.as_path().display()
),
)
})?;
match toml::from_str::<TomlValue>(&contents) {
Ok(value) => Ok(Some(value)),
Err(err) => {
tracing::error!("Failed to parse {}: {err}", path.as_path().display());
let config_error =
config_error_from_toml(path.as_path(), &contents, err.clone());
Err(io_error_from_config_error(
io::ErrorKind::InvalidData,
config_error,
Some(err),
))
}
}
}
Err(err) if err.kind() == io::ErrorKind::NotFound => {
if log_missing_as_info {
tracing::info!("{} not found, using defaults", path.as_path().display());
} else {
tracing::debug!("{} not found", path.as_path().display());
}
Ok(None)
}
Err(err) => {
tracing::error!("Failed to read {}: {err}", path.as_path().display());
Err(err)
}
}
}
/// Return the default managed config path.
pub(super) fn managed_config_default_path(codex_home: &Path) -> PathBuf {
#[cfg(unix)]
+554 -1
View File
@@ -7,10 +7,14 @@ mod tests;
use crate::config::ConfigToml;
use crate::config_loader::layer_io::LoadedConfigLayers;
use crate::config_loader::layer_io::load_config_layers_internal_with_filesystem;
use crate::config_loader::layer_io::read_config_from_path_with_filesystem;
use crate::git_info::resolve_root_git_project_for_trust;
use crate::util::resolve_path;
use codex_app_server_protocol::ConfigLayerSource;
use codex_config::CONFIG_TOML_FILE;
use codex_config::ConfigRequirementsWithSources;
use codex_exec_server::ExecutorFileSystem;
use codex_protocol::config_types::SandboxMode;
use codex_protocol::config_types::TrustLevel;
use codex_protocol::protocol::AskForApproval;
@@ -18,9 +22,9 @@ use codex_utils_absolute_path::AbsolutePathBuf;
use codex_utils_absolute_path::AbsolutePathBufGuard;
use dunce::canonicalize as normalize_path;
use serde::Deserialize;
use std::ffi::OsStr;
use std::io;
use std::path::Path;
#[cfg(windows)]
use std::path::PathBuf;
use toml::Value as TomlValue;
@@ -299,6 +303,199 @@ pub async fn load_config_layers_state(
)
}
pub async fn load_config_layers_state_with_filesystem<F>(
codex_home: &Path,
cwd: Option<AbsolutePathBuf>,
cli_overrides: &[(String, TomlValue)],
overrides: LoaderOverrides,
cloud_requirements: CloudRequirementsLoader,
filesystem: &F,
) -> io::Result<ConfigLayerStack>
where
F: ExecutorFileSystem + ?Sized,
{
let mut config_requirements_toml = ConfigRequirementsWithSources::default();
if let Some(requirements) = cloud_requirements.get().await.map_err(io::Error::other)? {
config_requirements_toml
.merge_unset_fields(RequirementSource::CloudRequirements, requirements);
}
#[cfg(target_os = "macos")]
macos::load_managed_admin_requirements_toml(
&mut config_requirements_toml,
overrides
.macos_managed_config_requirements_base64
.as_deref(),
)
.await?;
let requirements_toml_file = system_requirements_toml_file()?;
load_requirements_toml_with_filesystem(
&mut config_requirements_toml,
requirements_toml_file,
filesystem,
)
.await?;
let loaded_config_layers =
load_config_layers_internal_with_filesystem(codex_home, overrides, filesystem).await?;
load_requirements_from_legacy_scheme(
&mut config_requirements_toml,
loaded_config_layers.clone(),
)
.await?;
let mut layers = Vec::<ConfigLayerEntry>::new();
let cli_overrides_layer = if cli_overrides.is_empty() {
None
} else {
let cli_overrides_layer = build_cli_overrides_layer(cli_overrides);
let base_dir = cwd
.as_ref()
.map(AbsolutePathBuf::as_path)
.unwrap_or(codex_home);
Some(resolve_relative_paths_in_config_toml(
cli_overrides_layer,
base_dir,
)?)
};
let system_config_toml_file = system_config_toml_file()?;
let system_layer = load_config_toml_for_required_layer_with_filesystem(
&system_config_toml_file,
filesystem,
|config_toml| {
ConfigLayerEntry::new(
ConfigLayerSource::System {
file: system_config_toml_file.clone(),
},
config_toml,
)
},
)
.await?;
layers.push(system_layer);
let user_file = AbsolutePathBuf::resolve_path_against_base(CONFIG_TOML_FILE, codex_home)?;
let user_layer = load_config_toml_for_required_layer_with_filesystem(
&user_file,
filesystem,
|config_toml| {
ConfigLayerEntry::new(
ConfigLayerSource::User {
file: user_file.clone(),
},
config_toml,
)
},
)
.await?;
layers.push(user_layer);
if let Some(cwd) = cwd {
let mut merged_so_far = TomlValue::Table(toml::map::Map::new());
for layer in &layers {
merge_toml_values(&mut merged_so_far, &layer.config);
}
if let Some(cli_overrides_layer) = cli_overrides_layer.as_ref() {
merge_toml_values(&mut merged_so_far, cli_overrides_layer);
}
let project_root_markers = match project_root_markers_from_config(&merged_so_far) {
Ok(markers) => markers.unwrap_or_else(default_project_root_markers),
Err(err) => {
if let Some(config_error) = first_layer_config_error_from_entries(&layers).await {
return Err(io_error_from_config_error(
io::ErrorKind::InvalidData,
config_error,
/*source*/ None,
));
}
return Err(err);
}
};
let project_trust_context = match project_trust_context_with_filesystem(
&merged_so_far,
&cwd,
&project_root_markers,
codex_home,
&user_file,
filesystem,
)
.await
{
Ok(context) => context,
Err(err) => {
let source = err
.get_ref()
.and_then(|err| err.downcast_ref::<toml::de::Error>())
.cloned();
if let Some(config_error) = first_layer_config_error_from_entries(&layers).await {
return Err(io_error_from_config_error(
io::ErrorKind::InvalidData,
config_error,
source,
));
}
return Err(err);
}
};
let project_layers = load_project_layers_with_filesystem(
&cwd,
&project_trust_context.project_root,
&project_trust_context,
codex_home,
filesystem,
)
.await?;
layers.extend(project_layers);
}
if let Some(cli_overrides_layer) = cli_overrides_layer {
layers.push(ConfigLayerEntry::new(
ConfigLayerSource::SessionFlags,
cli_overrides_layer,
));
}
let LoadedConfigLayers {
managed_config,
managed_config_from_mdm,
} = loaded_config_layers;
if let Some(config) = managed_config {
let managed_parent = config.file.as_path().parent().ok_or_else(|| {
io::Error::new(
io::ErrorKind::InvalidData,
format!(
"Managed config file {} has no parent directory",
config.file.as_path().display()
),
)
})?;
let managed_config =
resolve_relative_paths_in_config_toml(config.managed_config, managed_parent)?;
layers.push(ConfigLayerEntry::new(
ConfigLayerSource::LegacyManagedConfigTomlFromFile { file: config.file },
managed_config,
));
}
if let Some(config) = managed_config_from_mdm {
layers.push(ConfigLayerEntry::new_with_raw_toml(
ConfigLayerSource::LegacyManagedConfigTomlFromMdm,
config.managed_config,
config.raw_toml,
));
}
ConfigLayerStack::new(
layers,
config_requirements_toml.clone().try_into()?,
config_requirements_toml.into_toml(),
)
}
/// Attempts to load a config.toml file from `config_toml`.
/// - If the file exists and is valid TOML, passes the parsed `toml::Value` to
/// `create_entry` and returns the resulting layer entry.
@@ -343,6 +540,39 @@ async fn load_config_toml_for_required_layer(
Ok(create_entry(toml_value))
}
async fn load_config_toml_for_required_layer_with_filesystem<F>(
config_toml: &AbsolutePathBuf,
filesystem: &F,
create_entry: impl FnOnce(TomlValue) -> ConfigLayerEntry,
) -> io::Result<ConfigLayerEntry>
where
F: ExecutorFileSystem + ?Sized,
{
let toml_value = match read_config_from_path_with_filesystem(
config_toml,
/*log_missing_as_info*/ false,
filesystem,
)
.await?
{
Some(config) => {
let config_parent = config_toml.as_path().parent().ok_or_else(|| {
io::Error::new(
io::ErrorKind::InvalidData,
format!(
"Config file {} has no parent directory",
config_toml.as_path().display()
),
)
})?;
resolve_relative_paths_in_config_toml(config, config_parent)?
}
None => TomlValue::Table(toml::map::Map::new()),
};
Ok(create_entry(toml_value))
}
/// If available, apply requirements from the platform system
/// `requirements.toml` location to `config_requirements_toml` by filling in
/// any unset fields.
@@ -387,6 +617,59 @@ async fn load_requirements_toml(
Ok(())
}
async fn load_requirements_toml_with_filesystem<F>(
config_requirements_toml: &mut ConfigRequirementsWithSources,
requirements_toml_file: impl AsRef<Path>,
filesystem: &F,
) -> io::Result<()>
where
F: ExecutorFileSystem + ?Sized,
{
let requirements_toml_file =
AbsolutePathBuf::from_absolute_path(requirements_toml_file.as_ref())?;
match filesystem.read_file(&requirements_toml_file).await {
Ok(contents) => {
let contents = String::from_utf8(contents).map_err(|err| {
io::Error::new(
io::ErrorKind::InvalidData,
format!(
"Failed to decode requirements file {} as UTF-8: {err}",
requirements_toml_file.as_ref().display(),
),
)
})?;
let requirements_config: ConfigRequirementsToml =
toml::from_str(&contents).map_err(|e| {
io::Error::new(
io::ErrorKind::InvalidData,
format!(
"Error parsing requirements file {}: {e}",
requirements_toml_file.as_ref().display(),
),
)
})?;
config_requirements_toml.merge_unset_fields(
RequirementSource::SystemRequirementsToml {
file: requirements_toml_file.clone(),
},
requirements_config,
);
}
Err(err) if err.kind() == io::ErrorKind::NotFound => {}
Err(err) => {
return Err(io::Error::new(
err.kind(),
format!(
"Failed to read requirements file {}: {err}",
requirements_toml_file.as_ref().display(),
),
));
}
}
Ok(())
}
#[cfg(unix)]
fn system_requirements_toml_file() -> io::Result<AbsolutePathBuf> {
AbsolutePathBuf::from_absolute_path(Path::new("/etc/codex/requirements.toml"))
@@ -705,6 +988,136 @@ async fn project_trust_context(
})
}
async fn project_trust_context_with_filesystem<F>(
merged_config: &TomlValue,
cwd: &AbsolutePathBuf,
project_root_markers: &[String],
config_base_dir: &Path,
user_config_file: &AbsolutePathBuf,
filesystem: &F,
) -> io::Result<ProjectTrustContext>
where
F: ExecutorFileSystem + ?Sized,
{
let project_trust_config: ProjectTrustConfigToml = {
let _guard = AbsolutePathBufGuard::new(config_base_dir);
merged_config
.clone()
.try_into()
.map_err(|err| std::io::Error::new(std::io::ErrorKind::InvalidData, err))?
};
let project_root =
find_project_root_with_filesystem(cwd, project_root_markers, filesystem).await?;
let projects = project_trust_config.projects.unwrap_or_default();
let project_root_key = project_root.as_path().to_string_lossy().to_string();
let repo_root = resolve_root_git_project_for_trust_with_filesystem(cwd, filesystem).await?;
let repo_root_key = repo_root
.as_ref()
.map(|root| root.to_string_lossy().to_string());
let projects_trust = projects
.into_iter()
.filter_map(|(key, project)| project.trust_level.map(|trust_level| (key, trust_level)))
.collect();
Ok(ProjectTrustContext {
project_root,
project_root_key,
repo_root_key,
projects_trust,
user_config_file: user_config_file.clone(),
})
}
pub(crate) async fn resolve_root_git_project_for_trust_with_filesystem<F>(
cwd: &AbsolutePathBuf,
filesystem: &F,
) -> io::Result<Option<PathBuf>>
where
F: ExecutorFileSystem + ?Sized,
{
let base = match filesystem.get_metadata(cwd).await {
Ok(metadata) if metadata.is_directory => cwd.as_path(),
Ok(_) => match cwd.as_path().parent() {
Some(parent) => parent,
None => return Ok(None),
},
Err(err) if err.kind() == io::ErrorKind::NotFound => return Ok(None),
Err(err) => return Err(err),
};
let Some((repo_root, dot_git)) =
find_ancestor_git_entry_with_filesystem(base, filesystem).await?
else {
return Ok(None);
};
let dot_git_abs = AbsolutePathBuf::from_absolute_path(&dot_git)?;
let dot_git_metadata = filesystem.get_metadata(&dot_git_abs).await?;
if dot_git_metadata.is_directory {
return Ok(Some(repo_root));
}
let git_dir_s =
String::from_utf8(filesystem.read_file(&dot_git_abs).await?).map_err(|err| {
io::Error::new(
io::ErrorKind::InvalidData,
format!(
"Failed to decode git metadata file {}: {err}",
dot_git.display()
),
)
})?;
let Some(git_dir_rel) = git_dir_s.trim().strip_prefix("gitdir:").map(str::trim) else {
return Ok(None);
};
if git_dir_rel.is_empty() {
return Ok(None);
}
let git_dir_path = resolve_path(&repo_root, &PathBuf::from(git_dir_rel));
let Some(worktrees_dir) = git_dir_path.parent() else {
return Ok(None);
};
if worktrees_dir.file_name() != Some(OsStr::new("worktrees")) {
return Ok(None);
}
let Some(common_dir) = worktrees_dir.parent() else {
return Ok(None);
};
let Some(main_repo_root) = common_dir.parent() else {
return Ok(None);
};
Ok(Some(main_repo_root.to_path_buf()))
}
async fn find_ancestor_git_entry_with_filesystem<F>(
base_dir: &Path,
filesystem: &F,
) -> io::Result<Option<(PathBuf, PathBuf)>>
where
F: ExecutorFileSystem + ?Sized,
{
let mut dir = base_dir.to_path_buf();
loop {
let dot_git = dir.join(".git");
let dot_git_abs = AbsolutePathBuf::from_absolute_path(&dot_git)?;
match filesystem.get_metadata(&dot_git_abs).await {
Ok(_) => return Ok(Some((dir, dot_git))),
Err(err) if err.kind() == io::ErrorKind::NotFound => {}
Err(err) => return Err(err),
}
if !dir.pop() {
break;
}
}
Ok(None)
}
/// Takes a `toml::Value` parsed from a config.toml file and walks through it,
/// resolving any `AbsolutePathBuf` fields against `base_dir`, returning a new
/// `toml::Value` with the same shape but with paths resolved.
@@ -784,6 +1197,31 @@ async fn find_project_root(
Ok(cwd.clone())
}
async fn find_project_root_with_filesystem<F>(
cwd: &AbsolutePathBuf,
project_root_markers: &[String],
filesystem: &F,
) -> io::Result<AbsolutePathBuf>
where
F: ExecutorFileSystem + ?Sized,
{
if project_root_markers.is_empty() {
return Ok(cwd.clone());
}
for ancestor in cwd.as_path().ancestors() {
for marker in project_root_markers {
let marker_path = AbsolutePathBuf::from_absolute_path(&ancestor.join(marker))?;
match filesystem.get_metadata(&marker_path).await {
Ok(_) => return AbsolutePathBuf::from_absolute_path(ancestor),
Err(err) if err.kind() == io::ErrorKind::NotFound => continue,
Err(err) => return Err(err),
}
}
}
Ok(cwd.clone())
}
/// Return the appropriate list of layers (each with
/// [ConfigLayerSource::Project] as the source) between `cwd` and
/// `project_root`, inclusive. The list is ordered in _increasing_ precdence,
@@ -895,6 +1333,121 @@ async fn load_project_layers(
Ok(layers)
}
async fn load_project_layers_with_filesystem<F>(
cwd: &AbsolutePathBuf,
project_root: &AbsolutePathBuf,
trust_context: &ProjectTrustContext,
codex_home: &Path,
filesystem: &F,
) -> io::Result<Vec<ConfigLayerEntry>>
where
F: ExecutorFileSystem + ?Sized,
{
let codex_home_abs = AbsolutePathBuf::from_absolute_path(codex_home)?;
let codex_home_normalized =
normalize_path(codex_home_abs.as_path()).unwrap_or_else(|_| codex_home_abs.to_path_buf());
let mut dirs = cwd
.as_path()
.ancestors()
.scan(false, |done, a| {
if *done {
None
} else {
if a == project_root.as_path() {
*done = true;
}
Some(a)
}
})
.collect::<Vec<_>>();
dirs.reverse();
let mut layers = Vec::new();
for dir in dirs {
let dot_codex = AbsolutePathBuf::from_absolute_path(&dir.join(".codex"))?;
let dot_codex_metadata = match filesystem.get_metadata(&dot_codex).await {
Ok(metadata) => metadata,
Err(err) if err.kind() == io::ErrorKind::NotFound => continue,
Err(err) => return Err(err),
};
if !dot_codex_metadata.is_directory {
continue;
}
let layer_dir = AbsolutePathBuf::from_absolute_path(dir)?;
let decision = trust_context.decision_for_dir(&layer_dir);
let dot_codex_normalized =
normalize_path(dot_codex.as_path()).unwrap_or_else(|_| dot_codex.to_path_buf());
if dot_codex == codex_home_abs || dot_codex_normalized == codex_home_normalized {
continue;
}
let config_file = dot_codex.join(CONFIG_TOML_FILE)?;
match filesystem.read_file(&config_file).await {
Ok(contents) => {
let contents = String::from_utf8(contents).map_err(|err| {
io::Error::new(
io::ErrorKind::InvalidData,
format!(
"Failed to decode project config file {} as UTF-8: {err}",
config_file.as_path().display()
),
)
})?;
let config: TomlValue = match toml::from_str(&contents) {
Ok(config) => config,
Err(e) => {
if decision.is_trusted() {
let config_file_display = config_file.as_path().display();
return Err(io::Error::new(
io::ErrorKind::InvalidData,
format!(
"Error parsing project config file {config_file_display}: {e}"
),
));
}
layers.push(project_layer_entry(
trust_context,
&dot_codex,
&layer_dir,
TomlValue::Table(toml::map::Map::new()),
/*config_toml_exists*/ true,
));
continue;
}
};
let config = resolve_relative_paths_in_config_toml(config, dot_codex.as_path())?;
let entry = project_layer_entry(
trust_context,
&dot_codex,
&layer_dir,
config,
/*config_toml_exists*/ true,
);
layers.push(entry);
}
Err(err) => {
if err.kind() == io::ErrorKind::NotFound {
layers.push(project_layer_entry(
trust_context,
&dot_codex,
&layer_dir,
TomlValue::Table(toml::map::Map::new()),
/*config_toml_exists*/ false,
));
} else {
let config_file_display = config_file.as_path().display();
return Err(io::Error::new(
err.kind(),
format!("Failed to read project config file {config_file_display}: {err}"),
));
}
}
}
}
Ok(layers)
}
/// The legacy mechanism for specifying admin-enforced configuration is to read
/// from a file like `/etc/codex/managed_config.toml` that has the same
/// structure as `config.toml` where fields like `approval_policy` can specify
+46 -9
View File
@@ -16,6 +16,7 @@ pub use codex_app_server_protocol::AppInfo;
pub use codex_app_server_protocol::AppMetadata;
use codex_connectors::AllConnectorsCacheKey;
use codex_connectors::DirectoryListResponse;
use codex_exec_server::Environment;
use codex_protocol::protocol::SandboxPolicy;
use rmcp::model::ToolAnnotations;
use serde::Deserialize;
@@ -43,10 +44,11 @@ use crate::mcp_connection_manager::McpConnectionManager;
use crate::mcp_connection_manager::codex_apps_tools_cache_key;
use crate::plugins::AppConnectorId;
use crate::plugins::PluginsManager;
use crate::plugins::list_tool_suggest_discoverable_plugins;
use crate::plugins::list_tool_suggest_discoverable_plugins_with_filesystem;
use crate::token_data::TokenData;
use crate::tools::discoverable::DiscoverablePluginInfo;
use crate::tools::discoverable::DiscoverableTool;
use codex_exec_server::ExecutorFileSystem;
pub use codex_connectors::CONNECTORS_CACHE_TTL;
const CONNECTORS_READY_TIMEOUT_ON_EMPTY_TOOLS: Duration = Duration::from_secs(30);
@@ -103,14 +105,18 @@ pub async fn list_accessible_connectors_from_mcp_tools(
)
}
pub(crate) async fn list_tool_suggest_discoverable_tools_with_auth(
pub(crate) async fn list_tool_suggest_discoverable_tools_with_auth_with_filesystem<F>(
config: &Config,
auth: Option<&CodexAuth>,
accessible_connectors: &[AppInfo],
) -> anyhow::Result<Vec<DiscoverableTool>> {
filesystem: &F,
) -> anyhow::Result<Vec<DiscoverableTool>>
where
F: ExecutorFileSystem + ?Sized,
{
let directory_connectors =
list_directory_connectors_for_tool_suggest_with_auth(config, auth).await?;
let connector_ids = tool_suggest_connector_ids(config);
let connector_ids = tool_suggest_connector_ids_with_filesystem(config, filesystem).await?;
let discoverable_connectors = filter_tool_suggest_discoverable_connectors(
directory_connectors,
accessible_connectors,
@@ -118,10 +124,12 @@ pub(crate) async fn list_tool_suggest_discoverable_tools_with_auth(
)
.into_iter()
.map(DiscoverableTool::from);
let discoverable_plugins = list_tool_suggest_discoverable_plugins(config)?
.into_iter()
.map(DiscoverablePluginInfo::from)
.map(DiscoverableTool::from);
let discoverable_plugins =
list_tool_suggest_discoverable_plugins_with_filesystem(config, filesystem)
.await?
.into_iter()
.map(DiscoverablePluginInfo::from)
.map(DiscoverableTool::from);
Ok(discoverable_connectors
.chain(discoverable_plugins)
.collect())
@@ -179,7 +187,10 @@ pub async fn list_accessible_connectors_from_mcp_tools_with_options_and_status(
}
let cache_key = accessible_connectors_cache_key(config, auth.as_ref());
let mcp_manager = McpManager::new(Arc::new(PluginsManager::new(config.codex_home.clone())));
let tool_plugin_provenance = mcp_manager.tool_plugin_provenance(config);
let environment = Environment::create(config.experimental_exec_server_url.clone()).await?;
let tool_plugin_provenance = mcp_manager
.tool_plugin_provenance_with_filesystem(config, &environment.get_filesystem())
.await;
if !force_refetch && let Some(cached_connectors) = read_cached_accessible_connectors(&cache_key)
{
let cached_connectors = filter_disallowed_connectors(cached_connectors);
@@ -395,6 +406,32 @@ fn tool_suggest_connector_ids(config: &Config) -> HashSet<String> {
connector_ids
}
async fn tool_suggest_connector_ids_with_filesystem<F>(
config: &Config,
filesystem: &F,
) -> anyhow::Result<HashSet<String>>
where
F: ExecutorFileSystem + ?Sized,
{
let mut connector_ids = PluginsManager::new(config.codex_home.clone())
.plugins_for_config_with_filesystem(config, /*force_reload*/ false, filesystem)
.await
.capability_summaries()
.iter()
.flat_map(|plugin| plugin.app_connector_ids.iter())
.map(|connector_id| connector_id.0.clone())
.collect::<HashSet<_>>();
connector_ids.extend(
config
.tool_suggest
.discoverables
.iter()
.filter(|discoverable| discoverable.kind == ToolSuggestDiscoverableType::Connector)
.map(|discoverable| discoverable.id.clone()),
);
Ok(connector_ids)
}
async fn list_directory_connectors_for_tool_suggest_with_auth(
config: &Config,
auth: Option<&CodexAuth>,
+48 -13
View File
@@ -1,44 +1,73 @@
use codex_exec_server::ExecutorFileSystem;
#[cfg(test)]
use codex_exec_server::LocalFileSystem;
use codex_protocol::custom_prompts::CustomPrompt;
use codex_utils_absolute_path::AbsolutePathBuf;
use std::collections::HashSet;
use std::path::Path;
use std::path::PathBuf;
use tokio::fs;
/// Return the default prompts directory: `$CODEX_HOME/prompts`.
/// If `CODEX_HOME` cannot be resolved, returns `None`.
pub fn default_prompts_dir() -> Option<PathBuf> {
crate::config::find_codex_home()
.ok()
.map(|home| home.join("prompts"))
.map(|home| prompts_dir(&home))
}
pub fn prompts_dir(codex_home: &Path) -> PathBuf {
codex_home.join("prompts")
}
/// Discover prompt files in the given directory, returning entries sorted by name.
/// Non-files are ignored. If the directory does not exist or cannot be read, returns empty.
#[cfg(test)]
pub async fn discover_prompts_in(dir: &Path) -> Vec<CustomPrompt> {
discover_prompts_in_excluding(dir, &HashSet::new()).await
}
/// Discover prompt files in the given directory, excluding any with names in `exclude`.
/// Returns entries sorted by name. Non-files are ignored. Missing/unreadable dir yields empty.
#[cfg(test)]
pub async fn discover_prompts_in_excluding(
dir: &Path,
exclude: &HashSet<String>,
) -> Vec<CustomPrompt> {
let Ok(dir) = AbsolutePathBuf::from_absolute_path(dir) else {
return Vec::new();
};
discover_prompts_in_excluding_with_filesystem(&dir, exclude, &LocalFileSystem).await
}
pub async fn discover_prompts_in_with_filesystem<F>(
dir: &AbsolutePathBuf,
filesystem: &F,
) -> Vec<CustomPrompt>
where
F: ExecutorFileSystem + ?Sized,
{
discover_prompts_in_excluding_with_filesystem(dir, &HashSet::new(), filesystem).await
}
pub async fn discover_prompts_in_excluding_with_filesystem<F>(
dir: &AbsolutePathBuf,
exclude: &HashSet<String>,
filesystem: &F,
) -> Vec<CustomPrompt>
where
F: ExecutorFileSystem + ?Sized,
{
let mut out: Vec<CustomPrompt> = Vec::new();
let mut entries = match fs::read_dir(dir).await {
let entries = match filesystem.read_directory(dir).await {
Ok(entries) => entries,
Err(_) => return out,
};
while let Ok(Some(entry)) = entries.next_entry().await {
let path = entry.path();
let is_file_like = fs::metadata(&path)
.await
.map(|m| m.is_file())
.unwrap_or(false);
if !is_file_like {
for entry in entries {
if !entry.is_file {
continue;
}
let path = dir.as_path().join(&entry.file_name);
// Only include Markdown files with a .md extension.
let is_md = path
.extension()
@@ -58,14 +87,20 @@ pub async fn discover_prompts_in_excluding(
if exclude.contains(&name) {
continue;
}
let content = match fs::read_to_string(&path).await {
Ok(s) => s,
let Ok(path) = AbsolutePathBuf::try_from(path) else {
continue;
};
let content = match filesystem.read_file(&path).await {
Ok(contents) => match String::from_utf8(contents) {
Ok(contents) => contents,
Err(_) => continue,
},
Err(_) => continue,
};
let (description, argument_hint, body) = parse_frontmatter(&content);
out.push(CustomPrompt {
name,
path,
path: path.to_path_buf(),
content: body,
description,
argument_hint,
+5
View File
@@ -9,6 +9,7 @@ use chrono::Datelike;
use chrono::Local;
use chrono::Utc;
use codex_async_utils::CancelErr;
use codex_exec_server::ExecServerError;
use codex_protocol::ThreadId;
use codex_protocol::protocol::CodexErrorInfo;
use codex_protocol::protocol::ErrorEvent;
@@ -164,6 +165,9 @@ pub enum CodexErr {
// -----------------------------------------------------------------
// Automatic conversions for common external error types
// -----------------------------------------------------------------
#[error(transparent)]
ExecServer(#[from] ExecServerError),
#[error(transparent)]
Io(#[from] io::Error),
@@ -221,6 +225,7 @@ impl CodexErr {
| CodexErr::ConnectionFailed(_)
| CodexErr::InternalServerError
| CodexErr::InternalAgentDied
| CodexErr::ExecServer(_)
| CodexErr::Io(_)
| CodexErr::Json(_)
| CodexErr::TokioJoin(_) => true,
+152
View File
@@ -9,6 +9,7 @@ use crate::config_loader::ConfigLayerStack;
use crate::config_loader::ConfigLayerStackOrdering;
use crate::is_dangerous_command::command_might_be_dangerous;
use crate::is_safe_command::is_known_safe_command;
use codex_exec_server::ExecutorFileSystem;
use codex_execpolicy::AmendError;
use codex_execpolicy::Decision;
use codex_execpolicy::Error as ExecPolicyRuleError;
@@ -211,6 +212,7 @@ impl ExecPolicyManager {
}
#[instrument(level = "info", skip_all)]
#[cfg(test)]
pub(crate) async fn load(config_stack: &ConfigLayerStack) -> Result<Self, ExecPolicyError> {
let (policy, warning) = load_exec_policy_with_warning(config_stack).await?;
if let Some(err) = warning.as_ref() {
@@ -219,6 +221,22 @@ impl ExecPolicyManager {
Ok(Self::new(Arc::new(policy)))
}
#[instrument(level = "info", skip_all)]
pub(crate) async fn load_with_filesystem<F>(
config_stack: &ConfigLayerStack,
filesystem: &F,
) -> Result<Self, ExecPolicyError>
where
F: ExecutorFileSystem + ?Sized,
{
let (policy, warning) =
load_exec_policy_with_warning_with_filesystem(config_stack, filesystem).await?;
if let Some(err) = warning.as_ref() {
tracing::warn!("failed to parse rules: {err}");
}
Ok(Self::new(Arc::new(policy)))
}
pub(crate) fn current(&self) -> Arc<Policy> {
self.policy.load_full()
}
@@ -484,6 +502,20 @@ async fn load_exec_policy_with_warning(
}
}
async fn load_exec_policy_with_warning_with_filesystem<F>(
config_stack: &ConfigLayerStack,
filesystem: &F,
) -> Result<(Policy, Option<ExecPolicyError>), ExecPolicyError>
where
F: ExecutorFileSystem + ?Sized,
{
match load_exec_policy_with_filesystem(config_stack, filesystem).await {
Ok(policy) => Ok((policy, None)),
Err(err @ ExecPolicyError::ParsePolicy { .. }) => Ok((Policy::empty(), Some(err))),
Err(err) => Err(err),
}
}
pub async fn load_exec_policy(config_stack: &ConfigLayerStack) -> Result<Policy, ExecPolicyError> {
// Iterate the layers in increasing order of precedence, adding the *.rules
// from each layer, so that higher-precedence layers can override
@@ -534,6 +566,76 @@ pub async fn load_exec_policy(config_stack: &ConfigLayerStack) -> Result<Policy,
Ok(policy.merge_overlay(requirements_policy.as_ref()))
}
pub async fn load_exec_policy_with_filesystem<F>(
config_stack: &ConfigLayerStack,
filesystem: &F,
) -> Result<Policy, ExecPolicyError>
where
F: ExecutorFileSystem + ?Sized,
{
let mut policy_paths = Vec::new();
for layer in config_stack.get_layers(
ConfigLayerStackOrdering::LowestPrecedenceFirst,
/*include_disabled*/ false,
) {
if let Some(config_folder) = layer.config_folder() {
#[expect(clippy::expect_used)]
let policy_dir = config_folder.join(RULES_DIR_NAME).expect("safe join");
let layer_policy_paths =
collect_policy_files_with_filesystem(&policy_dir, filesystem).await?;
policy_paths.extend(layer_policy_paths);
}
}
tracing::trace!(
policy_paths = ?policy_paths,
"loaded exec policies"
);
let mut parser = PolicyParser::new();
for policy_path in &policy_paths {
let absolute_path = AbsolutePathBuf::from_absolute_path(policy_path).map_err(|source| {
ExecPolicyError::ReadFile {
path: policy_path.clone(),
source: std::io::Error::new(
ErrorKind::InvalidInput,
format!("invalid policy path: {source}"),
),
}
})?;
let contents = filesystem
.read_file(&absolute_path)
.await
.map_err(|source| ExecPolicyError::ReadFile {
path: policy_path.clone(),
source,
})?;
let contents = String::from_utf8(contents).map_err(|source| ExecPolicyError::ReadFile {
path: policy_path.clone(),
source: std::io::Error::new(
ErrorKind::InvalidData,
format!("policy file is not valid UTF-8: {source}"),
),
})?;
let identifier = policy_path.to_string_lossy().to_string();
parser
.parse(&identifier, &contents)
.map_err(|source| ExecPolicyError::ParsePolicy {
path: identifier,
source,
})?;
}
let policy = parser.build();
tracing::debug!("loaded rules from {} files", policy_paths.len());
tracing::trace!(rules = ?policy, "exec policy rules loaded");
let Some(requirements_policy) = config_stack.requirements().exec_policy.as_deref() else {
return Ok(policy);
};
Ok(policy.merge_overlay(requirements_policy.as_ref()))
}
/// If a command is not matched by any execpolicy rule, derive a [`Decision`].
pub fn render_decision_for_unmatched_command(
approval_policy: AskForApproval,
@@ -867,6 +969,56 @@ async fn collect_policy_files(dir: impl AsRef<Path>) -> Result<Vec<PathBuf>, Exe
Ok(policy_paths)
}
async fn collect_policy_files_with_filesystem<F>(
dir: impl AsRef<Path>,
filesystem: &F,
) -> Result<Vec<PathBuf>, ExecPolicyError>
where
F: ExecutorFileSystem + ?Sized,
{
let dir = dir.as_ref();
let absolute_dir =
AbsolutePathBuf::from_absolute_path(dir).map_err(|source| ExecPolicyError::ReadDir {
dir: dir.to_path_buf(),
source: std::io::Error::new(
ErrorKind::InvalidInput,
format!("invalid rules directory: {source}"),
),
})?;
let entries = match filesystem.read_directory(&absolute_dir).await {
Ok(entries) => entries,
Err(err) if err.kind() == ErrorKind::NotFound => return Ok(Vec::new()),
Err(source) => {
return Err(ExecPolicyError::ReadDir {
dir: dir.to_path_buf(),
source,
});
}
};
let mut policy_paths = Vec::new();
for entry in entries {
let path = dir.join(entry.file_name);
if path
.extension()
.and_then(|ext| ext.to_str())
.is_some_and(|ext| ext == RULE_EXTENSION)
&& entry.is_file
{
policy_paths.push(path);
}
}
policy_paths.sort();
tracing::debug!(
"loaded {} .rules files in {}",
policy_paths.len(),
dir.display()
);
Ok(policy_paths)
}
#[cfg(test)]
#[path = "exec_policy_tests.rs"]
mod tests;
+100 -47
View File
@@ -3,13 +3,13 @@
use std::collections::HashMap;
use std::collections::HashSet;
use std::path::Path;
use std::path::PathBuf;
use std::sync::Arc;
use std::sync::Mutex;
use std::sync::RwLock;
use std::time::Duration;
use codex_exec_server::ExecutorFileSystem;
use notify::Event;
use notify::EventKind;
use notify::RecommendedWatcher;
@@ -27,11 +27,15 @@ use crate::skills::SkillsManager;
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum FileWatcherEvent {
SkillsChanged { paths: Vec<PathBuf> },
SkillsChanged {
environment_id: String,
paths: Vec<PathBuf>,
},
}
struct WatchState {
skills_root_ref_counts: HashMap<PathBuf, usize>,
skills_root_registrations: HashMap<PathBuf, HashMap<String, usize>>,
}
struct FileWatcherInner {
@@ -43,43 +47,54 @@ const WATCHER_THROTTLE_INTERVAL: Duration = Duration::from_secs(10);
/// Coalesces bursts of paths and emits at most once per interval.
struct ThrottledPaths {
pending: HashSet<PathBuf>,
pending: HashMap<String, HashSet<PathBuf>>,
next_allowed_at: Instant,
}
impl ThrottledPaths {
fn new(now: Instant) -> Self {
Self {
pending: HashSet::new(),
pending: HashMap::new(),
next_allowed_at: now,
}
}
fn add(&mut self, paths: Vec<PathBuf>) {
self.pending.extend(paths);
fn add(&mut self, paths: HashMap<String, Vec<PathBuf>>) {
for (environment_id, environment_paths) in paths {
self.pending
.entry(environment_id)
.or_default()
.extend(environment_paths);
}
}
fn next_deadline(&self, now: Instant) -> Option<Instant> {
(!self.pending.is_empty() && now < self.next_allowed_at).then_some(self.next_allowed_at)
}
fn take_ready(&mut self, now: Instant) -> Option<Vec<PathBuf>> {
fn take_ready(&mut self, now: Instant) -> Option<Vec<(String, Vec<PathBuf>)>> {
if self.pending.is_empty() || now < self.next_allowed_at {
return None;
}
Some(self.take_with_next_allowed(now))
}
fn take_pending(&mut self, now: Instant) -> Option<Vec<PathBuf>> {
fn take_pending(&mut self, now: Instant) -> Option<Vec<(String, Vec<PathBuf>)>> {
if self.pending.is_empty() {
return None;
}
Some(self.take_with_next_allowed(now))
}
fn take_with_next_allowed(&mut self, now: Instant) -> Vec<PathBuf> {
let mut paths: Vec<PathBuf> = self.pending.drain().collect();
paths.sort_unstable_by(|a, b| a.as_os_str().cmp(b.as_os_str()));
fn take_with_next_allowed(&mut self, now: Instant) -> Vec<(String, Vec<PathBuf>)> {
let mut pending = self.pending.drain().collect::<Vec<_>>();
pending.sort_unstable_by(|(left, _), (right, _)| left.cmp(right));
let mut paths = Vec::with_capacity(pending.len());
for (environment_id, environment_paths) in pending {
let mut environment_paths = environment_paths.into_iter().collect::<Vec<_>>();
environment_paths.sort_unstable_by(|a, b| a.as_os_str().cmp(b.as_os_str()));
paths.push((environment_id, environment_paths));
}
self.next_allowed_at = now + WATCHER_THROTTLE_INTERVAL;
paths
}
@@ -93,13 +108,14 @@ pub(crate) struct FileWatcher {
pub(crate) struct WatchRegistration {
file_watcher: std::sync::Weak<FileWatcher>,
environment_id: String,
roots: Vec<PathBuf>,
}
impl Drop for WatchRegistration {
fn drop(&mut self) {
if let Some(file_watcher) = self.file_watcher.upgrade() {
file_watcher.unregister_roots(&self.roots);
file_watcher.unregister_roots(&self.environment_id, &self.roots);
}
}
}
@@ -118,6 +134,7 @@ impl FileWatcher {
let (tx, _) = broadcast::channel(128);
let state = Arc::new(RwLock::new(WatchState {
skills_root_ref_counts: HashMap::new(),
skills_root_registrations: HashMap::new(),
}));
let file_watcher = Self {
inner: Some(Mutex::new(inner)),
@@ -134,6 +151,7 @@ impl FileWatcher {
inner: None,
state: Arc::new(RwLock::new(WatchState {
skills_root_ref_counts: HashMap::new(),
skills_root_registrations: HashMap::new(),
})),
tx,
}
@@ -143,24 +161,31 @@ impl FileWatcher {
self.tx.subscribe()
}
pub(crate) fn register_config(
pub(crate) async fn register_config_with_filesystem<F>(
self: &Arc<Self>,
config: &Config,
environment_id: String,
skills_manager: &SkillsManager,
) -> WatchRegistration {
filesystem: &F,
) -> WatchRegistration
where
F: ExecutorFileSystem + ?Sized,
{
let deduped_roots: HashSet<PathBuf> = skills_manager
.skill_roots_for_config(config)
.skill_roots_for_config_with_filesystem(config, filesystem)
.await
.into_iter()
.map(|root| root.path)
.collect();
let mut registered_roots: Vec<PathBuf> = deduped_roots.into_iter().collect();
registered_roots.sort_unstable_by(|a, b| a.as_os_str().cmp(b.as_os_str()));
for root in &registered_roots {
self.register_skills_root(root.clone());
self.register_skills_root(root.clone(), &environment_id);
}
WatchRegistration {
file_watcher: Arc::downgrade(self),
environment_id,
roots: registered_roots,
}
}
@@ -194,8 +219,13 @@ impl FileWatcher {
let now = Instant::now();
skills.add(skills_paths);
if let Some(paths) = skills.take_ready(now) {
let _ = tx.send(FileWatcherEvent::SkillsChanged { paths });
if let Some(changes) = skills.take_ready(now) {
for (environment_id, paths) in changes {
let _ = tx.send(FileWatcherEvent::SkillsChanged {
environment_id,
paths,
});
}
}
}
Some(Err(err)) => {
@@ -205,8 +235,13 @@ impl FileWatcher {
// Flush any pending changes before shutdown so subscribers
// see the latest state.
let now = Instant::now();
if let Some(paths) = skills.take_pending(now) {
let _ = tx.send(FileWatcherEvent::SkillsChanged { paths });
if let Some(changes) = skills.take_pending(now) {
for (environment_id, paths) in changes {
let _ = tx.send(FileWatcherEvent::SkillsChanged {
environment_id,
paths,
});
}
}
break;
}
@@ -214,8 +249,13 @@ impl FileWatcher {
}
_ = &mut timer => {
let now = Instant::now();
if let Some(paths) = skills.take_ready(now) {
let _ = tx.send(FileWatcherEvent::SkillsChanged { paths });
if let Some(changes) = skills.take_ready(now) {
for (environment_id, paths) in changes {
let _ = tx.send(FileWatcherEvent::SkillsChanged {
environment_id,
paths,
});
}
}
}
}
@@ -226,11 +266,17 @@ impl FileWatcher {
}
}
fn register_skills_root(&self, root: PathBuf) {
fn register_skills_root(&self, root: PathBuf, environment_id: &str) {
let mut state = self
.state
.write()
.unwrap_or_else(std::sync::PoisonError::into_inner);
*state
.skills_root_registrations
.entry(root.clone())
.or_default()
.entry(environment_id.to_string())
.or_insert(0) += 1;
let count = state
.skills_root_ref_counts
.entry(root.clone())
@@ -241,7 +287,7 @@ impl FileWatcher {
}
}
fn unregister_roots(&self, roots: &[PathBuf]) {
fn unregister_roots(&self, environment_id: &str, roots: &[PathBuf]) {
let mut state = self
.state
.write()
@@ -250,6 +296,18 @@ impl FileWatcher {
for root in roots {
let mut should_unwatch = false;
if let Some(environment_counts) = state.skills_root_registrations.get_mut(root) {
if let Some(count) = environment_counts.get_mut(environment_id) {
if *count > 1 {
*count -= 1;
} else {
environment_counts.remove(environment_id);
}
}
if environment_counts.is_empty() {
state.skills_root_registrations.remove(root);
}
}
if let Some(count) = state.skills_root_ref_counts.get_mut(root) {
if *count > 1 {
*count -= 1;
@@ -311,42 +369,37 @@ impl FileWatcher {
}
}
fn classify_event(event: &Event, state: &RwLock<WatchState>) -> Vec<PathBuf> {
fn classify_event(event: &Event, state: &RwLock<WatchState>) -> HashMap<String, Vec<PathBuf>> {
if !matches!(
event.kind,
EventKind::Create(_) | EventKind::Modify(_) | EventKind::Remove(_)
) {
return Vec::new();
return HashMap::new();
}
let mut skills_paths = Vec::new();
let skills_roots = match state.read() {
Ok(state) => state
.skills_root_ref_counts
.keys()
.cloned()
.collect::<HashSet<_>>(),
Err(err) => {
let state = err.into_inner();
state
.skills_root_ref_counts
.keys()
.cloned()
.collect::<HashSet<_>>()
}
let registrations = match state.read() {
Ok(state) => state.skills_root_registrations.clone(),
Err(err) => err.into_inner().skills_root_registrations.clone(),
};
let mut skills_paths = HashMap::<String, HashSet<PathBuf>>::new();
for path in &event.paths {
if is_skills_path(path, &skills_roots) {
skills_paths.push(path.clone());
for (root, environments) in &registrations {
if path.starts_with(root) {
for environment_id in environments.keys() {
skills_paths
.entry(environment_id.clone())
.or_default()
.insert(path.clone());
}
}
}
}
skills_paths
}
fn is_skills_path(path: &Path, roots: &HashSet<PathBuf>) -> bool {
roots.iter().any(|root| path.starts_with(root))
.into_iter()
.map(|(environment_id, paths)| (environment_id, paths.into_iter().collect()))
.collect()
}
#[cfg(test)]
+69 -19
View File
@@ -6,6 +6,7 @@ use notify::event::CreateKind;
use notify::event::ModifyKind;
use notify::event::RemoveKind;
use pretty_assertions::assert_eq;
use std::collections::HashMap;
use tokio::time::timeout;
fn path(name: &str) -> PathBuf {
@@ -20,39 +21,59 @@ fn notify_event(kind: EventKind, paths: Vec<PathBuf>) -> Event {
event
}
fn default_environment_id() -> String {
codex_exec_server::Environment::default_environment_id(None)
}
fn skills_root_registrations(
roots: &[(PathBuf, usize)],
) -> HashMap<PathBuf, HashMap<String, usize>> {
let environment_id = default_environment_id();
roots
.iter()
.cloned()
.map(|(root, count)| (root, HashMap::from([(environment_id.clone(), count)])))
.collect()
}
#[test]
fn throttles_and_coalesces_within_interval() {
let start = Instant::now();
let mut throttled = ThrottledPaths::new(start);
let environment_id = codex_exec_server::Environment::default_environment_id(None);
throttled.add(vec![path("a")]);
throttled.add(HashMap::from([(environment_id.clone(), vec![path("a")])]));
let first = throttled.take_ready(start).expect("first emit");
assert_eq!(first, vec![path("a")]);
assert_eq!(first, vec![(environment_id.clone(), vec![path("a")])]);
throttled.add(vec![path("b"), path("c")]);
throttled.add(HashMap::from([(
environment_id.clone(),
vec![path("b"), path("c")],
)]));
assert_eq!(throttled.take_ready(start), None);
let second = throttled
.take_ready(start + WATCHER_THROTTLE_INTERVAL)
.expect("coalesced emit");
assert_eq!(second, vec![path("b"), path("c")]);
assert_eq!(second, vec![(environment_id, vec![path("b"), path("c")])]);
}
#[test]
fn flushes_pending_on_shutdown() {
let start = Instant::now();
let mut throttled = ThrottledPaths::new(start);
let environment_id = default_environment_id();
throttled.add(vec![path("a")]);
throttled.add(HashMap::from([(environment_id.clone(), vec![path("a")])]));
let _ = throttled.take_ready(start).expect("first emit");
throttled.add(vec![path("b")]);
throttled.add(HashMap::from([(environment_id.clone(), vec![path("b")])]));
assert_eq!(throttled.take_ready(start), None);
let flushed = throttled
.take_pending(start)
.expect("shutdown flush emits pending paths");
assert_eq!(flushed, vec![path("b")]);
assert_eq!(flushed, vec![(environment_id, vec![path("b")])]);
}
#[test]
@@ -60,6 +81,7 @@ fn classify_event_filters_to_skills_roots() {
let root = path("/tmp/skills");
let state = RwLock::new(WatchState {
skills_root_ref_counts: HashMap::from([(root.clone(), 1)]),
skills_root_registrations: skills_root_registrations(&[(root.clone(), 1)]),
});
let event = notify_event(
EventKind::Create(CreateKind::Any),
@@ -70,7 +92,10 @@ fn classify_event_filters_to_skills_roots() {
);
let classified = classify_event(&event, &state);
assert_eq!(classified, vec![root.join("demo/SKILL.md")]);
assert_eq!(
classified,
HashMap::from([(default_environment_id(), vec![root.join("demo/SKILL.md")],)])
);
}
#[test]
@@ -79,6 +104,10 @@ fn classify_event_supports_multiple_roots_without_prefix_false_positives() {
let root_b = path("/tmp/workspace/.codex/skills");
let state = RwLock::new(WatchState {
skills_root_ref_counts: HashMap::from([(root_a.clone(), 1), (root_b.clone(), 1)]),
skills_root_registrations: skills_root_registrations(&[
(root_a.clone(), 1),
(root_b.clone(), 1),
]),
});
let event = notify_event(
EventKind::Modify(ModifyKind::Any),
@@ -92,7 +121,10 @@ fn classify_event_supports_multiple_roots_without_prefix_false_positives() {
let classified = classify_event(&event, &state);
assert_eq!(
classified,
vec![root_a.join("alpha/SKILL.md"), root_b.join("beta/SKILL.md")]
HashMap::from([(
default_environment_id(),
vec![root_a.join("alpha/SKILL.md"), root_b.join("beta/SKILL.md")],
)])
);
}
@@ -101,6 +133,7 @@ fn classify_event_ignores_non_mutating_event_kinds() {
let root = path("/tmp/skills");
let state = RwLock::new(WatchState {
skills_root_ref_counts: HashMap::from([(root.clone(), 1)]),
skills_root_registrations: skills_root_registrations(&[(root.clone(), 1)]),
});
let path = root.join("demo/SKILL.md");
@@ -108,22 +141,32 @@ fn classify_event_ignores_non_mutating_event_kinds() {
EventKind::Access(AccessKind::Open(AccessMode::Any)),
vec![path.clone()],
);
assert_eq!(classify_event(&access_event, &state), Vec::<PathBuf>::new());
assert_eq!(
classify_event(&access_event, &state),
HashMap::<String, Vec<PathBuf>>::new()
);
let any_event = notify_event(EventKind::Any, vec![path.clone()]);
assert_eq!(classify_event(&any_event, &state), Vec::<PathBuf>::new());
assert_eq!(
classify_event(&any_event, &state),
HashMap::<String, Vec<PathBuf>>::new()
);
let other_event = notify_event(EventKind::Other, vec![path]);
assert_eq!(classify_event(&other_event, &state), Vec::<PathBuf>::new());
assert_eq!(
classify_event(&other_event, &state),
HashMap::<String, Vec<PathBuf>>::new()
);
}
#[test]
fn register_skills_root_dedupes_state_entries() {
let watcher = FileWatcher::noop();
let root = path("/tmp/skills");
watcher.register_skills_root(root.clone());
watcher.register_skills_root(root);
watcher.register_skills_root(path("/tmp/other-skills"));
let environment_id = default_environment_id();
watcher.register_skills_root(root.clone(), &environment_id);
watcher.register_skills_root(root, &environment_id);
watcher.register_skills_root(path("/tmp/other-skills"), &environment_id);
let state = watcher.state.read().expect("state lock");
assert_eq!(state.skills_root_ref_counts.len(), 2);
@@ -133,9 +176,10 @@ fn register_skills_root_dedupes_state_entries() {
fn watch_registration_drop_unregisters_roots() {
let watcher = Arc::new(FileWatcher::noop());
let root = path("/tmp/skills");
watcher.register_skills_root(root.clone());
watcher.register_skills_root(root.clone(), &default_environment_id());
let registration = WatchRegistration {
file_watcher: Arc::downgrade(&watcher),
environment_id: default_environment_id(),
roots: vec![root],
};
@@ -152,15 +196,17 @@ fn unregister_holds_state_lock_until_unwatch_finishes() {
std::fs::create_dir(&root).expect("create root");
let watcher = Arc::new(FileWatcher::new(temp_dir.path().to_path_buf()).expect("watcher"));
watcher.register_skills_root(root.clone());
let environment_id = default_environment_id();
watcher.register_skills_root(root.clone(), &environment_id);
let inner = watcher.inner.as_ref().expect("watcher inner");
let inner_guard = inner.lock().expect("inner lock");
let unregister_watcher = Arc::clone(&watcher);
let unregister_root = root.clone();
let unregister_environment_id = environment_id.clone();
let unregister_thread = std::thread::spawn(move || {
unregister_watcher.unregister_roots(&[unregister_root]);
unregister_watcher.unregister_roots(&unregister_environment_id, &[unregister_root]);
});
let state_lock_observed = (0..100).any(|_| {
@@ -174,8 +220,9 @@ fn unregister_holds_state_lock_until_unwatch_finishes() {
let register_watcher = Arc::clone(&watcher);
let register_root = root.clone();
let register_environment_id = environment_id.clone();
let register_thread = std::thread::spawn(move || {
register_watcher.register_skills_root(register_root);
register_watcher.register_skills_root(register_root, &register_environment_id);
});
drop(inner_guard);
@@ -202,6 +249,7 @@ async fn spawn_event_loop_flushes_pending_changes_on_shutdown() {
{
let mut state = watcher.state.write().expect("state lock");
state.skills_root_ref_counts.insert(root.clone(), 1);
state.skills_root_registrations = skills_root_registrations(&[(root.clone(), 1)]);
}
let (raw_tx, raw_rx) = mpsc::unbounded_channel();
@@ -221,6 +269,7 @@ async fn spawn_event_loop_flushes_pending_changes_on_shutdown() {
assert_eq!(
first,
FileWatcherEvent::SkillsChanged {
environment_id: default_environment_id(),
paths: vec![root.join("a/SKILL.md")]
}
);
@@ -240,6 +289,7 @@ async fn spawn_event_loop_flushes_pending_changes_on_shutdown() {
assert_eq!(
second,
FileWatcherEvent::SkillsChanged {
environment_id: default_environment_id(),
paths: vec![root.join("b/SKILL.md")]
}
);
+92 -2
View File
@@ -15,6 +15,7 @@ use codex_protocol::mcp::Tool;
use codex_protocol::protocol::McpListToolsResponseEvent;
use codex_protocol::protocol::SandboxPolicy;
use serde_json::Value;
use tracing::warn;
use crate::AuthManager;
use crate::CodexAuth;
@@ -27,6 +28,7 @@ use crate::mcp_connection_manager::SandboxState;
use crate::mcp_connection_manager::codex_apps_tools_cache_key;
use crate::plugins::PluginCapabilitySummary;
use crate::plugins::PluginsManager;
use codex_exec_server::ExecutorFileSystem;
const MCP_TOOL_NAME_PREFIX: &str = "mcp";
const MCP_TOOL_NAME_DELIMITER: &str = "__";
@@ -221,6 +223,39 @@ impl McpManager {
let loaded_plugins = self.plugins_manager.plugins_for_config(config);
ToolPluginProvenance::from_capability_summaries(loaded_plugins.capability_summaries())
}
pub async fn effective_servers_with_filesystem<F>(
&self,
config: &Config,
auth: Option<&CodexAuth>,
filesystem: &F,
) -> HashMap<String, McpServerConfig>
where
F: ExecutorFileSystem + ?Sized,
{
effective_mcp_servers_with_filesystem(
config,
auth,
self.plugins_manager.as_ref(),
filesystem,
)
.await
}
pub async fn tool_plugin_provenance_with_filesystem<F>(
&self,
config: &Config,
filesystem: &F,
) -> ToolPluginProvenance
where
F: ExecutorFileSystem + ?Sized,
{
let loaded_plugins = self
.plugins_manager
.plugins_for_config_with_filesystem(config, /*force_reload*/ false, filesystem)
.await;
ToolPluginProvenance::from_capability_summaries(loaded_plugins.capability_summaries())
}
}
fn configured_mcp_servers(
@@ -249,6 +284,42 @@ fn effective_mcp_servers(
)
}
async fn configured_mcp_servers_with_filesystem<F>(
config: &Config,
plugins_manager: &PluginsManager,
filesystem: &F,
) -> HashMap<String, McpServerConfig>
where
F: ExecutorFileSystem + ?Sized,
{
let loaded_plugins = plugins_manager
.plugins_for_config_with_filesystem(config, /*force_reload*/ false, filesystem)
.await;
let mut servers = config.mcp_servers.get().clone();
for (name, plugin_server) in loaded_plugins.effective_mcp_servers() {
servers.entry(name).or_insert(plugin_server);
}
servers
}
async fn effective_mcp_servers_with_filesystem<F>(
config: &Config,
auth: Option<&CodexAuth>,
plugins_manager: &PluginsManager,
filesystem: &F,
) -> HashMap<String, McpServerConfig>
where
F: ExecutorFileSystem + ?Sized,
{
let servers = configured_mcp_servers_with_filesystem(config, plugins_manager, filesystem).await;
with_codex_apps_mcp(
servers,
config.features.apps_enabled_for_auth(auth),
auth,
config,
)
}
pub async fn collect_mcp_snapshot(config: &Config) -> McpListToolsResponseEvent {
let auth_manager = AuthManager::shared(
config.codex_home.clone(),
@@ -257,8 +328,27 @@ pub async fn collect_mcp_snapshot(config: &Config) -> McpListToolsResponseEvent
);
let auth = auth_manager.auth().await;
let mcp_manager = McpManager::new(Arc::new(PluginsManager::new(config.codex_home.clone())));
let mcp_servers = mcp_manager.effective_servers(config, auth.as_ref());
let tool_plugin_provenance = mcp_manager.tool_plugin_provenance(config);
let environment =
match codex_exec_server::Environment::create(config.experimental_exec_server_url.clone())
.await
{
Ok(environment) => environment,
Err(err) => {
warn!("failed to bind environment for MCP snapshot collection: {err}");
return McpListToolsResponseEvent {
tools: HashMap::new(),
resources: HashMap::new(),
resource_templates: HashMap::new(),
auth_statuses: HashMap::new(),
};
}
};
let mcp_servers = mcp_manager
.effective_servers_with_filesystem(config, auth.as_ref(), &environment.get_filesystem())
.await;
let tool_plugin_provenance = mcp_manager
.tool_plugin_provenance_with_filesystem(config, &environment.get_filesystem())
.await;
if mcp_servers.is_empty() {
return McpListToolsResponseEvent {
tools: HashMap::new(),
+6 -1
View File
@@ -293,7 +293,12 @@ pub(crate) async fn maybe_install_mcp_dependencies(
let mut refresh_servers = sess
.services
.mcp_manager
.effective_servers(config, auth.as_ref());
.effective_servers_with_filesystem(
config,
auth.as_ref(),
&sess.services.environment.get_filesystem(),
)
.await;
for (name, server_config) in &servers {
refresh_servers
.entry(name.clone())
+16
View File
@@ -1,4 +1,6 @@
use crate::default_client::build_reqwest_client;
use codex_exec_server::ExecutorFileSystem;
use codex_utils_absolute_path::AbsolutePathBuf;
use reqwest::Client;
use serde::Deserialize;
use std::fs;
@@ -43,6 +45,20 @@ pub(crate) fn read_curated_plugins_sha(codex_home: &Path) -> Option<String> {
read_sha_file(codex_home.join(CURATED_PLUGINS_SHA_FILE).as_path())
}
pub(crate) async fn read_curated_plugins_sha_with_filesystem<F>(
codex_home: &Path,
filesystem: &F,
) -> Option<String>
where
F: ExecutorFileSystem + ?Sized,
{
let sha_path = AbsolutePathBuf::try_from(codex_home.join(CURATED_PLUGINS_SHA_FILE)).ok()?;
let contents = filesystem.read_file(&sha_path).await.ok()?;
let sha = String::from_utf8(contents).ok()?;
let sha = sha.trim();
(!sha.is_empty()).then(|| sha.to_string())
}
pub(crate) fn sync_openai_plugins_repo(codex_home: &Path) -> Result<String, String> {
sync_openai_plugins_repo_with_api_base_url(codex_home, GITHUB_API_BASE_URL)
}
+67
View File
@@ -9,6 +9,7 @@ use super::PluginsManager;
use crate::config::Config;
use crate::config::types::ToolSuggestDiscoverableType;
use crate::features::Feature;
use codex_exec_server::ExecutorFileSystem;
const TOOL_SUGGEST_DISCOVERABLE_PLUGIN_ALLOWLIST: &[&str] = &[
"github@openai-curated",
@@ -22,6 +23,7 @@ const TOOL_SUGGEST_DISCOVERABLE_PLUGIN_ALLOWLIST: &[&str] = &[
"google-slides@openai-curated",
];
#[cfg(test)]
pub(crate) fn list_tool_suggest_discoverable_plugins(
config: &Config,
) -> anyhow::Result<Vec<PluginCapabilitySummary>> {
@@ -78,6 +80,71 @@ pub(crate) fn list_tool_suggest_discoverable_plugins(
Ok(discoverable_plugins)
}
pub(crate) async fn list_tool_suggest_discoverable_plugins_with_filesystem<F>(
config: &Config,
filesystem: &F,
) -> anyhow::Result<Vec<PluginCapabilitySummary>>
where
F: ExecutorFileSystem + ?Sized,
{
if !config.features.enabled(Feature::Plugins) {
return Ok(Vec::new());
}
let plugins_manager = PluginsManager::new(config.codex_home.clone());
let configured_plugin_ids = config
.tool_suggest
.discoverables
.iter()
.filter(|discoverable| discoverable.kind == ToolSuggestDiscoverableType::Plugin)
.map(|discoverable| discoverable.id.as_str())
.collect::<HashSet<_>>();
let marketplaces = plugins_manager
.list_marketplaces_for_config_with_filesystem(config, &[], filesystem)
.await
.context("failed to list plugin marketplaces for tool suggestions")?;
let Some(curated_marketplace) = marketplaces
.into_iter()
.find(|marketplace| marketplace.name == OPENAI_CURATED_MARKETPLACE_NAME)
else {
return Ok(Vec::new());
};
let mut discoverable_plugins = Vec::<PluginCapabilitySummary>::new();
for plugin in curated_marketplace.plugins {
if plugin.installed
|| (!TOOL_SUGGEST_DISCOVERABLE_PLUGIN_ALLOWLIST.contains(&plugin.id.as_str())
&& !configured_plugin_ids.contains(plugin.id.as_str()))
{
continue;
}
let plugin_id = plugin.id.clone();
let plugin_name = plugin.name.clone();
match plugins_manager
.read_plugin_for_config_with_filesystem(
config,
&PluginReadRequest {
plugin_name,
marketplace_path: curated_marketplace.path.clone(),
},
filesystem,
)
.await
{
Ok(plugin) => discoverable_plugins.push(plugin.plugin.into()),
Err(err) => warn!("failed to load discoverable plugin suggestion {plugin_id}: {err:#}"),
}
}
discoverable_plugins.sort_by(|left, right| {
left.display_name
.cmp(&right.display_name)
.then_with(|| left.config_name.cmp(&right.config_name))
});
Ok(discoverable_plugins)
}
#[cfg(test)]
#[path = "discoverable_tests.rs"]
mod tests;
File diff suppressed because it is too large Load Diff
+24 -1
View File
@@ -1,3 +1,4 @@
use codex_exec_server::ExecutorFileSystem;
use codex_utils_absolute_path::AbsolutePathBuf;
use serde::Deserialize;
use serde_json::Value as JsonValue;
@@ -118,7 +119,29 @@ pub(crate) fn load_plugin_manifest(plugin_root: &Path) -> Option<PluginManifest>
return None;
}
let contents = fs::read_to_string(&manifest_path).ok()?;
match serde_json::from_str::<RawPluginManifest>(&contents) {
parse_plugin_manifest_contents(plugin_root, &manifest_path, &contents)
}
pub(crate) async fn load_plugin_manifest_with_filesystem<F>(
plugin_root: &Path,
filesystem: &F,
) -> Option<PluginManifest>
where
F: ExecutorFileSystem + ?Sized,
{
let manifest_path = plugin_root.join(PLUGIN_MANIFEST_PATH);
let manifest_path = AbsolutePathBuf::from_absolute_path(&manifest_path).ok()?;
let contents = filesystem.read_file(&manifest_path).await.ok()?;
let contents = String::from_utf8(contents).ok()?;
parse_plugin_manifest_contents(plugin_root, manifest_path.as_path(), &contents)
}
fn parse_plugin_manifest_contents(
plugin_root: &Path,
manifest_path: &Path,
contents: &str,
) -> Option<PluginManifest> {
match serde_json::from_str::<RawPluginManifest>(contents) {
Ok(manifest) => {
let RawPluginManifest {
name: raw_name,
+210
View File
@@ -1,10 +1,12 @@
use super::PluginManifestInterface;
use super::load_plugin_manifest;
use super::manifest::load_plugin_manifest_with_filesystem;
use super::store::PluginId;
use super::store::PluginIdError;
use crate::git_info::get_git_repo_root;
use codex_app_server_protocol::PluginAuthPolicy;
use codex_app_server_protocol::PluginInstallPolicy;
use codex_exec_server::ExecutorFileSystem;
use codex_protocol::protocol::Product;
use codex_utils_absolute_path::AbsolutePathBuf;
use dirs::home_dir;
@@ -149,6 +151,39 @@ pub fn resolve_marketplace_plugin(
restriction_product: Option<Product>,
) -> Result<ResolvedMarketplacePlugin, MarketplaceError> {
let marketplace = load_raw_marketplace_manifest(marketplace_path)?;
resolve_marketplace_plugin_from_manifest(
marketplace_path,
marketplace,
plugin_name,
restriction_product,
)
}
pub(crate) async fn resolve_marketplace_plugin_with_filesystem<F>(
marketplace_path: &AbsolutePathBuf,
plugin_name: &str,
restriction_product: Option<Product>,
filesystem: &F,
) -> Result<ResolvedMarketplacePlugin, MarketplaceError>
where
F: ExecutorFileSystem + ?Sized,
{
let marketplace =
load_raw_marketplace_manifest_with_filesystem(marketplace_path, filesystem).await?;
resolve_marketplace_plugin_from_manifest(
marketplace_path,
marketplace,
plugin_name,
restriction_product,
)
}
fn resolve_marketplace_plugin_from_manifest(
marketplace_path: &AbsolutePathBuf,
marketplace: RawMarketplaceManifest,
plugin_name: &str,
restriction_product: Option<Product>,
) -> Result<ResolvedMarketplacePlugin, MarketplaceError> {
let marketplace_name = marketplace.name;
let plugin = marketplace
.plugins
@@ -195,6 +230,17 @@ pub fn list_marketplaces(
list_marketplaces_with_home(additional_roots, home_dir().as_deref())
}
pub(crate) async fn list_marketplaces_with_home_with_filesystem<F>(
additional_roots: &[AbsolutePathBuf],
home_dir: Option<&Path>,
filesystem: &F,
) -> Result<Vec<Marketplace>, MarketplaceError>
where
F: ExecutorFileSystem + ?Sized,
{
list_marketplaces_with_home_impl(additional_roots, home_dir, filesystem).await
}
pub(crate) fn load_marketplace(path: &AbsolutePathBuf) -> Result<Marketplace, MarketplaceError> {
let marketplace = load_raw_marketplace_manifest(path)?;
let mut plugins = Vec::new();
@@ -239,6 +285,56 @@ pub(crate) fn load_marketplace(path: &AbsolutePathBuf) -> Result<Marketplace, Ma
})
}
pub(crate) async fn load_marketplace_with_filesystem<F>(
path: &AbsolutePathBuf,
filesystem: &F,
) -> Result<Marketplace, MarketplaceError>
where
F: ExecutorFileSystem + ?Sized,
{
let marketplace = load_raw_marketplace_manifest_with_filesystem(path, filesystem).await?;
let mut plugins = Vec::new();
for plugin in marketplace.plugins {
let RawMarketplaceManifestPlugin {
name,
source,
policy,
category,
} = plugin;
let source_path = resolve_plugin_source_path(path, source)?;
let source = MarketplacePluginSource::Local {
path: source_path.clone(),
};
let mut interface = load_plugin_manifest_with_filesystem(source_path.as_path(), filesystem)
.await
.and_then(|manifest| manifest.interface);
if let Some(category) = category {
interface
.get_or_insert_with(PluginManifestInterface::default)
.category = Some(category);
}
plugins.push(MarketplacePlugin {
name,
source,
policy: MarketplacePluginPolicy {
installation: policy.installation,
authentication: policy.authentication,
products: policy.products,
},
interface,
});
}
Ok(Marketplace {
name: marketplace.name,
path: path.clone(),
interface: resolve_marketplace_interface(marketplace.interface),
plugins,
})
}
fn list_marketplaces_with_home(
additional_roots: &[AbsolutePathBuf],
home_dir: Option<&Path>,
@@ -261,6 +357,38 @@ fn list_marketplaces_with_home(
Ok(marketplaces)
}
async fn list_marketplaces_with_home_impl<F>(
additional_roots: &[AbsolutePathBuf],
home_dir: Option<&Path>,
filesystem: &F,
) -> Result<Vec<Marketplace>, MarketplaceError>
where
F: ExecutorFileSystem + ?Sized,
{
let mut marketplaces = Vec::new();
for marketplace_path in discover_marketplace_paths_from_roots_with_filesystem(
additional_roots,
home_dir,
filesystem,
)
.await?
{
match load_marketplace_with_filesystem(&marketplace_path, filesystem).await {
Ok(marketplace) => marketplaces.push(marketplace),
Err(err) => {
warn!(
path = %marketplace_path.display(),
error = %err,
"skipping marketplace that failed to load"
);
}
}
}
Ok(marketplaces)
}
fn discover_marketplace_paths_from_roots(
additional_roots: &[AbsolutePathBuf],
home_dir: Option<&Path>,
@@ -299,6 +427,51 @@ fn discover_marketplace_paths_from_roots(
paths
}
async fn discover_marketplace_paths_from_roots_with_filesystem<F>(
additional_roots: &[AbsolutePathBuf],
home_dir: Option<&Path>,
filesystem: &F,
) -> Result<Vec<AbsolutePathBuf>, MarketplaceError>
where
F: ExecutorFileSystem + ?Sized,
{
let mut paths = Vec::new();
if let Some(home) = home_dir {
let path = home.join(MARKETPLACE_RELATIVE_PATH);
let path = AbsolutePathBuf::try_from(path).map_err(|err| {
MarketplaceError::InvalidMarketplaceFile {
path: home.to_path_buf(),
message: format!("failed to resolve marketplace path: {err}"),
}
})?;
if path_exists_with_filesystem(path.as_path(), filesystem).await && !paths.contains(&path) {
paths.push(path);
}
}
for root in additional_roots {
let root_path = root.join(MARKETPLACE_RELATIVE_PATH);
if let Ok(path) = root_path
&& path_exists_with_filesystem(path.as_path(), filesystem).await
&& !paths.contains(&path)
{
paths.push(path);
continue;
}
if let Some(repo_root) = get_git_repo_root(root.as_path())
&& let Ok(repo_root) = AbsolutePathBuf::try_from(repo_root)
&& let Ok(path) = repo_root.join(MARKETPLACE_RELATIVE_PATH)
&& path_exists_with_filesystem(path.as_path(), filesystem).await
&& !paths.contains(&path)
{
paths.push(path);
}
}
Ok(paths)
}
fn load_raw_marketplace_manifest(
path: &AbsolutePathBuf,
) -> Result<RawMarketplaceManifest, MarketplaceError> {
@@ -317,6 +490,43 @@ fn load_raw_marketplace_manifest(
})
}
async fn load_raw_marketplace_manifest_with_filesystem<F>(
path: &AbsolutePathBuf,
filesystem: &F,
) -> Result<RawMarketplaceManifest, MarketplaceError>
where
F: ExecutorFileSystem + ?Sized,
{
let contents = filesystem
.read_file(path)
.await
.map_err(|err| match err.kind() {
io::ErrorKind::NotFound => MarketplaceError::MarketplaceNotFound {
path: path.to_path_buf(),
},
_ => MarketplaceError::io("failed to read marketplace file", err),
})?;
let contents =
String::from_utf8(contents).map_err(|err| MarketplaceError::InvalidMarketplaceFile {
path: path.to_path_buf(),
message: format!("marketplace file is not valid UTF-8: {err}"),
})?;
serde_json::from_str(&contents).map_err(|err| MarketplaceError::InvalidMarketplaceFile {
path: path.to_path_buf(),
message: err.to_string(),
})
}
async fn path_exists_with_filesystem<F>(path: &Path, filesystem: &F) -> bool
where
F: ExecutorFileSystem + ?Sized,
{
let Ok(path) = AbsolutePathBuf::from_absolute_path(path) else {
return false;
};
filesystem.get_metadata(&path).await.is_ok()
}
fn resolve_plugin_source_path(
marketplace_path: &AbsolutePathBuf,
source: RawMarketplaceManifestPluginSource,
+3 -1
View File
@@ -13,8 +13,9 @@ mod toggles;
pub(crate) use curated_repo::curated_plugins_repo_path;
pub(crate) use curated_repo::read_curated_plugins_sha;
pub(crate) use curated_repo::read_curated_plugins_sha_with_filesystem;
pub(crate) use curated_repo::sync_openai_plugins_repo;
pub(crate) use discoverable::list_tool_suggest_discoverable_plugins;
pub(crate) use discoverable::list_tool_suggest_discoverable_plugins_with_filesystem;
pub(crate) use injection::build_plugin_injections;
pub use manager::AppConnectorId;
pub use manager::ConfiguredMarketplace;
@@ -41,6 +42,7 @@ pub use manager::plugin_telemetry_metadata_from_root;
pub use manifest::PluginManifestInterface;
pub(crate) use manifest::PluginManifestPaths;
pub(crate) use manifest::load_plugin_manifest;
pub(crate) use manifest::load_plugin_manifest_with_filesystem;
pub use marketplace::MarketplaceError;
pub use marketplace::MarketplacePluginAuthPolicy;
pub use marketplace::MarketplacePluginInstallPolicy;
+185 -1
View File
@@ -1,5 +1,10 @@
use super::load_plugin_manifest;
use super::load_plugin_manifest_with_filesystem;
use super::manifest::PLUGIN_MANIFEST_PATH;
use codex_exec_server::CopyOptions;
use codex_exec_server::CreateDirectoryOptions;
use codex_exec_server::ExecutorFileSystem;
use codex_exec_server::RemoveOptions;
use codex_utils_absolute_path::AbsolutePathBuf;
use std::fs;
use std::io;
@@ -122,10 +127,70 @@ impl PluginStore {
.map(|plugin_version| self.plugin_root(plugin_id, &plugin_version))
}
pub async fn active_plugin_version_with_filesystem<F>(
&self,
plugin_id: &PluginId,
filesystem: &F,
) -> io::Result<Option<String>>
where
F: ExecutorFileSystem + ?Sized,
{
let mut discovered_versions = match filesystem
.read_directory(&self.plugin_base_root(plugin_id))
.await
{
Ok(entries) => entries
.into_iter()
.filter(|entry| entry.is_directory)
.filter_map(|entry| {
validate_plugin_segment(&entry.file_name, "plugin version")
.ok()
.map(|_| entry.file_name)
})
.collect::<Vec<_>>(),
Err(err) if err.kind() == io::ErrorKind::NotFound => return Ok(None),
Err(err) => return Err(err),
};
discovered_versions.sort_unstable();
Ok(if discovered_versions.len() == 1 {
discovered_versions.pop()
} else {
None
})
}
pub async fn active_plugin_root_with_filesystem<F>(
&self,
plugin_id: &PluginId,
filesystem: &F,
) -> io::Result<Option<AbsolutePathBuf>>
where
F: ExecutorFileSystem + ?Sized,
{
Ok(self
.active_plugin_version_with_filesystem(plugin_id, filesystem)
.await?
.map(|plugin_version| self.plugin_root(plugin_id, &plugin_version)))
}
pub fn is_installed(&self, plugin_id: &PluginId) -> bool {
self.active_plugin_version(plugin_id).is_some()
}
pub async fn is_installed_with_filesystem<F>(
&self,
plugin_id: &PluginId,
filesystem: &F,
) -> io::Result<bool>
where
F: ExecutorFileSystem + ?Sized,
{
Ok(self
.active_plugin_version_with_filesystem(plugin_id, filesystem)
.await?
.is_some())
}
pub fn install(
&self,
source_path: AbsolutePathBuf,
@@ -173,6 +238,104 @@ impl PluginStore {
pub fn uninstall(&self, plugin_id: &PluginId) -> Result<(), PluginStoreError> {
remove_existing_target(self.plugin_base_root(plugin_id).as_path())
}
pub async fn install_with_version_with_filesystem<F>(
&self,
source_path: AbsolutePathBuf,
plugin_id: PluginId,
plugin_version: String,
filesystem: &F,
) -> Result<PluginInstallResult, PluginStoreError>
where
F: ExecutorFileSystem + ?Sized,
{
let source_metadata = filesystem
.get_metadata(&source_path)
.await
.map_err(|err| PluginStoreError::io("failed to inspect plugin source path", err))?;
if !source_metadata.is_directory {
return Err(PluginStoreError::Invalid(format!(
"plugin source path is not a directory: {}",
source_path.display()
)));
}
let plugin_name =
plugin_name_for_source_with_filesystem(source_path.as_path(), filesystem).await?;
if plugin_name != plugin_id.plugin_name {
return Err(PluginStoreError::Invalid(format!(
"plugin manifest name `{plugin_name}` does not match marketplace plugin name `{}`",
plugin_id.plugin_name
)));
}
validate_plugin_segment(&plugin_version, "plugin version")
.map_err(PluginStoreError::Invalid)?;
let target_root = self.plugin_base_root(&plugin_id);
let Some(parent) = target_root.as_path().parent() else {
return Err(PluginStoreError::Invalid(format!(
"plugin cache path has no parent: {}",
target_root.display()
)));
};
let parent = AbsolutePathBuf::from_absolute_path(parent).map_err(|err| {
PluginStoreError::Invalid(format!("plugin cache parent should be absolute: {err}"))
})?;
filesystem
.create_directory(&parent, CreateDirectoryOptions { recursive: true })
.await
.map_err(|err| PluginStoreError::io("failed to create plugin cache directory", err))?;
filesystem
.remove(
&target_root,
RemoveOptions {
recursive: true,
force: true,
},
)
.await
.map_err(|err| {
PluginStoreError::io("failed to remove existing plugin cache entry", err)
})?;
let installed_path = self.plugin_root(&plugin_id, &plugin_version);
filesystem
.copy(
&source_path,
&installed_path,
CopyOptions { recursive: true },
)
.await
.map_err(|err| PluginStoreError::io("failed to copy plugin into cache", err))?;
Ok(PluginInstallResult {
plugin_id,
plugin_version,
installed_path,
})
}
pub async fn uninstall_with_filesystem<F>(
&self,
plugin_id: &PluginId,
filesystem: &F,
) -> Result<(), PluginStoreError>
where
F: ExecutorFileSystem + ?Sized,
{
filesystem
.remove(
&self.plugin_base_root(plugin_id),
RemoveOptions {
recursive: true,
force: true,
},
)
.await
.map_err(|err| {
PluginStoreError::io("failed to remove existing plugin cache entry", err)
})
}
}
#[derive(Debug, thiserror::Error)]
@@ -189,7 +352,7 @@ pub enum PluginStoreError {
}
impl PluginStoreError {
fn io(context: &'static str, source: io::Error) -> Self {
pub(crate) fn io(context: &'static str, source: io::Error) -> Self {
Self::Io { context, source }
}
}
@@ -216,6 +379,27 @@ fn plugin_name_for_source(source_path: &Path) -> Result<String, PluginStoreError
.map(|_| plugin_name)
}
async fn plugin_name_for_source_with_filesystem<F>(
source_path: &Path,
filesystem: &F,
) -> Result<String, PluginStoreError>
where
F: ExecutorFileSystem + ?Sized,
{
let manifest_path = source_path.join(PLUGIN_MANIFEST_PATH);
let Some(manifest) = load_plugin_manifest_with_filesystem(source_path, filesystem).await else {
return Err(PluginStoreError::Invalid(format!(
"missing or invalid plugin manifest: {}",
manifest_path.display()
)));
};
let plugin_name = manifest.name;
validate_plugin_segment(&plugin_name, "plugin name")
.map_err(PluginStoreError::Invalid)
.map(|_| plugin_name)
}
fn validate_plugin_segment(segment: &str, kind: &str) -> Result<(), String> {
if segment.is_empty() {
return Err(format!("invalid {kind}: must not be empty"));
+1
View File
@@ -78,6 +78,7 @@ fn render_js_repl_instructions(config: &Config) -> Option<String> {
/// Combines `Config::instructions` and `AGENTS.md` (if present) into a single
/// string of instructions.
#[cfg(test)]
pub(crate) async fn get_user_instructions(config: &Config) -> Option<String> {
let project_docs = read_project_docs(config).await;
-50
View File
@@ -15,7 +15,6 @@ use codex_otel::SessionTelemetry;
use codex_protocol::models::ResponseItem;
use codex_protocol::user_input::UserInput;
use codex_utils_absolute_path::AbsolutePathBuf;
use tokio::fs;
#[derive(Debug, Default)]
pub(crate) struct SkillInjections {
@@ -23,55 +22,6 @@ pub(crate) struct SkillInjections {
pub(crate) warnings: Vec<String>,
}
pub(crate) async fn build_skill_injections(
mentioned_skills: &[SkillMetadata],
otel: Option<&SessionTelemetry>,
analytics_client: &AnalyticsEventsClient,
tracking: TrackEventsContext,
) -> SkillInjections {
if mentioned_skills.is_empty() {
return SkillInjections::default();
}
let mut result = SkillInjections {
items: Vec::with_capacity(mentioned_skills.len()),
warnings: Vec::new(),
};
let mut invocations = Vec::new();
for skill in mentioned_skills {
match fs::read_to_string(&skill.path_to_skills_md).await {
Ok(contents) => {
emit_skill_injected_metric(otel, skill, "ok");
invocations.push(SkillInvocation {
skill_name: skill.name.clone(),
skill_scope: skill.scope,
skill_path: skill.path_to_skills_md.clone(),
invocation_type: InvocationType::Explicit,
});
result.items.push(ResponseItem::from(SkillInstructions {
name: skill.name.clone(),
path: skill.path_to_skills_md.to_string_lossy().into_owned(),
contents,
}));
}
Err(err) => {
emit_skill_injected_metric(otel, skill, "error");
let message = format!(
"Failed to load skill {name} at {path}: {err:#}",
name = skill.name,
path = skill.path_to_skills_md.display()
);
result.warnings.push(message);
}
}
}
analytics_client.track_skill_invocations(tracking, invocations);
result
}
pub(crate) async fn build_skill_injections_with_filesystem<F>(
mentioned_skills: &[SkillMetadata],
filesystem: &F,
+104 -1
View File
@@ -3,6 +3,7 @@ use crate::config_loader::ConfigLayerStackOrdering;
use crate::config_loader::default_project_root_markers;
use crate::config_loader::merge_toml_values;
use crate::config_loader::project_root_markers_from_config;
use crate::plugins::load_plugin_manifest_with_filesystem;
use crate::plugins::plugin_namespace_for_skill_path;
use crate::skills::model::SkillDependencies;
use crate::skills::model::SkillError;
@@ -788,7 +789,14 @@ where
.map_err(SkillParseError::Read)?;
let contents = String::from_utf8_lossy(&contents).to_string();
let loaded_metadata = load_skill_metadata_with_filesystem(path, filesystem).await;
parse_skill_file_from_contents(path, scope, &contents, loaded_metadata)
parse_skill_file_from_contents_with_filesystem(
path,
scope,
&contents,
loaded_metadata,
filesystem,
)
.await
}
fn parse_skill_file_from_contents(
@@ -854,6 +862,71 @@ fn parse_skill_file_from_contents(
})
}
async fn parse_skill_file_from_contents_with_filesystem<F>(
path: &Path,
scope: SkillScope,
contents: &str,
loaded_metadata: LoadedSkillMetadata,
filesystem: &F,
) -> Result<SkillMetadata, SkillParseError>
where
F: ExecutorFileSystem + ?Sized,
{
let frontmatter = extract_frontmatter(contents).ok_or(SkillParseError::MissingFrontmatter)?;
let parsed: SkillFrontmatter =
serde_yaml::from_str(&frontmatter).map_err(SkillParseError::InvalidYaml)?;
let base_name = parsed
.name
.as_deref()
.map(sanitize_single_line)
.filter(|value| !value.is_empty())
.unwrap_or_else(|| default_skill_name(path));
let name = namespaced_skill_name_with_filesystem(path, &base_name, filesystem).await;
let description = parsed
.description
.as_deref()
.map(sanitize_single_line)
.unwrap_or_default();
let short_description = parsed
.metadata
.short_description
.as_deref()
.map(sanitize_single_line)
.filter(|value| !value.is_empty());
let LoadedSkillMetadata {
interface,
dependencies,
policy,
permission_profile,
managed_network_override,
} = loaded_metadata;
validate_len(&name, MAX_NAME_LEN, "name")?;
validate_len(&description, MAX_DESCRIPTION_LEN, "description")?;
if let Some(short_description) = short_description.as_deref() {
validate_len(
short_description,
MAX_SHORT_DESCRIPTION_LEN,
"metadata.short-description",
)?;
}
Ok(SkillMetadata {
name,
description,
short_description,
interface,
dependencies,
policy,
permission_profile,
managed_network_override,
path_to_skills_md: path.to_path_buf(),
scope,
})
}
fn default_skill_name(path: &Path) -> String {
path.parent()
.and_then(Path::file_name)
@@ -869,6 +942,36 @@ fn namespaced_skill_name(path: &Path, base_name: &str) -> String {
.unwrap_or_else(|| base_name.to_string())
}
async fn namespaced_skill_name_with_filesystem<F>(
path: &Path,
base_name: &str,
filesystem: &F,
) -> String
where
F: ExecutorFileSystem + ?Sized,
{
plugin_namespace_for_skill_path_with_filesystem(path, filesystem)
.await
.map(|namespace| format!("{namespace}:{base_name}"))
.unwrap_or_else(|| base_name.to_string())
}
async fn plugin_namespace_for_skill_path_with_filesystem<F>(
path: &Path,
filesystem: &F,
) -> Option<String>
where
F: ExecutorFileSystem + ?Sized,
{
for ancestor in path.ancestors() {
if let Some(manifest) = load_plugin_manifest_with_filesystem(ancestor, filesystem).await {
return Some(manifest.name);
}
}
None
}
fn load_skill_metadata(skill_path: &Path) -> LoadedSkillMetadata {
// Fail open: optional metadata should not block loading SKILL.md.
let Some(skill_dir) = skill_path.parent() else {
+70 -17
View File
@@ -19,6 +19,7 @@ use crate::config_loader::CloudRequirementsLoader;
use crate::config_loader::ConfigLayerStackOrdering;
use crate::config_loader::LoaderOverrides;
use crate::config_loader::load_config_layers_state;
use crate::config_loader::load_config_layers_state_with_filesystem;
use crate::plugins::PluginsManager;
use crate::skills::SkillLoadOutcome;
use crate::skills::build_implicit_skill_path_indexes;
@@ -29,13 +30,14 @@ use crate::skills::loader::skill_roots;
use crate::skills::loader::skill_roots_with_filesystem;
use crate::skills::system::install_system_skills;
use crate::skills::system::uninstall_system_skills;
use codex_exec_server::Environment;
use codex_exec_server::ExecutorFileSystem;
pub struct SkillsManager {
codex_home: PathBuf,
plugins_manager: Arc<PluginsManager>,
restriction_product: Option<Product>,
cache_by_cwd: RwLock<HashMap<PathBuf, SkillLoadOutcome>>,
cache_by_cwd: RwLock<HashMap<CwdSkillsCacheKey, SkillLoadOutcome>>,
cache_by_config: RwLock<HashMap<ConfigSkillsCacheKey, SkillLoadOutcome>>,
}
@@ -84,7 +86,12 @@ impl SkillsManager {
/// to share a directory.
pub fn skills_for_config(&self, config: &Config) -> SkillLoadOutcome {
let roots = self.skill_roots_for_config(config);
let cache_key = config_skills_cache_key(&roots, &config.config_layer_stack);
let local_environment_id = Environment::default_environment_id(None);
let cache_key = config_skills_cache_key(
Some(local_environment_id.as_str()),
&roots,
&config.config_layer_stack,
);
if let Some(outcome) = self.cached_outcome_for_config(&cache_key) {
return outcome;
}
@@ -104,6 +111,7 @@ impl SkillsManager {
pub async fn skills_for_config_with_filesystem<F>(
&self,
config: &Config,
environment_id: &str,
filesystem: &F,
) -> SkillLoadOutcome
where
@@ -112,7 +120,8 @@ impl SkillsManager {
let roots = self
.skill_roots_for_config_with_filesystem(config, filesystem)
.await;
let cache_key = config_skills_cache_key(&roots, &config.config_layer_stack);
let cache_key =
config_skills_cache_key(Some(environment_id), &roots, &config.config_layer_stack);
if let Some(outcome) = self.cached_outcome_for_config(&cache_key) {
return outcome;
}
@@ -140,7 +149,10 @@ impl SkillsManager {
where
F: ExecutorFileSystem + ?Sized,
{
let loaded_plugins = self.plugins_manager.plugins_for_config(config);
let loaded_plugins = self
.plugins_manager
.plugins_for_config_with_filesystem(config, /*force_reload*/ false, filesystem)
.await;
let mut roots = skill_roots_with_filesystem(
&config.config_layer_stack,
&config.cwd,
@@ -173,10 +185,6 @@ impl SkillsManager {
config: &Config,
force_reload: bool,
) -> SkillLoadOutcome {
if !force_reload && let Some(outcome) = self.cached_outcome_for_cwd(cwd) {
return outcome;
}
self.skills_for_cwd_with_extra_user_roots(cwd, config, force_reload, &[])
.await
}
@@ -186,6 +194,7 @@ impl SkillsManager {
cwd: &Path,
config: &Config,
force_reload: bool,
environment_id: &str,
filesystem: &F,
) -> SkillLoadOutcome
where
@@ -196,6 +205,7 @@ impl SkillsManager {
config,
force_reload,
&[],
environment_id,
filesystem,
)
.await
@@ -208,7 +218,11 @@ impl SkillsManager {
force_reload: bool,
extra_user_roots: &[PathBuf],
) -> SkillLoadOutcome {
if !force_reload && let Some(outcome) = self.cached_outcome_for_cwd(cwd) {
let cache_key = CwdSkillsCacheKey {
environment_id: Some(Environment::default_environment_id(None)),
cwd: cwd.to_path_buf(),
};
if !force_reload && let Some(outcome) = self.cached_outcome_for_cwd(&cache_key) {
return outcome;
}
let normalized_extra_user_roots = normalize_extra_user_roots(extra_user_roots);
@@ -273,7 +287,7 @@ impl SkillsManager {
.cache_by_cwd
.write()
.unwrap_or_else(std::sync::PoisonError::into_inner);
cache.insert(cwd.to_path_buf(), outcome.clone());
cache.insert(cache_key, outcome.clone());
outcome
}
@@ -283,12 +297,17 @@ impl SkillsManager {
config: &Config,
force_reload: bool,
extra_user_roots: &[PathBuf],
environment_id: &str,
filesystem: &F,
) -> SkillLoadOutcome
where
F: ExecutorFileSystem + ?Sized,
{
if !force_reload && let Some(outcome) = self.cached_outcome_for_cwd(cwd) {
let cache_key = CwdSkillsCacheKey {
environment_id: Some(environment_id.to_string()),
cwd: cwd.to_path_buf(),
};
if !force_reload && let Some(outcome) = self.cached_outcome_for_cwd(&cache_key) {
return outcome;
}
let normalized_extra_user_roots = normalize_extra_user_roots(extra_user_roots);
@@ -307,12 +326,13 @@ impl SkillsManager {
};
let cli_overrides: Vec<(String, TomlValue)> = Vec::new();
let config_layer_stack = match load_config_layers_state(
let config_layer_stack = match load_config_layers_state_with_filesystem(
&self.codex_home,
Some(cwd_abs),
&cli_overrides,
LoaderOverrides::default(),
CloudRequirementsLoader::default(),
filesystem,
)
.await
{
@@ -330,7 +350,8 @@ impl SkillsManager {
let loaded_plugins = self
.plugins_manager
.plugins_for_config_with_force_reload(config, force_reload);
.plugins_for_config_with_filesystem(config, force_reload, filesystem)
.await;
let mut roots = skill_roots_with_filesystem(
&config_layer_stack,
cwd,
@@ -361,7 +382,7 @@ impl SkillsManager {
.cache_by_cwd
.write()
.unwrap_or_else(std::sync::PoisonError::into_inner);
cache.insert(cwd.to_path_buf(), outcome.clone());
cache.insert(cache_key, outcome.clone());
outcome
}
@@ -399,10 +420,33 @@ impl SkillsManager {
info!("skills cache cleared ({cleared} entries)");
}
fn cached_outcome_for_cwd(&self, cwd: &Path) -> Option<SkillLoadOutcome> {
pub fn clear_cache_for_environment(&self, environment_id: &str) {
let cleared_cwd = {
let mut cache = self
.cache_by_cwd
.write()
.unwrap_or_else(std::sync::PoisonError::into_inner);
let before = cache.len();
cache.retain(|key, _| key.environment_id.as_deref() != Some(environment_id));
before.saturating_sub(cache.len())
};
let cleared_config = {
let mut cache = self
.cache_by_config
.write()
.unwrap_or_else(std::sync::PoisonError::into_inner);
let before = cache.len();
cache.retain(|key, _| key.environment_id.as_deref() != Some(environment_id));
before.saturating_sub(cache.len())
};
let cleared = cleared_cwd + cleared_config;
info!("skills cache cleared for environment `{environment_id}` ({cleared} entries)");
}
fn cached_outcome_for_cwd(&self, cache_key: &CwdSkillsCacheKey) -> Option<SkillLoadOutcome> {
match self.cache_by_cwd.read() {
Ok(cache) => cache.get(cwd).cloned(),
Err(err) => err.into_inner().get(cwd).cloned(),
Ok(cache) => cache.get(cache_key).cloned(),
Err(err) => err.into_inner().get(cache_key).cloned(),
}
}
@@ -417,8 +461,15 @@ impl SkillsManager {
}
}
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
struct CwdSkillsCacheKey {
environment_id: Option<String>,
cwd: PathBuf,
}
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
struct ConfigSkillsCacheKey {
environment_id: Option<String>,
roots: Vec<(PathBuf, u8)>,
disabled_paths: Vec<PathBuf>,
}
@@ -484,6 +535,7 @@ fn disabled_paths_from_stack(
}
fn config_skills_cache_key(
environment_id: Option<&str>,
roots: &[SkillRoot],
config_layer_stack: &crate::config_loader::ConfigLayerStack,
) -> ConfigSkillsCacheKey {
@@ -493,6 +545,7 @@ fn config_skills_cache_key(
disabled_paths.sort_unstable();
ConfigSkillsCacheKey {
environment_id: environment_id.map(str::to_owned),
roots: roots
.iter()
.map(|root| {
@@ -413,6 +413,8 @@ enabled = true
crate::config::AgentRoleConfig {
description: None,
config_file: Some(role_path),
locked_model: None,
locked_reasoning_effort: None,
nickname_candidates: None,
},
);
+1 -1
View File
@@ -11,7 +11,7 @@ pub mod system;
pub(crate) use env_var_dependencies::collect_env_var_dependencies;
pub(crate) use env_var_dependencies::resolve_skill_dependencies_for_turn;
pub(crate) use injection::SkillInjections;
pub(crate) use injection::build_skill_injections;
pub(crate) use injection::build_skill_injections_with_filesystem;
pub(crate) use injection::collect_explicit_skill_mentions;
pub(crate) use invocation_utils::build_implicit_skill_path_indexes;
pub(crate) use invocation_utils::maybe_emit_implicit_skill_invocation;
+12 -3
View File
@@ -24,6 +24,7 @@ use crate::rollout::RolloutRecorder;
use crate::rollout::truncation;
use crate::shell_snapshot::ShellSnapshot;
use crate::skills::SkillsManager;
use codex_exec_server::Environment;
use codex_protocol::ThreadId;
use codex_protocol::config_types::CollaborationModeMask;
use codex_protocol::openai_models::ModelPreset;
@@ -101,8 +102,8 @@ fn build_file_watcher(codex_home: PathBuf, skills_manager: Arc<SkillsManager>) -
handle.spawn(async move {
loop {
match rx.recv().await {
Ok(FileWatcherEvent::SkillsChanged { .. }) => {
skills_manager.clear_cache();
Ok(FileWatcherEvent::SkillsChanged { environment_id, .. }) => {
skills_manager.clear_cache_for_environment(&environment_id);
}
Err(broadcast::error::RecvError::Closed) => break,
Err(broadcast::error::RecvError::Lagged(_)) => continue,
@@ -759,9 +760,17 @@ impl ThreadManagerState {
parent_trace: Option<W3cTraceContext>,
user_shell_override: Option<crate::shell::Shell>,
) -> CodexResult<NewThread> {
let environment = Environment::create(config.experimental_exec_server_url.clone()).await?;
let environment_filesystem = environment.get_filesystem();
let watch_registration = self
.file_watcher
.register_config(&config, self.skills_manager.as_ref());
.register_config_with_filesystem(
&config,
environment.environment_id().to_string(),
self.skills_manager.as_ref(),
&environment_filesystem,
)
.await;
let CodexSpawnOk {
codex, thread_id, ..
} = Codex::spawn(CodexSpawnArgs {
@@ -1,7 +1,7 @@
use super::*;
use crate::agent::control::SpawnAgentOptions;
use crate::agent::role::DEFAULT_ROLE_NAME;
use crate::agent::role::apply_role_to_config;
use crate::agent::role::apply_role_to_config_with_filesystem;
use crate::agent::exceeds_thread_spawn_depth_limit;
use crate::agent::next_thread_spawn_depth;
@@ -68,7 +68,8 @@ impl ToolHandler for Handler {
args.reasoning_effort,
)
.await?;
apply_role_to_config(&mut config, role_name)
let environment_filesystem = turn.environment.get_filesystem();
apply_role_to_config_with_filesystem(&mut config, role_name, &environment_filesystem)
.await
.map_err(FunctionCallError::RespondToModel)?;
apply_spawn_agent_runtime_overrides(&mut config, turn.as_ref())?;
@@ -118,23 +118,26 @@ impl ToolHandler for ToolSuggestHandler {
connectors::accessible_connectors_from_mcp_tools(&mcp_tools),
&turn.config,
);
let discoverable_tools = connectors::list_tool_suggest_discoverable_tools_with_auth(
&turn.config,
auth.as_ref(),
&accessible_connectors,
)
.await
.map(|discoverable_tools| {
filter_tool_suggest_discoverable_tools_for_client(
discoverable_tools,
turn.app_server_client_name.as_deref(),
let environment_filesystem = turn.environment.get_filesystem();
let discoverable_tools =
connectors::list_tool_suggest_discoverable_tools_with_auth_with_filesystem(
&turn.config,
auth.as_ref(),
&accessible_connectors,
&environment_filesystem,
)
})
.map_err(|err| {
FunctionCallError::RespondToModel(format!(
"tool suggestions are unavailable right now: {err}"
))
})?;
.await
.map(|discoverable_tools| {
filter_tool_suggest_discoverable_tools_for_client(
discoverable_tools,
turn.app_server_client_name.as_deref(),
)
})
.map_err(|err| {
FunctionCallError::RespondToModel(format!(
"tool suggestions are unavailable right now: {err}"
))
})?;
let tool = discoverable_tools
.into_iter()
@@ -494,6 +494,7 @@ impl CoreShellActionProvider {
&self.turn.cwd,
self.turn.config.as_ref(),
force_reload,
self.turn.environment.environment_id(),
&environment_filesystem,
)
.await;
@@ -460,6 +460,8 @@ async fn spawn_agent_role_overrides_requested_model_and_reasoning_settings() ->
AgentRoleConfig {
description: Some("Custom role".to_string()),
config_file: Some(role_path),
locked_model: None,
locked_reasoning_effort: None,
nickname_candidates: None,
},
);
@@ -508,6 +510,8 @@ async fn spawn_agent_tool_description_mentions_role_locked_settings() -> Result<
AgentRoleConfig {
description: Some("Custom role".to_string()),
config_file: Some(role_path),
locked_model: Some(ROLE_MODEL.to_string()),
locked_reasoning_effort: Some(ROLE_REASONING_EFFORT.to_string()),
nickname_candidates: None,
},
);
+2 -12
View File
@@ -229,20 +229,10 @@ impl ExecServerClient {
.map_err(Into::into)
}
pub async fn write(
&self,
process_id: &str,
chunk: Vec<u8>,
) -> Result<WriteResponse, ExecServerError> {
pub async fn write(&self, params: WriteParams) -> Result<WriteResponse, ExecServerError> {
self.inner
.client
.call(
EXEC_WRITE_METHOD,
&WriteParams {
process_id: process_id.to_string(),
chunk: chunk.into(),
},
)
.call(EXEC_WRITE_METHOD, &params)
.await
.map_err(Into::into)
}
+13 -2
View File
@@ -90,7 +90,10 @@ impl ExecutorFileSystem for RemoteEnvironmentFileSystem {
async fn read_file(&self, path: &AbsolutePathBuf) -> crate::fs::FileSystemResult<Vec<u8>> {
let response = self
.client
.fs_read_file(codex_app_server_protocol::FsReadFileParams { path: path.clone() })
.fs_read_file(codex_app_server_protocol::FsReadFileParams {
environment_id: None,
path: path.clone(),
})
.await
.map_err(exec_server_error_to_io)?;
base64::engine::general_purpose::STANDARD
@@ -105,6 +108,7 @@ impl ExecutorFileSystem for RemoteEnvironmentFileSystem {
) -> crate::fs::FileSystemResult<()> {
self.client
.fs_write_file(codex_app_server_protocol::FsWriteFileParams {
environment_id: None,
path: path.clone(),
data_base64: base64::engine::general_purpose::STANDARD.encode(contents),
})
@@ -120,6 +124,7 @@ impl ExecutorFileSystem for RemoteEnvironmentFileSystem {
) -> crate::fs::FileSystemResult<()> {
self.client
.fs_create_directory(codex_app_server_protocol::FsCreateDirectoryParams {
environment_id: None,
path: path.clone(),
recursive: Some(options.recursive),
})
@@ -134,7 +139,10 @@ impl ExecutorFileSystem for RemoteEnvironmentFileSystem {
) -> crate::fs::FileSystemResult<crate::fs::FileMetadata> {
let response = self
.client
.fs_get_metadata(codex_app_server_protocol::FsGetMetadataParams { path: path.clone() })
.fs_get_metadata(codex_app_server_protocol::FsGetMetadataParams {
environment_id: None,
path: path.clone(),
})
.await
.map_err(exec_server_error_to_io)?;
Ok(crate::fs::FileMetadata {
@@ -152,6 +160,7 @@ impl ExecutorFileSystem for RemoteEnvironmentFileSystem {
let response = self
.client
.fs_read_directory(codex_app_server_protocol::FsReadDirectoryParams {
environment_id: None,
path: path.clone(),
})
.await
@@ -174,6 +183,7 @@ impl ExecutorFileSystem for RemoteEnvironmentFileSystem {
) -> crate::fs::FileSystemResult<()> {
self.client
.fs_remove(codex_app_server_protocol::FsRemoveParams {
environment_id: None,
path: path.clone(),
recursive: Some(options.recursive),
force: Some(options.force),
@@ -191,6 +201,7 @@ impl ExecutorFileSystem for RemoteEnvironmentFileSystem {
) -> crate::fs::FileSystemResult<()> {
self.client
.fs_copy(codex_app_server_protocol::FsCopyParams {
environment_id: None,
source_path: source_path.clone(),
destination_path: destination_path.clone(),
recursive: options.recursive,

Some files were not shown because too many files have changed in this diff Show More