Skip to content

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

docs(storage): document the v2 scope grammar and boot sequence

Steven Enamakel543632d83fce
diff --git a/crates/openhuman-core/src/storage/README.md b/crates/openhuman-core/src/storage/README.md
index 231d856ea3..451fa2cd5e 100644
--- a/crates/openhuman-core/src/storage/README.md
+++ b/crates/openhuman-core/src/storage/README.md
@@ -63,7 +63,7 @@ record deleted afterwards is not resurrected from the untouched legacy file.
- `open(url)`: parse and open a backend (credentials are redacted in logs).
- `install(backend)` / `installed()` / `clear()`: the process slot domains
read the backend from.
-- `scope_for_agent(agent_id)`: the storage scope an agent's records live
+- `scope_for_agent(agent_id)`: the storage scope (`local/agent:<id>`) an agent's records live
under, the same mapping TinyAgents' `DriverSessionStores` uses, so every
domain agrees.
- `driver_is_shared(driver)` / `installed_is_shared()`: whether other
@@ -75,7 +75,7 @@ record deleted afterwards is not resurrected from the untouched legacy file.
within one process.
- `blob_sweep`: reclaims expired blobs (`StorageBackend::sweep_expired_blobs`)
for `local`, each known agent scope and each provisioned SaaS profile
- (`profile:<id>` plus the blob-owning modules' scopes for it) every hour,
+ (`u:<id>` plus the blob-owning modules' scopes for it) every hour,
spawned with the cron `ServiceSet` switch (`core::runtime::services`; not
gated on `[cron] enabled`, since it reclaims storage rather than running
jobs). Each scope's sweep is bounded by `SCOPE_SWEEP_TIMEOUT`; a timeout
@@ -83,7 +83,7 @@ record deleted afterwards is not resurrected from the untouched legacy file.
- `fence` / `fenced_backend`: lease-epoch write fencing (see
[Write fencing](#write-fencing)).
- `scope_for_profile(profile_id)`: the scope a SaaS profile's records live
- under (`profile:<id>`, hashed when that is not a valid scope).
+ under (`u:<id>`; tsd hashes the segment when it is not valid).
- `current_scope()` / `current_scoped()`: the tenant's scope — its profile's
when it serves one, else the acting agent's, else `local` on a single-user
host; an error in SaaS mode without a profile — and the installed backend
@@ -290,3 +290,11 @@ the session store over them is `tinyagents-session`. This domain only
resolves the URL, opens the backend, and holds it for the process. The
`[storage]` section is bootstrap configuration and is never read from
storage itself.
+
+## Schema, scopes and boot
+
+- `scope.rs`: the one scope grammar (`tenant_scope`, `scope_for_profile`, `scope_for_agent`, `scope_for_module`), built on tsd's `Scope::tenant/local/agent/module`.
+- `schema.rs`: `owner_schemas()` and the composed `registry()`; `module_items(owner)` feeds the generated module grants.
+- `boot.rs`: `prepare_and_migrate`, called by `open`: prepare, scope move, then per scope the importers and `Registry::migrate`.
+- `scope_move.rs`: the resumable copy of v1 scopes (`profile:`, `<profile>~<agent>`, raw agent ids, `module:<id>@<base>`, the `*-sha256:` fallbacks) into the v2 scopes.
+- The generated reference is `docs/storage-schema.md`; `node scripts/ci/check-storage-schema.mjs` lints it.
diff --git a/docs/gitbooks/en/developing/architecture/storage.md b/docs/gitbooks/en/developing/architecture/storage.md
index bf85efcc6b..33039e7873 100644
--- a/docs/gitbooks/en/developing/architecture/storage.md
+++ b/docs/gitbooks/en/developing/architecture/storage.md
@@ -7,7 +7,7 @@ icon: database
# Storage
-OpenHuman keeps durable state on the ports of [`tinystoragedrivers`](https://github.com/tinyhumansai/tinystoragedrivers), vendored at `vendor/tinystoragedrivers` (v0.11.0). The host and every module declare it by git tag, and the root and app `[patch."https://github.com/tinyhumansai/tinystoragedrivers"]` tables redirect each of them onto that one checkout, so the build holds one copy. The vendored commit must stay semver-compatible with the tag modules declare; `scripts/ci/check-storage-owner.mjs` fails when any `tinystoragedrivers*` package resolves outside `vendor/tinystoragedrivers/`. One URL picks the backend for the whole process. The host adapter is `crates/openhuman-core/src/storage/`. It resolves the URL, opens the backend, holds it in a process slot and decides which scope a call runs in. The ports, the drivers and their conformance suites belong to `tinystoragedrivers`, and the typed records stay with the domain that owns them.
+OpenHuman keeps durable state on the ports of [`tinystoragedrivers`](https://github.com/tinyhumansai/tinystoragedrivers), vendored at `vendor/tinystoragedrivers` (v1.0.0). The host and every module declare it by git tag, and the root and app `[patch."https://github.com/tinyhumansai/tinystoragedrivers"]` tables redirect each of them onto that one checkout, so the build holds one copy. The vendored commit must stay semver-compatible with the tag modules declare; `scripts/ci/check-storage-owner.mjs` fails when any `tinystoragedrivers*` package resolves outside `vendor/tinystoragedrivers/`. One URL picks the backend for the whole process. The host adapter is `crates/openhuman-core/src/storage/`. It resolves the URL, opens the backend, holds it in a process slot and decides which scope a call runs in. The ports, the drivers and their conformance suites belong to `tinystoragedrivers`, and the typed records stay with the domain that owns them.
With no URL configured, which is the desktop default, the host opens `sqlite:<workspace>/storage` and installs it. The URL decides where the whole process keeps its state, and there is no second layout: older installs' per-domain databases, transcripts and turn-state files are imported once, read-only, when the backend is installed. The files that remain outside the ports are listed under "What is not on the ports yet" below.
@@ -52,12 +52,14 @@ The host opens the configured backend before the core boots and installs it with
## Scopes
-Every record lives in a scope, and two scopes on one backend never see each other's data. A single-user install scopes by **agent id**, and a SaaS host scopes by **profile** (the tenant). `storage::scope_for_agent` and `storage::scope_for_profile` are the single mappings, and the session store uses the same ones, so every domain agrees. An agent's scope is its id when that is a valid scope name, else `sha256:<hex digest of the id>` (TinyAgents' `DriverSessionStores::scope_for`). A profile's scope is `profile:<id>`, or `profile-sha256:<hex digest of the id>` when that is not a valid scope name. The prefixes keep the two kinds of key from colliding, and `local` is the literal operator scope.
+Every record lives in a scope, and two scopes on one backend never see each other's data. There is one scope grammar, tinystoragedrivers' (`docs/specs/schema-v2.md` §1): a **tenant** (`local` for the desktop operator, `u:<profile>` for a SaaS profile) optionally narrowed to one **principal** (`<tenant>/agent:<id>` or `<tenant>/module:<id>`), plus the tenant-less `cluster` scope that holds the profile registry and the process leases. The host builds scopes only through `Scope::local()`, `Scope::tenant(id)`, `.agent(id)` and `.module(id)` (`storage::scope`: `tenant_scope`, `scope_for_profile`, `scope_for_agent`, `scope_for_module`), never from raw strings. A segment that is too long or holds characters outside the grammar is hashed by tsd itself (`~` plus 26 base32 characters), and only the segment is hashed, so a hashed agent or module still sits under its tenant.
+
+The **tenant is the isolation boundary.** A profile's lease fence guards its tenant scope and the prefix `<tenant>/`, which covers every agent and module scope beneath it, hashed or not (`profiles::fence::scopes_of`). A tenant purge covers the tenant scope and every principal under it, enumerated with `StorageBackend::scopes_of_tenant`. The session store is TinyAgents' `DriverSessionStores`, one provider per tenant (`agent::session_store::TenantSessionStores` routes the host's flat session key `<profile>~<agent>` to the provider of that tenant), so a session record is always in `local/agent:<id>` or `u:<profile>/agent:<id>`. Domain stores keep their records in the tenant scope, and a loadable module's records are in `<tenant>/module:<id>`.
`storage::current_scope()` resolves the scope of the current call from the task's tenant (`storage::scope_from(profile, agent, saas)`), in this order:
1. A **profile**, when the work runs for one: the scope is the profile's, whatever agent acts inside it. In SaaS mode the agents of one profile therefore share that profile's scope.
-2. Otherwise the acting **agent** (`CoreContext::session_agent`): the scope is that agent's.
+2. Otherwise the acting **agent** (`CoreContext::session_agent`): the scope is `local/agent:<id>`.
3. Otherwise `local`: the operator, the CLI and the desktop shell in single-user mode.
In SaaS mode a call with no profile is refused instead of falling into a bucket other users could share, whether or not an agent acts. If the task's tenant cannot be resolved at all (`current_tenant()` fails), `current_scope()` returns that error in every mode instead of falling back to `local`.
@@ -166,3 +168,15 @@ CI has two lanes for this:
- `crates/openhuman-core/src/storage/README.md` for the entry points and the consumers.
- The `tinystoragedrivers` specs: `docs/specs/storage-ports.md` and `docs/plans/storage-rollout.md` in the tinystoragedrivers repository (tag `v0.11.0`).
- [Security](security.md) for the keyring and credential stores.
+
+## Schema, boot and migration
+
+Every crate that stores records exports `storage_schema()` (collections named `<owner>.<entity>`, streams, blob and secret prefixes, each with a scope class and the v1 to v2 steps that bring older data forward). The host composes them in `storage::schema` (`Registry::compose`: harness, session, graph, tasks, flows and cron, skills, and with `modules` the mcp, channels, connectors, box and juice owners, with `web3` the wallet, with `tinymemes` the memes owner). The composed result is documented in [`docs/storage-schema.md`](../../../../storage-schema.md), generated by `scripts/ci/check-storage-schema.mjs` (`pnpm docs:generate`), which also lints names, casing, `*_ms` time fields and single ownership in `pnpm rust:layout`.
+
+`storage::open` runs `storage::boot::prepare_and_migrate` on the raw backend before the lease fence wraps it:
+
+1. `Registry::prepare` declares every collection.
+2. **Scope move** (`storage::scope_move`): every v1 scope is copied (documents with their schema, creation time and expiry; streams and their sealed state; blobs) into the v2 scope it maps to and then removed. v1 raw agent ids and `default` become `local/agent:<id>`, `<profile>~<agent>` becomes `u:<profile>/agent:<agent>`, `profile:<id>` becomes `u:<id>`, `module:<id>@<base>` and `<profile>~module:<id>` become `<tenant>/module:<id>`. A hashed v1 scope (`sha256:`, `profile-sha256:`, `module-sha256:`) maps only when a recorded agent, a provisioned profile or a registry module hashes to it; others stay in place and are logged. The move is resumable and recorded in the target scope's `tsd.migrations` (owner `host`, step `scope_move:<source>`).
+3. Per scope: the pre-migrate importer (`import_legacy_ledger`), `Registry::migrate` (each owner's steps), then the post-migrate importers that move key-value state and counters into `tsd.kv` and `tsd.counters` (`import_legacy_kv`, `import_legacy_leases`, `import_legacy_task_cache`, the flows importers). A failing piece is logged and retried at the next boot.
+
+A module's storage grant is generated from the module-scoped items of its owner's schema (`modules::storage_grant_schemas`), so it cannot drift from what the module declares. The module's legacy-file importers still write the v1 shapes; the owner's steps carry them forward.