adk_kernel/agents/store.rs
1//! The kernel's store of agents a person adds (spec 3.3 §5.7, §10 K7).
2
3use async_trait::async_trait;
4
5use crate::agents::record::AgentRecord;
6use crate::error::Result;
7
8/// Where the kernel keeps the agents a person adds (spec 3.3 §5.7, §10 K7), beside the
9/// partition and driver stores, on every kernel backend, with one conformance suite
10/// (`agent_store_conformance_tests!`, feature `conformance`):
11///
12/// | Backend | Type | Feature |
13/// |---|---|---|
14/// | In-memory | [`InMemoryAgentStore`](crate::stores::InMemoryAgentStore) | — |
15/// | SQLite | `SqliteAgentStore` | `sqlite` (default) |
16/// | PostgreSQL | `PostgresAgentStore` | `postgres` |
17/// | Redis | `RedisAgentStore` | `redis` |
18/// | MongoDB | `MongoAgentStore` | `mongodb` |
19/// | Firestore | `FirestoreAgentStore` | `firestore` |
20/// | Neo4j | `Neo4jAgentStore` | `neo4j` |
21///
22/// Every write is a compare-and-set on the stored version, so two replicas changing one
23/// agent change it once. A removed agent stays as a `Removed` tombstone, so its id is never
24/// given again (S-I14); only a suggestion nobody approved is ever deleted. Nothing outside
25/// the kernel's agent table writes it (S-I17).
26///
27/// # Example
28///
29/// ```rust
30/// use adk_core::principal::SubjectRef;
31/// use adk_kernel::agents::{AgentDefinition, AgentOrigin, AgentRecord, AgentRecordState, AgentStore};
32/// use adk_kernel::stores::InMemoryAgentStore;
33///
34/// # #[tokio::main(flavor = "current_thread")]
35/// # async fn main() -> adk_kernel::Result<()> {
36/// let store = InMemoryAgentStore::new();
37/// let record = AgentRecord::new("finance", AgentDefinition::new("Finance", "default"),
38/// AgentRecordState::Active, AgentOrigin::ByHand, SubjectRef::human("alice"), chrono::Utc::now());
39/// assert!(store.put_if_version(record.clone(), 0).await?);
40/// assert!(!store.put_if_version(record.clone(), 0).await?, "the id is taken");
41/// let mut paused = record.with_state(AgentRecordState::Paused);
42/// paused.version = 2;
43/// assert!(store.put_if_version(paused, 1).await?);
44/// assert_eq!(store.get("finance").await?.unwrap().version, 2);
45/// assert!(!store.delete("finance").await?, "only a suggestion is ever deleted");
46/// # Ok(())
47/// # }
48/// ```
49#[async_trait]
50pub trait AgentStore: Send + Sync {
51 /// Writes `record` when the stored record with its id has version `expected`, or, with
52 /// `expected` = 0, when there is none; `false`, writing nothing, otherwise.
53 ///
54 /// # Errors
55 ///
56 /// Returns [`KernelError::Store`](crate::KernelError::Store) when the backend fails.
57 async fn put_if_version(&self, record: AgentRecord, expected: u64) -> Result<bool>;
58
59 /// The record `id`, tombstones included, or `None`.
60 ///
61 /// # Errors
62 ///
63 /// Returns [`KernelError::Store`](crate::KernelError::Store) when the backend fails.
64 async fn get(&self, id: &str) -> Result<Option<AgentRecord>>;
65
66 /// Every record, tombstones included, ordered by id in byte order.
67 ///
68 /// # Errors
69 ///
70 /// Returns [`KernelError::Store`](crate::KernelError::Store) when the backend fails.
71 async fn list(&self) -> Result<Vec<AgentRecord>>;
72
73 /// Deletes the record `id` when it is a suggestion nobody approved
74 /// ([`Proposed`](crate::agents::AgentRecordState::Proposed)); `false`, deleting
75 /// nothing, for any other record or none.
76 ///
77 /// # Errors
78 ///
79 /// Returns [`KernelError::Store`](crate::KernelError::Store) when the backend fails.
80 async fn delete(&self, id: &str) -> Result<bool>;
81}