Cancel Guardian reviews with their tool calls (#40021)

## What changed

Propagate tool cancellation tokens into Guardian approval reviews so interrupting a tool also aborts its pending review. Apply the same cancellation behavior to server-initiated MCP approval elicitation.

## Testing

Add integration coverage for cancellation from direct tool calls, code-mode turns, background code-mode cells, and MCP elicitation.

GitOrigin-RevId: cbee4b422bbcbf6984e415ef6409cee705beebdc
This commit is contained in:
Won Park authored and copyberry committed 2026-08-21 21:51:12 +00:00
1 parent 677cfee000
commit 6143217c67
22 files changed
+500 -22

No files matched your search

@@ -55,6 +55,7 @@ async fn streaming_output_harness() -> anyhow::Result<StreamingOutputHarness> {
let context = UnifiedExecContext::new(
session,
crate::session::step_context::StepContext::for_test(turn),
tokio_util::sync::CancellationToken::new(),
"streaming-output-test".to_string(),
);
let transcript = Arc::new(tokio::sync::Mutex::new(HeadTailBuffer::default()));
+9 -1
View File
@@ -35,6 +35,7 @@ use codex_utils_path_uri::PathUri;
use rand::Rng;
use rand::rng;
use tokio::sync::Mutex;
use tokio_util::sync::CancellationToken;
use crate::sandboxing::SandboxPermissions;
use crate::session::session::Session;
@@ -78,14 +79,21 @@ pub(crate) const MAX_UNIFIED_EXEC_PROCESSES: usize = 64;
pub(crate) struct UnifiedExecContext {
pub session: Arc<Session>,
pub step_context: Arc<StepContext>,
pub cancellation_token: CancellationToken,
pub call_id: String,
}
impl UnifiedExecContext {
pub fn new(session: Arc<Session>, step_context: Arc<StepContext>, call_id: String) -> Self {
pub fn new(
session: Arc<Session>,
step_context: Arc<StepContext>,
cancellation_token: CancellationToken,
call_id: String,
) -> Self {
Self {
session,
step_context,
cancellation_token,
call_id,
}
}
@@ -126,6 +126,7 @@ async fn exec_command_with_tty(
let context = UnifiedExecContext::new(
Arc::clone(session),
crate::session::step_context::StepContext::for_test(Arc::clone(turn)),
tokio_util::sync::CancellationToken::new(),
"call".to_string(),
);
let started_at = Instant::now();
@@ -1303,6 +1303,7 @@ impl UnifiedExecProcessManager {
let tool_ctx = ToolCtx {
session: context.session.clone(),
step_context: Arc::clone(&context.step_context),
cancellation_token: context.cancellation_token.clone(),
call_id: context.call_id.clone(),
tool_name: ToolName::plain("exec_command"),
};
@@ -405,6 +405,7 @@ async fn failed_initial_end_for_unstored_process_uses_fallback_output() {
let context = UnifiedExecContext::new(
Arc::clone(&session),
crate::session::step_context::StepContext::for_test(Arc::clone(&turn)),
tokio_util::sync::CancellationToken::new(),
"call-unified-denied".to_string(),
);
let request = ExecCommandRequest {