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
refactor(storage): improve local backend initialization logic
Steven Enamakel927a93b1abee
diff --git a/crates/openhuman-core/src/storage/local.rs b/crates/openhuman-core/src/storage/local.rsindex b185909bf0..28f0e8e805 100644--- a/crates/openhuman-core/src/storage/local.rs+++ b/crates/openhuman-core/src/storage/local.rs@@ -48,26 +48,23 @@ impl Opened {/// Default backends this process opened for workspaces with nothing/// installed, by workspace directory.-static WORKSPACES: LazyLock<Mutex<HashMap<PathBuf, Arc<dyn StorageBackend>>>> =+type WorkspaceBackend = Arc<tokio::sync::OnceCell<Arc<dyn StorageBackend>>>;++static WORKSPACES: LazyLock<Mutex<HashMap<PathBuf, WorkspaceBackend>>> =LazyLock::new(Mutex::default);-/// The backend for `workspace_dir`: the installed one, else the workspace's-/// own default, opened on first use.-///-/// # Errors-///-/// In SaaS mode with no backend installed, or when the default cannot be-/// opened.-pub fn backend_for(workspace_dir: &Path) -> Result<Arc<dyn StorageBackend>> {- if let Some(backend) = installed() {- return Ok(backend);- }+fn workspace_backend(workspace_dir: &Path) -> WorkspaceBackend {let mut opened = WORKSPACES.lock().unwrap_or_else(PoisonError::into_inner);- if let Some(backend) = opened.get(workspace_dir) {- return Ok(Arc::clone(backend));- }- let url = super::config::default_url(workspace_dir);- super::config::prepare_default_dir(workspace_dir).with_context(|| {+ Arc::clone(+ opened+ .entry(workspace_dir.to_path_buf())+ .or_insert_with(|| Arc::new(tokio::sync::OnceCell::new())),+ )+}++async fn open_default_backend(workspace_dir: PathBuf) -> Result<Arc<dyn StorageBackend>> {+ let url = super::config::default_url(&workspace_dir);+ super::config::prepare_default_dir(&workspace_dir).with_context(|| {format!("create the storage directory for {}",workspace_dir.display()@@ -78,13 +75,34 @@ pub fn backend_for(workspace_dir: &Path) -> Result<Arc<dyn StorageBackend>> {workspace = %workspace_dir.display(),"[storage::local] opening the workspace default backend (none installed)");- let backend = super::block_on_anyhow({- let url = url.clone();- async move { Ok(super::open(&url).await?) }- })- .with_context(|| format!("open the default storage for {}", workspace_dir.display()))?;- opened.insert(workspace_dir.to_path_buf(), Arc::clone(&backend));- Ok(backend)+ super::open(&url)+ .await+ .with_context(|| format!("open the default storage for {}", workspace_dir.display()))+}++async fn initialized_backend(+ cell: WorkspaceBackend,+ workspace_dir: PathBuf,+) -> Result<Arc<dyn StorageBackend>> {+ cell.get_or_try_init(|| async move { open_default_backend(workspace_dir).await })+ .await+ .map(Arc::clone)+}++/// The backend for `workspace_dir`: the installed one, else the workspace's+/// own default, opened on first use.+///+/// # Errors+///+/// In SaaS mode with no backend installed, or when the default cannot be+/// opened.+pub fn backend_for(workspace_dir: &Path) -> Result<Arc<dyn StorageBackend>> {+ if let Some(backend) = installed() {+ return Ok(backend);+ }+ let cell = workspace_backend(workspace_dir);+ let workspace_dir = workspace_dir.to_path_buf();+ super::block_on_anyhow(async move { initialized_backend(cell, workspace_dir).await })}/// Async counterpart to [`backend_for`] for boot and module-proxy paths that@@ -94,31 +112,8 @@ pub async fn backend_for_async(workspace_dir: &Path) -> Result<Arc<dyn StorageBaif let Some(backend) = installed() {return Ok(backend);}- {- let opened = WORKSPACES.lock().unwrap_or_else(PoisonError::into_inner);- if let Some(backend) = opened.get(workspace_dir) {- return Ok(Arc::clone(backend));- }- }- let url = super::config::default_url(workspace_dir);- super::config::prepare_default_dir(workspace_dir).with_context(|| {- format!(- "create the storage directory for {}",- workspace_dir.display()- )- })?;- tracing::debug!(- target: "openhuman::storage",- workspace = %workspace_dir.display(),- "[storage::local] asynchronously opening the workspace default backend (none installed)"- );- let backend = super::open(&url)- .await- .with_context(|| format!("open the default storage for {}", workspace_dir.display()))?;- let mut opened = WORKSPACES.lock().unwrap_or_else(PoisonError::into_inner);- Ok(Arc::clone(- opened.entry(workspace_dir.to_path_buf()).or_insert(backend),- ))+ let cell = workspace_backend(workspace_dir);+ initialized_backend(cell, workspace_dir.to_path_buf()).await}/// The storage a store uses for this call: [`backend_for`] `workspace_dir`diff --git a/crates/openhuman-core/src/storage/local_tests.rs b/crates/openhuman-core/src/storage/local_tests.rsindex 91a6633f37..42d9992fc7 100644--- a/crates/openhuman-core/src/storage/local_tests.rs+++ b/crates/openhuman-core/src/storage/local_tests.rs@@ -64,3 +64,26 @@ fn forgetting_a_workspace_reopens_the_same_data() {"a restart keeps the data");}++#[tokio::test]+async fn concurrent_sync_and_async_open_share_one_workspace_backend() {+ let dir = tempfile::tempdir().unwrap();+ let workspace = dir.path().to_path_buf();+ let sync_workspace = workspace.clone();++ let (sync, asynchronous) = tokio::join!(+ tokio::task::spawn_blocking(move || backend_for(&sync_workspace)),+ backend_for_async(&workspace),+ );+ let sync = sync.unwrap().unwrap();+ let asynchronous = asynchronous.unwrap();++ assert!(+ Arc::ptr_eq(&sync, &asynchronous),+ "sync and async initializers must share the cached backend"+ );+ assert!(+ Arc::ptr_eq(&sync, &backend_for(dir.path()).unwrap()),+ "later sync opens must reuse the initialized backend"+ );+}