diff --git a/codex-rs/Cargo.lock b/codex-rs/Cargo.lock index d1787fdfe3..1ac5afb566 100644 --- a/codex-rs/Cargo.lock +++ b/codex-rs/Cargo.lock @@ -2292,6 +2292,7 @@ dependencies = [ "libc", "multimap", "pretty_assertions", + "prost 0.14.3", "schemars 0.8.22", "serde", "serde_json", @@ -2300,8 +2301,12 @@ dependencies = [ "tempfile", "thiserror 2.0.18", "tokio", + "tokio-stream", "toml 0.9.11+spec-1.1.0", "toml_edit 0.24.0+spec-1.1.0", + "tonic", + "tonic-prost", + "tonic-prost-build", "tracing", "wildmatch", "winapi-util", @@ -3375,6 +3380,7 @@ dependencies = [ "tonic", "tonic-prost", "tonic-prost-build", + "tracing", "uuid", ] diff --git a/codex-rs/app-server-protocol/schema/json/ClientRequest.json b/codex-rs/app-server-protocol/schema/json/ClientRequest.json index 8f2324d814..d2808a6262 100644 --- a/codex-rs/app-server-protocol/schema/json/ClientRequest.json +++ b/codex-rs/app-server-protocol/schema/json/ClientRequest.json @@ -3312,6 +3312,10 @@ "ephemeral": { "type": "boolean" }, + "excludeTurns": { + "description": "When true, return only thread metadata and live fork state without populating `thread.turns`. This is useful when the client plans to call `thread/turns/list` immediately after forking.", + "type": "boolean" + }, "model": { "description": "Configuration overrides for the forked thread, if any.", "type": [ @@ -3715,6 +3719,10 @@ "null" ] }, + "excludeTurns": { + "description": "When true, return only thread metadata and live-resume state without populating `thread.turns`. This is useful when the client plans to call `thread/turns/list` immediately after resuming.", + "type": "boolean" + }, "model": { "description": "Configuration overrides for the resumed thread, if any.", "type": [ diff --git a/codex-rs/app-server-protocol/schema/json/CommandExecutionRequestApprovalParams.json b/codex-rs/app-server-protocol/schema/json/CommandExecutionRequestApprovalParams.json index 3968da2a39..78e75d7c46 100644 --- a/codex-rs/app-server-protocol/schema/json/CommandExecutionRequestApprovalParams.json +++ b/codex-rs/app-server-protocol/schema/json/CommandExecutionRequestApprovalParams.json @@ -25,6 +25,7 @@ ] }, "read": { + "description": "This will be removed in favor of `entries`.", "items": { "$ref": "#/definitions/AbsolutePathBuf" }, @@ -34,6 +35,7 @@ ] }, "write": { + "description": "This will be removed in favor of `entries`.", "items": { "$ref": "#/definitions/AbsolutePathBuf" }, diff --git a/codex-rs/app-server-protocol/schema/json/PermissionsRequestApprovalParams.json b/codex-rs/app-server-protocol/schema/json/PermissionsRequestApprovalParams.json index c2e26c5532..ef268908f9 100644 --- a/codex-rs/app-server-protocol/schema/json/PermissionsRequestApprovalParams.json +++ b/codex-rs/app-server-protocol/schema/json/PermissionsRequestApprovalParams.json @@ -25,6 +25,7 @@ ] }, "read": { + "description": "This will be removed in favor of `entries`.", "items": { "$ref": "#/definitions/AbsolutePathBuf" }, @@ -34,6 +35,7 @@ ] }, "write": { + "description": "This will be removed in favor of `entries`.", "items": { "$ref": "#/definitions/AbsolutePathBuf" }, diff --git a/codex-rs/app-server-protocol/schema/json/PermissionsRequestApprovalResponse.json b/codex-rs/app-server-protocol/schema/json/PermissionsRequestApprovalResponse.json index e066108e6f..f49165296a 100644 --- a/codex-rs/app-server-protocol/schema/json/PermissionsRequestApprovalResponse.json +++ b/codex-rs/app-server-protocol/schema/json/PermissionsRequestApprovalResponse.json @@ -25,6 +25,7 @@ ] }, "read": { + "description": "This will be removed in favor of `entries`.", "items": { "$ref": "#/definitions/AbsolutePathBuf" }, @@ -34,6 +35,7 @@ ] }, "write": { + "description": "This will be removed in favor of `entries`.", "items": { "$ref": "#/definitions/AbsolutePathBuf" }, diff --git a/codex-rs/app-server-protocol/schema/json/ServerNotification.json b/codex-rs/app-server-protocol/schema/json/ServerNotification.json index 38acf1c902..59b3f5b45a 100644 --- a/codex-rs/app-server-protocol/schema/json/ServerNotification.json +++ b/codex-rs/app-server-protocol/schema/json/ServerNotification.json @@ -84,6 +84,7 @@ ] }, "read": { + "description": "This will be removed in favor of `entries`.", "items": { "$ref": "#/definitions/AbsolutePathBuf" }, @@ -93,6 +94,7 @@ ] }, "write": { + "description": "This will be removed in favor of `entries`.", "items": { "$ref": "#/definitions/AbsolutePathBuf" }, diff --git a/codex-rs/app-server-protocol/schema/json/ServerRequest.json b/codex-rs/app-server-protocol/schema/json/ServerRequest.json index 016dbe3415..84bf524739 100644 --- a/codex-rs/app-server-protocol/schema/json/ServerRequest.json +++ b/codex-rs/app-server-protocol/schema/json/ServerRequest.json @@ -25,6 +25,7 @@ ] }, "read": { + "description": "This will be removed in favor of `entries`.", "items": { "$ref": "#/definitions/AbsolutePathBuf" }, @@ -34,6 +35,7 @@ ] }, "write": { + "description": "This will be removed in favor of `entries`.", "items": { "$ref": "#/definitions/AbsolutePathBuf" }, 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 516e33aa7a..c87a704c00 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 @@ -5298,6 +5298,7 @@ ] }, "read": { + "description": "This will be removed in favor of `entries`.", "items": { "$ref": "#/definitions/v2/AbsolutePathBuf" }, @@ -5307,6 +5308,7 @@ ] }, "write": { + "description": "This will be removed in favor of `entries`.", "items": { "$ref": "#/definitions/v2/AbsolutePathBuf" }, @@ -14354,6 +14356,10 @@ "ephemeral": { "type": "boolean" }, + "excludeTurns": { + "description": "When true, return only thread metadata and live fork state without populating `thread.turns`. This is useful when the client plans to call `thread/turns/list` immediately after forking.", + "type": "boolean" + }, "model": { "description": "Configuration overrides for the forked thread, if any.", "type": [ @@ -15783,6 +15789,10 @@ "null" ] }, + "excludeTurns": { + "description": "When true, return only thread metadata and live-resume state without populating `thread.turns`. This is useful when the client plans to call `thread/turns/list` immediately after resuming.", + "type": "boolean" + }, "model": { "description": "Configuration overrides for the resumed thread, if any.", "type": [ 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 67a4cb186f..9f8b3d586e 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 @@ -148,6 +148,7 @@ ] }, "read": { + "description": "This will be removed in favor of `entries`.", "items": { "$ref": "#/definitions/AbsolutePathBuf" }, @@ -157,6 +158,7 @@ ] }, "write": { + "description": "This will be removed in favor of `entries`.", "items": { "$ref": "#/definitions/AbsolutePathBuf" }, @@ -12241,6 +12243,10 @@ "ephemeral": { "type": "boolean" }, + "excludeTurns": { + "description": "When true, return only thread metadata and live fork state without populating `thread.turns`. This is useful when the client plans to call `thread/turns/list` immediately after forking.", + "type": "boolean" + }, "model": { "description": "Configuration overrides for the forked thread, if any.", "type": [ @@ -13670,6 +13676,10 @@ "null" ] }, + "excludeTurns": { + "description": "When true, return only thread metadata and live-resume state without populating `thread.turns`. This is useful when the client plans to call `thread/turns/list` immediately after resuming.", + "type": "boolean" + }, "model": { "description": "Configuration overrides for the resumed thread, if any.", "type": [ diff --git a/codex-rs/app-server-protocol/schema/json/v2/ItemGuardianApprovalReviewCompletedNotification.json b/codex-rs/app-server-protocol/schema/json/v2/ItemGuardianApprovalReviewCompletedNotification.json index 27716e59b0..e4a278330c 100644 --- a/codex-rs/app-server-protocol/schema/json/v2/ItemGuardianApprovalReviewCompletedNotification.json +++ b/codex-rs/app-server-protocol/schema/json/v2/ItemGuardianApprovalReviewCompletedNotification.json @@ -25,6 +25,7 @@ ] }, "read": { + "description": "This will be removed in favor of `entries`.", "items": { "$ref": "#/definitions/AbsolutePathBuf" }, @@ -34,6 +35,7 @@ ] }, "write": { + "description": "This will be removed in favor of `entries`.", "items": { "$ref": "#/definitions/AbsolutePathBuf" }, diff --git a/codex-rs/app-server-protocol/schema/json/v2/ItemGuardianApprovalReviewStartedNotification.json b/codex-rs/app-server-protocol/schema/json/v2/ItemGuardianApprovalReviewStartedNotification.json index c039f2191a..b4ad6af44b 100644 --- a/codex-rs/app-server-protocol/schema/json/v2/ItemGuardianApprovalReviewStartedNotification.json +++ b/codex-rs/app-server-protocol/schema/json/v2/ItemGuardianApprovalReviewStartedNotification.json @@ -25,6 +25,7 @@ ] }, "read": { + "description": "This will be removed in favor of `entries`.", "items": { "$ref": "#/definitions/AbsolutePathBuf" }, @@ -34,6 +35,7 @@ ] }, "write": { + "description": "This will be removed in favor of `entries`.", "items": { "$ref": "#/definitions/AbsolutePathBuf" }, diff --git a/codex-rs/app-server-protocol/schema/json/v2/ThreadForkParams.json b/codex-rs/app-server-protocol/schema/json/v2/ThreadForkParams.json index 16a41d0d26..a603aff1e8 100644 --- a/codex-rs/app-server-protocol/schema/json/v2/ThreadForkParams.json +++ b/codex-rs/app-server-protocol/schema/json/v2/ThreadForkParams.json @@ -400,6 +400,10 @@ "ephemeral": { "type": "boolean" }, + "excludeTurns": { + "description": "When true, return only thread metadata and live fork state without populating `thread.turns`. This is useful when the client plans to call `thread/turns/list` immediately after forking.", + "type": "boolean" + }, "model": { "description": "Configuration overrides for the forked thread, if any.", "type": [ diff --git a/codex-rs/app-server-protocol/schema/json/v2/ThreadResumeParams.json b/codex-rs/app-server-protocol/schema/json/v2/ThreadResumeParams.json index 7fb27c08e8..19ccad14c1 100644 --- a/codex-rs/app-server-protocol/schema/json/v2/ThreadResumeParams.json +++ b/codex-rs/app-server-protocol/schema/json/v2/ThreadResumeParams.json @@ -1313,6 +1313,10 @@ "null" ] }, + "excludeTurns": { + "description": "When true, return only thread metadata and live-resume state without populating `thread.turns`. This is useful when the client plans to call `thread/turns/list` immediately after resuming.", + "type": "boolean" + }, "model": { "description": "Configuration overrides for the resumed thread, if any.", "type": [ diff --git a/codex-rs/app-server-protocol/schema/typescript/v2/AdditionalFileSystemPermissions.ts b/codex-rs/app-server-protocol/schema/typescript/v2/AdditionalFileSystemPermissions.ts index 2be8f7f0e7..e29263b95f 100644 --- a/codex-rs/app-server-protocol/schema/typescript/v2/AdditionalFileSystemPermissions.ts +++ b/codex-rs/app-server-protocol/schema/typescript/v2/AdditionalFileSystemPermissions.ts @@ -4,4 +4,12 @@ import type { AbsolutePathBuf } from "../AbsolutePathBuf"; import type { FileSystemSandboxEntry } from "./FileSystemSandboxEntry"; -export type AdditionalFileSystemPermissions = { read: Array | null, write: Array | null, globScanMaxDepth?: number, entries?: Array, }; +export type AdditionalFileSystemPermissions = { +/** + * This will be removed in favor of `entries`. + */ +read: Array | null, +/** + * This will be removed in favor of `entries`. + */ +write: Array | null, globScanMaxDepth?: number, entries?: Array, }; diff --git a/codex-rs/app-server-protocol/schema/typescript/v2/ThreadForkParams.ts b/codex-rs/app-server-protocol/schema/typescript/v2/ThreadForkParams.ts index bd73b3c3c3..f5f3f1878c 100644 --- a/codex-rs/app-server-protocol/schema/typescript/v2/ThreadForkParams.ts +++ b/codex-rs/app-server-protocol/schema/typescript/v2/ThreadForkParams.ts @@ -33,6 +33,11 @@ approvalsReviewer?: ApprovalsReviewer | null, sandbox?: SandboxMode | null, /** * with `sandbox`. */ permissionProfile?: PermissionProfile | null, config?: { [key in string]?: JsonValue } | null, baseInstructions?: string | null, developerInstructions?: string | null, ephemeral?: boolean, /** + * When true, return only thread metadata and live fork state without + * populating `thread.turns`. This is useful when the client plans to call + * `thread/turns/list` immediately after forking. + */ +excludeTurns?: boolean, /** * If true, persist additional rollout EventMsg variants required to * reconstruct a richer thread history on subsequent resume/fork/read. */ diff --git a/codex-rs/app-server-protocol/schema/typescript/v2/ThreadResumeParams.ts b/codex-rs/app-server-protocol/schema/typescript/v2/ThreadResumeParams.ts index d245efe4a2..452126be46 100644 --- a/codex-rs/app-server-protocol/schema/typescript/v2/ThreadResumeParams.ts +++ b/codex-rs/app-server-protocol/schema/typescript/v2/ThreadResumeParams.ts @@ -42,6 +42,11 @@ approvalsReviewer?: ApprovalsReviewer | null, sandbox?: SandboxMode | null, /** * with `sandbox`. */ permissionProfile?: PermissionProfile | null, config?: { [key in string]?: JsonValue } | null, baseInstructions?: string | null, developerInstructions?: string | null, personality?: Personality | null, /** + * When true, return only thread metadata and live-resume state without + * populating `thread.turns`. This is useful when the client plans to call + * `thread/turns/list` immediately after resuming. + */ +excludeTurns?: boolean, /** * If true, persist additional rollout EventMsg variants required to * reconstruct a richer thread history on subsequent resume/fork/read. */ diff --git a/codex-rs/app-server-protocol/src/protocol/v2.rs b/codex-rs/app-server-protocol/src/protocol/v2.rs index 655ad2d0ad..4ec916fd9f 100644 --- a/codex-rs/app-server-protocol/src/protocol/v2.rs +++ b/codex-rs/app-server-protocol/src/protocol/v2.rs @@ -325,7 +325,7 @@ impl From for AskForApproval { pub enum ApprovalsReviewer { #[serde(rename = "user")] User, - #[serde(rename = "auto_review", alias = "guardian_subagent")] + #[serde(rename = "guardian_subagent", alias = "auto_review")] AutoReview, } @@ -1273,7 +1273,9 @@ impl From for NetworkApprovalContext { #[serde(rename_all = "camelCase")] #[ts(export_to = "v2/")] pub struct AdditionalFileSystemPermissions { + /// This will be removed in favor of `entries`. pub read: Option>, + /// This will be removed in favor of `entries`. pub write: Option>, #[serde(default, skip_serializing_if = "Option::is_none")] #[ts(optional)] @@ -1286,11 +1288,26 @@ pub struct AdditionalFileSystemPermissions { impl From for AdditionalFileSystemPermissions { fn from(value: CoreFileSystemPermissions) -> Self { if let Some((read, write)) = value.legacy_read_write_roots() { + let mut entries = Vec::with_capacity( + read.as_ref().map_or(0, Vec::len) + write.as_ref().map_or(0, Vec::len), + ); + if let Some(paths) = read.as_ref() { + entries.extend(paths.iter().map(|path| FileSystemSandboxEntry { + path: FileSystemPath::Path { path: path.clone() }, + access: FileSystemAccessMode::Read, + })); + } + if let Some(paths) = write.as_ref() { + entries.extend(paths.iter().map(|path| FileSystemSandboxEntry { + path: FileSystemPath::Path { path: path.clone() }, + access: FileSystemAccessMode::Write, + })); + } Self { read, write, glob_scan_max_depth: None, - entries: None, + entries: Some(entries), } } else { Self { @@ -3412,6 +3429,11 @@ pub struct ThreadResumeParams { pub developer_instructions: Option, #[ts(optional = nullable)] pub personality: Option, + /// When true, return only thread metadata and live-resume state without + /// populating `thread.turns`. This is useful when the client plans to call + /// `thread/turns/list` immediately after resuming. + #[serde(default, skip_serializing_if = "std::ops::Not::not")] + pub exclude_turns: bool, /// If true, persist additional rollout EventMsg variants required to /// reconstruct a richer thread history on subsequent resume/fork/read. #[experimental("thread/resume.persistFullHistory")] @@ -3504,6 +3526,11 @@ pub struct ThreadForkParams { pub developer_instructions: Option, #[serde(default, skip_serializing_if = "std::ops::Not::not")] pub ephemeral: bool, + /// When true, return only thread metadata and live fork state without + /// populating `thread.turns`. This is useful when the client plans to call + /// `thread/turns/list` immediately after forking. + #[serde(default, skip_serializing_if = "std::ops::Not::not")] + pub exclude_turns: bool, /// If true, persist additional rollout EventMsg variants required to /// reconstruct a richer thread history on subsequent resume/fork/read. #[experimental("thread/fork.persistFullHistory")] @@ -7483,7 +7510,7 @@ mod tests { ); assert_eq!( serde_json::to_string(&ApprovalsReviewer::AutoReview).expect("serialize reviewer"), - "\"auto_review\"" + "\"guardian_subagent\"" ); for value in ["user", "auto_review", "guardian_subagent"] { @@ -7764,6 +7791,45 @@ mod tests { ); } + #[test] + fn additional_file_system_permissions_populates_entries_for_legacy_roots() { + let read_only_path = absolute_path("read-only"); + let read_write_path = absolute_path("read-write"); + let core_permissions = CoreFileSystemPermissions::from_read_write_roots( + Some(vec![read_only_path.clone()]), + Some(vec![read_write_path.clone()]), + ); + + let permissions = AdditionalFileSystemPermissions::from(core_permissions.clone()); + + assert_eq!( + permissions, + AdditionalFileSystemPermissions { + read: Some(vec![read_only_path.clone()]), + write: Some(vec![read_write_path.clone()]), + glob_scan_max_depth: None, + entries: Some(vec![ + FileSystemSandboxEntry { + path: FileSystemPath::Path { + path: read_only_path, + }, + access: FileSystemAccessMode::Read, + }, + FileSystemSandboxEntry { + path: FileSystemPath::Path { + path: read_write_path, + }, + access: FileSystemAccessMode::Write, + }, + ]), + } + ); + assert_eq!( + CoreFileSystemPermissions::from(permissions), + core_permissions + ); + } + #[test] fn additional_file_system_permissions_rejects_zero_glob_scan_depth() { serde_json::from_value::(json!({ diff --git a/codex-rs/app-server/README.md b/codex-rs/app-server/README.md index 0ba0a30ae8..9a6f4c4045 100644 --- a/codex-rs/app-server/README.md +++ b/codex-rs/app-server/README.md @@ -138,7 +138,7 @@ Example with notification opt-out: - `thread/start` — create a new thread; emits `thread/started` (including the current `thread.status`) and auto-subscribes you to turn/item events for that thread. When the request includes a `cwd` and the resolved sandbox is `workspace-write` or full access, app-server also marks that project as trusted in the user `config.toml`. Pass `sessionStartSource: "clear"` when starting a replacement thread after clearing the current session so `SessionStart` hooks receive `source: "clear"` instead of the default `"startup"`. For permissions, prefer `permissionProfile`; the legacy `sandbox` shorthand is still accepted but cannot be combined with `permissionProfile`. - `thread/resume` — reopen an existing thread by id so subsequent `turn/start` calls append to it. Accepts the same permission override rules as `thread/start`. -- `thread/fork` — fork an existing thread into a new thread id by copying the stored history; if the source thread is currently mid-turn, the fork records the same interruption marker as `turn/interrupt` instead of inheriting an unmarked partial turn suffix. The returned `thread.forkedFromId` points at the source thread when known. Accepts `ephemeral: true` for an in-memory temporary fork, emits `thread/started` (including the current `thread.status`), and auto-subscribes you to turn/item events for the new thread. Accepts the same permission override rules as `thread/start`. +- `thread/fork` — fork an existing thread into a new thread id by copying the stored history; if the source thread is currently mid-turn, the fork records the same interruption marker as `turn/interrupt` instead of inheriting an unmarked partial turn suffix. The returned `thread.forkedFromId` points at the source thread when known. Accepts `ephemeral: true` for an in-memory temporary fork, emits `thread/started` (including the current `thread.status`), and auto-subscribes you to turn/item events for the new thread. Pass `excludeTurns: true` when the client plans to page fork history via `thread/turns/list` instead of receiving the full turn array immediately. Accepts the same permission override rules as `thread/start`. - `thread/list` — page through stored rollouts; supports cursor-based pagination and optional `modelProviders`, `sourceKinds`, `archived`, `cwd`, and `searchTerm` filters. Each returned `thread` includes `status` (`ThreadStatus`), defaulting to `notLoaded` when the thread is not currently loaded. - `thread/loaded/list` — list the thread ids currently loaded in memory. - `thread/read` — read a stored thread by id without resuming it; optionally include turns via `includeTurns`. The returned `thread` includes `status` (`ThreadStatus`), defaulting to `notLoaded` when the thread is not currently loaded. @@ -259,6 +259,8 @@ Valid `personality` values are `"friendly"`, `"pragmatic"`, and `"none"`. When ` To continue a stored session, call `thread/resume` with the `thread.id` you previously recorded. The response shape matches `thread/start`. When the stored session includes persisted token usage, the server emits `thread/tokenUsage/updated` immediately after the response so clients can render restored usage before the next turn starts. You can also pass the same configuration overrides supported by `thread/start`, including `approvalsReviewer`. +By default, `thread/resume` includes the reconstructed turn history in `thread.turns`. Pass `excludeTurns: true` to return only thread metadata and live resume state, then call `thread/turns/list` separately if you want to page the turn history over the network. In that mode the server also skips replaying restored `thread/tokenUsage/updated`, which avoids rebuilding turns just to attribute historical usage. + By default, resume uses the latest persisted `model` and `reasoningEffort` values associated with the thread. Supplying any of `model`, `modelProvider`, `config.model`, or `config.model_reasoning_effort` disables that persisted fallback and uses the explicit overrides plus normal config resolution instead. Example: @@ -269,6 +271,12 @@ Example: "personality": "friendly" } } { "id": 11, "result": { "thread": { "id": "thr_123", … } } } + +{ "method": "thread/resume", "id": 12, "params": { + "threadId": "thr_123", + "excludeTurns": true +} } +{ "id": 12, "result": { "thread": { "id": "thr_123", "turns": [], … } } } ``` To branch from a stored session, call `thread/fork` with the `thread.id`. This creates a new thread id and emits a `thread/started` notification for it. When the source history includes persisted token usage, the server also emits `thread/tokenUsage/updated` for the new thread immediately after the response. If the source thread is actively running, the fork snapshots it as if the current turn had been interrupted first. Pass `ephemeral: true` when the fork should stay in-memory only: @@ -279,6 +287,8 @@ To branch from a stored session, call `thread/fork` with the `thread.id`. This c { "method": "thread/started", "params": { "thread": { … } } } ``` +Like `thread/resume`, `thread/fork` also accepts `excludeTurns: true` to return only thread metadata in `thread.turns` and let the client page history with `thread/turns/list`. In that mode the server skips replaying restored `thread/tokenUsage/updated`, which keeps the fork path from rebuilding turns just to attribute historical usage. + Experimental API: `thread/start`, `thread/resume`, and `thread/fork` accept `persistExtendedHistory: true` to persist a richer subset of ThreadItems for non-lossy history when calling `thread/read`, `thread/resume`, and `thread/fork` later. This does not backfill events that were not persisted previously. ### Example: List threads (with pagination & filters) diff --git a/codex-rs/app-server/src/codex_message_processor.rs b/codex-rs/app-server/src/codex_message_processor.rs index e1ee055757..967329b0be 100644 --- a/codex-rs/app-server/src/codex_message_processor.rs +++ b/codex-rs/app-server/src/codex_message_processor.rs @@ -4465,8 +4465,10 @@ impl CodexMessageProcessor { base_instructions, developer_instructions, personality, + exclude_turns, persist_extended_history, } = params; + let include_turns = !exclude_turns; let thread_history = if let Some(history) = history { let Some(thread_history) = self @@ -4522,7 +4524,6 @@ impl CodexMessageProcessor { } }; - let fallback_model_provider = config.model_provider_id.clone(); let instruction_sources = Self::instruction_sources_from_config(&config).await; let response_history = thread_history.clone(); @@ -4572,8 +4573,8 @@ impl CodexMessageProcessor { codex_thread.as_ref(), &response_history, rollout_path.as_path(), - fallback_model_provider.as_str(), persisted_resume_metadata.as_ref(), + include_turns, ) .await { @@ -4627,24 +4628,28 @@ impl CodexMessageProcessor { } let connection_id = request_id.connection_id; - let token_usage_thread = response.thread.clone(); - let token_usage_turn_id = latest_token_usage_turn_id_from_rollout_items( - &response_history.get_rollout_items(), - &token_usage_thread, - ); + let token_usage_thread = include_turns.then(|| response.thread.clone()); self.outgoing.send_response(request_id, response).await; - // The client needs restored usage before it starts another turn. - // Sending after the response preserves JSON-RPC request ordering while - // still filling the status line before the next turn lifecycle begins. - send_thread_token_usage_update_to_connection( - &self.outgoing, - connection_id, - thread_id, - &token_usage_thread, - codex_thread.as_ref(), - token_usage_turn_id, - ) - .await; + // `excludeTurns` is explicitly the cheap resume path, so avoid + // rebuilding history only to attribute a replayed usage update. + if let Some(token_usage_thread) = token_usage_thread { + let token_usage_turn_id = latest_token_usage_turn_id_from_rollout_items( + &response_history.get_rollout_items(), + token_usage_thread.turns.as_slice(), + ); + // The client needs restored usage before it starts another turn. + // Sending after the response preserves JSON-RPC request ordering while + // still filling the status line before the next turn lifecycle begins. + send_thread_token_usage_update_to_connection( + &self.outgoing, + connection_id, + thread_id, + &token_usage_thread, + codex_thread.as_ref(), + token_usage_turn_id, + ) + .await; + } } Err(err) => { let error = JSONRPCErrorError { @@ -4835,6 +4840,7 @@ impl CodexMessageProcessor { config_snapshot, instruction_sources, thread_summary, + include_turns: !params.exclude_turns, }), ); if listener_command_tx.send(command).is_err() { @@ -4938,22 +4944,22 @@ impl CodexMessageProcessor { thread: &CodexThread, thread_history: &InitialHistory, rollout_path: &Path, - fallback_provider: &str, persisted_resume_metadata: Option<&ThreadMetadata>, + include_turns: bool, ) -> std::result::Result { + let config_snapshot = thread.config_snapshot().await; let thread = match thread_history { InitialHistory::Resumed(resumed) => { load_thread_summary_for_rollout( &self.config, resumed.conversation_id, resumed.rollout_path.as_path(), - fallback_provider, + config_snapshot.model_provider_id.as_str(), persisted_resume_metadata, ) .await } InitialHistory::Forked(items) => { - let config_snapshot = thread.config_snapshot().await; let mut thread = build_thread_from_snapshot( thread_id, &config_snapshot, @@ -4969,13 +4975,15 @@ impl CodexMessageProcessor { let mut thread = thread?; thread.id = thread_id.to_string(); thread.path = Some(rollout_path.to_path_buf()); - let history_items = thread_history.get_rollout_items(); - populate_thread_turns( - &mut thread, - ThreadTurnSource::HistoryItems(&history_items), - /*active_turn*/ None, - ) - .await?; + if include_turns { + let history_items = thread_history.get_rollout_items(); + populate_thread_turns( + &mut thread, + ThreadTurnSource::HistoryItems(&history_items), + /*active_turn*/ None, + ) + .await?; + } self.attach_thread_name(thread_id, &mut thread).await; Ok(thread) } @@ -5002,8 +5010,10 @@ impl CodexMessageProcessor { base_instructions, developer_instructions, ephemeral, + exclude_turns, persist_extended_history, } = params; + let include_turns = !exclude_turns; if sandbox.is_some() && permission_profile.is_some() { self.send_invalid_request_error( request_id, @@ -5223,12 +5233,13 @@ impl CodexMessageProcessor { }) }) .map(|id| id.to_string()); - if let Err(message) = populate_thread_turns( - &mut thread, - ThreadTurnSource::HistoryItems(&history_items), - /*active_turn*/ None, - ) - .await + if include_turns + && let Err(message) = populate_thread_turns( + &mut thread, + ThreadTurnSource::HistoryItems(&history_items), + /*active_turn*/ None, + ) + .await { self.send_internal_error(request_id, message).await; return; @@ -5237,6 +5248,7 @@ impl CodexMessageProcessor { }; if let Some(fork_rollout_path) = session_configured.rollout_path.as_ref() + && include_turns && let Err(message) = populate_thread_turns( &mut thread, ThreadTurnSource::RolloutPath(fork_rollout_path.as_path()), @@ -5287,30 +5299,34 @@ impl CodexMessageProcessor { } let connection_id = request_id.connection_id; - let token_usage_thread = response.thread.clone(); - let token_usage_turn_id = if let Some(turn_id) = - latest_token_usage_turn_id_for_thread_path(&token_usage_thread).await - { - Some(turn_id) - } else { - latest_token_usage_turn_id_from_rollout_path( - rollout_path.as_path(), - &token_usage_thread, - ) - .await - }; + let token_usage_thread = include_turns.then(|| response.thread.clone()); self.outgoing.send_response(request_id, response).await; - // Mirror the resume contract for forks: the new thread is usable as soon - // as the response arrives, so restored usage must follow immediately. - send_thread_token_usage_update_to_connection( - &self.outgoing, - connection_id, - thread_id, - &token_usage_thread, - forked_thread.as_ref(), - token_usage_turn_id, - ) - .await; + // `excludeTurns` is the cheap fork path, so skip restored usage replay + // instead of rebuilding history only to attribute a historical update. + if let Some(token_usage_thread) = token_usage_thread { + let token_usage_turn_id = if let Some(turn_id) = + latest_token_usage_turn_id_for_thread_path(&token_usage_thread).await + { + Some(turn_id) + } else { + latest_token_usage_turn_id_from_rollout_path( + rollout_path.as_path(), + token_usage_thread.turns.as_slice(), + ) + .await + }; + // Mirror the resume contract for forks: the new thread is usable as soon + // as the response arrives, so restored usage must follow immediately. + send_thread_token_usage_update_to_connection( + &self.outgoing, + connection_id, + thread_id, + &token_usage_thread, + forked_thread.as_ref(), + token_usage_turn_id, + ) + .await; + } let notif = ThreadStartedNotification { thread }; self.outgoing @@ -8640,12 +8656,13 @@ async fn handle_pending_thread_resume_request( let request_id = pending.request_id; let connection_id = request_id.connection_id; let mut thread = pending.thread_summary; - if let Err(message) = populate_thread_turns( - &mut thread, - ThreadTurnSource::RolloutPath(pending.rollout_path.as_path()), - active_turn.as_ref(), - ) - .await + if pending.include_turns + && let Err(message) = populate_thread_turns( + &mut thread, + ThreadTurnSource::RolloutPath(pending.rollout_path.as_path()), + active_turn.as_ref(), + ) + .await { outgoing .send_error( @@ -8730,24 +8747,28 @@ async fn handle_pending_thread_resume_request( permission_profile, reasoning_effort, }; - let token_usage_thread = response.thread.clone(); - let token_usage_turn_id = latest_token_usage_turn_id_from_rollout_path( - pending.rollout_path.as_path(), - &token_usage_thread, - ) - .await; + let token_usage_thread = pending.include_turns.then(|| response.thread.clone()); outgoing.send_response(request_id, response).await; - // Rejoining a loaded thread has the same UI contract as a cold resume, but - // uses the live conversation state instead of reconstructing a new session. - send_thread_token_usage_update_to_connection( - outgoing, - connection_id, - conversation_id, - &token_usage_thread, - conversation.as_ref(), - token_usage_turn_id, - ) - .await; + // Match cold resume: metadata-only resume should attach the listener without + // paying the cost of turn reconstruction for historical usage replay. + if let Some(token_usage_thread) = token_usage_thread { + let token_usage_turn_id = latest_token_usage_turn_id_from_rollout_path( + pending.rollout_path.as_path(), + token_usage_thread.turns.as_slice(), + ) + .await; + // Rejoining a loaded thread has the same UI contract as a cold resume, but + // uses the live conversation state instead of reconstructing a new session. + send_thread_token_usage_update_to_connection( + outgoing, + connection_id, + conversation_id, + &token_usage_thread, + conversation.as_ref(), + token_usage_turn_id, + ) + .await; + } outgoing .replay_requests_to_connection_for_thread(connection_id, conversation_id) .await; @@ -10513,7 +10534,7 @@ mod tests { let err = std::io::Error::other(CloudRequirementsLoadError::new( CloudRequirementsLoadErrorCode::RequestFailed, /*status_code*/ None, - "failed to load your workspace-managed config", + "Failed to load cloud requirements (workspace-managed policies).", )); let error = config_load_error(&err); @@ -10523,7 +10544,7 @@ mod tests { Some(json!({ "reason": "cloudRequirements", "errorCode": "RequestFailed", - "detail": "failed to load your workspace-managed config", + "detail": "Failed to load cloud requirements (workspace-managed policies).", })) ); } @@ -10610,6 +10631,7 @@ mod tests { base_instructions: None, developer_instructions: None, personality: None, + exclude_turns: false, persist_extended_history: false, }; let config_snapshot = ThreadConfigSnapshot { diff --git a/codex-rs/app-server/src/codex_message_processor/token_usage_replay.rs b/codex-rs/app-server/src/codex_message_processor/token_usage_replay.rs index d3c06f5e7c..bcd972a470 100644 --- a/codex-rs/app-server/src/codex_message_processor/token_usage_replay.rs +++ b/codex-rs/app-server/src/codex_message_processor/token_usage_replay.rs @@ -17,6 +17,7 @@ use codex_app_server_protocol::Thread; use codex_app_server_protocol::ThreadHistoryBuilder; use codex_app_server_protocol::ThreadTokenUsage; use codex_app_server_protocol::ThreadTokenUsageUpdatedNotification; +use codex_app_server_protocol::Turn; use codex_app_server_protocol::TurnStatus; use codex_core::CodexThread; use codex_protocol::ThreadId; @@ -60,15 +61,15 @@ pub(super) async fn send_thread_token_usage_update_to_connection( pub(super) async fn latest_token_usage_turn_id_for_thread_path(thread: &Thread) -> Option { let rollout_path = thread.path.as_deref()?; - latest_token_usage_turn_id_from_rollout_path(rollout_path, thread).await + latest_token_usage_turn_id_from_rollout_path(rollout_path, thread.turns.as_slice()).await } pub(super) async fn latest_token_usage_turn_id_from_rollout_path( rollout_path: &Path, - thread: &Thread, + turns: &[Turn], ) -> Option { let rollout_items = read_rollout_items_from_rollout(rollout_path).await.ok()?; - latest_token_usage_turn_id_from_rollout_items(&rollout_items, thread) + latest_token_usage_turn_id_from_rollout_items(&rollout_items, turns) } /// Identifies the turn that was active when a `TokenCount` record appeared. @@ -82,21 +83,8 @@ struct TokenUsageTurnOwner { pub(super) fn latest_token_usage_turn_id_from_rollout_items( rollout_items: &[RolloutItem], - thread: &Thread, + turns: &[Turn], ) -> Option { - let owner = latest_token_usage_turn_owner_from_rollout_items(rollout_items)?; - if thread.turns.iter().any(|turn| turn.id == owner.id) { - return Some(owner.id); - } - owner - .position - .and_then(|position| thread.turns.get(position)) - .map(|turn| turn.id.clone()) -} - -fn latest_token_usage_turn_owner_from_rollout_items( - rollout_items: &[RolloutItem], -) -> Option { let mut builder = ThreadHistoryBuilder::new(); let mut token_usage_turn_owner = None; @@ -113,7 +101,15 @@ fn latest_token_usage_turn_owner_from_rollout_items( builder.handle_rollout_item(item); } - token_usage_turn_owner + let owner = token_usage_turn_owner?; + if turns.iter().any(|turn| turn.id == owner.id) { + Some(owner.id) + } else { + owner + .position + .and_then(|position| turns.get(position)) + .map(|turn| turn.id.clone()) + } } /// Chooses a fallback turn id that should own a replayed token usage update. diff --git a/codex-rs/app-server/src/thread_state.rs b/codex-rs/app-server/src/thread_state.rs index 504e59468b..323aba19d7 100644 --- a/codex-rs/app-server/src/thread_state.rs +++ b/codex-rs/app-server/src/thread_state.rs @@ -31,6 +31,7 @@ pub(crate) struct PendingThreadResumeRequest { pub(crate) config_snapshot: ThreadConfigSnapshot, pub(crate) instruction_sources: Vec, pub(crate) thread_summary: codex_app_server_protocol::Thread, + pub(crate) include_turns: bool, } // ThreadListenerCommand is used to perform operations in the context of the thread listener, for serialization purposes. diff --git a/codex-rs/app-server/tests/suite/v2/realtime_conversation.rs b/codex-rs/app-server/tests/suite/v2/realtime_conversation.rs index 793efbe1e1..dfc3fea318 100644 --- a/codex-rs/app-server/tests/suite/v2/realtime_conversation.rs +++ b/codex-rs/app-server/tests/suite/v2/realtime_conversation.rs @@ -72,6 +72,7 @@ use wiremock::matchers::path; use wiremock::matchers::path_regex; const DEFAULT_TIMEOUT: Duration = Duration::from_secs(10); +const DELEGATED_SHELL_TOOL_TIMEOUT_MS: u64 = 30_000; const STARTUP_CONTEXT_HEADER: &str = "Startup context from Codex."; const V2_STEERING_ACKNOWLEDGEMENT: &str = "This was sent to steer the previous background agent task."; @@ -1781,7 +1782,9 @@ async fn webrtc_v2_tool_call_delegated_turn_can_execute_shell_tool() -> Result<( create_shell_command_sse_response( realtime_tool_ok_command(), /*workdir*/ None, - Some(5000), + // Windows CI can spend several seconds starting the nested PowerShell command. This + // test verifies delegated shell-tool plumbing, not timeout enforcement. + Some(DELEGATED_SHELL_TOOL_TIMEOUT_MS), "shell_call", )?, create_final_assistant_message_sse_response("shell tool finished")?, diff --git a/codex-rs/app-server/tests/suite/v2/request_permissions.rs b/codex-rs/app-server/tests/suite/v2/request_permissions.rs index 4a4eb9340e..7acdc1b12f 100644 --- a/codex-rs/app-server/tests/suite/v2/request_permissions.rs +++ b/codex-rs/app-server/tests/suite/v2/request_permissions.rs @@ -78,12 +78,32 @@ async fn request_permissions_round_trip() -> Result<()> { assert_eq!(params.item_id, "call1"); assert!(params.cwd.as_path().is_absolute()); assert_eq!(params.reason, Some("Select a workspace root".to_string())); - let requested_writes = params + let requested_file_system = params .permissions .file_system - .and_then(|file_system| file_system.write) + .expect("request should include file system permissions"); + let requested_writes = requested_file_system + .write + .clone() .expect("request should include write permissions"); assert_eq!(requested_writes.len(), 2); + assert_eq!( + requested_file_system.entries, + Some(vec![ + codex_app_server_protocol::FileSystemSandboxEntry { + path: codex_app_server_protocol::FileSystemPath::Path { + path: requested_writes[0].clone(), + }, + access: codex_app_server_protocol::FileSystemAccessMode::Write, + }, + codex_app_server_protocol::FileSystemSandboxEntry { + path: codex_app_server_protocol::FileSystemPath::Path { + path: requested_writes[1].clone(), + }, + access: codex_app_server_protocol::FileSystemAccessMode::Write, + }, + ]) + ); let resolved_request_id = request_id.clone(); mcp.send_response( diff --git a/codex-rs/app-server/tests/suite/v2/thread_fork.rs b/codex-rs/app-server/tests/suite/v2/thread_fork.rs index 0425ce3e3c..7741ced163 100644 --- a/codex-rs/app-server/tests/suite/v2/thread_fork.rs +++ b/codex-rs/app-server/tests/suite/v2/thread_fork.rs @@ -241,6 +241,54 @@ async fn thread_fork_emits_restored_token_usage_before_next_turn() -> Result<()> Ok(()) } +#[tokio::test] +async fn thread_fork_can_exclude_turns_and_skip_restored_token_usage() -> Result<()> { + let server = create_mock_responses_server_repeating_assistant("Done").await; + let codex_home = TempDir::new()?; + create_config_toml(codex_home.path(), &server.uri())?; + + let conversation_id = create_fake_rollout_with_token_usage( + codex_home.path(), + "2025-01-05T12-00-00", + "2025-01-05T12:00:00Z", + "Saved user message", + Some("mock_provider"), + )?; + + let mut mcp = McpProcess::new(codex_home.path()).await?; + timeout(DEFAULT_READ_TIMEOUT, mcp.initialize()).await??; + + let fork_id = mcp + .send_thread_fork_request(ThreadForkParams { + thread_id: conversation_id.clone(), + exclude_turns: true, + ..Default::default() + }) + .await?; + let fork_resp: JSONRPCResponse = timeout( + DEFAULT_READ_TIMEOUT, + mcp.read_stream_until_response_message(RequestId::Integer(fork_id)), + ) + .await??; + let ThreadForkResponse { thread, .. } = to_response::(fork_resp)?; + + assert_eq!(thread.forked_from_id, Some(conversation_id)); + assert_eq!(thread.preview, "Saved user message"); + assert!(thread.turns.is_empty()); + + let note = timeout( + DEFAULT_READ_TIMEOUT, + mcp.read_stream_until_notification_message("thread/tokenUsage/updated"), + ) + .await; + assert!( + note.is_err(), + "excludeTurns=true should not replay token usage" + ); + + Ok(()) +} + #[tokio::test] async fn thread_fork_tracks_thread_initialized_analytics() -> Result<()> { let server = create_mock_responses_server_repeating_assistant("Done").await; diff --git a/codex-rs/app-server/tests/suite/v2/thread_resume.rs b/codex-rs/app-server/tests/suite/v2/thread_resume.rs index 7a5f2c543d..c86f88825d 100644 --- a/codex-rs/app-server/tests/suite/v2/thread_resume.rs +++ b/codex-rs/app-server/tests/suite/v2/thread_resume.rs @@ -285,6 +285,45 @@ async fn thread_resume_returns_rollout_history() -> Result<()> { Ok(()) } +#[tokio::test] +async fn thread_resume_can_skip_turns_for_metadata_only_resume() -> Result<()> { + let server = create_mock_responses_server_repeating_assistant("Done").await; + let codex_home = TempDir::new()?; + create_config_toml(codex_home.path(), &server.uri())?; + + let conversation_id = create_fake_rollout_with_text_elements( + codex_home.path(), + "2025-01-05T12-00-00", + "2025-01-05T12:00:00Z", + "Saved user message", + Vec::new(), + Some("mock_provider"), + /*git_info*/ None, + )?; + + let mut mcp = McpProcess::new(codex_home.path()).await?; + timeout(DEFAULT_READ_TIMEOUT, mcp.initialize()).await??; + + let resume_id = mcp + .send_thread_resume_request(ThreadResumeParams { + thread_id: conversation_id.clone(), + exclude_turns: true, + ..Default::default() + }) + .await?; + let resume_resp: JSONRPCResponse = timeout( + DEFAULT_READ_TIMEOUT, + mcp.read_stream_until_response_message(RequestId::Integer(resume_id)), + ) + .await??; + let ThreadResumeResponse { thread, .. } = to_response::(resume_resp)?; + + assert_eq!(thread.id, conversation_id); + assert!(thread.turns.is_empty()); + + Ok(()) +} + #[tokio::test] async fn thread_resume_emits_restored_token_usage_before_next_turn() -> Result<()> { let server = create_mock_responses_server_repeating_assistant("Done").await; @@ -338,6 +377,80 @@ async fn thread_resume_emits_restored_token_usage_before_next_turn() -> Result<( Ok(()) } +#[tokio::test] +async fn thread_resume_skips_restored_token_usage_when_turns_are_excluded() -> Result<()> { + let server = create_mock_responses_server_repeating_assistant("Done").await; + let codex_home = TempDir::new()?; + create_config_toml(codex_home.path(), &server.uri())?; + + let conversation_id = create_fake_rollout_with_token_usage( + codex_home.path(), + "2025-01-05T12-00-00", + "2025-01-05T12:00:00Z", + "Saved user message", + Some("mock_provider"), + )?; + + let mut mcp = McpProcess::new(codex_home.path()).await?; + timeout(DEFAULT_READ_TIMEOUT, mcp.initialize()).await??; + + let first_resume_id = mcp + .send_thread_resume_request(ThreadResumeParams { + thread_id: conversation_id.clone(), + ..Default::default() + }) + .await?; + let first_resume_resp: JSONRPCResponse = timeout( + DEFAULT_READ_TIMEOUT, + mcp.read_stream_until_response_message(RequestId::Integer(first_resume_id)), + ) + .await??; + let ThreadResumeResponse { thread, .. } = + to_response::(first_resume_resp)?; + let expected_turn_id = thread.turns[0].id.clone(); + + let first_note = timeout( + DEFAULT_READ_TIMEOUT, + mcp.read_stream_until_notification_message("thread/tokenUsage/updated"), + ) + .await??; + let parsed: ServerNotification = first_note.try_into()?; + let ServerNotification::ThreadTokenUsageUpdated(notification) = parsed else { + panic!("expected thread/tokenUsage/updated notification"); + }; + assert_eq!(notification.turn_id, expected_turn_id); + + let second_resume_id = mcp + .send_thread_resume_request(ThreadResumeParams { + thread_id: conversation_id, + exclude_turns: true, + ..Default::default() + }) + .await?; + let second_resume_resp: JSONRPCResponse = timeout( + DEFAULT_READ_TIMEOUT, + mcp.read_stream_until_response_message(RequestId::Integer(second_resume_id)), + ) + .await??; + let ThreadResumeResponse { + thread: resumed_again, + .. + } = to_response::(second_resume_resp)?; + assert!(resumed_again.turns.is_empty()); + + let second_note = timeout( + DEFAULT_READ_TIMEOUT, + mcp.read_stream_until_notification_message("thread/tokenUsage/updated"), + ) + .await; + assert!( + second_note.is_err(), + "excludeTurns=true should not replay token usage" + ); + + Ok(()) +} + #[tokio::test] async fn thread_resume_token_usage_replay_ignores_stale_interrupted_tail_turn() -> Result<()> { let server = create_mock_responses_server_repeating_assistant("Done").await; @@ -1339,6 +1452,84 @@ async fn thread_resume_rejoins_running_thread_even_with_override_mismatch() -> R Ok(()) } +#[tokio::test] +async fn thread_resume_can_skip_turns_when_thread_is_running() -> Result<()> { + let server = responses::start_mock_server().await; + let _response_mock = responses::mount_sse_once( + &server, + responses::sse(vec![ + responses::ev_response_created("resp-1"), + responses::ev_assistant_message("msg-1", "Done"), + responses::ev_completed("resp-1"), + ]), + ) + .await; + let codex_home = TempDir::new()?; + create_config_toml(codex_home.path(), &server.uri())?; + + let mut primary = McpProcess::new(codex_home.path()).await?; + timeout(DEFAULT_READ_TIMEOUT, primary.initialize()).await??; + + let start_id = primary + .send_thread_start_request(ThreadStartParams { + model: Some("gpt-5.4".to_string()), + ..Default::default() + }) + .await?; + let start_resp: JSONRPCResponse = timeout( + DEFAULT_READ_TIMEOUT, + primary.read_stream_until_response_message(RequestId::Integer(start_id)), + ) + .await??; + let ThreadStartResponse { thread, .. } = to_response::(start_resp)?; + + let turn_id = primary + .send_turn_start_request(TurnStartParams { + thread_id: thread.id.clone(), + input: vec![UserInput::Text { + text: "seed history".to_string(), + text_elements: Vec::new(), + }], + ..Default::default() + }) + .await?; + timeout( + DEFAULT_READ_TIMEOUT, + primary.read_stream_until_response_message(RequestId::Integer(turn_id)), + ) + .await??; + timeout( + DEFAULT_READ_TIMEOUT, + primary.read_stream_until_notification_message("turn/completed"), + ) + .await??; + + let mut secondary = McpProcess::new(codex_home.path()).await?; + timeout(DEFAULT_READ_TIMEOUT, secondary.initialize()).await??; + + let resume_id = secondary + .send_thread_resume_request(ThreadResumeParams { + thread_id: thread.id.clone(), + exclude_turns: true, + ..Default::default() + }) + .await?; + let resume_resp: JSONRPCResponse = timeout( + DEFAULT_READ_TIMEOUT, + secondary.read_stream_until_response_message(RequestId::Integer(resume_id)), + ) + .await??; + let ThreadResumeResponse { + thread: resumed, .. + } = to_response::(resume_resp)?; + + assert_eq!(resumed.id, thread.id); + assert_eq!(resumed.status, ThreadStatus::Idle); + assert!(resumed.turns.is_empty()); + + Ok(()) +} + #[tokio::test] async fn thread_resume_replays_pending_command_execution_request_approval() -> Result<()> { let responses = vec![ diff --git a/codex-rs/cli/src/main.rs b/codex-rs/cli/src/main.rs index 9852a2cd5f..3f4b018f13 100644 --- a/codex-rs/cli/src/main.rs +++ b/codex-rs/cli/src/main.rs @@ -176,6 +176,7 @@ enum Subcommand { } #[derive(Debug, Parser)] +#[command(bin_name = "codex plugin")] struct PluginCli { #[clap(flatten)] pub config_overrides: CliConfigOverrides, @@ -1773,6 +1774,30 @@ mod tests { assert!(matches!(cli.subcommand, Some(Subcommand::Responses(_)))); } + fn help_from_args(args: &[&str]) -> String { + let err = MultitoolCli::try_parse_from(args).expect_err("help should short-circuit"); + assert_eq!(err.kind(), clap::error::ErrorKind::DisplayHelp); + err.to_string() + } + + #[test] + fn plugin_marketplace_help_uses_plugin_namespace() { + let help = help_from_args(&["codex", "plugin", "marketplace", "--help"]); + assert!( + help.contains("Usage: codex plugin marketplace [OPTIONS] "), + "{help}" + ); + + for (subcommand, usage) in [ + ("add", "Usage: codex plugin marketplace add"), + ("upgrade", "Usage: codex plugin marketplace upgrade"), + ("remove", "Usage: codex plugin marketplace remove"), + ] { + let help = help_from_args(&["codex", "plugin", "marketplace", subcommand, "--help"]); + assert!(help.contains(usage), "{help}"); + } + } + #[test] fn plugin_marketplace_add_parses_under_plugin() { let cli = diff --git a/codex-rs/cli/src/marketplace_cmd.rs b/codex-rs/cli/src/marketplace_cmd.rs index a29f9ba76f..5130e9ebcd 100644 --- a/codex-rs/cli/src/marketplace_cmd.rs +++ b/codex-rs/cli/src/marketplace_cmd.rs @@ -13,6 +13,7 @@ use codex_core::plugins::remove_marketplace; use codex_utils_cli::CliConfigOverrides; #[derive(Debug, Parser)] +#[command(bin_name = "codex plugin marketplace")] pub struct MarketplaceCli { #[clap(flatten)] pub config_overrides: CliConfigOverrides, @@ -29,6 +30,7 @@ enum MarketplaceSubcommand { } #[derive(Debug, Parser)] +#[command(bin_name = "codex plugin marketplace add")] struct AddMarketplaceArgs { /// Marketplace source. Supports owner/repo[@ref], HTTP(S) Git URLs, SSH URLs, /// or local marketplace root directories. @@ -46,11 +48,13 @@ struct AddMarketplaceArgs { } #[derive(Debug, Parser)] +#[command(bin_name = "codex plugin marketplace upgrade")] struct UpgradeMarketplaceArgs { marketplace_name: Option, } #[derive(Debug, Parser)] +#[command(bin_name = "codex plugin marketplace remove")] struct RemoveMarketplaceArgs { /// Configured marketplace name to remove. marketplace_name: String, diff --git a/codex-rs/cloud-requirements/src/lib.rs b/codex-rs/cloud-requirements/src/lib.rs index aeed90e8b0..ca9ec56fe9 100644 --- a/codex-rs/cloud-requirements/src/lib.rs +++ b/codex-rs/cloud-requirements/src/lib.rs @@ -50,9 +50,16 @@ const CLOUD_REQUIREMENTS_CACHE_TTL: Duration = Duration::from_secs(30 * 60); const CLOUD_REQUIREMENTS_FETCH_ATTEMPT_METRIC: &str = "codex.cloud_requirements.fetch_attempt"; const CLOUD_REQUIREMENTS_FETCH_FINAL_METRIC: &str = "codex.cloud_requirements.fetch_final"; const CLOUD_REQUIREMENTS_LOAD_METRIC: &str = "codex.cloud_requirements.load"; -const CLOUD_REQUIREMENTS_LOAD_FAILED_MESSAGE: &str = "failed to load your workspace-managed config"; -const CLOUD_REQUIREMENTS_PARSE_FAILED_MESSAGE: &str = "Your workspace-managed config is invalid and could not be parsed. Please contact your workspace admin."; -const CLOUD_REQUIREMENTS_AUTH_RECOVERY_FAILED_MESSAGE: &str = "Your authentication session could not be refreshed automatically. Please log out and sign in again."; +const CLOUD_REQUIREMENTS_LOAD_FAILED_MESSAGE: &str = + "Failed to load cloud requirements (workspace-managed policies)."; +const CLOUD_REQUIREMENTS_PARSE_FAILED_MESSAGE: &str = concat!( + "Cloud requirements (workspace-managed policies) are invalid and could not be parsed. ", + "Please contact your workspace admin." +); +const CLOUD_REQUIREMENTS_AUTH_RECOVERY_FAILED_MESSAGE: &str = concat!( + "Your authentication session could not be refreshed automatically. ", + "Please log out and sign in again." +); const CLOUD_REQUIREMENTS_CACHE_WRITE_HMAC_KEY: &[u8] = b"codex-cloud-requirements-cache-v3-064f8542-75b4-494c-a294-97d3ce597271"; const CLOUD_REQUIREMENTS_CACHE_READ_HMAC_KEYS: &[&[u8]] = @@ -2097,10 +2104,7 @@ enabled = false .await .expect("cloud requirements task") .expect_err("cloud requirements retry exhaustion should fail closed"); - assert_eq!( - err.to_string(), - "failed to load your workspace-managed config" - ); + assert_eq!(err.to_string(), CLOUD_REQUIREMENTS_LOAD_FAILED_MESSAGE); assert_eq!(err.code(), CloudRequirementsLoadErrorCode::RequestFailed); assert_eq!( fetcher.request_count.load(Ordering::SeqCst), diff --git a/codex-rs/config/Cargo.toml b/codex-rs/config/Cargo.toml index a7a50b0720..9df08b115d 100644 --- a/codex-rs/config/Cargo.toml +++ b/codex-rs/config/Cargo.toml @@ -4,6 +4,10 @@ version.workspace = true edition.workspace = true license.workspace = true +[[example]] +name = "generate-proto" +path = "examples/generate-proto.rs" + [lints] workspace = true @@ -21,6 +25,7 @@ codex-utils-path = { workspace = true } futures = { workspace = true, features = ["alloc", "std"] } gethostname = { workspace = true } multimap = { workspace = true } +prost = "0.14.3" schemars = { workspace = true } serde = { workspace = true, features = ["derive"] } serde_json = { workspace = true } @@ -30,6 +35,8 @@ thiserror = { workspace = true } tokio = { workspace = true, features = ["fs"] } toml = { workspace = true } toml_edit = { workspace = true } +tonic = { workspace = true } +tonic-prost = { workspace = true } tracing = { workspace = true } wildmatch = { workspace = true } @@ -44,3 +51,6 @@ winapi-util = { workspace = true } pretty_assertions = { workspace = true } tempfile = { workspace = true } tokio = { workspace = true, features = ["full"] } +tokio-stream = { workspace = true, features = ["net"] } +tonic = { workspace = true, features = ["router", "transport"] } +tonic-prost-build = { version = "=0.14.3", default-features = false, features = ["transport"] } diff --git a/codex-rs/config/examples/generate-proto.rs b/codex-rs/config/examples/generate-proto.rs new file mode 100644 index 0000000000..03f0f796da --- /dev/null +++ b/codex-rs/config/examples/generate-proto.rs @@ -0,0 +1,19 @@ +use std::path::PathBuf; + +fn main() -> Result<(), Box> { + let Some(proto_dir_arg) = std::env::args().nth(1) else { + eprintln!("Usage: generate-proto "); + std::process::exit(1); + }; + + let proto_dir = PathBuf::from(proto_dir_arg); + let proto_file = proto_dir.join("codex.thread_config.v1.proto"); + + tonic_prost_build::configure() + .build_client(true) + .build_server(true) + .out_dir(&proto_dir) + .compile_protos(&[proto_file], &[proto_dir])?; + + Ok(()) +} diff --git a/codex-rs/config/scripts/generate-proto.sh b/codex-rs/config/scripts/generate-proto.sh new file mode 100755 index 0000000000..86af22b895 --- /dev/null +++ b/codex-rs/config/scripts/generate-proto.sh @@ -0,0 +1,38 @@ +#!/usr/bin/env bash +set -euo pipefail + +script_dir="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" +repo_root="$(cd "$script_dir/../../.." && pwd)" +proto_dir="$repo_root/codex-rs/config/src/thread_config/proto" +generated="$proto_dir/codex.thread_config.v1.rs" +tmpdir="$(mktemp -d)" + +cleanup() { + rm -rf "$tmpdir" +} +trap cleanup EXIT + +( + cd "$repo_root/codex-rs" + CARGO_TARGET_DIR="$tmpdir/target" cargo run \ + -p codex-config \ + --example generate-proto \ + -- "$proto_dir" +) + +if ! sed -n '2p' "$generated" | grep -q 'clippy::trivially_copy_pass_by_ref'; then + { + sed -n '1p' "$generated" + printf '#![allow(clippy::trivially_copy_pass_by_ref)]\n' + sed '1d' "$generated" + } > "$tmpdir/generated.rs" + mv "$tmpdir/generated.rs" "$generated" +fi + +rustfmt --edition 2024 "$generated" + +awk ' + NR == 3 && previous ~ /clippy::trivially_copy_pass_by_ref/ && $0 != "" { print "" } + { print; previous = $0 } +' "$generated" > "$tmpdir/formatted.rs" +mv "$tmpdir/formatted.rs" "$generated" diff --git a/codex-rs/config/src/lib.rs b/codex-rs/config/src/lib.rs index e90d09c09d..e3d95acb86 100644 --- a/codex-rs/config/src/lib.rs +++ b/codex-rs/config/src/lib.rs @@ -106,6 +106,7 @@ pub use state::ConfigLayerStack; pub use state::ConfigLayerStackOrdering; pub use state::LoaderOverrides; pub use thread_config::NoopThreadConfigLoader; +pub use thread_config::RemoteThreadConfigLoader; pub use thread_config::SessionThreadConfig; pub use thread_config::StaticThreadConfigLoader; pub use thread_config::ThreadConfigContext; diff --git a/codex-rs/config/src/thread_config.rs b/codex-rs/config/src/thread_config.rs index e1e598708e..1b3ea8fe87 100644 --- a/codex-rs/config/src/thread_config.rs +++ b/codex-rs/config/src/thread_config.rs @@ -10,6 +10,10 @@ use toml::Value as TomlValue; use crate::ConfigLayerEntry; +mod remote; + +pub use remote::RemoteThreadConfigLoader; + /// Context available to implementations when loading thread-scoped config. #[derive(Clone, Debug, Default, Eq, PartialEq)] pub struct ThreadConfigContext { diff --git a/codex-rs/config/src/thread_config/proto/codex.thread_config.v1.proto b/codex-rs/config/src/thread_config/proto/codex.thread_config.v1.proto new file mode 100644 index 0000000000..1efccfd1bf --- /dev/null +++ b/codex-rs/config/src/thread_config/proto/codex.thread_config.v1.proto @@ -0,0 +1,68 @@ +syntax = "proto3"; + +package codex.thread_config.v1; + +service ThreadConfigLoader { + rpc Load(LoadThreadConfigRequest) returns (LoadThreadConfigResponse); +} + +message LoadThreadConfigRequest { + optional string thread_id = 1; + optional string cwd = 2; +} + +message LoadThreadConfigResponse { + repeated ThreadConfigSource sources = 1; +} + +message ThreadConfigSource { + oneof source { + SessionThreadConfig session = 1; + UserThreadConfig user = 2; + } +} + +message SessionThreadConfig { + optional string model_provider = 1; + repeated ModelProvider model_providers = 2; + map features = 3; +} + +message UserThreadConfig {} + +message ModelProvider { + string id = 1; + string name = 2; + optional string base_url = 3; + optional string env_key = 4; + optional string env_key_instructions = 5; + optional string experimental_bearer_token = 6; + optional ModelProviderAuthInfo auth = 7; + WireApi wire_api = 8; + optional StringMap query_params = 9; + optional StringMap http_headers = 10; + optional StringMap env_http_headers = 11; + optional uint64 request_max_retries = 12; + optional uint64 stream_max_retries = 13; + optional uint64 stream_idle_timeout_ms = 14; + optional uint64 websocket_connect_timeout_ms = 15; + bool requires_openai_auth = 16; + bool supports_websockets = 17; +} + +message StringMap { + map values = 1; +} + +message ModelProviderAuthInfo { + string command = 1; + repeated string args = 2; + uint64 timeout_ms = 3; + uint64 refresh_interval_ms = 4; + string cwd = 5; +} + +enum WireApi { + WIRE_API_UNSPECIFIED = 0; + WIRE_API_RESPONSES = 1; +} diff --git a/codex-rs/config/src/thread_config/proto/codex.thread_config.v1.rs b/codex-rs/config/src/thread_config/proto/codex.thread_config.v1.rs new file mode 100644 index 0000000000..30a76bc6b2 --- /dev/null +++ b/codex-rs/config/src/thread_config/proto/codex.thread_config.v1.rs @@ -0,0 +1,400 @@ +// This file is @generated by prost-build. +#![allow(clippy::trivially_copy_pass_by_ref)] + +#[derive(Clone, PartialEq, Eq, Hash, ::prost::Message)] +pub struct LoadThreadConfigRequest { + #[prost(string, optional, tag = "1")] + pub thread_id: ::core::option::Option<::prost::alloc::string::String>, + #[prost(string, optional, tag = "2")] + pub cwd: ::core::option::Option<::prost::alloc::string::String>, +} +#[derive(Clone, PartialEq, ::prost::Message)] +pub struct LoadThreadConfigResponse { + #[prost(message, repeated, tag = "1")] + pub sources: ::prost::alloc::vec::Vec, +} +#[derive(Clone, PartialEq, ::prost::Message)] +pub struct ThreadConfigSource { + #[prost(oneof = "thread_config_source::Source", tags = "1, 2")] + pub source: ::core::option::Option, +} +/// Nested message and enum types in `ThreadConfigSource`. +pub mod thread_config_source { + #[derive(Clone, PartialEq, ::prost::Oneof)] + pub enum Source { + #[prost(message, tag = "1")] + Session(super::SessionThreadConfig), + #[prost(message, tag = "2")] + User(super::UserThreadConfig), + } +} +#[derive(Clone, PartialEq, ::prost::Message)] +pub struct SessionThreadConfig { + #[prost(string, optional, tag = "1")] + pub model_provider: ::core::option::Option<::prost::alloc::string::String>, + #[prost(message, repeated, tag = "2")] + pub model_providers: ::prost::alloc::vec::Vec, + #[prost(map = "string, bool", tag = "3")] + pub features: ::std::collections::HashMap<::prost::alloc::string::String, bool>, +} +#[derive(Clone, Copy, PartialEq, Eq, Hash, ::prost::Message)] +pub struct UserThreadConfig {} +#[derive(Clone, PartialEq, ::prost::Message)] +pub struct ModelProvider { + #[prost(string, tag = "1")] + pub id: ::prost::alloc::string::String, + #[prost(string, tag = "2")] + pub name: ::prost::alloc::string::String, + #[prost(string, optional, tag = "3")] + pub base_url: ::core::option::Option<::prost::alloc::string::String>, + #[prost(string, optional, tag = "4")] + pub env_key: ::core::option::Option<::prost::alloc::string::String>, + #[prost(string, optional, tag = "5")] + pub env_key_instructions: ::core::option::Option<::prost::alloc::string::String>, + #[prost(string, optional, tag = "6")] + pub experimental_bearer_token: ::core::option::Option<::prost::alloc::string::String>, + #[prost(message, optional, tag = "7")] + pub auth: ::core::option::Option, + #[prost(enumeration = "WireApi", tag = "8")] + pub wire_api: i32, + #[prost(message, optional, tag = "9")] + pub query_params: ::core::option::Option, + #[prost(message, optional, tag = "10")] + pub http_headers: ::core::option::Option, + #[prost(message, optional, tag = "11")] + pub env_http_headers: ::core::option::Option, + #[prost(uint64, optional, tag = "12")] + pub request_max_retries: ::core::option::Option, + #[prost(uint64, optional, tag = "13")] + pub stream_max_retries: ::core::option::Option, + #[prost(uint64, optional, tag = "14")] + pub stream_idle_timeout_ms: ::core::option::Option, + #[prost(uint64, optional, tag = "15")] + pub websocket_connect_timeout_ms: ::core::option::Option, + #[prost(bool, tag = "16")] + pub requires_openai_auth: bool, + #[prost(bool, tag = "17")] + pub supports_websockets: bool, +} +#[derive(Clone, PartialEq, ::prost::Message)] +pub struct StringMap { + #[prost(map = "string, string", tag = "1")] + pub values: + ::std::collections::HashMap<::prost::alloc::string::String, ::prost::alloc::string::String>, +} +#[derive(Clone, PartialEq, Eq, Hash, ::prost::Message)] +pub struct ModelProviderAuthInfo { + #[prost(string, tag = "1")] + pub command: ::prost::alloc::string::String, + #[prost(string, repeated, tag = "2")] + pub args: ::prost::alloc::vec::Vec<::prost::alloc::string::String>, + #[prost(uint64, tag = "3")] + pub timeout_ms: u64, + #[prost(uint64, tag = "4")] + pub refresh_interval_ms: u64, + #[prost(string, tag = "5")] + pub cwd: ::prost::alloc::string::String, +} +#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, PartialOrd, Ord, ::prost::Enumeration)] +#[repr(i32)] +pub enum WireApi { + Unspecified = 0, + Responses = 1, +} +impl WireApi { + /// String value of the enum field names used in the ProtoBuf definition. + /// + /// The values are not transformed in any way and thus are considered stable + /// (if the ProtoBuf definition does not change) and safe for programmatic use. + pub fn as_str_name(&self) -> &'static str { + match self { + Self::Unspecified => "WIRE_API_UNSPECIFIED", + Self::Responses => "WIRE_API_RESPONSES", + } + } + /// Creates an enum from field names used in the ProtoBuf definition. + pub fn from_str_name(value: &str) -> ::core::option::Option { + match value { + "WIRE_API_UNSPECIFIED" => Some(Self::Unspecified), + "WIRE_API_RESPONSES" => Some(Self::Responses), + _ => None, + } + } +} +/// Generated client implementations. +pub mod thread_config_loader_client { + #![allow( + unused_variables, + dead_code, + missing_docs, + clippy::wildcard_imports, + clippy::let_unit_value + )] + use tonic::codegen::http::Uri; + use tonic::codegen::*; + #[derive(Debug, Clone)] + pub struct ThreadConfigLoaderClient { + inner: tonic::client::Grpc, + } + impl ThreadConfigLoaderClient { + /// Attempt to create a new client by connecting to a given endpoint. + pub async fn connect(dst: D) -> Result + where + D: TryInto, + D::Error: Into, + { + let conn = tonic::transport::Endpoint::new(dst)?.connect().await?; + Ok(Self::new(conn)) + } + } + impl ThreadConfigLoaderClient + where + T: tonic::client::GrpcService, + T::Error: Into, + T::ResponseBody: Body + std::marker::Send + 'static, + ::Error: Into + std::marker::Send, + { + pub fn new(inner: T) -> Self { + let inner = tonic::client::Grpc::new(inner); + Self { inner } + } + pub fn with_origin(inner: T, origin: Uri) -> Self { + let inner = tonic::client::Grpc::with_origin(inner, origin); + Self { inner } + } + pub fn with_interceptor( + inner: T, + interceptor: F, + ) -> ThreadConfigLoaderClient> + where + F: tonic::service::Interceptor, + T::ResponseBody: Default, + T: tonic::codegen::Service< + http::Request, + Response = http::Response< + >::ResponseBody, + >, + >, + >>::Error: + Into + std::marker::Send + std::marker::Sync, + { + ThreadConfigLoaderClient::new(InterceptedService::new(inner, interceptor)) + } + /// Compress requests with the given encoding. + /// + /// This requires the server to support it otherwise it might respond with an + /// error. + #[must_use] + pub fn send_compressed(mut self, encoding: CompressionEncoding) -> Self { + self.inner = self.inner.send_compressed(encoding); + self + } + /// Enable decompressing responses. + #[must_use] + pub fn accept_compressed(mut self, encoding: CompressionEncoding) -> Self { + self.inner = self.inner.accept_compressed(encoding); + self + } + /// Limits the maximum size of a decoded message. + /// + /// Default: `4MB` + #[must_use] + pub fn max_decoding_message_size(mut self, limit: usize) -> Self { + self.inner = self.inner.max_decoding_message_size(limit); + self + } + /// Limits the maximum size of an encoded message. + /// + /// Default: `usize::MAX` + #[must_use] + pub fn max_encoding_message_size(mut self, limit: usize) -> Self { + self.inner = self.inner.max_encoding_message_size(limit); + self + } + pub async fn load( + &mut self, + request: impl tonic::IntoRequest, + ) -> std::result::Result, tonic::Status> + { + self.inner.ready().await.map_err(|e| { + tonic::Status::unknown(format!("Service was not ready: {}", e.into())) + })?; + let codec = tonic_prost::ProstCodec::default(); + let path = http::uri::PathAndQuery::from_static( + "/codex.thread_config.v1.ThreadConfigLoader/Load", + ); + let mut req = request.into_request(); + req.extensions_mut().insert(GrpcMethod::new( + "codex.thread_config.v1.ThreadConfigLoader", + "Load", + )); + self.inner.unary(req, path, codec).await + } + } +} +/// Generated server implementations. +pub mod thread_config_loader_server { + #![allow( + unused_variables, + dead_code, + missing_docs, + clippy::wildcard_imports, + clippy::let_unit_value + )] + use tonic::codegen::*; + /// Generated trait containing gRPC methods that should be implemented for use with ThreadConfigLoaderServer. + #[async_trait] + pub trait ThreadConfigLoader: std::marker::Send + std::marker::Sync + 'static { + async fn load( + &self, + request: tonic::Request, + ) -> std::result::Result, tonic::Status>; + } + #[derive(Debug)] + pub struct ThreadConfigLoaderServer { + inner: Arc, + accept_compression_encodings: EnabledCompressionEncodings, + send_compression_encodings: EnabledCompressionEncodings, + max_decoding_message_size: Option, + max_encoding_message_size: Option, + } + impl ThreadConfigLoaderServer { + pub fn new(inner: T) -> Self { + Self::from_arc(Arc::new(inner)) + } + pub fn from_arc(inner: Arc) -> Self { + Self { + inner, + accept_compression_encodings: Default::default(), + send_compression_encodings: Default::default(), + max_decoding_message_size: None, + max_encoding_message_size: None, + } + } + pub fn with_interceptor(inner: T, interceptor: F) -> InterceptedService + where + F: tonic::service::Interceptor, + { + InterceptedService::new(Self::new(inner), interceptor) + } + /// Enable decompressing requests with the given encoding. + #[must_use] + pub fn accept_compressed(mut self, encoding: CompressionEncoding) -> Self { + self.accept_compression_encodings.enable(encoding); + self + } + /// Compress responses with the given encoding, if the client supports it. + #[must_use] + pub fn send_compressed(mut self, encoding: CompressionEncoding) -> Self { + self.send_compression_encodings.enable(encoding); + self + } + /// Limits the maximum size of a decoded message. + /// + /// Default: `4MB` + #[must_use] + pub fn max_decoding_message_size(mut self, limit: usize) -> Self { + self.max_decoding_message_size = Some(limit); + self + } + /// Limits the maximum size of an encoded message. + /// + /// Default: `usize::MAX` + #[must_use] + pub fn max_encoding_message_size(mut self, limit: usize) -> Self { + self.max_encoding_message_size = Some(limit); + self + } + } + impl tonic::codegen::Service> for ThreadConfigLoaderServer + where + T: ThreadConfigLoader, + B: Body + std::marker::Send + 'static, + B::Error: Into + std::marker::Send + 'static, + { + type Response = http::Response; + type Error = std::convert::Infallible; + type Future = BoxFuture; + fn poll_ready( + &mut self, + _cx: &mut Context<'_>, + ) -> Poll> { + Poll::Ready(Ok(())) + } + fn call(&mut self, req: http::Request) -> Self::Future { + match req.uri().path() { + "/codex.thread_config.v1.ThreadConfigLoader/Load" => { + #[allow(non_camel_case_types)] + struct LoadSvc(pub Arc); + impl + tonic::server::UnaryService for LoadSvc + { + type Response = super::LoadThreadConfigResponse; + type Future = BoxFuture, tonic::Status>; + fn call( + &mut self, + request: tonic::Request, + ) -> Self::Future { + let inner = Arc::clone(&self.0); + let fut = async move { + ::load(&inner, request).await + }; + Box::pin(fut) + } + } + let accept_compression_encodings = self.accept_compression_encodings; + let send_compression_encodings = self.send_compression_encodings; + let max_decoding_message_size = self.max_decoding_message_size; + let max_encoding_message_size = self.max_encoding_message_size; + let inner = self.inner.clone(); + let fut = async move { + let method = LoadSvc(inner); + let codec = tonic_prost::ProstCodec::default(); + let mut grpc = tonic::server::Grpc::new(codec) + .apply_compression_config( + accept_compression_encodings, + send_compression_encodings, + ) + .apply_max_message_size_config( + max_decoding_message_size, + max_encoding_message_size, + ); + let res = grpc.unary(method, req).await; + Ok(res) + }; + Box::pin(fut) + } + _ => Box::pin(async move { + let mut response = http::Response::new(tonic::body::Body::default()); + let headers = response.headers_mut(); + headers.insert( + tonic::Status::GRPC_STATUS, + (tonic::Code::Unimplemented as i32).into(), + ); + headers.insert( + http::header::CONTENT_TYPE, + tonic::metadata::GRPC_CONTENT_TYPE, + ); + Ok(response) + }), + } + } + } + impl Clone for ThreadConfigLoaderServer { + fn clone(&self) -> Self { + let inner = self.inner.clone(); + Self { + inner, + accept_compression_encodings: self.accept_compression_encodings, + send_compression_encodings: self.send_compression_encodings, + max_decoding_message_size: self.max_decoding_message_size, + max_encoding_message_size: self.max_encoding_message_size, + } + } + } + /// Generated gRPC service name + pub const SERVICE_NAME: &str = "codex.thread_config.v1.ThreadConfigLoader"; + impl tonic::server::NamedService for ThreadConfigLoaderServer { + const NAME: &'static str = SERVICE_NAME; + } +} diff --git a/codex-rs/config/src/thread_config/remote.rs b/codex-rs/config/src/thread_config/remote.rs new file mode 100644 index 0000000000..7b7feacec5 --- /dev/null +++ b/codex-rs/config/src/thread_config/remote.rs @@ -0,0 +1,523 @@ +use std::collections::BTreeMap; +use std::collections::HashMap; +use std::num::NonZeroU64; +use std::time::Duration; + +use async_trait::async_trait; +use codex_model_provider_info::ModelProviderInfo; +use codex_model_provider_info::WireApi; +use codex_protocol::config_types::ModelProviderAuthInfo; +use codex_utils_absolute_path::AbsolutePathBuf; + +use super::SessionThreadConfig; +use super::ThreadConfigContext; +use super::ThreadConfigLoadError; +use super::ThreadConfigLoadErrorCode; +use super::ThreadConfigLoader; +use super::ThreadConfigSource; +use super::UserThreadConfig; +use proto::thread_config_loader_client::ThreadConfigLoaderClient; + +#[path = "proto/codex.thread_config.v1.rs"] +mod proto; + +const REMOTE_THREAD_CONFIG_LOAD_TIMEOUT: Duration = Duration::from_secs(5); + +/// gRPC-backed [`ThreadConfigLoader`] implementation. +#[derive(Clone, Debug)] +pub struct RemoteThreadConfigLoader { + endpoint: String, +} + +impl RemoteThreadConfigLoader { + pub fn new(endpoint: impl Into) -> Self { + Self { + endpoint: endpoint.into(), + } + } + + async fn client( + &self, + ) -> Result, ThreadConfigLoadError> { + ThreadConfigLoaderClient::connect(self.endpoint.clone()) + .await + .map_err(|err| { + ThreadConfigLoadError::new( + ThreadConfigLoadErrorCode::RequestFailed, + /*status_code*/ None, + format!("failed to connect to remote thread config loader: {err}"), + ) + }) + } +} + +#[async_trait] +impl ThreadConfigLoader for RemoteThreadConfigLoader { + async fn load( + &self, + context: ThreadConfigContext, + ) -> Result, ThreadConfigLoadError> { + let response = self + .client() + .await? + .load(load_thread_config_request(context)) + .await + .map_err(remote_status_to_error)? + .into_inner(); + + response + .sources + .into_iter() + .map(thread_config_source_from_proto) + .collect() + } +} + +fn load_thread_config_request( + context: ThreadConfigContext, +) -> tonic::Request { + let mut request = tonic::Request::new(proto::LoadThreadConfigRequest { + thread_id: context.thread_id, + cwd: context.cwd.map(|cwd| cwd.to_string_lossy().into_owned()), + }); + request.set_timeout(REMOTE_THREAD_CONFIG_LOAD_TIMEOUT); + request +} + +fn remote_status_to_error(status: tonic::Status) -> ThreadConfigLoadError { + let code = match status.code() { + tonic::Code::Unauthenticated | tonic::Code::PermissionDenied => { + ThreadConfigLoadErrorCode::Auth + } + tonic::Code::DeadlineExceeded => ThreadConfigLoadErrorCode::Timeout, + tonic::Code::Ok + | tonic::Code::Cancelled + | tonic::Code::Unknown + | tonic::Code::InvalidArgument + | tonic::Code::NotFound + | tonic::Code::AlreadyExists + | tonic::Code::ResourceExhausted + | tonic::Code::FailedPrecondition + | tonic::Code::Aborted + | tonic::Code::OutOfRange + | tonic::Code::Unimplemented + | tonic::Code::Internal + | tonic::Code::Unavailable + | tonic::Code::DataLoss => ThreadConfigLoadErrorCode::RequestFailed, + }; + ThreadConfigLoadError::new( + code, + /*status_code*/ None, + format!("remote thread config request failed: {status}"), + ) +} + +fn thread_config_source_from_proto( + source: proto::ThreadConfigSource, +) -> Result { + match source.source { + Some(proto::thread_config_source::Source::Session(config)) => { + session_thread_config_from_proto(config).map(ThreadConfigSource::Session) + } + Some(proto::thread_config_source::Source::User(_)) => { + Ok(ThreadConfigSource::User(UserThreadConfig::default())) + } + None => Err(parse_error("remote thread config omitted source payload")), + } +} + +fn session_thread_config_from_proto( + config: proto::SessionThreadConfig, +) -> Result { + let model_providers = config + .model_providers + .into_iter() + .map(model_provider_from_proto) + .collect::, _>>()?; + + Ok(SessionThreadConfig { + model_provider: config.model_provider, + model_providers, + features: config.features.into_iter().collect::>(), + }) +} + +fn model_provider_from_proto( + provider: proto::ModelProvider, +) -> Result<(String, ModelProviderInfo), ThreadConfigLoadError> { + if provider.id.is_empty() { + return Err(parse_error( + "remote thread config returned model provider without an id", + )); + } + let id = provider.id; + let wire_api = match proto::WireApi::try_from(provider.wire_api) { + Ok(proto::WireApi::Responses) => WireApi::Responses, + Ok(proto::WireApi::Unspecified) => { + return Err(parse_error("remote thread config omitted wire_api")); + } + Err(_) => { + return Err(parse_error(format!( + "remote thread config returned unknown wire_api: {}", + provider.wire_api + ))); + } + }; + let info = ModelProviderInfo { + name: provider.name, + base_url: provider.base_url, + env_key: provider.env_key, + env_key_instructions: provider.env_key_instructions, + experimental_bearer_token: provider.experimental_bearer_token, + auth: provider + .auth + .map(model_provider_auth_from_proto) + .transpose()?, + aws: None, + wire_api, + query_params: provider.query_params.map(|map| map.values), + http_headers: provider.http_headers.map(|map| map.values), + env_http_headers: provider.env_http_headers.map(|map| map.values), + request_max_retries: provider.request_max_retries, + stream_max_retries: provider.stream_max_retries, + stream_idle_timeout_ms: provider.stream_idle_timeout_ms, + websocket_connect_timeout_ms: provider.websocket_connect_timeout_ms, + requires_openai_auth: provider.requires_openai_auth, + supports_websockets: provider.supports_websockets, + }; + Ok((id, info)) +} + +#[cfg(test)] +fn model_provider_to_proto( + id: impl Into, + provider: ModelProviderInfo, +) -> proto::ModelProvider { + let ModelProviderInfo { + name, + base_url, + env_key, + env_key_instructions, + experimental_bearer_token, + auth, + aws: _, + wire_api, + query_params, + http_headers, + env_http_headers, + request_max_retries, + stream_max_retries, + stream_idle_timeout_ms, + websocket_connect_timeout_ms, + requires_openai_auth, + supports_websockets, + } = provider; + + proto::ModelProvider { + id: id.into(), + name, + base_url, + env_key, + env_key_instructions, + experimental_bearer_token, + auth: auth.map(model_provider_auth_to_proto), + wire_api: proto_wire_api(wire_api).into(), + query_params: query_params.map(proto_string_map), + http_headers: http_headers.map(proto_string_map), + env_http_headers: env_http_headers.map(proto_string_map), + request_max_retries, + stream_max_retries, + stream_idle_timeout_ms, + websocket_connect_timeout_ms, + requires_openai_auth, + supports_websockets, + } +} + +fn model_provider_auth_from_proto( + auth: proto::ModelProviderAuthInfo, +) -> Result { + let timeout_ms = NonZeroU64::new(auth.timeout_ms) + .ok_or_else(|| parse_error("remote thread config returned zero auth timeout_ms"))?; + let cwd = AbsolutePathBuf::from_absolute_path_checked(&auth.cwd).map_err(|err| { + parse_error(format!( + "remote thread config returned invalid auth cwd {:?}: {err}", + auth.cwd + )) + })?; + + Ok(ModelProviderAuthInfo { + command: auth.command, + args: auth.args, + timeout_ms, + refresh_interval_ms: auth.refresh_interval_ms, + cwd, + }) +} + +#[cfg(test)] +fn model_provider_auth_to_proto(auth: ModelProviderAuthInfo) -> proto::ModelProviderAuthInfo { + let ModelProviderAuthInfo { + command, + args, + timeout_ms, + refresh_interval_ms, + cwd, + } = auth; + + proto::ModelProviderAuthInfo { + command, + args, + timeout_ms: timeout_ms.get(), + refresh_interval_ms, + cwd: cwd.to_string_lossy().into_owned(), + } +} + +#[cfg(test)] +fn proto_string_map(values: HashMap) -> proto::StringMap { + proto::StringMap { values } +} + +#[cfg(test)] +fn proto_wire_api(wire_api: WireApi) -> proto::WireApi { + match wire_api { + WireApi::Responses => proto::WireApi::Responses, + } +} + +fn parse_error(message: impl Into) -> ThreadConfigLoadError { + ThreadConfigLoadError::new( + ThreadConfigLoadErrorCode::Parse, + /*status_code*/ None, + message.into(), + ) +} + +#[cfg(test)] +mod tests { + use std::collections::BTreeMap; + use std::collections::HashMap; + use std::num::NonZeroU64; + + use codex_model_provider_info::ModelProviderInfo; + use codex_model_provider_info::WireApi; + use codex_protocol::config_types::ModelProviderAuthInfo; + use codex_utils_absolute_path::AbsolutePathBuf; + use pretty_assertions::assert_eq; + use tonic::Request; + use tonic::Response; + use tonic::Status; + use tonic::transport::Server; + + use super::proto::thread_config_loader_server; + use super::proto::thread_config_loader_server::ThreadConfigLoaderServer; + use super::*; + use crate::SessionThreadConfig; + use crate::UserThreadConfig; + + struct TestServer { + sources: Vec, + expected_cwd: String, + } + + #[tonic::async_trait] + impl thread_config_loader_server::ThreadConfigLoader for TestServer { + async fn load( + &self, + request: Request, + ) -> Result, Status> { + assert_eq!( + request.into_inner(), + proto::LoadThreadConfigRequest { + thread_id: Some("thread-1".to_string()), + cwd: Some(self.expected_cwd.clone()), + } + ); + + Ok(Response::new(proto::LoadThreadConfigResponse { + sources: self.sources.clone(), + })) + } + } + + #[tokio::test] + async fn load_thread_config_calls_remote_service() { + let cwd = workspace_dir().join("project"); + let expected_cwd = cwd.to_string_lossy().into_owned(); + let listener = tokio::net::TcpListener::bind("127.0.0.1:0") + .await + .expect("bind test server"); + let addr = listener.local_addr().expect("test server addr"); + let (shutdown_tx, shutdown_rx) = tokio::sync::oneshot::channel(); + let server = tokio::spawn(async move { + Server::builder() + .add_service(ThreadConfigLoaderServer::new(TestServer { + sources: proto_sources(), + expected_cwd, + })) + .serve_with_incoming_shutdown( + tokio_stream::wrappers::TcpListenerStream::new(listener), + async { + let _ = shutdown_rx.await; + }, + ) + .await + }); + + let loader = RemoteThreadConfigLoader::new(format!("http://{addr}")); + let loaded = loader + .load(ThreadConfigContext { + thread_id: Some("thread-1".to_string()), + cwd: Some(cwd), + }) + .await; + + let _ = shutdown_tx.send(()); + server.await.expect("join server").expect("server"); + + assert_eq!(loaded.expect("load thread config"), expected_sources()); + } + + #[test] + fn load_thread_config_request_sets_timeout() { + let request = load_thread_config_request(ThreadConfigContext::default()); + + assert_eq!( + request + .metadata() + .get("grpc-timeout") + .and_then(|value| value.to_str().ok()), + Some("5000000u") + ); + } + + #[test] + fn model_provider_proto_roundtrips_through_domain_type() { + let expected = expected_provider(); + let proto = model_provider_to_proto("local", expected.clone()); + let (id, actual) = model_provider_from_proto(proto).expect("model provider from proto"); + + assert_eq!(id, "local"); + assert_eq!(actual, expected); + } + + fn proto_sources() -> Vec { + let workspace_cwd = workspace_dir().to_string_lossy().into_owned(); + vec![ + proto::ThreadConfigSource { + source: Some(proto::thread_config_source::Source::Session( + proto::SessionThreadConfig { + model_provider: Some("local".to_string()), + model_providers: vec![proto::ModelProvider { + id: "local".to_string(), + name: "Local".to_string(), + base_url: Some("http://127.0.0.1:8061/api/codex".to_string()), + env_key: None, + env_key_instructions: None, + experimental_bearer_token: None, + auth: Some(proto::ModelProviderAuthInfo { + command: "token-helper".to_string(), + args: vec!["--json".to_string()], + timeout_ms: 5_000, + refresh_interval_ms: 300_000, + cwd: workspace_cwd, + }), + wire_api: proto::WireApi::Responses.into(), + query_params: Some(proto::StringMap { + values: HashMap::from([( + "api-version".to_string(), + "2026-04-16".to_string(), + )]), + }), + http_headers: Some(proto::StringMap { + values: HashMap::from([( + "X-Test".to_string(), + "enabled".to_string(), + )]), + }), + env_http_headers: Some(proto::StringMap { + values: HashMap::from([( + "X-Env".to_string(), + "LOCAL_HEADER".to_string(), + )]), + }), + request_max_retries: Some(7), + stream_max_retries: Some(8), + stream_idle_timeout_ms: Some(9_000), + websocket_connect_timeout_ms: Some(10_000), + requires_openai_auth: false, + supports_websockets: true, + }], + features: HashMap::from([ + ("plugins".to_string(), false), + ("tools".to_string(), true), + ]), + }, + )), + }, + proto::ThreadConfigSource { + source: Some(proto::thread_config_source::Source::User( + proto::UserThreadConfig {}, + )), + }, + ] + } + + fn expected_sources() -> Vec { + vec![ + ThreadConfigSource::Session(SessionThreadConfig { + model_provider: Some("local".to_string()), + model_providers: HashMap::from([("local".to_string(), expected_provider())]), + features: BTreeMap::from([ + ("plugins".to_string(), false), + ("tools".to_string(), true), + ]), + }), + ThreadConfigSource::User(UserThreadConfig::default()), + ] + } + + fn expected_provider() -> ModelProviderInfo { + ModelProviderInfo { + name: "Local".to_string(), + base_url: Some("http://127.0.0.1:8061/api/codex".to_string()), + env_key: None, + env_key_instructions: None, + experimental_bearer_token: None, + auth: Some(ModelProviderAuthInfo { + command: "token-helper".to_string(), + args: vec!["--json".to_string()], + timeout_ms: NonZeroU64::new(5_000).expect("non-zero timeout"), + refresh_interval_ms: 300_000, + cwd: workspace_dir(), + }), + wire_api: WireApi::Responses, + query_params: Some(HashMap::from([( + "api-version".to_string(), + "2026-04-16".to_string(), + )])), + http_headers: Some(HashMap::from([( + "X-Test".to_string(), + "enabled".to_string(), + )])), + env_http_headers: Some(HashMap::from([( + "X-Env".to_string(), + "LOCAL_HEADER".to_string(), + )])), + request_max_retries: Some(7), + stream_max_retries: Some(8), + stream_idle_timeout_ms: Some(9_000), + websocket_connect_timeout_ms: Some(10_000), + requires_openai_auth: false, + supports_websockets: true, + aws: None, + } + } + + fn workspace_dir() -> AbsolutePathBuf { + AbsolutePathBuf::current_dir() + .expect("current dir") + .join("workspace") + } +} diff --git a/codex-rs/core/src/codex_delegate.rs b/codex-rs/core/src/codex_delegate.rs index 631c839743..1a30d3263f 100644 --- a/codex-rs/core/src/codex_delegate.rs +++ b/codex-rs/core/src/codex_delegate.rs @@ -95,6 +95,7 @@ pub(crate) async fn run_codex_thread_interactive( inherited_rollout_trace: codex_rollout_trace::RolloutTraceRecorder::disabled(), parent_trace: None, analytics_events_client: Some(parent_session.services.analytics_events_client.clone()), + thread_store: Arc::clone(&parent_session.services.thread_store), })) .await?; if parent_session.enabled(codex_features::Feature::GeneralAnalytics) { diff --git a/codex-rs/core/src/config/config_tests.rs b/codex-rs/core/src/config/config_tests.rs index 820cd7e8ba..3dddde05d1 100644 --- a/codex-rs/core/src/config/config_tests.rs +++ b/codex-rs/core/src/config/config_tests.rs @@ -6610,6 +6610,28 @@ async fn feature_requirements_normalize_effective_feature_values() -> std::io::R Ok(()) } +#[tokio::test] +async fn feature_requirements_auto_review_disables_guardian_approval() -> std::io::Result<()> { + let codex_home = TempDir::new()?; + + let config = ConfigBuilder::without_managed_config_for_tests() + .codex_home(codex_home.path().to_path_buf()) + .cloud_requirements(CloudRequirementsLoader::new(async { + Ok(Some(crate::config_loader::ConfigRequirementsToml { + feature_requirements: Some(crate::config_loader::FeatureRequirementsToml { + entries: BTreeMap::from([("auto_review".to_string(), false)]), + }), + ..Default::default() + })) + })) + .build() + .await?; + + assert!(!config.features.enabled(Feature::GuardianApproval)); + + Ok(()) +} + #[tokio::test] async fn browser_feature_requirements_are_valid() -> std::io::Result<()> { let codex_home = TempDir::new()?; diff --git a/codex-rs/core/src/config/managed_features.rs b/codex-rs/core/src/config/managed_features.rs index 2ebd4749a3..fd3032920d 100644 --- a/codex-rs/core/src/config/managed_features.rs +++ b/codex-rs/core/src/config/managed_features.rs @@ -199,6 +199,11 @@ fn parse_feature_requirements( ) -> BTreeMap { let mut pinned_features = BTreeMap::new(); for (key, enabled) in feature_requirements.entries { + if key == "auto_review" { + pinned_features.insert(Feature::GuardianApproval, enabled); + continue; + } + if let Some(feature) = canonical_feature_for_key(&key) { pinned_features.insert(feature, enabled); continue; diff --git a/codex-rs/core/src/config/mod.rs b/codex-rs/core/src/config/mod.rs index 5f77a6c5e6..d30ed79a6e 100644 --- a/codex-rs/core/src/config/mod.rs +++ b/codex-rs/core/src/config/mod.rs @@ -1948,6 +1948,13 @@ impl Config { let history = cfg.history.unwrap_or_default(); + let agent_max_threads_from_config = cfg.agents.as_ref().and_then(|agents| agents.max_threads); + if features.enabled(Feature::MultiAgentV2) && agent_max_threads_from_config.is_some() { + return Err(std::io::Error::new( + std::io::ErrorKind::InvalidInput, + "agents.max_threads cannot be set when multi_agent_v2 is enabled", + )); + } let agent_max_threads = cfg .agents .as_ref() diff --git a/codex-rs/core/src/context/mod.rs b/codex-rs/core/src/context/mod.rs index bbc5bcacf1..848a4f7f0c 100644 --- a/codex-rs/core/src/context/mod.rs +++ b/codex-rs/core/src/context/mod.rs @@ -20,7 +20,6 @@ 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; @@ -50,7 +49,6 @@ 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/spawn_agent_instructions.rs b/codex-rs/core/src/context/spawn_agent_instructions.rs deleted file mode 100644 index f8a3f4175e..0000000000 --- a/codex-rs/core/src/context/spawn_agent_instructions.rs +++ /dev/null @@ -1,14 +0,0 @@ -use super::ContextualUserFragment; - -#[derive(Debug, Clone, Copy, PartialEq, Eq)] -pub(crate) struct SpawnAgentInstructions; - -impl ContextualUserFragment for SpawnAgentInstructions { - const ROLE: &'static str = "developer"; - const START_MARKER: &'static str = ""; - const END_MARKER: &'static str = ""; - - fn body(&self) -> String { - "\nYou are a newly spawned agent in a team of agents collaborating to complete a task. You can spawn sub-agents to handle subtasks, and those sub-agents can spawn their own sub-agents. You are responsible for returning the response to your assigned task in the final channel. When you give your response, the contents of your response in the final channel will be immediately delivered back to your parent agent. The prior conversation history was forked from your parent agent. Treat the next user message as your assigned task, and use the forked history only as background context.\n".to_string() - } -} diff --git a/codex-rs/core/src/guardian/review_session.rs b/codex-rs/core/src/guardian/review_session.rs index 50123ef503..96778d0dee 100644 --- a/codex-rs/core/src/guardian/review_session.rs +++ b/codex-rs/core/src/guardian/review_session.rs @@ -751,7 +751,7 @@ async fn load_rollout_items_for_fork( session: &Session, ) -> anyhow::Result>> { session.flush_rollout().await?; - let Some(rollout_path) = session.current_rollout_path().await else { + let Some(rollout_path) = session.current_rollout_path().await? else { return Ok(None); }; let history = RolloutRecorder::get_rollout_history(rollout_path.as_path()).await?; diff --git a/codex-rs/core/src/hook_runtime.rs b/codex-rs/core/src/hook_runtime.rs index c647d146d4..db47688685 100644 --- a/codex-rs/core/src/hook_runtime.rs +++ b/codex-rs/core/src/hook_runtime.rs @@ -35,6 +35,7 @@ use crate::context::HookAdditionalContext; use crate::event_mapping::parse_turn_item; use crate::session::session::Session; use crate::session::turn_context::TurnContext; +use crate::tools::hook_names::HookToolName; use crate::tools::sandboxing::PermissionRequestPayload; pub(crate) struct HookRuntimeOutcome { @@ -137,9 +138,8 @@ pub(crate) async fn run_pre_tool_use_hooks( sess: &Arc, turn_context: &Arc, tool_use_id: String, - tool_name: String, - matcher_aliases: Vec, - command: String, + tool_name: &HookToolName, + tool_input: &Value, ) -> Option { let request = PreToolUseRequest { session_id: sess.conversation_id, @@ -148,10 +148,10 @@ pub(crate) async fn run_pre_tool_use_hooks( transcript_path: sess.hook_transcript_path().await, model: turn_context.model_info.slug.clone(), permission_mode: hook_permission_mode(turn_context), - tool_name, - matcher_aliases, + tool_name: tool_name.name().to_string(), + matcher_aliases: tool_name.matcher_aliases().to_vec(), tool_use_id, - command, + tool_input: tool_input.clone(), }; let preview_runs = sess.hooks().preview_pre_tool_use(&request); emit_hook_started_events(sess, turn_context, preview_runs).await; @@ -163,7 +163,22 @@ pub(crate) async fn run_pre_tool_use_hooks( } = sess.hooks().run_pre_tool_use(request).await; emit_hook_completed_events(sess, turn_context, hook_events).await; - if should_block { block_reason } else { None } + if should_block { + block_reason.map(|reason| { + if (tool_name.name() == "Bash" || tool_name.name() == "apply_patch") + && let Some(command) = tool_input.get("command").and_then(Value::as_str) + { + format!("Command blocked by PreToolUse hook: {reason}. Command: {command}") + } else { + format!( + "Tool call blocked by PreToolUse hook: {reason}. Tool: {}", + tool_name.name() + ) + } + }) + } else { + None + } } // PermissionRequest hooks share the same preview/start/completed event flow as @@ -185,8 +200,7 @@ pub(crate) async fn run_permission_request_hooks( tool_name: payload.tool_name.name().to_string(), matcher_aliases: payload.tool_name.matcher_aliases().to_vec(), run_id_suffix: run_id_suffix.to_string(), - command: payload.command, - description: payload.description, + tool_input: payload.tool_input, }; let preview_runs = sess.hooks().preview_permission_request(&request); emit_hook_started_events(sess, turn_context, preview_runs).await; @@ -202,7 +216,7 @@ pub(crate) async fn run_permission_request_hooks( /// Runs matching `PostToolUse` hooks after a tool has produced a successful output. /// -/// The `tool_name`, matcher aliases, `command`, and `tool_response` values are +/// The `tool_name`, matcher aliases, `tool_input`, and `tool_response` values are /// already adapted by the tool handler into the stable hook contract. Passing /// raw internal tool data here would leak implementation details into user hook /// matchers and hook logs. @@ -212,7 +226,7 @@ pub(crate) async fn run_post_tool_use_hooks( tool_use_id: String, tool_name: String, matcher_aliases: Vec, - command: String, + tool_input: Value, tool_response: Value, ) -> PostToolUseOutcome { let request = PostToolUseRequest { @@ -225,7 +239,7 @@ pub(crate) async fn run_post_tool_use_hooks( tool_name, matcher_aliases, tool_use_id, - command, + tool_input, tool_response, }; let preview_runs = sess.hooks().preview_post_tool_use(&request); diff --git a/codex-rs/core/src/mcp_tool_call.rs b/codex-rs/core/src/mcp_tool_call.rs index 82bbc7ca4a..a9a0d6eb5c 100644 --- a/codex-rs/core/src/mcp_tool_call.rs +++ b/codex-rs/core/src/mcp_tool_call.rs @@ -25,16 +25,20 @@ use crate::guardian::guardian_timeout_message; use crate::guardian::new_guardian_review_id; use crate::guardian::review_approval_request; use crate::guardian::routes_approval_to_guardian; +use crate::hook_runtime::run_permission_request_hooks; use crate::mcp_openai_file::rewrite_mcp_tool_arguments_for_openai_files; use crate::mcp_tool_approval_templates::RenderedMcpToolApprovalParam; use crate::mcp_tool_approval_templates::render_mcp_tool_approval_template; use crate::session::session::Session; use crate::session::turn_context::TurnContext; +use crate::tools::hook_names::HookToolName; +use crate::tools::sandboxing::PermissionRequestPayload; use codex_analytics::AppInvocation; use codex_analytics::InvocationType; use codex_analytics::build_track_events_context; use codex_config::types::AppToolApproval; use codex_features::Feature; +use codex_hooks::PermissionRequestDecision; use codex_mcp::CODEX_APPS_MCP_SERVER_NAME; use codex_mcp::SandboxState; use codex_mcp::declared_openai_file_input_param_names; @@ -59,6 +63,7 @@ use codex_utils_absolute_path::AbsolutePathBuf; use rmcp::model::ToolAnnotations; use serde::Deserialize; use serde::Serialize; +use serde_json::Value as JsonValue; use std::sync::Arc; use toml_edit::value; use tracing::Instrument; @@ -77,8 +82,9 @@ pub(crate) async fn handle_mcp_tool_call( call_id: String, server: String, tool_name: String, + hook_tool_name: String, arguments: String, -) -> CallToolResult { +) -> HandledMcpToolCall { // Parse the `arguments` as JSON. An empty string is OK, but invalid JSON // is not. let arguments_value = if arguments.trim().is_empty() { @@ -88,7 +94,10 @@ pub(crate) async fn handle_mcp_tool_call( Ok(value) => Some(value), Err(e) => { error!("failed to parse tool call arguments: {e}"); - return CallToolResult::from_error_text(format!("err: {e}")); + return HandledMcpToolCall { + result: CallToolResult::from_error_text(format!("err: {e}")), + tool_input: JsonValue::Object(serde_json::Map::new()), + }; } } }; @@ -144,7 +153,11 @@ pub(crate) async fn handle_mcp_tool_call( /*inc*/ 1, &[("status", status)], ); - return CallToolResult::from_result(result); + return HandledMcpToolCall { + result: CallToolResult::from_result(result), + tool_input: arguments_value + .unwrap_or_else(|| JsonValue::Object(serde_json::Map::new())), + }; } let request_meta = build_mcp_tool_call_request_meta(turn_context.as_ref(), &server, metadata.as_ref()); @@ -154,13 +167,6 @@ pub(crate) async fn handle_mcp_tool_call( let connector_name = metadata .as_ref() .and_then(|metadata| metadata.connector_name.clone()); - let server_origin = sess - .services - .mcp_connection_manager - .read() - .await - .server_origin(&server) - .map(str::to_string); let tool_call_begin_event = EventMsg::McpToolCallBegin(McpToolCallBeginEvent { call_id: call_id.clone(), @@ -174,115 +180,64 @@ pub(crate) async fn handle_mcp_tool_call( turn_context, &call_id, &invocation, + &hook_tool_name, metadata.as_ref(), approval_mode, ) .await { - let (result, call_duration) = match decision { + let result = match decision { McpToolApprovalDecision::Accept | McpToolApprovalDecision::AcceptForSession | McpToolApprovalDecision::AcceptAndRemember => { - maybe_mark_thread_memory_mode_polluted(sess.as_ref(), turn_context.as_ref()).await; - - let start = Instant::now(); - let result = async { - execute_mcp_tool_call( - sess.as_ref(), - turn_context.as_ref(), - &server, - &tool_name, - arguments_value.clone(), - metadata.as_ref(), - request_meta.clone(), - ) - .await - } - .instrument(mcp_tool_call_span( + return handle_approved_mcp_tool_call( sess.as_ref(), turn_context.as_ref(), - McpToolCallSpanFields { - server_name: &server, - tool_name: &tool_name, - call_id: &call_id, - server_origin: server_origin.as_deref(), - connector_id: connector_id.as_deref(), - connector_name: connector_name.as_deref(), - }, - )) - .await; - if let Err(error) = &result { - tracing::warn!("MCP tool call error: {error:?}"); - } - let duration = start.elapsed(); - let tool_call_end_event = EventMsg::McpToolCallEnd(McpToolCallEndEvent { - call_id: call_id.clone(), + &call_id, invocation, - mcp_app_resource_uri: mcp_app_resource_uri.clone(), - duration, - result: result.clone(), - }); - notify_mcp_tool_call_event( - sess.as_ref(), - turn_context.as_ref(), - tool_call_end_event.clone(), + metadata.as_ref(), + request_meta, + mcp_app_resource_uri, ) .await; - maybe_track_codex_app_used( - sess.as_ref(), - turn_context.as_ref(), - &server, - &tool_name, - ) - .await; - (result, Some(duration)) } McpToolApprovalDecision::Decline { message } => { let message = message.unwrap_or_else(|| "user rejected MCP tool call".to_string()); - ( - notify_mcp_tool_call_skip( - sess.as_ref(), - turn_context.as_ref(), - &call_id, - invocation, - mcp_app_resource_uri.clone(), - message, - /*already_started*/ true, - ) - .await, - None, + notify_mcp_tool_call_skip( + sess.as_ref(), + turn_context.as_ref(), + &call_id, + invocation, + mcp_app_resource_uri.clone(), + message, + /*already_started*/ true, ) + .await } McpToolApprovalDecision::Cancel => { let message = "user cancelled MCP tool call".to_string(); - ( - notify_mcp_tool_call_skip( - sess.as_ref(), - turn_context.as_ref(), - &call_id, - invocation, - mcp_app_resource_uri.clone(), - message, - /*already_started*/ true, - ) - .await, - None, + notify_mcp_tool_call_skip( + sess.as_ref(), + turn_context.as_ref(), + &call_id, + invocation, + mcp_app_resource_uri.clone(), + message, + /*already_started*/ true, ) + .await } McpToolApprovalDecision::BlockedBySafetyMonitor(message) => { - ( - notify_mcp_tool_call_skip( - sess.as_ref(), - turn_context.as_ref(), - &call_id, - invocation, - mcp_app_resource_uri.clone(), - message, - /*already_started*/ true, - ) - .await, - None, + notify_mcp_tool_call_skip( + sess.as_ref(), + turn_context.as_ref(), + &call_id, + invocation, + mcp_app_resource_uri.clone(), + message, + /*already_started*/ true, ) + .await } }; @@ -293,37 +248,93 @@ pub(crate) async fn handle_mcp_tool_call( &tool_name, connector_id.as_deref(), connector_name.as_deref(), - call_duration, + /*duration*/ None, ); - return CallToolResult::from_result(result); + return HandledMcpToolCall { + result: CallToolResult::from_result(result), + tool_input: arguments_value + .unwrap_or_else(|| JsonValue::Object(serde_json::Map::new())), + }; } - maybe_mark_thread_memory_mode_polluted(sess.as_ref(), turn_context.as_ref()).await; + handle_approved_mcp_tool_call( + sess.as_ref(), + turn_context.as_ref(), + &call_id, + invocation, + metadata.as_ref(), + request_meta, + mcp_app_resource_uri, + ) + .await +} + +pub(crate) struct HandledMcpToolCall { + pub(crate) result: CallToolResult, + pub(crate) tool_input: JsonValue, +} + +async fn handle_approved_mcp_tool_call( + sess: &Session, + turn_context: &TurnContext, + call_id: &str, + invocation: McpInvocation, + metadata: Option<&McpToolApprovalMetadata>, + request_meta: Option, + mcp_app_resource_uri: Option, +) -> HandledMcpToolCall { + maybe_mark_thread_memory_mode_polluted(sess, turn_context).await; + + let server = invocation.server.clone(); + let tool_name = invocation.tool.clone(); + let arguments_value = invocation.arguments.clone(); + let connector_id = metadata.and_then(|metadata| metadata.connector_id.as_deref()); + let connector_name = metadata.and_then(|metadata| metadata.connector_name.as_deref()); + let server_origin = sess + .services + .mcp_connection_manager + .read() + .await + .server_origin(&server) + .map(str::to_string); let start = Instant::now(); + let rewrite = rewrite_mcp_tool_arguments_for_openai_files( + sess, + turn_context, + arguments_value.clone(), + metadata.and_then(|metadata| metadata.openai_file_input_params.as_deref()), + ) + .await; + let tool_input = match &rewrite { + Ok(Some(rewritten_arguments)) => rewritten_arguments.clone(), + Ok(None) | Err(_) => arguments_value + .clone() + .unwrap_or_else(|| JsonValue::Object(serde_json::Map::new())), + }; let result = async { + let rewritten_arguments = rewrite?; execute_mcp_tool_call( - sess.as_ref(), - turn_context.as_ref(), + sess, + turn_context, &server, &tool_name, - arguments_value.clone(), - metadata.as_ref(), + rewritten_arguments, request_meta, ) .await } .instrument(mcp_tool_call_span( - sess.as_ref(), - turn_context.as_ref(), + sess, + turn_context, McpToolCallSpanFields { server_name: &server, tool_name: &tool_name, - call_id: &call_id, + call_id, server_origin: server_origin.as_deref(), - connector_id: connector_id.as_deref(), - connector_name: connector_name.as_deref(), + connector_id, + connector_name, }, )) .await; @@ -332,32 +343,29 @@ pub(crate) async fn handle_mcp_tool_call( } let duration = start.elapsed(); let tool_call_end_event = EventMsg::McpToolCallEnd(McpToolCallEndEvent { - call_id: call_id.clone(), + call_id: call_id.to_string(), invocation, mcp_app_resource_uri, duration, result: result.clone(), }); - - notify_mcp_tool_call_event( - sess.as_ref(), - turn_context.as_ref(), - tool_call_end_event.clone(), - ) - .await; - maybe_track_codex_app_used(sess.as_ref(), turn_context.as_ref(), &server, &tool_name).await; + notify_mcp_tool_call_event(sess, turn_context, tool_call_end_event.clone()).await; + maybe_track_codex_app_used(sess, turn_context, &server, &tool_name).await; let status = if result.is_ok() { "ok" } else { "error" }; emit_mcp_call_metrics( - turn_context.as_ref(), + turn_context, status, &tool_name, - connector_id.as_deref(), - connector_name.as_deref(), + connector_id, + connector_name, Some(duration), ); - CallToolResult::from_result(result) + HandledMcpToolCall { + result: CallToolResult::from_result(result), + tool_input, + } } fn emit_mcp_call_metrics( @@ -466,17 +474,9 @@ async fn execute_mcp_tool_call( turn_context: &TurnContext, server: &str, tool_name: &str, - arguments_value: Option, - metadata: Option<&McpToolApprovalMetadata>, - request_meta: Option, + rewritten_arguments: Option, + request_meta: Option, ) -> Result { - let rewritten_arguments = rewrite_mcp_tool_arguments_for_openai_files( - sess, - turn_context, - arguments_value, - 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 = @@ -814,6 +814,7 @@ async fn maybe_request_mcp_tool_approval( turn_context: &Arc, call_id: &str, invocation: &McpInvocation, + hook_tool_name: &str, metadata: Option<&McpToolApprovalMetadata>, approval_mode: AppToolApproval, ) -> Option { @@ -863,6 +864,32 @@ async fn maybe_request_mcp_tool_approval( { return Some(McpToolApprovalDecision::Accept); } + + match run_permission_request_hooks( + sess, + turn_context, + call_id, + PermissionRequestPayload { + tool_name: HookToolName::new(hook_tool_name), + tool_input: invocation + .arguments + .clone() + .unwrap_or_else(|| serde_json::Value::Object(serde_json::Map::new())), + }, + ) + .await + { + Some(PermissionRequestDecision::Allow) => { + return Some(McpToolApprovalDecision::Accept); + } + Some(PermissionRequestDecision::Deny { message }) => { + return Some(McpToolApprovalDecision::Decline { + message: Some(message), + }); + } + None => {} + } + let tool_call_mcp_elicitation_enabled = turn_context .config .features diff --git a/codex-rs/core/src/mcp_tool_call_tests.rs b/codex-rs/core/src/mcp_tool_call_tests.rs index ed355c47b4..522686446b 100644 --- a/codex-rs/core/src/mcp_tool_call_tests.rs +++ b/codex-rs/core/src/mcp_tool_call_tests.rs @@ -12,6 +12,8 @@ use codex_config::types::ApprovalsReviewer; use codex_config::types::AppsConfigToml; use codex_config::types::McpServerConfig; use codex_config::types::McpServerToolConfig; +use codex_hooks::Hooks; +use codex_hooks::HooksConfig; use codex_model_provider::create_model_provider; use codex_protocol::protocol::AskForApproval; use codex_protocol::protocol::SandboxPolicy; @@ -76,6 +78,81 @@ fn prompt_options( } } +fn install_mcp_permission_request_hook( + session: &mut Session, + turn_context: &TurnContext, + matcher: &str, + hook_output: &serde_json::Value, +) -> std::path::PathBuf { + let script_path = turn_context + .config + .codex_home + .join("mcp_permission_request_hook.py"); + let log_path = turn_context + .config + .codex_home + .join("mcp_permission_request_hook_log.jsonl"); + let hook_output = hook_output.to_string(); + std::fs::create_dir_all(&turn_context.config.codex_home) + .expect("create codex home for MCP permission hook"); + let script = format!( + r#"import json +from pathlib import Path +import sys + +payload = json.load(sys.stdin) +with Path(r"{log_path}").open("a", encoding="utf-8") as handle: + handle.write(json.dumps(payload) + "\n") + +print({hook_output:?}) +"#, + log_path = log_path.display(), + hook_output = hook_output, + ); + + std::fs::write(&script_path, script).expect("write MCP permission hook script"); + let python = if cfg!(windows) { "python" } else { "python3" }; + let script_path_arg = if cfg!(windows) { + script_path.display().to_string() + } else { + format!( + "'{}'", + script_path.display().to_string().replace('\'', "'\\''") + ) + }; + std::fs::write( + turn_context.config.codex_home.join("hooks.json"), + serde_json::json!({ + "hooks": { + "PermissionRequest": [{ + "matcher": matcher, + "hooks": [{ + "type": "command", + "command": format!("{python} {script_path_arg}"), + "timeout_sec": 5, + }] + }] + } + }) + .to_string(), + ) + .expect("write hooks.json"); + + session.services.hooks = Hooks::new(HooksConfig { + feature_enabled: true, + config_layer_stack: Some(turn_context.config.config_layer_stack.clone()), + shell_program: (!cfg!(windows)).then_some("/bin/sh".to_string()), + shell_args: if cfg!(windows) { + Vec::new() + } else { + vec!["-c".to_string()] + }, + ..HooksConfig::default() + }); + + log_path.to_path_buf() +} + #[test] fn mcp_app_resource_uri_reads_known_tool_meta_keys() { let nested = serde_json::json!({ @@ -1381,6 +1458,7 @@ async fn approve_mode_skips_when_annotations_do_not_require_approval() { &turn_context, "call-1", &invocation, + "mcp__test__tool", Some(&metadata), AppToolApproval::Approve, ) @@ -1453,6 +1531,7 @@ async fn guardian_mode_skips_auto_when_annotations_do_not_require_approval() { &turn_context, "call-guardian", &invocation, + "mcp__test__tool", Some(&metadata), AppToolApproval::Auto, ) @@ -1461,6 +1540,203 @@ async fn guardian_mode_skips_auto_when_annotations_do_not_require_approval() { assert_eq!(decision, None); } +#[tokio::test] +async fn permission_request_hook_allows_mcp_tool_call() { + let (mut session, turn_context) = make_session_and_context().await; + let log_path = install_mcp_permission_request_hook( + &mut session, + &turn_context, + "mcp__memory__.*", + &serde_json::json!({ + "hookSpecificOutput": { + "hookEventName": "PermissionRequest", + "decision": { "behavior": "allow" } + } + }), + ); + let session = Arc::new(session); + let turn_context = Arc::new(turn_context); + let invocation = McpInvocation { + server: "memory".to_string(), + tool: "create_entities".to_string(), + arguments: Some(serde_json::json!({ + "entities": [{ + "name": "Ada", + "entityType": "person" + }] + })), + }; + let metadata = McpToolApprovalMetadata { + annotations: Some(annotations( + Some(false), + Some(true), + /*open_world*/ None, + )), + connector_id: None, + connector_name: None, + connector_description: None, + tool_title: Some("Create entities".to_string()), + tool_description: None, + mcp_app_resource_uri: None, + codex_apps_meta: None, + openai_file_input_params: None, + }; + + let decision = maybe_request_mcp_tool_approval( + &session, + &turn_context, + "call-mcp-hook", + &invocation, + "mcp__memory__create_entities", + Some(&metadata), + AppToolApproval::Auto, + ) + .await; + + assert_eq!(decision, Some(McpToolApprovalDecision::Accept)); + let log = std::fs::read_to_string(log_path).expect("read MCP permission hook log"); + let inputs = log + .lines() + .map(|line| serde_json::from_str::(line).expect("parse hook input")) + .collect::>(); + assert_eq!( + inputs, + vec![serde_json::json!({ + "session_id": session.conversation_id, + "turn_id": "turn_id", + "cwd": turn_context.cwd, + "transcript_path": null, + "model": turn_context.model_info.slug, + "permission_mode": "default", + "tool_name": "mcp__memory__create_entities", + "hook_event_name": "PermissionRequest", + "tool_input": { + "entities": [{ + "name": "Ada", + "entityType": "person" + }] + } + })] + ); +} + +#[tokio::test] +async fn permission_request_hook_uses_hook_tool_name_without_metadata() { + let (mut session, turn_context) = make_session_and_context().await; + let log_path = install_mcp_permission_request_hook( + &mut session, + &turn_context, + "mcp__memory__.*", + &serde_json::json!({ + "hookSpecificOutput": { + "hookEventName": "PermissionRequest", + "decision": { "behavior": "allow" } + } + }), + ); + let session = Arc::new(session); + let turn_context = Arc::new(turn_context); + let invocation = McpInvocation { + server: "memory".to_string(), + tool: "create_entities".to_string(), + arguments: Some(serde_json::json!({ "entities": [] })), + }; + + let decision = maybe_request_mcp_tool_approval( + &session, + &turn_context, + "call-mcp-hook-no-metadata", + &invocation, + "mcp__memory__create_entities", + /*metadata*/ None, + AppToolApproval::Auto, + ) + .await; + + assert_eq!(decision, Some(McpToolApprovalDecision::Accept)); + let log = std::fs::read_to_string(log_path).expect("read MCP permission hook log"); + let inputs = log + .lines() + .map(|line| serde_json::from_str::(line).expect("parse hook input")) + .collect::>(); + assert_eq!( + inputs, + vec![serde_json::json!({ + "session_id": session.conversation_id, + "turn_id": "turn_id", + "cwd": turn_context.cwd, + "transcript_path": null, + "model": turn_context.model_info.slug, + "permission_mode": "default", + "tool_name": "mcp__memory__create_entities", + "hook_event_name": "PermissionRequest", + "tool_input": { "entities": [] } + })] + ); +} + +#[tokio::test] +async fn permission_request_hook_runs_after_remembered_mcp_approval() { + let (mut session, turn_context) = make_session_and_context().await; + let log_path = install_mcp_permission_request_hook( + &mut session, + &turn_context, + "mcp__memory__.*", + &serde_json::json!({ + "hookSpecificOutput": { + "hookEventName": "PermissionRequest", + "decision": { + "behavior": "deny", + "message": "should be skipped" + } + } + }), + ); + let invocation = McpInvocation { + server: "memory".to_string(), + tool: "create_entities".to_string(), + arguments: Some(serde_json::json!({ "entities": [] })), + }; + let metadata = McpToolApprovalMetadata { + annotations: Some(annotations( + Some(false), + Some(true), + /*open_world*/ None, + )), + connector_id: None, + connector_name: None, + connector_description: None, + tool_title: Some("Create entities".to_string()), + tool_description: None, + mcp_app_resource_uri: None, + codex_apps_meta: None, + openai_file_input_params: None, + }; + let remembered_key = + session_mcp_tool_approval_key(&invocation, Some(&metadata), AppToolApproval::Auto) + .expect("memory MCP tool should support session approval"); + remember_mcp_tool_approval(&session, remembered_key).await; + + let session = Arc::new(session); + let turn_context = Arc::new(turn_context); + let decision = maybe_request_mcp_tool_approval( + &session, + &turn_context, + "call-mcp-remembered", + &invocation, + "mcp__memory__create_entities", + Some(&metadata), + AppToolApproval::Auto, + ) + .await; + + assert_eq!(decision, Some(McpToolApprovalDecision::Accept)); + assert!( + !log_path.exists(), + "remembered approval should skip PermissionRequest hooks" + ); +} + #[tokio::test] async fn guardian_mode_mcp_denial_returns_rationale_message() { let server = start_mock_server().await; @@ -1528,6 +1804,7 @@ async fn guardian_mode_mcp_denial_returns_rationale_message() { &turn_context, "call-guardian-deny", &invocation, + "mcp__test__tool", Some(&metadata), AppToolApproval::Auto, ) @@ -1584,6 +1861,7 @@ async fn prompt_mode_waits_for_approval_when_annotations_do_not_require_approval &turn_context, "call-prompt", &invocation, + "mcp__test__tool", Some(&metadata), AppToolApproval::Prompt, ) @@ -1658,6 +1936,7 @@ async fn approve_mode_blocks_when_arc_returns_interrupt_for_model() { &turn_context, "call-2", &invocation, + "mcp__test__tool", Some(&metadata), AppToolApproval::Approve, ) @@ -1729,6 +2008,7 @@ async fn custom_approve_mode_blocks_when_arc_returns_interrupt_for_model() { &turn_context, "call-2-custom", &invocation, + "mcp__test__tool", Some(&metadata), AppToolApproval::Approve, ) @@ -1800,6 +2080,7 @@ async fn approve_mode_blocks_when_arc_returns_interrupt_without_annotations() { &turn_context, "call-3", &invocation, + "mcp__test__tool", Some(&metadata), AppToolApproval::Approve, ) @@ -1884,6 +2165,7 @@ async fn full_access_mode_skips_arc_monitor_for_all_approval_modes() { &turn_context, "call-2", &invocation, + "mcp__test__tool", Some(&metadata), approval_mode, ) @@ -1985,6 +2267,7 @@ async fn approve_mode_routes_arc_ask_user_to_guardian_when_guardian_reviewer_is_ &turn_context, "call-3", &invocation, + "mcp__test__tool", Some(&metadata), AppToolApproval::Approve, ) diff --git a/codex-rs/core/src/realtime_context.rs b/codex-rs/core/src/realtime_context.rs index bc769d29f0..f9bb49a425 100644 --- a/codex-rs/core/src/realtime_context.rs +++ b/codex-rs/core/src/realtime_context.rs @@ -9,7 +9,6 @@ use codex_thread_store::ListThreadsParams; use codex_thread_store::SortDirection; use codex_thread_store::StoredThread; use codex_thread_store::ThreadSortKey; -use codex_thread_store::ThreadStore; use codex_utils_absolute_path::AbsolutePathBuf; use codex_utils_output_truncation::TruncationPolicy; use codex_utils_output_truncation::truncate_text; diff --git a/codex-rs/core/src/rollout.rs b/codex-rs/core/src/rollout.rs index 2405b83fc6..4d282cdb55 100644 --- a/codex-rs/core/src/rollout.rs +++ b/codex-rs/core/src/rollout.rs @@ -50,12 +50,7 @@ pub(crate) mod list { pub use codex_rollout::find_thread_path_by_id_str; } -pub(crate) mod metadata { - pub(crate) use codex_rollout::builder_from_items; -} - pub(crate) mod policy { - pub use codex_rollout::EventPersistenceMode; pub use codex_rollout::should_persist_response_item_for_memories; } diff --git a/codex-rs/core/src/session/handlers.rs b/codex-rs/core/src/session/handlers.rs index 9c91f78188..c0f9620f33 100644 --- a/codex-rs/core/src/session/handlers.rs +++ b/codex-rs/core/src/session/handlers.rs @@ -27,8 +27,6 @@ use codex_features::Feature; use codex_utils_absolute_path::AbsolutePathBuf; use crate::review_prompts::resolve_review_request; -use crate::rollout::RolloutRecorder; -use crate::rollout::read_session_meta_line; use crate::tasks::CompactTask; use crate::tasks::UndoTask; use crate::tasks::UserShellCommandMode; @@ -757,36 +755,25 @@ pub async fn thread_rollback(sess: &Arc, sub_id: String, num_turns: u32 } let turn_context = sess.new_default_turn_with_sub_id(sub_id).await; - let rollout_path = { - let recorder = { - let guard = sess.services.rollout.lock().await; - guard.clone() - }; - let Some(recorder) = recorder else { + let live_thread = match sess.live_thread_for_persistence("rollback thread") { + Ok(live_thread) => live_thread, + Err(_) => { sess.send_event_raw(Event { id: turn_context.sub_id.clone(), msg: EventMsg::Error(ErrorEvent { - message: "thread rollback requires a persisted rollout path".to_string(), + message: "thread rollback requires persisted thread history".to_string(), codex_error_info: Some(CodexErrorInfo::ThreadRollbackFailed), }), }) .await; return; - }; - recorder.rollout_path().to_path_buf() + } }; - if let Some(recorder) = { - let guard = sess.services.rollout.lock().await; - guard.clone() - } && let Err(err) = recorder.flush().await - { + if let Err(err) = live_thread.flush().await { sess.send_event_raw(Event { id: turn_context.sub_id.clone(), msg: EventMsg::Error(ErrorEvent { - message: format!( - "failed to flush rollout `{}` for rollback replay: {err}", - rollout_path.display() - ), + message: format!("failed to flush thread persistence for rollback replay: {err}"), codex_error_info: Some(CodexErrorInfo::ThreadRollbackFailed), }), }) @@ -794,16 +781,13 @@ pub async fn thread_rollback(sess: &Arc, sub_id: String, num_turns: u32 return; } - let initial_history = match RolloutRecorder::get_rollout_history(rollout_path.as_path()).await { + let stored_history = match live_thread.load_history(/*include_archived*/ false).await { Ok(history) => history, Err(err) => { sess.send_event_raw(Event { id: turn_context.sub_id.clone(), msg: EventMsg::Error(ErrorEvent { - message: format!( - "failed to load rollout `{}` for rollback replay: {err}", - rollout_path.display() - ), + message: format!("failed to load thread history for rollback replay: {err}"), codex_error_info: Some(CodexErrorInfo::ThreadRollbackFailed), }), }) @@ -814,8 +798,8 @@ pub async fn thread_rollback(sess: &Arc, sub_id: String, num_turns: u32 let rollback_event = ThreadRolledBackEvent { num_turns }; let rollback_msg = EventMsg::ThreadRolledBack(rollback_event.clone()); - let replay_items = initial_history - .get_rollout_items() + let replay_items = stored_history + .items .into_iter() .chain(std::iter::once(RolloutItem::EventMsg(rollback_msg.clone()))) .collect::>(); @@ -850,14 +834,12 @@ async fn persist_thread_name_update( ) -> anyhow::Result { let msg = EventMsg::ThreadNameUpdated(event); let item = RolloutItem::EventMsg(msg.clone()); - let recorder = { - let guard = sess.services.rollout.lock().await; - guard.clone() - } - .ok_or_else(|| anyhow::anyhow!("Session persistence is disabled; cannot rename thread."))?; - recorder.persist().await?; - recorder.record_items(std::slice::from_ref(&item)).await?; - recorder.flush().await?; + let live_thread = sess.live_thread_for_persistence("rename thread")?; + live_thread.persist().await?; + live_thread + .append_items(std::slice::from_ref(&item)) + .await?; + live_thread.flush().await?; Ok(msg) } @@ -865,36 +847,13 @@ pub(super) async fn persist_thread_memory_mode_update( sess: &Arc, mode: ThreadMemoryMode, ) -> anyhow::Result<()> { - let recorder = { - let guard = sess.services.rollout.lock().await; - guard.clone() - } - .ok_or_else(|| { - anyhow::anyhow!("Session persistence is disabled; cannot update thread memory mode.") - })?; - recorder.persist().await?; - recorder.flush().await?; - - let rollout_path = recorder.rollout_path().to_path_buf(); - let mut session_meta = read_session_meta_line(rollout_path.as_path()).await?; - if session_meta.meta.id != sess.conversation_id { - anyhow::bail!( - "rollout session metadata id mismatch: expected {}, found {}", - sess.conversation_id, - session_meta.meta.id - ); - } - session_meta.meta.memory_mode = Some( - match mode { - ThreadMemoryMode::Enabled => "enabled", - ThreadMemoryMode::Disabled => "disabled", - } - .to_string(), - ); - - let item = RolloutItem::SessionMeta(session_meta); - recorder.record_items(std::slice::from_ref(&item)).await?; - recorder.flush().await?; + let live_thread = sess.live_thread_for_persistence("update thread memory mode")?; + live_thread.persist().await?; + live_thread.flush().await?; + live_thread + .update_memory_mode(mode, /*include_archived*/ false) + .await?; + live_thread.flush().await?; Ok(()) } @@ -996,20 +955,16 @@ pub async fn shutdown(sess: &Arc, sub_id: String) -> bool { &[], ); - // Gracefully flush and shutdown rollout recorder on session end so tests - // that inspect the rollout file do not race with the background writer. - let recorder_opt = { - let mut guard = sess.services.rollout.lock().await; - guard.take() - }; - if let Some(rec) = recorder_opt - && let Err(e) = rec.shutdown().await + // Gracefully flush and shutdown thread persistence on session end so tests + // that inspect durable state do not race with the background writer. + if let Some(live_thread) = sess.live_thread() + && let Err(e) = live_thread.shutdown().await { - warn!("failed to shutdown rollout recorder: {e}"); + warn!("failed to shutdown thread persistence: {e}"); let event = Event { id: sub_id.clone(), msg: EventMsg::Error(ErrorEvent { - message: "Failed to shutdown rollout recorder".to_string(), + message: "Failed to shutdown thread persistence".to_string(), codex_error_info: Some(CodexErrorInfo::Other), }), }; diff --git a/codex-rs/core/src/session/mod.rs b/codex-rs/core/src/session/mod.rs index abd210b2bf..65c97f8ab3 100644 --- a/codex-rs/core/src/session/mod.rs +++ b/codex-rs/core/src/session/mod.rs @@ -120,14 +120,19 @@ use codex_protocol::request_permissions::RequestPermissionsResponse; use codex_protocol::request_user_input::RequestUserInputArgs; use codex_protocol::request_user_input::RequestUserInputResponse; use codex_rmcp_client::ElicitationResponse; -use codex_rollout::RolloutConfig; use codex_rollout::state_db; use codex_rollout_trace::RolloutTraceRecorder; use codex_rollout_trace::ThreadStartedTraceMetadata; use codex_sandboxing::policy_transforms::intersect_permission_profiles; use codex_shell_command::parse_command::parse_command; use codex_terminal_detection::user_agent; +use codex_thread_store::CreateThreadParams; +use codex_thread_store::LiveThread; +use codex_thread_store::LiveThreadInitGuard; use codex_thread_store::LocalThreadStore; +use codex_thread_store::ResumeThreadParams; +use codex_thread_store::ThreadEventPersistenceMode; +use codex_thread_store::ThreadStore; use codex_utils_output_truncation::TruncationPolicy; use futures::future::BoxFuture; use futures::future::Shared; @@ -269,11 +274,7 @@ use crate::mcp::McpManager; use crate::memories; use crate::network_policy_decision::execpolicy_network_rule_amendment; use crate::plugins::PluginsManager; -use crate::rollout::RolloutRecorder; -use crate::rollout::RolloutRecorderParams; use crate::rollout::map_session_init_error; -use crate::rollout::metadata; -use crate::rollout::policy::EventPersistenceMode; use crate::session_startup_prewarm::SessionStartupPrewarmHandle; use crate::shell; use crate::shell_snapshot::ShellSnapshot; @@ -408,6 +409,7 @@ pub(crate) struct CodexSpawnArgs { pub(crate) user_shell_override: Option, pub(crate) parent_trace: Option, pub(crate) analytics_events_client: Option, + pub(crate) thread_store: Arc, } pub(crate) const INITIAL_SUBMIT_ID: &str = ""; @@ -463,6 +465,7 @@ impl Codex { inherited_rollout_trace, parent_trace: _, analytics_events_client, + thread_store, } = args; let (tx_sub, rx_sub) = async_channel::bounded(SUBMISSION_CHANNEL_CAPACITY); let (tx_event, rx_event) = async_channel::unbounded(); @@ -665,6 +668,7 @@ impl Codex { agent_control, environment_manager, analytics_events_client, + thread_store, inherited_rollout_trace, ) .await @@ -1025,33 +1029,37 @@ impl Session { self.services.state_db.clone() } + pub(crate) fn live_thread_for_persistence( + &self, + operation: &str, + ) -> anyhow::Result<&LiveThread> { + self.live_thread() + .ok_or_else(|| anyhow::anyhow!("Session persistence is disabled; cannot {operation}.")) + } + + pub(crate) fn live_thread(&self) -> Option<&LiveThread> { + self.services.live_thread.as_ref() + } + /// Flush rollout writes and return the final durability-barrier result. pub(crate) async fn flush_rollout(&self) -> std::io::Result<()> { - let recorder = { - let guard = self.services.rollout.lock().await; - guard.clone() - }; - if let Some(recorder) = recorder { - recorder.flush().await + if let Some(live_thread) = self.live_thread() { + live_thread.flush().await.map_err(std::io::Error::other) } else { Ok(()) } } pub(crate) async fn try_ensure_rollout_materialized(&self) -> std::io::Result<()> { - let recorder = { - let guard = self.services.rollout.lock().await; - guard.clone() - }; - if let Some(rec) = recorder { - rec.persist().await?; + if let Some(live_thread) = self.live_thread() { + live_thread.persist().await.map_err(std::io::Error::other)?; } Ok(()) } pub(crate) async fn ensure_rollout_materialized(&self) { if let Err(e) = self.try_ensure_rollout_materialized().await { - warn!("failed to materialize rollout recorder: {e}"); + warn!("failed to materialize thread persistence: {e}"); } } @@ -1213,7 +1221,7 @@ impl Session { state.set_token_info(Some(info)); } - // If persisting, persist all rollout items as-is (recorder filters) + // If persisting, persist all rollout items as-is (the store filters). if !rollout_items.is_empty() { self.persist_rollout_items(&rollout_items).await; } @@ -1556,7 +1564,7 @@ impl Session { } pub(crate) async fn send_event_raw(&self, event: Event) { - // Persist the event into rollout (recorder filters as needed) + // Persist the event into rollout storage (the store filters as needed). let rollout_items = vec![RolloutItem::EventMsg(event.msg.clone())]; self.persist_rollout_items(&rollout_items).await; self.deliver_event_raw(event).await; @@ -2668,12 +2676,8 @@ impl Session { } pub(crate) async fn persist_rollout_items(&self, items: &[RolloutItem]) { - let recorder = { - let guard = self.services.rollout.lock().await; - guard.clone() - }; - if let Some(rec) = recorder - && let Err(e) = rec.record_items(items).await + if let Some(live_thread) = self.live_thread() + && let Err(e) = live_thread.append_items(items).await { error!("failed to record rollout items: {e:#}"); } @@ -3192,17 +3196,22 @@ impl Session { Arc::clone(&self.services.user_shell) } - pub(crate) async fn current_rollout_path(&self) -> Option { - let recorder = { - let guard = self.services.rollout.lock().await; - guard.clone() + pub(crate) async fn current_rollout_path(&self) -> anyhow::Result> { + let Some(live_thread) = self.live_thread() else { + return Ok(None); }; - recorder.map(|recorder| recorder.rollout_path().to_path_buf()) + live_thread.local_rollout_path().await.map_err(Into::into) } pub(crate) async fn hook_transcript_path(&self) -> Option { self.ensure_rollout_materialized().await; - self.current_rollout_path().await + match self.current_rollout_path().await { + Ok(path) => path, + Err(err) => { + warn!("{err}"); + None + } + } } pub(crate) async fn take_pending_session_start_source( diff --git a/codex-rs/core/src/session/session.rs b/codex-rs/core/src/session/session.rs index 95740cc4d3..42e98ea586 100644 --- a/codex-rs/core/src/session/session.rs +++ b/codex-rs/core/src/session/session.rs @@ -272,6 +272,7 @@ impl Session { agent_control: AgentControl, environment_manager: Arc, analytics_events_client: Option, + thread_store: Arc, inherited_rollout_trace: RolloutTraceRecorder, ) -> anyhow::Result> { debug!( @@ -281,38 +282,16 @@ impl Session { ); let forked_from_id = initial_history.forked_from_id(); - let (conversation_id, rollout_params) = match &initial_history { + let event_persistence_mode = if session_configuration.persist_extended_history { + ThreadEventPersistenceMode::Extended + } else { + ThreadEventPersistenceMode::Limited + }; + let conversation_id = match &initial_history { InitialHistory::New | InitialHistory::Cleared | InitialHistory::Forked(_) => { - let conversation_id = ThreadId::default(); - ( - conversation_id, - RolloutRecorderParams::new( - conversation_id, - forked_from_id, - session_source, - BaseInstructions { - text: session_configuration.base_instructions.clone(), - }, - session_configuration.dynamic_tools.clone(), - if session_configuration.persist_extended_history { - EventPersistenceMode::Extended - } else { - EventPersistenceMode::Limited - }, - ), - ) + ThreadId::default() } - InitialHistory::Resumed(resumed_history) => ( - resumed_history.conversation_id, - RolloutRecorderParams::resume( - resumed_history.rollout_path.clone(), - if session_configuration.persist_extended_history { - EventPersistenceMode::Extended - } else { - EventPersistenceMode::Limited - }, - ), - ), + InitialHistory::Resumed(resumed_history) => resumed_history.conversation_id, }; let window_generation = match &initial_history { InitialHistory::Resumed(resumed_history) => u64::try_from( @@ -325,37 +304,68 @@ impl Session { .unwrap_or(u64::MAX), InitialHistory::New | InitialHistory::Cleared | InitialHistory::Forked(_) => 0, }; - let state_builder = match &initial_history { - InitialHistory::Resumed(resumed) => metadata::builder_from_items( - resumed.history.as_slice(), - resumed.rollout_path.as_path(), - ), - InitialHistory::New | InitialHistory::Cleared | InitialHistory::Forked(_) => None, - }; - // Kick off independent async setup tasks in parallel to reduce startup latency. // - // - initialize RolloutRecorder with new or resumed session info + // - initialize thread persistence with new or resumed session info // - perform default shell discovery // - load history metadata (skipped for subagents) - let rollout_fut = async { + let thread_persistence_fut = async { if config.ephemeral { - Ok::<_, anyhow::Error>((None, None)) + Ok::<_, anyhow::Error>(None) } else { - let state_db_ctx = state_db::init(&config).await; - let rollout_recorder = RolloutRecorder::new( - &config, - rollout_params, - state_db_ctx.clone(), - state_builder.clone(), - ) - .await?; - Ok((Some(rollout_recorder), state_db_ctx)) + let live_thread = match &initial_history { + InitialHistory::New | InitialHistory::Cleared | InitialHistory::Forked(_) => { + LiveThread::create( + Arc::clone(&thread_store), + CreateThreadParams { + thread_id: conversation_id, + forked_from_id, + source: session_source, + base_instructions: BaseInstructions { + text: session_configuration.base_instructions.clone(), + }, + dynamic_tools: session_configuration.dynamic_tools.clone(), + event_persistence_mode, + }, + ) + .await? + } + InitialHistory::Resumed(resumed_history) => { + LiveThread::resume( + Arc::clone(&thread_store), + ResumeThreadParams { + thread_id: resumed_history.conversation_id, + rollout_path: Some(resumed_history.rollout_path.clone()), + history: Some(resumed_history.history.clone()), + include_archived: true, + event_persistence_mode, + }, + ) + .await? + } + }; + Ok(Some(live_thread)) } } .instrument(info_span!( - "session_init.rollout", - otel.name = "session_init.rollout", + "session_init.thread_persistence", + otel.name = "session_init.thread_persistence", + session_init.ephemeral = config.ephemeral, + )); + let state_db_fut = async { + if config.ephemeral { + None + } else if let Some(local_store) = + thread_store.as_any().downcast_ref::() + { + local_store.state_db().await + } else { + None + } + } + .instrument(info_span!( + "session_init.state_db", + otel.name = "session_init.state_db", session_init.ephemeral = config.ephemeral, )); @@ -397,542 +407,562 @@ impl Session { // Join all independent futures. let ( - rollout_recorder_and_state_db, + thread_persistence_result, + state_db_ctx, (history_log_id, history_entry_count), (auth, mcp_servers, auth_statuses), - ) = tokio::join!(rollout_fut, history_meta_fut, auth_and_mcp_fut); + ) = tokio::join!( + thread_persistence_fut, + state_db_fut, + history_meta_fut, + auth_and_mcp_fut + ); - let (rollout_recorder, state_db_ctx) = rollout_recorder_and_state_db.map_err(|e| { - error!("failed to initialize rollout recorder: {e:#}"); - e - })?; - let rollout_path = rollout_recorder - .as_ref() - .map(|rec| rec.rollout_path().to_path_buf()); - let trace_agent_path = session_configuration - .session_source - .get_agent_path() - .unwrap_or_else(codex_protocol::AgentPath::root); - let trace_task_name = - (!trace_agent_path.is_root()).then(|| trace_agent_path.name().to_string()); - let trace_metadata = ThreadStartedTraceMetadata { - thread_id: conversation_id.to_string(), - agent_path: trace_agent_path.to_string(), - task_name: trace_task_name, - nickname: session_configuration.session_source.get_nickname(), - agent_role: session_configuration.session_source.get_agent_role(), - session_source: session_configuration.session_source.clone(), - cwd: session_configuration.cwd.to_path_buf(), - rollout_path: rollout_path.clone(), - model: session_configuration.collaboration_mode.model().to_string(), - provider_name: config.model_provider_id.clone(), - approval_policy: session_configuration.approval_policy.value().to_string(), - sandbox_policy: format!("{:?}", session_configuration.sandbox_policy.get()), - }; - let rollout_trace = if matches!( - session_configuration.session_source, - SessionSource::SubAgent(SubAgentSource::ThreadSpawn { .. }) - ) { - // Spawned child threads are part of their root rollout tree. If - // the parent had no trace recorder, do not create an orphan child - // bundle that looks like an independent rollout. - inherited_rollout_trace - } else { - RolloutTraceRecorder::create_root_or_disabled(conversation_id) - }; - rollout_trace.record_thread_started(trace_metadata); + let mut live_thread_init = + LiveThreadInitGuard::new(thread_persistence_result.map_err(|e| { + error!("failed to initialize thread persistence: {e:#}"); + e + })?); + let session_result: anyhow::Result> = async { + let rollout_path = if let Some(live_thread) = live_thread_init.as_ref() { + live_thread.local_rollout_path().await? + } else { + None + }; + let trace_agent_path = session_configuration + .session_source + .get_agent_path() + .unwrap_or_else(codex_protocol::AgentPath::root); + let trace_task_name = + (!trace_agent_path.is_root()).then(|| trace_agent_path.name().to_string()); + let trace_metadata = ThreadStartedTraceMetadata { + thread_id: conversation_id.to_string(), + agent_path: trace_agent_path.to_string(), + task_name: trace_task_name, + nickname: session_configuration.session_source.get_nickname(), + agent_role: session_configuration.session_source.get_agent_role(), + session_source: session_configuration.session_source.clone(), + cwd: session_configuration.cwd.to_path_buf(), + rollout_path: rollout_path.clone(), + model: session_configuration.collaboration_mode.model().to_string(), + provider_name: config.model_provider_id.clone(), + approval_policy: session_configuration.approval_policy.value().to_string(), + sandbox_policy: format!("{:?}", session_configuration.sandbox_policy.get()), + }; + let rollout_trace = if matches!( + session_configuration.session_source, + SessionSource::SubAgent(SubAgentSource::ThreadSpawn { .. }) + ) { + // Spawned child threads are part of their root rollout tree. If + // the parent had no trace recorder, do not create an orphan child + // bundle that looks like an independent rollout. + inherited_rollout_trace + } else { + RolloutTraceRecorder::create_root_or_disabled(conversation_id) + }; + rollout_trace.record_thread_started(trace_metadata); - let mut post_session_configured_events = Vec::::new(); + let mut post_session_configured_events = Vec::::new(); - for usage in config.features.legacy_feature_usages() { - post_session_configured_events.push(Event { - id: INITIAL_SUBMIT_ID.to_owned(), - msg: EventMsg::DeprecationNotice(DeprecationNoticeEvent { - summary: usage.summary.clone(), - details: usage.details.clone(), - }), - }); - } - if crate::config::uses_deprecated_instructions_file(&config.config_layer_stack) { - post_session_configured_events.push(Event { - id: INITIAL_SUBMIT_ID.to_owned(), - msg: EventMsg::DeprecationNotice(DeprecationNoticeEvent { - summary: "`experimental_instructions_file` is deprecated and ignored. Use `model_instructions_file` instead." - .to_string(), - details: Some( - "Move the setting to `model_instructions_file` in config.toml (or under a profile) to load instructions from a file." + for usage in config.features.legacy_feature_usages() { + post_session_configured_events.push(Event { + id: INITIAL_SUBMIT_ID.to_owned(), + msg: EventMsg::DeprecationNotice(DeprecationNoticeEvent { + summary: usage.summary.clone(), + details: usage.details.clone(), + }), + }); + } + if crate::config::uses_deprecated_instructions_file(&config.config_layer_stack) { + post_session_configured_events.push(Event { + id: INITIAL_SUBMIT_ID.to_owned(), + msg: EventMsg::DeprecationNotice(DeprecationNoticeEvent { + summary: "`experimental_instructions_file` is deprecated and ignored. Use `model_instructions_file` instead." .to_string(), - ), - }), - }); - } - for message in &config.startup_warnings { - post_session_configured_events.push(Event { - id: "".to_owned(), - msg: EventMsg::Warning(WarningEvent { - message: message.clone(), - }), - }); - } - let config_path = config.codex_home.join(CONFIG_TOML_FILE); - if let Some(event) = unstable_features_warning_event( - config - .config_layer_stack - .effective_config() - .get("features") - .and_then(TomlValue::as_table), - config.suppress_unstable_features_warning, - &config.features, - &config_path.display().to_string(), - ) { - post_session_configured_events.push(event); - } - if config.permissions.approval_policy.value() == AskForApproval::OnFailure { - post_session_configured_events.push(Event { - id: "".to_owned(), - msg: EventMsg::Warning(WarningEvent { - message: "`on-failure` approval policy is deprecated and will be removed in a future release. Use `on-request` for interactive approvals or `never` for non-interactive runs.".to_string(), - }), - }); - } + details: Some( + "Move the setting to `model_instructions_file` in config.toml (or under a profile) to load instructions from a file." + .to_string(), + ), + }), + }); + } + for message in &config.startup_warnings { + post_session_configured_events.push(Event { + id: "".to_owned(), + msg: EventMsg::Warning(WarningEvent { + message: message.clone(), + }), + }); + } + let config_path = config.codex_home.join(CONFIG_TOML_FILE); + if let Some(event) = unstable_features_warning_event( + config + .config_layer_stack + .effective_config() + .get("features") + .and_then(TomlValue::as_table), + config.suppress_unstable_features_warning, + &config.features, + &config_path.display().to_string(), + ) { + post_session_configured_events.push(event); + } + if config.permissions.approval_policy.value() == AskForApproval::OnFailure { + post_session_configured_events.push(Event { + id: "".to_owned(), + msg: EventMsg::Warning(WarningEvent { + message: "`on-failure` approval policy is deprecated and will be removed in a future release. Use `on-request` for interactive approvals or `never` for non-interactive runs.".to_string(), + }), + }); + } - let auth = auth.as_ref(); - let auth_mode = auth.map(CodexAuth::auth_mode).map(TelemetryAuthMode::from); - let account_id = auth.and_then(CodexAuth::get_account_id); - let account_email = auth.and_then(CodexAuth::get_account_email); - let originator = originator().value; - let terminal_type = user_agent(); - let session_model = session_configuration.collaboration_mode.model().to_string(); - let auth_env_telemetry = collect_auth_env_telemetry( - &session_configuration.provider, - auth_manager.codex_api_key_env_enabled(), - ); - let mut session_telemetry = SessionTelemetry::new( - conversation_id, - session_model.as_str(), - session_model.as_str(), - account_id.clone(), - account_email.clone(), - auth_mode, - originator.clone(), - config.otel.log_user_prompt, - terminal_type.clone(), - session_configuration.session_source.clone(), - ) - .with_auth_env(auth_env_telemetry.to_otel_metadata()); - if let Some(service_name) = session_configuration.metrics_service_name.as_deref() { - session_telemetry = session_telemetry.with_metrics_service_name(service_name); - } - let network_proxy_audit_metadata = NetworkProxyAuditMetadata { - conversation_id: Some(conversation_id.to_string()), - app_version: Some(env!("CARGO_PKG_VERSION").to_string()), - user_account_id: account_id, - auth_mode: auth_mode.map(|mode| mode.to_string()), - originator: Some(originator), - user_email: account_email, - terminal_type: Some(terminal_type), - model: Some(session_model.clone()), - slug: Some(session_model), - }; - config.features.emit_metrics(&session_telemetry); - session_telemetry.counter( - THREAD_STARTED_METRIC, - /*inc*/ 1, - &[( - "is_git", - if get_git_repo_root(&session_configuration.cwd).is_some() { - "true" + let auth = auth.as_ref(); + let auth_mode = auth.map(CodexAuth::auth_mode).map(TelemetryAuthMode::from); + let account_id = auth.and_then(CodexAuth::get_account_id); + let account_email = auth.and_then(CodexAuth::get_account_email); + let originator = originator().value; + let terminal_type = user_agent(); + let session_model = session_configuration.collaboration_mode.model().to_string(); + let auth_env_telemetry = collect_auth_env_telemetry( + &session_configuration.provider, + auth_manager.codex_api_key_env_enabled(), + ); + let mut session_telemetry = SessionTelemetry::new( + conversation_id, + session_model.as_str(), + session_model.as_str(), + account_id.clone(), + account_email.clone(), + auth_mode, + originator.clone(), + config.otel.log_user_prompt, + terminal_type.clone(), + session_configuration.session_source.clone(), + ) + .with_auth_env(auth_env_telemetry.to_otel_metadata()); + if let Some(service_name) = session_configuration.metrics_service_name.as_deref() { + session_telemetry = session_telemetry.with_metrics_service_name(service_name); + } + let network_proxy_audit_metadata = NetworkProxyAuditMetadata { + conversation_id: Some(conversation_id.to_string()), + app_version: Some(env!("CARGO_PKG_VERSION").to_string()), + user_account_id: account_id, + auth_mode: auth_mode.map(|mode| mode.to_string()), + originator: Some(originator), + user_email: account_email, + terminal_type: Some(terminal_type), + model: Some(session_model.clone()), + slug: Some(session_model), + }; + config.features.emit_metrics(&session_telemetry); + session_telemetry.counter( + THREAD_STARTED_METRIC, + /*inc*/ 1, + &[( + "is_git", + if get_git_repo_root(&session_configuration.cwd).is_some() { + "true" + } else { + "false" + }, + )], + ); + + session_telemetry.conversation_starts( + config.model_provider.name.as_str(), + session_configuration.collaboration_mode.reasoning_effort(), + config + .model_reasoning_summary + .unwrap_or(ReasoningSummaryConfig::Auto), + config.model_context_window, + config.model_auto_compact_token_limit, + config.permissions.approval_policy.value(), + config.permissions.sandbox_policy.get().clone(), + mcp_servers.keys().map(String::as_str).collect(), + config.active_profile.clone(), + ); + + let use_zsh_fork_shell = config.features.enabled(Feature::ShellZshFork); + let mut default_shell = if let Some(user_shell_override) = + session_configuration.user_shell_override.clone() + { + user_shell_override + } else if use_zsh_fork_shell { + let zsh_path = config.zsh_path.as_ref().ok_or_else(|| { + anyhow::anyhow!( + "zsh fork feature enabled, but `zsh_path` is not configured; set `zsh_path` in config.toml" + ) + })?; + let zsh_path = zsh_path.to_path_buf(); + shell::get_shell(shell::ShellType::Zsh, Some(&zsh_path)).ok_or_else(|| { + anyhow::anyhow!( + "zsh fork feature enabled, but zsh_path `{}` is not usable; set `zsh_path` to a valid zsh executable", + zsh_path.display() + ) + })? + } else { + shell::default_user_shell() + }; + // Create the mutable state for the Session. + let shell_snapshot_tx = if config.features.enabled(Feature::ShellSnapshot) { + if let Some(snapshot) = session_configuration.inherited_shell_snapshot.clone() { + let (tx, rx) = watch::channel(Some(snapshot)); + default_shell.shell_snapshot = rx; + tx } else { - "false" - }, - )], - ); - - session_telemetry.conversation_starts( - config.model_provider.name.as_str(), - session_configuration.collaboration_mode.reasoning_effort(), - config - .model_reasoning_summary - .unwrap_or(ReasoningSummaryConfig::Auto), - config.model_context_window, - config.model_auto_compact_token_limit, - config.permissions.approval_policy.value(), - config.permissions.sandbox_policy.get().clone(), - mcp_servers.keys().map(String::as_str).collect(), - config.active_profile.clone(), - ); - - let use_zsh_fork_shell = config.features.enabled(Feature::ShellZshFork); - let mut default_shell = if let Some(user_shell_override) = - session_configuration.user_shell_override.clone() - { - user_shell_override - } else if use_zsh_fork_shell { - let zsh_path = config.zsh_path.as_ref().ok_or_else(|| { - anyhow::anyhow!( - "zsh fork feature enabled, but `zsh_path` is not configured; set `zsh_path` in config.toml" - ) - })?; - let zsh_path = zsh_path.to_path_buf(); - shell::get_shell(shell::ShellType::Zsh, Some(&zsh_path)).ok_or_else(|| { - anyhow::anyhow!( - "zsh fork feature enabled, but zsh_path `{}` is not usable; set `zsh_path` to a valid zsh executable", - zsh_path.display() - ) - })? - } else { - shell::default_user_shell() - }; - // Create the mutable state for the Session. - let shell_snapshot_tx = if config.features.enabled(Feature::ShellSnapshot) { - if let Some(snapshot) = session_configuration.inherited_shell_snapshot.clone() { - let (tx, rx) = watch::channel(Some(snapshot)); + ShellSnapshot::start_snapshotting( + config.codex_home.clone(), + conversation_id, + session_configuration.cwd.clone(), + &mut default_shell, + session_telemetry.clone(), + ) + } + } else { + let (tx, rx) = watch::channel(None); default_shell.shell_snapshot = rx; tx + }; + let thread_name = + thread_title_from_state_db(state_db_ctx.as_ref(), &config.codex_home, conversation_id) + .instrument(info_span!( + "session_init.thread_name_lookup", + otel.name = "session_init.thread_name_lookup", + )) + .await; + session_configuration.thread_name = thread_name.clone(); + let state = SessionState::new(session_configuration.clone()); + let managed_network_requirements_configured = config + .config_layer_stack + .requirements_toml() + .network + .is_some(); + let managed_network_requirements_enabled = config.managed_network_requirements_enabled(); + let network_approval = Arc::new(NetworkApprovalService::default()); + // The managed proxy can call back into core for allowlist-miss decisions. + let network_policy_decider_session = if managed_network_requirements_configured { + config + .permissions + .network + .as_ref() + .map(|_| Arc::new(RwLock::new(std::sync::Weak::::new()))) } else { - ShellSnapshot::start_snapshotting( - config.codex_home.clone(), - conversation_id, - session_configuration.cwd.clone(), - &mut default_shell, - session_telemetry.clone(), - ) - } - } else { - let (tx, rx) = watch::channel(None); - default_shell.shell_snapshot = rx; - tx - }; - let thread_name = - thread_title_from_state_db(state_db_ctx.as_ref(), &config.codex_home, conversation_id) - .instrument(info_span!( - "session_init.thread_name_lookup", - otel.name = "session_init.thread_name_lookup", - )) - .await; - session_configuration.thread_name = thread_name.clone(); - let state = SessionState::new(session_configuration.clone()); - let managed_network_requirements_configured = config - .config_layer_stack - .requirements_toml() - .network - .is_some(); - let managed_network_requirements_enabled = config.managed_network_requirements_enabled(); - let network_approval = Arc::new(NetworkApprovalService::default()); - // The managed proxy can call back into core for allowlist-miss decisions. - let network_policy_decider_session = if managed_network_requirements_configured { - config - .permissions - .network - .as_ref() - .map(|_| Arc::new(RwLock::new(std::sync::Weak::::new()))) - } else { - None - }; - let blocked_request_observer = if managed_network_requirements_configured { - config - .permissions - .network - .as_ref() - .map(|_| build_blocked_request_observer(Arc::clone(&network_approval))) - } else { - None - }; - let network_policy_decider = - network_policy_decider_session - .as_ref() - .map(|network_policy_decider_session| { - build_network_policy_decider( - Arc::clone(&network_approval), - Arc::clone(network_policy_decider_session), + None + }; + let blocked_request_observer = if managed_network_requirements_configured { + config + .permissions + .network + .as_ref() + .map(|_| build_blocked_request_observer(Arc::clone(&network_approval))) + } else { + None + }; + let network_policy_decider = + network_policy_decider_session + .as_ref() + .map(|network_policy_decider_session| { + build_network_policy_decider( + Arc::clone(&network_approval), + Arc::clone(network_policy_decider_session), + ) + }); + let (network_proxy, session_network_proxy) = + if let Some(spec) = config.permissions.network.as_ref() { + let current_exec_policy = exec_policy.current(); + let (network_proxy, session_network_proxy) = Self::start_managed_network_proxy( + spec, + current_exec_policy.as_ref(), + config.permissions.sandbox_policy.get(), + network_policy_decider.as_ref().map(Arc::clone), + blocked_request_observer.as_ref().map(Arc::clone), + managed_network_requirements_configured, + network_proxy_audit_metadata, ) + .instrument(info_span!( + "session_init.network_proxy", + otel.name = "session_init.network_proxy", + session_init.managed_network_requirements_enabled = + managed_network_requirements_enabled, + )) + .await?; + (Some(network_proxy), Some(session_network_proxy)) + } else { + (None, None) + }; + + let mut hook_shell_argv = + default_shell.derive_exec_args("", /*use_login_shell*/ false); + let hook_shell_program = hook_shell_argv.remove(0); + let _ = hook_shell_argv.pop(); + let hooks = Hooks::new(HooksConfig { + legacy_notify_argv: config.notify.clone(), + feature_enabled: config.features.enabled(Feature::CodexHooks), + config_layer_stack: Some(config.config_layer_stack.clone()), + shell_program: Some(hook_shell_program), + shell_args: hook_shell_argv, + }); + for warning in hooks.startup_warnings() { + post_session_configured_events.push(Event { + id: INITIAL_SUBMIT_ID.to_owned(), + msg: EventMsg::Warning(WarningEvent { + message: warning.clone(), + }), }); - let (network_proxy, session_network_proxy) = - if let Some(spec) = config.permissions.network.as_ref() { - let current_exec_policy = exec_policy.current(); - let (network_proxy, session_network_proxy) = Self::start_managed_network_proxy( - spec, - current_exec_policy.as_ref(), - config.permissions.sandbox_policy.get(), - network_policy_decider.as_ref().map(Arc::clone), - blocked_request_observer.as_ref().map(Arc::clone), - managed_network_requirements_configured, - network_proxy_audit_metadata, + } + + let installation_id = resolve_installation_id(&config.codex_home).await?; + let analytics_events_client = analytics_events_client.unwrap_or_else(|| { + AnalyticsEventsClient::new( + Arc::clone(&auth_manager), + config.chatgpt_base_url.trim_end_matches('/').to_string(), + config.analytics_enabled, ) - .instrument(info_span!( - "session_init.network_proxy", - otel.name = "session_init.network_proxy", - session_init.managed_network_requirements_enabled = - managed_network_requirements_enabled, - )) - .await?; - (Some(network_proxy), Some(session_network_proxy)) + }); + let services = SessionServices { + // Initialize the MCP connection manager with an uninitialized + // instance. It will be replaced with one created via + // McpConnectionManager::new() once all its constructor args are + // available. This also ensures `SessionConfigured` is emitted + // before any MCP-related events. It is reasonable to consider + // changing this to use Option or OnceCell, though the current + // setup is straightforward enough and performs well. + mcp_connection_manager: Arc::new(RwLock::new(McpConnectionManager::new_uninitialized( + &config.permissions.approval_policy, + &config.permissions.sandbox_policy, + ))), + mcp_startup_cancellation_token: Mutex::new(CancellationToken::new()), + unified_exec_manager: UnifiedExecProcessManager::new( + config.background_terminal_max_timeout, + ), + shell_zsh_path: config.zsh_path.clone(), + main_execve_wrapper_exe: config.main_execve_wrapper_exe.clone(), + analytics_events_client, + hooks, + rollout_trace, + user_shell: Arc::new(default_shell), + shell_snapshot_tx, + show_raw_agent_reasoning: config.show_raw_agent_reasoning, + exec_policy, + auth_manager: Arc::clone(&auth_manager), + session_telemetry, + models_manager: Arc::clone(&models_manager), + tool_approvals: Mutex::new(ApprovalStore::default()), + guardian_rejections: Mutex::new(HashMap::new()), + guardian_rejection_circuit_breaker: Mutex::new(Default::default()), + runtime_handle: tokio::runtime::Handle::current(), + skills_manager, + plugins_manager: Arc::clone(&plugins_manager), + mcp_manager: Arc::clone(&mcp_manager), + skills_watcher, + agent_control, + network_proxy, + network_approval: Arc::clone(&network_approval), + state_db: state_db_ctx.clone(), + live_thread: live_thread_init.as_ref().cloned(), + thread_store: Arc::clone(&thread_store), + model_client: ModelClient::new( + Some(Arc::clone(&auth_manager)), + conversation_id, + installation_id, + session_configuration.provider.clone(), + session_configuration.session_source.clone(), + config.model_verbosity, + config.features.enabled(Feature::EnableRequestCompression), + config.features.enabled(Feature::RuntimeMetrics), + Self::build_model_client_beta_features_header(config.as_ref()), + ), + code_mode_service: crate::tools::code_mode::CodeModeService::new( + config.js_repl_node_path.clone(), + ), + environment_manager, + }; + services + .model_client + .set_window_generation(window_generation); + let js_repl = Arc::new(JsReplHandle::with_node_path( + config.js_repl_node_path.clone(), + config.js_repl_node_module_dirs.clone(), + )); + let (out_of_band_elicitation_paused, _out_of_band_elicitation_paused_rx) = + watch::channel(false); + + let (mailbox, mailbox_rx) = Mailbox::new(); + let sess = Arc::new(Session { + conversation_id, + tx_event: tx_event.clone(), + agent_status, + out_of_band_elicitation_paused, + state: Mutex::new(state), + managed_network_proxy_refresh_lock: Semaphore::new(/*permits*/ 1), + features: config.features.clone(), + pending_mcp_server_refresh_config: Mutex::new(None), + conversation: Arc::new(RealtimeConversationManager::new()), + active_turn: Mutex::new(None), + mailbox, + mailbox_rx: Mutex::new(mailbox_rx), + idle_pending_input: Mutex::new(Vec::new()), + guardian_review_session: GuardianReviewSessionManager::default(), + services, + js_repl, + next_internal_sub_id: AtomicU64::new(0), + }); + if let Some(network_policy_decider_session) = network_policy_decider_session { + let mut guard = network_policy_decider_session.write().await; + *guard = Arc::downgrade(&sess); + } + // Dispatch the SessionConfiguredEvent first and then report any errors. + // If resuming, include converted initial messages in the payload so UIs can render them immediately. + let initial_messages = initial_history.get_event_msgs(); + let permission_profile = if matches!( + session_configuration.file_system_sandbox_policy.kind, + FileSystemSandboxKind::ExternalSandbox + ) { + None } else { - (None, None) + Some(session_configuration.permission_profile()) + }; + let events = std::iter::once(Event { + id: INITIAL_SUBMIT_ID.to_owned(), + msg: EventMsg::SessionConfigured(SessionConfiguredEvent { + session_id: conversation_id, + forked_from_id, + thread_name: session_configuration.thread_name.clone(), + model: session_configuration.collaboration_mode.model().to_string(), + model_provider_id: config.model_provider_id.clone(), + service_tier: session_configuration.service_tier, + approval_policy: session_configuration.approval_policy.value(), + approvals_reviewer: session_configuration.approvals_reviewer, + sandbox_policy: session_configuration.sandbox_policy.get().clone(), + permission_profile, + cwd: session_configuration.cwd.clone(), + reasoning_effort: session_configuration.collaboration_mode.reasoning_effort(), + history_log_id, + history_entry_count, + initial_messages, + network_proxy: session_network_proxy.filter(|_| { + Self::managed_network_proxy_active_for_sandbox_policy( + session_configuration.sandbox_policy.get(), + ) + }), + rollout_path, + }), + }) + .chain(post_session_configured_events.into_iter()); + for event in events { + sess.send_event_raw(event).await; + } + + // Start the watcher after SessionConfigured so it cannot emit earlier events. + sess.start_skills_watcher_listener(); + let mut required_mcp_servers: Vec = mcp_servers + .iter() + .filter(|(_, server)| server.enabled && server.required) + .map(|(name, _)| name.clone()) + .collect(); + required_mcp_servers.sort(); + let enabled_mcp_server_count = mcp_servers.values().filter(|server| server.enabled).count(); + let required_mcp_server_count = required_mcp_servers.len(); + let tool_plugin_provenance = mcp_manager.tool_plugin_provenance(config.as_ref()).await; + { + let mut cancel_guard = sess.services.mcp_startup_cancellation_token.lock().await; + cancel_guard.cancel(); + *cancel_guard = CancellationToken::new(); + } + let (mcp_connection_manager, cancel_token) = McpConnectionManager::new( + &mcp_servers, + config.mcp_oauth_credentials_store_mode, + auth_statuses.clone(), + &session_configuration.approval_policy, + INITIAL_SUBMIT_ID.to_owned(), + tx_event.clone(), + session_configuration.sandbox_policy.get().clone(), + McpRuntimeEnvironment::new( + sess.services + .environment_manager + .default_environment() + .unwrap_or_else(|| sess.services.environment_manager.local_environment()), + session_configuration.cwd.to_path_buf(), + ), + config.codex_home.to_path_buf(), + codex_apps_tools_cache_key(auth), + tool_plugin_provenance, + ) + .instrument(info_span!( + "session_init.mcp_manager_init", + otel.name = "session_init.mcp_manager_init", + session_init.enabled_mcp_server_count = enabled_mcp_server_count, + session_init.required_mcp_server_count = required_mcp_server_count, + )) + .await; + { + let mut manager_guard = sess.services.mcp_connection_manager.write().await; + *manager_guard = mcp_connection_manager; + } + { + let mut cancel_guard = sess.services.mcp_startup_cancellation_token.lock().await; + if cancel_guard.is_cancelled() { + cancel_token.cancel(); + } + *cancel_guard = cancel_token; + } + if !required_mcp_servers.is_empty() { + let failures = sess + .services + .mcp_connection_manager + .read() + .await + .required_startup_failures(&required_mcp_servers) + .instrument(info_span!( + "session_init.required_mcp_wait", + otel.name = "session_init.required_mcp_wait", + session_init.required_mcp_server_count = required_mcp_server_count, + )) + .await; + if !failures.is_empty() { + let details = failures + .iter() + .map(|failure| format!("{}: {}", failure.server, failure.error)) + .collect::>() + .join("; "); + anyhow::bail!("required MCP servers failed to initialize: {details}"); + } + } + sess.schedule_startup_prewarm(session_configuration.base_instructions.clone()) + .await; + let session_start_source = match &initial_history { + InitialHistory::Resumed(_) => codex_hooks::SessionStartSource::Resume, + InitialHistory::New | InitialHistory::Forked(_) => { + codex_hooks::SessionStartSource::Startup + } + InitialHistory::Cleared => codex_hooks::SessionStartSource::Clear, }; - let mut hook_shell_argv = - default_shell.derive_exec_args("", /*use_login_shell*/ false); - let hook_shell_program = hook_shell_argv.remove(0); - let _ = hook_shell_argv.pop(); - let hooks = Hooks::new(HooksConfig { - legacy_notify_argv: config.notify.clone(), - feature_enabled: config.features.enabled(Feature::CodexHooks), - config_layer_stack: Some(config.config_layer_stack.clone()), - shell_program: Some(hook_shell_program), - shell_args: hook_shell_argv, - }); - for warning in hooks.startup_warnings() { - post_session_configured_events.push(Event { - id: INITIAL_SUBMIT_ID.to_owned(), - msg: EventMsg::Warning(WarningEvent { - message: warning.clone(), - }), - }); - } + // record_initial_history can emit events. We record only after the SessionConfiguredEvent is emitted. + sess.record_initial_history(initial_history).await; + { + let mut state = sess.state.lock().await; + state.set_pending_session_start_source(Some(session_start_source)); + } - let installation_id = resolve_installation_id(&config.codex_home).await?; - let analytics_events_client = analytics_events_client.unwrap_or_else(|| { - AnalyticsEventsClient::new( - Arc::clone(&auth_manager), - config.chatgpt_base_url.trim_end_matches('/').to_string(), - config.analytics_enabled, - ) - }); - let services = SessionServices { - // Initialize the MCP connection manager with an uninitialized - // instance. It will be replaced with one created via - // McpConnectionManager::new() once all its constructor args are - // available. This also ensures `SessionConfigured` is emitted - // before any MCP-related events. It is reasonable to consider - // changing this to use Option or OnceCell, though the current - // setup is straightforward enough and performs well. - mcp_connection_manager: Arc::new(RwLock::new(McpConnectionManager::new_uninitialized( - &config.permissions.approval_policy, - &config.permissions.sandbox_policy, - ))), - mcp_startup_cancellation_token: Mutex::new(CancellationToken::new()), - unified_exec_manager: UnifiedExecProcessManager::new( - config.background_terminal_max_timeout, - ), - shell_zsh_path: config.zsh_path.clone(), - main_execve_wrapper_exe: config.main_execve_wrapper_exe.clone(), - analytics_events_client, - hooks, - rollout: Mutex::new(rollout_recorder), - rollout_trace, - user_shell: Arc::new(default_shell), - shell_snapshot_tx, - show_raw_agent_reasoning: config.show_raw_agent_reasoning, - exec_policy, - auth_manager: Arc::clone(&auth_manager), - session_telemetry, - models_manager: Arc::clone(&models_manager), - tool_approvals: Mutex::new(ApprovalStore::default()), - guardian_rejections: Mutex::new(HashMap::new()), - guardian_rejection_circuit_breaker: Mutex::new(Default::default()), - runtime_handle: tokio::runtime::Handle::current(), - skills_manager, - plugins_manager: Arc::clone(&plugins_manager), - mcp_manager: Arc::clone(&mcp_manager), - skills_watcher, - agent_control, - network_proxy, - network_approval: Arc::clone(&network_approval), - state_db: state_db_ctx.clone(), - thread_store: LocalThreadStore::new(RolloutConfig::from_view(config.as_ref())), - model_client: ModelClient::new( - Some(Arc::clone(&auth_manager)), - conversation_id, - installation_id, - session_configuration.provider.clone(), - session_configuration.session_source.clone(), - config.model_verbosity, - config.features.enabled(Feature::EnableRequestCompression), - config.features.enabled(Feature::RuntimeMetrics), - Self::build_model_client_beta_features_header(config.as_ref()), - ), - code_mode_service: crate::tools::code_mode::CodeModeService::new( - config.js_repl_node_path.clone(), - ), - environment_manager, - }; - services - .model_client - .set_window_generation(window_generation); - let js_repl = Arc::new(JsReplHandle::with_node_path( - config.js_repl_node_path.clone(), - config.js_repl_node_module_dirs.clone(), - )); - let (out_of_band_elicitation_paused, _out_of_band_elicitation_paused_rx) = - watch::channel(false); + memories::start_memories_startup_task( + &sess, + Arc::clone(&config), + &session_configuration.session_source, + ); - let (mailbox, mailbox_rx) = Mailbox::new(); - let sess = Arc::new(Session { - conversation_id, - tx_event: tx_event.clone(), - agent_status, - out_of_band_elicitation_paused, - state: Mutex::new(state), - managed_network_proxy_refresh_lock: Semaphore::new(/*permits*/ 1), - features: config.features.clone(), - pending_mcp_server_refresh_config: Mutex::new(None), - conversation: Arc::new(RealtimeConversationManager::new()), - active_turn: Mutex::new(None), - mailbox, - mailbox_rx: Mutex::new(mailbox_rx), - idle_pending_input: Mutex::new(Vec::new()), - guardian_review_session: GuardianReviewSessionManager::default(), - services, - js_repl, - next_internal_sub_id: AtomicU64::new(0), - }); - if let Some(network_policy_decider_session) = network_policy_decider_session { - let mut guard = network_policy_decider_session.write().await; - *guard = Arc::downgrade(&sess); + Ok(sess) } - // Dispatch the SessionConfiguredEvent first and then report any errors. - // If resuming, include converted initial messages in the payload so UIs can render them immediately. - let initial_messages = initial_history.get_event_msgs(); - let permission_profile = if matches!( - session_configuration.file_system_sandbox_policy.kind, - FileSystemSandboxKind::ExternalSandbox - ) { - None - } else { - Some(session_configuration.permission_profile()) - }; - let events = std::iter::once(Event { - id: INITIAL_SUBMIT_ID.to_owned(), - msg: EventMsg::SessionConfigured(SessionConfiguredEvent { - session_id: conversation_id, - forked_from_id, - thread_name: session_configuration.thread_name.clone(), - model: session_configuration.collaboration_mode.model().to_string(), - model_provider_id: config.model_provider_id.clone(), - service_tier: session_configuration.service_tier, - approval_policy: session_configuration.approval_policy.value(), - approvals_reviewer: session_configuration.approvals_reviewer, - sandbox_policy: session_configuration.sandbox_policy.get().clone(), - permission_profile, - cwd: session_configuration.cwd.clone(), - reasoning_effort: session_configuration.collaboration_mode.reasoning_effort(), - history_log_id, - history_entry_count, - initial_messages, - network_proxy: session_network_proxy.filter(|_| { - Self::managed_network_proxy_active_for_sandbox_policy( - session_configuration.sandbox_policy.get(), - ) - }), - rollout_path, - }), - }) - .chain(post_session_configured_events.into_iter()); - for event in events { - sess.send_event_raw(event).await; - } - - // Start the watcher after SessionConfigured so it cannot emit earlier events. - sess.start_skills_watcher_listener(); - let mut required_mcp_servers: Vec = mcp_servers - .iter() - .filter(|(_, server)| server.enabled && server.required) - .map(|(name, _)| name.clone()) - .collect(); - required_mcp_servers.sort(); - let enabled_mcp_server_count = mcp_servers.values().filter(|server| server.enabled).count(); - let required_mcp_server_count = required_mcp_servers.len(); - let tool_plugin_provenance = mcp_manager.tool_plugin_provenance(config.as_ref()).await; - { - let mut cancel_guard = sess.services.mcp_startup_cancellation_token.lock().await; - cancel_guard.cancel(); - *cancel_guard = CancellationToken::new(); - } - let (mcp_connection_manager, cancel_token) = McpConnectionManager::new( - &mcp_servers, - config.mcp_oauth_credentials_store_mode, - auth_statuses.clone(), - &session_configuration.approval_policy, - INITIAL_SUBMIT_ID.to_owned(), - tx_event.clone(), - session_configuration.sandbox_policy.get().clone(), - McpRuntimeEnvironment::new( - sess.services - .environment_manager - .default_environment() - .unwrap_or_else(|| sess.services.environment_manager.local_environment()), - session_configuration.cwd.to_path_buf(), - ), - config.codex_home.to_path_buf(), - codex_apps_tools_cache_key(auth), - tool_plugin_provenance, - ) - .instrument(info_span!( - "session_init.mcp_manager_init", - otel.name = "session_init.mcp_manager_init", - session_init.enabled_mcp_server_count = enabled_mcp_server_count, - session_init.required_mcp_server_count = required_mcp_server_count, - )) .await; - { - let mut manager_guard = sess.services.mcp_connection_manager.write().await; - *manager_guard = mcp_connection_manager; - } - { - let mut cancel_guard = sess.services.mcp_startup_cancellation_token.lock().await; - if cancel_guard.is_cancelled() { - cancel_token.cancel(); + match session_result { + Ok(sess) => { + live_thread_init.commit(); + Ok(sess) } - *cancel_guard = cancel_token; - } - if !required_mcp_servers.is_empty() { - let failures = sess - .services - .mcp_connection_manager - .read() - .await - .required_startup_failures(&required_mcp_servers) - .instrument(info_span!( - "session_init.required_mcp_wait", - otel.name = "session_init.required_mcp_wait", - session_init.required_mcp_server_count = required_mcp_server_count, - )) - .await; - if !failures.is_empty() { - let details = failures - .iter() - .map(|failure| format!("{}: {}", failure.server, failure.error)) - .collect::>() - .join("; "); - return Err(anyhow::anyhow!( - "required MCP servers failed to initialize: {details}" - )); + Err(err) => { + live_thread_init.discard().await; + Err(err) } } - sess.schedule_startup_prewarm(session_configuration.base_instructions.clone()) - .await; - let session_start_source = match &initial_history { - InitialHistory::Resumed(_) => codex_hooks::SessionStartSource::Resume, - InitialHistory::New | InitialHistory::Forked(_) => { - codex_hooks::SessionStartSource::Startup - } - InitialHistory::Cleared => codex_hooks::SessionStartSource::Clear, - }; - - // record_initial_history can emit events. We record only after the SessionConfiguredEvent is emitted. - sess.record_initial_history(initial_history).await; - { - let mut state = sess.state.lock().await; - state.set_pending_session_start_source(Some(session_start_source)); - } - - memories::start_memories_startup_task( - &sess, - Arc::clone(&config), - &session_configuration.session_source, - ); - - Ok(sess) } } diff --git a/codex-rs/core/src/session/tests.rs b/codex-rs/core/src/session/tests.rs index 69f156ce30..2e1069b9e8 100644 --- a/codex-rs/core/src/session/tests.rs +++ b/codex-rs/core/src/session/tests.rs @@ -45,8 +45,6 @@ use codex_protocol::request_permissions::PermissionGrantScope; use codex_protocol::request_permissions::RequestPermissionProfile; use tracing::Span; -use crate::RolloutRecorderParams; -use crate::rollout::policy::EventPersistenceMode; use crate::rollout::recorder::RolloutRecorder; use crate::state::TaskKind; use crate::tasks::SessionTask; @@ -1785,8 +1783,11 @@ async fn record_initial_history_forked_hydrates_previous_turn_settings() { #[tokio::test] async fn thread_rollback_drops_last_turn_from_history() { - let (sess, tc, rx) = make_session_and_context_with_rx().await; - let rollout_path = attach_rollout_recorder(&sess).await; + let (mut sess, tc, rx) = make_session_and_context_with_rx().await; + let rollout_path = attach_thread_persistence( + Arc::get_mut(&mut sess).expect("session should not have additional references"), + ) + .await; let initial_context = sess.build_initial_context(tc.as_ref()).await; let turn_1 = vec![ @@ -1849,8 +1850,11 @@ async fn thread_rollback_drops_last_turn_from_history() { #[tokio::test] async fn thread_rollback_clears_history_when_num_turns_exceeds_existing_turns() { - let (sess, tc, rx) = make_session_and_context_with_rx().await; - attach_rollout_recorder(&sess).await; + let (mut sess, tc, rx) = make_session_and_context_with_rx().await; + attach_thread_persistence( + Arc::get_mut(&mut sess).expect("session should not have additional references"), + ) + .await; let initial_context = sess.build_initial_context(tc.as_ref()).await; let turn_1 = vec![user_message("turn 1 user")]; @@ -1875,7 +1879,7 @@ async fn thread_rollback_clears_history_when_num_turns_exceeds_existing_turns() } #[tokio::test] -async fn thread_rollback_fails_without_persisted_rollout_path() { +async fn thread_rollback_fails_without_persisted_thread_history() { let (sess, tc, rx) = make_session_and_context_with_rx().await; let initial_context = sess.build_initial_context(tc.as_ref()).await; @@ -1887,7 +1891,7 @@ async fn thread_rollback_fails_without_persisted_rollout_path() { let error_event = wait_for_thread_rollback_failed(&rx).await; assert_eq!( error_event.message, - "thread rollback requires a persisted rollout path" + "thread rollback requires persisted thread history" ); assert_eq!( error_event.codex_error_info, @@ -1898,8 +1902,11 @@ async fn thread_rollback_fails_without_persisted_rollout_path() { #[tokio::test] async fn thread_rollback_recomputes_previous_turn_settings_and_reference_context_from_replay() { - let (sess, tc, rx) = make_session_and_context_with_rx().await; - attach_rollout_recorder(&sess).await; + let (mut sess, tc, rx) = make_session_and_context_with_rx().await; + attach_thread_persistence( + Arc::get_mut(&mut sess).expect("session should not have additional references"), + ) + .await; let first_context_item = tc.to_turn_context_item(); let first_turn_id = first_context_item @@ -2009,8 +2016,11 @@ async fn thread_rollback_recomputes_previous_turn_settings_and_reference_context #[tokio::test] async fn thread_rollback_restores_cleared_reference_context_item_after_compaction() { - let (sess, tc, rx) = make_session_and_context_with_rx().await; - attach_rollout_recorder(&sess).await; + let (mut sess, tc, rx) = make_session_and_context_with_rx().await; + attach_thread_persistence( + Arc::get_mut(&mut sess).expect("session should not have additional references"), + ) + .await; let first_context_item = tc.to_turn_context_item(); let first_turn_id = first_context_item @@ -2114,8 +2124,11 @@ async fn thread_rollback_restores_cleared_reference_context_item_after_compactio #[tokio::test] async fn thread_rollback_persists_marker_and_replays_cumulatively() { - let (sess, tc, rx) = make_session_and_context_with_rx().await; - let rollout_path = attach_rollout_recorder(&sess).await; + let (mut sess, tc, rx) = make_session_and_context_with_rx().await; + let rollout_path = attach_thread_persistence( + Arc::get_mut(&mut sess).expect("session should not have additional references"), + ) + .await; let turn_context_item = tc.to_turn_context_item(); sess.persist_rollout_items(&[ @@ -2660,34 +2673,31 @@ async fn wait_for_thread_rollback_failed(rx: &async_channel::Receiver) -> } } -async fn attach_rollout_recorder(session: &Arc) -> PathBuf { - let config = session.get_config().await; - let recorder = RolloutRecorder::new( - config.as_ref(), - RolloutRecorderParams::new( - session.conversation_id, - /*forked_from_id*/ None, - SessionSource::Exec, - BaseInstructions::default(), - Vec::new(), - EventPersistenceMode::Limited, - ), - /*state_db_ctx*/ None, - /*state_builder*/ None, +async fn attach_thread_persistence(session: &mut Session) -> PathBuf { + let live_thread = LiveThread::create( + Arc::clone(&session.services.thread_store), + CreateThreadParams { + thread_id: session.conversation_id, + forked_from_id: None, + source: SessionSource::Exec, + base_instructions: BaseInstructions::default(), + dynamic_tools: Vec::new(), + event_persistence_mode: ThreadEventPersistenceMode::Limited, + }, ) .await - .expect("create rollout recorder"); - let rollout_path = recorder.rollout_path().to_path_buf(); - { - let mut rollout = session.services.rollout.lock().await; - *rollout = Some(recorder); - } + .expect("create thread persistence"); + session.services.live_thread = Some(live_thread); session.ensure_rollout_materialized().await; session .flush_rollout() .await .expect("attached rollout should flush"); - rollout_path + session + .current_rollout_path() + .await + .expect("load rollout path") + .expect("thread should have rollout path") } fn text_block(s: &str) -> serde_json::Value { @@ -3217,6 +3227,9 @@ async fn session_new_fails_when_zsh_fork_enabled_without_zsh_path() { AgentControl::default(), Arc::new(codex_exec_server::EnvironmentManager::default_for_tests()), /*analytics_events_client*/ None, + Arc::new(codex_thread_store::LocalThreadStore::new( + codex_rollout::RolloutConfig::from_view(config.as_ref()), + )), RolloutTraceRecorder::disabled(), ) .await; @@ -3339,7 +3352,6 @@ pub(crate) async fn make_session_and_context() -> (Session, TurnContext) { legacy_notify_argv: config.notify.clone(), ..HooksConfig::default() }), - rollout: Mutex::new(None), rollout_trace: RolloutTraceRecorder::disabled(), user_shell: Arc::new(default_user_shell()), shell_snapshot_tx: watch::channel(None).0, @@ -3360,9 +3372,10 @@ pub(crate) async fn make_session_and_context() -> (Session, TurnContext) { network_proxy: None, network_approval: Arc::clone(&network_approval), state_db: None, - thread_store: codex_thread_store::LocalThreadStore::new( + live_thread: None, + thread_store: Arc::new(codex_thread_store::LocalThreadStore::new( codex_rollout::RolloutConfig::from_view(config.as_ref()), - ), + )), model_client: ModelClient::new( Some(auth_manager.clone()), conversation_id, @@ -3536,6 +3549,9 @@ async fn make_session_with_config_and_rx( AgentControl::default(), Arc::new(codex_exec_server::EnvironmentManager::default_for_tests()), /*analytics_events_client*/ None, + Arc::new(codex_thread_store::LocalThreadStore::new( + codex_rollout::RolloutConfig::from_view(config.as_ref()), + )), RolloutTraceRecorder::disabled(), ) .await?; @@ -4651,7 +4667,6 @@ pub(crate) async fn make_session_and_context_with_dynamic_tools_and_rx( legacy_notify_argv: config.notify.clone(), ..HooksConfig::default() }), - rollout: Mutex::new(None), rollout_trace: RolloutTraceRecorder::disabled(), user_shell: Arc::new(default_user_shell()), shell_snapshot_tx: watch::channel(None).0, @@ -4672,9 +4687,10 @@ pub(crate) async fn make_session_and_context_with_dynamic_tools_and_rx( network_proxy: None, network_approval: Arc::clone(&network_approval), state_db: None, - thread_store: codex_thread_store::LocalThreadStore::new( + live_thread: None, + thread_store: Arc::new(codex_thread_store::LocalThreadStore::new( codex_rollout::RolloutConfig::from_view(config.as_ref()), - ), + )), model_client: ModelClient::new( Some(Arc::clone(&auth_manager)), conversation_id, @@ -5568,7 +5584,7 @@ async fn record_context_updates_and_set_reference_context_item_reinjects_full_co #[tokio::test] async fn record_context_updates_and_set_reference_context_item_persists_baseline_without_emitting_diffs() { - let (session, previous_context) = make_session_and_context().await; + let (mut session, previous_context) = make_session_and_context().await; let next_model = if previous_context.model_info.slug == "gpt-5.4" { "gpt-5.2" } else { @@ -5582,27 +5598,7 @@ async fn record_context_updates_and_set_reference_context_item_persists_baseline let mut state = session.state.lock().await; state.set_reference_context_item(Some(previous_context_item.clone())); } - let config = session.get_config().await; - let recorder = RolloutRecorder::new( - config.as_ref(), - RolloutRecorderParams::new( - ThreadId::default(), - /*forked_from_id*/ None, - SessionSource::Exec, - BaseInstructions::default(), - Vec::new(), - EventPersistenceMode::Limited, - ), - /*state_db_ctx*/ None, - /*state_builder*/ None, - ) - .await - .expect("create rollout recorder"); - let rollout_path = recorder.rollout_path().to_path_buf(); - { - let mut rollout = session.services.rollout.lock().await; - *rollout = Some(recorder); - } + let rollout_path = attach_thread_persistence(&mut session).await; let update_items = session .build_settings_update_items(Some(&previous_context_item), &turn_context) @@ -5647,30 +5643,10 @@ async fn record_context_updates_and_set_reference_context_item_persists_baseline #[tokio::test] async fn record_context_updates_and_set_reference_context_item_persists_split_file_system_policy_to_rollout() { - let (session, mut turn_context) = make_session_and_context().await; + let (mut session, mut turn_context) = make_session_and_context().await; let file_system_sandbox_policy = file_system_policy_with_unreadable_glob(&turn_context); turn_context.file_system_sandbox_policy = file_system_sandbox_policy.clone(); - let config = session.get_config().await; - let recorder = RolloutRecorder::new( - config.as_ref(), - RolloutRecorderParams::new( - ThreadId::default(), - /*forked_from_id*/ None, - SessionSource::Exec, - BaseInstructions::default(), - Vec::new(), - EventPersistenceMode::Limited, - ), - /*state_db_ctx*/ None, - /*state_builder*/ None, - ) - .await - .expect("create rollout recorder"); - let rollout_path = recorder.rollout_path().to_path_buf(); - { - let mut rollout = session.services.rollout.lock().await; - *rollout = Some(recorder); - } + let rollout_path = attach_thread_persistence(&mut session).await; session .record_context_updates_and_set_reference_context_item(&turn_context) @@ -5720,7 +5696,7 @@ async fn build_initial_context_prepends_model_switch_message() { #[tokio::test] async fn record_context_updates_and_set_reference_context_item_persists_full_reinjection_to_rollout() { - let (session, previous_context) = make_session_and_context().await; + let (mut session, previous_context) = make_session_and_context().await; let next_model = if previous_context.model_info.slug == "gpt-5.4" { "gpt-5.2" } else { @@ -5729,27 +5705,7 @@ async fn record_context_updates_and_set_reference_context_item_persists_full_rei let turn_context = previous_context .with_model(next_model.to_string(), &session.services.models_manager) .await; - let config = session.get_config().await; - let recorder = RolloutRecorder::new( - config.as_ref(), - RolloutRecorderParams::new( - ThreadId::default(), - /*forked_from_id*/ None, - SessionSource::Exec, - BaseInstructions::default(), - Vec::new(), - EventPersistenceMode::Limited, - ), - /*state_db_ctx*/ None, - /*state_builder*/ None, - ) - .await - .expect("create rollout recorder"); - let rollout_path = recorder.rollout_path().to_path_buf(); - { - let mut rollout = session.services.rollout.lock().await; - *rollout = Some(recorder); - } + let rollout_path = attach_thread_persistence(&mut session).await; session .persist_rollout_items(&[RolloutItem::EventMsg(EventMsg::UserMessage( diff --git a/codex-rs/core/src/session/tests/guardian_tests.rs b/codex-rs/core/src/session/tests/guardian_tests.rs index 22db9f3937..45ebc26367 100644 --- a/codex-rs/core/src/session/tests/guardian_tests.rs +++ b/codex-rs/core/src/session/tests/guardian_tests.rs @@ -744,6 +744,9 @@ async fn guardian_subagent_does_not_inherit_parent_exec_policy_rules() { )); let mcp_manager = Arc::new(McpManager::new(Arc::clone(&plugins_manager))); let skills_watcher = Arc::new(SkillsWatcher::noop()); + let thread_store = Arc::new(codex_thread_store::LocalThreadStore::new( + codex_rollout::RolloutConfig::from_view(&config), + )); let CodexSpawnOk { codex, .. } = Codex::spawn(CodexSpawnArgs { config, @@ -768,6 +771,7 @@ async fn guardian_subagent_does_not_inherit_parent_exec_policy_rules() { user_shell_override: None, parent_trace: None, analytics_events_client: None, + thread_store, }) .await .expect("spawn guardian subagent"); diff --git a/codex-rs/core/src/state/service.rs b/codex-rs/core/src/state/service.rs index f93a44d7fe..2c62e04c8c 100644 --- a/codex-rs/core/src/state/service.rs +++ b/codex-rs/core/src/state/service.rs @@ -1,7 +1,6 @@ use std::collections::HashMap; use std::sync::Arc; -use crate::RolloutRecorder; use crate::SkillsManager; use crate::agent::AgentControl; use crate::client::ModelClient; @@ -25,7 +24,8 @@ use codex_models_manager::manager::ModelsManager; use codex_otel::SessionTelemetry; use codex_rollout::state_db::StateDbHandle; use codex_rollout_trace::RolloutTraceRecorder; -use codex_thread_store::LocalThreadStore; +use codex_thread_store::LiveThread; +use codex_thread_store::ThreadStore; use std::path::PathBuf; use tokio::runtime::Handle; use tokio::sync::Mutex; @@ -43,7 +43,6 @@ pub(crate) struct SessionServices { pub(crate) main_execve_wrapper_exe: Option, pub(crate) analytics_events_client: AnalyticsEventsClient, pub(crate) hooks: Hooks, - pub(crate) rollout: Mutex>, pub(crate) rollout_trace: RolloutTraceRecorder, pub(crate) user_shell: Arc, pub(crate) shell_snapshot_tx: watch::Sender>>, @@ -64,7 +63,8 @@ pub(crate) struct SessionServices { pub(crate) network_proxy: Option, pub(crate) network_approval: Arc, pub(crate) state_db: Option, - pub(crate) thread_store: LocalThreadStore, + pub(crate) live_thread: Option, + pub(crate) thread_store: Arc, /// Session-scoped model client shared across turns. pub(crate) model_client: ModelClient, pub(crate) code_mode_service: CodeModeService, diff --git a/codex-rs/core/src/thread_manager.rs b/codex-rs/core/src/thread_manager.rs index 4b8e9cc532..af7c3023ed 100644 --- a/codex-rs/core/src/thread_manager.rs +++ b/codex-rs/core/src/thread_manager.rs @@ -44,7 +44,11 @@ use codex_protocol::protocol::SessionSource; use codex_protocol::protocol::TurnAbortReason; use codex_protocol::protocol::TurnAbortedEvent; use codex_protocol::protocol::W3cTraceContext; +use codex_rollout::RolloutConfig; use codex_state::DirectionalThreadSpawnEdgeStatus; +use codex_thread_store::LocalThreadStore; +use codex_thread_store::RemoteThreadStore; +use codex_thread_store::ThreadStore; use codex_utils_absolute_path::AbsolutePathBuf; use futures::StreamExt; use futures::stream::FuturesUnordered; @@ -237,6 +241,13 @@ pub fn build_models_manager( )) } +fn configured_thread_store(config: &Config) -> Arc { + if let Some(endpoint) = config.experimental_thread_store_endpoint.clone() { + return Arc::new(RemoteThreadStore::new(endpoint)); + } + Arc::new(LocalThreadStore::new(RolloutConfig::from_view(config))) +} + impl ThreadManager { pub fn new( config: &Config, @@ -514,8 +525,10 @@ impl ThreadManager { metrics_service_name: Option, parent_trace: Option, ) -> CodexResult { + let thread_store = configured_thread_store(&config); Box::pin(self.state.spawn_thread( config, + thread_store, initial_history, Arc::clone(&self.state.auth_manager), self.agent_control(), @@ -554,8 +567,10 @@ impl ThreadManager { persist_extended_history: bool, parent_trace: Option, ) -> CodexResult { + let thread_store = configured_thread_store(&config); Box::pin(self.state.spawn_thread( config, + thread_store, initial_history, auth_manager, self.agent_control(), @@ -573,8 +588,10 @@ impl ThreadManager { config: Config, user_shell_override: crate::shell::Shell, ) -> CodexResult { + let thread_store = configured_thread_store(&config); Box::pin(self.state.spawn_thread( config, + thread_store, InitialHistory::New, Arc::clone(&self.state.auth_manager), self.agent_control(), @@ -595,8 +612,10 @@ impl ThreadManager { user_shell_override: crate::shell::Shell, ) -> CodexResult { let initial_history = RolloutRecorder::get_rollout_history(&rollout_path).await?; + let thread_store = configured_thread_store(&config); Box::pin(self.state.spawn_thread( config, + thread_store, initial_history, auth_manager, self.agent_control(), @@ -703,8 +722,10 @@ impl ThreadManager { } } }; + let thread_store = configured_thread_store(&config); Box::pin(self.state.spawn_thread( config, + thread_store, history, Arc::clone(&self.state.auth_manager), self.agent_control(), @@ -801,8 +822,10 @@ impl ThreadManagerState { inherited_shell_snapshot: Option>, inherited_exec_policy: Option>, ) -> CodexResult { + let thread_store = configured_thread_store(&config); Box::pin(self.spawn_thread_with_source( config, + thread_store, InitialHistory::New, Arc::clone(&self.auth_manager), agent_control, @@ -828,8 +851,10 @@ impl ThreadManagerState { inherited_exec_policy: Option>, ) -> CodexResult { let initial_history = RolloutRecorder::get_rollout_history(&rollout_path).await?; + let thread_store = configured_thread_store(&config); Box::pin(self.spawn_thread_with_source( config, + thread_store, initial_history, Arc::clone(&self.auth_manager), agent_control, @@ -856,8 +881,10 @@ impl ThreadManagerState { inherited_shell_snapshot: Option>, inherited_exec_policy: Option>, ) -> CodexResult { + let thread_store = configured_thread_store(&config); Box::pin(self.spawn_thread_with_source( config, + thread_store, initial_history, Arc::clone(&self.auth_manager), agent_control, @@ -878,6 +905,7 @@ impl ThreadManagerState { pub(crate) async fn spawn_thread( &self, config: Config, + thread_store: Arc, initial_history: InitialHistory, auth_manager: Arc, agent_control: AgentControl, @@ -889,6 +917,7 @@ impl ThreadManagerState { ) -> CodexResult { Box::pin(self.spawn_thread_with_source( config, + thread_store, initial_history, auth_manager, agent_control, @@ -908,6 +937,7 @@ impl ThreadManagerState { pub(crate) async fn spawn_thread_with_source( &self, config: Config, + thread_store: Arc, initial_history: InitialHistory, auth_manager: Arc, agent_control: AgentControl, @@ -957,6 +987,7 @@ impl ThreadManagerState { user_shell_override, parent_trace, analytics_events_client: self.analytics_events_client.clone(), + thread_store, }) .await?; self.finalize_thread_spawn(codex, thread_id, watch_registration) diff --git a/codex-rs/core/src/tools/context.rs b/codex-rs/core/src/tools/context.rs index feb4b02d71..63bb641ef8 100644 --- a/codex-rs/core/src/tools/context.rs +++ b/codex-rs/core/src/tools/context.rs @@ -132,6 +132,7 @@ impl ToolOutput for CallToolResult { #[derive(Clone, Debug)] pub struct McpToolOutput { pub result: CallToolResult, + pub tool_input: JsonValue, pub wall_time: Duration, pub original_image_detail_supported: bool, } @@ -162,6 +163,10 @@ impl ToolOutput for McpToolOutput { JsonValue::String(format!("failed to serialize mcp result: {err}")) }) } + + fn post_tool_use_response(&self, _call_id: &str, _payload: &ToolPayload) -> Option { + serde_json::to_value(&self.result).ok() + } } impl McpToolOutput { diff --git a/codex-rs/core/src/tools/context_tests.rs b/codex-rs/core/src/tools/context_tests.rs index 60ad456151..4f40342e49 100644 --- a/codex-rs/core/src/tools/context_tests.rs +++ b/codex-rs/core/src/tools/context_tests.rs @@ -98,6 +98,7 @@ fn mcp_tool_output_response_item_includes_wall_time() { is_error: Some(false), meta: None, }, + tool_input: json!({}), wall_time: std::time::Duration::from_millis(1250), original_image_detail_supported: false, }; @@ -150,6 +151,7 @@ fn mcp_tool_output_response_item_preserves_content_items() { is_error: Some(false), meta: None, }, + tool_input: json!({}), wall_time: std::time::Duration::from_millis(500), original_image_detail_supported: false, }; @@ -203,6 +205,7 @@ fn mcp_tool_output_code_mode_result_stays_raw_call_tool_result() { is_error: Some(false), meta: None, }, + tool_input: json!({}), wall_time: std::time::Duration::from_millis(1250), original_image_detail_supported: false, }; diff --git a/codex-rs/core/src/tools/handlers/apply_patch.rs b/codex-rs/core/src/tools/handlers/apply_patch.rs index 0ccc62d9e9..91d5543c0f 100644 --- a/codex-rs/core/src/tools/handlers/apply_patch.rs +++ b/codex-rs/core/src/tools/handlers/apply_patch.rs @@ -316,21 +316,23 @@ impl ToolHandler for ApplyPatchHandler { fn pre_tool_use_payload(&self, invocation: &ToolInvocation) -> Option { apply_patch_payload_command(&invocation.payload).map(|command| PreToolUsePayload { tool_name: HookToolName::apply_patch(), - command, + tool_input: serde_json::json!({ "command": command }), }) } fn post_tool_use_payload( &self, - call_id: &str, - payload: &ToolPayload, + invocation: &ToolInvocation, result: &Self::Output, ) -> Option { - let tool_response = result.post_tool_use_response(call_id, payload)?; + let tool_response = + result.post_tool_use_response(&invocation.call_id, &invocation.payload)?; Some(PostToolUsePayload { tool_name: HookToolName::apply_patch(), - tool_use_id: call_id.to_string(), - command: apply_patch_payload_command(payload)?, + tool_use_id: invocation.call_id.clone(), + tool_input: serde_json::json!({ + "command": apply_patch_payload_command(&invocation.payload)?, + }), tool_response, }) } diff --git a/codex-rs/core/src/tools/handlers/apply_patch_tests.rs b/codex-rs/core/src/tools/handlers/apply_patch_tests.rs index 39c3133103..b1b9d0cbbe 100644 --- a/codex-rs/core/src/tools/handlers/apply_patch_tests.rs +++ b/codex-rs/core/src/tools/handlers/apply_patch_tests.rs @@ -54,7 +54,7 @@ async fn pre_tool_use_payload_uses_json_patch_input() { handler.pre_tool_use_payload(&invocation), Some(PreToolUsePayload { tool_name: HookToolName::apply_patch(), - command: patch.to_string(), + tool_input: json!({ "command": patch }), }) ); } @@ -72,26 +72,27 @@ async fn pre_tool_use_payload_uses_freeform_patch_input() { handler.pre_tool_use_payload(&invocation), Some(PreToolUsePayload { tool_name: HookToolName::apply_patch(), - command: patch.to_string(), + tool_input: json!({ "command": patch }), }) ); } -#[test] -fn post_tool_use_payload_uses_patch_input_and_tool_output() { +#[tokio::test] +async fn post_tool_use_payload_uses_patch_input_and_tool_output() { let patch = sample_patch(); let payload = ToolPayload::Custom { input: patch.to_string(), }; + let invocation = invocation_for_payload(payload).await; let output = ApplyPatchToolOutput::from_text("Success. Updated files.".to_string()); let handler = ApplyPatchHandler; assert_eq!( - handler.post_tool_use_payload("call-apply-patch", &payload, &output), + handler.post_tool_use_payload(&invocation, &output), Some(PostToolUsePayload { tool_name: HookToolName::apply_patch(), tool_use_id: "call-apply-patch".to_string(), - command: patch.to_string(), + tool_input: json!({ "command": patch }), tool_response: json!("Success. Updated files."), }) ); diff --git a/codex-rs/core/src/tools/handlers/mcp.rs b/codex-rs/core/src/tools/handlers/mcp.rs index b24db734b9..0de45ea6bc 100644 --- a/codex-rs/core/src/tools/handlers/mcp.rs +++ b/codex-rs/core/src/tools/handlers/mcp.rs @@ -6,9 +6,14 @@ use crate::mcp_tool_call::handle_mcp_tool_call; use crate::original_image_detail::can_request_original_image_detail; use crate::tools::context::McpToolOutput; use crate::tools::context::ToolInvocation; +use crate::tools::context::ToolOutput; use crate::tools::context::ToolPayload; +use crate::tools::hook_names::HookToolName; +use crate::tools::registry::PostToolUsePayload; +use crate::tools::registry::PreToolUsePayload; use crate::tools::registry::ToolHandler; use crate::tools::registry::ToolKind; +use serde_json::Value; pub struct McpHandler; impl ToolHandler for McpHandler { @@ -18,11 +23,42 @@ impl ToolHandler for McpHandler { ToolKind::Mcp } + fn pre_tool_use_payload(&self, invocation: &ToolInvocation) -> Option { + let ToolPayload::Mcp { raw_arguments, .. } = &invocation.payload else { + return None; + }; + + Some(PreToolUsePayload { + tool_name: HookToolName::new(invocation.tool_name.display()), + tool_input: mcp_hook_tool_input(raw_arguments), + }) + } + + fn post_tool_use_payload( + &self, + invocation: &ToolInvocation, + result: &Self::Output, + ) -> Option { + let ToolPayload::Mcp { .. } = &invocation.payload else { + return None; + }; + + let tool_response = + result.post_tool_use_response(&invocation.call_id, &invocation.payload)?; + Some(PostToolUsePayload { + tool_name: HookToolName::new(invocation.tool_name.display()), + tool_use_id: invocation.call_id.clone(), + tool_input: result.tool_input.clone(), + tool_response, + }) + } + async fn handle(&self, invocation: ToolInvocation) -> Result { let ToolInvocation { session, turn, call_id, + tool_name: model_tool_name, payload, .. } = invocation; @@ -50,14 +86,133 @@ impl ToolHandler for McpHandler { call_id.clone(), server, tool, + model_tool_name.display(), arguments_str, ) .await; Ok(McpToolOutput { - result, + result: result.result, + tool_input: result.tool_input, wall_time: started.elapsed(), original_image_detail_supported: can_request_original_image_detail(&turn.model_info), }) } } + +fn mcp_hook_tool_input(raw_arguments: &str) -> Value { + if raw_arguments.trim().is_empty() { + return Value::Object(serde_json::Map::new()); + } + + serde_json::from_str(raw_arguments).unwrap_or_else(|_| Value::String(raw_arguments.to_string())) +} + +#[cfg(test)] +mod tests { + use super::*; + use crate::session::tests::make_session_and_context; + use crate::turn_diff_tracker::TurnDiffTracker; + use pretty_assertions::assert_eq; + use serde_json::json; + use std::time::Duration; + use tokio::sync::Mutex; + + #[tokio::test] + async fn mcp_pre_tool_use_payload_uses_model_tool_name_and_raw_args() { + let payload = ToolPayload::Mcp { + server: "memory".to_string(), + tool: "create_entities".to_string(), + raw_arguments: json!({ + "entities": [{ + "name": "Ada", + "entityType": "person" + }] + }) + .to_string(), + }; + let (session, turn) = make_session_and_context().await; + + assert_eq!( + McpHandler.pre_tool_use_payload(&ToolInvocation { + session: session.into(), + turn: turn.into(), + cancellation_token: tokio_util::sync::CancellationToken::new(), + tracker: Arc::new(Mutex::new(TurnDiffTracker::new())), + call_id: "call-mcp-pre".to_string(), + tool_name: codex_tools::ToolName::namespaced("mcp__memory__", "create_entities"), + payload, + }), + Some(PreToolUsePayload { + tool_name: HookToolName::new("mcp__memory__create_entities"), + tool_input: json!({ + "entities": [{ + "name": "Ada", + "entityType": "person" + }] + }), + }) + ); + } + + #[tokio::test] + async fn mcp_post_tool_use_payload_uses_model_tool_name_args_and_result() { + let payload = ToolPayload::Mcp { + server: "filesystem".to_string(), + tool: "read_file".to_string(), + raw_arguments: json!({ "path": "/tmp/notes.txt" }).to_string(), + }; + let output = McpToolOutput { + result: codex_protocol::mcp::CallToolResult { + content: vec![json!({ + "type": "text", + "text": "notes" + })], + structured_content: Some(json!({ "bytes": 5 })), + is_error: None, + meta: None, + }, + tool_input: json!({ + "path": { + "file_id": "file_123" + } + }), + wall_time: Duration::from_millis(42), + original_image_detail_supported: true, + }; + let (session, turn) = make_session_and_context().await; + let invocation = ToolInvocation { + session: session.into(), + turn: turn.into(), + cancellation_token: tokio_util::sync::CancellationToken::new(), + tracker: Arc::new(Mutex::new(TurnDiffTracker::new())), + call_id: "call-mcp-post".to_string(), + tool_name: codex_tools::ToolName::namespaced("mcp__filesystem__", "read_file"), + payload, + }; + assert_eq!( + McpHandler.post_tool_use_payload(&invocation, &output), + Some(PostToolUsePayload { + tool_name: HookToolName::new("mcp__filesystem__read_file"), + tool_use_id: "call-mcp-post".to_string(), + tool_input: json!({ + "path": { + "file_id": "file_123" + } + }), + tool_response: json!({ + "content": [{ + "type": "text", + "text": "notes" + }], + "structuredContent": { "bytes": 5 } + }), + }) + ); + } + + #[test] + fn mcp_hook_tool_input_defaults_empty_args_to_object() { + assert_eq!(mcp_hook_tool_input(" "), json!({})); + } +} diff --git a/codex-rs/core/src/tools/handlers/multi_agents_v2/spawn.rs b/codex-rs/core/src/tools/handlers/multi_agents_v2/spawn.rs index fabb01b7d4..00986311a2 100644 --- a/codex-rs/core/src/tools/handlers/multi_agents_v2/spawn.rs +++ b/codex-rs/core/src/tools/handlers/multi_agents_v2/spawn.rs @@ -5,8 +5,6 @@ use crate::agent::control::render_input_preview; use crate::agent::next_thread_spawn_depth; use crate::agent::role::DEFAULT_ROLE_NAME; use crate::agent::role::apply_role_to_config; -use crate::context::ContextualUserFragment; -use crate::context::SpawnAgentInstructions; use codex_protocol::AgentPath; use codex_protocol::protocol::InterAgentCommunication; use codex_protocol::protocol::Op; @@ -88,18 +86,6 @@ impl ToolHandler for Handler { } apply_spawn_agent_runtime_overrides(&mut config, turn.as_ref())?; apply_spawn_agent_overrides(&mut config, child_depth); - let spawn_agent_instructions = SpawnAgentInstructions.render(); - config.developer_instructions = Some( - if let Some(mut existing_instructions) = config.developer_instructions.take() { - if !existing_instructions.ends_with('\n') { - existing_instructions.push('\n'); - } - existing_instructions.push_str(&spawn_agent_instructions); - existing_instructions - } else { - spawn_agent_instructions - }, - ); let spawn_source = thread_spawn_source( session.conversation_id, diff --git a/codex-rs/core/src/tools/handlers/shell.rs b/codex-rs/core/src/tools/handlers/shell.rs index b9718173b7..f4ab61b524 100644 --- a/codex-rs/core/src/tools/handlers/shell.rs +++ b/codex-rs/core/src/tools/handlers/shell.rs @@ -208,21 +208,22 @@ impl ToolHandler for ShellHandler { fn pre_tool_use_payload(&self, invocation: &ToolInvocation) -> Option { shell_payload_command(&invocation.payload).map(|command| PreToolUsePayload { tool_name: HookToolName::bash(), - command, + tool_input: serde_json::json!({ "command": command }), }) } fn post_tool_use_payload( &self, - call_id: &str, - payload: &ToolPayload, + invocation: &ToolInvocation, result: &Self::Output, ) -> Option { - let tool_response = result.post_tool_use_response(call_id, payload)?; + let tool_response = + result.post_tool_use_response(&invocation.call_id, &invocation.payload)?; + let command = shell_payload_command(&invocation.payload)?; Some(PostToolUsePayload { tool_name: HookToolName::bash(), - tool_use_id: call_id.to_string(), - command: shell_payload_command(payload)?, + tool_use_id: invocation.call_id.clone(), + tool_input: serde_json::json!({ "command": command }), tool_response, }) } @@ -321,21 +322,22 @@ impl ToolHandler for ShellCommandHandler { fn pre_tool_use_payload(&self, invocation: &ToolInvocation) -> Option { shell_command_payload_command(&invocation.payload).map(|command| PreToolUsePayload { tool_name: HookToolName::bash(), - command, + tool_input: serde_json::json!({ "command": command }), }) } fn post_tool_use_payload( &self, - call_id: &str, - payload: &ToolPayload, + invocation: &ToolInvocation, result: &Self::Output, ) -> Option { - let tool_response = result.post_tool_use_response(call_id, payload)?; + let tool_response = + result.post_tool_use_response(&invocation.call_id, &invocation.payload)?; + let command = shell_command_payload_command(&invocation.payload)?; Some(PostToolUsePayload { tool_name: HookToolName::bash(), - tool_use_id: call_id.to_string(), - command: shell_command_payload_command(payload)?, + tool_use_id: invocation.call_id.clone(), + tool_input: serde_json::json!({ "command": command }), tool_response, }) } diff --git a/codex-rs/core/src/tools/handlers/shell_tests.rs b/codex-rs/core/src/tools/handlers/shell_tests.rs index c13d834c06..83f52d0ea5 100644 --- a/codex-rs/core/src/tools/handlers/shell_tests.rs +++ b/codex-rs/core/src/tools/handlers/shell_tests.rs @@ -234,7 +234,7 @@ async fn shell_pre_tool_use_payload_uses_joined_command() { }), Some(crate::tools::registry::PreToolUsePayload { tool_name: HookToolName::bash(), - command: "bash -lc 'printf hi'".to_string(), + tool_input: json!({ "command": "bash -lc 'printf hi'" }), }) ); } @@ -261,13 +261,13 @@ async fn shell_command_pre_tool_use_payload_uses_raw_command() { }), Some(crate::tools::registry::PreToolUsePayload { tool_name: HookToolName::bash(), - command: "printf shell command".to_string(), + tool_input: json!({ "command": "printf shell command" }), }) ); } -#[test] -fn build_post_tool_use_payload_uses_tool_output_wire_value() { +#[tokio::test] +async fn build_post_tool_use_payload_uses_tool_output_wire_value() { let payload = ToolPayload::Function { arguments: json!({ "command": "printf shell command" }).to_string(), }; @@ -279,13 +279,22 @@ fn build_post_tool_use_payload_uses_tool_output_wire_value() { let handler = ShellCommandHandler { backend: super::ShellCommandBackend::Classic, }; - + let (session, turn) = make_session_and_context().await; + let invocation = ToolInvocation { + session: session.into(), + turn: turn.into(), + cancellation_token: tokio_util::sync::CancellationToken::new(), + tracker: Arc::new(Mutex::new(TurnDiffTracker::new())), + call_id: "call-42".to_string(), + tool_name: codex_tools::ToolName::plain("shell_command"), + payload, + }; assert_eq!( - handler.post_tool_use_payload("call-42", &payload, &output), + handler.post_tool_use_payload(&invocation, &output), Some(crate::tools::registry::PostToolUsePayload { tool_name: HookToolName::bash(), tool_use_id: "call-42".to_string(), - command: "printf shell command".to_string(), + tool_input: json!({ "command": "printf shell command" }), tool_response: json!("shell output"), }) ); diff --git a/codex-rs/core/src/tools/handlers/unified_exec.rs b/codex-rs/core/src/tools/handlers/unified_exec.rs index 813f4f8628..05b54ff884 100644 --- a/codex-rs/core/src/tools/handlers/unified_exec.rs +++ b/codex-rs/core/src/tools/handlers/unified_exec.rs @@ -139,31 +139,30 @@ impl ToolHandler for UnifiedExecHandler { .ok() .map(|args| PreToolUsePayload { tool_name: HookToolName::bash(), - command: args.cmd, + tool_input: serde_json::json!({ "command": args.cmd }), }) } fn post_tool_use_payload( &self, - call_id: &str, - payload: &ToolPayload, + invocation: &ToolInvocation, result: &Self::Output, ) -> Option { - let ToolPayload::Function { .. } = payload else { + let ToolPayload::Function { .. } = &invocation.payload else { return None; }; let command = result.hook_command.clone()?; let tool_use_id = if result.event_call_id.is_empty() { - call_id.to_string() + invocation.call_id.clone() } else { result.event_call_id.clone() }; - let tool_response = result.post_tool_use_response(&tool_use_id, payload)?; + let tool_response = result.post_tool_use_response(&tool_use_id, &invocation.payload)?; Some(PostToolUsePayload { tool_name: HookToolName::bash(), tool_use_id, - command, + tool_input: serde_json::json!({ "command": command }), tool_response, }) } diff --git a/codex-rs/core/src/tools/handlers/unified_exec_tests.rs b/codex-rs/core/src/tools/handlers/unified_exec_tests.rs index 0a5abe51d7..0dd656bd65 100644 --- a/codex-rs/core/src/tools/handlers/unified_exec_tests.rs +++ b/codex-rs/core/src/tools/handlers/unified_exec_tests.rs @@ -22,6 +22,23 @@ use crate::tools::registry::ToolHandler; use crate::turn_diff_tracker::TurnDiffTracker; use tokio::sync::Mutex; +async fn invocation_for_payload( + tool_name: &str, + call_id: &str, + payload: ToolPayload, +) -> ToolInvocation { + let (session, turn) = make_session_and_context().await; + ToolInvocation { + session: session.into(), + turn: turn.into(), + cancellation_token: tokio_util::sync::CancellationToken::new(), + tracker: Arc::new(Mutex::new(TurnDiffTracker::new())), + call_id: call_id.to_string(), + tool_name: codex_tools::ToolName::plain(tool_name), + payload, + } +} + #[test] fn test_get_command_uses_default_shell_when_unspecified() -> anyhow::Result<()> { let json = r#"{"cmd": "echo hello"}"#; @@ -219,7 +236,7 @@ async fn exec_command_pre_tool_use_payload_uses_raw_command() { }), Some(crate::tools::registry::PreToolUsePayload { tool_name: HookToolName::bash(), - command: "printf exec command".to_string(), + tool_input: serde_json::json!({ "command": "printf exec command" }), }) ); } @@ -246,8 +263,8 @@ async fn exec_command_pre_tool_use_payload_skips_write_stdin() { ); } -#[test] -fn exec_command_post_tool_use_payload_uses_output_for_noninteractive_one_shot_commands() { +#[tokio::test] +async fn exec_command_post_tool_use_payload_uses_output_for_noninteractive_one_shot_commands() { let payload = ToolPayload::Function { arguments: serde_json::json!({ "cmd": "echo three", "tty": false }).to_string(), }; @@ -262,20 +279,20 @@ fn exec_command_post_tool_use_payload_uses_output_for_noninteractive_one_shot_co original_token_count: None, hook_command: Some("echo three".to_string()), }; - + let invocation = invocation_for_payload("exec_command", "call-43", payload).await; assert_eq!( - UnifiedExecHandler.post_tool_use_payload("call-43", &payload, &output), + UnifiedExecHandler.post_tool_use_payload(&invocation, &output), Some(crate::tools::registry::PostToolUsePayload { tool_name: HookToolName::bash(), tool_use_id: "call-43".to_string(), - command: "echo three".to_string(), + tool_input: serde_json::json!({ "command": "echo three" }), tool_response: serde_json::json!("three"), }) ); } -#[test] -fn exec_command_post_tool_use_payload_uses_output_for_interactive_completion() { +#[tokio::test] +async fn exec_command_post_tool_use_payload_uses_output_for_interactive_completion() { let payload = ToolPayload::Function { arguments: serde_json::json!({ "cmd": "echo three", "tty": true }).to_string(), }; @@ -290,20 +307,21 @@ fn exec_command_post_tool_use_payload_uses_output_for_interactive_completion() { original_token_count: None, hook_command: Some("echo three".to_string()), }; + let invocation = invocation_for_payload("exec_command", "call-44", payload).await; assert_eq!( - UnifiedExecHandler.post_tool_use_payload("call-44", &payload, &output), + UnifiedExecHandler.post_tool_use_payload(&invocation, &output), Some(crate::tools::registry::PostToolUsePayload { tool_name: HookToolName::bash(), tool_use_id: "call-44".to_string(), - command: "echo three".to_string(), + tool_input: serde_json::json!({ "command": "echo three" }), tool_response: serde_json::json!("three"), }) ); } -#[test] -fn exec_command_post_tool_use_payload_skips_running_sessions() { +#[tokio::test] +async fn exec_command_post_tool_use_payload_skips_running_sessions() { let payload = ToolPayload::Function { arguments: serde_json::json!({ "cmd": "echo three", "tty": false }).to_string(), }; @@ -318,15 +336,15 @@ fn exec_command_post_tool_use_payload_skips_running_sessions() { original_token_count: None, hook_command: Some("echo three".to_string()), }; - + let invocation = invocation_for_payload("exec_command", "call-45", payload).await; assert_eq!( - UnifiedExecHandler.post_tool_use_payload("call-45", &payload, &output), + UnifiedExecHandler.post_tool_use_payload(&invocation, &output), None ); } -#[test] -fn write_stdin_post_tool_use_payload_uses_original_exec_call_id_and_command_on_completion() { +#[tokio::test] +async fn write_stdin_post_tool_use_payload_uses_original_exec_call_id_and_command_on_completion() { let payload = ToolPayload::Function { arguments: serde_json::json!({ "session_id": 45, @@ -345,20 +363,21 @@ fn write_stdin_post_tool_use_payload_uses_original_exec_call_id_and_command_on_c original_token_count: None, hook_command: Some("sleep 1; echo finished".to_string()), }; + let invocation = invocation_for_payload("write_stdin", "write-stdin-call", payload).await; assert_eq!( - UnifiedExecHandler.post_tool_use_payload("write-stdin-call", &payload, &output), + UnifiedExecHandler.post_tool_use_payload(&invocation, &output), Some(crate::tools::registry::PostToolUsePayload { tool_name: HookToolName::bash(), tool_use_id: "exec-call-45".to_string(), - command: "sleep 1; echo finished".to_string(), + tool_input: serde_json::json!({ "command": "sleep 1; echo finished" }), tool_response: serde_json::json!("finished\n"), }) ); } -#[test] -fn write_stdin_post_tool_use_payload_keeps_parallel_session_metadata_separate() { +#[tokio::test] +async fn write_stdin_post_tool_use_payload_keeps_parallel_session_metadata_separate() { let payload = ToolPayload::Function { arguments: serde_json::json!({ "session_id": 45, "chars": "" }).to_string(), }; @@ -384,10 +403,12 @@ fn write_stdin_post_tool_use_payload_keeps_parallel_session_metadata_separate() original_token_count: None, hook_command: Some("sleep 1; echo beta".to_string()), }; + let invocation_b = invocation_for_payload("write_stdin", "write-call-b", payload.clone()).await; + let invocation_a = invocation_for_payload("write_stdin", "write-call-a", payload).await; let payloads = [ - UnifiedExecHandler.post_tool_use_payload("write-call-b", &payload, &output_b), - UnifiedExecHandler.post_tool_use_payload("write-call-a", &payload, &output_a), + UnifiedExecHandler.post_tool_use_payload(&invocation_b, &output_b), + UnifiedExecHandler.post_tool_use_payload(&invocation_a, &output_a), ]; assert_eq!( @@ -396,13 +417,13 @@ fn write_stdin_post_tool_use_payload_keeps_parallel_session_metadata_separate() Some(crate::tools::registry::PostToolUsePayload { tool_name: HookToolName::bash(), tool_use_id: "exec-call-b".to_string(), - command: "sleep 1; echo beta".to_string(), + tool_input: serde_json::json!({ "command": "sleep 1; echo beta" }), tool_response: serde_json::json!("beta\n"), }), Some(crate::tools::registry::PostToolUsePayload { tool_name: HookToolName::bash(), tool_use_id: "exec-call-a".to_string(), - command: "sleep 2; echo alpha".to_string(), + tool_input: serde_json::json!({ "command": "sleep 2; echo alpha" }), tool_response: serde_json::json!("alpha\n"), }), ] diff --git a/codex-rs/core/src/tools/network_approval.rs b/codex-rs/core/src/tools/network_approval.rs index d43859e626..888add09fc 100644 --- a/codex-rs/core/src/tools/network_approval.rs +++ b/codex-rs/core/src/tools/network_approval.rs @@ -8,7 +8,6 @@ use crate::guardian::routes_approval_to_guardian; use crate::hook_runtime::run_permission_request_hooks; use crate::network_policy_decision::denied_network_policy_message; use crate::session::session::Session; -use crate::tools::hook_names::HookToolName; use crate::tools::sandboxing::PermissionRequestPayload; use crate::tools::sandboxing::ToolError; use codex_hooks::PermissionRequestDecision; @@ -393,11 +392,7 @@ impl NetworkApprovalService { &session, &turn_context, &guardian_approval_id, - PermissionRequestPayload { - tool_name: HookToolName::bash(), - command, - description: Some(format!("network-access {target}")), - }, + PermissionRequestPayload::bash(command, Some(format!("network-access {target}"))), ) .await { diff --git a/codex-rs/core/src/tools/registry.rs b/codex-rs/core/src/tools/registry.rs index 678e620bd8..523d8ed650 100644 --- a/codex-rs/core/src/tools/registry.rs +++ b/codex-rs/core/src/tools/registry.rs @@ -70,8 +70,7 @@ pub trait ToolHandler: Send + Sync { fn post_tool_use_payload( &self, - _call_id: &str, - _payload: &ToolPayload, + _invocation: &ToolInvocation, _result: &Self::Output, ) -> Option { None @@ -136,8 +135,11 @@ pub(crate) struct PreToolUsePayload { /// The canonical name is serialized to hook stdin, while aliases are used /// only for matcher compatibility. pub(crate) tool_name: HookToolName, - /// Command-shaped input exposed at `tool_input.command`. - pub(crate) command: String, + /// Tool-specific input exposed at `tool_input`. + /// + /// Shell-like tools use `{ "command": ... }`; MCP tools use their resolved + /// JSON arguments. + pub(crate) tool_input: Value, } #[derive(Debug, Clone, PartialEq)] @@ -149,8 +151,8 @@ pub(crate) struct PostToolUsePayload { pub(crate) tool_name: HookToolName, /// The originating tool-use id exposed at `tool_use_id`. pub(crate) tool_use_id: String, - /// Command-shaped input exposed at `tool_input.command`. - pub(crate) command: String, + /// Tool-specific input exposed at `tool_input`. + pub(crate) tool_input: Value, /// Tool result exposed at `tool_response`. pub(crate) tool_response: Value, } @@ -195,9 +197,9 @@ where Box::pin(async move { let call_id = invocation.call_id.clone(); let payload = invocation.payload.clone(); - let output = self.handle(invocation).await?; + let output = self.handle(invocation.clone()).await?; let post_tool_use_payload = - ToolHandler::post_tool_use_payload(self, &call_id, &payload, &output); + ToolHandler::post_tool_use_payload(self, &invocation, &output); Ok(AnyToolResult { call_id, payload, @@ -328,20 +330,16 @@ impl ToolRegistry { } if let Some(pre_tool_use_payload) = handler.pre_tool_use_payload(&invocation) - && let Some(reason) = run_pre_tool_use_hooks( + && let Some(message) = run_pre_tool_use_hooks( &invocation.session, &invocation.turn, invocation.call_id.clone(), - pre_tool_use_payload.tool_name.name().to_string(), - pre_tool_use_payload.tool_name.matcher_aliases().to_vec(), - pre_tool_use_payload.command.clone(), + &pre_tool_use_payload.tool_name, + &pre_tool_use_payload.tool_input, ) .await { - return Err(FunctionCallError::RespondToModel(format!( - "Command blocked by PreToolUse hook: {reason}. Command: {}", - pre_tool_use_payload.command - ))); + return Err(FunctionCallError::RespondToModel(message)); } let is_mutating = handler.is_mutating(&invocation).await; @@ -402,7 +400,7 @@ impl ToolRegistry { post_tool_use_payload.tool_use_id, post_tool_use_payload.tool_name.name().to_string(), post_tool_use_payload.tool_name.matcher_aliases().to_vec(), - post_tool_use_payload.command, + post_tool_use_payload.tool_input, post_tool_use_payload.tool_response, ) .await, diff --git a/codex-rs/core/src/tools/runtimes/apply_patch.rs b/codex-rs/core/src/tools/runtimes/apply_patch.rs index 5de3235f83..ed325a4e68 100644 --- a/codex-rs/core/src/tools/runtimes/apply_patch.rs +++ b/codex-rs/core/src/tools/runtimes/apply_patch.rs @@ -204,8 +204,7 @@ impl Approvable for ApplyPatchRuntime { ) -> Option { Some(PermissionRequestPayload { tool_name: HookToolName::apply_patch(), - command: req.action.patch.clone(), - description: None, + tool_input: serde_json::json!({ "command": req.action.patch }), }) } } diff --git a/codex-rs/core/src/tools/runtimes/apply_patch_tests.rs b/codex-rs/core/src/tools/runtimes/apply_patch_tests.rs index fea640b448..112a3d3a56 100644 --- a/codex-rs/core/src/tools/runtimes/apply_patch_tests.rs +++ b/codex-rs/core/src/tools/runtimes/apply_patch_tests.rs @@ -105,8 +105,10 @@ fn permission_request_payload_uses_apply_patch_hook_name_and_aliases() { payload.tool_name.matcher_aliases(), &["Write".to_string(), "Edit".to_string()] ); - assert_eq!(payload.command, expected_patch); - assert_eq!(payload.description, None); + assert_eq!( + payload.tool_input, + serde_json::json!({ "command": expected_patch }) + ); } #[test] diff --git a/codex-rs/core/src/tools/runtimes/shell.rs b/codex-rs/core/src/tools/runtimes/shell.rs index 4bbbe774a7..e18d2c269a 100644 --- a/codex-rs/core/src/tools/runtimes/shell.rs +++ b/codex-rs/core/src/tools/runtimes/shell.rs @@ -17,7 +17,6 @@ use crate::sandboxing::ExecOptions; use crate::sandboxing::SandboxPermissions; use crate::sandboxing::execute_env; use crate::shell::ShellType; -use crate::tools::hook_names::HookToolName; use crate::tools::network_approval::NetworkApprovalMode; use crate::tools::network_approval::NetworkApprovalSpec; use crate::tools::runtimes::build_sandbox_command; @@ -202,11 +201,10 @@ impl Approvable for ShellRuntime { } fn permission_request_payload(&self, req: &ShellRequest) -> Option { - Some(PermissionRequestPayload { - tool_name: HookToolName::bash(), - command: req.hook_command.clone(), - description: req.justification.clone(), - }) + Some(PermissionRequestPayload::bash( + req.hook_command.clone(), + req.justification.clone(), + )) } fn sandbox_mode_for_first_attempt(&self, req: &ShellRequest) -> SandboxOverride { diff --git a/codex-rs/core/src/tools/runtimes/shell/unix_escalation.rs b/codex-rs/core/src/tools/runtimes/shell/unix_escalation.rs index 361de9a96b..83075c3674 100644 --- a/codex-rs/core/src/tools/runtimes/shell/unix_escalation.rs +++ b/codex-rs/core/src/tools/runtimes/shell/unix_escalation.rs @@ -13,7 +13,6 @@ use crate::sandboxing::ExecOptions; use crate::sandboxing::ExecRequest; use crate::sandboxing::SandboxPermissions; use crate::shell::ShellType; -use crate::tools::hook_names::HookToolName; use crate::tools::runtimes::build_sandbox_command; use crate::tools::sandboxing::PermissionRequestPayload; use crate::tools::sandboxing::SandboxAttempt; @@ -402,11 +401,10 @@ impl CoreShellActionProvider { Ok(stopwatch .pause_for(async move { // 1) Run PermissionRequest hooks - let permission_request = PermissionRequestPayload { - tool_name: HookToolName::bash(), - command: codex_shell_command::parse_command::shlex_join(&command), - description: None, - }; + let permission_request = PermissionRequestPayload::bash( + codex_shell_command::parse_command::shlex_join(&command), + /*description*/ None, + ); let effective_approval_id = approval_id.clone().unwrap_or_else(|| call_id.clone()); match run_permission_request_hooks( &session, diff --git a/codex-rs/core/src/tools/runtimes/unified_exec.rs b/codex-rs/core/src/tools/runtimes/unified_exec.rs index 1a4132da66..96d7abf64c 100644 --- a/codex-rs/core/src/tools/runtimes/unified_exec.rs +++ b/codex-rs/core/src/tools/runtimes/unified_exec.rs @@ -14,7 +14,6 @@ use crate::sandboxing::ExecOptions; use crate::sandboxing::ExecServerEnvConfig; use crate::sandboxing::SandboxPermissions; use crate::shell::ShellType; -use crate::tools::hook_names::HookToolName; use crate::tools::network_approval::NetworkApprovalMode; use crate::tools::network_approval::NetworkApprovalSpec; use crate::tools::runtimes::build_sandbox_command; @@ -187,11 +186,10 @@ impl Approvable for UnifiedExecRuntime<'_> { &self, req: &UnifiedExecRequest, ) -> Option { - Some(PermissionRequestPayload { - tool_name: HookToolName::bash(), - command: req.hook_command.clone(), - description: req.justification.clone(), - }) + Some(PermissionRequestPayload::bash( + req.hook_command.clone(), + req.justification.clone(), + )) } fn sandbox_mode_for_first_attempt(&self, req: &UnifiedExecRequest) -> SandboxOverride { diff --git a/codex-rs/core/src/tools/sandboxing.rs b/codex-rs/core/src/tools/sandboxing.rs index fcdcd78507..922ce6a1d7 100644 --- a/codex-rs/core/src/tools/sandboxing.rs +++ b/codex-rs/core/src/tools/sandboxing.rs @@ -135,8 +135,25 @@ pub(crate) struct ApprovalCtx<'a> { #[derive(Clone, Debug, PartialEq, Eq)] pub(crate) struct PermissionRequestPayload { pub tool_name: HookToolName, - pub command: String, - pub description: Option, + pub tool_input: serde_json::Value, +} + +impl PermissionRequestPayload { + pub(crate) fn bash(command: String, description: Option) -> Self { + let mut tool_input = serde_json::Map::new(); + tool_input.insert("command".to_string(), serde_json::Value::String(command)); + if let Some(description) = description { + tool_input.insert( + "description".to_string(), + serde_json::Value::String(description), + ); + } + + Self { + tool_name: HookToolName::bash(), + tool_input: serde_json::Value::Object(tool_input), + } + } } // Specifies what tool orchestrator should do with a given tool call. diff --git a/codex-rs/core/src/tools/sandboxing_tests.rs b/codex-rs/core/src/tools/sandboxing_tests.rs index 4a4dac3e81..19eb7a67d9 100644 --- a/codex-rs/core/src/tools/sandboxing_tests.rs +++ b/codex-rs/core/src/tools/sandboxing_tests.rs @@ -1,8 +1,38 @@ use super::*; use crate::sandboxing::SandboxPermissions; +use crate::tools::hook_names::HookToolName; use codex_protocol::protocol::GranularApprovalConfig; use codex_protocol::protocol::NetworkAccess; use pretty_assertions::assert_eq; +use serde_json::json; + +#[test] +fn bash_permission_request_payload_omits_missing_description() { + assert_eq!( + PermissionRequestPayload::bash("echo hi".to_string(), /*description*/ None), + PermissionRequestPayload { + tool_name: HookToolName::bash(), + tool_input: json!({ "command": "echo hi" }), + } + ); +} + +#[test] +fn bash_permission_request_payload_includes_description_when_present() { + assert_eq!( + PermissionRequestPayload::bash( + "echo hi".to_string(), + Some("network-access example.com".to_string()), + ), + PermissionRequestPayload { + tool_name: HookToolName::bash(), + tool_input: json!({ + "command": "echo hi", + "description": "network-access example.com", + }), + } + ); +} #[test] fn external_sandbox_skips_exec_approval_on_request() { diff --git a/codex-rs/core/tests/suite/hooks_mcp.rs b/codex-rs/core/tests/suite/hooks_mcp.rs new file mode 100644 index 0000000000..2157630e02 --- /dev/null +++ b/codex-rs/core/tests/suite/hooks_mcp.rs @@ -0,0 +1,350 @@ +use std::collections::HashMap; +use std::fs; +use std::path::Path; +use std::time::Duration; + +use anyhow::Context; +use anyhow::Result; +use codex_config::types::AppToolApproval; +use codex_config::types::McpServerConfig; +use codex_config::types::McpServerTransportConfig; +use codex_core::config::Config; +use codex_features::Feature; +use core_test_support::responses::ev_assistant_message; +use core_test_support::responses::ev_completed; +use core_test_support::responses::ev_function_call_with_namespace; +use core_test_support::responses::ev_response_created; +use core_test_support::responses::mount_sse_once; +use core_test_support::responses::mount_sse_sequence; +use core_test_support::responses::sse; +use core_test_support::responses::start_mock_server; +use core_test_support::skip_if_no_network; +use core_test_support::stdio_server_bin; +use core_test_support::test_codex::test_codex; +use pretty_assertions::assert_eq; +use serde_json::Value; +use serde_json::json; + +const RMCP_SERVER: &str = "rmcp"; +const RMCP_NAMESPACE: &str = "mcp__rmcp__"; +const RMCP_ECHO_TOOL_NAME: &str = "mcp__rmcp__echo"; +const RMCP_HOOK_MATCHER: &str = "mcp__rmcp__.*"; +const RMCP_ECHO_MESSAGE: &str = "hook e2e ping"; + +fn write_pre_tool_use_hook(home: &Path, reason: &str) -> Result<()> { + let script_path = home.join("pre_tool_use_hook.py"); + let log_path = home.join("pre_tool_use_hook_log.jsonl"); + let reason_json = serde_json::to_string(reason).context("serialize pre tool use reason")?; + let script = format!( + r#"import json +from pathlib import Path +import sys + +payload = json.load(sys.stdin) + +with Path(r"{log_path}").open("a", encoding="utf-8") as handle: + handle.write(json.dumps(payload) + "\n") + +print(json.dumps({{ + "hookSpecificOutput": {{ + "hookEventName": "PreToolUse", + "permissionDecision": "deny", + "permissionDecisionReason": {reason_json} + }} +}})) +"#, + log_path = log_path.display(), + reason_json = reason_json, + ); + let hooks = serde_json::json!({ + "hooks": { + "PreToolUse": [{ + "matcher": RMCP_HOOK_MATCHER, + "hooks": [{ + "type": "command", + "command": format!("python3 {}", script_path.display()), + "statusMessage": "running MCP pre tool use hook", + }] + }] + } + }); + + fs::write(&script_path, script).context("write pre tool use hook script")?; + fs::write(home.join("hooks.json"), hooks.to_string()).context("write hooks.json")?; + Ok(()) +} + +fn write_post_tool_use_hook(home: &Path, additional_context: &str) -> Result<()> { + let script_path = home.join("post_tool_use_hook.py"); + let log_path = home.join("post_tool_use_hook_log.jsonl"); + let additional_context_json = + serde_json::to_string(additional_context).context("serialize post tool use context")?; + let script = format!( + r#"import json +from pathlib import Path +import sys + +payload = json.load(sys.stdin) + +with Path(r"{log_path}").open("a", encoding="utf-8") as handle: + handle.write(json.dumps(payload) + "\n") + +print(json.dumps({{ + "hookSpecificOutput": {{ + "hookEventName": "PostToolUse", + "additionalContext": {additional_context_json} + }} +}})) +"#, + log_path = log_path.display(), + additional_context_json = additional_context_json, + ); + let hooks = serde_json::json!({ + "hooks": { + "PostToolUse": [{ + "matcher": RMCP_HOOK_MATCHER, + "hooks": [{ + "type": "command", + "command": format!("python3 {}", script_path.display()), + "statusMessage": "running MCP post tool use hook", + }] + }] + } + }); + + fs::write(&script_path, script).context("write post tool use hook script")?; + fs::write(home.join("hooks.json"), hooks.to_string()).context("write hooks.json")?; + Ok(()) +} + +fn read_hook_inputs(home: &Path, log_name: &str) -> Result> { + fs::read_to_string(home.join(log_name)) + .with_context(|| format!("read {log_name}"))? + .lines() + .filter(|line| !line.trim().is_empty()) + .map(|line| serde_json::from_str(line).with_context(|| format!("parse {log_name} line"))) + .collect() +} + +fn insert_rmcp_test_server(config: &mut Config, command: String, approval_mode: AppToolApproval) { + let mut servers = config.mcp_servers.get().clone(); + servers.insert( + RMCP_SERVER.to_string(), + McpServerConfig { + transport: McpServerTransportConfig::Stdio { + command, + args: Vec::new(), + env: None, + env_vars: Vec::new(), + cwd: None, + }, + experimental_environment: None, + enabled: true, + required: false, + supports_parallel_tool_calls: false, + disabled_reason: None, + startup_timeout_sec: Some(Duration::from_secs(10)), + tool_timeout_sec: None, + default_tools_approval_mode: Some(approval_mode), + enabled_tools: None, + disabled_tools: None, + scopes: None, + oauth_resource: None, + tools: HashMap::new(), + }, + ); + if let Err(err) = config.mcp_servers.set(servers) { + panic!("test mcp servers should accept any configuration: {err}"); + } +} + +fn enable_hooks_and_rmcp_server( + config: &mut Config, + rmcp_test_server_bin: String, + approval_mode: AppToolApproval, +) { + if let Err(err) = config.features.enable(Feature::CodexHooks) { + panic!("test config should allow feature update: {err}"); + } + insert_rmcp_test_server(config, rmcp_test_server_bin, approval_mode); +} + +#[tokio::test(flavor = "multi_thread", worker_threads = 2)] +async fn pre_tool_use_blocks_mcp_tool_before_execution() -> Result<()> { + skip_if_no_network!(Ok(())); + + let server = start_mock_server().await; + let call_id = "pretooluse-rmcp-echo"; + let arguments = json!({ "message": RMCP_ECHO_MESSAGE }).to_string(); + let responses = mount_sse_sequence( + &server, + vec![ + sse(vec![ + ev_response_created("resp-1"), + ev_function_call_with_namespace(call_id, RMCP_NAMESPACE, "echo", &arguments), + ev_completed("resp-1"), + ]), + sse(vec![ + ev_response_created("resp-2"), + ev_assistant_message("msg-1", "mcp hook blocked it"), + ev_completed("resp-2"), + ]), + ], + ) + .await; + + let block_reason = "blocked mcp pre hook"; + let rmcp_test_server_bin = stdio_server_bin()?; + let test = test_codex() + .with_pre_build_hook(move |home| { + if let Err(error) = write_pre_tool_use_hook(home, block_reason) { + panic!("failed to write MCP pre tool use hook fixture: {error}"); + } + }) + .with_config(move |config| { + enable_hooks_and_rmcp_server(config, rmcp_test_server_bin, AppToolApproval::Approve); + }) + .build(&server) + .await?; + + test.submit_turn("call the rmcp echo tool with the MCP pre hook") + .await?; + + let requests = responses.requests(); + assert_eq!(requests.len(), 2); + let output_item = requests[1].function_call_output(call_id); + let output = output_item + .get("output") + .and_then(Value::as_str) + .expect("blocked MCP tool output string"); + assert!( + output.contains(&format!( + "Tool call blocked by PreToolUse hook: {block_reason}. Tool: {RMCP_ECHO_TOOL_NAME}" + )), + "blocked MCP tool output should surface the hook reason and tool name", + ); + + let hook_inputs = read_hook_inputs(test.codex_home_path(), "pre_tool_use_hook_log.jsonl")?; + assert_eq!(hook_inputs.len(), 1); + assert_eq!( + json!({ + "hook_event_name": hook_inputs[0]["hook_event_name"], + "tool_name": hook_inputs[0]["tool_name"], + "tool_use_id": hook_inputs[0]["tool_use_id"], + "tool_input": hook_inputs[0]["tool_input"], + }), + json!({ + "hook_event_name": "PreToolUse", + "tool_name": RMCP_ECHO_TOOL_NAME, + "tool_use_id": call_id, + "tool_input": { "message": RMCP_ECHO_MESSAGE }, + }) + ); + let transcript_path = hook_inputs[0]["transcript_path"] + .as_str() + .expect("pre tool use hook transcript_path"); + assert!( + Path::new(transcript_path).exists(), + "pre tool use hook transcript_path should be materialized on disk", + ); + + Ok(()) +} + +#[tokio::test(flavor = "multi_thread", worker_threads = 2)] +async fn post_tool_use_records_mcp_tool_payload_and_context() -> Result<()> { + skip_if_no_network!(Ok(())); + + let server = start_mock_server().await; + let call_id = "posttooluse-rmcp-echo"; + let arguments = json!({ "message": RMCP_ECHO_MESSAGE }).to_string(); + let call_mock = mount_sse_once( + &server, + sse(vec![ + ev_response_created("resp-1"), + ev_function_call_with_namespace(call_id, RMCP_NAMESPACE, "echo", &arguments), + ev_completed("resp-1"), + ]), + ) + .await; + let final_mock = mount_sse_once( + &server, + sse(vec![ + ev_response_created("resp-2"), + ev_assistant_message("msg-1", "mcp post hook context observed"), + ev_completed("resp-2"), + ]), + ) + .await; + + let post_context = "Remember the MCP post-tool note."; + let rmcp_test_server_bin = stdio_server_bin()?; + let test = test_codex() + .with_pre_build_hook(move |home| { + if let Err(error) = write_post_tool_use_hook(home, post_context) { + panic!("failed to write MCP post tool use hook fixture: {error}"); + } + }) + .with_config(move |config| { + enable_hooks_and_rmcp_server(config, rmcp_test_server_bin, AppToolApproval::Approve); + }) + .build(&server) + .await?; + + test.submit_turn("call the rmcp echo tool with the MCP post hook") + .await?; + + let final_request = final_mock.single_request(); + assert!( + final_request + .message_input_texts("developer") + .contains(&post_context.to_string()), + "follow-up request should include MCP post tool use additional context", + ); + let output_item = final_request.function_call_output(call_id); + let output = output_item + .get("output") + .and_then(Value::as_str) + .expect("MCP tool output string"); + assert!( + output.contains(&format!("ECHOING: {RMCP_ECHO_MESSAGE}")), + "MCP tool output should still reach the model", + ); + + let hook_inputs = read_hook_inputs(test.codex_home_path(), "post_tool_use_hook_log.jsonl")?; + assert_eq!(hook_inputs.len(), 1); + assert_eq!( + json!({ + "hook_event_name": hook_inputs[0]["hook_event_name"], + "tool_name": hook_inputs[0]["tool_name"], + "tool_use_id": hook_inputs[0]["tool_use_id"], + "tool_input": hook_inputs[0]["tool_input"], + "tool_response": hook_inputs[0]["tool_response"], + }), + json!({ + "hook_event_name": "PostToolUse", + "tool_name": RMCP_ECHO_TOOL_NAME, + "tool_use_id": call_id, + "tool_input": { "message": RMCP_ECHO_MESSAGE }, + "tool_response": { + "content": [], + "structuredContent": { + "echo": format!("ECHOING: {RMCP_ECHO_MESSAGE}"), + "env": null, + }, + "isError": false, + }, + }) + ); + let transcript_path = hook_inputs[0]["transcript_path"] + .as_str() + .expect("post tool use hook transcript_path"); + assert!( + Path::new(transcript_path).exists(), + "post tool use hook transcript_path should be materialized on disk", + ); + + call_mock.single_request(); + + Ok(()) +} diff --git a/codex-rs/core/tests/suite/mod.rs b/codex-rs/core/tests/suite/mod.rs index 6c4cb1981f..8f9f9e6840 100644 --- a/codex-rs/core/tests/suite/mod.rs +++ b/codex-rs/core/tests/suite/mod.rs @@ -47,6 +47,8 @@ mod fork_thread; mod hierarchical_agents; #[cfg(not(target_os = "windows"))] mod hooks; +#[cfg(not(target_os = "windows"))] +mod hooks_mcp; mod image_rollout; mod items; mod js_repl; diff --git a/codex-rs/core/tests/suite/openai_file_mcp.rs b/codex-rs/core/tests/suite/openai_file_mcp.rs index 211aa67381..2912b5fb0e 100644 --- a/codex-rs/core/tests/suite/openai_file_mcp.rs +++ b/codex-rs/core/tests/suite/openai_file_mcp.rs @@ -1,5 +1,9 @@ #![cfg(not(target_os = "windows"))] +use std::fs; +use std::path::Path; + +use anyhow::Context; use anyhow::Result; use codex_core::config::Config; use codex_features::Feature; @@ -28,6 +32,7 @@ use wiremock::matchers::path; const DOCUMENT_EXTRACT_NAMESPACE: &str = "mcp__codex_apps__calendar"; const DOCUMENT_EXTRACT_TOOL: &str = "_extract_text"; +const DOCUMENT_EXTRACT_HOOK_MATCHER: &str = "mcp__codex_apps__calendar_extract_text"; fn configure_apps(config: &mut Config, chatgpt_base_url: &str) { if let Err(err) = config.features.enable(Feature::Apps) { @@ -36,6 +41,55 @@ fn configure_apps(config: &mut Config, chatgpt_base_url: &str) { config.chatgpt_base_url = chatgpt_base_url.to_string(); } +fn write_post_tool_use_hook(home: &Path) -> Result<()> { + let script_path = home.join("post_tool_use_hook.py"); + let log_path = home.join("post_tool_use_hook_log.jsonl"); + let script = format!( + r#"import json +from pathlib import Path +import sys + +payload = json.load(sys.stdin) + +with Path(r"{log_path}").open("a", encoding="utf-8") as handle: + handle.write(json.dumps(payload) + "\n") + +print(json.dumps({{ + "hookSpecificOutput": {{ + "hookEventName": "PostToolUse", + "additionalContext": "observed apps file payload" + }} +}})) +"#, + log_path = log_path.display(), + ); + let hooks = serde_json::json!({ + "hooks": { + "PostToolUse": [{ + "matcher": DOCUMENT_EXTRACT_HOOK_MATCHER, + "hooks": [{ + "type": "command", + "command": format!("python3 {}", script_path.display()), + "statusMessage": "running apps file post tool use hook", + }] + }] + } + }); + + fs::write(&script_path, script).context("write post tool use hook script")?; + fs::write(home.join("hooks.json"), hooks.to_string()).context("write hooks.json")?; + Ok(()) +} + +fn read_post_tool_use_hook_inputs(home: &Path) -> Result> { + fs::read_to_string(home.join("post_tool_use_hook_log.jsonl")) + .context("read post tool use hook log")? + .lines() + .filter(|line| !line.trim().is_empty()) + .map(|line| serde_json::from_str(line).context("parse post tool use hook input")) + .collect() +} + #[tokio::test(flavor = "multi_thread", worker_threads = 2)] async fn codex_apps_file_params_upload_local_paths_before_mcp_tool_call() -> Result<()> { let server = start_mock_server().await; @@ -101,7 +155,17 @@ async fn codex_apps_file_params_upload_local_paths_before_mcp_tool_call() -> Res let mut builder = test_codex() .with_auth(CodexAuth::create_dummy_chatgpt_auth_for_testing()) - .with_config(move |config| configure_apps(config, apps_server.chatgpt_base_url.as_str())); + .with_pre_build_hook(move |home| { + if let Err(error) = write_post_tool_use_hook(home) { + panic!("failed to write apps file post tool use hook fixture: {error}"); + } + }) + .with_config(move |config| { + configure_apps(config, apps_server.chatgpt_base_url.as_str()); + if let Err(err) = config.features.enable(Feature::CodexHooks) { + panic!("test config should allow feature update: {err}"); + } + }); let test = builder.build(&server).await?; tokio::fs::write(test.cwd.path().join("report.txt"), b"hello world").await?; @@ -164,6 +228,20 @@ async fn codex_apps_file_params_upload_local_paths_before_mcp_tool_call() -> Res })) ); + let hook_inputs = read_post_tool_use_hook_inputs(test.codex_home_path())?; + assert_eq!(hook_inputs.len(), 1); + assert_eq!( + hook_inputs[0]["tool_input"]["file"], + json!({ + "download_url": format!("{}/download/file_123", server.uri()), + "file_id": "file_123", + "mime_type": "text/plain", + "file_name": "report.txt", + "uri": "sediment://file_123", + "file_size_bytes": 11, + }) + ); + server.verify().await; Ok(()) } diff --git a/codex-rs/core/tests/suite/subagent_notifications.rs b/codex-rs/core/tests/suite/subagent_notifications.rs index f12c684279..3f457967c1 100644 --- a/codex-rs/core/tests/suite/subagent_notifications.rs +++ b/codex-rs/core/tests/suite/subagent_notifications.rs @@ -37,7 +37,6 @@ const REQUESTED_MODEL: &str = "gpt-5.4"; const REQUESTED_REASONING_EFFORT: ReasoningEffort = ReasoningEffort::Low; const ROLE_MODEL: &str = "gpt-5.4"; const ROLE_REASONING_EFFORT: ReasoningEffort = ReasoningEffort::High; -const SPAWNED_AGENT_DEVELOPER_INSTRUCTIONS: &str = "You are a newly spawned agent in a team of agents collaborating to complete a task. You can spawn sub-agents to handle subtasks, and those sub-agents can spawn their own sub-agents. You are responsible for returning the response to your assigned task in the final channel. When you give your response, the contents of your response in the final channel will be immediately delivered back to your parent agent. The prior conversation history was forked from your parent agent. Treat the next user message as your assigned task, and use the forked history only as background context."; fn body_contains(req: &wiremock::Request, text: &str) -> bool { let is_zstd = req @@ -425,7 +424,7 @@ async fn spawn_agent_requested_model_and_reasoning_override_inherited_settings_w } #[tokio::test(flavor = "multi_thread", worker_threads = 2)] -async fn spawned_multi_agent_v2_child_receives_xml_tagged_developer_context() -> Result<()> { +async fn spawned_multi_agent_v2_child_inherits_parent_developer_context() -> Result<()> { skip_if_no_network!(Ok(())); let server = start_mock_server().await; @@ -446,9 +445,7 @@ async fn spawned_multi_agent_v2_child_receives_xml_tagged_developer_context() -> let _child_request_log = mount_sse_once_match( &server, - |req: &wiremock::Request| { - body_contains(req, CHILD_PROMPT) && body_contains(req, "") - }, + |req: &wiremock::Request| body_contains(req, CHILD_PROMPT), sse(vec![ ev_response_created("resp-child-1"), ev_completed("resp-child-1"), @@ -459,9 +456,7 @@ async fn spawned_multi_agent_v2_child_receives_xml_tagged_developer_context() -> let _turn1_followup = mount_sse_once_match( &server, |req: &wiremock::Request| { - body_contains(req, "function_call_output") - && body_contains(req, "/root/worker") - && !body_contains(req, "") + body_contains(req, "function_call_output") && body_contains(req, "/root/worker") }, sse(vec![ ev_response_created("resp-turn1-2"), @@ -494,9 +489,7 @@ async fn spawned_multi_agent_v2_child_receives_xml_tagged_developer_context() -> .unwrap_or_default() .into_iter() .find(|request| { - body_contains(request, CHILD_PROMPT) - && body_contains(request, "") - && body_contains(request, SPAWNED_AGENT_DEVELOPER_INSTRUCTIONS) + body_contains(request, CHILD_PROMPT) && !body_contains(request, SPAWN_CALL_ID) }) { break request; @@ -510,11 +503,6 @@ async fn spawned_multi_agent_v2_child_receives_xml_tagged_developer_context() -> &child_request, "Parent developer instructions." )); - assert!(body_contains(&child_request, "")); - assert!(body_contains( - &child_request, - SPAWNED_AGENT_DEVELOPER_INSTRUCTIONS - )); assert!(body_contains(&child_request, CHILD_PROMPT)); Ok(()) @@ -542,9 +530,7 @@ async fn skills_toggle_skips_instructions_for_parent_and_spawned_child() -> Resu let _child_request_log = mount_sse_once_match( &server, - |req: &wiremock::Request| { - body_contains(req, CHILD_PROMPT) && body_contains(req, "") - }, + |req: &wiremock::Request| body_contains(req, CHILD_PROMPT), sse(vec![ ev_response_created("resp-child-1"), ev_completed("resp-child-1"), @@ -555,9 +541,7 @@ async fn skills_toggle_skips_instructions_for_parent_and_spawned_child() -> Resu let _turn1_followup = mount_sse_once_match( &server, |req: &wiremock::Request| { - body_contains(req, "function_call_output") - && body_contains(req, "/root/worker") - && !body_contains(req, "") + body_contains(req, "function_call_output") && body_contains(req, "/root/worker") }, sse(vec![ ev_response_created("resp-turn1-2"), @@ -599,8 +583,7 @@ async fn skills_toggle_skips_instructions_for_parent_and_spawned_child() -> Resu .unwrap_or_default() .into_iter() .find(|request| { - body_contains(request, CHILD_PROMPT) - && body_contains(request, "") + body_contains(request, CHILD_PROMPT) && !body_contains(request, SPAWN_CALL_ID) }) { break request; diff --git a/codex-rs/exec-server/src/local_process.rs b/codex-rs/exec-server/src/local_process.rs index aa88e37fdd..6446c36507 100644 --- a/codex-rs/exec-server/src/local_process.rs +++ b/codex-rs/exec-server/src/local_process.rs @@ -323,8 +323,11 @@ impl LocalProcess { ) }; + let has_new_terminal_event = + response.exited && after_seq < response.next_seq.saturating_sub(1); if !response.chunks.is_empty() - || response.exited + || response.closed + || has_new_terminal_event || tokio::time::Instant::now() >= deadline { let _total_bytes: usize = response diff --git a/codex-rs/hooks/schema/generated/permission-request.command.input.schema.json b/codex-rs/hooks/schema/generated/permission-request.command.input.schema.json index 7d432e1551..55b3843c0b 100644 --- a/codex-rs/hooks/schema/generated/permission-request.command.input.schema.json +++ b/codex-rs/hooks/schema/generated/permission-request.command.input.schema.json @@ -7,21 +7,6 @@ "string", "null" ] - }, - "PermissionRequestToolInput": { - "additionalProperties": false, - "properties": { - "command": { - "type": "string" - }, - "description": { - "type": "string" - } - }, - "required": [ - "command" - ], - "type": "object" } }, "properties": { @@ -48,9 +33,7 @@ "session_id": { "type": "string" }, - "tool_input": { - "$ref": "#/definitions/PermissionRequestToolInput" - }, + "tool_input": true, "tool_name": { "type": "string" }, diff --git a/codex-rs/hooks/schema/generated/post-tool-use.command.input.schema.json b/codex-rs/hooks/schema/generated/post-tool-use.command.input.schema.json index 6a3fcfc47c..1ec5fb3082 100644 --- a/codex-rs/hooks/schema/generated/post-tool-use.command.input.schema.json +++ b/codex-rs/hooks/schema/generated/post-tool-use.command.input.schema.json @@ -7,18 +7,6 @@ "string", "null" ] - }, - "PostToolUseToolInput": { - "additionalProperties": false, - "properties": { - "command": { - "type": "string" - } - }, - "required": [ - "command" - ], - "type": "object" } }, "properties": { @@ -45,9 +33,7 @@ "session_id": { "type": "string" }, - "tool_input": { - "$ref": "#/definitions/PostToolUseToolInput" - }, + "tool_input": true, "tool_name": { "type": "string" }, diff --git a/codex-rs/hooks/schema/generated/pre-tool-use.command.input.schema.json b/codex-rs/hooks/schema/generated/pre-tool-use.command.input.schema.json index df3bec7ff6..f9cde01020 100644 --- a/codex-rs/hooks/schema/generated/pre-tool-use.command.input.schema.json +++ b/codex-rs/hooks/schema/generated/pre-tool-use.command.input.schema.json @@ -7,18 +7,6 @@ "string", "null" ] - }, - "PreToolUseToolInput": { - "additionalProperties": false, - "properties": { - "command": { - "type": "string" - } - }, - "required": [ - "command" - ], - "type": "object" } }, "properties": { @@ -45,9 +33,7 @@ "session_id": { "type": "string" }, - "tool_input": { - "$ref": "#/definitions/PreToolUseToolInput" - }, + "tool_input": true, "tool_name": { "type": "string" }, diff --git a/codex-rs/hooks/src/engine/mod_tests.rs b/codex-rs/hooks/src/engine/mod_tests.rs index 527574b500..81004aefb4 100644 --- a/codex-rs/hooks/src/engine/mod_tests.rs +++ b/codex-rs/hooks/src/engine/mod_tests.rs @@ -125,7 +125,7 @@ with Path(r"{log_path}").open("a", encoding="utf-8") as handle: tool_name: "Bash".to_string(), matcher_aliases: Vec::new(), tool_use_id: "tool-1".to_string(), - command: "echo hello".to_string(), + tool_input: serde_json::json!({ "command": "echo hello" }), }); assert_eq!(preview.len(), 1); assert_eq!(preview[0].source_path, managed_dir); @@ -141,7 +141,7 @@ with Path(r"{log_path}").open("a", encoding="utf-8") as handle: tool_name: "Bash".to_string(), matcher_aliases: Vec::new(), tool_use_id: "tool-1".to_string(), - command: "echo hello".to_string(), + tool_input: serde_json::json!({ "command": "echo hello" }), }) .await; @@ -212,7 +212,7 @@ fn requirements_managed_hooks_warn_when_managed_dir_is_missing() { tool_name: "Bash".to_string(), matcher_aliases: Vec::new(), tool_use_id: "tool-1".to_string(), - command: "echo hello".to_string(), + tool_input: serde_json::json!({ "command": "echo hello" }), }) .is_empty() ); @@ -318,7 +318,7 @@ fn discovers_hooks_from_json_and_toml_in_the_same_layer() { tool_name: "Bash".to_string(), matcher_aliases: Vec::new(), tool_use_id: "tool-1".to_string(), - command: "echo hello".to_string(), + tool_input: serde_json::json!({ "command": "echo hello" }), }); assert_eq!(preview.len(), 2); assert!(engine.handlers.iter().all(|handler| !handler.is_managed)); diff --git a/codex-rs/hooks/src/events/common.rs b/codex-rs/hooks/src/events/common.rs index 80dd325378..de3f3292ac 100644 --- a/codex-rs/hooks/src/events/common.rs +++ b/codex-rs/hooks/src/events/common.rs @@ -183,7 +183,7 @@ mod tests { } #[test] - fn matcher_uses_regex_matching() { + fn exact_matcher_supports_pipe_alternatives() { assert!(matches_matcher(Some("Edit|Write"), Some("Edit"))); assert!(matches_matcher(Some("Edit|Write"), Some("Write"))); assert!(!matches_matcher(Some("Edit|Write"), Some("Bash"))); @@ -191,23 +191,40 @@ mod tests { } #[test] - fn matcher_exact_string_does_not_substring_match() { + fn literal_matcher_uses_exact_matching() { assert!(matches_matcher(Some("Bash"), Some("Bash"))); assert!(!matches_matcher(Some("Bash"), Some("BashOutput"))); - assert_eq!(validate_matcher_pattern("Bash"), Ok(())); - } - - #[test] - fn matcher_uses_regex_when_it_contains_other_characters() { - assert!(matches_matcher(Some("^Bash"), Some("BashOutput"))); assert!(matches_matcher( - Some("mcp__memory__.*"), + Some("mcp__memory__create_entities"), Some("mcp__memory__create_entities") )); assert!(!matches_matcher( Some("mcp__memory"), Some("mcp__memory__create_entities") )); + assert_eq!(validate_matcher_pattern("mcp__memory"), Ok(())); + } + + #[test] + fn matcher_uses_regex_when_it_contains_regex_characters() { + assert!(matches_matcher(Some("^Bash"), Some("BashOutput"))); + assert_eq!(validate_matcher_pattern("^Bash"), Ok(())); + } + + #[test] + fn mcp_matchers_support_regex_wildcards() { + assert!(matches_matcher( + Some("mcp__memory__.*"), + Some("mcp__memory__create_entities") + )); + assert!(matches_matcher( + Some("mcp__.*__write.*"), + Some("mcp__filesystem__write_file") + )); + assert!(!matches_matcher( + Some("mcp__.*__write.*"), + Some("mcp__filesystem__read_file") + )); assert_eq!(validate_matcher_pattern("mcp__memory__.*"), Ok(())); } diff --git a/codex-rs/hooks/src/events/permission_request.rs b/codex-rs/hooks/src/events/permission_request.rs index 59b8f61d22..2cebd0e002 100644 --- a/codex-rs/hooks/src/events/permission_request.rs +++ b/codex-rs/hooks/src/events/permission_request.rs @@ -22,7 +22,6 @@ use crate::engine::command_runner::CommandRunResult; use crate::engine::dispatcher; use crate::engine::output_parser; use crate::schema::PermissionRequestCommandInput; -use crate::schema::PermissionRequestToolInput; use codex_protocol::ThreadId; use codex_protocol::protocol::HookCompletedEvent; use codex_protocol::protocol::HookEventName; @@ -30,6 +29,7 @@ use codex_protocol::protocol::HookOutputEntry; use codex_protocol::protocol::HookOutputEntryKind; use codex_protocol::protocol::HookRunStatus; use codex_protocol::protocol::HookRunSummary; +use serde_json::Value; #[derive(Debug, Clone)] pub struct PermissionRequestRequest { @@ -42,8 +42,7 @@ pub struct PermissionRequestRequest { pub tool_name: String, pub matcher_aliases: Vec, pub run_id_suffix: String, - pub command: String, - pub description: Option, + pub tool_input: Value, } #[derive(Debug, Clone, PartialEq, Eq)] @@ -178,10 +177,7 @@ fn build_command_input(request: &PermissionRequestRequest) -> PermissionRequestC model: request.model.clone(), permission_mode: request.permission_mode.clone(), tool_name: request.tool_name.clone(), - tool_input: PermissionRequestToolInput { - command: request.command.clone(), - description: request.description.clone(), - }, + tool_input: request.tool_input.clone(), } } diff --git a/codex-rs/hooks/src/events/post_tool_use.rs b/codex-rs/hooks/src/events/post_tool_use.rs index 265572069b..20cdfd2010 100644 --- a/codex-rs/hooks/src/events/post_tool_use.rs +++ b/codex-rs/hooks/src/events/post_tool_use.rs @@ -17,7 +17,6 @@ use crate::engine::command_runner::CommandRunResult; use crate::engine::dispatcher; use crate::engine::output_parser; use crate::schema::PostToolUseCommandInput; -use crate::schema::PostToolUseToolInput; #[derive(Debug, Clone)] pub struct PostToolUseRequest { @@ -30,7 +29,7 @@ pub struct PostToolUseRequest { pub tool_name: String, pub matcher_aliases: Vec, pub tool_use_id: String, - pub command: String, + pub tool_input: Value, pub tool_response: Value, } @@ -146,7 +145,8 @@ pub(crate) async fn run( /// /// Handler selection may include internal matcher aliases, but hook stdin keeps /// the canonical `tool_name` for logs and for consumers that pair pre/post -/// events across processes. +/// events across processes. Shell-like tools pass `{ "command": ... }` as +/// `tool_input`; MCP tools pass their resolved JSON arguments. fn command_input_json(request: &PostToolUseRequest) -> Result { serde_json::to_string(&PostToolUseCommandInput { session_id: request.session_id.to_string(), @@ -157,9 +157,7 @@ fn command_input_json(request: &PostToolUseRequest) -> Result, pub tool_use_id: String, - pub command: String, + pub tool_input: Value, } #[derive(Debug)] @@ -124,7 +125,8 @@ pub(crate) async fn run( /// /// Handler selection may include internal matcher aliases, but hook stdin keeps /// the canonical `tool_name` so audit logs and downstream policy decisions stay -/// stable. +/// stable. Shell-like tools pass `{ "command": ... }` as `tool_input`; MCP +/// tools pass their resolved JSON arguments. fn command_input_json(request: &PreToolUseRequest) -> Result { serde_json::to_string(&PreToolUseCommandInput { session_id: request.session_id.to_string(), @@ -135,9 +137,7 @@ fn command_input_json(request: &PreToolUseRequest) -> Result, -} - #[derive(Debug, Clone, Serialize, JsonSchema)] #[serde(deny_unknown_fields)] #[schemars(rename = "permission-request.command.input")] @@ -260,14 +244,7 @@ pub(crate) struct PermissionRequestCommandInput { #[schemars(schema_with = "permission_mode_schema")] pub permission_mode: String, pub tool_name: String, - pub tool_input: PermissionRequestToolInput, -} - -#[derive(Debug, Clone, Serialize, JsonSchema)] -#[serde(rename_all = "camelCase")] -#[serde(deny_unknown_fields)] -pub(crate) struct PostToolUseToolInput { - pub command: String, + pub tool_input: Value, } #[derive(Debug, Clone, Serialize, JsonSchema)] @@ -285,7 +262,7 @@ pub(crate) struct PostToolUseCommandInput { #[schemars(schema_with = "permission_mode_schema")] pub permission_mode: String, pub tool_name: String, - pub tool_input: PostToolUseToolInput, + pub tool_input: Value, pub tool_response: Value, pub tool_use_id: String, } diff --git a/codex-rs/protocol/src/config_types.rs b/codex-rs/protocol/src/config_types.rs index 06484c6441..2be5c6f124 100644 --- a/codex-rs/protocol/src/config_types.rs +++ b/codex-rs/protocol/src/config_types.rs @@ -87,8 +87,8 @@ pub enum ApprovalsReviewer { #[default] #[serde(rename = "user")] User, - #[serde(rename = "auto_review", alias = "guardian_subagent")] - #[strum(serialize = "auto_review")] + #[serde(rename = "guardian_subagent", alias = "auto_review")] + #[strum(serialize = "guardian_subagent")] AutoReview, } @@ -609,7 +609,7 @@ mod tests { ); assert_eq!( serde_json::to_string(&ApprovalsReviewer::AutoReview).expect("serialize reviewer"), - "\"auto_review\"" + "\"guardian_subagent\"" ); for value in ["user", "auto_review", "guardian_subagent"] { diff --git a/codex-rs/thread-store/Cargo.toml b/codex-rs/thread-store/Cargo.toml index 61e4e41453..3a0428f20e 100644 --- a/codex-rs/thread-store/Cargo.toml +++ b/codex-rs/thread-store/Cargo.toml @@ -26,8 +26,10 @@ prost = "0.14.3" serde = { workspace = true, features = ["derive"] } serde_json = { workspace = true } thiserror = { workspace = true } +tokio = { workspace = true } tonic = { workspace = true } tonic-prost = { workspace = true } +tracing = { workspace = true } [dev-dependencies] pretty_assertions = { workspace = true } diff --git a/codex-rs/thread-store/src/lib.rs b/codex-rs/thread-store/src/lib.rs index cb87bdd04b..15ec238fa8 100644 --- a/codex-rs/thread-store/src/lib.rs +++ b/codex-rs/thread-store/src/lib.rs @@ -5,16 +5,17 @@ //! any other backing store. mod error; +mod live_thread; mod local; -mod recorder; mod remote; mod store; mod types; pub use error::ThreadStoreError; pub use error::ThreadStoreResult; +pub use live_thread::LiveThread; +pub use live_thread::LiveThreadInitGuard; pub use local::LocalThreadStore; -pub use recorder::ThreadRecorder; pub use remote::RemoteThreadStore; pub use store::ThreadStore; pub use types::AppendThreadItemsParams; @@ -25,7 +26,7 @@ pub use types::ListThreadsParams; pub use types::LoadThreadHistoryParams; pub use types::OptionalStringPatch; pub use types::ReadThreadParams; -pub use types::ResumeThreadRecorderParams; +pub use types::ResumeThreadParams; pub use types::SortDirection; pub use types::StoredThread; pub use types::StoredThreadHistory; diff --git a/codex-rs/thread-store/src/live_thread.rs b/codex-rs/thread-store/src/live_thread.rs new file mode 100644 index 0000000000..bcce1c7645 --- /dev/null +++ b/codex-rs/thread-store/src/live_thread.rs @@ -0,0 +1,176 @@ +use std::path::PathBuf; +use std::sync::Arc; + +use codex_protocol::ThreadId; +use codex_protocol::protocol::RolloutItem; +use codex_protocol::protocol::ThreadMemoryMode; +use tracing::warn; + +use crate::AppendThreadItemsParams; +use crate::CreateThreadParams; +use crate::LoadThreadHistoryParams; +use crate::LocalThreadStore; +use crate::ResumeThreadParams; +use crate::StoredThreadHistory; +use crate::ThreadMetadataPatch; +use crate::ThreadStore; +use crate::ThreadStoreResult; +use crate::UpdateThreadMetadataParams; + +/// Handle for an active thread's persistence lifecycle. +/// +/// `LiveThread` keeps lifecycle decisions with the caller while delegating storage details to +/// [`ThreadStore`]. Local stores may use a rollout file internally and remote stores may use a +/// service, but session code should only need this handle for the active thread. +#[derive(Clone)] +pub struct LiveThread { + thread_id: ThreadId, + thread_store: Arc, +} + +/// Owns a live thread while session initialization is still fallible. +/// +/// If initialization returns early after persistence has been opened, dropping this guard discards +/// the live writer without forcing lazy in-memory state to become durable. Call [`commit`] once the +/// session owns the live thread for normal operation. +pub struct LiveThreadInitGuard { + live_thread: Option, +} + +impl LiveThreadInitGuard { + pub fn new(live_thread: Option) -> Self { + Self { live_thread } + } + + pub fn as_ref(&self) -> Option<&LiveThread> { + self.live_thread.as_ref() + } + + pub fn commit(&mut self) { + self.live_thread = None; + } + + pub async fn discard(&mut self) { + let Some(live_thread) = self.live_thread.take() else { + return; + }; + if let Err(err) = live_thread.discard().await { + warn!("failed to discard thread persistence for failed session init: {err}"); + } + } +} + +impl Drop for LiveThreadInitGuard { + fn drop(&mut self) { + let Some(live_thread) = self.live_thread.take() else { + return; + }; + let Ok(handle) = tokio::runtime::Handle::try_current() else { + warn!("failed to discard thread persistence for failed session init: no Tokio runtime"); + return; + }; + handle.spawn(async move { + if let Err(err) = live_thread.discard().await { + warn!("failed to discard thread persistence for failed session init: {err}"); + } + }); + } +} + +impl LiveThread { + pub async fn create( + thread_store: Arc, + params: CreateThreadParams, + ) -> ThreadStoreResult { + let thread_id = params.thread_id; + thread_store.create_thread(params).await?; + Ok(Self { + thread_id, + thread_store, + }) + } + + pub async fn resume( + thread_store: Arc, + params: ResumeThreadParams, + ) -> ThreadStoreResult { + let thread_id = params.thread_id; + thread_store.resume_thread(params).await?; + Ok(Self { + thread_id, + thread_store, + }) + } + + pub async fn append_items(&self, items: &[RolloutItem]) -> ThreadStoreResult<()> { + self.thread_store + .append_items(AppendThreadItemsParams { + thread_id: self.thread_id, + items: items.to_vec(), + }) + .await + } + + pub async fn persist(&self) -> ThreadStoreResult<()> { + self.thread_store.persist_thread(self.thread_id).await + } + + pub async fn flush(&self) -> ThreadStoreResult<()> { + self.thread_store.flush_thread(self.thread_id).await + } + + pub async fn shutdown(&self) -> ThreadStoreResult<()> { + self.thread_store.shutdown_thread(self.thread_id).await + } + + pub async fn discard(&self) -> ThreadStoreResult<()> { + self.thread_store.discard_thread(self.thread_id).await + } + + pub async fn load_history( + &self, + include_archived: bool, + ) -> ThreadStoreResult { + self.thread_store + .load_history(LoadThreadHistoryParams { + thread_id: self.thread_id, + include_archived, + }) + .await + } + + pub async fn update_memory_mode( + &self, + mode: ThreadMemoryMode, + include_archived: bool, + ) -> ThreadStoreResult<()> { + self.thread_store + .update_thread_metadata(UpdateThreadMetadataParams { + thread_id: self.thread_id, + patch: ThreadMetadataPatch { + memory_mode: Some(mode), + ..Default::default() + }, + include_archived, + }) + .await?; + Ok(()) + } + + /// Returns the live local rollout path for legacy local-only callers. + /// + /// Remote stores do not expose rollout files, so they return `Ok(None)`. + pub async fn local_rollout_path(&self) -> ThreadStoreResult> { + let Some(local_store) = self + .thread_store + .as_any() + .downcast_ref::() + else { + return Ok(None); + }; + local_store + .live_rollout_path(self.thread_id) + .await + .map(Some) + } +} diff --git a/codex-rs/thread-store/src/local/create_thread.rs b/codex-rs/thread-store/src/local/create_thread.rs new file mode 100644 index 0000000000..69f19adc6a --- /dev/null +++ b/codex-rs/thread-store/src/local/create_thread.rs @@ -0,0 +1,41 @@ +use super::LocalThreadStore; +use crate::CreateThreadParams; +use crate::ThreadEventPersistenceMode; +use crate::ThreadStoreError; +use crate::ThreadStoreResult; +use codex_rollout::EventPersistenceMode; +use codex_rollout::RolloutRecorder; +use codex_rollout::RolloutRecorderParams; + +pub(super) async fn create_thread( + store: &LocalThreadStore, + params: CreateThreadParams, +) -> ThreadStoreResult { + let state_db_ctx = store.state_db().await; + let recorder = RolloutRecorder::new( + &store.config, + RolloutRecorderParams::new( + params.thread_id, + params.forked_from_id, + params.source, + params.base_instructions, + params.dynamic_tools, + event_persistence_mode(params.event_persistence_mode), + ), + state_db_ctx, + /*state_builder*/ None, + ) + .await + .map_err(|err| ThreadStoreError::Internal { + message: format!("failed to initialize local thread recorder: {err}"), + })?; + + Ok(recorder) +} + +pub(super) fn event_persistence_mode(mode: ThreadEventPersistenceMode) -> EventPersistenceMode { + match mode { + ThreadEventPersistenceMode::Limited => EventPersistenceMode::Limited, + ThreadEventPersistenceMode::Extended => EventPersistenceMode::Extended, + } +} diff --git a/codex-rs/thread-store/src/local/live_writer.rs b/codex-rs/thread-store/src/local/live_writer.rs new file mode 100644 index 0000000000..fd8cd93c79 --- /dev/null +++ b/codex-rs/thread-store/src/local/live_writer.rs @@ -0,0 +1,152 @@ +use std::path::PathBuf; + +use codex_protocol::ThreadId; +use codex_rollout::RolloutRecorder; +use codex_rollout::RolloutRecorderParams; +use codex_rollout::builder_from_items; + +use super::LocalThreadStore; +use super::create_thread; +use crate::AppendThreadItemsParams; +use crate::CreateThreadParams; +use crate::ReadThreadParams; +use crate::ResumeThreadParams; +use crate::ThreadStoreError; +use crate::ThreadStoreResult; + +pub(super) async fn create_thread( + store: &LocalThreadStore, + params: CreateThreadParams, +) -> ThreadStoreResult<()> { + let thread_id = params.thread_id; + store.ensure_live_recorder_absent(thread_id).await?; + let recorder = create_thread::create_thread(store, params).await?; + store.insert_live_recorder(thread_id, recorder).await +} + +pub(super) async fn resume_thread( + store: &LocalThreadStore, + params: ResumeThreadParams, +) -> ThreadStoreResult<()> { + store.ensure_live_recorder_absent(params.thread_id).await?; + let (rollout_path, history) = match (params.rollout_path, params.history) { + (Some(rollout_path), history) => (rollout_path, history), + (None, history) => { + let thread = super::read_thread::read_thread( + store, + ReadThreadParams { + thread_id: params.thread_id, + include_archived: params.include_archived, + include_history: history.is_none(), + }, + ) + .await?; + let rollout_path = thread + .rollout_path + .ok_or_else(|| ThreadStoreError::Internal { + message: format!("thread {} does not have a rollout path", params.thread_id), + })?; + ( + rollout_path, + history.or_else(|| thread.history.map(|history| history.items)), + ) + } + }; + let state_builder = history + .as_deref() + .and_then(|items| builder_from_items(items, rollout_path.as_path())); + let state_db_ctx = store.state_db().await; + let recorder = RolloutRecorder::new( + &store.config, + RolloutRecorderParams::resume( + rollout_path, + create_thread::event_persistence_mode(params.event_persistence_mode), + ), + state_db_ctx, + state_builder, + ) + .await + .map_err(|err| ThreadStoreError::Internal { + message: format!("failed to resume local thread recorder: {err}"), + })?; + store.insert_live_recorder(params.thread_id, recorder).await +} + +pub(super) async fn append_items( + store: &LocalThreadStore, + params: AppendThreadItemsParams, +) -> ThreadStoreResult<()> { + store + .live_recorder(params.thread_id) + .await? + .record_items(params.items.as_slice()) + .await + .map_err(thread_store_io_error) +} + +pub(super) async fn persist_thread( + store: &LocalThreadStore, + thread_id: ThreadId, +) -> ThreadStoreResult<()> { + store + .live_recorder(thread_id) + .await? + .persist() + .await + .map_err(thread_store_io_error) +} + +pub(super) async fn flush_thread( + store: &LocalThreadStore, + thread_id: ThreadId, +) -> ThreadStoreResult<()> { + store + .live_recorder(thread_id) + .await? + .flush() + .await + .map_err(thread_store_io_error) +} + +pub(super) async fn shutdown_thread( + store: &LocalThreadStore, + thread_id: ThreadId, +) -> ThreadStoreResult<()> { + let recorder = store.live_recorder(thread_id).await?; + recorder.shutdown().await.map_err(thread_store_io_error)?; + store.live_recorders.lock().await.remove(&thread_id); + Ok(()) +} + +pub(super) async fn discard_thread( + store: &LocalThreadStore, + thread_id: ThreadId, +) -> ThreadStoreResult<()> { + store + .live_recorders + .lock() + .await + .remove(&thread_id) + .map(|_| ()) + .ok_or(ThreadStoreError::ThreadNotFound { thread_id }) +} + +pub(super) async fn rollout_path( + store: &LocalThreadStore, + thread_id: ThreadId, +) -> ThreadStoreResult { + Ok(store + .live_recorders + .lock() + .await + .get(&thread_id) + .ok_or(ThreadStoreError::ThreadNotFound { thread_id })? + .rollout_path() + .to_path_buf()) +} + +fn thread_store_io_error(err: std::io::Error) -> ThreadStoreError { + ThreadStoreError::Internal { + message: err.to_string(), + } +} diff --git a/codex-rs/thread-store/src/local/mod.rs b/codex-rs/thread-store/src/local/mod.rs index c7c62f4cfa..e5f73ff831 100644 --- a/codex-rs/thread-store/src/local/mod.rs +++ b/codex-rs/thread-store/src/local/mod.rs @@ -1,6 +1,8 @@ mod archive_thread; +mod create_thread; mod helpers; mod list_threads; +mod live_writer; mod read_thread; mod unarchive_thread; mod update_thread_metadata; @@ -9,7 +11,16 @@ mod update_thread_metadata; mod test_support; use async_trait::async_trait; +use codex_protocol::ThreadId; use codex_rollout::RolloutConfig; +use codex_rollout::RolloutRecorder; +use codex_rollout::StateDbHandle; +use std::collections::HashMap; +use std::collections::hash_map::Entry; +use std::path::PathBuf; +use std::sync::Arc; +use tokio::sync::Mutex; +use tokio::sync::OnceCell; use crate::AppendThreadItemsParams; use crate::ArchiveThreadParams; @@ -17,32 +28,56 @@ use crate::CreateThreadParams; use crate::ListThreadsParams; use crate::LoadThreadHistoryParams; use crate::ReadThreadParams; -use crate::ResumeThreadRecorderParams; +use crate::ResumeThreadParams; use crate::StoredThread; use crate::StoredThreadHistory; use crate::ThreadPage; -use crate::ThreadRecorder; use crate::ThreadStore; use crate::ThreadStoreError; use crate::ThreadStoreResult; use crate::UpdateThreadMetadataParams; /// Local filesystem/SQLite-backed implementation of [`ThreadStore`]. -#[derive(Clone, Debug)] +#[derive(Clone)] pub struct LocalThreadStore { pub(super) config: RolloutConfig, + live_recorders: Arc>>, + state_db: Arc>, +} + +impl std::fmt::Debug for LocalThreadStore { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + f.debug_struct("LocalThreadStore") + .field("config", &self.config) + .finish_non_exhaustive() + } } impl LocalThreadStore { /// Create a local store from the rollout configuration used by existing local persistence. pub fn new(config: RolloutConfig) -> Self { - Self { config } + Self { + config, + live_recorders: Arc::new(Mutex::new(HashMap::new())), + state_db: Arc::new(OnceCell::new()), + } + } + + /// Return the state DB handle used by local rollout writers. + pub async fn state_db(&self) -> Option { + self.state_db + .get_or_try_init(|| async { + codex_rollout::state_db::init(&self.config).await.ok_or(()) + }) + .await + .ok() + .cloned() } /// Read a local rollout-backed thread by path. pub async fn read_thread_by_rollout_path( &self, - rollout_path: std::path::PathBuf, + rollout_path: PathBuf, include_archived: bool, include_history: bool, ) -> ThreadStoreResult { @@ -54,6 +89,51 @@ impl LocalThreadStore { ) .await } + + /// Return the live local rollout path for legacy local-only code paths. + pub async fn live_rollout_path(&self, thread_id: ThreadId) -> ThreadStoreResult { + live_writer::rollout_path(self, thread_id).await + } + + pub(super) async fn live_recorder( + &self, + thread_id: ThreadId, + ) -> ThreadStoreResult { + self.live_recorders + .lock() + .await + .get(&thread_id) + .cloned() + .ok_or(ThreadStoreError::ThreadNotFound { thread_id }) + } + + pub(super) async fn ensure_live_recorder_absent( + &self, + thread_id: ThreadId, + ) -> ThreadStoreResult<()> { + if self.live_recorders.lock().await.contains_key(&thread_id) { + return Err(ThreadStoreError::InvalidRequest { + message: format!("thread {thread_id} already has a live local writer"), + }); + } + Ok(()) + } + + pub(super) async fn insert_live_recorder( + &self, + thread_id: ThreadId, + recorder: RolloutRecorder, + ) -> ThreadStoreResult<()> { + match self.live_recorders.lock().await.entry(thread_id) { + Entry::Occupied(entry) => Err(ThreadStoreError::InvalidRequest { + message: format!("thread {} already has a live local writer", entry.key()), + }), + Entry::Vacant(entry) => { + entry.insert(recorder); + Ok(()) + } + } + } } #[async_trait] @@ -62,29 +142,65 @@ impl ThreadStore for LocalThreadStore { self } - async fn create_thread( - &self, - _params: CreateThreadParams, - ) -> ThreadStoreResult> { - unsupported("create_thread") + async fn create_thread(&self, params: CreateThreadParams) -> ThreadStoreResult<()> { + live_writer::create_thread(self, params).await } - async fn resume_thread_recorder( - &self, - _params: ResumeThreadRecorderParams, - ) -> ThreadStoreResult> { - unsupported("resume_thread_recorder") + async fn resume_thread(&self, params: ResumeThreadParams) -> ThreadStoreResult<()> { + live_writer::resume_thread(self, params).await } - async fn append_items(&self, _params: AppendThreadItemsParams) -> ThreadStoreResult<()> { - unsupported("append_items") + async fn append_items(&self, params: AppendThreadItemsParams) -> ThreadStoreResult<()> { + live_writer::append_items(self, params).await + } + + async fn persist_thread(&self, thread_id: ThreadId) -> ThreadStoreResult<()> { + live_writer::persist_thread(self, thread_id).await + } + + async fn flush_thread(&self, thread_id: ThreadId) -> ThreadStoreResult<()> { + live_writer::flush_thread(self, thread_id).await + } + + async fn shutdown_thread(&self, thread_id: ThreadId) -> ThreadStoreResult<()> { + live_writer::shutdown_thread(self, thread_id).await + } + + async fn discard_thread(&self, thread_id: ThreadId) -> ThreadStoreResult<()> { + live_writer::discard_thread(self, thread_id).await } async fn load_history( &self, - _params: LoadThreadHistoryParams, + params: LoadThreadHistoryParams, ) -> ThreadStoreResult { - unsupported("load_history") + if let Ok(rollout_path) = live_writer::rollout_path(self, params.thread_id).await { + return read_thread::read_thread_by_rollout_path( + self, + rollout_path, + /*include_archived*/ true, + /*include_history*/ true, + ) + .await? + .history + .ok_or_else(|| ThreadStoreError::Internal { + message: format!("failed to load history for thread {}", params.thread_id), + }); + } + + read_thread::read_thread( + self, + ReadThreadParams { + thread_id: params.thread_id, + include_archived: params.include_archived, + include_history: true, + }, + ) + .await? + .history + .ok_or_else(|| ThreadStoreError::Internal { + message: format!("failed to load history for thread {}", params.thread_id), + }) } async fn read_thread(&self, params: ReadThreadParams) -> ThreadStoreResult { @@ -114,8 +230,313 @@ impl ThreadStore for LocalThreadStore { } } -fn unsupported(operation: &str) -> ThreadStoreResult { - Err(ThreadStoreError::Internal { - message: format!("local thread store does not implement {operation} in this slice"), - }) +#[cfg(test)] +mod tests { + use codex_protocol::ThreadId; + use codex_protocol::models::BaseInstructions; + use codex_protocol::protocol::EventMsg; + use codex_protocol::protocol::RolloutItem; + use codex_protocol::protocol::SessionSource; + use codex_protocol::protocol::UserMessageEvent; + use tempfile::TempDir; + + use super::*; + use crate::ThreadEventPersistenceMode; + use crate::local::test_support::test_config; + use crate::local::test_support::write_archived_session_file; + use crate::local::test_support::write_session_file; + + #[tokio::test] + async fn live_writer_lifecycle_writes_and_closes() { + let home = TempDir::new().expect("temp dir"); + let store = LocalThreadStore::new(test_config(home.path())); + let thread_id = ThreadId::default(); + + store + .create_thread(create_thread_params(thread_id)) + .await + .expect("create live thread"); + let rollout_path = store + .live_rollout_path(thread_id) + .await + .expect("load rollout path"); + + store + .append_items(AppendThreadItemsParams { + thread_id, + items: vec![user_message_item("first live write")], + }) + .await + .expect("append live item"); + store + .persist_thread(thread_id) + .await + .expect("persist live thread"); + store + .flush_thread(thread_id) + .await + .expect("flush live thread"); + + assert_rollout_contains_message(rollout_path.as_path(), "first live write").await; + + store + .shutdown_thread(thread_id) + .await + .expect("shutdown live thread"); + let err = store + .append_items(AppendThreadItemsParams { + thread_id, + items: vec![user_message_item("write after shutdown")], + }) + .await + .expect_err("shutdown should remove the live thread writer"); + assert!( + matches!(err, ThreadStoreError::ThreadNotFound { thread_id: missing } if missing == thread_id) + ); + } + + #[tokio::test] + async fn discard_thread_drops_unmaterialized_live_writer() { + let home = TempDir::new().expect("temp dir"); + let store = LocalThreadStore::new(test_config(home.path())); + let thread_id = ThreadId::default(); + + store + .create_thread(create_thread_params(thread_id)) + .await + .expect("create live thread"); + let rollout_path = store + .live_rollout_path(thread_id) + .await + .expect("load rollout path"); + store + .discard_thread(thread_id) + .await + .expect("discard live thread"); + + assert!( + !tokio::fs::try_exists(rollout_path.as_path()) + .await + .expect("check rollout path") + ); + let err = store + .append_items(AppendThreadItemsParams { + thread_id, + items: vec![user_message_item("write after discard")], + }) + .await + .expect_err("discard should remove the live thread writer"); + assert!( + matches!(err, ThreadStoreError::ThreadNotFound { thread_id: missing } if missing == thread_id) + ); + } + + #[tokio::test] + async fn resume_thread_reopens_live_writer_and_appends() { + let home = TempDir::new().expect("temp dir"); + let config = test_config(home.path()); + let thread_id = ThreadId::default(); + + let first_store = LocalThreadStore::new(config.clone()); + first_store + .create_thread(create_thread_params(thread_id)) + .await + .expect("create initial thread"); + first_store + .append_items(AppendThreadItemsParams { + thread_id, + items: vec![user_message_item("before resume")], + }) + .await + .expect("append initial item"); + first_store + .persist_thread(thread_id) + .await + .expect("persist initial thread"); + first_store + .flush_thread(thread_id) + .await + .expect("flush initial thread"); + let rollout_path = first_store + .live_rollout_path(thread_id) + .await + .expect("load rollout path"); + first_store + .shutdown_thread(thread_id) + .await + .expect("shutdown initial writer"); + + let resumed_store = LocalThreadStore::new(config); + resumed_store + .resume_thread(ResumeThreadParams { + thread_id, + rollout_path: None, + history: None, + include_archived: true, + event_persistence_mode: ThreadEventPersistenceMode::Limited, + }) + .await + .expect("resume live thread"); + resumed_store + .append_items(AppendThreadItemsParams { + thread_id, + items: vec![user_message_item("after resume")], + }) + .await + .expect("append resumed item"); + resumed_store + .flush_thread(thread_id) + .await + .expect("flush resumed thread"); + + assert_rollout_contains_message(rollout_path.as_path(), "before resume").await; + assert_rollout_contains_message(rollout_path.as_path(), "after resume").await; + } + + #[tokio::test] + async fn create_thread_rejects_duplicate_live_writer() { + let home = TempDir::new().expect("temp dir"); + let store = LocalThreadStore::new(test_config(home.path())); + let thread_id = ThreadId::default(); + + store + .create_thread(create_thread_params(thread_id)) + .await + .expect("create live thread"); + + let err = store + .create_thread(create_thread_params(thread_id)) + .await + .expect_err("duplicate live writer should fail"); + + assert!(matches!(err, ThreadStoreError::InvalidRequest { .. })); + assert!(err.to_string().contains("already has a live local writer")); + } + + #[tokio::test] + async fn load_history_uses_live_writer_rollout_path() { + let home = TempDir::new().expect("temp dir"); + let external_home = TempDir::new().expect("external temp dir"); + let store = LocalThreadStore::new(test_config(home.path())); + let uuid = uuid::Uuid::from_u128(404); + let thread_id = ThreadId::from_string(&uuid.to_string()).expect("valid thread id"); + let rollout_path = write_session_file(external_home.path(), "2025-01-04T10-00-00", uuid) + .expect("external session file"); + + store + .resume_thread(ResumeThreadParams { + thread_id, + rollout_path: Some(rollout_path), + history: None, + include_archived: true, + event_persistence_mode: ThreadEventPersistenceMode::Limited, + }) + .await + .expect("resume live thread"); + store + .append_items(AppendThreadItemsParams { + thread_id, + items: vec![user_message_item("external history item")], + }) + .await + .expect("append live item"); + store + .flush_thread(thread_id) + .await + .expect("flush live thread"); + + let history = store + .load_history(LoadThreadHistoryParams { + thread_id, + include_archived: false, + }) + .await + .expect("load external live history"); + + assert!(history.items.iter().any(|item| { + matches!( + item, + RolloutItem::EventMsg(EventMsg::UserMessage(event)) if event.message == "external history item" + ) + })); + } + + #[tokio::test] + async fn load_history_uses_live_writer_rollout_path_for_archived_source() { + let home = TempDir::new().expect("temp dir"); + let store = LocalThreadStore::new(test_config(home.path())); + let uuid = uuid::Uuid::from_u128(405); + let thread_id = ThreadId::from_string(&uuid.to_string()).expect("valid thread id"); + let rollout_path = write_archived_session_file(home.path(), "2025-01-04T10-30-00", uuid) + .expect("archived session file"); + + store + .resume_thread(ResumeThreadParams { + thread_id, + rollout_path: Some(rollout_path), + history: None, + include_archived: true, + event_persistence_mode: ThreadEventPersistenceMode::Limited, + }) + .await + .expect("resume live archived thread"); + store + .append_items(AppendThreadItemsParams { + thread_id, + items: vec![user_message_item("archived live history item")], + }) + .await + .expect("append live item"); + store + .flush_thread(thread_id) + .await + .expect("flush live thread"); + + let history = store + .load_history(LoadThreadHistoryParams { + thread_id, + include_archived: false, + }) + .await + .expect("load archived live history"); + + assert!(history.items.iter().any(|item| { + matches!( + item, + RolloutItem::EventMsg(EventMsg::UserMessage(event)) if event.message == "archived live history item" + ) + })); + } + + fn create_thread_params(thread_id: ThreadId) -> CreateThreadParams { + CreateThreadParams { + thread_id, + forked_from_id: None, + source: SessionSource::Exec, + base_instructions: BaseInstructions::default(), + dynamic_tools: Vec::new(), + event_persistence_mode: ThreadEventPersistenceMode::Limited, + } + } + + fn user_message_item(message: &str) -> RolloutItem { + RolloutItem::EventMsg(EventMsg::UserMessage(UserMessageEvent { + message: message.to_string(), + images: None, + local_images: Vec::new(), + text_elements: Vec::new(), + })) + } + + async fn assert_rollout_contains_message(path: &std::path::Path, expected: &str) { + let (items, _, _) = RolloutRecorder::load_rollout_items(path) + .await + .expect("load rollout items"); + assert!(items.iter().any(|item| { + matches!( + item, + RolloutItem::EventMsg(EventMsg::UserMessage(event)) if event.message == expected + ) + })); + } } diff --git a/codex-rs/thread-store/src/local/update_thread_metadata.rs b/codex-rs/thread-store/src/local/update_thread_metadata.rs index fae90e017b..52c937c6bb 100644 --- a/codex-rs/thread-store/src/local/update_thread_metadata.rs +++ b/codex-rs/thread-store/src/local/update_thread_metadata.rs @@ -5,7 +5,7 @@ use codex_protocol::protocol::EventMsg; use codex_protocol::protocol::RolloutItem; use codex_protocol::protocol::ThreadMemoryMode; use codex_protocol::protocol::ThreadNameUpdatedEvent; -use codex_rollout::StateDbHandle; +use codex_rollout::ARCHIVED_SESSIONS_SUBDIR; use codex_rollout::append_rollout_item_to_path; use codex_rollout::append_thread_name; use codex_rollout::find_archived_thread_path_by_id_str; @@ -13,6 +13,7 @@ use codex_rollout::find_thread_path_by_id_str; use codex_rollout::read_session_meta_line; use super::LocalThreadStore; +use super::live_writer; use crate::ReadThreadParams; use crate::StoredThread; use crate::ThreadStoreError; @@ -53,7 +54,7 @@ pub(super) async fn update_thread_metadata( .await?; } - let state_db_ctx = open_state_db_for_direct_thread_lookup(store).await; + let state_db_ctx = store.state_db().await; codex_rollout::state_db::reconcile_rollout( state_db_ctx.as_deref(), resolved_rollout_path.path.as_path(), @@ -65,7 +66,7 @@ pub(super) async fn update_thread_metadata( ) .await; - read_thread::read_thread( + match read_thread::read_thread( store, ReadThreadParams { thread_id, @@ -74,6 +75,18 @@ pub(super) async fn update_thread_metadata( }, ) .await + { + Ok(thread) => Ok(thread), + Err(_) => { + read_thread::read_thread_by_rollout_path( + store, + resolved_rollout_path.path, + params.include_archived, + /*include_history*/ false, + ) + .await + } + } } async fn apply_thread_name( @@ -127,15 +140,6 @@ async fn apply_thread_memory_mode( }) } -async fn open_state_db_for_direct_thread_lookup(store: &LocalThreadStore) -> Option { - codex_state::StateRuntime::init( - store.config.sqlite_home.clone(), - store.config.model_provider_id.clone(), - ) - .await - .ok() -} - fn memory_mode_as_str(mode: ThreadMemoryMode) -> &'static str { match mode { ThreadMemoryMode::Enabled => "enabled", @@ -148,6 +152,11 @@ async fn resolve_rollout_path( thread_id: ThreadId, include_archived: bool, ) -> ThreadStoreResult { + if let Ok(path) = live_writer::rollout_path(store, thread_id).await { + let archived = rollout_path_is_archived(store, path.as_path()); + return Ok(ResolvedRolloutPath { path, archived }); + } + let active_path = find_thread_path_by_id_str(store.config.codex_home.as_path(), &thread_id.to_string()) .await @@ -179,6 +188,10 @@ async fn resolve_rollout_path( }) } +fn rollout_path_is_archived(store: &LocalThreadStore, path: &std::path::Path) -> bool { + path.starts_with(store.config.codex_home.join(ARCHIVED_SESSIONS_SUBDIR)) +} + #[cfg(test)] mod tests { use pretty_assertions::assert_eq; @@ -187,6 +200,8 @@ mod tests { use uuid::Uuid; use super::*; + use crate::ResumeThreadParams; + use crate::ThreadEventPersistenceMode; use crate::ThreadMetadataPatch; use crate::ThreadStore; use crate::local::LocalThreadStore; @@ -231,11 +246,18 @@ mod tests { #[tokio::test] async fn update_thread_metadata_sets_memory_mode_on_active_rollout() { let home = TempDir::new().expect("temp dir"); - let store = LocalThreadStore::new(test_config(home.path())); + let config = test_config(home.path()); + let store = LocalThreadStore::new(config.clone()); let uuid = Uuid::from_u128(302); let thread_id = ThreadId::from_string(&uuid.to_string()).expect("valid thread id"); let path = write_session_file(home.path(), "2025-01-03T14-30-00", uuid).expect("session file"); + let runtime = codex_state::StateRuntime::init( + home.path().to_path_buf(), + config.model_provider_id.clone(), + ) + .await + .expect("state db should initialize"); let thread = store .update_thread_metadata(UpdateThreadMetadataParams { @@ -254,6 +276,51 @@ mod tests { assert_eq!(appended["type"], "session_meta"); assert_eq!(appended["payload"]["id"], thread_id.to_string()); assert_eq!(appended["payload"]["memory_mode"], "disabled"); + let memory_mode = runtime + .get_thread_memory_mode(thread_id) + .await + .expect("thread memory mode should be readable"); + assert_eq!(memory_mode.as_deref(), Some("disabled")); + } + + #[tokio::test] + async fn update_thread_metadata_uses_live_rollout_path_for_external_resume() { + let home = TempDir::new().expect("temp dir"); + let external_home = TempDir::new().expect("external temp dir"); + let store = LocalThreadStore::new(test_config(home.path())); + let uuid = Uuid::from_u128(307); + let thread_id = ThreadId::from_string(&uuid.to_string()).expect("valid thread id"); + let path = write_session_file(external_home.path(), "2025-01-03T14-45-00", uuid) + .expect("external session file"); + + store + .resume_thread(ResumeThreadParams { + thread_id, + rollout_path: Some(path.clone()), + history: None, + include_archived: true, + event_persistence_mode: ThreadEventPersistenceMode::Limited, + }) + .await + .expect("resume external live thread"); + + let thread = store + .update_thread_metadata(UpdateThreadMetadataParams { + thread_id, + patch: ThreadMetadataPatch { + memory_mode: Some(ThreadMemoryMode::Disabled), + ..Default::default() + }, + include_archived: false, + }) + .await + .expect("set memory mode on external live thread"); + + assert_eq!(thread.thread_id, thread_id); + assert!(thread.rollout_path.is_some()); + let appended = last_rollout_item(path.as_path()); + assert_eq!(appended["type"], "session_meta"); + assert_eq!(appended["payload"]["memory_mode"], "disabled"); } #[tokio::test] @@ -385,6 +452,70 @@ mod tests { ); } + #[tokio::test] + async fn update_thread_metadata_keeps_live_archived_thread_archived_in_sqlite() { + let home = TempDir::new().expect("temp dir"); + let config = test_config(home.path()); + let store = LocalThreadStore::new(config.clone()); + let uuid = Uuid::from_u128(308); + let thread_id = ThreadId::from_string(&uuid.to_string()).expect("valid thread id"); + let archived_path = write_archived_session_file(home.path(), "2025-01-03T16-30-00", uuid) + .expect("archived session file"); + let runtime = codex_state::StateRuntime::init( + home.path().to_path_buf(), + config.model_provider_id.clone(), + ) + .await + .expect("state db should initialize"); + runtime + .mark_backfill_complete(/*last_watermark*/ None) + .await + .expect("backfill should be complete"); + codex_rollout::state_db::reconcile_rollout( + Some(runtime.as_ref()), + archived_path.as_path(), + config.model_provider_id.as_str(), + /*builder*/ None, + &[], + /*archived_only*/ Some(true), + /*new_thread_memory_mode*/ None, + ) + .await; + store + .resume_thread(ResumeThreadParams { + thread_id, + rollout_path: Some(archived_path.clone()), + history: None, + include_archived: true, + event_persistence_mode: ThreadEventPersistenceMode::Limited, + }) + .await + .expect("resume archived live thread"); + + let thread = store + .update_thread_metadata(UpdateThreadMetadataParams { + thread_id, + patch: ThreadMetadataPatch { + name: Some("Live archived title".to_string()), + ..Default::default() + }, + include_archived: true, + }) + .await + .expect("set archived thread name"); + + assert!(thread.archived_at.is_some()); + assert!( + runtime + .get_thread(thread_id) + .await + .expect("get metadata") + .expect("metadata") + .archived_at + .is_some() + ); + } + fn last_rollout_item(path: &std::path::Path) -> Value { let last_line = std::fs::read_to_string(path) .expect("read rollout") diff --git a/codex-rs/thread-store/src/recorder.rs b/codex-rs/thread-store/src/recorder.rs deleted file mode 100644 index 03b02e80c8..0000000000 --- a/codex-rs/thread-store/src/recorder.rs +++ /dev/null @@ -1,28 +0,0 @@ -use async_trait::async_trait; -use codex_protocol::ThreadId; -use codex_protocol::protocol::RolloutItem; - -use crate::ThreadStoreResult; - -/// Live append handle for a thread. -/// -/// This is the storage-neutral version of the existing rollout recorder API. The local -/// implementation is expected to wrap `codex_rollout::RolloutRecorder` and preserve its lazy -/// materialization, filtering, flush, and shutdown behavior. -#[async_trait] -pub trait ThreadRecorder: Send + Sync { - /// Returns the thread id this recorder appends to. - fn thread_id(&self) -> ThreadId; - - /// Queues items for persistence according to this recorder's filtering policy. - async fn record_items(&self, items: &[RolloutItem]) -> ThreadStoreResult<()>; - - /// Materializes the thread if persistence is lazy, then persists all queued items. - async fn persist(&self) -> ThreadStoreResult<()>; - - /// Flushes all queued items and returns once they are durable/readable. - async fn flush(&self) -> ThreadStoreResult<()>; - - /// Flushes pending items and closes the recorder. - async fn shutdown(&self) -> ThreadStoreResult<()>; -} diff --git a/codex-rs/thread-store/src/remote/mod.rs b/codex-rs/thread-store/src/remote/mod.rs index 5be760870e..b3ddeecf76 100644 --- a/codex-rs/thread-store/src/remote/mod.rs +++ b/codex-rs/thread-store/src/remote/mod.rs @@ -2,6 +2,7 @@ mod helpers; mod list_threads; use async_trait::async_trait; +use codex_protocol::ThreadId; use crate::AppendThreadItemsParams; use crate::ArchiveThreadParams; @@ -9,11 +10,10 @@ use crate::CreateThreadParams; use crate::ListThreadsParams; use crate::LoadThreadHistoryParams; use crate::ReadThreadParams; -use crate::ResumeThreadRecorderParams; +use crate::ResumeThreadParams; use crate::StoredThread; use crate::StoredThreadHistory; use crate::ThreadPage; -use crate::ThreadRecorder; use crate::ThreadStore; use crate::ThreadStoreError; use crate::ThreadStoreResult; @@ -52,24 +52,34 @@ impl ThreadStore for RemoteThreadStore { self } - async fn create_thread( - &self, - _params: CreateThreadParams, - ) -> ThreadStoreResult> { + async fn create_thread(&self, _params: CreateThreadParams) -> ThreadStoreResult<()> { Err(not_implemented("create_thread")) } - async fn resume_thread_recorder( - &self, - _params: ResumeThreadRecorderParams, - ) -> ThreadStoreResult> { - Err(not_implemented("resume_thread_recorder")) + async fn resume_thread(&self, _params: ResumeThreadParams) -> ThreadStoreResult<()> { + Err(not_implemented("resume_thread")) } async fn append_items(&self, _params: AppendThreadItemsParams) -> ThreadStoreResult<()> { Err(not_implemented("append_items")) } + async fn persist_thread(&self, _thread_id: ThreadId) -> ThreadStoreResult<()> { + Err(not_implemented("persist_thread")) + } + + async fn flush_thread(&self, _thread_id: ThreadId) -> ThreadStoreResult<()> { + Err(not_implemented("flush_thread")) + } + + async fn shutdown_thread(&self, _thread_id: ThreadId) -> ThreadStoreResult<()> { + Err(not_implemented("shutdown_thread")) + } + + async fn discard_thread(&self, _thread_id: ThreadId) -> ThreadStoreResult<()> { + Err(not_implemented("discard_thread")) + } + async fn load_history( &self, _params: LoadThreadHistoryParams, diff --git a/codex-rs/thread-store/src/store.rs b/codex-rs/thread-store/src/store.rs index 56cbd05f5c..def8bf0828 100644 --- a/codex-rs/thread-store/src/store.rs +++ b/codex-rs/thread-store/src/store.rs @@ -1,6 +1,6 @@ -use std::any::Any; - use async_trait::async_trait; +use codex_protocol::ThreadId; +use std::any::Any; use crate::AppendThreadItemsParams; use crate::ArchiveThreadParams; @@ -8,11 +8,10 @@ use crate::CreateThreadParams; use crate::ListThreadsParams; use crate::LoadThreadHistoryParams; use crate::ReadThreadParams; -use crate::ResumeThreadRecorderParams; +use crate::ResumeThreadParams; use crate::StoredThread; use crate::StoredThreadHistory; use crate::ThreadPage; -use crate::ThreadRecorder; use crate::ThreadStoreResult; use crate::UpdateThreadMetadataParams; @@ -23,21 +22,31 @@ pub trait ThreadStore: Any + Send + Sync { /// make sense for a concrete store implementation. fn as_any(&self) -> &dyn Any; - /// Creates a new thread and returns a live recorder for future appends. - async fn create_thread( - &self, - params: CreateThreadParams, - ) -> ThreadStoreResult>; + /// Creates a new live thread. + async fn create_thread(&self, params: CreateThreadParams) -> ThreadStoreResult<()>; - /// Reopens a live recorder for an existing thread. - async fn resume_thread_recorder( - &self, - params: ResumeThreadRecorderParams, - ) -> ThreadStoreResult>; + /// Reopens an existing thread for live appends. + async fn resume_thread(&self, params: ResumeThreadParams) -> ThreadStoreResult<()>; - /// Appends items to a stored thread outside the live-recorder path. + /// Appends items to a live thread. async fn append_items(&self, params: AppendThreadItemsParams) -> ThreadStoreResult<()>; + /// Materializes the thread if persistence is lazy, then persists all queued items. + async fn persist_thread(&self, thread_id: ThreadId) -> ThreadStoreResult<()>; + + /// Flushes all queued items and returns once they are durable/readable. + async fn flush_thread(&self, thread_id: ThreadId) -> ThreadStoreResult<()>; + + /// Flushes pending items and closes the live thread writer. + async fn shutdown_thread(&self, thread_id: ThreadId) -> ThreadStoreResult<()>; + + /// Discards the live thread writer without forcing pending in-memory items to become durable. + /// + /// Core calls this when session initialization fails after a live writer has been created. + /// Implementations should release any live writer resources for the thread while preserving + /// already-durable thread data. + async fn discard_thread(&self, thread_id: ThreadId) -> ThreadStoreResult<()>; + /// Loads persisted history for resume, fork, rollback, and memory jobs. async fn load_history( &self, diff --git a/codex-rs/thread-store/src/types.rs b/codex-rs/thread-store/src/types.rs index 1f12bea64a..537b093200 100644 --- a/codex-rs/thread-store/src/types.rs +++ b/codex-rs/thread-store/src/types.rs @@ -26,7 +26,7 @@ pub enum ThreadEventPersistenceMode { Extended, } -/// Parameters required to create a persisted thread and its recorder. +/// Parameters required to create a persisted thread. #[derive(Clone, Debug, Serialize, Deserialize)] pub struct CreateThreadParams { /// Thread id generated by Codex before opening persistence. @@ -39,22 +39,26 @@ pub struct CreateThreadParams { pub base_instructions: BaseInstructions, /// Dynamic tools available to the thread at startup. pub dynamic_tools: Vec, - /// Whether the recorder should persist the extended event surface. + /// Whether persistence should include the extended event surface. pub event_persistence_mode: ThreadEventPersistenceMode, } /// Parameters required to reopen persistence for an existing thread. -#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)] -pub struct ResumeThreadRecorderParams { +#[derive(Clone, Debug, Serialize, Deserialize)] +pub struct ResumeThreadParams { /// Existing thread id whose future items should be appended. pub thread_id: ThreadId, + /// Known local rollout path when the caller resumed from a specific file. + pub rollout_path: Option, + /// Known replay history for the resumed thread, if already loaded by the caller. + pub history: Option>, /// Whether archived threads may be reopened. pub include_archived: bool, - /// Whether the recorder should persist the extended event surface. + /// Whether persistence should include the extended event surface. pub event_persistence_mode: ThreadEventPersistenceMode, } -/// Parameters for appending rollout items outside a live recorder. +/// Parameters for appending rollout items to a live thread. #[derive(Clone, Debug, Serialize, Deserialize)] pub struct AppendThreadItemsParams { /// Thread id to append to. diff --git a/codex-rs/tui/src/app/app_server_requests.rs b/codex-rs/tui/src/app/app_server_requests.rs index 3149dd9f59..5c2c693855 100644 --- a/codex-rs/tui/src/app/app_server_requests.rs +++ b/codex-rs/tui/src/app/app_server_requests.rs @@ -562,7 +562,20 @@ mod tests { read: Some(vec![absolute_path(read_path)]), write: Some(vec![absolute_path(write_path)]), glob_scan_max_depth: None, - entries: None, + entries: Some(vec![ + codex_app_server_protocol::FileSystemSandboxEntry { + path: codex_app_server_protocol::FileSystemPath::Path { + path: absolute_path(read_path), + }, + access: codex_app_server_protocol::FileSystemAccessMode::Read, + }, + codex_app_server_protocol::FileSystemSandboxEntry { + path: codex_app_server_protocol::FileSystemPath::Path { + path: absolute_path(write_path), + }, + access: codex_app_server_protocol::FileSystemAccessMode::Write, + }, + ]), }), }, scope: PermissionGrantScope::Session, diff --git a/codex-rs/tui/src/app/config_persistence.rs b/codex-rs/tui/src/app/config_persistence.rs index 7ed3b29766..3fc4ed0bdc 100644 --- a/codex-rs/tui/src/app/config_persistence.rs +++ b/codex-rs/tui/src/app/config_persistence.rs @@ -72,13 +72,15 @@ impl App { "Failed to carry forward approval policy override: {err}" )); } - if let Some(policy) = self.runtime_sandbox_policy_override.as_ref() - && let Err(err) = config.permissions.sandbox_policy.set(policy.clone()) - { - tracing::warn!(%err, "failed to carry forward sandbox policy override"); - self.chat_widget.add_error_message(format!( - "Failed to carry forward sandbox policy override: {err}" - )); + if let Some(policy) = self.runtime_sandbox_policy_override.as_ref() { + if let Err(err) = config.permissions.sandbox_policy.set(policy.clone()) { + tracing::warn!(%err, "failed to carry forward sandbox policy override"); + self.chat_widget.add_error_message(format!( + "Failed to carry forward sandbox policy override: {err}" + )); + } else { + sync_runtime_permissions_from_legacy_sandbox_policy(config); + } } } @@ -117,6 +119,7 @@ impl App { .add_error_message(format!("{user_message_prefix}: {err}")); return false; } + sync_runtime_permissions_from_legacy_sandbox_policy(config); true } @@ -306,6 +309,10 @@ impl App { self.chat_widget .add_error_message(format!("Failed to enable Auto-review: {err}")); } + if sandbox_policy_override.is_some() { + self.runtime_sandbox_policy_override = + Some(self.config.permissions.sandbox_policy.get().clone()); + } if approval_policy_override.is_some() || approvals_reviewer_override.is_some() @@ -536,6 +543,17 @@ impl App { } } +fn sync_runtime_permissions_from_legacy_sandbox_policy(config: &mut Config) { + let sandbox_policy = config.permissions.sandbox_policy.get(); + config.permissions.file_system_sandbox_policy = + codex_protocol::permissions::FileSystemSandboxPolicy::from_legacy_sandbox_policy( + sandbox_policy, + &config.cwd, + ); + config.permissions.network_sandbox_policy = + codex_protocol::permissions::NetworkSandboxPolicy::from(sandbox_policy); +} + #[cfg(test)] mod tests { use super::*; diff --git a/codex-rs/tui/src/app/startup_prompts.rs b/codex-rs/tui/src/app/startup_prompts.rs index c5bc541a03..284f94fbcb 100644 --- a/codex-rs/tui/src/app/startup_prompts.rs +++ b/codex-rs/tui/src/app/startup_prompts.rs @@ -139,6 +139,28 @@ pub(super) fn target_preset_for_upgrade<'a>( .find(|preset| preset.model == target_model && preset.show_in_picker) } +pub(super) fn apply_accepted_model_migration( + config: &mut Config, + app_event_tx: &AppEventSender, + from_model: String, + target_model: String, + target_default_effort: ReasoningEffortConfig, +) { + app_event_tx.send(AppEvent::PersistModelMigrationPromptAcknowledged { + from_model, + to_model: target_model.clone(), + }); + + config.model = Some(target_model.clone()); + config.model_reasoning_effort = Some(target_default_effort); + app_event_tx.send(AppEvent::UpdateModel(target_model.clone())); + app_event_tx.send(AppEvent::UpdateReasoningEffort(Some(target_default_effort))); + app_event_tx.send(AppEvent::PersistModelSelection { + model: target_model, + effort: Some(target_default_effort), + }); +} + pub(super) const MODEL_AVAILABILITY_NUX_MAX_SHOW_COUNT: u32 = 4; #[derive(Debug, Clone, PartialEq, Eq)] @@ -218,7 +240,7 @@ pub(super) async fn handle_model_migration_prompt_if_needed( if let Some(ModelUpgrade { id: target_model, - reasoning_effort_mapping, + reasoning_effort_mapping: _, migration_config_key, model_link, upgrade_copy, @@ -263,30 +285,13 @@ pub(super) async fn handle_model_migration_prompt_if_needed( ); match run_model_migration_prompt(tui, prompt_copy).await { ModelMigrationOutcome::Accepted => { - app_event_tx.send(AppEvent::PersistModelMigrationPromptAcknowledged { - from_model: model.to_string(), - to_model: target_model.clone(), - }); - - let mapped_effort = if let Some(reasoning_effort_mapping) = reasoning_effort_mapping - && let Some(reasoning_effort) = config.model_reasoning_effort - { - reasoning_effort_mapping - .get(&reasoning_effort) - .cloned() - .or(config.model_reasoning_effort) - } else { - config.model_reasoning_effort - }; - - config.model = Some(target_model.clone()); - config.model_reasoning_effort = mapped_effort; - app_event_tx.send(AppEvent::UpdateModel(target_model.clone())); - app_event_tx.send(AppEvent::UpdateReasoningEffort(mapped_effort)); - app_event_tx.send(AppEvent::PersistModelSelection { - model: target_model.clone(), - effort: mapped_effort, - }); + apply_accepted_model_migration( + config, + app_event_tx, + model.to_string(), + target_model.clone(), + target_preset.default_reasoning_effort, + ); } ModelMigrationOutcome::Rejected => { app_event_tx.send(AppEvent::PersistModelMigrationPromptAcknowledged { diff --git a/codex-rs/tui/src/app/tests.rs b/codex-rs/tui/src/app/tests.rs index f473804cf2..b2141977ce 100644 --- a/codex-rs/tui/src/app/tests.rs +++ b/codex-rs/tui/src/app/tests.rs @@ -1643,7 +1643,10 @@ async fn update_feature_flags_enabling_guardian_selects_auto_review() -> Result< auto_review.approvals_reviewer ); assert_eq!(app.runtime_approval_policy_override, None); - assert_eq!(app.runtime_sandbox_policy_override, None); + assert_eq!( + app.runtime_sandbox_policy_override, + Some(auto_review.sandbox_policy.clone()) + ); assert_eq!( op_rx.try_recv(), Ok(Op::OverrideTurnContext { @@ -1675,7 +1678,7 @@ async fn update_feature_flags_enabling_guardian_selects_auto_review() -> Result< let config = std::fs::read_to_string(codex_home.path().join("config.toml"))?; assert!(config.contains("guardian_approval = true")); - assert!(config.contains("approvals_reviewer = \"auto_review\"")); + assert!(config.contains("approvals_reviewer = \"guardian_subagent\"")); assert!(config.contains("approval_policy = \"on-request\"")); assert!(config.contains("sandbox_mode = \"workspace-write\"")); Ok(()) @@ -1835,7 +1838,7 @@ async fn update_feature_flags_enabling_guardian_overrides_explicit_manual_review ); let config = std::fs::read_to_string(codex_home.path().join("config.toml"))?; - assert!(config.contains("approvals_reviewer = \"auto_review\"")); + assert!(config.contains("approvals_reviewer = \"guardian_subagent\"")); assert!(config.contains("guardian_approval = true")); assert!(config.contains("approval_policy = \"on-request\"")); assert!(config.contains("sandbox_mode = \"workspace-write\"")); @@ -1969,7 +1972,7 @@ async fn update_feature_flags_enabling_guardian_in_profile_sets_profile_auto_rev ); assert_eq!( profile_config.get("approvals_reviewer"), - Some(&TomlValue::String("auto_review".to_string())) + Some(&TomlValue::String("guardian_subagent".to_string())) ); Ok(()) } diff --git a/codex-rs/tui/src/app/tests/model_catalog.rs b/codex-rs/tui/src/app/tests/model_catalog.rs index e74d01e6db..ba04a3634f 100644 --- a/codex-rs/tui/src/app/tests/model_catalog.rs +++ b/codex-rs/tui/src/app/tests/model_catalog.rs @@ -1,7 +1,9 @@ use super::*; +use assert_matches::assert_matches; use codex_config::types::ModelAvailabilityNuxConfig; use codex_protocol::openai_models::ModelAvailabilityNux; use pretty_assertions::assert_eq; +use tokio::sync::mpsc::unbounded_channel; fn all_model_presets() -> Vec { crate::legacy_core::test_support::all_model_presets().clone() @@ -172,6 +174,61 @@ fn select_model_availability_nux_returns_none_when_all_models_are_exhausted() { assert_eq!(selected, None); } +#[tokio::test] +async fn accepted_model_migration_persists_target_default_reasoning_effort() { + let codex_home = tempdir().expect("temp codex home"); + let mut config = ConfigBuilder::default() + .codex_home(codex_home.path().to_path_buf()) + .build() + .await + .expect("config"); + config.model = Some("gpt-5.2".to_string()); + config.model_reasoning_effort = Some(ReasoningEffortConfig::XHigh); + + let (tx_raw, mut rx) = unbounded_channel(); + let app_event_tx = AppEventSender::new(tx_raw); + + apply_accepted_model_migration( + &mut config, + &app_event_tx, + "gpt-5.2".to_string(), + "gpt-5.4".to_string(), + ReasoningEffortConfig::Medium, + ); + + assert_eq!(config.model.as_deref(), Some("gpt-5.4")); + assert_eq!( + config.model_reasoning_effort, + Some(ReasoningEffortConfig::Medium) + ); + + let acknowledged = rx.try_recv().expect("acknowledged event"); + assert_matches!( + acknowledged, + AppEvent::PersistModelMigrationPromptAcknowledged { from_model, to_model } + if from_model == "gpt-5.2" && to_model == "gpt-5.4" + ); + + let update_model = rx.try_recv().expect("update model event"); + assert_matches!( + update_model, + AppEvent::UpdateModel(model) if model == "gpt-5.4" + ); + + let update_effort = rx.try_recv().expect("update effort event"); + assert_matches!( + update_effort, + AppEvent::UpdateReasoningEffort(Some(ReasoningEffortConfig::Medium)) + ); + + let persist_selection = rx.try_recv().expect("persist model selection event"); + assert_matches!( + persist_selection, + AppEvent::PersistModelSelection { model, effort } + if model == "gpt-5.4" && effort == Some(ReasoningEffortConfig::Medium) + ); +} + #[tokio::test] async fn model_migration_prompt_respects_hide_flag_and_self_target() { let mut seen = BTreeMap::new(); diff --git a/codex-rs/tui/src/app/thread_routing.rs b/codex-rs/tui/src/app/thread_routing.rs index 241695cc27..406e9ea7db 100644 --- a/codex-rs/tui/src/app/thread_routing.rs +++ b/codex-rs/tui/src/app/thread_routing.rs @@ -571,6 +571,14 @@ impl App { } } if should_start_turn { + let permission_profile = if !app_server.is_remote() + && self.runtime_sandbox_policy_override.is_some() + && !matches!(sandbox_policy, SandboxPolicy::ExternalSandbox { .. }) + { + Some(self.config.permissions.permission_profile()) + } else { + None + }; app_server .turn_start( thread_id, @@ -580,6 +588,7 @@ impl App { approvals_reviewer .unwrap_or(self.chat_widget.config_ref().approvals_reviewer), sandbox_policy.clone(), + permission_profile, model.to_string(), effort, *summary, diff --git a/codex-rs/tui/src/app/thread_session_state.rs b/codex-rs/tui/src/app/thread_session_state.rs index d33df0f681..302bcb5a63 100644 --- a/codex-rs/tui/src/app/thread_session_state.rs +++ b/codex-rs/tui/src/app/thread_session_state.rs @@ -3,6 +3,7 @@ use crate::app_server_session::ThreadSessionState; use crate::read_session_model; use codex_app_server_protocol::Thread; use codex_protocol::ThreadId; +use codex_protocol::protocol::SandboxPolicy; impl App { pub(super) async fn sync_active_thread_permission_settings_to_cached_session(&mut self) { @@ -13,10 +14,22 @@ impl App { let approval_policy = self.config.permissions.approval_policy.value(); let approvals_reviewer = self.config.approvals_reviewer; let sandbox_policy = self.config.permissions.sandbox_policy.get().clone(); + let permission_profile = if matches!(sandbox_policy, SandboxPolicy::ExternalSandbox { .. }) + { + None + } else { + Some( + self.chat_widget + .config_ref() + .permissions + .permission_profile(), + ) + }; let update_session = |session: &mut ThreadSessionState| { session.approval_policy = approval_policy; session.approvals_reviewer = approvals_reviewer; session.sandbox_policy = sandbox_policy.clone(); + session.permission_profile = permission_profile.clone(); }; if self.primary_thread_id == Some(active_thread_id) @@ -91,7 +104,14 @@ mod tests { use crate::test_support::PathBufExt; use crate::test_support::test_path_buf; use codex_config::types::ApprovalsReviewer; + use codex_protocol::models::PermissionProfile; use codex_protocol::protocol::AskForApproval; + use codex_protocol::protocol::FileSystemAccessMode; + use codex_protocol::protocol::FileSystemPath; + use codex_protocol::protocol::FileSystemSandboxEntry; + use codex_protocol::protocol::FileSystemSandboxPolicy; + use codex_protocol::protocol::FileSystemSpecialPath; + use codex_protocol::protocol::NetworkSandboxPolicy; use codex_protocol::protocol::SandboxPolicy; use pretty_assertions::assert_eq; use std::path::PathBuf; @@ -157,8 +177,17 @@ mod tests { app.config.permissions.approval_policy = codex_config::Constrained::allow_any(AskForApproval::OnRequest); app.config.approvals_reviewer = ApprovalsReviewer::AutoReview; + let expected_sandbox_policy = SandboxPolicy::new_workspace_write_policy(); + let expected_permission_profile = PermissionProfile::from_legacy_sandbox_policy( + &expected_sandbox_policy, + &main_session.cwd, + ); + app.chat_widget.handle_thread_session(main_session.clone()); + app.chat_widget + .set_sandbox_policy(expected_sandbox_policy.clone()) + .expect("set widget sandbox policy"); app.config.permissions.sandbox_policy = - codex_config::Constrained::allow_any(SandboxPolicy::new_workspace_write_policy()); + codex_config::Constrained::allow_any(expected_sandbox_policy.clone()); app.sync_active_thread_permission_settings_to_cached_session() .await; @@ -166,7 +195,8 @@ mod tests { let expected_main_session = ThreadSessionState { approval_policy: AskForApproval::OnRequest, approvals_reviewer: ApprovalsReviewer::AutoReview, - sandbox_policy: SandboxPolicy::new_workspace_write_policy(), + sandbox_policy: expected_sandbox_policy, + permission_profile: Some(expected_permission_profile), ..main_session }; assert_eq!( @@ -196,4 +226,67 @@ mod tests { .clone(); assert_eq!(side_store_session, Some(side_session)); } + + #[tokio::test] + async fn permission_settings_sync_preserves_active_profile_only_rules() { + let mut app = make_test_app().await; + let thread_id = + ThreadId::from_string("00000000-0000-0000-0000-000000000403").expect("valid thread"); + let profile = PermissionProfile::from_runtime_permissions( + &FileSystemSandboxPolicy::restricted(vec![ + FileSystemSandboxEntry { + path: FileSystemPath::Special { + value: FileSystemSpecialPath::Root, + }, + access: FileSystemAccessMode::Read, + }, + FileSystemSandboxEntry { + path: FileSystemPath::GlobPattern { + pattern: "**/.env".to_string(), + }, + access: FileSystemAccessMode::None, + }, + ]), + NetworkSandboxPolicy::Restricted, + ); + let session = ThreadSessionState { + permission_profile: Some(profile.clone()), + ..test_thread_session(thread_id, test_path_buf("/tmp/main")) + }; + + app.primary_thread_id = Some(thread_id); + app.active_thread_id = Some(thread_id); + app.primary_session_configured = Some(session.clone()); + app.thread_event_channels.insert( + thread_id, + ThreadEventChannel::new_with_session(/*capacity*/ 4, session.clone(), Vec::new()), + ); + app.chat_widget.handle_thread_session(session.clone()); + app.config.permissions.approval_policy = + codex_config::Constrained::allow_any(AskForApproval::OnRequest); + + app.sync_active_thread_permission_settings_to_cached_session() + .await; + + let expected_session = ThreadSessionState { + approval_policy: AskForApproval::OnRequest, + permission_profile: Some(profile), + ..session + }; + assert_eq!( + app.primary_session_configured, + Some(expected_session.clone()) + ); + + let store_session = app + .thread_event_channels + .get(&thread_id) + .expect("thread channel") + .store + .lock() + .await + .session + .clone(); + assert_eq!(store_session, Some(expected_session)); + } } diff --git a/codex-rs/tui/src/app_server_approval_conversions.rs b/codex-rs/tui/src/app_server_approval_conversions.rs index a26f9b2a90..a0d86db7d6 100644 --- a/codex-rs/tui/src/app_server_approval_conversions.rs +++ b/codex-rs/tui/src/app_server_approval_conversions.rs @@ -93,7 +93,20 @@ mod tests { read: Some(vec![absolute_path("/tmp/read-only")]), write: Some(vec![absolute_path("/tmp/write")]), glob_scan_max_depth: None, - entries: None, + entries: Some(vec![ + codex_app_server_protocol::FileSystemSandboxEntry { + path: codex_app_server_protocol::FileSystemPath::Path { + path: absolute_path("/tmp/read-only"), + }, + access: codex_app_server_protocol::FileSystemAccessMode::Read, + }, + codex_app_server_protocol::FileSystemSandboxEntry { + path: codex_app_server_protocol::FileSystemPath::Path { + path: absolute_path("/tmp/write"), + }, + access: codex_app_server_protocol::FileSystemAccessMode::Write, + }, + ]), }), } ); diff --git a/codex-rs/tui/src/app_server_session.rs b/codex-rs/tui/src/app_server_session.rs index b1f1e01c9a..8992d97d67 100644 --- a/codex-rs/tui/src/app_server_session.rs +++ b/codex-rs/tui/src/app_server_session.rs @@ -526,6 +526,7 @@ impl AppServerSession { approval_policy: AskForApproval, approvals_reviewer: codex_protocol::config_types::ApprovalsReviewer, sandbox_policy: SandboxPolicy, + permission_profile: Option, model: String, effort: Option, summary: Option, @@ -535,13 +536,11 @@ impl AppServerSession { output_schema: Option, ) -> Result { let request_id = self.next_request_id(); - let sandbox_policy = if self.is_remote() - || matches!(sandbox_policy, SandboxPolicy::ExternalSandbox { .. }) - { - Some(sandbox_policy.into()) - } else { - None - }; + let (sandbox_policy, permission_profile) = turn_start_permission_overrides( + self.thread_params_mode(), + sandbox_policy, + permission_profile, + ); self.client .request_typed(ClientRequest::TurnStart { request_id, @@ -553,11 +552,8 @@ impl AppServerSession { cwd: Some(cwd), approval_policy: Some(approval_policy.into()), approvals_reviewer: Some(approvals_reviewer.into()), - // Embedded sessions already installed their full profile - // at thread start/resume/fork. Avoid sending a lossy - // legacy projection until user turns carry profiles. sandbox_policy, - permission_profile: None, + permission_profile, model: Some(model), service_tier, effort, @@ -1046,6 +1042,26 @@ fn sandbox_mode_from_policy( } } +fn turn_start_permission_overrides( + mode: ThreadParamsMode, + sandbox_policy: SandboxPolicy, + permission_profile: Option, +) -> ( + Option, + Option, +) { + let is_external_sandbox = matches!(&sandbox_policy, SandboxPolicy::ExternalSandbox { .. }); + match (mode, is_external_sandbox, permission_profile) { + (ThreadParamsMode::Embedded, false, Some(permission_profile)) => { + (None, Some(permission_profile.into())) + } + (ThreadParamsMode::Embedded, false, None) => (None, None), + (ThreadParamsMode::Embedded, true, _) | (ThreadParamsMode::Remote, _, _) => { + (Some(sandbox_policy.into()), None) + } + } +} + fn permission_profile_override_from_config( config: &Config, thread_params_mode: ThreadParamsMode, @@ -1530,6 +1546,55 @@ mod tests { assert_eq!(fork.permission_profile, None); } + #[test] + fn turn_start_permission_overrides_send_profiles_only_for_embedded_runtime_overrides() { + let cwd = test_path_buf("/tmp/project"); + let workspace_write = SandboxPolicy::new_workspace_write_policy(); + let workspace_write_profile = + PermissionProfile::from_legacy_sandbox_policy(&workspace_write, &cwd); + + let (sandbox, profile) = turn_start_permission_overrides( + ThreadParamsMode::Embedded, + workspace_write.clone(), + Some(workspace_write_profile.clone()), + ); + assert_eq!(sandbox, None); + assert_eq!(profile, Some(workspace_write_profile.into())); + + let (sandbox, profile) = turn_start_permission_overrides( + ThreadParamsMode::Embedded, + workspace_write.clone(), + /*permission_profile*/ None, + ); + assert_eq!(sandbox, None); + assert_eq!(profile, None); + + let (sandbox, profile) = turn_start_permission_overrides( + ThreadParamsMode::Remote, + workspace_write.clone(), + Some(PermissionProfile::from_legacy_sandbox_policy( + &workspace_write, + &cwd, + )), + ); + assert_eq!(sandbox, Some(workspace_write.into())); + assert_eq!(profile, None); + + let external_sandbox = SandboxPolicy::ExternalSandbox { + network_access: codex_protocol::protocol::NetworkAccess::Restricted, + }; + let (sandbox, profile) = turn_start_permission_overrides( + ThreadParamsMode::Embedded, + external_sandbox.clone(), + Some(PermissionProfile::from_legacy_sandbox_policy( + &external_sandbox, + &cwd, + )), + ); + assert_eq!(sandbox, Some(external_sandbox.into())); + assert_eq!(profile, None); + } + #[tokio::test] async fn thread_fork_params_forward_instruction_overrides() { let temp_dir = tempfile::tempdir().expect("tempdir"); diff --git a/codex-rs/tui/src/chatwidget.rs b/codex-rs/tui/src/chatwidget.rs index 91a2e7555e..927305c4b7 100644 --- a/codex-rs/tui/src/chatwidget.rs +++ b/codex-rs/tui/src/chatwidget.rs @@ -259,7 +259,7 @@ const MULTI_AGENT_ENABLE_TITLE: &str = "Enable subagents?"; const MULTI_AGENT_ENABLE_YES: &str = "Yes, enable"; const MULTI_AGENT_ENABLE_NO: &str = "Not now"; const MULTI_AGENT_ENABLE_NOTICE: &str = "Subagents will be enabled in the next session."; -const TRUSTED_ACCESS_FOR_CYBER_VERIFICATION_WARNING: &str = "Your account was flagged for potentially high-risk cyber activity. Requests may be slower while additional verification is applied. To regain faster access, apply for trusted access: https://chatgpt.com/cyber or learn more: https://developers.openai.com/codex/concepts/cyber-safety"; +const TRUSTED_ACCESS_FOR_CYBER_VERIFICATION_WARNING: &str = "Your conversations have multiple flags for possible cybersecurity risk. Responses may take longer because extra safety checks are on. To get authorized for security work, join the Trusted Access for Cyber program: https://chatgpt.com/cyber"; const MEMORIES_DOC_URL: &str = "https://developers.openai.com/codex/memories"; const MEMORIES_ENABLE_TITLE: &str = "Enable memories?"; const MEMORIES_ENABLE_YES: &str = "Yes, enable"; @@ -2119,6 +2119,20 @@ impl ChatWidget { display: SessionConfiguredDisplay, fork_parent_title: Option, ) { + let (file_system_sandbox_policy, network_sandbox_policy) = match event + .permission_profile + .as_ref() + { + Some(permission_profile) => permission_profile.to_runtime_permissions(), + None => ( + codex_protocol::permissions::FileSystemSandboxPolicy::from_legacy_sandbox_policy( + &event.sandbox_policy, + &event.cwd, + ), + codex_protocol::permissions::NetworkSandboxPolicy::from(&event.sandbox_policy), + ), + }; + self.last_agent_markdown = None; self.agent_turn_markdowns.clear(); self.visible_user_turn_count = 0; @@ -2156,6 +2170,8 @@ impl ChatWidget { self.config.permissions.sandbox_policy = Constrained::allow_only(event.sandbox_policy.clone()); } + self.config.permissions.file_system_sandbox_policy = file_system_sandbox_policy; + self.config.permissions.network_sandbox_policy = network_sandbox_policy; self.config.approvals_reviewer = event.approvals_reviewer; self.status_line_project_root_name_cache = None; let forked_from_id = event.forked_from_id; @@ -9764,6 +9780,14 @@ impl ChatWidget { #[cfg_attr(not(target_os = "windows"), allow(dead_code))] pub(crate) fn set_sandbox_policy(&mut self, policy: SandboxPolicy) -> ConstraintResult<()> { self.config.permissions.sandbox_policy.set(policy)?; + let sandbox_policy = self.config.permissions.sandbox_policy.get(); + self.config.permissions.file_system_sandbox_policy = + codex_protocol::permissions::FileSystemSandboxPolicy::from_legacy_sandbox_policy( + sandbox_policy, + &self.config.cwd, + ); + self.config.permissions.network_sandbox_policy = + codex_protocol::permissions::NetworkSandboxPolicy::from(sandbox_policy); Ok(()) } diff --git a/codex-rs/tui/src/chatwidget/tests/app_server.rs b/codex-rs/tui/src/chatwidget/tests/app_server.rs index 3320b39e54..b9ec1d871b 100644 --- a/codex-rs/tui/src/chatwidget/tests/app_server.rs +++ b/codex-rs/tui/src/chatwidget/tests/app_server.rs @@ -738,8 +738,9 @@ async fn live_app_server_model_verification_renders_warning() { let cells = drain_insert_history(&mut rx); assert_eq!(cells.len(), 1); let rendered = lines_to_single_string(&cells[0]); - assert!(rendered.contains("flagged for potentially high-risk cyber activity")); - assert!(rendered.contains("slower while additional verification")); + assert!(rendered.contains("multiple flags for possible cybersecurity risk")); + assert!(rendered.contains("extra safety checks are on")); + assert!(rendered.contains("Trusted Access for Cyber")); assert!(rendered.contains("https://chatgpt.com/cyber")); } diff --git a/codex-rs/tui/src/chatwidget/tests/history_replay.rs b/codex-rs/tui/src/chatwidget/tests/history_replay.rs index 3e2f5b4d74..5f089ebbfe 100644 --- a/codex-rs/tui/src/chatwidget/tests/history_replay.rs +++ b/codex-rs/tui/src/chatwidget/tests/history_replay.rs @@ -1,4 +1,12 @@ use super::*; +use codex_protocol::protocol::FileSystemAccessMode; +use codex_protocol::protocol::FileSystemPath; +use codex_protocol::protocol::FileSystemSandboxEntry; +use codex_protocol::protocol::FileSystemSandboxKind; +use codex_protocol::protocol::FileSystemSandboxPolicy; +use codex_protocol::protocol::FileSystemSpecialPath; +use codex_protocol::protocol::NetworkAccess; +use codex_protocol::protocol::NetworkSandboxPolicy; use pretty_assertions::assert_eq; #[tokio::test] @@ -252,6 +260,25 @@ async fn session_configured_syncs_widget_config_permissions_and_cwd() { let expected_sandbox = SandboxPolicy::new_read_only_policy(); let expected_cwd = test_path_buf("/home/user/sub-agent").abs(); + let expected_file_system_policy = FileSystemSandboxPolicy::restricted(vec![ + FileSystemSandboxEntry { + path: FileSystemPath::Special { + value: FileSystemSpecialPath::Root, + }, + access: FileSystemAccessMode::Read, + }, + FileSystemSandboxEntry { + path: FileSystemPath::GlobPattern { + pattern: "**/.secret".to_string(), + }, + access: FileSystemAccessMode::None, + }, + ]); + let expected_permission_profile = + codex_protocol::models::PermissionProfile::from_runtime_permissions( + &expected_file_system_policy, + NetworkSandboxPolicy::Restricted, + ); let configured = codex_protocol::protocol::SessionConfiguredEvent { session_id: ThreadId::new(), forked_from_id: None, @@ -262,7 +289,7 @@ async fn session_configured_syncs_widget_config_permissions_and_cwd() { approval_policy: AskForApproval::Never, approvals_reviewer: ApprovalsReviewer::User, sandbox_policy: expected_sandbox.clone(), - permission_profile: None, + permission_profile: Some(expected_permission_profile.clone()), cwd: expected_cwd.clone(), reasoning_effort: Some(ReasoningEffortConfig::default()), history_log_id: 0, @@ -285,7 +312,72 @@ async fn session_configured_syncs_widget_config_permissions_and_cwd() { chat.config_ref().permissions.sandbox_policy.get(), &expected_sandbox ); + assert_eq!( + chat.config_ref().permissions.permission_profile(), + expected_permission_profile + ); assert_eq!(&chat.config_ref().cwd, &expected_cwd); + + let updated_sandbox = SandboxPolicy::new_workspace_write_policy(); + chat.set_sandbox_policy(updated_sandbox.clone()) + .expect("set sandbox policy"); + assert_eq!( + chat.config_ref().permissions.permission_profile(), + codex_protocol::models::PermissionProfile::from_legacy_sandbox_policy( + &updated_sandbox, + &expected_cwd + ), + "local sandbox changes should replace SessionConfigured profile-derived runtime permissions" + ); +} + +#[tokio::test] +async fn session_configured_external_sandbox_keeps_external_runtime_policy() { + let (mut chat, _rx, _ops) = make_chatwidget_manual(/*model_override*/ None).await; + + let expected_sandbox = SandboxPolicy::ExternalSandbox { + network_access: NetworkAccess::Restricted, + }; + let configured = codex_protocol::protocol::SessionConfiguredEvent { + session_id: ThreadId::new(), + forked_from_id: None, + thread_name: None, + model: "test-model".to_string(), + model_provider_id: "test-provider".to_string(), + service_tier: None, + approval_policy: AskForApproval::Never, + approvals_reviewer: ApprovalsReviewer::User, + sandbox_policy: expected_sandbox.clone(), + permission_profile: None, + cwd: test_path_buf("/home/user/external").abs(), + reasoning_effort: Some(ReasoningEffortConfig::default()), + history_log_id: 0, + history_entry_count: 0, + initial_messages: None, + network_proxy: None, + rollout_path: None, + }; + + chat.handle_codex_event(Event { + id: "session-configured".into(), + msg: EventMsg::SessionConfigured(configured), + }); + + assert_eq!( + chat.config_ref().permissions.sandbox_policy.get(), + &expected_sandbox + ); + assert_eq!( + chat.config_ref() + .permissions + .file_system_sandbox_policy + .kind, + FileSystemSandboxKind::ExternalSandbox, + ); + assert_eq!( + chat.config_ref().permissions.network_sandbox_policy, + NetworkSandboxPolicy::Restricted, + ); } #[tokio::test] diff --git a/codex-rs/tui/src/chatwidget/tests/status_and_layout.rs b/codex-rs/tui/src/chatwidget/tests/status_and_layout.rs index a01f723180..46756a962f 100644 --- a/codex-rs/tui/src/chatwidget/tests/status_and_layout.rs +++ b/codex-rs/tui/src/chatwidget/tests/status_and_layout.rs @@ -62,8 +62,9 @@ async fn core_model_verification_renders_warning() { let cells = drain_insert_history(&mut rx); assert_eq!(cells.len(), 1); let rendered = lines_to_single_string(&cells[0]); - assert!(rendered.contains("flagged for potentially high-risk cyber activity")); - assert!(rendered.contains("slower while additional verification")); + assert!(rendered.contains("multiple flags for possible cybersecurity risk")); + assert!(rendered.contains("extra safety checks are on")); + assert!(rendered.contains("Trusted Access for Cyber")); assert!(rendered.contains("https://chatgpt.com/cyber")); } diff --git a/codex-rs/tui/src/debug_config.rs b/codex-rs/tui/src/debug_config.rs index 1502db16f7..1c48c44918 100644 --- a/codex-rs/tui/src/debug_config.rs +++ b/codex-rs/tui/src/debug_config.rs @@ -724,7 +724,7 @@ mod tests { rendered.contains("allowed_approval_policies: on-request (source: cloud requirements)") ); assert!(rendered.contains( - "allowed_approvals_reviewers: auto_review (source: MDM managed_config.toml (legacy))" + "allowed_approvals_reviewers: guardian_subagent (source: MDM managed_config.toml (legacy))" )); assert!( rendered.contains( @@ -779,7 +779,7 @@ mod tests { let rendered = render_to_text(&render_debug_config_lines(&stack)); assert!(rendered.contains( - "allowed_approvals_reviewers: auto_review (source: MDM managed_config.toml (legacy))" + "allowed_approvals_reviewers: guardian_subagent (source: MDM managed_config.toml (legacy))" )); assert!(!rendered.contains("Requirements:\n ")); } diff --git a/sdk/python/src/codex_app_server/generated/v2_all.py b/sdk/python/src/codex_app_server/generated/v2_all.py index c75b3e89af..fac98223d7 100644 --- a/sdk/python/src/codex_app_server/generated/v2_all.py +++ b/sdk/python/src/codex_app_server/generated/v2_all.py @@ -146,6 +146,7 @@ class AppToolsConfig(BaseModel): class ApprovalsReviewer(Enum): user = "user" + auto_review = "auto_review" guardian_subagent = "guardian_subagent" diff --git a/sdk/python/tests/test_client_rpc_methods.py b/sdk/python/tests/test_client_rpc_methods.py index 2742180566..f1dd353b1f 100644 --- a/sdk/python/tests/test_client_rpc_methods.py +++ b/sdk/python/tests/test_client_rpc_methods.py @@ -4,7 +4,12 @@ from pathlib import Path from typing import Any from codex_app_server.client import AppServerClient, _params_dict -from codex_app_server.generated.v2_all import ThreadListParams, ThreadTokenUsageUpdatedNotification +from codex_app_server.generated.v2_all import ( + ApprovalsReviewer, + ThreadListParams, + ThreadResumeResponse, + ThreadTokenUsageUpdatedNotification, +) from codex_app_server.models import UnknownNotification ROOT = Path(__file__).resolve().parents[1] @@ -40,6 +45,34 @@ def test_generated_v2_bundle_has_single_shared_plan_type_definition() -> None: assert source.count("class PlanType(") == 1 +def test_thread_resume_response_accepts_auto_review_reviewer() -> None: + response = ThreadResumeResponse.model_validate( + { + "approvalPolicy": "on-request", + "approvalsReviewer": "auto_review", + "cwd": "/tmp", + "model": "gpt-5", + "modelProvider": "openai", + "sandbox": {"type": "dangerFullAccess"}, + "thread": { + "cliVersion": "1.0.0", + "createdAt": 1, + "cwd": "/tmp", + "ephemeral": False, + "id": "thread-1", + "modelProvider": "openai", + "preview": "", + "source": "cli", + "status": {"type": "idle"}, + "turns": [], + "updatedAt": 1, + }, + } + ) + + assert response.approvals_reviewer is ApprovalsReviewer.auto_review + + def test_notifications_are_typed_with_canonical_v2_methods() -> None: client = AppServerClient() event = client._coerce_notification( @@ -89,7 +122,9 @@ def test_unknown_notifications_fall_back_to_unknown_payloads() -> None: def test_invalid_notification_payload_falls_back_to_unknown() -> None: client = AppServerClient() - event = client._coerce_notification("thread/tokenUsage/updated", {"threadId": "missing"}) + event = client._coerce_notification( + "thread/tokenUsage/updated", {"threadId": "missing"} + ) assert event.method == "thread/tokenUsage/updated" assert isinstance(event.payload, UnknownNotification)