Riapplica la fase 5 di aKode (86e7c72e) sulla base ripulita:
- crate codex-* -> akode-*, codex-rs -> akode-rs, codex-cli -> akode-cli, binario e ausiliari akode;
- ~/.codex -> ~/.akode, CODEX_HOME -> AKODE_HOME, variabili CODEX_* -> AKODE_*, .codex/ -> .akode/ di progetto;
nessun fallback a ~/.codex o a CODEX_*;
- testi per l'utente "Codex" -> "aKode"; testi verso il modello e istruzioni di sistema invariati;
- snapshot insta rinominati (non ancora rigenerati).
I conflitti con la base ripulita sono stati risolti riapplicando la rinomina ai file senza le
funzioni di imeAI (script di mappa dei token per file).
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
196 lines
7.5 KiB
Rust
196 lines
7.5 KiB
Rust
//! Resolves approval contributors and the synchronous fallback in one place.
|
|
//! Cached approvals still validate the bound action before recording their outcome.
|
|
|
|
use crate::ReviewDenials;
|
|
use crate::ReviewHost;
|
|
use akode_extension_api::ApprovalDecision;
|
|
use akode_extension_api::ApprovalDecisionInput;
|
|
use akode_extension_api::ExtensionData;
|
|
use akode_extension_api::ExtensionMetrics;
|
|
use akode_extension_api::ExtensionRegistry;
|
|
use akode_extension_api::SynchronousApprovalReviewer;
|
|
use akode_otel::SessionTelemetry;
|
|
use akode_protocol::ThreadId;
|
|
use akode_protocol::approvals::GuardianReviewReason;
|
|
use akode_protocol::config_types::ApprovalsReviewer;
|
|
use akode_protocol::openai_models::GuardianScope;
|
|
use akode_protocol::openai_models::ModelInfo;
|
|
use akode_protocol::protocol::AskForApproval;
|
|
use akode_protocol::protocol::ReviewDecision;
|
|
use std::sync::Arc;
|
|
use tokio_util::sync::CancellationToken;
|
|
|
|
/// One captured action and its host-enforced constraints. Guardian derives the
|
|
/// contributor input and owns cancellation for the complete review operation.
|
|
pub struct ReviewRequest<'a, H> {
|
|
/// Final serving value retained by the host for this action.
|
|
pub serving_assessment: Option<bool>,
|
|
pub host: H,
|
|
pub approval_id: &'a str,
|
|
pub tool_call_id: Option<&'a str>,
|
|
/// Absent when the host could not render the action for review.
|
|
pub action: Option<&'a serde_json::Value>,
|
|
pub thread_id: ThreadId,
|
|
pub thread_store: &'a ExtensionData,
|
|
pub turn_store: &'a ExtensionData,
|
|
pub category: GuardianScope,
|
|
pub approval_policy: AskForApproval,
|
|
pub approvals_reviewer: ApprovalsReviewer,
|
|
pub require_guardian: bool,
|
|
pub require_synchronous_review: bool,
|
|
pub model_requires_review: bool,
|
|
pub async_enabled: bool,
|
|
pub retried: bool,
|
|
pub escalated_exec: bool,
|
|
pub full_access: bool,
|
|
pub cancellation: CancellationToken,
|
|
/// Overall review budget; None disables only the review deadline.
|
|
pub timeout: Option<std::time::Duration>,
|
|
pub model: &'a ModelInfo,
|
|
pub telemetry: &'a SessionTelemetry,
|
|
pub metrics: Option<Arc<dyn ExtensionMetrics>>,
|
|
}
|
|
|
|
pub fn routes_approval_policy_to_guardian(
|
|
policy: AskForApproval,
|
|
reviewer: ApprovalsReviewer,
|
|
) -> bool {
|
|
matches!(
|
|
policy,
|
|
AskForApproval::OnRequest | AskForApproval::Granular(_)
|
|
) && reviewer == ApprovalsReviewer::AutoReview
|
|
}
|
|
|
|
impl<H: ReviewHost> ReviewRequest<'_, H> {
|
|
pub async fn decide<C: Sync>(
|
|
mut self,
|
|
registry: &ExtensionRegistry<C>,
|
|
) -> Option<ReviewDecision> {
|
|
// Include fast decisions, preparation and cancellation, not just model review time.
|
|
let _decision_timer = self
|
|
.telemetry
|
|
.start_timer("codex.guardian.decision.duration_ms", &[])
|
|
.ok();
|
|
let _blocking = self
|
|
.thread_store
|
|
.get::<crate::ReviewBlocking>()
|
|
.map(|blocking| blocking.enter());
|
|
let runtime = self.thread_store.get::<crate::ReviewerTasks>();
|
|
let _task = runtime.as_ref().map(|runtime| runtime.tasks.token());
|
|
if runtime
|
|
.as_ref()
|
|
.is_some_and(|runtime| runtime.cancellation.is_cancelled())
|
|
{
|
|
return Some(ReviewDecision::Abort);
|
|
}
|
|
let cancellation = self.cancellation.child_token();
|
|
let _cancel_on_drop = cancellation.clone().drop_guard();
|
|
self.cancellation = cancellation.clone();
|
|
let decision = async {
|
|
if self.full_access {
|
|
return Some(match self.host.validate_action() {
|
|
Ok(_) => ReviewDecision::Approved,
|
|
Err(decision) => decision,
|
|
});
|
|
}
|
|
let Some(action) = self.action else {
|
|
return Some(ReviewDecision::denied(
|
|
"automatic approval review could not prepare the action",
|
|
));
|
|
};
|
|
let permissions = self.host.permissions();
|
|
let input = ApprovalDecisionInput {
|
|
serving_assessment: self.serving_assessment,
|
|
permissions: permissions.as_ref(),
|
|
approval_id: self.approval_id,
|
|
tool_call_id: self.tool_call_id,
|
|
action,
|
|
thread_id: self.thread_id,
|
|
thread_store: self.thread_store,
|
|
turn_store: self.turn_store,
|
|
category: self.category,
|
|
approval_policy: self.approval_policy,
|
|
approvals_reviewer: self.approvals_reviewer,
|
|
require_guardian: self.require_guardian,
|
|
require_fresh_review: self.require_synchronous_review
|
|
|| self.model_requires_review && !self.async_enabled
|
|
|| self.cancellation.is_cancelled()
|
|
|| self.retried
|
|
|| self.escalated_exec,
|
|
full_access: self.full_access,
|
|
metrics: self.metrics.clone(),
|
|
synchronous_reviewer: &self,
|
|
};
|
|
match registry.decide_approval(&input).await {
|
|
Some(ApprovalDecision::Reviewed(decision)) => Some(decision),
|
|
Some(ApprovalDecision::Allow) if !input.require_fresh_review => {
|
|
Some(self.cached_approval().await)
|
|
}
|
|
Some(ApprovalDecision::Allow) => {
|
|
self.review(
|
|
GuardianReviewReason::FreshRequired,
|
|
/*async_approval*/ None,
|
|
)
|
|
.await
|
|
}
|
|
Some(ApprovalDecision::AskUser) if !self.require_guardian => None,
|
|
None if !self.require_guardian
|
|
&& !routes_approval_policy_to_guardian(
|
|
self.approval_policy,
|
|
self.approvals_reviewer,
|
|
) =>
|
|
{
|
|
None
|
|
}
|
|
None | Some(ApprovalDecision::AskUser) => {
|
|
self.review(GuardianReviewReason::Policy, /*async_approval*/ None)
|
|
.await
|
|
}
|
|
}
|
|
};
|
|
tokio::pin!(decision);
|
|
let result = if let Some(runtime) = runtime.as_ref() {
|
|
tokio::select! {
|
|
biased;
|
|
_ = runtime.cancellation.cancelled() => {
|
|
cancellation.cancel();
|
|
// Drive cancellation through reporting and agent cleanup before releasing
|
|
// the tracked operation. Parent stop joins this work before closing history.
|
|
decision.await
|
|
}
|
|
result = &mut decision => result,
|
|
}
|
|
} else {
|
|
decision.await
|
|
};
|
|
if cancellation.is_cancelled()
|
|
|| runtime
|
|
.as_ref()
|
|
.is_some_and(|runtime| runtime.cancellation.is_cancelled())
|
|
{
|
|
Some(ReviewDecision::Abort)
|
|
} else {
|
|
result
|
|
}
|
|
}
|
|
|
|
pub(super) async fn cached_approval(&self) -> ReviewDecision {
|
|
if let Err(decision) = self.host.validate_action() {
|
|
return decision;
|
|
}
|
|
if self.cancellation.is_cancelled() {
|
|
return ReviewDecision::Abort;
|
|
}
|
|
if let Some(turn_id) = self.host.servicing_turn().await {
|
|
ReviewDenials::for_thread(self.thread_store)
|
|
.record_non_denial(&turn_id)
|
|
.await;
|
|
}
|
|
ReviewDecision::Approved
|
|
}
|
|
}
|
|
|
|
#[cfg(test)]
|
|
#[path = "routing_tests.rs"]
|
|
mod tests;
|