feat(reconstruction): evaluate end-to-end readiness

This commit is contained in:
morluto
2026-07-24 13:18:10 +00:00
parent 9d48ca788c
commit 38ff7418ef
21 changed files with 2578 additions and 0 deletions
@@ -0,0 +1,23 @@
const {
app,
BrowserWindow,
ipcMain,
shell,
utilityProcess,
} = require("electron");
const windows = new Set();
app.on("open-url", (_event, url) => {
for (const window of windows) window.webContents.send("deep-link", url);
});
app.whenReady().then(() => {
const window = new BrowserWindow({
webPreferences: { preload: require.resolve("./preload.js") },
});
windows.add(window);
window.loadFile("renderer.html");
utilityProcess.fork(require.resolve("./utility.js"));
ipcMain.handle("readiness:echo", (_event, value) => ({ value }));
window.webContents.on("render-process-gone", () => window.reload());
shell.openExternal("https://blocked.invalid").catch(() => undefined);
});
@@ -0,0 +1,5 @@
{
"name": "rea-readiness-electron",
"version": "1.0.0",
"main": "main.js"
}
@@ -0,0 +1,6 @@
const { contextBridge, ipcRenderer } = require("electron");
contextBridge.exposeInMainWorld("readiness", {
echo: (value) => ipcRenderer.invoke("readiness:echo", value),
onDeepLink: (listener) => ipcRenderer.on("deep-link", listener),
});
@@ -0,0 +1,8 @@
<!doctype html>
<html lang="en">
<body>
<button id="run">Run</button>
<output id="result"></output>
<script src="./renderer.js"></script>
</body>
</html>
@@ -0,0 +1,7 @@
document.querySelector("#run").addEventListener("click", async () => {
document.querySelector("#result").textContent =
await window.readiness.echo("renderer");
});
window.readiness.onDeepLink((_event, url) => {
document.querySelector("#result").textContent = url;
});
@@ -0,0 +1,5 @@
const { parentPort } = require("electron");
parentPort.on("message", (event) => {
parentPort.postMessage({ echoed: event.data });
});
@@ -0,0 +1,34 @@
import { spawn } from "node:child_process";
import { readFile, writeFile } from "node:fs/promises";
import { createServer } from "node:http";
import process from "node:process";
import readline from "node:readline";
const configurationPath =
process.env.REA_READINESS_CONFIG ?? "./readiness-config.json";
const configuration = JSON.parse(
await readFile(configurationPath, "utf8").catch(() => "{}"),
);
const prompt = readline.createInterface({
input: process.stdin,
output: process.stdout,
});
process.stdout.write("\u001b[?1049h");
const answer = await new Promise((resolve) =>
prompt.question("readiness> ", resolve),
);
prompt.close();
const child = spawn(process.execPath, ["-e", "process.stdout.write('child')"]);
const server = createServer((request, response) => {
response.end(JSON.stringify({ path: request.url, answer }));
});
server.listen(Number(configuration.port ?? 0), "127.0.0.1");
process.on("SIGTERM", () => {
child.kill("SIGTERM");
server.close();
process.stdout.write("\u001b[?1049l");
});
await writeFile(
configurationPath,
JSON.stringify({ ...configuration, lastAnswer: answer }),
);
@@ -0,0 +1,6 @@
{
"name": "rea-readiness-javascript-cli",
"version": "1.0.0",
"type": "module",
"bin": "index.js"
}
+163
View File
@@ -0,0 +1,163 @@
import { describe, expect, it } from "vitest";
import { RECONSTRUCTION_READINESS_EXAMPLE } from "../src/contracts/reconstructionReadinessExample.js";
import { createReconstructionReadinessReport } from "../src/domain/reconstructionReadiness.js";
import type { ReconstructionReadinessInput } from "../src/domain/reconstructionReadinessSchemas.js";
const input = (
overrides: Partial<ReconstructionReadinessInput> = {},
): ReconstructionReadinessInput => ({
...structuredClone(RECONSTRUCTION_READINESS_EXAMPLE),
...overrides,
});
describe("reconstruction readiness report", () => {
it("passes the complete public-contract journey deterministically", () => {
const first = createReconstructionReadinessReport(input());
const second = createReconstructionReadinessReport(input());
expect(first.status).toBe("pass");
expect(first.summary).toMatchObject({
required_stages: 9,
passed_required_stages: 9,
findings: 0,
});
expect(first.report_id).toBe(second.report_id);
expect(first.report_digest).toBe(second.report_digest);
expect(first.metrics).toMatchObject({
first_valid_workflow_selection_rate: 1,
incompatible_provider_invocations: 0,
false_equivalence_count: 0,
unowned_obligation_count: 0,
cleanup_leak_count: 0,
nondeterministic_replay_count: 0,
});
});
it("rejects equivalence over truncated concurrent evidence", () => {
const value = input();
const comparison = value.comparisons[0];
if (comparison === undefined) throw new TypeError("Missing comparison");
value.comparisons[0] = {
...comparison,
schedule_semantics: "total-order",
truncated: true,
};
const report = createReconstructionReadinessReport(value);
expect(report.status).toBe("fail");
expect(report.findings.map(({ code }) => code)).toEqual(
expect.arrayContaining([
"false-equivalence",
"unsafe-concurrency-semantics",
]),
);
expect(report.metrics.false_equivalence_count).toBe(1);
});
it("keeps green stages unknown while one reconstruction obligation is open", () => {
const value = input();
value.obligation_ledger = {
...value.obligation_ledger,
status: "open",
summary: {
...value.obligation_ledger.summary,
verified: 0,
required_open: 1,
},
};
const report = createReconstructionReadinessReport(value);
expect(report.status).toBe("unknown");
expect(report.stages).toContainEqual(
expect.objectContaining({
stage_id: "verify-reconstruction-closure",
status: "unknown",
}),
);
expect(report.findings).toContainEqual(
expect.objectContaining({ code: "reconstruction-closure-open" }),
);
});
it("fails typed operation outcomes that ordinary automation sees as success", () => {
const value = input();
const cliOutcome = value.operation_outcomes[2];
const mcpOutcome = value.operation_outcomes[3];
if (cliOutcome === undefined || mcpOutcome === undefined)
throw new TypeError("Missing operation outcome pair");
value.operation_outcomes[2] = {
...cliOutcome,
cli_exit_code: 0,
};
value.operation_outcomes[3] = {
...mcpOutcome,
mcp_status: "success",
};
const report = createReconstructionReadinessReport(value);
expect(report.status).toBe("fail");
expect(report.findings.map(({ code }) => code)).toEqual(
expect.arrayContaining(["cli-status-mismatch", "mcp-status-mismatch"]),
);
});
it("detects tampering against the exported source digest", () => {
const baseline = createReconstructionReadinessReport(input());
const replay = input({
replay: {
...RECONSTRUCTION_READINESS_EXAMPLE.replay,
expected_source_digest: baseline.source_digest,
},
});
expect(createReconstructionReadinessReport(replay).source_digest).toBe(
baseline.source_digest,
);
replay.identity.cli_version = "tampered";
const tampered = createReconstructionReadinessReport(replay);
expect(tampered.status).toBe("fail");
expect(tampered.findings).toContainEqual(
expect.objectContaining({ code: "replay-source-digest-mismatch" }),
);
});
it("fails a reconstruction candidate that delegates to the authority", () => {
const value = input();
const check = value.delegation_checks[0];
if (check === undefined) throw new TypeError("Missing delegation check");
value.delegation_checks[0] = {
...check,
delegates_to_authority: true,
};
const report = createReconstructionReadinessReport(value);
expect(report.status).toBe("fail");
expect(report.findings).toContainEqual(
expect.objectContaining({ code: "candidate-delegates-to-authority" }),
);
});
it("preserves an exact unsupported stage without collapsing it to pass", () => {
const value = input();
const stage = value.stages.find(
({ stage_id: stageId }) => stageId === "reactive-scenarios",
);
const check = stage?.checks[0];
if (stage === undefined || check === undefined)
throw new TypeError("Missing reactive stage");
stage.status = "unsupported";
check.status = "unsupported";
stage.capability_issue = "electron-runtime-unavailable";
stage.next_action = "Run on a host with the approved Electron runtime.";
const report = createReconstructionReadinessReport(value);
expect(report.status).toBe("unsupported");
expect(report.stages).toContainEqual(
expect.objectContaining({
stage_id: "reactive-scenarios",
status: "unsupported",
}),
);
});
});
+36
View File
@@ -0,0 +1,36 @@
import { execFile } from "node:child_process";
import { promisify } from "node:util";
import { describe, expect, it } from "vitest";
import { RECONSTRUCTION_READINESS_EXAMPLE } from "../src/contracts/reconstructionReadinessExample.js";
const execute = promisify(execFile);
describe("reconstruction readiness CLI parity", () => {
it("emits the same passing report through the public CLI", async () => {
const { stdout } = await execute(
process.execPath,
[
"scripts/rea.mjs",
"evaluate-reconstruction-readiness",
JSON.stringify(RECONSTRUCTION_READINESS_EXAMPLE),
"--json",
],
{
cwd: process.cwd(),
env: process.env,
maxBuffer: 32 * 1_024 * 1_024,
},
);
expect(JSON.parse(stdout)).toMatchObject({
operation: "evaluate_reconstruction_readiness",
predicate_type: "rea.reconstruction-readiness-report/v1",
normalized_result: {
schema: "ReconstructionReadinessReport",
status: "pass",
summary: { passed_required_stages: 9 },
},
});
}, 20_000);
});
+65
View File
@@ -0,0 +1,65 @@
import { Client, InMemoryTransport } from "@modelcontextprotocol/client";
import { describe, expect, it } from "vitest";
import { BinarySession } from "../src/application/BinarySession.js";
import { RECONSTRUCTION_READINESS_EXAMPLE } from "../src/contracts/reconstructionReadinessExample.js";
import { createServer } from "../src/server/createServer.js";
import { observed } from "./fixtures/analysisExecution.js";
describe("reconstruction readiness MCP parity", () => {
it("returns a passing compact result and retains the full report resource", async () => {
const session = new BinarySession(() => ({
execute: () => Promise.resolve(observed(null)),
close: () => Promise.resolve(),
}));
const server = createServer(session, session);
const client = new Client({ name: "readiness-test", version: "1" });
const [clientTransport, serverTransport] =
InMemoryTransport.createLinkedPair();
try {
await server.connect(serverTransport);
await client.connect(clientTransport);
const result = await client.callTool({
name: "evaluate_reconstruction_readiness",
arguments: RECONSTRUCTION_READINESS_EXAMPLE,
});
expect(result.isError).not.toBe(true);
expect(result.structuredContent).toMatchObject({
evidence_id: expect.stringMatching(/^ev_[a-f0-9]{64}$/u),
result: {
schema: "ReconstructionReadinessReport",
status: "pass",
summary: { passed_required_stages: 9 },
report_resource_uri: expect.stringMatching(
/^rea:\/\/evidence\/ev_[a-f0-9]{64}\/reconstruction-readiness-report$/u,
),
},
});
expect(result.structuredContent).not.toHaveProperty("result.snapshot");
expect(result.structuredContent).not.toHaveProperty("result.stages");
const content = result.structuredContent;
if (
content === undefined ||
content === null ||
typeof content !== "object" ||
!("evidence_id" in content) ||
typeof content.evidence_id !== "string"
)
throw new TypeError("Missing readiness Evidence ID");
const resource = await client.readResource({
uri: `rea://evidence/${content.evidence_id}/reconstruction-readiness-report`,
});
expect(resource.contents[0]).toEqual(
expect.objectContaining({
text: expect.stringContaining(
'"schema": "ReconstructionReadinessReport"',
),
}),
);
} finally {
await client.close();
await server.close();
await session.close();
}
});
});