Skip to content

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

test(memory): builtin runs on the app's session; guards follow its user dir

M3gA-Mindc03347660496
diff --git a/scripts/memory-scenarios/engines.mjs b/scripts/memory-scenarios/engines.mjs
index 4e3f83a710..20cf916103 100644
--- a/scripts/memory-scenarios/engines.mjs
+++ b/scripts/memory-scenarios/engines.mjs
@@ -219,26 +219,62 @@ export function cortexReader({ endpoint, apiKey }) {
// ---------------------------------------------------------------------------
export const API_KEY_ENV = "OPENHUMAN_BACKEND_API_KEY";
+export const SESSION_ENV = "OPENHUMAN_BACKEND_SESSION_TOKEN";
+const HOME_DIR = process.env.HOME || "";
+const SESSION_FILE =
+ process.env.MEMSCEN_SESSION_FILE || path.join(HOME_DIR, ".memscen-session");
const KEY_FILE =
- process.env.MEMSCEN_KEY_FILE ||
- path.join(process.env.HOME || "", ".memscen-key");
+ process.env.MEMSCEN_KEY_FILE || path.join(HOME_DIR, ".memscen-key");
/**
- * The account's TinyHumans API key, read from ~/.memscen-key at spawn time.
- * It reaches the core only as OPENHUMAN_BACKEND_API_KEY (seeded at boot by
- * security/credentials/ops/boot_env.rs); it is never printed, logged, written
- * to the run dir or passed to an RPC. The run refuses builtin without it.
+ * The account credential for the builtin engine, read at spawn time:
+ * the app's session JWT from ~/.memscen-session when present (preferred: it
+ * carries the memory scope), else the API key from ~/.memscen-key. It reaches
+ * the core only through the environment (security/credentials/ops/boot_env.rs
+ * seeds it); it is never printed, logged, written to the run dir or passed
+ * to an RPC. `subject` (a session's user id) is returned for guard placement
+ * only and is never printed either.
*/
-export async function builtinCredential() {
- let key;
- try {
- key = (await fsp.readFile(KEY_FILE, "utf8")).trim();
- } catch {
- throw new Error(`builtin needs a TinyHumans API key in ${KEY_FILE}`);
+export async function builtinCredential({ only } = {}) {
+ const read = (f) =>
+ fsp
+ .readFile(f, "utf8")
+ .then((t) => t.trim())
+ .catch(() => null);
+ const session = only === "key" ? null : await read(SESSION_FILE);
+ if (session) {
+ const parts = session.split(".");
+ if (parts.length !== 3)
+ throw new Error(`${SESSION_FILE} does not hold a JWT`);
+ let subject;
+ try {
+ const payload = JSON.parse(Buffer.from(parts[1], "base64url").toString());
+ subject = payload.sub ?? payload._id ?? payload.userId;
+ } catch {
+ throw new Error(`${SESSION_FILE}: the JWT payload does not decode`);
+ }
+ if (!subject)
+ throw new Error(`${SESSION_FILE}: the session carries no subject`);
+ return {
+ kind: "session",
+ env: { [SESSION_ENV]: session },
+ secret: session,
+ subject: String(subject),
+ };
}
+ const key = await read(KEY_FILE);
+ if (!key)
+ throw new Error(
+ `builtin needs ${SESSION_FILE} (session JWT) or ${KEY_FILE} (API key)`,
+ );
if (!/^tiny_[A-Za-z0-9_-]{8,}$/.test(key))
throw new Error(`${KEY_FILE} does not hold a tiny_ API key`);
- return { env: { [API_KEY_ENV]: key }, secret: key };
+ return {
+ kind: "key",
+ env: { [API_KEY_ENV]: key },
+ secret: key,
+ subject: null,
+ };
}
/** The active workspace directory, read from the running core. */
diff --git a/scripts/memory-scenarios/run.mjs b/scripts/memory-scenarios/run.mjs
index 4a923e8d14..a83e608f33 100644
--- a/scripts/memory-scenarios/run.mjs
+++ b/scripts/memory-scenarios/run.mjs
@@ -164,6 +164,7 @@ async function runEngine(engine, { opts, runDir, runId, findings, results }) {
let composio = null;
let wire = null;
let guardPlaced = false;
+ let builtinSubject = null;
let core = null;
let events = null;
const ledger = new Ledger();
@@ -193,7 +194,7 @@ async function runEngine(engine, { opts, runDir, runId, findings, results }) {
// Chat runs on the account's managed route (user decision); memory stays on
// the container. The key reaches the core only through the environment.
try {
- const cred = await builtinCredential();
+ const cred = await builtinCredential({ only: "key" });
secrets.push(cred.secret);
Object.assign(extraEnv, cred.env);
} catch (e) {
@@ -225,13 +226,17 @@ async function runEngine(engine, { opts, runDir, runId, findings, results }) {
// runs on its own. An API key is seeded as a plain profile write with no
// user-dir activation (boot_env.rs), so the root config is the active
// one. Verified after boot; the run aborts if it is not in effect.
- // Without a session the core boots into the pre-login user dir
- // (`users/local/config.toml`, READ in a run's core.log), so the gate goes
- // there as well as in the root config.
+ // Guard 0 goes in every config the core may treat as active: the root,
+ // the pre-login user dir (a key activates none) and, for a session, the
+ // account's own user dir (`users/<subject>`). Verified after boot.
const gated = BASE_CONFIG(["[scheduler_gate]", 'mode = "off"', ""]);
await fsp.writeFile(path.join(oh, "config.toml"), gated);
- await fsp.mkdir(path.join(oh, "users", "local"), { recursive: true });
- await fsp.writeFile(path.join(oh, "users", "local", "config.toml"), gated);
+ for (const id of ["local", ...(cred.subject ? [cred.subject] : [])]) {
+ await fsp.mkdir(path.join(oh, "users", id), { recursive: true });
+ await fsp.writeFile(path.join(oh, "users", id, "config.toml"), gated);
+ }
+ builtinSubject = cred.subject;
+ engineResults.credential = cred.kind;
}
const rpcLog = new JsonlLog(path.join(dir, "rpc.jsonl"), secrets);
@@ -373,7 +378,27 @@ async function runEngine(engine, { opts, runDir, runId, findings, results }) {
await setupLocal({ withKey: true });
} else {
// Guard 0 verified: the gate the account's config carries is off.
- const snap = await core.rpc("openhuman.config_get", {});
+ // A session activates the account's user dir asynchronously at boot:
+ // wait for it, then read the guard back from THAT config.
+ let snap = await core.rpc("openhuman.config_get", {});
+ if (builtinSubject) {
+ const want = path.join("users", builtinSubject, "config.toml");
+ snap = await waitFor(
+ async () => {
+ const s2 = await core.rpc("openhuman.config_get", {});
+ return String(s2?.config_path ?? "").endsWith(want) ? s2 : null;
+ },
+ {
+ timeoutMs: 60_000,
+ intervalMs: 500,
+ what: "the account's user dir active",
+ },
+ ).catch(() => {
+ throw new Error(
+ "ABORT builtin: the session did not activate the account's user dir",
+ );
+ });
+ }
const mode = pick(snap, "config.scheduler_gate.mode");
if (mode !== "off")
throw new Error(