From 4ad972321e2d8a94e77194685f5a0309cb130b43 Mon Sep 17 00:00:00 2001 From: Felipe Coury Date: Sat, 30 May 2026 19:26:28 -0300 Subject: [PATCH] fix(core): harden runtime workspace updates --- .../PermissionsRequestApprovalParams.json | 39 ++++++++++ .../schema/json/ServerNotification.json | 39 ++++++++++ .../schema/json/ServerRequest.json | 39 ++++++++++ .../codex_app_server_protocol.schemas.json | 49 ++++++++++++ .../codex_app_server_protocol.v2.schemas.json | 39 ++++++++++ ...anApprovalReviewCompletedNotification.json | 39 ++++++++++ ...dianApprovalReviewStartedNotification.json | 39 ++++++++++ .../v2/GuardianApprovalReviewAction.ts | 3 +- .../v2/PermissionsRequestApprovalParams.ts | 3 +- .../v2/WorkspaceMutationApprovalRequest.ts | 7 ++ .../v2/WorkspaceMutationOperation.ts | 5 ++ .../schema/typescript/v2/index.ts | 2 + codex-rs/core/src/session/mod.rs | 9 +++ .../src/session/rollout_reconstruction.rs | 2 +- codex-rs/core/src/session/tests.rs | 74 +++++++++++++++++++ codex-rs/core/src/state/turn.rs | 1 + codex-rs/core/src/thread_manager_tests.rs | 24 ++++++ .../handlers/unified_exec/exec_command.rs | 7 +- codex-rs/core/src/tools/orchestrator.rs | 13 ++-- codex-rs/core/src/unified_exec/mod.rs | 2 + .../core/src/unified_exec/process_manager.rs | 2 +- .../src/unified_exec/process_manager_tests.rs | 1 + 22 files changed, 428 insertions(+), 10 deletions(-) create mode 100644 codex-rs/app-server-protocol/schema/typescript/v2/WorkspaceMutationApprovalRequest.ts create mode 100644 codex-rs/app-server-protocol/schema/typescript/v2/WorkspaceMutationOperation.ts diff --git a/codex-rs/app-server-protocol/schema/json/PermissionsRequestApprovalParams.json b/codex-rs/app-server-protocol/schema/json/PermissionsRequestApprovalParams.json index f2ab783342..cd45670aaf 100644 --- a/codex-rs/app-server-protocol/schema/json/PermissionsRequestApprovalParams.json +++ b/codex-rs/app-server-protocol/schema/json/PermissionsRequestApprovalParams.json @@ -279,6 +279,35 @@ } }, "type": "object" + }, + "WorkspaceMutationApprovalRequest": { + "properties": { + "operation": { + "$ref": "#/definitions/WorkspaceMutationOperation" + }, + "resultingWorkspaceRoots": { + "items": { + "$ref": "#/definitions/AbsolutePathBuf" + }, + "type": "array" + }, + "target": { + "$ref": "#/definitions/AbsolutePathBuf" + } + }, + "required": [ + "operation", + "resultingWorkspaceRoots", + "target" + ], + "type": "object" + }, + "WorkspaceMutationOperation": { + "enum": [ + "setWorkingDirectory", + "addWorkspaceRoot" + ], + "type": "string" } }, "properties": { @@ -314,6 +343,16 @@ }, "turnId": { "type": "string" + }, + "workspaceMutation": { + "anyOf": [ + { + "$ref": "#/definitions/WorkspaceMutationApprovalRequest" + }, + { + "type": "null" + } + ] } }, "required": [ diff --git a/codex-rs/app-server-protocol/schema/json/ServerNotification.json b/codex-rs/app-server-protocol/schema/json/ServerNotification.json index 1c37ea6f33..a7050c1813 100644 --- a/codex-rs/app-server-protocol/schema/json/ServerNotification.json +++ b/codex-rs/app-server-protocol/schema/json/ServerNotification.json @@ -1754,6 +1754,16 @@ ], "title": "RequestPermissionsGuardianApprovalReviewActionType", "type": "string" + }, + "workspaceMutation": { + "anyOf": [ + { + "$ref": "#/definitions/WorkspaceMutationApprovalRequest" + }, + { + "type": "null" + } + ] } }, "required": [ @@ -5380,6 +5390,35 @@ "samplePaths" ], "type": "object" + }, + "WorkspaceMutationApprovalRequest": { + "properties": { + "operation": { + "$ref": "#/definitions/WorkspaceMutationOperation" + }, + "resultingWorkspaceRoots": { + "items": { + "$ref": "#/definitions/AbsolutePathBuf" + }, + "type": "array" + }, + "target": { + "$ref": "#/definitions/AbsolutePathBuf" + } + }, + "required": [ + "operation", + "resultingWorkspaceRoots", + "target" + ], + "type": "object" + }, + "WorkspaceMutationOperation": { + "enum": [ + "setWorkingDirectory", + "addWorkspaceRoot" + ], + "type": "string" } }, "description": "Notification sent from the server to the client.", diff --git a/codex-rs/app-server-protocol/schema/json/ServerRequest.json b/codex-rs/app-server-protocol/schema/json/ServerRequest.json index 5ab8d7c63b..73046ac492 100644 --- a/codex-rs/app-server-protocol/schema/json/ServerRequest.json +++ b/codex-rs/app-server-protocol/schema/json/ServerRequest.json @@ -1619,6 +1619,16 @@ }, "turnId": { "type": "string" + }, + "workspaceMutation": { + "anyOf": [ + { + "$ref": "#/definitions/WorkspaceMutationApprovalRequest" + }, + { + "type": "null" + } + ] } }, "required": [ @@ -1759,6 +1769,35 @@ "question" ], "type": "object" + }, + "WorkspaceMutationApprovalRequest": { + "properties": { + "operation": { + "$ref": "#/definitions/WorkspaceMutationOperation" + }, + "resultingWorkspaceRoots": { + "items": { + "$ref": "#/definitions/AbsolutePathBuf" + }, + "type": "array" + }, + "target": { + "$ref": "#/definitions/AbsolutePathBuf" + } + }, + "required": [ + "operation", + "resultingWorkspaceRoots", + "target" + ], + "type": "object" + }, + "WorkspaceMutationOperation": { + "enum": [ + "setWorkingDirectory", + "addWorkspaceRoot" + ], + "type": "string" } }, "description": "Request initiated from the server and sent to the client.", 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 9aedb8a7ea..880df47088 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 @@ -3859,6 +3859,16 @@ }, "turnId": { "type": "string" + }, + "workspaceMutation": { + "anyOf": [ + { + "$ref": "#/definitions/v2/WorkspaceMutationApprovalRequest" + }, + { + "type": "null" + } + ] } }, "required": [ @@ -10084,6 +10094,16 @@ ], "title": "RequestPermissionsGuardianApprovalReviewActionType", "type": "string" + }, + "workspaceMutation": { + "anyOf": [ + { + "$ref": "#/definitions/v2/WorkspaceMutationApprovalRequest" + }, + { + "type": "null" + } + ] } }, "required": [ @@ -19707,6 +19727,35 @@ "title": "WindowsWorldWritableWarningNotification", "type": "object" }, + "WorkspaceMutationApprovalRequest": { + "properties": { + "operation": { + "$ref": "#/definitions/v2/WorkspaceMutationOperation" + }, + "resultingWorkspaceRoots": { + "items": { + "$ref": "#/definitions/v2/AbsolutePathBuf" + }, + "type": "array" + }, + "target": { + "$ref": "#/definitions/v2/AbsolutePathBuf" + } + }, + "required": [ + "operation", + "resultingWorkspaceRoots", + "target" + ], + "type": "object" + }, + "WorkspaceMutationOperation": { + "enum": [ + "setWorkingDirectory", + "addWorkspaceRoot" + ], + "type": "string" + }, "WriteStatus": { "enum": [ "ok", 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 ebe1efe26d..f6e90f23ae 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 @@ -6508,6 +6508,16 @@ ], "title": "RequestPermissionsGuardianApprovalReviewActionType", "type": "string" + }, + "workspaceMutation": { + "anyOf": [ + { + "$ref": "#/definitions/WorkspaceMutationApprovalRequest" + }, + { + "type": "null" + } + ] } }, "required": [ @@ -17515,6 +17525,35 @@ "title": "WindowsWorldWritableWarningNotification", "type": "object" }, + "WorkspaceMutationApprovalRequest": { + "properties": { + "operation": { + "$ref": "#/definitions/WorkspaceMutationOperation" + }, + "resultingWorkspaceRoots": { + "items": { + "$ref": "#/definitions/AbsolutePathBuf" + }, + "type": "array" + }, + "target": { + "$ref": "#/definitions/AbsolutePathBuf" + } + }, + "required": [ + "operation", + "resultingWorkspaceRoots", + "target" + ], + "type": "object" + }, + "WorkspaceMutationOperation": { + "enum": [ + "setWorkingDirectory", + "addWorkspaceRoot" + ], + "type": "string" + }, "WriteStatus": { "enum": [ "ok", 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 a366c99a41..6a211d2543 100644 --- a/codex-rs/app-server-protocol/schema/json/v2/ItemGuardianApprovalReviewCompletedNotification.json +++ b/codex-rs/app-server-protocol/schema/json/v2/ItemGuardianApprovalReviewCompletedNotification.json @@ -484,6 +484,16 @@ ], "title": "RequestPermissionsGuardianApprovalReviewActionType", "type": "string" + }, + "workspaceMutation": { + "anyOf": [ + { + "$ref": "#/definitions/WorkspaceMutationApprovalRequest" + }, + { + "type": "null" + } + ] } }, "required": [ @@ -567,6 +577,35 @@ } }, "type": "object" + }, + "WorkspaceMutationApprovalRequest": { + "properties": { + "operation": { + "$ref": "#/definitions/WorkspaceMutationOperation" + }, + "resultingWorkspaceRoots": { + "items": { + "$ref": "#/definitions/AbsolutePathBuf" + }, + "type": "array" + }, + "target": { + "$ref": "#/definitions/AbsolutePathBuf" + } + }, + "required": [ + "operation", + "resultingWorkspaceRoots", + "target" + ], + "type": "object" + }, + "WorkspaceMutationOperation": { + "enum": [ + "setWorkingDirectory", + "addWorkspaceRoot" + ], + "type": "string" } }, "description": "[UNSTABLE] Temporary notification payload for approval auto-review. This shape is expected to change soon.", 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 bc081c7be9..95946a24ff 100644 --- a/codex-rs/app-server-protocol/schema/json/v2/ItemGuardianApprovalReviewStartedNotification.json +++ b/codex-rs/app-server-protocol/schema/json/v2/ItemGuardianApprovalReviewStartedNotification.json @@ -477,6 +477,16 @@ ], "title": "RequestPermissionsGuardianApprovalReviewActionType", "type": "string" + }, + "workspaceMutation": { + "anyOf": [ + { + "$ref": "#/definitions/WorkspaceMutationApprovalRequest" + }, + { + "type": "null" + } + ] } }, "required": [ @@ -560,6 +570,35 @@ } }, "type": "object" + }, + "WorkspaceMutationApprovalRequest": { + "properties": { + "operation": { + "$ref": "#/definitions/WorkspaceMutationOperation" + }, + "resultingWorkspaceRoots": { + "items": { + "$ref": "#/definitions/AbsolutePathBuf" + }, + "type": "array" + }, + "target": { + "$ref": "#/definitions/AbsolutePathBuf" + } + }, + "required": [ + "operation", + "resultingWorkspaceRoots", + "target" + ], + "type": "object" + }, + "WorkspaceMutationOperation": { + "enum": [ + "setWorkingDirectory", + "addWorkspaceRoot" + ], + "type": "string" } }, "description": "[UNSTABLE] Temporary notification payload for approval auto-review. This shape is expected to change soon.", diff --git a/codex-rs/app-server-protocol/schema/typescript/v2/GuardianApprovalReviewAction.ts b/codex-rs/app-server-protocol/schema/typescript/v2/GuardianApprovalReviewAction.ts index 4f00e37d20..6579adc2d2 100644 --- a/codex-rs/app-server-protocol/schema/typescript/v2/GuardianApprovalReviewAction.ts +++ b/codex-rs/app-server-protocol/schema/typescript/v2/GuardianApprovalReviewAction.ts @@ -5,5 +5,6 @@ import type { AbsolutePathBuf } from "../AbsolutePathBuf"; import type { GuardianCommandSource } from "./GuardianCommandSource"; import type { NetworkApprovalProtocol } from "./NetworkApprovalProtocol"; import type { RequestPermissionProfile } from "./RequestPermissionProfile"; +import type { WorkspaceMutationApprovalRequest } from "./WorkspaceMutationApprovalRequest"; -export type GuardianApprovalReviewAction = { "type": "command", source: GuardianCommandSource, command: string, cwd: AbsolutePathBuf, } | { "type": "execve", source: GuardianCommandSource, program: string, argv: Array, cwd: AbsolutePathBuf, } | { "type": "applyPatch", cwd: AbsolutePathBuf, files: Array, } | { "type": "networkAccess", target: string, host: string, protocol: NetworkApprovalProtocol, port: number, } | { "type": "mcpToolCall", server: string, toolName: string, connectorId: string | null, connectorName: string | null, toolTitle: string | null, } | { "type": "requestPermissions", reason: string | null, permissions: RequestPermissionProfile, }; +export type GuardianApprovalReviewAction = { "type": "command", source: GuardianCommandSource, command: string, cwd: AbsolutePathBuf, } | { "type": "execve", source: GuardianCommandSource, program: string, argv: Array, cwd: AbsolutePathBuf, } | { "type": "applyPatch", cwd: AbsolutePathBuf, files: Array, } | { "type": "networkAccess", target: string, host: string, protocol: NetworkApprovalProtocol, port: number, } | { "type": "mcpToolCall", server: string, toolName: string, connectorId: string | null, connectorName: string | null, toolTitle: string | null, } | { "type": "requestPermissions", reason: string | null, permissions: RequestPermissionProfile, workspaceMutation: WorkspaceMutationApprovalRequest | null, }; diff --git a/codex-rs/app-server-protocol/schema/typescript/v2/PermissionsRequestApprovalParams.ts b/codex-rs/app-server-protocol/schema/typescript/v2/PermissionsRequestApprovalParams.ts index d0677f5f0c..6c1129ee1b 100644 --- a/codex-rs/app-server-protocol/schema/typescript/v2/PermissionsRequestApprovalParams.ts +++ b/codex-rs/app-server-protocol/schema/typescript/v2/PermissionsRequestApprovalParams.ts @@ -3,9 +3,10 @@ // This file was generated by [ts-rs](https://github.com/Aleph-Alpha/ts-rs). Do not edit this file manually. import type { AbsolutePathBuf } from "../AbsolutePathBuf"; import type { RequestPermissionProfile } from "./RequestPermissionProfile"; +import type { WorkspaceMutationApprovalRequest } from "./WorkspaceMutationApprovalRequest"; export type PermissionsRequestApprovalParams = { threadId: string, turnId: string, itemId: string, environmentId: string | null, /** * Unix timestamp (in milliseconds) when this approval request started. */ -startedAtMs: number, cwd: AbsolutePathBuf, reason: string | null, permissions: RequestPermissionProfile, }; +startedAtMs: number, cwd: AbsolutePathBuf, reason: string | null, permissions: RequestPermissionProfile, workspaceMutation: WorkspaceMutationApprovalRequest | null, }; diff --git a/codex-rs/app-server-protocol/schema/typescript/v2/WorkspaceMutationApprovalRequest.ts b/codex-rs/app-server-protocol/schema/typescript/v2/WorkspaceMutationApprovalRequest.ts new file mode 100644 index 0000000000..0f721c129b --- /dev/null +++ b/codex-rs/app-server-protocol/schema/typescript/v2/WorkspaceMutationApprovalRequest.ts @@ -0,0 +1,7 @@ +// GENERATED CODE! DO NOT MODIFY BY HAND! + +// This file was generated by [ts-rs](https://github.com/Aleph-Alpha/ts-rs). Do not edit this file manually. +import type { AbsolutePathBuf } from "../AbsolutePathBuf"; +import type { WorkspaceMutationOperation } from "./WorkspaceMutationOperation"; + +export type WorkspaceMutationApprovalRequest = { operation: WorkspaceMutationOperation, target: AbsolutePathBuf, resultingWorkspaceRoots: Array, }; diff --git a/codex-rs/app-server-protocol/schema/typescript/v2/WorkspaceMutationOperation.ts b/codex-rs/app-server-protocol/schema/typescript/v2/WorkspaceMutationOperation.ts new file mode 100644 index 0000000000..267010e606 --- /dev/null +++ b/codex-rs/app-server-protocol/schema/typescript/v2/WorkspaceMutationOperation.ts @@ -0,0 +1,5 @@ +// GENERATED CODE! DO NOT MODIFY BY HAND! + +// This file was generated by [ts-rs](https://github.com/Aleph-Alpha/ts-rs). Do not edit this file manually. + +export type WorkspaceMutationOperation = "setWorkingDirectory" | "addWorkspaceRoot"; diff --git a/codex-rs/app-server-protocol/schema/typescript/v2/index.ts b/codex-rs/app-server-protocol/schema/typescript/v2/index.ts index c46e95f95a..0c85fafee1 100644 --- a/codex-rs/app-server-protocol/schema/typescript/v2/index.ts +++ b/codex-rs/app-server-protocol/schema/typescript/v2/index.ts @@ -479,4 +479,6 @@ export type { WindowsSandboxSetupMode } from "./WindowsSandboxSetupMode"; export type { WindowsSandboxSetupStartParams } from "./WindowsSandboxSetupStartParams"; export type { WindowsSandboxSetupStartResponse } from "./WindowsSandboxSetupStartResponse"; export type { WindowsWorldWritableWarningNotification } from "./WindowsWorldWritableWarningNotification"; +export type { WorkspaceMutationApprovalRequest } from "./WorkspaceMutationApprovalRequest"; +export type { WorkspaceMutationOperation } from "./WorkspaceMutationOperation"; export type { WriteStatus } from "./WriteStatus"; diff --git a/codex-rs/core/src/session/mod.rs b/codex-rs/core/src/session/mod.rs index 9d41308c68..5c6ca794a7 100644 --- a/codex-rs/core/src/session/mod.rs +++ b/codex-rs/core/src/session/mod.rs @@ -2403,6 +2403,7 @@ impl Session { tx_response, requested_permissions: requested_permissions.clone(), environment: environment.clone(), + workspace_mutation: workspace_mutation.is_some(), }, ) } @@ -2565,6 +2566,14 @@ impl Session { }; match entry { Some(entry) => { + let response = if entry.workspace_mutation { + RequestPermissionsResponse { + scope: PermissionGrantScope::Session, + ..response + } + } else { + response + }; let response = Self::normalize_request_permissions_response( entry.requested_permissions, response, diff --git a/codex-rs/core/src/session/rollout_reconstruction.rs b/codex-rs/core/src/session/rollout_reconstruction.rs index ca2019120e..909230c0f8 100644 --- a/codex-rs/core/src/session/rollout_reconstruction.rs +++ b/codex-rs/core/src/session/rollout_reconstruction.rs @@ -293,7 +293,7 @@ pub(crate) fn runtime_workspace_baseline_from_initial_history( reconstruct_rollout_metadata(rollout_items).reference_context_item?; Some(RuntimeWorkspaceBaseline { cwd: AbsolutePathBuf::try_from(reference_context_item.cwd).ok()?, - workspace_roots: reference_context_item.workspace_roots?, + workspace_roots: reference_context_item.workspace_roots.unwrap_or_default(), }) } diff --git a/codex-rs/core/src/session/tests.rs b/codex-rs/core/src/session/tests.rs index fbd5698d11..c21cb22569 100644 --- a/codex-rs/core/src/session/tests.rs +++ b/codex-rs/core/src/session/tests.rs @@ -5881,6 +5881,80 @@ async fn request_permissions_response_materializes_session_cwd_grants_before_rec ); } +#[tokio::test] +async fn workspace_mutation_permission_response_forces_session_scope() { + let (session, turn_context, rx) = make_session_and_context_with_rx().await; + *session.active_turn.lock().await = Some(ActiveTurn::default()); + let session = Arc::new(session); + let turn_context = Arc::new(turn_context); + #[allow(deprecated)] + let cwd = turn_context.cwd.clone(); + let requested_permissions = RequestPermissionProfile { + file_system: Some(FileSystemPermissions::from_read_write_roots( + /*read*/ None, + Some(vec![cwd.clone()]), + )), + ..Default::default() + }; + + let handle = tokio::spawn({ + let session = Arc::clone(&session); + let turn_context = Arc::clone(&turn_context); + let cwd = cwd.clone(); + let requested_permissions = requested_permissions.clone(); + async move { + session + .request_workspace_permissions_for_cwd( + &turn_context, + "workspace-mutation".to_string(), + codex_protocol::request_permissions::RequestPermissionsArgs { + reason: Some("persist workspace mutation".to_string()), + permissions: requested_permissions, + }, + cwd.clone(), + codex_protocol::request_permissions::WorkspaceMutationApprovalRequest { + operation: + codex_protocol::request_permissions::WorkspaceMutationOperation::SetWorkingDirectory, + target: cwd.clone(), + resulting_workspace_roots: vec![cwd], + }, + CancellationToken::new(), + ) + .await + } + }); + + let request_event = tokio::time::timeout(StdDuration::from_secs(1), rx.recv()) + .await + .expect("request_permissions event timed out") + .expect("request_permissions event missing"); + let EventMsg::RequestPermissions(request) = request_event.msg else { + panic!("expected request_permissions event"); + }; + session + .notify_request_permissions_response( + &request.call_id, + codex_protocol::request_permissions::RequestPermissionsResponse { + permissions: request.permissions, + scope: PermissionGrantScope::Turn, + strict_auto_review: false, + }, + ) + .await; + + let response = tokio::time::timeout(StdDuration::from_secs(1), handle) + .await + .expect("workspace mutation request timed out") + .expect("workspace mutation request join error") + .expect("workspace mutation response"); + assert_eq!(response.scope, PermissionGrantScope::Session); + assert_eq!( + session.granted_session_permissions().await, + Some(requested_permissions.into()) + ); + assert_eq!(session.granted_turn_permissions().await, None); +} + #[tokio::test] async fn request_permissions_is_auto_denied_when_granular_policy_blocks_tool_requests() { let (session, mut turn_context, rx) = make_session_and_context_with_rx().await; diff --git a/codex-rs/core/src/state/turn.rs b/codex-rs/core/src/state/turn.rs index d1e31b7994..5c4d7c6a54 100644 --- a/codex-rs/core/src/state/turn.rs +++ b/codex-rs/core/src/state/turn.rs @@ -103,6 +103,7 @@ pub(crate) struct PendingRequestPermissions { pub(crate) tx_response: oneshot::Sender, pub(crate) requested_permissions: RequestPermissionProfile, pub(crate) environment: TurnEnvironmentSelection, + pub(crate) workspace_mutation: bool, } impl TurnState { diff --git a/codex-rs/core/src/thread_manager_tests.rs b/codex-rs/core/src/thread_manager_tests.rs index 862882c3e5..4d8b4bfef8 100644 --- a/codex-rs/core/src/thread_manager_tests.rs +++ b/codex-rs/core/src/thread_manager_tests.rs @@ -205,6 +205,30 @@ async fn hydrate_runtime_workspace_from_history_restores_persisted_baseline() { assert_eq!(config.permissions.workspace_roots(), persisted_roots); } +#[tokio::test] +async fn hydrate_runtime_workspace_from_history_restores_cwd_with_empty_roots() { + let (_session, turn_context) = make_session_and_context().await; + let persisted_cwd = PathBuf::from("/workspace/updated").abs(); + let mut context_item = turn_context.to_turn_context_item(); + context_item.cwd = persisted_cwd.to_path_buf(); + context_item.workspace_roots = None; + let history = InitialHistory::Forked(vec![ + RolloutItem::ResponseItem(user_msg("mutate workspace")), + RolloutItem::TurnContext(context_item), + ]); + let mut config = test_config().await; + + hydrate_runtime_workspace_from_history( + &mut config, + &history, + RuntimeWorkspaceReplayOverrides::default(), + ); + + assert_eq!(config.cwd, persisted_cwd); + assert_eq!(config.workspace_roots, Vec::new()); + assert_eq!(config.permissions.workspace_roots(), Vec::new()); +} + #[tokio::test] async fn hydrate_runtime_workspace_from_history_preserves_explicit_replacements() { let (_session, turn_context) = make_session_and_context().await; diff --git a/codex-rs/core/src/tools/handlers/unified_exec/exec_command.rs b/codex-rs/core/src/tools/handlers/unified_exec/exec_command.rs index b80f39443f..d20e760bc0 100644 --- a/codex-rs/core/src/tools/handlers/unified_exec/exec_command.rs +++ b/codex-rs/core/src/tools/handlers/unified_exec/exec_command.rs @@ -283,7 +283,12 @@ impl ExecCommandHandler { yield_time_ms, max_output_tokens, cwd, - sandbox_cwd: turn_environment.cwd.clone(), + sandbox_cwd: if turn.environments.turn_environments.len() == 1 { + runtime_workspace.cwd + } else { + turn_environment.cwd.clone() + }, + permission_profile: runtime_workspace.permission_profile, environment, shell_mode, network: context.turn.network.clone(), diff --git a/codex-rs/core/src/tools/orchestrator.rs b/codex-rs/core/src/tools/orchestrator.rs index 77cbffde32..9c721564c5 100644 --- a/codex-rs/core/src/tools/orchestrator.rs +++ b/codex-rs/core/src/tools/orchestrator.rs @@ -148,7 +148,10 @@ impl ToolOrchestrator { // 1) Approval let mut already_approved = false; - let file_system_sandbox_policy = turn_ctx.file_system_sandbox_policy(); + let runtime_workspace = turn_ctx.runtime_workspace.snapshot().await; + let file_system_sandbox_policy = runtime_workspace + .permission_profile + .file_system_sandbox_policy(); let network_sandbox_policy = turn_ctx.network_sandbox_policy(); let requirement = tool.exec_approval_requirement(req).unwrap_or_else(|| { default_exec_approval_requirement(approval_policy, &file_system_sandbox_policy) @@ -238,11 +241,11 @@ impl ToolOrchestrator { // Platform-specific flag gating is handled by SandboxManager::select_initial. let use_legacy_landlock = turn_ctx.features.use_legacy_landlock(); #[allow(deprecated)] - let sandbox_cwd = tool.sandbox_cwd(req).unwrap_or(&turn_ctx.cwd); - let workspace_roots = turn_ctx.config.effective_workspace_roots(); + let sandbox_cwd = tool.sandbox_cwd(req).unwrap_or(&runtime_workspace.cwd); + let workspace_roots = runtime_workspace.workspace_roots; let initial_attempt = SandboxAttempt { sandbox: initial_sandbox, - permissions: &turn_ctx.permission_profile, + permissions: &runtime_workspace.permission_profile, enforce_managed_network: managed_network_active, manager: &self.sandbox, sandbox_cwd, @@ -415,7 +418,7 @@ impl ToolOrchestrator { }; let retry_attempt = SandboxAttempt { sandbox: retry_sandbox, - permissions: &turn_ctx.permission_profile, + permissions: &runtime_workspace.permission_profile, enforce_managed_network: managed_network_active, manager: &self.sandbox, sandbox_cwd, diff --git a/codex-rs/core/src/unified_exec/mod.rs b/codex-rs/core/src/unified_exec/mod.rs index 65b18b064d..a1fc8929b0 100644 --- a/codex-rs/core/src/unified_exec/mod.rs +++ b/codex-rs/core/src/unified_exec/mod.rs @@ -30,6 +30,7 @@ use std::sync::Weak; use codex_exec_server::Environment; use codex_network_proxy::NetworkProxy; use codex_protocol::models::AdditionalPermissionProfile; +use codex_protocol::models::PermissionProfile; use codex_tools::UnifiedExecShellMode; use codex_utils_absolute_path::AbsolutePathBuf; use codex_utils_output_truncation::TruncationPolicy; @@ -97,6 +98,7 @@ pub(crate) struct ExecCommandRequest { pub max_output_tokens: Option, pub cwd: AbsolutePathBuf, pub sandbox_cwd: AbsolutePathBuf, + pub permission_profile: PermissionProfile, pub environment: Arc, pub shell_mode: UnifiedExecShellMode, pub network: Option, diff --git a/codex-rs/core/src/unified_exec/process_manager.rs b/codex-rs/core/src/unified_exec/process_manager.rs index d744a72c95..f2a6f17b9b 100644 --- a/codex-rs/core/src/unified_exec/process_manager.rs +++ b/codex-rs/core/src/unified_exec/process_manager.rs @@ -1050,7 +1050,7 @@ impl UnifiedExecProcessManager { .create_exec_approval_requirement_for_command(ExecApprovalRequest { command: &request.command, approval_policy: context.turn.approval_policy.value(), - permission_profile: context.turn.permission_profile(), + permission_profile: request.permission_profile.clone(), windows_sandbox_level: context.turn.windows_sandbox_level, sandbox_permissions: if request.additional_permissions_preapproved { crate::sandboxing::SandboxPermissions::UseDefault diff --git a/codex-rs/core/src/unified_exec/process_manager_tests.rs b/codex-rs/core/src/unified_exec/process_manager_tests.rs index 9aa0e73814..e871625465 100644 --- a/codex-rs/core/src/unified_exec/process_manager_tests.rs +++ b/codex-rs/core/src/unified_exec/process_manager_tests.rs @@ -175,6 +175,7 @@ async fn failed_initial_end_for_unstored_process_uses_fallback_output() { cwd: turn.cwd.clone(), #[allow(deprecated)] sandbox_cwd: turn.cwd.clone(), + permission_profile: turn.permission_profile(), environment: turn .environments .primary_environment()