Skip to content

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

feat(config): add storage schema source for config loading

Steven Enamakelab337e0c1ceb
diff --git a/crates/openhuman-core/src/config/schema/load/source/document.rs b/crates/openhuman-core/src/config/schema/load/source/document.rs
index 74ef4afa01..5923cf6dd9 100644
--- a/crates/openhuman-core/src/config/schema/load/source/document.rs
+++ b/crates/openhuman-core/src/config/schema/load/source/document.rs
@@ -11,8 +11,8 @@ use tinystoragedrivers::{CollectionSpec, DocumentStore, Precondition};
use super::{apply_bootstrap, strip_bootstrap, ConfigRead, ConfigSource, FileConfigSource};
use crate::storage::Scope;
-/// The collection holding one config document per scope.
-pub(crate) const CONFIG_COLLECTION: &str = "config";
+pub(crate) use super::storage_schema::CONFIG_COLLECTION;
+use super::storage_schema::document_id;
/// The document field holding the encrypted TOML body (`enc2:` hex).
const BODY_FIELD: &str = "toml_enc";
@@ -101,7 +101,7 @@ impl DocumentConfigSource {
.context("declare the config collection")?;
let stored = self
.docs
- .get(CONFIG_COLLECTION, self.scope.as_str())
+ .get(CONFIG_COLLECTION, &document_id(self.scope.as_str())?)
.await
.context("read the config document")?;
let Some(versioned) = stored else {
@@ -144,7 +144,7 @@ impl ConfigSource for DocumentConfigSource {
.context("declare the config collection")?;
let stored = self
.docs
- .get(CONFIG_COLLECTION, self.scope.as_str())
+ .get(CONFIG_COLLECTION, &document_id(self.scope.as_str())?)
.await
.context("read the config document")?;
if stored.is_some() {
@@ -194,7 +194,7 @@ impl ConfigSource for DocumentConfigSource {
self.docs
.put(
CONFIG_COLLECTION,
- self.scope.as_str(),
+ &document_id(self.scope.as_str())?,
json!({ BODY_FIELD: sealed }),
Precondition::None,
)
diff --git a/crates/openhuman-core/src/config/schema/load/source/mod.rs b/crates/openhuman-core/src/config/schema/load/source/mod.rs
index 3cf3bf25e7..5eb601dbb9 100644
--- a/crates/openhuman-core/src/config/schema/load/source/mod.rs
+++ b/crates/openhuman-core/src/config/schema/load/source/mod.rs
@@ -29,6 +29,7 @@
mod document;
mod file;
+pub(crate) mod storage_schema;
use anyhow::{Context, Result};
use async_trait::async_trait;
diff --git a/crates/openhuman-core/src/config/schema/load/source/storage_schema.rs b/crates/openhuman-core/src/config/schema/load/source/storage_schema.rs
new file mode 100644
index 0000000000..594c1bb85d
--- /dev/null
+++ b/crates/openhuman-core/src/config/schema/load/source/storage_schema.rs
@@ -0,0 +1,57 @@
+//! The `config` owner's storage schema: the per-scope config document and the
+//! step that brings the v1 collection forward.
+//!
+//! | v1 | v2 | change |
+//! |---|---|---|
+//! | `config` (id = the scope string) | `config.documents` (id `Key::of([scope])`) | rename; `toml_enc` (an encrypted value) is kept as is |
+
+use tinystoragedrivers::schema::{Schema, ScopeClass, SourceRecord, Step, TargetRecord};
+use tinystoragedrivers::{CollectionSpec, Result};
+
+use crate::storage::schema_support::{key_of, owner};
+
+/// The v2 collection holding one config document per scope.
+pub(crate) const CONFIG_COLLECTION: &str = "config.documents";
+/// The v1 name.
+pub(crate) const LEGACY_CONFIG_COLLECTION: &str = "config";
+const SCHEMA: u32 = 2;
+
+/// What the config source keeps and the step from the v1 collection.
+#[must_use]
+pub fn storage_schema() -> Schema {
+ Schema::new(owner("config"))
+ .collection_with(
+ SCHEMA,
+ CollectionSpec::new(CONFIG_COLLECTION),
+ ScopeClass::Tenant,
+ )
+ .step(
+ Step::new("documents_v2", CONFIG_COLLECTION, 1, SCHEMA, documents_v2)
+ .source(LEGACY_CONFIG_COLLECTION),
+ )
+}
+
+/// The document id of the config of `scope`.
+///
+/// # Errors
+///
+/// When `scope` is empty.
+pub(crate) fn document_id(scope: &str) -> std::result::Result<String, tinystoragedrivers::StorageError> {
+ key_of(scope)
+}
+
+fn documents_v2(batch: Vec<SourceRecord>) -> Result<Vec<TargetRecord>> {
+ batch
+ .into_iter()
+ .map(|record| {
+ Ok(
+ TargetRecord::new(CONFIG_COLLECTION, document_id(&record.id)?, record.body)
+ .created_ms(record.meta.created_ms),
+ )
+ })
+ .collect()
+}
+
+#[cfg(test)]
+#[path = "storage_schema_tests.rs"]
+mod tests;
diff --git a/crates/openhuman-core/src/config/schema/load/source/storage_schema_tests.rs b/crates/openhuman-core/src/config/schema/load/source/storage_schema_tests.rs
new file mode 100644
index 0000000000..094bec7994
--- /dev/null
+++ b/crates/openhuman-core/src/config/schema/load/source/storage_schema_tests.rs
@@ -0,0 +1,21 @@
+use super::*;
+use serde_json::json;
+use tinystoragedrivers::Meta;
+
+#[test]
+fn a_scopes_config_document_keeps_its_sealed_body_under_the_new_name() {
+ let out = documents_v2(vec![SourceRecord {
+ id: "u:alice/agent:x".into(),
+ body: json!({ "toml_enc": "enc2:abcd" }),
+ meta: Meta::unknown(),
+ }])
+ .unwrap();
+ assert_eq!(out[0].collection, CONFIG_COLLECTION);
+ assert_eq!(out[0].id, "u:alice%2Fagent:x");
+ assert_eq!(out[0].body, json!({ "toml_enc": "enc2:abcd" }));
+}
+
+#[test]
+fn the_schema_composes_under_its_owner() {
+ tinystoragedrivers::schema::Registry::compose([storage_schema()]).unwrap();
+}
diff --git a/crates/openhuman-core/src/config/schema/mod.rs b/crates/openhuman-core/src/config/schema/mod.rs
index e315760604..481259b6fa 100644
--- a/crates/openhuman-core/src/config/schema/mod.rs
+++ b/crates/openhuman-core/src/config/schema/mod.rs
@@ -35,6 +35,7 @@ pub use profile_layout::{users_dir, ProfileLayout};
mod identity_cost;
mod load;
pub(crate) use load::config_from_toml_str;
+pub(crate) use load::source::storage_schema::storage_schema as config_storage_schema;
pub use load::{
action_dir_env_override, active_user_marker_path, active_workspace_dir,
active_workspace_dir_cached, active_workspace_snapshot, clear_active_user, default_action_dir,