The fastest, cheapest, most efficient open-source agent harness. Run more than 500 agents on a $10 VPS.
Rust agent-orchestrationai-agentsai-assistantdesktopllmlocal-firstmcppersonal-aiprivacyrustsecond-braintauri
feat(store-sqlite): add sqlite-backed store crate
Steven Enamakel7b71bcb28281
diff --git a/Cargo.lock b/Cargo.lockindex 28fbaf642e..c2318ad57a 100644--- a/Cargo.lock+++ b/Cargo.lock@@ -4235,6 +4235,19 @@ dependencies = ["uuid",]+[[package]]+name = "openhuman-store-sqlite"+version = "0.64.10"+dependencies = [+ "anyhow",+ "chrono",+ "log",+ "tempfile",+ "tinyagents-harness",+ "tinyagents-session",+ "tokio",+]+[[package]]name = "openhuman-tinyhumans"version = "0.64.10"diff --git a/Cargo.toml b/Cargo.tomlindex b8656d19a4..7659d0e40a 100644--- a/Cargo.toml+++ b/Cargo.toml@@ -16,6 +16,7 @@ members = ["crates/openhuman-core","crates/openhuman-embed","crates/openhuman-rpc",+ "crates/openhuman-store-sqlite","crates/openhuman-tinyhumans","crates/openhuman-tui",]@@ -36,6 +37,7 @@ repository = "https://github.com/tinyhumansai/openhuman"[workspace.dependencies]openhuman-core = { path = "crates/openhuman-core", package = "openhuman" }openhuman-rpc = { path = "crates/openhuman-rpc", default-features = false }+openhuman-store-sqlite = { path = "crates/openhuman-store-sqlite" }openhuman-embed = { path = "crates/openhuman-embed", default-features = false }openhuman-tinyhumans = { path = "crates/openhuman-tinyhumans", default-features = false }diff --git a/crates/openhuman-store-sqlite/Cargo.toml b/crates/openhuman-store-sqlite/Cargo.tomlnew file mode 100644index 0000000000..208c831dd6--- /dev/null+++ b/crates/openhuman-store-sqlite/Cargo.toml@@ -0,0 +1,22 @@+[package]+name = "openhuman-store-sqlite"+description = "The desktop session store: OpenHuman's on-disk transcript, run-ledger and turn-state layout behind the TinyAgents session store port"+version.workspace = true+edition.workspace = true+license.workspace = true+repository.workspace = true+publish = false++# Deliberately free of the core: this crate only wraps TinyAgents' file and+# SQLite building blocks behind `SessionStoreProvider`, so the core and embed+# stay storage-agnostic and a host chooses this layout by installing it.+[dependencies]+anyhow = "1"+chrono = "0.4"+log = "0.4"+tinyagents-harness = { path = "../../vendor/tinyagents/crates/tinyagents-harness", default-features = false }+tinyagents-session = { path = "../../vendor/tinyagents/crates/tinyagents-session" }++[dev-dependencies]+tempfile = "3"+tokio = { version = "1", features = ["macros", "rt-multi-thread"] }diff --git a/crates/openhuman-store-sqlite/src/lib.rs b/crates/openhuman-store-sqlite/src/lib.rsnew file mode 100644index 0000000000..c675115742--- /dev/null+++ b/crates/openhuman-store-sqlite/src/lib.rs@@ -0,0 +1,133 @@+//! The desktop session store.+//!+//! [`SqliteSessionStores`] is a+//! [`SessionStoreProvider`](tinyagents_session::port::SessionStoreProvider)+//! over the layout OpenHuman has always written under its workspace:+//!+//! ```text+//! {workspace}/session_raw/*.jsonl transcripts+//! {workspace}/session_db/sessions.db run ledger (SQLite)+//! {workspace}/memory/conversations/turn_states/… turn snapshots+//! {workspace}/tinyagents_store/{kv,journal}/ run status, goals,+//! todos, turn journal+//! ```+//!+//! The desktop app, the CLI and the TUI install it, so the core and the embed+//! facade carry no storage of their own. It serves one operator: every agent+//! shares the workspace, as it always has, so it does not claim the+//! per-agent isolation a multi-user host's store must provide.+//!+//! The workspace is resolved on every call rather than fixed at+//! construction, because the desktop rebinds it when a different user signs+//! in.+//!+//! ```+//! use std::sync::Arc;+//! use openhuman_store_sqlite::SqliteSessionStores;+//! use tinyagents_session::port::SessionStoreProvider;+//!+//! let dir = tempfile::tempdir().unwrap();+//! let stores = SqliteSessionStores::at(dir.path());+//! assert_eq!(stores.workspace_dir().as_deref(), Some(dir.path()));+//! let agent = stores.for_agent("orchestrator");+//! assert!(agent.transcripts.root_for_thread("no-such-thread").is_none());+//! ```++use std::path::{Path, PathBuf};+use std::sync::Arc;++use tinyagents_session::port::{AgentStores, SessionStoreProvider};+use tinyagents_session::transcript::import::ops::open_session_stores;+use tinyagents_session::transcript::FileTranscriptLocator;+use tinyagents_session::turn_state::TurnStateStore;++/// Resolves the workspace the stores live in, at the moment they are needed.+type WorkspaceResolver = dyn Fn() -> PathBuf + Send + Sync;++/// OpenHuman's on-disk session layout as a session store provider.+#[derive(Clone)]+pub struct SqliteSessionStores {+ workspace: Arc<WorkspaceResolver>,+}++impl std::fmt::Debug for SqliteSessionStores {+ fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {+ f.debug_struct("SqliteSessionStores")+ .field("workspace", &(self.workspace)())+ .finish()+ }+}++impl SqliteSessionStores {+ /// Stores under the fixed `workspace` directory.+ pub fn at(workspace: impl Into<PathBuf>) -> Self {+ let workspace = workspace.into();+ Self::resolving(move || workspace.clone())+ }++ /// Stores under whichever workspace `resolve` names when asked — for a+ /// host whose workspace can change while it runs.+ pub fn resolving(resolve: impl Fn() -> PathBuf + Send + Sync + 'static) -> Self {+ Self {+ workspace: Arc::new(resolve),+ }+ }++ fn current(&self) -> PathBuf {+ (self.workspace)()+ }+}++impl SessionStoreProvider for SqliteSessionStores {+ /// Every agent shares the workspace: the single-operator layout.+ fn for_agent(&self, agent_id: &str) -> AgentStores {+ let workspace = self.current();+ log::trace!(+ "[store-sqlite] stores for agent={agent_id} workspace={}",+ workspace.display()+ );+ stores_at(&workspace)+ }++ /// Marks turns left in flight by an unclean shutdown interrupted, and+ /// settles run-ledger rows a dead process left running — the two sweeps+ /// the core always ran at boot.+ fn recover(&self) -> anyhow::Result<()> {+ let workspace = self.current();+ let now = chrono::Utc::now().to_rfc3339();+ let turns = tinyagents_session::turn_state::store::mark_all_interrupted(+ workspace.clone(),+ &now,+ )+ .map_err(anyhow::Error::msg)?;+ let runs = tinyagents_session::run_ledger::interrupt_orphaned_agent_runs(&workspace)?;+ log::info!(+ "[store-sqlite] recovered workspace={} interrupted_turns={turns} settled_runs={runs}",+ workspace.display()+ );+ Ok(())+ }++ fn destination_key(&self) -> Option<String> {+ Some(self.current().to_string_lossy().into_owned())+ }++ fn workspace_dir(&self) -> Option<PathBuf> {+ Some(self.current())+ }+}++/// The stores the layout keeps under `workspace`.+fn stores_at(workspace: &Path) -> AgentStores {+ let kv_and_journal = open_session_stores(workspace);+ AgentStores {+ transcripts: Arc::new(FileTranscriptLocator::new(workspace)),+ turn_states: Arc::new(TurnStateStore::new(workspace.to_path_buf())),+ kv: Arc::new(kv_and_journal.kv),+ journal: Arc::new(kv_and_journal.journal),+ }+}++#[cfg(test)]+#[path = "lib_tests.rs"]+mod tests;diff --git a/crates/openhuman-store-sqlite/src/lib_tests.rs b/crates/openhuman-store-sqlite/src/lib_tests.rsnew file mode 100644index 0000000000..abab468c93--- /dev/null+++ b/crates/openhuman-store-sqlite/src/lib_tests.rs@@ -0,0 +1,46 @@+use super::*;+use std::sync::Mutex;+use tinyagents_session::testkit::conformance::session_store_conformance;+use tinyagents_session::turn_state::{TurnLifecycle, TurnState};++#[tokio::test]+async fn the_desktop_layout_meets_the_session_store_contract() {+ let dir = tempfile::tempdir().unwrap();+ session_store_conformance(&SqliteSessionStores::at(dir.path())).await;+ // Everything landed in the classic layout.+ assert!(dir.path().join("session_raw").is_dir());+ assert!(dir.path().join("tinyagents_store").is_dir());+}++#[test]+fn the_workspace_follows_the_resolver() {+ let first = tempfile::tempdir().unwrap();+ let second = tempfile::tempdir().unwrap();+ let current = Arc::new(Mutex::new(first.path().to_path_buf()));+ let resolver = current.clone();+ let stores = SqliteSessionStores::resolving(move || resolver.lock().unwrap().clone());+ assert_eq!(stores.workspace_dir().as_deref(), Some(first.path()));+ *current.lock().unwrap() = second.path().to_path_buf();+ assert_eq!(stores.workspace_dir().as_deref(), Some(second.path()));+ assert_eq!(+ stores.destination_key(),+ Some(second.path().to_string_lossy().into_owned())+ );+ assert!(format!("{stores:?}").contains("SqliteSessionStores"));+}++#[test]+fn recovery_interrupts_turns_left_in_flight() {+ let dir = tempfile::tempdir().unwrap();+ let stores = SqliteSessionStores::at(dir.path());+ let agent = stores.for_agent("orchestrator");+ agent+ .turn_states+ .put(&TurnState::started("t", "r", 4, "2026-01-01T00:00:00Z"))+ .unwrap();+ stores.recover().unwrap();+ assert_eq!(+ agent.turn_states.get("t").unwrap().map(|turn| turn.lifecycle),+ Some(TurnLifecycle::Interrupted)+ );+}