Base: codex-main originale (prima del fork aKode)
This commit is contained in:
220
codex-rs/ext/guardian-reviewer/src/completion.rs
Normal file
220
codex-rs/ext/guardian-reviewer/src/completion.rs
Normal file
@@ -0,0 +1,220 @@
|
||||
//! Converts reviewer outcomes into decisions and reports. The host publishes these
|
||||
//! reports and applies them only to the action whose evidence was reviewed.
|
||||
|
||||
use codex_analytics::GuardianReviewAnalyticsResult;
|
||||
use codex_analytics::GuardianReviewDecision;
|
||||
use codex_analytics::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 analytics: GuardianReviewAnalyticsResult,
|
||||
/// 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 analytics: GuardianReviewAnalyticsResult,
|
||||
) -> 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());
|
||||
}
|
||||
analytics.decision = GuardianReviewDecision::Approved;
|
||||
analytics.terminal_status = GuardianReviewTerminalStatus::Approved;
|
||||
analytics.failure_reason = None;
|
||||
return ReviewCompletion {
|
||||
decision: Some(ReviewDecision::Approved),
|
||||
event,
|
||||
warning: None,
|
||||
analytics,
|
||||
assessment_outcome: None,
|
||||
};
|
||||
}
|
||||
GuardianReviewOutcome::Completed(assessment) => {
|
||||
let approved = matches!(assessment.outcome, GuardianAssessmentOutcome::Allow);
|
||||
analytics.decision = if approved {
|
||||
GuardianReviewDecision::Approved
|
||||
} else {
|
||||
GuardianReviewDecision::Denied
|
||||
};
|
||||
analytics.terminal_status = if approved {
|
||||
GuardianReviewTerminalStatus::Approved
|
||||
} else {
|
||||
GuardianReviewTerminalStatus::Denied
|
||||
};
|
||||
analytics.failure_reason = None;
|
||||
analytics.risk_level = Some(assessment.risk_level);
|
||||
analytics.user_authorization = Some(assessment.user_authorization);
|
||||
analytics.outcome = Some(assessment.outcome);
|
||||
assessment
|
||||
}
|
||||
GuardianReviewOutcome::Error(error) => {
|
||||
analytics.failure_reason = Some(error.failure_reason());
|
||||
match error {
|
||||
GuardianReviewError::InputBudgetExceeded if !require_guardian => {
|
||||
let rationale =
|
||||
format!("Automatic approval review failed: {INPUT_BUDGET_MESSAGE}");
|
||||
analytics.decision = GuardianReviewDecision::Aborted;
|
||||
analytics.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),
|
||||
analytics,
|
||||
assessment_outcome: None,
|
||||
};
|
||||
}
|
||||
GuardianReviewError::Timeout => {
|
||||
let rationale = "Automatic approval review timed out while evaluating the requested approval.".to_string();
|
||||
analytics.decision = GuardianReviewDecision::Denied;
|
||||
analytics.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),
|
||||
analytics,
|
||||
assessment_outcome: None,
|
||||
};
|
||||
}
|
||||
GuardianReviewError::Cancelled => {
|
||||
analytics.decision = GuardianReviewDecision::Aborted;
|
||||
analytics.terminal_status = GuardianReviewTerminalStatus::Aborted;
|
||||
if let Some(event) = event.as_mut() {
|
||||
event.status = GuardianAssessmentStatus::Aborted;
|
||||
}
|
||||
return ReviewCompletion {
|
||||
decision: Some(ReviewDecision::Abort),
|
||||
event,
|
||||
warning: None,
|
||||
analytics,
|
||||
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"
|
||||
}
|
||||
};
|
||||
analytics.decision = GuardianReviewDecision::Denied;
|
||||
analytics.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),
|
||||
analytics,
|
||||
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),
|
||||
analytics,
|
||||
assessment_outcome: completed_assessment,
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user