Simplify and harden investigation workflows

This commit is contained in:
morluto
2026-07-12 21:45:26 +00:00
parent fbe8ec1f13
commit 293cea7723
35 changed files with 531 additions and 1041 deletions
+55
View File
@@ -0,0 +1,55 @@
import { mkdtemp, rm, writeFile } from "node:fs/promises";
import { tmpdir } from "node:os";
import { join } from "node:path";
import { describe, expect, it } from "vitest";
import {
paginateArtifactInventory,
scanArtifactInventory,
} from "../src/application/ArtifactInventory.js";
const LIMITS = {
maxEntries: 10,
maxTotalBytes: 1_024,
maxEntryBytes: 512,
maxCompressionRatio: 10,
maxDepth: 8,
maxPathBytes: 256,
} as const;
describe("artifact inventory snapshot", () => {
it("projects every page from the same completed scan", async () => {
const root = await mkdtemp(join(tmpdir(), "rea-inventory-snapshot-"));
await writeFile(join(root, "a.txt"), "a");
await writeFile(join(root, "b.txt"), "b");
const snapshot = await scanArtifactInventory(root, LIMITS);
await rm(join(root, "b.txt"));
const first = paginateArtifactInventory(snapshot, {
nodeOffset: 0,
nodeLimit: 1,
occurrenceOffset: 0,
occurrenceLimit: 2,
edgeOffset: 0,
edgeLimit: 1,
});
const second = paginateArtifactInventory(snapshot, {
nodeOffset: 1,
nodeLimit: 10,
occurrenceOffset: 2,
occurrenceLimit: 2,
edgeOffset: 1,
edgeLimit: 10,
});
expect(first.manifest).toEqual(second.manifest);
expect(
[...first.occurrences.items, ...second.occurrences.items].map(
({ logical_path: path }) => path,
),
).toEqual([".", "a.txt", "b.txt"]);
expect(first.occurrences.total).toBe(3);
expect(second.occurrences.total).toBe(3);
});
});
+39
View File
@@ -16,6 +16,45 @@ import { createServer } from "../src/server/createServer.js";
import { observed } from "./fixtures/analysisExecution.js";
describe("artifact graph MCP integration", () => {
it("rejects altered payloads that reuse session Evidence IDs", async () => {
const session = new BinarySession(() => ({
health: () => Promise.resolve(),
execute: () => Promise.resolve(observed(null)),
close: () => Promise.resolve(),
}));
session.recordEvidence(ARTIFACT_COMPARISON_EXAMPLE.left);
session.recordEvidence(ARTIFACT_COMPARISON_EXAMPLE.right);
const server = createServer(session, session);
const client = new Client({
name: "artifact-authority-test",
version: "1",
});
const [clientTransport, serverTransport] =
InMemoryTransport.createLinkedPair();
try {
await server.connect(serverTransport);
await client.connect(clientTransport);
const result = await client.callTool({
name: "compare_artifacts",
arguments: {
...ARTIFACT_COMPARISON_EXAMPLE,
left: {
...ARTIFACT_COMPARISON_EXAMPLE.left,
limitations: ["caller altered this record"],
},
},
});
expect(result.isError).toBe(true);
expect(session.exportEvidenceBundle().records).toHaveLength(2);
} finally {
await Promise.allSettled([
client.close(),
server.close(),
session.close(),
]);
}
});
it("rejects comparison Evidence that is not owned by the session", async () => {
const session = new BinarySession(() => ({
health: () => Promise.resolve(),
+39
View File
@@ -23,6 +23,45 @@ const sourceEvidence = (label: string) =>
);
describe("bundle comparison MCP integration", () => {
it("rejects altered records that reuse session Evidence IDs", async () => {
const session = new BinarySession(() => ({
health: () => Promise.resolve(),
execute: () => Promise.resolve(observed(null)),
close: () => Promise.resolve(),
}));
const leftRecord = sourceEvidence("left-authority");
const rightRecord = sourceEvidence("right-authority");
session.recordEvidence(leftRecord);
session.recordEvidence(rightRecord);
const altered = {
...leftRecord,
limitations: ["caller altered this record"],
};
const server = createServer(session, session);
const client = new Client({ name: "bundle-authority-test", version: "1" });
const [clientTransport, serverTransport] =
InMemoryTransport.createLinkedPair();
try {
await server.connect(serverTransport);
await client.connect(clientTransport);
const result = await client.callTool({
name: "compare_bundles",
arguments: {
left: createEvidenceBundle([altered]),
right: createEvidenceBundle([rightRecord]),
},
});
expect(result.isError).toBe(true);
expect(session.exportEvidenceBundle().records).toHaveLength(2);
} finally {
await Promise.allSettled([
client.close(),
server.close(),
session.close(),
]);
}
});
it("rejects unknown histories that are not owned by the session", async () => {
const session = new BinarySession(() => ({
health: () => Promise.resolve(),
-49
View File
@@ -1,49 +0,0 @@
import { execFile } from "node:child_process";
import { readFile, writeFile } from "node:fs/promises";
import { promisify } from "node:util";
import { afterEach, describe, expect, it } from "vitest";
const exec = promisify(execFile);
const manifestPath = "metadata/completion-manifest.json";
let original: string | undefined;
afterEach(async () => {
if (original !== undefined) await writeFile(manifestPath, original);
original = undefined;
});
describe("generated completion manifest", () => {
it("is deterministic, current, path-independent, and never promotes unknowns", async () => {
await exec(process.execPath, ["scripts/generate-completion-manifest.mjs"]);
const first = await readFile(manifestPath, "utf8");
await exec(process.execPath, ["scripts/generate-completion-manifest.mjs"]);
expect(await readFile(manifestPath, "utf8")).toBe(first);
await expect(
exec(process.execPath, [
"scripts/generate-completion-manifest.mjs",
"--check",
]),
).resolves.toBeDefined();
expect(first).not.toContain(process.cwd());
const manifest = JSON.parse(first) as {
outcomes: { pass: unknown[]; unsupported: unknown[]; unknown: unknown[] };
};
expect(manifest.outcomes.pass).toEqual([]);
expect(manifest.outcomes.unsupported).toHaveLength(1);
expect(manifest.outcomes.unknown).toHaveLength(1);
});
it("check mode detects manual tampering without rewriting it", async () => {
original = await readFile(manifestPath, "utf8");
const tampered = `${original.trimEnd()} `;
await writeFile(manifestPath, tampered);
await expect(
exec(process.execPath, [
"scripts/generate-completion-manifest.mjs",
"--check",
]),
).rejects.toThrow(/stale or tampered/u);
expect(await readFile(manifestPath, "utf8")).toBe(tampered);
});
});
+39
View File
@@ -97,4 +97,43 @@ describe("function comparison MCP integration", () => {
]);
}
});
it("rejects altered payloads that reuse session Evidence IDs", async () => {
const session = new BinarySession(() => ({
health: () => Promise.resolve(),
execute: () => Promise.resolve(observed(null)),
close: () => Promise.resolve(),
}));
session.recordEvidence(FUNCTION_COMPARISON_EXAMPLE.left);
session.recordEvidence(FUNCTION_COMPARISON_EXAMPLE.right);
const server = createServer(session, session);
const client = new Client({
name: "function-authority-test",
version: "1",
});
const [clientTransport, serverTransport] =
InMemoryTransport.createLinkedPair();
try {
await server.connect(serverTransport);
await client.connect(clientTransport);
const result = await client.callTool({
name: "compare_functions",
arguments: {
left: {
...FUNCTION_COMPARISON_EXAMPLE.left,
limitations: ["caller altered this record"],
},
right: FUNCTION_COMPARISON_EXAMPLE.right,
},
});
expect(result.isError).toBe(true);
expect(session.exportEvidenceBundle().records).toHaveLength(2);
} finally {
await Promise.allSettled([
client.close(),
server.close(),
session.close(),
]);
}
});
});
+15
View File
@@ -12,6 +12,7 @@ import {
probeProcessCaptureCapability,
ProcessCaptureError,
} from "../src/application/ProcessHarness.js";
import { snapshotRoots } from "../src/application/FilesystemSnapshot.js";
import {
authorizeProcessScenario,
compareProcessCaptures,
@@ -31,6 +32,20 @@ describe("process capture domain", () => {
working_directory: "/tmp",
};
it("cancels filesystem snapshots before traversing declared roots", async () => {
const controller = new AbortController();
controller.abort();
await expect(
snapshotRoots(
parseProcessScenario({
...base,
filesystem_roots: ["/tmp"],
}),
controller.signal,
),
).rejects.toMatchObject({ name: "AbortError" });
});
it("parses bounded scenarios and rejects unordered events", () => {
expect(parseProcessScenario(base).timeout_ms).toBe(30_000);
expect(() =>
+1 -1
View File
@@ -118,7 +118,7 @@ describe("reference source import", () => {
const result = await importTree(root, parent);
expect(result.ok).toBe(true);
if (!result.ok) throw result.error;
expect(result.value.vcs).toEqual({ kind: "git", head: oid, dirty: true });
expect(result.value.vcs).toEqual({ kind: "git", head: oid, dirty: null });
} finally {
await rm(parent, { recursive: true, force: true });
}
+1 -1
View File
@@ -58,7 +58,7 @@ describe("production stdio runtime", () => {
);
expect(stderr).not.toContain("HOPPER_LOADER_ARGS_JSON");
expect(stderr).not.toContain(fixturePath);
}, 20_000);
}, 15_000);
it("honors the configured kind for an initial database target", async () => {
const transport = new StdioClientTransport({
+25 -5
View File
@@ -1,4 +1,5 @@
import { access, mkdtemp, readdir } from "node:fs/promises";
import { createHash } from "node:crypto";
import { access, mkdtemp, readFile, readdir } from "node:fs/promises";
import { tmpdir } from "node:os";
import { join } from "node:path";
import { Readable } from "node:stream";
@@ -16,19 +17,19 @@ const LIMITS = {
maxPathBytes: 256,
} as const;
describe("safe artifact output transaction", () => {
describe("safe artifact output tree", () => {
it("removes only its owned tree after digest failure and proves absence", async () => {
const parent = await mkdtemp(join(tmpdir(), "rea-safe-output-"));
const output = join(parent, "published");
const transaction = await SafeOutputTree.create(output, LIMITS);
const tree = await SafeOutputTree.create(output, LIMITS);
await expect(
transaction.write(
tree.write(
"nested/file.txt",
Readable.from(Buffer.from("unexpected")),
"0".repeat(64),
),
).rejects.toThrow(/disagrees/u);
expect(await transaction.rollback()).toMatchObject({
expect(await tree.rollback()).toMatchObject({
attempted: true,
verified: true,
residualPaths: [],
@@ -36,4 +37,23 @@ describe("safe artifact output transaction", () => {
await expect(access(output)).rejects.toThrow();
expect(await readdir(parent)).toEqual([]);
});
it("publishes files while building and preserves them after sealing", async () => {
const parent = await mkdtemp(join(tmpdir(), "rea-safe-output-"));
const output = join(parent, "published");
const tree = await SafeOutputTree.create(output, LIMITS);
const bytes = Buffer.from("visible before seal");
const digest = createHash("sha256").update(bytes).digest("hex");
await tree.write("file.txt", Readable.from(bytes), digest);
expect(await readFile(join(output, "file.txt"), "utf8")).toBe(
"visible before seal",
);
await tree.commit();
expect(await tree.rollback()).toMatchObject({ attempted: false });
expect(await readFile(join(output, "file.txt"), "utf8")).toBe(
"visible before seal",
);
});
});
+1 -1
View File
@@ -170,7 +170,7 @@ describe("target-free MCP lifecycle", () => {
expect(
text(await mcp.callTool({ name: "binary_session", arguments: {} })),
).toContain('"open": false');
}, 20_000);
}, 10_000);
it("records approved process residuals in the unknown registry", async () => {
const session = new BinarySession(() => client("fixture", []));