adk-core 3.0.0

Core traits and types for Rust Agent Development Kit (ADK-Rust) agents, tools, sessions, and events
Documentation
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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
//! Agent identity: who an agent is, who sponsors it, and on whose behalf it acts.
//!
//! | Type | Identifies |
//! |------|------------|
//! | [`AgentId`] | One agent, stable across redeploys and replicas |
//! | [`SubjectRef`] | A human, a visitor, a service, or an agent, by kind and identifier |
//! | [`AgentPrincipal`] | An agent with its tenant, class, instance, sponsor, and attributes |
//! | [`ActingContext`] | An agent acting on behalf of a subject, through a delegation chain |
//!
//! Every type serializes with camelCase field names, so a principal recorded by one
//! process (in a run record, an audit entry, or a governance state) reads back unchanged
//! in another.
//!
//! # Example
//!
//! ```rust
//! use adk_core::principal::{ActingContext, AgentId, AgentPrincipal, SubjectRef};
//!
//! let agent = AgentPrincipal::new(AgentId::new("urn:adk:agent:billing")?)
//!     .with_tenant("acme")
//!     .with_sponsor(SubjectRef::human("alice@example.com"));
//! let acting = ActingContext::new(agent)
//!     .on_behalf_of(SubjectRef::human("bob@example.com"));
//!
//! let json = serde_json::to_value(&acting)?;
//! assert_eq!(json["agent"]["id"], "urn:adk:agent:billing");
//! assert_eq!(json["onBehalfOf"]["kind"], "human");
//! # Ok::<(), Box<dyn std::error::Error>>(())
//! ```

use std::collections::BTreeMap;
use std::fmt;
use std::str::FromStr;

use serde::{Deserialize, Serialize};
use thiserror::Error;

/// Maximum length of an [`AgentId`], in bytes.
pub const MAX_AGENT_ID_LEN: usize = 512;

/// Error returned when a string is not a valid [`AgentId`].
///
/// # Example
///
/// ```rust
/// use adk_core::principal::{AgentId, PrincipalError};
///
/// assert_eq!(AgentId::new(""), Err(PrincipalError::Empty));
/// ```
#[derive(Debug, Error, Clone, PartialEq, Eq)]
#[non_exhaustive]
pub enum PrincipalError {
    /// The agent identifier was empty.
    #[error("agent id must not be empty; use a URI such as `urn:adk:agent:<name>`")]
    Empty,
    /// The agent identifier exceeded [`MAX_AGENT_ID_LEN`].
    #[error(
        "agent id is {len} bytes, more than the {MAX_AGENT_ID_LEN}-byte limit; use a shorter URI"
    )]
    TooLong {
        /// The length of the rejected identifier, in bytes.
        len: usize,
    },
    /// The agent identifier contained a control character.
    #[error("agent id must not contain control characters (found U+{code_point:04X})")]
    ContainsControl {
        /// The first control character found.
        code_point: u32,
    },
}

/// Opaque, stable agent identifier.
///
/// A URI such as `spiffe://example.org/agent/billing` or `urn:adk:agent:billing`. It
/// survives redeploys and is shared by every replica of the agent. Validation requires
/// a non-empty value of at most [`MAX_AGENT_ID_LEN`] bytes with no control characters;
/// the URI scheme is not checked, so any identity system's naming fits.
///
/// Deserialization applies the same validation.
///
/// # Example
///
/// ```rust
/// use adk_core::principal::AgentId;
///
/// let id: AgentId = "spiffe://example.org/agent/billing".parse()?;
/// assert_eq!(id.as_str(), "spiffe://example.org/agent/billing");
/// assert!(AgentId::new("bad\nid").is_err());
/// # Ok::<(), adk_core::principal::PrincipalError>(())
/// ```
#[derive(Debug, Clone, PartialEq, Eq, Hash, PartialOrd, Ord, Serialize, Deserialize)]
#[serde(try_from = "String", into = "String")]
pub struct AgentId(String);

