mirror of
https://github.com/openai/codex.git
synced 2026-11-27 23:31:09 +00:00
Replace guardian review result tuples with named structs (#45985)
## What changed Introduce `ReviewTurnResult` and `ReviewSessionResult` to carry review outcomes, session disposition, and completion or analytics data. Propagate `SessionDisposition` directly through review execution and pooling, replacing boolean reuse flags and positional tuple access while preserving existing behavior. Update existing review-session tests to assert the named fields and explicit session dispositions. GitOrigin-RevId: 93eea8b482cd592167956444042becc1ef5f2eb5
This commit is contained in:
1 parent
49305d74b4
commit
66dfadbe66
6 files changed
+192
-113
No files matched your search
@@ -15,6 +15,7 @@ use tokio::time::Instant;
|
||||
use tokio_util::sync::CancellationToken;
|
||||
|
||||
use crate::GuardianReviewSessionOutcome;
|
||||
use crate::SessionDisposition;
|
||||
|
||||
const GUARDIAN_INTERRUPT_DRAIN_TIMEOUT: Duration = Duration::from_secs(5);
|
||||
|
||||
@@ -54,13 +55,21 @@ pub async fn start_review_turn(
|
||||
}
|
||||
}
|
||||
|
||||
/// A submitted turn's terminal outcome and the state left behind for reuse and accounting.
|
||||
pub struct ReviewTurnResult {
|
||||
pub outcome: GuardianReviewSessionOutcome,
|
||||
pub disposition: SessionDisposition,
|
||||
/// A matching TurnComplete event supplied authoritative usage for this turn.
|
||||
pub turn_completed: bool,
|
||||
}
|
||||
|
||||
pub async fn wait_for_guardian_review(
|
||||
runtime: &impl ReviewerRuntime,
|
||||
expected_turn_id: &str,
|
||||
deadline: tokio::time::Instant,
|
||||
external_cancel: Option<&CancellationToken>,
|
||||
analytics_result: &mut GuardianReviewAnalyticsResult,
|
||||
) -> (GuardianReviewSessionOutcome, bool, bool) {
|
||||
) -> ReviewTurnResult {
|
||||
let timeout = tokio::time::sleep_until(deadline);
|
||||
tokio::pin!(timeout);
|
||||
let mut last_error: Option<ErrorEvent> = None;
|
||||
@@ -68,13 +77,16 @@ pub async fn wait_for_guardian_review(
|
||||
loop {
|
||||
tokio::select! {
|
||||
_ = &mut timeout => {
|
||||
let keep_review_session = interrupt_and_drain_turn(
|
||||
runtime,
|
||||
expected_turn_id,
|
||||
)
|
||||
.await
|
||||
.is_ok();
|
||||
return (GuardianReviewSessionOutcome::TimedOut, keep_review_session, false);
|
||||
let disposition = if interrupt_and_drain_turn(runtime, expected_turn_id).await.is_ok() {
|
||||
SessionDisposition::Reusable
|
||||
} else {
|
||||
SessionDisposition::Discard
|
||||
};
|
||||
return ReviewTurnResult {
|
||||
outcome: GuardianReviewSessionOutcome::TimedOut,
|
||||
disposition,
|
||||
turn_completed: false,
|
||||
};
|
||||
}
|
||||
_ = async {
|
||||
if let Some(cancel_token) = external_cancel {
|
||||
@@ -83,13 +95,16 @@ pub async fn wait_for_guardian_review(
|
||||
std::future::pending::<()>().await;
|
||||
}
|
||||
} => {
|
||||
let keep_review_session = interrupt_and_drain_turn(
|
||||
runtime,
|
||||
expected_turn_id,
|
||||
)
|
||||
.await
|
||||
.is_ok();
|
||||
return (GuardianReviewSessionOutcome::Aborted, keep_review_session, false);
|
||||
let disposition = if interrupt_and_drain_turn(runtime, expected_turn_id).await.is_ok() {
|
||||
SessionDisposition::Reusable
|
||||
} else {
|
||||
SessionDisposition::Discard
|
||||
};
|
||||
return ReviewTurnResult {
|
||||
outcome: GuardianReviewSessionOutcome::Aborted,
|
||||
disposition,
|
||||
turn_completed: false,
|
||||
};
|
||||
}
|
||||
event = runtime.next_event() => {
|
||||
match event {
|
||||
@@ -105,36 +120,40 @@ pub async fn wait_for_guardian_review(
|
||||
if turn_complete.last_agent_message.is_none()
|
||||
&& let Some(error) = last_error
|
||||
{
|
||||
return (
|
||||
GuardianReviewSessionOutcome::SessionFailed {
|
||||
return ReviewTurnResult {
|
||||
outcome: GuardianReviewSessionOutcome::SessionFailed {
|
||||
error: anyhow!(error.message),
|
||||
error_info: error.codex_error_info,
|
||||
retry_at: runtime.retry_at(expected_turn_id),
|
||||
},
|
||||
true,
|
||||
true,
|
||||
);
|
||||
disposition: SessionDisposition::Reusable,
|
||||
turn_completed: true,
|
||||
};
|
||||
}
|
||||
return (
|
||||
GuardianReviewSessionOutcome::Completed(Ok(turn_complete.last_agent_message)),
|
||||
true,
|
||||
true,
|
||||
);
|
||||
return ReviewTurnResult {
|
||||
outcome: GuardianReviewSessionOutcome::Completed(Ok(turn_complete.last_agent_message)),
|
||||
disposition: SessionDisposition::Reusable,
|
||||
turn_completed: true,
|
||||
};
|
||||
}
|
||||
EventMsg::Error(error) => {
|
||||
last_error = Some(error);
|
||||
}
|
||||
EventMsg::TurnAborted(_) => {
|
||||
return (GuardianReviewSessionOutcome::Aborted, true, false);
|
||||
return ReviewTurnResult {
|
||||
outcome: GuardianReviewSessionOutcome::Aborted,
|
||||
disposition: SessionDisposition::Reusable,
|
||||
turn_completed: false,
|
||||
};
|
||||
}
|
||||
_ => {}
|
||||
},
|
||||
Err(err) => {
|
||||
return (
|
||||
GuardianReviewSessionOutcome::Completed(Err(err)),
|
||||
false,
|
||||
false,
|
||||
);
|
||||
return ReviewTurnResult {
|
||||
outcome: GuardianReviewSessionOutcome::Completed(Err(err)),
|
||||
disposition: SessionDisposition::Discard,
|
||||
turn_completed: false,
|
||||
};
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -41,6 +41,7 @@ pub const MAX_REVIEW_ATTEMPTS: i64 = 3;
|
||||
pub const REVIEW_TIMEOUT: std::time::Duration = std::time::Duration::from_secs(90);
|
||||
|
||||
pub use deadline::run_before_review_deadline;
|
||||
pub use pool::ReviewSessionResult;
|
||||
pub use pool::ReviewerPool;
|
||||
pub use pool::ReviewerRequest;
|
||||
pub use pool::ReviewerSession;
|
||||
@@ -53,6 +54,7 @@ pub use completion::ReviewCompletion;
|
||||
pub use completion::complete_review;
|
||||
pub use completion::guardian_timeout_message;
|
||||
|
||||
pub use execution::ReviewTurnResult;
|
||||
pub use execution::ReviewerRuntime;
|
||||
pub use execution::start_review_turn;
|
||||
pub use execution::wait_for_guardian_review;
|
||||
|
||||
@@ -54,13 +54,14 @@ pub trait ReviewerRequest: Send + Sync {
|
||||
&self,
|
||||
session: &Self::Session,
|
||||
kind: GuardianReviewSessionKind,
|
||||
) -> impl Future<
|
||||
Output = (
|
||||
GuardianReviewSessionOutcome,
|
||||
SessionDisposition,
|
||||
GuardianReviewAnalyticsResult,
|
||||
),
|
||||
> + Send;
|
||||
) -> impl Future<Output = ReviewSessionResult> + Send;
|
||||
}
|
||||
|
||||
/// Result of one review, including whether its session can serve the next request.
|
||||
pub struct ReviewSessionResult {
|
||||
pub outcome: GuardianReviewSessionOutcome,
|
||||
pub disposition: SessionDisposition,
|
||||
pub analytics: GuardianReviewAnalyticsResult,
|
||||
}
|
||||
|
||||
/// Whether the host drained the session sufficiently for another review to use it.
|
||||
@@ -259,7 +260,11 @@ impl<S: ReviewerSession> ReviewerPool<S> {
|
||||
};
|
||||
// Dropping a review before it drains its turn must not leave a reusable agent.
|
||||
let review_lifetime = trunk.cancellation.clone().drop_guard();
|
||||
let (outcome, disposition, analytics) = request.run(&trunk.session, kind).await;
|
||||
let ReviewSessionResult {
|
||||
outcome,
|
||||
disposition,
|
||||
analytics,
|
||||
} = request.run(&trunk.session, kind).await;
|
||||
if disposition == SessionDisposition::Reusable
|
||||
&& matches!(outcome, GuardianReviewSessionOutcome::Completed(_))
|
||||
{
|
||||
@@ -316,7 +321,9 @@ impl<S: ReviewerSession> ReviewerPool<S> {
|
||||
}
|
||||
Err(outcome) => return (outcome, GuardianReviewAnalyticsResult::without_session()),
|
||||
};
|
||||
let (outcome, _, analytics) = request
|
||||
let ReviewSessionResult {
|
||||
outcome, analytics, ..
|
||||
} = request
|
||||
.run(&session, GuardianReviewSessionKind::EphemeralForked)
|
||||
.await;
|
||||
(outcome, analytics)
|
||||
|
||||
Reference in new issue
Block a user