mirror of
https://github.com/openai/codex.git
synced 2026-11-18 22:20:48 +00:00
Use attached Guardian reviewers for synchronous approvals (#62309)
## Why An attached reviewer needs to catch up on evidence before issuing a verdict. If the approval context changes during that preparation, the first verdict must still be checked against the authorization captured before preparation began. ## What changed Bind the attached reviewer to the pending action and reserve a review session after evidence preparation. Submit the action and its permissions for a synchronous verdict, checking authorization before and after the verdict. Retries reserve a new session under the original review deadline. ## Testing Extend the review tests to check that preparation and verdict execution share a deadline. Cover when optional synchronous review falls back to user approval after local input exhaustion and when context-limit or server failures result in denial. GitOrigin-RevId: 0ea835ba897069d19d045605d97b58f47406a4b4
This commit is contained in:
@@ -0,0 +1,216 @@
|
||||
//! Holds one action's prepared reviewer and its pre-capture authorization revision.
|
||||
//! Native retries take a new reservation under the original verdict deadline.
|
||||
use super::super::*;
|
||||
use super::PreparedApproval;
|
||||
use super::ReviewAuthorization;
|
||||
use crate::guardian::input_budget::PendingReviewContext;
|
||||
use crate::guardian::review_session::PreparedVerdictTurn;
|
||||
use crate::guardian::review_session::ensure_guardian_followup_reminder;
|
||||
use crate::guardian::review_session::prepare_verdict_session;
|
||||
use crate::guardian::review_session::review_turn;
|
||||
use crate::guardian_review::BoundReviewer;
|
||||
use crate::guardian_review::PreparedAssessment;
|
||||
use crate::guardian_review::ReviewPreparationError;
|
||||
use codex_analytics::GuardianReviewSessionKind;
|
||||
use codex_guardian_context::ComposedContext;
|
||||
use codex_guardian_reviewer::GuardianReviewSessionOutcome;
|
||||
use codex_guardian_reviewer::SessionDisposition;
|
||||
use codex_protocol::user_input::UserInput;
|
||||
use tokio::sync::Mutex;
|
||||
|
||||
pub(super) struct AttachedApproval {
|
||||
provider: BoundReviewer,
|
||||
config: GuardianReviewSessionConfig,
|
||||
first: Mutex<Option<(ReviewAuthorization, Box<dyn PreparedAssessment>)>>,
|
||||
}
|
||||
|
||||
impl AttachedApproval {
|
||||
pub async fn prepare(
|
||||
provider: BoundReviewer,
|
||||
authorization: ReviewAuthorization,
|
||||
parent: &Arc<Session>,
|
||||
context: &GuardianReviewContext,
|
||||
deadline: ReviewDeadline,
|
||||
cancel: &CancellationToken,
|
||||
) -> Result<Self, ReviewPreparationError> {
|
||||
let prepare = async {
|
||||
let config = guardian_review_session_config(parent, context, provider.retrieval)
|
||||
.await
|
||||
.map_err(|_| ReviewPreparationError::ReviewerUnavailable)?;
|
||||
let reserved = Self::reserve(&provider, &config, parent, context, cancel).await?;
|
||||
Ok(Self {
|
||||
provider,
|
||||
config,
|
||||
first: Mutex::new(Some((authorization, reserved))),
|
||||
})
|
||||
};
|
||||
tokio::select! {
|
||||
biased;
|
||||
_ = cancel.cancelled() => Err(ReviewPreparationError::Cancelled),
|
||||
_ = deadline.wait() => Err(ReviewPreparationError::PreparationTimedOut),
|
||||
result = prepare => result,
|
||||
}
|
||||
}
|
||||
|
||||
async fn reserve(
|
||||
provider: &BoundReviewer,
|
||||
config: &GuardianReviewSessionConfig,
|
||||
parent: &Arc<Session>,
|
||||
context: &GuardianReviewContext,
|
||||
cancel: &CancellationToken,
|
||||
) -> Result<Box<dyn PreparedAssessment>, ReviewPreparationError> {
|
||||
let setup = crate::guardian_review::PreparedGuardianContext::prepare(
|
||||
parent.clone(),
|
||||
context.clone(),
|
||||
config.spawn_config.clone(),
|
||||
&parent.clone_history().await,
|
||||
&config.node_repl_policy,
|
||||
)
|
||||
.await
|
||||
.map_err(|_| ReviewPreparationError::ReviewerUnavailable)?;
|
||||
provider.preparation.prepare(&setup, cancel).await
|
||||
}
|
||||
|
||||
pub async fn attempt(
|
||||
&self,
|
||||
parent: &Arc<Session>,
|
||||
approval: &PreparedApproval,
|
||||
reasons: &ApprovalRequestReasons,
|
||||
authorization: ReviewAuthorization,
|
||||
deadline: ReviewDeadline,
|
||||
cancel: &CancellationToken,
|
||||
) -> (GuardianReviewOutcome, GuardianReviewAnalyticsResult) {
|
||||
let mut reservation = None;
|
||||
let mut analytics = GuardianReviewAnalyticsResult::without_session();
|
||||
let mut usage_at_start = None;
|
||||
let operation = async {
|
||||
let first = self.first.lock().await.take();
|
||||
let (authorization, model) = match first {
|
||||
Some(first) => first,
|
||||
None => (
|
||||
authorization,
|
||||
Self::reserve(
|
||||
&self.provider,
|
||||
&self.config,
|
||||
parent,
|
||||
&approval.context,
|
||||
cancel,
|
||||
)
|
||||
.await?,
|
||||
),
|
||||
};
|
||||
let model = reservation.insert(model);
|
||||
let config = &self.config;
|
||||
let kind = model.session_kind();
|
||||
let runtime = model.runtime();
|
||||
analytics = runtime.analytics(
|
||||
&config.review_model,
|
||||
config.default_reasoning_effort.as_ref(),
|
||||
&config.spawn_config.model_provider_id,
|
||||
kind,
|
||||
matches!(kind, GuardianReviewSessionKind::TrunkReused),
|
||||
);
|
||||
if let Some(stale) = authorization.invalidation(parent, Some(cancel)).await {
|
||||
analytics.context_changed = stale.context_changed;
|
||||
return anyhow::Ok((stale.outcome, SessionDisposition::Discard));
|
||||
}
|
||||
let action = codex_guardian_reviewer::build_guardian_planned_action(
|
||||
&approval.request,
|
||||
reasons.retry.clone().or_else(|| reasons.approval.clone()),
|
||||
approval.context.enclosing_invocation_id.as_deref(),
|
||||
)?;
|
||||
let permissions = super::super::super::permissions::for_request(
|
||||
&approval.request,
|
||||
Some(&approval.context),
|
||||
)?;
|
||||
let action_context = ComposedContext::for_pending_action(action, permissions.as_ref())?;
|
||||
let request = review_turn(
|
||||
parent,
|
||||
&approval.context,
|
||||
&config.spawn_config,
|
||||
&config.review_model,
|
||||
// Final admission replaces the turn-start marker with the typed action and permissions.
|
||||
vec![UserInput::Text {
|
||||
text: super::super::super::prompt::GUARDIAN_TRANSCRIPT_START.to_owned(),
|
||||
text_elements: Vec::new(),
|
||||
}],
|
||||
guardian_output_schema(),
|
||||
);
|
||||
let turn = PreparedVerdictTurn {
|
||||
request,
|
||||
evidence: PendingReviewContext {
|
||||
context: action_context,
|
||||
elastic: None,
|
||||
},
|
||||
node_repl_evidence_sequence: None,
|
||||
};
|
||||
usage_at_start = Some(runtime.token_usage_baseline().await);
|
||||
if matches!(kind, GuardianReviewSessionKind::TrunkReused) {
|
||||
ensure_guardian_followup_reminder(runtime).await;
|
||||
}
|
||||
let result = async {
|
||||
prepare_verdict_session(
|
||||
runtime,
|
||||
&approval.context,
|
||||
approval.category,
|
||||
&config.node_repl_policy,
|
||||
)
|
||||
.await
|
||||
.map_err(GuardianReviewSessionOutcome::PromptBuildFailed)?;
|
||||
runtime
|
||||
.run_sync_verdict(
|
||||
&approval.context,
|
||||
turn,
|
||||
deadline,
|
||||
Some(cancel),
|
||||
&mut analytics,
|
||||
)
|
||||
.await
|
||||
}
|
||||
.await;
|
||||
if let Some(stale) = authorization.invalidation(parent, Some(cancel)).await {
|
||||
analytics.context_changed |= stale.context_changed;
|
||||
return Ok((stale.outcome, SessionDisposition::Discard));
|
||||
}
|
||||
Ok(match result {
|
||||
Ok((turn, _)) => (turn.outcome, turn.disposition),
|
||||
Err(outcome) => (outcome, SessionDisposition::Discard),
|
||||
})
|
||||
};
|
||||
let result = run_before_review_deadline(deadline, Some(cancel), operation)
|
||||
.await
|
||||
.and_then(|inner| inner.map_err(GuardianReviewSessionOutcome::PromptBuildFailed));
|
||||
let (outcome, disposition) =
|
||||
result.unwrap_or_else(|outcome| (outcome, SessionDisposition::Discard));
|
||||
let Some(model) = reservation else {
|
||||
return (outcome.into(), analytics);
|
||||
};
|
||||
if let Some(start) = usage_at_start {
|
||||
model
|
||||
.runtime()
|
||||
.record_verdict_usage(&start, &mut analytics)
|
||||
.await;
|
||||
}
|
||||
model
|
||||
.runtime()
|
||||
.record_failed_review(
|
||||
parent,
|
||||
&approval.context,
|
||||
&approval.request,
|
||||
&self.config.review_model.model,
|
||||
codex_guardian_reviewer::ReviewFeedbackSettings {
|
||||
enabled: self.config.spawn_config.feedback_enabled,
|
||||
ephemeral: self.config.spawn_config.ephemeral,
|
||||
},
|
||||
&outcome,
|
||||
)
|
||||
.await;
|
||||
let outcome = GuardianReviewOutcome::from(outcome);
|
||||
model.finish(if matches!(outcome, GuardianReviewOutcome::Completed(_)) {
|
||||
disposition
|
||||
} else {
|
||||
SessionDisposition::Discard
|
||||
});
|
||||
(outcome, analytics)
|
||||
}
|
||||
}
|
||||
@@ -4,6 +4,7 @@
|
||||
use codex_async_utils::THREAD_STACK_SIZE_BYTES;
|
||||
use codex_protocol::config_types::ApprovalsReviewer;
|
||||
use codex_protocol::openai_models::GuardianScope;
|
||||
use codex_protocol::protocol::AskForApproval;
|
||||
use codex_protocol::protocol::ReviewDecision;
|
||||
use std::sync::Arc;
|
||||
use tokio::sync::oneshot;
|
||||
@@ -54,6 +55,20 @@ pub(crate) fn decide_approval(
|
||||
) -> futures::future::BoxFuture<'static, Option<ReviewDecision>> {
|
||||
let mut context: GuardianReviewContext = context.into();
|
||||
let request: ReviewAction = request.into();
|
||||
let bind_reviewer = |session: &Session| {
|
||||
session
|
||||
.services
|
||||
.thread_extension_data
|
||||
.get::<Arc<dyn crate::guardian_review::AttachedReviewer>>()
|
||||
.map(|reviewer| Arc::clone(reviewer.as_ref()).bind())
|
||||
};
|
||||
// Only Never can grant full access. Bind other requests before scheduling as usual.
|
||||
let may_have_full_access = context.approval_policy == AskForApproval::Never;
|
||||
let attached_reviewer = if may_have_full_access {
|
||||
None
|
||||
} else {
|
||||
bind_reviewer(&session)
|
||||
};
|
||||
// Bind the invocation before scheduling. Async review cleanup can outlive
|
||||
// the decision, but must not extend the time blocking this tool.
|
||||
let review_timing = if context
|
||||
@@ -136,7 +151,13 @@ pub(crate) fn decide_approval(
|
||||
context.approval_policy,
|
||||
&turn.config.permissions.effective_permission_profile(),
|
||||
);
|
||||
let mut require_synchronous_review = options.require_synchronous_review;
|
||||
let attached_reviewer = if may_have_full_access && !full_access {
|
||||
bind_reviewer(&session)
|
||||
} else {
|
||||
attached_reviewer
|
||||
};
|
||||
let mut require_synchronous_review =
|
||||
options.require_synchronous_review || attached_reviewer.is_some();
|
||||
if let Some(assessment) = &context.serving_assessment {
|
||||
let permissions = request.request.as_ref().ok().and_then(|request| {
|
||||
super::permissions::for_environment(&context, request.target_environment_id()).ok()
|
||||
@@ -167,6 +188,7 @@ pub(crate) fn decide_approval(
|
||||
let decision = codex_guardian_reviewer::ReviewRequest {
|
||||
serving_assessment: context.serving_assessment.as_ref().map(|value| value.requires_review),
|
||||
host: ReviewRuntime {
|
||||
attached_reviewer,
|
||||
session: Arc::clone(&session),
|
||||
history_reset: history_reset.clone(),
|
||||
context: context.clone(),
|
||||
|
||||
@@ -106,6 +106,7 @@ pub(super) struct GuardianReviewSessionConfig {
|
||||
pub(super) spawn_config: crate::config::Config,
|
||||
pub(super) node_repl_policy: GuardianNodeReplPolicy,
|
||||
review_model: ReviewModel,
|
||||
default_reasoning_effort: Option<codex_protocol::openai_models::ReasoningEffort>,
|
||||
}
|
||||
|
||||
/// Selects the thread's reviewer and leaves configuration resolution under the caller's deadline.
|
||||
@@ -169,6 +170,7 @@ pub(super) async fn guardian_review_session_config(
|
||||
spawn_config,
|
||||
node_repl_policy: GuardianNodeReplPolicy::from_messages(model_messages),
|
||||
review_model,
|
||||
default_reasoning_effort: guardian_model_info.default_reasoning_level.clone(),
|
||||
})
|
||||
}
|
||||
|
||||
|
||||
@@ -1,9 +1,13 @@
|
||||
//! Binds an immutable action and root lifetime to fresh per-attempt authorization evidence.
|
||||
//! The extension chooses effects; this adapter supplies evidence, validation and publication.
|
||||
|
||||
#[path = "attached_review.rs"]
|
||||
mod attached;
|
||||
use super::super::runtime::validate;
|
||||
use super::*;
|
||||
use crate::codex_thread::GuardianAuthorizationVersion;
|
||||
use crate::guardian_review::ReviewPreparationError;
|
||||
use attached::AttachedApproval;
|
||||
use codex_config::config_toml::CircuitBreakAction;
|
||||
use codex_guardian_reviewer::ReviewDeadline;
|
||||
use codex_guardian_reviewer::ReviewHost;
|
||||
@@ -12,10 +16,12 @@ use codex_protocol::protocol::ErrorEvent;
|
||||
|
||||
pub(in crate::guardian) struct PreparedApproval {
|
||||
request: GuardianApprovalRequest,
|
||||
category: GuardianScope,
|
||||
context: GuardianReviewContext,
|
||||
// Unlike authorization, the original history lifetime cannot advance on retry.
|
||||
root_history: Option<(codex_protocol::ThreadId, u64)>,
|
||||
formatted_action: Option<String>,
|
||||
attached: Option<AttachedApproval>,
|
||||
}
|
||||
|
||||
pub(in crate::guardian) struct ApprovalEvidence {
|
||||
@@ -123,6 +129,7 @@ impl ReviewHost for super::super::runtime::ReviewRuntime {
|
||||
let super::super::runtime::ReviewRuntime {
|
||||
session,
|
||||
history_reset: _,
|
||||
attached_reviewer,
|
||||
mut context,
|
||||
request,
|
||||
reasons: _,
|
||||
@@ -174,34 +181,91 @@ impl ReviewHost for super::super::runtime::ReviewRuntime {
|
||||
.as_ref()
|
||||
.map(PluginCommandAttribution::serialized_fields)
|
||||
.unzip();
|
||||
let report =
|
||||
codex_guardian_reviewer::ReviewReport::new(codex_guardian_reviewer::ReviewMetadata {
|
||||
thread_id: session.thread_id.to_string(),
|
||||
turn_id: assessment_turn_id,
|
||||
turn_trigger: turn.turn_metadata_state.current_turn_trigger(),
|
||||
review_id: review_id.to_owned(),
|
||||
target_item_id,
|
||||
plugin_id,
|
||||
script_path,
|
||||
approval_request_source,
|
||||
reviewed_action: guardian_reviewed_action(&request),
|
||||
action: guardian_assessment_action(&request),
|
||||
review_reason,
|
||||
model_context,
|
||||
timeout: turn.config.guardian_review_timeout,
|
||||
});
|
||||
let root_history = session
|
||||
let metadata = codex_guardian_reviewer::ReviewMetadata {
|
||||
thread_id: session.thread_id.to_string(),
|
||||
turn_id: assessment_turn_id,
|
||||
turn_trigger: turn.turn_metadata_state.current_turn_trigger(),
|
||||
review_id: review_id.to_owned(),
|
||||
target_item_id,
|
||||
plugin_id,
|
||||
script_path,
|
||||
approval_request_source,
|
||||
reviewed_action: guardian_reviewed_action(&request),
|
||||
action: guardian_assessment_action(&request),
|
||||
review_reason,
|
||||
model_context,
|
||||
timeout: turn.config.guardian_review_timeout,
|
||||
};
|
||||
let root = session
|
||||
.services
|
||||
.agent_control
|
||||
.get_guardian_package(session.thread_id)
|
||||
.await
|
||||
.map(|snapshot| (snapshot.root_thread_id, snapshot.history_reset_version));
|
||||
.get_guardian_package(session.thread_id);
|
||||
let (report, root_history, attached) = if let Some(provider) = attached_reviewer {
|
||||
let root = root.await;
|
||||
let root_history = root
|
||||
.as_ref()
|
||||
.map(|root| (root.root_thread_id, root.history_reset_version));
|
||||
let history = session.conversation_history_snapshot().await;
|
||||
let authorization = ReviewAuthorization {
|
||||
user_message_revision: history.user_message_revision(),
|
||||
review_context_revision: history.guardian_review_context_revision(),
|
||||
root_history,
|
||||
root_authorization_version: root.as_ref().map(|root| root.authorization_version),
|
||||
root_review_context_revision: root
|
||||
.as_ref()
|
||||
.map(|root| root.review_context_revision),
|
||||
history_reset: self.history_reset.clone(),
|
||||
};
|
||||
drop(history);
|
||||
let started = Instant::now();
|
||||
tracing::info!(%review_id, "Guardian V3 review preparation started");
|
||||
let result = AttachedApproval::prepare(
|
||||
provider,
|
||||
authorization,
|
||||
&session,
|
||||
&context,
|
||||
deadline,
|
||||
cancellation,
|
||||
)
|
||||
.await;
|
||||
let result = if cancellation.is_cancelled() {
|
||||
Err(ReviewPreparationError::Cancelled)
|
||||
} else {
|
||||
result
|
||||
};
|
||||
tracing::info!(
|
||||
%review_id,
|
||||
ready = result.is_ok(),
|
||||
failure_reason = result.as_ref().err().map(tracing::field::display),
|
||||
preparation_ms = started.elapsed().as_millis() as u64,
|
||||
"Guardian V3 review preparation finished"
|
||||
);
|
||||
let prepared = result.map_err(|error| match error {
|
||||
ReviewPreparationError::Cancelled => ReviewDecision::Abort,
|
||||
_ => ReviewDecision::denied(format!(
|
||||
"Guardian V3 could not prepare the evidence for this action: {error}"
|
||||
)),
|
||||
})?;
|
||||
(
|
||||
codex_guardian_reviewer::ReviewReport::new(metadata),
|
||||
root_history,
|
||||
Some(prepared),
|
||||
)
|
||||
} else {
|
||||
let report = codex_guardian_reviewer::ReviewReport::new(metadata);
|
||||
let root_history = root
|
||||
.await
|
||||
.map(|root| (root.root_thread_id, root.history_reset_version));
|
||||
(report, root_history, None)
|
||||
};
|
||||
Ok((
|
||||
PreparedApproval {
|
||||
formatted_action: format_guardian_action_pretty(&request).ok(),
|
||||
request,
|
||||
category: self.request.category,
|
||||
context,
|
||||
root_history,
|
||||
attached,
|
||||
},
|
||||
report,
|
||||
))
|
||||
@@ -265,26 +329,40 @@ impl ReviewHost for super::super::runtime::ReviewRuntime {
|
||||
root_versions,
|
||||
});
|
||||
drop(history);
|
||||
let (outcome, analytics) = run_guardian_review_session_before_deadline(
|
||||
Arc::clone(&self.session),
|
||||
prepared.context.clone(),
|
||||
prepared.request.clone(),
|
||||
self.request.category,
|
||||
self.reasons.clone(),
|
||||
ReviewAttemptOptions {
|
||||
authorization: Some(ReviewAuthorization {
|
||||
user_message_revision,
|
||||
review_context_revision,
|
||||
root_history: prepared.root_history,
|
||||
root_authorization_version,
|
||||
root_review_context_revision,
|
||||
history_reset: self.history_reset.clone(),
|
||||
}),
|
||||
external_cancel: Some(cancellation.clone()),
|
||||
deadline,
|
||||
},
|
||||
)
|
||||
.await;
|
||||
let authorization = ReviewAuthorization {
|
||||
user_message_revision,
|
||||
review_context_revision,
|
||||
root_history: prepared.root_history,
|
||||
root_authorization_version,
|
||||
root_review_context_revision,
|
||||
history_reset: self.history_reset.clone(),
|
||||
};
|
||||
let (outcome, analytics) = if let Some(attached) = &prepared.attached {
|
||||
attached
|
||||
.attempt(
|
||||
session,
|
||||
prepared,
|
||||
&self.reasons,
|
||||
authorization,
|
||||
deadline,
|
||||
cancellation,
|
||||
)
|
||||
.await
|
||||
} else {
|
||||
run_guardian_review_session_before_deadline(
|
||||
Arc::clone(&self.session),
|
||||
prepared.context.clone(),
|
||||
prepared.request.clone(),
|
||||
prepared.category,
|
||||
self.reasons.clone(),
|
||||
ReviewAttemptOptions {
|
||||
authorization: Some(authorization),
|
||||
external_cancel: Some(cancellation.clone()),
|
||||
deadline,
|
||||
},
|
||||
)
|
||||
.await
|
||||
};
|
||||
|
||||
let review_evidence = match &outcome {
|
||||
GuardianReviewOutcome::Completed(_) => review_evidence,
|
||||
|
||||
@@ -97,6 +97,7 @@ use super::prompt::GuardianPromptMode;
|
||||
#[cfg(test)]
|
||||
use super::prompt::GuardianTranscriptCursor;
|
||||
use super::prompt::GuardianTranscriptHistory;
|
||||
use super::request_budget::ExhaustedReviewBudget;
|
||||
use super::review::guardian_review_session_config;
|
||||
pub(crate) use super::reviewer_config::build_guardian_review_session_config;
|
||||
use codex_guardian_reviewer::run_before_review_deadline;
|
||||
@@ -654,36 +655,14 @@ async fn run_review_on_session(
|
||||
state.transcript_history_version = reviewer_history_version;
|
||||
state.transcript_source = Some(transcript_source);
|
||||
}
|
||||
let budget_exhausted = review_session
|
||||
.session
|
||||
.services
|
||||
.thread_extension_data
|
||||
.remove::<super::request_budget::ExhaustedReviewBudget>();
|
||||
let result = match turn_result.outcome {
|
||||
GuardianReviewSessionOutcome::SessionFailed {
|
||||
error_info: Some(CodexErrorInfo::ContextWindowExceeded),
|
||||
..
|
||||
} if matches!(
|
||||
budget_exhausted.as_deref(),
|
||||
Some(super::request_budget::ExhaustedReviewBudget::Detected)
|
||||
) =>
|
||||
{
|
||||
GuardianReviewSessionOutcome::InputBudgetExceeded
|
||||
}
|
||||
result => result,
|
||||
};
|
||||
ReviewSessionResult {
|
||||
outcome: result,
|
||||
disposition: if budget_exhausted.is_some() {
|
||||
SessionDisposition::Discard
|
||||
} else {
|
||||
turn_result.disposition
|
||||
},
|
||||
outcome: turn_result.outcome,
|
||||
disposition: turn_result.disposition,
|
||||
analytics: analytics_result,
|
||||
}
|
||||
}
|
||||
|
||||
async fn ensure_guardian_followup_reminder(review_session: &GuardianReviewSession) {
|
||||
pub(super) async fn ensure_guardian_followup_reminder(review_session: &GuardianReviewSession) {
|
||||
let followup_reminder = GuardianFollowupReviewReminder
|
||||
.body()
|
||||
.into_text()
|
||||
@@ -712,7 +691,7 @@ async fn ensure_guardian_followup_reminder(review_session: &GuardianReviewSessio
|
||||
.await;
|
||||
}
|
||||
|
||||
async fn prepare_verdict_session(
|
||||
pub(super) async fn prepare_verdict_session(
|
||||
review_session: &GuardianReviewSession,
|
||||
context: &GuardianReviewContext,
|
||||
category: GuardianScope,
|
||||
@@ -783,6 +762,26 @@ async fn prepare_verdict_session(
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn finish_budgeted_verdict(
|
||||
mut turn: ReviewTurnResult,
|
||||
budget: Option<&ExhaustedReviewBudget>,
|
||||
) -> ReviewTurnResult {
|
||||
let Some(budget) = budget else { return turn };
|
||||
turn.disposition = SessionDisposition::Discard;
|
||||
if matches!(budget, ExhaustedReviewBudget::Detected)
|
||||
&& matches!(
|
||||
turn.outcome,
|
||||
GuardianReviewSessionOutcome::SessionFailed {
|
||||
error_info: Some(CodexErrorInfo::ContextWindowExceeded),
|
||||
..
|
||||
}
|
||||
)
|
||||
{
|
||||
turn.outcome = GuardianReviewSessionOutcome::InputBudgetExceeded;
|
||||
}
|
||||
turn
|
||||
}
|
||||
|
||||
/// Input for one verdict, with the evidence that replaces the prompt marker at admission.
|
||||
pub(super) struct PreparedVerdictTurn {
|
||||
pub(super) request: TurnInputRequest,
|
||||
@@ -821,7 +820,8 @@ impl GuardianReviewSession {
|
||||
}
|
||||
let result = wait_for_guardian_review(self, &id, deadline, cancel, analytics).await;
|
||||
observation.finish(result.turn_completed);
|
||||
Ok(result)
|
||||
let budget = store.remove::<ExhaustedReviewBudget>();
|
||||
Ok(finish_budgeted_verdict(result, budget.as_deref()))
|
||||
}
|
||||
.await;
|
||||
store.remove::<super::PendingReviewRouting>();
|
||||
|
||||
@@ -30,7 +30,7 @@ pub struct PreparedGuardianContext {
|
||||
}
|
||||
|
||||
impl PreparedGuardianContext {
|
||||
async fn prepare(
|
||||
pub(in crate::guardian) async fn prepare(
|
||||
parent: Arc<Session>,
|
||||
mut context: GuardianReviewContext,
|
||||
config: Config,
|
||||
|
||||
@@ -907,6 +907,49 @@ async fn wait_for_guardian_review_preserves_structured_session_error() {
|
||||
assert!(turn_completed);
|
||||
}
|
||||
|
||||
#[test_case::test_case(CodexErrorInfo::ContextWindowExceeded, Some(ExhaustedReviewBudget::Detected), true; "local exhaustion asks the user")]
|
||||
#[test_case::test_case(CodexErrorInfo::ContextWindowExceeded, None, false; "backend context limit denies")]
|
||||
#[test_case::test_case(CodexErrorInfo::ContextWindowExceeded, Some(ExhaustedReviewBudget::Compacting), false; "compaction failure denies")]
|
||||
#[test_case::test_case(CodexErrorInfo::ServerOverloaded, Some(ExhaustedReviewBudget::Detected), false; "unrelated server failure denies")]
|
||||
fn optional_sync_verdict_falls_back_only_for_local_input_exhaustion(
|
||||
error_info: CodexErrorInfo,
|
||||
budget: Option<ExhaustedReviewBudget>,
|
||||
expect_user_approval: bool,
|
||||
) {
|
||||
let turn = ReviewTurnResult {
|
||||
outcome: GuardianReviewSessionOutcome::SessionFailed {
|
||||
error: anyhow::anyhow!("review failed"),
|
||||
error_info: Some(error_info),
|
||||
retry_at: None,
|
||||
},
|
||||
disposition: SessionDisposition::Reusable,
|
||||
turn_completed: true,
|
||||
};
|
||||
let turn = finish_budgeted_verdict(turn, budget.as_ref());
|
||||
let completion = codex_guardian_reviewer::complete_review(
|
||||
turn.outcome.into(),
|
||||
&model_info_from_slug("active-model"),
|
||||
/*require_guardian*/ false,
|
||||
/*event*/ None,
|
||||
GuardianReviewAnalyticsResult::without_session(),
|
||||
);
|
||||
match completion.decision {
|
||||
None => assert!(expect_user_approval),
|
||||
Some(codex_protocol::protocol::ReviewDecision::Denied { .. }) => {
|
||||
assert!(!expect_user_approval)
|
||||
}
|
||||
other => panic!("unexpected review decision: {other:?}"),
|
||||
}
|
||||
assert_eq!(
|
||||
turn.disposition,
|
||||
if budget.is_some() {
|
||||
SessionDisposition::Discard
|
||||
} else {
|
||||
SessionDisposition::Reusable
|
||||
}
|
||||
);
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn wait_for_guardian_review_ignores_prior_turn_aborts() {
|
||||
let (review_session, tx_event, _rx_sub) = test_review_session().await;
|
||||
|
||||
@@ -83,6 +83,7 @@ pub(super) fn validate<'a>(
|
||||
|
||||
#[derive(Clone)]
|
||||
pub(super) struct ReviewRuntime {
|
||||
pub(super) attached_reviewer: Option<crate::guardian_review::BoundReviewer>,
|
||||
pub(super) session: Arc<Session>,
|
||||
pub(super) history_reset: CancellationToken,
|
||||
pub(super) context: GuardianReviewContext,
|
||||
|
||||
@@ -36,3 +36,51 @@ pub fn reviewer_headers(config: &crate::config::Config) -> crate::CodexResponses
|
||||
)]),
|
||||
}
|
||||
}
|
||||
|
||||
/// A thread's synchronous reviewer. Bind the evidence catch-up to the action before it waits.
|
||||
pub trait AttachedReviewer: Send + Sync {
|
||||
fn bind(self: std::sync::Arc<Self>) -> BoundReviewer;
|
||||
}
|
||||
|
||||
/// Evidence preparation bound to one action, shared by its review attempts.
|
||||
#[derive(Clone)]
|
||||
pub struct BoundReviewer {
|
||||
pub preparation: std::sync::Arc<dyn ReviewerPreparation>,
|
||||
pub retrieval: ReviewRetrieval,
|
||||
}
|
||||
|
||||
/// Safe to report with a review ID before a verdict has started.
|
||||
#[derive(Clone, Copy, Debug, PartialEq, Eq, thiserror::Error)]
|
||||
pub enum ReviewPreparationError {
|
||||
#[error("Guardian board evidence is unavailable")]
|
||||
EvidenceUnavailable,
|
||||
#[error("timed out preparing Guardian review")]
|
||||
PreparationTimedOut,
|
||||
#[error("reviewer is unavailable")]
|
||||
ReviewerUnavailable,
|
||||
#[error("evidence delivery failed")]
|
||||
DeliveryFailed,
|
||||
#[error("preparation was cancelled")]
|
||||
Cancelled,
|
||||
}
|
||||
|
||||
/// Reserve the actor's reviewer after routing and delivery have covered the bound action.
|
||||
pub trait ReviewerPreparation: Send + Sync {
|
||||
fn prepare<'a>(
|
||||
&'a self,
|
||||
setup: &'a PreparedGuardianContext,
|
||||
cancel: &'a tokio_util::sync::CancellationToken,
|
||||
) -> codex_extension_api::ExtensionFuture<
|
||||
'a,
|
||||
Result<Box<dyn PreparedAssessment>, ReviewPreparationError>,
|
||||
>;
|
||||
}
|
||||
|
||||
/// Holds the model session until the host finishes validating the original authority.
|
||||
/// Dropping an interrupted reservation discards its partially admitted conversation.
|
||||
pub trait PreparedAssessment: Send + Sync {
|
||||
fn runtime(&self) -> &GuardianReviewSession;
|
||||
/// Whether this reservation opened a reviewer or continued a retained one.
|
||||
fn session_kind(&self) -> codex_analytics::GuardianReviewSessionKind;
|
||||
fn finish(self: Box<Self>, disposition: codex_guardian_reviewer::SessionDisposition);
|
||||
}
|
||||
|
||||
@@ -34,7 +34,7 @@ struct GatedHost {
|
||||
}
|
||||
|
||||
impl ReviewHost for GatedHost {
|
||||
type Prepared = ();
|
||||
type Prepared = ReviewDeadline;
|
||||
type Evidence = ();
|
||||
|
||||
fn permissions(&self) -> Option<codex_guardian_context::PermissionContext> {
|
||||
@@ -50,11 +50,13 @@ impl ReviewHost for GatedHost {
|
||||
&self,
|
||||
approval_id: &str,
|
||||
reason: GuardianReviewReason,
|
||||
_: ReviewDeadline,
|
||||
deadline: ReviewDeadline,
|
||||
_: &CancellationToken,
|
||||
) -> Result<((), ReviewReport), ReviewDecision> {
|
||||
) -> Result<(Self::Prepared, ReviewReport), ReviewDecision> {
|
||||
// Make a fresh timeout at verdict time distinguishable from the original deadline.
|
||||
tokio::time::advance(std::time::Duration::from_secs(1)).await;
|
||||
Ok((
|
||||
(),
|
||||
deadline,
|
||||
ReviewReport::new(ReviewMetadata {
|
||||
thread_id: "thread".into(),
|
||||
turn_id: "turn".into(),
|
||||
@@ -80,14 +82,15 @@ impl ReviewHost for GatedHost {
|
||||
}
|
||||
async fn attempt(
|
||||
&self,
|
||||
_: &(),
|
||||
_: ReviewDeadline,
|
||||
preparation_deadline: &Self::Prepared,
|
||||
deadline: ReviewDeadline,
|
||||
cancellation: &CancellationToken,
|
||||
) -> (
|
||||
GuardianReviewOutcome,
|
||||
GuardianReviewAnalyticsResult,
|
||||
Option<()>,
|
||||
) {
|
||||
assert_eq!(deadline, *preparation_deadline);
|
||||
self.started.notify_one();
|
||||
cancellation.cancelled().await;
|
||||
self.cleaning.notify_one();
|
||||
@@ -102,12 +105,12 @@ impl ReviewHost for GatedHost {
|
||||
Some(()),
|
||||
)
|
||||
}
|
||||
async fn cached_approval_is_current(&self, _: &()) -> bool {
|
||||
async fn cached_approval_is_current(&self, _: &Self::Prepared) -> bool {
|
||||
self.validations.fetch_add(/*val*/ 1, Ordering::SeqCst);
|
||||
self.current
|
||||
}
|
||||
async fn emit(&self, _: EventMsg) {}
|
||||
async fn record_evidence(&self, _: &(), _: (), _: &GuardianAssessmentEvent) {
|
||||
async fn record_evidence(&self, _: &Self::Prepared, _: (), _: &GuardianAssessmentEvent) {
|
||||
self.evidence_recorded.store(true, Ordering::SeqCst);
|
||||
}
|
||||
async fn interrupt(&self, _: &str, _: EventMsg, _: ErrorEvent) {
|
||||
@@ -125,7 +128,7 @@ enum ApprovalState {
|
||||
#[test_case::test_case(ApprovalState::Current; "current action")]
|
||||
#[test_case::test_case(ApprovalState::Invalidated; "invalidated action")]
|
||||
#[test_case::test_case(ApprovalState::Cancelled; "cancelled action")]
|
||||
#[tokio::test]
|
||||
#[tokio::test(start_paused = true)]
|
||||
async fn async_approval_revalidates_without_waiting_for_tracked_cleanup(state: ApprovalState) {
|
||||
let host = GatedHost {
|
||||
started: Arc::new(Notify::new()),
|
||||
|
||||
Reference in New Issue
Block a user