mirror of
https://github.com/openai/codex.git
synced 2026-11-29 00:00:54 +00:00
guardian: own approval review routing in the extension
This commit is contained in:
@@ -7,6 +7,16 @@ use codex_protocol::protocol::AskForApproval;
|
||||
use codex_protocol::protocol::ReviewDecision;
|
||||
|
||||
use crate::ExtensionData;
|
||||
use crate::ExtensionFuture;
|
||||
|
||||
/// Request-scoped host capability that executes the configured approval review.
|
||||
///
|
||||
/// Contributors decide whether they own a request before invoking this runner.
|
||||
/// The host retains responsibility for the underlying review runtime while the
|
||||
/// contributor owns routing policy and the returned decision.
|
||||
pub trait ApprovalReviewRunner: Send + Sync {
|
||||
fn run(&self) -> ExtensionFuture<'_, Result<ApprovalReviewOutcome, ApprovalReviewError>>;
|
||||
}
|
||||
|
||||
/// Identifies the runtime that originated an approval-review request.
|
||||
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
|
||||
@@ -37,6 +47,7 @@ pub struct ApprovalReviewInput<'a> {
|
||||
pub approval_policy: &'a AskForApproval,
|
||||
pub retry_reason: Option<&'a str>,
|
||||
pub source: ApprovalReviewSource,
|
||||
pub runner: &'a dyn ApprovalReviewRunner,
|
||||
}
|
||||
|
||||
/// Result of offering an approval request to one extension contributor.
|
||||
|
||||
@@ -8,6 +8,7 @@ mod user_instructions;
|
||||
pub use approval_review::ApprovalReviewError;
|
||||
pub use approval_review::ApprovalReviewInput;
|
||||
pub use approval_review::ApprovalReviewOutcome;
|
||||
pub use approval_review::ApprovalReviewRunner;
|
||||
pub use approval_review::ApprovalReviewSource;
|
||||
pub use capabilities::AgentSpawnFuture;
|
||||
pub use capabilities::AgentSpawner;
|
||||
|
||||
@@ -5,6 +5,7 @@ use codex_extension_api::ApprovalReviewContributor;
|
||||
use codex_extension_api::ApprovalReviewError;
|
||||
use codex_extension_api::ApprovalReviewInput;
|
||||
use codex_extension_api::ApprovalReviewOutcome;
|
||||
use codex_extension_api::ApprovalReviewRunner;
|
||||
use codex_extension_api::ApprovalReviewSource;
|
||||
use codex_extension_api::ConfigContributor;
|
||||
use codex_extension_api::ContextContributor;
|
||||
@@ -260,6 +261,16 @@ struct RecordingApprovalContributor {
|
||||
calls: Arc<Mutex<Vec<ApprovalCall>>>,
|
||||
}
|
||||
|
||||
struct UnusedApprovalRunner;
|
||||
|
||||
impl ApprovalReviewRunner for UnusedApprovalRunner {
|
||||
fn run(&self) -> ExtensionFuture<'_, Result<ApprovalReviewOutcome, ApprovalReviewError>> {
|
||||
Box::pin(std::future::ready(Err(ApprovalReviewError::new(
|
||||
"test contributor should not invoke the runner",
|
||||
))))
|
||||
}
|
||||
}
|
||||
|
||||
impl ApprovalReviewContributor for RecordingApprovalContributor {
|
||||
fn review<'a>(
|
||||
&'a self,
|
||||
@@ -303,6 +314,7 @@ fn approval_review_input<'a>(
|
||||
action: &'a GuardianAssessmentAction,
|
||||
approval_policy: &'a AskForApproval,
|
||||
) -> ApprovalReviewInput<'a> {
|
||||
static RUNNER: UnusedApprovalRunner = UnusedApprovalRunner;
|
||||
ApprovalReviewInput {
|
||||
session_store,
|
||||
thread_store,
|
||||
@@ -316,6 +328,7 @@ fn approval_review_input<'a>(
|
||||
approval_policy,
|
||||
retry_reason: Some("initial review timed out"),
|
||||
source: ApprovalReviewSource::DelegatedSubagent,
|
||||
runner: &RUNNER,
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -3,11 +3,16 @@ use std::sync::Arc;
|
||||
use codex_core::config::Config;
|
||||
use codex_extension_api::AgentSpawnFuture;
|
||||
use codex_extension_api::AgentSpawner;
|
||||
use codex_extension_api::ApprovalReviewContributor;
|
||||
use codex_extension_api::ApprovalReviewInput;
|
||||
use codex_extension_api::ApprovalReviewOutcome;
|
||||
use codex_extension_api::ExtensionFuture;
|
||||
use codex_extension_api::ExtensionRegistryBuilder;
|
||||
use codex_extension_api::ThreadLifecycleContributor;
|
||||
use codex_extension_api::ThreadStartInput;
|
||||
use codex_protocol::ThreadId;
|
||||
use codex_protocol::config_types::ApprovalsReviewer;
|
||||
use codex_protocol::protocol::AskForApproval;
|
||||
|
||||
/// Guardian extension dependencies supplied by the host at construction time.
|
||||
#[derive(Clone, Debug)]
|
||||
@@ -68,10 +73,35 @@ where
|
||||
}
|
||||
}
|
||||
|
||||
impl<S> ApprovalReviewContributor for GuardianExtension<S>
|
||||
where
|
||||
S: Send + Sync,
|
||||
{
|
||||
fn review<'a>(
|
||||
&'a self,
|
||||
input: ApprovalReviewInput<'a>,
|
||||
) -> ExtensionFuture<'a, Result<ApprovalReviewOutcome, codex_extension_api::ApprovalReviewError>>
|
||||
{
|
||||
Box::pin(async move {
|
||||
if input.reviewer != ApprovalsReviewer::AutoReview
|
||||
|| !matches!(
|
||||
input.approval_policy,
|
||||
AskForApproval::OnRequest | AskForApproval::Granular(_)
|
||||
)
|
||||
{
|
||||
return Ok(ApprovalReviewOutcome::Abstain);
|
||||
}
|
||||
input.runner.run().await
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
/// Installs the guardian contributors into the extension registry.
|
||||
pub fn install<S>(registry: &mut ExtensionRegistryBuilder<Config>, agent_spawner: S)
|
||||
where
|
||||
S: Send + Sync + 'static,
|
||||
{
|
||||
registry.thread_lifecycle_contributor(Arc::new(GuardianExtension::new(agent_spawner)));
|
||||
let extension = Arc::new(GuardianExtension::new(agent_spawner));
|
||||
registry.thread_lifecycle_contributor(extension.clone());
|
||||
registry.approval_review_contributor(extension);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user