Files
rea/experiments/atomic-codemode/verify-executor.mjs
T
f34425dc24 fix(experiment): enforce Code Mode payload budgets on guest errors
Reject oversized error delivery with explicit omission metadata, account for JSON escaping, and retain source observations and their lookup after failure. Reproduce and verify the real MCP path without claiming model-token savings from byte counts.

Co-authored-by: gensokyo-dev <342580529+gensokyo-dev@users.noreply.github.com>
Co-authored-by: Kaoru0822 <208281322+kaoru0822-kitauji@users.noreply.github.com>
2026-10-16 07:33:09 +00:00

89 lines
3.3 KiB
JavaScript

// Real MCP failure delivery, including source access after an oversized error.
import assert from "node:assert/strict";
import { writeFile } from "node:fs/promises";
import { resolve } from "node:path";
import { Client } from "@modelcontextprotocol/client";
import { StdioClientTransport } from "@modelcontextprotocol/client/stdio";
import {
createVerifierRun,
completeVerifierRun,
assertVerifierCleanupComplete,
} from "../../scripts/lib/verifier-run.mjs";
const [binary, output] = process.argv.slice(2);
const verifier = createVerifierRun();
const client = new Client({ name: "rea-executor-verifier", version: "0.1" });
const transport = new StdioClientTransport({
command: process.execPath,
args: [resolve("experiments/atomic-codemode/server.mjs")],
env: { ...process.env },
stderr: "pipe",
});
const observations = [],
failures = [];
const call = async (name, input = {}) => {
const result = await client.callTool(
{ name, arguments: input },
{ timeout: 180000 },
);
assert.equal(result._meta?.provenance?.untrusted_content, true);
const value = JSON.parse(result.content[0].text);
observations.push({
name,
input,
value,
encoded_utf8_bytes: Buffer.byteLength(result.content[0].text),
});
return value;
};
try {
await client.connect(transport);
for (const code of [
'throw new Error("x".repeat(40000))',
'throw new Error("\\0".repeat(6000))',
]) {
const result = await call("execute_javascript", { code });
assert.equal(result.ok, false);
assert.equal(result.error.code, "output_budget");
assert.equal(result.error.details.original_message_omitted, true);
assert.ok(observations.at(-1).encoded_utf8_bytes < 32768);
}
const ordinary = await call("execute_javascript", {
code: 'throw new Error("x".repeat(10000))',
});
assert.equal(ordinary.error.code, "javascript_error");
assert.ok(ordinary.error.message.includes("x".repeat(10000)));
const opened = await call("open_binary", { path: binary });
assert.equal(opened.ok, true);
const result = await call("execute_javascript", {
code: 'const r=await rea.strings({query:"incorrect header check"});if(!r.ok)throw new Error(JSON.stringify(r));throw new Error("x".repeat(40000));',
});
assert.equal(result.error.code, "output_budget");
assert.equal(result.context.lease, opened.context.lease);
assert.equal(result.evidence_ids.length, 1);
const source = await call("execute_javascript", {
code: `const r=await rea.evidence({evidence_id:${JSON.stringify(result.evidence_ids[0])},limit:64});if(!r.ok)throw new Error(JSON.stringify(r));return r.data;`,
});
assert.equal(source.ok, true);
assert.equal(source.value.source.evidence_id, result.evidence_ids[0]);
assert.ok(source.value.source.bytes > 0);
assert.ok(source.value.text.length > 0);
assert.equal((await call("close_binary")).ok, true);
} catch (error) {
failures.push(error.stack ?? String(error));
} finally {
try {
await client.close();
await transport.close();
} catch (error) {
failures.push(String(error));
}
}
const lifecycle = await completeVerifierRun(verifier);
assertVerifierCleanupComplete(lifecycle.process_lineage);
await writeFile(output, JSON.stringify({ observations, failures, lifecycle }), {
flag: "wx",
});
console.log(JSON.stringify({ output, failures, cleanup_verified: true }));
if (failures.length) process.exitCode = 1;