Skip to main content

strands/types/
snapshot.rs

1//! Snapshot types shared by agent and multi-agent snapshots.
2
3use serde::{Deserialize, Serialize};
4
5use super::json::JsonObject;
6
7/// The current snapshot schema version (`SNAPSHOT_SCHEMA_VERSION`).
8pub const SNAPSHOT_SCHEMA_VERSION: &str = "1.0";
9
10/// The context a snapshot belongs to (`Scope`).
11#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
12#[serde(rename_all = "camelCase")]
13#[non_exhaustive]
14pub enum Scope {
15    /// An agent, `"agent"`.
16    Agent,
17    /// A multi-agent orchestrator, `"multiAgent"`.
18    MultiAgent,
19}
20
21/// A point-in-time capture of agent or orchestrator state (`Snapshot`).
22#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
23#[serde(rename_all = "camelCase")]
24#[non_exhaustive]
25pub struct Snapshot {
26    /// The context the snapshot belongs to.
27    pub scope: Scope,
28    /// Schema version, for forward compatibility.
29    pub schema_version: String,
30    /// ISO 8601 creation time in UTC with milliseconds, such as `2026-11-11T12:00:00.000Z`.
31    pub created_at: String,
32    /// Framework-owned state.
33    pub data: JsonObject,
34    /// Application-owned data. Strands never reads or modifies it.
35    pub app_data: JsonObject,
36}
37
38impl Snapshot {
39    /// Creates a snapshot at the current schema version with empty app data.
40    pub fn new(scope: Scope, created_at: impl Into<String>, data: JsonObject) -> Self {
41        Self {
42            scope,
43            schema_version: SNAPSHOT_SCHEMA_VERSION.to_owned(),
44            created_at: created_at.into(),
45            data,
46            app_data: JsonObject::new(),
47        }
48    }
49
50    /// Sets the application-owned data.
51    #[must_use]
52    pub fn with_app_data(mut self, app_data: JsonObject) -> Self {
53        self.app_data = app_data;
54        self
55    }
56}
57
58#[cfg(test)]
59mod tests {
60    use serde_json::json;
61
62    use super::*;
63
64    #[test]
65    fn snapshot_serializes_in_ts_key_order() {
66        let mut data = JsonObject::new();
67        data.insert("messages".into(), json!([]));
68        let snapshot = Snapshot::new(Scope::Agent, "2026-11-11T12:00:00.000Z", data)
69            .with_app_data(json!({"k": 1}).as_object().unwrap().clone());
70        assert_eq!(
71            serde_json::to_string(&snapshot).unwrap(),
72            r#"{"scope":"agent","schemaVersion":"1.0","createdAt":"2026-11-11T12:00:00.000Z","data":{"messages":[]},"appData":{"k":1}}"#
73        );
74    }
75
76    #[test]
77    fn multi_agent_scope_uses_camel_case() {
78        assert_eq!(serde_json::to_value(Scope::MultiAgent).unwrap(), json!("multiAgent"));
79    }
80}