Skip to content

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

test(storage): pin a document backend per thread to cover the store dispatch

Steven Enamakel1737c709b2b8
diff --git a/crates/openhuman-core/src/agent/orchestration/running_subagents/task_ledger_documents_tests.rs b/crates/openhuman-core/src/agent/orchestration/running_subagents/task_ledger_documents_tests.rs
index 2b4512c2e6..3634c6fa17 100644
--- a/crates/openhuman-core/src/agent/orchestration/running_subagents/task_ledger_documents_tests.rs
+++ b/crates/openhuman-core/src/agent/orchestration/running_subagents/task_ledger_documents_tests.rs
@@ -113,9 +113,14 @@ fn a_malformed_document_is_skipped_on_open() {
let storage = MemoryStorage::new();
let repo = repo_in(&storage, "local");
repo.run(|docs| async move {
- docs.put(TASKS, "bad", serde_json::json!({ "record": 1 }), Precondition::None)
- .await
- .map(|_| ())
+ docs.put(
+ TASKS,
+ "bad",
+ serde_json::json!({ "record": 1 }),
+ Precondition::None,
+ )
+ .await
+ .map(|_| ())
})
.unwrap();
let store = DocumentTaskStore::open(repo.clone()).unwrap();
diff --git a/crates/openhuman-core/src/agent/orchestration/subagent_sessions/store_documents.rs b/crates/openhuman-core/src/agent/orchestration/subagent_sessions/store_documents.rs
index 3144a6d31e..e38ad69e63 100644
--- a/crates/openhuman-core/src/agent/orchestration/subagent_sessions/store_documents.rs
+++ b/crates/openhuman-core/src/agent/orchestration/subagent_sessions/store_documents.rs
@@ -43,12 +43,6 @@ pub(super) fn current() -> Result<Option<Docs>> {
Ok(Repo::current(DOMAIN, collections)?.map(Docs))
}
-/// Whether a test pinned a document store for this thread.
-#[cfg(test)]
-pub(super) fn overridden() -> bool {
- TEST_OVERRIDE.with(|slot| slot.borrow().is_some())
-}
-
/// Runs `f` with `docs` standing in for the installed backend, on this thread
/// only, so tests exercise the dispatch without the process-wide slot.
#[cfg(test)]
diff --git a/crates/openhuman-core/src/agent/orchestration/subagent_sessions/store_documents_tests.rs b/crates/openhuman-core/src/agent/orchestration/subagent_sessions/store_documents_tests.rs
index 81f5a1aea4..ffe97a82c5 100644
--- a/crates/openhuman-core/src/agent/orchestration/subagent_sessions/store_documents_tests.rs
+++ b/crates/openhuman-core/src/agent/orchestration/subagent_sessions/store_documents_tests.rs
@@ -105,5 +105,8 @@ fn the_store_dispatches_to_documents_when_a_backend_is_pinned() {
assert_eq!(store.load().unwrap().len(), 1);
});
assert_eq!(docs.load().unwrap().len(), 1);
- assert!(!dir.path().join(".openhuman/subagent_sessions.json").exists());
+ assert!(!dir
+ .path()
+ .join(".openhuman/subagent_sessions.json")
+ .exists());
}
diff --git a/crates/openhuman-core/src/integrations/composio/file_store_documents.rs b/crates/openhuman-core/src/integrations/composio/file_store_documents.rs
index 413260de0a..599ecce3d8 100644
--- a/crates/openhuman-core/src/integrations/composio/file_store_documents.rs
+++ b/crates/openhuman-core/src/integrations/composio/file_store_documents.rs
@@ -39,22 +39,6 @@ pub(super) fn current() -> Result<Option<Docs>> {
Ok(Repo::current(DOMAIN, collections)?.map(Docs))
}
-/// Whether a test pinned a document store for this thread.
-#[cfg(test)]
-pub(super) fn overridden() -> bool {
- TEST_OVERRIDE.with(|slot| slot.borrow().is_some())
-}
-
-/// Runs `f` with `docs` standing in for the installed backend, on this thread
-/// only, so tests exercise the dispatch without the process-wide slot.
-#[cfg(test)]
-pub(super) fn with_override<T>(docs: Docs, f: impl FnOnce() -> T) -> T {
- TEST_OVERRIDE.with(|slot| *slot.borrow_mut() = Some(docs));
- let out = f();
- TEST_OVERRIDE.with(|slot| *slot.borrow_mut() = None);
- out
-}
-
#[cfg(test)]
thread_local! {
static TEST_OVERRIDE: std::cell::RefCell<Option<Docs>> = const { std::cell::RefCell::new(None) };
diff --git a/crates/openhuman-core/src/integrations/composio/file_store_documents_tests.rs b/crates/openhuman-core/src/integrations/composio/file_store_documents_tests.rs
index abdbf42b38..83670f49e6 100644
--- a/crates/openhuman-core/src/integrations/composio/file_store_documents_tests.rs
+++ b/crates/openhuman-core/src/integrations/composio/file_store_documents_tests.rs
@@ -54,7 +54,6 @@ async fn the_file_store_dispatches_to_documents_when_a_backend_is_pinned() {
let file = dir.path().join("composio_identities.json");
let mut value = BTreeMap::new();
value.insert("gmail".to_string(), 7u32);
- with_override(docs.clone(), || ());
// Pin on this thread for the awaits below (a current-thread runtime).
super::TEST_OVERRIDE.with(|slot| *slot.borrow_mut() = Some(docs.clone()));
super::super::file_store::save(&file, &value).await.unwrap();
diff --git a/crates/openhuman-core/src/platform/cost/tracker_documents.rs b/crates/openhuman-core/src/platform/cost/tracker_documents.rs
index 674e9bcb3c..b693cc2cb8 100644
--- a/crates/openhuman-core/src/platform/cost/tracker_documents.rs
+++ b/crates/openhuman-core/src/platform/cost/tracker_documents.rs
@@ -40,12 +40,6 @@ pub(super) fn current() -> Result<Option<CostDocs>> {
Ok(Repo::current(DOMAIN, collections)?.map(CostDocs))
}
-/// Whether a test pinned a document store for this thread.
-#[cfg(test)]
-pub(super) fn overridden() -> bool {
- TEST_OVERRIDE.with(|slot| slot.borrow().is_some())
-}
-
/// Runs `f` with `docs` standing in for the installed backend, on this thread
/// only, so tests exercise the dispatch without the process-wide slot.
#[cfg(test)]
diff --git a/crates/openhuman-core/src/platform/cost/tracker_tests.rs b/crates/openhuman-core/src/platform/cost/tracker_tests.rs
index 6c4dfac6ff..5011fa56bd 100644
--- a/crates/openhuman-core/src/platform/cost/tracker_tests.rs
+++ b/crates/openhuman-core/src/platform/cost/tracker_tests.rs
@@ -565,3 +565,53 @@ fn empty_ledger_sums_are_not_negative_zero() {
assert!(dashboard.monthly_pace_usd.is_sign_positive());
assert!(!serde_json::to_string(&dashboard).unwrap().contains("-0.0"));
}
+
+#[test]
+fn the_ledger_dispatches_to_documents_when_a_backend_is_pinned() {
+ use crate::storage::{MemoryStorage, Scope, StorageBackend};
+ let storage = MemoryStorage::new();
+ let docs_for = |scope: &str| {
+ super::super::tracker_documents::CostDocs::over(
+ &storage.for_scope(&Scope::new(scope).unwrap()).unwrap(),
+ )
+ };
+ let tmp = TempDir::new().unwrap();
+ let tracker = CostTracker::new(enabled_config(), tmp.path()).unwrap();
+
+ super::super::tracker_documents::with_override(docs_for("alice"), || {
+ tracker
+ .record_usage(TokenUsage::new(MANAGED_MODEL, 1000, 500, 1.0, 2.0))
+ .unwrap();
+ tracker
+ .record_usage(TokenUsage::new(BYOK_MODEL, 100, 50, 1.0, 2.0))
+ .unwrap();
+ let summary = tracker.get_summary().unwrap();
+ assert!(summary.daily_cost_usd > 0.0);
+ assert_eq!(summary.request_count, 2);
+ let now = Utc::now();
+ assert!(tracker.get_daily_cost(now.date_naive()).unwrap() > 0.0);
+ assert!(tracker.get_monthly_cost(now.year(), now.month()).unwrap() > 0.0);
+ assert!(
+ tracker
+ .get_managed_monthly_cost(now.year(), now.month())
+ .unwrap()
+ > 0.0
+ );
+ assert_eq!(tracker.get_recent_records(1, 10).unwrap().len(), 2);
+ });
+
+ // Another agent's scope starts empty: the period totals are not a shared cache.
+ super::super::tracker_documents::with_override(docs_for("bob"), || {
+ let summary = tracker.get_summary().unwrap();
+ assert_eq!(summary.daily_cost_usd, 0.0);
+ assert!(tracker.get_recent_records(1, 10).unwrap().is_empty());
+ });
+
+ // Nothing reached the JSONL file.
+ assert!(
+ !tmp.path().join("state/costs.jsonl").exists()
+ || std::fs::read_to_string(tmp.path().join("state/costs.jsonl"))
+ .unwrap()
+ .is_empty()
+ );
+}