Use issuing-step context for Guardian MCP elicitation reviews (#51067)

## Why

MCP elicitation reviews used turn-start context, which could omit remote environments that became ready before the tool invocation and use stale environment permissions.

## What changed

Capture the issuing step's Guardian review context with each live MCP invocation and reuse it for elicitation reviews. Fall back to turn context when no matching live invocation is available, and preserve the captured context for in-flight actions.

## Testing

Extend the deferred-executor regression test to cover MCP elicitations, checking that Guardian receives the newly ready remote environment and its denied paths, excludes stale local denied paths, and returns the denial to the parent model.

GitOrigin-RevId: 0cc58b8f30543a9af87d53e1a90f574a8e6a2f84
This commit is contained in:
jif
2026-10-05 10:08:59 +00:00
committed by copyberry
parent 315f0efb34
commit 39e013c8b7
7 changed files with 112 additions and 53 deletions
@@ -37,7 +37,7 @@ use wiremock::matchers::body_partial_json;
// The tool itself needs no approval. Its server requests a separate Guardian
// review after tools/call, exercising the ordinary MCP elicitation path.
const ELICITATION_SERVER: &str = r#"
pub(super) const ELICITATION_SERVER: &str = r#"
import json
import sys
+44 -17
View File
@@ -3154,12 +3154,22 @@ async fn deferred_executor_spawn_agent_inherits_ready_step_environments(
Ok(())
}
#[test_case("exec_command")]
#[test_case("mcp_elicitation")]
#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
async fn deferred_executor_guardian_uses_newly_ready_step_environment() -> Result<()> {
async fn deferred_executor_guardian_uses_newly_ready_step_environment(
tool: &'static str,
) -> Result<()> {
const WAIT_CALL_ID: &str = "wait-for-guardian-environment";
const EXEC_CALL_ID: &str = "guardian-ready-environment-command";
const DENIAL_RATIONALE: &str = "The remote environment policy denies this action.";
if tool == "mcp_elicitation" {
core_test_support::skip_if_wine_exec!(
Ok(()),
"the MCP fixture requires a host Python interpreter"
);
}
let listener = TcpListener::bind("127.0.0.1:0").await?;
let server = start_mock_server().await;
let completed_response =
@@ -3177,17 +3187,21 @@ async fn deferred_executor_guardian_uses_newly_ready_step_environment() -> Resul
),
completed_response(
"resp-guardian-command",
ev_function_call(
EXEC_CALL_ID,
"exec_command",
&json!({
"cmd": "printf guardian-should-not-run",
"environment_id": REMOTE_ENVIRONMENT_ID,
"sandbox_permissions": SandboxPermissions::RequireEscalated,
"justification": "Review the newly ready remote environment.",
})
.to_string(),
),
if tool == "mcp_elicitation" {
ev_function_call_with_namespace(EXEC_CALL_ID, "mcp__cua_repl", "js", "{}")
} else {
ev_function_call(
EXEC_CALL_ID,
"exec_command",
&json!({
"cmd": "printf guardian-should-not-run",
"environment_id": REMOTE_ENVIRONMENT_ID,
"sandbox_permissions": SandboxPermissions::RequireEscalated,
"justification": "Review the newly ready remote environment.",
})
.to_string(),
)
},
),
completed_response(
"resp-guardian-review",
@@ -3205,11 +3219,21 @@ async fn deferred_executor_guardian_uses_newly_ready_step_environment() -> Resul
.await;
let mut builder = test_codex()
.with_exec_server_url(format!("ws://{}", listener.local_addr()?))
.with_config(|config| {
.with_config(move |config| {
config.project_doc_max_bytes = 0;
config.permissions.approval_policy = Constrained::allow_any(AskForApproval::OnRequest);
config.approvals_reviewer = ApprovalsReviewer::AutoReview;
assert!(config.features.enable(Feature::DeferredExecutor).is_ok());
if tool == "mcp_elicitation" {
config.mcp_servers.set(serde_json::from_value(json!({
"cua_repl": {
"command": if cfg!(windows) { "python" } else { "python3" },
"args": ["-u", "-c", super::guardian_mcp_elicitation::ELICITATION_SERVER,
json!([{"codex_sensitive_action": true}]).to_string(), "forward"],
"default_tools_approval_mode": "approve",
}
})).expect("MCP server config")).expect("set MCP fixture");
}
});
let (attach_tx, attach_rx) = tokio::sync::oneshot::channel();
let (shutdown_tx, shutdown_rx) = tokio::sync::oneshot::channel();
@@ -3220,6 +3244,9 @@ async fn deferred_executor_guardian_uses_newly_ready_step_environment() -> Resul
shutdown_rx,
));
let test = expect_startup(builder.build_with_remote_and_local_env(&server)).await;
if tool == "mcp_elicitation" {
core_test_support::wait_for_mcp_server(&test.codex, "cua_repl").await?;
}
let remote_cwd = test.cwd.path().join("guardian-remote").abs();
let local_cwd = test.cwd.path().abs();
fs::create_dir_all(remote_cwd.as_path())?;
@@ -3305,10 +3332,10 @@ async fn deferred_executor_guardian_uses_newly_ready_step_environment() -> Resul
"Guardian used the stale local environment's denied-read policy: {guardian_context}"
);
let rejection = requests
.iter()
.find_map(|request| request.function_call_output_text(EXEC_CALL_ID))
.context("Guardian denial should be returned to the parent model")?;
assert!(rejection.contains(DENIAL_RATIONALE));
.last()
.context("expected parent continuation after review")?
.function_call_output(EXEC_CALL_ID);
assert!(rejection.to_string().contains(DENIAL_RATIONALE));
shutdown_tx
.send(())