mirror of
https://github.com/openai/codex.git
synced 2026-11-18 22:20:48 +00:00
Run environment hooks on their owning executors (#56343)
## What changed - Include ready environments' configured hooks in lifecycle events and execute command hooks using each owner's shell, working directory, shell environment policy, and managed network policy. - Resolve tool environments before `PreToolUse` and use those targets for tool hooks, execution, and permission review. Route MCP hooks by server ownership and file-copy hooks to both endpoints. - Preserve environment, MCP server, and terminal session selectors across hook input rewrites, rejecting attempts to switch targets. ## Testing Add regression coverage for permission-request selector preservation, MCP resource hook ownership, and file-copy hooks that rewrite paths while preserving source and target environments. GitOrigin-RevId: 31a3a6830523965285dab451cfc866c620ef6c83
This commit is contained in:
@@ -62,15 +62,20 @@ impl StepContext {
|
||||
pub(crate) fn environments(&self) -> Vec<(&TurnEnvironment, impl EnvironmentAccess + '_)> {
|
||||
self.environments
|
||||
.turn_environments()
|
||||
.map(|environment| {
|
||||
let grants = self
|
||||
.turn
|
||||
.granted_permissions(&environment.selection.environment_id);
|
||||
(environment, environment.fs_accessor(grants))
|
||||
})
|
||||
.map(|environment| (environment, self.fs_accessor(environment)))
|
||||
.collect()
|
||||
}
|
||||
|
||||
pub(crate) fn fs_accessor<'a>(
|
||||
&self,
|
||||
environment: &'a TurnEnvironment,
|
||||
) -> impl EnvironmentAccess + 'a + use<'a> {
|
||||
let grants = self
|
||||
.turn
|
||||
.granted_permissions(&environment.selection.environment_id);
|
||||
environment.fs_accessor(grants)
|
||||
}
|
||||
|
||||
/// Persist the context captured for this request, even after a live update.
|
||||
pub(crate) fn to_turn_context_item(&self) -> TurnContextItem {
|
||||
let mut item = self.turn.to_turn_context_item();
|
||||
|
||||
@@ -248,6 +248,15 @@ pub(crate) fn mcp_config_for_test(config: &crate::config::Config) -> Arc<codex_m
|
||||
))
|
||||
}
|
||||
|
||||
pub(crate) fn test_environment_id(turn: &TurnContext) -> &str {
|
||||
&turn
|
||||
.initial_environments
|
||||
.single_local_environment()
|
||||
.expect("test turn should have one local environment")
|
||||
.selection
|
||||
.environment_id
|
||||
}
|
||||
|
||||
/// Updates both initial/current views before sharing a test turn context.
|
||||
pub(crate) fn update_turn_settings_for_test(
|
||||
turn: &mut TurnContext,
|
||||
@@ -7796,11 +7805,13 @@ async fn request_permissions_tool_resolves_legacy_paths_against_selected_environ
|
||||
current_environment.shell,
|
||||
)
|
||||
};
|
||||
turn_context_mut.initial_environments.environments[0] =
|
||||
TurnEnvironmentState::Ready(environment);
|
||||
turn_context_mut
|
||||
.initial_environments
|
||||
.environments
|
||||
.push(TurnEnvironmentState::Ready(environment));
|
||||
|
||||
let call_id = "call-1".to_string();
|
||||
let handler = RequestPermissionsHandler;
|
||||
let registry = ToolRegistry::with_handler_for_test(Arc::new(RequestPermissionsHandler));
|
||||
let tracker = Arc::new(tokio::sync::Mutex::new(TurnDiffTracker::new()));
|
||||
let step_context = StepContext::for_test(Arc::clone(&turn_context));
|
||||
let handle = tokio::spawn({
|
||||
@@ -7810,30 +7821,32 @@ async fn request_permissions_tool_resolves_legacy_paths_against_selected_environ
|
||||
let tracker = Arc::clone(&tracker);
|
||||
let call_id = call_id.clone();
|
||||
async move {
|
||||
handler
|
||||
.handle(ToolInvocation {
|
||||
session,
|
||||
step_context,
|
||||
turn: turn_context,
|
||||
cancellation_token: CancellationToken::new(),
|
||||
tracker,
|
||||
call_id,
|
||||
tool_name: codex_tools::ToolName::plain("request_permissions"),
|
||||
source: ToolCallSource::Direct,
|
||||
payload: ToolPayload::Function {
|
||||
arguments: json!({
|
||||
"environment_id": "remote",
|
||||
"reason": "need write",
|
||||
"permissions": {
|
||||
"file_system": {
|
||||
"read": null,
|
||||
"write": ["relative.txt", "~/home-relative.txt"],
|
||||
},
|
||||
let invocation = ToolInvocation {
|
||||
session,
|
||||
step_context,
|
||||
turn: turn_context,
|
||||
cancellation_token: CancellationToken::new(),
|
||||
tracker,
|
||||
call_id,
|
||||
tool_name: codex_tools::ToolName::plain("request_permissions"),
|
||||
source: ToolCallSource::Direct,
|
||||
environments: Default::default(),
|
||||
payload: ToolPayload::Function {
|
||||
arguments: json!({
|
||||
"environment_id": "remote",
|
||||
"reason": "need write",
|
||||
"permissions": {
|
||||
"file_system": {
|
||||
"read": null,
|
||||
"write": ["relative.txt", "~/home-relative.txt"],
|
||||
},
|
||||
})
|
||||
.to_string(),
|
||||
},
|
||||
})
|
||||
},
|
||||
})
|
||||
.to_string(),
|
||||
},
|
||||
};
|
||||
registry
|
||||
.dispatch_any_with_state(invocation, /*call_state*/ None)
|
||||
.await
|
||||
}
|
||||
});
|
||||
@@ -7894,28 +7907,31 @@ async fn request_permissions_tool_rejects_invalid_requests(
|
||||
environment.selection.cwd = PathUri::parse("file:///workspace/%FF").expect("non-UTF8 cwd");
|
||||
let turn_context = Arc::new(turn_context);
|
||||
let step_context = StepContext::for_test(Arc::clone(&turn_context));
|
||||
let result = RequestPermissionsHandler
|
||||
.handle(ToolInvocation {
|
||||
session: Arc::new(session),
|
||||
step_context,
|
||||
turn: turn_context,
|
||||
cancellation_token: CancellationToken::new(),
|
||||
tracker: Arc::new(tokio::sync::Mutex::new(TurnDiffTracker::new())),
|
||||
call_id: "call-1".to_string(),
|
||||
tool_name: codex_tools::ToolName::plain("request_permissions"),
|
||||
source: ToolCallSource::Direct,
|
||||
payload: ToolPayload::Function {
|
||||
arguments: json!({
|
||||
"environment_id": environment_id,
|
||||
"permissions": {
|
||||
"file_system": {
|
||||
"write": ["relative"],
|
||||
},
|
||||
let registry = ToolRegistry::with_handler_for_test(Arc::new(RequestPermissionsHandler));
|
||||
let invocation = ToolInvocation {
|
||||
session: Arc::new(session),
|
||||
step_context,
|
||||
turn: turn_context,
|
||||
cancellation_token: CancellationToken::new(),
|
||||
tracker: Arc::new(tokio::sync::Mutex::new(TurnDiffTracker::new())),
|
||||
call_id: "call-1".to_string(),
|
||||
tool_name: codex_tools::ToolName::plain("request_permissions"),
|
||||
source: ToolCallSource::Direct,
|
||||
environments: Default::default(),
|
||||
payload: ToolPayload::Function {
|
||||
arguments: json!({
|
||||
"environment_id": environment_id,
|
||||
"permissions": {
|
||||
"file_system": {
|
||||
"write": ["relative"],
|
||||
},
|
||||
})
|
||||
.to_string(),
|
||||
},
|
||||
})
|
||||
},
|
||||
})
|
||||
.to_string(),
|
||||
},
|
||||
};
|
||||
let result = registry
|
||||
.dispatch_any_with_state(invocation, /*call_state*/ None)
|
||||
.await;
|
||||
|
||||
let Err(FunctionCallError::RespondToModel(output)) = result else {
|
||||
@@ -13376,6 +13392,7 @@ async fn unified_exec_rejects_escalated_permissions_when_policy_not_on_request()
|
||||
call_id: "exec-call".to_string(),
|
||||
tool_name: codex_tools::ToolName::plain("exec_command"),
|
||||
source: crate::tools::context::ToolCallSource::Direct,
|
||||
environments: Default::default(),
|
||||
payload: ToolPayload::Function {
|
||||
arguments: serde_json::json!({
|
||||
"cmd": "echo hi",
|
||||
|
||||
@@ -616,6 +616,7 @@ async fn guardian_allows_exec_command_additional_permissions_requests_past_polic
|
||||
call_id: "test-call".to_string(),
|
||||
tool_name: codex_tools::ToolName::plain("exec_command"),
|
||||
source: crate::tools::context::ToolCallSource::Direct,
|
||||
environments: Default::default(),
|
||||
payload: ToolPayload::Function {
|
||||
arguments: serde_json::json!({
|
||||
"cmd": "echo hi",
|
||||
@@ -740,6 +741,7 @@ async fn strict_auto_review_turn_grant_forces_guardian_for_exec_command_policy_s
|
||||
call_id: "strict-shell-command-call".to_string(),
|
||||
tool_name: codex_tools::ToolName::plain("exec_command"),
|
||||
source: ToolCallSource::Direct,
|
||||
environments: Default::default(),
|
||||
payload: ToolPayload::Function {
|
||||
arguments: serde_json::json!({
|
||||
"cmd": "echo hi",
|
||||
@@ -892,6 +894,7 @@ async fn delayed_exec_command_uses_its_captured_authority_after_next_turn_starts
|
||||
call_id: call_id.to_string(),
|
||||
tool_name: codex_tools::ToolName::plain("exec_command"),
|
||||
source: ToolCallSource::Direct,
|
||||
environments: Default::default(),
|
||||
payload: ToolPayload::Function {
|
||||
arguments: serde_json::json!({
|
||||
"cmd": command,
|
||||
@@ -1069,6 +1072,7 @@ async fn guardian_allows_unified_exec_additional_permissions_requests_past_polic
|
||||
call_id: "exec-call".to_string(),
|
||||
tool_name: codex_tools::ToolName::plain("exec_command"),
|
||||
source: crate::tools::context::ToolCallSource::Direct,
|
||||
environments: Default::default(),
|
||||
payload: ToolPayload::Function {
|
||||
arguments: serde_json::json!({
|
||||
"cmd": "echo hi",
|
||||
@@ -1193,6 +1197,7 @@ async fn exec_command_allows_sticky_turn_permissions_without_inline_request_perm
|
||||
call_id: "sticky-turn-grant".to_string(),
|
||||
tool_name: codex_tools::ToolName::plain("exec_command"),
|
||||
source: crate::tools::context::ToolCallSource::Direct,
|
||||
environments: Default::default(),
|
||||
payload: ToolPayload::Function {
|
||||
arguments: serde_json::json!({
|
||||
"cmd": "echo hi",
|
||||
|
||||
Reference in New Issue
Block a user