Skip to content

Open-source platform for 3D AI agents. Turn text or a photo into a rigged, animated GLB avatar, give it an LLM brain, memory and a wallet, and embed it anywhere with one web component. Remote MCP server, x402 payments, Three.js, browser-native.

test(x402): extend the a2a spend-hardening proof and retire the cron03 probe

nirholas017d86416d84
diff --git a/package.json b/package.json
index fc8d72e4c..ab36239d5 100644
--- a/package.json
+++ b/package.json
@@ -361,6 +361,7 @@
"clean:worktrees": "node scripts/clean-deploy-worktrees.mjs",
"check:browser-graph": "node scripts/check-browser-graph.mjs",
"prove:guards": "node scripts/prove-guards.mjs",
+ "prove:a2a-spend": "node scripts/a2a-spend-hardening-proof.mjs",
"capture:docs-world": "node scripts/capture-docs-media.mjs",
"check:runnable-docs": "node scripts/check-runnable-docs.mjs",
"capture:doc-media": "node scripts/capture-doc-media.mjs",
diff --git a/scripts/a2a-spend-hardening-proof.mjs b/scripts/a2a-spend-hardening-proof.mjs
index 77cf93b09..bc62264a0 100644
--- a/scripts/a2a-spend-hardening-proof.mjs
+++ b/scripts/a2a-spend-hardening-proof.mjs
@@ -132,10 +132,26 @@ function startShim(pg) {
const shimPath = '/tmp/a2a-spend-proof-shim.mjs';
fs.writeFileSync(shimPath, `
import http from 'node:http';
-import pg from ${JSON.stringify(path.join(root, 'node_modules/pg/index.js'))};
+import pg from ${JSON.stringify(path.join(root, 'node_modules/pg/esm/index.mjs'))};
const client = new pg.Client(${JSON.stringify(PG_URL)});
-client.setTypeParser = undefined;
await client.connect();
+// Raw text rows would break the guard SQL: a float8 cap parameter that comes
+// back as the string '1' makes Postgres compare 'double > text', and every cap
+// silently passes. Serialize per column type so the HTTP response carries real
+// JSON values the way Neon's own endpoint does. bool(16), ints(20,21,23,26),
+// floats(700,701,1700), and json/jsonb(114,3802) get typed values; everything
+// else stays text (uuid, timestamptz, ...) exactly like production.
+const NUMERIC_OIDS = new Set([700, 701, 1700]);
+const INT_OIDS = new Set([20, 21, 23, 26]);
+const JSON_OIDS = new Set([114, 3802]);
+function coerce(value, oid) {
+ if (value === null || value === undefined) return null;
+ if (oid === 16) return value === 't' || value === 'true';
+ if (INT_OIDS.has(oid)) { const n = BigInt(value); return n <= 9007199254740991n && n >= -9007199254740991n ? Number(n) : value; }
+ if (NUMERIC_OIDS.has(oid)) return Number(value);
+ if (JSON_OIDS.has(oid)) { try { return JSON.parse(value); } catch { return value; } }
+ return value;
+}
http.createServer(async (req, res) => {
if (req.method !== 'POST') { res.writeHead(405); res.end(); return; }
let body = '';
@@ -146,10 +162,11 @@ http.createServer(async (req, res) => {
text: query, values: params || [], rowMode: 'array',
types: { getTypeParser: () => (v) => v },
});
+ const rows = r.rows.map((row) => row.map((v, i) => coerce(v, r.fields[i].dataTypeID)));
res.writeHead(200, { 'content-type': 'application/json' });
res.end(JSON.stringify({
fields: r.fields.map((f) => ({ name: f.name, dataTypeID: f.dataTypeID })),
- rows: r.rows, rowCount: r.rowCount, command: r.command,
+ rows, rowCount: r.rowCount, command: r.command,
}));
} catch (e) {
res.writeHead(500, { 'content-type': 'application/json' });
@@ -226,9 +243,25 @@ async function main() {
const pg = await loadPg();
let db = null;
+ const dbQuery = async (text, params) => {
+ if (!db) throw new Error('db not connected');
+ try {
+ return await db.query(text, params);
+ } catch (e) {
+ // The container or the connection can drop mid-run; reconnect once and retry.
+ if (/terminated|ECONNRESET|Connection/i.test(String(e?.message))) {
+ try { db.end().catch(() => {}); } catch { /* gone */ }
+ db = new pg.Client(PG_URL);
+ await db.connect();
+ return await db.query(text, params);
+ }
+ throw e;
+ }
+ };
await waitFor(async () => {
try {
const c = new pg.Client(PG_URL);
+ c.on('error', () => {});
await c.connect();
await c.query('select 1');
db = c;
@@ -259,21 +292,26 @@ async function main() {
const mustHave = ['users', 'sessions', 'csrf_tokens', 'agent_identities', 'agent_custody_events'];
const missing = [];
for (const t of mustHave) {
- const { rows } = await db.query('select to_regclass($1) AS r', [`public.${t}`]);
+ const { rows } = await dbQuery('select to_regclass($1) AS r', [`public.${t}`]);
if (!rows[0].r) missing.push(t);
}
check('schema applied', applied > 0 && missing.length === 0,
`${applied} files applied, ${skipped.length} skipped from drift; required tables present`);
if (missing.length) throw new Error(`required tables missing: ${missing.join(', ')}`);
- // Seed the proof agent (library phase). Direct SQL, real rows.
- const { rows: [u1] } = await db.query(
+ // Seed the proof agent (library phase). Direct SQL, real rows. The anomaly
+ // guard (the wallet's behavioral immune system) is disabled on proof agents:
+ // it auto-freezes a fresh wallet's first out-of-pattern spend, which is the
+ // correct production behavior but would freeze the harness mid-proof. The
+ // anomaly guard has its own dedicated test coverage; here it is noise.
+ const ANOMALY_OFF = { enabled: false };
+ const { rows: [u1] } = await dbQuery(
`insert into users (email, password_hash, email_verified) values ($1, 'x', true) returning id`,
[`proof-lib-${Date.now()}@proof.local`],
);
- const { rows: [a1] } = await db.query(
- `insert into agent_identities (user_id, name) values ($1, $2) returning id`,
- [u1.id, 'spend-proof-agent'],
+ const { rows: [a1] } = await dbQuery(
+ `insert into agent_identities (user_id, name, meta) values ($1, $2, jsonb_build_object('anomaly', $3::jsonb)) returning id`,
+ [u1.id, 'spend-proof-agent', JSON.stringify(ANOMALY_OFF)],
);
const agentId = a1.id;
pass('proof user + agent seeded', `agent ${agentId}`);
@@ -296,26 +334,39 @@ async function main() {
import { reserveSpendUsd, enforceSpendLimit, updateCustodyEvent, releaseSpendReservation,
recordCustodyEvent, SpendLimitError } from ${JSON.stringify(path.join(root, 'api/_lib/agent-trade-guards.js'))};
let buf = '';
+// Serialize RPCs: the shim runs one pg client, and pg deprecated overlapping
+// queries on a single connection. Each guard call is a single SQL statement
+// anyway; the concurrency this proof cares about is the DATABASE advisory lock
+// across statements, not socket overlap inside the harness.
+const queue = [];
+let busy = false;
+async function drain() {
+ if (busy) return;
+ busy = true;
+ while (queue.length) {
+ const { id, fn, args } = queue.shift();
+ try {
+ const out = await ({ reserveSpendUsd, enforceSpendLimit, updateCustodyEvent,
+ releaseSpendReservation, recordCustodyEvent })[fn](...(args || []));
+ process.stdout.write(JSON.stringify({ id, ok: true, out: out ?? null }) + '\\n');
+ } catch (e) {
+ process.stdout.write(JSON.stringify({ id, ok: false, err: {
+ name: e?.name, code: e?.code || null, status: e?.status || null,
+ message: String(e?.message || e), detail: e?.detail || null,
+ } }) + '\\n');
+ }
+ }
+ busy = false;
+}
process.stdin.on('data', (d) => {
buf += d;
let i;
while ((i = buf.indexOf('\\n')) >= 0) {
const line = buf.slice(0, i); buf = buf.slice(i + 1);
if (!line.trim()) continue;
- (async () => {
- const { id, fn, args } = JSON.parse(line);
- try {
- const out = await ({ reserveSpendUsd, enforceSpendLimit, updateCustodyEvent,
- releaseSpendReservation, recordCustodyEvent })[fn](...(args || []));
- process.stdout.write(JSON.stringify({ id, ok: true, out: out ?? null }) + '\\n');
- } catch (e) {
- process.stdout.write(JSON.stringify({ id, ok: false, err: {
- name: e?.name, code: e?.code || null, status: e?.status || null,
- message: String(e?.message || e), detail: e?.detail || null,
- } }) + '\\n');
- }
- })();
+ queue.push(JSON.parse(line));
}
+ drain();
});
`);
const worker = spawn('node', ['--import', preloadPath, workerPath], {
@@ -367,9 +418,13 @@ process.stdin.on('data', (d) => {
await call('updateCustodyEvent', reservationId, { status: 'ok', signature: sig });
};
const setLimits = async (patch) => {
- await db.query(
- `update agent_identities set meta = jsonb_set(coalesce(meta,'{}'::jsonb), '{spend_limits}', $2::jsonb) where id = $1`,
- [agentId, JSON.stringify(patch)],
+ await dbQuery(
+ `update agent_identities set meta =
+ jsonb_set(
+ jsonb_set(coalesce(meta,'{}'::jsonb), '{spend_limits}', $2::jsonb),
+ '{anomaly}', $3::jsonb
+ ) where id = $1`,
+ [agentId, JSON.stringify(patch), JSON.stringify(ANOMALY_OFF)],
);
};
@@ -417,9 +472,9 @@ process.stdin.on('data', (d) => {
step('3. per_counterparty_daily_usd - concentrated-drain ceiling');
{
// Fresh agent so the wallet-wide cap never fires first.
- const { rows: [a2] } = await db.query(
- `insert into agent_identities (user_id, name) values ($1, $2) returning id`,
- [u1.id, 'spend-proof-agent-cp'],
+ const { rows: [a2] } = await dbQuery(
+ `insert into agent_identities (user_id, name, meta) values ($1, $2, jsonb_build_object('anomaly', $3::jsonb)) returning id`,
+ [u1.id, 'spend-proof-agent-cp', JSON.stringify(ANOMALY_OFF)],
);
const cpAgent = a2.id;
for (const usd of [0.6, 0.3]) {
@@ -428,10 +483,11 @@ process.stdin.on('data', (d) => {
asset: 'USDC', usd, destination: PEER_A, status: 'ok', meta: { proof: 'backfill' },
});
}
- await db.query(
+ await dbQuery(
`update agent_identities set meta = jsonb_set(coalesce(meta,'{}'::jsonb), '{spend_limits}', $2::jsonb) where id = $1`,
[cpAgent, JSON.stringify({ daily_usd: null, per_tx_usd: null, per_counterparty_daily_usd: 1, withdraw_allowlist: [], frozen: false, require_capabilities: false })],
);
+ // (anomaly already disabled at insert time)
const spendCp = (over = {}) => ({
agentId: cpAgent, userId: u1.id, category: 'x402', usdValue: 0.2,
destination: PEER_A, network: 'mainnet', asset: 'USDC', ...over,
@@ -449,15 +505,19 @@ process.stdin.on('data', (d) => {
step('4. kill switch - frozen halts every autonomous path immediately');
{
// Fresh agent, generous caps, no history: only the freeze can block.
- const { rows: [a3] } = await db.query(
- `insert into agent_identities (user_id, name) values ($1, $2) returning id`,
- [u1.id, 'spend-proof-agent-kill'],
+ const { rows: [a3] } = await dbQuery(
+ `insert into agent_identities (user_id, name, meta) values ($1, $2, jsonb_build_object('anomaly', $3::jsonb)) returning id`,
+ [u1.id, 'spend-proof-agent-kill', JSON.stringify(ANOMALY_OFF)],
);
const killAgent = a3.id;
const open = { daily_usd: 1000, per_tx_usd: 1000, per_counterparty_daily_usd: null, withdraw_allowlist: [], frozen: false, require_capabilities: false };
- await db.query(
- `update agent_identities set meta = jsonb_set(coalesce(meta,'{}'::jsonb), '{spend_limits}', $2::jsonb) where id = $1`,
- [killAgent, JSON.stringify(open)],
+ await dbQuery(
+ `update agent_identities set meta =
+ jsonb_set(
+ jsonb_set(coalesce(meta,'{}'::jsonb), '{spend_limits}', $2::jsonb),
+ '{anomaly}', $3::jsonb
+ ) where id = $1`,
+ [killAgent, JSON.stringify(open), JSON.stringify(ANOMALY_OFF)],
);
const spendKill = (over = {}) => ({
agentId: killAgent, userId: u1.id, category: 'x402', usdValue: 0.5,
@@ -467,7 +527,7 @@ process.stdin.on('data', (d) => {
check('control: unfrozen agent spends fine', !before.thrown && !!before.out?.reservationId, before.thrown?.message);
if (before.out?.reservationId) await call('releaseSpendReservation', before.out.reservationId, 'proof_cleanup');
- await setLimitsOn(db, killAgent, { ...open, frozen: true });
+ await setLimitsOn(dbQuery, killAgent, { ...open, frozen: true });
for (const category of ['x402', 'trade', 'snipe']) {
const blocked = await callErr('reserveSpendUsd', spendKill({ category }));
check(`frozen blocks category=${category} instantly`, blocked.thrown?.code === 'wallet_frozen',
@@ -478,7 +538,7 @@ process.stdin.on('data', (d) => {
ownerSweep.thrown?.message || 'a freeze never traps the owner funds');
if (ownerSweep.out?.reservationId) await call('releaseSpendReservation', ownerSweep.out.reservationId, 'proof_cleanup');
- await setLimitsOn(db, killAgent, { ...open, frozen: false });
+ await setLimitsOn(dbQuery, killAgent, { ...open, frozen: false });
const after = await callErr('reserveSpendUsd', spendKill());
check('unfreeze resumes spending', !after.thrown && !!after.out?.reservationId, after.thrown?.message);
if (after.out?.reservationId) await call('releaseSpendReservation', after.out.reservationId, 'proof_cleanup');
@@ -499,7 +559,7 @@ process.stdin.on('data', (d) => {
if (!r.ok) throw new Error(`receipt seeding reserve blocked unexpectedly: ${r.err?.message}`);
await confirm(r.out.reservationId, sigs[i]);
}
- const seeded = await db.query(
+ const seeded = await dbQuery(
`select count(*)::int as n from agent_custody_events where agent_id = $1 and event_type = 'spend' and status = 'ok'`,
[agentId],
);
@@ -566,7 +626,7 @@ process.stdin.on('data', (d) => {
// The frozen agent must be unable to spend: drive a REAL reserve through
// the live meta (read back from the DB the server just wrote).
- const { rows: [liveRow] } = await db.query('select meta from agent_identities where id = $1', [liveAgentId]);
+ const { rows: [liveRow] } = await dbQuery('select meta from agent_identities where id = $1', [liveAgentId]);
const frozenAttempt = await callErr('reserveSpendUsd', {
agentId: liveAgentId, userId: null, meta: liveRow.meta, category: 'x402',
usdValue: 1, destination: PEER_A, network: 'mainnet', asset: 'USDC',
@@ -636,8 +696,8 @@ process.stdin.on('data', (d) => {
}
}
-async function setLimitsOn(db, agentId, limits) {
- await db.query(
+async function setLimitsOn(dbQuery, agentId, limits) {
+ await dbQuery(
`update agent_identities set meta = jsonb_set(coalesce(meta,'{}'::jsonb), '{spend_limits}', $2::jsonb) where id = $1`,
[agentId, JSON.stringify(limits)],
);
diff --git a/scripts/cron03-probe.mjs b/scripts/cron03-probe.mjs
deleted file mode 100644
index 9432c0196..000000000
--- a/scripts/cron03-probe.mjs
+++ /dev/null
@@ -1,85 +0,0 @@
-// api-cron-03 probe: load each batch handler through its true import graph and
-// invoke it with synthetic req/res (the same wiring server/index.mjs uses).
-// CRON_SECRET is a session-local value so requireCron passes only when we present it.
-process.env.CRON_SECRET = 'cron03-audit-secret';
-process.env.DATABASE_URL = process.env.CRON03_AUDIT_DATABASE_URL || 'postgres://127.0.0.1:5545/cron03';
-// Point app origin at loopback so outbound HTTP (forge-smoke, forge-seed submits)
-// fails fast instead of hanging on real HTTPS, and keep every spend lane in its
-// clean-skip path. No lane is configured here, so no real spend can occur.
-process.env.APP_ORIGIN = 'http://127.0.0.1:9';
-
-const files = [
- 'api/cron/forge-seed-cron.js',
- 'api/cron/forge-smoke.js',
- 'api/cron/forge-thumbnail-backfill.js',
- 'api/cron/free-model-audit.js',
- 'api/cron/garment-catalog-audit.js',
- 'api/cron/garment-job-sweep.js',
- 'api/cron/gcp-burn-report.js',
- 'api/cron/gmgn-seed.js',
- 'api/cron/gpu-keepwarm.js',
- 'api/cron/intel-learn.js',
-];
-
-function makeRes() {
- return {
- statusCode: 200,
- headers: {},
- _body: '',
- setHeader(k, v) { this.headers[String(k).toLowerCase()] = v; },
- getHeader(k) { return this.headers[String(k).toLowerCase()]; },
- writeHead(s, h) { this.statusCode = s; if (h) for (const [k, v] of Object.entries(h)) this.setHeader(k, v); return this; },
- write(c) { this._body += c; },
- end(c) { if (c) this._body += c; this._done(this); },
- json(o) { this._body = JSON.stringify(o); this._done(this); },
- };
-}
-function makeReq({ method = 'GET', url = '/', headers = {} } = {}) {
- return {
- method,
- url,
- headers,
- socket: { remoteAddress: '127.0.0.1' },
- on() {}, once() {}, removeListener() {},
- };
-}
-
-async function call(handler, req) {
- const res = makeRes();
- const done = new Promise((resolve) => { res._done = resolve; });
- try { await handler(req, res); } catch (err) {
- res.statusCode = -1;
- res._body = 'THROW: ' + (err?.message || String(err));
- }
- if (!res.headersSent && !res.writableEnded && res._done) {
- // If handler never ended, wait briefly for async completion.
- await Promise.race([done, new Promise((r) => setTimeout(r, 100))]);
- }
- let parsed = null;
- try { parsed = JSON.parse(res._body); } catch { /* non-JSON */ }
- return { status: res.statusCode, body: parsed ?? res._body.slice(0, 200) };
-}
-
-const secret = process.env.CRON_SECRET;
-for (const f of files) {
- const mod = await import('/workspaces/three.ws/' + f);
- const handler = mod.default;
- const name = f.replace('api/cron/', '').replace('.js', '');
-
- // 1. No auth -> must be 401 (fail-closed).
- const noauth = await call(handler, makeReq({ url: `/api/cron/${name}` }));
- // 2. Wrong secret -> 401.
- const wrong = await call(handler, makeReq({ url: `/api/cron/${name}`, headers: { authorization: 'Bearer nope' } }));
- // 3. Valid secret -> success path (200).
- const ok = await call(handler, makeReq({ url: `/api/cron/${name}`, headers: { authorization: `Bearer ${secret}` } }));
- // 4. Wrong method -> 405.
- const m405 = await call(handler, makeReq({ method: 'DELETE', url: `/api/cron/${name}`, headers: { authorization: `Bearer ${secret}` } }));
-
- const short = (r) => typeof r.body === 'string' ? r.body : JSON.stringify(r.body).slice(0, 160);
- console.log(`\n=== ${name} ===`);
- console.log(` noauth -> ${noauth.status} ${short(noauth)}`);
- console.log(` wrongkey -> ${wrong.status} ${short(wrong)}`);
- console.log(` authed -> ${ok.status} ${short(ok)}`);
- console.log(` method -> ${m405.status} ${short(m405)}`);
-}
-process.exit(0);