Preserve attached Guardian reviewer sessions across retries (#65328)

## Why

A transient failure during an approval review could discard the attached
reviewer session and lose context the reviewer had already gathered.

## What changed

Use the Guardian retry policy when finishing an attached review. Honor
the review's session disposition for retryable failures and force
discard for non-retryable errors.

## Testing

Extend the prewarmed root reviewer test to simulate a server overload
after the reviewer subscribes to a channel. Verify that the retry
reconnects with the same reviewer thread ID and carries forward the
subscription result.

GitOrigin-RevId: 5a538e872f71714fee9bbd97ed775969a6893154
This commit is contained in:
felixxia-oai
2026-12-02 20:19:42 +00:00
committed by copyberry
parent ea5f6b303c
commit 5592958044
4 changed files with 51 additions and 12 deletions
@@ -14,6 +14,7 @@ use codex_analytics::GuardianReviewSessionKind;
use codex_guardian_context::ComposedContext;
use codex_guardian_reviewer::GuardianReviewSessionOutcome;
use codex_guardian_reviewer::SessionDisposition;
use codex_guardian_reviewer::should_retry_guardian_review;
use codex_protocol::user_input::UserInput;
use tokio::sync::Mutex;
@@ -256,11 +257,14 @@ impl AttachedApproval {
)
.await;
let outcome = GuardianReviewOutcome::from(outcome);
model.finish(if matches!(outcome, GuardianReviewOutcome::Completed(_)) {
disposition
} else {
let disposition = if matches!(outcome, GuardianReviewOutcome::Error(_))
&& !should_retry_guardian_review(&outcome)
{
SessionDisposition::Discard
});
} else {
disposition
};
model.finish(disposition);
(outcome, analytics)
}
}
@@ -88,6 +88,7 @@ pub use outcome::GuardianReviewSessionOutcome;
pub use planned_action::build_guardian_planned_action;
pub use retry::GuardianReviewSessionLimits;
pub use retry::run_with_retry;
pub use retry::should_retry_guardian_review;
pub const MAX_REVIEW_ATTEMPTS: i64 = 3;
/// Default overall review budget when `auto_review.timeout_ms` is omitted.
+2 -1
View File
@@ -174,7 +174,8 @@ async fn wait_before_guardian_retry(
}
}
fn should_retry_guardian_review(outcome: &GuardianReviewOutcome) -> bool {
/// Whether a failed review is eligible for another attempt within the retry limits.
pub fn should_retry_guardian_review(outcome: &GuardianReviewOutcome) -> bool {
match outcome {
GuardianReviewOutcome::Error(
GuardianReviewError::Parse { .. } | GuardianReviewError::StaleAuthorization,
+40 -7
View File
@@ -1288,15 +1288,29 @@ async fn subscribed_evidence_reaches_the_retained_reviewer_when_it_becomes_idle(
}
#[tokio::test]
async fn first_approval_reuses_the_prewarmed_root_reviewer() -> anyhow::Result<()> {
async fn first_approval_and_retry_reuse_the_prewarmed_root_reviewer() -> anyhow::Result<()> {
core_test_support::skip_if_no_network!(Ok(()));
// Only Guardian uses this endpoint. Leave room for an unexpected child reviewer.
// The reviewer subscribes before a transient overload interrupts its verdict.
let warmup = vec![ev_response_created("warm"), ev_completed("warm")];
let subscribe = vec![
ev_function_call_with_namespace(
"follow",
"guardian_board",
"subscribe",
r#"{"channel_name":"release"}"#,
),
ev_completed("follow"),
];
let overload = vec![json!({
"type": "response.failed",
"response": {"error": {"code": "server_is_overloaded", "message": "temporary reviewer overload"}}
})];
let verdict = vec![
ev_assistant_message("allow", r#"{"outcome":"allow"}"#),
ev_completed("allow"),
];
let server = start_websocket_server(vec![vec![warmup, verdict]; 2]).await;
let server =
start_websocket_server(vec![vec![warmup, subscribe, overload], vec![verdict]]).await;
let fixture = reviewer_fixture(ChildKind::Named, Some(&server)).await?;
let warm = tokio::time::timeout(
Duration::from_secs(15),
@@ -1380,12 +1394,31 @@ async fn first_approval_reuses_the_prewarmed_root_reviewer() -> anyhow::Result<(
assert_eq!(grant["permissions"]["network"]["enabled"], true);
fixture.shutdown().await?;
// An eager child reviewer or a replacement root would open a second connection.
assert_eq!(server.handshakes().len(), 1);
let connections = server.connections();
let [_, review] = connections[0].as_slice() else {
anyhow::bail!("expected one warmup and one verdict");
let [first, reconnected] = connections.as_slice() else {
anyhow::bail!("expected one reconnect after the overload");
};
let [_, review, failed] = first.as_slice() else {
anyhow::bail!("expected warmup, subscription and failed verdict on the first connection");
};
let [retry] = reconnected.as_slice() else {
anyhow::bail!("expected a single retry on the new connection");
};
let retry = retry.body_json();
assert_eq!(retry["client_metadata"]["thread_id"], reviewer);
let failed = failed.body_json();
let subscription = failed["input"]
.as_array()
.expect("verdict input")
.iter()
.find(|item| item["type"] == "function_call_output" && item["call_id"] == "follow")
.expect("subscription result");
assert!(
retry["input"]
.as_array()
.expect("retry input")
.contains(subscription)
);
let review = review.body_json();
assert_eq!(review["text"]["format"]["type"], "json_schema");
assert_eq!(review["client_metadata"]["thread_id"], reviewer);