Skip to content

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

feat(storage): resolve default storage url per profile layout

Steven Enamakelaedf83eaae0b
diff --git a/crates/openhuman-core/src/security/credentials/ops/user_scope.rs b/crates/openhuman-core/src/security/credentials/ops/user_scope.rs
index f173fd0804..304f86a1ec 100644
--- a/crates/openhuman-core/src/security/credentials/ops/user_scope.rs
+++ b/crates/openhuman-core/src/security/credentials/ops/user_scope.rs
@@ -73,10 +73,13 @@ pub(super) fn activate_user_scope(ctx: &DispatchCtx, user_id: &str) -> Result<Ve
// `list_threads` / `purge_threads` on any workspace, so purge and
// concurrent thread RPC in this process cannot interleave.
// The pre-login profile's workspace follows its layout (`workspace`
- // for v1, `files` for v2); resolving never moves data.
- let pre_ws = crate::config::schema::ProfileLayout::resolve_at(pre_login_user_dir(
- &root_dir,
- ))
+ // for v1, `files` for v2). Detection only: this must not create the
+ // profile directory as a side effect.
+ let pre_dir = pre_login_user_dir(&root_dir);
+ let pre_ws = crate::config::schema::ProfileLayout::with_version(
+ pre_dir.clone(),
+ crate::config::schema::profile_layout::LayoutVersion::detect(&pre_dir),
+ )
.workspace_dir;
let pre_ws_log = pre_ws.display().to_string();
match conversations::purge_threads(ctx, pre_ws) {
diff --git a/crates/openhuman-core/src/storage/config.rs b/crates/openhuman-core/src/storage/config.rs
index 2a051bab3a..e5f5b424cc 100644
--- a/crates/openhuman-core/src/storage/config.rs
+++ b/crates/openhuman-core/src/storage/config.rs
@@ -5,7 +5,8 @@
//!
//! 1. `OPENHUMAN_STORAGE_URL`;
//! 2. `[storage] url` in `config.toml`;
-//! 3. the default, `sqlite:<workspace_dir>/storage` ([`default_url`]).
+//! 3. the default ([`default_url`]): `sqlite:<user dir>/storage.db` for a
+//! layout-v2 profile, `sqlite:<workspace_dir>/storage` for a v1 one.
//!
//! Every host opens the resolved URL once at startup and installs it
//! ([`super::install`]); every domain then reaches storage through it, and the
@@ -25,9 +26,10 @@
use std::path::Path;
+use crate::config::schema::profile_layout::{LayoutVersion, ProfileLayout};
use crate::config::Config;
-/// The directory under the workspace the default backend lives in.
+/// The directory under the workspace the v1 default backend lives in.
pub const DEFAULT_DIR: &str = "storage";
/// The retired opt-out values (`classic`, `legacy`). They are no longer a mode:
@@ -74,10 +76,40 @@ pub fn mode(config: &Config) -> StorageMode {
mode_from(std::env::var(super::STORAGE_URL_VAR).ok(), config)
}
-/// The default URL for `workspace_dir`: SQLite in directory mode under
-/// `<workspace>/storage`, one file per named database.
+/// The default URL for a profile of layout `layout`: `sqlite:<dir>/storage.db`
+/// for V2 (one file for the whole profile), `sqlite:<workspace>/storage` for
+/// V1 (SQLite in directory mode, one file per named database).
+pub fn default_url_for(layout: &ProfileLayout) -> String {
+ match layout.version {
+ LayoutVersion::V2 => format!("sqlite:{}", layout.storage_db.display()),
+ LayoutVersion::V1 => format!(
+ "sqlite:{}",
+ layout.workspace_dir.join(DEFAULT_DIR).display()
+ ),
+ }
+}
+
+/// The default URL for `workspace_dir`. A V2 profile's files directory
+/// ([`ProfileLayout::of_workspace`]) gets its `storage.db`; any other
+/// workspace is V1 and gets the directory-mode backend inside it.
pub fn default_url(workspace_dir: &Path) -> String {
- format!("sqlite:{}", workspace_dir.join(DEFAULT_DIR).display())
+ match ProfileLayout::of_workspace(workspace_dir) {
+ Some(layout) => default_url_for(&layout),
+ None => format!("sqlite:{}", workspace_dir.join(DEFAULT_DIR).display()),
+ }
+}
+
+/// Creates what [`default_url`] opens into: the profile directory for a V2
+/// `storage.db`, `<workspace>/storage` for V1.
+///
+/// # Errors
+///
+/// When the directory cannot be created.
+pub fn prepare_default_dir(workspace_dir: &Path) -> std::io::Result<()> {
+ match ProfileLayout::of_workspace(workspace_dir) {
+ Some(layout) => std::fs::create_dir_all(&layout.dir),
+ None => std::fs::create_dir_all(workspace_dir.join(DEFAULT_DIR)),
+ }
}
/// The URL in effect for `mode`: the configured one, or the default under
diff --git a/crates/openhuman-core/src/storage/local.rs b/crates/openhuman-core/src/storage/local.rs
index 19b4a44903..7f0af3aa52 100644
--- a/crates/openhuman-core/src/storage/local.rs
+++ b/crates/openhuman-core/src/storage/local.rs
@@ -4,7 +4,7 @@
//! store reaches it through [`super::installed`]. A unit test, or an embedder
//! that never ran the host boot, installs none; rather than keep a second,
//! file-shaped implementation of each store for them, such a call opens **the
-//! same default a host would**: `sqlite:<workspace>/storage`
+//! same default a host would**: `sqlite:<workspace>/storage` (v1) or `sqlite:<user dir>/storage.db` (v2)
//! ([`super::config::default_url`]), once per workspace for the life of the
//! process. A different workspace is a different database, which is also what
//! isolates tests that use a temp directory each.
@@ -67,9 +67,9 @@ pub fn backend_for(workspace_dir: &Path) -> Result<Arc<dyn StorageBackend>> {
return Ok(Arc::clone(backend));
}
let url = super::config::default_url(workspace_dir);
- std::fs::create_dir_all(workspace_dir.join(super::config::DEFAULT_DIR)).with_context(|| {
+ super::config::prepare_default_dir(workspace_dir).with_context(|| {
format!(
- "create the storage directory under {}",
+ "create the storage directory for {}",
workspace_dir.display()
)
})?;
diff --git a/crates/openhuman-core/src/storage/rebind.rs b/crates/openhuman-core/src/storage/rebind.rs
index 88bff50ff6..3ae4e77ed2 100644
--- a/crates/openhuman-core/src/storage/rebind.rs
+++ b/crates/openhuman-core/src/storage/rebind.rs
@@ -1,7 +1,7 @@
//! Following the workspace.
//!
-//! The default backend is `sqlite:<workspace>/storage`, so it belongs to a
-//! workspace. The desktop rebinds its workspace when a different user signs in
+//! The default backend is `sqlite:<user dir>/storage.db` (layout v2) or
+//! `sqlite:<workspace>/storage` (v1), so it belongs to a workspace. The desktop rebinds its workspace when a different user signs in
//! (`CoreContext::rebind_default_workspace`); this module is the storage half
//! of that rebind. It re-resolves the URL for the new workspace and, when it
//! differs from the one the host installed:
@@ -84,14 +84,12 @@ pub(crate) fn rebind_with(
workspace = %config.workspace_dir.display(),
"[storage] the workspace moved; reopening the storage backend"
);
- std::fs::create_dir_all(config.workspace_dir.join(super::config::DEFAULT_DIR)).with_context(
- || {
- format!(
- "create the storage directory under {}",
- config.workspace_dir.display()
- )
- },
- )?;
+ super::config::prepare_default_dir(&config.workspace_dir).with_context(|| {
+ format!(
+ "create the storage directory for {}",
+ config.workspace_dir.display()
+ )
+ })?;
let url = next.clone();
let backend = super::block_on_anyhow(async move { Ok(super::open(&url).await?) })
.with_context(|| format!("open the storage backend {redacted_to}"))?;