Skip to content

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

refactor(flows): extract flow namespace filter helper

Steven Enamakel90af7c06feae
diff --git a/crates/openhuman-core/src/flows/catalog_storage.rs b/crates/openhuman-core/src/flows/catalog_storage.rs
index 6cef552751..7735ac85d3 100644
--- a/crates/openhuman-core/src/flows/catalog_storage.rs
+++ b/crates/openhuman-core/src/flows/catalog_storage.rs
@@ -267,6 +267,14 @@ fn allowed_set(collection: &str) -> Result<(), StorageError> {
}
}
+fn flow_namespace_filter(collection: &str, filter: Filter) -> Filter {
+ if GRAPH_COLLECTIONS.contains(&collection) {
+ Filter::eq("ns", "flows").and(filter)
+ } else {
+ filter
+ }
+}
+
fn encoded<T: Serialize>(value: T) -> Result<Value, StorageError> {
serde_json::to_value(value)
.map_err(|error| StorageError::invalid_input(format!("catalog value encoding failed: {error}")))
@@ -388,9 +396,7 @@ impl CatalogStoragePort {
let result = async {
allowed_set(&request.collection)?;
let mut query: Query = decoded(encoded(request.query)?)?;
- if GRAPH_COLLECTIONS.contains(&request.collection.as_str()) {
- query.filter = Filter::eq("ns", "flows").and(query.filter);
- }
+ query.filter = flow_namespace_filter(&request.collection, query.filter);
let page: Page<Versioned<Value>> = self
.scope
.documents()?
@@ -425,10 +431,8 @@ impl CatalogStoragePort {
) -> BusResult<CatalogStorageReply> {
let result = async {
allowed_set(&request.collection)?;
- let mut filter: Filter = decoded(encoded(request.filter)?)?;
- if GRAPH_COLLECTIONS.contains(&request.collection.as_str()) {
- filter = Filter::eq("ns", "flows").and(filter);
- }
+ let filter: Filter = decoded(encoded(request.filter)?)?;
+ let filter = flow_namespace_filter(&request.collection, filter);
self.scope
.documents()?
.delete_where(&request.collection, &filter)
diff --git a/crates/openhuman-core/src/flows/catalog_storage_tests.rs b/crates/openhuman-core/src/flows/catalog_storage_tests.rs
new file mode 100644
index 0000000000..6d13e6ff4a
--- /dev/null
+++ b/crates/openhuman-core/src/flows/catalog_storage_tests.rs
@@ -0,0 +1,141 @@
+use super::*;
+use serde_json::json;
+
+#[test]
+fn storage_paths_are_stable_and_unambiguous_for_workspace_and_scope() {
+ let key = RegistrationKey {
+ workspace_dir: PathBuf::from("/workspace/a"),
+ scope: "u:b".into(),
+ };
+ let same = RegistrationKey {
+ workspace_dir: PathBuf::from("/workspace/a"),
+ scope: "u:b".into(),
+ };
+ let different_workspace = RegistrationKey {
+ workspace_dir: PathBuf::from("/workspace/a/u:b"),
+ scope: "local".into(),
+ };
+ assert_eq!(path_for(&key), path_for(&same));
+ assert_ne!(path_for(&key), path_for(&different_workspace));
+}
+
+#[test]
+fn document_records_cannot_address_other_shared_or_graph_keys() {
+ for collection in [
+ "tsd.kv",
+ "tsd.leases",
+ "tsd.counters",
+ "graph.checkpoints",
+ "graph.threads",
+ "graph.writes",
+ ] {
+ assert!(allowed_record(collection, "other-owner/key").is_err(), "{collection}");
+ assert!(allowed_record(collection, "flows/thread/checkpoint").is_ok(), "{collection}");
+ }
+ for (collection, id) in [
+ ("tsd.migrations", "other/definitions_v2"),
+ ("tsd.migrations", "flows/unknown/migration"),
+ ("account.credentials", "flows/secret"),
+ ] {
+ assert!(allowed_record(collection, id).is_err());
+ }
+}
+
+#[test]
+fn graph_query_and_delete_filters_are_confined_to_the_flows_namespace() {
+ let all = flow_namespace_filter("graph.checkpoints", Filter::All);
+ assert!(all.matches("flows/thread/1", &json!({"ns":"flows"})));
+ assert!(!all.matches("other/thread/1", &json!({"ns":"other"})));
+
+ let conflicting = flow_namespace_filter(
+ "graph.writes",
+ Filter::eq("ns", "other"),
+ );
+ assert!(!conflicting.matches("flows/thread/1", &json!({"ns":"flows"})));
+ assert!(!conflicting.matches("other/thread/1", &json!({"ns":"other"})));
+
+ let widened = flow_namespace_filter(
+ "graph.threads",
+ Filter::one_of("ns", ["flows", "other"]),
+ );
+ assert!(widened.matches("flows/thread", &json!({"ns":"flows"})));
+ assert!(!widened.matches("other/thread", &json!({"ns":"other"})));
+}
+
+#[tokio::test]
+async fn cancelling_the_caller_after_serve_keeps_one_cached_registration() {
+ use crate::core::runtime::{ContextOverlay, CoreContext, DispatchCtx, DomainSet};
+ use crate::tools::toolpacks::ToolGroups;
+
+ let workspace = tempfile::tempdir().expect("isolated workspace");
+ let config = Config {
+ workspace_dir: workspace.path().to_path_buf(),
+ ..Config::default()
+ };
+ let root = CoreContext::for_test_with_config(DomainSet::full(), config.clone());
+ let core = root.derive_with(
+ ContextOverlay::new(config.clone(), DomainSet::full(), ToolGroups::default())
+ .profile("catalog-storage-cancellation-test"),
+ );
+ let context = DispatchCtx::new(core);
+ let scope = crate::storage::scope_for_profile("catalog-storage-cancellation-test");
+ let key = RegistrationKey {
+ workspace_dir: config.workspace_dir.clone(),
+ scope: scope.as_str().to_owned(),
+ };
+ let path = path_for(&key);
+ let (entered_tx, entered_rx) = tokio::sync::oneshot::channel();
+ let (release_tx, release_rx) = tokio::sync::oneshot::channel();
+ REGISTRATION_GATES
+ .lock()
+ .unwrap_or_else(std::sync::PoisonError::into_inner)
+ .insert(
+ path.clone(),
+ RegistrationGate {
+ entered: entered_tx,
+ release: release_rx,
+ },
+ );
+
+ let caller_context = context.clone();
+ let caller_config = config.clone();
+ let caller = tokio::spawn(async move { register(&caller_context, &caller_config).await });
+ entered_rx.await.expect("module task served the callback");
+ caller.abort();
+ let _ = caller.await;
+ release_tx.send(()).expect("finish cache insertion");
+
+ let first = register(&context, &config)
+ .await
+ .expect("the completed process task committed its registration");
+ let second = register(&context, &config)
+ .await
+ .expect("the cache returns the same scoped registration");
+ assert!(Arc::ptr_eq(&first, &second));
+ assert_eq!(first.storage().callback.object_path, path);
+ assert_eq!(
+ REGISTRATION_SERVE_COUNTS
+ .lock()
+ .unwrap_or_else(std::sync::PoisonError::into_inner)
+ .get(&path),
+ Some(&1),
+ "retry must not serve a replacement after the cancelled caller"
+ );
+
+ let runtime = crate::modules::runtime::runtime()
+ .await
+ .expect("module runtime remains available");
+ let proxy = runtime
+ .connection()
+ .proxy(
+ module::HOST_BUS_NAME,
+ &path,
+ catalog::STORAGE_INTERFACE,
+ )
+ .expect("storage callback address is valid");
+ let reply: CatalogStorageReply = proxy
+ .call(catalog::storage::CAPABILITIES, &())
+ .await
+ .expect("cached callback remains served");
+ assert!(matches!(reply, CatalogStorageReply::Value { .. }));
+}
diff --git a/crates/openhuman-core/src/flows/store.rs b/crates/openhuman-core/src/flows/store.rs
deleted file mode 100644
index f612caf446..0000000000
--- a/crates/openhuman-core/src/flows/store.rs
+++ /dev/null
@@ -1,451 +0,0 @@
-//! This host's binding of the flow catalog to its workspace.
-//!
-//! The catalog is `tinyflows_drivers::catalog::FlowCatalogDocuments`: flows,
-//! revisions, runs, state, suggestions and drafts as documents on the storage
-//! ports, in the acting agent's scope of the storage backend
-//! ([`crate::storage`]). tinyflows no
-//! longer has a SQLite store: the `flows.db`, `checkpoints.db` and
-//! `drafts/*.json` it wrote are imported once, read-only, when the backend is installed
-//! ([`crate::storage::import`]).
-//!
-//! Every function below is that one substitution and nothing else. They are
-//! spelled out rather than replaced by a `pub use` so the existing
-//! `store::*(config, …)` call sites keep resolving unchanged, and so the seam
-//! stays visible: anything appearing in one of these bodies beyond
-//! `documents(config)` is host policy that has leaked into persistence.
-
-use crate::config::Config;
-use anyhow::Result;
-use std::path::PathBuf;
-use tinyflows_catalog::store::FlowUpdateError;
-use tinyflows_catalog::{
- Flow, FlowRevision, FlowRun, FlowRunStep, FlowSuggestion, SuggestionStatus,
-};
-
-use tinyflows_drivers::catalog::FlowCatalogDocuments;
-
-/// Where tinyflows kept its flow files: `<workspace_dir>/flows`
-/// (`flows.db`, `checkpoints.db`, `drafts/`). Read once by the importer.
-pub fn dir(config: &Config) -> PathBuf {
- config.workspace_dir.join("flows")
-}
-
-/// The storage the flow catalog and its checkpoints use for this call: the
-/// storage backend in the acting scope ([`crate::storage::local::open`]).
-pub(crate) fn scoped(config: &Config) -> Result<crate::storage::ScopedStorage> {
- crate::storage::local::open("flows::store", &config.workspace_dir)?.scoped()
-}
-
-/// The document catalog for this call.
-pub(crate) fn documents(config: &Config) -> Result<FlowCatalogDocuments> {
- Ok(FlowCatalogDocuments::new(std::sync::Arc::clone(
- scoped(config)?.documents(),
- )))
-}
-
-/// Runs a document-catalog call from this synchronous API.
-pub(crate) fn run<T: Send + 'static>(
- future: impl std::future::Future<Output = Result<T>> + Send + 'static,
-) -> Result<T> {
- crate::storage::block_on_anyhow(future)
-}
-
-/// The catalog's `upsert_flow` for this call.
-#[inline]
-pub fn upsert_flow(config: &Config, flow: &Flow) -> Result<()> {
- let docs = documents(config)?;
- let flow = flow.clone();
- run(async move { docs.upsert_flow(&flow).await })
-}
-
-/// The catalog's `insert_duplicate_flow` for this call.
-#[inline]
-pub fn insert_duplicate_flow(config: &Config, source: &Flow, new_name: String) -> Result<Flow> {
- let docs = documents(config)?;
- let source = source.clone();
- run(async move { docs.insert_duplicate_flow(&source, new_name).await })
-}
-
-/// The catalog's `create_flow` for this call.
-#[inline]
-pub fn create_flow(
- config: &Config,
- name: String,
- graph: tinyflows::model::WorkflowGraph,
- require_approval: bool,
- enabled: bool,
-) -> Result<Flow> {
- let docs = documents(config)?;
- run(async move {
- docs.create_flow(name, graph, require_approval, enabled)
- .await
- })
-}
-
-/// The catalog's `get_flow` for this call.
-#[inline]
-pub fn get_flow(config: &Config, id: &str) -> Result<Option<Flow>> {
- let docs = documents(config)?;
- let id = id.to_string();
- run(async move { docs.get_flow(&id).await })
-}
-
-/// The catalog's `list_flows` for this call.
-#[inline]
-pub fn list_flows(config: &Config) -> Result<(Vec<Flow>, usize)> {
- let docs = documents(config)?;
- run(async move { docs.list_flows().await })
-}
-
-/// The catalog's `list_enabled_flows` for this call.
-#[inline]
-pub fn list_enabled_flows(config: &Config) -> Result<(Vec<Flow>, usize)> {
- let docs = documents(config)?;
- run(async move { docs.list_enabled_flows().await })
-}
-
-/// The catalog's `remove_flow` for this call.
-#[inline]
-pub fn remove_flow(config: &Config, id: &str) -> Result<()> {
- let docs = documents(config)?;
- let id = id.to_string();
- run(async move { docs.remove_flow(&id).await })
-}
-
-/// The catalog's `set_enabled` for this call.
-#[inline]
-pub fn set_enabled(config: &Config, id: &str, enabled: bool) -> Result<Flow> {
- let docs = documents(config)?;
- let id = id.to_string();
- run(async move { docs.set_enabled(&id, enabled).await })
-}
-
-/// The catalog's `update_flow_graph` for this call.
-#[inline]
-pub fn update_flow_graph(
- config: &Config,
- id: &str,
- name: String,
- graph: tinyflows::model::WorkflowGraph,
- require_approval: bool,
- enabled_override: Option<bool>,
- force_disarm_if_automatic: bool,
- expected_updated_at: Option<&str>,
-) -> std::result::Result<Flow, FlowUpdateError> {
- let docs = documents(config).map_err(FlowUpdateError::Store)?;
- let id = id.to_string();
- let expected_updated_at = expected_updated_at.map(str::to_string);
- 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()))?
-}
-
-/// The catalog's `list_revisions` for this call.
-#[inline]
-pub fn list_revisions(config: &Config, flow_id: &str, limit: usize) -> Result<Vec<FlowRevision>> {
- let docs = documents(config)?;
- let flow_id = flow_id.to_string();
- run(async move { docs.list_revisions(&flow_id, limit).await })
-}
-
-/// The catalog's `revision_by_id` for this call.
-#[inline]
-pub fn revision_by_id(
- config: &Config,
- flow_id: &str,
- revision_id: &str,
-) -> Result<Option<FlowRevision>> {
- let docs = documents(config)?;
- let flow_id = flow_id.to_string();
- let revision_id = revision_id.to_string();
- run(async move { docs.revision_by_id(&flow_id, &revision_id).await })
-}
-
-/// The catalog's `record_run` for this call.
-#[inline]
-pub fn record_run(config: &Config, id: &str, status: &str) -> Result<()> {
- let docs = documents(config)?;
- let id = id.to_string();
- let status = status.to_string();
- run(async move { docs.record_run(&id, &status).await })
-}
-
-/// The catalog's `kv_get` for this call.
-#[inline]
-pub fn kv_get(config: &Config, namespace: &str, key: &str) -> Result<Option<serde_json::Value>> {
- let docs = documents(config)?;
- let namespace = namespace.to_string();
- let key = key.to_string();
- run(async move { docs.kv_get(&namespace, &key).await })
-}
-
-/// The catalog's `kv_set` for this call.
-#[inline]
-pub fn kv_set(
- config: &Config,
- namespace: &str,
- key: &str,
- value: &serde_json::Value,
-) -> Result<()> {
- let docs = documents(config)?;
- let namespace = namespace.to_string();
- let key = key.to_string();
- let value = value.clone();
- run(async move { docs.kv_set(&namespace, &key, &value).await })
-}
-
-/// The catalog's `insert_flow_run` for this call.
-#[inline]
-pub fn insert_flow_run(
- config: &Config,
- id: &str,
- flow_id: &str,
- thread_id: &str,
- started_at: &str,
-) -> Result<()> {
- let docs = documents(config)?;
- let id = id.to_string();
- let flow_id = flow_id.to_string();
- let thread_id = thread_id.to_string();
- let started_at = started_at.to_string();
- run(async move {
- docs.insert_flow_run(&id, &flow_id, &thread_id, &started_at)
- .await
- })
-}
-
-/// The catalog's `prune_flow_runs` for this call.
-#[inline]
-pub fn prune_flow_runs(config: &Config, flow_id: &str, keep: usize) -> Result<usize> {
- let docs = documents(config)?;
- let flow_id = flow_id.to_string();
- run(async move { docs.prune_flow_runs(&flow_id, keep).await })
-}
-
-/// The catalog's `finish_flow_run` for this call.
-#[inline]
-pub fn finish_flow_run(
- config: &Config,
- id: &str,
- status: &str,
- finished_at: &str,
- steps: &[FlowRunStep],
- pending_approvals: &[String],
- error: Option<&str>,
- graph_hash: Option<&str>,
-) -> Result<bool> {
- let docs = documents(config)?;
- let id = id.to_string();
- let status = status.to_string();
- let finished_at = finished_at.to_string();
- let steps = steps.to_vec();
- let pending_approvals = pending_approvals.to_vec();
- let error = error.map(str::to_string);
- let graph_hash = graph_hash.map(str::to_string);
- run(async move {
- docs.finish_flow_run(
- &id,
- &status,
- &finished_at,
- &steps,
- &pending_approvals,
- error.as_deref(),
- graph_hash.as_deref(),
- )
- .await
- })
-}
-
-/// The catalog's parked-only `finish_parked_flow_run` compare-and-swap.
-#[inline]
-pub fn finish_parked_flow_run(
- config: &Config,
- id: &str,
- status: &str,
- finished_at: &str,
- steps: &[FlowRunStep],
- pending_approvals: &[String],
- error: Option<&str>,
- graph_hash: Option<&str>,
-) -> Result<bool> {
- let docs = documents(config)?;
- let id = id.to_string();
- let status = status.to_string();
- let finished_at = finished_at.to_string();
- let steps = steps.to_vec();
- let pending_approvals = pending_approvals.to_vec();
- let error = error.map(str::to_string);
- let graph_hash = graph_hash.map(str::to_string);
- run(async move {
- docs.finish_parked_flow_run(
- &id,
- &status,
- &finished_at,
- &steps,
- &pending_approvals,
- error.as_deref(),
- graph_hash.as_deref(),
- )
- .await
- })
-}
-
-/// The catalog's `upsert_flow_run_step` for this call.
-#[inline]
-pub fn upsert_flow_run_step(config: &Config, run_id: &str, step: &FlowRunStep) -> Result<()> {
- let docs = documents(config)?;
- let run_id = run_id.to_string();
- let step = step.clone();
- run(async move { docs.upsert_flow_run_step(&run_id, &step).await })
-}
-
-/// The catalog's `expire_parked_runs` for this call.
-#[inline]
-pub fn expire_parked_runs(
- config: &Config,
- cutoff: &str,
- now: &str,
- error_msg: &str,
-) -> Result<Vec<(String, String)>> {
- let docs = documents(config)?;
- let cutoff = cutoff.to_string();
- let now = now.to_string();
- let error_msg = error_msg.to_string();
- run(async move { docs.expire_parked_runs(&cutoff, &now, &error_msg).await })
-}
-
-/// The catalog's `list_running_run_ids` for this call.
-#[inline]
-pub fn list_running_run_ids(
- config: &Config,
- started_before: &str,
-) -> Result<Vec<(String, String)>> {
- let docs = documents(config)?;
- let started_before = started_before.to_string();
- run(async move { docs.list_running_run_ids(&started_before).await })
-}
-
-/// The catalog's `force_run_status_for_test` for this call.
-///
-/// Test-only: the drivers crate exposes it behind its `test-fixtures` feature,
-/// which this crate turns on as a dev-dependency and never in a shipped build.
-#[cfg(test)]
-#[inline]
-pub fn force_run_status_for_test(
- config: &Config,
- id: &str,
- status: &str,
- error: Option<&str>,
-) -> Result<()> {
- let docs = documents(config)?;
- let id = id.to_string();
- let status = status.to_string();
- let error = error.map(str::to_string);
- run(async move {
- docs.force_run_status_for_test(&id, &status, error.as_deref())
- .await
- })
-}
-
-/// The catalog's `force_corrupt_graph_json_for_test` for this call.
-///
-/// Test-only: the drivers crate exposes it behind its `test-fixtures` feature,
-/// which this crate turns on as a dev-dependency and never in a shipped build.
-#[cfg(test)]
-#[inline]
-pub fn force_corrupt_graph_json_for_test(
- config: &Config,
- flow_id: &str,
- raw_graph_json: &str,
-) -> Result<()> {
- let docs = documents(config)?;
- let flow_id = flow_id.to_string();
- let raw_graph_json = raw_graph_json.to_string();
- run(async move {
- docs.force_corrupt_graph_json_for_test(&flow_id, &raw_graph_json)
- .await
- })
-}
-
-/// The catalog's `mark_run_resuming` for this call.
-#[inline]
-pub fn mark_run_resuming(config: &Config, id: &str) -> Result<bool> {
- let docs = documents(config)?;
- let id = id.to_string();
- run(async move { docs.mark_run_resuming(&id).await })
-}
-
-/// The catalog's `mark_run_interrupted` for this call.
-#[inline]
-pub fn mark_run_interrupted(config: &Config, id: &str, now: &str, reason: &str) -> Result<bool> {
- let docs = documents(config)?;
- let id = id.to_string();
- let now = now.to_string();
- let reason = reason.to_string();
- run(async move { docs.mark_run_interrupted(&id, &now, &reason).await })
-}
-
-/// The catalog's `get_flow_run` for this call.
-#[inline]
-pub fn get_flow_run(config: &Config, id: &str) -> Result<Option<FlowRun>> {
- let docs = documents(config)?;
- let id = id.to_string();
- run(async move { docs.get_flow_run(&id).await })
-}
-
-/// The catalog's `list_flow_runs` for this call.
-#[inline]
-pub fn list_flow_runs(config: &Config, flow_id: &str, limit: usize) -> Result<Vec<FlowRun>> {
- let docs = documents(config)?;
- let flow_id = flow_id.to_string();
- run(async move { docs.list_flow_runs(&flow_id, limit).await })
-}
-
-/// The catalog's `list_all_flow_runs` for this call.
-#[inline]
-pub fn list_all_flow_runs(config: &Config, limit: usize) -> Result<Vec<FlowRun>> {
- let docs = documents(config)?;
- run(async move { docs.list_all_flow_runs(limit).await })
-}
-
-/// The catalog's `upsert_suggestions` for this call.
-#[inline]
-pub fn upsert_suggestions(config: &Config, suggestions: &[FlowSuggestion]) -> Result<usize> {
- let docs = documents(config)?;
- let suggestions = suggestions.to_vec();
- run(async move { docs.upsert_suggestions(&suggestions).await })
-}
-
-/// The catalog's `list_suggestions` for this call.
-#[inline]
-pub fn list_suggestions(
- config: &Config,
- status: Option<SuggestionStatus>,
- limit: usize,
-) -> Result<Vec<FlowSuggestion>> {
- let docs = documents(config)?;
- run(async move { docs.list_suggestions(status, limit).await })
-}
-
-/// The catalog's `set_suggestion_status` for this call.
-#[inline]
-pub fn set_suggestion_status(config: &Config, id: &str, status: SuggestionStatus) -> Result<bool> {
- let docs = documents(config)?;
- let id = id.to_string();
- run(async move { docs.set_suggestion_status(&id, status).await })
-}
-
-#[cfg(test)]
-#[path = "store_tests.rs"]
-mod tests;