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(schema): update storage schema dump to reflect v2 collections
Steven Enamakel1d17fe9b58eb
diff --git a/examples/storage_schema_dump.rs b/examples/storage_schema_dump.rsindex 5112ca1e0a..1a62331ef5 100644--- a/examples/storage_schema_dump.rs+++ b/examples/storage_schema_dump.rs@@ -1,83 +1,17 @@-//! Prints the composed storage schema as JSON, for `scripts/ci/check-storage-schema.mjs`.+//! Prints the composed v2 collection declarations as JSON for the storage+//! schema documentation check.//!-//! One object per owner, in composition order: its collections (name, version,-//! scope class, index and search field names), streams, blob and secret-//! prefixes, and the ids of its migration steps. The script lints names and-//! single ownership from this and renders `docs/storage-schema.md`.+//! The dump reflects the `CollectionSpec` values the host prepares: names,+//! indexes, expiry fields, and search fields. Record shapes and migrations+//! remain owned by their domains.//!//! Run: `cargo run -p openhuman-cli --example storage_schema_dump`-//! Feature: web3 (adds the wallet owner)+//! Feature: `web3` adds the wallet owner's collections.-use openhuman_core::storage::schema::owner_schemas;-use serde_json::{json, Value};-use tinystoragedrivers_core::schema::{Item, ScopeClass};--fn scope(class: ScopeClass) -> &'static str {- match class {- ScopeClass::Tenant => "tenant",- ScopeClass::Agent => "agent",- ScopeClass::Module => "module",- ScopeClass::Cluster => "cluster",- }-}--fn item(item: &Item) -> Value {- match item {- Item::Collection {- name,- version,- spec,- scope: class,- } => json!({- "kind": "collection",- "name": name,- "version": version,- "scope": scope(*class),- "index_fields": spec- .indexes- .iter()- .flat_map(|index| index.fields.iter().cloned())- .collect::<Vec<_>>(),- }),- Item::Stream {- name, scope: class, ..- } => {- json!({ "kind": "stream", "name": name, "scope": scope(*class) })- }- Item::Blobs {- prefix,- scope: class,- } => {- json!({ "kind": "blobs", "name": prefix, "scope": scope(*class) })- }- Item::Secrets {- prefix,- scope: class,- } => {- json!({ "kind": "secrets", "name": prefix, "scope": scope(*class) })- }- }-}+use openhuman_core::storage::schema::owner_collections;+use serde_json::json;fn main() {- let owners: Vec<Value> = owner_schemas()- .iter()- .map(|schema| {- json!({- "owner": schema.owner.as_str(),- "items": schema.items.iter().map(item).collect::<Vec<_>>(),- "steps": schema.steps.iter().map(|step| json!({- "id": step.id,- "item": step.item,- "from": step.from,- "to": step.to,- "source": step.source,- })).collect::<Vec<_>>(),- })- })- .collect();- println!(- "{}",- serde_json::to_string_pretty(&json!({ "owners": owners })).unwrap()- );+ let collections = owner_collections().expect("owner collection declarations compose");+ println!("{}", serde_json::to_string_pretty(&json!({ "collections": collections })).unwrap());}diff --git a/scripts/ci/check-storage-schema.mjs b/scripts/ci/check-storage-schema.mjsindex 6bf9767b3d..70769c057c 100644--- a/scripts/ci/check-storage-schema.mjs+++ b/scripts/ci/check-storage-schema.mjs@@ -1,20 +1,18 @@#!/usr/bin/env node// Storage-schema gate (tinyhumansai/openhuman#7992, part of #7973).//-// Every crate that stores records exports `storage_schema()`; the host composes-// them (`crates/openhuman-core/src/storage/schema.rs`). This check dumps the-// composed schema as JSON (the `storage_schema_dump` example of-// `openhuman-cli`), lints it, and keeps `docs/storage-schema.md` in step.+// Owners expose v2 `CollectionSpec` values; the host composes them+// (`crates/openhuman-core/src/storage/schema.rs`). This check dumps the+// composed declarations as JSON (the `storage_schema_dump` example of+// `openhuman-cli`), lints them, and keeps `docs/storage-schema.md` in step.//// Lint rules (the spec is docs/specs/schema-v2.md in tinystoragedrivers):-// 1. owner names are `[a-z][a-z0-9_]{1,23}` and `tsd` is reserved;-// 2. a collection or stream is `<owner>.<entity>` with a snake_case entity-// (lowercase letters, digits and `_`), no version suffix (`_v2`);-// 3. a blob or secret prefix is `<owner>/<path>`;-// 4. a name or prefix is declared by exactly one owner (single ownership);-// 5. indexed fields that hold a time are `*_ms` (no `_at`, `_ns`, `_us`,+// 1. collection names are `<owner>.<entity>` with a snake_case entity+// (lowercase letters, digits and `_`);+// 2. each composed collection name and index name is unique;+// 3. indexed fields that hold a time are `*_ms` (no `_at`, `_ns`, `_us`,// `_secs`, `_ts` or `_time` suffix);-// 6. a migration step names an item its owner declares (or a legacy source).+// 4. expiry and search declarations are carried through the composed dump.//// Usage:// check-storage-schema.mjs lint and fail on doc drift@@ -32,11 +30,9 @@ const HERE = dirname(fileURLToPath(import.meta.url));export const REPO_ROOT = resolve(HERE, '..', '..');export const DOC_PATH = resolve(REPO_ROOT, 'docs', 'storage-schema.md');-const OWNER = /^[a-z][a-z0-9_]{1,23}$/;+const COLLECTION = /^[a-z][a-z0-9_]{1,23}\.[a-z][a-z0-9_]*$/;const ENTITY = /^[a-z][a-z0-9_]*$/;-const VERSION_SUFFIX = /_v\d+$/;const TIME_SUFFIX = /(_at|_ns|_us|_secs|_ts|_time)$/;-const SCOPES = new Set(['tenant', 'agent', 'module', 'cluster']);/** Features the dump is built with: the contributor set plus the wallet owner. */export const DUMP_FEATURES = 'web3';@@ -91,52 +87,31 @@ export function dumpSchema(root = REPO_ROOT, { run: spawn = spawnSync, env = pro/** @returns {string[]} one message per violation */export function lint(dump) {const problems = [];- const claimed = new Map();- const owners = new Set();- for (const { owner, items, steps } of dump.owners) {- if (!OWNER.test(owner) || owner === 'tsd') {- problems.push(`owner "${owner}" is not a valid owner name`);+ const names = new Set();+ for (const collection of dump.collections ?? []) {+ const { name, indexes = [], ttl_field: ttlField, search } = collection;+ if (!COLLECTION.test(name)) {+ problems.push(`collection "${name}" must be <owner>.<entity> with a snake_case entity`);}- if (owners.has(owner)) problems.push(`owner "${owner}" is declared twice`);- owners.add(owner);- const declared = new Set();- for (const item of items) {- declared.add(item.name);- if (!SCOPES.has(item.scope)) {- problems.push(`${owner}: "${item.name}" has unknown scope class "${item.scope}"`);+ if (names.has(name)) problems.push(`collection "${name}" is declared more than once`);+ names.add(name);+ const indexNames = new Set();+ for (const index of indexes) {+ if (!index.name || indexNames.has(index.name)) {+ problems.push(`${name}: index name "${index.name}" is empty or duplicated`);}- const where = `${owner}: ${item.kind} "${item.name}"`;- if (item.kind === 'collection' || item.kind === 'stream') {- const dot = item.name.indexOf('.');- const prefix = dot < 0 ? '' : item.name.slice(0, dot);- const entity = dot < 0 ? '' : item.name.slice(dot + 1);- if (prefix !== owner || !ENTITY.test(entity)) {- problems.push(`${where} must be <owner>.<entity> with a snake_case entity`);- } else if (VERSION_SUFFIX.test(entity)) {- problems.push(`${where} carries a version suffix; the version lives in the registry`);- }- } else if (!item.name.startsWith(`${owner}/`) || item.name === `${owner}/`) {- problems.push(`${where} must be <owner>/<path>`);- }- for (const field of item.index_fields ?? []) {+ indexNames.add(index.name);+ for (const field of index.fields ?? []) {if (TIME_SUFFIX.test(field)) {- problems.push(`${where}: indexed field "${field}" holds a time and must be named *_ms`);+ problems.push(`${name}: indexed field "${field}" holds a time and must be named *_ms`);}}- const key = `${item.kind}:${item.name}`;- if (claimed.has(key)) {- problems.push(`${where} is also declared by "${claimed.get(key)}" (single ownership)`);- } else {- claimed.set(key, owner);- }}- for (const step of steps) {- if (!declared.has(step.item)) {- problems.push(`${owner}: step "${step.id}" names undeclared item "${step.item}"`);- }- if (!(step.to > step.from)) {- problems.push(`${owner}: step "${step.id}" does not go up (${step.from} -> ${step.to})`);- }+ if (ttlField && TIME_SUFFIX.test(ttlField)) {+ problems.push(`${name}: expiry field "${ttlField}" holds a time and must be named *_ms`);+ }+ for (const field of search?.fields ?? []) {+ if (!field) problems.push(`${name}: search fields must not be empty`);}}return problems;@@ -148,32 +123,21 @@ export function render(dump) {'# Storage schema','','<!-- Generated by `pnpm docs:generate` (scripts/ci/check-storage-schema.mjs) from the',- 'composed registry (`crates/openhuman-core/src/storage/schema.rs`). Do not edit by hand. -->',+ 'composed owner `CollectionSpec` values (`crates/openhuman-core/src/storage/schema.rs`). Do not edit by hand. -->','',- 'Every crate that stores records declares them in `storage_schema()`; the host composes',- 'the declarations into one registry, prepares the backend from it and migrates every',- 'scope at boot. Names are `<owner>.<entity>`; the scope class says where a record lives',- '(`tenant`: `local` or `u:<id>`, `agent`: `<tenant>/agent:<id>`, `module`:',- '`<tenant>/module:<id>`, `cluster`: the shared registry scope).',+ 'Record owners declare collections with `CollectionSpec`; the host composes and prepares',+ 'those declarations at boot. The table lists collection names, indexes, expiry fields, and',+ 'full-text search fields. Record shapes and migrations stay with their owners.','',];- const sorted = [...dump.owners].sort((a, b) => a.owner.localeCompare(b.owner));- for (const { owner, items, steps } of sorted) {- out.push(`## ${owner}`, '', '| Kind | Name | Version | Scope | Indexed fields |', '| --- | --- | --- | --- | --- |');- for (const item of [...items].sort((a, b) => a.name.localeCompare(b.name))) {- out.push(- `| ${item.kind} | \`${item.name}\` | ${item.version ?? ''} | ${item.scope} | ${(item.index_fields ?? []).map(f => `\`${f}\``).join(', ')} |`- );- }- out.push('');- if (steps.length > 0) {- out.push('Migration steps:', '');- for (const step of steps) {- const source = step.source ? ` from \`${step.source}\`` : '';- out.push(`- \`${step.id}\`: \`${step.item}\` v${step.from} to v${step.to}${source}`);- }- out.push('');- }+ out.push('| Collection | Indexes | Expiry field | Search fields |', '| --- | --- | --- | --- |');+ for (const collection of [...(dump.collections ?? [])].sort((a, b) => a.name.localeCompare(b.name))) {+ const indexes = (collection.indexes ?? []).map(index =>+ `\`${index.name}\` (${index.fields.map(field => `\`${field}\``).join(', ')})${index.unique ? ' unique' : ''}`+ ).join('<br>');+ out.push(+ `| \`${collection.name}\` | ${indexes} | ${collection.ttl_field ? `\`${collection.ttl_field}\`` : ''} | ${(collection.search?.fields ?? []).map(field => `\`${field}\``).join(', ')} |`+ );}return `${out.join('\n').trimEnd()}\n`;}