mirror of
https://github.com/openai/codex.git
synced 2026-11-27 23:50:52 +00:00
Preserve command approval items through turn interruption (#64349)
## Why The app server could show a command awaiting approval before core had recorded its start. Interrupting the turn or abandoning launch after approval could leave the live command item and saved history out of sync. ## What changed Have core publish the command item before prompting for approval and retain it across approval and sandbox retries until execution takes over. Complete abandoned items as declined when interrupted while awaiting approval, or as failed when interrupted after approval but before launch. Wait for pending approval item writes during shutdown. ## Testing Extend the app-server interrupt test to restart the server and verify that the interrupted turn contains exactly one declined command item with the original approval item ID. GitOrigin-RevId: c9f027fc7096d091dc8cf1936c88aa69d3eed8c1
This commit is contained in:
@@ -13,6 +13,7 @@ use crate::hook_runtime::run_permission_request_hooks;
|
||||
use crate::mcp_tool_call::request_mcp_tool_user_approval;
|
||||
use crate::sandboxing::SandboxPermissions;
|
||||
use crate::session::NetworkApprovalTurn;
|
||||
use crate::session::command_approval::CommandApproval;
|
||||
use crate::session::session::Session;
|
||||
use crate::tools::hook_names::HookToolName;
|
||||
use crate::tools::runtimes::apply_patch::ApplyPatchApprovalKey;
|
||||
@@ -551,11 +552,12 @@ impl Session {
|
||||
self: &Arc<Self>,
|
||||
action: ApprovalAction,
|
||||
ctx: ApprovalContext,
|
||||
command_approval: Option<&mut CommandApproval>,
|
||||
) -> ApprovalResult {
|
||||
self.request_approval_with_user_prompt(
|
||||
&action,
|
||||
&ctx,
|
||||
self.request_user_approval(&action, &ctx),
|
||||
self.request_user_approval(&action, &ctx, command_approval),
|
||||
)
|
||||
.await
|
||||
}
|
||||
@@ -785,9 +787,10 @@ impl Session {
|
||||
}
|
||||
|
||||
pub(super) async fn request_user_approval(
|
||||
&self,
|
||||
self: &Arc<Self>,
|
||||
action: &ApprovalAction,
|
||||
ctx: &ApprovalContext,
|
||||
command_approval: Option<&mut CommandApproval>,
|
||||
) -> ApprovalResolution {
|
||||
let decision = match action {
|
||||
ApprovalAction::ExecCommand {
|
||||
@@ -805,6 +808,11 @@ impl Session {
|
||||
.or_else(|| ctx.approval_reason.clone())
|
||||
.or_else(|| justification.clone());
|
||||
let human_once = ctx.requires_human_approval();
|
||||
let environment = ctx
|
||||
.review_context
|
||||
.environments()
|
||||
.turn_environments()
|
||||
.find(|environment| environment.selection.environment_id == *environment_id);
|
||||
let request = || async {
|
||||
self.request_command_approval(
|
||||
ctx.review_context.turn(),
|
||||
@@ -841,7 +849,10 @@ impl Session {
|
||||
ReviewDecision::Abort,
|
||||
]
|
||||
}),
|
||||
/*plugin_attribution_override*/ None,
|
||||
environment
|
||||
.filter(|_| !human_once && ctx.network_approval_context.is_none())
|
||||
.map(|env| env.environment().as_ref()),
|
||||
command_approval,
|
||||
)
|
||||
.await
|
||||
};
|
||||
@@ -849,11 +860,7 @@ impl Session {
|
||||
request().await
|
||||
} else {
|
||||
let tool_name = "unified_exec";
|
||||
let policy_fingerprint = ctx
|
||||
.review_context
|
||||
.environments()
|
||||
.turn_environments()
|
||||
.find(|environment| environment.selection.environment_id == *environment_id)
|
||||
let policy_fingerprint = environment
|
||||
.and_then(|environment| environment.config().exec_policy.as_ref())
|
||||
.map(codex_execpolicy::RequirementsExecPolicy::fingerprint);
|
||||
let cache_keys = action
|
||||
@@ -894,7 +901,8 @@ impl Session {
|
||||
/*proposed_execpolicy_amendment*/ None,
|
||||
additional_permissions.clone(),
|
||||
Some(vec![ReviewDecision::Approved, ReviewDecision::Abort]),
|
||||
/*plugin_attribution_override*/ None,
|
||||
/*command_environment*/ None,
|
||||
command_approval,
|
||||
)
|
||||
.await
|
||||
}
|
||||
@@ -970,7 +978,8 @@ impl Session {
|
||||
/*proposed_execpolicy_amendment*/ None,
|
||||
/*additional_permissions*/ None,
|
||||
/*available_decisions*/ None,
|
||||
/*plugin_attribution_override*/ None,
|
||||
/*command_environment*/ None,
|
||||
command_approval,
|
||||
)
|
||||
.await
|
||||
}
|
||||
|
||||
@@ -155,7 +155,7 @@ async fn non_utf8_cwd_preserves_approval_routing(
|
||||
tty: false,
|
||||
proposed_execpolicy_amendment: None,
|
||||
};
|
||||
let prompt = session.request_user_approval(&action, &context);
|
||||
let prompt = session.request_user_approval(&action, &context, /*command_approval*/ None);
|
||||
let approval = session.request_reviewer_approval(&action, &context, prompt);
|
||||
tokio::pin!(approval);
|
||||
let expected = if reviewer == ApprovalsReviewer::User {
|
||||
@@ -229,7 +229,7 @@ async fn explicit_mcp_reviewer_override_takes_precedence_over_action_context() {
|
||||
network_approval_context: None,
|
||||
};
|
||||
|
||||
let prompt = session.request_user_approval(&action, &context);
|
||||
let prompt = session.request_user_approval(&action, &context, /*command_approval*/ None);
|
||||
tokio::select! {
|
||||
resolution = session.request_reviewer_approval(&action, &context, prompt) => {
|
||||
panic!("expected a user approval request, got {resolution:?}");
|
||||
|
||||
@@ -42,6 +42,7 @@ pub(super) fn truncate_rejection_message(message: &str) -> String {
|
||||
|
||||
#[derive(Clone, Copy)]
|
||||
pub(crate) struct ToolEventCtx<'a> {
|
||||
pub proposal_started_at_ms: Option<i64>,
|
||||
pub sandbox_type: Option<codex_protocol::sandbox::SandboxType>,
|
||||
pub model_context: Option<&'a ModelInvocationContext>,
|
||||
pub session: &'a Session,
|
||||
@@ -61,6 +62,7 @@ impl<'a> ToolEventCtx<'a> {
|
||||
turn_diff_tracker: Option<&'a SharedTurnDiffTracker>,
|
||||
) -> Self {
|
||||
Self {
|
||||
proposal_started_at_ms: None,
|
||||
model_context: None,
|
||||
sandbox_type: None,
|
||||
session,
|
||||
@@ -167,28 +169,35 @@ async fn emit_exec_command_begin(ctx: ToolEventCtx<'_>, exec_input: &ExecCommand
|
||||
);
|
||||
}
|
||||
let (plugin_id, script_path) = plugin_attribution_fields(exec_input.plugin_attribution);
|
||||
ctx.session
|
||||
.emit_turn_item_started(
|
||||
ctx.turn,
|
||||
&TurnItem::CommandExecution(CommandExecutionItem {
|
||||
id: ctx.call_id.to_string(),
|
||||
model_context: ctx.model_context.cloned(),
|
||||
sandbox_type: ctx.sandbox_type,
|
||||
plugin_id,
|
||||
script_path,
|
||||
process_id: exec_input.process_id.map(str::to_owned),
|
||||
command: exec_input.command.to_vec(),
|
||||
cwd: exec_input.cwd.clone(),
|
||||
parsed_cmd: exec_input.parsed_cmd.to_vec(),
|
||||
source: exec_input.source,
|
||||
interaction_input: exec_input.interaction_input.map(str::to_owned),
|
||||
status: CommandExecutionStatus::InProgress,
|
||||
aggregated_output: None,
|
||||
exit_code: None,
|
||||
duration: None,
|
||||
}),
|
||||
)
|
||||
.await;
|
||||
let item = CommandExecutionItem {
|
||||
id: ctx.call_id.to_string(),
|
||||
model_context: ctx.model_context.cloned(),
|
||||
sandbox_type: ctx.sandbox_type,
|
||||
plugin_id,
|
||||
script_path,
|
||||
process_id: exec_input.process_id.map(str::to_owned),
|
||||
command: exec_input.command.to_vec(),
|
||||
cwd: exec_input.cwd.clone(),
|
||||
parsed_cmd: exec_input.parsed_cmd.to_vec(),
|
||||
source: exec_input.source,
|
||||
interaction_input: exec_input.interaction_input.map(str::to_owned),
|
||||
status: CommandExecutionStatus::InProgress,
|
||||
aggregated_output: None,
|
||||
exit_code: None,
|
||||
duration: None,
|
||||
};
|
||||
if let Some(started_at_ms) = ctx.proposal_started_at_ms {
|
||||
ctx.session
|
||||
.send_event(
|
||||
ctx.turn,
|
||||
item.as_legacy_begin_event(ctx.turn.sub_id.clone(), started_at_ms),
|
||||
)
|
||||
.await;
|
||||
} else {
|
||||
ctx.session
|
||||
.emit_turn_item_started(ctx.turn, &TurnItem::CommandExecution(item))
|
||||
.await;
|
||||
}
|
||||
}
|
||||
// Concrete, allocation-free emitter: avoid trait objects and boxed futures.
|
||||
pub(crate) enum ToolEmitter {
|
||||
|
||||
@@ -934,7 +934,11 @@ impl NetworkApprovalService {
|
||||
.as_ref()
|
||||
.map(|call| call.trigger.call_id.as_str()),
|
||||
&policy_denial_message,
|
||||
session.request_user_approval(&action, &approval_context),
|
||||
session.request_user_approval(
|
||||
&action,
|
||||
&approval_context,
|
||||
/*command_approval*/ None,
|
||||
),
|
||||
);
|
||||
let approval =
|
||||
session.request_approval_with_user_prompt(&action, &approval_context, user_prompt);
|
||||
|
||||
@@ -183,7 +183,7 @@ impl ToolOrchestrator {
|
||||
};
|
||||
let approval = tool_ctx
|
||||
.session
|
||||
.request_approval(action, approval_ctx)
|
||||
.request_approval(action, approval_ctx, tool.command_approval())
|
||||
.await;
|
||||
approval.result?;
|
||||
initial_approval_source = approval.source;
|
||||
@@ -219,7 +219,7 @@ impl ToolOrchestrator {
|
||||
};
|
||||
let approval = tool_ctx
|
||||
.session
|
||||
.request_approval(action, approval_ctx)
|
||||
.request_approval(action, approval_ctx, tool.command_approval())
|
||||
.await;
|
||||
approval.result?;
|
||||
initial_approval_source = approval.source;
|
||||
@@ -541,13 +541,13 @@ impl ToolOrchestrator {
|
||||
network_ctx.exec_requirement = None;
|
||||
tool_ctx
|
||||
.session
|
||||
.request_approval(action.clone(), network_ctx)
|
||||
.request_approval(action.clone(), network_ctx, tool.command_approval())
|
||||
.await
|
||||
.result?;
|
||||
}
|
||||
tool_ctx
|
||||
.session
|
||||
.request_approval(action, approval_ctx)
|
||||
.request_approval(action, approval_ctx, tool.command_approval())
|
||||
.await
|
||||
.result?;
|
||||
}
|
||||
|
||||
@@ -13,6 +13,7 @@ use crate::plugins::metrics::sidecar_for_command;
|
||||
use crate::sandboxing::ExecOptions;
|
||||
use crate::sandboxing::ExecServerEnvConfig;
|
||||
use crate::sandboxing::SandboxPermissions;
|
||||
use crate::session::command_approval::CommandApproval;
|
||||
use crate::session::turn_context::TurnEnvironment;
|
||||
use crate::shell::ShellType;
|
||||
use crate::shell_snapshot::ShellSnapshotSandbox;
|
||||
@@ -100,10 +101,12 @@ pub struct UnifiedExecApprovalKey {
|
||||
/// Runtime adapter that keeps policy and sandbox orchestration on the
|
||||
/// unified-exec side while delegating process startup to the manager.
|
||||
pub struct UnifiedExecRuntime<'a> {
|
||||
pub(crate) command_approval: CommandApproval,
|
||||
manager: &'a UnifiedExecProcessManager,
|
||||
}
|
||||
|
||||
pub(crate) struct UnifiedExecAttempt {
|
||||
pub(crate) command_approval: CommandApproval,
|
||||
pub(crate) process: UnifiedExecProcess,
|
||||
pub(crate) metrics_sidecar: Option<PluginMetricsSidecar>,
|
||||
pub(crate) permissions: TerminalPermissions,
|
||||
@@ -125,7 +128,10 @@ fn unified_exec_options(
|
||||
impl<'a> UnifiedExecRuntime<'a> {
|
||||
/// Creates a runtime bound to the shared unified-exec process manager.
|
||||
pub fn new(manager: &'a UnifiedExecProcessManager) -> Self {
|
||||
Self { manager }
|
||||
Self {
|
||||
manager,
|
||||
command_approval: CommandApproval::default(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -163,6 +169,10 @@ impl Approvable<UnifiedExecRequest<'_>> for UnifiedExecRuntime<'_> {
|
||||
}
|
||||
|
||||
impl<'a> ToolRuntime<UnifiedExecRequest<'_>, UnifiedExecAttempt> for UnifiedExecRuntime<'a> {
|
||||
fn command_approval(&mut self) -> Option<&mut CommandApproval> {
|
||||
Some(&mut self.command_approval)
|
||||
}
|
||||
|
||||
fn turn_environment<'b>(&self, req: &'b UnifiedExecRequest<'_>) -> &'b TurnEnvironment {
|
||||
&req.prepared.turn_environment
|
||||
}
|
||||
@@ -651,9 +661,11 @@ impl<'a> ToolRuntime<UnifiedExecRequest<'_>, UnifiedExecAttempt> for UnifiedExec
|
||||
prepared.turn_environment.environment().as_ref(),
|
||||
)
|
||||
.await;
|
||||
let mut process = with_launch_failure_events(process, prepared, ctx).await?;
|
||||
let mut process =
|
||||
with_launch_failure_events(process, prepared, ctx, &mut self.command_approval).await?;
|
||||
process._shell_snapshot = shell_snapshot;
|
||||
Ok(UnifiedExecAttempt {
|
||||
command_approval: std::mem::take(&mut self.command_approval),
|
||||
process,
|
||||
metrics_sidecar,
|
||||
permissions,
|
||||
|
||||
@@ -2,6 +2,7 @@
|
||||
//! Approval rejections retain their own lifecycle, and sandbox denials remain retryable.
|
||||
//! Session shutdown drains failure publications that outlive the caller.
|
||||
|
||||
use crate::session::command_approval::CommandApproval;
|
||||
use crate::tools::events::ToolEmitter;
|
||||
use crate::tools::events::ToolEventCtx;
|
||||
use crate::tools::events::ToolEventFailure;
|
||||
@@ -18,6 +19,7 @@ pub(super) async fn with_launch_failure_events(
|
||||
result: Result<UnifiedExecProcess, ToolError>,
|
||||
req: &PreparedExecCommand,
|
||||
ctx: &ToolCtx,
|
||||
command_approval: &mut CommandApproval,
|
||||
) -> Result<UnifiedExecProcess, ToolError> {
|
||||
let Err(ToolError::Rejected(message)) = &result else {
|
||||
return result;
|
||||
@@ -63,6 +65,8 @@ pub(super) async fn with_launch_failure_events(
|
||||
let step_context = Arc::clone(&ctx.step_context);
|
||||
let call_id = ctx.call_id.clone();
|
||||
let message = message.clone();
|
||||
// The failure write is registered, so the proposal can release its shutdown token.
|
||||
let proposal_started_at_ms = command_approval.handoff();
|
||||
// Dropping the join handle on cancellation leaves both lifecycle events running.
|
||||
let publish = async move {
|
||||
let model_context = step_context.model_context();
|
||||
@@ -73,6 +77,7 @@ pub(super) async fn with_launch_failure_events(
|
||||
&call_id,
|
||||
/*turn_diff_tracker*/ None,
|
||||
);
|
||||
event_ctx.proposal_started_at_ms = proposal_started_at_ms;
|
||||
event_ctx.model_context = Some(&model_context);
|
||||
emitter.emit(event_ctx, ToolEventStage::Begin).await;
|
||||
emitter
|
||||
|
||||
@@ -6,6 +6,7 @@
|
||||
|
||||
use crate::sandboxing::ExecOptions;
|
||||
use crate::sandboxing::SandboxPermissions;
|
||||
use crate::session::command_approval::CommandApproval;
|
||||
use crate::session::session::Session;
|
||||
use crate::session::step_context::StepContext;
|
||||
use crate::session::turn_context::TurnEnvironment;
|
||||
@@ -397,6 +398,10 @@ pub(crate) enum ToolError {
|
||||
}
|
||||
|
||||
pub(crate) trait ToolRuntime<Req, Out>: Approvable<Req> + Sandboxable {
|
||||
fn command_approval(&mut self) -> Option<&mut CommandApproval> {
|
||||
None
|
||||
}
|
||||
|
||||
fn turn_environment<'a>(&self, req: &'a Req) -> &'a TurnEnvironment;
|
||||
|
||||
fn uses_executor_managed_process_sandbox(&self, _req: &Req) -> bool {
|
||||
|
||||
Reference in New Issue
Block a user