Skip to main content

agent_hub_runtime/core/state_store/
mod.rs

1//! Shared local state primitives. Callers retain their namespace, schema and storage ownership.
2use anyhow::{Context, Result};
3use serde_json::Value;
4use std::path::{Path, PathBuf};
5
6/// Python callers use the same transactional implementation instead of a task-local copy.
7pub fn python_module() -> Result<PathBuf> {
8    super::python_support::publish("state_store", include_str!("store.py"))
9}
10
11/// Read a checkpoint without resetting missing or malformed state or following a file symlink.
12pub fn read_json(path: &Path) -> Result<Option<Value>> {
13    let metadata = match path.symlink_metadata() {
14        Ok(metadata) => metadata,
15        Err(error) if error.kind() == std::io::ErrorKind::NotFound => return Ok(None),
16        Err(error) => return Err(error.into()),
17    };
18    anyhow::ensure!(
19        metadata.is_file() && !metadata.file_type().is_symlink(),
20        "State must be a regular file"
21    );
22    Ok(Some(
23        serde_json::from_slice(&std::fs::read(path)?)
24            .context("State format is invalid; preserve the existing record")?,
25    ))
26}
27
28#[cfg(test)]
29mod tests;