mirror of
https://github.com/openai/codex.git
synced 2026-11-27 23:31:09 +00:00
Use step environments for Guardian approval reviews (#37618)
## Why Deferred environments can become ready after a turn starts. Guardian approval reviews must use the environment selected for the current step so their working directory and permission context are not taken from the stale turn snapshot. ## What changed - Carry `StepContext` through tool execution and approval handling. - Add a Guardian review context that pairs the parent turn with the current step's environment snapshot. - Use that snapshot for Guardian prompts, reviewer sessions, and delegated reviewer threads while retaining turn-level fallbacks for callers without a step context. ## Testing Added an integration test that makes a deferred remote environment ready before an escalated command and verifies Guardian receives that environment's working directory and denied-read policy instead of the initial local environment's. GitOrigin-RevId: 779096bc7d0e0c7453a009c02141029714b9b407
This commit is contained in:
1 parent
936f5eb3ee
commit
dd43a9967f
29 files changed
+387
-131
No files matched your search
@@ -10,6 +10,7 @@ use crate::apply_patch;
|
||||
use crate::apply_patch::convert_apply_patch_to_protocol;
|
||||
use crate::function_tool::FunctionCallError;
|
||||
use crate::session::session::Session;
|
||||
use crate::session::step_context::StepContext;
|
||||
use crate::session::turn_context::TurnContext;
|
||||
use crate::session::turn_context::TurnEnvironment;
|
||||
use crate::tools::context::ApplyPatchToolOutput;
|
||||
@@ -397,7 +398,7 @@ impl ApplyPatchHandler {
|
||||
codex_apply_patch::MaybeApplyPatchVerified::Body(changes) => {
|
||||
let tool_ctx = ToolCtx {
|
||||
session,
|
||||
turn,
|
||||
step_context: Arc::clone(&step_context),
|
||||
call_id,
|
||||
tool_name,
|
||||
};
|
||||
@@ -487,11 +488,12 @@ pub(crate) async fn intercept_apply_patch(
|
||||
fs: &dyn ExecutorFileSystem,
|
||||
turn_environment: TurnEnvironment,
|
||||
session: Arc<Session>,
|
||||
turn: Arc<TurnContext>,
|
||||
step_context: Arc<StepContext>,
|
||||
tracker: Option<&SharedTurnDiffTracker>,
|
||||
call_id: &str,
|
||||
tool_name: &str,
|
||||
) -> Result<Option<FunctionToolOutput>, FunctionCallError> {
|
||||
let turn = &step_context.turn;
|
||||
let sandbox =
|
||||
turn.file_system_sandbox_context(/*additional_permissions*/ None, &turn_environment);
|
||||
match codex_apply_patch::maybe_parse_apply_patch_verified(command, cwd, fs, Some(&sandbox))
|
||||
@@ -500,7 +502,7 @@ pub(crate) async fn intercept_apply_patch(
|
||||
codex_apply_patch::MaybeApplyPatchVerified::Body(changes) => {
|
||||
let tool_ctx = ToolCtx {
|
||||
session,
|
||||
turn,
|
||||
step_context,
|
||||
call_id: call_id.to_string(),
|
||||
tool_name: ToolName::plain(tool_name),
|
||||
};
|
||||
@@ -533,7 +535,7 @@ async fn execute_verified_patch(
|
||||
.await
|
||||
.unwrap_or_else(|_| patch_permissions_without_path_matching(&action));
|
||||
let apply = apply_patch::prepare_apply_patch(
|
||||
tool_ctx.turn.as_ref(),
|
||||
tool_ctx.step_context.turn.as_ref(),
|
||||
turn_environment.permission_profile(),
|
||||
&file_system_sandbox_policy,
|
||||
action,
|
||||
@@ -546,7 +548,7 @@ async fn execute_verified_patch(
|
||||
);
|
||||
let event_ctx = ToolEventCtx::new(
|
||||
tool_ctx.session.as_ref(),
|
||||
tool_ctx.turn.as_ref(),
|
||||
tool_ctx.step_context.turn.as_ref(),
|
||||
&tool_ctx.call_id,
|
||||
tracker,
|
||||
);
|
||||
@@ -568,8 +570,8 @@ async fn execute_verified_patch(
|
||||
&mut runtime,
|
||||
&request,
|
||||
&tool_ctx,
|
||||
tool_ctx.turn.as_ref(),
|
||||
tool_ctx.turn.approval_policy(),
|
||||
tool_ctx.step_context.turn.as_ref(),
|
||||
tool_ctx.step_context.turn.approval_policy(),
|
||||
)
|
||||
.await
|
||||
.map(|result| result.output);
|
||||
@@ -579,7 +581,7 @@ async fn execute_verified_patch(
|
||||
};
|
||||
let event_ctx = ToolEventCtx::new(
|
||||
tool_ctx.session.as_ref(),
|
||||
tool_ctx.turn.as_ref(),
|
||||
tool_ctx.step_context.turn.as_ref(),
|
||||
&tool_ctx.call_id,
|
||||
tracker,
|
||||
);
|
||||
|
||||
@@ -46,7 +46,6 @@ impl RequestPermissionsHandler {
|
||||
) -> Result<Box<dyn crate::tools::context::ToolOutput>, FunctionCallError> {
|
||||
let ToolInvocation {
|
||||
session,
|
||||
turn,
|
||||
step_context,
|
||||
cancellation_token,
|
||||
call_id,
|
||||
@@ -94,7 +93,7 @@ impl RequestPermissionsHandler {
|
||||
|
||||
let response = session
|
||||
.request_permissions_for_environment(
|
||||
&turn,
|
||||
&step_context,
|
||||
call_id,
|
||||
args,
|
||||
turn_environment.selection(),
|
||||
|
||||
@@ -7,7 +7,7 @@ use tokio_util::sync::CancellationToken;
|
||||
use crate::exec::ExecParams;
|
||||
use crate::exec_policy::ExecApprovalRequest;
|
||||
use crate::function_tool::FunctionCallError;
|
||||
use crate::session::turn_context::TurnContext;
|
||||
use crate::session::step_context::StepContext;
|
||||
use crate::session::turn_context::TurnEnvironment;
|
||||
use crate::shell::ShellType;
|
||||
use crate::tools::context::FunctionToolOutput;
|
||||
@@ -53,7 +53,7 @@ struct RunExecLikeArgs {
|
||||
additional_permissions: Option<AdditionalPermissionProfile>,
|
||||
prefix_rule: Option<Vec<String>>,
|
||||
session: Arc<crate::session::session::Session>,
|
||||
turn: Arc<TurnContext>,
|
||||
step_context: Arc<StepContext>,
|
||||
turn_environment: TurnEnvironment,
|
||||
tracker: crate::tools::context::SharedTurnDiffTracker,
|
||||
call_id: String,
|
||||
@@ -70,12 +70,13 @@ async fn run_exec_like(args: RunExecLikeArgs) -> Result<FunctionToolOutput, Func
|
||||
additional_permissions,
|
||||
prefix_rule,
|
||||
session,
|
||||
turn,
|
||||
step_context,
|
||||
turn_environment,
|
||||
tracker,
|
||||
call_id,
|
||||
shell_runtime_backend,
|
||||
} = args;
|
||||
let turn = Arc::clone(&step_context.turn);
|
||||
|
||||
let fs = turn_environment.environment.get_filesystem();
|
||||
|
||||
@@ -145,7 +146,7 @@ async fn run_exec_like(args: RunExecLikeArgs) -> Result<FunctionToolOutput, Func
|
||||
fs.as_ref(),
|
||||
turn_environment.clone(),
|
||||
session.clone(),
|
||||
turn.clone(),
|
||||
Arc::clone(&step_context),
|
||||
Some(&tracker),
|
||||
&call_id,
|
||||
tool_name.name.as_str(),
|
||||
@@ -213,7 +214,7 @@ async fn run_exec_like(args: RunExecLikeArgs) -> Result<FunctionToolOutput, Func
|
||||
let mut runtime = ShellRuntime::for_shell_command(shell_runtime_backend);
|
||||
let tool_ctx = ToolCtx {
|
||||
session: session.clone(),
|
||||
turn: turn.clone(),
|
||||
step_context,
|
||||
call_id: call_id.clone(),
|
||||
tool_name,
|
||||
};
|
||||
|
||||
@@ -230,7 +230,7 @@ impl ShellCommandHandler {
|
||||
additional_permissions: params.additional_permissions.clone(),
|
||||
prefix_rule,
|
||||
session,
|
||||
turn,
|
||||
step_context,
|
||||
turn_environment,
|
||||
tracker,
|
||||
call_id,
|
||||
|
||||
@@ -128,7 +128,8 @@ impl ExecCommandHandler {
|
||||
};
|
||||
|
||||
let manager: &UnifiedExecProcessManager = &session.services.unified_exec_manager;
|
||||
let context = UnifiedExecContext::new(session.clone(), turn.clone(), call_id.clone());
|
||||
let context =
|
||||
UnifiedExecContext::new(session.clone(), step_context.clone(), call_id.clone());
|
||||
let environment_args: ExecCommandEnvironmentArgs = parse_arguments(&arguments)?;
|
||||
let Some(turn_environment) = resolve_tool_environment(
|
||||
&step_context.environments,
|
||||
@@ -194,7 +195,7 @@ impl ExecCommandHandler {
|
||||
if let Some(native_cwd) = native_cwd.as_ref() {
|
||||
maybe_emit_implicit_skill_invocation(
|
||||
session.as_ref(),
|
||||
context.turn.as_ref(),
|
||||
context.step_context.turn.as_ref(),
|
||||
&hook_command,
|
||||
native_cwd,
|
||||
)
|
||||
@@ -292,11 +293,11 @@ impl ExecCommandHandler {
|
||||
.requests_sandbox_override()
|
||||
&& !effective_additional_permissions.permissions_preapproved
|
||||
&& !matches!(
|
||||
context.turn.approval_policy(),
|
||||
context.step_context.turn.approval_policy(),
|
||||
codex_protocol::protocol::AskForApproval::OnRequest
|
||||
)
|
||||
{
|
||||
let approval_policy = context.turn.approval_policy();
|
||||
let approval_policy = context.step_context.turn.approval_policy();
|
||||
manager.release_process_id(process_id).await;
|
||||
return Err(FunctionCallError::RespondToModel(format!(
|
||||
"approval policy is {approval_policy:?}; reject command — you cannot ask for escalated permissions if the approval policy is {approval_policy:?}"
|
||||
@@ -312,7 +313,7 @@ impl ExecCommandHandler {
|
||||
|| {
|
||||
normalize_and_validate_additional_permissions(
|
||||
additional_permissions_allowed,
|
||||
context.turn.approval_policy(),
|
||||
context.step_context.turn.approval_policy(),
|
||||
effective_additional_permissions.sandbox_permissions,
|
||||
effective_additional_permissions.additional_permissions,
|
||||
effective_additional_permissions.permissions_preapproved,
|
||||
@@ -334,7 +335,7 @@ impl ExecCommandHandler {
|
||||
fs.as_ref(),
|
||||
turn_environment.clone(),
|
||||
context.session.clone(),
|
||||
context.turn.clone(),
|
||||
Arc::clone(&context.step_context),
|
||||
Some(&tracker),
|
||||
&context.call_id,
|
||||
"exec_command",
|
||||
@@ -371,7 +372,7 @@ impl ExecCommandHandler {
|
||||
sandbox_cwd: native_environment_cwd,
|
||||
turn_environment: turn_environment.clone(),
|
||||
shell_mode,
|
||||
network: context.turn.network.clone(),
|
||||
network: context.step_context.turn.network.clone(),
|
||||
tty,
|
||||
sandbox_permissions: effective_additional_permissions.sandbox_permissions,
|
||||
additional_permissions: normalized_additional_permissions,
|
||||
|
||||
Reference in new issue
Block a user