166 lines
5.9 KiB
Rust
166 lines
5.9 KiB
Rust
//! Owns review reporting and the per-thread denial window. The host publishes
|
|
//! events and enforces interruption only on the still-active owning turn.
|
|
//! Internal accounting runs even when no client event is available. Review
|
|
//! completion captures one monotonic duration for latency reporting while retaining
|
|
//! the actual wall-clock completion timestamp for event ordering.
|
|
|
|
use std::sync::Arc;
|
|
use std::time::Duration;
|
|
use std::time::Instant;
|
|
|
|
use crate::GuardianApprovalRequestSource;
|
|
use crate::GuardianReviewDetails;
|
|
use crate::GuardianReviewedAction;
|
|
use akode_extension_api::ExtensionData;
|
|
use akode_otel::SessionTelemetry;
|
|
use akode_protocol::approvals::GuardianAssessmentAction;
|
|
use akode_protocol::approvals::GuardianReviewReason;
|
|
use akode_protocol::items::ModelInvocationContext;
|
|
use akode_protocol::openai_models::ModelInfo;
|
|
use akode_protocol::protocol::GuardianAssessmentDecisionSource;
|
|
use akode_protocol::protocol::GuardianAssessmentEvent;
|
|
use akode_protocol::protocol::GuardianAssessmentStatus;
|
|
use tokio::sync::Mutex;
|
|
|
|
use crate::GuardianRejectionCircuitBreaker;
|
|
use crate::GuardianRejectionCircuitBreakerAction;
|
|
use crate::GuardianReviewOutcome;
|
|
use crate::ReviewCompletion;
|
|
|
|
/// Captured action attribution used for both the started and terminal report.
|
|
pub struct ReviewMetadata {
|
|
pub thread_id: String,
|
|
pub turn_id: String,
|
|
pub review_id: String,
|
|
pub target_item_id: Option<String>,
|
|
pub plugin_id: Option<String>,
|
|
pub script_path: Option<String>,
|
|
pub approval_request_source: GuardianApprovalRequestSource,
|
|
pub reviewed_action: GuardianReviewedAction,
|
|
/// Absent when the action has no client-facing representation.
|
|
pub action: Option<GuardianAssessmentAction>,
|
|
pub review_reason: GuardianReviewReason,
|
|
pub model_context: ModelInvocationContext,
|
|
/// Configured review budget; None is reported as zero (unlimited).
|
|
pub timeout: Option<Duration>,
|
|
}
|
|
|
|
pub struct ReviewReport {
|
|
started: Option<GuardianAssessmentEvent>,
|
|
started_instant: Instant,
|
|
approval_request_source: GuardianApprovalRequestSource,
|
|
reviewed_action: GuardianReviewedAction,
|
|
}
|
|
|
|
impl ReviewReport {
|
|
pub fn new(metadata: ReviewMetadata) -> Self {
|
|
let started_instant = Instant::now();
|
|
let started_at_ms = crate::now_unix_millis();
|
|
let started = metadata.action.map(|action| GuardianAssessmentEvent {
|
|
review_reason: Some(metadata.review_reason),
|
|
model_context: Some(metadata.model_context),
|
|
id: metadata.review_id,
|
|
target_item_id: metadata.target_item_id,
|
|
plugin_id: metadata.plugin_id,
|
|
script_path: metadata.script_path,
|
|
turn_id: metadata.turn_id,
|
|
started_at_ms: started_at_ms.try_into().unwrap_or_default(),
|
|
completed_at_ms: None,
|
|
status: GuardianAssessmentStatus::InProgress,
|
|
risk_level: None,
|
|
user_authorization: None,
|
|
rationale: None,
|
|
decision_source: None,
|
|
action,
|
|
});
|
|
Self {
|
|
started,
|
|
started_instant,
|
|
approval_request_source: metadata.approval_request_source,
|
|
reviewed_action: metadata.reviewed_action,
|
|
}
|
|
}
|
|
|
|
pub fn started_event(&self) -> Option<GuardianAssessmentEvent> {
|
|
self.started.clone()
|
|
}
|
|
|
|
/// Completes the report and captures the elapsed duration used for local metrics.
|
|
pub fn complete(
|
|
&self,
|
|
outcome: GuardianReviewOutcome,
|
|
model: &ModelInfo,
|
|
require_guardian: bool,
|
|
details: GuardianReviewDetails,
|
|
completed_at_ms: i64,
|
|
) -> (ReviewCompletion, Duration) {
|
|
let elapsed = self.started_instant.elapsed();
|
|
let mut event = self.started.clone();
|
|
if let Some(event) = &mut event {
|
|
event.completed_at_ms = Some(completed_at_ms);
|
|
event.decision_source = Some(GuardianAssessmentDecisionSource::Agent);
|
|
}
|
|
(
|
|
crate::complete_review(outcome, model, require_guardian, event, details),
|
|
elapsed,
|
|
)
|
|
}
|
|
|
|
/// Records local-only review metrics. Nothing is sent anywhere.
|
|
pub fn record_metrics(
|
|
&self,
|
|
telemetry: &SessionTelemetry,
|
|
result: GuardianReviewDetails,
|
|
elapsed: Duration,
|
|
) {
|
|
crate::metrics::emit_guardian_review_metrics(
|
|
telemetry,
|
|
&result,
|
|
self.approval_request_source,
|
|
&self.reviewed_action,
|
|
elapsed,
|
|
);
|
|
}
|
|
}
|
|
|
|
/// Thread-scoped accounting shared by synchronous and cached approvals.
|
|
#[derive(Default)]
|
|
pub struct ReviewDenials(Mutex<GuardianRejectionCircuitBreaker>);
|
|
|
|
impl ReviewDenials {
|
|
pub fn for_thread(store: &ExtensionData) -> Arc<Self> {
|
|
store.get_or_init(Self::default)
|
|
}
|
|
|
|
pub async fn clear_turn(store: &ExtensionData, turn_id: &str) {
|
|
if let Some(state) = store.get::<Self>() {
|
|
state.0.lock().await.clear_turn(turn_id);
|
|
}
|
|
}
|
|
|
|
pub async fn record_non_denial(&self, turn_id: &str) {
|
|
self.0.lock().await.record_non_denial(turn_id);
|
|
}
|
|
|
|
/// Returns a warning exactly once when this turn reaches its denial limit.
|
|
pub async fn record_denial(&self, turn_id: &str) -> Option<String> {
|
|
let action = self.0.lock().await.record_denial(turn_id);
|
|
match action {
|
|
GuardianRejectionCircuitBreakerAction::Continue => None,
|
|
GuardianRejectionCircuitBreakerAction::InterruptTurn {
|
|
consecutive_denials,
|
|
recent_denials,
|
|
} => {
|
|
let window = crate::AUTO_REVIEW_DENIAL_WINDOW_SIZE;
|
|
Some(format!(
|
|
"Automatic approval review rejected too many approval requests for this turn ({consecutive_denials} consecutive, {recent_denials} in the last {window} reviews); interrupting the turn."
|
|
))
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
#[cfg(test)]
|
|
#[path = "reporting_tests.rs"]
|
|
mod tests;
|