Skip to content

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

feat(scripts): storage scenarios against the real core

M3gA-Mindecd7a0119df0
diff --git a/scripts/__tests__/storage-scenarios-db.test.mjs b/scripts/__tests__/storage-scenarios-db.test.mjs
new file mode 100644
index 0000000000..580a2af710
--- /dev/null
+++ b/scripts/__tests__/storage-scenarios-db.test.mjs
@@ -0,0 +1,53 @@
+import assert from "node:assert/strict";
+import fs from "node:fs";
+import os from "node:os";
+import path from "node:path";
+import test from "node:test";
+
+import { StorageDb, sqliteFile, grepTree } from "../storage-scenarios/lib.mjs";
+
+// The table shapes of tinystoragedrivers' SQLite driver, under names other
+// than the defaults: the reader must find them by their columns.
+const TSD_SHAPE = `
+ CREATE TABLE d (scope TEXT, coll TEXT, id TEXT, version INTEGER, schema INTEGER,
+ created_ms INTEGER, updated_ms INTEGER, expires_ms INTEGER, deleted INTEGER, body TEXT);
+ CREATE TABLE s (scope TEXT, name TEXT, seq INTEGER, body TEXT);
+ CREATE TABLE b (scope TEXT, key TEXT, content_type TEXT, size INTEGER, sha256 BLOB,
+ expires_ms INTEGER, bytes BLOB);
+ CREATE TABLE k (scope TEXT, key TEXT, version INTEGER, expires_ms INTEGER, deleted INTEGER, value TEXT);
+ INSERT INTO d VALUES ('local', 'cron.jobs', 'j1', 1, 0, 0, 0, NULL, 0, '{"name":"storscen-a"}');
+ INSERT INTO d VALUES ('local', 'cron.jobs', 'j2', 1, 0, 0, 0, NULL, 1, '{"name":"gone"}');
+ INSERT INTO d VALUES ('u:x', 'secrets', 'k', 1, 0, 0, 0, NULL, 0, 'enc2:abc');
+ INSERT INTO s VALUES ('local', 'conversations.messages/t', 0, '{"content":"hello"}');
+ INSERT INTO b VALUES ('local', 'voice_notes/n.webm', 'audio/webm', 3, x'00', NULL, x'010203');
+ INSERT INTO k VALUES ('local', 'memory/state/jobs', 1, NULL, 0, '{}');
+`;
+
+test("StorageDb finds tsd tables by their columns and reads them read-only", () => {
+ const dir = fs.mkdtempSync(path.join(os.tmpdir(), "storscen-"));
+ const file = path.join(dir, "storage.db");
+ sqliteFile(file, TSD_SHAPE);
+ const db = new StorageDb(file);
+ try {
+ assert.deepEqual(db.tables, { docs: "d", streams: "s", blobs: "b", kv: "k" });
+ assert.deepEqual(db.docs("cron.jobs").map((r) => r.id), ["j1"], "deleted rows are not live");
+ assert.deepEqual(db.census(), [
+ { scope: "local", coll: "cron.jobs", n: 1 },
+ { scope: "u:x", coll: "secrets", n: 1 },
+ ]);
+ assert.deepEqual(db.scopes(), ["local", "u:x"]);
+ assert.equal(Buffer.from(db.blob("%n.webm%")[0].bytes).toString("hex"), "010203");
+ assert.deepEqual(db.integrity(), ["ok"]);
+ assert.throws(() => db.db.exec("DELETE FROM d"), "opened read-only");
+ } finally {
+ db.close();
+ }
+});
+
+test("grepTree finds bytes in binary files and misses absent ones", async () => {
+ const dir = fs.mkdtempSync(path.join(os.tmpdir(), "storscen-"));
+ fs.mkdirSync(path.join(dir, "a"));
+ fs.writeFileSync(path.join(dir, "a", "x.bin"), Buffer.concat([Buffer.from([0, 1]), Buffer.from("needle-42"), Buffer.from([255])]));
+ assert.equal((await grepTree(dir, "needle-42")).length, 1);
+ assert.equal((await grepTree(dir, "needle-43")).length, 0);
+});
diff --git a/scripts/storage-scenarios/README.md b/scripts/storage-scenarios/README.md
new file mode 100644
index 0000000000..ec42aa650b
--- /dev/null
+++ b/scripts/storage-scenarios/README.md
@@ -0,0 +1,184 @@
+# Storage scenarios
+
+How [tinystoragedrivers](../../vendor/tinystoragedrivers) works inside
+OpenHuman, and whether data sent through the real core is stored correctly.
+The script boots `openhuman-core serve`, drives data through the same JSON-RPC
+methods the app calls, then checks it two ways: read back over RPC, and read
+straight from the database or file tree. Every failed check is a finding for
+a person to read; **nothing is fixed during a run**.
+
+```bash
+# every driver (sqlite, then file, then memory)
+node scripts/storage-scenarios/run.mjs
+
+# one driver, some scenarios (by id prefix)
+node scripts/storage-scenarios/run.mjs --driver sqlite --only A,B,C
+
+# re-render the report of an old run
+node scripts/storage-scenarios/run.mjs --grade-only target/storage-scenarios/<run-id>
+```
+
+Requires a core build (`cargo build -p openhuman-cli --bin openhuman-core`) and
+Node 24 (it reads SQLite with the built-in `node:sqlite`). It needs no account,
+no network and no chat model: every record is written by an RPC, never by an
+LLM turn.
+
+The driver is [`run.mjs`](./run.mjs). The scenarios on the driver's own core
+are in [`scenarios.mjs`](./scenarios.mjs), the ones that need a HOME of their
+own in [`isolated.mjs`](./isolated.mjs), and the legacy-profile fixture in
+[`legacy.mjs`](./legacy.mjs). The core rig (own HOME, JSON-RPC, scrubbed logs,
+findings) is shared with [`memory-scenarios`](../memory-scenarios/lib.mjs).
+
+## How storage works
+
+What follows is read from the code (`crates/openhuman-core/src/storage/`,
+`docs/gitbooks/en/developing/architecture/storage.md`); the scenarios below
+check it.
+
+**One backend per process, chosen by one URL.** The first that is set wins:
+`OPENHUMAN_STORAGE_URL`, then `[storage] url` in `config.toml`, then the
+default, a SQLite database that belongs to the active profile. The host opens
+it before the core boots and installs it in a process slot
+(`storage::install`); every domain store and TinyAgents' session store read
+from that slot. A URL for a driver the build lacks fails at boot, naming the
+Cargo feature.
+
+| URL | Driver | Where the data is |
+| --- | --- | --- |
+| (default, profile layout v2) | SQLite | `~/.openhuman/users/<id>/storage.db` (+ `-wal`, `-shm`) |
+| (default, profile layout v1) | SQLite | `~/.openhuman/users/<id>/workspace/storage/storage.db` |
+| `sqlite:<dir or file.db>` | SQLite | that directory's `storage.db`, or that file |
+| `file:<dir>` | files | `<dir>/scopes/<scope>/docs/<collection>/<id>.json`; streams as JSONL and blobs as raw files under the same scope directory |
+| `memory` | in-process | nowhere: gone when the process exits |
+| `mongodb://…/<db>` | MongoDB | one database for every scope (a cloud build only) |
+
+The default follows the profile, so a different user is a different database:
+signing another user in rebinds the backend to that user's file. An explicit
+URL names one backend for every user and is never rebound.
+
+**Six ports.** A backend hands out one handle per scope, and every port on the
+handle is already bound to that scope.
+
+| Port | Holds | Driven here by |
+| --- | --- | --- |
+| `DocumentStore` | versioned JSON documents in named collections, with compare-and-swap | `cron_add`, `notification_ingest`, `approval_preauthorize_flow`, `flows_create` / `flows_update` |
+| `StreamStore` | append-only logs with dense offsets | `threads_message_append` (thread messages) |
+| `BlobStore` | opaque bytes by key | `channel_web_voice_note_store` (`voice_notes/<id>.<ext>`) |
+| `KvStore` | small JSON values by key | not drivable by RPC without a memory engine |
+| `QueueStore` | leased queue deliveries | not driven |
+| `SecretStore` | encrypted values | `auth_store_provider_credentials`, `auth_set_credential` |
+
+On SQLite every port is a table with a `scope` column (`docs` holds
+`(scope, coll, id, version, body)`, streams `(scope, name, seq, body)`, blobs
+`(scope, key, bytes, sha256)`), so one file holds every scope and a query
+always names one.
+
+**Scopes.** Every record lives in exactly one scope, and two scopes never see
+each other's data. A scope is a tenant (`local` for the desktop operator,
+`u:<profile>` for a SaaS profile), optionally narrowed to a principal
+(`local/agent:<id>`, `local/module:<id>`), plus `cluster` for process-wide
+leases. The scope of a call is the profile's when it serves one, else the
+acting agent's, else `local`; in SaaS mode a call with no profile is refused.
+Domain stores (cron, notifications, approvals, threads, the flows catalog)
+write to the acting scope, which for the desktop operator is `local`; a
+session record is under `local/agent:<id>`.
+
+**Secrets.** With a backend installed, a secret is an encrypted document
+(collection `secrets`) in the acting scope. Each scope's data key is derived
+with HKDF-SHA256 from the keyring master key (`OPENHUMAN_KEYRING_MASTER_KEY` or
+`_FILE`, else the OS keychain); with no master key, secret writes fail closed.
+Provider credentials are one secret (`file:auth-profiles.json`); the session
+token is a profile inside it.
+
+**Legacy import.** At boot, before any domain store opens, the core imports
+what older releases kept in per-domain files: `approval/approval.db`,
+`devices/devices.db`, `notifications/notifications.db`,
+`task_sources/sources.db`, the tinyflows stores (`cron/jobs.db`,
+`flows/flows.db`, drafts, checkpoints) and the sessions (`session_db/`,
+`session_raw/*.jsonl`, turn states). The import reads copies, writes each
+record only if absent, and stores a marker in `storage.import_state`
+(`storage_import/<domain>@<workspace digest>`), so a later boot imports
+nothing and a deleted record is never resurrected. The cost ledger
+(`state/costs.jsonl`) is imported on first use and renamed to
+`costs.jsonl.migrated`; `auth-profiles.json` is adopted into the secret on
+first read and left in place.
+
+**Layout v2.** A profile directory with content and no `.layout` marker is
+layout v1 (`users/<id>/workspace/` holding everything). `[storage]
+auto_migrate_layout = true` (or `storage.migrate` with the core stopped)
+moves it to v2: it imports, `VACUUM INTO`s `users/<id>/storage.db`, moves user
+files to `files/`, writes and verifies `backups/<user>-<ts>.tar.zst`, imports
+the credential files when a master key is resolvable (and leaves them in place
+when it is not, #8076), then writes the marker.
+
+## Isolation and safety
+
+- Each core runs with **its own `HOME`** under
+ `target/storage-scenarios/<run-id>/<driver>/`; the operator's
+ `OPENHUMAN_STORAGE_URL`, workspace, master key and backend credentials are
+ removed from the environment before anything starts.
+- Each core gets a **random master key**, passed through the environment only.
+ It, every token the run mints and every fixture secret are scrubbed from
+ `core.log` and `rpc.jsonl`.
+- **No background job runs** (`[scheduler_gate] mode = "off"` in every config
+ the run writes, including each signed-in user's): the legacy fixture holds
+ cron jobs that are due. Composio is off.
+- Users are signed in with an unsigned, locally minted session token; the core
+ never validates it against a backend.
+- **Every absence check has a positive control.** Before "not found" counts,
+ the same query (or the same grep) must find a record that is there: the
+ plaintext grep must find a stored message marker; bob must see his own
+ records before "bob does not see alice's" passes; the memory driver must
+ list a job written after the restart before "the old job is gone" passes.
+
+## Scenarios
+
+| id | drivers | what it checks |
+| --- | --- | --- |
+| A-write-read | all | per port: a cron job, a notification, a flow pre-authorisation (documents); a thread of 20 messages, including 64 KiB, unicode, JSON-ish and CR/LF bodies (stream); 4096 random bytes as a voice note (blob); a provider credential and a session token (secrets). Each read back byte-exact over RPC; the session token through `auth_get_session_token`, which returns it decrypted |
+| B-durability | sqlite, file | everything from A survives a restart; appends in flight when the core is SIGKILLed: every acked message present after reboot, none torn, duplicated or phantom; `PRAGMA integrity_check` = ok (sqlite) or every JSON/JSONL record parses (file) |
+| C-on-disk | sqlite, file | the database opened read-only: each record in the expected collection and scope (`local`); the blob's bytes; secrets are `secrets` documents with no plaintext; neither the provider token nor the session token appears anywhere under HOME |
+| D-isolation | sqlite | two users: separate `storage.db` files; neither sees the other's cron job, thread, voice note or credential over RPC; neither profile directory holds the other's marker; switching back restores the first user's records |
+| E-legacy-import | sqlite | a v1 profile seeded with every legacy store: imported (cron jobs with ids kept, flows, notification, approvals, costs, auth profile), one marker per domain, originals byte-identical, the cost ledger renamed, the plaintext legacy token not in the database, and a second boot imports nothing |
+| F-layout-migration | sqlite | E's profile moved to v2 by `auto_migrate_layout`: `storage_status` says 2, the marker, `storage.db` and `files/`, a non-empty `.tar.zst` backup, no collection shrank, the credential imported and the file archived; then a second v1 profile with **no master key**: the credential file stays byte-identical and its token reaches no database (#8076) |
+| G-clear-app-data | sqlite | `config_reset_local_data` removes the active user's folder and `active_user.toml`, and leaves the other user's folder byte-identical (#8248); with an explicit storage URL, what survives the reset is reported |
+| H-memory-restart | memory | A passes on the memory driver, and after a restart everything is gone, as designed |
+| I-concurrency | sqlite | 8 `flows_update` calls with the same `expected_version`: exactly one wins, seven get `version_conflict`, the winner's name is stored, and the flow's scope; 50 parallel appends to one thread: none lost or doubled |
+| J-signout-keeps-key | sqlite | session plus API key, then a session-only sign-out: `auth_get_state` is still authenticated by `api-key`, also after a restart, and the key is never on disk in plaintext (#8366) |
+
+**Not driven** (said so in every report): the KV port (no RPC writes it
+without a bound memory engine), agent and module principal scopes (they need
+an agent turn or a module storage grant, and no module declares one), SaaS
+tenants (they need the SaaS server mode), and artifact blobs (no RPC creates
+an artifact).
+
+## The fixtures are fictional
+
+Tokens are random `storscen-…` strings, users are `storscen-<name>`, and the
+legacy profile's tables carry the same placeholder rows as
+`tests/storage_desktop_upgrade_e2e.rs`. The tinyflows stores are the committed
+fixture in `crates/openhuman-core/src/storage/import/fixtures/tinyflows/`,
+copied, never written to.
+
+## Output
+
+```
+target/storage-scenarios/<run-id>/
+ report.md per driver: scenario table, findings, notes, what was not driven
+ findings.json [{id, engine (the driver), scenario, severity, expected, actual, evidence, basis}]
+ checks.json every check, passed or not
+ results.json measurements: per-scenario counts, the (scope, collection) census, scopes on disk, the crash and CAS numbers
+ <driver>/core.log, <driver>/rpc.jsonl the main core's log and every RPC (scrubbed)
+ <driver>/iso-<name>/core.log, rpc.jsonl, home/ each isolated core
+ <driver>/home/ the main core's HOME, left for a look
+```
+
+A finding's `basis` is **READ** when it was seen (an RPC answer, a row, a
+file) and **INFERRED** when it was judged.
+
+## See also
+
+- [Storage architecture](../../docs/gitbooks/en/developing/architecture/storage.md) and the [storage domain README](../../crates/openhuman-core/src/storage/README.md).
+- [`docs/storage-schema.md`](../../docs/storage-schema.md), the generated list of collections.
+- The upstream ports and their conformance suites: [tinystoragedrivers](https://github.com/tinyhumansai/tinystoragedrivers), `docs/specs/storage-ports.md`.
+- [`memory-scenarios`](../memory-scenarios/README.md), the same rig for memory.
diff --git a/scripts/storage-scenarios/isolated.mjs b/scripts/storage-scenarios/isolated.mjs
new file mode 100644
index 0000000000..b6b60dfe1e
--- /dev/null
+++ b/scripts/storage-scenarios/isolated.mjs
@@ -0,0 +1,322 @@
+// Scenarios that need a HOME of their own: D (two users), E (legacy import),
+// F (layout v2 migration, then the no-master-key profile of #8076), G (Clear
+// App Data, #8248) and J (sign-out keeps the API key, #8366). Each builds a
+// rig with `ctx.isolated(name, opts)`: a fresh HOME and core, same checks.
+
+import fs from "node:fs";
+import fsp from "node:fs/promises";
+import path from "node:path";
+import { randomBytes } from "node:crypto";
+import { pick, grepTree, findFiles, withDb, treeDigest } from "./lib.mjs";
+import { seedV1Profile, LEGACY_EXPECT, tinyflowsExpected } from "./legacy.mjs";
+
+const FAR_FUTURE = "2099-01-01T00:00:00Z";
+const json = (v) => JSON.stringify(v ?? null);
+const errText = (r) => (r.ok ? "" : json(r.error));
+const jobsOf = (r) => pick(r.value, "jobs", "result.jobs") ?? [];
+const exists = (p) => fs.existsSync(p);
+const userDb = (dbs, user) => dbs.filter((p) => p.includes(`${path.sep}users${path.sep}${user}${path.sep}`));
+
+/** One user's footprint on every RPC-reachable port. */
+async function writeUserData(rig, user, tag) {
+ const d = {
+ cronName: `storscen-${user}-${tag}`,
+ thread: `storscen-${user}-thread-${tag}`,
+ note: `storscen_${user}_${tag}`.replace(/[^A-Za-z0-9_-]/g, "_"),
+ provider: `storscen-${user}-provider`,
+ marker: `storscen-marker-${user}-${tag}`,
+ };
+ const add = await rig.tryRpc("openhuman.cron_add", { name: d.cronName, schedule: { kind: "at", at: FAR_FUTURE }, job_type: "agent", prompt: d.marker });
+ d.cronId = pick(add.value, "job.id", "result.job.id", "id");
+ await rig.tryRpc("openhuman.threads_upsert", { id: d.thread, title: d.marker, created_at: new Date().toISOString() });
+ await rig.tryRpc("openhuman.threads_message_append", { thread_id: d.thread, message: { id: `${d.thread}-m0`, content: d.marker, type: "text", extraMetadata: {}, sender: "user", createdAt: new Date().toISOString() } });
+ await rig.tryRpc("openhuman.channel_web_voice_note_store", { note_id: d.note, audio_base64: Buffer.from(d.marker).toString("base64") });
+ await rig.tryRpc("openhuman.auth_store_provider_credentials", { provider: d.provider, profile: "default", token: `storscen-token-${randomBytes(8).toString("hex")}` });
+ return d;
+}
+
+/** What `rig` sees of user data `d`: {cron, thread, note, provider} booleans. */
+async function sees(rig, d) {
+ const cron = jobsOf(await rig.tryRpc("openhuman.cron_list", {})).some((j) => j.id === d.cronId);
+ const msgs = await rig.tryRpc("openhuman.threads_messages_list", { thread_id: d.thread });
+ const thread = (pick(msgs.value, "messages", "result.messages") ?? []).some((m) => m.content === d.marker);
+ const nr = await rig.tryRpc("openhuman.channel_web_voice_note_read", { note_id: d.note });
+ const b64 = pick(nr.value, "audio_base64", "result.audio_base64");
+ const note = !!b64 && Buffer.from(b64, "base64").toString() === d.marker;
+ const pl = await rig.tryRpc("openhuman.auth_list_provider_credentials", {});
+ const provider = json(pl.value).includes(`"${d.provider}"`);
+ return { cron, thread, note, provider };
+}
+
+// ---------------------------------------------------------------------------
+// D. isolation
+// ---------------------------------------------------------------------------
+
+const isolation = {
+ id: "D-isolation",
+ title: "Two users: separate databases, no cross-reads",
+ drivers: ["sqlite"],
+ async run(ctx) {
+ const rig = await ctx.isolated("D");
+ await rig.start();
+ await rig.signIn("storscen-alice");
+ const alice = await writeUserData(rig, "storscen-alice", ctx.tag);
+ await rig.signIn("storscen-bob");
+ const bob = await writeUserData(rig, "storscen-bob", ctx.tag);
+
+ const own = await sees(rig, bob);
+ const other = await sees(rig, alice);
+ for (const port of ["cron", "thread", "note", "provider"]) {
+ ctx.check({
+ id: `D-${port}`,
+ ok: own[port] && !other[port],
+ expected: `bob sees his own ${port} record (control) and not alice's`,
+ actual: `own: ${own[port] ? "seen" : "NOT seen (control failed)"}; alice's: ${other[port] ? "SEEN" : "not seen"}`,
+ severity: other[port] ? "high" : "medium",
+ });
+ }
+
+ await rig.stop();
+ const dbs = await rig.dbFiles();
+ const [aDb] = userDb(dbs, "storscen-alice");
+ const [bDb] = userDb(dbs, "storscen-bob");
+ ctx.check({ id: "D-separate-files", ok: !!aDb && !!bDb && aDb !== bDb, expected: "users/storscen-alice/ and users/storscen-bob/ each have their own storage.db", actual: dbs.map((p) => path.relative(rig.oh, p)).join(", ") || "none" });
+ if (aDb && bDb) {
+ const aDir = path.dirname(aDb);
+ const bDir = path.dirname(bDb);
+ const bOwn = (await grepTree(bDir, bob.marker)).length;
+ const bForeign = (await grepTree(bDir, alice.marker)).length;
+ const aOwn = (await grepTree(aDir, alice.marker)).length;
+ const aForeign = (await grepTree(aDir, bob.marker)).length;
+ ctx.check({ id: "D-on-disk", ok: bOwn > 0 && aOwn > 0 && bForeign === 0 && aForeign === 0, expected: "each profile directory holds its own marker (control) and never the other's", actual: `alice dir: own ${aOwn}, bob's ${aForeign}; bob dir: own ${bOwn}, alice's ${bForeign}`, severity: "high" });
+ withDb(aDb, (db) => ctx.result("D-scopes-alice", db.scopes()));
+ withDb(bDb, (db) => ctx.result("D-scopes-bob", db.scopes()));
+ }
+
+ await rig.start();
+ await rig.signIn("storscen-alice");
+ const back = await sees(rig, alice);
+ ctx.check({ id: "D-switch-back", ok: back.cron && back.thread && back.note, expected: "signing alice back in shows her records again", actual: json(back) });
+ ctx.note("D-scope-coverage", "Agent and module principal scopes need an agent turn or a module storage grant (none declared); not driven. SaaS tenants need the SaaS server mode; not driven.");
+ await rig.stop();
+ },
+};
+
+// ---------------------------------------------------------------------------
+// E + F. legacy import, then layout v2
+// ---------------------------------------------------------------------------
+
+const legacyImport = {
+ id: "E-legacy-import",
+ title: "A v1 workspace is imported once, idempotently, originals untouched",
+ drivers: ["sqlite"],
+ async run(ctx) {
+ const rig = await ctx.isolated("E");
+ const user = "storscen-legacy";
+ const token = `storscen-legacy-secret-${randomBytes(12).toString("hex")}`;
+ rig.addSecret(token);
+ const { profile, workspace } = await seedV1Profile({ oh: rig.oh, userId: user, baseConfig: rig.baseConfig(), providerToken: token });
+ ctx.state.legacy = { rig, user, token, profile, workspace };
+ const before = await treeDigest(workspace);
+ const authBefore = await fsp.readFile(path.join(profile, "auth-profiles.json"));
+
+ await rig.start();
+ const exp = await tinyflowsExpected();
+ const cronIds = exp.cron.jobs.map((j) => j.id);
+ const jobs = jobsOf(await rig.tryRpc("openhuman.cron_list", {}));
+ ctx.check({ id: "E-cron", ok: cronIds.every((id) => jobs.some((j) => j.id === id)), expected: `cron_list has the ${cronIds.length} legacy jobs, ids kept`, actual: `${jobs.filter((j) => cronIds.includes(j.id)).length}/${cronIds.length}` });
+ const notifs = await rig.tryRpc("openhuman.notification_list", { limit: 100 });
+ ctx.check({ id: "E-notifications", ok: json(notifs.value).includes('"n-1"'), expected: "notification_list has legacy notification n-1", actual: notifs.ok ? json(notifs.value).slice(0, 200) : errText(notifs) });
+ const decisions = await rig.tryRpc("openhuman.approval_list_recent_decisions", { limit: 100 });
+ ctx.check({ id: "E-approvals", ok: json(decisions.value).includes("already-approved"), expected: "approval_list_recent_decisions has the legacy decided request", actual: decisions.ok ? json(decisions.value).slice(0, 200) : errText(decisions) });
+ if (rig.has("openhuman.flows_list")) {
+ const flows = json((await rig.tryRpc("openhuman.flows_list", {})).value);
+ ctx.check({ id: "E-flows", ok: flows.includes("flow-alpha") && flows.includes("flow-beta"), expected: "flows_list has flow-alpha and flow-beta", actual: flows.slice(0, 200) });
+ }
+ const usage = await rig.tryRpc("openhuman.cost_get_usage_log", { days: 30, limit: 100 });
+ const costHits = LEGACY_EXPECT.costs.filter((id) => json(usage.value).includes(id)).length;
+ ctx.check({ id: "E-costs", ok: costHits === LEGACY_EXPECT.costs.length, expected: `cost_get_usage_log lists the ${LEGACY_EXPECT.costs.length} legacy cost records`, actual: usage.ok ? `${costHits} listed` : errText(usage) });
+ const creds = await rig.tryRpc("openhuman.auth_list_provider_credentials", {});
+ ctx.check({ id: "E-auth-profile", ok: json(creds.value).includes(LEGACY_EXPECT.provider), expected: "the legacy auth profile is listed (adopted on first read)", actual: creds.ok ? json(creds.value).slice(0, 200) : errText(creds) });
+
+ await rig.stop();
+ const after = await treeDigest(workspace);
+ const changed = Object.keys({ ...before, ...after }).filter(
+ (k) => before[k] !== after[k] && !/(^|\/)storage(\/|$)/.test(k) && !k.endsWith("-shm") && !k.endsWith("-wal") && !k.startsWith("state/costs.jsonl"),
+ );
+ ctx.check({ id: "E-originals-untouched", ok: changed.length === 0, expected: "every legacy file is byte-identical after import (cost ledger aside)", actual: changed.length ? changed.slice(0, 8).join(", ") : "identical" , severity: "high" });
+ ctx.check({ id: "E-costs-renamed", ok: !exists(path.join(workspace, "state/costs.jsonl")) && exists(path.join(workspace, "state/costs.jsonl.migrated")), expected: "state/costs.jsonl renamed to costs.jsonl.migrated", actual: fs.readdirSync(path.join(workspace, "state")).join(", ") });
+ ctx.check({ id: "E-auth-file-untouched", ok: (await fsp.readFile(path.join(profile, "auth-profiles.json"))).equals(authBefore), expected: "auth-profiles.json left in place unchanged (adoption copies, never deletes)", actual: "compared bytes" });
+
+ const dbFile = path.join(workspace, "storage", "storage.db");
+ ctx.check({ id: "E-db-location", ok: exists(dbFile), expected: "the v1 default database is <workspace>/storage/storage.db", actual: (await rig.dbFiles()).map((p) => path.relative(rig.oh, p)).join(", ") });
+ if (!exists(dbFile)) return;
+ const first = withDb(dbFile, (db) => ({ census: db.census(), markers: db.docs("storage.import_state").map((d) => d.id) }));
+ ctx.result("E-census", first.census);
+ const missing = LEGACY_EXPECT.markerDomains.filter((d) => !first.markers.some((m) => m.includes(`${d}@`)));
+ ctx.check({ id: "E-markers", ok: missing.length === 0, expected: `an import marker per domain (${LEGACY_EXPECT.markerDomains.join(", ")})`, actual: `markers: ${first.markers.join(", ") || "none"}; missing ${missing.join(", ") || "none"}` });
+ const leaked = await grepTree(path.join(workspace, "storage"), token);
+ ctx.check({ id: "E-secret-encrypted", ok: leaked.length === 0, expected: "the legacy plaintext token is not in the database after adoption", actual: `${leaked.length} files`, severity: "high" });
+
+ // A second boot imports nothing new.
+ await rig.start();
+ await rig.stop();
+ const second = withDb(dbFile, (db) => ({ census: db.census(), markers: db.docs("storage.import_state").map((d) => d.id) }));
+ const delta = second.census.filter((r) => {
+ const was = first.census.find((x) => x.scope === r.scope && x.coll === r.coll);
+ return !was || was.n !== r.n;
+ }).filter((r) => !/journal|leases|run|admission|kv/.test(r.coll));
+ ctx.check({ id: "E-idempotent", ok: delta.length === 0 && json(second.markers.sort()) === json(first.markers.sort()), expected: "a second boot adds no imported record and no marker", actual: delta.length ? json(delta).slice(0, 300) : "unchanged" });
+ ctx.state.legacy.census = first.census;
+ },
+};
+
+const layoutMigration = {
+ id: "F-layout-migration",
+ title: "Layout v2 opt-in migration: archive, data intact; no master key leaves credentials in place (#8076)",
+ drivers: ["sqlite"],
+ async run(ctx) {
+ const L = ctx.state.legacy;
+ if (!L) {
+ ctx.note("F-skipped", "E did not run, so there is no v1 profile to migrate.");
+ return;
+ }
+ const { rig, user, profile } = L;
+ await rig.editConfig(path.join(profile, "config.toml"), "storage", { auto_migrate_layout: true });
+ await rig.start();
+ const status = await rig.tryRpc("openhuman.storage_status", {});
+ ctx.check({ id: "F-status", ok: json(status.value).includes('"layout_version":2') || pick(status.value, "layout_version", "result.layout_version") === 2, expected: "storage_status reports layout_version 2", actual: status.ok ? json(status.value).slice(0, 200) : errText(status) });
+ const jobs = jobsOf(await rig.tryRpc("openhuman.cron_list", {}));
+ const cronIds = (await tinyflowsExpected()).cron.jobs.map((j) => j.id);
+ ctx.check({ id: "F-data-intact-rpc", ok: cronIds.every((id) => jobs.some((j) => j.id === id)), expected: "the imported cron jobs are still listed after the move", actual: `${jobs.filter((j) => cronIds.includes(j.id)).length}/${cronIds.length}` });
+ const creds = json((await rig.tryRpc("openhuman.auth_list_provider_credentials", {})).value);
+ ctx.check({ id: "F-credential-intact", ok: creds.includes(LEGACY_EXPECT.provider), expected: "the legacy credential is still listed after the move", actual: creds.slice(0, 200) });
+ await rig.stop();
+
+ const marker = path.join(profile, ".layout");
+ ctx.check({ id: "F-marker", ok: exists(marker) && fs.readFileSync(marker, "utf8").trim() === "2", expected: "users/<id>/.layout reads 2", actual: exists(marker) ? fs.readFileSync(marker, "utf8").trim() : "absent" });
+ ctx.check({ id: "F-files", ok: exists(path.join(profile, "storage.db")) && exists(path.join(profile, "files")), expected: "users/<id>/storage.db and files/ exist", actual: fs.readdirSync(profile).join(", ") });
+ const archives = (await findAll(rig.oh, (n) => n.endsWith(".tar.zst")));
+ ctx.check({ id: "F-backup", ok: archives.length > 0 && archives.every((p) => fs.statSync(p).size > 0), expected: "a non-empty backups/<user>-<ts>.tar.zst exists", actual: archives.map((p) => `${path.relative(rig.oh, p)} (${fs.statSync(p).size} B)`).join(", ") || "none" });
+ if (exists(path.join(profile, "storage.db")) && L.census) {
+ const now = withDb(path.join(profile, "storage.db"), (db) => db.census());
+ const lost = L.census.filter((r) => {
+ const n = now.find((x) => x.scope === r.scope && x.coll === r.coll)?.n ?? 0;
+ return n < r.n;
+ });
+ ctx.check({ id: "F-counts", ok: lost.length === 0, expected: "every (scope, collection) has at least as many records as before the move", actual: lost.length ? json(lost).slice(0, 300) : "no collection shrank", severity: "high" });
+ }
+ ctx.check({ id: "F-credential-file-archived", ok: !exists(path.join(profile, "auth-profiles.json")), expected: "with a master key, auth-profiles.json is imported, archived and removed", actual: exists(path.join(profile, "auth-profiles.json")) ? "still in place" : "removed" });
+
+ // F2 (#8076): the same move with no resolvable master key.
+ const nokey = await ctx.isolated("F2", { masterKey: false });
+ const token = `storscen-nokey-secret-${randomBytes(12).toString("hex")}`;
+ nokey.addSecret(token);
+ const seeded = await seedV1Profile({ oh: nokey.oh, userId: user, baseConfig: nokey.baseConfig(), providerToken: token });
+ await nokey.editConfig(path.join(seeded.profile, "config.toml"), "storage", { auto_migrate_layout: true });
+ const authBefore = await fsp.readFile(path.join(seeded.profile, "auth-profiles.json"));
+ const boot = await nokey.start().then(() => "booted", (e) => `boot failed: ${e.message}`);
+ await nokey.stop();
+ const authFile = path.join(seeded.profile, "auth-profiles.json");
+ const kept = exists(authFile) && (await fsp.readFile(authFile)).equals(authBefore);
+ ctx.check({ id: "F2-credentials-left-in-place", ok: kept, expected: "with no master key the credential file is not imported and stays byte-identical (#8076)", actual: `${boot}; auth-profiles.json ${exists(authFile) ? (kept ? "unchanged" : "CHANGED") : "GONE"}`, severity: "high" });
+ const leak = await grepTree(path.join(nokey.oh, "users", user), token);
+ const leakDb = leak.filter((p) => p.endsWith(".db") || p.endsWith("-wal"));
+ ctx.check({ id: "F2-no-plaintext-in-db", ok: leakDb.length === 0, expected: "the plaintext token is in no database file", actual: leakDb.map((p) => path.relative(nokey.oh, p)).join(", ") || "none", severity: "high" });
+ const layout = path.join(seeded.profile, ".layout");
+ ctx.note("F2-layout", `no-master-key profile after boot: .layout ${exists(layout) ? fs.readFileSync(layout, "utf8").trim() : "absent"}; entries: ${fs.readdirSync(seeded.profile).join(", ")}`);
+ },
+};
+
+async function findAll(root, pred) {
+ const out = [];
+ async function walk(dir) {
+ for (const e of await fsp.readdir(dir, { withFileTypes: true }).catch(() => [])) {
+ const p = path.join(dir, e.name);
+ if (e.isDirectory()) await walk(p);
+ else if (pred(e.name)) out.push(p);
+ }
+ }
+ await walk(root);
+ return out;
+}
+
+// ---------------------------------------------------------------------------
+// G. Clear App Data (#8248)
+// ---------------------------------------------------------------------------
+
+const clearAppData = {
+ id: "G-clear-app-data",
+ title: "Clear App Data removes only the current user's folder",
+ drivers: ["sqlite"],
+ async run(ctx) {
+ const rig = await ctx.isolated("G");
+ await rig.start();
+ await rig.signIn("storscen-g2");
+ await writeUserData(rig, "storscen-g2", ctx.tag);
+ await rig.signIn("storscen-g1");
+ await writeUserData(rig, "storscen-g1", ctx.tag);
+ await rig.stop();
+ const g1 = path.join(rig.oh, "users", "storscen-g1");
+ const g2 = path.join(rig.oh, "users", "storscen-g2");
+ const g2Before = await treeDigest(g2);
+ ctx.check({ id: "G-control", ok: exists(g1) && Object.keys(g2Before).length > 0, expected: "both user folders exist before the reset (control)", actual: `g1 ${exists(g1)}, g2 ${Object.keys(g2Before).length} files` });
+ await rig.start();
+ const reset = await rig.tryRpc("openhuman.config_reset_local_data", {});
+ ctx.check({ id: "G-reset", ok: reset.ok, expected: "config_reset_local_data succeeds", actual: reset.ok ? json(reset.value).slice(0, 200) : errText(reset) });
+ await rig.stop();
+ ctx.check({ id: "G-current-gone", ok: !exists(g1), expected: "users/storscen-g1 (the active user) is removed", actual: exists(g1) ? `still there: ${fs.readdirSync(g1).join(", ")}` : "removed", severity: "high" });
+ const g2After = await treeDigest(g2, { skip: (r) => r.endsWith("-shm") });
+ const g2BeforeNoShm = Object.fromEntries(Object.entries(g2Before).filter(([k]) => !k.endsWith("-shm")));
+ ctx.check({ id: "G-other-untouched", ok: json(g2After) === json(g2BeforeNoShm), expected: "users/storscen-g2 is byte-identical", actual: json(g2After) === json(g2BeforeNoShm) ? "identical" : "CHANGED", severity: "high" });
+ ctx.check({ id: "G-active-user-cleared", ok: !exists(path.join(rig.oh, "active_user.toml")), expected: "active_user.toml removed", actual: exists(path.join(rig.oh, "active_user.toml")) ? "still there" : "removed" });
+
+ // G2: an explicit backend is not a folder; what survives a reset is reported.
+ const shared = await ctx.isolated("G2", { url: "sqlite:<home>/shared-storage" });
+ await shared.start();
+ await shared.signIn("storscen-g1");
+ const d = await writeUserData(shared, "storscen-g1", ctx.tag);
+ const control = await sees(shared, d);
+ await shared.tryRpc("openhuman.config_reset_local_data", {});
+ await shared.restart();
+ await shared.signIn("storscen-g1");
+ const after = await sees(shared, d);
+ ctx.note("G2-explicit-backend", `explicit OPENHUMAN_STORAGE_URL: before reset ${json(control)}; after reset and sign-in again ${json(after)}. The reset removes the user folder only; records on an explicit backend are outside it.`);
+ await shared.stop();
+ },
+};
+
+// ---------------------------------------------------------------------------
+// J. #8366: a session sign-out keeps the API key
+// ---------------------------------------------------------------------------
+
+const signOutKeepsKey = {
+ id: "J-signout-keeps-key",
+ title: "Signing out of the session keeps the API key on a storage backend (#8366)",
+ drivers: ["sqlite"],
+ async run(ctx) {
+ const rig = await ctx.isolated("J");
+ await rig.start();
+ await rig.signIn("storscen-j1");
+ const key = `storscen-apikey-${randomBytes(16).toString("hex")}`;
+ rig.addSecret(key);
+ const set = await rig.tryRpc("openhuman.auth_set_credential", { token: key, kind: "api-key" });
+ ctx.check({ id: "J-key-set", ok: set.ok, expected: "auth_set_credential stores the API key", actual: set.ok ? "ok" : errText(set) });
+ const clear = await rig.tryRpc("openhuman.auth_clear_credential", { kind: "session" });
+ ctx.check({ id: "J-signout", ok: clear.ok, expected: "auth_clear_credential {kind: session} succeeds", actual: clear.ok ? json(clear.value).slice(0, 200) : errText(clear) });
+ const credOf = async () => {
+ const s = await rig.tryRpc("openhuman.auth_get_state", {});
+ return { auth: pick(s.value, "is_authenticated", "isAuthenticated", "state.is_authenticated", "state.isAuthenticated", "result.is_authenticated", "result.isAuthenticated"), cred: pick(s.value, "credential", "state.credential", "result.credential"), raw: s.ok ? json(s.value).slice(0, 200) : errText(s) };
+ };
+ const now = await credOf();
+ ctx.check({ id: "J-key-kept", ok: now.auth === true && now.cred === "api-key", expected: "after the session sign-out auth_get_state is authenticated by credential \"api-key\"", actual: now.raw, severity: "high" });
+ await rig.restart();
+ const later = await credOf();
+ ctx.check({ id: "J-key-kept-after-restart", ok: later.auth === true && later.cred === "api-key", expected: "the API key is still in effect after a restart", actual: later.raw, severity: "high" });
+ await rig.stop();
+ const hits = await grepTree(rig.home, key);
+ ctx.check({ id: "J-key-not-plaintext", ok: hits.length === 0, expected: "the API key appears nowhere under HOME in plaintext", actual: hits.map((p) => path.relative(rig.home, p)).join(", ") || "none", severity: "high" });
+ },
+};
+
+export const ISOLATED = [isolation, legacyImport, layoutMigration, clearAppData, signOutKeepsKey];
diff --git a/scripts/storage-scenarios/legacy.mjs b/scripts/storage-scenarios/legacy.mjs
new file mode 100644
index 0000000000..a60e463187
--- /dev/null
+++ b/scripts/storage-scenarios/legacy.mjs
@@ -0,0 +1,231 @@
+// A layout-v1 profile as the pre-storage releases left it: per-domain SQLite
+// files, a cost ledger, a classic session transcript and an auth-profiles
+// file, all fictional. The table shapes are the ones the importers read (the
+// same DDL as tests/storage_desktop_upgrade_e2e.rs); the tinyflows stores are
+// the committed fixture written by the retired tinyflows-sqlite writers.
+
+import fsp from "node:fs/promises";
+import path from "node:path";
+import { fileURLToPath } from "node:url";
+import { sqliteFile } from "./lib.mjs";
+
+const HERE = path.dirname(fileURLToPath(import.meta.url));
+const REPO = path.resolve(HERE, "..", "..");
+export const TINYFLOWS_FIXTURE = path.join(
+ REPO,
+ "crates/openhuman-core/src/storage/import/fixtures/tinyflows",
+);
+
+/** What the seeded profile holds, so checks read against the same numbers. */
+export const LEGACY_EXPECT = {
+ approvals: ["still-pending", "already-approved"],
+ flowTrust: "flow-1",
+ devices: ["chan-a", "chan-b"],
+ notifications: ["n-1"],
+ taskSources: ["src-1"],
+ costs: ["storscen-cost-1", "storscen-cost-2", "storscen-cost-3"],
+ provider: "storscen-legacy-provider",
+ // The import markers (`storage.import_state`, `storage_import/<domain>@<digest>`).
+ markerDomains: ["approvals", "devices", "notifications", "task_sources", "cron", "flows", "sessions"],
+};
+
+async function copyTree(from, to) {
+ await fsp.mkdir(to, { recursive: true });
+ for (const e of await fsp.readdir(from, { withFileTypes: true })) {
+ const s = path.join(from, e.name);
+ const d = path.join(to, e.name);
+ if (e.isDirectory()) await copyTree(s, d);
+ else await fsp.copyFile(s, d);
+ }
+}
+
+async function write(file, text) {
+ await fsp.mkdir(path.dirname(file), { recursive: true });
+ await fsp.writeFile(file, text);
+}
+
+/** The cron jobs and flows of the committed tinyflows fixture. */
+export async function tinyflowsExpected() {
+ return JSON.parse(
+ await fsp.readFile(path.join(TINYFLOWS_FIXTURE, "expected.json"), "utf8"),
+ );
+}
+
+/**
+ * Seed `users/<userId>/` under `oh` (`$HOME/.openhuman`) as a v1 profile and
+ * make it the active user. `providerToken` goes into the legacy
+ * auth-profiles.json in plaintext, as the old file kept it.
+ */
+export async function seedV1Profile({ oh, userId, baseConfig, providerToken }) {
+ const profile = path.join(oh, "users", userId);
+ const ws = path.join(profile, "workspace");
+ await write(path.join(oh, "active_user.toml"), `user_id = "${userId}"\n`);
+ await write(path.join(profile, "config.toml"), baseConfig);
+
+ await copyTree(path.join(TINYFLOWS_FIXTURE, "workspace"), ws);
+
+ sqliteFile(
+ path.join(ws, "approval/approval.db"),
+ `CREATE TABLE pending_approvals (
+ request_id TEXT PRIMARY KEY, tool_name TEXT NOT NULL,
+ action_summary TEXT NOT NULL, args_redacted TEXT NOT NULL,
+ session_id TEXT NOT NULL, created_at TEXT NOT NULL, expires_at TEXT,
+ decided_at TEXT, decision TEXT, executed_at TEXT, execution_outcome TEXT,
+ execution_error TEXT, source_context TEXT, tool_call_id TEXT, agent_id TEXT);
+ CREATE TABLE flow_tool_trust (
+ flow_id TEXT NOT NULL, tool_name TEXT NOT NULL, created_at TEXT NOT NULL,
+ PRIMARY KEY (flow_id, tool_name));
+ INSERT INTO pending_approvals
+ (request_id, tool_name, action_summary, args_redacted, session_id, created_at, expires_at)
+ VALUES ('still-pending', 'shell', 'run ls', '{"cmd":"ls"}', 'sess',
+ '2026-01-01T00:00:00+00:00', '2099-01-01T00:00:00+00:00');
+ INSERT INTO pending_approvals
+ (request_id, tool_name, action_summary, args_redacted, session_id, created_at,
+ expires_at, decided_at, decision)
+ VALUES ('already-approved', 'shell', 'run pwd', '{}', 'sess',
+ '2026-01-01T00:00:00+00:00', '2099-01-01T00:00:00+00:00',
+ '2026-01-01T00:01:00+00:00', 'approve_once');
+ INSERT INTO flow_tool_trust (flow_id, tool_name, created_at)
+ VALUES ('flow-1', 'shell', '2026-01-01T00:00:00+00:00');`,
+ );
+ sqliteFile(
+ path.join(ws, "devices/devices.db"),
+ `CREATE TABLE paired_devices (
+ channel_id TEXT PRIMARY KEY, label TEXT NOT NULL, device_pubkey TEXT NOT NULL,
+ core_session_token_hash TEXT NOT NULL, shared_secret_encrypted BLOB,
+ created_at TEXT NOT NULL, last_seen_at TEXT, revoked INTEGER NOT NULL DEFAULT 0);
+ INSERT INTO paired_devices
+ (channel_id, label, device_pubkey, core_session_token_hash, created_at, revoked)
+ VALUES ('chan-a', 'Phone', 'pk', 'hash', '2026-01-01T00:00:00+00:00', 0),
+ ('chan-b', 'Old laptop', 'pk', 'hash', '2026-01-01T00:00:00+00:00', 1);`,
+ );
+ sqliteFile(
+ path.join(ws, "notifications/notifications.db"),
+ `CREATE TABLE integration_notifications (
+ id TEXT PRIMARY KEY, provider TEXT NOT NULL, account_id TEXT, title TEXT NOT NULL,
+ body TEXT NOT NULL, raw_payload TEXT NOT NULL, importance_score REAL,
+ triage_action TEXT, triage_reason TEXT, status TEXT NOT NULL DEFAULT 'unread',
+ received_at TEXT NOT NULL, scored_at TEXT);
+ CREATE TABLE notification_settings (
+ provider TEXT PRIMARY KEY, enabled INTEGER NOT NULL DEFAULT 1,
+ importance_threshold REAL NOT NULL DEFAULT 0.0,
+ route_to_orchestrator INTEGER NOT NULL DEFAULT 1);
+ CREATE TABLE core_notifications (
+ id TEXT PRIMARY KEY, payload TEXT NOT NULL, timestamp_ms INTEGER NOT NULL,
+ read INTEGER NOT NULL DEFAULT 0, created_at TEXT NOT NULL);
+ INSERT INTO integration_notifications
+ (id, provider, account_id, title, body, raw_payload, received_at)
+ VALUES ('n-1', 'gmail', 'acct', 'Hello', 'World', '{}', '2026-01-01T00:00:00+00:00');
+ INSERT INTO notification_settings VALUES ('gmail', 1, 0.5, 1);
+ INSERT INTO core_notifications VALUES
+ ('core-1', '{"id":"core-1","title":"cron done"}', 1767225600000, 0,
+ '2026-01-01T00:00:00+00:00');`,
+ );
+ sqliteFile(
+ path.join(ws, "task_sources/sources.db"),
+ `CREATE TABLE task_sources (
+ id TEXT PRIMARY KEY, provider TEXT NOT NULL, connection_id TEXT, name TEXT,
+ enabled INTEGER NOT NULL DEFAULT 1, filter TEXT NOT NULL, interval_secs INTEGER NOT NULL,
+ target TEXT NOT NULL, max_tasks_per_fetch INTEGER NOT NULL, created_at TEXT NOT NULL,
+ last_fetch_at TEXT, last_status TEXT);
+ CREATE TABLE ingested_tasks (
+ source_id TEXT NOT NULL, external_id TEXT NOT NULL, content_hash TEXT NOT NULL,
+ title TEXT, payload TEXT, ingested_at TEXT NOT NULL, card_id TEXT,
+ PRIMARY KEY (source_id, external_id));
+ INSERT INTO task_sources
+ (id, provider, filter, interval_secs, target, max_tasks_per_fetch, created_at)
+ VALUES ('src-1', 'github', '{"provider":"github"}', 600, '"todo_only"', 5,
+ '2026-01-01T00:00:00+00:00');
+ PRAGMA user_version = 0;
+ INSERT INTO ingested_tasks (source_id, external_id, content_hash, ingested_at)
+ VALUES ('src-1', 'issue-9', 'abc', '2026-01-01T00:00:00+00:00');`,
+ );
+ sqliteFile(
+ path.join(ws, "session_db/sessions.db"),
+ `CREATE TABLE sessions (
+ id TEXT PRIMARY KEY, agent_definition_id TEXT NOT NULL,
+ agent_definition_name TEXT NOT NULL, session_key TEXT NOT NULL,
+ parent_session_id TEXT, thread_id TEXT, source_channel TEXT,
+ status TEXT NOT NULL DEFAULT 'running', model TEXT,
+ turn_count INTEGER NOT NULL DEFAULT 0, input_tokens INTEGER NOT NULL DEFAULT 0,
+ output_tokens INTEGER NOT NULL DEFAULT 0, cached_input_tokens INTEGER NOT NULL DEFAULT 0,
+ cost_usd REAL NOT NULL DEFAULT 0.0, transcript_path TEXT,
+ started_at TEXT NOT NULL, ended_at TEXT);
+ INSERT INTO sessions (id, agent_definition_id, agent_definition_name, session_key, started_at)
+ VALUES ('sess-1', 'orchestrator', 'Orchestrator', 'thread-1', '2026-01-01T00:00:00Z');`,
+ );
+ // A classic transcript: the `_meta` line, then one message. An unknown stem
+ // falls back to the default agent.
+ await write(
+ path.join(ws, "session_raw/storscen-legacy-thread.jsonl"),
+ [
+ JSON.stringify({
+ _meta: {
+ version: 1,
+ agent: "orchestrator",
+ dispatcher: "native",
+ created: "2026-01-01T00:00:00Z",
+ updated: "2026-01-01T00:00:00Z",
+ turn_count: 1,
+ input_tokens: 0,
+ output_tokens: 0,
+ cached_input_tokens: 0,
+ charged_amount_usd: 0,
+ agent_id: "orchestrator",
+ thread_id: "storscen-legacy-thread",
+ },
+ }),
+ JSON.stringify({ role: "user", content: "hello from the old layout" }),
+ "",
+ ].join("\n"),
+ );
+
+ // The cost ledger, stamped recently so a 30-day usage log lists it.
+ const now = Date.now();
+ await write(
+ path.join(ws, "state/costs.jsonl"),
+ LEGACY_EXPECT.costs
+ .map((id, i) =>
+ JSON.stringify({
+ id,
+ session_id: "sess-1",
+ usage: {
+ model: "storscen-fixture-model",
+ input_tokens: 10 + i,
+ output_tokens: 5,
+ total_tokens: 15 + i,
+ cost_usd: 0.001 * (i + 1),
+ timestamp: new Date(now - (i + 1) * 3_600_000).toISOString(),
+ },
+ }),
+ )
+ .join("\n") + "\n",
+ );
+
+ // The credential file sits beside config.toml, token in plaintext as the
+ // old file could hold it (adopted, then encrypted, on first read).
+ const stamp = "2026-01-01T00:00:00Z";
+ await write(
+ path.join(profile, "auth-profiles.json"),
+ JSON.stringify(
+ {
+ schema_version: 1,
+ updated_at: stamp,
+ active_profiles: { [LEGACY_EXPECT.provider]: `${LEGACY_EXPECT.provider}:default` },
+ profiles: {
+ [`${LEGACY_EXPECT.provider}:default`]: {
+ provider: LEGACY_EXPECT.provider,
+ profile_name: "default",
+ kind: "token",
+ token: providerToken,
+ created_at: stamp,
+ updated_at: stamp,
+ },
+ },
+ },
+ null,
+ 2,
+ ),
+ );
+ return { profile, workspace: ws };
+}
diff --git a/scripts/storage-scenarios/lib.mjs b/scripts/storage-scenarios/lib.mjs
new file mode 100644
index 0000000000..49cea83a2c
--- /dev/null
+++ b/scripts/storage-scenarios/lib.mjs
@@ -0,0 +1,232 @@
+// Storage-scenario plumbing. The core rig (a headless `openhuman-core serve`
+// with its own HOME, JSON-RPC, scrubbed logs, findings) is memory-scenarios';
+// this file adds what storage checks need: a hard kill, a read-only view of
+// the SQLite database, tree hashes and a plaintext grep.
+
+import fs from "node:fs";
+import fsp from "node:fs/promises";
+import path from "node:path";
+import { createHash, randomBytes } from "node:crypto";
+import { DatabaseSync } from "node:sqlite";
+import { Core, sleep } from "../memory-scenarios/lib.mjs";
+
+export {
+ Core,
+ JsonlLog,
+ Findings,
+ RpcError,
+ waitFor,
+ withRetries,
+ pick,
+ scrub,
+ sleep,
+ randomUUID,
+} from "../memory-scenarios/lib.mjs";
+
+/** SIGKILL the core (no shutdown path runs) and wait for the exit. */
+export async function killHard(core) {
+ if (!core?.proc || core.exited) return;
+ core.proc.kill("SIGKILL");
+ const deadline = Date.now() + 10_000;
+ while (!core.exited && Date.now() < deadline) await sleep(50);
+}
+
+/**
+ * An unsigned session JWT for `userId`. The core never validates a session
+ * against the backend (`auth_set_credential`); it only reads `sub` and `exp`.
+ * `nonce` makes each token unique, so it can be grepped for on disk.
+ */
+export function mintSessionToken(userId, nonce = randomBytes(12).toString("hex")) {
+ const b64 = (o) => Buffer.from(JSON.stringify(o)).toString("base64url");
+ const now = Math.floor(Date.now() / 1000);
+ return [
+ b64({ alg: "none", typ: "JWT" }),
+ b64({
+ sub: userId,
+ iat: now,
+ exp: now + 86_400,
+ email: `${userId}@storscen.invalid`,
+ nonce,
+ }),
+ "storscen",
+ ].join(".");
+}
+
+export const sha256 = (bytes) => createHash("sha256").update(bytes).digest("hex");
+
+/** Every file under `root` (relative path -> sha256), for before/after comparisons. */
+export async function treeDigest(root, { skip = () => false } = {}) {
+ const out = {};
+ async function walk(dir) {
+ let entries = [];
+ try {
+ entries = await fsp.readdir(dir, { withFileTypes: true });
+ } catch {
+ return;
+ }
+ for (const e of entries) {
+ const p = path.join(dir, e.name);
+ const rel = path.relative(root, p);
+ if (skip(rel)) continue;
+ if (e.isDirectory()) await walk(p);
+ else if (e.isFile()) out[rel] = sha256(await fsp.readFile(p));
+ }
+ }
+ await walk(root);
+ return out;
+}
+
+/** Files under `root` whose bytes contain `needle` (binary-safe). */
+export async function grepTree(root, needle, { skip = () => false } = {}) {
+ const hits = [];
+ const n = Buffer.from(needle);
+ async function walk(dir) {
+ let entries = [];
+ try {
+ entries = await fsp.readdir(dir, { withFileTypes: true });
+ } catch {
+ return;
+ }
+ for (const e of entries) {
+ const p = path.join(dir, e.name);
+ if (skip(path.relative(root, p))) continue;
+ if (e.isDirectory()) await walk(p);
+ else if (e.isFile() && (await fsp.readFile(p)).includes(n)) hits.push(p);
+ }
+ }
+ await walk(root);
+ return hits;
+}
+
+/** Every file named `name` under `root`. */
+export async function findFiles(root, name) {
+ const found = [];
+ async function walk(dir) {
+ let entries = [];
+ try {
+ entries = await fsp.readdir(dir, { withFileTypes: true });
+ } catch {
+ return;
+ }
+ for (const e of entries) {
+ const p = path.join(dir, e.name);
+ if (e.isDirectory()) await walk(p);
+ else if (e.name === name) found.push(p);
+ }
+ }
+ await walk(root);
+ return found;
+}
+
+/**
+ * A read-only view of a tinystoragedrivers SQLite database. The driver's table
+ * names are configurable, so tables are found by their columns:
+ * documents (scope, coll, id, body), streams (scope, name, seq, body), blobs
+ * (scope, key, bytes) and KV (scope, key, value, version).
+ *
+ * Opening read-only never creates the file; the core must be stopped first
+ * when the result has to be final (WAL frames are read either way).
+ */
+export class StorageDb {
+ constructor(file) {
+ if (!fs.existsSync(file)) throw new Error(`no database at ${file}`);
+ this.file = file;
+ this.db = new DatabaseSync(file, { readOnly: true });
+ this.tables = {};
+ const names = this.db
+ .prepare("SELECT name FROM sqlite_master WHERE type = 'table'")
+ .all()
+ .map((r) => r.name);
+ for (const t of names) {
+ const cols = new Set(
+ this.db.prepare(`PRAGMA table_info("${t}")`).all().map((c) => c.name),
+ );
+ const has = (...c) => c.every((x) => cols.has(x));
+ if (has("scope", "coll", "id", "body", "version")) this.tables.docs = t;
+ else if (has("scope", "name", "seq", "body")) this.tables.streams = t;
+ else if (has("scope", "key", "bytes", "sha256")) this.tables.blobs = t;
+ else if (has("scope", "key", "value", "version")) this.tables.kv = t;
+ }
+ }
+ all(sql, ...args) {
+ return this.db.prepare(sql).all(...args);
+ }
+ integrity() {
+ return this.all("PRAGMA integrity_check").map((r) => Object.values(r)[0]);
+ }
+ /** Live documents of `coll`, optionally in one scope. */
+ docs(coll, scope) {
+ if (!this.tables.docs) return [];
+ const q = `SELECT scope, coll, id, version, body FROM "${this.tables.docs}" WHERE coll = ? AND deleted = 0`;
+ return scope === undefined
+ ? this.all(q, coll)
+ : this.all(`${q} AND scope = ?`, coll, scope);
+ }
+ /** Rows of the documents table whose body contains `text`. */
+ docsContaining(text) {
+ if (!this.tables.docs) return [];
+ return this.all(
+ `SELECT scope, coll, id FROM "${this.tables.docs}" WHERE deleted = 0 AND instr(body, ?) > 0`,
+ text,
+ );
+ }
+ /** Count of documents per (scope, coll). */
+ census() {
+ if (!this.tables.docs) return [];
+ return this.all(
+ `SELECT scope, coll, count(*) AS n FROM "${this.tables.docs}" WHERE deleted = 0 GROUP BY scope, coll ORDER BY scope, coll`,
+ );
+ }
+ streamRows(nameLike) {
+ if (!this.tables.streams) return [];
+ return this.all(
+ `SELECT scope, name, seq, body FROM "${this.tables.streams}" WHERE name LIKE ? ORDER BY scope, name, seq`,
+ nameLike,
+ );
+ }
+ blob(keyLike) {
+ if (!this.tables.blobs) return [];
+ return this.all(
+ `SELECT scope, key, size, bytes FROM "${this.tables.blobs}" WHERE key LIKE ?`,
+ keyLike,
+ );
+ }
+ kvRows() {
+ if (!this.tables.kv) return [];
+ return this.all(
+ `SELECT scope, key FROM "${this.tables.kv}" WHERE deleted = 0`,
+ );
+ }
+ /** Every distinct scope that holds any record. */
+ scopes() {
+ const out = new Set();
+ for (const t of Object.values(this.tables))
+ for (const r of this.all(`SELECT DISTINCT scope FROM "${t}"`))
+ out.add(r.scope);
+ return [...out].sort();
+ }
+ close() {
+ this.db.close();
+ }
+}
+
+/** Open, run `fn`, close. */
+export function withDb(file, fn) {
+ const db = new StorageDb(file);
+ try {
+ return fn(db);
+ } finally {
+ db.close();
+ }
+}
+
+/** Write a SQLite file from DDL + rows (used to build legacy fixtures). */
+export function sqliteFile(file, sql) {
+ fs.mkdirSync(path.dirname(file), { recursive: true });
+ const db = new DatabaseSync(file);
+ try {
+ db.exec(sql);
+ } finally {
+ db.close();
+ }
+}
diff --git a/scripts/storage-scenarios/run.mjs b/scripts/storage-scenarios/run.mjs
new file mode 100644
index 0000000000..c0ee5a4f28
--- /dev/null
+++ b/scripts/storage-scenarios/run.mjs
@@ -0,0 +1,348 @@
+#!/usr/bin/env node
+// Storage scenarios: how tinystoragedrivers works inside OpenHuman, driven
+// through a real headless core over JSON-RPC, and checked on disk. Every
+// failed check is a finding for a person; nothing is fixed during a run. See
+// README.md.
+
+import fs from "node:fs";
+import fsp from "node:fs/promises";
+import path from "node:path";
+import { randomBytes } from "node:crypto";
+import { fileURLToPath } from "node:url";
+import {
+ Core,
+ JsonlLog,
+ Findings,
+ scrub,
+ killHard,
+ mintSessionToken,
+ findFiles,
+} from "./lib.mjs";
+import { SCENARIOS } from "./scenarios.mjs";
+import { ISOLATED } from "./isolated.mjs";
+
+const HERE = path.dirname(fileURLToPath(import.meta.url));
+const REPO = path.resolve(HERE, "..", "..");
+const DRIVERS = ["sqlite", "file", "memory"];
+
+// The run sets its own storage URL, workspace and master key per core; the
+// operator's must never leak into one.
+for (const k of [
+ "OPENHUMAN_STORAGE_URL",
+ "OPENHUMAN_WORKSPACE",
+ "OPENHUMAN_KEYRING_MASTER_KEY",
+ "OPENHUMAN_KEYRING_MASTER_KEY_FILE",
+ "OPENHUMAN_KEYRING_BACKEND",
+ "OPENHUMAN_BACKEND_SESSION_TOKEN",
+ "OPENHUMAN_BACKEND_API_KEY",
+])
+ delete process.env[k];
+
+function parseArgs(argv) {
+ const opts = {
+ only: [],
+ drivers: DRIVERS,
+ gradeOnly: "",
+ coreBin: path.join(REPO, "target", "debug", "openhuman-core"),
+ runRoot: path.join(REPO, "target", "storage-scenarios"),
+ };
+ for (let i = 0; i < argv.length; i += 1) {
+ const a = argv[i];
+ const next = () => argv[++i];
+ if (a === "--only") opts.only.push(...next().split(",").map((s) => s.trim().toUpperCase()));
+ else if (a === "--driver") opts.drivers = next().split(",");
+ else if (a === "--grade-only") opts.gradeOnly = next();
+ else if (a === "--core-bin") opts.coreBin = next();
+ else throw new Error(`unknown argument ${a}`);
+ }
+ for (const d of opts.drivers)
+ if (!DRIVERS.includes(d)) throw new Error(`unknown driver ${d} (${DRIVERS.join(", ")})`);
+ return opts;
+}
+
+const BASE_CONFIG = [
+ "schema_version = 13",
+ "onboarding_completed = true",
+ "chat_onboarding_completed = true",
+ "",
+ "[observability]",
+ "analytics_enabled = false",
+ "share_usage_data = false",
+ "",
+ // No background job runs on its own: imported legacy cron jobs are due.
+ "[scheduler_gate]",
+ 'mode = "off"',
+ "",
+ "[cron]",
+ "enabled = true",
+ "",
+ "[composio]",
+ 'mode = "disabled"',
+ "",
+].join("\n");
+
+/** Set `key = value` lines in a TOML section of `file` (a small, line-based edit). */
+async function editToml(file, section, kv) {
+ const lines = (await fsp.readFile(file, "utf8").catch(() => "")).split("\n");
+ const header = `[${section}]`;
+ let start = lines.findIndex((l) => l.trim() === header);
+ if (start < 0) {
+ lines.push("", header);
+ start = lines.length - 1;
+ }
+ let end = lines.findIndex((l, i) => i > start && /^\s*\[/.test(l));
+ if (end < 0) end = lines.length;
+ for (const [k, v] of Object.entries(kv)) {
+ const line = `${k} = ${typeof v === "string" ? JSON.stringify(v) : v}`;
+ const at = lines.slice(start + 1, end).findIndex((l) => l.trim().startsWith(`${k} =`));
+ if (at >= 0) lines[start + 1 + at] = line;
+ else lines.splice(end++, 0, line);
+ }
+ await fsp.writeFile(file, lines.join("\n"));
+}
+
+/**
+ * One core with its own HOME. Scenario code talks to it through the same
+ * methods whether it is the driver's main core or an isolated one.
+ */
+class Rig {
+ constructor({ name, dir, driver, url, masterKey = true, findings, binding, secrets, results }) {
+ this.name = name;
+ this.dir = dir;
+ this.driver = driver;
+ this.home = path.join(dir, "home");
+ this.oh = path.join(this.home, ".openhuman");
+ this.findings = findings;
+ this.binding = binding;
+ this.secrets = secrets;
+ this.results = results;
+ this.methods = null;
+ this.core = null;
+ this.env = {};
+ if (masterKey) {
+ const key = randomBytes(32).toString("hex");
+ this.secrets.push(key);
+ this.env.OPENHUMAN_KEYRING_MASTER_KEY = key;
+ }
+ this.storageDir = null;
+ if (url) {
+ const resolved = url.replace("<home>", this.home);
+ this.env.OPENHUMAN_STORAGE_URL = resolved;
+ if (resolved.startsWith("file:")) this.storageDir = resolved.slice(5);
+ }
+ this.rpcLog = new JsonlLog(path.join(dir, "rpc.jsonl"), secrets);
+ }
+
+ baseConfig() {
+ return BASE_CONFIG;
+ }
+ editConfig(file, section, kv) {
+ return editToml(file, section, kv);
+ }
+ addSecret(s) {
+ this.secrets.push(s);
+ }
+
+ async start() {
+ await fsp.mkdir(this.oh, { recursive: true });
+ if (!fs.existsSync(path.join(this.oh, "config.toml")))
+ await fsp.writeFile(path.join(this.oh, "config.toml"), BASE_CONFIG);
+ this.core = new Core({ coreBin: this.binding.coreBin, rpcLog: this.rpcLog, secrets: this.secrets });
+ await this.core.start({
+ home: this.home,
+ actionDir: path.join(this.dir, "action"),
+ logPath: path.join(this.dir, "core.log"),
+ extraEnv: this.env,
+ });
+ if (!this.methods) {
+ const text = await fetch(`${this.core.url}/schema`).then((r) => r.text()).catch(() => "");
+ this.methods = new Set(text.match(/openhuman\.[a-z0-9_]+/g) ?? []);
+ }
+ }
+ async stop() {
+ await this.core?.stop();
+ }
+ async restart() {
+ await this.stop();
+ await this.start();
+ }
+ async kill9() {
+ await killHard(this.core);
+ }
+ get alive() {
+ return !!this.core && !this.core.exited;
+ }
+
+ /** Whether the build serves `method` (true when /schema could not be read). */
+ has(method) {
+ return !this.methods?.size || this.methods.has(method);
+ }
+ rpc(method, params, timeoutMs) {
+ return this.core.rpc(method, params, timeoutMs);
+ }
+ tryRpc(method, params, timeoutMs) {
+ return this.core.tryRpc(method, params, timeoutMs);
+ }
+
+ /**
+ * Sign `userId` in with a locally minted session (never validated by the
+ * core). A brand-new profile is pre-seeded as layout v2 with the run's
+ * config, so its own config keeps the scheduler gate off.
+ */
+ async signIn(userId) {
+ const dir = path.join(this.oh, "users", userId);
+ if (!fs.existsSync(dir)) {
+ await fsp.mkdir(dir, { recursive: true });
+ await fsp.writeFile(path.join(dir, ".layout"), "2\n");
+ await fsp.writeFile(path.join(dir, "config.toml"), BASE_CONFIG);
+ }
+ const token = mintSessionToken(userId);
+ this.addSecret(token);
+ await this.rpc("openhuman.auth_set_credential", { token, kind: "session", userId });
+ return token;
+ }
+
+ /** Every SQLite storage database under this HOME (backups excluded). */
+ async dbFiles() {
+ return (await findFiles(this.home, "storage.db")).filter((p) => !p.includes(`${path.sep}backups${path.sep}`));
+ }
+
+ check(c) {
+ return this.findings.check({ engine: this.driver, scenario: this.binding.scenario, ...c });
+ }
+ note(id, text) {
+ this.findings.note({ engine: this.driver, scenario: this.binding.scenario, id, text });
+ }
+ result(key, value) {
+ this.results[`${this.binding.scenario}/${key}`] = value;
+ }
+}
+
+async function runDriver(driver, { opts, runDir, runId, findings, results }) {
+ const dir = path.join(runDir, driver);
+ const secrets = [];
+ const binding = { scenario: "setup", coreBin: opts.coreBin };
+ const driverResults = { scenarios: {}, results: {} };
+ results.drivers[driver] = driverResults;
+ const url = { sqlite: null, file: "file:<home>/storage-file", memory: "memory" }[driver];
+ const main = new Rig({ name: "main", dir, driver, url, findings, binding, secrets, results: driverResults.results });
+ const isolated = [];
+ // Scenarios talk to the main rig directly; it carries the run's tag, the
+ // state shared between scenarios and the factory for isolated rigs.
+ const ctx = Object.assign(main, {
+ tag: runId.slice(-12).replace(/[^0-9A-Za-z]/g, "").toLowerCase(),
+ state: {},
+ isolated: async (name, o = {}) => {
+ const r = new Rig({ name, dir: path.join(dir, `iso-${name}`), driver, url: o.url, masterKey: o.masterKey !== false, findings, binding, secrets, results: driverResults.results });
+ isolated.push(r);
+ return r;
+ },
+ });
+
+ await main.start();
+ const wanted = (s) => !opts.only.length || opts.only.some((o) => s.id.toUpperCase().startsWith(o));
+ const list = [...SCENARIOS, ...ISOLATED].filter((s) => s.drivers.includes(driver) && wanted(s));
+ for (const s of list) {
+ binding.scenario = s.id;
+ const before = findings.checks.length;
+ const notesBefore = findings.items.filter((f) => f.severity === "note").length;
+ const started = Date.now();
+ let error = null;
+ console.log(`${driver.padEnd(7)}: ${s.id}`);
+ try {
+ if (!main.alive) await main.start();
+ await s.run(ctx);
+ } catch (e) {
+ error = scrub(e.message, secrets);
+ findings.add({ engine: driver, scenario: s.id, id: "crashed", severity: "high", expected: "the scenario runs to the end", actual: error, evidence: `${driver}/core.log` });
+ } finally {
+ // Isolated cores never outlive their scenario (F restarts E's rig itself).
+ for (const r of isolated.splice(0)) await r.stop().catch(() => {});
+ }
+ const checks = findings.checks.slice(before).filter((c) => c.engine === driver);
+ driverResults.scenarios[s.id] = {
+ checks: checks.length,
+ failed: checks.filter((c) => !c.ok).length,
+ notes: findings.items.filter((f) => f.severity === "note").length - notesBefore,
+ ms: Date.now() - started,
+ error,
+ };
+ }
+ await main.stop();
+ if (ctx.state.legacy?.rig) await ctx.state.legacy.rig.stop().catch(() => {});
+}
+
+function renderReport({ runId, results, findings }) {
+ const lines = [`# Storage scenarios ${runId}`, ""];
+ for (const [driver, dr] of Object.entries(results.drivers)) {
+ lines.push(`## ${driver}`, "", "| scenario | checks passed | notes | time | crashed |", "| --- | --- | --- | --- | --- |");
+ for (const [id, s] of Object.entries(dr.scenarios))
+ lines.push(`| ${id} | ${s.checks - s.failed}/${s.checks} | ${s.notes} | ${((s.ms ?? 0) / 1000).toFixed(0)}s | ${s.error ? "yes" : ""} |`);
+ lines.push("");
+ }
+ const real = findings.filter((f) => f.severity !== "note");
+ lines.push(`## Findings (${real.length})`, "", "| id | severity | basis | expected | actual |", "| --- | --- | --- | --- | --- |");
+ const order = { high: 0, medium: 1, low: 2 };
+ for (const f of [...real].sort((a, b) => (order[a.severity] ?? 3) - (order[b.severity] ?? 3)))
+ lines.push(`| ${f.id} | ${f.severity} | ${f.basis} | ${f.expected} | ${String(f.actual).replace(/\|/g, "\\|").replace(/\n/g, " ").slice(0, 300)} |`);
+ lines.push("", "## Notes", "");
+ for (const f of findings.filter((x) => x.severity === "note")) lines.push(`- **${f.id}** (${f.basis}): ${f.actual}`);
+ lines.push(
+ "",
+ "## Not driven (not findings)",
+ "",
+ "- KV port: no RPC writes `tsd.kv` without a bound memory engine.",
+ "- Agent and module principal scopes: need an agent turn or a module storage grant (no module declares one).",
+ "- SaaS tenants: need the SaaS server mode and a profile lease.",
+ "- Artifact blobs: no RPC creates an artifact; the blob port is driven through voice notes.",
+ );
+ return lines.join("\n") + "\n";
+}
+
+async function writeOutputs(runDir, runId, results, findings) {
+ await fsp.writeFile(path.join(runDir, "findings.json"), JSON.stringify(findings.items, null, 2));
+ await fsp.writeFile(path.join(runDir, "checks.json"), JSON.stringify(findings.checks, null, 2));
+ await fsp.writeFile(path.join(runDir, "results.json"), JSON.stringify(results, null, 2));
+ const md = renderReport({ runId, results, findings: findings.items });
+ await fsp.writeFile(path.join(runDir, "report.md"), md);
+ return md;
+}
+
+async function main() {
+ const opts = parseArgs(process.argv.slice(2));
+ if (opts.gradeOnly) {
+ const findings = JSON.parse(await fsp.readFile(path.join(opts.gradeOnly, "findings.json"), "utf8"));
+ const results = JSON.parse(await fsp.readFile(path.join(opts.gradeOnly, "results.json"), "utf8"));
+ const md = renderReport({ runId: path.basename(opts.gradeOnly), results, findings });
+ await fsp.writeFile(path.join(opts.gradeOnly, "report.md"), md);
+ console.log(md);
+ return;
+ }
+ if (!fs.existsSync(opts.coreBin))
+ throw new Error(`core binary not found at ${opts.coreBin}\nbuild it: cargo build -p openhuman-cli --bin openhuman-core`);
+ const runId = new Date().toISOString().replace(/[:.]/g, "-");
+ const runDir = path.join(opts.runRoot, runId);
+ await fsp.mkdir(runDir, { recursive: true });
+ console.log(`run dir : ${runDir}`);
+ const findings = new Findings();
+ const results = { run_id: runId, started: new Date().toISOString(), drivers: {} };
+ for (const driver of opts.drivers) {
+ try {
+ await runDriver(driver, { opts, runDir, runId, findings, results });
+ } catch (e) {
+ const msg = scrub(e.message, []);
+ console.error(`${driver}: ${msg}`);
+ findings.add({ engine: driver, scenario: "setup", id: "aborted", severity: "high", expected: "the driver's core boots", actual: msg, evidence: `${driver}/core.log` });
+ }
+ }
+ results.finished = new Date().toISOString();
+ await writeOutputs(runDir, runId, results, findings);
+ console.log(`\nreport : ${path.join(runDir, "report.md")}\nfindings: ${findings.items.filter((f) => f.severity !== "note").length}`);
+}
+
+main()
+ .then(() => process.exit(0))
+ .catch((e) => {
+ console.error(`\nfatal: ${scrub(e.stack || e.message, [])}`);
+ process.exit(1);
+ });
diff --git a/scripts/storage-scenarios/scenarios.mjs b/scripts/storage-scenarios/scenarios.mjs
new file mode 100644
index 0000000000..8e406ba7cb
--- /dev/null
+++ b/scripts/storage-scenarios/scenarios.mjs
@@ -0,0 +1,476 @@
+// The storage scenarios that run on the driver's own core: A (write and read
+// back per port), B (durability), C (on-disk truth), H (the memory driver
+// keeps nothing) and I (concurrency). The ones that need a HOME of their own
+// (D, E, F, G, J) are in isolated.mjs.
+//
+// `ctx` (built by run.mjs): driver ("sqlite" | "file" | "memory"), tag,
+// state (shared between scenarios of one driver), rpc(), tryRpc(), has(),
+// check(), note(), signIn(), start/stop/restart/kill9(), home, oh,
+// storageDir (file driver), dbFiles(), isolated().
+
+import fsp from "node:fs/promises";
+import path from "node:path";
+import { randomBytes } from "node:crypto";
+import {
+ pick,
+ sleep,
+ grepTree,
+ findFiles,
+ withDb,
+ sha256,
+} from "./lib.mjs";
+import { tinyflowsExpected } from "./legacy.mjs";
+
+const FAR_FUTURE = "2099-01-01T00:00:00Z";
+const json = (v) => JSON.stringify(v ?? null);
+const errText = (r) => (r.ok ? "" : json(r.error));
+
+/** The A records' contents, built once per driver. */
+function fixtures(ctx) {
+ const t = ctx.tag;
+ const big = `storscen-${t}-msg2 ` + "x".repeat(65_536);
+ const contents = [
+ `storscen-${t}-msg0 plain ascii`,
+ `storscen-${t}-msg1 Zoë — 日本語 — emoji 🚀 — RTL שלום`,
+ big,
+ `storscen-${t}-msg3 {"looks":"like json","n":[1,2,3],"nested":{"q":"\\"quoted\\""}}`,
+ `storscen-${t}-msg4 line one\nline two\r\n\ttab, back\\slash, NUL-free`,
+ ...Array.from({ length: 15 }, (_, i) => `storscen-${t}-msg${i + 5} #${i + 5}`),
+ ];
+ return {
+ sessionUser: `storscen-a-${t}`,
+ cronName: `storscen-cron-${t}`,
+ cronPrompt: `storscen fixture prompt ${t}: this job never runs`,
+ notifTitle: `storscen notification ${t}`,
+ notifBody: `Body for ${t}: café ☕ <b>not html</b>`,
+ flowTrustId: `storscen-flow-${t}`,
+ threadId: `storscen-thread-${t}`,
+ messages: contents.map((content, i) => ({ id: `storscen-${t}-m${i}`, content })),
+ noteId: `storscen_${t}`,
+ noteBytes: randomBytes(4096),
+ provider: `storscen-provider-${t}`,
+ providerToken: `storscen-provider-secret-${randomBytes(16).toString("hex")}`,
+ };
+}
+
+/** Read every A record back; `label` prefixes the check ids (A, B1, ...). */
+async function readBackA(ctx, label) {
+ const a = ctx.state.a;
+ const out = {};
+ if (a.cronId) {
+ const r = await ctx.tryRpc("openhuman.cron_list", {});
+ const jobs = pick(r.value, "jobs", "result.jobs") ?? [];
+ const job = jobs.find?.((j) => j.id === a.cronId);
+ out.cron = ctx.check({
+ id: `${label}-cron`,
+ ok: !!job && job.name === a.cronName && job.prompt === a.cronPrompt,
+ expected: `cron_list has job ${a.cronId} with its name and prompt unchanged`,
+ actual: job ? json({ name: job.name, prompt: job.prompt }) : `not listed (${errText(r) || `${jobs.length} jobs`})`,
+ });
+ }
+ if (a.notifId) {
+ const r = await ctx.tryRpc("openhuman.notification_list", { provider: "gmail", limit: 200 });
+ const items = pick(r.value, "items", "result.items") ?? [];
+ const n = items.find?.((x) => x.id === a.notifId);
+ out.notif = ctx.check({
+ id: `${label}-notification`,
+ ok: !!n && n.title === a.notifTitle && n.body === a.notifBody,
+ expected: "notification_list returns the notification with title and body byte-exact",
+ actual: n ? json({ title: n.title, body: n.body }) : `not listed (${errText(r) || `${items.length} items`})`,
+ });
+ }
+ if (a.flowTrustGranted) {
+ const r = await ctx.tryRpc("openhuman.approval_list_recent_decisions", { limit: 200 });
+ out.approval = ctx.check({
+ id: `${label}-approval`,
+ ok: r.ok && json(r.value).includes(a.flowTrustId),
+ expected: `approval_list_recent_decisions names flow ${a.flowTrustId}`,
+ actual: r.ok ? json(r.value).slice(0, 300) : errText(r),
+ });
+ }
+ {
+ const r = await ctx.tryRpc("openhuman.threads_messages_list", { thread_id: a.threadId });
+ const msgs = pick(r.value, "messages", "result.messages") ?? [];
+ const ours = msgs.filter?.((m) => String(m.id).startsWith(`storscen-${ctx.tag}-m`)) ?? [];
+ const bad = a.messages.filter((m, i) => ours[i]?.id !== m.id || ours[i]?.content !== m.content);
+ out.stream = ctx.check({
+ id: `${label}-stream`,
+ ok: r.ok && ours.length === a.messages.length && bad.length === 0,
+ expected: `threads_messages_list returns all ${a.messages.length} messages in order, byte-exact (incl. 64 KiB, unicode, JSON-ish, CR/LF)`,
+ actual: r.ok
+ ? `${ours.length} returned; mismatched: ${bad.map((m) => m.id).join(",") || "none"}`
+ : errText(r),
+ });
+ }
+ if (a.noteStored) {
+ const r = await ctx.tryRpc("openhuman.channel_web_voice_note_read", { note_id: a.noteId });
+ const b64 = pick(r.value, "audio_base64", "result.audio_base64");
+ const got = b64 ? Buffer.from(b64, "base64") : null;
+ out.blob = ctx.check({
+ id: `${label}-blob`,
+ ok: !!got && got.equals(a.noteBytes),
+ expected: "channel_web_voice_note_read returns the 4096 stored bytes exactly",
+ actual: got ? `${got.length} bytes, sha256 ${sha256(got).slice(0, 16)} (want ${sha256(a.noteBytes).slice(0, 16)})` : `no bytes (${errText(r) || json(r.value)})`,
+ });
+ }
+ {
+ const r = await ctx.tryRpc("openhuman.auth_list_provider_credentials", { provider: a.provider });
+ const list = pick(r.value, "profiles", "result.profiles", "result") ?? r.value;
+ const p = (Array.isArray(list) ? list : []).find((x) => x.provider === a.provider);
+ out.secret = ctx.check({
+ id: `${label}-secret-listed`,
+ ok: !!p && (p.hasToken === true || p.has_token === true),
+ expected: "auth_list_provider_credentials lists the provider with hasToken (masked read-back)",
+ actual: p ? json(p) : `not listed (${errText(r) || json(r.value).slice(0, 200)})`,
+ });
+ }
+ {
+ const r = await ctx.tryRpc("openhuman.auth_get_session_token", {});
+ const tok = typeof r.value === "string" ? r.value : pick(r.value, "token", "result.token", "session_token");
+ out.session = ctx.check({
+ id: `${label}-secret-roundtrip`,
+ ok: tok === a.sessionToken,
+ expected: "auth_get_session_token returns the stored session token exactly (decrypted read-back)",
+ actual: r.ok ? (tok === a.sessionToken ? "exact" : `different value (${tok ? `${String(tok).length} chars` : "none"})`) : errText(r),
+ severity: "high",
+ });
+ }
+ return out;
+}
+
+// ---------------------------------------------------------------------------
+// A. write -> read back per port
+// ---------------------------------------------------------------------------
+
+const writeRead = {
+ id: "A-write-read",
+ title: "Write, then read back, on every port through the real RPCs",
+ drivers: ["sqlite", "file", "memory"],
+ async run(ctx) {
+ const a = (ctx.state.a = fixtures(ctx));
+ ctx.addSecret(a.providerToken);
+
+ // A0: a signed-in user, so every record lives in that user's profile.
+ a.sessionToken = await ctx.signIn(a.sessionUser);
+
+ // Documents: a cron job, a notification, a flow pre-authorisation.
+ const cron = await ctx.tryRpc("openhuman.cron_add", {
+ name: a.cronName,
+ schedule: { kind: "at", at: FAR_FUTURE },
+ job_type: "agent",
+ prompt: a.cronPrompt,
+ delete_after_run: false,
+ });
+ a.cronId = pick(cron.value, "job.id", "result.job.id", "id");
+ ctx.check({
+ id: "A-cron-add",
+ ok: !!a.cronId,
+ expected: "cron_add stores a job and returns its id",
+ actual: a.cronId ?? errText(cron),
+ });
+ const notif = await ctx.tryRpc("openhuman.notification_ingest", {
+ provider: "gmail",
+ account_id: `storscen-${ctx.tag}`,
+ title: a.notifTitle,
+ body: a.notifBody,
+ raw_payload: { storscen: ctx.tag, list: [1, 2, 3] },
+ });
+ a.notifId = pick(notif.value, "id", "result.id");
+ ctx.check({
+ id: "A-notification-ingest",
+ ok: !!a.notifId,
+ expected: "notification_ingest stores the notification (not skipped)",
+ actual: a.notifId ?? (notif.ok ? json(notif.value) : errText(notif)),
+ });
+ const trust = await ctx.tryRpc("openhuman.approval_preauthorize_flow", {
+ flow_id: a.flowTrustId,
+ tool_names: ["storscen_tool"],
+ });
+ a.flowTrustGranted = trust.ok;
+ ctx.check({
+ id: "A-approval-preauthorize",
+ ok: trust.ok && pick(trust.value, "gate_installed", "result.gate_installed") !== false,
+ expected: "approval_preauthorize_flow grants trust with the approval gate installed",
+ actual: trust.ok ? json(trust.value) : errText(trust),
+ });
+
+ // Stream: a thread with 20 messages.
+ const up = await ctx.tryRpc("openhuman.threads_upsert", {
+ id: a.threadId,
+ title: `storscen thread ${ctx.tag}`,
+ created_at: new Date().toISOString(),
+ });
+ ctx.check({ id: "A-thread-upsert", ok: up.ok, expected: "threads_upsert creates the thread", actual: up.ok ? "ok" : errText(up) });
+ let appended = 0;
+ for (const m of a.messages) {
+ const r = await ctx.tryRpc("openhuman.threads_message_append", {
+ thread_id: a.threadId,
+ message: { id: m.id, content: m.content, type: "text", extraMetadata: { storscen: ctx.tag }, sender: "user", createdAt: new Date().toISOString() },
+ });
+ if (r.ok) appended += 1;
+ else ctx.state.appendError ??= errText(r);
+ }
+ ctx.check({
+ id: "A-thread-append",
+ ok: appended === a.messages.length,
+ expected: `all ${a.messages.length} threads_message_append calls succeed`,
+ actual: `${appended} succeeded${ctx.state.appendError ? `; first error ${ctx.state.appendError}` : ""}`,
+ });
+
+ // Blob: a voice note.
+ const note = await ctx.tryRpc("openhuman.channel_web_voice_note_store", {
+ note_id: a.noteId,
+ audio_base64: a.noteBytes.toString("base64"),
+ mime: "audio/webm",
+ });
+ a.noteStored = note.ok;
+ ctx.check({ id: "A-blob-store", ok: note.ok, expected: "channel_web_voice_note_store stores the bytes", actual: note.ok ? json(note.value) : errText(note) });
+
+ // Secret: a provider credential.
+ const cred = await ctx.tryRpc("openhuman.auth_store_provider_credentials", {
+ provider: a.provider,
+ profile: "default",
+ token: a.providerToken,
+ fields: { region: "eu-fictional" },
+ setActive: true,
+ });
+ ctx.check({ id: "A-secret-store", ok: cred.ok, expected: "auth_store_provider_credentials stores the credential", actual: cred.ok ? "ok" : errText(cred) });
+
+ ctx.note("A-kv", "KV port not driven: no RPC writes tsd.kv without a bound memory engine (memory state is its only user; app state is a file on purpose). C lists the KV rows the run produced.");
+
+ await readBackA(ctx, "A");
+ },
+};
+
+// ---------------------------------------------------------------------------
+// B. durability
+// ---------------------------------------------------------------------------
+
+const durability = {
+ id: "B-durability",
+ title: "Restart keeps everything; kill -9 mid-write leaves no torn records",
+ drivers: ["sqlite", "file"],
+ async run(ctx) {
+ await ctx.restart();
+ await readBackA(ctx, "B1");
+
+ // B2: appends in flight when the core is SIGKILLed.
+ const thread = `storscen-crash-${ctx.tag}`;
+ await ctx.rpc("openhuman.threads_upsert", { id: thread, title: "crash", created_at: new Date().toISOString() });
+ const body = (i) => `storscen-${ctx.tag}-crash${i} ` + "y".repeat(2048 + i);
+ const acked = new Set();
+ const attempted = new Set();
+ let next = 0;
+ let killed = false;
+ const worker = async () => {
+ while (!killed && next < 400) {
+ const i = next++;
+ const id = `crash-${ctx.tag}-${i}`;
+ attempted.add(id);
+ const r = await ctx.tryRpc("openhuman.threads_message_append", {
+ thread_id: thread,
+ message: { id, content: body(i), type: "text", extraMetadata: {}, sender: "user", createdAt: new Date().toISOString() },
+ }, 10_000);
+ if (r.ok) acked.add(id);
+ }
+ };
+ const workers = Array.from({ length: 4 }, worker);
+ while (acked.size < 40 && next < 400) await sleep(5);
+ killed = true;
+ await ctx.kill9();
+ await Promise.allSettled(workers);
+ ctx.result("crash", { attempted: attempted.size, acked_before_kill: acked.size });
+
+ await ctx.start();
+ const r = await ctx.tryRpc("openhuman.threads_messages_list", { thread_id: thread });
+ const msgs = pick(r.value, "messages", "result.messages") ?? [];
+ const ids = msgs.map((m) => m.id);
+ const lost = [...acked].filter((id) => !ids.includes(id));
+ const torn = msgs.filter((m) => {
+ const i = Number(String(m.id).split("-").pop());
+ return m.content !== body(i);
+ });
+ const dup = ids.length - new Set(ids).size;
+ const phantom = ids.filter((id) => !attempted.has(id));
+ ctx.check({ id: "B2-no-acked-loss", ok: r.ok && lost.length === 0, expected: "every append acked before the kill is present after reboot", actual: r.ok ? `${acked.size} acked, ${lost.length} lost${lost.length ? `: ${lost.slice(0, 5)}` : ""}` : errText(r), severity: "high" });
+ ctx.check({ id: "B2-no-torn", ok: r.ok && torn.length === 0 && dup === 0 && phantom.length === 0, expected: "no torn, duplicated or phantom message after the kill", actual: `${msgs.length} present; torn ${torn.length}, duplicates ${dup}, phantom ${phantom.length}`, severity: "high" });
+
+ await ctx.stop();
+ if (ctx.driver === "sqlite") {
+ for (const db of await ctx.dbFiles()) {
+ const res = withDb(db, (d) => d.integrity());
+ ctx.check({ id: `B2-integrity-${path.basename(path.dirname(db))}`, ok: res.length === 1 && res[0] === "ok", expected: "PRAGMA integrity_check = ok after the kill", actual: res.join("; ").slice(0, 300), evidence: db, severity: "high" });
+ }
+ } else {
+ // The file driver's records are JSON / JSONL: every one must parse.
+ const broken = [];
+ async function walk(dir) {
+ for (const e of await fsp.readdir(dir, { withFileTypes: true }).catch(() => [])) {
+ const p = path.join(dir, e.name);
+ if (e.isDirectory()) await walk(p);
+ else if (p.endsWith(".json")) {
+ try { JSON.parse(await fsp.readFile(p, "utf8")); } catch { broken.push(p); }
+ } else if (p.endsWith(".jsonl")) {
+ for (const line of (await fsp.readFile(p, "utf8")).split("\n").filter(Boolean))
+ try { JSON.parse(line); } catch { broken.push(p); break; }
+ }
+ }
+ }
+ await walk(ctx.storageDir);
+ ctx.check({ id: "B2-file-records-parse", ok: broken.length === 0, expected: "every JSON/JSONL record under the file store parses after the kill", actual: `${broken.length} broken${broken.length ? `: ${broken.slice(0, 3).map((p) => path.relative(ctx.storageDir, p))}` : ""}`, severity: "high" });
+ }
+ await ctx.start();
+ },
+};
+
+// ---------------------------------------------------------------------------
+// C. on-disk truth
+// ---------------------------------------------------------------------------
+
+const onDisk = {
+ id: "C-on-disk",
+ title: "Records sit in the expected scope and collection; secrets are not plaintext",
+ drivers: ["sqlite", "file"],
+ async run(ctx) {
+ const a = ctx.state.a;
+ await ctx.stop();
+ const marker0 = a.messages[0].content.split(" ")[0];
+ // Positive control first: the grep must find a plaintext value that IS stored.
+ const control = await grepTree(ctx.home, marker0);
+ ctx.check({ id: "C-grep-control", ok: control.length > 0, expected: "the plaintext grep finds a stored message marker (positive control)", actual: `${control.length} files` });
+ for (const [what, secret] of [["provider token", a.providerToken], ["session token", a.sessionToken]]) {
+ const hits = await grepTree(ctx.home, secret);
+ ctx.check({
+ id: `C-no-plaintext-${what.replace(" ", "-")}`,
+ ok: control.length > 0 && hits.length === 0,
+ expected: `the ${what} appears nowhere under HOME in plaintext`,
+ actual: control.length === 0 ? "control failed: grep proves nothing" : `${hits.length} files${hits.length ? `: ${hits.map((p) => path.relative(ctx.home, p)).join(", ")}` : ""}`,
+ severity: "high",
+ });
+ }
+
+ if (ctx.driver === "sqlite") {
+ const dbs = await ctx.dbFiles();
+ const own = dbs.filter((p) => p.includes(`${path.sep}users${path.sep}${a.sessionUser}${path.sep}`));
+ ctx.check({ id: "C-db-location", ok: own.length === 1, expected: `one storage.db under users/${a.sessionUser}/ (layout v2 profile)`, actual: dbs.map((p) => path.relative(ctx.oh, p)).join(", ") || "none" });
+ const file = own[0] ?? dbs[0];
+ if (file) {
+ withDb(file, (db) => {
+ const where = (coll, needle) => db.docs(coll).filter((d) => d.body.includes(needle)).map((d) => d.scope);
+ const expectLocal = (id, coll, needle, why) => {
+ const scopes = where(coll, needle);
+ ctx.check({ id, ok: scopes.length > 0 && scopes.every((s) => s === "local"), expected: `${coll} holds it in the acting scope \`local\` (${why})`, actual: scopes.length ? scopes.join(",") : `not in ${coll}` });
+ };
+ expectLocal("C-cron-scope", "cron.jobs", a.cronName, "cron/documents_port.rs: DocScope::acting");
+ expectLocal("C-notification-scope", "notifications.integration", a.notifTitle, "domain stores use the tenant scope");
+ expectLocal("C-approval-scope", "approvals.flow_trust", a.flowTrustId, "domain stores use the tenant scope");
+ const stream = db.tables.streams
+ ? db.all(`SELECT scope, name FROM "${db.tables.streams}" WHERE instr(body, ?) > 0`, marker0)
+ : [];
+ ctx.check({ id: "C-stream-scope", ok: stream.length > 0 && stream.every((s) => s.scope === "local"), expected: "the thread's messages are stream rows in scope `local`", actual: stream.length ? json(stream[0]) : "no stream row holds the marker" });
+ const blob = db.blob(`%${a.noteId}%`);
+ ctx.check({ id: "C-blob", ok: blob.length === 1 && blob[0].scope === "local" && Buffer.from(blob[0].bytes).equals(a.noteBytes), expected: "the voice note is one blob row in `local` with the exact bytes", actual: blob.length ? `${blob[0].scope} ${blob[0].key} ${blob[0].size} bytes` : "no blob row" });
+ const secrets = db.docs("secrets");
+ const leak = secrets.filter((s) => s.body.includes(a.providerToken) || s.body.includes(a.sessionToken));
+ ctx.check({ id: "C-secret-docs", ok: secrets.length > 0 && leak.length === 0, expected: "secrets are encrypted documents (collection `secrets`), no plaintext in their bodies", actual: `${secrets.length} secret docs in ${[...new Set(secrets.map((s) => s.scope))].join(",") || "-"}; ${leak.length} with plaintext` , severity: "high" });
+ ctx.result("census", db.census());
+ ctx.result("scopes", db.scopes());
+ ctx.note("C-census", `scopes on disk: ${db.scopes().join(", ")}; KV rows: ${db.kvRows().length}; tables: ${json(db.tables)}`);
+ });
+ }
+ } else {
+ const docHits = await grepTree(ctx.storageDir, a.cronName);
+ ctx.check({ id: "C-file-cron-scope", ok: docHits.some((p) => p.includes(`${path.sep}scopes${path.sep}local${path.sep}`)), expected: "the cron job is a JSON document under <dir>/scopes/local/", actual: docHits.map((p) => path.relative(ctx.storageDir, p)).join(", ") || "not found" });
+ const blobHits = await findFiles(ctx.storageDir, `${a.noteId}.webm`);
+ let same = false;
+ for (const p of blobHits) same ||= (await fsp.readFile(p)).equals(a.noteBytes);
+ ctx.check({ id: "C-file-blob", ok: same, expected: "the voice note is a raw file with the exact bytes", actual: blobHits.map((p) => path.relative(ctx.storageDir, p)).join(", ") || "not found" });
+ }
+ await ctx.start();
+ },
+};
+
+// ---------------------------------------------------------------------------
+// H. the memory driver keeps nothing across a restart (by design)
+// ---------------------------------------------------------------------------
+
+const memoryRestart = {
+ id: "H-memory-restart",
+ title: "memory driver: everything gone after a restart, as designed",
+ drivers: ["memory"],
+ async run(ctx) {
+ const a = ctx.state.a;
+ await ctx.restart();
+ await ctx.signIn(a.sessionUser);
+ // Positive control: a record written now IS listed, so an empty answer means gone.
+ const probeName = `storscen-probe-${ctx.tag}`;
+ const add = await ctx.tryRpc("openhuman.cron_add", { name: probeName, schedule: { kind: "at", at: FAR_FUTURE }, job_type: "agent", prompt: "probe" });
+ const list = await ctx.tryRpc("openhuman.cron_list", {});
+ const jobs = pick(list.value, "jobs", "result.jobs") ?? [];
+ const control = add.ok && jobs.some((j) => j.name === probeName);
+ ctx.check({ id: "H-control", ok: control, expected: "a job added after the restart is listed (positive control)", actual: control ? "listed" : errText(add) || errText(list) });
+ ctx.check({ id: "H-cron-gone", ok: control && !jobs.some((j) => j.id === a.cronId), expected: "the A cron job is gone after a restart", actual: control ? (jobs.some((j) => j.id === a.cronId) ? "still listed" : "gone") : "control failed" });
+ const msgs = await ctx.tryRpc("openhuman.threads_messages_list", { thread_id: a.threadId });
+ const n = (pick(msgs.value, "messages", "result.messages") ?? []).length;
+ ctx.check({ id: "H-stream-gone", ok: control && n === 0, expected: "the A thread has no messages after a restart", actual: msgs.ok ? `${n} messages` : `error (${errText(msgs).slice(0, 120)})` });
+ },
+};
+
+// ---------------------------------------------------------------------------
+// I. concurrency
+// ---------------------------------------------------------------------------
+
+const concurrency = {
+ id: "I-concurrency",
+ title: "Concurrent writers: compare-and-swap has one winner; parallel appends lose nothing",
+ drivers: ["sqlite"],
+ async run(ctx) {
+ if (ctx.has("openhuman.flows_create") && ctx.has("openhuman.flows_update")) {
+ const graph = (await tinyflowsExpected()).catalog.flows[0].graph;
+ const made = await ctx.tryRpc("openhuman.flows_create", { name: `storscen-race-${ctx.tag}`, graph });
+ const flow = pick(made.value, "flow", "result.flow", "result") ?? made.value;
+ const id = flow?.id;
+ const version = flow?.updated_at ?? flow?.updatedAt;
+ ctx.check({ id: "I-flow-create", ok: !!id && !!version, expected: "flows_create returns the flow with its id and updated_at", actual: id ? `${id} @ ${version}` : errText(made) || json(made.value).slice(0, 200) });
+ if (id && version) {
+ const N = 8;
+ const res = await Promise.all(
+ Array.from({ length: N }, (_, i) => ctx.tryRpc("openhuman.flows_update", { id, name: `storscen-race-${ctx.tag}-${i}`, expected_version: version })),
+ );
+ const conflict = (r) => json(r.ok ? r.value : r.error).includes("version_conflict");
+ const winners = res.map((r, i) => (r.ok && !conflict(r) ? i : -1)).filter((i) => i >= 0);
+ const conflicts = res.filter(conflict).length;
+ ctx.result("cas", { writers: N, winners: winners.length, conflicts });
+ ctx.check({ id: "I-cas-one-winner", ok: winners.length === 1 && conflicts === N - 1, expected: `exactly 1 of ${N} same-version updates wins; the other ${N - 1} get version_conflict`, actual: `${winners.length} won, ${conflicts} conflicts, ${N - winners.length - conflicts} other`, severity: "high" });
+ const all = await ctx.tryRpc("openhuman.flows_list", {});
+ const now = json(all.value).includes(`storscen-race-${ctx.tag}-${winners[0]}"`);
+ ctx.check({ id: "I-cas-final-state", ok: winners.length === 1 && now, expected: "the stored flow carries the winner's name", actual: now ? "winner's name stored" : `not found (${errText(all).slice(0, 120)})` });
+ const dbs = await ctx.dbFiles();
+ const scopes = dbs.length
+ ? withDb(dbs.find((p) => p.includes(ctx.state.a?.sessionUser ?? "")) ?? dbs[0], (db) =>
+ db.all(`SELECT scope, coll FROM "${db.tables.docs}" WHERE id = ? OR instr(body, ?) > 0`, id, id))
+ : [];
+ const flowRows = scopes.filter((r) => r.coll.startsWith("flows"));
+ ctx.check({ id: "I-flow-scope", ok: flowRows.length > 0 && flowRows.every((r) => r.scope === "local"), expected: "the flow is stored in the acting scope `local` (flows/catalog_storage.rs binds the module's callback to the acting scope)", actual: flowRows.map((r) => `${r.scope} ${r.coll}`).join("; ") || "no flow row found" });
+ if (flowRows.some((r) => r.scope.includes("/module:")))
+ ctx.note("I-flow-module-scope", "flow records sit in a module principal scope");
+ }
+ } else {
+ ctx.note("I-cas-skipped", "flows_create / flows_update are not in this build's /schema; the compare-and-swap race was not run.");
+ }
+
+ const thread = `storscen-race-thread-${ctx.tag}`;
+ await ctx.rpc("openhuman.threads_upsert", { id: thread, title: "race", created_at: new Date().toISOString() });
+ const N = 50;
+ const res = await Promise.all(
+ Array.from({ length: N }, (_, i) =>
+ ctx.tryRpc("openhuman.threads_message_append", { thread_id: thread, message: { id: `race-${i}`, content: `race ${i}`, type: "text", extraMetadata: {}, sender: "user", createdAt: new Date().toISOString() } })),
+ );
+ const ok = res.filter((r) => r.ok).length;
+ const list = await ctx.tryRpc("openhuman.threads_messages_list", { thread_id: thread });
+ const ids = (pick(list.value, "messages", "result.messages") ?? []).map((m) => m.id);
+ ctx.check({ id: "I-append-race", ok: ok === N && ids.length === N && new Set(ids).size === N, expected: `${N} parallel appends to one thread: all succeed, all ${N} listed once`, actual: `${ok} succeeded, ${ids.length} listed, ${new Set(ids).size} unique`, severity: "high" });
+ },
+};
+
+export const SCENARIOS = [writeRead, durability, onDisk, memoryRestart, concurrency];