Skip to content

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

chore(flows): add tinyflows-drivers test fixtures and reformat call sites

Steven Enamakela122c4a227a0
diff --git a/crates/openhuman-core/Cargo.toml b/crates/openhuman-core/Cargo.toml
index de43d18c26..cc363aec52 100644
--- a/crates/openhuman-core/Cargo.toml
+++ b/crates/openhuman-core/Cargo.toml
@@ -636,6 +636,9 @@ tinymemory-api = { path = "../../vendor/tinymemory/crates/tinymemory-api", featu
# write path can produce. Declared here rather than in `[dependencies]` so the
# shipped binary never carries those two writers.
tinyflows-sqlite = { path = "../../vendor/tinyflows/crates/tinyflows-sqlite", features = ["test-fixtures"] }
+# The document catalog's `*_for_test` fixtures, which the flows store forwards
+# in test builds.
+tinyflows-drivers = { path = "../../vendor/tinyflows/crates/tinyflows-drivers", default-features = false, features = ["schedule", "test-fixtures"] }
# Test fixtures derive a known account from the BIP-39 vector phrase. Production
# does not derive at all — see the note on the main `tinywallet` entry. Cargo
# does not link dev-dependency features into the shipped binary, so enabling
diff --git a/crates/openhuman-core/src/agent/orchestration/delegation.rs b/crates/openhuman-core/src/agent/orchestration/delegation.rs
index 9afa6f4ba7..453415c22a 100644
--- a/crates/openhuman-core/src/agent/orchestration/delegation.rs
+++ b/crates/openhuman-core/src/agent/orchestration/delegation.rs
@@ -88,12 +88,11 @@ pub(crate) async fn run_subagent_delegation_with_parent_context(
match crate::storage::current_scoped()
.map_err(|e| format!("resolve the storage scope for graph checkpoints: {e}"))?
{
- Some(scoped) => Arc::new(
- tinyagents_graph::checkpoint::DriverCheckpointer::<DelegationState>::with_prefix(
- Arc::clone(scoped.documents()),
- "delegation_graph",
- ),
- ),
+ Some(scoped) => Arc::new(tinyagents_graph::checkpoint::DriverCheckpointer::<
+ DelegationState,
+ >::with_prefix(
+ Arc::clone(scoped.documents()), "delegation_graph"
+ )),
None => {
let checkpoint_db = config.workspace_dir.join("graph_checkpoints.db");
Arc::new(
diff --git a/crates/openhuman-core/src/cron/store.rs b/crates/openhuman-core/src/cron/store.rs
index 6493fa54ae..8956f165f3 100644
--- a/crates/openhuman-core/src/cron/store.rs
+++ b/crates/openhuman-core/src/cron/store.rs
@@ -12,9 +12,9 @@
use crate::config::Config;
use anyhow::Result;
use chrono::{DateTime, Utc};
+use tinyflows_drivers::schedule::CronDocuments;
use tinyflows_schedule::DeliveryStatus;
use tinyflows_schedule::{CronJob, CronJobPatch, CronRun, DeliveryConfig, Schedule, SessionTarget};
-use tinyflows_drivers::schedule::CronDocuments;
use tinyflows_sqlite::schedule::{self as upstream, AgentJobSpec, CronStoreOptions};
/// Builds the store options from the host config: `<workspace>/cron/jobs.db`,
@@ -76,7 +76,18 @@ pub fn add_agent_job(
) -> Result<CronJob> {
if let Some(docs) = documents(config)? {
let prompt = prompt.to_string();
- return run(async move { docs.add_agent_job(name, schedule, &prompt, session_target, model, delivery, delete_after_run).await });
+ return run(async move {
+ docs.add_agent_job(
+ name,
+ schedule,
+ &prompt,
+ session_target,
+ model,
+ delivery,
+ delete_after_run,
+ )
+ .await
+ });
}
upstream::add_agent_job(
&opts(config),
@@ -107,7 +118,20 @@ pub fn add_agent_job_with_definition(
) -> Result<CronJob> {
if let Some(docs) = documents(config)? {
let prompt = prompt.to_string();
- return run(async move { docs.add_agent_job_with_definition(name, schedule, &prompt, session_target, model, delivery, delete_after_run, agent_id, enabled).await });
+ return run(async move {
+ docs.add_agent_job_with_definition(
+ name,
+ schedule,
+ &prompt,
+ session_target,
+ model,
+ delivery,
+ delete_after_run,
+ agent_id,
+ enabled,
+ )
+ .await
+ });
}
upstream::add_agent_job_with_definition(
&opts(config),
@@ -220,7 +244,10 @@ pub fn record_last_run(
) -> Result<()> {
if let Some(docs) = documents(config)? {
let (job_id, output) = (job_id.to_string(), output.to_string());
- return run(async move { docs.record_last_run(&job_id, finished_at, success, &output).await });
+ return run(async move {
+ docs.record_last_run(&job_id, finished_at, success, &output)
+ .await
+ });
}
upstream::record_last_run(&opts(config), job_id, finished_at, success, output)
}
@@ -248,8 +275,22 @@ pub fn record_run(
duration_ms: i64,
) -> Result<()> {
if let Some(docs) = documents(config)? {
- let (job_id, status, output) = (job_id.to_string(), status.to_string(), output.map(str::to_string));
- return run(async move { docs.record_run(&job_id, started_at, finished_at, &status, output.as_deref(), duration_ms).await });
+ let (job_id, status, output) = (
+ job_id.to_string(),
+ status.to_string(),
+ output.map(str::to_string),
+ );
+ return run(async move {
+ docs.record_run(
+ &job_id,
+ started_at,
+ finished_at,
+ &status,
+ output.as_deref(),
+ duration_ms,
+ )
+ .await
+ });
}
upstream::record_run(
&opts(config),
@@ -275,8 +316,23 @@ pub fn record_run_with_delivery(
delivery_status: Option<DeliveryStatus>,
) -> Result<()> {
if let Some(docs) = documents(config)? {
- let (job_id, status, output) = (job_id.to_string(), status.to_string(), output.map(str::to_string));
- return run(async move { docs.record_run_with_delivery(&job_id, started_at, finished_at, &status, output.as_deref(), duration_ms, delivery_status).await });
+ let (job_id, status, output) = (
+ job_id.to_string(),
+ status.to_string(),
+ output.map(str::to_string),
+ );
+ return run(async move {
+ docs.record_run_with_delivery(
+ &job_id,
+ started_at,
+ finished_at,
+ &status,
+ output.as_deref(),
+ duration_ms,
+ delivery_status,
+ )
+ .await
+ });
}
upstream::record_run_with_delivery(
&opts(config),
diff --git a/crates/openhuman-core/src/flows/bus/dedup_commit.rs b/crates/openhuman-core/src/flows/bus/dedup_commit.rs
index c8bee21a1d..8116d8a904 100644
--- a/crates/openhuman-core/src/flows/bus/dedup_commit.rs
+++ b/crates/openhuman-core/src/flows/bus/dedup_commit.rs
@@ -5,13 +5,13 @@
use crate::config::Config;
use crate::core::events::DomainEvent;
use crate::flows::store;
+use crate::flows::tinyflows::state::FlowState;
use async_trait::async_trait;
use std::collections::HashMap;
use std::sync::{Arc, LazyLock, Mutex};
use tinybus::EventHandler;
use tinyflows::model::NodeKind;
use tinyflows::nodes::control_flow::dedup_settle::{self, CommitOutcome, Settlement};
-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
diff --git a/crates/openhuman-core/src/flows/store.rs b/crates/openhuman-core/src/flows/store.rs
index e985f77709..d5e39bc9c4 100644
--- a/crates/openhuman-core/src/flows/store.rs
+++ b/crates/openhuman-core/src/flows/store.rs
@@ -22,8 +22,8 @@ use tinyflows_catalog::{
Flow, FlowRevision, FlowRun, FlowRunStep, FlowSuggestion, SuggestionStatus,
};
-pub use tinyflows_sqlite::flows::{FlowUpdateError, MAX_FLOW_RUNS_PER_FLOW};
use tinyflows_drivers::catalog::FlowCatalogDocuments;
+pub use tinyflows_sqlite::flows::{FlowUpdateError, MAX_FLOW_RUNS_PER_FLOW};
/// The document catalog for this call when the host configured a storage
/// backend ([`crate::storage`]), in the acting agent's scope.
@@ -39,7 +39,6 @@ pub(crate) fn run<T: Send + 'static>(
crate::storage::block_on_anyhow(future)
}
-
/// Where this host keeps the flow catalog: `<workspace_dir>/flows`.
///
/// `flows.db`, `checkpoints.db` and the `drafts/` directory are all created
@@ -78,7 +77,10 @@ pub fn create_flow(
enabled: bool,
) -> Result<Flow> {
if let Some(docs) = documents(config)? {
- return run(async move { docs.create_flow(name, graph, require_approval, enabled).await });
+ return run(async move {
+ docs.create_flow(name, graph, require_approval, enabled)
+ .await
+ });
}
tinyflows_sqlite::flows::create_flow(&dir(config), name, graph, require_approval, enabled)
}
@@ -146,8 +148,20 @@ pub fn update_flow_graph(
if let Some(docs) = documents(config).map_err(FlowUpdateError::Store)? {
let id = id.to_string();
let expected_updated_at = expected_updated_at.map(str::to_string);
- return crate::storage::block_on(async move { Ok(docs.update_flow_graph(&id, name, graph, require_approval, enabled_override, force_disarm_if_automatic, expected_updated_at.as_deref()).await) })
- .map_err(|error| FlowUpdateError::Store(error.into()))?;
+ return crate::storage::block_on(async move {
+ Ok(docs
+ .update_flow_graph(
+ &id,
+ name,
+ graph,
+ require_approval,
+ enabled_override,
+ force_disarm_if_automatic,
+ expected_updated_at.as_deref(),
+ )
+ .await)
+ })
+ .map_err(|error| FlowUpdateError::Store(error.into()))?;
}
tinyflows_sqlite::flows::update_flow_graph(
&dir(config),
@@ -239,7 +253,10 @@ pub fn insert_flow_run(
let flow_id = flow_id.to_string();
let thread_id = thread_id.to_string();
let started_at = started_at.to_string();
- return run(async move { docs.insert_flow_run(&id, &flow_id, &thread_id, &started_at).await });
+ return run(async move {
+ docs.insert_flow_run(&id, &flow_id, &thread_id, &started_at)
+ .await
+ });
}
tinyflows_sqlite::flows::insert_flow_run(&dir(config), id, flow_id, thread_id, started_at)
}
@@ -274,7 +291,18 @@ pub fn finish_flow_run(
let pending_approvals = pending_approvals.to_vec();
let error = error.map(str::to_string);
let graph_hash = graph_hash.map(str::to_string);
- return run(async move { docs.finish_flow_run(&id, &status, &finished_at, &steps, &pending_approvals, error.as_deref(), graph_hash.as_deref()).await });
+ return run(async move {
+ docs.finish_flow_run(
+ &id,
+ &status,
+ &finished_at,
+ &steps,
+ &pending_approvals,
+ error.as_deref(),
+ graph_hash.as_deref(),
+ )
+ .await
+ });
}
tinyflows_sqlite::flows::finish_flow_run(
&dir(config),
@@ -345,7 +373,10 @@ pub fn force_run_status_for_test(
let id = id.to_string();
let status = status.to_string();
let error = error.map(str::to_string);
- return run(async move { docs.force_run_status_for_test(&id, &status, error.as_deref()).await });
+ return run(async move {
+ docs.force_run_status_for_test(&id, &status, error.as_deref())
+ .await
+ });
}
tinyflows_sqlite::flows::force_run_status_for_test(&dir(config), id, status, error)
}
@@ -364,7 +395,10 @@ pub fn force_corrupt_graph_json_for_test(
if let Some(docs) = documents(config)? {
let flow_id = flow_id.to_string();
let raw_graph_json = raw_graph_json.to_string();
- return run(async move { docs.force_corrupt_graph_json_for_test(&flow_id, &raw_graph_json).await });
+ return run(async move {
+ docs.force_corrupt_graph_json_for_test(&flow_id, &raw_graph_json)
+ .await
+ });
}
tinyflows_sqlite::flows::force_corrupt_graph_json_for_test(
&dir(config),
diff --git a/crates/openhuman-core/src/flows/tinyflows/caps/ops.rs b/crates/openhuman-core/src/flows/tinyflows/caps/ops.rs
index 7331942c4c..176bb393e3 100644
--- a/crates/openhuman-core/src/flows/tinyflows/caps/ops.rs
+++ b/crates/openhuman-core/src/flows/tinyflows/caps/ops.rs
@@ -672,11 +672,9 @@ pub fn open_flow_checkpointer(
) -> anyhow::Result<Arc<dyn tinyflows::engine::Checkpointer<serde_json::Value>>> {
if let Some(scoped) = crate::storage::current_scoped()? {
tracing::debug!(target: "flows", "[flows] opening checkpointer on the storage backend");
- return Ok(Arc::new(
- tinyflows_drivers::DriverCheckpointer::<serde_json::Value>::new(Arc::clone(
- scoped.documents(),
- )),
- ));
+ return Ok(Arc::new(tinyflows_drivers::DriverCheckpointer::<
+ serde_json::Value,
+ >::new(Arc::clone(scoped.documents()))));
}
let db_path = config.workspace_dir.join("flows").join("checkpoints.db");
if let Some(parent) = db_path.parent() {
diff --git a/crates/openhuman-core/src/flows/tinyflows/state_tests.rs b/crates/openhuman-core/src/flows/tinyflows/state_tests.rs
new file mode 100644
index 0000000000..a37ea0ea2c
--- /dev/null
+++ b/crates/openhuman-core/src/flows/tinyflows/state_tests.rs
@@ -0,0 +1,74 @@
+use super::*;
+use crate::storage::{MemoryStorage, Scope, StorageBackend};
+use serde_json::json;
+use tinyflows_drivers::catalog::FlowCatalogDocuments;
+
+fn documents(namespace: &str) -> FlowState {
+ let scoped = MemoryStorage::new()
+ .for_scope(&Scope::new("local").unwrap())
+ .unwrap();
+ FlowState::Documents(FlowStateDocuments::new(
+ FlowCatalogDocuments::new(Arc::clone(scoped.documents())),
+ namespace,
+ ))
+}
+
+fn sqlite(dir: &std::path::Path, namespace: &str) -> FlowState {
+ FlowState::Sqlite(SqliteStateStore::new(dir, 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_sqlite_store_backs_both_views() {
+ let dir = tempfile::tempdir().unwrap();
+ engine_and_dedup_share_records(sqlite(dir.path(), "flow:a")).await;
+}
+
+#[tokio::test(flavor = "multi_thread")]
+async fn namespaces_keep_flows_apart() {
+ let dir = tempfile::tempdir().unwrap();
+ let (a, b) = (sqlite(dir.path(), "flow:a"), sqlite(dir.path(), "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_sqlite() {
+ let dir = tempfile::tempdir().unwrap();
+ let config = Config {
+ workspace_dir: dir.path().to_path_buf(),
+ ..Config::default()
+ };
+ if crate::storage::installed().is_none() {
+ assert!(matches!(
+ FlowState::open(&config, "flow:a"),
+ FlowState::Sqlite(_)
+ ));
+ }
+}