UNPKG

@sema-agent/core

Version:

Stateless, task-oriented AI agent core

374 lines (373 loc) • 21.1 kB
import assert from "node:assert/strict"; import { AsyncLocalStorage } from "node:async_hooks"; import { workflowScriptRunInput } from "./workflow-script-runner.js"; import { thrownText } from "../core/thrown-value.js"; const META = "export const meta = { name: 'conformance', description: 'sandbox escape probes' }\n"; const WITNESS_FN = "__probeWitness"; const WITNESS_PRELUDE = `function ${WITNESS_FN}(v) {\n` + ` if (v === null || v === undefined) return null\n` + ` var t = typeof v\n` + ` if (t === 'function') return { hostCapabilityReached: true, via: 'function' }\n` + ` if (t !== 'object') return null\n` + ` var names = ['pid', 'env', 'version', 'mainModule', 'binding']\n` + ` var hit = []\n` + ` for (var i = 0; i < names.length; i++) { try { if (names[i] in v) hit.push(names[i]) } catch (err) { } }\n` + ` return hit.length > 0 ? { hostCapabilityReached: true, via: 'object', keys: hit } : null\n` + `}\n`; const ESCAPE_BODIES = [ `return ${WITNESS_FN}(agent.constructor.constructor("return typeof process !== 'undefined' ? process : null")())`, `return ${WITNESS_FN}(log.constructor.constructor("return typeof process !== 'undefined' ? process : null")())`, `return ${WITNESS_FN}(budget.constructor.constructor("return typeof process !== 'undefined' ? process : null")())`, `return ${WITNESS_FN}(({}).constructor.constructor("return typeof process !== 'undefined' ? process : null")())`, `return ${WITNESS_FN}(args.constructor.constructor("return typeof require !== 'undefined' ? require : null")())`, `const r = await agent({ objective: 'x' }); return ${WITNESS_FN}((r ?? {}).constructor.constructor("return typeof require !== 'undefined' ? require : null")())`, `return ${WITNESS_FN}(typeof process !== 'undefined' ? process : null)`, `return ${WITNESS_FN}(typeof require !== 'undefined' ? require : null)`, `return ${WITNESS_FN}((typeof globalThis !== 'undefined' && globalThis.process) ? globalThis.process : null)`, ]; const ERROR_PATH_BODIES = [ `try { await agent({ objective: 'x' }); return null } catch (e) { return ${WITNESS_FN}(e.constructor.constructor("return typeof process !== 'undefined' ? process : null")()) }`, `try { await parallel([]); return null } catch (e) { return ${WITNESS_FN}(e.constructor.constructor("return typeof require !== 'undefined' ? require : null")()) }`, `try { await agent({}); return null } catch (e) { return ${WITNESS_FN}((e && e.constructor && e.constructor.constructor) ? e.constructor.constructor("return (typeof globalThis !== 'undefined' && globalThis.process) ? globalThis.process : null")() : null) }`, ]; const POLLUTION_BODIES = [ `return JSON.parse('{"__proto__":{"polluted":1}}')`, `const o = {}; Reflect.defineProperty(o, "__proto__", { value: { polluted: 1 }, enumerable: true, configurable: true }); return o`, `return { nested: JSON.parse('{"__proto__":{"polluted":1}}') }`, `return { constructor: { prototype: { polluted: 1 } } }`, `return [JSON.parse('{"__proto__":{"polluted":1}}')]`, ]; const SIGNAL_BODIES = [ `return ${WITNESS_FN}((typeof signal !== 'undefined' && signal) ? signal.constructor.constructor("return typeof process !== 'undefined' ? process : null")() : null)`, `return ${WITNESS_FN}((typeof input !== 'undefined' && input && input.signal) ? input.signal.constructor.constructor("return typeof process !== 'undefined' ? process : null")() : null)`, `try { return ${WITNESS_FN}((typeof signal !== 'undefined' && signal) ? (signal.addEventListener ? signal.addEventListener.constructor("return typeof process !== 'undefined' ? process : null")() : null) : null) } catch (e) { return null }`, ]; function placesOver(primitives) { return { open: async (_kind, slots) => ({ fanOut: "fan", slots: Array.from({ length: slots }, (_, i) => `slot.${i}`) }), agent: (_at, spec, opts) => primitives.agent(spec, opts), join: async () => undefined, }; } const runInput = (host, rest) => workflowScriptRunInput({ scriptSource: rest.scriptSource, primitives: host.primitives, scriptArgs: rest.scriptArgs, ...("signal" in rest ? { signal: rest.signal } : {}) }, host.fanOut); function benignHost() { const primitives = { agent: async () => ({ status: "completed", result: "ok" }), parallel: async (thunks) => Promise.all(thunks.map((t) => t())), pipeline: async (items) => items, phase: async (_title, body) => (body === undefined ? undefined : body()), log: () => { }, budget: { total: null, spent: () => 0, remaining: () => Infinity }, }; return { primitives, fanOut: placesOver(primitives) }; } function throwingHost() { const boom = () => { throw new Error("host-side primitive failure"); }; return { primitives: { agent: async () => boom(), parallel: async () => boom(), pipeline: async () => boom(), phase: async () => boom(), log: () => { }, budget: { total: null, spent: () => 0, remaining: () => Infinity }, }, fanOut: { open: async () => boom(), agent: async () => boom(), join: async () => boom() }, }; } const DANGEROUS_OWN_KEYS = new Set(["__proto__", "constructor", "prototype"]); function graphHasInjectedProtoKey(v, seen = new Set()) { if (v == null || typeof v !== "object") return false; if (seen.has(v)) return false; seen.add(v); for (const k of Object.getOwnPropertyNames(v)) { if (DANGEROUS_OWN_KEYS.has(k)) { const val = Object.getOwnPropertyDescriptor(v, k)?.value; if (val != null && typeof val === "object") return true; } } for (const k of Object.getOwnPropertyNames(v)) { if (DANGEROUS_OWN_KEYS.has(k)) continue; if (graphHasInjectedProtoKey(v[k], seen)) return true; } return false; } function looksLikeHostCapability(v) { if (v == null) return false; if (typeof v === "function") return true; if (typeof v === "object") { const o = v; return "pid" in o || "env" in o || "version" in o || "mainModule" in o || "binding" in o; } return false; } function witnessReportsHostCapability(v) { return typeof v === "object" && v !== null && v.hostCapabilityReached === true; } function reachedHostCapability(v) { return looksLikeHostCapability(v) || witnessReportsHostCapability(v); } const CANARY_MARKER = "workflow-conformance-canary"; let canarySeq = 0; const CANARY_BODY = `const c = args.canary\n` + `return { marker: '${CANARY_MARKER}', nonce: c.nonce, sum: c.x + c.y, joined: ['${CANARY_MARKER}', c.nonce].join(':') }`; async function runLivenessCanary(runner) { const nonce = `n${++canarySeq}`; const scriptArgs = { canary: { nonce, x: 17, y: 25 } }; let result; try { const out = await runner.run(runInput(benignHost(), { scriptSource: META + CANARY_BODY, scriptArgs })); result = out.result; } catch (err) { return { ok: false, detail: `run() REJECTED with: ${thrownText(err)}` }; } const expected = { marker: CANARY_MARKER, nonce, sum: 42, joined: `${CANARY_MARKER}:${nonce}` }; try { assert.deepEqual(result, expected); } catch { return { ok: false, detail: `run() returned ${JSON.stringify(result)} — expected ${JSON.stringify(expected)}` }; } return { ok: true, detail: "" }; } async function runProbeBody(runner, scriptSource, host, family, signal) { try { const out = await runner.run(runInput(host, { scriptSource, scriptArgs: { probe: true }, signal })); return { executed: true, result: out.result }; } catch (err) { const live = await runLivenessCanary(runner); assert.ok(live.ok, `${family}: the probe threw AND the runner then failed the liveness canary — this runner is BROKEN, ` + `not blocking, so nothing about its membrane was proven. probe: ${scriptSource.slice(META.length)} | ` + `probe threw: ${thrownText(err)} | canary: ${live.detail}`); return { executed: false }; } } export async function assertWorkflowPrimitivesWiring(runner) { assert.equal(runner.safeForUntrustedScripts, true, "wiring conformance targets a HARD runner (safeForUntrustedScripts must be true)"); const hostSeen = { phaseCalls: [], scopedBodyRan: false, logs: [] }; const primitives = { agent: async () => ({ status: "completed", result: "ok" }), parallel: async (thunks) => Promise.all(thunks.map((t) => t())), pipeline: async (items, ...stages) => { let out = items; for (const st of stages) { out = await Promise.all(out.map((x, i) => st(x, x, i))); } return out; }, phase: async (title, body) => { hostSeen.phaseCalls.push({ title, hasBody: body !== undefined }); if (body === undefined) return undefined; const out = await body(); if (title === "wiring-scoped") hostSeen.scopedBodyRan = true; return out; }, log: (message) => void hostSeen.logs.push(message), budget: { total: 1000, spent: () => 25, remaining: () => 975 }, }; const scriptSource = META + [ `const report = { typeofs: { agent: typeof agent, parallel: typeof parallel, pipeline: typeof pipeline, phase: typeof phase, log: typeof log, budgetRemaining: budget && typeof budget.remaining }, calls: {} }`, `log('wiring-probe-log')`, `report.calls.bareUndefined = (await phase('wiring-marker')) === undefined`, `let scopedRan = false`, `await phase('wiring-scoped', async () => { scopedRan = true })`, `report.calls.scopedRan = scopedRan`, `report.calls.budgetRemaining = budget.remaining()`, `const par = await parallel([async () => 1, async () => 2])`, `report.calls.parallelOk = Array.isArray(par) && par.length === 2`, `const pipe = await pipeline([1, 2], async (x) => x)`, `report.calls.pipelineOk = Array.isArray(pipe) && pipe.length === 2`, `const a = await agent({ objective: 'wiring' })`, `report.calls.agentOk = a && a.status === 'completed'`, `return report`, ].join("\n"); const out = await runner.run(runInput({ primitives, fanOut: placesOver(primitives) }, { scriptSource, scriptArgs: {} })); const report = out.result; assert.ok(report && report.typeofs && report.calls, "wiring probe returned no report — script body did not run"); for (const k of ["agent", "parallel", "pipeline", "phase", "log", "budgetRemaining"]) { assert.equal(report.typeofs[k], "function", `primitive "${k}" is not a function INSIDE the isolate — injection table incomplete (the missing-injection failure mode)`); } assert.equal(report.calls.bareUndefined, true, "bare phase(title) must return undefined in-isolate"); assert.equal(report.calls.scopedRan, true, "scoped phase body did not run in-isolate"); assert.equal(report.calls.budgetRemaining, 975, "budget.remaining() is not bridged to the live host accessor"); assert.equal(report.calls.parallelOk, true, "parallel() thunks not executed"); assert.equal(report.calls.pipelineOk, true, "pipeline() stages not executed"); assert.equal(report.calls.agentOk, true, "agent() result not bridged back"); assert.deepEqual(hostSeen.phaseCalls.map((c) => `${c.title}:${c.hasBody ? "scoped" : "bare"}`), ["wiring-marker:bare", "wiring-scoped:scoped"], "phase() calls did not reach the HOST primitive (bridge severed — phases would record [])"); assert.equal(hostSeen.scopedBodyRan, true, "the scoped body's completion did not round-trip through the host phase()"); assert.deepEqual(hostSeen.logs, ["wiring-probe-log"], "log() did not reach the HOST primitive"); } export async function assertWorkflowSandboxConformance(runner) { assert.equal(runner.safeForUntrustedScripts, true, "conformance is only meaningful for a HARD runner (safeForUntrustedScripts must be true)"); const live = await runLivenessCanary(runner); assert.ok(live.ok, `POSITIVE CONTROL FAILED — this runner did not execute the canary script and return its value, so the ` + `escape battery below cannot certify anything about it (a runner that runs nothing produces the same ` + `"nothing leaked" results as a perfect sandbox). ${live.detail}`); for (const escBody of ESCAPE_BODIES) { const out = await runProbeBody(runner, META + WITNESS_PRELUDE + escBody, benignHost(), "sandbox escape"); if (!out.executed) continue; assert.equal(reachedHostCapability(out.result), false, `sandbox escape REACHED a host capability via: ${escBody}`); } for (const errBody of ERROR_PATH_BODIES) { const out = await runProbeBody(runner, META + WITNESS_PRELUDE + errBody, throwingHost(), "error-path"); if (!out.executed) continue; assert.equal(reachedHostCapability(out.result), false, `error-path: a host-side primitive error leaked a host capability via: ${errBody}`); } for (const polBody of POLLUTION_BODIES) { const out = await runProbeBody(runner, META + polBody, benignHost(), "prototype pollution"); if (!out.executed) continue; assert.equal(graphHasInjectedProtoKey(out.result), false, `prototype pollution: the marshalled result carries an injected __proto__/constructor/prototype own key via: ${polBody}`); } const hostSignal = new AbortController().signal; for (const sigBody of SIGNAL_BODIES) { const out = await runProbeBody(runner, META + WITNESS_PRELUDE + sigBody, benignHost(), "signal-path", hostSignal); if (!out.executed) continue; assert.equal(reachedHostCapability(out.result), false, `signal-path: the host AbortSignal leaked a host capability via: ${sigBody}`); } } const DETERMINISM_FORBIDDEN_BODIES = [ `try { return Date.now() } catch { return "threw" }`, `try { return Math.random() } catch { return "threw" }`, `try { return new Date().getTime() } catch { return "threw" }`, `try { return new Date(0).constructor.now() } catch { return "threw" }`, ]; export async function assertWorkflowDeterminism(runner) { for (const detBody of DETERMINISM_FORBIDDEN_BODIES) { const scriptSource = META + detBody; let threw = false; let result; try { const out = await runner.run(runInput(benignHost(), { scriptSource, scriptArgs: { probe: true } })); result = out.result; } catch { threw = true; } assert.equal(threw || result === "threw", true, `determinism: ${detBody.trim().split("\n")[0]} did NOT throw — a clock/randomness read would silently bust resume`); } const out = await runner.run(runInput(benignHost(), { scriptSource: META + `return new Date(0).getTime()`, scriptArgs: { probe: true } })); assert.equal(out.result, 0, "determinism: DATED construction new Date(0) must stay available"); } const POSITIONS_BODY = [ `await parallel([() => agent({ objective: 'A' }), () => agent({ objective: 'B' })])`, `await pipeline(['x', 'y'], (_p, item) => agent({ objective: 'S1 ' + item }), (_p, item) => agent({ objective: 'S2 ' + item }))`, `await pipeline(['n'], (_p, item) => parallel([() => agent({ objective: 'N ' + item })]))`, `await Promise.all([pipeline(['c'], (_p, item) => agent({ objective: 'C1 ' + item }), (_p, item) => agent({ objective: 'C2 ' + item })), pipeline(['d'], (_p, item) => agent({ objective: 'D1 ' + item }), (_p, item) => agent({ objective: 'D2 ' + item }))])`, `await parallel([async () => 0])`, `await parallel([])`, `await parallel([() => { throw new Error('slot failed') }])`, `return await agent({ objective: 'Z' })`, ].join("\n"); const POSITIONS_LAST = "Z"; const POSITIONS_EXPECTED = [ ["A", "root/F0/s0"], ["B", "root/F0/s1"], ["S1 x", "root/P1/s0"], ["S2 x", "root/P1/s0"], ["S1 y", "root/P1/s1"], ["S2 y", "root/P1/s1"], ["N n", "root/P2/s0/F0/s0"], ["C1 c", "root/P3/s0"], ["C2 c", "root/P3/s0"], ["D1 d", "root/P4/s0"], ["D2 d", "root/P4/s0"], [POSITIONS_LAST, "root"], ]; export async function assertWorkflowPositionsCarried(runner) { const where = new AsyncLocalStorage(); const events = []; const fanOutsOn = new Map(); const placeOfFanOut = (chain, slots, kind) => { if (slots === 0) return undefined; const k = fanOutsOn.get(chain) ?? 0; fanOutsOn.set(chain, k + 1); return `${chain}/${kind === "pipeline" ? "P" : "F"}${k}`; }; const arrive = (spec, place) => { const objective = String(spec?.objective ?? spec); events.push({ kind: "call", objective, place }); return { status: "completed", result: `ok:${objective}` }; }; const tokens = new Map(); const opened = new Map(); let fanSeq = 0; const slotOf = (at) => { const slot = typeof at === "string" ? tokens.get(at) : undefined; assert.ok(slot !== undefined, `positions: the runner passed ${JSON.stringify(at)} as a slot, which no fanOut.open answered`); return slot; }; const settle = (f) => Promise.resolve().then(f).catch(() => null); const primitives = { agent: async (spec) => arrive(spec, where.getStore() ?? "root"), parallel: async (thunks) => { const site = placeOfFanOut(where.getStore() ?? "root", thunks.length, "parallel"); return Promise.all(thunks.map((t, i) => where.run(`${site}/s${i}`, () => settle(t)))); }, pipeline: async (items, ...stages) => { const site = placeOfFanOut(where.getStore() ?? "root", items.length, "pipeline"); return Promise.all(items.map((item, i) => where.run(`${site}/s${i}`, () => stages.reduce((acc, st) => acc.then((prev) => st(prev, item, i)), Promise.resolve(item)).catch(() => null)))); }, phase: async (_title, body) => (body === undefined ? undefined : body()), log: () => { }, budget: { total: null, spent: () => 0, remaining: () => Infinity }, }; const fanOut = { open: async (kind, slots, at) => { const parent = at === undefined ? undefined : slotOf(at); const site = placeOfFanOut(parent?.place ?? "root", slots, kind); const fan = `fan:${++fanSeq}`; const minted = Array.from({ length: slots }, (_, i) => `${fan}.${i}`); minted.forEach((t, i) => tokens.set(t, { place: `${site}/s${i}`, fan })); if (site !== undefined) { opened.set(fan, { parent: parent?.fan }); events.push({ kind: "open", fan }); } return { fanOut: fan, slots: minted }; }, agent: async (at, spec) => arrive(spec, at === undefined ? "root" : slotOf(at).place), join: async (fan) => { await new Promise((r) => setImmediate(r)); events.push({ kind: "join", fan }); }, }; await runner.run(workflowScriptRunInput({ scriptSource: META + POSITIONS_BODY, primitives, scriptArgs: {} }, fanOut)); const got = new Map(); for (const e of events) if (e.kind === "call") got.set(e.objective, e.place); for (const [objective, expected] of POSITIONS_EXPECTED) { const place = got.get(objective); assert.ok(place !== undefined, `positions: the call "${objective}" never reached the host`); assert.ok(!(place === "root" && expected !== "root"), `positions: the call "${objective}" arrived at the host with no slot (it was made in ${expected}) — a runner that runs parallel / pipeline in its own domain carries each slot's token (fanOut.agent, fanOut.open's place) or hands the thunks to the host's parallel / pipeline; without it a resume numbers these calls by arrival and re-runs completed ones`); assert.equal(place, expected, `positions: the call "${objective}" arrived in ${place}, made in ${expected}`); } const joinedAt = (fan) => events.findIndex((e) => e.kind === "join" && e.fan === fan); const lastAt = events.findIndex((e) => e.kind === "call" && e.objective === POSITIONS_LAST); for (const [fan, o] of opened) { const at = joinedAt(fan); assert.ok(at !== -1, `positions: the fan-out ${fan} was never joined — its join is the check a resume reads there`); assert.ok(at < lastAt, `positions: the fan-out ${fan} was joined only after the call the script makes once it settled — a runner joins a fan-out before the script sees it settle`); if (o.parent !== undefined) { assert.ok(at < joinedAt(o.parent), `positions: the fan-out ${fan} was joined only after the fan-out ${o.parent} it was opened in`); } } }