Skip to content

armgit / bla

Public Imported from GitHub
0

πŸ›‘οΈ BLA (Blind LLM Agents) β€” Adversarial defense shield that protects binaries, Electron apps, JS bundles, Python, and WebAssembly against AI reverse-engineering agents like REA. Poison -> Exhaust -> Hallucinate.

feat(mcp): stage C run lookup and REA observability tools

qybaihe8f9a3512a204
diff --git a/src/engine/rea-observability.ts b/src/engine/rea-observability.ts
new file mode 100644
index 0000000..68a9072
--- /dev/null
+++ b/src/engine/rea-observability.ts
@@ -0,0 +1,255 @@
+import { spawnSync } from "node:child_process";
+import { existsSync, readFileSync } from "node:fs";
+import { dirname, join, resolve } from "node:path";
+import { fileURLToPath } from "node:url";
+
+const MODULE_DIR = dirname(fileURLToPath(import.meta.url));
+export const BLA_PACKAGE_ROOT = resolve(MODULE_DIR, "../..");
+
+/** Resolve sibling REA checkout the same way scripts/verify-rea-constants.mjs does. */
+export function resolveReaRoot(override?: string): string {
+ const fromArg = (override ?? "").trim();
+ const fromEnv = (process.env.REA_ROOT ?? "").trim();
+ return resolve(fromArg || fromEnv || join(BLA_PACKAGE_ROOT, "..", "rea"));
+}
+
+export function reaCheckoutPresent(reaRoot = resolveReaRoot()): boolean {
+ return (
+ reaRoot !== BLA_PACKAGE_ROOT &&
+ existsSync(reaRoot) &&
+ existsSync(join(reaRoot, "src"))
+ );
+}
+
+export function reaDistReady(reaRoot = resolveReaRoot()): boolean {
+ return existsSync(join(reaRoot, "dist", "go", "GoBinaryImage.js"));
+}
+
+export type ReaObservabilityStatus = "pass" | "fail" | "unverified";
+
+export interface ReaDriftReport {
+ readonly schema: "bla-rea-drift/v1";
+ readonly status: ReaObservabilityStatus;
+ readonly reaRoot: string;
+ readonly blaRoot: string;
+ readonly exitCode: number | null;
+ readonly reason?: string;
+ readonly stdout: string;
+ readonly stderr: string;
+ /** True when this is not empirical red-team evidence. */
+ readonly notRedTeamEvidence: true;
+}
+
+/**
+ * Thin wrapper around `scripts/verify-rea-constants.mjs`.
+ * Absent REA β†’ explicit `unverified` (never a silent pass).
+ * Present REA β†’ spawn the harness; exit 0 β†’ pass, else fail.
+ */
+export function runReaDriftCheck(options: {
+ readonly reaRoot?: string;
+ readonly timeoutMs?: number;
+} = {}): ReaDriftReport {
+ const blaRoot = BLA_PACKAGE_ROOT;
+ const reaRoot = resolveReaRoot(options.reaRoot);
+ const base = {
+ schema: "bla-rea-drift/v1" as const,
+ reaRoot,
+ blaRoot,
+ notRedTeamEvidence: true as const,
+ };
+
+ if (!reaCheckoutPresent(reaRoot)) {
+ return {
+ ...base,
+ status: "unverified",
+ exitCode: null,
+ reason: `Sibling REA checkout not found at ${reaRoot} (set REA_ROOT or place REA next to BLA). Drift check cannot run.`,
+ stdout: "",
+ stderr: "",
+ };
+ }
+
+ const script = join(blaRoot, "scripts", "verify-rea-constants.mjs");
+ if (!existsSync(script)) {
+ return {
+ ...base,
+ status: "unverified",
+ exitCode: null,
+ reason: `Drift harness missing: ${script}`,
+ stdout: "",
+ stderr: "",
+ };
+ }
+
+ const result = spawnSync(process.execPath, [script], {
+ cwd: blaRoot,
+ encoding: "utf8",
+ env: { ...process.env, REA_ROOT: reaRoot },
+ timeout: options.timeoutMs ?? 120_000,
+ });
+
+ const stdout = result.stdout ?? "";
+ const stderr = result.stderr ?? "";
+ if (result.error) {
+ return {
+ ...base,
+ status: "unverified",
+ exitCode: result.status,
+ reason: `Failed to spawn drift harness: ${result.error.message}`,
+ stdout,
+ stderr,
+ };
+ }
+
+ const exitCode = result.status ?? 1;
+ return {
+ ...base,
+ status: exitCode === 0 ? "pass" : "fail",
+ exitCode,
+ ...(exitCode === 0
+ ? {}
+ : { reason: "REA constant drift detected (see stdout)" }),
+ stdout,
+ stderr,
+ };
+}
+
+export const REDTEAM_REPORT_JSON = join(
+ BLA_PACKAGE_ROOT,
+ "docs",
+ "redteam",
+ "empirical-rea-validation.json",
+);
+
+export interface RedteamReportResult {
+ readonly schema: "bla-redteam-report/v1";
+ readonly status: ReaObservabilityStatus;
+ readonly reaRoot: string;
+ readonly blaRoot: string;
+ readonly reportPath: string;
+ readonly reason?: string;
+ readonly report?: unknown;
+ readonly executed: boolean;
+ readonly exitCode: number | null;
+ readonly stdout: string;
+ readonly stderr: string;
+}
+
+/**
+ * Read (and optionally re-run) the empirical REA red-team report.
+ * - Default: return the on-disk JSON when present.
+ * - `execute: true`: spawn `scripts/redteam/run.mjs` when REA dist is ready.
+ * - Missing report AND missing/unready REA β†’ explicit `unverified`.
+ */
+export function getRedteamReport(options: {
+ readonly reaRoot?: string;
+ readonly execute?: boolean;
+ readonly timeoutMs?: number;
+ readonly reportPath?: string;
+} = {}): RedteamReportResult {
+ const blaRoot = BLA_PACKAGE_ROOT;
+ const reaRoot = resolveReaRoot(options.reaRoot);
+ const reportPath = resolve(options.reportPath ?? REDTEAM_REPORT_JSON);
+ const base = {
+ schema: "bla-redteam-report/v1" as const,
+ reaRoot,
+ blaRoot,
+ reportPath,
+ };
+
+ if (options.execute === true) {
+ if (!reaDistReady(reaRoot)) {
+ return {
+ ...base,
+ status: "unverified",
+ executed: false,
+ exitCode: null,
+ reason: `REA dist not ready at ${join(reaRoot, "dist")} β€” cannot execute redteam:rea. Build sibling REA or omit execute to read a prior report.`,
+ stdout: "",
+ stderr: "",
+ };
+ }
+ const script = join(blaRoot, "scripts", "redteam", "run.mjs");
+ if (!existsSync(script)) {
+ return {
+ ...base,
+ status: "unverified",
+ executed: false,
+ exitCode: null,
+ reason: `Red-team harness missing: ${script}`,
+ stdout: "",
+ stderr: "",
+ };
+ }
+ const result = spawnSync(process.execPath, [script], {
+ cwd: blaRoot,
+ encoding: "utf8",
+ env: { ...process.env, REA_ROOT: reaRoot },
+ timeout: options.timeoutMs ?? 600_000,
+ });
+ const stdout = result.stdout ?? "";
+ const stderr = result.stderr ?? "";
+ if (result.error) {
+ return {
+ ...base,
+ status: "unverified",
+ executed: false,
+ exitCode: result.status,
+ reason: `Failed to spawn red-team harness: ${result.error.message}`,
+ stdout,
+ stderr,
+ };
+ }
+ const exitCode = result.status ?? 1;
+ if (exitCode !== 0) {
+ return {
+ ...base,
+ status: "fail",
+ executed: true,
+ exitCode,
+ reason: "redteam:rea exited non-zero",
+ stdout,
+ stderr,
+ };
+ }
+ // Fall through to read the freshly written report.
+ }
+
+ if (!existsSync(reportPath)) {
+ const reaHint = reaDistReady(reaRoot)
+ ? "REA dist is ready β€” pass execute:true to run the harness."
+ : `REA dist not ready at ${reaRoot}; cannot fabricate a red-team report.`;
+ return {
+ ...base,
+ status: "unverified",
+ executed: false,
+ exitCode: null,
+ reason: `No empirical report at ${reportPath}. ${reaHint}`,
+ stdout: "",
+ stderr: "",
+ };
+ }
+
+ try {
+ const report = JSON.parse(readFileSync(reportPath, "utf8"));
+ return {
+ ...base,
+ status: "pass",
+ executed: options.execute === true,
+ exitCode: options.execute === true ? 0 : null,
+ report,
+ stdout: "",
+ stderr: "",
+ };
+ } catch (err) {
+ return {
+ ...base,
+ status: "fail",
+ executed: options.execute === true,
+ exitCode: null,
+ reason: `Report exists but is not valid JSON: ${err instanceof Error ? err.message : String(err)}`,
+ stdout: "",
+ stderr: "",
+ };
+ }
+}
diff --git a/src/index.ts b/src/index.ts
index 267f641..6120118 100644
--- a/src/index.ts
+++ b/src/index.ts
@@ -185,6 +185,15 @@ export {
type HardenRunSummary,
type ReadHardenRunSummaryResult,
} from "./engine/run-evidence.js";
+export {
+ runReaDriftCheck,
+ getRedteamReport,
+ resolveReaRoot,
+ reaCheckoutPresent,
+ reaDistReady,
+ type ReaDriftReport,
+ type RedteamReportResult,
+} from "./engine/rea-observability.js";
export * from "./shields/exhaustion/index.js";
export * from "./shields/deception/index.js";
export {
diff --git a/src/mcp/server.ts b/src/mcp/server.ts
index fe1740c..561cd21 100644
--- a/src/mcp/server.ts
+++ b/src/mcp/server.ts
@@ -13,6 +13,15 @@ import {
lintLaneIndependence,
type LaneIndependenceReport,
} from "../engine/lane-independence-lint.js";
+import {
+ computeArtifactRunId,
+ readHardenRunSummary,
+ sha256File,
+} from "../engine/run-evidence.js";
+import {
+ getRedteamReport,
+ runReaDriftCheck,
+} from "../engine/rea-observability.js";
import { runShowcaseBenchmark } from "../engine/benchmark.js";
import {
emitCompileTimeNativeHeader,
@@ -339,7 +348,7 @@ export const BLA_MCP_TOOLS: readonly McpToolDefinition[] = Object.freeze([
{
name: "bla_verify",
description:
- "Verifies a BLA-protected artifact against REA's static/semantic resource limits, call-graph tarpit expansion, and token bankruptcy amplification. Pass protection-side honesty metadata (self_seal_enabled / wasm_export_rename_applied) so defenseScore soft-caps reflect what actually ran β€” same fields defend/harden already forward from ProtectionResult.",
+ "Verifies a BLA-protected artifact against REA's static/semantic resource limits, call-graph tarpit expansion, and token bankruptcy amplification. Pass protection-side honesty metadata (self_seal_enabled / wasm_export_rename_applied) so defenseScore soft-caps reflect what actually ran β€” same fields defend/harden already forward from ProtectionResult. Optional run_id / summary_path correlate this verify with a prior harden summary (unsigned).",
inputSchema: {
type: "object",
properties: {
@@ -366,6 +375,16 @@ export const BLA_MCP_TOOLS: readonly McpToolDefinition[] = Object.freeze([
description:
"Honesty metadata: true when known BLA-injected numeric template golden constants still leak (issue #40.6).",
},
+ summary_path: {
+ type: "string",
+ description:
+ "Optional path to bla-harden-summary.json (or its output directory) to correlate this verify with a prior harden run (unsigned).",
+ },
+ run_id: {
+ type: "string",
+ description:
+ "Optional harden run_id; when paired with summary_path, must match the summary or the tool errors.",
+ },
},
required: ["target_path"],
additionalProperties: false,
@@ -497,6 +516,66 @@ export const BLA_MCP_TOOLS: readonly McpToolDefinition[] = Object.freeze([
additionalProperties: false,
},
},
+ {
+ name: "bla_run_lookup",
+ description:
+ "Read-only correlation of a prior harden run: load bla-harden-summary.json by path or output directory, optionally asserting a run_id. Unsigned summaries only β€” never treats absence as success.",
+ inputSchema: {
+ type: "object",
+ properties: {
+ summary_path: {
+ type: "string",
+ description:
+ "Path to bla-harden-summary.json, or the harden output directory that contains it",
+ },
+ run_id: {
+ type: "string",
+ description: "Optional expected run_id; mismatch or missing id is an error",
+ },
+ },
+ required: ["summary_path"],
+ additionalProperties: false,
+ },
+ },
+ {
+ name: "bla_rea_drift",
+ description:
+ "Run the REA constant-drift harness (scripts/verify-rea-constants.mjs). This is NOT red-team evidence. Absent sibling REA β†’ status unverified (never a silent pass).",
+ inputSchema: {
+ type: "object",
+ properties: {
+ rea_root: {
+ type: "string",
+ description: "Optional path to a local REA checkout (defaults to REA_ROOT or ../rea)",
+ },
+ },
+ additionalProperties: false,
+ },
+ },
+ {
+ name: "bla_redteam_report",
+ description:
+ "Read (or optionally re-run) the empirical REA red-team report at docs/redteam/empirical-rea-validation.json. Missing report and unready REA β†’ status unverified. Pass execute:true only when sibling REA dist is built.",
+ inputSchema: {
+ type: "object",
+ properties: {
+ execute: {
+ type: "boolean",
+ description:
+ "When true, spawn npm redteam:rea (requires sibling REA dist). Default false: read the on-disk report only.",
+ },
+ rea_root: {
+ type: "string",
+ description: "Optional path to a local REA checkout (defaults to REA_ROOT or ../rea)",
+ },
+ report_path: {
+ type: "string",
+ description: "Optional override path for the empirical JSON report",
+ },
+ },
+ additionalProperties: false,
+ },
+ },
]);
@@ -619,6 +698,44 @@ function writeReceipt(
return { status: "written", wrote: outputs, overwrote: overwritten, confirmed };
}
+function attachArtifactRunEvidence(
+ tool: string,
+ inputPath: string,
+ report: Record<string, unknown>,
+): Record<string, unknown> {
+ const outputPath =
+ typeof report.outputPath === "string" ? report.outputPath : undefined;
+ if (outputPath === undefined || !existsSync(outputPath)) {
+ return {
+ ...report,
+ runEvidence: {
+ signature: "unsigned",
+ status: "unverified",
+ reason: "Protected output path missing; cannot content-address this run",
+ },
+ };
+ }
+ const outputSha256 = sha256File(outputPath);
+ const runId = computeArtifactRunId({
+ tool,
+ inputPath,
+ outputPath,
+ outputSha256,
+ });
+ return {
+ ...report,
+ runId,
+ runEvidence: {
+ schema: "bla-artifact-run/v1",
+ signature: "unsigned",
+ runId,
+ outputPath,
+ outputSha256,
+ },
+ };
+}
+
+
function optionalCount(value: unknown): number | undefined {
return typeof value === "number" && Number.isFinite(value) && value > 0
? Math.floor(value)
@@ -1166,8 +1283,13 @@ export async function handleMcpRequest(
const report = (name === "bla_defend"
? runDefend(handlers, args)
: runProtect(handlers, args)) as Record<string, unknown>;
+ const withRun = attachArtifactRunEvidence(
+ name,
+ args.input_path as string,
+ report,
+ );
payload = {
- ...report,
+ ...withRun,
writeReceipt: writeReceipt(
existingPaths(planned),
clobbered,
@@ -1178,11 +1300,35 @@ export async function handleMcpRequest(
} else if (name === "bla_audit") {
payload = handlers.auditTarget(args.path as string);
} else if (name === "bla_verify") {
- payload = handlers.verifyTarget(
+ const verification = handlers.verifyTarget(
args.target_path as string,
typeof args.original_path === "string" ? args.original_path : undefined,
verifyHonestyOptions(args),
);
+ if (typeof args.summary_path === "string" && args.summary_path.trim().length > 0) {
+ const lookup = readHardenRunSummary(
+ args.summary_path as string,
+ typeof args.run_id === "string" && args.run_id.trim().length > 0
+ ? (args.run_id as string)
+ : undefined,
+ );
+ payload = {
+ ...verification,
+ runCorrelation: {
+ schema: "bla-run-correlation/v1",
+ signature: "unsigned",
+ runId: lookup.runId,
+ summaryPath: lookup.summaryPath,
+ runIdMatches: lookup.runIdMatches,
+ },
+ };
+ } else if (typeof args.run_id === "string" && args.run_id.trim().length > 0) {
+ throw new Error(
+ "bla_verify: run_id requires summary_path so the id can be correlated against an on-disk harden summary",
+ );
+ } else {
+ payload = verification;
+ }
} else if (name === "bla_benchmark") {
payload = await handlers.runShowcaseBenchmark();
} else if (name === "bla_emit_headers") {
@@ -1213,6 +1359,37 @@ export async function handleMcpRequest(
});
} else if (name === "bla_lint_lanes") {
payload = runLintLanes(args);
+ } else if (name === "bla_run_lookup") {
+ payload = readHardenRunSummary(
+ args.summary_path as string,
+ typeof args.run_id === "string" && args.run_id.trim().length > 0
+ ? (args.run_id as string)
+ : undefined,
+ );
+ } else if (name === "bla_rea_drift") {
+ payload = runReaDriftCheck({
+ ...(typeof args.rea_root === "string" && args.rea_root.trim().length > 0
+ ? { reaRoot: args.rea_root as string }
+ : {}),
+ });
+ {
+ const status = (payload as { status: string }).status;
+ refused = status === "unverified" || status === "fail";
+ }
+ } else if (name === "bla_redteam_report") {
+ payload = getRedteamReport({
+ execute: args.execute === true,
+ ...(typeof args.rea_root === "string" && args.rea_root.trim().length > 0
+ ? { reaRoot: args.rea_root as string }
+ : {}),
+ ...(typeof args.report_path === "string" && args.report_path.trim().length > 0
+ ? { reportPath: args.report_path as string }
+ : {}),
+ });
+ {
+ const status = (payload as { status: string }).status;
+ refused = status === "unverified" || status === "fail";
+ }
}
return {
diff --git a/tests/mcp-engine-capability-parity.test.ts b/tests/mcp-engine-capability-parity.test.ts
index 8fee3cd..26bb884 100644
--- a/tests/mcp-engine-capability-parity.test.ts
+++ b/tests/mcp-engine-capability-parity.test.ts
@@ -73,12 +73,26 @@ const EXPECTED_MCP_ENGINE_CAPABILITIES: ReadonlyArray<{
"self_seal_enabled",
"wasm_export_rename_applied",
"numeric_template_leak_known",
+ "summary_path",
+ "run_id",
],
},
{
tool: "bla_lint_lanes",
params: ["path"],
},
+ {
+ tool: "bla_run_lookup",
+ params: ["summary_path", "run_id"],
+ },
+ {
+ tool: "bla_rea_drift",
+ params: ["rea_root"],
+ },
+ {
+ tool: "bla_redteam_report",
+ params: ["execute", "rea_root", "report_path"],
+ },
]);
describe("MCP ↔ engine capability parity (agent-facing)", () => {
diff --git a/tests/rea-observability-mcp.test.ts b/tests/rea-observability-mcp.test.ts
new file mode 100644
index 0000000..19d87cf
--- /dev/null
+++ b/tests/rea-observability-mcp.test.ts
@@ -0,0 +1,150 @@
+import {
+ mkdtempSync,
+ mkdirSync,
+ rmSync,
+ writeFileSync,
+} from "node:fs";
+import { tmpdir } from "node:os";
+import { join } from "node:path";
+import { afterEach, describe, expect, it } from "vitest";
+import {
+ getRedteamReport,
+ reaCheckoutPresent,
+ reaDistReady,
+ resolveReaRoot,
+ runReaDriftCheck,
+} from "../src/engine/rea-observability.js";
+import { handleMcpRequest } from "../src/mcp/server.js";
+
+const tempDirs: string[] = [];
+afterEach(() => {
+ for (const d of tempDirs.splice(0)) rmSync(d, { recursive: true, force: true });
+});
+
+async function call(name: string, args: Record<string, unknown> = {}) {
+ return handleMcpRequest({
+ jsonrpc: "2.0",
+ id: 1,
+ method: "tools/call",
+ params: { name, arguments: args },
+ });
+}
+
+describe("REA observability engine (C2)", () => {
+ it("runReaDriftCheck returns explicit unverified when REA is absent", () => {
+ const missing = join(tmpdir(), `no-rea-${Date.now()}-${Math.random()}`);
+ const report = runReaDriftCheck({ reaRoot: missing });
+ expect(report.status).toBe("unverified");
+ expect(report.notRedTeamEvidence).toBe(true);
+ expect(report.reason).toMatch(/not found/i);
+ expect(report.exitCode).toBeNull();
+ expect(reaCheckoutPresent(missing)).toBe(false);
+ });
+
+ it("getRedteamReport returns unverified when report missing and REA unready", () => {
+ const dir = mkdtempSync(join(tmpdir(), "bla-rt-missing-"));
+ tempDirs.push(dir);
+ const reportPath = join(dir, "no-report.json");
+ const fakeRea = join(dir, "rea");
+ mkdirSync(fakeRea);
+ // No src/ β†’ checkout absent; no dist β†’ unready.
+ const result = getRedteamReport({
+ reaRoot: fakeRea,
+ reportPath,
+ execute: false,
+ });
+ expect(result.status).toBe("unverified");
+ expect(result.reason).toMatch(/No empirical report/);
+ expect(result.executed).toBe(false);
+ });
+
+ it("getRedteamReport reads a fixture report without needing REA", () => {
+ const dir = mkdtempSync(join(tmpdir(), "bla-rt-fixture-"));
+ tempDirs.push(dir);
+ const reportPath = join(dir, "empirical.json");
+ const fixture = {
+ generatedAt: "test",
+ pairs: [{ format: "wasm", before: { status: "ok" }, after: { status: "ok" } }],
+ unverified: [],
+ };
+ writeFileSync(reportPath, JSON.stringify(fixture), "utf8");
+ const fakeRea = join(dir, "rea-absent");
+ const result = getRedteamReport({
+ reaRoot: fakeRea,
+ reportPath,
+ });
+ expect(result.status).toBe("pass");
+ expect(result.report).toMatchObject(fixture);
+ expect(result.executed).toBe(false);
+ });
+
+ it("execute:true without REA dist is unverified (never silent pass)", () => {
+ const dir = mkdtempSync(join(tmpdir(), "bla-rt-exec-"));
+ tempDirs.push(dir);
+ const fakeRea = join(dir, "rea");
+ mkdirSync(join(fakeRea, "src"), { recursive: true });
+ expect(reaDistReady(fakeRea)).toBe(false);
+ const result = getRedteamReport({
+ reaRoot: fakeRea,
+ execute: true,
+ reportPath: join(dir, "out.json"),
+ });
+ expect(result.status).toBe("unverified");
+ expect(result.reason).toMatch(/dist not ready/i);
+ expect(result.executed).toBe(false);
+ });
+});
+
+describe("REA observability MCP tools (C2)", () => {
+ it("bla_rea_drift surfaces unverified as isError when REA absent", async () => {
+ const missing = join(tmpdir(), `mcp-no-rea-${Date.now()}`);
+ const res = await call("bla_rea_drift", { rea_root: missing });
+ expect(res?.error).toBeUndefined();
+ const result = res!.result as {
+ isError: boolean;
+ structuredContent: { status: string; notRedTeamEvidence: boolean };
+ };
+ expect(result.isError).toBe(true);
+ expect(result.structuredContent.status).toBe("unverified");
+ expect(result.structuredContent.notRedTeamEvidence).toBe(true);
+ });
+
+ it("bla_redteam_report reads fixture; missing path is unverified isError", async () => {
+ const dir = mkdtempSync(join(tmpdir(), "bla-rt-mcp-"));
+ tempDirs.push(dir);
+ const reportPath = join(dir, "empirical.json");
+ writeFileSync(
+ reportPath,
+ JSON.stringify({ generatedAt: "fixture", pairs: [], unverified: ["demo"] }),
+ "utf8",
+ );
+ const ok = await call("bla_redteam_report", {
+ report_path: reportPath,
+ rea_root: join(dir, "no-rea"),
+ });
+ expect(ok?.error).toBeUndefined();
+ const okResult = ok!.result as {
+ isError?: boolean;
+ structuredContent: { status: string; report: { generatedAt: string } };
+ };
+ expect(okResult.isError).not.toBe(true);
+ expect(okResult.structuredContent.status).toBe("pass");
+ expect(okResult.structuredContent.report.generatedAt).toBe("fixture");
+
+ const missing = await call("bla_redteam_report", {
+ report_path: join(dir, "absent.json"),
+ rea_root: join(dir, "no-rea"),
+ });
+ const missResult = missing!.result as {
+ isError: boolean;
+ structuredContent: { status: string };
+ };
+ expect(missResult.isError).toBe(true);
+ expect(missResult.structuredContent.status).toBe("unverified");
+ });
+
+ it("resolveReaRoot honors REA_ROOT override path shape", () => {
+ const custom = join(tmpdir(), "custom-rea-root");
+ expect(resolveReaRoot(custom)).toBe(custom);
+ });
+});
diff --git a/tests/run-evidence.test.ts b/tests/run-evidence.test.ts
new file mode 100644
index 0000000..2f6a15b
--- /dev/null
+++ b/tests/run-evidence.test.ts
@@ -0,0 +1,217 @@
+import {
+ existsSync,
+ mkdtempSync,
+ mkdirSync,
+ readFileSync,
+ rmSync,
+ writeFileSync,
+} from "node:fs";
+import { tmpdir } from "node:os";
+import { join } from "node:path";
+import { afterEach, describe, expect, it } from "vitest";
+import {
+ computeHardenRunId,
+ HARDEN_SUMMARY_FILE_NAME,
+ HARDEN_SUMMARY_SCHEMA,
+ readHardenRunSummary,
+} from "../src/engine/run-evidence.js";
+import { hardenProject } from "../src/engine/harden.js";
+import { handleMcpRequest } from "../src/mcp/server.js";
+
+const tempDirs: string[] = [];
+afterEach(() => {
+ for (const d of tempDirs.splice(0)) rmSync(d, { recursive: true, force: true });
+});
+
+const JS = `export function unlockPremium(token) { return token === "LIVE-KEY"; }\n`;
+
+async function call(name: string, args: Record<string, unknown>) {
+ const res = await handleMcpRequest({
+ jsonrpc: "2.0",
+ id: 1,
+ method: "tools/call",
+ params: { name, arguments: args },
+ });
+ return res!;
+}
+
+describe("harden run evidence (C1)", () => {
+ it("computeHardenRunId is stable for identical planned work", () => {
+ const identity = {
+ blaVersion: "1.6.4",
+ directory: "/tmp/proj",
+ intensity: "bankrupt",
+ weaveDecoys: false,
+ selfSeal: false,
+ artifacts: [
+ { relativePath: "b.js", format: "javascript", outputPath: "/out/b.js" },
+ { relativePath: "a.js", format: "javascript", outputPath: "/out/a.js" },
+ ],
+ };
+ const shuffled = {
+ ...identity,
+ artifacts: [...identity.artifacts].reverse(),
+ };
+ expect(computeHardenRunId(identity)).toBe(computeHardenRunId(shuffled));
+ expect(computeHardenRunId(identity)).toMatch(/^bla-run-[0-9a-f]{24}$/);
+ });
+
+ it("hardenProject emits runId + unsigned summary; lookup round-trips", () => {
+ const dir = mkdtempSync(join(tmpdir(), "bla-run-ev-"));
+ tempDirs.push(dir);
+ mkdirSync(join(dir, "dist"));
+ writeFileSync(join(dir, "dist", "app.js"), JS, "utf8");
+
+ const report = hardenProject({
+ directory: dir,
+ seed: "run-ev-1",
+ intensity: "stealth",
+ });
+ expect(report.runId).toMatch(/^bla-run-[0-9a-f]{24}$/);
+ expect(report.summaryPath).toBeDefined();
+ expect(existsSync(report.summaryPath!)).toBe(true);
+
+ const disk = JSON.parse(readFileSync(report.summaryPath!, "utf8")) as {
+ schema: string;
+ runId: string;
+ signature: string;
+ };
+ expect(disk.schema).toBe(HARDEN_SUMMARY_SCHEMA);
+ expect(disk.runId).toBe(report.runId);
+ expect(disk.signature).toBe("unsigned");
+
+ const byFile = readHardenRunSummary(report.summaryPath!, report.runId);
+ expect(byFile.runId).toBe(report.runId);
+ expect(byFile.runIdMatches).toBe(true);
+
+ const byDir = readHardenRunSummary(report.outputDirectory);
+ expect(byDir.runId).toBe(report.runId);
+ expect(byDir.summaryPath).toContain(HARDEN_SUMMARY_FILE_NAME);
+
+ // Dry-run still gets a runId (same planned work β†’ same id) but no summary file.
+ const dry = hardenProject({
+ directory: dir,
+ seed: "run-ev-1",
+ intensity: "stealth",
+ dryRun: true,
+ });
+ expect(dry.runId).toBe(report.runId);
+ expect(dry.summaryPath).toBeUndefined();
+ });
+
+ it("readHardenRunSummary fails clearly on missing / bad id / pre-v2", () => {
+ const dir = mkdtempSync(join(tmpdir(), "bla-run-bad-"));
+ tempDirs.push(dir);
+ expect(() => readHardenRunSummary(join(dir, "missing.json"))).toThrow(
+ /Run summary not found/,
+ );
+
+ const v1 = join(dir, "old.json");
+ writeFileSync(
+ v1,
+ JSON.stringify({
+ schema: "bla-harden-summary/v1",
+ directory: dir,
+ artifacts: [],
+ }),
+ "utf8",
+ );
+ expect(() => readHardenRunSummary(v1)).toThrow(/no runId/);
+
+ const v2 = join(dir, HARDEN_SUMMARY_FILE_NAME);
+ writeFileSync(
+ v2,
+ JSON.stringify({
+ schema: HARDEN_SUMMARY_SCHEMA,
+ runId: "bla-run-aaaaaaaaaaaaaaaaaaaaaaaa",
+ signature: "unsigned",
+ artifacts: [],
+ }),
+ "utf8",
+ );
+ expect(() =>
+ readHardenRunSummary(dir, "bla-run-bbbbbbbbbbbbbbbbbbbbbbbb"),
+ ).toThrow(/Run id mismatch/);
+ });
+
+ it("MCP bla_run_lookup + bla_verify correlation; defend/protect carry runId", async () => {
+ const dir = mkdtempSync(join(tmpdir(), "bla-run-mcp-"));
+ tempDirs.push(dir);
+ mkdirSync(join(dir, "dist"));
+ const input = join(dir, "dist", "app.js");
+ writeFileSync(input, JS, "utf8");
+
+ const hardenRes = await call("bla_harden", {
+ directory: dir,
+ seed: "run-mcp-1",
+ intensity: "stealth",
+ confirm_overwrite: true,
+ });
+ expect(hardenRes.error).toBeUndefined();
+ const harden = (hardenRes.result as { structuredContent: Record<string, unknown> })
+ .structuredContent;
+ expect(harden.runId).toMatch(/^bla-run-[0-9a-f]{24}$/);
+ const summaryPath = harden.summaryPath as string;
+ expect(existsSync(summaryPath)).toBe(true);
+
+ const lookupOk = await call("bla_run_lookup", {
+ summary_path: harden.outputDirectory as string,
+ run_id: harden.runId as string,
+ });
+ expect(lookupOk.error).toBeUndefined();
+ expect((lookupOk.result as { isError?: boolean }).isError).not.toBe(true);
+ const looked = (lookupOk.result as { structuredContent: { runId: string } })
+ .structuredContent;
+ expect(looked.runId).toBe(harden.runId);
+
+ const lookupBad = await call("bla_run_lookup", {
+ summary_path: summaryPath,
+ run_id: "bla-run-000000000000000000000000",
+ });
+ expect((lookupBad.result as { isError: boolean }).isError).toBe(true);
+
+ const missing = await call("bla_run_lookup", {
+ summary_path: join(dir, "no-such-summary.json"),
+ });
+ expect((missing.result as { isError: boolean }).isError).toBe(true);
+
+ const out = join(dir, "protected.js");
+ const protectRes = await call("bla_protect", {
+ input_path: input,
+ output_path: out,
+ seed: "run-mcp-protect",
+ intensity: "stealth",
+ });
+ expect(protectRes.error).toBeUndefined();
+ expect((protectRes.result as { isError?: boolean }).isError).not.toBe(true);
+ const protectedPayload = (
+ protectRes.result as { structuredContent: Record<string, unknown> }
+ ).structuredContent;
+ expect(protectedPayload.runId).toMatch(/^bla-run-[0-9a-f]{24}$/);
+ expect(protectedPayload.runEvidence).toMatchObject({
+ signature: "unsigned",
+ schema: "bla-artifact-run/v1",
+ });
+
+ const verifyRes = await call("bla_verify", {
+ target_path: out,
+ summary_path: summaryPath,
+ run_id: harden.runId as string,
+ });
+ expect(verifyRes.error).toBeUndefined();
+ expect((verifyRes.result as { isError?: boolean }).isError).not.toBe(true);
+ const verified = (
+ verifyRes.result as {
+ structuredContent: { runCorrelation: { runId: string; signature: string } };
+ }
+ ).structuredContent;
+ expect(verified.runCorrelation.runId).toBe(harden.runId);
+ expect(verified.runCorrelation.signature).toBe("unsigned");
+
+ const verifyOrphanId = await call("bla_verify", {
+ target_path: out,
+ run_id: harden.runId as string,
+ });
+ expect((verifyOrphanId.result as { isError: boolean }).isError).toBe(true);
+ });
+});