pub struct GovernanceControl { /* private fields */ }Expand description
Core traits and types.
Always available regardless of feature flags. Includes:
Agent- The fundamental trait for all agentsTool/Toolset- For extending agents with capabilitiesSession/State- For managing conversation contextEvent- For streaming agent responsesAdkError/Result- Unified error handling An organisation-wide kill switch for agent execution.
Clones share one flag. While frozen, a run fails at its start, an agent stops
before its next model call, no tool executes, and a call waiting for a confirmation
decision stops waiting and is refused: each check ends the run with an error carrying
the freeze reason. refused resolves when the control starts refusing
work.
Attach a control to a runner with its builder’s governance, or to a single run
with RunConfigBuilder::governance.
§Replicated kill switch
GovernanceControl::new is process-local. GovernanceControl::mirrored builds a
control that mirrors a GovernanceStateStore shared by every replica:
| Mechanism | Behaviour |
|---|---|
spawn_mirror | Reads the mirrored scopes every poll_interval, and at once when the optional GovernanceWatch reports a change |
| Staleness lease | Past max_staleness without a successful read, check fails closed with governance.stale, unless the mirror is configured fail-open |
freeze_replicated / unfreeze_replicated | Compare-and-swap the store, then apply the new state locally |
A local freeze still works on a mirrored control and holds until a
local unfreeze; the store never lifts it.
§Example
use adk_core::GovernanceControl;
let control = GovernanceControl::new();
assert!(control.check().is_ok());
control.freeze("incident 4012: unexpected payouts");
let error = control.check().unwrap_err();
assert!(error.to_string().contains("incident 4012"));
control.unfreeze();
assert!(!control.is_frozen());Implementations§
Source§impl GovernanceControl
impl GovernanceControl
Sourcepub fn new() -> GovernanceControl
pub fn new() -> GovernanceControl
Creates an unfrozen, process-local control.
Sourcepub fn mirrored(
store: Arc<dyn GovernanceStateStore>,
config: GovernanceMirrorConfig,
) -> GovernanceControl
pub fn mirrored( store: Arc<dyn GovernanceStateStore>, config: GovernanceMirrorConfig, ) -> GovernanceControl
Creates an unfrozen control that mirrors store.
The control is stale until its first successful read, so it fails closed until
refresh or spawn_mirror reads the store
(unless config.fail_open).
§Example
use std::sync::Arc;
use adk_core::GovernanceControl;
use adk_core::governance::GovernanceMirrorConfig;
use adk_core::governance_state::InMemoryGovernanceStateStore;
let store = Arc::new(InMemoryGovernanceStateStore::new());
let a = GovernanceControl::mirrored(store.clone(), GovernanceMirrorConfig::default());
let b = GovernanceControl::mirrored(store, GovernanceMirrorConfig::default());
assert!(a.check().is_err(), "stale until the first read");
a.freeze_replicated("incident 7", None).await?;
b.refresh().await?;
assert!(b.is_frozen());Sourcepub fn is_mirrored(&self) -> bool
pub fn is_mirrored(&self) -> bool
Whether the control mirrors a GovernanceStateStore.
Sourcepub fn freeze(&self, reason: impl Into<String>)
pub fn freeze(&self, reason: impl Into<String>)
Freezes execution everywhere this control is attached, in this process only.
With an attached audit trail, the freeze is recorded in the
background; use freeze_recorded to wait for the record.
Sourcepub fn unfreeze(&self)
pub fn unfreeze(&self)
Lifts a local freeze. A freeze read from a mirrored store stays in force.
With an attached audit trail, the change is recorded in the
background; use unfreeze_recorded to lift the
freeze only once it is recorded.
Sourcepub fn attach_audit(&self, audit: AuditTrail)
pub fn attach_audit(&self, audit: AuditTrail)
Records freezes and unfreezes of this control, and of every clone, through audit.
Sourcepub fn audit(&self) -> Option<AuditTrail>
pub fn audit(&self) -> Option<AuditTrail>
The attached audit trail, if any.
Sourcepub async fn freeze_recorded(
&self,
reason: impl Into<String>,
actor: AuditActor,
) -> Result<(), AdkError>
pub async fn freeze_recorded( &self, reason: impl Into<String>, actor: AuditActor, ) -> Result<(), AdkError>
Freezes execution, then records the freeze as done by actor.
A mirrored control freezes every replica, as
freeze_replicated does, with actor’s subject as the
issuer. The freeze takes effect whether or not the record is written.
§Errors
Returns the replicated store’s error, as freeze_replicated does (this replica
is frozen regardless), and audit.record_failed when the attached trail is in
Block mode and the record cannot be
written; execution stays frozen.
§Example
use std::sync::Arc;
use adk_core::GovernanceControl;
use adk_core::audit_log::{AuditActor, AuditTrail, InMemoryAuditRecorder};
use adk_core::principal::SubjectRef;
let recorder = InMemoryAuditRecorder::new();
let control = GovernanceControl::new();
control.attach_audit(AuditTrail::new(Arc::new(recorder.clone())));
let admin = AuditActor::subject(SubjectRef::human("oncall"));
control.freeze_recorded("incident 4012", admin.clone()).await?;
control.unfreeze_recorded(admin).await?;
assert_eq!(recorder.records().len(), 2);Sourcepub async fn unfreeze_recorded(&self, actor: AuditActor) -> Result<(), AdkError>
pub async fn unfreeze_recorded(&self, actor: AuditActor) -> Result<(), AdkError>
Records the unfreeze as done by actor, then lifts the freeze; on every replica
for a mirrored control, as unfreeze_replicated does.
§Errors
Returns the replicated store’s error, as unfreeze_replicated does, and
audit.record_failed when the attached trail is in
Block mode and the record cannot be
written; execution stays frozen.
Sourcepub fn same_as(&self, other: &GovernanceControl) -> bool
pub fn same_as(&self, other: &GovernanceControl) -> bool
Whether other shares this control’s flag.
Sourcepub fn staleness(&self) -> Option<Duration>
pub fn staleness(&self) -> Option<Duration>
How long ago the mirrored store was last read successfully; None for a
process-local control or before the first read.
Sourcepub fn is_stale(&self) -> bool
pub fn is_stale(&self) -> bool
Whether a mirrored control has gone past max_staleness without a successful
read. Always false for a process-local control.
Sourcepub fn check(&self) -> Result<(), AdkError>
pub fn check(&self) -> Result<(), AdkError>
Returns an error when frozen, or when a mirrored, fail-closed control is stale.
§Errors
Returns a non-retryable ErrorCategory::Forbidden error with code
governance.frozen while frozen, and a retryable ErrorCategory::Unavailable
error with code GOVERNANCE_STALE_CODE while a fail-closed mirror is stale.
Sourcepub async fn refresh(&self) -> Result<(), AdkError>
pub async fn refresh(&self) -> Result<(), AdkError>
Reads the mirrored scopes once and applies them. A no-op for a process-local control.
§Errors
Returns the store’s error; the previous state stays in force and its age keeps growing.
Sourcepub async fn refused(&self) -> AdkError
pub async fn refused(&self) -> AdkError
Waits until the control refuses work, and returns the error check
gives then: frozen, locally or by the mirrored store, or a fail-closed mirror past
its staleness limit.
Resolves at once when the control already refuses work. Freezes and mirror reads wake it as they happen and the staleness limit is timed, so nothing polls. The governed path races every wait for a confirmation decision against it, so a freeze ends the wait instead of letting the call run once someone decides it.
§Example
use adk_core::{GOVERNANCE_FROZEN_CODE, GovernanceControl};
let control = GovernanceControl::new();
// A person who never answers.
let decision = std::future::pending::<()>();
let operator = control.clone();
tokio::spawn(async move { operator.freeze("incident 4012") });
tokio::select! {
() = decision => unreachable!("nobody answers"),
error = control.refused() => assert_eq!(error.code, GOVERNANCE_FROZEN_CODE),
}Sourcepub fn spawn_mirror(&self) -> Option<JoinHandle<()>>
pub fn spawn_mirror(&self) -> Option<JoinHandle<()>>
Spawns the task that keeps a mirrored control in step with its store, and returns
its handle; None for a process-local control.
The task reads the store at once, then every poll_interval and whenever the
configured GovernanceWatch reports a change. It ends when every clone of the
control has been dropped, or when the handle is aborted.
Sourcepub async fn freeze_replicated(
&self,
reason: impl Into<String>,
issued_by: Option<SubjectRef>,
) -> Result<Option<GovernanceState>, AdkError>
pub async fn freeze_replicated( &self, reason: impl Into<String>, issued_by: Option<SubjectRef>, ) -> Result<Option<GovernanceState>, AdkError>
Freezes execution on every replica that mirrors the same store, and returns the stored state.
On a mirrored control this writes the first mirrored scope by compare-and-swap,
retrying when another change lands first, then applies the state locally. When
the store cannot be written, the control freezes locally so this replica stops
regardless, and the error is returned. On a process-local control it is
freeze and returns None.
§Errors
Returns the store’s error, or governance.conflict when every attempt lost the race.
Sourcepub async fn unfreeze_replicated(
&self,
issued_by: Option<SubjectRef>,
) -> Result<Option<GovernanceState>, AdkError>
pub async fn unfreeze_replicated( &self, issued_by: Option<SubjectRef>, ) -> Result<Option<GovernanceState>, AdkError>
Lifts a replicated freeze, and any local one, and returns the stored state.
On a mirrored control this writes the first mirrored scope by compare-and-swap and
clears the local freeze only once the store is written. On a process-local control
it is unfreeze and returns None.
§Errors
Returns the store’s error, or governance.conflict when every attempt lost the
race; the freeze then stays in force.
Trait Implementations§
Source§impl Clone for GovernanceControl
impl Clone for GovernanceControl
Source§fn clone(&self) -> GovernanceControl
fn clone(&self) -> GovernanceControl
1.0.0 (const: unstable) · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read moreSource§impl Debug for GovernanceControl
impl Debug for GovernanceControl
Source§impl Default for GovernanceControl
impl Default for GovernanceControl
Source§fn default() -> GovernanceControl
fn default() -> GovernanceControl
Auto Trait Implementations§
impl !RefUnwindSafe for GovernanceControl
impl !UnwindSafe for GovernanceControl
impl Forget for GovernanceControl
impl Freeze for GovernanceControl
impl Move for GovernanceControl
impl Send for GovernanceControl
impl Sync for GovernanceControl
impl Unpin for GovernanceControl
impl UnsafeUnpin for GovernanceControlwhere
*const ArcInner<ControlState> is !null: UnsafeUnpin,
PhantomData<ArcInner<ControlState>>: UnsafeUnpin,
Blanket Implementations§
Source§impl<'a, T, E> AsTaggedExplicit<'a, E> for Twhere
T: 'a,
impl<'a, T, E> AsTaggedExplicit<'a, E> for Twhere
T: 'a,
Source§impl<'a, T, E> AsTaggedImplicit<'a, E> for Twhere
T: 'a,
impl<'a, T, E> AsTaggedImplicit<'a, E> for Twhere
T: 'a,
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
impl<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<T> FmtForward for T
impl<T> FmtForward for T
Source§fn fmt_binary(self) -> FmtBinary<Self>where
Self: Binary,
fn fmt_binary(self) -> FmtBinary<Self>where
Self: Binary,
self to use its Binary implementation when Debug-formatted.Source§fn fmt_display(self) -> FmtDisplay<Self>where
Self: Display,
fn fmt_display(self) -> FmtDisplay<Self>where
Self: Display,
self to use its Display implementation when
Debug-formatted.Source§fn fmt_lower_exp(self) -> FmtLowerExp<Self>where
Self: LowerExp,
fn fmt_lower_exp(self) -> FmtLowerExp<Self>where
Self: LowerExp,
self to use its LowerExp implementation when
Debug-formatted.Source§fn fmt_lower_hex(self) -> FmtLowerHex<Self>where
Self: LowerHex,
fn fmt_lower_hex(self) -> FmtLowerHex<Self>where
Self: LowerHex,
self to use its LowerHex implementation when
Debug-formatted.Source§fn fmt_octal(self) -> FmtOctal<Self>where
Self: Octal,
fn fmt_octal(self) -> FmtOctal<Self>where
Self: Octal,
self to use its Octal implementation when Debug-formatted.Source§fn fmt_pointer(self) -> FmtPointer<Self>where
Self: Pointer,
fn fmt_pointer(self) -> FmtPointer<Self>where
Self: Pointer,
self to use its Pointer implementation when
Debug-formatted.Source§fn fmt_upper_exp(self) -> FmtUpperExp<Self>where
Self: UpperExp,
fn fmt_upper_exp(self) -> FmtUpperExp<Self>where
Self: UpperExp,
self to use its UpperExp implementation when
Debug-formatted.Source§fn fmt_upper_hex(self) -> FmtUpperHex<Self>where
Self: UpperHex,
fn fmt_upper_hex(self) -> FmtUpperHex<Self>where
Self: UpperHex,
self to use its UpperHex implementation when
Debug-formatted.Source§impl<T> FutureExt for T
impl<T> FutureExt for T
Source§fn with_context(self, otel_cx: Context) -> WithContext<Self> ⓘ
fn with_context(self, otel_cx: Context) -> WithContext<Self> ⓘ
Source§fn with_current_context(self) -> WithContext<Self> ⓘ
fn with_current_context(self) -> WithContext<Self> ⓘ
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self> ⓘ
fn instrument(self, span: Span) -> Instrumented<Self> ⓘ
Source§fn in_current_span(self) -> Instrumented<Self> ⓘ
fn in_current_span(self) -> Instrumented<Self> ⓘ
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreimpl<T> MaybeSend for Twhere
T: Send,
Source§impl<T> Pipe for Twhere
T: ?Sized,
impl<T> Pipe for Twhere
T: ?Sized,
Source§fn pipe<R>(self, func: impl FnOnce(Self) -> R) -> Rwhere
Self: Sized,
fn pipe<R>(self, func: impl FnOnce(Self) -> R) -> Rwhere
Self: Sized,
Source§fn pipe_ref<'a, R>(&'a self, func: impl FnOnce(&'a Self) -> R) -> Rwhere
R: 'a,
fn pipe_ref<'a, R>(&'a self, func: impl FnOnce(&'a Self) -> R) -> Rwhere
R: 'a,
self and passes that borrow into the pipe function. Read moreSource§fn pipe_ref_mut<'a, R>(&'a mut self, func: impl FnOnce(&'a mut Self) -> R) -> Rwhere
R: 'a,
fn pipe_ref_mut<'a, R>(&'a mut self, func: impl FnOnce(&'a mut Self) -> R) -> Rwhere
R: 'a,
self and passes that borrow into the pipe function. Read moreSource§fn pipe_borrow<'a, B, R>(&'a self, func: impl FnOnce(&'a B) -> R) -> R
fn pipe_borrow<'a, B, R>(&'a self, func: impl FnOnce(&'a B) -> R) -> R
Source§fn pipe_borrow_mut<'a, B, R>(
&'a mut self,
func: impl FnOnce(&'a mut B) -> R,
) -> R
fn pipe_borrow_mut<'a, B, R>( &'a mut self, func: impl FnOnce(&'a mut B) -> R, ) -> R
Source§fn pipe_as_ref<'a, U, R>(&'a self, func: impl FnOnce(&'a U) -> R) -> R
fn pipe_as_ref<'a, U, R>(&'a self, func: impl FnOnce(&'a U) -> R) -> R
self, then passes self.as_ref() into the pipe function.Source§fn pipe_as_mut<'a, U, R>(&'a mut self, func: impl FnOnce(&'a mut U) -> R) -> R
fn pipe_as_mut<'a, U, R>(&'a mut self, func: impl FnOnce(&'a mut U) -> R) -> R
self, then passes self.as_mut() into the pipe
function.Source§fn pipe_deref<'a, T, R>(&'a self, func: impl FnOnce(&'a T) -> R) -> R
fn pipe_deref<'a, T, R>(&'a self, func: impl FnOnce(&'a T) -> R) -> R
self, then passes self.deref() into the pipe function.Source§impl<T> Pointable for T
impl<T> Pointable for T
Source§impl<T> PolicyExt for Twhere
T: ?Sized,
impl<T> PolicyExt for Twhere
T: ?Sized,
impl<T> Read<Exclusive, BecauseExclusive> for Twhere
T: ?Sized,
Source§impl<SS, SP> SupersetOf<SS> for SPwhere
SS: SubsetOf<SP>,
impl<SS, SP> SupersetOf<SS> for SPwhere
SS: SubsetOf<SP>,
Source§fn to_subset(&self) -> Option<SS>
fn to_subset(&self) -> Option<SS>
self from the equivalent element of its
superset. Read moreSource§fn is_in_subset(&self) -> bool
fn is_in_subset(&self) -> bool
self is actually part of its subset T (and can be converted to it).Source§fn to_subset_unchecked(&self) -> SS
fn to_subset_unchecked(&self) -> SS
self.to_subset but without any property checks. Always succeeds.Source§fn from_subset(element: &SS) -> SP
fn from_subset(element: &SS) -> SP
self to the equivalent element of its superset.Source§impl<T> Tap for T
impl<T> Tap for T
Source§fn tap_borrow<B>(self, func: impl FnOnce(&B)) -> Self
fn tap_borrow<B>(self, func: impl FnOnce(&B)) -> Self
Borrow<B> of a value. Read moreSource§fn tap_borrow_mut<B>(self, func: impl FnOnce(&mut B)) -> Self
fn tap_borrow_mut<B>(self, func: impl FnOnce(&mut B)) -> Self
BorrowMut<B> of a value. Read moreSource§fn tap_ref<R>(self, func: impl FnOnce(&R)) -> Self
fn tap_ref<R>(self, func: impl FnOnce(&R)) -> Self
AsRef<R> view of a value. Read moreSource§fn tap_ref_mut<R>(self, func: impl FnOnce(&mut R)) -> Self
fn tap_ref_mut<R>(self, func: impl FnOnce(&mut R)) -> Self
AsMut<R> view of a value. Read moreSource§fn tap_deref<T>(self, func: impl FnOnce(&T)) -> Self
fn tap_deref<T>(self, func: impl FnOnce(&T)) -> Self
Deref::Target of a value. Read moreSource§fn tap_deref_mut<T>(self, func: impl FnOnce(&mut T)) -> Self
fn tap_deref_mut<T>(self, func: impl FnOnce(&mut T)) -> Self
Deref::Target of a value. Read moreSource§fn tap_dbg(self, func: impl FnOnce(&Self)) -> Self
fn tap_dbg(self, func: impl FnOnce(&Self)) -> Self
.tap() only in debug builds, and is erased in release builds.Source§fn tap_mut_dbg(self, func: impl FnOnce(&mut Self)) -> Self
fn tap_mut_dbg(self, func: impl FnOnce(&mut Self)) -> Self
.tap_mut() only in debug builds, and is erased in release
builds.Source§fn tap_borrow_dbg<B>(self, func: impl FnOnce(&B)) -> Self
fn tap_borrow_dbg<B>(self, func: impl FnOnce(&B)) -> Self
.tap_borrow() only in debug builds, and is erased in release
builds.Source§fn tap_borrow_mut_dbg<B>(self, func: impl FnOnce(&mut B)) -> Self
fn tap_borrow_mut_dbg<B>(self, func: impl FnOnce(&mut B)) -> Self
.tap_borrow_mut() only in debug builds, and is erased in release
builds.Source§fn tap_ref_dbg<R>(self, func: impl FnOnce(&R)) -> Self
fn tap_ref_dbg<R>(self, func: impl FnOnce(&R)) -> Self
.tap_ref() only in debug builds, and is erased in release
builds.Source§fn tap_ref_mut_dbg<R>(self, func: impl FnOnce(&mut R)) -> Self
fn tap_ref_mut_dbg<R>(self, func: impl FnOnce(&mut R)) -> Self
.tap_ref_mut() only in debug builds, and is erased in release
builds.Source§fn tap_deref_dbg<T>(self, func: impl FnOnce(&T)) -> Self
fn tap_deref_dbg<T>(self, func: impl FnOnce(&T)) -> Self
.tap_deref() only in debug builds, and is erased in release
builds.