Skip to main content

agent_core/memory/
author.rs

1//! Who a run is: the identity a chat or one of its workers carries in its
2//! memory binding and that `tool.call` forwards to extensions as `caller`.
3//! An identity, not a secret and not a proof of who calls.
4use serde::{Deserialize, Serialize};
5
6#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
7#[serde(deny_unknown_fields)]
8pub struct Author {
9    pub actor: String,
10    pub group: String,
11    #[serde(default, skip_serializing_if = "Option::is_none")]
12    pub parent: Option<String>,
13}
14
15impl Author {
16    pub fn fresh() -> Self {
17        Self {
18            actor: format!("actor-{}", uuid::Uuid::new_v4().simple()),
19            group: format!("group-{}", uuid::Uuid::new_v4().simple()),
20            parent: None,
21        }
22    }
23
24    pub fn child(&self) -> Self {
25        Self {
26            actor: format!("actor-{}", uuid::Uuid::new_v4().simple()),
27            group: self.group.clone(),
28            parent: Some(self.actor.clone()),
29        }
30    }
31
32    pub fn validate(&self) -> Result<(), String> {
33        if !prefixed_hex(&self.actor, "actor-", 32)
34            || !prefixed_hex(&self.group, "group-", 32)
35            || self
36                .parent
37                .as_ref()
38                .is_some_and(|p| !prefixed_hex(p, "actor-", 32) || p == &self.actor)
39        {
40            return Err("invalid author".into());
41        }
42        Ok(())
43    }
44}
45
46impl Default for Author {
47    fn default() -> Self {
48        Self::fresh()
49    }
50}
51
52fn prefixed_hex(s: &str, prefix: &str, length: usize) -> bool {
53    s.strip_prefix(prefix).is_some_and(|tail| {
54        tail.len() == length
55            && tail
56                .bytes()
57                .all(|b| b.is_ascii_digit() || (b'a'..=b'f').contains(&b))
58    })
59}
60
61#[cfg(test)]
62mod tests {
63    use super::*;
64
65    #[test]
66    fn child_keeps_group_and_names_parent() {
67        let a = Author::fresh();
68        a.validate().unwrap();
69        let child = a.child();
70        assert_eq!(a.group, child.group);
71        assert_eq!(child.parent, Some(a.actor.clone()));
72        assert_ne!(a.actor, child.actor);
73        child.validate().unwrap();
74    }
75
76    #[test]
77    fn malformed_authors_do_not_validate() {
78        let good = Author::fresh();
79        for bad in [
80            Author { actor: "actor-x".into(), ..good.clone() },
81            Author { group: format!("group-{}", "A".repeat(32)), ..good.clone() },
82            Author { parent: Some(good.actor.clone()), ..good.clone() },
83            Author { parent: Some("group-0".into()), ..good.clone() },
84        ] {
85            assert!(bad.validate().is_err(), "{bad:?}");
86        }
87    }
88
89    #[test]
90    fn serialises_without_parent_for_a_chat() {
91        let a = Author::fresh();
92        let v = serde_json::to_value(&a).unwrap();
93        assert!(v.get("parent").is_none());
94        assert_eq!(serde_json::from_value::<Author>(v).unwrap(), a);
95    }
96}