Skip to content

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

refactor(tinyflows): route flow state through FlowState wrapper

Steven Enamakel6e08491c3e55
diff --git a/crates/openhuman-core/src/flows/bus/dedup_commit.rs b/crates/openhuman-core/src/flows/bus/dedup_commit.rs
index 93a8aa75e5..c8bee21a1d 100644
--- a/crates/openhuman-core/src/flows/bus/dedup_commit.rs
+++ b/crates/openhuman-core/src/flows/bus/dedup_commit.rs
@@ -11,7 +11,7 @@ use std::sync::{Arc, LazyLock, Mutex};
use tinybus::EventHandler;
use tinyflows::model::NodeKind;
use tinyflows::nodes::control_flow::dedup_settle::{self, CommitOutcome, Settlement};
-use tinyflows_sqlite::flows::SqliteStateStore;
+use crate::flows::tinyflows::state::FlowState;
/// Listens for `DomainEvent::FlowRunFinished` and settles every `dedup` node
/// in the finished flow's graph — the host half of the commit-on-success
@@ -246,7 +246,7 @@ impl DedupCommitSubscriber {
tracing::trace!(target: "flows", %flow_id, %run_id, "[dedup-commit] acquired per-flow commit lock");
self.maybe_test_delay().await;
- let state = SqliteStateStore::new(store::dir(&self.config), format!("flow:{flow_id}"));
+ let state = FlowState::open(&self.config, format!("flow:{flow_id}"));
for node_id in node_ids {
self.settle_node(&state, &node_id, success, flow_id, run_id);
}
@@ -259,7 +259,7 @@ impl DedupCommitSubscriber {
/// `tinyflows`' pure commit/release logic, logging each outcome.
fn settle_node(
&self,
- store: &SqliteStateStore,
+ store: &FlowState,
node_id: &str,
success: bool,
flow_id: &str,
diff --git a/crates/openhuman-core/src/flows/tinyflows/caps/ops.rs b/crates/openhuman-core/src/flows/tinyflows/caps/ops.rs
index 1a043215b0..7331942c4c 100644
--- a/crates/openhuman-core/src/flows/tinyflows/caps/ops.rs
+++ b/crates/openhuman-core/src/flows/tinyflows/caps/ops.rs
@@ -590,7 +590,8 @@ impl ToolInvoker for OpenHumanTools {
/// and [`super::memory_adapter::OpenHumanMemory`] for `memory`, for its
/// contract).
///
-/// `state_namespace` scopes the [`tinyflows_sqlite::flows::SqliteStateStore`] KV so two saved flows that
+/// `state_namespace` scopes the flow state KV ([`crate::flows::tinyflows::state::FlowState`]:
+/// `flows.db`, or the storage backend when one is configured) so two saved flows that
/// use the same state key never read or overwrite each other — callers pass a
/// per-flow namespace (e.g. `"flow:<id>"`). This KV namespace is unrelated to
/// flow-scoped memory: `OpenHumanMemory` tags memory items with
@@ -621,10 +622,8 @@ pub fn build_capabilities(config: Arc<Config>, state_namespace: impl Into<String
config: config.clone(),
security: security.clone(),
}),
- state: Arc::new(tinyflows_sqlite::flows::SqliteStateStore::new(
- crate::flows::store::dir(&config),
- state_namespace,
- )),
+ state: crate::flows::tinyflows::state::FlowState::open(&config, state_namespace)
+ .into_state_store(),
agent: Some(Arc::new(OpenHumanAgentRunner {
config: config.clone(),
})),
diff --git a/crates/openhuman-core/src/flows/tinyflows/mod.rs b/crates/openhuman-core/src/flows/tinyflows/mod.rs
index 95ce70fc2b..457927b27d 100644
--- a/crates/openhuman-core/src/flows/tinyflows/mod.rs
+++ b/crates/openhuman-core/src/flows/tinyflows/mod.rs
@@ -19,6 +19,7 @@ pub mod caps;
pub mod langfuse_export;
pub mod memory_adapter;
pub mod observability;
+pub mod state;
#[cfg(test)]
#[path = "tinyflows_tests.rs"]
mod tests;
diff --git a/crates/openhuman-core/src/flows/tinyflows/state.rs b/crates/openhuman-core/src/flows/tinyflows/state.rs
new file mode 100644
index 0000000000..482257958d
--- /dev/null
+++ b/crates/openhuman-core/src/flows/tinyflows/state.rs
@@ -0,0 +1,113 @@
+//! 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. Without a
+//! storage backend that is `tinyflows_sqlite::flows::SqliteStateStore` over
+//! `flows/flows.db`; with one it is
+//! `tinyflows_drivers::catalog::FlowStateDocuments` in the acting agent's
+//! scope. When the scope cannot be resolved (SaaS mode with no acting agent)
+//! every call fails instead of falling back to the local file.
+
+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 tinyflows_sqlite::flows::SqliteStateStore;
+
+use crate::config::Config;
+
+/// One flow's state namespace on the store this call resolves to.
+#[derive(Debug, Clone)]
+pub enum FlowState {
+ /// `flows/flows.db` (no storage backend).
+ Sqlite(SqliteStateStore),
+ /// The storage backend's catalog.
+ 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(Some(catalog)) => Self::Documents(FlowStateDocuments::new(catalog, namespace)),
+ Ok(None) => Self::Sqlite(SqliteStateStore::new(
+ crate::flows::store::dir(config),
+ 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::Sqlite(store) => store.load(key).await,
+ 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::Sqlite(store) => store.store(key, value).await,
+ 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::Sqlite(store) => store.kv_get(key),
+ 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::Sqlite(store) => store.kv_set(key, value),
+ 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::Sqlite(store) => store.kv_delete(key),
+ Self::Documents(store) => store.kv_delete(key),
+ Self::Unavailable(reason) => Err(Self::unavailable(reason)),
+ }
+ }
+}
+
+#[cfg(test)]
+#[path = "state_tests.rs"]
+mod tests;