From b06fc8bd0d2cf3c090aaaf90420a41d7f2943954 Mon Sep 17 00:00:00 2001 From: Michael Bolin Date: Tue, 21 Apr 2026 09:48:31 -0700 Subject: [PATCH 01/22] core: make test-log a dev dependency (#18846) The `test-log` crate is only used by `codex-core` tests, so it does not need to be part of the normal `codex-core` dependency graph. Keeping `test-log` in `dev-dependencies` removes it from normal `codex-core` builds and keeps the production dependency set a little smaller. Verification: - `cargo tree -p codex-core --edges normal --invert test-log` - `cargo check -p codex-core --lib` - `cargo test -p codex-core --lib` --- codex-rs/core/Cargo.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/codex-rs/core/Cargo.toml b/codex-rs/core/Cargo.toml index 6ef6741fd9..d1cca0ac32 100644 --- a/codex-rs/core/Cargo.toml +++ b/codex-rs/core/Cargo.toml @@ -104,7 +104,6 @@ sha2 = { workspace = true } shlex = { workspace = true } similar = { workspace = true } tempfile = { workspace = true } -test-log = { workspace = true } thiserror = { workspace = true } tokio = { workspace = true, features = [ "io-std", @@ -166,6 +165,7 @@ opentelemetry_sdk = { workspace = true, features = [ ] } serial_test = { workspace = true } tempfile = { workspace = true } +test-log = { workspace = true } tracing-opentelemetry = { workspace = true } tracing-subscriber = { workspace = true } tracing-test = { workspace = true, features = ["no-env-filter"] } From 48f82ca7c5f9a039b44ccf71765e1b2d4fb190f5 Mon Sep 17 00:00:00 2001 From: Ruslan Nigmatullin Date: Tue, 21 Apr 2026 10:08:42 -0700 Subject: [PATCH 02/22] app-server: define device key v2 protocol (#18428) ## Why Clients need a stable app-server protocol surface for enrolling a local device key, retrieving its public key, and producing a device-bound proof. The protocol reports `protectionClass` explicitly so clients can distinguish hardware-backed keys from an explicitly allowed OS-protected fallback. Signing uses a tagged `DeviceKeySignPayload` enum rather than arbitrary bytes so each signed statement is auditable at the API boundary. ## What changed - Added v2 JSON-RPC methods for `device/key/create`, `device/key/public`, and `device/key/sign`. - Added request/response types for device-key metadata, SPKI public keys, protection classes, and ECDSA signatures. - Added `DeviceKeyProtectionPolicy` with hardware-only default behavior and an explicit `allow_os_protected_nonextractable` option. - Added the initial `remoteControlClientConnection` signing payload variant. - Regenerated JSON Schema and TypeScript fixtures for app-server clients. ## Stack This is PR 1 of 4 in the device-key app-server stack. ## Validation - `just write-app-server-schema` - `cargo test -p codex-app-server-protocol` --- .../schema/json/ClientRequest.json | 280 +++++++++++++ .../schema/json/DynamicToolCallParams.json | 2 +- .../codex_app_server_protocol.schemas.json | 386 +++++++++++++++++- .../codex_app_server_protocol.v2.schemas.json | 380 +++++++++++++++++ .../schema/json/v2/DeviceKeyCreateParams.json | 39 ++ .../json/v2/DeviceKeyCreateResponse.json | 45 ++ .../schema/json/v2/DeviceKeyPublicParams.json | 14 + .../json/v2/DeviceKeyPublicResponse.json | 45 ++ .../schema/json/v2/DeviceKeySignParams.json | 165 ++++++++ .../schema/json/v2/DeviceKeySignResponse.json | 33 ++ .../schema/typescript/ClientRequest.ts | 5 +- .../typescript/v2/DeviceKeyAlgorithm.ts | 8 + .../typescript/v2/DeviceKeyCreateParams.ts | 13 + .../typescript/v2/DeviceKeyCreateResponse.ts | 14 + .../typescript/v2/DeviceKeyProtectionClass.ts | 8 + .../v2/DeviceKeyProtectionPolicy.ts | 8 + .../typescript/v2/DeviceKeyPublicParams.ts | 8 + .../typescript/v2/DeviceKeyPublicResponse.ts | 14 + .../typescript/v2/DeviceKeySignParams.ts | 9 + .../typescript/v2/DeviceKeySignPayload.ts | 54 +++ .../typescript/v2/DeviceKeySignResponse.ts | 18 + .../RemoteControlClientConnectionAudience.ts | 8 + .../RemoteControlClientEnrollmentAudience.ts | 8 + .../schema/typescript/v2/index.ts | 12 + .../src/protocol/common.rs | 12 + .../app-server-protocol/src/protocol/v2.rs | 333 +++++++++++++++ .../app-server/src/codex_message_processor.rs | 5 + 27 files changed, 1921 insertions(+), 5 deletions(-) create mode 100644 codex-rs/app-server-protocol/schema/json/v2/DeviceKeyCreateParams.json create mode 100644 codex-rs/app-server-protocol/schema/json/v2/DeviceKeyCreateResponse.json create mode 100644 codex-rs/app-server-protocol/schema/json/v2/DeviceKeyPublicParams.json create mode 100644 codex-rs/app-server-protocol/schema/json/v2/DeviceKeyPublicResponse.json create mode 100644 codex-rs/app-server-protocol/schema/json/v2/DeviceKeySignParams.json create mode 100644 codex-rs/app-server-protocol/schema/json/v2/DeviceKeySignResponse.json create mode 100644 codex-rs/app-server-protocol/schema/typescript/v2/DeviceKeyAlgorithm.ts create mode 100644 codex-rs/app-server-protocol/schema/typescript/v2/DeviceKeyCreateParams.ts create mode 100644 codex-rs/app-server-protocol/schema/typescript/v2/DeviceKeyCreateResponse.ts create mode 100644 codex-rs/app-server-protocol/schema/typescript/v2/DeviceKeyProtectionClass.ts create mode 100644 codex-rs/app-server-protocol/schema/typescript/v2/DeviceKeyProtectionPolicy.ts create mode 100644 codex-rs/app-server-protocol/schema/typescript/v2/DeviceKeyPublicParams.ts create mode 100644 codex-rs/app-server-protocol/schema/typescript/v2/DeviceKeyPublicResponse.ts create mode 100644 codex-rs/app-server-protocol/schema/typescript/v2/DeviceKeySignParams.ts create mode 100644 codex-rs/app-server-protocol/schema/typescript/v2/DeviceKeySignPayload.ts create mode 100644 codex-rs/app-server-protocol/schema/typescript/v2/DeviceKeySignResponse.ts create mode 100644 codex-rs/app-server-protocol/schema/typescript/v2/RemoteControlClientConnectionAudience.ts create mode 100644 codex-rs/app-server-protocol/schema/typescript/v2/RemoteControlClientEnrollmentAudience.ts diff --git a/codex-rs/app-server-protocol/schema/json/ClientRequest.json b/codex-rs/app-server-protocol/schema/json/ClientRequest.json index 43871d74a0..352cf02113 100644 --- a/codex-rs/app-server-protocol/schema/json/ClientRequest.json +++ b/codex-rs/app-server-protocol/schema/json/ClientRequest.json @@ -521,6 +521,200 @@ } ] }, + "DeviceKeyCreateParams": { + "description": "Create a controller-local device key with a random key id.", + "properties": { + "accountUserId": { + "type": "string" + }, + "clientId": { + "type": "string" + }, + "protectionPolicy": { + "anyOf": [ + { + "$ref": "#/definitions/DeviceKeyProtectionPolicy" + }, + { + "type": "null" + } + ], + "description": "Defaults to `hardware_only` when omitted." + } + }, + "required": [ + "accountUserId", + "clientId" + ], + "type": "object" + }, + "DeviceKeyProtectionPolicy": { + "description": "Protection policy for creating or loading a controller-local device key.", + "enum": [ + "hardware_only", + "allow_os_protected_nonextractable" + ], + "type": "string" + }, + "DeviceKeyPublicParams": { + "description": "Fetch a controller-local device key public key by id.", + "properties": { + "keyId": { + "type": "string" + } + }, + "required": [ + "keyId" + ], + "type": "object" + }, + "DeviceKeySignParams": { + "description": "Sign an accepted structured payload with a controller-local device key.", + "properties": { + "keyId": { + "type": "string" + }, + "payload": { + "$ref": "#/definitions/DeviceKeySignPayload" + } + }, + "required": [ + "keyId", + "payload" + ], + "type": "object" + }, + "DeviceKeySignPayload": { + "description": "Structured payloads accepted by `device/key/sign`.", + "oneOf": [ + { + "description": "Payload bound to one remote-control controller websocket `/client` connection challenge.", + "properties": { + "accountUserId": { + "type": "string" + }, + "audience": { + "$ref": "#/definitions/RemoteControlClientConnectionAudience" + }, + "clientId": { + "type": "string" + }, + "nonce": { + "type": "string" + }, + "scopes": { + "description": "Must contain exactly `remote_control_controller_websocket`.", + "items": { + "type": "string" + }, + "type": "array" + }, + "sessionId": { + "description": "Backend-issued websocket session id that this proof authorizes.", + "type": "string" + }, + "targetOrigin": { + "description": "Origin of the backend endpoint that issued the challenge and will verify this proof.", + "type": "string" + }, + "targetPath": { + "description": "Websocket route path that this proof authorizes.", + "type": "string" + }, + "tokenExpiresAt": { + "description": "Remote-control token expiration as Unix seconds.", + "format": "int64", + "type": "integer" + }, + "tokenSha256Base64url": { + "description": "SHA-256 of the controller-scoped remote-control token, encoded as unpadded base64url.", + "type": "string" + }, + "type": { + "enum": [ + "remoteControlClientConnection" + ], + "title": "RemoteControlClientConnectionDeviceKeySignPayloadType", + "type": "string" + } + }, + "required": [ + "accountUserId", + "audience", + "clientId", + "nonce", + "scopes", + "sessionId", + "targetOrigin", + "targetPath", + "tokenExpiresAt", + "tokenSha256Base64url", + "type" + ], + "title": "RemoteControlClientConnectionDeviceKeySignPayload", + "type": "object" + }, + { + "description": "Payload bound to a remote-control client `/client/enroll` ownership challenge.", + "properties": { + "accountUserId": { + "type": "string" + }, + "audience": { + "$ref": "#/definitions/RemoteControlClientEnrollmentAudience" + }, + "challengeExpiresAt": { + "description": "Enrollment challenge expiration as Unix seconds.", + "format": "int64", + "type": "integer" + }, + "challengeId": { + "description": "Backend-issued enrollment challenge id that this proof authorizes.", + "type": "string" + }, + "clientId": { + "type": "string" + }, + "deviceIdentitySha256Base64url": { + "description": "SHA-256 of the requested device identity operation, encoded as unpadded base64url.", + "type": "string" + }, + "nonce": { + "type": "string" + }, + "targetOrigin": { + "description": "Origin of the backend endpoint that issued the challenge and will verify this proof.", + "type": "string" + }, + "targetPath": { + "description": "HTTP route path that this proof authorizes.", + "type": "string" + }, + "type": { + "enum": [ + "remoteControlClientEnrollment" + ], + "title": "RemoteControlClientEnrollmentDeviceKeySignPayloadType", + "type": "string" + } + }, + "required": [ + "accountUserId", + "audience", + "challengeExpiresAt", + "challengeId", + "clientId", + "deviceIdentitySha256Base64url", + "nonce", + "targetOrigin", + "targetPath", + "type" + ], + "title": "RemoteControlClientEnrollmentDeviceKeySignPayload", + "type": "object" + } + ] + }, "DynamicToolSpec": { "properties": { "deferLoading": { @@ -1747,6 +1941,20 @@ } ] }, + "RemoteControlClientConnectionAudience": { + "description": "Audience for a remote-control client connection device-key proof.", + "enum": [ + "remote_control_client_websocket" + ], + "type": "string" + }, + "RemoteControlClientEnrollmentAudience": { + "description": "Audience for a remote-control client enrollment device-key proof.", + "enum": [ + "remote_control_client_enrollment" + ], + "type": "string" + }, "RequestId": { "anyOf": [ { @@ -4360,6 +4568,78 @@ "title": "App/listRequest", "type": "object" }, + { + "properties": { + "id": { + "$ref": "#/definitions/RequestId" + }, + "method": { + "enum": [ + "device/key/create" + ], + "title": "Device/key/createRequestMethod", + "type": "string" + }, + "params": { + "$ref": "#/definitions/DeviceKeyCreateParams" + } + }, + "required": [ + "id", + "method", + "params" + ], + "title": "Device/key/createRequest", + "type": "object" + }, + { + "properties": { + "id": { + "$ref": "#/definitions/RequestId" + }, + "method": { + "enum": [ + "device/key/public" + ], + "title": "Device/key/publicRequestMethod", + "type": "string" + }, + "params": { + "$ref": "#/definitions/DeviceKeyPublicParams" + } + }, + "required": [ + "id", + "method", + "params" + ], + "title": "Device/key/publicRequest", + "type": "object" + }, + { + "properties": { + "id": { + "$ref": "#/definitions/RequestId" + }, + "method": { + "enum": [ + "device/key/sign" + ], + "title": "Device/key/signRequestMethod", + "type": "string" + }, + "params": { + "$ref": "#/definitions/DeviceKeySignParams" + } + }, + "required": [ + "id", + "method", + "params" + ], + "title": "Device/key/signRequest", + "type": "object" + }, { "properties": { "id": { diff --git a/codex-rs/app-server-protocol/schema/json/DynamicToolCallParams.json b/codex-rs/app-server-protocol/schema/json/DynamicToolCallParams.json index 0aa8b9c571..991733da66 100644 --- a/codex-rs/app-server-protocol/schema/json/DynamicToolCallParams.json +++ b/codex-rs/app-server-protocol/schema/json/DynamicToolCallParams.json @@ -30,4 +30,4 @@ ], "title": "DynamicToolCallParams", "type": "object" -} +} \ No newline at end of file diff --git a/codex-rs/app-server-protocol/schema/json/codex_app_server_protocol.schemas.json b/codex-rs/app-server-protocol/schema/json/codex_app_server_protocol.schemas.json index 2cb5fb84df..630e8d4934 100644 --- a/codex-rs/app-server-protocol/schema/json/codex_app_server_protocol.schemas.json +++ b/codex-rs/app-server-protocol/schema/json/codex_app_server_protocol.schemas.json @@ -737,6 +737,78 @@ "title": "App/listRequest", "type": "object" }, + { + "properties": { + "id": { + "$ref": "#/definitions/v2/RequestId" + }, + "method": { + "enum": [ + "device/key/create" + ], + "title": "Device/key/createRequestMethod", + "type": "string" + }, + "params": { + "$ref": "#/definitions/v2/DeviceKeyCreateParams" + } + }, + "required": [ + "id", + "method", + "params" + ], + "title": "Device/key/createRequest", + "type": "object" + }, + { + "properties": { + "id": { + "$ref": "#/definitions/v2/RequestId" + }, + "method": { + "enum": [ + "device/key/public" + ], + "title": "Device/key/publicRequestMethod", + "type": "string" + }, + "params": { + "$ref": "#/definitions/v2/DeviceKeyPublicParams" + } + }, + "required": [ + "id", + "method", + "params" + ], + "title": "Device/key/publicRequest", + "type": "object" + }, + { + "properties": { + "id": { + "$ref": "#/definitions/v2/RequestId" + }, + "method": { + "enum": [ + "device/key/sign" + ], + "title": "Device/key/signRequestMethod", + "type": "string" + }, + "params": { + "$ref": "#/definitions/v2/DeviceKeySignParams" + } + }, + "required": [ + "id", + "method", + "params" + ], + "title": "Device/key/signRequest", + "type": "object" + }, { "properties": { "id": { @@ -7324,6 +7396,300 @@ "title": "DeprecationNoticeNotification", "type": "object" }, + "DeviceKeyAlgorithm": { + "description": "Device-key algorithm reported at enrollment and signing boundaries.", + "enum": [ + "ecdsa_p256_sha256" + ], + "type": "string" + }, + "DeviceKeyCreateParams": { + "$schema": "http://json-schema.org/draft-07/schema#", + "description": "Create a controller-local device key with a random key id.", + "properties": { + "accountUserId": { + "type": "string" + }, + "clientId": { + "type": "string" + }, + "protectionPolicy": { + "anyOf": [ + { + "$ref": "#/definitions/v2/DeviceKeyProtectionPolicy" + }, + { + "type": "null" + } + ], + "description": "Defaults to `hardware_only` when omitted." + } + }, + "required": [ + "accountUserId", + "clientId" + ], + "title": "DeviceKeyCreateParams", + "type": "object" + }, + "DeviceKeyCreateResponse": { + "$schema": "http://json-schema.org/draft-07/schema#", + "description": "Device-key metadata and public key returned by create/public APIs.", + "properties": { + "algorithm": { + "$ref": "#/definitions/v2/DeviceKeyAlgorithm" + }, + "keyId": { + "type": "string" + }, + "protectionClass": { + "$ref": "#/definitions/v2/DeviceKeyProtectionClass" + }, + "publicKeySpkiDerBase64": { + "description": "SubjectPublicKeyInfo DER encoded as base64.", + "type": "string" + } + }, + "required": [ + "algorithm", + "keyId", + "protectionClass", + "publicKeySpkiDerBase64" + ], + "title": "DeviceKeyCreateResponse", + "type": "object" + }, + "DeviceKeyProtectionClass": { + "description": "Platform protection class for a controller-local device key.", + "enum": [ + "hardware_secure_enclave", + "hardware_tpm", + "os_protected_nonextractable" + ], + "type": "string" + }, + "DeviceKeyProtectionPolicy": { + "description": "Protection policy for creating or loading a controller-local device key.", + "enum": [ + "hardware_only", + "allow_os_protected_nonextractable" + ], + "type": "string" + }, + "DeviceKeyPublicParams": { + "$schema": "http://json-schema.org/draft-07/schema#", + "description": "Fetch a controller-local device key public key by id.", + "properties": { + "keyId": { + "type": "string" + } + }, + "required": [ + "keyId" + ], + "title": "DeviceKeyPublicParams", + "type": "object" + }, + "DeviceKeyPublicResponse": { + "$schema": "http://json-schema.org/draft-07/schema#", + "description": "Device-key public metadata returned by `device/key/public`.", + "properties": { + "algorithm": { + "$ref": "#/definitions/v2/DeviceKeyAlgorithm" + }, + "keyId": { + "type": "string" + }, + "protectionClass": { + "$ref": "#/definitions/v2/DeviceKeyProtectionClass" + }, + "publicKeySpkiDerBase64": { + "description": "SubjectPublicKeyInfo DER encoded as base64.", + "type": "string" + } + }, + "required": [ + "algorithm", + "keyId", + "protectionClass", + "publicKeySpkiDerBase64" + ], + "title": "DeviceKeyPublicResponse", + "type": "object" + }, + "DeviceKeySignParams": { + "$schema": "http://json-schema.org/draft-07/schema#", + "description": "Sign an accepted structured payload with a controller-local device key.", + "properties": { + "keyId": { + "type": "string" + }, + "payload": { + "$ref": "#/definitions/v2/DeviceKeySignPayload" + } + }, + "required": [ + "keyId", + "payload" + ], + "title": "DeviceKeySignParams", + "type": "object" + }, + "DeviceKeySignPayload": { + "description": "Structured payloads accepted by `device/key/sign`.", + "oneOf": [ + { + "description": "Payload bound to one remote-control controller websocket `/client` connection challenge.", + "properties": { + "accountUserId": { + "type": "string" + }, + "audience": { + "$ref": "#/definitions/v2/RemoteControlClientConnectionAudience" + }, + "clientId": { + "type": "string" + }, + "nonce": { + "type": "string" + }, + "scopes": { + "description": "Must contain exactly `remote_control_controller_websocket`.", + "items": { + "type": "string" + }, + "type": "array" + }, + "sessionId": { + "description": "Backend-issued websocket session id that this proof authorizes.", + "type": "string" + }, + "targetOrigin": { + "description": "Origin of the backend endpoint that issued the challenge and will verify this proof.", + "type": "string" + }, + "targetPath": { + "description": "Websocket route path that this proof authorizes.", + "type": "string" + }, + "tokenExpiresAt": { + "description": "Remote-control token expiration as Unix seconds.", + "format": "int64", + "type": "integer" + }, + "tokenSha256Base64url": { + "description": "SHA-256 of the controller-scoped remote-control token, encoded as unpadded base64url.", + "type": "string" + }, + "type": { + "enum": [ + "remoteControlClientConnection" + ], + "title": "RemoteControlClientConnectionDeviceKeySignPayloadType", + "type": "string" + } + }, + "required": [ + "accountUserId", + "audience", + "clientId", + "nonce", + "scopes", + "sessionId", + "targetOrigin", + "targetPath", + "tokenExpiresAt", + "tokenSha256Base64url", + "type" + ], + "title": "RemoteControlClientConnectionDeviceKeySignPayload", + "type": "object" + }, + { + "description": "Payload bound to a remote-control client `/client/enroll` ownership challenge.", + "properties": { + "accountUserId": { + "type": "string" + }, + "audience": { + "$ref": "#/definitions/v2/RemoteControlClientEnrollmentAudience" + }, + "challengeExpiresAt": { + "description": "Enrollment challenge expiration as Unix seconds.", + "format": "int64", + "type": "integer" + }, + "challengeId": { + "description": "Backend-issued enrollment challenge id that this proof authorizes.", + "type": "string" + }, + "clientId": { + "type": "string" + }, + "deviceIdentitySha256Base64url": { + "description": "SHA-256 of the requested device identity operation, encoded as unpadded base64url.", + "type": "string" + }, + "nonce": { + "type": "string" + }, + "targetOrigin": { + "description": "Origin of the backend endpoint that issued the challenge and will verify this proof.", + "type": "string" + }, + "targetPath": { + "description": "HTTP route path that this proof authorizes.", + "type": "string" + }, + "type": { + "enum": [ + "remoteControlClientEnrollment" + ], + "title": "RemoteControlClientEnrollmentDeviceKeySignPayloadType", + "type": "string" + } + }, + "required": [ + "accountUserId", + "audience", + "challengeExpiresAt", + "challengeId", + "clientId", + "deviceIdentitySha256Base64url", + "nonce", + "targetOrigin", + "targetPath", + "type" + ], + "title": "RemoteControlClientEnrollmentDeviceKeySignPayload", + "type": "object" + } + ] + }, + "DeviceKeySignResponse": { + "$schema": "http://json-schema.org/draft-07/schema#", + "description": "ASN.1 DER signature returned by `device/key/sign`.", + "properties": { + "algorithm": { + "$ref": "#/definitions/v2/DeviceKeyAlgorithm" + }, + "signatureDerBase64": { + "description": "ECDSA signature DER encoded as base64.", + "type": "string" + }, + "signedPayloadBase64": { + "description": "Exact bytes signed by the device key, encoded as base64. Verifiers must verify this byte string directly and must not reserialize `payload`.", + "type": "string" + } + }, + "required": [ + "algorithm", + "signatureDerBase64", + "signedPayloadBase64" + ], + "title": "DeviceKeySignResponse", + "type": "object" + }, "DynamicToolCallOutputContentItem": { "oneOf": [ { @@ -11642,6 +12008,20 @@ "title": "ReasoningTextDeltaNotification", "type": "object" }, + "RemoteControlClientConnectionAudience": { + "description": "Audience for a remote-control client connection device-key proof.", + "enum": [ + "remote_control_client_websocket" + ], + "type": "string" + }, + "RemoteControlClientEnrollmentAudience": { + "description": "Audience for a remote-control client enrollment device-key proof.", + "enum": [ + "remote_control_client_enrollment" + ], + "type": "string" + }, "RequestId": { "anyOf": [ { @@ -14099,8 +14479,8 @@ }, "namespace": { "type": [ - "null", - "string" + "string", + "null" ] }, "status": { @@ -16612,4 +16992,4 @@ }, "title": "CodexAppServerProtocol", "type": "object" -} +} \ No newline at end of file diff --git a/codex-rs/app-server-protocol/schema/json/codex_app_server_protocol.v2.schemas.json b/codex-rs/app-server-protocol/schema/json/codex_app_server_protocol.v2.schemas.json index 4ff918e642..82c990533a 100644 --- a/codex-rs/app-server-protocol/schema/json/codex_app_server_protocol.v2.schemas.json +++ b/codex-rs/app-server-protocol/schema/json/codex_app_server_protocol.v2.schemas.json @@ -1418,6 +1418,78 @@ "title": "App/listRequest", "type": "object" }, + { + "properties": { + "id": { + "$ref": "#/definitions/RequestId" + }, + "method": { + "enum": [ + "device/key/create" + ], + "title": "Device/key/createRequestMethod", + "type": "string" + }, + "params": { + "$ref": "#/definitions/DeviceKeyCreateParams" + } + }, + "required": [ + "id", + "method", + "params" + ], + "title": "Device/key/createRequest", + "type": "object" + }, + { + "properties": { + "id": { + "$ref": "#/definitions/RequestId" + }, + "method": { + "enum": [ + "device/key/public" + ], + "title": "Device/key/publicRequestMethod", + "type": "string" + }, + "params": { + "$ref": "#/definitions/DeviceKeyPublicParams" + } + }, + "required": [ + "id", + "method", + "params" + ], + "title": "Device/key/publicRequest", + "type": "object" + }, + { + "properties": { + "id": { + "$ref": "#/definitions/RequestId" + }, + "method": { + "enum": [ + "device/key/sign" + ], + "title": "Device/key/signRequestMethod", + "type": "string" + }, + "params": { + "$ref": "#/definitions/DeviceKeySignParams" + } + }, + "required": [ + "id", + "method", + "params" + ], + "title": "Device/key/signRequest", + "type": "object" + }, { "properties": { "id": { @@ -3935,6 +4007,300 @@ "title": "DeprecationNoticeNotification", "type": "object" }, + "DeviceKeyAlgorithm": { + "description": "Device-key algorithm reported at enrollment and signing boundaries.", + "enum": [ + "ecdsa_p256_sha256" + ], + "type": "string" + }, + "DeviceKeyCreateParams": { + "$schema": "http://json-schema.org/draft-07/schema#", + "description": "Create a controller-local device key with a random key id.", + "properties": { + "accountUserId": { + "type": "string" + }, + "clientId": { + "type": "string" + }, + "protectionPolicy": { + "anyOf": [ + { + "$ref": "#/definitions/DeviceKeyProtectionPolicy" + }, + { + "type": "null" + } + ], + "description": "Defaults to `hardware_only` when omitted." + } + }, + "required": [ + "accountUserId", + "clientId" + ], + "title": "DeviceKeyCreateParams", + "type": "object" + }, + "DeviceKeyCreateResponse": { + "$schema": "http://json-schema.org/draft-07/schema#", + "description": "Device-key metadata and public key returned by create/public APIs.", + "properties": { + "algorithm": { + "$ref": "#/definitions/DeviceKeyAlgorithm" + }, + "keyId": { + "type": "string" + }, + "protectionClass": { + "$ref": "#/definitions/DeviceKeyProtectionClass" + }, + "publicKeySpkiDerBase64": { + "description": "SubjectPublicKeyInfo DER encoded as base64.", + "type": "string" + } + }, + "required": [ + "algorithm", + "keyId", + "protectionClass", + "publicKeySpkiDerBase64" + ], + "title": "DeviceKeyCreateResponse", + "type": "object" + }, + "DeviceKeyProtectionClass": { + "description": "Platform protection class for a controller-local device key.", + "enum": [ + "hardware_secure_enclave", + "hardware_tpm", + "os_protected_nonextractable" + ], + "type": "string" + }, + "DeviceKeyProtectionPolicy": { + "description": "Protection policy for creating or loading a controller-local device key.", + "enum": [ + "hardware_only", + "allow_os_protected_nonextractable" + ], + "type": "string" + }, + "DeviceKeyPublicParams": { + "$schema": "http://json-schema.org/draft-07/schema#", + "description": "Fetch a controller-local device key public key by id.", + "properties": { + "keyId": { + "type": "string" + } + }, + "required": [ + "keyId" + ], + "title": "DeviceKeyPublicParams", + "type": "object" + }, + "DeviceKeyPublicResponse": { + "$schema": "http://json-schema.org/draft-07/schema#", + "description": "Device-key public metadata returned by `device/key/public`.", + "properties": { + "algorithm": { + "$ref": "#/definitions/DeviceKeyAlgorithm" + }, + "keyId": { + "type": "string" + }, + "protectionClass": { + "$ref": "#/definitions/DeviceKeyProtectionClass" + }, + "publicKeySpkiDerBase64": { + "description": "SubjectPublicKeyInfo DER encoded as base64.", + "type": "string" + } + }, + "required": [ + "algorithm", + "keyId", + "protectionClass", + "publicKeySpkiDerBase64" + ], + "title": "DeviceKeyPublicResponse", + "type": "object" + }, + "DeviceKeySignParams": { + "$schema": "http://json-schema.org/draft-07/schema#", + "description": "Sign an accepted structured payload with a controller-local device key.", + "properties": { + "keyId": { + "type": "string" + }, + "payload": { + "$ref": "#/definitions/DeviceKeySignPayload" + } + }, + "required": [ + "keyId", + "payload" + ], + "title": "DeviceKeySignParams", + "type": "object" + }, + "DeviceKeySignPayload": { + "description": "Structured payloads accepted by `device/key/sign`.", + "oneOf": [ + { + "description": "Payload bound to one remote-control controller websocket `/client` connection challenge.", + "properties": { + "accountUserId": { + "type": "string" + }, + "audience": { + "$ref": "#/definitions/RemoteControlClientConnectionAudience" + }, + "clientId": { + "type": "string" + }, + "nonce": { + "type": "string" + }, + "scopes": { + "description": "Must contain exactly `remote_control_controller_websocket`.", + "items": { + "type": "string" + }, + "type": "array" + }, + "sessionId": { + "description": "Backend-issued websocket session id that this proof authorizes.", + "type": "string" + }, + "targetOrigin": { + "description": "Origin of the backend endpoint that issued the challenge and will verify this proof.", + "type": "string" + }, + "targetPath": { + "description": "Websocket route path that this proof authorizes.", + "type": "string" + }, + "tokenExpiresAt": { + "description": "Remote-control token expiration as Unix seconds.", + "format": "int64", + "type": "integer" + }, + "tokenSha256Base64url": { + "description": "SHA-256 of the controller-scoped remote-control token, encoded as unpadded base64url.", + "type": "string" + }, + "type": { + "enum": [ + "remoteControlClientConnection" + ], + "title": "RemoteControlClientConnectionDeviceKeySignPayloadType", + "type": "string" + } + }, + "required": [ + "accountUserId", + "audience", + "clientId", + "nonce", + "scopes", + "sessionId", + "targetOrigin", + "targetPath", + "tokenExpiresAt", + "tokenSha256Base64url", + "type" + ], + "title": "RemoteControlClientConnectionDeviceKeySignPayload", + "type": "object" + }, + { + "description": "Payload bound to a remote-control client `/client/enroll` ownership challenge.", + "properties": { + "accountUserId": { + "type": "string" + }, + "audience": { + "$ref": "#/definitions/RemoteControlClientEnrollmentAudience" + }, + "challengeExpiresAt": { + "description": "Enrollment challenge expiration as Unix seconds.", + "format": "int64", + "type": "integer" + }, + "challengeId": { + "description": "Backend-issued enrollment challenge id that this proof authorizes.", + "type": "string" + }, + "clientId": { + "type": "string" + }, + "deviceIdentitySha256Base64url": { + "description": "SHA-256 of the requested device identity operation, encoded as unpadded base64url.", + "type": "string" + }, + "nonce": { + "type": "string" + }, + "targetOrigin": { + "description": "Origin of the backend endpoint that issued the challenge and will verify this proof.", + "type": "string" + }, + "targetPath": { + "description": "HTTP route path that this proof authorizes.", + "type": "string" + }, + "type": { + "enum": [ + "remoteControlClientEnrollment" + ], + "title": "RemoteControlClientEnrollmentDeviceKeySignPayloadType", + "type": "string" + } + }, + "required": [ + "accountUserId", + "audience", + "challengeExpiresAt", + "challengeId", + "clientId", + "deviceIdentitySha256Base64url", + "nonce", + "targetOrigin", + "targetPath", + "type" + ], + "title": "RemoteControlClientEnrollmentDeviceKeySignPayload", + "type": "object" + } + ] + }, + "DeviceKeySignResponse": { + "$schema": "http://json-schema.org/draft-07/schema#", + "description": "ASN.1 DER signature returned by `device/key/sign`.", + "properties": { + "algorithm": { + "$ref": "#/definitions/DeviceKeyAlgorithm" + }, + "signatureDerBase64": { + "description": "ECDSA signature DER encoded as base64.", + "type": "string" + }, + "signedPayloadBase64": { + "description": "Exact bytes signed by the device key, encoded as base64. Verifiers must verify this byte string directly and must not reserialize `payload`.", + "type": "string" + } + }, + "required": [ + "algorithm", + "signatureDerBase64", + "signedPayloadBase64" + ], + "title": "DeviceKeySignResponse", + "type": "object" + }, "DynamicToolCallOutputContentItem": { "oneOf": [ { @@ -8408,6 +8774,20 @@ "title": "ReasoningTextDeltaNotification", "type": "object" }, + "RemoteControlClientConnectionAudience": { + "description": "Audience for a remote-control client connection device-key proof.", + "enum": [ + "remote_control_client_websocket" + ], + "type": "string" + }, + "RemoteControlClientEnrollmentAudience": { + "description": "Audience for a remote-control client enrollment device-key proof.", + "enum": [ + "remote_control_client_enrollment" + ], + "type": "string" + }, "RequestId": { "anyOf": [ { diff --git a/codex-rs/app-server-protocol/schema/json/v2/DeviceKeyCreateParams.json b/codex-rs/app-server-protocol/schema/json/v2/DeviceKeyCreateParams.json new file mode 100644 index 0000000000..fe2c0f0895 --- /dev/null +++ b/codex-rs/app-server-protocol/schema/json/v2/DeviceKeyCreateParams.json @@ -0,0 +1,39 @@ +{ + "$schema": "http://json-schema.org/draft-07/schema#", + "definitions": { + "DeviceKeyProtectionPolicy": { + "description": "Protection policy for creating or loading a controller-local device key.", + "enum": [ + "hardware_only", + "allow_os_protected_nonextractable" + ], + "type": "string" + } + }, + "description": "Create a controller-local device key with a random key id.", + "properties": { + "accountUserId": { + "type": "string" + }, + "clientId": { + "type": "string" + }, + "protectionPolicy": { + "anyOf": [ + { + "$ref": "#/definitions/DeviceKeyProtectionPolicy" + }, + { + "type": "null" + } + ], + "description": "Defaults to `hardware_only` when omitted." + } + }, + "required": [ + "accountUserId", + "clientId" + ], + "title": "DeviceKeyCreateParams", + "type": "object" +} \ No newline at end of file diff --git a/codex-rs/app-server-protocol/schema/json/v2/DeviceKeyCreateResponse.json b/codex-rs/app-server-protocol/schema/json/v2/DeviceKeyCreateResponse.json new file mode 100644 index 0000000000..12072588a9 --- /dev/null +++ b/codex-rs/app-server-protocol/schema/json/v2/DeviceKeyCreateResponse.json @@ -0,0 +1,45 @@ +{ + "$schema": "http://json-schema.org/draft-07/schema#", + "definitions": { + "DeviceKeyAlgorithm": { + "description": "Device-key algorithm reported at enrollment and signing boundaries.", + "enum": [ + "ecdsa_p256_sha256" + ], + "type": "string" + }, + "DeviceKeyProtectionClass": { + "description": "Platform protection class for a controller-local device key.", + "enum": [ + "hardware_secure_enclave", + "hardware_tpm", + "os_protected_nonextractable" + ], + "type": "string" + } + }, + "description": "Device-key metadata and public key returned by create/public APIs.", + "properties": { + "algorithm": { + "$ref": "#/definitions/DeviceKeyAlgorithm" + }, + "keyId": { + "type": "string" + }, + "protectionClass": { + "$ref": "#/definitions/DeviceKeyProtectionClass" + }, + "publicKeySpkiDerBase64": { + "description": "SubjectPublicKeyInfo DER encoded as base64.", + "type": "string" + } + }, + "required": [ + "algorithm", + "keyId", + "protectionClass", + "publicKeySpkiDerBase64" + ], + "title": "DeviceKeyCreateResponse", + "type": "object" +} \ No newline at end of file diff --git a/codex-rs/app-server-protocol/schema/json/v2/DeviceKeyPublicParams.json b/codex-rs/app-server-protocol/schema/json/v2/DeviceKeyPublicParams.json new file mode 100644 index 0000000000..37cc5fbe2c --- /dev/null +++ b/codex-rs/app-server-protocol/schema/json/v2/DeviceKeyPublicParams.json @@ -0,0 +1,14 @@ +{ + "$schema": "http://json-schema.org/draft-07/schema#", + "description": "Fetch a controller-local device key public key by id.", + "properties": { + "keyId": { + "type": "string" + } + }, + "required": [ + "keyId" + ], + "title": "DeviceKeyPublicParams", + "type": "object" +} \ No newline at end of file diff --git a/codex-rs/app-server-protocol/schema/json/v2/DeviceKeyPublicResponse.json b/codex-rs/app-server-protocol/schema/json/v2/DeviceKeyPublicResponse.json new file mode 100644 index 0000000000..39f98b7623 --- /dev/null +++ b/codex-rs/app-server-protocol/schema/json/v2/DeviceKeyPublicResponse.json @@ -0,0 +1,45 @@ +{ + "$schema": "http://json-schema.org/draft-07/schema#", + "definitions": { + "DeviceKeyAlgorithm": { + "description": "Device-key algorithm reported at enrollment and signing boundaries.", + "enum": [ + "ecdsa_p256_sha256" + ], + "type": "string" + }, + "DeviceKeyProtectionClass": { + "description": "Platform protection class for a controller-local device key.", + "enum": [ + "hardware_secure_enclave", + "hardware_tpm", + "os_protected_nonextractable" + ], + "type": "string" + } + }, + "description": "Device-key public metadata returned by `device/key/public`.", + "properties": { + "algorithm": { + "$ref": "#/definitions/DeviceKeyAlgorithm" + }, + "keyId": { + "type": "string" + }, + "protectionClass": { + "$ref": "#/definitions/DeviceKeyProtectionClass" + }, + "publicKeySpkiDerBase64": { + "description": "SubjectPublicKeyInfo DER encoded as base64.", + "type": "string" + } + }, + "required": [ + "algorithm", + "keyId", + "protectionClass", + "publicKeySpkiDerBase64" + ], + "title": "DeviceKeyPublicResponse", + "type": "object" +} \ No newline at end of file diff --git a/codex-rs/app-server-protocol/schema/json/v2/DeviceKeySignParams.json b/codex-rs/app-server-protocol/schema/json/v2/DeviceKeySignParams.json new file mode 100644 index 0000000000..054765b297 --- /dev/null +++ b/codex-rs/app-server-protocol/schema/json/v2/DeviceKeySignParams.json @@ -0,0 +1,165 @@ +{ + "$schema": "http://json-schema.org/draft-07/schema#", + "definitions": { + "DeviceKeySignPayload": { + "description": "Structured payloads accepted by `device/key/sign`.", + "oneOf": [ + { + "description": "Payload bound to one remote-control controller websocket `/client` connection challenge.", + "properties": { + "accountUserId": { + "type": "string" + }, + "audience": { + "$ref": "#/definitions/RemoteControlClientConnectionAudience" + }, + "clientId": { + "type": "string" + }, + "nonce": { + "type": "string" + }, + "scopes": { + "description": "Must contain exactly `remote_control_controller_websocket`.", + "items": { + "type": "string" + }, + "type": "array" + }, + "sessionId": { + "description": "Backend-issued websocket session id that this proof authorizes.", + "type": "string" + }, + "targetOrigin": { + "description": "Origin of the backend endpoint that issued the challenge and will verify this proof.", + "type": "string" + }, + "targetPath": { + "description": "Websocket route path that this proof authorizes.", + "type": "string" + }, + "tokenExpiresAt": { + "description": "Remote-control token expiration as Unix seconds.", + "format": "int64", + "type": "integer" + }, + "tokenSha256Base64url": { + "description": "SHA-256 of the controller-scoped remote-control token, encoded as unpadded base64url.", + "type": "string" + }, + "type": { + "enum": [ + "remoteControlClientConnection" + ], + "title": "RemoteControlClientConnectionDeviceKeySignPayloadType", + "type": "string" + } + }, + "required": [ + "accountUserId", + "audience", + "clientId", + "nonce", + "scopes", + "sessionId", + "targetOrigin", + "targetPath", + "tokenExpiresAt", + "tokenSha256Base64url", + "type" + ], + "title": "RemoteControlClientConnectionDeviceKeySignPayload", + "type": "object" + }, + { + "description": "Payload bound to a remote-control client `/client/enroll` ownership challenge.", + "properties": { + "accountUserId": { + "type": "string" + }, + "audience": { + "$ref": "#/definitions/RemoteControlClientEnrollmentAudience" + }, + "challengeExpiresAt": { + "description": "Enrollment challenge expiration as Unix seconds.", + "format": "int64", + "type": "integer" + }, + "challengeId": { + "description": "Backend-issued enrollment challenge id that this proof authorizes.", + "type": "string" + }, + "clientId": { + "type": "string" + }, + "deviceIdentitySha256Base64url": { + "description": "SHA-256 of the requested device identity operation, encoded as unpadded base64url.", + "type": "string" + }, + "nonce": { + "type": "string" + }, + "targetOrigin": { + "description": "Origin of the backend endpoint that issued the challenge and will verify this proof.", + "type": "string" + }, + "targetPath": { + "description": "HTTP route path that this proof authorizes.", + "type": "string" + }, + "type": { + "enum": [ + "remoteControlClientEnrollment" + ], + "title": "RemoteControlClientEnrollmentDeviceKeySignPayloadType", + "type": "string" + } + }, + "required": [ + "accountUserId", + "audience", + "challengeExpiresAt", + "challengeId", + "clientId", + "deviceIdentitySha256Base64url", + "nonce", + "targetOrigin", + "targetPath", + "type" + ], + "title": "RemoteControlClientEnrollmentDeviceKeySignPayload", + "type": "object" + } + ] + }, + "RemoteControlClientConnectionAudience": { + "description": "Audience for a remote-control client connection device-key proof.", + "enum": [ + "remote_control_client_websocket" + ], + "type": "string" + }, + "RemoteControlClientEnrollmentAudience": { + "description": "Audience for a remote-control client enrollment device-key proof.", + "enum": [ + "remote_control_client_enrollment" + ], + "type": "string" + } + }, + "description": "Sign an accepted structured payload with a controller-local device key.", + "properties": { + "keyId": { + "type": "string" + }, + "payload": { + "$ref": "#/definitions/DeviceKeySignPayload" + } + }, + "required": [ + "keyId", + "payload" + ], + "title": "DeviceKeySignParams", + "type": "object" +} \ No newline at end of file diff --git a/codex-rs/app-server-protocol/schema/json/v2/DeviceKeySignResponse.json b/codex-rs/app-server-protocol/schema/json/v2/DeviceKeySignResponse.json new file mode 100644 index 0000000000..83fec90330 --- /dev/null +++ b/codex-rs/app-server-protocol/schema/json/v2/DeviceKeySignResponse.json @@ -0,0 +1,33 @@ +{ + "$schema": "http://json-schema.org/draft-07/schema#", + "definitions": { + "DeviceKeyAlgorithm": { + "description": "Device-key algorithm reported at enrollment and signing boundaries.", + "enum": [ + "ecdsa_p256_sha256" + ], + "type": "string" + } + }, + "description": "ASN.1 DER signature returned by `device/key/sign`.", + "properties": { + "algorithm": { + "$ref": "#/definitions/DeviceKeyAlgorithm" + }, + "signatureDerBase64": { + "description": "ECDSA signature DER encoded as base64.", + "type": "string" + }, + "signedPayloadBase64": { + "description": "Exact bytes signed by the device key, encoded as base64. Verifiers must verify this byte string directly and must not reserialize `payload`.", + "type": "string" + } + }, + "required": [ + "algorithm", + "signatureDerBase64", + "signedPayloadBase64" + ], + "title": "DeviceKeySignResponse", + "type": "object" +} \ No newline at end of file diff --git a/codex-rs/app-server-protocol/schema/typescript/ClientRequest.ts b/codex-rs/app-server-protocol/schema/typescript/ClientRequest.ts index c26ff9161f..fdb934ed53 100644 --- a/codex-rs/app-server-protocol/schema/typescript/ClientRequest.ts +++ b/codex-rs/app-server-protocol/schema/typescript/ClientRequest.ts @@ -16,6 +16,9 @@ import type { CommandExecWriteParams } from "./v2/CommandExecWriteParams"; import type { ConfigBatchWriteParams } from "./v2/ConfigBatchWriteParams"; import type { ConfigReadParams } from "./v2/ConfigReadParams"; import type { ConfigValueWriteParams } from "./v2/ConfigValueWriteParams"; +import type { DeviceKeyCreateParams } from "./v2/DeviceKeyCreateParams"; +import type { DeviceKeyPublicParams } from "./v2/DeviceKeyPublicParams"; +import type { DeviceKeySignParams } from "./v2/DeviceKeySignParams"; import type { ExperimentalFeatureEnablementSetParams } from "./v2/ExperimentalFeatureEnablementSetParams"; import type { ExperimentalFeatureListParams } from "./v2/ExperimentalFeatureListParams"; import type { ExternalAgentConfigDetectParams } from "./v2/ExternalAgentConfigDetectParams"; @@ -71,4 +74,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": "thread/turns/list", id: RequestId, params: ThreadTurnsListParams, } | { "method": "thread/inject_items", id: RequestId, params: ThreadInjectItemsParams, } | { "method": "skills/list", id: RequestId, params: SkillsListParams, } | { "method": "marketplace/add", id: RequestId, params: MarketplaceAddParams, } | { "method": "marketplace/remove", id: RequestId, params: MarketplaceRemoveParams, } | { "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": "fs/watch", id: RequestId, params: FsWatchParams, } | { "method": "fs/unwatch", id: RequestId, params: FsUnwatchParams, } | { "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": "experimentalFeature/enablement/set", id: RequestId, params: ExperimentalFeatureEnablementSetParams, } | { "method": "mcpServer/oauth/login", id: RequestId, params: McpServerOauthLoginParams, } | { "method": "config/mcpServer/reload", id: RequestId, params: undefined, } | { "method": "mcpServerStatus/list", id: RequestId, params: ListMcpServerStatusParams, } | { "method": "mcpServer/resource/read", id: RequestId, params: McpResourceReadParams, } | { "method": "mcpServer/tool/call", id: RequestId, params: McpServerToolCallParams, } | { "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": "account/sendAddCreditsNudgeEmail", id: RequestId, params: SendAddCreditsNudgeEmailParams, } | { "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": "thread/turns/list", id: RequestId, params: ThreadTurnsListParams, } | { "method": "thread/inject_items", id: RequestId, params: ThreadInjectItemsParams, } | { "method": "skills/list", id: RequestId, params: SkillsListParams, } | { "method": "marketplace/add", id: RequestId, params: MarketplaceAddParams, } | { "method": "marketplace/remove", id: RequestId, params: MarketplaceRemoveParams, } | { "method": "plugin/list", id: RequestId, params: PluginListParams, } | { "method": "plugin/read", id: RequestId, params: PluginReadParams, } | { "method": "app/list", id: RequestId, params: AppsListParams, } | { "method": "device/key/create", id: RequestId, params: DeviceKeyCreateParams, } | { "method": "device/key/public", id: RequestId, params: DeviceKeyPublicParams, } | { "method": "device/key/sign", id: RequestId, params: DeviceKeySignParams, } | { "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": "fs/watch", id: RequestId, params: FsWatchParams, } | { "method": "fs/unwatch", id: RequestId, params: FsUnwatchParams, } | { "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": "experimentalFeature/enablement/set", id: RequestId, params: ExperimentalFeatureEnablementSetParams, } | { "method": "mcpServer/oauth/login", id: RequestId, params: McpServerOauthLoginParams, } | { "method": "config/mcpServer/reload", id: RequestId, params: undefined, } | { "method": "mcpServerStatus/list", id: RequestId, params: ListMcpServerStatusParams, } | { "method": "mcpServer/resource/read", id: RequestId, params: McpResourceReadParams, } | { "method": "mcpServer/tool/call", id: RequestId, params: McpServerToolCallParams, } | { "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": "account/sendAddCreditsNudgeEmail", id: RequestId, params: SendAddCreditsNudgeEmailParams, } | { "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, }; diff --git a/codex-rs/app-server-protocol/schema/typescript/v2/DeviceKeyAlgorithm.ts b/codex-rs/app-server-protocol/schema/typescript/v2/DeviceKeyAlgorithm.ts new file mode 100644 index 0000000000..6809c41eb5 --- /dev/null +++ b/codex-rs/app-server-protocol/schema/typescript/v2/DeviceKeyAlgorithm.ts @@ -0,0 +1,8 @@ +// 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. + +/** + * Device-key algorithm reported at enrollment and signing boundaries. + */ +export type DeviceKeyAlgorithm = "ecdsa_p256_sha256"; diff --git a/codex-rs/app-server-protocol/schema/typescript/v2/DeviceKeyCreateParams.ts b/codex-rs/app-server-protocol/schema/typescript/v2/DeviceKeyCreateParams.ts new file mode 100644 index 0000000000..7ffd9b5fa3 --- /dev/null +++ b/codex-rs/app-server-protocol/schema/typescript/v2/DeviceKeyCreateParams.ts @@ -0,0 +1,13 @@ +// GENERATED CODE! DO NOT MODIFY BY HAND! + +// This file was generated by [ts-rs](https://github.com/Aleph-Alpha/ts-rs). Do not edit this file manually. +import type { DeviceKeyProtectionPolicy } from "./DeviceKeyProtectionPolicy"; + +/** + * Create a controller-local device key with a random key id. + */ +export type DeviceKeyCreateParams = { +/** + * Defaults to `hardware_only` when omitted. + */ +protectionPolicy?: DeviceKeyProtectionPolicy | null, accountUserId: string, clientId: string, }; diff --git a/codex-rs/app-server-protocol/schema/typescript/v2/DeviceKeyCreateResponse.ts b/codex-rs/app-server-protocol/schema/typescript/v2/DeviceKeyCreateResponse.ts new file mode 100644 index 0000000000..6ace37934a --- /dev/null +++ b/codex-rs/app-server-protocol/schema/typescript/v2/DeviceKeyCreateResponse.ts @@ -0,0 +1,14 @@ +// 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 { DeviceKeyAlgorithm } from "./DeviceKeyAlgorithm"; +import type { DeviceKeyProtectionClass } from "./DeviceKeyProtectionClass"; + +/** + * Device-key metadata and public key returned by create/public APIs. + */ +export type DeviceKeyCreateResponse = { keyId: string, +/** + * SubjectPublicKeyInfo DER encoded as base64. + */ +publicKeySpkiDerBase64: string, algorithm: DeviceKeyAlgorithm, protectionClass: DeviceKeyProtectionClass, }; diff --git a/codex-rs/app-server-protocol/schema/typescript/v2/DeviceKeyProtectionClass.ts b/codex-rs/app-server-protocol/schema/typescript/v2/DeviceKeyProtectionClass.ts new file mode 100644 index 0000000000..ba7ff311ad --- /dev/null +++ b/codex-rs/app-server-protocol/schema/typescript/v2/DeviceKeyProtectionClass.ts @@ -0,0 +1,8 @@ +// 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. + +/** + * Platform protection class for a controller-local device key. + */ +export type DeviceKeyProtectionClass = "hardware_secure_enclave" | "hardware_tpm" | "os_protected_nonextractable"; diff --git a/codex-rs/app-server-protocol/schema/typescript/v2/DeviceKeyProtectionPolicy.ts b/codex-rs/app-server-protocol/schema/typescript/v2/DeviceKeyProtectionPolicy.ts new file mode 100644 index 0000000000..66fceafb51 --- /dev/null +++ b/codex-rs/app-server-protocol/schema/typescript/v2/DeviceKeyProtectionPolicy.ts @@ -0,0 +1,8 @@ +// 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. + +/** + * Protection policy for creating or loading a controller-local device key. + */ +export type DeviceKeyProtectionPolicy = "hardware_only" | "allow_os_protected_nonextractable"; diff --git a/codex-rs/app-server-protocol/schema/typescript/v2/DeviceKeyPublicParams.ts b/codex-rs/app-server-protocol/schema/typescript/v2/DeviceKeyPublicParams.ts new file mode 100644 index 0000000000..5a5b77899d --- /dev/null +++ b/codex-rs/app-server-protocol/schema/typescript/v2/DeviceKeyPublicParams.ts @@ -0,0 +1,8 @@ +// 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. + +/** + * Fetch a controller-local device key public key by id. + */ +export type DeviceKeyPublicParams = { keyId: string, }; diff --git a/codex-rs/app-server-protocol/schema/typescript/v2/DeviceKeyPublicResponse.ts b/codex-rs/app-server-protocol/schema/typescript/v2/DeviceKeyPublicResponse.ts new file mode 100644 index 0000000000..9967c0936e --- /dev/null +++ b/codex-rs/app-server-protocol/schema/typescript/v2/DeviceKeyPublicResponse.ts @@ -0,0 +1,14 @@ +// 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 { DeviceKeyAlgorithm } from "./DeviceKeyAlgorithm"; +import type { DeviceKeyProtectionClass } from "./DeviceKeyProtectionClass"; + +/** + * Device-key public metadata returned by `device/key/public`. + */ +export type DeviceKeyPublicResponse = { keyId: string, +/** + * SubjectPublicKeyInfo DER encoded as base64. + */ +publicKeySpkiDerBase64: string, algorithm: DeviceKeyAlgorithm, protectionClass: DeviceKeyProtectionClass, }; diff --git a/codex-rs/app-server-protocol/schema/typescript/v2/DeviceKeySignParams.ts b/codex-rs/app-server-protocol/schema/typescript/v2/DeviceKeySignParams.ts new file mode 100644 index 0000000000..0886e45d93 --- /dev/null +++ b/codex-rs/app-server-protocol/schema/typescript/v2/DeviceKeySignParams.ts @@ -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. +import type { DeviceKeySignPayload } from "./DeviceKeySignPayload"; + +/** + * Sign an accepted structured payload with a controller-local device key. + */ +export type DeviceKeySignParams = { keyId: string, payload: DeviceKeySignPayload, }; diff --git a/codex-rs/app-server-protocol/schema/typescript/v2/DeviceKeySignPayload.ts b/codex-rs/app-server-protocol/schema/typescript/v2/DeviceKeySignPayload.ts new file mode 100644 index 0000000000..8596445490 --- /dev/null +++ b/codex-rs/app-server-protocol/schema/typescript/v2/DeviceKeySignPayload.ts @@ -0,0 +1,54 @@ +// 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 { RemoteControlClientConnectionAudience } from "./RemoteControlClientConnectionAudience"; +import type { RemoteControlClientEnrollmentAudience } from "./RemoteControlClientEnrollmentAudience"; + +/** + * Structured payloads accepted by `device/key/sign`. + */ +export type DeviceKeySignPayload = { "type": "remoteControlClientConnection", nonce: string, audience: RemoteControlClientConnectionAudience, +/** + * Backend-issued websocket session id that this proof authorizes. + */ +sessionId: string, +/** + * Origin of the backend endpoint that issued the challenge and will verify this proof. + */ +targetOrigin: string, +/** + * Websocket route path that this proof authorizes. + */ +targetPath: string, accountUserId: string, clientId: string, +/** + * Remote-control token expiration as Unix seconds. + */ +tokenExpiresAt: number, +/** + * SHA-256 of the controller-scoped remote-control token, encoded as unpadded base64url. + */ +tokenSha256Base64url: string, +/** + * Must contain exactly `remote_control_controller_websocket`. + */ +scopes: Array, } | { "type": "remoteControlClientEnrollment", nonce: string, audience: RemoteControlClientEnrollmentAudience, +/** + * Backend-issued enrollment challenge id that this proof authorizes. + */ +challengeId: string, +/** + * Origin of the backend endpoint that issued the challenge and will verify this proof. + */ +targetOrigin: string, +/** + * HTTP route path that this proof authorizes. + */ +targetPath: string, accountUserId: string, clientId: string, +/** + * SHA-256 of the requested device identity operation, encoded as unpadded base64url. + */ +deviceIdentitySha256Base64url: string, +/** + * Enrollment challenge expiration as Unix seconds. + */ +challengeExpiresAt: number, }; diff --git a/codex-rs/app-server-protocol/schema/typescript/v2/DeviceKeySignResponse.ts b/codex-rs/app-server-protocol/schema/typescript/v2/DeviceKeySignResponse.ts new file mode 100644 index 0000000000..cf77fae27f --- /dev/null +++ b/codex-rs/app-server-protocol/schema/typescript/v2/DeviceKeySignResponse.ts @@ -0,0 +1,18 @@ +// 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 { DeviceKeyAlgorithm } from "./DeviceKeyAlgorithm"; + +/** + * ASN.1 DER signature returned by `device/key/sign`. + */ +export type DeviceKeySignResponse = { +/** + * ECDSA signature DER encoded as base64. + */ +signatureDerBase64: string, +/** + * Exact bytes signed by the device key, encoded as base64. Verifiers must verify this byte + * string directly and must not reserialize `payload`. + */ +signedPayloadBase64: string, algorithm: DeviceKeyAlgorithm, }; diff --git a/codex-rs/app-server-protocol/schema/typescript/v2/RemoteControlClientConnectionAudience.ts b/codex-rs/app-server-protocol/schema/typescript/v2/RemoteControlClientConnectionAudience.ts new file mode 100644 index 0000000000..e4d41ff4c2 --- /dev/null +++ b/codex-rs/app-server-protocol/schema/typescript/v2/RemoteControlClientConnectionAudience.ts @@ -0,0 +1,8 @@ +// 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. + +/** + * Audience for a remote-control client connection device-key proof. + */ +export type RemoteControlClientConnectionAudience = "remote_control_client_websocket"; diff --git a/codex-rs/app-server-protocol/schema/typescript/v2/RemoteControlClientEnrollmentAudience.ts b/codex-rs/app-server-protocol/schema/typescript/v2/RemoteControlClientEnrollmentAudience.ts new file mode 100644 index 0000000000..b65fb3d11b --- /dev/null +++ b/codex-rs/app-server-protocol/schema/typescript/v2/RemoteControlClientEnrollmentAudience.ts @@ -0,0 +1,8 @@ +// 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. + +/** + * Audience for a remote-control client enrollment device-key proof. + */ +export type RemoteControlClientEnrollmentAudience = "remote_control_client_enrollment"; diff --git a/codex-rs/app-server-protocol/schema/typescript/v2/index.ts b/codex-rs/app-server-protocol/schema/typescript/v2/index.ts index e37759112a..7bddd0f9d6 100644 --- a/codex-rs/app-server-protocol/schema/typescript/v2/index.ts +++ b/codex-rs/app-server-protocol/schema/typescript/v2/index.ts @@ -74,6 +74,16 @@ export type { ConfigWriteResponse } from "./ConfigWriteResponse"; export type { ContextCompactedNotification } from "./ContextCompactedNotification"; export type { CreditsSnapshot } from "./CreditsSnapshot"; export type { DeprecationNoticeNotification } from "./DeprecationNoticeNotification"; +export type { DeviceKeyAlgorithm } from "./DeviceKeyAlgorithm"; +export type { DeviceKeyCreateParams } from "./DeviceKeyCreateParams"; +export type { DeviceKeyCreateResponse } from "./DeviceKeyCreateResponse"; +export type { DeviceKeyProtectionClass } from "./DeviceKeyProtectionClass"; +export type { DeviceKeyProtectionPolicy } from "./DeviceKeyProtectionPolicy"; +export type { DeviceKeyPublicParams } from "./DeviceKeyPublicParams"; +export type { DeviceKeyPublicResponse } from "./DeviceKeyPublicResponse"; +export type { DeviceKeySignParams } from "./DeviceKeySignParams"; +export type { DeviceKeySignPayload } from "./DeviceKeySignPayload"; +export type { DeviceKeySignResponse } from "./DeviceKeySignResponse"; export type { DynamicToolCallOutputContentItem } from "./DynamicToolCallOutputContentItem"; export type { DynamicToolCallParams } from "./DynamicToolCallParams"; export type { DynamicToolCallResponse } from "./DynamicToolCallResponse"; @@ -259,6 +269,8 @@ export type { ReasoningEffortOption } from "./ReasoningEffortOption"; export type { ReasoningSummaryPartAddedNotification } from "./ReasoningSummaryPartAddedNotification"; export type { ReasoningSummaryTextDeltaNotification } from "./ReasoningSummaryTextDeltaNotification"; export type { ReasoningTextDeltaNotification } from "./ReasoningTextDeltaNotification"; +export type { RemoteControlClientConnectionAudience } from "./RemoteControlClientConnectionAudience"; +export type { RemoteControlClientEnrollmentAudience } from "./RemoteControlClientEnrollmentAudience"; export type { RequestPermissionProfile } from "./RequestPermissionProfile"; export type { ResidencyRequirement } from "./ResidencyRequirement"; export type { ReviewDelivery } from "./ReviewDelivery"; diff --git a/codex-rs/app-server-protocol/src/protocol/common.rs b/codex-rs/app-server-protocol/src/protocol/common.rs index aa3ff8331c..4179e18b55 100644 --- a/codex-rs/app-server-protocol/src/protocol/common.rs +++ b/codex-rs/app-server-protocol/src/protocol/common.rs @@ -360,6 +360,18 @@ client_request_definitions! { params: v2::AppsListParams, response: v2::AppsListResponse, }, + DeviceKeyCreate => "device/key/create" { + params: v2::DeviceKeyCreateParams, + response: v2::DeviceKeyCreateResponse, + }, + DeviceKeyPublic => "device/key/public" { + params: v2::DeviceKeyPublicParams, + response: v2::DeviceKeyPublicResponse, + }, + DeviceKeySign => "device/key/sign" { + params: v2::DeviceKeySignParams, + response: v2::DeviceKeySignResponse, + }, FsReadFile => "fs/readFile" { params: v2::FsReadFileParams, response: v2::FsReadFileResponse, diff --git a/codex-rs/app-server-protocol/src/protocol/v2.rs b/codex-rs/app-server-protocol/src/protocol/v2.rs index 8ac6651ae9..a78db2e0e2 100644 --- a/codex-rs/app-server-protocol/src/protocol/v2.rs +++ b/codex-rs/app-server-protocol/src/protocol/v2.rs @@ -2494,6 +2494,164 @@ pub struct FeedbackUploadResponse { pub thread_id: String, } +/// Device-key algorithm reported at enrollment and signing boundaries. +#[derive(Serialize, Deserialize, Debug, Clone, Copy, PartialEq, Eq, JsonSchema, TS)] +#[serde(rename_all = "snake_case")] +#[ts(rename_all = "snake_case", export_to = "v2/")] +pub enum DeviceKeyAlgorithm { + EcdsaP256Sha256, +} + +/// Platform protection class for a controller-local device key. +#[derive(Serialize, Deserialize, Debug, Clone, Copy, PartialEq, Eq, JsonSchema, TS)] +#[serde(rename_all = "snake_case")] +#[ts(rename_all = "snake_case", export_to = "v2/")] +pub enum DeviceKeyProtectionClass { + HardwareSecureEnclave, + HardwareTpm, + OsProtectedNonextractable, +} + +/// Protection policy for creating or loading a controller-local device key. +#[derive(Serialize, Deserialize, Debug, Clone, Copy, PartialEq, Eq, JsonSchema, TS)] +#[serde(rename_all = "snake_case")] +#[ts(rename_all = "snake_case", export_to = "v2/")] +pub enum DeviceKeyProtectionPolicy { + HardwareOnly, + AllowOsProtectedNonextractable, +} + +/// Create a controller-local device key with a random key id. +#[derive(Serialize, Deserialize, Debug, Clone, PartialEq, Eq, JsonSchema, TS)] +#[serde(rename_all = "camelCase")] +#[ts(export_to = "v2/")] +pub struct DeviceKeyCreateParams { + /// Defaults to `hardware_only` when omitted. + #[ts(optional = nullable)] + pub protection_policy: Option, + pub account_user_id: String, + pub client_id: String, +} + +/// Device-key metadata and public key returned by create/public APIs. +#[derive(Serialize, Deserialize, Debug, Clone, PartialEq, Eq, JsonSchema, TS)] +#[serde(rename_all = "camelCase")] +#[ts(export_to = "v2/")] +pub struct DeviceKeyCreateResponse { + pub key_id: String, + /// SubjectPublicKeyInfo DER encoded as base64. + pub public_key_spki_der_base64: String, + pub algorithm: DeviceKeyAlgorithm, + pub protection_class: DeviceKeyProtectionClass, +} + +/// Fetch a controller-local device key public key by id. +#[derive(Serialize, Deserialize, Debug, Clone, PartialEq, Eq, JsonSchema, TS)] +#[serde(rename_all = "camelCase")] +#[ts(export_to = "v2/")] +pub struct DeviceKeyPublicParams { + pub key_id: String, +} + +/// Device-key public metadata returned by `device/key/public`. +#[derive(Serialize, Deserialize, Debug, Clone, PartialEq, Eq, JsonSchema, TS)] +#[serde(rename_all = "camelCase")] +#[ts(export_to = "v2/")] +pub struct DeviceKeyPublicResponse { + pub key_id: String, + /// SubjectPublicKeyInfo DER encoded as base64. + pub public_key_spki_der_base64: String, + pub algorithm: DeviceKeyAlgorithm, + pub protection_class: DeviceKeyProtectionClass, +} + +/// Audience for a remote-control client connection device-key proof. +#[derive(Serialize, Deserialize, Debug, Clone, Copy, PartialEq, Eq, JsonSchema, TS)] +#[serde(rename_all = "snake_case")] +#[ts(rename_all = "snake_case", export_to = "v2/")] +pub enum RemoteControlClientConnectionAudience { + RemoteControlClientWebsocket, +} + +/// Audience for a remote-control client enrollment device-key proof. +#[derive(Serialize, Deserialize, Debug, Clone, Copy, PartialEq, Eq, JsonSchema, TS)] +#[serde(rename_all = "snake_case")] +#[ts(rename_all = "snake_case", export_to = "v2/")] +pub enum RemoteControlClientEnrollmentAudience { + RemoteControlClientEnrollment, +} + +/// Structured payloads accepted by `device/key/sign`. +#[derive(Serialize, Deserialize, Debug, Clone, PartialEq, Eq, JsonSchema, TS)] +#[serde(tag = "type", rename_all = "camelCase")] +#[ts(tag = "type", export_to = "v2/")] +pub enum DeviceKeySignPayload { + /// Payload bound to one remote-control controller websocket `/client` connection challenge. + #[serde(rename_all = "camelCase")] + #[ts(rename_all = "camelCase")] + RemoteControlClientConnection { + nonce: String, + audience: RemoteControlClientConnectionAudience, + /// Backend-issued websocket session id that this proof authorizes. + session_id: String, + /// Origin of the backend endpoint that issued the challenge and will verify this proof. + target_origin: String, + /// Websocket route path that this proof authorizes. + target_path: String, + account_user_id: String, + client_id: String, + /// Remote-control token expiration as Unix seconds. + #[ts(type = "number")] + token_expires_at: i64, + /// SHA-256 of the controller-scoped remote-control token, encoded as unpadded base64url. + token_sha256_base64url: String, + /// Must contain exactly `remote_control_controller_websocket`. + scopes: Vec, + }, + /// Payload bound to a remote-control client `/client/enroll` ownership challenge. + #[serde(rename_all = "camelCase")] + #[ts(rename_all = "camelCase")] + RemoteControlClientEnrollment { + nonce: String, + audience: RemoteControlClientEnrollmentAudience, + /// Backend-issued enrollment challenge id that this proof authorizes. + challenge_id: String, + /// Origin of the backend endpoint that issued the challenge and will verify this proof. + target_origin: String, + /// HTTP route path that this proof authorizes. + target_path: String, + account_user_id: String, + client_id: String, + /// SHA-256 of the requested device identity operation, encoded as unpadded base64url. + device_identity_sha256_base64url: String, + /// Enrollment challenge expiration as Unix seconds. + #[ts(type = "number")] + challenge_expires_at: i64, + }, +} + +/// Sign an accepted structured payload with a controller-local device key. +#[derive(Serialize, Deserialize, Debug, Clone, PartialEq, Eq, JsonSchema, TS)] +#[serde(rename_all = "camelCase")] +#[ts(export_to = "v2/")] +pub struct DeviceKeySignParams { + pub key_id: String, + pub payload: DeviceKeySignPayload, +} + +/// ASN.1 DER signature returned by `device/key/sign`. +#[derive(Serialize, Deserialize, Debug, Clone, PartialEq, Eq, JsonSchema, TS)] +#[serde(rename_all = "camelCase")] +#[ts(export_to = "v2/")] +pub struct DeviceKeySignResponse { + /// ECDSA signature DER encoded as base64. + pub signature_der_base64: String, + /// Exact bytes signed by the device key, encoded as base64. Verifiers must verify this byte + /// string directly and must not reserialize `payload`. + pub signed_payload_base64: String, + pub algorithm: DeviceKeyAlgorithm, +} + /// Read a file from the host filesystem. #[derive(Serialize, Deserialize, Debug, Clone, PartialEq, Eq, JsonSchema, TS)] #[serde(rename_all = "camelCase")] @@ -7386,6 +7544,181 @@ mod tests { assert_eq!(decoded, params); } + #[test] + fn device_key_create_params_round_trip_uses_protection_policy() { + let params = DeviceKeyCreateParams { + protection_policy: None, + account_user_id: "account-user-1".to_string(), + client_id: "cli_123".to_string(), + }; + + let value = serde_json::to_value(¶ms).expect("serialize device/key/create params"); + assert_eq!( + value, + json!({ + "accountUserId": "account-user-1", + "clientId": "cli_123", + "protectionPolicy": null, + }) + ); + + let decoded = serde_json::from_value::(value) + .expect("deserialize device/key/create params"); + assert_eq!(decoded, params); + + let params = DeviceKeyCreateParams { + protection_policy: Some(DeviceKeyProtectionPolicy::AllowOsProtectedNonextractable), + account_user_id: "account-user-1".to_string(), + client_id: "cli_123".to_string(), + }; + let value = serde_json::to_value(¶ms) + .expect("serialize device/key/create params with protection policy"); + assert_eq!( + value, + json!({ + "accountUserId": "account-user-1", + "clientId": "cli_123", + "protectionPolicy": "allow_os_protected_nonextractable", + }) + ); + } + + #[test] + fn device_key_create_response_round_trips_protection_class() { + let response = DeviceKeyCreateResponse { + key_id: "dk_123".to_string(), + public_key_spki_der_base64: "MFkwEwYHKoZIzj0CAQYIKoZIzj0DAQcDQgAE".to_string(), + algorithm: DeviceKeyAlgorithm::EcdsaP256Sha256, + protection_class: DeviceKeyProtectionClass::OsProtectedNonextractable, + }; + + let value = serde_json::to_value(&response).expect("serialize device/key/create response"); + assert_eq!( + value, + json!({ + "keyId": "dk_123", + "publicKeySpkiDerBase64": "MFkwEwYHKoZIzj0CAQYIKoZIzj0DAQcDQgAE", + "algorithm": "ecdsa_p256_sha256", + "protectionClass": "os_protected_nonextractable", + }) + ); + + let decoded = serde_json::from_value::(value) + .expect("deserialize device/key/create response"); + assert_eq!(decoded, response); + } + + #[test] + fn device_key_sign_params_round_trip_uses_accepted_payload_enum() { + let params = DeviceKeySignParams { + key_id: "dk_123".to_string(), + payload: DeviceKeySignPayload::RemoteControlClientConnection { + nonce: "nonce-1".to_string(), + audience: RemoteControlClientConnectionAudience::RemoteControlClientWebsocket, + session_id: "wssess_123".to_string(), + target_origin: "https://chatgpt.com".to_string(), + target_path: "/api/codex/remote/control/client".to_string(), + account_user_id: "account-user-1".to_string(), + client_id: "cli_123".to_string(), + token_sha256_base64url: "47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU".to_string(), + token_expires_at: 1_700_000_000, + scopes: vec!["remote_control_controller_websocket".to_string()], + }, + }; + + let value = serde_json::to_value(¶ms).expect("serialize device/key/sign params"); + assert_eq!( + value, + json!({ + "keyId": "dk_123", + "payload": { + "type": "remoteControlClientConnection", + "nonce": "nonce-1", + "audience": "remote_control_client_websocket", + "sessionId": "wssess_123", + "targetOrigin": "https://chatgpt.com", + "targetPath": "/api/codex/remote/control/client", + "accountUserId": "account-user-1", + "clientId": "cli_123", + "tokenSha256Base64url": "47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU", + "tokenExpiresAt": 1_700_000_000, + "scopes": ["remote_control_controller_websocket"], + }, + }) + ); + + let decoded = serde_json::from_value::(value) + .expect("deserialize device/key/sign params"); + assert_eq!(decoded, params); + } + + #[test] + fn device_key_sign_params_round_trip_uses_enrollment_payload() { + let params = DeviceKeySignParams { + key_id: "dk_123".to_string(), + payload: DeviceKeySignPayload::RemoteControlClientEnrollment { + nonce: "nonce-1".to_string(), + audience: RemoteControlClientEnrollmentAudience::RemoteControlClientEnrollment, + challenge_id: "rch_123".to_string(), + target_origin: "https://chatgpt.com".to_string(), + target_path: "/wham/remote/control/client/enroll".to_string(), + account_user_id: "account-user-1".to_string(), + client_id: "cli_123".to_string(), + device_identity_sha256_base64url: "47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU" + .to_string(), + challenge_expires_at: 1_700_000_000, + }, + }; + + let value = serde_json::to_value(¶ms) + .expect("serialize device/key/sign params with enrollment payload"); + assert_eq!( + value, + json!({ + "keyId": "dk_123", + "payload": { + "type": "remoteControlClientEnrollment", + "nonce": "nonce-1", + "audience": "remote_control_client_enrollment", + "challengeId": "rch_123", + "targetOrigin": "https://chatgpt.com", + "targetPath": "/wham/remote/control/client/enroll", + "accountUserId": "account-user-1", + "clientId": "cli_123", + "deviceIdentitySha256Base64url": "47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU", + "challengeExpiresAt": 1_700_000_000, + }, + }) + ); + + let decoded = serde_json::from_value::(value) + .expect("deserialize device/key/sign params with enrollment payload"); + assert_eq!(decoded, params); + } + + #[test] + fn device_key_sign_response_returns_signed_payload_bytes() { + let response = DeviceKeySignResponse { + signature_der_base64: "MEUCIQD".to_string(), + signed_payload_base64: "eyJkb21haW4iOiJjb2RleA".to_string(), + algorithm: DeviceKeyAlgorithm::EcdsaP256Sha256, + }; + + let value = serde_json::to_value(&response).expect("serialize device/key/sign response"); + assert_eq!( + value, + json!({ + "signatureDerBase64": "MEUCIQD", + "signedPayloadBase64": "eyJkb21haW4iOiJjb2RleA", + "algorithm": "ecdsa_p256_sha256", + }) + ); + + let decoded = serde_json::from_value::(value) + .expect("deserialize device/key/sign response"); + assert_eq!(decoded, response); + } + #[test] fn fs_create_directory_params_round_trip_with_default_recursive() { let params = FsCreateDirectoryParams { diff --git a/codex-rs/app-server/src/codex_message_processor.rs b/codex-rs/app-server/src/codex_message_processor.rs index 7a2ada0765..f522908be9 100644 --- a/codex-rs/app-server/src/codex_message_processor.rs +++ b/codex-rs/app-server/src/codex_message_processor.rs @@ -1186,6 +1186,11 @@ impl CodexMessageProcessor { self.command_exec_terminate(to_connection_request_id(request_id), params) .await; } + ClientRequest::DeviceKeyCreate { .. } + | ClientRequest::DeviceKeyPublic { .. } + | ClientRequest::DeviceKeySign { .. } => { + warn!("Device key request reached CodexMessageProcessor unexpectedly"); + } ClientRequest::ConfigRead { .. } | ClientRequest::ConfigValueWrite { .. } | ClientRequest::ConfigBatchWrite { .. } From 3a9df58d0611e51dc059d2e9175f4b338d8b99a1 Mon Sep 17 00:00:00 2001 From: Rennie Date: Tue, 21 Apr 2026 10:09:46 -0700 Subject: [PATCH 03/22] Propagate thread id in MCP tool metadata (#18093) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit ## Summary - attach the authoritative Codex thread id to MCP tool request `_meta.threadId` for model-initiated tool calls - attach the same thread id for manual `mcpServer/tool/call` requests before invoking the MCP server - cover both metadata helper behavior and the manual app-server MCP path in tests needed because the Rust app-server is the last place that still has authoritative knowledge of “this model-generated MCP tool call belongs to conversation/thread X” before the request leaves Codex and reaches Hoopa. It adds threadId to MCP request metadata in the model-generated tool-call path, using sess.conversation_id, and also does the same for the manual mcpServer/tool/call path. ## Test plan - `cargo test -p codex-core mcp_tool_call_thread_id_meta_is_added_to_request_meta --lib` - `cargo test -p codex-app-server mcp_server_tool_call_returns_tool_result` Paired Hoopa consumer PR: https://github.com/openai/openai/pull/833263 --- .../app-server/src/codex_message_processor.rs | 32 +++++++++++++++++-- .../app-server/tests/suite/v2/mcp_tool.rs | 13 ++++++-- codex-rs/core/src/mcp_tool_call.rs | 27 ++++++++++++++++ codex-rs/core/src/mcp_tool_call_tests.rs | 29 +++++++++++++++++ 4 files changed, 97 insertions(+), 4 deletions(-) diff --git a/codex-rs/app-server/src/codex_message_processor.rs b/codex-rs/app-server/src/codex_message_processor.rs index f522908be9..a60a079898 100644 --- a/codex-rs/app-server/src/codex_message_processor.rs +++ b/codex-rs/app-server/src/codex_message_processor.rs @@ -6046,17 +6046,19 @@ impl CodexMessageProcessor { params: McpServerToolCallParams, ) { let outgoing = Arc::clone(&self.outgoing); - let (_, thread) = match self.load_thread(¶ms.thread_id).await { + let thread_id = params.thread_id.clone(); + let (_, thread) = match self.load_thread(&thread_id).await { Ok(thread) => thread, Err(error) => { self.outgoing.send_error(request_id, error).await; return; } }; + let meta = with_mcp_tool_call_thread_id_meta(params.meta, &thread_id); tokio::spawn(async move { let result = thread - .call_mcp_tool(¶ms.server, ¶ms.tool, params.arguments, params.meta) + .call_mcp_tool(¶ms.server, ¶ms.tool, params.arguments, meta) .await; match result { Ok(result) => { @@ -9896,6 +9898,32 @@ fn thread_store_archive_error(operation: &str, err: ThreadStoreError) -> JSONRPC } } +const MCP_TOOL_THREAD_ID_META_KEY: &str = "threadId"; + +fn with_mcp_tool_call_thread_id_meta( + meta: Option, + thread_id: &str, +) -> Option { + match meta { + Some(serde_json::Value::Object(mut map)) => { + map.insert( + MCP_TOOL_THREAD_ID_META_KEY.to_string(), + serde_json::Value::String(thread_id.to_string()), + ); + Some(serde_json::Value::Object(map)) + } + None => { + let mut map = serde_json::Map::new(); + map.insert( + MCP_TOOL_THREAD_ID_META_KEY.to_string(), + serde_json::Value::String(thread_id.to_string()), + ); + Some(serde_json::Value::Object(map)) + } + other => other, + } +} + fn summary_from_stored_thread( thread: StoredThread, fallback_provider: &str, diff --git a/codex-rs/app-server/tests/suite/v2/mcp_tool.rs b/codex-rs/app-server/tests/suite/v2/mcp_tool.rs index 87556c8364..8a323efcef 100644 --- a/codex-rs/app-server/tests/suite/v2/mcp_tool.rs +++ b/codex-rs/app-server/tests/suite/v2/mcp_tool.rs @@ -83,10 +83,11 @@ url = "{mcp_server_url}/mcp" ) .await??; let ThreadStartResponse { thread, .. } = to_response(thread_start_resp)?; + let thread_id = thread.id.clone(); let tool_call_request_id = mcp .send_mcp_server_tool_call_request(McpServerToolCallParams { - thread_id: thread.id, + thread_id: thread_id.clone(), server: TEST_SERVER_NAME.to_string(), tool: TEST_TOOL_NAME.to_string(), arguments: Some(json!({ @@ -114,6 +115,7 @@ url = "{mcp_server_url}/mcp" response.structured_content, Some(json!({ "echoed": "hello from app", + "threadId": thread_id, })) ); assert_eq!(response.is_error, Some(false)); @@ -203,7 +205,7 @@ impl ServerHandler for ToolAppsMcpServer { async fn call_tool( &self, request: CallToolRequestParams, - _context: RequestContext, + context: RequestContext, ) -> Result { assert_eq!(request.name.as_ref(), TEST_TOOL_NAME); let message = request @@ -212,12 +214,19 @@ impl ServerHandler for ToolAppsMcpServer { .and_then(|arguments| arguments.get("message")) .and_then(|value| value.as_str()) .unwrap_or_default(); + let thread_id = context + .meta + .0 + .get("threadId") + .and_then(|value| value.as_str()) + .unwrap_or_default(); let mut meta = Meta::new(); meta.0.insert("calledBy".to_string(), json!("mcp-app")); let mut result = CallToolResult::structured(json!({ "echoed": message, + "threadId": thread_id, })); result.content = vec![Content::text(format!("echo: {message}"))]; result.meta = Some(meta); diff --git a/codex-rs/core/src/mcp_tool_call.rs b/codex-rs/core/src/mcp_tool_call.rs index c5d4068d27..82bbc7ca4a 100644 --- a/codex-rs/core/src/mcp_tool_call.rs +++ b/codex-rs/core/src/mcp_tool_call.rs @@ -477,6 +477,8 @@ async fn execute_mcp_tool_call( metadata.and_then(|metadata| metadata.openai_file_input_params.as_deref()), ) .await?; + let request_meta = + with_mcp_tool_call_thread_id_meta(request_meta, &sess.conversation_id.to_string()); let request_meta = augment_mcp_tool_request_meta_with_sandbox_state(sess, turn_context, server, request_meta) .await @@ -660,6 +662,7 @@ pub(crate) struct McpToolApprovalMetadata { const MCP_TOOL_CODEX_APPS_META_KEY: &str = "_codex_apps"; const MCP_TOOL_OPENAI_OUTPUT_TEMPLATE_META_KEY: &str = "openai/outputTemplate"; const MCP_TOOL_UI_RESOURCE_URI_META_KEY: &str = "ui/resourceUri"; +const MCP_TOOL_THREAD_ID_META_KEY: &str = "threadId"; fn custom_mcp_tool_approval_mode( turn_context: &TurnContext, @@ -714,6 +717,30 @@ fn build_mcp_tool_call_request_meta( (!request_meta.is_empty()).then_some(serde_json::Value::Object(request_meta)) } +fn with_mcp_tool_call_thread_id_meta( + meta: Option, + thread_id: &str, +) -> Option { + match meta { + Some(serde_json::Value::Object(mut map)) => { + map.insert( + MCP_TOOL_THREAD_ID_META_KEY.to_string(), + serde_json::Value::String(thread_id.to_string()), + ); + Some(serde_json::Value::Object(map)) + } + None => { + let mut map = serde_json::Map::new(); + map.insert( + MCP_TOOL_THREAD_ID_META_KEY.to_string(), + serde_json::Value::String(thread_id.to_string()), + ); + Some(serde_json::Value::Object(map)) + } + other => other, + } +} + #[derive(Clone, Copy)] struct McpToolApprovalPromptOptions { allow_session_remember: bool, diff --git a/codex-rs/core/src/mcp_tool_call_tests.rs b/codex-rs/core/src/mcp_tool_call_tests.rs index 1893ef054c..d20f233951 100644 --- a/codex-rs/core/src/mcp_tool_call_tests.rs +++ b/codex-rs/core/src/mcp_tool_call_tests.rs @@ -651,6 +651,35 @@ async fn codex_apps_tool_call_request_meta_includes_turn_metadata_and_codex_apps ); } +#[test] +fn mcp_tool_call_thread_id_meta_is_added_to_request_meta() { + assert_eq!( + with_mcp_tool_call_thread_id_meta( + Some(serde_json::json!({ + "source": "test-client", + "threadId": "stale-thread", + })), + "thread-live", + ), + Some(serde_json::json!({ + "source": "test-client", + "threadId": "thread-live", + })) + ); + + assert_eq!( + with_mcp_tool_call_thread_id_meta(/*meta*/ None, "thread-live"), + Some(serde_json::json!({ + "threadId": "thread-live", + })) + ); + + assert_eq!( + with_mcp_tool_call_thread_id_meta(Some(serde_json::json!("invalid-meta")), "thread-live"), + Some(serde_json::json!("invalid-meta")) + ); +} + #[test] fn accepted_elicitation_content_converts_to_request_user_input_response() { let response = request_user_input_response_from_elicitation_content(Some(serde_json::json!( From 41652665f52674e3ad4eeb9570b49b073256a14b Mon Sep 17 00:00:00 2001 From: Casey Chow Date: Tue, 21 Apr 2026 13:10:36 -0400 Subject: [PATCH 04/22] [codex] Add tmux-aware OSC 9 notifications (#17836) ## Summary - wrap OSC 9 notifications in tmux's DCS passthrough so terminal notifications make it through tmux - use codex-terminal-detection for OSC 9 auto-selection so tmux sessions inherit the underlying client terminal support - add focused notification backend tests for plain OSC 9 and tmux-wrapped output ## Stack - base PR: #18479 - review order: #18479, then this PR ## Why Tmux does not forward OSC 9 notifications directly; the sequence has to be wrapped in tmux's DCS passthrough envelope. Codex also had local notification heuristics that could miss supported terminals when running under tmux, even though codex-terminal-detection already knows how to attribute tmux sessions to the client terminal. ## Validation - `just fmt` - `cargo test -p codex-tui` *(currently blocked by an unrelated existing compile error in `app-server/src/message_processor.rs:754` referencing `connection_id` out of scope; not caused by this change)* Co-authored-by: Codex --- codex-rs/terminal-detection/src/lib.rs | 8 +- .../terminal-detection/src/terminal_tests.rs | 38 +++++ codex-rs/tui/src/notifications/mod.rs | 131 ++++++++---------- codex-rs/tui/src/notifications/osc9.rs | 100 ++++++++++++- 4 files changed, 192 insertions(+), 85 deletions(-) diff --git a/codex-rs/terminal-detection/src/lib.rs b/codex-rs/terminal-detection/src/lib.rs index 69b6f47459..6474c189a5 100644 --- a/codex-rs/terminal-detection/src/lib.rs +++ b/codex-rs/terminal-detection/src/lib.rs @@ -145,10 +145,10 @@ impl TerminalInfo { /// Creates terminal metadata from a `TERM` capability value. fn from_term(term: String, multiplexer: Option) -> Self { - let name = if term == "dumb" { - TerminalName::Dumb - } else { - TerminalName::Unknown + let name = match term.as_str() { + "dumb" => TerminalName::Dumb, + "wezterm" | "wezterm-mux" => TerminalName::WezTerm, + _ => TerminalName::Unknown, }; Self::new( name, diff --git a/codex-rs/terminal-detection/src/terminal_tests.rs b/codex-rs/terminal-detection/src/terminal_tests.rs index 52974a0c31..ec2a3b1166 100644 --- a/codex-rs/terminal-detection/src/terminal_tests.rs +++ b/codex-rs/terminal-detection/src/terminal_tests.rs @@ -456,6 +456,44 @@ fn detects_wezterm() { "WezTerm", "wezterm_empty_user_agent" ); + + let env = FakeEnvironment::new().with_var("TERM", "wezterm"); + let terminal = detect_terminal_info_from_env(&env); + assert_eq!( + terminal, + terminal_info( + TerminalName::WezTerm, + /*term_program*/ None, + /*version*/ None, + Some("wezterm"), + /*multiplexer*/ None + ), + "wezterm_term_info" + ); + assert_eq!( + terminal.user_agent_token(), + "wezterm", + "wezterm_term_user_agent" + ); + + let env = FakeEnvironment::new().with_var("TERM", "wezterm-mux"); + let terminal = detect_terminal_info_from_env(&env); + assert_eq!( + terminal, + terminal_info( + TerminalName::WezTerm, + /*term_program*/ None, + /*version*/ None, + Some("wezterm-mux"), + /*multiplexer*/ None + ), + "wezterm_mux_term_info" + ); + assert_eq!( + terminal.user_agent_token(), + "wezterm-mux", + "wezterm_mux_term_user_agent" + ); } #[test] diff --git a/codex-rs/tui/src/notifications/mod.rs b/codex-rs/tui/src/notifications/mod.rs index e5e1085995..ee6694b357 100644 --- a/codex-rs/tui/src/notifications/mod.rs +++ b/codex-rs/tui/src/notifications/mod.rs @@ -1,11 +1,13 @@ mod bel; mod osc9; -use std::env; use std::io; use bel::BelBackend; use codex_config::types::NotificationMethod; +use codex_terminal_detection::TerminalInfo; +use codex_terminal_detection::TerminalName; +use codex_terminal_detection::terminal_info; use osc9::Osc9Backend; #[derive(Debug)] @@ -18,13 +20,13 @@ impl DesktopNotificationBackend { pub fn for_method(method: NotificationMethod) -> Self { match method { NotificationMethod::Auto => { - if supports_osc9() { - Self::Osc9(Osc9Backend) + if supports_osc9(&terminal_info()) { + Self::Osc9(Osc9Backend::new()) } else { Self::Bel(BelBackend) } } - NotificationMethod::Osc9 => Self::Osc9(Osc9Backend), + NotificationMethod::Osc9 => Self::Osc9(Osc9Backend::new()), NotificationMethod::Bel => Self::Bel(BelBackend), } } @@ -48,67 +50,33 @@ pub fn detect_backend(method: NotificationMethod) -> DesktopNotificationBackend DesktopNotificationBackend::for_method(method) } -fn supports_osc9() -> bool { - if env::var_os("WT_SESSION").is_some() { - return false; - } - // Prefer TERM_PROGRAM when present, but keep fallbacks for shells/launchers - // that don't set it (e.g., tmux/ssh) to avoid regressing OSC 9 support. - if matches!( - env::var("TERM_PROGRAM").ok().as_deref(), - Some("WezTerm" | "WarpTerminal" | "ghostty") - ) { - return true; - } - // iTerm still provides a strong session signal even when TERM_PROGRAM is missing. - if env::var_os("ITERM_SESSION_ID").is_some() { - return true; - } - // TERM-based hints cover kitty/wezterm setups without TERM_PROGRAM. +fn supports_osc9(terminal: &TerminalInfo) -> bool { matches!( - env::var("TERM").ok().as_deref(), - Some("xterm-kitty" | "wezterm" | "wezterm-mux") + terminal.name, + TerminalName::Ghostty + | TerminalName::Iterm2 + | TerminalName::Kitty + | TerminalName::WarpTerminal + | TerminalName::WezTerm ) } #[cfg(test)] mod tests { use super::detect_backend; + use super::supports_osc9; use codex_config::types::NotificationMethod; - use serial_test::serial; - use std::ffi::OsString; + use codex_terminal_detection::TerminalInfo; + use codex_terminal_detection::TerminalName; + use pretty_assertions::assert_eq; - struct EnvVarGuard { - key: &'static str, - original: Option, - } - - impl EnvVarGuard { - fn set(key: &'static str, value: &str) -> Self { - let original = std::env::var_os(key); - unsafe { - std::env::set_var(key, value); - } - Self { key, original } - } - - fn remove(key: &'static str) -> Self { - let original = std::env::var_os(key); - unsafe { - std::env::remove_var(key); - } - Self { key, original } - } - } - - impl Drop for EnvVarGuard { - fn drop(&mut self) { - unsafe { - match &self.original { - Some(value) => std::env::set_var(self.key, value), - None => std::env::remove_var(self.key), - } - } + fn test_terminal(name: TerminalName) -> TerminalInfo { + TerminalInfo { + name, + term_program: None, + version: None, + term: None, + multiplexer: None, } } @@ -129,28 +97,39 @@ mod tests { } #[test] - #[serial] - fn auto_prefers_bel_without_hints() { - let _term = EnvVarGuard::remove("TERM"); - let _term_program = EnvVarGuard::remove("TERM_PROGRAM"); - let _iterm = EnvVarGuard::remove("ITERM_SESSION_ID"); - let _wt = EnvVarGuard::remove("WT_SESSION"); - assert!(matches!( - detect_backend(NotificationMethod::Auto), - super::DesktopNotificationBackend::Bel(_) - )); + fn supports_osc9_for_supported_terminals() { + for name in [ + TerminalName::Ghostty, + TerminalName::Iterm2, + TerminalName::Kitty, + TerminalName::WarpTerminal, + TerminalName::WezTerm, + ] { + assert!( + supports_osc9(&test_terminal(name)), + "{name:?} should support OSC 9" + ); + } } #[test] - #[serial] - fn auto_uses_osc9_for_iterm() { - let _term = EnvVarGuard::remove("TERM"); - let _term_program = EnvVarGuard::remove("TERM_PROGRAM"); - let _iterm = EnvVarGuard::set("ITERM_SESSION_ID", "abc"); - let _wt = EnvVarGuard::remove("WT_SESSION"); - assert!(matches!( - detect_backend(NotificationMethod::Auto), - super::DesktopNotificationBackend::Osc9(_) - )); + fn supports_osc9_for_unsupported_terminals() { + for name in [ + TerminalName::AppleTerminal, + TerminalName::Alacritty, + TerminalName::Dumb, + TerminalName::GnomeTerminal, + TerminalName::Konsole, + TerminalName::Unknown, + TerminalName::VsCode, + TerminalName::Vte, + TerminalName::WindowsTerminal, + ] { + assert_eq!( + supports_osc9(&test_terminal(name)), + false, + "{name:?} should not support OSC 9" + ); + } } } diff --git a/codex-rs/tui/src/notifications/osc9.rs b/codex-rs/tui/src/notifications/osc9.rs index fd3a8ad96d..bbe48f176f 100644 --- a/codex-rs/tui/src/notifications/osc9.rs +++ b/codex-rs/tui/src/notifications/osc9.rs @@ -2,25 +2,55 @@ use std::fmt; use std::io; use std::io::stdout; +use codex_terminal_detection::Multiplexer; +use codex_terminal_detection::terminal_info; use crossterm::Command; use ratatui::crossterm::execute; -#[derive(Debug, Default)] -pub struct Osc9Backend; +#[derive(Debug)] +pub struct Osc9Backend { + dcs_passthrough: bool, +} + +impl Default for Osc9Backend { + fn default() -> Self { + Self::new() + } +} impl Osc9Backend { + pub fn new() -> Self { + Self { + dcs_passthrough: matches!(terminal_info().multiplexer, Some(Multiplexer::Tmux { .. })), + } + } + pub fn notify(&mut self, message: &str) -> io::Result<()> { - execute!(stdout(), PostNotification(message.to_string())) + execute!( + stdout(), + PostNotification { + message: message.to_string(), + dcs_passthrough: self.dcs_passthrough, + } + ) } } /// Command that emits an OSC 9 desktop notification with a message. #[derive(Debug, Clone)] -pub struct PostNotification(pub String); +pub struct PostNotification { + pub message: String, + pub dcs_passthrough: bool, +} impl Command for PostNotification { fn write_ansi(&self, f: &mut impl fmt::Write) -> fmt::Result { - write!(f, "\x1b]9;{}\x07", self.0) + if self.dcs_passthrough { + let escaped_message = escape_tmux_dcs_passthrough_payload(&self.message); + write!(f, "\x1bPtmux;\x1b\x1b]9;{escaped_message}\x07\x1b\\") + } else { + write!(f, "\x1b]9;{}\x07", self.message) + } } #[cfg(windows)] @@ -35,3 +65,63 @@ impl Command for PostNotification { true } } + +fn escape_tmux_dcs_passthrough_payload(message: &str) -> String { + message.replace('\u{1b}', "\u{1b}\u{1b}") +} + +#[cfg(test)] +mod tests { + use crossterm::Command; + use pretty_assertions::assert_eq; + + use super::PostNotification; + + #[test] + fn post_notification_writes_plain_osc9_sequence() { + let mut ansi = String::new(); + let command = PostNotification { + message: "hello".to_string(), + dcs_passthrough: false, + }; + + command + .write_ansi(&mut ansi) + .expect("OSC 9 command should format"); + + assert_eq!(ansi, "\u{1b}]9;hello\u{7}"); + } + + #[test] + fn post_notification_writes_tmux_dcs_wrapped_osc9_sequence() { + let mut ansi = String::new(); + let command = PostNotification { + message: "done".to_string(), + dcs_passthrough: true, + }; + + command + .write_ansi(&mut ansi) + .expect("OSC 9 command should format"); + + assert_eq!(ansi, "\u{1b}Ptmux;\u{1b}\u{1b}]9;done\u{7}\u{1b}\\"); + } + + #[test] + fn post_notification_escapes_escape_bytes_inside_tmux_payload() { + let mut ansi = String::new(); + let command = PostNotification { + message: "danger\u{1b}[31m".to_string(), + dcs_passthrough: true, + }; + + command + .write_ansi(&mut ansi) + .expect("OSC 9 command should format"); + + assert_eq!( + ansi, + "\u{1b}Ptmux;\u{1b}\u{1b}]9;danger\u{1b}\u{1b}[31m\u{7}\u{1b}\\" + ); + } +} From b7fec54354096e3c1884de67b8f2ffbdf836f63e Mon Sep 17 00:00:00 2001 From: Eric Traut Date: Tue, 21 Apr 2026 10:13:13 -0700 Subject: [PATCH 05/22] Queue follow-up input during user shell commands (#18820) Fixes #17954. ## Why When a manual shell command like `!sleep 10` is running, submitting plain text such as `hi` currently sends that text as a steer for the active shell turn. User shell turns are not steerable like model turns, so the TUI can remain stuck in `Working` after the shell command finishes. ## What Changed - Detect when the only active work is one or more `ExecCommandSource::UserShell` commands. - Queue plain submitted input in that state so it drains after the shell command and shell turn complete. - Preserve `!cmd` submissions during running work so explicit shell commands keep their existing behavior. - Add regression coverage for the `!sleep 10` plus `hi` flow in `chatwidget::tests::exec_flow::user_message_during_user_shell_command_is_queued_not_steered`. ## Verification - Manually confirmed hang before the fix and no hang after the fix --- codex-rs/tui/src/chatwidget.rs | 15 ++++++ .../tui/src/chatwidget/tests/exec_flow.rs | 52 +++++++++++++++++++ 2 files changed, 67 insertions(+) diff --git a/codex-rs/tui/src/chatwidget.rs b/codex-rs/tui/src/chatwidget.rs index 860846f281..d226fa3c8a 100644 --- a/codex-rs/tui/src/chatwidget.rs +++ b/codex-rs/tui/src/chatwidget.rs @@ -5411,6 +5411,12 @@ impl ChatWidget { let should_submit_now = self.is_session_configured() && !self.is_plan_streaming_in_tui(); if should_submit_now { + if self.only_user_shell_commands_running() + && !user_message.text.starts_with('!') + { + self.queue_user_message(user_message); + return; + } // Submitted is emitted when user submits. // Reset any reasoning header only when we are actually submitting a turn. self.reasoning_buffer.clear(); @@ -7476,6 +7482,15 @@ impl ChatWidget { self.user_turn_pending_start || self.bottom_pane.is_task_running() } + fn only_user_shell_commands_running(&self) -> bool { + self.agent_turn_running + && !self.running_commands.is_empty() + && self + .running_commands + .values() + .all(|command| command.source == ExecCommandSource::UserShell) + } + /// Rebuild and update the bottom-pane pending-input preview. fn refresh_pending_input_preview(&mut self) { let queued_messages: Vec = self diff --git a/codex-rs/tui/src/chatwidget/tests/exec_flow.rs b/codex-rs/tui/src/chatwidget/tests/exec_flow.rs index 9f181d3d21..04a5fa44ba 100644 --- a/codex-rs/tui/src/chatwidget/tests/exec_flow.rs +++ b/codex-rs/tui/src/chatwidget/tests/exec_flow.rs @@ -1036,6 +1036,58 @@ async fn bang_shell_enter_while_task_running_submits_run_user_shell_command() { assert_matches!(rx.try_recv(), Err(TryRecvError::Empty)); } +#[tokio::test] +async fn user_message_during_user_shell_command_is_queued_not_steered() { + let (mut chat, _rx, mut op_rx) = make_chatwidget_manual(/*model_override*/ None).await; + chat.thread_id = Some(ThreadId::new()); + chat.handle_codex_event(Event { + id: "turn-start".into(), + msg: EventMsg::TurnStarted(TurnStartedEvent { + turn_id: "turn-1".to_string(), + started_at: None, + model_context_window: None, + collaboration_mode_kind: ModeKind::Default, + }), + }); + let begin = begin_exec_with_source( + &mut chat, + "user-shell-sleep", + "sleep 10", + ExecCommandSource::UserShell, + ); + + assert!(chat.only_user_shell_commands_running()); + chat.bottom_pane + .set_composer_text("hi".to_string(), Vec::new(), Vec::new()); + chat.handle_key_event(KeyEvent::new(KeyCode::Enter, KeyModifiers::NONE)); + + assert_matches!(op_rx.try_recv(), Err(TryRecvError::Empty)); + assert_eq!(chat.queued_user_message_texts(), vec!["hi".to_string()]); + + end_exec(&mut chat, begin, "", "", /*exit_code*/ 0); + chat.handle_codex_event(Event { + id: "turn-complete".into(), + msg: EventMsg::TurnComplete(TurnCompleteEvent { + turn_id: "turn-1".to_string(), + last_agent_message: Some("done".to_string()), + completed_at: None, + duration_ms: None, + }), + }); + + match next_submit_op(&mut op_rx) { + Op::UserTurn { items, .. } => assert_eq!( + items, + vec![UserInput::Text { + text: "hi".to_string(), + text_elements: Vec::new(), + }] + ), + other => panic!("expected queued user message after shell completion, got {other:?}"), + } + assert!(chat.queued_user_messages.is_empty()); +} + #[tokio::test] async fn disabled_slash_command_while_task_running_snapshot() { // Build a chat widget and simulate an active task From 10e1659d4fcd8960315ceab9da138f94320b4986 Mon Sep 17 00:00:00 2001 From: jif-oai Date: Tue, 21 Apr 2026 18:15:37 +0100 Subject: [PATCH 06/22] Stabilize debug clear memories integration test (#18858) ## Why `debug_clear_memories_resets_state_and_removes_memory_dir` can be flaky because the test drops its `sqlx::SqlitePool` immediately before invoking `codex debug clear-memories`. Dropping the pool does not wait for all SQLite connections to close, so the CLI can race with still-open test connections. ## What changed - Await `pool.close()` before spawning `codex debug clear-memories`. - Close the reopened verification pool before the temp `CODEX_HOME` is torn down. ## Verification - `cargo test -p codex-cli --test debug_clear_memories debug_clear_memories_resets_state_and_removes_memory_dir` --- codex-rs/cli/tests/debug_clear_memories.rs | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/codex-rs/cli/tests/debug_clear_memories.rs b/codex-rs/cli/tests/debug_clear_memories.rs index a24f7ebdde..9d5e114dbf 100644 --- a/codex-rs/cli/tests/debug_clear_memories.rs +++ b/codex-rs/cli/tests/debug_clear_memories.rs @@ -105,7 +105,7 @@ INSERT INTO jobs ( let memory_root = codex_home.path().join("memories"); std::fs::create_dir_all(&memory_root)?; std::fs::write(memory_root.join("memory_summary.md"), "stale memory")?; - drop(pool); + pool.close().await; let mut cmd = codex_command(codex_home.path())?; cmd.args(["debug", "clear-memories"]) @@ -127,6 +127,7 @@ INSERT INTO jobs ( assert_eq!(memory_jobs_count, 0); assert!(memory_root.exists()); assert_eq!(std::fs::read_dir(memory_root)?.count(), 0); + pool.close().await; Ok(()) } From 4ed722ab8d2fd195c652a7cbfcc31844318cd4ea Mon Sep 17 00:00:00 2001 From: Eric Traut Date: Tue, 21 Apr 2026 10:16:51 -0700 Subject: [PATCH 07/22] Move TUI app tests to modules they cover (#18799) ## Summary The TUI app refactor in #18753 moved the old `app.rs` tests into a single `app/tests.rs` file. That kept the split mechanically simple, but it left several focused unit tests far from the modules they exercise. This PR is a follow-up that moves tests next to the code they cover. It also adds `tui/src/app/test_support.rs` for shared fixture construction. This is just a mechanical refactoring (no functional changes) and does not affect any production code. --- codex-rs/tui/src/app.rs | 4 +- codex-rs/tui/src/app/background_requests.rs | 145 +++++ codex-rs/tui/src/app/config_persistence.rs | 173 ++++++ codex-rs/tui/src/app/platform_actions.rs | 35 ++ codex-rs/tui/src/app/session_lifecycle.rs | 58 ++ codex-rs/tui/src/app/side.rs | 51 ++ codex-rs/tui/src/app/startup_prompts.rs | 28 + codex-rs/tui/src/app/test_support.rs | 89 +++ codex-rs/tui/src/app/tests.rs | 626 -------------------- codex-rs/tui/src/app/thread_events.rs | 274 +++++++++ 10 files changed, 855 insertions(+), 628 deletions(-) create mode 100644 codex-rs/tui/src/app/test_support.rs diff --git a/codex-rs/tui/src/app.rs b/codex-rs/tui/src/app.rs index 80e8f4a0f1..f2dfd5fa2b 100644 --- a/codex-rs/tui/src/app.rs +++ b/codex-rs/tui/src/app.rs @@ -200,8 +200,6 @@ mod thread_session_state; use self::agent_navigation::AgentNavigationDirection; use self::agent_navigation::AgentNavigationState; use self::app_server_requests::PendingAppServerRequests; -#[cfg(test)] -use self::background_requests::*; use self::loaded_threads::find_loaded_subagent_threads_for_primary; use self::pending_interactive_replay::PendingInteractiveReplayState; use self::platform_actions::*; @@ -1152,5 +1150,7 @@ impl Drop for App { } } +#[cfg(test)] +pub(super) mod test_support; #[cfg(test)] mod tests; diff --git a/codex-rs/tui/src/app/background_requests.rs b/codex-rs/tui/src/app/background_requests.rs index 9fc741ff5c..80879dbd79 100644 --- a/codex-rs/tui/src/app/background_requests.rs +++ b/codex-rs/tui/src/app/background_requests.rs @@ -637,3 +637,148 @@ pub(super) fn mcp_inventory_maps_from_statuses(statuses: Vec) - (tools, resources, resource_templates, auth_statuses) } + +#[cfg(test)] +mod tests { + use super::*; + use codex_app_server_protocol::PluginMarketplaceEntry; + use codex_protocol::mcp::Tool; + use codex_utils_absolute_path::AbsolutePathBuf; + use pretty_assertions::assert_eq; + + fn test_absolute_path(path: &str) -> AbsolutePathBuf { + AbsolutePathBuf::try_from(PathBuf::from(path)).expect("absolute test path") + } + + #[test] + fn hide_cli_only_plugin_marketplaces_removes_openai_bundled() { + let mut response = PluginListResponse { + marketplaces: vec![ + PluginMarketplaceEntry { + name: "openai-bundled".to_string(), + path: Some(test_absolute_path("/marketplaces/openai-bundled")), + interface: None, + plugins: Vec::new(), + }, + PluginMarketplaceEntry { + name: "openai-curated".to_string(), + path: Some(test_absolute_path("/marketplaces/openai-curated")), + interface: None, + plugins: Vec::new(), + }, + ], + marketplace_load_errors: Vec::new(), + featured_plugin_ids: Vec::new(), + }; + + hide_cli_only_plugin_marketplaces(&mut response); + + assert_eq!( + response.marketplaces, + vec![PluginMarketplaceEntry { + name: "openai-curated".to_string(), + path: Some(test_absolute_path("/marketplaces/openai-curated")), + interface: None, + plugins: Vec::new(), + }] + ); + } + + #[test] + fn mcp_inventory_maps_prefix_tool_names_by_server() { + let statuses = vec![ + McpServerStatus { + name: "docs".to_string(), + tools: HashMap::from([( + "list".to_string(), + Tool { + description: None, + name: "list".to_string(), + title: None, + input_schema: serde_json::json!({"type": "object"}), + output_schema: None, + annotations: None, + icons: None, + meta: None, + }, + )]), + resources: Vec::new(), + resource_templates: Vec::new(), + auth_status: codex_app_server_protocol::McpAuthStatus::Unsupported, + }, + McpServerStatus { + name: "disabled".to_string(), + tools: HashMap::new(), + resources: Vec::new(), + resource_templates: Vec::new(), + auth_status: codex_app_server_protocol::McpAuthStatus::Unsupported, + }, + ]; + + let (tools, resources, resource_templates, auth_statuses) = + mcp_inventory_maps_from_statuses(statuses); + let mut resource_names = resources.keys().cloned().collect::>(); + resource_names.sort(); + let mut template_names = resource_templates.keys().cloned().collect::>(); + template_names.sort(); + + assert_eq!( + tools.keys().cloned().collect::>(), + vec!["mcp__docs__list".to_string()] + ); + assert_eq!(resource_names, vec!["disabled", "docs"]); + assert_eq!(template_names, vec!["disabled", "docs"]); + assert_eq!( + auth_statuses.get("disabled"), + Some(&McpAuthStatus::Unsupported) + ); + } + + #[test] + fn build_feedback_upload_params_includes_thread_id_and_rollout_path() { + let thread_id = ThreadId::new(); + let rollout_path = PathBuf::from("/tmp/rollout.jsonl"); + + let params = build_feedback_upload_params( + Some(thread_id), + Some(rollout_path.clone()), + FeedbackCategory::SafetyCheck, + Some("needs follow-up".to_string()), + Some("turn-123".to_string()), + /*include_logs*/ true, + ); + + assert_eq!(params.classification, "safety_check"); + assert_eq!(params.reason, Some("needs follow-up".to_string())); + assert_eq!(params.thread_id, Some(thread_id.to_string())); + assert_eq!( + params + .tags + .as_ref() + .and_then(|tags| tags.get("turn_id")) + .map(String::as_str), + Some("turn-123") + ); + assert_eq!(params.include_logs, true); + assert_eq!(params.extra_log_files, Some(vec![rollout_path])); + } + + #[test] + fn build_feedback_upload_params_omits_rollout_path_without_logs() { + let params = build_feedback_upload_params( + /*origin_thread_id*/ None, + Some(PathBuf::from("/tmp/rollout.jsonl")), + FeedbackCategory::GoodResult, + /*reason*/ None, + /*turn_id*/ None, + /*include_logs*/ false, + ); + + assert_eq!(params.classification, "good_result"); + assert_eq!(params.reason, None); + assert_eq!(params.thread_id, None); + assert_eq!(params.tags, None); + assert_eq!(params.include_logs, false); + assert_eq!(params.extra_log_files, None); + } +} diff --git a/codex-rs/tui/src/app/config_persistence.rs b/codex-rs/tui/src/app/config_persistence.rs index c909a37c1f..edfde7ceeb 100644 --- a/codex-rs/tui/src/app/config_persistence.rs +++ b/codex-rs/tui/src/app/config_persistence.rs @@ -537,3 +537,176 @@ impl App { } } } + +#[cfg(test)] +mod tests { + use super::*; + use crate::app::test_support::app_enabled_in_effective_config; + use crate::app::test_support::make_test_app; + use crate::test_support::PathBufExt; + use codex_protocol::protocol::Event; + use codex_protocol::protocol::EventMsg; + use codex_protocol::protocol::SessionConfiguredEvent; + use pretty_assertions::assert_eq; + use tempfile::tempdir; + + #[tokio::test] + async fn update_reasoning_effort_updates_collaboration_mode() { + let mut app = make_test_app().await; + app.chat_widget + .set_reasoning_effort(Some(ReasoningEffortConfig::Medium)); + + app.on_update_reasoning_effort(Some(ReasoningEffortConfig::High)); + + assert_eq!( + app.chat_widget.current_reasoning_effort(), + Some(ReasoningEffortConfig::High) + ); + assert_eq!( + app.config.model_reasoning_effort, + Some(ReasoningEffortConfig::High) + ); + } + + #[tokio::test] + async fn refresh_in_memory_config_from_disk_loads_latest_apps_state() -> Result<()> { + let mut app = make_test_app().await; + let codex_home = tempdir()?; + app.config.codex_home = codex_home.path().to_path_buf().abs(); + let app_id = "unit_test_refresh_in_memory_config_connector".to_string(); + + assert_eq!(app_enabled_in_effective_config(&app.config, &app_id), None); + + ConfigEditsBuilder::new(&app.config.codex_home) + .with_edits([ + ConfigEdit::SetPath { + segments: vec!["apps".to_string(), app_id.clone(), "enabled".to_string()], + value: false.into(), + }, + ConfigEdit::SetPath { + segments: vec![ + "apps".to_string(), + app_id.clone(), + "disabled_reason".to_string(), + ], + value: "user".into(), + }, + ]) + .apply() + .await + .expect("persist app toggle"); + + assert_eq!(app_enabled_in_effective_config(&app.config, &app_id), None); + + app.refresh_in_memory_config_from_disk().await?; + + assert_eq!( + app_enabled_in_effective_config(&app.config, &app_id), + Some(false) + ); + Ok(()) + } + + #[tokio::test] + async fn refresh_in_memory_config_from_disk_best_effort_keeps_current_config_on_error() + -> Result<()> { + let mut app = make_test_app().await; + let codex_home = tempdir()?; + app.config.codex_home = codex_home.path().to_path_buf().abs(); + std::fs::write(codex_home.path().join("config.toml"), "[broken")?; + let original_config = app.config.clone(); + + app.refresh_in_memory_config_from_disk_best_effort("starting a new thread") + .await; + + assert_eq!(app.config, original_config); + Ok(()) + } + + #[tokio::test] + async fn refresh_in_memory_config_from_disk_uses_active_chat_widget_cwd() -> Result<()> { + let mut app = make_test_app().await; + let original_cwd = app.config.cwd.clone(); + let next_cwd_tmp = tempdir()?; + let next_cwd = next_cwd_tmp.path().to_path_buf(); + + app.chat_widget.handle_codex_event(Event { + id: String::new(), + msg: EventMsg::SessionConfigured(SessionConfiguredEvent { + session_id: ThreadId::new(), + forked_from_id: None, + thread_name: None, + model: "gpt-test".to_string(), + model_provider_id: "test-provider".to_string(), + service_tier: None, + approval_policy: AskForApproval::Never, + approvals_reviewer: ApprovalsReviewer::User, + sandbox_policy: SandboxPolicy::new_read_only_policy(), + cwd: next_cwd.clone().abs(), + reasoning_effort: None, + history_log_id: 0, + history_entry_count: 0, + initial_messages: None, + network_proxy: None, + rollout_path: Some(PathBuf::new()), + }), + }); + + assert_eq!(app.chat_widget.config_ref().cwd.to_path_buf(), next_cwd); + assert_eq!(app.config.cwd, original_cwd); + + app.refresh_in_memory_config_from_disk().await?; + + assert_eq!(app.config.cwd, app.chat_widget.config_ref().cwd); + Ok(()) + } + + #[tokio::test] + async fn rebuild_config_for_resume_or_fallback_uses_current_config_on_same_cwd_error() + -> Result<()> { + let mut app = make_test_app().await; + let codex_home = tempdir()?; + app.config.codex_home = codex_home.path().to_path_buf().abs(); + std::fs::write(codex_home.path().join("config.toml"), "[broken")?; + let current_config = app.config.clone(); + let current_cwd = current_config.cwd.clone(); + + let resume_config = app + .rebuild_config_for_resume_or_fallback(¤t_cwd, current_cwd.to_path_buf()) + .await?; + + assert_eq!(resume_config, current_config); + Ok(()) + } + + #[tokio::test] + async fn rebuild_config_for_resume_or_fallback_errors_when_cwd_changes() -> Result<()> { + let mut app = make_test_app().await; + let codex_home = tempdir()?; + app.config.codex_home = codex_home.path().to_path_buf().abs(); + std::fs::write(codex_home.path().join("config.toml"), "[broken")?; + let current_cwd = app.config.cwd.clone(); + let next_cwd_tmp = tempdir()?; + let next_cwd = next_cwd_tmp.path().to_path_buf(); + + let result = app + .rebuild_config_for_resume_or_fallback(¤t_cwd, next_cwd) + .await; + + assert!(result.is_err()); + Ok(()) + } + + #[tokio::test] + async fn sync_tui_theme_selection_updates_chat_widget_config_copy() { + let mut app = make_test_app().await; + + app.sync_tui_theme_selection("dracula".to_string()); + + assert_eq!(app.config.tui_theme.as_deref(), Some("dracula")); + assert_eq!( + app.chat_widget.config_ref().tui_theme.as_deref(), + Some("dracula") + ); + } +} diff --git a/codex-rs/tui/src/app/platform_actions.rs b/codex-rs/tui/src/app/platform_actions.rs index 36559feb94..b8e2888352 100644 --- a/codex-rs/tui/src/app/platform_actions.rs +++ b/codex-rs/tui/src/app/platform_actions.rs @@ -59,3 +59,38 @@ pub(super) fn side_return_shortcut_matches(key_event: KeyEvent) -> bool { _ => false, } } + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn side_return_shortcuts_match_esc_and_ctrl_c() { + assert!(side_return_shortcut_matches(KeyEvent::new( + KeyCode::Esc, + KeyModifiers::NONE, + ))); + assert!(side_return_shortcut_matches(KeyEvent::new_with_kind( + KeyCode::Esc, + KeyModifiers::NONE, + KeyEventKind::Repeat, + ))); + assert!(side_return_shortcut_matches(KeyEvent::new( + KeyCode::Char('c'), + KeyModifiers::CONTROL, + ))); + assert!(side_return_shortcut_matches(KeyEvent::new( + KeyCode::Char('C'), + KeyModifiers::CONTROL, + ))); + assert!(!side_return_shortcut_matches(KeyEvent::new( + KeyCode::Char('d'), + KeyModifiers::CONTROL, + ))); + assert!(!side_return_shortcut_matches(KeyEvent::new_with_kind( + KeyCode::Esc, + KeyModifiers::NONE, + KeyEventKind::Release, + ))); + } +} diff --git a/codex-rs/tui/src/app/session_lifecycle.rs b/codex-rs/tui/src/app/session_lifecycle.rs index 0551d32538..89d5e1d43e 100644 --- a/codex-rs/tui/src/app/session_lifecycle.rs +++ b/codex-rs/tui/src/app/session_lifecycle.rs @@ -730,3 +730,61 @@ impl App { Ok(AppRunControl::Continue) } } + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn terminal_thread_read_error_detection_matches_not_loaded_errors() { + let err = color_eyre::eyre::eyre!( + "thread/read failed during TUI session lookup: thread/read failed: thread not loaded: thr_123" + ); + + assert!(App::is_terminal_thread_read_error(&err)); + } + + #[test] + fn terminal_thread_read_error_detection_ignores_transient_failures() { + let err = color_eyre::eyre::eyre!( + "thread/read failed during TUI session lookup: thread/read transport error: broken pipe" + ); + + assert!(!App::is_terminal_thread_read_error(&err)); + } + + #[test] + fn closed_state_for_thread_read_error_preserves_live_state_without_cache_on_transient_error() { + let err = color_eyre::eyre::eyre!( + "thread/read failed during TUI session lookup: thread/read transport error: broken pipe" + ); + + assert!(!App::closed_state_for_thread_read_error( + &err, /*existing_is_closed*/ None + )); + } + + #[test] + fn closed_state_for_thread_read_error_marks_terminal_uncached_threads_closed() { + let err = color_eyre::eyre::eyre!( + "thread/read failed during TUI session lookup: thread/read failed: thread not loaded: thr_123" + ); + + assert!(App::closed_state_for_thread_read_error( + &err, /*existing_is_closed*/ None + )); + } + + #[test] + fn include_turns_fallback_detection_handles_unmaterialized_and_ephemeral_threads() { + let unmaterialized = color_eyre::eyre::eyre!( + "thread/read failed during TUI session lookup: thread/read failed: thread thr_123 is not materialized yet; includeTurns is unavailable before first user message" + ); + let ephemeral = color_eyre::eyre::eyre!( + "thread/read failed during TUI session lookup: thread/read failed: ephemeral threads do not support includeTurns" + ); + + assert!(App::can_fallback_from_include_turns_error(&unmaterialized)); + assert!(App::can_fallback_from_include_turns_error(&ephemeral)); + } +} diff --git a/codex-rs/tui/src/app/side.rs b/codex-rs/tui/src/app/side.rs index e951233c5d..4ca3785ebf 100644 --- a/codex-rs/tui/src/app/side.rs +++ b/codex-rs/tui/src/app/side.rs @@ -97,6 +97,57 @@ impl SideParentStatus { } } +#[cfg(test)] +mod tests { + use super::*; + use pretty_assertions::assert_eq; + + #[test] + fn side_boundary_prompt_marks_inherited_history_reference_only() { + let item = App::side_boundary_prompt_item(); + let ResponseItem::Message { role, content, .. } = item else { + panic!("expected hidden side boundary prompt to be a user message"); + }; + assert_eq!(role, "user"); + let [ContentItem::InputText { text }] = content.as_slice() else { + panic!("expected hidden side boundary prompt text"); + }; + assert!(text.contains("Side conversation boundary.")); + assert!(text.contains("Everything before this boundary is inherited history")); + assert!(text.contains("It is not your current task.")); + assert!(text.contains("Only messages submitted after this boundary are active")); + assert!(text.contains("Do not continue, execute, or complete")); + assert!(text.contains("separate from the main thread")); + assert!( + text.contains("External tools may be available according to this thread's current") + ); + assert!(text.contains("Any tool calls or outputs visible before this boundary happened")); + assert!(text.contains("Do not modify files")); + } + + #[test] + fn side_start_error_message_explains_missing_first_prompt() { + let err = color_eyre::eyre::eyre!( + "thread/fork failed during TUI bootstrap: thread/fork failed: no rollout found for thread id 019da1a1-bed9-7a43-88a2-b49d43915021" + ); + + assert_eq!( + App::side_start_error_message(&err), + "'/side' is unavailable until the current conversation has started. Send a message first, then try /side again." + ); + } + + #[test] + fn side_start_error_message_uses_generic_start_wording() { + let err = color_eyre::eyre::eyre!("transport disconnected"); + + assert_eq!( + App::side_start_error_message(&err), + "Failed to start side conversation: transport disconnected" + ); + } +} + #[derive(Clone, Copy, Debug, PartialEq, Eq)] pub(super) enum SideParentStatusChange { Set(SideParentStatus), diff --git a/codex-rs/tui/src/app/startup_prompts.rs b/codex-rs/tui/src/app/startup_prompts.rs index 23c116ee5f..c5bc541a03 100644 --- a/codex-rs/tui/src/app/startup_prompts.rs +++ b/codex-rs/tui/src/app/startup_prompts.rs @@ -324,3 +324,31 @@ pub(super) fn normalize_harness_overrides_for_cwd( overrides.additional_writable_roots = normalized; Ok(overrides) } + +#[cfg(test)] +mod tests { + use super::*; + use crate::test_support::PathBufExt; + use pretty_assertions::assert_eq; + use std::path::PathBuf; + use tempfile::tempdir; + + #[test] + fn normalize_harness_overrides_resolves_relative_add_dirs() -> Result<()> { + let temp_dir = tempdir()?; + let base_cwd = temp_dir.path().join("base").abs(); + std::fs::create_dir_all(base_cwd.as_path())?; + + let overrides = ConfigOverrides { + additional_writable_roots: vec![PathBuf::from("rel")], + ..Default::default() + }; + let normalized = normalize_harness_overrides_for_cwd(overrides, &base_cwd)?; + + assert_eq!( + normalized.additional_writable_roots, + vec![base_cwd.join("rel").into_path_buf()] + ); + Ok(()) + } +} diff --git a/codex-rs/tui/src/app/test_support.rs b/codex-rs/tui/src/app/test_support.rs new file mode 100644 index 0000000000..8b2dfe8d47 --- /dev/null +++ b/codex-rs/tui/src/app/test_support.rs @@ -0,0 +1,89 @@ +//! Shared App fixtures for app submodule unit tests. +//! +//! This module keeps heavyweight `App` construction and config-inspection helpers available to +//! focused sibling test modules without making `app/tests.rs` the only practical place to test +//! app-owned behavior. + +use super::*; +use crate::chatwidget::tests::make_chatwidget_manual_with_sender; + +pub(super) async fn make_test_app() -> App { + let (chat_widget, app_event_tx, _rx, _op_rx) = make_chatwidget_manual_with_sender().await; + let config = chat_widget.config_ref().clone(); + let file_search = FileSearchManager::new(config.cwd.to_path_buf(), app_event_tx.clone()); + let model = crate::legacy_core::test_support::get_model_offline(config.model.as_deref()); + let session_telemetry = test_session_telemetry(&config, model.as_str()); + + App { + model_catalog: chat_widget.model_catalog(), + session_telemetry, + app_event_tx, + chat_widget, + config, + active_profile: None, + cli_kv_overrides: Vec::new(), + harness_overrides: ConfigOverrides::default(), + runtime_approval_policy_override: None, + runtime_sandbox_policy_override: None, + file_search, + transcript_cells: Vec::new(), + overlay: None, + deferred_history_lines: Vec::new(), + has_emitted_history_lines: false, + enhanced_keys_supported: false, + commit_anim_running: Arc::new(AtomicBool::new(false)), + status_line_invalid_items_warned: Arc::new(AtomicBool::new(false)), + terminal_title_invalid_items_warned: Arc::new(AtomicBool::new(false)), + backtrack: BacktrackState::default(), + backtrack_render_pending: false, + feedback: codex_feedback::CodexFeedback::new(), + feedback_audience: FeedbackAudience::External, + environment_manager: Arc::new(EnvironmentManager::new(/*exec_server_url*/ None)), + remote_app_server_url: None, + remote_app_server_auth_token: None, + pending_update_action: None, + pending_shutdown_exit_thread_id: None, + windows_sandbox: WindowsSandboxState::default(), + thread_event_channels: HashMap::new(), + thread_event_listener_tasks: HashMap::new(), + agent_navigation: AgentNavigationState::default(), + side_threads: HashMap::new(), + active_thread_id: None, + active_thread_rx: None, + primary_thread_id: None, + last_subagent_backfill_attempt: None, + primary_session_configured: None, + pending_primary_events: VecDeque::new(), + pending_app_server_requests: PendingAppServerRequests::default(), + pending_plugin_enabled_writes: HashMap::new(), + } +} + +fn test_session_telemetry(config: &Config, model: &str) -> SessionTelemetry { + let model_info = crate::legacy_core::test_support::construct_model_info_offline(model, config); + SessionTelemetry::new( + ThreadId::new(), + model, + model_info.slug.as_str(), + /*account_id*/ None, + /*account_email*/ None, + /*auth_mode*/ None, + "test_originator".to_string(), + /*log_user_prompts*/ false, + "test".to_string(), + SessionSource::Cli, + ) +} + +pub(super) fn app_enabled_in_effective_config(config: &Config, app_id: &str) -> Option { + config + .config_layer_stack + .effective_config() + .as_table() + .and_then(|table| table.get("apps")) + .and_then(TomlValue::as_table) + .and_then(|apps| apps.get(app_id)) + .and_then(TomlValue::as_table) + .and_then(|app| app.get("enabled")) + .and_then(TomlValue::as_bool) +} diff --git a/codex-rs/tui/src/app/tests.rs b/codex-rs/tui/src/app/tests.rs index 5ed786ef05..d34a49ec55 100644 --- a/codex-rs/tui/src/app/tests.rs +++ b/codex-rs/tui/src/app/tests.rs @@ -28,16 +28,6 @@ use codex_app_server_protocol::AdditionalPermissionProfile; use codex_app_server_protocol::AgentMessageDeltaNotification; use codex_app_server_protocol::CommandExecutionRequestApprovalParams; use codex_app_server_protocol::ConfigWarningNotification; -use codex_app_server_protocol::HookCompletedNotification; -use codex_app_server_protocol::HookEventName as AppServerHookEventName; -use codex_app_server_protocol::HookExecutionMode as AppServerHookExecutionMode; -use codex_app_server_protocol::HookHandlerType as AppServerHookHandlerType; -use codex_app_server_protocol::HookOutputEntry as AppServerHookOutputEntry; -use codex_app_server_protocol::HookOutputEntryKind as AppServerHookOutputEntryKind; -use codex_app_server_protocol::HookRunStatus as AppServerHookRunStatus; -use codex_app_server_protocol::HookRunSummary as AppServerHookRunSummary; -use codex_app_server_protocol::HookScope as AppServerHookScope; -use codex_app_server_protocol::HookStartedNotification; use codex_app_server_protocol::JSONRPCErrorError; use codex_app_server_protocol::McpServerStartupState; use codex_app_server_protocol::McpServerStatusUpdatedNotification; @@ -47,7 +37,6 @@ use codex_app_server_protocol::NetworkPolicyAmendment as AppServerNetworkPolicyA use codex_app_server_protocol::NetworkPolicyRuleAction as AppServerNetworkPolicyRuleAction; use codex_app_server_protocol::NonSteerableTurnKind as AppServerNonSteerableTurnKind; use codex_app_server_protocol::PermissionsRequestApprovalParams; -use codex_app_server_protocol::PluginMarketplaceEntry; use codex_app_server_protocol::RequestId as AppServerRequestId; use codex_app_server_protocol::ServerNotification; use codex_app_server_protocol::ServerRequest; @@ -72,14 +61,12 @@ use codex_protocol::config_types::CollaborationMode; use codex_protocol::config_types::CollaborationModeMask; use codex_protocol::config_types::ModeKind; use codex_protocol::config_types::Settings; -use codex_protocol::mcp::Tool; use codex_protocol::models::FileSystemPermissions; use codex_protocol::models::NetworkPermissions; use codex_protocol::models::PermissionProfile; use codex_protocol::protocol::AskForApproval; use codex_protocol::protocol::Event; use codex_protocol::protocol::EventMsg; -use codex_protocol::protocol::McpAuthStatus; use codex_protocol::protocol::NetworkApprovalContext; use codex_protocol::protocol::NetworkApprovalProtocol; use codex_protocol::protocol::RolloutItem; @@ -114,109 +101,6 @@ fn test_absolute_path(path: &str) -> AbsolutePathBuf { AbsolutePathBuf::try_from(PathBuf::from(path)).expect("absolute test path") } -#[test] -fn hide_cli_only_plugin_marketplaces_removes_openai_bundled() { - let mut response = PluginListResponse { - marketplaces: vec![ - PluginMarketplaceEntry { - name: "openai-bundled".to_string(), - path: Some(test_absolute_path("/marketplaces/openai-bundled")), - interface: None, - plugins: Vec::new(), - }, - PluginMarketplaceEntry { - name: "openai-curated".to_string(), - path: Some(test_absolute_path("/marketplaces/openai-curated")), - interface: None, - plugins: Vec::new(), - }, - ], - marketplace_load_errors: Vec::new(), - featured_plugin_ids: Vec::new(), - }; - - hide_cli_only_plugin_marketplaces(&mut response); - - assert_eq!( - response.marketplaces, - vec![PluginMarketplaceEntry { - name: "openai-curated".to_string(), - path: Some(test_absolute_path("/marketplaces/openai-curated")), - interface: None, - plugins: Vec::new(), - }] - ); -} - -#[test] -fn normalize_harness_overrides_resolves_relative_add_dirs() -> Result<()> { - let temp_dir = tempdir()?; - let base_cwd = temp_dir.path().join("base").abs(); - std::fs::create_dir_all(base_cwd.as_path())?; - - let overrides = ConfigOverrides { - additional_writable_roots: vec![PathBuf::from("rel")], - ..Default::default() - }; - let normalized = normalize_harness_overrides_for_cwd(overrides, &base_cwd)?; - - assert_eq!( - normalized.additional_writable_roots, - vec![base_cwd.join("rel").into_path_buf()] - ); - Ok(()) -} - -#[test] -fn mcp_inventory_maps_prefix_tool_names_by_server() { - let statuses = vec![ - McpServerStatus { - name: "docs".to_string(), - tools: HashMap::from([( - "list".to_string(), - Tool { - description: None, - name: "list".to_string(), - title: None, - input_schema: serde_json::json!({"type": "object"}), - output_schema: None, - annotations: None, - icons: None, - meta: None, - }, - )]), - resources: Vec::new(), - resource_templates: Vec::new(), - auth_status: codex_app_server_protocol::McpAuthStatus::Unsupported, - }, - McpServerStatus { - name: "disabled".to_string(), - tools: HashMap::new(), - resources: Vec::new(), - resource_templates: Vec::new(), - auth_status: codex_app_server_protocol::McpAuthStatus::Unsupported, - }, - ]; - - let (tools, resources, resource_templates, auth_statuses) = - mcp_inventory_maps_from_statuses(statuses); - let mut resource_names = resources.keys().cloned().collect::>(); - resource_names.sort(); - let mut template_names = resource_templates.keys().cloned().collect::>(); - template_names.sort(); - - assert_eq!( - tools.keys().cloned().collect::>(), - vec!["mcp__docs__list".to_string()] - ); - assert_eq!(resource_names, vec!["disabled", "docs"]); - assert_eq!(template_names, vec!["disabled", "docs"]); - assert_eq!( - auth_statuses.get("disabled"), - Some(&McpAuthStatus::Unsupported) - ); -} - #[tokio::test] async fn handle_mcp_inventory_result_clears_committed_loading_cell() { let mut app = make_test_app().await; @@ -1438,59 +1322,6 @@ async fn open_agent_picker_marks_terminal_read_errors_closed() -> Result<()> { Ok(()) } -#[test] -fn terminal_thread_read_error_detection_matches_not_loaded_errors() { - let err = color_eyre::eyre::eyre!( - "thread/read failed during TUI session lookup: thread/read failed: thread not loaded: thr_123" - ); - - assert!(App::is_terminal_thread_read_error(&err)); -} - -#[test] -fn terminal_thread_read_error_detection_ignores_transient_failures() { - let err = color_eyre::eyre::eyre!( - "thread/read failed during TUI session lookup: thread/read transport error: broken pipe" - ); - - assert!(!App::is_terminal_thread_read_error(&err)); -} - -#[test] -fn closed_state_for_thread_read_error_preserves_live_state_without_cache_on_transient_error() { - let err = color_eyre::eyre::eyre!( - "thread/read failed during TUI session lookup: thread/read transport error: broken pipe" - ); - - assert!(!App::closed_state_for_thread_read_error( - &err, /*existing_is_closed*/ None - )); -} - -#[test] -fn closed_state_for_thread_read_error_marks_terminal_uncached_threads_closed() { - let err = color_eyre::eyre::eyre!( - "thread/read failed during TUI session lookup: thread/read failed: thread not loaded: thr_123" - ); - - assert!(App::closed_state_for_thread_read_error( - &err, /*existing_is_closed*/ None - )); -} - -#[test] -fn include_turns_fallback_detection_handles_unmaterialized_and_ephemeral_threads() { - let unmaterialized = color_eyre::eyre::eyre!( - "thread/read failed during TUI session lookup: thread/read failed: thread thr_123 is not materialized yet; includeTurns is unavailable before first user message" - ); - let ephemeral = color_eyre::eyre::eyre!( - "thread/read failed during TUI session lookup: thread/read failed: ephemeral threads do not support includeTurns" - ); - - assert!(App::can_fallback_from_include_turns_error(&unmaterialized)); - assert!(App::can_fallback_from_include_turns_error(&ephemeral)); -} - #[tokio::test] async fn open_agent_picker_marks_loaded_threads_open() -> Result<()> { let mut app = make_test_app().await; @@ -3080,57 +2911,6 @@ async fn side_fork_config_is_ephemeral_and_appends_developer_guardrails() { assert!(app.transcript_cells.is_empty()); } -#[test] -fn side_boundary_prompt_marks_inherited_history_reference_only() { - let item = App::side_boundary_prompt_item(); - let codex_protocol::models::ResponseItem::Message { role, content, .. } = item else { - panic!("expected hidden side boundary prompt to be a user message"); - }; - assert_eq!(role, "user"); - let [codex_protocol::models::ContentItem::InputText { text }] = content.as_slice() else { - panic!("expected hidden side boundary prompt text"); - }; - assert!(text.contains("Side conversation boundary.")); - assert!(text.contains("Everything before this boundary is inherited history")); - assert!(text.contains("It is not your current task.")); - assert!(text.contains("Only messages submitted after this boundary are active")); - assert!(text.contains("Do not continue, execute, or complete")); - assert!(text.contains("separate from the main thread")); - assert!(text.contains("External tools may be available according to this thread's current")); - assert!(text.contains("Any tool calls or outputs visible before this boundary happened")); - assert!(text.contains("Do not modify files")); -} - -#[test] -fn side_return_shortcuts_match_esc_and_ctrl_c() { - assert!(side_return_shortcut_matches(KeyEvent::new( - KeyCode::Esc, - KeyModifiers::NONE, - ))); - assert!(side_return_shortcut_matches(KeyEvent::new_with_kind( - KeyCode::Esc, - KeyModifiers::NONE, - KeyEventKind::Repeat, - ))); - assert!(side_return_shortcut_matches(KeyEvent::new( - KeyCode::Char('c'), - KeyModifiers::CONTROL, - ))); - assert!(side_return_shortcut_matches(KeyEvent::new( - KeyCode::Char('C'), - KeyModifiers::CONTROL, - ))); - assert!(!side_return_shortcut_matches(KeyEvent::new( - KeyCode::Char('d'), - KeyModifiers::CONTROL, - ))); - assert!(!side_return_shortcut_matches(KeyEvent::new_with_kind( - KeyCode::Esc, - KeyModifiers::NONE, - KeyEventKind::Release, - ))); -} - #[tokio::test] async fn side_start_block_message_tracks_open_side_conversation() { let mut app = make_test_app().await; @@ -3281,28 +3061,6 @@ async fn side_parent_status_prioritizes_input_over_approval() -> Result<()> { Ok(()) } -#[test] -fn side_start_error_message_explains_missing_first_prompt() { - let err = color_eyre::eyre::eyre!( - "thread/fork failed during TUI bootstrap: thread/fork failed: no rollout found for thread id 019da1a1-bed9-7a43-88a2-b49d43915021" - ); - - assert_eq!( - App::side_start_error_message(&err), - "'/side' is unavailable until the current conversation has started. Send a message first, then try /side again." - ); -} - -#[test] -fn side_start_error_message_uses_generic_start_wording() { - let err = color_eyre::eyre::eyre!("transport disconnected"); - - assert_eq!( - App::side_start_error_message(&err), - "Failed to start side conversation: transport disconnected" - ); -} - #[tokio::test] async fn side_thread_snapshot_hides_forked_parent_transcript() { let parent_thread_id = ThreadId::new(); @@ -3991,61 +3749,6 @@ fn token_usage_notification( }) } -fn hook_started_notification(thread_id: ThreadId, turn_id: &str) -> ServerNotification { - ServerNotification::HookStarted(HookStartedNotification { - thread_id: thread_id.to_string(), - turn_id: Some(turn_id.to_string()), - run: AppServerHookRunSummary { - id: "user-prompt-submit:0:/tmp/hooks.json".to_string(), - event_name: AppServerHookEventName::UserPromptSubmit, - handler_type: AppServerHookHandlerType::Command, - execution_mode: AppServerHookExecutionMode::Sync, - scope: AppServerHookScope::Turn, - source_path: test_path_buf("/tmp/hooks.json").abs(), - source: codex_app_server_protocol::HookSource::User, - display_order: 0, - status: AppServerHookRunStatus::Running, - status_message: Some("checking go-workflow input policy".to_string()), - started_at: 1, - completed_at: None, - duration_ms: None, - entries: Vec::new(), - }, - }) -} - -fn hook_completed_notification(thread_id: ThreadId, turn_id: &str) -> ServerNotification { - ServerNotification::HookCompleted(HookCompletedNotification { - thread_id: thread_id.to_string(), - turn_id: Some(turn_id.to_string()), - run: AppServerHookRunSummary { - id: "user-prompt-submit:0:/tmp/hooks.json".to_string(), - event_name: AppServerHookEventName::UserPromptSubmit, - handler_type: AppServerHookHandlerType::Command, - execution_mode: AppServerHookExecutionMode::Sync, - scope: AppServerHookScope::Turn, - source_path: test_path_buf("/tmp/hooks.json").abs(), - source: codex_app_server_protocol::HookSource::User, - display_order: 0, - status: AppServerHookRunStatus::Stopped, - status_message: Some("checking go-workflow input policy".to_string()), - started_at: 1, - completed_at: Some(11), - duration_ms: Some(10), - entries: vec![ - AppServerHookOutputEntry { - kind: AppServerHookOutputEntryKind::Warning, - text: "go-workflow must start from PlanMode".to_string(), - }, - AppServerHookOutputEntry { - kind: AppServerHookOutputEntryKind::Stop, - text: "prompt blocked".to_string(), - }, - ], - }, - }) -} - fn agent_message_delta_notification( thread_id: ThreadId, turn_id: &str, @@ -4098,162 +3801,6 @@ fn request_user_input_request(thread_id: ThreadId, turn_id: &str, item_id: &str) } } -#[test] -fn thread_event_store_tracks_active_turn_lifecycle() { - let mut store = ThreadEventStore::new(/*capacity*/ 8); - assert_eq!(store.active_turn_id(), None); - - let thread_id = ThreadId::new(); - store.push_notification(turn_started_notification(thread_id, "turn-1")); - assert_eq!(store.active_turn_id(), Some("turn-1")); - - store.push_notification(turn_completed_notification( - thread_id, - "turn-2", - TurnStatus::Completed, - )); - assert_eq!(store.active_turn_id(), Some("turn-1")); - - store.push_notification(turn_completed_notification( - thread_id, - "turn-1", - TurnStatus::Interrupted, - )); - assert_eq!(store.active_turn_id(), None); -} - -#[test] -fn thread_event_store_restores_active_turn_from_snapshot_turns() { - let thread_id = ThreadId::new(); - let session = test_thread_session(thread_id, test_path_buf("/tmp/project")); - let turns = vec![ - test_turn("turn-1", TurnStatus::Completed, Vec::new()), - test_turn("turn-2", TurnStatus::InProgress, Vec::new()), - ]; - - let store = - ThreadEventStore::new_with_session(/*capacity*/ 8, session.clone(), turns.clone()); - assert_eq!(store.active_turn_id(), Some("turn-2")); - - let mut refreshed_store = ThreadEventStore::new(/*capacity*/ 8); - refreshed_store.set_session(session, turns); - assert_eq!(refreshed_store.active_turn_id(), Some("turn-2")); -} - -#[test] -fn thread_event_store_clear_active_turn_id_resets_cached_turn() { - let mut store = ThreadEventStore::new(/*capacity*/ 8); - let thread_id = ThreadId::new(); - store.push_notification(turn_started_notification(thread_id, "turn-1")); - - store.clear_active_turn_id(); - - assert_eq!(store.active_turn_id(), None); -} - -#[test] -fn thread_event_store_rebase_preserves_resolved_request_state() { - let thread_id = ThreadId::new(); - let mut store = ThreadEventStore::new(/*capacity*/ 8); - store.push_request(exec_approval_request( - thread_id, - "turn-approval", - "call-approval", - /*approval_id*/ None, - )); - store.push_notification(ServerNotification::ServerRequestResolved( - codex_app_server_protocol::ServerRequestResolvedNotification { - request_id: AppServerRequestId::Integer(1), - thread_id: thread_id.to_string(), - }, - )); - - store.rebase_buffer_after_session_refresh(); - - let snapshot = store.snapshot(); - assert!(snapshot.events.is_empty()); - assert_eq!(store.has_pending_thread_approvals(), false); -} - -#[test] -fn thread_event_store_rebase_preserves_hook_notifications() { - let thread_id = ThreadId::new(); - let mut store = ThreadEventStore::new(/*capacity*/ 8); - store.push_notification(hook_started_notification(thread_id, "turn-hook")); - store.push_notification(hook_completed_notification(thread_id, "turn-hook")); - - store.rebase_buffer_after_session_refresh(); - - let snapshot = store.snapshot(); - let hook_notifications = snapshot - .events - .into_iter() - .map(|event| match event { - ThreadBufferedEvent::Notification(notification) => { - serde_json::to_value(notification).expect("hook notification should serialize") - } - other => panic!("expected buffered hook notification, saw: {other:?}"), - }) - .collect::>(); - assert_eq!( - hook_notifications, - vec![ - serde_json::to_value(hook_started_notification(thread_id, "turn-hook")) - .expect("hook notification should serialize"), - serde_json::to_value(hook_completed_notification(thread_id, "turn-hook")) - .expect("hook notification should serialize"), - ] - ); -} - -#[test] -fn build_feedback_upload_params_includes_thread_id_and_rollout_path() { - let thread_id = ThreadId::new(); - let rollout_path = PathBuf::from("/tmp/rollout.jsonl"); - - let params = build_feedback_upload_params( - Some(thread_id), - Some(rollout_path.clone()), - FeedbackCategory::SafetyCheck, - Some("needs follow-up".to_string()), - Some("turn-123".to_string()), - /*include_logs*/ true, - ); - - assert_eq!(params.classification, "safety_check"); - assert_eq!(params.reason, Some("needs follow-up".to_string())); - assert_eq!(params.thread_id, Some(thread_id.to_string())); - assert_eq!( - params - .tags - .as_ref() - .and_then(|tags| tags.get("turn_id")) - .map(String::as_str), - Some("turn-123") - ); - assert_eq!(params.include_logs, true); - assert_eq!(params.extra_log_files, Some(vec![rollout_path])); -} - -#[test] -fn build_feedback_upload_params_omits_rollout_path_without_logs() { - let params = build_feedback_upload_params( - /*origin_thread_id*/ None, - Some(PathBuf::from("/tmp/rollout.jsonl")), - FeedbackCategory::GoodResult, - /*reason*/ None, - /*turn_id*/ None, - /*include_logs*/ false, - ); - - assert_eq!(params.classification, "good_result"); - assert_eq!(params.reason, None); - assert_eq!(params.thread_id, None); - assert_eq!(params.tags, None); - assert_eq!(params.include_logs, false); - assert_eq!(params.extra_log_files, None); -} - #[tokio::test] async fn feedback_submission_without_thread_emits_error_history_cell() { let (mut app, mut app_event_rx, _op_rx) = make_test_app_with_channels().await; @@ -4383,19 +3930,6 @@ fn test_session_telemetry(config: &Config, model: &str) -> SessionTelemetry { ) } -fn app_enabled_in_effective_config(config: &Config, app_id: &str) -> Option { - config - .config_layer_stack - .effective_config() - .as_table() - .and_then(|table| table.get("apps")) - .and_then(TomlValue::as_table) - .and_then(|apps| apps.get(app_id)) - .and_then(TomlValue::as_table) - .and_then(|app| app.get("enabled")) - .and_then(TomlValue::as_bool) -} - #[test] fn active_turn_not_steerable_turn_error_extracts_structured_server_error() { let turn_error = AppServerTurnError { @@ -4457,166 +3991,6 @@ fn active_turn_steer_race_extracts_actual_turn_id_from_mismatch() { ); } -#[tokio::test] -async fn update_reasoning_effort_updates_collaboration_mode() { - let mut app = make_test_app().await; - app.chat_widget - .set_reasoning_effort(Some(ReasoningEffortConfig::Medium)); - - app.on_update_reasoning_effort(Some(ReasoningEffortConfig::High)); - - assert_eq!( - app.chat_widget.current_reasoning_effort(), - Some(ReasoningEffortConfig::High) - ); - assert_eq!( - app.config.model_reasoning_effort, - Some(ReasoningEffortConfig::High) - ); -} - -#[tokio::test] -async fn refresh_in_memory_config_from_disk_loads_latest_apps_state() -> Result<()> { - let mut app = make_test_app().await; - let codex_home = tempdir()?; - app.config.codex_home = codex_home.path().to_path_buf().abs(); - let app_id = "unit_test_refresh_in_memory_config_connector".to_string(); - - assert_eq!(app_enabled_in_effective_config(&app.config, &app_id), None); - - ConfigEditsBuilder::new(&app.config.codex_home) - .with_edits([ - ConfigEdit::SetPath { - segments: vec!["apps".to_string(), app_id.clone(), "enabled".to_string()], - value: false.into(), - }, - ConfigEdit::SetPath { - segments: vec![ - "apps".to_string(), - app_id.clone(), - "disabled_reason".to_string(), - ], - value: "user".into(), - }, - ]) - .apply() - .await - .expect("persist app toggle"); - - assert_eq!(app_enabled_in_effective_config(&app.config, &app_id), None); - - app.refresh_in_memory_config_from_disk().await?; - - assert_eq!( - app_enabled_in_effective_config(&app.config, &app_id), - Some(false) - ); - Ok(()) -} - -#[tokio::test] -async fn refresh_in_memory_config_from_disk_best_effort_keeps_current_config_on_error() -> Result<()> -{ - let mut app = make_test_app().await; - let codex_home = tempdir()?; - app.config.codex_home = codex_home.path().to_path_buf().abs(); - std::fs::write(codex_home.path().join("config.toml"), "[broken")?; - let original_config = app.config.clone(); - - app.refresh_in_memory_config_from_disk_best_effort("starting a new thread") - .await; - - assert_eq!(app.config, original_config); - Ok(()) -} - -#[tokio::test] -async fn refresh_in_memory_config_from_disk_uses_active_chat_widget_cwd() -> Result<()> { - let mut app = make_test_app().await; - let original_cwd = app.config.cwd.clone(); - let next_cwd_tmp = tempdir()?; - let next_cwd = next_cwd_tmp.path().to_path_buf(); - - app.chat_widget.handle_codex_event(Event { - id: String::new(), - msg: EventMsg::SessionConfigured(SessionConfiguredEvent { - session_id: ThreadId::new(), - forked_from_id: None, - thread_name: None, - model: "gpt-test".to_string(), - model_provider_id: "test-provider".to_string(), - service_tier: None, - approval_policy: AskForApproval::Never, - approvals_reviewer: ApprovalsReviewer::User, - sandbox_policy: SandboxPolicy::new_read_only_policy(), - cwd: next_cwd.clone().abs(), - reasoning_effort: None, - history_log_id: 0, - history_entry_count: 0, - initial_messages: None, - network_proxy: None, - rollout_path: Some(PathBuf::new()), - }), - }); - - assert_eq!(app.chat_widget.config_ref().cwd.to_path_buf(), next_cwd); - assert_eq!(app.config.cwd, original_cwd); - - app.refresh_in_memory_config_from_disk().await?; - - assert_eq!(app.config.cwd, app.chat_widget.config_ref().cwd); - Ok(()) -} - -#[tokio::test] -async fn rebuild_config_for_resume_or_fallback_uses_current_config_on_same_cwd_error() -> Result<()> -{ - let mut app = make_test_app().await; - let codex_home = tempdir()?; - app.config.codex_home = codex_home.path().to_path_buf().abs(); - std::fs::write(codex_home.path().join("config.toml"), "[broken")?; - let current_config = app.config.clone(); - let current_cwd = current_config.cwd.clone(); - - let resume_config = app - .rebuild_config_for_resume_or_fallback(¤t_cwd, current_cwd.to_path_buf()) - .await?; - - assert_eq!(resume_config, current_config); - Ok(()) -} - -#[tokio::test] -async fn rebuild_config_for_resume_or_fallback_errors_when_cwd_changes() -> Result<()> { - let mut app = make_test_app().await; - let codex_home = tempdir()?; - app.config.codex_home = codex_home.path().to_path_buf().abs(); - std::fs::write(codex_home.path().join("config.toml"), "[broken")?; - let current_cwd = app.config.cwd.clone(); - let next_cwd_tmp = tempdir()?; - let next_cwd = next_cwd_tmp.path().to_path_buf(); - - let result = app - .rebuild_config_for_resume_or_fallback(¤t_cwd, next_cwd) - .await; - - assert!(result.is_err()); - Ok(()) -} - -#[tokio::test] -async fn sync_tui_theme_selection_updates_chat_widget_config_copy() { - let mut app = make_test_app().await; - - app.sync_tui_theme_selection("dracula".to_string()); - - assert_eq!(app.config.tui_theme.as_deref(), Some("dracula")); - assert_eq!( - app.chat_widget.config_ref().tui_theme.as_deref(), - Some("dracula") - ); -} - #[tokio::test] async fn fresh_session_config_uses_current_service_tier() { let mut app = make_test_app().await; diff --git a/codex-rs/tui/src/app/thread_events.rs b/codex-rs/tui/src/app/thread_events.rs index 6bbeca04e4..89cef2332c 100644 --- a/codex-rs/tui/src/app/thread_events.rs +++ b/codex-rs/tui/src/app/thread_events.rs @@ -264,3 +264,277 @@ impl ThreadEventChannel { } } } + +#[cfg(test)] +mod tests { + use super::*; + use crate::test_support::PathBufExt; + use crate::test_support::test_path_buf; + use codex_app_server_protocol::CommandExecutionRequestApprovalParams; + use codex_app_server_protocol::HookCompletedNotification; + use codex_app_server_protocol::HookEventName as AppServerHookEventName; + use codex_app_server_protocol::HookExecutionMode as AppServerHookExecutionMode; + use codex_app_server_protocol::HookHandlerType as AppServerHookHandlerType; + use codex_app_server_protocol::HookOutputEntry as AppServerHookOutputEntry; + use codex_app_server_protocol::HookOutputEntryKind as AppServerHookOutputEntryKind; + use codex_app_server_protocol::HookRunStatus as AppServerHookRunStatus; + use codex_app_server_protocol::HookRunSummary as AppServerHookRunSummary; + use codex_app_server_protocol::HookScope as AppServerHookScope; + use codex_app_server_protocol::HookStartedNotification; + use codex_app_server_protocol::RequestId as AppServerRequestId; + use codex_app_server_protocol::TurnCompletedNotification; + use codex_app_server_protocol::TurnStartedNotification; + use codex_config::types::ApprovalsReviewer; + use codex_protocol::protocol::AskForApproval; + use codex_protocol::protocol::SandboxPolicy; + use pretty_assertions::assert_eq; + use std::path::PathBuf; + + fn test_thread_session(thread_id: ThreadId, cwd: PathBuf) -> ThreadSessionState { + ThreadSessionState { + thread_id, + forked_from_id: None, + fork_parent_title: None, + thread_name: None, + model: "gpt-test".to_string(), + model_provider_id: "test-provider".to_string(), + service_tier: None, + approval_policy: AskForApproval::Never, + approvals_reviewer: ApprovalsReviewer::User, + sandbox_policy: SandboxPolicy::new_read_only_policy(), + cwd: cwd.abs(), + instruction_source_paths: Vec::new(), + reasoning_effort: None, + history_log_id: 0, + history_entry_count: 0, + network_proxy: None, + rollout_path: Some(PathBuf::new()), + } + } + + fn test_turn(turn_id: &str, status: TurnStatus, items: Vec) -> Turn { + Turn { + id: turn_id.to_string(), + items, + status, + error: None, + started_at: None, + completed_at: None, + duration_ms: None, + } + } + + fn turn_started_notification(thread_id: ThreadId, turn_id: &str) -> ServerNotification { + ServerNotification::TurnStarted(TurnStartedNotification { + thread_id: thread_id.to_string(), + turn: Turn { + started_at: Some(0), + ..test_turn(turn_id, TurnStatus::InProgress, Vec::new()) + }, + }) + } + + fn turn_completed_notification( + thread_id: ThreadId, + turn_id: &str, + status: TurnStatus, + ) -> ServerNotification { + ServerNotification::TurnCompleted(TurnCompletedNotification { + thread_id: thread_id.to_string(), + turn: Turn { + completed_at: Some(0), + duration_ms: Some(1), + ..test_turn(turn_id, status, Vec::new()) + }, + }) + } + + fn hook_started_notification(thread_id: ThreadId, turn_id: &str) -> ServerNotification { + ServerNotification::HookStarted(HookStartedNotification { + thread_id: thread_id.to_string(), + turn_id: Some(turn_id.to_string()), + run: AppServerHookRunSummary { + id: "user-prompt-submit:0:/tmp/hooks.json".to_string(), + event_name: AppServerHookEventName::UserPromptSubmit, + handler_type: AppServerHookHandlerType::Command, + execution_mode: AppServerHookExecutionMode::Sync, + scope: AppServerHookScope::Turn, + source_path: test_path_buf("/tmp/hooks.json").abs(), + source: codex_app_server_protocol::HookSource::User, + display_order: 0, + status: AppServerHookRunStatus::Running, + status_message: Some("checking go-workflow input policy".to_string()), + started_at: 1, + completed_at: None, + duration_ms: None, + entries: Vec::new(), + }, + }) + } + + fn hook_completed_notification(thread_id: ThreadId, turn_id: &str) -> ServerNotification { + ServerNotification::HookCompleted(HookCompletedNotification { + thread_id: thread_id.to_string(), + turn_id: Some(turn_id.to_string()), + run: AppServerHookRunSummary { + id: "user-prompt-submit:0:/tmp/hooks.json".to_string(), + event_name: AppServerHookEventName::UserPromptSubmit, + handler_type: AppServerHookHandlerType::Command, + execution_mode: AppServerHookExecutionMode::Sync, + scope: AppServerHookScope::Turn, + source_path: test_path_buf("/tmp/hooks.json").abs(), + source: codex_app_server_protocol::HookSource::User, + display_order: 0, + status: AppServerHookRunStatus::Stopped, + status_message: Some("checking go-workflow input policy".to_string()), + started_at: 1, + completed_at: Some(11), + duration_ms: Some(10), + entries: vec![ + AppServerHookOutputEntry { + kind: AppServerHookOutputEntryKind::Warning, + text: "go-workflow must start from PlanMode".to_string(), + }, + AppServerHookOutputEntry { + kind: AppServerHookOutputEntryKind::Stop, + text: "prompt blocked".to_string(), + }, + ], + }, + }) + } + + fn exec_approval_request( + thread_id: ThreadId, + turn_id: &str, + item_id: &str, + approval_id: Option<&str>, + ) -> ServerRequest { + ServerRequest::CommandExecutionRequestApproval { + request_id: AppServerRequestId::Integer(1), + params: CommandExecutionRequestApprovalParams { + thread_id: thread_id.to_string(), + turn_id: turn_id.to_string(), + item_id: item_id.to_string(), + approval_id: approval_id.map(str::to_string), + reason: Some("needs approval".to_string()), + network_approval_context: None, + command: Some("echo hello".to_string()), + cwd: Some(test_path_buf("/tmp/project").abs()), + command_actions: None, + additional_permissions: None, + proposed_execpolicy_amendment: None, + proposed_network_policy_amendments: None, + available_decisions: None, + }, + } + } + + #[test] + fn thread_event_store_tracks_active_turn_lifecycle() { + let mut store = ThreadEventStore::new(/*capacity*/ 8); + assert_eq!(store.active_turn_id(), None); + + let thread_id = ThreadId::new(); + store.push_notification(turn_started_notification(thread_id, "turn-1")); + assert_eq!(store.active_turn_id(), Some("turn-1")); + + store.push_notification(turn_completed_notification( + thread_id, + "turn-2", + TurnStatus::Completed, + )); + assert_eq!(store.active_turn_id(), Some("turn-1")); + + store.push_notification(turn_completed_notification( + thread_id, + "turn-1", + TurnStatus::Interrupted, + )); + assert_eq!(store.active_turn_id(), None); + } + + #[test] + fn thread_event_store_restores_active_turn_from_snapshot_turns() { + let thread_id = ThreadId::new(); + let session = test_thread_session(thread_id, test_path_buf("/tmp/project")); + let turns = vec![ + test_turn("turn-1", TurnStatus::Completed, Vec::new()), + test_turn("turn-2", TurnStatus::InProgress, Vec::new()), + ]; + + let store = + ThreadEventStore::new_with_session(/*capacity*/ 8, session.clone(), turns.clone()); + assert_eq!(store.active_turn_id(), Some("turn-2")); + + let mut refreshed_store = ThreadEventStore::new(/*capacity*/ 8); + refreshed_store.set_session(session, turns); + assert_eq!(refreshed_store.active_turn_id(), Some("turn-2")); + } + + #[test] + fn thread_event_store_clear_active_turn_id_resets_cached_turn() { + let mut store = ThreadEventStore::new(/*capacity*/ 8); + let thread_id = ThreadId::new(); + store.push_notification(turn_started_notification(thread_id, "turn-1")); + + store.clear_active_turn_id(); + + assert_eq!(store.active_turn_id(), None); + } + + #[test] + fn thread_event_store_rebase_preserves_resolved_request_state() { + let thread_id = ThreadId::new(); + let mut store = ThreadEventStore::new(/*capacity*/ 8); + store.push_request(exec_approval_request( + thread_id, + "turn-approval", + "call-approval", + /*approval_id*/ None, + )); + store.push_notification(ServerNotification::ServerRequestResolved( + codex_app_server_protocol::ServerRequestResolvedNotification { + request_id: AppServerRequestId::Integer(1), + thread_id: thread_id.to_string(), + }, + )); + + store.rebase_buffer_after_session_refresh(); + + let snapshot = store.snapshot(); + assert!(snapshot.events.is_empty()); + assert_eq!(store.has_pending_thread_approvals(), false); + } + + #[test] + fn thread_event_store_rebase_preserves_hook_notifications() { + let thread_id = ThreadId::new(); + let mut store = ThreadEventStore::new(/*capacity*/ 8); + store.push_notification(hook_started_notification(thread_id, "turn-hook")); + store.push_notification(hook_completed_notification(thread_id, "turn-hook")); + + store.rebase_buffer_after_session_refresh(); + + let snapshot = store.snapshot(); + let hook_notifications = snapshot + .events + .into_iter() + .map(|event| match event { + ThreadBufferedEvent::Notification(notification) => { + serde_json::to_value(notification).expect("hook notification should serialize") + } + other => panic!("expected buffered hook notification, saw: {other:?}"), + }) + .collect::>(); + assert_eq!( + hook_notifications, + vec![ + serde_json::to_value(hook_started_notification(thread_id, "turn-hook")) + .expect("hook notification should serialize"), + serde_json::to_value(hook_completed_notification(thread_id, "turn-hook")) + .expect("hook notification should serialize"), + ] + ); + } +} From 5fe767e8e1398213f77e45e7507be244c5035e6d Mon Sep 17 00:00:00 2001 From: pakrym-oai Date: Tue, 21 Apr 2026 10:22:26 -0700 Subject: [PATCH 08/22] Refactor app-server config loading into ConfigManager (#18442) Localize app-server configuration loading in one place. --- codex-rs/Cargo.lock | 2 + codex-rs/app-server/Cargo.toml | 2 + .../app-server/src/codex_message_processor.rs | 397 ++++-------------- codex-rs/app-server/src/config_api.rs | 173 ++------ codex-rs/app-server/src/config_manager.rs | 326 ++++++++++++++ .../src/config_manager_service.rs} | 225 +++------- .../src/config_manager_service_tests.rs} | 37 +- codex-rs/app-server/src/lib.rs | 2 + codex-rs/app-server/src/message_processor.rs | 21 +- .../suite/v2/experimental_feature_list.rs | 4 + codex-rs/core/src/config/mod.rs | 15 +- codex-rs/core/src/plugins/manager.rs | 28 +- 12 files changed, 585 insertions(+), 647 deletions(-) create mode 100644 codex-rs/app-server/src/config_manager.rs rename codex-rs/{core/src/config/service.rs => app-server/src/config_manager_service.rs} (74%) rename codex-rs/{core/src/config/service_tests.rs => app-server/src/config_manager_service_tests.rs} (95%) diff --git a/codex-rs/Cargo.lock b/codex-rs/Cargo.lock index 18c26e6a14..de1db07093 100644 --- a/codex-rs/Cargo.lock +++ b/codex-rs/Cargo.lock @@ -1508,11 +1508,13 @@ dependencies = [ "sha2", "shlex", "tempfile", + "thiserror 2.0.18", "time", "tokio", "tokio-tungstenite", "tokio-util", "toml 0.9.11+spec-1.1.0", + "toml_edit 0.24.0+spec-1.1.0", "tracing", "tracing-opentelemetry", "tracing-subscriber", diff --git a/codex-rs/app-server/Cargo.toml b/codex-rs/app-server/Cargo.toml index f354539640..ea49058e6c 100644 --- a/codex-rs/app-server/Cargo.toml +++ b/codex-rs/app-server/Cargo.toml @@ -72,8 +72,10 @@ serde = { workspace = true, features = ["derive"] } serde_json = { workspace = true } sha2 = { workspace = true } tempfile = { workspace = true } +thiserror = { workspace = true } time = { workspace = true } toml = { workspace = true } +toml_edit = { workspace = true } tokio = { workspace = true, features = [ "io-std", "macros", diff --git a/codex-rs/app-server/src/codex_message_processor.rs b/codex-rs/app-server/src/codex_message_processor.rs index a60a079898..3b92d4199f 100644 --- a/codex-rs/app-server/src/codex_message_processor.rs +++ b/codex-rs/app-server/src/codex_message_processor.rs @@ -2,7 +2,7 @@ use crate::bespoke_event_handling::apply_bespoke_event_handling; use crate::bespoke_event_handling::maybe_emit_hook_prompt_item_completed; use crate::command_exec::CommandExecManager; use crate::command_exec::StartCommandExecParams; -use crate::config_api::apply_runtime_feature_enablement; +use crate::config_manager::ConfigManager; use crate::error_code::INPUT_TOO_LARGE_ERROR_CODE; use crate::error_code::INTERNAL_ERROR_CODE; use crate::error_code::INVALID_PARAMS_ERROR_CODE; @@ -212,8 +212,6 @@ use codex_arg0::Arg0DispatchPaths; use codex_backend_client::AddCreditsNudgeCreditType as BackendAddCreditsNudgeCreditType; use codex_backend_client::Client as BackendClient; use codex_chatgpt::connectors; -use codex_cloud_requirements::cloud_requirements_loader; -use codex_config::ThreadConfigLoader; use codex_config::types::McpServerTransportConfig; use codex_core::CodexThread; use codex_core::ForkSnapshot; @@ -231,9 +229,6 @@ use codex_core::config::edit::ConfigEdit; use codex_core::config::edit::ConfigEditsBuilder; use codex_core::config_loader::CloudRequirementsLoadError; use codex_core::config_loader::CloudRequirementsLoadErrorCode; -use codex_core::config_loader::CloudRequirementsLoader; -use codex_core::config_loader::LoaderOverrides; -use codex_core::config_loader::load_config_layers_state; use codex_core::config_loader::project_trust_key; use codex_core::exec::ExecCapturePolicy; use codex_core::exec::ExecExpiration; @@ -280,7 +275,6 @@ use codex_login::ServerOptions as LoginServerOptions; use codex_login::ShutdownHandle; use codex_login::auth::login_with_chatgpt_auth_tokens; use codex_login::complete_device_code_login; -use codex_login::default_client::set_default_client_residency_requirement; use codex_login::login_with_api_key; use codex_login::request_device_code; use codex_login::run_login_server; @@ -348,16 +342,13 @@ use codex_thread_store::ThreadStore; use codex_thread_store::ThreadStoreError; use codex_thread_store::UpdateThreadMetadataParams as StoreUpdateThreadMetadataParams; use codex_utils_absolute_path::AbsolutePathBuf; -use codex_utils_json_to_toml::json_to_toml; use codex_utils_pty::DEFAULT_OUTPUT_BYTES_CAP; -use std::collections::BTreeMap; use std::collections::HashMap; use std::collections::HashSet; use std::io::Error as IoError; use std::path::Path; use std::path::PathBuf; use std::sync::Arc; -use std::sync::RwLock; use std::sync::atomic::AtomicBool; use std::sync::atomic::Ordering; use std::time::Duration; @@ -478,10 +469,7 @@ pub(crate) struct CodexMessageProcessor { arg0_paths: Arg0DispatchPaths, config: Arc, thread_store: Arc, - cli_overrides: Arc>>, - runtime_feature_enablement: Arc>>, - cloud_requirements: Arc>, - thread_config_loader: Arc, + config_manager: ConfigManager, active_login: Arc>>, pending_thread_unloads: Arc>>, thread_state_manager: ThreadStateManager, @@ -638,11 +626,10 @@ pub(crate) struct CodexMessageProcessorArgs { pub(crate) outgoing: Arc, pub(crate) analytics_events_client: AnalyticsEventsClient, pub(crate) arg0_paths: Arg0DispatchPaths, + /// Startup config used as the process baseline. Fresh effective config loads + /// go through `config_manager`. pub(crate) config: Arc, - pub(crate) cli_overrides: Arc>>, - pub(crate) runtime_feature_enablement: Arc>>, - pub(crate) cloud_requirements: Arc>, - pub(crate) thread_config_loader: Arc, + pub(crate) config_manager: ConfigManager, pub(crate) feedback: CodexFeedback, pub(crate) log_db: Option, } @@ -727,10 +714,7 @@ impl CodexMessageProcessor { analytics_events_client, arg0_paths, config, - cli_overrides, - runtime_feature_enablement, - cloud_requirements, - thread_config_loader, + config_manager, feedback, log_db, } = args; @@ -742,10 +726,7 @@ impl CodexMessageProcessor { arg0_paths, thread_store: configured_thread_store(&config), config, - cli_overrides, - runtime_feature_enablement, - cloud_requirements, - thread_config_loader, + config_manager, active_login: Arc::new(Mutex::new(None)), pending_thread_unloads: Arc::new(Mutex::new(HashSet::new())), thread_state_manager: ThreadStateManager::new(), @@ -763,44 +744,14 @@ impl CodexMessageProcessor { &self, fallback_cwd: Option, ) -> Result { - let cloud_requirements = self.current_cloud_requirements(); - let mut config = codex_core::config::ConfigBuilder::default() - .cli_overrides(self.current_cli_overrides()) - .fallback_cwd(fallback_cwd) - .cloud_requirements(cloud_requirements) - .build() + self.config_manager + .load_latest_config(fallback_cwd) .await .map_err(|err| JSONRPCErrorError { code: INTERNAL_ERROR_CODE, message: format!("failed to reload config: {err}"), data: None, - })?; - apply_runtime_feature_enablement(&mut config, &self.current_runtime_feature_enablement()); - config.codex_self_exe = self.arg0_paths.codex_self_exe.clone(); - config.codex_linux_sandbox_exe = self.arg0_paths.codex_linux_sandbox_exe.clone(); - config.main_execve_wrapper_exe = self.arg0_paths.main_execve_wrapper_exe.clone(); - Ok(config) - } - - fn current_cloud_requirements(&self) -> CloudRequirementsLoader { - self.cloud_requirements - .read() - .map(|guard| guard.clone()) - .unwrap_or_default() - } - - fn current_cli_overrides(&self) -> Vec<(String, TomlValue)> { - self.cli_overrides - .read() - .map(|guard| guard.clone()) - .unwrap_or_default() - } - - fn current_runtime_feature_enablement(&self) -> BTreeMap { - self.runtime_feature_enablement - .read() - .map(|guard| guard.clone()) - .unwrap_or_default() + }) } /// If a client sends `developer_instructions: null` during a mode switch, @@ -1424,10 +1375,8 @@ impl CodexMessageProcessor { let outgoing_clone = self.outgoing.clone(); let active_login = self.active_login.clone(); let auth_manager = self.auth_manager.clone(); - let cloud_requirements = self.cloud_requirements.clone(); + let config_manager = self.config_manager.clone(); let chatgpt_base_url = self.config.chatgpt_base_url.clone(); - let codex_home = self.config.codex_home.to_path_buf(); - let cli_overrides = self.current_cli_overrides(); let auth_url = server.auth_url.clone(); tokio::spawn(async move { let (success, error_msg) = match tokio::time::timeout( @@ -1457,17 +1406,13 @@ impl CodexMessageProcessor { if success { auth_manager.reload(); - replace_cloud_requirements_loader( - cloud_requirements.as_ref(), + config_manager.replace_cloud_requirements_loader( auth_manager.clone(), chatgpt_base_url, - codex_home, ); - sync_default_client_residency_requirement( - &cli_overrides, - cloud_requirements.as_ref(), - ) - .await; + config_manager + .sync_default_client_residency_requirement() + .await; // Notify clients with the actual current auth mode. let auth = auth_manager.auth_cached(); @@ -1541,10 +1486,8 @@ impl CodexMessageProcessor { let outgoing_clone = self.outgoing.clone(); let active_login = self.active_login.clone(); let auth_manager = self.auth_manager.clone(); - let cloud_requirements = self.cloud_requirements.clone(); + let config_manager = self.config_manager.clone(); let chatgpt_base_url = self.config.chatgpt_base_url.clone(); - let codex_home = self.config.codex_home.to_path_buf(); - let cli_overrides = self.current_cli_overrides(); tokio::spawn(async move { let (success, error_msg) = tokio::select! { _ = cancel.cancelled() => { @@ -1571,17 +1514,13 @@ impl CodexMessageProcessor { if success { auth_manager.reload(); - replace_cloud_requirements_loader( - cloud_requirements.as_ref(), + config_manager.replace_cloud_requirements_loader( auth_manager.clone(), chatgpt_base_url, - codex_home, ); - sync_default_client_residency_requirement( - &cli_overrides, - cloud_requirements.as_ref(), - ) - .await; + config_manager + .sync_default_client_residency_requirement() + .await; let auth = auth_manager.auth_cached(); let payload_v2 = AccountUpdatedNotification { @@ -1711,14 +1650,12 @@ impl CodexMessageProcessor { return; } self.auth_manager.reload(); - replace_cloud_requirements_loader( - self.cloud_requirements.as_ref(), + self.config_manager.replace_cloud_requirements_loader( self.auth_manager.clone(), self.config.chatgpt_base_url.clone(), - self.config.codex_home.to_path_buf(), ); - let cli_overrides = self.current_cli_overrides(); - sync_default_client_residency_requirement(&cli_overrides, self.cloud_requirements.as_ref()) + self.config_manager + .sync_default_client_residency_requirement() .await; self.outgoing @@ -2429,8 +2366,6 @@ impl CodexMessageProcessor { personality, ); typesafe_overrides.ephemeral = ephemeral; - let cloud_requirements = self.current_cloud_requirements(); - let cli_overrides = self.current_cli_overrides(); let listener_task_context = ListenerTaskContext { thread_manager: Arc::clone(&self.thread_manager), thread_state_manager: self.thread_state_manager.clone(), @@ -2443,15 +2378,11 @@ impl CodexMessageProcessor { codex_home: self.config.codex_home.to_path_buf(), }; let request_trace = request_context.request_trace(); - let runtime_feature_enablement = self.current_runtime_feature_enablement(); - let thread_config_loader = Arc::clone(&self.thread_config_loader); + let config_manager = self.config_manager.clone(); let thread_start_task = async move { Self::thread_start_task( listener_task_context, - cli_overrides, - runtime_feature_enablement, - cloud_requirements, - thread_config_loader, + config_manager, request_id, app_server_client_name, app_server_client_version, @@ -2525,10 +2456,7 @@ impl CodexMessageProcessor { #[allow(clippy::too_many_arguments)] async fn thread_start_task( listener_task_context: ListenerTaskContext, - cli_overrides: Vec<(String, TomlValue)>, - runtime_feature_enablement: BTreeMap, - cloud_requirements: CloudRequirementsLoader, - thread_config_loader: Arc, + config_manager: ConfigManager, request_id: ConnectionRequestId, app_server_client_name: Option, app_server_client_version: Option, @@ -2542,16 +2470,9 @@ impl CodexMessageProcessor { request_trace: Option, ) { let requested_cwd = typesafe_overrides.cwd.clone(); - let mut config = match derive_config_from_params( - &cli_overrides, - config_overrides.clone(), - typesafe_overrides.clone(), - Arc::clone(&thread_config_loader), - &cloud_requirements, - &listener_task_context.codex_home, - &runtime_feature_enablement, - ) - .await + let mut config = match config_manager + .load_with_overrides(config_overrides.clone(), typesafe_overrides.clone()) + .await { Ok(config) => config, Err(err) => { @@ -2590,6 +2511,7 @@ impl CodexMessageProcessor { let trust_target = resolve_root_git_project_for_trust(LOCAL_FS.as_ref(), &config.cwd) .await .unwrap_or_else(|| config.cwd.clone()); + let current_cli_overrides = config_manager.current_cli_overrides(); let cli_overrides_with_trust; let cli_overrides_for_reload = if let Err(err) = codex_core::config::set_project_trust_level( @@ -2611,7 +2533,7 @@ impl CodexMessageProcessor { project_trust_key(trust_target.as_path()), TomlValue::Table(project), ); - cli_overrides_with_trust = cli_overrides + cli_overrides_with_trust = current_cli_overrides .iter() .cloned() .chain(std::iter::once(( @@ -2621,19 +2543,17 @@ impl CodexMessageProcessor { .collect::>(); cli_overrides_with_trust.as_slice() } else { - &cli_overrides + current_cli_overrides.as_slice() }; - config = match derive_config_from_params( - cli_overrides_for_reload, - config_overrides, - typesafe_overrides, - thread_config_loader, - &cloud_requirements, - &listener_task_context.codex_home, - &runtime_feature_enablement, - ) - .await + config = match config_manager + .load_with_cli_overrides( + cli_overrides_for_reload, + config_overrides, + typesafe_overrides, + /*fallback_cwd*/ None, + ) + .await { Ok(config) => config, Err(err) => { @@ -4417,19 +4337,10 @@ impl CodexMessageProcessor { .await; // Derive a Config using the same logic as new conversation, honoring overrides if provided. - let cloud_requirements = self.current_cloud_requirements(); - let cli_overrides = self.current_cli_overrides(); - let runtime_feature_enablement = self.current_runtime_feature_enablement(); - let config = match derive_config_for_cwd( - &cli_overrides, - request_overrides, - typesafe_overrides, - history_cwd, - &cloud_requirements, - &self.config.codex_home, - &runtime_feature_enablement, - ) - .await + let config = match self + .config_manager + .load_for_cwd(request_overrides, typesafe_overrides, history_cwd) + .await { Ok(config) => config, Err(err) => { @@ -4997,19 +4908,10 @@ impl CodexMessageProcessor { ); typesafe_overrides.ephemeral = ephemeral.then_some(true); // Derive a Config using the same logic as new conversation, honoring overrides if provided. - let cloud_requirements = self.current_cloud_requirements(); - let cli_overrides = self.current_cli_overrides(); - let runtime_feature_enablement = self.current_runtime_feature_enablement(); - let config = match derive_config_for_cwd( - &cli_overrides, - request_overrides, - typesafe_overrides, - history_cwd, - &cloud_requirements, - &self.config.codex_home, - &runtime_feature_enablement, - ) - .await + let config = match self + .config_manager + .load_for_cwd(request_overrides, typesafe_overrides, history_cwd) + .await { Ok(config) => config, Err(err) => { @@ -6584,8 +6486,6 @@ impl CodexMessageProcessor { return; } }; - let cli_overrides = self.current_cli_overrides(); - let host_name = codex_config::host_name(); let mut data = Vec::new(); for cwd in cwds { let extra_roots = extra_roots_by_cwd @@ -6606,17 +6506,10 @@ impl CodexMessageProcessor { continue; } }; - let config_layer_stack = match load_config_layers_state( - LOCAL_FS.as_ref(), - &self.config.codex_home, - Some(cwd_abs.clone()), - &cli_overrides, - LoaderOverrides::default(), - CloudRequirementsLoader::default(), - self.thread_config_loader.as_ref(), - host_name.as_deref(), - ) - .await + let config_layer_stack = match self + .config_manager + .load_config_layers_for_cwd(cwd_abs.clone()) + .await { Ok(config_layer_stack) => config_layer_stack, Err(err) => { @@ -8772,30 +8665,25 @@ impl CodexMessageProcessor { WindowsSandboxSetupMode::Unelevated => CoreWindowsSandboxSetupMode::Unelevated, }; let config = Arc::clone(&self.config); - let cloud_requirements = self.current_cloud_requirements(); + let config_manager = self.config_manager.clone(); let command_cwd = params .cwd .map(PathBuf::from) .unwrap_or_else(|| config.cwd.to_path_buf()); - let cli_overrides = self.current_cli_overrides(); - let runtime_feature_enablement = self.current_runtime_feature_enablement(); let outgoing = Arc::clone(&self.outgoing); let connection_id = request_id.connection_id; tokio::spawn(async move { - let derived_config = derive_config_for_cwd( - &cli_overrides, - /*request_overrides*/ None, - ConfigOverrides { - cwd: Some(command_cwd.clone()), - ..Default::default() - }, - Some(command_cwd.clone()), - &cloud_requirements, - &config.codex_home, - &runtime_feature_enablement, - ) - .await; + let derived_config = config_manager + .load_for_cwd( + /*request_overrides*/ None, + ConfigOverrides { + cwd: Some(command_cwd.clone()), + ..Default::default() + }, + Some(command_cwd.clone()), + ) + .await; let setup_result = match derived_config { Ok(config) => { let setup_request = WindowsSandboxSetupRequest { @@ -9525,116 +9413,6 @@ fn validate_dynamic_tools(tools: &[ApiDynamicToolSpec]) -> Result<(), String> { Ok(()) } -fn replace_cloud_requirements_loader( - cloud_requirements: &RwLock, - auth_manager: Arc, - chatgpt_base_url: String, - codex_home: PathBuf, -) { - let loader = cloud_requirements_loader(auth_manager, chatgpt_base_url, codex_home); - if let Ok(mut guard) = cloud_requirements.write() { - *guard = loader; - } else { - warn!("failed to update cloud requirements loader"); - } -} - -async fn sync_default_client_residency_requirement( - cli_overrides: &[(String, TomlValue)], - cloud_requirements: &RwLock, -) { - let loader = cloud_requirements - .read() - .map(|guard| guard.clone()) - .unwrap_or_default(); - match codex_core::config::ConfigBuilder::default() - .cli_overrides(cli_overrides.to_vec()) - .cloud_requirements(loader) - .build() - .await - { - Ok(config) => set_default_client_residency_requirement(config.enforce_residency.value()), - Err(err) => warn!( - error = %err, - "failed to sync default client residency requirement after auth refresh" - ), - } -} - -/// Derive the effective [`Config`] by layering three override sources. -/// -/// Precedence (lowest to highest): -/// - `cli_overrides`: process-wide startup `--config` flags. -/// - `request_overrides`: per-request dotted-path overrides (`params.config`), converted JSON->TOML. -/// - `typesafe_overrides`: Request objects such as `NewThreadParams` and -/// `ThreadStartParams` support a limited set of _explicit_ config overrides, so -/// `typesafe_overrides` is a `ConfigOverrides` derived from the respective request object. -/// Because the overrides are defined explicitly in the `*Params`, this takes priority over -/// the more general "bag of config options" provided by `cli_overrides` and `request_overrides`. -async fn derive_config_from_params( - cli_overrides: &[(String, TomlValue)], - request_overrides: Option>, - typesafe_overrides: ConfigOverrides, - thread_config_loader: Arc, - cloud_requirements: &CloudRequirementsLoader, - codex_home: &Path, - runtime_feature_enablement: &BTreeMap, -) -> std::io::Result { - let merged_cli_overrides = cli_overrides - .iter() - .cloned() - .chain( - request_overrides - .unwrap_or_default() - .into_iter() - .map(|(k, v)| (k, json_to_toml(v))), - ) - .collect::>(); - - let mut config = codex_core::config::ConfigBuilder::default() - .codex_home(codex_home.to_path_buf()) - .cli_overrides(merged_cli_overrides) - .harness_overrides(typesafe_overrides) - .cloud_requirements(cloud_requirements.clone()) - .thread_config_loader(thread_config_loader) - .build() - .await?; - apply_runtime_feature_enablement(&mut config, runtime_feature_enablement); - Ok(config) -} - -async fn derive_config_for_cwd( - cli_overrides: &[(String, TomlValue)], - request_overrides: Option>, - typesafe_overrides: ConfigOverrides, - cwd: Option, - cloud_requirements: &CloudRequirementsLoader, - codex_home: &Path, - runtime_feature_enablement: &BTreeMap, -) -> std::io::Result { - let merged_cli_overrides = cli_overrides - .iter() - .cloned() - .chain( - request_overrides - .unwrap_or_default() - .into_iter() - .map(|(k, v)| (k, json_to_toml(v))), - ) - .collect::>(); - - let mut config = codex_core::config::ConfigBuilder::default() - .codex_home(codex_home.to_path_buf()) - .cli_overrides(merged_cli_overrides) - .harness_overrides(typesafe_overrides) - .fallback_cwd(cwd) - .cloud_requirements(cloud_requirements.clone()) - .build() - .await?; - apply_runtime_feature_enablement(&mut config, runtime_feature_enablement); - Ok(config) -} - async fn read_history_cwd_from_state_db( config: &Config, thread_id: Option, @@ -10556,6 +10334,8 @@ mod tests { use codex_config::SessionThreadConfig; use codex_config::StaticThreadConfigLoader; use codex_config::ThreadConfigSource; + use codex_core::config_loader::CloudRequirementsLoader; + use codex_core::config_loader::LoaderOverrides; use codex_model_provider_info::ModelProviderInfo; use codex_model_provider_info::WireApi; use codex_protocol::ThreadId; @@ -10569,8 +10349,10 @@ mod tests { use codex_utils_absolute_path::test_support::test_path_buf; use pretty_assertions::assert_eq; use serde_json::json; + use std::collections::BTreeMap; use std::path::PathBuf; use std::sync::Arc; + use std::sync::RwLock; use tempfile::TempDir; #[test] @@ -10844,21 +10626,13 @@ mod tests { requires_openai_auth: false, supports_websockets: true, }; - let config = derive_config_from_params( - &[], - Some(HashMap::from([ - ("model_provider".to_string(), json!("request")), - ("features.plugins".to_string(), json!(true)), - ( - "model_providers.session".to_string(), - json!({ - "name": "request", - "base_url": "http://127.0.0.1:9999/api/codex", - "wire_api": "responses", - }), - ), - ])), - ConfigOverrides::default(), + let config_manager = ConfigManager::new( + temp_dir.path().to_path_buf(), + Arc::new(RwLock::new(Vec::new())), + Arc::new(RwLock::new(BTreeMap::new())), + LoaderOverrides::default(), + Arc::new(RwLock::new(CloudRequirementsLoader::default())), + Arg0DispatchPaths::default(), Arc::new(StaticThreadConfigLoader::new(vec![ ThreadConfigSource::Session(SessionThreadConfig { model_provider: Some("session".to_string()), @@ -10869,11 +10643,24 @@ mod tests { features: BTreeMap::from([("plugins".to_string(), false)]), }), ])), - &CloudRequirementsLoader::default(), - temp_dir.path(), - &BTreeMap::new(), - ) - .await?; + ); + let config = config_manager + .load_with_overrides( + Some(HashMap::from([ + ("model_provider".to_string(), json!("request")), + ("features.plugins".to_string(), json!(true)), + ( + "model_providers.session".to_string(), + json!({ + "name": "request", + "base_url": "http://127.0.0.1:9999/api/codex", + "wire_api": "responses", + }), + ), + ])), + ConfigOverrides::default(), + ) + .await?; assert_eq!(config.model_provider_id, "session"); assert_eq!(config.model_provider, session_provider); diff --git a/codex-rs/app-server/src/config_api.rs b/codex-rs/app-server/src/config_api.rs index c57d3893c1..ffccd6b1b2 100644 --- a/codex-rs/app-server/src/config_api.rs +++ b/codex-rs/app-server/src/config_api.rs @@ -1,3 +1,5 @@ +use crate::config_manager::ConfigManager; +use crate::config_manager_service::ConfigManagerError; use crate::error_code::INTERNAL_ERROR_CODE; use crate::error_code::INVALID_REQUEST_ERROR_CODE; use async_trait::async_trait; @@ -19,11 +21,7 @@ use codex_app_server_protocol::NetworkUnixSocketPermission; use codex_app_server_protocol::SandboxMode; use codex_core::ThreadManager; use codex_core::config::Config; -use codex_core::config::ConfigService; -use codex_core::config::ConfigServiceError; -use codex_core::config_loader::CloudRequirementsLoader; use codex_core::config_loader::ConfigRequirementsToml; -use codex_core::config_loader::LoaderOverrides; use codex_core::config_loader::ResidencyRequirement as CoreResidencyRequirement; use codex_core::config_loader::SandboxModeRequirement as CoreSandboxModeRequirement; use codex_core::plugins::PluginId; @@ -34,12 +32,8 @@ use codex_features::feature_for_key; use codex_protocol::config_types::WebSearchMode; use codex_protocol::protocol::Op; use serde_json::json; -use std::collections::BTreeMap; -use std::collections::BTreeSet; use std::path::PathBuf; use std::sync::Arc; -use std::sync::RwLock; -use toml::Value as TomlValue; use tracing::warn; const SUPPORTED_EXPERIMENTAL_FEATURE_ENABLEMENT: &[&str] = &[ @@ -72,86 +66,36 @@ impl UserConfigReloader for ThreadManager { #[derive(Clone)] pub(crate) struct ConfigApi { - codex_home: PathBuf, - cli_overrides: Arc>>, - runtime_feature_enablement: Arc>>, - loader_overrides: LoaderOverrides, - cloud_requirements: Arc>, + config_manager: ConfigManager, user_config_reloader: Arc, analytics_events_client: AnalyticsEventsClient, } impl ConfigApi { pub(crate) fn new( - codex_home: PathBuf, - cli_overrides: Arc>>, - runtime_feature_enablement: Arc>>, - loader_overrides: LoaderOverrides, - cloud_requirements: Arc>, + config_manager: ConfigManager, user_config_reloader: Arc, analytics_events_client: AnalyticsEventsClient, ) -> Self { Self { - codex_home, - cli_overrides, - runtime_feature_enablement, - loader_overrides, - cloud_requirements, + config_manager, user_config_reloader, analytics_events_client, } } - fn config_service(&self) -> ConfigService { - ConfigService::new( - self.codex_home.clone(), - self.current_cli_overrides(), - self.loader_overrides.clone(), - self.current_cloud_requirements(), - codex_config::host_name(), - ) - } - - fn current_cli_overrides(&self) -> Vec<(String, TomlValue)> { - self.cli_overrides - .read() - .map(|guard| guard.clone()) - .unwrap_or_default() - } - - fn current_runtime_feature_enablement(&self) -> BTreeMap { - self.runtime_feature_enablement - .read() - .map(|guard| guard.clone()) - .unwrap_or_default() - } - - fn current_cloud_requirements(&self) -> CloudRequirementsLoader { - self.cloud_requirements - .read() - .map(|guard| guard.clone()) - .unwrap_or_default() - } - pub(crate) async fn load_latest_config( &self, fallback_cwd: Option, ) -> Result { - let mut config = codex_core::config::ConfigBuilder::default() - .codex_home(self.codex_home.clone()) - .cli_overrides(self.current_cli_overrides()) - .loader_overrides(self.loader_overrides.clone()) - .fallback_cwd(fallback_cwd) - .cloud_requirements(self.current_cloud_requirements()) - .build() + self.config_manager + .load_latest_config(fallback_cwd) .await .map_err(|err| JSONRPCErrorError { code: INTERNAL_ERROR_CODE, message: format!("failed to resolve feature override precedence: {err}"), data: None, - })?; - apply_runtime_feature_enablement(&mut config, &self.current_runtime_feature_enablement()); - Ok(config) + }) } pub(crate) async fn read( @@ -159,11 +103,7 @@ impl ConfigApi { params: ConfigReadParams, ) -> Result { let fallback_cwd = params.cwd.as_ref().map(PathBuf::from); - let mut response = self - .config_service() - .read(params) - .await - .map_err(map_error)?; + let mut response = self.config_manager.read(params).await.map_err(map_error)?; let config = self.load_latest_config(fallback_cwd).await?; for feature_key in SUPPORTED_EXPERIMENTAL_FEATURE_ENABLEMENT { let Some(feature) = feature_for_key(feature_key) else { @@ -191,7 +131,7 @@ impl ConfigApi { &self, ) -> Result { let requirements = self - .config_service() + .config_manager .read_requirements() .await .map_err(map_error)? @@ -207,7 +147,7 @@ impl ConfigApi { let pending_changes = collect_plugin_enabled_candidates([(¶ms.key_path, ¶ms.value)].into_iter()); let response = self - .config_service() + .config_manager .write_value(params) .await .map_err(map_error)?; @@ -227,7 +167,7 @@ impl ConfigApi { .map(|edit| (&edit.key_path, &edit.value)), ); let response = self - .config_service() + .config_manager .batch_write(params) .await .map_err(map_error)?; @@ -278,21 +218,17 @@ impl ConfigApi { return Ok(ExperimentalFeatureEnablementSetResponse { enablement }); } - { - let mut runtime_feature_enablement = - self.runtime_feature_enablement - .write() - .map_err(|_| JSONRPCErrorError { - code: INTERNAL_ERROR_CODE, - message: "failed to update feature enablement".to_string(), - data: None, - })?; - runtime_feature_enablement.extend( + self.config_manager + .extend_runtime_feature_enablement( enablement .iter() .map(|(name, enabled)| (name.clone(), *enabled)), - ); - } + ) + .map_err(|_| JSONRPCErrorError { + code: INTERNAL_ERROR_CODE, + message: "failed to update feature enablement".to_string(), + data: None, + })?; self.load_latest_config(/*fallback_cwd*/ None).await?; self.user_config_reloader.reload_user_config().await; @@ -309,7 +245,8 @@ impl ConfigApi { continue; }; let metadata = - installed_plugin_telemetry_metadata(self.codex_home.as_path(), &plugin_id).await; + installed_plugin_telemetry_metadata(self.config_manager.codex_home(), &plugin_id) + .await; if enabled { self.analytics_events_client.track_plugin_enabled(metadata); } else { @@ -319,49 +256,6 @@ impl ConfigApi { } } -pub(crate) fn protected_feature_keys( - config_layer_stack: &codex_core::config_loader::ConfigLayerStack, -) -> BTreeSet { - let mut protected_features = config_layer_stack - .effective_config() - .get("features") - .and_then(toml::Value::as_table) - .map(|features| features.keys().cloned().collect::>()) - .unwrap_or_default(); - - if let Some(feature_requirements) = config_layer_stack - .requirements_toml() - .feature_requirements - .as_ref() - { - protected_features.extend(feature_requirements.entries.keys().cloned()); - } - - protected_features -} - -pub(crate) fn apply_runtime_feature_enablement( - config: &mut Config, - runtime_feature_enablement: &BTreeMap, -) { - let protected_features = protected_feature_keys(&config.config_layer_stack); - for (name, enabled) in runtime_feature_enablement { - if protected_features.contains(name) { - continue; - } - let Some(feature) = feature_for_key(name) else { - continue; - }; - if let Err(err) = config.features.set_enabled(feature, *enabled) { - warn!( - feature = name, - error = %err, - "failed to apply runtime feature enablement" - ); - } - } -} - fn map_requirements_toml_to_api(requirements: ConfigRequirementsToml) -> ConfigRequirements { ConfigRequirements { allowed_approval_policies: requirements.allowed_approval_policies.map(|policies| { @@ -495,7 +389,7 @@ fn map_network_unix_socket_permission_to_api( } } -fn map_error(err: ConfigServiceError) -> JSONRPCErrorError { +fn map_error(err: ConfigManagerError) -> JSONRPCErrorError { if let Some(code) = err.write_error_code() { return config_write_error(code, err.to_string()); } @@ -520,7 +414,11 @@ fn config_write_error(code: ConfigWriteErrorCode, message: impl Into) -> #[cfg(test)] mod tests { use super::*; + use crate::config_manager::apply_runtime_feature_enablement; use codex_analytics::AnalyticsEventsClient; + use codex_arg0::Arg0DispatchPaths; + use codex_core::config_loader::CloudRequirementsLoader; + use codex_core::config_loader::LoaderOverrides; use codex_core::config_loader::NetworkDomainPermissionToml as CoreNetworkDomainPermissionToml; use codex_core::config_loader::NetworkDomainPermissionsToml as CoreNetworkDomainPermissionsToml; use codex_core::config_loader::NetworkRequirementsToml as CoreNetworkRequirementsToml; @@ -533,9 +431,12 @@ mod tests { use codex_protocol::protocol::AskForApproval as CoreAskForApproval; use pretty_assertions::assert_eq; use serde_json::json; + use std::collections::BTreeMap; + use std::sync::RwLock; use std::sync::atomic::AtomicUsize; use std::sync::atomic::Ordering; use tempfile::TempDir; + use toml::Value as TomlValue; #[derive(Default)] struct RecordingUserConfigReloader { @@ -830,11 +731,15 @@ mod tests { ); let auth_manager = AuthManager::from_auth_for_testing(CodexAuth::from_api_key("test")); let config_api = ConfigApi::new( - codex_home.path().to_path_buf(), - Arc::new(RwLock::new(Vec::new())), - Arc::new(RwLock::new(BTreeMap::new())), - LoaderOverrides::default(), - Arc::new(RwLock::new(CloudRequirementsLoader::default())), + ConfigManager::new( + codex_home.path().to_path_buf(), + Arc::new(RwLock::new(Vec::new())), + Arc::new(RwLock::new(BTreeMap::new())), + LoaderOverrides::default(), + Arc::new(RwLock::new(CloudRequirementsLoader::default())), + Arg0DispatchPaths::default(), + Arc::new(codex_config::NoopThreadConfigLoader), + ), reloader.clone(), AnalyticsEventsClient::new( auth_manager, diff --git a/codex-rs/app-server/src/config_manager.rs b/codex-rs/app-server/src/config_manager.rs new file mode 100644 index 0000000000..67e0072f47 --- /dev/null +++ b/codex-rs/app-server/src/config_manager.rs @@ -0,0 +1,326 @@ +use codex_arg0::Arg0DispatchPaths; +use codex_cloud_requirements::cloud_requirements_loader; +use codex_config::ThreadConfigLoader; +use codex_core::config::Config; +use codex_core::config::ConfigOverrides; +use codex_core::config_loader::CloudRequirementsLoader; +use codex_core::config_loader::ConfigLayerStack; +use codex_core::config_loader::LoaderOverrides; +use codex_core::config_loader::load_config_layers_state; +use codex_exec_server::LOCAL_FS; +use codex_features::feature_for_key; +use codex_login::AuthManager; +use codex_login::default_client::set_default_client_residency_requirement; +use codex_utils_absolute_path::AbsolutePathBuf; +use codex_utils_json_to_toml::json_to_toml; +use std::collections::BTreeMap; +use std::collections::BTreeSet; +use std::collections::HashMap; +use std::path::Path; +use std::path::PathBuf; +use std::sync::Arc; +use std::sync::RwLock; +use toml::Value as TomlValue; +use tracing::warn; + +/// Shared app-server entry point for loading effective Codex configuration. +#[derive(Clone)] +pub(crate) struct ConfigManager { + codex_home: PathBuf, + cli_overrides: Arc>>, + runtime_feature_enablement: Arc>>, + loader_overrides: LoaderOverrides, + cloud_requirements: Arc>, + arg0_paths: Arg0DispatchPaths, + thread_config_loader: Arc, + host_name: Option, +} + +impl ConfigManager { + pub(crate) fn new( + codex_home: PathBuf, + cli_overrides: Arc>>, + runtime_feature_enablement: Arc>>, + loader_overrides: LoaderOverrides, + cloud_requirements: Arc>, + arg0_paths: Arg0DispatchPaths, + thread_config_loader: Arc, + ) -> Self { + Self::new_with_host_name( + codex_home, + cli_overrides, + runtime_feature_enablement, + loader_overrides, + cloud_requirements, + arg0_paths, + thread_config_loader, + codex_config::host_name(), + ) + } + + #[allow(clippy::too_many_arguments)] + fn new_with_host_name( + codex_home: PathBuf, + cli_overrides: Arc>>, + runtime_feature_enablement: Arc>>, + loader_overrides: LoaderOverrides, + cloud_requirements: Arc>, + arg0_paths: Arg0DispatchPaths, + thread_config_loader: Arc, + host_name: Option, + ) -> Self { + Self { + codex_home, + cli_overrides, + runtime_feature_enablement, + loader_overrides, + cloud_requirements, + arg0_paths, + thread_config_loader, + host_name, + } + } + + pub(crate) fn codex_home(&self) -> &Path { + self.codex_home.as_path() + } + + pub(crate) fn current_cli_overrides(&self) -> Vec<(String, TomlValue)> { + self.cli_overrides + .read() + .map(|guard| guard.clone()) + .unwrap_or_default() + } + + pub(crate) fn current_cloud_requirements(&self) -> CloudRequirementsLoader { + self.cloud_requirements + .read() + .map(|guard| guard.clone()) + .unwrap_or_default() + } + + pub(crate) fn extend_runtime_feature_enablement(&self, enablement: I) -> Result<(), ()> + where + I: IntoIterator, + { + let mut runtime_feature_enablement = + self.runtime_feature_enablement.write().map_err(|_| ())?; + runtime_feature_enablement.extend(enablement); + Ok(()) + } + + pub(crate) fn replace_cloud_requirements_loader( + &self, + auth_manager: Arc, + chatgpt_base_url: String, + ) { + let loader = + cloud_requirements_loader(auth_manager, chatgpt_base_url, self.codex_home.clone()); + if let Ok(mut guard) = self.cloud_requirements.write() { + *guard = loader; + } else { + warn!("failed to update cloud requirements loader"); + } + } + + pub(crate) async fn sync_default_client_residency_requirement(&self) { + match self.load_latest_config(/*fallback_cwd*/ None).await { + Ok(config) => { + set_default_client_residency_requirement(config.enforce_residency.value()); + } + Err(err) => warn!( + error = %err, + "failed to sync default client residency requirement after auth refresh" + ), + } + } + + pub(crate) async fn load_latest_config( + &self, + fallback_cwd: Option, + ) -> std::io::Result { + self.load_with_cli_overrides( + &self.current_cli_overrides(), + /*request_overrides*/ None, + ConfigOverrides::default(), + fallback_cwd, + ) + .await + } + + pub(crate) async fn load_with_overrides( + &self, + request_overrides: Option>, + typesafe_overrides: ConfigOverrides, + ) -> std::io::Result { + self.load_with_cli_overrides( + &self.current_cli_overrides(), + request_overrides, + typesafe_overrides, + /*fallback_cwd*/ None, + ) + .await + } + + pub(crate) async fn load_for_cwd( + &self, + request_overrides: Option>, + typesafe_overrides: ConfigOverrides, + cwd: Option, + ) -> std::io::Result { + self.load_with_cli_overrides( + &self.current_cli_overrides(), + request_overrides, + typesafe_overrides, + cwd, + ) + .await + } + + pub(crate) async fn load_with_cli_overrides( + &self, + cli_overrides: &[(String, TomlValue)], + request_overrides: Option>, + typesafe_overrides: ConfigOverrides, + fallback_cwd: Option, + ) -> std::io::Result { + let merged_cli_overrides = cli_overrides + .iter() + .cloned() + .chain( + request_overrides + .unwrap_or_default() + .into_iter() + .map(|(key, value)| (key, json_to_toml(value))), + ) + .collect::>(); + + let mut config = codex_core::config::ConfigBuilder::default() + .codex_home(self.codex_home.clone()) + .cli_overrides(merged_cli_overrides) + .loader_overrides(self.loader_overrides.clone()) + .harness_overrides(typesafe_overrides) + .fallback_cwd(fallback_cwd) + .cloud_requirements(self.current_cloud_requirements()) + .thread_config_loader(Arc::clone(&self.thread_config_loader)) + .host_name(self.host_name.clone()) + .build() + .await?; + self.apply_runtime_feature_enablement(&mut config); + self.apply_arg0_paths(&mut config); + Ok(config) + } + + pub(crate) async fn load_config_layers_for_cwd( + &self, + cwd: AbsolutePathBuf, + ) -> std::io::Result { + self.load_config_layers(Some(cwd)).await + } + + pub(crate) async fn load_config_layers( + &self, + cwd: Option, + ) -> std::io::Result { + load_config_layers_state( + LOCAL_FS.as_ref(), + &self.codex_home, + cwd, + &self.current_cli_overrides(), + self.loader_overrides.clone(), + self.current_cloud_requirements(), + self.thread_config_loader.as_ref(), + self.host_name.as_deref(), + ) + .await + } + + fn apply_runtime_feature_enablement(&self, config: &mut Config) { + apply_runtime_feature_enablement(config, &self.current_runtime_feature_enablement()); + } + + fn current_runtime_feature_enablement(&self) -> BTreeMap { + self.runtime_feature_enablement + .read() + .map(|guard| guard.clone()) + .unwrap_or_default() + } + + fn apply_arg0_paths(&self, config: &mut Config) { + config.codex_self_exe = self.arg0_paths.codex_self_exe.clone(); + config.codex_linux_sandbox_exe = self.arg0_paths.codex_linux_sandbox_exe.clone(); + config.main_execve_wrapper_exe = self.arg0_paths.main_execve_wrapper_exe.clone(); + } + + #[cfg(test)] + pub(crate) fn new_for_tests( + codex_home: PathBuf, + cli_overrides: Vec<(String, TomlValue)>, + loader_overrides: LoaderOverrides, + cloud_requirements: CloudRequirementsLoader, + host_name: Option, + ) -> Self { + Self::new_with_host_name( + codex_home, + Arc::new(RwLock::new(cli_overrides)), + Arc::new(RwLock::new(BTreeMap::new())), + loader_overrides, + Arc::new(RwLock::new(cloud_requirements)), + Arg0DispatchPaths::default(), + Arc::new(codex_config::NoopThreadConfigLoader), + host_name, + ) + } + + #[cfg(test)] + pub(crate) fn without_managed_config_for_tests(codex_home: PathBuf) -> Self { + Self::new_for_tests( + codex_home, + Vec::new(), + LoaderOverrides::without_managed_config_for_tests(), + CloudRequirementsLoader::default(), + /*host_name*/ None, + ) + } +} + +pub(crate) fn protected_feature_keys(config_layer_stack: &ConfigLayerStack) -> BTreeSet { + let mut protected_features = config_layer_stack + .effective_config() + .get("features") + .and_then(toml::Value::as_table) + .map(|features| features.keys().cloned().collect::>()) + .unwrap_or_default(); + + if let Some(feature_requirements) = config_layer_stack + .requirements_toml() + .feature_requirements + .as_ref() + { + protected_features.extend(feature_requirements.entries.keys().cloned()); + } + + protected_features +} + +pub(crate) fn apply_runtime_feature_enablement( + config: &mut Config, + runtime_feature_enablement: &BTreeMap, +) { + let protected_features = protected_feature_keys(&config.config_layer_stack); + for (name, enabled) in runtime_feature_enablement { + if protected_features.contains(name) { + continue; + } + let Some(feature) = feature_for_key(name) else { + continue; + }; + if let Err(err) = config.features.set_enabled(feature, *enabled) { + warn!( + feature = name, + error = %err, + "failed to apply runtime feature enablement" + ); + } + } +} diff --git a/codex-rs/core/src/config/service.rs b/codex-rs/app-server/src/config_manager_service.rs similarity index 74% rename from codex-rs/core/src/config/service.rs rename to codex-rs/app-server/src/config_manager_service.rs index e9546ef4db..0104429a4b 100644 --- a/codex-rs/core/src/config/service.rs +++ b/codex-rs/app-server/src/config_manager_service.rs @@ -1,20 +1,4 @@ -use super::deserialize_config_toml_with_base; -use crate::config::edit::ConfigEdit; -use crate::config::edit::ConfigEditsBuilder; -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; -use crate::config_loader::ConfigLayerEntry; -use crate::config_loader::ConfigLayerStack; -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::merge_toml_values; -use crate::path_utils; -use crate::path_utils::SymlinkWritePaths; -use crate::path_utils::resolve_symlink_write_paths; -use crate::path_utils::write_atomically; +use crate::config_manager::ConfigManager; use codex_app_server_protocol::Config as ApiConfig; use codex_app_server_protocol::ConfigBatchWriteParams; use codex_app_server_protocol::ConfigLayerMetadata; @@ -29,7 +13,19 @@ use codex_app_server_protocol::OverriddenMetadata; use codex_app_server_protocol::WriteStatus; use codex_config::CONFIG_TOML_FILE; use codex_config::config_toml::ConfigToml; -use codex_exec_server::LOCAL_FS; +use codex_core::config::deserialize_config_toml_with_base; +use codex_core::config::edit::ConfigEdit; +use codex_core::config::edit::ConfigEditsBuilder; +use codex_core::config::validate_feature_requirements_for_config_toml; +use codex_core::config_loader::ConfigLayerEntry; +use codex_core::config_loader::ConfigLayerStack; +use codex_core::config_loader::ConfigLayerStackOrdering; +use codex_core::config_loader::ConfigRequirementsToml; +use codex_core::config_loader::merge_toml_values; +use codex_core::path_utils; +use codex_core::path_utils::SymlinkWritePaths; +use codex_core::path_utils::resolve_symlink_write_paths; +use codex_core::path_utils::write_atomically; use codex_utils_absolute_path::AbsolutePathBuf; use serde_json::Value as JsonValue; use std::borrow::Cow; @@ -41,7 +37,7 @@ use toml::Value as TomlValue; use toml_edit::Item as TomlItem; #[derive(Debug, Error)] -pub enum ConfigServiceError { +pub(crate) enum ConfigManagerError { #[error("{message}")] Write { code: ConfigWriteErrorCode, @@ -77,7 +73,7 @@ pub enum ConfigServiceError { }, } -impl ConfigServiceError { +impl ConfigManagerError { fn write(code: ConfigWriteErrorCode, message: impl Into) -> Self { Self::Write { code, @@ -101,7 +97,7 @@ impl ConfigServiceError { Self::Anyhow { context, source } } - pub fn write_error_code(&self) -> Option { + pub(crate) fn write_error_code(&self) -> Option { match self { Self::Write { code, .. } => Some(code.clone()), _ => None, @@ -109,78 +105,22 @@ impl ConfigServiceError { } } -#[derive(Clone)] -pub struct ConfigService { - codex_home: PathBuf, - cli_overrides: Vec<(String, TomlValue)>, - loader_overrides: LoaderOverrides, - cloud_requirements: CloudRequirementsLoader, - host_name: Option, -} - -impl ConfigService { - pub fn new( - codex_home: PathBuf, - cli_overrides: Vec<(String, TomlValue)>, - loader_overrides: LoaderOverrides, - cloud_requirements: CloudRequirementsLoader, - host_name: Option, - ) -> Self { - Self { - codex_home, - cli_overrides, - loader_overrides, - cloud_requirements, - host_name, - } - } - - pub fn new_with_defaults(codex_home: PathBuf) -> Self { - Self { - codex_home, - cli_overrides: Vec::new(), - loader_overrides: LoaderOverrides::default(), - cloud_requirements: CloudRequirementsLoader::default(), - host_name: codex_config::host_name(), - } - } - - #[cfg(test)] - pub(crate) fn without_managed_config_for_tests(codex_home: PathBuf) -> Self { - Self::new( - codex_home, - Vec::new(), - LoaderOverrides::without_managed_config_for_tests(), - CloudRequirementsLoader::default(), - /*host_name*/ None, - ) - } - - pub async fn read( +impl ConfigManager { + pub(crate) async fn read( &self, params: ConfigReadParams, - ) -> Result { + ) -> Result { 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) + ConfigManagerError::io("failed to resolve config cwd to an absolute path", err) })?; - crate::config::ConfigBuilder::default() - .codex_home(self.codex_home.clone()) - .cli_overrides(self.cli_overrides.clone()) - .loader_overrides(self.loader_overrides.clone()) - .fallback_cwd(Some(cwd.to_path_buf())) - .cloud_requirements(self.cloud_requirements.clone()) - .host_name(self.host_name.clone()) - .build() - .await - .map_err(|err| { - ConfigServiceError::io("failed to read configuration layers", err) - })? - .config_layer_stack + self.load_config_layers(Some(cwd)).await.map_err(|err| { + ConfigManagerError::io("failed to read configuration layers", err) + })? } None => self.load_thread_agnostic_config().await.map_err(|err| { - ConfigServiceError::io("failed to read configuration layers", err) + ConfigManagerError::io("failed to read configuration layers", err) })?, }; @@ -188,12 +128,12 @@ impl ConfigService { let effective_config_toml: ConfigToml = effective .try_into() - .map_err(|err| ConfigServiceError::toml("invalid configuration", err))?; + .map_err(|err| ConfigManagerError::toml("invalid configuration", err))?; let json_value = serde_json::to_value(&effective_config_toml) - .map_err(|err| ConfigServiceError::json("failed to serialize configuration", err))?; + .map_err(|err| ConfigManagerError::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))?; + .map_err(|err| ConfigManagerError::json("failed to deserialize configuration", err))?; Ok(ConfigReadResponse { config, @@ -211,13 +151,13 @@ impl ConfigService { }) } - pub async fn read_requirements( + pub(crate) async fn read_requirements( &self, - ) -> Result, ConfigServiceError> { + ) -> Result, ConfigManagerError> { let layers = self .load_thread_agnostic_config() .await - .map_err(|err| ConfigServiceError::io("failed to read configuration layers", err))?; + .map_err(|err| ConfigManagerError::io("failed to read configuration layers", err))?; let requirements = layers.requirements_toml().clone(); if requirements.is_empty() { @@ -227,19 +167,19 @@ impl ConfigService { } } - pub async fn write_value( + pub(crate) async fn write_value( &self, params: ConfigValueWriteParams, - ) -> Result { + ) -> Result { let edits = vec![(params.key_path, params.value, params.merge_strategy)]; self.apply_edits(params.file_path, params.expected_version, edits) .await } - pub async fn batch_write( + pub(crate) async fn batch_write( &self, params: ConfigBatchWriteParams, - ) -> Result { + ) -> Result { let edits = params .edits .into_iter() @@ -250,37 +190,22 @@ impl ConfigService { .await } - pub async fn load_user_saved_config( - &self, - ) -> Result { - let layers = self - .load_thread_agnostic_config() - .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, expected_version: Option, edits: Vec<(String, JsonValue, MergeStrategy)>, - ) -> Result { + ) -> Result { let allowed_path = - AbsolutePathBuf::resolve_path_against_base(CONFIG_TOML_FILE, &self.codex_home); + AbsolutePathBuf::resolve_path_against_base(CONFIG_TOML_FILE, self.codex_home()); 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))?, + .map_err(|err| ConfigManagerError::io("failed to resolve user config path", err))?, None => allowed_path.clone(), }; if !paths_match(&allowed_path, &provided_path) { - return Err(ConfigServiceError::write( + return Err(ConfigManagerError::write( ConfigWriteErrorCode::ConfigLayerReadonly, "Only writes to the user config are allowed", )); @@ -289,7 +214,7 @@ impl ConfigService { let layers = self .load_thread_agnostic_config() .await - .map_err(|err| ConfigServiceError::io("failed to load configuration", err))?; + .map_err(|err| ConfigManagerError::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(&allowed_path).await?), @@ -298,7 +223,7 @@ impl ConfigService { if let Some(expected) = expected_version.as_deref() && expected != user_layer.version { - return Err(ConfigServiceError::write( + return Err(ConfigManagerError::write( ConfigWriteErrorCode::ConfigVersionConflict, "Configuration was modified since last read. Fetch latest version and retry.", )); @@ -310,20 +235,20 @@ impl ConfigService { for (key_path, value, strategy) in edits.into_iter() { let segments = parse_key_path(&key_path).map_err(|message| { - ConfigServiceError::write(ConfigWriteErrorCode::ConfigValidationError, message) + ConfigManagerError::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) + ConfigManagerError::write(ConfigWriteErrorCode::ConfigValidationError, message) })?; apply_merge(&mut user_config, &segments, parsed_value.as_ref(), strategy).map_err( |err| match err { - MergeError::PathNotFound => ConfigServiceError::write( + MergeError::PathNotFound => ConfigManagerError::write( ConfigWriteErrorCode::ConfigPathNotFound, "Path not found", ), - MergeError::Validation(message) => ConfigServiceError::write( + MergeError::Validation(message) => ConfigManagerError::write( ConfigWriteErrorCode::ConfigValidationError, message, ), @@ -336,7 +261,7 @@ impl ConfigService { Some(value) => ConfigEdit::SetPath { segments: segments.clone(), value: toml_value_to_item(&value).map_err(|err| { - ConfigServiceError::anyhow("failed to build config edits", err) + ConfigManagerError::anyhow("failed to build config edits", err) })?, }, None => ConfigEdit::ClearPath { @@ -350,56 +275,45 @@ impl ConfigService { } validate_config(&user_config).map_err(|err| { - ConfigServiceError::write( + ConfigManagerError::write( ConfigWriteErrorCode::ConfigValidationError, format!("Invalid configuration: {err}"), ) })?; let user_config_toml = - deserialize_config_toml_with_base(user_config.clone(), &self.codex_home).map_err( + deserialize_config_toml_with_base(user_config.clone(), self.codex_home()).map_err( |err| { - ConfigServiceError::write( + ConfigManagerError::write( ConfigWriteErrorCode::ConfigValidationError, format!("Invalid configuration: {err}"), ) }, )?; - validate_explicit_feature_settings_in_config_toml( + validate_feature_requirements_for_config_toml( &user_config_toml, layers.requirements().feature_requirements.as_ref(), ) .map_err(|err| { - ConfigServiceError::write( + ConfigManagerError::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( + ConfigManagerError::write( ConfigWriteErrorCode::ConfigValidationError, format!("Invalid configuration: {err}"), ) })?; if !config_edits.is_empty() { - ConfigEditsBuilder::new(&self.codex_home) + ConfigEditsBuilder::new(self.codex_home()) .with_edits(config_edits) .apply() .await - .map_err(|err| ConfigServiceError::anyhow("failed to persist config.toml", err))?; + .map_err(|err| ConfigManagerError::anyhow("failed to persist config.toml", err))?; } let overridden = first_overridden_edit(&updated_layers, &effective, &parsed_segments); @@ -413,7 +327,7 @@ impl ConfigService { version: updated_layers .get_user_layer() .ok_or_else(|| { - ConfigServiceError::write( + ConfigManagerError::write( ConfigWriteErrorCode::UserLayerNotFound, "user layer not found in updated layers", ) @@ -429,40 +343,29 @@ impl ConfigService { /// include any in-repo .codex/ folders because there is no cwd/project root /// associated with this query. async fn load_thread_agnostic_config(&self) -> std::io::Result { - let cwd: Option = None; - load_config_layers_state( - LOCAL_FS.as_ref(), - &self.codex_home, - cwd, - &self.cli_overrides, - self.loader_overrides.clone(), - self.cloud_requirements.clone(), - &codex_config::NoopThreadConfigLoader, - self.host_name.as_deref(), - ) - .await + self.load_config_layers(/*cwd*/ None).await } } async fn create_empty_user_layer( config_toml: &AbsolutePathBuf, -) -> Result { +) -> Result { let SymlinkWritePaths { read_path, write_path, } = resolve_symlink_write_paths(config_toml.as_path()) - .map_err(|err| ConfigServiceError::io("failed to resolve user config path", err))?; + .map_err(|err| ConfigManagerError::io("failed to resolve user config path", err))?; let toml_value = match read_path { Some(path) => match tokio::fs::read_to_string(&path).await { Ok(contents) => toml::from_str(&contents).map_err(|e| { - ConfigServiceError::toml("failed to parse existing user config.toml", e) + ConfigManagerError::toml("failed to parse existing user config.toml", e) })?, Err(err) if err.kind() == std::io::ErrorKind::NotFound => { write_empty_user_config(write_path.clone()).await?; TomlValue::Table(toml::map::Map::new()) } Err(err) => { - return Err(ConfigServiceError::io( + return Err(ConfigManagerError::io( "failed to read user config.toml", err, )); @@ -481,11 +384,11 @@ async fn create_empty_user_layer( )) } -async fn write_empty_user_config(write_path: PathBuf) -> Result<(), ConfigServiceError> { +async fn write_empty_user_config(write_path: PathBuf) -> Result<(), ConfigManagerError> { task::spawn_blocking(move || write_atomically(&write_path, "")) .await - .map_err(|err| ConfigServiceError::anyhow("config persistence task panicked", err.into()))? - .map_err(|err| ConfigServiceError::io("failed to create empty user config.toml", err)) + .map_err(|err| ConfigManagerError::anyhow("config persistence task panicked", err.into()))? + .map_err(|err| ConfigManagerError::io("failed to create empty user config.toml", err)) } fn parse_value(value: JsonValue) -> Result, String> { @@ -746,5 +649,5 @@ fn find_effective_layer( } #[cfg(test)] -#[path = "service_tests.rs"] +#[path = "config_manager_service_tests.rs"] mod tests; diff --git a/codex-rs/core/src/config/service_tests.rs b/codex-rs/app-server/src/config_manager_service_tests.rs similarity index 95% rename from codex-rs/core/src/config/service_tests.rs rename to codex-rs/app-server/src/config_manager_service_tests.rs index ceb801c456..a871d8e43f 100644 --- a/codex-rs/core/src/config/service_tests.rs +++ b/codex-rs/app-server/src/config_manager_service_tests.rs @@ -4,6 +4,9 @@ use codex_app_server_protocol::AppConfig; use codex_app_server_protocol::AppToolApproval; use codex_app_server_protocol::AppsConfig; use codex_app_server_protocol::AskForApproval; +use codex_core::config_loader::CloudRequirementsLoader; +use codex_core::config_loader::FeatureRequirementsToml; +use codex_core::config_loader::LoaderOverrides; use codex_utils_absolute_path::AbsolutePathBuf; use pretty_assertions::assert_eq; use std::collections::BTreeMap; @@ -74,7 +77,7 @@ unified_exec = true "#; std::fs::write(tmp.path().join(CONFIG_TOML_FILE), original)?; - let service = ConfigService::without_managed_config_for_tests(tmp.path().to_path_buf()); + let service = ConfigManager::without_managed_config_for_tests(tmp.path().to_path_buf()); service .write_value(ConfigValueWriteParams { file_path: Some(tmp.path().join(CONFIG_TOML_FILE).display().to_string()), @@ -108,7 +111,7 @@ async fn write_value_supports_nested_app_paths() -> Result<()> { let tmp = tempdir().expect("tempdir"); std::fs::write(tmp.path().join(CONFIG_TOML_FILE), "")?; - let service = ConfigService::without_managed_config_for_tests(tmp.path().to_path_buf()); + let service = ConfigManager::without_managed_config_for_tests(tmp.path().to_path_buf()); service .write_value(ConfigValueWriteParams { file_path: Some(tmp.path().join(CONFIG_TOML_FILE).display().to_string()), @@ -172,7 +175,7 @@ async fn write_value_supports_custom_mcp_server_default_tool_approval_mode() -> "[mcp_servers.docs]\ncommand = \"docs-server\"\n", )?; - let service = ConfigService::without_managed_config_for_tests(tmp.path().to_path_buf()); + let service = ConfigManager::without_managed_config_for_tests(tmp.path().to_path_buf()); service .write_value(ConfigValueWriteParams { file_path: Some(tmp.path().join(CONFIG_TOML_FILE).display().to_string()), @@ -218,7 +221,7 @@ async fn read_includes_origins_and_layers() { std::fs::write(&managed_path, "approval_policy = \"never\"").unwrap(); let managed_file = AbsolutePathBuf::try_from(managed_path.clone()).expect("managed file"); - let service = ConfigService::new( + let service = ConfigManager::new_for_tests( tmp.path().to_path_buf(), vec![], LoaderOverrides::with_managed_config_path_for_tests(managed_path.clone()), @@ -297,7 +300,7 @@ writable_roots = ["~/code"] ), ); - let service = ConfigService::new( + let service = ConfigManager::new_for_tests( tmp.path().to_path_buf(), vec![], loader_overrides, @@ -338,7 +341,7 @@ async fn write_value_reports_override() { std::fs::write(&managed_path, "approval_policy = \"never\"").unwrap(); let managed_file = AbsolutePathBuf::try_from(managed_path.clone()).expect("managed file"); - let service = ConfigService::new( + let service = ConfigManager::new_for_tests( tmp.path().to_path_buf(), vec![], LoaderOverrides::with_managed_config_path_for_tests(managed_path.clone()), @@ -388,7 +391,7 @@ async fn version_conflict_rejected() { let user_path = tmp.path().join(CONFIG_TOML_FILE); std::fs::write(&user_path, "model = \"user\"").unwrap(); - let service = ConfigService::without_managed_config_for_tests(tmp.path().to_path_buf()); + let service = ConfigManager::without_managed_config_for_tests(tmp.path().to_path_buf()); let error = service .write_value(ConfigValueWriteParams { file_path: Some(tmp.path().join(CONFIG_TOML_FILE).display().to_string()), @@ -411,7 +414,7 @@ async fn write_value_defaults_to_user_config_path() { let tmp = tempdir().expect("tempdir"); std::fs::write(tmp.path().join(CONFIG_TOML_FILE), "").unwrap(); - let service = ConfigService::without_managed_config_for_tests(tmp.path().to_path_buf()); + let service = ConfigManager::without_managed_config_for_tests(tmp.path().to_path_buf()); service .write_value(ConfigValueWriteParams { file_path: None, @@ -438,7 +441,7 @@ async fn invalid_user_value_rejected_even_if_overridden_by_managed() { let managed_path = tmp.path().join("managed_config.toml"); std::fs::write(&managed_path, "approval_policy = \"never\"").unwrap(); - let service = ConfigService::new( + let service = ConfigManager::new_for_tests( tmp.path().to_path_buf(), vec![], LoaderOverrides::with_managed_config_path_for_tests(managed_path.clone()), @@ -471,7 +474,7 @@ async fn reserved_builtin_provider_override_rejected() { let tmp = tempdir().expect("tempdir"); std::fs::write(tmp.path().join(CONFIG_TOML_FILE), "model = \"user\"\n").unwrap(); - let service = ConfigService::without_managed_config_for_tests(tmp.path().to_path_buf()); + let service = ConfigManager::without_managed_config_for_tests(tmp.path().to_path_buf()); let error = service .write_value(ConfigValueWriteParams { file_path: Some(tmp.path().join(CONFIG_TOML_FILE).display().to_string()), @@ -499,13 +502,13 @@ async fn write_value_rejects_feature_requirement_conflict() { let tmp = tempdir().expect("tempdir"); std::fs::write(tmp.path().join(CONFIG_TOML_FILE), "").unwrap(); - let service = ConfigService::new( + let service = ConfigManager::new_for_tests( tmp.path().to_path_buf(), vec![], LoaderOverrides::without_managed_config_for_tests(), CloudRequirementsLoader::new(async { Ok(Some(ConfigRequirementsToml { - feature_requirements: Some(crate::config_loader::FeatureRequirementsToml { + feature_requirements: Some(FeatureRequirementsToml { entries: BTreeMap::from([("personality".to_string(), true)]), }), ..Default::default() @@ -546,13 +549,13 @@ async fn write_value_rejects_profile_feature_requirement_conflict() { let tmp = tempdir().expect("tempdir"); std::fs::write(tmp.path().join(CONFIG_TOML_FILE), "").unwrap(); - let service = ConfigService::new( + let service = ConfigManager::new_for_tests( tmp.path().to_path_buf(), vec![], LoaderOverrides::without_managed_config_for_tests(), CloudRequirementsLoader::new(async { Ok(Some(ConfigRequirementsToml { - feature_requirements: Some(crate::config_loader::FeatureRequirementsToml { + feature_requirements: Some(FeatureRequirementsToml { entries: BTreeMap::from([("personality".to_string(), true)]), }), ..Default::default() @@ -604,7 +607,7 @@ async fn read_reports_managed_overrides_user_and_session_flags() { TomlValue::String("session".to_string()), )]; - let service = ConfigService::new( + let service = ConfigManager::new_for_tests( tmp.path().to_path_buf(), cli_overrides, LoaderOverrides::with_managed_config_path_for_tests(managed_path.clone()), @@ -658,7 +661,7 @@ async fn write_value_reports_managed_override() { std::fs::write(&managed_path, "approval_policy = \"never\"").unwrap(); let managed_file = AbsolutePathBuf::try_from(managed_path.clone()).expect("managed file"); - let service = ConfigService::new( + let service = ConfigManager::new_for_tests( tmp.path().to_path_buf(), vec![], LoaderOverrides::with_managed_config_path_for_tests(managed_path.clone()), @@ -714,7 +717,7 @@ alpha = "a" std::fs::write(&path, base)?; - let service = ConfigService::without_managed_config_for_tests(tmp.path().to_path_buf()); + let service = ConfigManager::without_managed_config_for_tests(tmp.path().to_path_buf()); service .write_value(ConfigValueWriteParams { file_path: Some(path.display().to_string()), diff --git a/codex-rs/app-server/src/lib.rs b/codex-rs/app-server/src/lib.rs index 0eac54df2a..d4573b267a 100644 --- a/codex-rs/app-server/src/lib.rs +++ b/codex-rs/app-server/src/lib.rs @@ -72,6 +72,8 @@ mod codex_message_processor; mod command_exec; mod config; mod config_api; +mod config_manager; +mod config_manager_service; mod dynamic_tools; mod error_code; mod external_agent_config_api; diff --git a/codex-rs/app-server/src/message_processor.rs b/codex-rs/app-server/src/message_processor.rs index 2032eb61cd..53d9f2df4c 100644 --- a/codex-rs/app-server/src/message_processor.rs +++ b/codex-rs/app-server/src/message_processor.rs @@ -10,6 +10,7 @@ use std::sync::atomic::Ordering; use crate::codex_message_processor::CodexMessageProcessor; use crate::codex_message_processor::CodexMessageProcessorArgs; use crate::config_api::ConfigApi; +use crate::config_manager::ConfigManager; use crate::error_code::INVALID_REQUEST_ERROR_CODE; use crate::external_agent_config_api::ExternalAgentConfigApi; use crate::fs_api::FsApi; @@ -294,6 +295,15 @@ impl MessageProcessor { let cli_overrides = Arc::new(RwLock::new(cli_overrides)); let runtime_feature_enablement = Arc::new(RwLock::new(BTreeMap::new())); let cloud_requirements = Arc::new(RwLock::new(cloud_requirements)); + let config_manager = ConfigManager::new( + config.codex_home.to_path_buf(), + cli_overrides, + runtime_feature_enablement, + loader_overrides, + cloud_requirements, + arg0_paths.clone(), + thread_config_loader, + ); let codex_message_processor = CodexMessageProcessor::new(CodexMessageProcessorArgs { auth_manager: auth_manager.clone(), thread_manager: Arc::clone(&thread_manager), @@ -301,10 +311,7 @@ impl MessageProcessor { analytics_events_client: analytics_events_client.clone(), arg0_paths, config: Arc::clone(&config), - cli_overrides: cli_overrides.clone(), - runtime_feature_enablement: runtime_feature_enablement.clone(), - cloud_requirements: cloud_requirements.clone(), - thread_config_loader, + config_manager: config_manager.clone(), feedback, log_db, }); @@ -314,11 +321,7 @@ impl MessageProcessor { .plugins_manager() .maybe_start_plugin_startup_tasks_for_config(&config, auth_manager.clone()); let config_api = ConfigApi::new( - config.codex_home.to_path_buf(), - cli_overrides, - runtime_feature_enablement, - loader_overrides, - cloud_requirements, + config_manager, thread_manager.clone(), analytics_events_client.clone(), ); diff --git a/codex-rs/app-server/tests/suite/v2/experimental_feature_list.rs b/codex-rs/app-server/tests/suite/v2/experimental_feature_list.rs index 07bdd0bde1..3f7e6989f8 100644 --- a/codex-rs/app-server/tests/suite/v2/experimental_feature_list.rs +++ b/codex-rs/app-server/tests/suite/v2/experimental_feature_list.rs @@ -15,6 +15,7 @@ use codex_app_server_protocol::JSONRPCError; use codex_app_server_protocol::JSONRPCResponse; use codex_app_server_protocol::RequestId; use codex_core::config::ConfigBuilder; +use codex_core::config_loader::LoaderOverrides; use codex_features::FEATURES; use codex_features::Stage; use pretty_assertions::assert_eq; @@ -32,6 +33,9 @@ async fn experimental_feature_list_returns_feature_metadata_with_stage() -> Resu let config = ConfigBuilder::default() .codex_home(codex_home.path().to_path_buf()) .fallback_cwd(Some(codex_home.path().to_path_buf())) + .loader_overrides(LoaderOverrides::with_managed_config_path_for_tests( + codex_home.path().join("managed_config.toml"), + )) .build() .await?; let mut mcp = McpProcess::new(codex_home.path()).await?; diff --git a/codex-rs/core/src/config/mod.rs b/codex-rs/core/src/config/mod.rs index a6240fdcb3..7ae710f83c 100644 --- a/codex-rs/core/src/config/mod.rs +++ b/codex-rs/core/src/config/mod.rs @@ -7,6 +7,7 @@ use crate::config_loader::ConfigLayerStackOrdering; use crate::config_loader::ConfigRequirements; use crate::config_loader::ConfigRequirementsToml; use crate::config_loader::ConstrainedWithSource; +use crate::config_loader::FeatureRequirementsToml; use crate::config_loader::LoaderOverrides; use crate::config_loader::McpServerIdentity; use crate::config_loader::McpServerRequirement; @@ -108,7 +109,6 @@ mod network_proxy_spec; mod permissions; #[cfg(test)] mod schema; -pub(crate) mod service; pub use codex_config::Constrained; pub use codex_config::ConstraintError; pub use codex_config::ConstraintResult; @@ -118,8 +118,6 @@ pub use managed_features::ManagedFeatures; pub use network_proxy_spec::NetworkProxySpec; pub use network_proxy_spec::StartedNetworkProxy; pub(crate) use permissions::resolve_permission_profile; -pub use service::ConfigService; -pub use service::ConfigServiceError; pub use codex_git_utils::GhostSnapshotConfig; @@ -947,7 +945,7 @@ pub async fn load_config_as_toml_with_cli_and_loader_overrides( Ok(cfg) } -pub(crate) fn deserialize_config_toml_with_base( +pub fn deserialize_config_toml_with_base( root_value: TomlValue, config_base_dir: &Path, ) -> std::io::Result { @@ -959,6 +957,15 @@ pub(crate) fn deserialize_config_toml_with_base( .map_err(|e| std::io::Error::new(std::io::ErrorKind::InvalidData, e)) } +/// Validate user-visible feature settings against managed feature requirements. +pub fn validate_feature_requirements_for_config_toml( + cfg: &ConfigToml, + feature_requirements: Option<&Sourced>, +) -> std::io::Result<()> { + managed_features::validate_explicit_feature_settings_in_config_toml(cfg, feature_requirements)?; + managed_features::validate_feature_requirements_in_config_toml(cfg, feature_requirements) +} + fn load_catalog_json(path: &AbsolutePathBuf) -> std::io::Result { let file_contents = std::fs::read_to_string(path)?; let catalog = serde_json::from_str::(&file_contents).map_err(|err| { diff --git a/codex-rs/core/src/plugins/manager.rs b/codex-rs/core/src/plugins/manager.rs index 3698b4b191..efa91edc7c 100644 --- a/codex-rs/core/src/plugins/manager.rs +++ b/codex-rs/core/src/plugins/manager.rs @@ -6,14 +6,10 @@ use super::startup_sync::start_startup_remote_plugin_sync_once; use super::sync_openai_plugins_repo; use crate::SkillMetadata; use crate::config::Config; -use crate::config::ConfigService; -use crate::config::ConfigServiceError; use crate::config::edit::ConfigEdit; use crate::config::edit::ConfigEditsBuilder; use crate::config_loader::ConfigLayerStack; use codex_analytics::AnalyticsEventsClient; -use codex_app_server_protocol::ConfigValueWriteParams; -use codex_app_server_protocol::MergeStrategy; use codex_config::types::PluginConfig; use codex_core_plugins::loader::configured_curated_plugin_ids_from_codex_home; use codex_core_plugins::loader::installed_plugin_telemetry_metadata; @@ -61,7 +57,6 @@ use codex_plugin::PluginIdError; use codex_plugin::prompt_safe_plugin_description; use codex_protocol::protocol::Product; use codex_utils_absolute_path::AbsolutePathBuf; -use serde_json::json; use std::collections::HashMap; use std::collections::HashSet; use std::path::PathBuf; @@ -609,18 +604,17 @@ impl PluginsManager { .await .map_err(PluginInstallError::join)??; - ConfigService::new_with_defaults(self.codex_home.clone()) - .write_value(ConfigValueWriteParams { - key_path: format!("plugins.{}", result.plugin_id.as_key()), - value: json!({ - "enabled": true, - }), - merge_strategy: MergeStrategy::Replace, - file_path: None, - expected_version: None, - }) + ConfigEditsBuilder::new(&self.codex_home) + .with_edits([ConfigEdit::SetPath { + segments: vec![ + "plugins".to_string(), + result.plugin_id.as_key(), + "enabled".to_string(), + ], + value: value(true), + }]) + .apply() .await - .map(|_| ()) .map_err(PluginInstallError::from)?; let analytics_events_client = match self.analytics_events_client.read() { @@ -1538,7 +1532,7 @@ pub enum PluginInstallError { Store(#[from] PluginStoreError), #[error("{0}")] - Config(#[from] ConfigServiceError), + Config(#[from] anyhow::Error), #[error("failed to join plugin install task: {0}")] Join(#[from] tokio::task::JoinError), From 2a226096f60450bb9978d02e030ce29283f00c55 Mon Sep 17 00:00:00 2001 From: pakrym-oai Date: Tue, 21 Apr 2026 10:22:36 -0700 Subject: [PATCH 09/22] Split DeveloperInstructions into individual fragments. (#18813) Split DeveloperInstructions into individual fragments. --- .codex/skills/code-review-context/SKILL.md | 1 + codex-rs/core-skills/src/lib.rs | 4 +- codex-rs/core-skills/src/render.rs | 63 +- codex-rs/core/src/apps/mod.rs | 3 +- codex-rs/core/src/apps/render.rs | 17 +- .../context/approved_command_prefix_saved.rs | 24 + .../core/src/context/apps_instructions.rs | 30 + .../context/available_plugins_instructions.rs | 58 ++ .../context/available_skills_instructions.rs | 56 ++ .../collaboration_mode_instructions.rs | 32 + .../core/src/context/environment_context.rs | 2 +- codex-rs/core/src/context/fragment.rs | 30 +- .../guardian_followup_review_reminder.rs | 21 + .../src/context/hook_additional_context.rs | 22 + .../context/image_generation_instructions.rs | 30 + codex-rs/core/src/context/mod.rs | 38 +- .../src/context/model_switch_instructions.rs | 27 + .../core/src/context/network_rule_saved.rs | 35 + .../src/context/permissions_instructions.rs | 316 +++++++ .../context/permissions_instructions_tests.rs | 412 +++++++++ .../context/personality_spec_instructions.rs | 25 + .../core/src/context/plugin_instructions.rs | 22 + .../permissions/approval_policy/never.md | 0 .../permissions/approval_policy/on_failure.md | 0 .../permissions/approval_policy/on_request.md | 0 .../on_request_rule_request_permission.md | 0 .../approval_policy/unless_trusted.md | 0 .../sandbox_mode/danger_full_access.md | 0 .../permissions/sandbox_mode/read_only.md | 0 .../sandbox_mode/workspace_write.md | 0 .../context}/prompts/realtime/realtime_end.md | 0 .../prompts/realtime/realtime_start.md | 0 .../src/context/realtime_end_instructions.rs | 32 + .../context/realtime_start_instructions.rs | 18 + .../realtime_start_with_instructions.rs | 26 + .../core/src/context/skill_instructions.rs | 2 +- .../src/context/spawn_agent_instructions.rs | 14 + .../core/src/context/subagent_notification.rs | 2 +- codex-rs/core/src/context/turn_aborted.rs | 2 +- .../core/src/context/user_instructions.rs | 2 +- .../core/src/context/user_shell_command.rs | 2 +- codex-rs/core/src/context_manager/updates.rs | 63 +- codex-rs/core/src/guardian/review_session.rs | 14 +- codex-rs/core/src/hook_runtime.rs | 6 +- codex-rs/core/src/lib.rs | 4 +- codex-rs/core/src/plugins/injection.rs | 7 +- codex-rs/core/src/plugins/mod.rs | 1 - codex-rs/core/src/plugins/render.rs | 42 +- codex-rs/core/src/session/mod.rs | 69 +- codex-rs/core/src/session/tests.rs | 19 +- codex-rs/core/src/skills.rs | 2 +- codex-rs/core/src/stream_events_utils.rs | 14 +- .../tools/handlers/multi_agents_v2/spawn.rs | 22 +- .../core/tests/suite/permissions_messages.rs | 15 +- codex-rs/protocol/src/models.rs | 822 ------------------ 55 files changed, 1410 insertions(+), 1058 deletions(-) create mode 100644 codex-rs/core/src/context/approved_command_prefix_saved.rs create mode 100644 codex-rs/core/src/context/apps_instructions.rs create mode 100644 codex-rs/core/src/context/available_plugins_instructions.rs create mode 100644 codex-rs/core/src/context/available_skills_instructions.rs create mode 100644 codex-rs/core/src/context/collaboration_mode_instructions.rs create mode 100644 codex-rs/core/src/context/guardian_followup_review_reminder.rs create mode 100644 codex-rs/core/src/context/hook_additional_context.rs create mode 100644 codex-rs/core/src/context/image_generation_instructions.rs create mode 100644 codex-rs/core/src/context/model_switch_instructions.rs create mode 100644 codex-rs/core/src/context/network_rule_saved.rs create mode 100644 codex-rs/core/src/context/permissions_instructions.rs create mode 100644 codex-rs/core/src/context/permissions_instructions_tests.rs create mode 100644 codex-rs/core/src/context/personality_spec_instructions.rs create mode 100644 codex-rs/core/src/context/plugin_instructions.rs rename codex-rs/{protocol/src => core/src/context}/prompts/permissions/approval_policy/never.md (100%) rename codex-rs/{protocol/src => core/src/context}/prompts/permissions/approval_policy/on_failure.md (100%) rename codex-rs/{protocol/src => core/src/context}/prompts/permissions/approval_policy/on_request.md (100%) rename codex-rs/{protocol/src => core/src/context}/prompts/permissions/approval_policy/on_request_rule_request_permission.md (100%) rename codex-rs/{protocol/src => core/src/context}/prompts/permissions/approval_policy/unless_trusted.md (100%) rename codex-rs/{protocol/src => core/src/context}/prompts/permissions/sandbox_mode/danger_full_access.md (100%) rename codex-rs/{protocol/src => core/src/context}/prompts/permissions/sandbox_mode/read_only.md (100%) rename codex-rs/{protocol/src => core/src/context}/prompts/permissions/sandbox_mode/workspace_write.md (100%) rename codex-rs/{protocol/src => core/src/context}/prompts/realtime/realtime_end.md (100%) rename codex-rs/{protocol/src => core/src/context}/prompts/realtime/realtime_start.md (100%) create mode 100644 codex-rs/core/src/context/realtime_end_instructions.rs create mode 100644 codex-rs/core/src/context/realtime_start_instructions.rs create mode 100644 codex-rs/core/src/context/realtime_start_with_instructions.rs create mode 100644 codex-rs/core/src/context/spawn_agent_instructions.rs diff --git a/.codex/skills/code-review-context/SKILL.md b/.codex/skills/code-review-context/SKILL.md index 3d33192ec5..7faf3d7cd2 100644 --- a/.codex/skills/code-review-context/SKILL.md +++ b/.codex/skills/code-review-context/SKILL.md @@ -10,3 +10,4 @@ Codex maintains a context (history of messages) that is sent to the model in inf 3. No unbounded items - everything injected in the model context must have a bounded size and a hard cap. 4. No items larger than 10K tokens. 5. Highlight new individual items that can cross >1k tokens as P0. These need an additional manual review. +6. All injected fragments must be defined as structs in `core/context` and implement ContextualUserFragment trait \ No newline at end of file diff --git a/codex-rs/core-skills/src/lib.rs b/codex-rs/core-skills/src/lib.rs index 6d3ac7a4fe..06ced0d5d4 100644 --- a/codex-rs/core-skills/src/lib.rs +++ b/codex-rs/core-skills/src/lib.rs @@ -22,8 +22,8 @@ pub use model::SkillLoadOutcome; pub use model::SkillMetadata; pub use model::SkillPolicy; pub use model::filter_skill_load_outcome_for_product; -pub use render::RenderedSkillsSection; +pub use render::AvailableSkills; pub use render::SkillMetadataBudget; pub use render::SkillRenderReport; +pub use render::build_available_skills; pub use render::default_skill_metadata_budget; -pub use render::render_skills_section; diff --git a/codex-rs/core-skills/src/render.rs b/codex-rs/core-skills/src/render.rs index ad674fbffa..6bcd5e4e05 100644 --- a/codex-rs/core-skills/src/render.rs +++ b/codex-rs/core-skills/src/render.rs @@ -3,8 +3,6 @@ use codex_otel::SessionTelemetry; use codex_otel::THREAD_SKILLS_ENABLED_TOTAL_METRIC; use codex_otel::THREAD_SKILLS_KEPT_TOTAL_METRIC; use codex_otel::THREAD_SKILLS_TRUNCATED_METRIC; -use codex_protocol::protocol::SKILLS_INSTRUCTIONS_CLOSE_TAG; -use codex_protocol::protocol::SKILLS_INSTRUCTIONS_OPEN_TAG; use codex_protocol::protocol::SkillScope; use codex_utils_output_truncation::approx_token_count; @@ -48,8 +46,8 @@ pub enum SkillRenderSideEffects<'a> { } #[derive(Debug, Clone, PartialEq, Eq)] -pub struct RenderedSkillsSection { - pub text: String, +pub struct AvailableSkills { + pub skill_lines: Vec, pub report: SkillRenderReport, pub emit_warning: bool, } @@ -71,11 +69,11 @@ pub fn default_skill_metadata_budget(context_window: Option) -> SkillMetada )) } -pub fn render_skills_section( +pub fn build_available_skills( skills: &[SkillMetadata], budget: SkillMetadataBudget, side_effects: SkillRenderSideEffects<'_>, -) -> Option { +) -> Option { if skills.is_empty() { let _ = record_skill_render_side_effects( side_effects, @@ -93,39 +91,8 @@ pub fn render_skills_section( report.included_count, report.omitted_count > 0, ); - let mut lines: Vec = Vec::new(); - lines.push("## Skills".to_string()); - lines.push("A skill is a set of local instructions to follow that is stored in a `SKILL.md` file. Below is the list of skills that can be used. Each entry includes a name, description, and file path so you can open the source for full instructions when using a specific skill.".to_string()); - lines.push("### Available skills".to_string()); - if !skill_lines.is_empty() { - lines.extend(skill_lines); - } - - lines.push("### How to use skills".to_string()); - lines.push( - r###"- Discovery: The list above is the skills available in this session (name + description + file path). Skill bodies live on disk at the listed paths. -- Trigger rules: If the user names a skill (with `$SkillName` or plain text) OR the task clearly matches a skill's description shown above, you must use that skill for that turn. Multiple mentions mean use them all. Do not carry skills across turns unless re-mentioned. -- Missing/blocked: If a named skill isn't in the list or the path can't be read, say so briefly and continue with the best fallback. -- How to use a skill (progressive disclosure): - 1) After deciding to use a skill, open its `SKILL.md`. Read only enough to follow the workflow. - 2) When `SKILL.md` references relative paths (e.g., `scripts/foo.py`), resolve them relative to the skill directory listed above first, and only consider other paths if needed. - 3) If `SKILL.md` points to extra folders such as `references/`, load only the specific files needed for the request; don't bulk-load everything. - 4) If `scripts/` exist, prefer running or patching them instead of retyping large code blocks. - 5) If `assets/` or templates exist, reuse them instead of recreating from scratch. -- Coordination and sequencing: - - If multiple skills apply, choose the minimal set that covers the request and state the order you'll use them. - - Announce which skill(s) you're using and why (one short line). If you skip an obvious skill, say why. -- Context hygiene: - - Keep context small: summarize long sections instead of pasting them; only load extra files when needed. - - Avoid deep reference-chasing: prefer opening only files directly linked from `SKILL.md` unless you're blocked. - - When variants exist (frameworks, providers, domains), pick only the relevant reference file(s) and note that choice. -- Safety and fallback: If a skill can't be applied cleanly (missing files, unclear instructions), state the issue, pick the next-best approach, and continue."### - .to_string(), - ); - - let body = lines.join("\n"); - Some(RenderedSkillsSection { - text: format!("{SKILLS_INSTRUCTIONS_OPEN_TAG}\n{body}\n{SKILLS_INSTRUCTIONS_CLOSE_TAG}"), + Some(AvailableSkills { + skill_lines, report, emit_warning, }) @@ -274,7 +241,7 @@ mod tests { .cost(&format!("{}\n", render_skill_line(&admin))); let budget = SkillMetadataBudget::Characters(system_cost + admin_cost); - let rendered = render_skills_section( + let rendered = build_available_skills( &[system, user, repo, admin], budget, SkillRenderSideEffects::None, @@ -284,10 +251,11 @@ mod tests { assert_eq!(rendered.report.included_count, 2); assert_eq!(rendered.report.omitted_count, 2); assert!(!rendered.emit_warning); - assert!(rendered.text.contains("- system-skill:")); - assert!(rendered.text.contains("- admin-skill:")); - assert!(!rendered.text.contains("- repo-skill:")); - assert!(!rendered.text.contains("- user-skill:")); + let rendered_text = rendered.skill_lines.join("\n"); + assert!(rendered_text.contains("- system-skill:")); + assert!(rendered_text.contains("- admin-skill:")); + assert!(!rendered_text.contains("- repo-skill:")); + assert!(!rendered_text.contains("- user-skill:")); } #[test] @@ -300,13 +268,14 @@ mod tests { let budget = SkillMetadataBudget::Characters(repo_cost); let rendered = - render_skills_section(&[oversized, repo], budget, SkillRenderSideEffects::None) + build_available_skills(&[oversized, repo], budget, SkillRenderSideEffects::None) .expect("skills render"); assert_eq!(rendered.report.included_count, 1); assert_eq!(rendered.report.omitted_count, 1); assert!(!rendered.emit_warning); - assert!(!rendered.text.contains("- oversized-system-skill:")); - assert!(rendered.text.contains("- repo-skill:")); + let rendered_text = rendered.skill_lines.join("\n"); + assert!(!rendered_text.contains("- oversized-system-skill:")); + assert!(rendered_text.contains("- repo-skill:")); } } diff --git a/codex-rs/core/src/apps/mod.rs b/codex-rs/core/src/apps/mod.rs index dcb5c4c20a..5a58d22204 100644 --- a/codex-rs/core/src/apps/mod.rs +++ b/codex-rs/core/src/apps/mod.rs @@ -1,3 +1,2 @@ +#[cfg(test)] mod render; - -pub(crate) use render::render_apps_section; diff --git a/codex-rs/core/src/apps/render.rs b/codex-rs/core/src/apps/render.rs index fe23a09f8d..3793231e10 100644 --- a/codex-rs/core/src/apps/render.rs +++ b/codex-rs/core/src/apps/render.rs @@ -1,22 +1,11 @@ +use crate::context::AppsInstructions; +use crate::context::ContextualUserFragment; use codex_app_server_protocol::AppInfo; -use codex_mcp::CODEX_APPS_MCP_SERVER_NAME; use codex_protocol::protocol::APPS_INSTRUCTIONS_CLOSE_TAG; use codex_protocol::protocol::APPS_INSTRUCTIONS_OPEN_TAG; pub(crate) fn render_apps_section(connectors: &[AppInfo]) -> Option { - if !connectors - .iter() - .any(|connector| connector.is_accessible && connector.is_enabled) - { - return None; - } - - let body = format!( - "## Apps (Connectors)\nApps (Connectors) can be explicitly triggered in user messages in the format `[$app-name](app://{{connector_id}})`. Apps can also be implicitly triggered as long as the context suggests usage of available apps.\nAn app is equivalent to a set of MCP tools within the `{CODEX_APPS_MCP_SERVER_NAME}` MCP.\nAn installed app's MCP tools are either provided to you already, or can be lazy-loaded through the `tool_search` tool. If `tool_search` is available, the apps that are searchable by `tools_search` will be listed by it.\nDo not additionally call list_mcp_resources or list_mcp_resource_templates for apps." - ); - Some(format!( - "{APPS_INSTRUCTIONS_OPEN_TAG}\n{body}\n{APPS_INSTRUCTIONS_CLOSE_TAG}" - )) + AppsInstructions::from_connectors(connectors).map(|instructions| instructions.render()) } #[cfg(test)] diff --git a/codex-rs/core/src/context/approved_command_prefix_saved.rs b/codex-rs/core/src/context/approved_command_prefix_saved.rs new file mode 100644 index 0000000000..3aac17712a --- /dev/null +++ b/codex-rs/core/src/context/approved_command_prefix_saved.rs @@ -0,0 +1,24 @@ +use super::ContextualUserFragment; + +#[derive(Debug, Clone, PartialEq)] +pub(crate) struct ApprovedCommandPrefixSaved { + prefixes: String, +} + +impl ApprovedCommandPrefixSaved { + pub(crate) fn new(prefixes: impl Into) -> Self { + Self { + prefixes: prefixes.into(), + } + } +} + +impl ContextualUserFragment for ApprovedCommandPrefixSaved { + const ROLE: &'static str = "developer"; + const START_MARKER: &'static str = ""; + const END_MARKER: &'static str = ""; + + fn body(&self) -> String { + format!("Approved command prefix saved:\n{}", self.prefixes) + } +} diff --git a/codex-rs/core/src/context/apps_instructions.rs b/codex-rs/core/src/context/apps_instructions.rs new file mode 100644 index 0000000000..55865b429a --- /dev/null +++ b/codex-rs/core/src/context/apps_instructions.rs @@ -0,0 +1,30 @@ +use codex_app_server_protocol::AppInfo; +use codex_mcp::CODEX_APPS_MCP_SERVER_NAME; +use codex_protocol::protocol::APPS_INSTRUCTIONS_CLOSE_TAG; +use codex_protocol::protocol::APPS_INSTRUCTIONS_OPEN_TAG; + +use super::ContextualUserFragment; + +#[derive(Debug, Clone, PartialEq)] +pub(crate) struct AppsInstructions; + +impl AppsInstructions { + pub(crate) fn from_connectors(connectors: &[AppInfo]) -> Option { + connectors + .iter() + .any(|connector| connector.is_accessible && connector.is_enabled) + .then_some(Self) + } +} + +impl ContextualUserFragment for AppsInstructions { + const ROLE: &'static str = "developer"; + const START_MARKER: &'static str = APPS_INSTRUCTIONS_OPEN_TAG; + const END_MARKER: &'static str = APPS_INSTRUCTIONS_CLOSE_TAG; + + fn body(&self) -> String { + format!( + "\n## Apps (Connectors)\nApps (Connectors) can be explicitly triggered in user messages in the format `[$app-name](app://{{connector_id}})`. Apps can also be implicitly triggered as long as the context suggests usage of available apps.\nAn app is equivalent to a set of MCP tools within the `{CODEX_APPS_MCP_SERVER_NAME}` MCP.\nAn installed app's MCP tools are either provided to you already, or can be lazy-loaded through the `tool_search` tool. If `tool_search` is available, the apps that are searchable by `tools_search` will be listed by it.\nDo not additionally call list_mcp_resources or list_mcp_resource_templates for apps.\n" + ) + } +} diff --git a/codex-rs/core/src/context/available_plugins_instructions.rs b/codex-rs/core/src/context/available_plugins_instructions.rs new file mode 100644 index 0000000000..52f9c8df48 --- /dev/null +++ b/codex-rs/core/src/context/available_plugins_instructions.rs @@ -0,0 +1,58 @@ +use codex_plugin::PluginCapabilitySummary; +use codex_protocol::protocol::PLUGINS_INSTRUCTIONS_CLOSE_TAG; +use codex_protocol::protocol::PLUGINS_INSTRUCTIONS_OPEN_TAG; + +use super::ContextualUserFragment; + +#[derive(Debug, Clone, PartialEq)] +pub(crate) struct AvailablePluginsInstructions { + plugins: Vec, +} + +impl AvailablePluginsInstructions { + pub(crate) fn from_plugins(plugins: &[PluginCapabilitySummary]) -> Option { + if plugins.is_empty() { + return None; + } + + Some(Self { + plugins: plugins.to_vec(), + }) + } +} + +impl ContextualUserFragment for AvailablePluginsInstructions { + const ROLE: &'static str = "developer"; + const START_MARKER: &'static str = PLUGINS_INSTRUCTIONS_OPEN_TAG; + const END_MARKER: &'static str = PLUGINS_INSTRUCTIONS_CLOSE_TAG; + + fn body(&self) -> String { + let mut lines = vec![ + "## Plugins".to_string(), + "A plugin is a local bundle of skills, MCP servers, and apps. Below is the list of plugins that are enabled and available in this session.".to_string(), + "### Available plugins".to_string(), + ]; + + lines.extend( + self.plugins + .iter() + .map(|plugin| match plugin.description.as_deref() { + Some(description) => format!("- `{}`: {description}", plugin.display_name), + None => format!("- `{}`", plugin.display_name), + }), + ); + + lines.push("### How to use plugins".to_string()); + lines.push( + r###"- Discovery: The list above is the plugins available in this session. +- Skill naming: If a plugin contributes skills, those skill entries are prefixed with `plugin_name:` in the Skills list. +- Trigger rules: If the user explicitly names a plugin, prefer capabilities associated with that plugin for that turn. +- Relationship to capabilities: Plugins are not invoked directly. Use their underlying skills, MCP tools, and app tools to help solve the task. +- Preference: When a relevant plugin is available, prefer using capabilities associated with that plugin over standalone capabilities that provide similar functionality. +- Missing/blocked: If the user requests a plugin that is not listed above, or the plugin does not have relevant callable capabilities for the task, say so briefly and continue with the best fallback."### + .to_string(), + ); + + format!("\n{}\n", lines.join("\n")) + } +} diff --git a/codex-rs/core/src/context/available_skills_instructions.rs b/codex-rs/core/src/context/available_skills_instructions.rs new file mode 100644 index 0000000000..aba4b20135 --- /dev/null +++ b/codex-rs/core/src/context/available_skills_instructions.rs @@ -0,0 +1,56 @@ +use codex_core_skills::AvailableSkills; +use codex_protocol::protocol::SKILLS_INSTRUCTIONS_CLOSE_TAG; +use codex_protocol::protocol::SKILLS_INSTRUCTIONS_OPEN_TAG; + +use super::ContextualUserFragment; + +#[derive(Debug, Clone, PartialEq, Eq)] +pub(crate) struct AvailableSkillsInstructions { + skill_lines: Vec, +} + +impl From for AvailableSkillsInstructions { + fn from(available_skills: AvailableSkills) -> Self { + Self { + skill_lines: available_skills.skill_lines, + } + } +} + +impl ContextualUserFragment for AvailableSkillsInstructions { + const ROLE: &'static str = "developer"; + const START_MARKER: &'static str = SKILLS_INSTRUCTIONS_OPEN_TAG; + const END_MARKER: &'static str = SKILLS_INSTRUCTIONS_CLOSE_TAG; + + fn body(&self) -> String { + let mut lines: Vec = Vec::new(); + lines.push("## Skills".to_string()); + lines.push("A skill is a set of local instructions to follow that is stored in a `SKILL.md` file. Below is the list of skills that can be used. Each entry includes a name, description, and file path so you can open the source for full instructions when using a specific skill.".to_string()); + lines.push("### Available skills".to_string()); + lines.extend(self.skill_lines.iter().cloned()); + + lines.push("### How to use skills".to_string()); + lines.push( + r###"- Discovery: The list above is the skills available in this session (name + description + file path). Skill bodies live on disk at the listed paths. +- Trigger rules: If the user names a skill (with `$SkillName` or plain text) OR the task clearly matches a skill's description shown above, you must use that skill for that turn. Multiple mentions mean use them all. Do not carry skills across turns unless re-mentioned. +- Missing/blocked: If a named skill isn't in the list or the path can't be read, say so briefly and continue with the best fallback. +- How to use a skill (progressive disclosure): + 1) After deciding to use a skill, open its `SKILL.md`. Read only enough to follow the workflow. + 2) When `SKILL.md` references relative paths (e.g., `scripts/foo.py`), resolve them relative to the skill directory listed above first, and only consider other paths if needed. + 3) If `SKILL.md` points to extra folders such as `references/`, load only the specific files needed for the request; don't bulk-load everything. + 4) If `scripts/` exist, prefer running or patching them instead of retyping large code blocks. + 5) If `assets/` or templates exist, reuse them instead of recreating from scratch. +- Coordination and sequencing: + - If multiple skills apply, choose the minimal set that covers the request and state the order you'll use them. + - Announce which skill(s) you're using and why (one short line). If you skip an obvious skill, say why. +- Context hygiene: + - Keep context small: summarize long sections instead of pasting them; only load extra files when needed. + - Avoid deep reference-chasing: prefer opening only files directly linked from `SKILL.md` unless you're blocked. + - When variants exist (frameworks, providers, domains), pick only the relevant reference file(s) and note that choice. +- Safety and fallback: If a skill can't be applied cleanly (missing files, unclear instructions), state the issue, pick the next-best approach, and continue."### + .to_string(), + ); + + format!("\n{}\n", lines.join("\n")) + } +} diff --git a/codex-rs/core/src/context/collaboration_mode_instructions.rs b/codex-rs/core/src/context/collaboration_mode_instructions.rs new file mode 100644 index 0000000000..dcf3bac1de --- /dev/null +++ b/codex-rs/core/src/context/collaboration_mode_instructions.rs @@ -0,0 +1,32 @@ +use super::ContextualUserFragment; +use codex_protocol::config_types::CollaborationMode; +use codex_protocol::protocol::COLLABORATION_MODE_CLOSE_TAG; +use codex_protocol::protocol::COLLABORATION_MODE_OPEN_TAG; + +#[derive(Debug, Clone, PartialEq)] +pub(crate) struct CollaborationModeInstructions { + instructions: String, +} + +impl CollaborationModeInstructions { + pub(crate) fn from_collaboration_mode(collaboration_mode: &CollaborationMode) -> Option { + collaboration_mode + .settings + .developer_instructions + .as_ref() + .filter(|instructions| !instructions.is_empty()) + .map(|instructions| Self { + instructions: instructions.clone(), + }) + } +} + +impl ContextualUserFragment for CollaborationModeInstructions { + const ROLE: &'static str = "developer"; + const START_MARKER: &'static str = COLLABORATION_MODE_OPEN_TAG; + const END_MARKER: &'static str = COLLABORATION_MODE_CLOSE_TAG; + + fn body(&self) -> String { + self.instructions.clone() + } +} diff --git a/codex-rs/core/src/context/environment_context.rs b/codex-rs/core/src/context/environment_context.rs index c676bfb9d9..c4e77624f8 100644 --- a/codex-rs/core/src/context/environment_context.rs +++ b/codex-rs/core/src/context/environment_context.rs @@ -200,7 +200,7 @@ impl ContextualUserFragment for EnvironmentContext { lines.extend(subagents.lines().map(|line| format!(" {line}"))); lines.push(" ".to_string()); } - format!("\n{}", lines.join("\n")) + format!("\n{}\n", lines.join("\n")) } } diff --git a/codex-rs/core/src/context/fragment.rs b/codex-rs/core/src/context/fragment.rs index e0e6d03303..34f4a7c367 100644 --- a/codex-rs/core/src/context/fragment.rs +++ b/codex-rs/core/src/context/fragment.rs @@ -28,13 +28,16 @@ impl FragmentRegistration for FragmentRegistrationPro } } -/// Context payload that is injected as a user-authored message fragment. +/// Context payload that is injected as a message fragment. /// -/// Implementations own the response role, start/end markers used to recognize -/// the fragment, and provide the fragment body appended directly after the -/// start marker. The default helpers wrap that body and convert it into the -/// response item shape expected by model input assembly. -pub(crate) trait ContextualUserFragment { +/// Implementations own the response role and provide the exact fragment body. +/// Marked fragments also provide start/end markers used to recognize injected +/// context later. `render()` concatenates markers and body without adding +/// separators, so implementations should include any whitespace they need +/// between tags in `body()`. Unmarked fragments should leave both markers empty, +/// in which case the default helpers render only the body and never match +/// arbitrary text. +pub trait ContextualUserFragment { const ROLE: &'static str; const START_MARKER: &'static str; const END_MARKER: &'static str; @@ -45,6 +48,10 @@ pub(crate) trait ContextualUserFragment { where Self: Sized, { + if Self::START_MARKER.is_empty() || Self::END_MARKER.is_empty() { + return false; + } + let trimmed = text.trim_start(); let starts_with_marker = trimmed .get(..Self::START_MARKER.len()) @@ -57,12 +64,11 @@ pub(crate) trait ContextualUserFragment { } fn render(&self) -> String { - format!( - "{}{}\n{}", - Self::START_MARKER, - self.body(), - Self::END_MARKER - ) + if Self::START_MARKER.is_empty() && Self::END_MARKER.is_empty() { + return self.body(); + } + + format!("{}{}{}", Self::START_MARKER, self.body(), Self::END_MARKER) } fn into(self) -> ResponseItem diff --git a/codex-rs/core/src/context/guardian_followup_review_reminder.rs b/codex-rs/core/src/context/guardian_followup_review_reminder.rs new file mode 100644 index 0000000000..972355ba95 --- /dev/null +++ b/codex-rs/core/src/context/guardian_followup_review_reminder.rs @@ -0,0 +1,21 @@ +use super::ContextualUserFragment; + +#[derive(Debug, Clone, Copy, PartialEq, Eq)] +pub(crate) struct GuardianFollowupReviewReminder; + +impl ContextualUserFragment for GuardianFollowupReviewReminder { + const ROLE: &'static str = "developer"; + const START_MARKER: &'static str = ""; + const END_MARKER: &'static str = ""; + + fn body(&self) -> String { + concat!( + "Use prior reviews as context, not binding precedent. ", + "Follow the Workspace Policy. ", + "If the user explicitly approves a previously rejected action after being informed of the ", + "concrete risks, set outcome to \"allow\" unless the policy explicitly disallows user ", + "overwrites in such cases." + ) + .to_string() + } +} diff --git a/codex-rs/core/src/context/hook_additional_context.rs b/codex-rs/core/src/context/hook_additional_context.rs new file mode 100644 index 0000000000..01c95a6524 --- /dev/null +++ b/codex-rs/core/src/context/hook_additional_context.rs @@ -0,0 +1,22 @@ +use super::ContextualUserFragment; + +#[derive(Debug, Clone, PartialEq)] +pub(crate) struct HookAdditionalContext { + text: String, +} + +impl HookAdditionalContext { + pub(crate) fn new(text: impl Into) -> Self { + Self { text: text.into() } + } +} + +impl ContextualUserFragment for HookAdditionalContext { + const ROLE: &'static str = "developer"; + const START_MARKER: &'static str = ""; + const END_MARKER: &'static str = ""; + + fn body(&self) -> String { + self.text.clone() + } +} diff --git a/codex-rs/core/src/context/image_generation_instructions.rs b/codex-rs/core/src/context/image_generation_instructions.rs new file mode 100644 index 0000000000..01abee9096 --- /dev/null +++ b/codex-rs/core/src/context/image_generation_instructions.rs @@ -0,0 +1,30 @@ +use super::ContextualUserFragment; +use std::fmt::Display; + +#[derive(Debug, Clone, PartialEq)] +pub(crate) struct ImageGenerationInstructions { + image_output_dir: String, + image_output_path: String, +} + +impl ImageGenerationInstructions { + pub(crate) fn new(image_output_dir: impl Display, image_output_path: impl Display) -> Self { + Self { + image_output_dir: image_output_dir.to_string(), + image_output_path: image_output_path.to_string(), + } + } +} + +impl ContextualUserFragment for ImageGenerationInstructions { + const ROLE: &'static str = "developer"; + const START_MARKER: &'static str = ""; + const END_MARKER: &'static str = ""; + + fn body(&self) -> String { + format!( + "Generated images are saved to {} as {} by default.\nIf you need to use a generated image at another path, copy it and leave the original in place unless the user explicitly asks you to delete it.", + self.image_output_dir, self.image_output_path + ) + } +} diff --git a/codex-rs/core/src/context/mod.rs b/codex-rs/core/src/context/mod.rs index 94c5849f9d..bbc5bcacf1 100644 --- a/codex-rs/core/src/context/mod.rs +++ b/codex-rs/core/src/context/mod.rs @@ -1,20 +1,56 @@ +//! Context fragments injected into model input. + +mod approved_command_prefix_saved; +mod apps_instructions; +mod available_plugins_instructions; +mod available_skills_instructions; +mod collaboration_mode_instructions; mod contextual_user_message; mod environment_context; mod fragment; +mod guardian_followup_review_reminder; +mod hook_additional_context; +mod image_generation_instructions; +mod model_switch_instructions; +mod network_rule_saved; +mod permissions_instructions; +mod personality_spec_instructions; +mod plugin_instructions; +mod realtime_end_instructions; +mod realtime_start_instructions; +mod realtime_start_with_instructions; mod skill_instructions; +mod spawn_agent_instructions; mod subagent_notification; mod turn_aborted; mod user_instructions; mod user_shell_command; +pub(crate) use approved_command_prefix_saved::ApprovedCommandPrefixSaved; +pub(crate) use apps_instructions::AppsInstructions; +pub(crate) use available_plugins_instructions::AvailablePluginsInstructions; +pub(crate) use available_skills_instructions::AvailableSkillsInstructions; +pub(crate) use collaboration_mode_instructions::CollaborationModeInstructions; pub(crate) use contextual_user_message::is_contextual_user_fragment; pub(crate) use contextual_user_message::is_memory_excluded_contextual_user_fragment; pub(crate) use contextual_user_message::parse_visible_hook_prompt_message; pub(crate) use environment_context::EnvironmentContext; -pub(crate) use fragment::ContextualUserFragment; +pub use fragment::ContextualUserFragment; pub(crate) use fragment::FragmentRegistration; pub(crate) use fragment::FragmentRegistrationProxy; +pub(crate) use guardian_followup_review_reminder::GuardianFollowupReviewReminder; +pub(crate) use hook_additional_context::HookAdditionalContext; +pub(crate) use image_generation_instructions::ImageGenerationInstructions; +pub(crate) use model_switch_instructions::ModelSwitchInstructions; +pub(crate) use network_rule_saved::NetworkRuleSaved; +pub use permissions_instructions::PermissionsInstructions; +pub(crate) use personality_spec_instructions::PersonalitySpecInstructions; +pub(crate) use plugin_instructions::PluginInstructions; +pub(crate) use realtime_end_instructions::RealtimeEndInstructions; +pub(crate) use realtime_start_instructions::RealtimeStartInstructions; +pub(crate) use realtime_start_with_instructions::RealtimeStartWithInstructions; pub(crate) use skill_instructions::SkillInstructions; +pub(crate) use spawn_agent_instructions::SpawnAgentInstructions; pub(crate) use subagent_notification::SubagentNotification; pub(crate) use turn_aborted::TurnAborted; pub(crate) use user_instructions::UserInstructions; diff --git a/codex-rs/core/src/context/model_switch_instructions.rs b/codex-rs/core/src/context/model_switch_instructions.rs new file mode 100644 index 0000000000..0b75afb61a --- /dev/null +++ b/codex-rs/core/src/context/model_switch_instructions.rs @@ -0,0 +1,27 @@ +use super::ContextualUserFragment; + +#[derive(Debug, Clone, PartialEq)] +pub(crate) struct ModelSwitchInstructions { + model_instructions: String, +} + +impl ModelSwitchInstructions { + pub(crate) fn new(model_instructions: impl Into) -> Self { + Self { + model_instructions: model_instructions.into(), + } + } +} + +impl ContextualUserFragment for ModelSwitchInstructions { + const ROLE: &'static str = "developer"; + const START_MARKER: &'static str = ""; + const END_MARKER: &'static str = ""; + + fn body(&self) -> String { + format!( + "\nThe user was previously using a different model. Please continue the conversation according to the following instructions:\n\n{}\n", + self.model_instructions + ) + } +} diff --git a/codex-rs/core/src/context/network_rule_saved.rs b/codex-rs/core/src/context/network_rule_saved.rs new file mode 100644 index 0000000000..8162e6bda2 --- /dev/null +++ b/codex-rs/core/src/context/network_rule_saved.rs @@ -0,0 +1,35 @@ +use super::ContextualUserFragment; +use codex_protocol::approvals::NetworkPolicyAmendment; +use codex_protocol::approvals::NetworkPolicyRuleAction; + +#[derive(Debug, Clone, PartialEq)] +pub(crate) struct NetworkRuleSaved { + action: NetworkPolicyRuleAction, + host: String, +} + +impl NetworkRuleSaved { + pub(crate) fn new(amendment: &NetworkPolicyAmendment) -> Self { + Self { + action: amendment.action, + host: amendment.host.clone(), + } + } +} + +impl ContextualUserFragment for NetworkRuleSaved { + const ROLE: &'static str = "developer"; + const START_MARKER: &'static str = ""; + const END_MARKER: &'static str = ""; + + fn body(&self) -> String { + let (action, list_name) = match self.action { + NetworkPolicyRuleAction::Allow => ("Allowed", "allowlist"), + NetworkPolicyRuleAction::Deny => ("Denied", "denylist"), + }; + format!( + "{action} network rule saved in execpolicy ({list_name}): {}", + self.host + ) + } +} diff --git a/codex-rs/core/src/context/permissions_instructions.rs b/codex-rs/core/src/context/permissions_instructions.rs new file mode 100644 index 0000000000..db420f7464 --- /dev/null +++ b/codex-rs/core/src/context/permissions_instructions.rs @@ -0,0 +1,316 @@ +use super::ContextualUserFragment; +use codex_execpolicy::Policy; +use codex_protocol::config_types::ApprovalsReviewer; +use codex_protocol::config_types::SandboxMode; +use codex_protocol::models::format_allow_prefixes; +use codex_protocol::protocol::AskForApproval; +use codex_protocol::protocol::GranularApprovalConfig; +use codex_protocol::protocol::NetworkAccess; +use codex_protocol::protocol::SandboxPolicy; +use codex_protocol::protocol::WritableRoot; +use codex_utils_template::Template; +use std::path::Path; +use std::sync::LazyLock; + +const APPROVAL_POLICY_NEVER: &str = include_str!("prompts/permissions/approval_policy/never.md"); +const APPROVAL_POLICY_UNLESS_TRUSTED: &str = + include_str!("prompts/permissions/approval_policy/unless_trusted.md"); +const APPROVAL_POLICY_ON_FAILURE: &str = + include_str!("prompts/permissions/approval_policy/on_failure.md"); +const APPROVAL_POLICY_ON_REQUEST_RULE: &str = + include_str!("prompts/permissions/approval_policy/on_request.md"); +const APPROVAL_POLICY_ON_REQUEST_RULE_REQUEST_PERMISSION: &str = + include_str!("prompts/permissions/approval_policy/on_request_rule_request_permission.md"); +const AUTO_REVIEW_APPROVAL_SUFFIX: &str = "`approvals_reviewer` is `auto_review`: Sandbox escalations with require_escalated will be reviewed for compliance with the policy. If a rejection happens, you should proceed only with a materially safer alternative, or inform the user of the risk and send a final message to ask for approval."; + +const SANDBOX_MODE_DANGER_FULL_ACCESS: &str = + include_str!("prompts/permissions/sandbox_mode/danger_full_access.md"); +const SANDBOX_MODE_WORKSPACE_WRITE: &str = + include_str!("prompts/permissions/sandbox_mode/workspace_write.md"); +const SANDBOX_MODE_READ_ONLY: &str = include_str!("prompts/permissions/sandbox_mode/read_only.md"); + +static SANDBOX_MODE_DANGER_FULL_ACCESS_TEMPLATE: LazyLock