Some checks failed
blocking-ci / rust-ci (push) Has been cancelled
blocking-ci / sdk (push) Has been cancelled
blocking-ci / CI required (push) Has been cancelled
blocking-ci / Bazel (push) Has been cancelled
blocking-ci / Blob size policy (push) Has been cancelled
blocking-ci / cargo-deny (push) Has been cancelled
blocking-ci / Codespell (push) Has been cancelled
blocking-ci / repo-checks (push) Has been cancelled
postmerge-ci / rust-ci-full (push) Has been cancelled
postmerge-ci / v8-canary (push) Has been cancelled
postmerge-ci / Postmerge CI results (push) Has been cancelled
221 lines
9.9 KiB
Rust
221 lines
9.9 KiB
Rust
//! Converts reviewer outcomes into decisions and reports. The host publishes these
|
|
//! reports and applies them only to the action whose evidence was reviewed.
|
|
|
|
use crate::GuardianReviewDecision;
|
|
use crate::GuardianReviewDetails;
|
|
use crate::GuardianReviewTerminalStatus;
|
|
use codex_prompts::ResolvedModelMessages;
|
|
use codex_prompts::render_guardian_rejection;
|
|
use codex_protocol::openai_models::ModelInfo;
|
|
use codex_protocol::protocol::GuardianAssessmentEvent;
|
|
use codex_protocol::protocol::GuardianAssessmentOutcome;
|
|
use codex_protocol::protocol::GuardianAssessmentStatus;
|
|
use codex_protocol::protocol::GuardianRiskLevel;
|
|
use codex_protocol::protocol::GuardianUserAuthorization;
|
|
use codex_protocol::protocol::ReviewDecision;
|
|
|
|
use crate::GuardianReviewError;
|
|
use crate::GuardianReviewOutcome;
|
|
|
|
const REVIEW_FAILURE_INSTRUCTIONS: &str = concat!(
|
|
"The action was not executed because automatic approval review could not be completed. ",
|
|
"This is a review failure, not a determination that the action is unsafe. ",
|
|
"Do not bypass the approval check; resolve the error or ask the user for guidance.",
|
|
);
|
|
const INPUT_BUDGET_MESSAGE: &str =
|
|
"the complete action and minimum review context exceed the reviewer input budget";
|
|
|
|
pub struct ReviewCompletion {
|
|
/// `None` requests user approval after optional review exhausts its input budget.
|
|
pub decision: Option<ReviewDecision>,
|
|
/// Absent for actions reviewed without an action-specific client report.
|
|
pub event: Option<GuardianAssessmentEvent>,
|
|
pub warning: Option<String>,
|
|
pub details: GuardianReviewDetails,
|
|
/// Only completed assessments may enter the evidence cache or count as policy denials.
|
|
pub assessment_outcome: Option<GuardianAssessmentOutcome>,
|
|
}
|
|
|
|
pub fn complete_review(
|
|
outcome: GuardianReviewOutcome,
|
|
model: &ModelInfo,
|
|
require_guardian: bool,
|
|
mut event: Option<GuardianAssessmentEvent>,
|
|
mut details: GuardianReviewDetails,
|
|
) -> ReviewCompletion {
|
|
let completed_assessment = match &outcome {
|
|
GuardianReviewOutcome::Completed(assessment) => Some(assessment.outcome),
|
|
GuardianReviewOutcome::CachedApproval | GuardianReviewOutcome::Error(_) => None,
|
|
};
|
|
let assessment = match outcome {
|
|
GuardianReviewOutcome::CachedApproval => {
|
|
if let Some(event) = event.as_mut() {
|
|
event.status = GuardianAssessmentStatus::Approved;
|
|
event.decision_source = None;
|
|
event.rationale = Some("Approved using a current low-risk async score.".to_owned());
|
|
}
|
|
details.decision = GuardianReviewDecision::Approved;
|
|
details.terminal_status = GuardianReviewTerminalStatus::Approved;
|
|
details.failure_reason = None;
|
|
return ReviewCompletion {
|
|
decision: Some(ReviewDecision::Approved),
|
|
event,
|
|
warning: None,
|
|
details,
|
|
assessment_outcome: None,
|
|
};
|
|
}
|
|
GuardianReviewOutcome::Completed(assessment) => {
|
|
let approved = matches!(assessment.outcome, GuardianAssessmentOutcome::Allow);
|
|
details.decision = if approved {
|
|
GuardianReviewDecision::Approved
|
|
} else {
|
|
GuardianReviewDecision::Denied
|
|
};
|
|
details.terminal_status = if approved {
|
|
GuardianReviewTerminalStatus::Approved
|
|
} else {
|
|
GuardianReviewTerminalStatus::Denied
|
|
};
|
|
details.failure_reason = None;
|
|
details.risk_level = Some(assessment.risk_level);
|
|
details.user_authorization = Some(assessment.user_authorization);
|
|
details.outcome = Some(assessment.outcome);
|
|
assessment
|
|
}
|
|
GuardianReviewOutcome::Error(error) => {
|
|
details.failure_reason = Some(error.failure_reason());
|
|
match error {
|
|
GuardianReviewError::InputBudgetExceeded if !require_guardian => {
|
|
let rationale =
|
|
format!("Automatic approval review failed: {INPUT_BUDGET_MESSAGE}");
|
|
details.decision = GuardianReviewDecision::Aborted;
|
|
details.terminal_status = GuardianReviewTerminalStatus::Aborted;
|
|
if let Some(event) = event.as_mut() {
|
|
event.status = GuardianAssessmentStatus::Aborted;
|
|
event.rationale = Some(rationale.clone());
|
|
}
|
|
return ReviewCompletion {
|
|
decision: None,
|
|
event,
|
|
warning: Some(rationale),
|
|
details,
|
|
assessment_outcome: None,
|
|
};
|
|
}
|
|
GuardianReviewError::Timeout => {
|
|
let rationale = "Automatic approval review timed out while evaluating the requested approval.".to_string();
|
|
details.decision = GuardianReviewDecision::Denied;
|
|
details.terminal_status = GuardianReviewTerminalStatus::TimedOut;
|
|
if let Some(event) = event.as_mut() {
|
|
event.status = GuardianAssessmentStatus::TimedOut;
|
|
event.rationale = Some(rationale.clone());
|
|
}
|
|
return ReviewCompletion {
|
|
decision: Some(ReviewDecision::TimedOut),
|
|
event,
|
|
warning: Some(rationale),
|
|
details,
|
|
assessment_outcome: None,
|
|
};
|
|
}
|
|
GuardianReviewError::Cancelled => {
|
|
details.decision = GuardianReviewDecision::Aborted;
|
|
details.terminal_status = GuardianReviewTerminalStatus::Aborted;
|
|
if let Some(event) = event.as_mut() {
|
|
event.status = GuardianAssessmentStatus::Aborted;
|
|
}
|
|
return ReviewCompletion {
|
|
decision: Some(ReviewDecision::Abort),
|
|
event,
|
|
warning: None,
|
|
details,
|
|
assessment_outcome: None,
|
|
};
|
|
}
|
|
GuardianReviewError::StaleAuthorization
|
|
| GuardianReviewError::InputBudgetExceeded
|
|
| GuardianReviewError::PromptBuild { .. }
|
|
| GuardianReviewError::Session { .. }
|
|
| GuardianReviewError::Parse { .. } => {
|
|
let message = match &error {
|
|
GuardianReviewError::InputBudgetExceeded => INPUT_BUDGET_MESSAGE,
|
|
GuardianReviewError::StaleAuthorization => {
|
|
"authorization changed before approval could be applied"
|
|
}
|
|
GuardianReviewError::PromptBuild { message }
|
|
| GuardianReviewError::Session { message, .. }
|
|
| GuardianReviewError::Parse { message } => message,
|
|
GuardianReviewError::Timeout | GuardianReviewError::Cancelled => {
|
|
"guardian review failed"
|
|
}
|
|
};
|
|
details.decision = GuardianReviewDecision::Denied;
|
|
details.terminal_status = GuardianReviewTerminalStatus::FailedClosed;
|
|
let rationale = format!("Automatic approval review failed: {message}");
|
|
// Keep the existing blocked status for client compatibility.
|
|
if let Some(event) = event.as_mut() {
|
|
event.status = GuardianAssessmentStatus::Denied;
|
|
event.rationale = Some(rationale.clone());
|
|
}
|
|
return ReviewCompletion {
|
|
decision: Some(ReviewDecision::denied(format!(
|
|
"{rationale}\n{REVIEW_FAILURE_INSTRUCTIONS}"
|
|
))),
|
|
event,
|
|
warning: Some(rationale),
|
|
details,
|
|
assessment_outcome: None,
|
|
};
|
|
}
|
|
}
|
|
}
|
|
};
|
|
let approved = matches!(assessment.outcome, GuardianAssessmentOutcome::Allow);
|
|
let verdict = if approved { "approved" } else { "denied" };
|
|
let authorization = match assessment.user_authorization {
|
|
GuardianUserAuthorization::Unknown => "unknown",
|
|
GuardianUserAuthorization::Low => "low",
|
|
GuardianUserAuthorization::Medium => "medium",
|
|
GuardianUserAuthorization::High => "high",
|
|
};
|
|
let risk = match assessment.risk_level {
|
|
GuardianRiskLevel::Low => "low",
|
|
GuardianRiskLevel::Medium => "medium",
|
|
GuardianRiskLevel::High => "high",
|
|
GuardianRiskLevel::Critical => "critical",
|
|
};
|
|
let warning = format!(
|
|
"Automatic approval review {verdict} (risk: {risk}, authorization: {authorization}): {}",
|
|
assessment.rationale
|
|
);
|
|
if let Some(event) = event.as_mut() {
|
|
event.status = if approved {
|
|
GuardianAssessmentStatus::Approved
|
|
} else {
|
|
GuardianAssessmentStatus::Denied
|
|
};
|
|
event.risk_level = Some(assessment.risk_level);
|
|
event.user_authorization = Some(assessment.user_authorization);
|
|
event.rationale = Some(assessment.rationale.clone());
|
|
}
|
|
let decision = if approved {
|
|
ReviewDecision::Approved
|
|
} else {
|
|
let rejection_instructions = ResolvedModelMessages::from_model(model)
|
|
.auto_review()
|
|
.rejection_instructions;
|
|
ReviewDecision::denied(render_guardian_rejection(
|
|
&assessment.rationale,
|
|
rejection_instructions,
|
|
))
|
|
};
|
|
ReviewCompletion {
|
|
decision: Some(decision),
|
|
event,
|
|
warning: Some(warning),
|
|
details,
|
|
assessment_outcome: completed_assessment,
|
|
}
|
|
}
|