Skip to content

The fastest, cheapest, most efficient open-source agent harness. Run more than 500 agents on a $10 VPS.

refactor(flows): remove tinyflows flow state module

Steven Enamakel3df6fe9aaa30
diff --git a/crates/openhuman-core/src/flows/tinyflows/state.rs b/crates/openhuman-core/src/flows/tinyflows/state.rs
deleted file mode 100644
index 024909f934..0000000000
--- a/crates/openhuman-core/src/flows/tinyflows/state.rs
+++ /dev/null
@@ -1,100 +0,0 @@
-//! A flow's namespaced state (`flow:<id>`), on whichever store this host uses.
-//!
-//! The engine reads and writes it through `StateStore` during a run, and the
-//! dedup settlement (`flows::bus::dedup_commit`) reads it back through
-//! `DedupKv` after the run, so both must name the same records. They are
-//! `tinyflows_drivers::catalog::FlowStateDocuments` in the acting agent's scope
-//! of the flow catalog's storage (`flows::store`). When the scope cannot be
-//! resolved (SaaS mode with no acting agent) every call fails instead of
-//! falling back to a shared bucket.
-
-use std::sync::Arc;
-
-use async_trait::async_trait;
-use serde_json::Value;
-use tinyflows::caps::StateStore;
-use tinyflows::error::{EngineError, Result as EngineResult};
-use tinyflows::nodes::control_flow::dedup_settle::DedupKv;
-use tinyflows_drivers::catalog::FlowStateDocuments;
-
-use crate::config::Config;
-
-/// One flow's state namespace on the store this call resolves to.
-#[derive(Debug, Clone)]
-pub enum FlowState {
- /// The flow catalog's storage.
- Documents(FlowStateDocuments),
- /// The storage scope could not be resolved; every call fails with this.
- Unavailable(String),
-}
-
-impl FlowState {
- /// The state of `namespace` for this call.
- pub fn open(config: &Config, namespace: impl Into<String>) -> Self {
- let namespace = namespace.into();
- match crate::flows::store::documents(config) {
- Ok(catalog) => Self::Documents(FlowStateDocuments::new(catalog, namespace)),
- Err(error) => {
- tracing::warn!(
- target: "flows",
- %error,
- "[flows] flow state unavailable: the storage scope did not resolve"
- );
- Self::Unavailable(error.to_string())
- }
- }
- }
-
- /// As the engine's `StateStore` capability.
- pub fn into_state_store(self) -> Arc<dyn StateStore> {
- Arc::new(self)
- }
-
- fn unavailable(reason: &str) -> String {
- format!("flow state unavailable: {reason}")
- }
-}
-
-#[async_trait]
-impl StateStore for FlowState {
- async fn load(&self, key: &str) -> EngineResult<Option<Value>> {
- match self {
- Self::Documents(store) => store.load(key).await,
- Self::Unavailable(reason) => Err(EngineError::Capability(Self::unavailable(reason))),
- }
- }
-
- async fn store(&self, key: &str, value: Value) -> EngineResult<()> {
- match self {
- Self::Documents(store) => store.store(key, value).await,
- Self::Unavailable(reason) => Err(EngineError::Capability(Self::unavailable(reason))),
- }
- }
-}
-
-impl DedupKv for FlowState {
- fn kv_get(&self, key: &str) -> std::result::Result<Option<Value>, String> {
- match self {
- Self::Documents(store) => store.kv_get(key),
- Self::Unavailable(reason) => Err(Self::unavailable(reason)),
- }
- }
-
- fn kv_set(&self, key: &str, value: &Value) -> std::result::Result<(), String> {
- match self {
- Self::Documents(store) => store.kv_set(key, value),
- Self::Unavailable(reason) => Err(Self::unavailable(reason)),
- }
- }
-
- fn kv_delete(&self, key: &str) -> std::result::Result<(), String> {
- match self {
- Self::Documents(store) => store.kv_delete(key),
- Self::Unavailable(reason) => Err(Self::unavailable(reason)),
- }
- }
-}
-
-#[cfg(test)]
-#[path = "state_tests.rs"]
-mod tests;
diff --git a/crates/openhuman-core/src/flows/tinyflows/state_tests.rs b/crates/openhuman-core/src/flows/tinyflows/state_tests.rs
deleted file mode 100644
index ae4abbc0e7..0000000000
--- a/crates/openhuman-core/src/flows/tinyflows/state_tests.rs
+++ /dev/null
@@ -1,80 +0,0 @@
-use super::*;
-use crate::storage::{MemoryStorage, Scope, StorageBackend};
-use serde_json::json;
-use tinyflows_drivers::catalog::FlowCatalogDocuments;
-
-fn documents(namespace: &str) -> FlowState {
- documents_on(&MemoryStorage::new(), namespace)
-}
-
-fn documents_on(storage: &MemoryStorage, namespace: &str) -> FlowState {
- let scoped = storage.for_scope(&Scope::new("local").unwrap()).unwrap();
- FlowState::Documents(FlowStateDocuments::new(
- FlowCatalogDocuments::new(Arc::clone(scoped.documents())),
- namespace,
- ))
-}
-
-/// What a run stores through `StateStore` is what dedup settlement reads
-/// through `DedupKv`, on both stores.
-async fn engine_and_dedup_share_records(state: FlowState) {
- state.store("seen", json!(["a"])).await.unwrap();
- assert_eq!(state.kv_get("seen").unwrap(), Some(json!(["a"])));
- state.kv_set("seen", &json!(["a", "b"])).unwrap();
- assert_eq!(state.load("seen").await.unwrap(), Some(json!(["a", "b"])));
- state.kv_delete("seen").unwrap();
- assert_eq!(state.load("seen").await.unwrap(), None);
-}
-
-#[tokio::test(flavor = "multi_thread")]
-async fn the_document_store_backs_both_views() {
- engine_and_dedup_share_records(documents("flow:a")).await;
-}
-
-#[tokio::test(flavor = "multi_thread")]
-async fn the_workspace_database_backs_both_views() {
- let dir = tempfile::tempdir().unwrap();
- let config = Config {
- workspace_dir: dir.path().to_path_buf(),
- ..Config::default()
- };
- engine_and_dedup_share_records(FlowState::open(&config, "flow:a")).await;
-}
-
-#[tokio::test(flavor = "multi_thread")]
-async fn namespaces_keep_flows_apart() {
- let storage = MemoryStorage::new();
- let (a, b) = (
- documents_on(&storage, "flow:a"),
- documents_on(&storage, "flow:b"),
- );
- a.store("k", json!(1)).await.unwrap();
- assert_eq!(b.load("k").await.unwrap(), None);
-}
-
-#[tokio::test(flavor = "current_thread")]
-async fn an_unresolved_scope_fails_every_call() {
- let state = FlowState::Unavailable("no acting agent".to_string());
- assert!(state.load("k").await.is_err());
- assert!(state.store("k", json!(1)).await.is_err());
- let error = state.kv_get("k").unwrap_err();
- assert!(error.contains("no acting agent"), "{error}");
- assert!(state.kv_set("k", &json!(1)).is_err());
- assert!(state.kv_delete("k").is_err());
-}
-
-#[test]
-fn without_a_backend_the_flow_state_is_in_the_workspace_default_database() {
- let dir = tempfile::tempdir().unwrap();
- let config = Config {
- workspace_dir: dir.path().to_path_buf(),
- ..Config::default()
- };
- // Nothing in this binary installs a backend, so the slot is empty.
- assert!(crate::storage::installed().is_none());
- assert!(matches!(
- FlowState::open(&config, "flow:a"),
- FlowState::Documents(_)
- ));
- assert!(config.workspace_dir.join("storage/storage.db").is_file());
-}