impl AgentId {
    /// Creates an agent identifier after validating it.
    ///
    /// # Errors
    ///
    /// Returns [`PrincipalError`] when the value is empty, longer than
    /// [`MAX_AGENT_ID_LEN`] bytes, or contains a control character.
    pub fn new(value: impl Into<String>) -> Result<Self, PrincipalError> {
        let value = value.into();
        if value.is_empty() {
            return Err(PrincipalError::Empty);
        }
        if value.len() > MAX_AGENT_ID_LEN {
            return Err(PrincipalError::TooLong { len: value.len() });
        }
        if let Some(control) = value.chars().find(|c| c.is_control()) {
            return Err(PrincipalError::ContainsControl { code_point: control as u32 });
        }
        Ok(Self(value))
    }

    /// Returns the identifier as a string slice.
    pub fn as_str(&self) -> &str {
        &self.0
    }
}

impl fmt::Display for AgentId {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.write_str(&self.0)
    }
}

impl AsRef<str> for AgentId {
    fn as_ref(&self) -> &str {
        &self.0
    }
}

impl FromStr for AgentId {
    type Err = PrincipalError;

    fn from_str(s: &str) -> Result<Self, Self::Err> {
        Self::new(s)
    }
}

impl TryFrom<String> for AgentId {
    type Error = PrincipalError;

    fn try_from(value: String) -> Result<Self, Self::Error> {
        Self::new(value)
    }
}

impl TryFrom<&str> for AgentId {
    type Error = PrincipalError;

    fn try_from(value: &str) -> Result<Self, Self::Error> {
        Self::new(value)
    }
}

impl From<AgentId> for String {
    fn from(id: AgentId) -> Self {
        id.0
    }
}

/// What kind of subject a [`SubjectRef`] names.
///
/// # Example
///
/// ```rust
/// use adk_core::principal::SubjectKind;
///
/// assert_eq!(serde_json::to_value(SubjectKind::Service).unwrap(), "service");
/// ```
#[derive(Debug, Clone, Copy, Default, PartialEq, Eq, Hash, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
#[non_exhaustive]
pub enum SubjectKind {
    /// A person. The default: an interactive run acts for a person.
    #[default]
    Human,
    /// A non-agent workload, such as a backend service or a scheduler.
    Service,
    /// An agent; the identifier is an [`AgentId`] string.
    Agent,
    /// A person the deployment knows only by an id it issued, such as a website visitor
    /// who has not signed in; the identifier is a
    /// [`VisitorId`](crate::subject::VisitorId) string.
    ///
    /// The id is pseudonymous, not anonymous: it names one person, so the data held under
    /// it is that person's. A visitor's run is never authenticated.
    Visitor,
}

/// A reference to a human, a visitor, a service, or an agent.
///
/// # Example
///
/// ```rust
/// use adk_core::principal::{SubjectKind, SubjectRef};
///
/// let alice = SubjectRef::human("alice@example.com");
/// assert_eq!(alice, SubjectRef { kind: SubjectKind::Human, id: "alice@example.com".into() });
/// ```
#[derive(Debug, Clone, PartialEq, Eq, Hash, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct SubjectRef {
    /// The kind of subject.
    pub kind: SubjectKind,
    /// The subject's identifier in its identity system.
    pub id: String,
}

impl SubjectRef {
    /// Creates a reference to a subject of `kind`.
    pub fn new(kind: SubjectKind, id: impl Into<String>) -> Self {
        Self { kind, id: id.into() }
    }

    /// Creates a reference to a person.
    pub fn human(id: impl Into<String>) -> Self {
        Self::new(SubjectKind::Human, id)
    }

    /// Creates a reference to a non-agent workload.
    pub fn service(id: impl Into<String>) -> Self {
        Self::new(SubjectKind::Service, id)
    }

    /// Creates a reference to an agent.
    pub fn agent(id: &AgentId) -> Self {
        Self::new(SubjectKind::Agent, id.as_str())
    }

