Skip to main content

graphrt_core/
snapshot.rs

1//! Persistable run state for pause / resume.
2//!
3//! A snapshot never contains the graph, environment variables or secrets. To
4//! resume, the host rebuilds the same graph (checked via
5//! [`Snapshot::graph_fingerprint`]) and supplies env variables again.
6
7use indexmap::IndexMap;
8use serde::{Deserialize, Serialize};
9
10use crate::event::Usage;
11use crate::pool::{PoolSnapshot, Selector};
12use crate::value::Value;
13
14/// Current snapshot format version.
15pub const SNAPSHOT_FORMAT: u32 = 1;
16
17/// Complete persisted state of a paused run.
18#[derive(Clone, Debug, Default, PartialEq, Serialize, Deserialize)]
19pub struct Snapshot {
20    pub format: u32,
21    pub run_id: String,
22    /// Fingerprint of the graph the snapshot was taken from.
23    pub graph_fingerprint: String,
24    pub elapsed_ms: u64,
25    pub steps: u64,
26    pub usage: Usage,
27    pub handled_errors: u32,
28    /// What the run charged against its token and cost budget, and its limits.
29    #[serde(default, skip_serializing_if = "crate::budget::BudgetState::is_empty")]
30    pub budget: crate::budget::BudgetState,
31    pub root: FrameSnapshot,
32}
33
34/// Persisted state of one execution frame (the root graph, or one container body run).
35#[derive(Clone, Debug, Default, PartialEq, Serialize, Deserialize)]
36pub struct FrameSnapshot {
37    pub pool: PoolSnapshot,
38    /// Per-node state, keyed by node id. Nodes absent from the map have not been reached.
39    pub nodes: IndexMap<String, NodeSnapshot>,
40    /// Edge states, keyed by edge id.
41    pub edges: IndexMap<String, EdgeStatus>,
42    /// Accumulated response outputs of this frame (answer text, end outputs).
43    pub outputs: IndexMap<String, Value>,
44    /// Writes to namespaces outside this frame's scope, in commit order (child frames only).
45    #[serde(default, skip_serializing_if = "Vec::is_empty")]
46    pub writes: Vec<(Selector, Value)>,
47    /// Whether a `loop-end` node completed in this frame.
48    #[serde(default, skip_serializing_if = "std::ops::Not::not")]
49    pub broke: bool,
50    /// Usage accumulated by this frame's nodes.
51    #[serde(default)]
52    pub usage: Usage,
53}
54
55/// Lifecycle state of a node inside a frame.
56#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Serialize, Deserialize)]
57#[serde(rename_all = "snake_case")]
58#[non_exhaustive]
59pub enum NodeStatus {
60    /// Ready to run (or was running when the frame paused and must be re-run).
61    #[default]
62    Pending,
63    /// Paused by the node itself; it will be resumed with its saved state.
64    Paused,
65    Succeeded,
66    Failed,
67    Skipped,
68}
69
70/// Persisted state of one node.
71#[derive(Clone, Debug, Default, PartialEq, Serialize, Deserialize)]
72pub struct NodeSnapshot {
73    pub status: NodeStatus,
74    pub exec_id: String,
75    pub attempt: u32,
76    /// Node-owned resume state (for nodes that paused themselves, e.g. human input
77    /// or a container holding the snapshots of its child frames).
78    #[serde(default, skip_serializing_if = "Option::is_none")]
79    pub resume_state: Option<serde_json::Value>,
80    /// The progress of a node that was running when the run was preempted
81    /// ([`crate::node::NodeContext::checkpoint`]): its next attempt gets it.
82    #[serde(default, skip_serializing_if = "Option::is_none")]
83    pub checkpoint: Option<serde_json::Value>,
84    /// What the run already counted of a node that paused (its usage before
85    /// the pause): taken back from what it reports when it finishes.
86    #[serde(default, skip_serializing_if = "Option::is_none")]
87    pub counted: Option<crate::event::Usage>,
88}
89
90/// Traversal state of an edge.
91#[derive(Clone, Copy, Debug, PartialEq, Eq, Serialize, Deserialize)]
92#[serde(rename_all = "snake_case")]
93#[non_exhaustive]
94pub enum EdgeStatus {
95    Unknown,
96    Taken,
97    Skipped,
98}
99
100impl Snapshot {
101    /// Encode as JSON.
102    pub fn to_json(&self) -> Result<String, serde_json::Error> {
103        serde_json::to_string(self)
104    }
105
106    /// Decode from JSON.
107    pub fn from_json(text: &str) -> Result<Self, serde_json::Error> {
108        serde_json::from_slice(text.as_bytes())
109    }
110}