aither_agent/checkpoint.rs
1//! Agent checkpoint bundles for restart-safe persistence.
2
3use std::sync::Arc;
4
5use arc_swap::ArcSwapOption;
6use serde::{Deserialize, Serialize};
7
8use crate::{ContextCheckpoint, ContextWindowSnapshot};
9
10/// Checkpoint payload exported by the agent runtime.
11#[derive(Debug, Clone, Serialize, Deserialize)]
12#[serde(deny_unknown_fields)]
13pub struct AgentCheckpoint {
14 /// Non-persistent runtime context managed by the agent.
15 pub context: ContextCheckpoint,
16 /// Hash of the active tool surface for compatibility checks.
17 pub tool_surface_hash: String,
18 /// Structured snapshot of the current context window.
19 pub context_window: ContextWindowSnapshot,
20}
21
22/// The newest checkpoint of a run in progress, readable while the run is
23/// being driven.
24///
25/// The run itself holds the agent, so an embedder cannot call
26/// [`Agent::export_checkpoint`](crate::Agent::export_checkpoint) between its
27/// events. The run instead records a checkpoint each time it emits
28/// [`AgentEvent::Checkpoint`](crate::AgentEvent::Checkpoint), before the event
29/// is queued; this handle, taken with
30/// [`Run::checkpoints`](crate::Run::checkpoints) before the run is driven,
31/// reads it from the event handler.
32///
33/// The checkpoint read is the one of that event or of a later one: the run
34/// keeps progressing while the handler awaits, so it may have checkpointed
35/// again. Either is a consistent state to restore.
36#[derive(Debug, Clone, Default)]
37pub struct Checkpoints(Arc<ArcSwapOption<AgentCheckpoint>>);
38
39impl Checkpoints {
40 /// The newest checkpoint the run recorded, or `None` before its first
41 /// [`AgentEvent::Checkpoint`](crate::AgentEvent::Checkpoint).
42 #[must_use]
43 pub fn latest(&self) -> Option<AgentCheckpoint> {
44 self.0.load_full().map(|checkpoint| (*checkpoint).clone())
45 }
46
47 pub(crate) fn record(&self, checkpoint: AgentCheckpoint) {
48 self.0.store(Some(Arc::new(checkpoint)));
49 }
50}