Expand description
External guard adapter infrastructure.
The building blocks in this module let you wrap a synchronous external
API (cloud guardrails, threat intel feeds, ML classifiers) as an async
Chio guard without leaking I/O concerns into the sync chio_kernel::Guard
trait.
The pieces are:
ExternalGuard– the async trait a concrete external adapter implements. It describes the one operation we actually want to make resilient:eval(ctx) -> Result<Verdict, _>.AsyncGuardAdapter– composes acircuit_breaker::CircuitBreaker,token_bucket::TokenBucket,cache::TtlCache, andretry_with_jitteraround anExternalGuard.CircuitOpenVerdict– what the adapter returns when the breaker is open. Default isCircuitOpenVerdict::Deny(fail-closed).RateLimitedVerdict– what the adapter returns when the rate limiter rejects a call. Default isRateLimitedVerdict::Deny(fail-closed, per the phase-13.1 acceptance criteria).
§Example
ⓘ
use std::sync::Arc;
use std::time::Duration;
use async_trait::async_trait;
use chio_guards::external::{
AsyncGuardAdapter, ExternalGuard, ExternalGuardError, GuardCallContext,
};
use chio_kernel::Verdict;
struct HelloGuard;
#[async_trait]
impl ExternalGuard for HelloGuard {
fn name(&self) -> &str { "hello" }
fn cache_key(&self, ctx: &GuardCallContext) -> Option<String> {
Some(ctx.tool_name.clone())
}
async fn eval(&self, _ctx: &GuardCallContext) -> Result<Verdict, ExternalGuardError> {
Ok(Verdict::Allow)
}
}
let adapter = AsyncGuardAdapter::builder(Arc::new(HelloGuard))
.cache_ttl(Duration::from_secs(30))
.build();Re-exports§
pub use cache::Clock;pub use cache::TokioClock;pub use cache::TtlCache;pub use circuit_breaker::CircuitBreaker;pub use circuit_breaker::CircuitBreakerConfig;pub use circuit_breaker::CircuitState;pub use retry::retry_with_jitter;pub use retry::retry_with_jitter_rng;pub use retry::BackoffStrategy;pub use retry::RetryConfig;pub use token_bucket::TokenBucket;
Modules§
- cache
- TTL cache with LRU eviction for external guard verdicts.
- circuit_
breaker - Three-state circuit breaker for external service calls.
- retry
- Retry with deterministic jitter for transient external failures.
- token_
bucket - Simple token bucket rate limiter for the external guard adapter.
Structs§
- Async
Guard Adapter - Adapter that composes circuit breaker + token bucket + TTL cache + retry
on top of an
ExternalGuard. - Async
Guard Adapter Builder - Fluent builder for
AsyncGuardAdapter. - Async
Guard Adapter Config - Configuration for
AsyncGuardAdapter. Built viaAsyncGuardAdapter::builder. - Guard
Call Context - Subset of guard-request information passed to an
ExternalGuard.
Enums§
- Circuit
Open Verdict - Verdict returned when the circuit breaker is open.
- External
Guard Error - Errors surfaced from an
ExternalGuardcall. - Rate
Limited Verdict - Verdict returned when the token bucket rejects a call.
Traits§
- External
Guard - Trait implemented by guards that call external services.