    /// Creates a reference to a visitor known by `id`, a
    /// [`VisitorId`](crate::subject::VisitorId) the deployment issued.
    ///
    /// # Example
    ///
    /// ```rust
    /// use adk_core::principal::{SubjectKind, SubjectRef};
    /// use adk_core::subject::VisitorId;
    ///
    /// let visitor = SubjectRef::visitor(VisitorId::generate().as_str());
    /// assert_eq!(visitor.kind, SubjectKind::Visitor);
    /// ```
    pub fn visitor(id: impl Into<String>) -> Self {
        Self::new(SubjectKind::Visitor, id)
    }
}

/// An agent's identity: its stable identifier plus where and for whom it runs.
///
/// # Example
///
/// ```rust
/// use adk_core::principal::{AgentId, AgentPrincipal, SubjectRef};
///
/// let principal = AgentPrincipal::new(AgentId::new("urn:adk:agent:billing")?)
///     .with_tenant("acme")
///     .with_class("billing-v2")
///     .with_instance("replica-3")
///     .with_sponsor(SubjectRef::human("alice@example.com"))
///     .with_attribute("cost-center", "finance");
/// assert_eq!(principal.attributes["cost-center"], "finance");
/// # Ok::<(), adk_core::principal::PrincipalError>(())
/// ```
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct AgentPrincipal {
    /// The agent's stable identifier.
    pub id: AgentId,
    /// The tenant the agent belongs to, in a multi-tenant deployment.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub tenant: Option<String>,
    /// The template or blueprint the agent was created from.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub class: Option<String>,
    /// The deployment or replica running the agent.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub instance: Option<String>,
    /// The subject accountable for the agent.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub sponsor: Option<SubjectRef>,
    /// Further attributes, ordered by key so serialization is deterministic.
    #[serde(default, skip_serializing_if = "BTreeMap::is_empty")]
    pub attributes: BTreeMap<String, String>,
}

impl AgentPrincipal {
    /// Creates a principal with only an identifier.
    pub fn new(id: AgentId) -> Self {
        Self {
            id,
            tenant: None,
            class: None,
            instance: None,
            sponsor: None,
            attributes: BTreeMap::new(),
        }
    }

    /// Sets the tenant.
    pub fn with_tenant(mut self, tenant: impl Into<String>) -> Self {
        self.tenant = Some(tenant.into());
        self
    }

    /// Sets the class (template or blueprint).
    pub fn with_class(mut self, class: impl Into<String>) -> Self {
        self.class = Some(class.into());
        self
    }

    /// Sets the instance (deployment or replica).
    pub fn with_instance(mut self, instance: impl Into<String>) -> Self {
        self.instance = Some(instance.into());
        self
    }

    /// Sets the sponsor.
    pub fn with_sponsor(mut self, sponsor: SubjectRef) -> Self {
        self.sponsor = Some(sponsor);
        self
    }

    /// Adds or replaces one attribute.
    pub fn with_attribute(mut self, key: impl Into<String>, value: impl Into<String>) -> Self {
        self.attributes.insert(key.into(), value.into());
        self
    }
}

/// An agent acting for a subject, through a delegation chain.
///
/// The context names no invocation: the invocation an agent acts in is the execution
/// identity's ([`ReadonlyContext::invocation_id`](crate::ReadonlyContext::invocation_id)),
/// so there is one source for it. Audit records pair the two with
/// [`AuditActor::new`](crate::audit_log::AuditActor::new).
///
/// `delegation_chain` lists the subjects that handed the work down, outermost first:
/// a human who asked an orchestrator agent, which delegated to this agent, reads
/// `[human, orchestrator]`.
///
/// # Example
///
/// ```rust
/// use adk_core::principal::{ActingContext, AgentId, AgentPrincipal, SubjectRef};
///
/// let orchestrator = AgentId::new("urn:adk:agent:orchestrator")?;
/// let acting = ActingContext::new(AgentPrincipal::new(AgentId::new("urn:adk:agent:billing")?))
///     .on_behalf_of(SubjectRef::human("bob@example.com"))
///     .delegated_by(SubjectRef::human("bob@example.com"))
///     .delegated_by(SubjectRef::agent(&orchestrator));
/// assert_eq!(acting.delegation_chain.len(), 2);
/// # Ok::<(), adk_core::principal::PrincipalError>(())
/// ```
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct ActingContext {
    /// The acting agent.
    pub agent: AgentPrincipal,
    /// The subject the agent acts for, when it acts for one.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub on_behalf_of: Option<SubjectRef>,
    /// The subjects that delegated the work, outermost first.
    #[serde(default, skip_serializing_if = "Vec::is_empty")]
    pub delegation_chain: Vec<SubjectRef>,
}

