mirror of
https://github.com/openai/codex.git
synced 2026-11-25 07:23:22 +00:00
Preserve complete actions in Guardian approval reviews (#44569)
## Why Truncating action arguments can leave approval reviewers evaluating incomplete actions. Large actions need complete review input and explicit handling when they exceed the review budget. ## What changed - Remove action truncation and the fixed synchronous action byte limit. Admit complete actions against the whole-request budget, splitting long text losslessly into bounded transport parts and accounting for their framing. - Route actions exceeding the asynchronous action budget to synchronous review. Prevent cached scores from covering oversized calls, including expanded approval arguments, while allowing later small actions to recover score reuse. - Request user approval when optional review exhausts its local input budget. Keep required review and compaction service failures closed to approval, and retire exhausted review sessions. ## Testing Add coverage for complete large-action delivery, optional user fallback, required-review denial, subsequent review recovery, async overflow through MCP approval routing, and lossless text splitting with budget accounting. GitOrigin-RevId: 08f06e68a94b2f779480dd6b6cf5bc30b241f6dd
This commit is contained in:
1 parent
3319d9b296
commit
9c4879f3a5
33 files changed
+841
-547
No files matched your search
@@ -32,6 +32,8 @@ const TIMEOUT_INSTRUCTIONS: &str = concat!(
|
||||
"Do not assume the action is unsafe based on the timeout alone. ",
|
||||
"You may retry once, or ask the user for guidance or explicit approval.",
|
||||
);
|
||||
const INPUT_BUDGET_MESSAGE: &str =
|
||||
"the complete action and minimum review context exceed the reviewer input budget";
|
||||
|
||||
pub fn guardian_timeout_message(model_info: &ModelInfo) -> String {
|
||||
model_info
|
||||
@@ -44,7 +46,8 @@ pub fn guardian_timeout_message(model_info: &ModelInfo) -> String {
|
||||
}
|
||||
|
||||
pub struct ReviewCompletion {
|
||||
pub decision: ReviewDecision,
|
||||
/// `None` requests user approval after optional review exhausts its input budget.
|
||||
pub decision: Option<ReviewDecision>,
|
||||
pub event: GuardianAssessmentEvent,
|
||||
pub warning: Option<String>,
|
||||
pub analytics: GuardianReviewAnalyticsResult,
|
||||
@@ -55,6 +58,7 @@ pub struct ReviewCompletion {
|
||||
pub fn complete_review(
|
||||
outcome: GuardianReviewOutcome,
|
||||
model: &ModelInfo,
|
||||
require_guardian: bool,
|
||||
mut event: GuardianAssessmentEvent,
|
||||
mut analytics: GuardianReviewAnalyticsResult,
|
||||
) -> ReviewCompletion {
|
||||
@@ -83,6 +87,20 @@ pub fn complete_review(
|
||||
}
|
||||
GuardianReviewOutcome::Error(error) => {
|
||||
analytics.failure_reason = Some(error.failure_reason());
|
||||
if matches!(error, GuardianReviewError::InputBudgetExceeded) && !require_guardian {
|
||||
let rationale = format!("Automatic approval review failed: {INPUT_BUDGET_MESSAGE}");
|
||||
analytics.decision = GuardianReviewDecision::Aborted;
|
||||
analytics.terminal_status = GuardianReviewTerminalStatus::Aborted;
|
||||
event.status = GuardianAssessmentStatus::Aborted;
|
||||
event.rationale = Some(rationale.clone());
|
||||
return ReviewCompletion {
|
||||
decision: None,
|
||||
event,
|
||||
warning: Some(rationale),
|
||||
analytics,
|
||||
assessment_outcome: None,
|
||||
};
|
||||
}
|
||||
match error {
|
||||
GuardianReviewError::Timeout => {
|
||||
let rationale = "Automatic approval review timed out while evaluating the requested approval.".to_string();
|
||||
@@ -91,7 +109,7 @@ pub fn complete_review(
|
||||
event.status = GuardianAssessmentStatus::TimedOut;
|
||||
event.rationale = Some(rationale.clone());
|
||||
return ReviewCompletion {
|
||||
decision: ReviewDecision::TimedOut,
|
||||
decision: Some(ReviewDecision::TimedOut),
|
||||
event,
|
||||
warning: Some(rationale),
|
||||
analytics,
|
||||
@@ -103,16 +121,26 @@ pub fn complete_review(
|
||||
analytics.terminal_status = GuardianReviewTerminalStatus::Aborted;
|
||||
event.status = GuardianAssessmentStatus::Aborted;
|
||||
return ReviewCompletion {
|
||||
decision: ReviewDecision::Abort,
|
||||
decision: Some(ReviewDecision::Abort),
|
||||
event,
|
||||
warning: None,
|
||||
analytics,
|
||||
assessment_outcome: None,
|
||||
};
|
||||
}
|
||||
GuardianReviewError::PromptBuild { message }
|
||||
| GuardianReviewError::Session { message, .. }
|
||||
| GuardianReviewError::Parse { message } => {
|
||||
GuardianReviewError::InputBudgetExceeded
|
||||
| GuardianReviewError::PromptBuild { .. }
|
||||
| GuardianReviewError::Session { .. }
|
||||
| GuardianReviewError::Parse { .. } => {
|
||||
let message = match &error {
|
||||
GuardianReviewError::InputBudgetExceeded => INPUT_BUDGET_MESSAGE,
|
||||
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}");
|
||||
@@ -120,9 +148,9 @@ pub fn complete_review(
|
||||
event.status = GuardianAssessmentStatus::Denied;
|
||||
event.rationale = Some(rationale.clone());
|
||||
return ReviewCompletion {
|
||||
decision: ReviewDecision::denied(format!(
|
||||
decision: Some(ReviewDecision::denied(format!(
|
||||
"{rationale}\n{REVIEW_FAILURE_INSTRUCTIONS}"
|
||||
)),
|
||||
))),
|
||||
event,
|
||||
warning: Some(rationale),
|
||||
analytics,
|
||||
@@ -177,7 +205,7 @@ pub fn complete_review(
|
||||
))
|
||||
};
|
||||
ReviewCompletion {
|
||||
decision,
|
||||
decision: Some(decision),
|
||||
event,
|
||||
warning: Some(warning),
|
||||
analytics,
|
||||
|
||||
@@ -74,6 +74,7 @@ impl<'a> FailedReviewFeedback<'a> {
|
||||
GuardianReviewSessionOutcome::Completed(Ok(None)) => ("missing_decision", None),
|
||||
GuardianReviewSessionOutcome::Completed(Err(_))
|
||||
| GuardianReviewSessionOutcome::PromptBuildFailed(_)
|
||||
| GuardianReviewSessionOutcome::InputBudgetExceeded
|
||||
| GuardianReviewSessionOutcome::SessionFailed { .. } => ("failed", None),
|
||||
GuardianReviewSessionOutcome::TimedOut => ("timed_out", None),
|
||||
GuardianReviewSessionOutcome::Aborted => ("aborted", None),
|
||||
|
||||
@@ -13,6 +13,7 @@ pub enum GuardianReviewOutcome {
|
||||
|
||||
#[derive(Debug)]
|
||||
pub enum GuardianReviewError {
|
||||
InputBudgetExceeded,
|
||||
PromptBuild {
|
||||
message: String,
|
||||
},
|
||||
@@ -61,7 +62,9 @@ impl GuardianReviewError {
|
||||
pub fn failure_reason(&self) -> GuardianReviewFailureReason {
|
||||
match self {
|
||||
Self::PromptBuild { .. } => GuardianReviewFailureReason::PromptBuildError,
|
||||
Self::Session { .. } => GuardianReviewFailureReason::SessionError,
|
||||
Self::Session { .. } | Self::InputBudgetExceeded => {
|
||||
GuardianReviewFailureReason::SessionError
|
||||
}
|
||||
Self::Parse { .. } => GuardianReviewFailureReason::ParseError,
|
||||
Self::Timeout => GuardianReviewFailureReason::Timeout,
|
||||
Self::Cancelled => GuardianReviewFailureReason::Cancelled,
|
||||
@@ -73,6 +76,7 @@ impl GuardianReviewError {
|
||||
pub enum GuardianReviewSessionOutcome {
|
||||
Completed(anyhow::Result<Option<String>>),
|
||||
PromptBuildFailed(anyhow::Error),
|
||||
InputBudgetExceeded,
|
||||
SessionFailed {
|
||||
error: anyhow::Error,
|
||||
error_info: Option<CodexErrorInfo>,
|
||||
@@ -102,6 +106,9 @@ impl From<GuardianReviewSessionOutcome> for GuardianReviewOutcome {
|
||||
GuardianReviewSessionOutcome::PromptBuildFailed(error) => {
|
||||
Self::Error(GuardianReviewError::prompt_build(error))
|
||||
}
|
||||
GuardianReviewSessionOutcome::InputBudgetExceeded => {
|
||||
Self::Error(GuardianReviewError::InputBudgetExceeded)
|
||||
}
|
||||
GuardianReviewSessionOutcome::SessionFailed {
|
||||
error,
|
||||
error_info,
|
||||
|
||||
@@ -92,13 +92,14 @@ impl ReviewReport {
|
||||
&self,
|
||||
outcome: GuardianReviewOutcome,
|
||||
model: &ModelInfo,
|
||||
require_guardian: bool,
|
||||
analytics: GuardianReviewAnalyticsResult,
|
||||
completed_at_ms: i64,
|
||||
) -> ReviewCompletion {
|
||||
let mut event = self.started.clone();
|
||||
event.completed_at_ms = Some(completed_at_ms);
|
||||
event.decision_source = Some(GuardianAssessmentDecisionSource::Agent);
|
||||
crate::complete_review(outcome, model, event, analytics)
|
||||
crate::complete_review(outcome, model, require_guardian, event, analytics)
|
||||
}
|
||||
|
||||
pub fn track(
|
||||
|
||||
@@ -110,7 +110,8 @@ fn should_retry_guardian_review(outcome: &GuardianReviewOutcome) -> bool {
|
||||
},
|
||||
GuardianReviewOutcome::Completed(_)
|
||||
| GuardianReviewOutcome::Error(
|
||||
GuardianReviewError::PromptBuild { .. }
|
||||
GuardianReviewError::InputBudgetExceeded
|
||||
| GuardianReviewError::PromptBuild { .. }
|
||||
| GuardianReviewError::Session {
|
||||
error_info: None, ..
|
||||
}
|
||||
|
||||
@@ -71,6 +71,10 @@ fn guardian_review_retry_only_retries_transient_session_and_parse_errors() {
|
||||
.collect::<Vec<_>>();
|
||||
outcomes.extend([
|
||||
(GuardianReviewOutcome::Completed(assessment), false),
|
||||
(
|
||||
GuardianReviewOutcome::Error(GuardianReviewError::InputBudgetExceeded),
|
||||
false,
|
||||
),
|
||||
(
|
||||
GuardianReviewOutcome::Error(GuardianReviewError::prompt_build(anyhow::anyhow!(
|
||||
"prompt"
|
||||
|
||||
@@ -17,6 +17,7 @@ use crate::GuardianReviewSessionLimits;
|
||||
/// Runtime operations bound to one immutable approval action and its issuing context.
|
||||
/// Preparation captures trusted evidence. Completion must invalidate stale approvals
|
||||
/// and may only satisfy the review gate, never expand the action's execution authority.
|
||||
/// Completion may return `None` for user approval only when host policy permits it.
|
||||
pub trait ReviewHost: Send + Sync {
|
||||
type Prepared: Send + Sync;
|
||||
|
||||
@@ -36,7 +37,7 @@ pub trait ReviewHost: Send + Sync {
|
||||
prepared: Self::Prepared,
|
||||
outcome: GuardianReviewOutcome,
|
||||
analytics: GuardianReviewAnalyticsResult,
|
||||
) -> impl Future<Output = ReviewDecision> + Send;
|
||||
) -> impl Future<Output = Option<ReviewDecision>> + Send;
|
||||
}
|
||||
|
||||
/// One review bound by the host before Guardian's approval policy chooses to run it.
|
||||
@@ -51,12 +52,12 @@ impl<H: ReviewHost> SynchronousReview<H> {
|
||||
}
|
||||
|
||||
impl<H: ReviewHost> SynchronousApprovalReviewer for SynchronousReview<H> {
|
||||
fn review(&self, reason: GuardianReviewReason) -> ExtensionFuture<'_, ReviewDecision> {
|
||||
fn review(&self, reason: GuardianReviewReason) -> ExtensionFuture<'_, Option<ReviewDecision>> {
|
||||
Box::pin(async move {
|
||||
let deadline = Instant::now() + crate::REVIEW_TIMEOUT;
|
||||
let prepared = match self.host.prepare(reason, deadline).await {
|
||||
Ok(prepared) => prepared,
|
||||
Err(decision) => return decision,
|
||||
Err(decision) => return Some(decision),
|
||||
};
|
||||
let (outcome, analytics) = Box::pin(crate::run_with_retry(
|
||||
GuardianReviewSessionLimits {
|
||||
|
||||
Reference in new issue
Block a user