Prevent later Guardian scores from releasing earlier pending reviews (#51400)

## Why

A low-risk score for a later action could release an earlier pending Guardian review when the thread's score lag reached zero, bypassing that action's synchronous review result.

## What changed

Track the accepted score's source action and allow it to release a pending review only when it comes from that action or an earlier one. Keep cached-score approval available before synchronous review starts, but prevent the initial computer-use allowance from releasing an already pending review.

## Testing

Add an app-server regression test with two pending reviews: a later low-risk score approves its own action while the earlier action remains pending and honors its synchronous denial.

GitOrigin-RevId: ed8b9c4793b2538f634edfc5699797a6983358cc
This commit is contained in:
jif
2026-10-06 15:43:56 +00:00
committed by copyberry
parent 57d57df608
commit a4ebc509f4
5 changed files with 210 additions and 55 deletions
@@ -105,7 +105,7 @@ impl GuardianApprovalReviewer {
match cached_evidence(thread, input, config, &policy, input.metrics.as_deref())
.await
{
Ok(()) => return ApprovalDecision::Allow,
Ok(_) => return ApprovalDecision::Allow,
Err(reason) => reason,
}
}
@@ -133,9 +133,8 @@ impl GuardianApprovalReviewer {
// Subscribe before checking so a concurrently published score is not lost.
let mut updates = progress.updates.subscribe();
loop {
if cached_evidence(thread, input, config, &policy, /*metrics*/ None)
.await
.is_ok()
if let Ok(true) =
cached_evidence(thread, input, config, &policy, /*metrics*/ None).await
{
return;
}
@@ -157,13 +156,14 @@ impl GuardianApprovalReviewer {
}
}
/// Checks score reuse; the success value says whether it can also release a pending review.
async fn cached_evidence(
thread: &CodexThread,
input: &ApprovalDecisionInput<'_>,
config: &GuardianV2Config,
policy: &GuardianModelPolicy,
metrics: Option<&dyn ExtensionMetrics>,
) -> Result<(), GuardianReviewReason> {
) -> Result<bool, GuardianReviewReason> {
let store = input.thread_store;
let Some(progress) = store.get::<GuardianV2ScoreProgress>() else {
record_fast_decision(metrics, "deferred", "missing_score");
@@ -217,7 +217,7 @@ async fn cached_evidence(
&& cached.js_executions == 1
{
record_fast_decision(metrics, "approved", "initial_cua_call");
return Ok(());
return Ok(false);
}
let Some(permissions) = input.permissions else {
record_fast_decision(metrics, "deferred", "permission_resolution_error");
@@ -268,7 +268,7 @@ async fn cached_evidence(
)
} else {
record_fast_decision(metrics, "approved", "low_risk");
return Ok(());
return Ok(cached.score_at_or_before_action);
}
}
Some(score) if score >= config.review_threshold => {
@@ -25,6 +25,7 @@ pub(super) struct GuardianV2ScoreProgress {
#[derive(Default)]
struct ScoreState {
score: Option<SecurityRiskScore>,
score_index: usize,
wrapper_lag: WrapperLag,
latest_tool_call: usize,
js_executions: usize,
@@ -38,6 +39,7 @@ struct ScoreState {
#[derive(Debug, PartialEq)]
pub(super) struct CachedScore {
pub(super) lag: usize,
pub(super) score_at_or_before_action: bool,
pub(super) has_unscored_failure: bool,
pub(super) js_executions: usize,
pub(super) oversized: bool,
@@ -115,6 +117,7 @@ impl GuardianV2ScoreProgress {
.is_none_or(|previous| previous.sampled_at < score.sampled_at);
if accepted {
state.score = Some(score);
state.score_index = index;
state.authorization = Some(authorization);
state.latest_scored_tool_call = state.latest_scored_tool_call.max(index);
self.updates.send_replace(());
@@ -156,6 +159,11 @@ impl GuardianV2ScoreProgress {
.lock()
.unwrap_or_else(std::sync::PoisonError::into_inner);
CachedScore {
// Use the accepted score's source, not the coverage high-water mark.
score_at_or_before_action: state
.wrapper_lag
.get(call_id)
.is_some_and(|start| state.score_index <= start.index),
lag: state
.latest_tool_call
.saturating_sub(state.latest_scored_tool_call)
@@ -1,5 +1,5 @@
//! Discounts only an approval's own unscored code-mode wrapper. Missing provenance
//! keeps the full lag; the bounded history never changes scoring or failure order.
//! Tracks action order and discounts an approval's own unscored code-mode wrapper.
//! Missing provenance keeps the full lag; bounded history never changes scoring or failure order.
//! The score owner's state lock protects this history.
use std::collections::VecDeque;
@@ -9,9 +9,9 @@ use codex_extension_api::ToolPayload;
use codex_extension_api::ToolStartInput;
use codex_protocol::ResponseItemId;
struct ToolStart {
pub(super) struct ToolStart {
call_id: String,
index: usize,
pub(super) index: usize,
wrapper_item_id: Option<ResponseItemId>,
parent_wrapper_index: Option<usize>,
}
@@ -54,16 +54,18 @@ impl WrapperLag {
});
}
pub(super) fn get(&self, call_id: Option<&str>) -> Option<&ToolStart> {
let call_id = call_id?;
self.starts
.iter()
.rev()
.find(|start| start.call_id == call_id)
}
pub(super) fn discount(&self, call_id: Option<&str>, latest_scored: usize) -> usize {
usize::from(
call_id
.and_then(|call_id| {
self.starts
.iter()
.rev()
.find(|start| start.call_id == call_id)
.and_then(|start| start.parent_wrapper_index)
})
self.get(call_id)
.and_then(|start| start.parent_wrapper_index)
.is_some_and(|parent| parent > latest_scored),
)
}