impl ActingContext {
    /// Creates a context for `agent` acting for no subject.
    pub fn new(agent: AgentPrincipal) -> Self {
        Self { agent, on_behalf_of: None, delegation_chain: Vec::new() }
    }

    /// Sets the subject the agent acts for.
    pub fn on_behalf_of(mut self, subject: SubjectRef) -> Self {
        self.on_behalf_of = Some(subject);
        self
    }

    /// Appends a subject to the end of the delegation chain.
    pub fn delegated_by(mut self, subject: SubjectRef) -> Self {
        self.delegation_chain.push(subject);
        self
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use serde_json::json;

    #[test]
    fn agent_id_validation() {
        assert_eq!(AgentId::new(""), Err(PrincipalError::Empty));
        assert_eq!(
            AgentId::new("a".repeat(MAX_AGENT_ID_LEN + 1)),
            Err(PrincipalError::TooLong { len: MAX_AGENT_ID_LEN + 1 })
        );
        assert_eq!(
            AgentId::new("urn:adk:\u{7f}"),
            Err(PrincipalError::ContainsControl { code_point: 0x7f })
        );
        assert_eq!(AgentId::new("x\0y"), Err(PrincipalError::ContainsControl { code_point: 0 }));
        assert!(AgentId::new("a".repeat(MAX_AGENT_ID_LEN)).is_ok());
        assert!(AgentId::new("urn:adk:agent:fakturering/ΓΈ").is_ok());
    }

    #[test]
    fn agent_id_deserialization_validates() {
        let parsed: AgentId = serde_json::from_value(json!("urn:adk:agent:a")).unwrap();
        assert_eq!(parsed.as_str(), "urn:adk:agent:a");
        assert!(serde_json::from_value::<AgentId>(json!("")).is_err());
        assert!(serde_json::from_value::<AgentId>(json!("a\tb")).is_err());
    }

    #[test]
    fn acting_context_round_trips_in_camel_case() {
        let acting = ActingContext::new(
            AgentPrincipal::new(AgentId::new("spiffe://example.org/agent/billing").unwrap())
                .with_tenant("acme")
                .with_class("billing")
                .with_instance("r1")
                .with_sponsor(SubjectRef::human("alice"))
                .with_attribute("tier", "gold"),
        )
        .on_behalf_of(SubjectRef::service("crm"))
        .delegated_by(SubjectRef::human("alice"));

        let value = serde_json::to_value(&acting).unwrap();
        assert_eq!(
            value,
            json!({
                "agent": {
                    "id": "spiffe://example.org/agent/billing",
                    "tenant": "acme",
                    "class": "billing",
                    "instance": "r1",
                    "sponsor": {"kind": "human", "id": "alice"},
                    "attributes": {"tier": "gold"}
                },
                "onBehalfOf": {"kind": "service", "id": "crm"},
                "delegationChain": [{"kind": "human", "id": "alice"}]
            })
        );
        let back: ActingContext = serde_json::from_value(value).unwrap();
        assert_eq!(back, acting);

        let minimal =
            ActingContext::new(AgentPrincipal::new(AgentId::new("urn:adk:agent:a").unwrap()));
        let back: ActingContext =
            serde_json::from_value(serde_json::to_value(&minimal).unwrap()).unwrap();
        assert_eq!(back, minimal);
    }

    #[test]
    fn contexts_recorded_with_an_invocation_id_still_read() {
        // Records written before the invocation id moved to the execution identity.
        let back: ActingContext = serde_json::from_value(json!({
            "agent": {"id": "urn:adk:agent:a"},
            "invocationId": "inv-old"
        }))
        .unwrap();
        assert_eq!(
            back,
            ActingContext::new(AgentPrincipal::new(AgentId::new("urn:adk:agent:a").unwrap()))
        );
    }
}