mirror of
https://github.com/openai/codex.git
synced 2026-11-16 06:54:36 +00:00
Reuse Guardian reviews in async risk scoring (#40013)
## What changed - Retain bounded evidence from completed synchronous Guardian allow and deny reviews and supply it to subsequent Guardian v2 async classifier samples as trusted developer context. - Keep review evidence isolated from the conversation transcript, escape and truncate its fields, and ignore failed or incomplete reviews. - Invalidate retained evidence after conversation history rewrites or new user messages, including authorization changes in a worker's root thread. ## Testing - Cover approved, denied, malformed, and forged review inputs, plus root rollback and authorization-change scenarios. GitOrigin-RevId: 27817e1fde9a136de727048c4220d148fcf72f42
This commit is contained in:
1 parent
ab8768306f
commit
9949c9eafa
16 files changed
+688
-74
No files matched your search
@@ -142,7 +142,8 @@ pub(crate) async fn build_guardian_prompt_items_with_parent_turn(
|
||||
.services
|
||||
.agent_control
|
||||
.root_user_authorization(session.thread_id)
|
||||
.await;
|
||||
.await
|
||||
.map(|snapshot| snapshot.messages);
|
||||
let transcript_entries = collect_guardian_transcript_entries(history.raw_items());
|
||||
let transcript_cursor = GuardianTranscriptCursor {
|
||||
parent_history_version: history.history_version(),
|
||||
|
||||
@@ -29,6 +29,8 @@ use tokio::time::Instant;
|
||||
use tokio::time::sleep_until;
|
||||
use tokio_util::sync::CancellationToken;
|
||||
|
||||
use crate::codex_thread::GuardianAuthorizationVersion;
|
||||
use crate::context::GuardianReviewEvidence;
|
||||
use crate::session::session::Session;
|
||||
use crate::session::turn_context::TurnContext;
|
||||
use crate::turn_timing::now_unix_timestamp_ms;
|
||||
@@ -44,6 +46,7 @@ use super::GuardianAssessmentOutcome;
|
||||
use super::GuardianRejectionCircuitBreakerAction;
|
||||
use super::GuardianRejectionCircuitBreakerPolicy;
|
||||
use super::GuardianReviewContext;
|
||||
use super::approval_request::format_guardian_action_pretty;
|
||||
use super::approval_request::guardian_approval_request_to_json;
|
||||
use super::approval_request::guardian_assessment_action;
|
||||
use super::approval_request::guardian_request_target_item_id;
|
||||
@@ -451,6 +454,32 @@ async fn run_guardian_review(
|
||||
|
||||
let schema = guardian_output_schema();
|
||||
let terminal_action = action_summary.clone();
|
||||
let review_evidence = if let Some(evidence) = session
|
||||
.services
|
||||
.thread_extension_data
|
||||
.get::<GuardianReviewEvidence>()
|
||||
{
|
||||
// Root rewrites and new user messages during this review make its evidence
|
||||
// stale even if it later completes against a newer prompt snapshot.
|
||||
let history = session.conversation_history_snapshot().await;
|
||||
let authorization_version = GuardianAuthorizationVersion::from_history(history.as_ref());
|
||||
let root_authorization_version = session
|
||||
.services
|
||||
.agent_control
|
||||
.root_user_authorization(session.thread_id)
|
||||
.await
|
||||
.map(|snapshot| snapshot.authorization_version);
|
||||
format_guardian_action_pretty(&request).ok().map(|action| {
|
||||
(
|
||||
evidence,
|
||||
action.text,
|
||||
authorization_version,
|
||||
root_authorization_version,
|
||||
)
|
||||
})
|
||||
} else {
|
||||
None
|
||||
};
|
||||
let (outcome, analytics_result) = Box::pin(run_guardian_review_session_with_retry(
|
||||
session.clone(),
|
||||
context,
|
||||
@@ -463,6 +492,7 @@ async fn run_guardian_review(
|
||||
.await;
|
||||
|
||||
let completed_at_ms = now_unix_timestamp_ms();
|
||||
let completed_review = matches!(&outcome, GuardianReviewOutcome::Completed(_));
|
||||
let (assessment, count_denial_for_circuit_breaker) = match outcome {
|
||||
GuardianReviewOutcome::Completed(assessment) => {
|
||||
let approved = matches!(assessment.outcome, GuardianAssessmentOutcome::Allow);
|
||||
@@ -645,24 +675,36 @@ async fn run_guardian_review(
|
||||
} else {
|
||||
GuardianAssessmentStatus::Denied
|
||||
};
|
||||
let assessment_event = GuardianAssessmentEvent {
|
||||
id: review_id,
|
||||
target_item_id,
|
||||
plugin_id: plugin_id.clone(),
|
||||
script_path: script_path.clone(),
|
||||
turn_id: assessment_turn_id.clone(),
|
||||
started_at_ms,
|
||||
completed_at_ms: Some(completed_at_ms),
|
||||
status,
|
||||
risk_level: Some(assessment.risk_level),
|
||||
user_authorization: Some(assessment.user_authorization),
|
||||
rationale: Some(assessment.rationale.clone()),
|
||||
decision_source: Some(GuardianAssessmentDecisionSource::Agent),
|
||||
action: terminal_action,
|
||||
};
|
||||
if completed_review
|
||||
&& let Some((evidence, action, authorization_version, root_authorization_version)) =
|
||||
review_evidence
|
||||
{
|
||||
evidence.record(
|
||||
&assessment_event,
|
||||
&action,
|
||||
authorization_version,
|
||||
root_authorization_version,
|
||||
);
|
||||
}
|
||||
session
|
||||
.send_event(
|
||||
turn.as_ref(),
|
||||
EventMsg::GuardianAssessment(GuardianAssessmentEvent {
|
||||
id: review_id,
|
||||
target_item_id,
|
||||
plugin_id: plugin_id.clone(),
|
||||
script_path: script_path.clone(),
|
||||
turn_id: assessment_turn_id.clone(),
|
||||
started_at_ms,
|
||||
completed_at_ms: Some(completed_at_ms),
|
||||
status,
|
||||
risk_level: Some(assessment.risk_level),
|
||||
user_authorization: Some(assessment.user_authorization),
|
||||
rationale: Some(assessment.rationale.clone()),
|
||||
decision_source: Some(GuardianAssessmentDecisionSource::Agent),
|
||||
action: terminal_action,
|
||||
}),
|
||||
EventMsg::GuardianAssessment(assessment_event),
|
||||
)
|
||||
.await;
|
||||
|
||||
|
||||
Reference in new issue
Block a user