Files
codex/codex-rs/core/src/guardian/runtime.rs
T
felixxia-oai 457ea27b0b Shadow fresh Guardian V2 approvals with Guardian V3 (#64782)
## Why

Collect Guardian V3 verdicts for actions that require a fresh Guardian V2
review while keeping Guardian V2 in control of approval and tool execution.

## What changed

When Guardian V3 shadowing is enabled and an enrolled reviewer is available,
run a single background review for eligible fresh approvals and log the
outcome with the live review ID. Skip shadowing when the reviewer is busy,
shadow intake has stopped, or a rollout budget is configured.

## Testing

Add app-server coverage for immediate approvals, ordinary synchronous
reviews, and fresh reviews with and without a rollout budget. Hold a shadow
denial in flight to verify that the tool and turn can finish, a second
request skips the busy reviewer, and stopped intake prevents new shadow
reviews while the in-flight verdict can still finish.

GitOrigin-RevId: 5e31f3d7e3b0433455b8f2a5fb52e483588b1b29
2026-11-30 18:27:38 +00:00

94 lines
3.5 KiB
Rust

//! Prepares and validates host approval actions for the extension-owned reviewer.
use codex_protocol::protocol::ReviewDecision;
use codex_utils_path_uri::PathConvention;
use std::sync::Arc;
use tokio_util::sync::CancellationToken;
use super::ApprovalRequestReasons;
use super::GuardianApprovalRequest;
use super::GuardianReviewContext;
use super::GuardianReviewOptions;
use crate::session::session::Session;
use crate::tools::sandboxing::ApprovalAction;
pub(crate) use codex_guardian_reviewer::ReviewAction;
pub(crate) fn from_approval_action(
action: ApprovalAction,
context: &GuardianReviewContext,
) -> ReviewAction {
let exec_command_cwd_convention = match &action {
ApprovalAction::ExecCommand {
environment_id,
cwd,
..
} => Some(
match context
.environments()
.turn_environments()
.find(|environment| environment.selection.environment_id == *environment_id)
.and_then(|environment| environment.executor_platform_os.as_deref())
{
Some("windows") => PathConvention::Windows,
Some(_) => PathConvention::Posix,
// Legacy executors did not report their OS. Preserve the
// previous spelling-based behavior for those executors.
None => cwd
.infer_path_convention()
.unwrap_or_else(PathConvention::native),
},
),
_ => None,
};
let category = action.guardian_scope();
let original = serde_json::to_value(&action).map_err(|error| error.to_string());
match action.into_guardian_request(exec_command_cwd_convention) {
Ok(request) => ReviewAction {
category,
..ReviewAction::from(request)
},
Err(error) => ReviewAction {
action: original,
category,
tool_call_id: None,
request: Err(error.to_string()),
},
}
}
/// Preserve the checks previously made before entering Guardian from tool approvals.
pub(super) fn validate<'a>(
action: &'a ReviewAction,
context: &GuardianReviewContext,
) -> Result<&'a GuardianApprovalRequest, ReviewDecision> {
let request = action.request.as_ref().map_err(|error| {
tracing::error!(%error, "failed to build automatic approval action");
ReviewDecision::denied("automatic approval review could not prepare the action")
})?;
if let Some(environment_id) = request.target_environment_id()
&& !context
.environments()
.turn_environments()
.any(|environment| environment.selection.environment_id == environment_id)
{
let message = if matches!(request, GuardianApprovalRequest::WriteStdin { .. }) {
"automatic approval review cannot access the terminal's environment; select it before retrying"
} else {
"automatic approval review cannot access the request's environment"
};
return Err(ReviewDecision::denied(message));
}
Ok(request)
}
#[derive(Clone)]
pub(super) struct ReviewRuntime {
pub(super) attached_reviewer: Option<Arc<dyn crate::guardian_review::AttachedReviewer>>,
pub(super) session: Arc<Session>,
pub(super) history_reset: CancellationToken,
pub(super) context: GuardianReviewContext,
pub(super) request: Arc<ReviewAction>,
pub(super) reasons: ApprovalRequestReasons,
pub(super) options: GuardianReviewOptions,
}