1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
//! [`VisionCx`]: what every vision call carries.
use adk_core::audit_log::{AuditActor, AuditRecordDraft, AuditTrail};
use adk_core::{ActingContext, GovernanceControl, ModelCallScope};
use tokio_util::sync::CancellationToken;
use crate::error::{Result, VisionError};
use crate::media::MediaScope;
/// What every vision call carries: cancellation, the metering scope (budget, spend ledger
/// and kill switches), the acting agent, the media scope new images are stored under, the
/// audit trail `vision.*` records go to, and the tracing span.
///
/// # Example
///
/// ```
/// use adk_core::GovernanceControl;
/// use adk_vision::VisionCx;
///
/// let control = GovernanceControl::new();
/// let cx = VisionCx::new().with_governance(control.clone());
/// assert!(cx.check().is_ok());
/// control.freeze("incident 42");
/// assert!(cx.check().is_err());
/// ```
#[derive(Clone, Debug)]
pub struct VisionCx {
cancel: CancellationToken,
scope: ModelCallScope,
acting: Option<ActingContext>,
media: Option<MediaScope>,
audit: Option<VisionAudit>,
span: tracing::Span,
}
/// The audit trail of the run a vision call belongs to, and who acts in it.
#[derive(Clone)]
struct VisionAudit {
trail: AuditTrail,
actor: AuditActor,
}
impl std::fmt::Debug for VisionAudit {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_struct("VisionAudit").field("actor", &self.actor).finish_non_exhaustive()
}
}
impl Default for VisionCx {
fn default() -> Self {
Self::new()
}
}
impl VisionCx {
/// A context that meters nothing and is never cancelled.
pub fn new() -> Self {
Self {
cancel: CancellationToken::new(),
scope: ModelCallScope::new(),
acting: None,
media: None,
audit: None,
span: tracing::Span::current(),
}
}
/// A context under the metering scope made current by an enclosing
/// [`ModelCallScope::within`], such as the one agents set around every tool call.
pub fn current() -> Self {
Self { scope: ModelCallScope::current().unwrap_or_default(), ..Self::new() }
}
/// Meters calls under `scope`: its budget, spend ledger and kill switches.
pub fn with_scope(mut self, scope: ModelCallScope) -> Self {
self.scope = scope;
self
}
/// Adds a kill switch: while it is frozen, every call and every release is refused
/// (V-I17).
pub fn with_governance(mut self, control: GovernanceControl) -> Self {
self.scope = self.scope.with_governance(control);
self
}
/// Cancels calls when `token` fires.
pub fn with_cancellation(mut self, token: CancellationToken) -> Self {
self.cancel = token;
self
}
/// Names the acting agent.
pub fn with_acting(mut self, acting: ActingContext) -> Self {
self.acting = Some(acting);
self
}
/// Stores new images (edits, egress copies) under `scope`.
pub fn with_media_scope(mut self, scope: MediaScope) -> Self {
self.media = Some(scope);
self
}
/// Writes the call's `vision.*` audit records — egress releases and refusals, perception
/// calls and edits (spec 3.7 §9.3) — to `trail`, with `actor` as the acting agent. The
/// trail is the run's (`RunConfig::audit`), signed when the run's recorder signs.
pub fn with_audit(mut self, trail: AuditTrail, actor: AuditActor) -> Self {
self.audit = Some(VisionAudit { trail, actor });
self
}
/// Records the call's spans under `span`.
pub fn with_span(mut self, span: tracing::Span) -> Self {
self.span = span;
self
}
/// The cancellation token.
pub fn cancellation(&self) -> &CancellationToken {
&self.cancel
}
/// The metering scope.
pub fn scope(&self) -> &ModelCallScope {
&self.scope
}
/// The acting agent, when one is named.
pub fn acting(&self) -> Option<&ActingContext> {
self.acting.as_ref()
}
/// The media scope, when one is set.
pub fn media_scope(&self) -> Option<&MediaScope> {
self.media.as_ref()
}
/// The audit trail `vision.*` records go to, when one is set.
pub fn audit_trail(&self) -> Option<&AuditTrail> {
self.audit.as_ref().map(|audit| &audit.trail)
}
/// Writes `draft` to the audit trail as the acting agent; does nothing without a trail.
///
/// # Errors
///
/// The trail's error, when its failure mode fails closed.
pub(crate) async fn audit(&self, draft: AuditRecordDraft) -> Result<()> {
if let Some(audit) = &self.audit {
audit.trail.record(draft.actor(audit.actor.clone())).await?;
}
Ok(())
}
/// The tracing span.
pub fn span(&self) -> &tracing::Span {
&self.span
}
/// Refuses when the call is cancelled or a kill switch is frozen.
///
/// # Errors
///
/// [`VisionError::Cancelled`], or the frozen control's `governance.frozen` error.
pub fn check(&self) -> Result<()> {
if self.cancel.is_cancelled() {
return Err(VisionError::Cancelled);
}
self.scope.check_governance()?;
Ok(())
}
}