Skip to content

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

fix(scripts): read storage-scenarios answers in both RPC envelopes

M3gA-Mind3d4877547fd4
diff --git a/scripts/storage-scenarios/isolated.mjs b/scripts/storage-scenarios/isolated.mjs
index b6b60dfe1e..cce55eccd8 100644
--- a/scripts/storage-scenarios/isolated.mjs
+++ b/scripts/storage-scenarios/isolated.mjs
@@ -7,13 +7,13 @@ 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 { pick, grepTree, withDb, treeDigest, listOf } 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 jobsOf = (r) => listOf(r.value, "jobs");
const exists = (p) => fs.existsSync(p);
const userDb = (dbs, user) => dbs.filter((p) => p.includes(`${path.sep}users${path.sep}${user}${path.sep}`));
@@ -39,7 +39,7 @@ async function writeUserData(rig, user, tag) {
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 thread = listOf(msgs.value, "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;
@@ -124,7 +124,8 @@ const legacyImport = {
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 missingJobs = exp.cron.jobs.filter((e) => !jobs.some((j) => j.id === e.id));
+ ctx.check({ id: "E-cron", ok: missingJobs.length === 0, expected: `cron_list has the ${cronIds.length} legacy jobs with their ids kept`, actual: missingJobs.length ? `missing ${missingJobs.map((e) => `${e.id} (${e.name})`).join(", ")}; listed: ${jobs.map((j) => `${j.id} (${j.name})`).join(", ").slice(0, 400)}` : "all listed" });
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 });
@@ -141,19 +142,21 @@ const legacyImport = {
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"),
+ // Only the seeded originals count: the core may add files of its own
+ // (its storage directory, built-in flow skills) beside them.
+ const changed = Object.keys(before).filter(
+ (k) => before[k] !== after[k] && !k.endsWith("-shm") && !k.endsWith("-wal") && k !== "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-originals-untouched", ok: changed.length === 0, expected: `every one of the ${Object.keys(before).length} seeded legacy files is byte-identical after import (cost ledger aside)`, actual: changed.length ? changed.slice(0, 8).map((k) => `${k}${after[k] ? " (modified)" : " (removed)"}`).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) }));
+ const first = withDb(dbFile, (db) => ({ census: db.census(), markers: markerIds(db) }));
ctx.result("E-census", first.census);
- const missing = LEGACY_EXPECT.markerDomains.filter((d) => !first.markers.some((m) => m.includes(`${d}@`)));
+ const missing = LEGACY_EXPECT.markerDomains.filter((d) => !first.markers.some((m) => m.includes(`storage_import/${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" });
@@ -161,12 +164,18 @@ const legacyImport = {
// 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 second = withDb(dbFile, (db) => ({ census: db.census(), markers: markerIds(db) }));
+ // The imported domains' own collections in the tenant scope; storage
+ // bookkeeping (tsd.*, leases, journals) and module scopes move on their own.
+ const imported = (r) => r.scope === "local" && !/^tsd\.|journal|leases|admission|^storage\./.test(r.coll);
+ const delta = second.census.filter(imported).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" });
+ });
+ const legacyMarkers = (m) => m.filter((x) => x.includes("storage_import/")).sort();
+ ctx.check({ id: "E-idempotent", ok: delta.length === 0 && json(legacyMarkers(second.markers)) === json(legacyMarkers(first.markers)), expected: "a second boot adds no imported record and no import marker", actual: delta.length ? json(delta).slice(0, 300) : `unchanged; ${legacyMarkers(second.markers).length} markers` });
+ const other = second.census.filter((r) => !imported(r)).filter((r) => (first.census.find((x) => x.scope === r.scope && x.coll === r.coll)?.n ?? 0) !== r.n);
+ if (other.length) ctx.note("E-bookkeeping-delta", `second boot changed storage bookkeeping (not imported records): ${json(other).slice(0, 400)}`);
ctx.state.legacy.census = first.census;
},
};
@@ -228,6 +237,15 @@ const layoutMigration = {
},
};
+/**
+ * Ids of the `storage.import_state` documents, decoded: tsd stores a key as
+ * `/`-joined hex segments (`claim/73746f72616765/...`), the step id is inside.
+ */
+function markerIds(db) {
+ const hex = (seg) => (/^([0-9a-f]{2})+$/.test(seg) ? Buffer.from(seg, "hex").toString("utf8") : seg);
+ return db.docs("storage.import_state").filter((d) => d.scope === "local").map((d) => d.id.split("/").map(hex).join("/"));
+}
+
async function findAll(root, pred) {
const out = [];
async function walk(dir) {
@@ -302,6 +320,9 @@ const signOutKeepsKey = {
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 credOfEarly = await rig.tryRpc("openhuman.auth_get_state", {});
+ const early = pick(credOfEarly.value, "credential", "state.credential", "result.credential");
+ ctx.check({ id: "J-control", ok: early === "api-key", expected: "before the sign-out, auth_get_state is authenticated by the API key (control)", actual: credOfEarly.ok ? json(credOfEarly.value).slice(0, 200) : errText(credOfEarly) });
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 () => {
diff --git a/scripts/storage-scenarios/lib.mjs b/scripts/storage-scenarios/lib.mjs
index 49cea83a2c..404d8936f3 100644
--- a/scripts/storage-scenarios/lib.mjs
+++ b/scripts/storage-scenarios/lib.mjs
@@ -8,7 +8,7 @@ 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";
+import { sleep } from "../memory-scenarios/lib.mjs";
export {
Core,
@@ -54,6 +54,40 @@ export function mintSessionToken(userId, nonce = randomBytes(12).toString("hex")
export const sha256 = (bytes) => createHash("sha256").update(bytes).digest("hex");
+/**
+ * The list in an RPC answer. Answers come in two envelopes: `{logs, result}`
+ * (unwrapped by Core.rpc, so the list may be the value itself) and the
+ * threads domain's `{data, meta}`; inside either, the list may sit under
+ * `key` (`{messages: [...]}`).
+ */
+export function listOf(value, key) {
+ let v = value;
+ if (v && typeof v === "object" && !Array.isArray(v) && "data" in v && "meta" in v) v = v.data;
+ if (Array.isArray(v)) return v;
+ const inner = v?.[key] ?? v?.result?.[key] ?? v?.result;
+ return Array.isArray(inner) ? inner : [];
+}
+
+/** Files under `root` whose whole content equals `bytes`. */
+export async function filesWithBytes(root, bytes) {
+ const hits = [];
+ 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.isFile() && (await fsp.stat(p)).size === bytes.length && (await fsp.readFile(p)).equals(bytes)) hits.push(p);
+ }
+ }
+ await walk(root);
+ return hits;
+}
+
/** Every file under `root` (relative path -> sha256), for before/after comparisons. */
export async function treeDigest(root, { skip = () => false } = {}) {
const out = {};
@@ -148,8 +182,9 @@ export class StorageDb {
else if (has("scope", "key", "value", "version")) this.tables.kv = t;
}
}
+ /** Rows as plain objects (node:sqlite returns null-prototype rows). */
all(sql, ...args) {
- return this.db.prepare(sql).all(...args);
+ return this.db.prepare(sql).all(...args).map((r) => ({ ...r }));
}
integrity() {
return this.all("PRAGMA integrity_check").map((r) => Object.values(r)[0]);
diff --git a/scripts/storage-scenarios/run.mjs b/scripts/storage-scenarios/run.mjs
index c0ee5a4f28..9c2b39e6ef 100644
--- a/scripts/storage-scenarios/run.mjs
+++ b/scripts/storage-scenarios/run.mjs
@@ -38,6 +38,11 @@ for (const k of [
])
delete process.env[k];
+// Credential and secret paths log their store/load hits and misses at debug;
+// a secrets check that fails is explained by those lines in core.log.
+process.env.RUST_LOG ||=
+ "info,openhuman_core::security::credentials=debug,openhuman_core::security::secret_storage=debug,openhuman_core::storage::secrets=debug";
+
function parseArgs(argv) {
const opts = {
only: [],
diff --git a/scripts/storage-scenarios/scenarios.mjs b/scripts/storage-scenarios/scenarios.mjs
index 8e406ba7cb..2ea3160692 100644
--- a/scripts/storage-scenarios/scenarios.mjs
+++ b/scripts/storage-scenarios/scenarios.mjs
@@ -11,14 +11,7 @@
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 { pick, sleep, grepTree, withDb, sha256, listOf, filesWithBytes } from "./lib.mjs";
import { tinyflowsExpected } from "./legacy.mjs";
const FAR_FUTURE = "2099-01-01T00:00:00Z";
@@ -44,6 +37,7 @@ function fixtures(ctx) {
notifTitle: `storscen notification ${t}`,
notifBody: `Body for ${t}: café ☕ <b>not html</b>`,
flowTrustId: `storscen-flow-${t}`,
+ flowTrustTool: `storscen_tool_${t}`,
threadId: `storscen-thread-${t}`,
messages: contents.map((content, i) => ({ id: `storscen-${t}-m${i}`, content })),
noteId: `storscen_${t}`,
@@ -59,8 +53,8 @@ async function readBackA(ctx, label) {
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);
+ const jobs = listOf(r.value, "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,
@@ -70,8 +64,8 @@ async function readBackA(ctx, label) {
}
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);
+ const items = listOf(r.value, "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,
@@ -83,15 +77,15 @@ async function readBackA(ctx, label) {
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}`,
+ ok: r.ok && listOf(r.value, "decisions").some((d) => d.tool_name === a.flowTrustTool && d.decision === "approve_always_for_flow"),
+ expected: `approval_list_recent_decisions has an approve_always_for_flow decision for tool ${a.flowTrustTool} (rows carry the tool, not the flow id)`,
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 msgs = listOf(r.value, "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`,
@@ -184,7 +178,7 @@ const writeRead = {
});
const trust = await ctx.tryRpc("openhuman.approval_preauthorize_flow", {
flow_id: a.flowTrustId,
- tool_names: ["storscen_tool"],
+ tool_names: [a.flowTrustTool],
});
a.flowTrustGranted = trust.ok;
ctx.check({
@@ -283,7 +277,7 @@ const durability = {
await ctx.start();
const r = await ctx.tryRpc("openhuman.threads_messages_list", { thread_id: thread });
- const msgs = pick(r.value, "messages", "result.messages") ?? [];
+ const msgs = listOf(r.value, "messages");
const ids = msgs.map((m) => m.id);
const lost = [...acked].filter((id) => !ids.includes(id));
const torn = msgs.filter((m) => {
@@ -335,9 +329,10 @@ const onDisk = {
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` });
+ // Positive control first: the grep must find a plaintext value that IS stored
+ // (the cron job's prompt, a plain document field on every driver).
+ const control = await grepTree(ctx.home, a.cronPrompt);
+ ctx.check({ id: "C-grep-control", ok: control.length > 0, expected: "the plaintext grep finds the stored cron prompt (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({
@@ -381,10 +376,8 @@ const onDisk = {
} 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" });
+ const blobHits = await filesWithBytes(ctx.storageDir, a.noteBytes);
+ ctx.check({ id: "C-file-blob", ok: blobHits.length === 1 && blobHits[0].includes(`${path.sep}scopes${path.sep}local${path.sep}`), expected: "the voice note is one raw file under <dir>/scopes/local/ holding exactly the stored bytes", actual: blobHits.map((p) => path.relative(ctx.storageDir, p)).join(", ") || "no file holds the bytes" });
}
await ctx.start();
},
@@ -406,12 +399,12 @@ const memoryRestart = {
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 jobs = listOf(list.value, "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;
+ const n = listOf(msgs.value, "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)})` });
},
};
@@ -468,7 +461,7 @@ const concurrency = {
);
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);
+ const ids = listOf(list.value, "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" });
},
};