Skip to content

armgit / rea

Public Imported from GitHub
0

Reverse engineer anything with agents, from app behavior down to native binaries.

fix(evidence): classify noncanonical bundles at the input boundary

morluto3a34ad37bdb7
diff --git a/src/application/ReconstructionObligationLedgerService.ts b/src/application/ReconstructionObligationLedgerService.ts
index e0b1f160a..cffee6330 100644
--- a/src/application/ReconstructionObligationLedgerService.ts
+++ b/src/application/ReconstructionObligationLedgerService.ts
@@ -2,6 +2,7 @@ import { z } from "zod";
import { AnalysisProtocolError } from "../domain/analysisErrorCore.js";
import type { AnalysisError } from "../domain/analysisErrorBase.js";
+import { NonCanonicalEvidenceBundleError } from "../domain/evidenceBundle.js";
import { createEvidence, type Evidence } from "../domain/evidence.js";
import { jsonObjectSchema, jsonValueSchema } from "../domain/jsonValue.js";
import {
@@ -45,6 +46,8 @@ export const buildReconstructionObligationLedgerEvidenceValidated = (
});
return ok(createLedgerEvidence(input, ledger));
} catch (cause: unknown) {
+ if (cause instanceof NonCanonicalEvidenceBundleError)
+ return err(workflowInputError(OPERATION, cause));
if (cause instanceof z.ZodError)
return err(workflowInputError(OPERATION, cause));
return err(
diff --git a/src/domain/evidenceBundle.test.ts b/src/domain/evidenceBundle.test.ts
index d2f90cbf6..1936e5046 100644
--- a/src/domain/evidenceBundle.test.ts
+++ b/src/domain/evidenceBundle.test.ts
@@ -5,6 +5,7 @@ import { ANALYSIS_SNAPSHOT_TARGET } from "./analysisSnapshot.fixture.js";
import {
createEvidenceBundle,
createImmutableEvidenceBundle,
+ NonCanonicalEvidenceBundleError,
describeEvidenceBundleFailure,
evidenceBundleForTarget,
parseEvidenceBundle,
@@ -20,6 +21,23 @@ const targetDigest = "a".repeat(64);
const foreignDigest = "b".repeat(64);
describe("evidence bundle failure classification", () => {
+ it("tags valid-shaped bundles that fail canonical ordering", () => {
+ const records = ["first", "second"].map((label) =>
+ createEvidence(undefined, provider, {
+ operation: "observe",
+ parameters: { label },
+ result: { label },
+ }),
+ );
+ const canonical = createEvidenceBundle(records);
+ expect(() =>
+ parseEvidenceBundle({
+ ...canonical,
+ records: [...canonical.records].reverse(),
+ }),
+ ).toThrow(NonCanonicalEvidenceBundleError);
+ });
+
it("treats only authenticated standalone Evidence as the wrong input kind", () => {
const record = createEvidence(undefined, provider, {
operation: "observe",
diff --git a/src/domain/evidenceBundle.ts b/src/domain/evidenceBundle.ts
index a70969b8e..6df730f0e 100644
--- a/src/domain/evidenceBundle.ts
+++ b/src/domain/evidenceBundle.ts
@@ -52,6 +52,14 @@ export const evidenceBundleSchema = z.strictObject({
export type EvidenceBundle = z.infer<typeof evidenceBundleSchema>;
const immutableBundles = new WeakMap<object, EvidenceBundle>();
+/** Caller input has valid shape but is not in authenticated canonical form. */
+export class NonCanonicalEvidenceBundleError extends TypeError {
+ constructor() {
+ super("Evidence bundle manifests are not canonical");
+ this.name = "NonCanonicalEvidenceBundleError";
+ }
+}
+
/** Build and authenticate a sealed bundle while reusing immutable Evidence records. */
export const createImmutableEvidenceBundle = (
records: readonly Evidence[],
@@ -307,7 +315,7 @@ const authenticateBundle = (
parsed.unknowns,
);
if (JSON.stringify(parsed) !== JSON.stringify(canonical))
- throw new TypeError("Evidence bundle manifests are not canonical");
+ throw new NonCanonicalEvidenceBundleError();
if (seal) {
freezeJsonSnapshot(canonical);
immutableBundles.set(canonical, canonical);
diff --git a/tests/boundary/cli/reconstructionObligationLedgerCli.test.ts b/tests/boundary/cli/reconstructionObligationLedgerCli.test.ts
index 63f3e94c4..eee6b3df1 100644
--- a/tests/boundary/cli/reconstructionObligationLedgerCli.test.ts
+++ b/tests/boundary/cli/reconstructionObligationLedgerCli.test.ts
@@ -3,11 +3,86 @@ import { promisify } from "node:util";
import { describe, expect, it } from "vitest";
+import { createEvidence } from "../../../src/domain/evidence.js";
import { createEvidenceBundle } from "../../../src/domain/evidenceBundle.js";
const execute = promisify(execFile);
describe("reconstruction obligation ledger CLI parity", () => {
+ it("reports non-canonical authenticated bundles as caller input", async () => {
+ const records = ["first", "second"].map((label) =>
+ createEvidence(
+ undefined,
+ { id: "fixture", name: "Fixture", version: "1" },
+ {
+ operation: "observe",
+ parameters: { label },
+ result: { label },
+ },
+ ),
+ );
+ const canonical = createEvidenceBundle(records);
+ const input = {
+ evidence_bundle: canonical,
+ reviewed_obligations: [],
+ manifest: { bindings: [], contradictions: [] },
+ };
+ const accepted = await execute(
+ process.execPath,
+ [
+ "scripts/rea.mjs",
+ "build-reconstruction-obligation-ledger",
+ JSON.stringify(input),
+ "--json",
+ ],
+ { cwd: process.cwd(), env: process.env, maxBuffer: 16 * 1_024 * 1_024 },
+ );
+ expect(JSON.parse(accepted.stdout)).toMatchObject({
+ normalized_result: { status: "unknown", summary: { total: 0 } },
+ });
+ input.evidence_bundle = {
+ ...canonical,
+ records: [...canonical.records].reverse(),
+ };
+ let stdout: string;
+ try {
+ await execute(
+ process.execPath,
+ [
+ "scripts/rea.mjs",
+ "build-reconstruction-obligation-ledger",
+ JSON.stringify(input),
+ "--json",
+ ],
+ { cwd: process.cwd(), env: process.env, maxBuffer: 16 * 1_024 * 1_024 },
+ );
+ throw new Error("Expected non-canonical bundle input to fail");
+ } catch (cause: unknown) {
+ if (
+ !(cause instanceof Error) ||
+ !("stdout" in cause) ||
+ typeof cause.stdout !== "string"
+ )
+ throw cause;
+ stdout = cause.stdout;
+ }
+ expect(JSON.parse(stdout)).toMatchObject({
+ code: "invalid_request",
+ category: "invalid_input",
+ retryable: true,
+ details: {
+ operation: "build_reconstruction_obligation_ledger",
+ issues: [
+ expect.objectContaining({
+ message: expect.stringContaining(
+ "Evidence bundle manifests are not canonical",
+ ),
+ }),
+ ],
+ },
+ });
+ }, 20_000);
+
it("builds the same Evidence-backed empty ledger through the CLI", async () => {
const input = {
evidence_bundle: createEvidenceBundle([]),
diff --git a/tests/boundary/mcp/reconstructionObligationLedgerMcp.test.ts b/tests/boundary/mcp/reconstructionObligationLedgerMcp.test.ts
index 653977a8f..f9b61b751 100644
--- a/tests/boundary/mcp/reconstructionObligationLedgerMcp.test.ts
+++ b/tests/boundary/mcp/reconstructionObligationLedgerMcp.test.ts
@@ -3,11 +3,79 @@ import { Client, InMemoryTransport } from "@modelcontextprotocol/client";
import { describe, expect, it } from "vitest";
import { createTestBinarySession } from "../../fixtures/binarySession.js";
+import { parseMcpToolError } from "../../fixtures/mcpToolError.js";
import { createEvidenceBundle } from "../../../src/domain/evidenceBundle.js";
+import { createEvidence } from "../../../src/domain/evidence.js";
import { createServer } from "../../../src/server/createServer.js";
import { observed } from "../../fixtures/analysisExecution.js";
describe("reconstruction obligation ledger MCP parity", () => {
+ it("returns canonical bundle failures as caller input errors", async () => {
+ const records = ["first", "second"].map((label) =>
+ createEvidence(
+ undefined,
+ { id: "fixture", name: "Fixture", version: "1" },
+ {
+ operation: "observe",
+ parameters: { label },
+ result: { label },
+ },
+ ),
+ );
+ const canonical = createEvidenceBundle(records);
+ const session = createTestBinarySession(() => ({
+ execute: () => Promise.resolve(observed(null)),
+ close: () => Promise.resolve(resultOk(null)),
+ }));
+ const server = createServer({ kind: "session", session });
+ const client = new Client({ name: "obligation-ledger-test", version: "1" });
+ const [clientTransport, serverTransport] =
+ InMemoryTransport.createLinkedPair();
+ try {
+ await server.connect(serverTransport);
+ await client.connect(clientTransport);
+ const request = (evidenceBundle: unknown) =>
+ client.callTool({
+ name: "build_reconstruction_obligation_ledger",
+ arguments: {
+ evidence_bundle: evidenceBundle,
+ reviewed_obligations: [],
+ manifest: { bindings: [], contradictions: [] },
+ },
+ });
+ const valid = await request(canonical);
+ expect(valid.isError).not.toBe(true);
+ expect(valid.structuredContent).toMatchObject({
+ normalized_result: { status: "unknown", summary: { total: 0 } },
+ });
+ const result = await request({
+ ...canonical,
+ records: [...canonical.records].reverse(),
+ });
+ expect(result.isError).toBe(true);
+ expect(parseMcpToolError(result)).toMatchObject({
+ error: {
+ code: "invalid_request",
+ category: "invalid_input",
+ details: {
+ operation: "build_reconstruction_obligation_ledger",
+ issues: [
+ expect.objectContaining({
+ message: expect.stringContaining(
+ "Evidence bundle manifests are not canonical",
+ ),
+ }),
+ ],
+ },
+ },
+ });
+ } finally {
+ await client.close();
+ await server.close();
+ await session.close();
+ }
+ });
+
it("returns the complete Evidence-backed ledger inline", async () => {
const session = createTestBinarySession(() => ({
execute: () => Promise.resolve(observed(null)),