Skip to content

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

feat(memory): root layout v3 at org scopes with legacy user reads

Steven Enamakel669ade002129
diff --git a/crates/openhuman-core/src/config/schema/memory.rs b/crates/openhuman-core/src/config/schema/memory.rs
index d6382eb0c5..9319949ea3 100644
--- a/crates/openhuman-core/src/config/schema/memory.rs
+++ b/crates/openhuman-core/src/config/schema/memory.rs
@@ -114,7 +114,7 @@ pub struct MemoryConfig {
pub root: Option<String>,
/// Where memory sits on the engine: `legacy` (the shared
/// `app:tinymemory` tree, the default) or `v3` (the signed-in person's
- /// own `user:<id>` subtree, chats pooled at `ws:main`). Switched by the
+ /// own `org:<id>` subtree, chats pooled at `ws:main`). Switched by the
/// layout migration once the person's memory has moved, never by hand.
#[serde(skip_serializing_if = "MemoryLayoutMode::is_legacy")]
pub layout: MemoryLayoutMode,
@@ -155,6 +155,13 @@ pub struct MemoryConfig {
/// is written again without it.
#[serde(skip_serializing_if = "std::ops::Not::not")]
pub observed_actor: bool,
+ /// While memory written below the earlier `user:<id>` scope root is
+ /// moved to the person's `org:<id>` root (cortexdb-saas
+ /// `reroot-user-segment`), layout v3 still reads and forgets below it
+ /// too, merged by item id; writes go only to `org:<id>`. On by default;
+ /// turn it off once the move is verified. Written only when off.
+ #[serde(skip_serializing_if = "is_true")]
+ pub legacy_user_segment_read: bool,
}
/// `[memory] layout`: where memory sits on the engine.
@@ -164,7 +171,7 @@ pub enum MemoryLayoutMode {
/// The shared `app:tinymemory` tree, as before layout v3.
#[default]
Legacy,
- /// The person's own `user:<id>` subtree, every kind under a leaf of its
+ /// The person's own `org:<id>` subtree, every kind under a leaf of its
/// own, chats pooled at `ws:main`.
V3,
}
@@ -226,6 +233,7 @@ impl Default for MemoryConfig {
agents: BTreeMap::new(),
split_github_by_repo: true,
observed_actor: false,
+ legacy_user_segment_read: true,
}
}
}
diff --git a/crates/openhuman-core/src/memory/engine.rs b/crates/openhuman-core/src/memory/engine.rs
index 716c082b0b..ac0553cf05 100644
--- a/crates/openhuman-core/src/memory/engine.rs
+++ b/crates/openhuman-core/src/memory/engine.rs
@@ -14,9 +14,12 @@
//! repeated calls reuse one HTTP client.
//!
//! **Layout.** Under `[memory] layout = "v3"` the engine keeps everything
-//! below the signed-in person's own scope root (`user:<id>`, see
-//! [`super::scope::user_root`]), registered as owned by them; otherwise the
-//! legacy shared tree. [`bind_with_root`] binds either layout explicitly,
+//! below the signed-in person's own scope root (`org:<id>`, see
+//! [`super::scope::user_root`]): on CortexDB's own API as its root,
+//! registered as owned by their actor `user:<id>`; on the TinyHumans wire as
+//! the tenant root the backend pins, so no root segment is sent. While
+//! `[memory] legacy_user_segment_read` is on, the earlier `user:<id>` root is
+//! read and forgotten as well. Otherwise the legacy shared tree. [`bind_with_root`] binds either layout explicitly,
//! for the layout migration, which holds both at once.
//!
//! An embedding host can bring its own engine instead ([`install_host_engine`]):
@@ -218,8 +221,8 @@ pub fn resolve(config: &Config) -> Binding {
}
/// The engine the layout migration needs, for an explicit layout whatever
-/// `[memory] layout` says: `None` the legacy tree, `Some("user:<id>")` the
-/// person's v3 subtree, with `user:<id>` as its owner. Same endpoint,
+/// `[memory] layout` says: `None` the legacy tree, `Some("org:<id>")` the
+/// person's v3 subtree, with the actor `user:<id>` as its owner. Same endpoint,
/// credential, headers and scrubbing as [`resolve`]; the two layouts are
/// cached apart, so both can be held at once.
///
@@ -311,15 +314,48 @@ fn off(engine: Option<&str>, endpoint: Option<String>, reason: &str) -> Binding
}
}
-/// The engine settings' scope root and owner, and the fingerprint suffix
-/// that keeps each layout's engine apart in the cache.
-fn rooted(settings: EngineSettings, root: Option<&str>) -> (EngineSettings, String) {
- let settings = EngineSettings {
- scope_root: root.map(str::to_string),
- scope_owner: root.map(str::to_string),
- ..settings
+/// The engine settings' layout for the person's root `root` (`org:<id>`,
+/// `None` the legacy tree), and the fingerprint suffix that keeps each
+/// layout's engine apart in the cache. On the TinyHumans wire (`tenant`)
+/// the root is the tenant's own, pinned by the backend, so none is sent;
+/// on CortexDB's own API it is the root, owned by the actor `user:<id>`.
+/// With `legacy_read`, the earlier `user:<id>` root is still read and
+/// forgotten (never written).
+fn rooted(
+ settings: EngineSettings,
+ root: Option<&str>,
+ tenant: bool,
+ legacy_read: bool,
+) -> (EngineSettings, String) {
+ let Some(root) = root else {
+ return (settings, "|root=legacy".to_string());
};
- (settings, format!("|root={}", root.unwrap_or("legacy")))
+ let actor = super::scope::actor_of_root(root);
+ let retired = legacy_read.then(|| actor.clone());
+ let fingerprint = format!(
+ "|root={root}|tenant={tenant}|retired={}",
+ retired.as_deref().unwrap_or("-")
+ );
+ let settings = if tenant {
+ EngineSettings {
+ tenant_root: true,
+ retired_scope_root: retired,
+ ..settings
+ }
+ } else {
+ EngineSettings {
+ scope_root: Some(root.to_string()),
+ scope_owner: Some(actor),
+ retired_scope_root: retired,
+ ..settings
+ }
+ };
+ tracing::debug!(
+ tenant,
+ legacy_read,
+ "[memory:engine] binding layout v3 below the person's org root"
+ );
+ (settings, fingerprint)
}
fn resolve_tinyhumans(config: &Config, root: Option<&str>) -> Binding {
@@ -362,6 +398,8 @@ fn resolve_tinyhumans(config: &Config, root: Option<&str>) -> Binding {
..EngineSettings::default()
},
root,
+ true,
+ config.memory.legacy_user_segment_read,
);
build_cached(
TINYHUMANS_ENGINE,
@@ -422,6 +460,8 @@ fn resolve_cortexdb(config: &Config, root: Option<&str>) -> Binding {
..EngineSettings::default()
},
root,
+ false,
+ config.memory.legacy_user_segment_read,
);
build_cached(
CORTEXDB_ENGINE,
diff --git a/crates/openhuman-core/src/memory/layout_migration/app_host.rs b/crates/openhuman-core/src/memory/layout_migration/app_host.rs
index 56e865cfe7..f8a6706aa9 100644
--- a/crates/openhuman-core/src/memory/layout_migration/app_host.rs
+++ b/crates/openhuman-core/src/memory/layout_migration/app_host.rs
@@ -61,7 +61,9 @@ impl LayoutHost for AppHost {
if config.memory.engine.trim() != CORTEXDB_ENGINE {
return Ok(None);
}
- let owner = signed_in_root(config)?;
+ // The account, by its actor (`user:<id>`): a claim made before
+ // `org:` roots named the same string, so it stays this account's.
+ let owner = scope::actor_of_root(&signed_in_root(config)?);
// `<app>/users/<id>/config.toml`: the app directory every account shares.
let app_dir = config
.config_path
diff --git a/crates/openhuman-core/src/memory/layout_migration/claim.rs b/crates/openhuman-core/src/memory/layout_migration/claim.rs
index 5a88d94e0c..d2b2f48747 100644
--- a/crates/openhuman-core/src/memory/layout_migration/claim.rs
+++ b/crates/openhuman-core/src/memory/layout_migration/claim.rs
@@ -20,7 +20,7 @@ use crate::memory::error::{MemoryError, MemoryResult};
pub struct ClaimKey {
/// The marker file.
pub marker: PathBuf,
- /// The claiming account's scope root (`user:<id>`).
+ /// The claiming account's actor (`user:<id>`).
pub owner: String,
/// The engine endpoint the tree lives on, normalised.
pub endpoint: String,
diff --git a/crates/openhuman-core/src/memory/scope.rs b/crates/openhuman-core/src/memory/scope.rs
index 46e10ed1b5..54be27a2a7 100644
--- a/crates/openhuman-core/src/memory/scope.rs
+++ b/crates/openhuman-core/src/memory/scope.rs
@@ -171,7 +171,7 @@ fn pooled(layout: MemoryLayout) -> MemoryLayout {
}
/// Where every agent's chats are under layout v3: `ws:main` below
-/// `layout`'s root. Relative to the engine's scope root (`user:<id>`), which
+/// `layout`'s root. Relative to the engine's scope root (`org:<id>`), which
/// is not a namespace segment.
#[must_use]
pub fn chat_node(layout: &MemoryLayout) -> Namespace {
@@ -187,11 +187,12 @@ pub fn layout_is_v3(config: &Config) -> bool {
config.memory.layout == MemoryLayoutMode::V3
}
-/// The engine scope root of the person `config` belongs to: `user:<id>`
-/// for a TinyHumans account (its 24-hex id), `user:local-<digest>` for a
+/// The engine scope root of the person `config` belongs to: `org:<id>`
+/// for a TinyHumans account (its 24-hex id), `org:local-<digest>` for a
/// local session (hashed, so no device name reaches the engine), and `None`
/// before anyone signs in. Read from where the config lives
-/// (`<root>/users/<id>/config.toml`).
+/// (`<root>/users/<id>/config.toml`). One root per person, with no `user:`
+/// segment below it: `user:` names the person's actor ([`actor_of_root`]).
#[must_use]
pub fn user_root(config: &Config) -> Option<String> {
let dir = config.config_path.parent()?;
@@ -209,14 +210,29 @@ fn user_root_for(id: &str) -> Option<String> {
}
let account = id.len() == 24 && id.bytes().all(|b| b.is_ascii_hexdigit());
if account {
- return Some(format!("user:{}", id.to_ascii_lowercase()));
+ return Some(format!("{ROOT_TYPE}:{}", id.to_ascii_lowercase()));
}
let digest: String = Sha256::digest(id.as_bytes())
.iter()
.take(8)
.map(|byte| format!("{byte:02x}"))
.collect();
- Some(format!("user:local-{digest}"))
+ Some(format!("{ROOT_TYPE}:local-{digest}"))
+}
+
+/// The scope type of a person's root.
+const ROOT_TYPE: &str = "org";
+
+/// The actor that owns the person's root `root` (`org:<id>` → `user:<id>`).
+/// It is also where layout v3 rooted that person before `org:` roots: the
+/// retired root read during the move, and the account a legacy-tree claim
+/// names.
+#[must_use]
+pub fn actor_of_root(root: &str) -> String {
+ match root.strip_prefix(ROOT_TYPE).and_then(|rest| rest.strip_prefix(':')) {
+ Some(id) => format!("user:{id}"),
+ None => root.to_string(),
+ }
}
/// Switches the memory of the person `config` belongs to to layout v3: