refactor: remove cross-version investigation workflow

This commit is contained in:
morluto
2026-09-29 08:06:43 +08:00
parent 0d96275b68
commit 81996df7a5
42 changed files with 66 additions and 3750 deletions
@@ -1,313 +0,0 @@
import { mkdir, writeFile } from "node:fs/promises";
import { join } from "node:path";
import { describe, expect, it, vi } from "vitest";
import { createPackageWithOptions } from "@electron/asar";
import { createTestTempDirectory } from "../../fixtures/temporaryDirectory.js";
import { runCrossVersionInvestigation } from "../../../src/application/CrossVersionInvestigation.js";
import {
readInvestigationWorkspace,
writeInvestigationWorkspace,
} from "../../../src/application/InvestigationWorkspaceStore.js";
import { createEvidenceBundle } from "../../../src/domain/evidenceBundle.js";
import type { EvidenceFilePolicy } from "../../../src/domain/evidenceBundle.js";
import {
createInvestigationWorkspace,
crossVersionInvestigationInputSchema,
investigationRunSchema,
} from "../../../src/domain/investigationWorkspace.js";
import { ok } from "../../../src/domain/result.js";
const policy = (root: string): EvidenceFilePolicy => ({
roots: [root],
maxBytes: 64 * 1024 * 1024,
maxDepth: 64,
maxStringLength: 1024 * 1024,
maxNodes: 1_000_000,
});
const fixture = async () => {
const directory = await createTestTempDirectory("rea-workspace-");
const left = join(directory, "left");
const right = join(directory, "right");
await Promise.all([mkdir(left), mkdir(right)]);
await Promise.all([
writeFile(join(left, "app.txt"), "version one\n"),
writeFile(join(right, "app.txt"), "version two\n"),
writeFile(join(right, "added.txt"), "new behavior candidate\n"),
]);
const path = join(directory, "workspace.json");
const input = crossVersionInvestigationInputSchema.parse({
approved: true,
workspace_path: path,
workspace_name: "release-diff",
left_path: left,
right_path: right,
options: { page_size: 2, change_limit: 100 },
});
return { directory, left, right, path, input };
};
describe("persistent cross-version investigation workspace", () => {
it("uses valid workspace roots when another configured root is missing", async () => {
const root = await createTestTempDirectory("rea-workspace-stale-root-");
const path = join(root, "workspace.json");
const workspace = createInvestigationWorkspace(
"stale-root",
createEvidenceBundle([]),
[],
);
const configured = {
...policy(root),
roots: [join(root, "missing"), root],
};
expect(
await writeInvestigationWorkspace(workspace, path, null, configured),
).toMatchObject({ ok: true });
expect(await readInvestigationWorkspace(path, configured)).toEqual({
ok: true,
value: workspace,
});
});
it("stores workspaces in dot-prefixed child directories", async () => {
const root = await createTestTempDirectory("rea-workspace-dot-child-");
const directory = join(root, "..cache");
await mkdir(directory);
const path = join(directory, "workspace.json");
const workspace = createInvestigationWorkspace(
"dot-child",
createEvidenceBundle([]),
[],
);
expect(
await writeInvestigationWorkspace(workspace, path, null, policy(root)),
).toMatchObject({ ok: true });
expect(await readInvestigationWorkspace(path, policy(root))).toEqual({
ok: true,
value: workspace,
});
});
it("enforces shared JSON depth limits while reading workspaces", async () => {
const { directory, path, input } = await fixture();
const completed = await runCrossVersionInvestigation(
input,
policy(directory),
{},
);
if (!completed.ok) throw completed.error;
await expect(
readInvestigationWorkspace(path, {
...policy(directory),
maxDepth: 1,
}),
).resolves.toMatchObject({
ok: false,
error: { _tag: "InvestigationWorkspaceError", reason: "too-large" },
});
});
it("resumes from a comparison checkpoint without recomputing its identity", async () => {
const { directory, input } = await fixture();
const completed = await runCrossVersionInvestigation(
input,
policy(directory),
{},
);
if (!completed.ok) throw completed.error;
const run = completed.value.workspace.runs[0];
if (run === undefined || run.comparison_evidence_id === null)
throw new Error("missing completed run");
expect(
investigationRunSchema.safeParse({ ...run, status: "running" }).success,
).toBe(false);
const partialRun = investigationRunSchema.parse({
...run,
status: "running",
completed_stages: [
"inventory_left",
"inventory_right",
"compare_artifacts",
],
result_evidence_id: null,
});
const records = completed.value.workspace.bundle.records.filter(
({ evidence_id: id }) => id !== run.result_evidence_id,
);
const partial = createInvestigationWorkspace(
"release-diff",
createEvidenceBundle(records),
[partialRun],
);
const resumePath = join(directory, "resume.json");
expect(
await writeInvestigationWorkspace(
partial,
resumePath,
null,
policy(directory),
),
).toMatchObject({ ok: true });
const replayAuthorization = vi.fn(() => Promise.resolve(ok(null)));
await expect(
runCrossVersionInvestigation(
{
...input,
workspace_path: resumePath,
replay_run_id: partialRun.run_id,
},
policy(directory),
{},
),
).resolves.toMatchObject({
ok: false,
error: {
_tag: "InvestigationWorkspaceError",
reason: "revision-conflict",
},
});
expect(replayAuthorization).not.toHaveBeenCalled();
const resumed = await runCrossVersionInvestigation(
{ ...input, workspace_path: resumePath },
policy(directory),
{},
);
expect(resumed).toMatchObject({
ok: true,
value: {
reused: false,
evidence: { evidence_id: completed.value.evidence.evidence_id },
workspace: { revision: 2 },
},
});
});
});
describe("persistent investigation workspace history", () => {
it("appends a changed-content run without replacing earlier Evidence", async () => {
const { directory, right, input } = await fixture();
const first = await runCrossVersionInvestigation(
input,
policy(directory),
{},
);
if (!first.ok) throw first.error;
const firstRun = first.value.workspace.runs[0];
if (firstRun === undefined || firstRun.result_evidence_id === null)
throw new Error("missing first completed run");
await writeFile(join(right, "app.txt"), "version three\n");
const second = await runCrossVersionInvestigation(
input,
policy(directory),
{},
);
expect(second).toMatchObject({
ok: true,
value: {
reused: false,
workspace: {
revision: 6,
runs: [{ status: "complete" }, { status: "complete" }],
},
},
});
if (!second.ok) throw second.error;
expect(second.value.workspace.runs).toHaveLength(2);
expect(second.value.workspace.runs.map(({ run_id: id }) => id)).toContain(
firstRun.run_id,
);
expect(
second.value.workspace.bundle.records.some(
({ evidence_id: id }) => id === firstRun.result_evidence_id,
),
).toBe(true);
});
it("checkpoints bounded integrity contradictions and safely reuses them", async () => {
const directory = await createTestTempDirectory("rea-workspace-integrity-");
const source = join(directory, "source");
await mkdir(source);
await writeFile(join(source, "addon.node"), "verified native addon\n");
const left = join(directory, "left.asar");
const right = join(directory, "right.asar");
await Promise.all([
createPackageWithOptions(source, left, { unpack: "*.node" }),
createPackageWithOptions(source, right, { unpack: "*.node" }),
]);
await writeFile(join(`${right}.unpacked`, "addon.node"), "tampered\n");
const base = {
approved: true as const,
workspace_path: join(directory, "strict.json"),
workspace_name: "integrity-diff",
left_path: left,
right_path: right,
options: { page_size: 500, change_limit: 100 },
};
const strict = await runCrossVersionInvestigation(
crossVersionInvestigationInputSchema.parse(base),
policy(directory),
{},
);
expect(strict).toMatchObject({
ok: false,
error: { _tag: "ArtifactOperationError", reason: "integrity" },
});
const continuedInput = crossVersionInvestigationInputSchema.parse({
...base,
workspace_path: join(directory, "continued.json"),
integrity_policy: "record-and-continue",
integrity_continue_approved: true,
max_integrity_mismatches: 2,
});
const continued = await runCrossVersionInvestigation(
continuedInput,
policy(directory),
{ integrityContinueEnabled: true },
);
expect(continued).toMatchObject({
ok: true,
value: { reused: false, workspace: { revision: 3 } },
});
if (!continued.ok) throw continued.error;
expect(
continued.value.workspace.bundle.records.some((record) =>
JSON.stringify(record.normalized_result).includes(
'"integrity_contradictions":[{',
),
),
).toBe(true);
expect(
continued.value.workspace.bundle.records.find(
({ operation }) => operation === "compare_artifacts",
)?.normalized_result,
).toMatchObject({ status: "contradiction" });
await expect(
runCrossVersionInvestigation(continuedInput, policy(directory), {
integrityContinueEnabled: true,
}),
).resolves.toMatchObject({ ok: true, value: { reused: true } });
const strictAfterContinuation = crossVersionInvestigationInputSchema.parse({
...base,
workspace_path: continuedInput.workspace_path,
});
await expect(
runCrossVersionInvestigation(
strictAfterContinuation,
policy(directory),
{},
),
).resolves.toMatchObject({
ok: false,
error: { _tag: "ArtifactOperationError", reason: "integrity" },
});
});
});
@@ -1,152 +0,0 @@
import {
chmod,
mkdir,
readFile,
rm,
stat,
symlink,
writeFile,
} from "node:fs/promises";
import { join } from "node:path";
import { describe, expect, it } from "vitest";
import { createTestTempDirectory } from "../../fixtures/temporaryDirectory.js";
import { runCrossVersionInvestigation } from "../../../src/application/CrossVersionInvestigation.js";
import { readInvestigationWorkspace } from "../../../src/application/InvestigationWorkspaceStore.js";
import type { EvidenceFilePolicy } from "../../../src/domain/evidenceBundle.js";
import { crossVersionInvestigationInputSchema } from "../../../src/domain/investigationWorkspace.js";
const policy = (root: string): EvidenceFilePolicy => ({
roots: [root],
maxBytes: 64 * 1024 * 1024,
maxDepth: 64,
maxStringLength: 1024 * 1024,
maxNodes: 1_000_000,
});
const fixture = async () => {
const directory = await createTestTempDirectory("rea-workspace-");
const left = join(directory, "left");
const right = join(directory, "right");
await Promise.all([mkdir(left), mkdir(right)]);
await Promise.all([
writeFile(join(left, "app.txt"), "version one\n"),
writeFile(join(right, "app.txt"), "version two\n"),
writeFile(join(right, "added.txt"), "new behavior candidate\n"),
]);
const path = join(directory, "workspace.json");
const input = crossVersionInvestigationInputSchema.parse({
approved: true,
workspace_path: path,
workspace_name: "release-diff",
left_path: left,
right_path: right,
options: { page_size: 2, change_limit: 100 },
});
return { directory, left, right, path, input };
};
describe("persistent cross-version investigation workspace", () => {
it("fails closed on locks, CAS conflicts, tampering, and cancellation", async () => {
const { directory, path, input } = await fixture();
await writeFile(`${path}.lock`, `${String(process.pid)}\n`, {
mode: 0o600,
});
expect(
await runCrossVersionInvestigation(input, policy(directory), {}),
).toMatchObject({
ok: false,
error: { _tag: "InvestigationWorkspaceError", reason: "locked" },
});
await rm(`${path}.lock`);
await writeFile(`${path}.lock`, "occupied\n", { mode: 0o600 });
expect(
await runCrossVersionInvestigation(input, policy(directory), {}),
).toMatchObject({
ok: false,
error: { _tag: "InvestigationWorkspaceError", reason: "locked" },
});
await rm(`${path}.lock`);
await writeFile(`${path}.lock`, "2147483647\n", { mode: 0o600 });
const completed = await runCrossVersionInvestigation(
input,
policy(directory),
{},
);
if (!completed.ok) throw completed.error;
await expect(stat(`${path}.lock`)).rejects.toMatchObject({
code: "ENOENT",
});
expect(
await runCrossVersionInvestigation(
{ ...input, expected_workspace_revision: 2 },
policy(directory),
{},
),
).toMatchObject({
ok: false,
error: {
_tag: "InvestigationWorkspaceError",
reason: "revision-conflict",
},
});
const decoded = JSON.parse(await readFile(path, "utf8")) as unknown;
if (typeof decoded !== "object" || decoded === null)
throw new Error("invalid workspace fixture");
await chmod(path, 0o600);
await writeFile(path, JSON.stringify({ ...decoded, revision: 99 }));
expect(
await readInvestigationWorkspace(path, policy(directory)),
).toMatchObject({
ok: false,
error: { _tag: "InvestigationWorkspaceError", reason: "integrity" },
});
const cancelledPath = join(directory, "cancelled.json");
const controller = new AbortController();
controller.abort();
expect(
await runCrossVersionInvestigation(
{ ...input, workspace_path: cancelledPath },
policy(directory),
{ signal: controller.signal },
),
).toMatchObject({
ok: false,
error: { _tag: "AnalysisCancelledError" },
});
});
it("rejects workspace paths and symlinks outside approved roots", async () => {
const { directory, input } = await fixture();
const approved = join(directory, "approved");
await mkdir(approved);
expect(
await runCrossVersionInvestigation(input, policy(approved), {}),
).toMatchObject({
ok: false,
error: {
_tag: "InvestigationWorkspaceError",
reason: "outside-root",
},
});
const outside = join(directory, "outside.json");
await writeFile(outside, "{}", { mode: 0o600 });
const escaped = join(approved, "escaped.json");
await symlink(outside, escaped);
expect(
await runCrossVersionInvestigation(
{ ...input, workspace_path: escaped },
policy(approved),
{},
),
).toMatchObject({
ok: false,
error: { _tag: "InvestigationWorkspaceError", reason: "not-file" },
});
});
});
@@ -1,379 +0,0 @@
import { mkdir, readFile, rm, stat, writeFile } from "node:fs/promises";
import { createHash } from "node:crypto";
import { join } from "node:path";
import { describe, expect, it } from "vitest";
import canonicalize from "canonicalize";
import { createTestTempDirectory } from "../../fixtures/temporaryDirectory.js";
import { runCrossVersionInvestigation } from "../../../src/application/CrossVersionInvestigation.js";
import {
readInvestigationWorkspace,
writeInvestigationWorkspace,
} from "../../../src/application/InvestigationWorkspaceStore.js";
import { changedBehaviorResultSchema } from "../../../src/domain/changedBehavior.js";
import type { EvidenceFilePolicy } from "../../../src/domain/evidenceBundle.js";
import {
createInvestigationWorkspace,
crossVersionInvestigationInputSchema,
investigationRunSchema,
parseInvestigationWorkspace,
serializeInvestigationWorkspace,
} from "../../../src/domain/investigationWorkspace.js";
const digestCanonical = (value: unknown): string => {
const encoded = canonicalize(value);
if (encoded === undefined) throw new TypeError("fixture is not canonical");
return createHash("sha256").update(encoded).digest("hex");
};
const policy = (root: string): EvidenceFilePolicy => ({
roots: [root],
maxBytes: 64 * 1024 * 1024,
maxDepth: 64,
maxStringLength: 1024 * 1024,
maxNodes: 1_000_000,
});
const fixture = async () => {
const directory = await createTestTempDirectory("rea-workspace-");
const left = join(directory, "left");
const right = join(directory, "right");
await Promise.all([mkdir(left), mkdir(right)]);
await Promise.all([
writeFile(join(left, "app.txt"), "version one\n"),
writeFile(join(right, "app.txt"), "version two\n"),
writeFile(join(right, "added.txt"), "new behavior candidate\n"),
]);
const path = join(directory, "workspace.json");
const input = crossVersionInvestigationInputSchema.parse({
approved: true,
workspace_path: path,
workspace_name: "release-diff",
left_path: left,
right_path: right,
options: { page_size: 2, change_limit: 100 },
});
return { directory, left, right, path, input };
};
describe("persistent cross-version investigation workspace", () => {
it("migrates legacy v1 runs without losing their revision chain", async () => {
const { directory, input } = await fixture();
const completed = await runCrossVersionInvestigation(
input,
policy(directory),
{},
);
if (!completed.ok) throw completed.error;
const current = completed.value.workspace;
const currentRun = current.runs[0];
if (currentRun === undefined) throw new Error("missing completed run");
expect(() =>
investigationRunSchema.parse({
...currentRun,
legacy_request_identity: true,
}),
).toThrow(/migrated strict runs/iu);
const legacyIdentity = digestCanonical({
schema: "rea.cross-version-investigation-request/v1",
left: currentRun.left,
right: currentRun.right,
options: currentRun.options,
});
expect(() =>
investigationRunSchema.parse({
...currentRun,
legacy_request_identity: true,
run_id: `run_${legacyIdentity}`,
request_sha256: legacyIdentity,
max_integrity_mismatches: 100,
}),
).toThrow(/migrated strict runs/iu);
const legacyRuns = current.runs.map((run) => {
const requestSha256 = digestCanonical({
schema: "rea.cross-version-investigation-request/v1",
left: run.left,
right: run.right,
options: run.options,
});
const {
integrity_policy: _integrityPolicy,
integrity_continue_approved: _integrityApproved,
max_integrity_mismatches: _integrityLimit,
...legacy
} = run;
return {
...legacy,
run_id: `run_${requestSha256}`,
request_sha256: requestSha256,
};
});
const legacySemantic = {
workspace_version: 1 as const,
workspace_id: current.workspace_id,
name: current.name,
revision: current.revision,
previous_revision_digest: current.previous_revision_digest,
bundle: current.bundle,
runs: legacyRuns,
};
const legacyDigest = `wrev_${digestCanonical(legacySemantic)}`;
expect(() =>
parseInvestigationWorkspace({
...legacySemantic,
runs: legacyRuns.map((run) => ({
...run,
max_integrity_mismatches: 100,
})),
revision_digest: legacyDigest,
}),
).toThrow(/unrecognized key/iu);
const migrated = parseInvestigationWorkspace({
...legacySemantic,
revision_digest: legacyDigest,
});
expect(migrated).toMatchObject({
revision: current.revision,
previous_revision_digest: current.previous_revision_digest,
runs: [
{
legacy_request_identity: true,
integrity_policy: "fail",
integrity_continue_approved: false,
max_integrity_mismatches: 10,
},
],
});
const encodedLegacy = canonicalize({
...legacySemantic,
revision_digest: legacyDigest,
});
if (encodedLegacy === undefined)
throw new TypeError("legacy fixture is not canonical");
await writeFile(input.workspace_path, encodedLegacy, { mode: 0o600 });
await expect(
runCrossVersionInvestigation(
{ ...input, expected_workspace_revision: current.revision },
policy(directory),
{},
),
).resolves.toMatchObject({ ok: true, value: { reused: true } });
await expect(
runCrossVersionInvestigation(
{
...input,
options: {
...input.options,
change_limit: input.options.change_limit + 1,
},
},
policy(directory),
{},
),
).resolves.toMatchObject({ ok: true, value: { reused: false } });
});
});
describe("persistent investigation workspace reuse", () => {
it("checkpoints, validates, and reuses a completed deterministic run", async () => {
const { directory, path, input } = await fixture();
const first = await runCrossVersionInvestigation(
input,
policy(directory),
{},
);
expect(first).toMatchObject({
ok: true,
value: { reused: false, workspace: { revision: 3 } },
});
if (!first.ok) throw first.error;
const result = changedBehaviorResultSchema.parse(
first.value.evidence.normalized_result,
);
expect(result).toMatchObject({
behavior_status: "unknown",
summary: { static_candidates: 3 },
investigation_run: {
workspace_id: first.value.workspace.workspace_id,
run_id: first.value.workspace.runs[0]?.run_id,
},
});
expect(result.limitations).toContain(
"No process comparison Evidence was supplied.",
);
expect(first.value.workspace.bundle.records).toHaveLength(5);
expect((await stat(path)).mode & 0o777).toBe(0o600);
expect(await readFile(path, "utf8")).toBe(
serializeInvestigationWorkspace(first.value.workspace),
);
const second = await runCrossVersionInvestigation(
input,
policy(directory),
{},
);
expect(second).toMatchObject({
ok: true,
value: {
reused: true,
evidence: { evidence_id: first.value.evidence.evidence_id },
workspace: { revision: 3 },
},
});
expect(await readInvestigationWorkspace(path, policy(directory))).toEqual({
ok: true,
value: first.value.workspace,
});
});
it("produces stable multi-provider Evidence closure across independent version runs", async () => {
const { directory, input } = await fixture();
const repeatedInput = crossVersionInvestigationInputSchema.parse({
...input,
workspace_path: join(directory, "repeated-workspace.json"),
});
const [first, repeated] = await Promise.all([
runCrossVersionInvestigation(input, policy(directory), {}),
runCrossVersionInvestigation(repeatedInput, policy(directory), {}),
]);
if (!first.ok) throw first.error;
if (!repeated.ok) throw repeated.error;
expect(repeated.value.evidence).toEqual(first.value.evidence);
expect(
[
...new Set(
first.value.workspace.bundle.records.map(
({ provider }) => provider.id,
),
),
].sort(),
).toEqual([
"rea-artifact-comparison",
"rea-artifact-graph",
"rea-changed-behavior",
]);
const comparison = first.value.workspace.bundle.records.find(
({ operation }) => operation === "compare_artifacts",
);
if (comparison === undefined)
throw new Error("missing artifact comparison Evidence");
const result = changedBehaviorResultSchema.parse(
first.value.evidence.normalized_result,
);
const evidenceIds = new Set(
first.value.workspace.bundle.records.map(({ evidence_id: id }) => id),
);
expect(result.findings.items).not.toHaveLength(0);
for (const finding of result.findings.items) {
expect(finding).toMatchObject({
classification: "derived_relationship",
source_comparison_id: comparison.evidence_id,
});
expect(finding.evidence_links).toContain(comparison.evidence_id);
expect(
finding.evidence_links.every((evidenceId) =>
evidenceIds.has(evidenceId),
),
).toBe(true);
}
});
});
describe("persistent investigation workspace replay", () => {
it("replays only an explicitly selected and fully verified complete run", async () => {
const { directory, left, right, input } = await fixture();
const completed = await runCrossVersionInvestigation(
input,
policy(directory),
{},
);
if (!completed.ok) throw completed.error;
const run = completed.value.workspace.runs[0];
if (run === undefined || run.comparison_evidence_id === null)
throw new Error("missing completed run");
await expect(
runCrossVersionInvestigation(input, policy(directory), {}),
).resolves.toMatchObject({ ok: true, value: { reused: true } });
const inconsistentRun = investigationRunSchema.parse({
...run,
result_evidence_id: run.comparison_evidence_id,
});
const inconsistent = createInvestigationWorkspace(
input.workspace_name,
completed.value.workspace.bundle,
[inconsistentRun],
);
const inconsistentPath = join(directory, "inconsistent.json");
expect(
await writeInvestigationWorkspace(
inconsistent,
inconsistentPath,
null,
policy(directory),
),
).toMatchObject({ ok: true });
await expect(
runCrossVersionInvestigation(
{
...input,
workspace_path: inconsistentPath,
replay_run_id: run.run_id,
},
policy(directory),
{},
),
).resolves.toMatchObject({
ok: false,
error: { _tag: "EvidenceIntegrityError" },
});
await Promise.all([
rm(left, { recursive: true, force: true }),
rm(right, { recursive: true, force: true }),
]);
await expect(
runCrossVersionInvestigation(
{
...input,
expected_workspace_revision: completed.value.workspace.revision,
replay_run_id: run.run_id,
},
policy(directory),
{},
),
).resolves.toMatchObject({
ok: true,
value: {
reused: true,
evidence: { evidence_id: completed.value.evidence.evidence_id },
workspace: { revision: completed.value.workspace.revision },
},
});
for (const replayInput of [
{ ...input, replay_run_id: `run_${"f".repeat(64)}` },
{ ...input, right_path: `${right}-other`, replay_run_id: run.run_id },
{
...input,
options: { ...input.options, change_limit: 99 },
replay_run_id: run.run_id,
},
])
await expect(
runCrossVersionInvestigation(replayInput, policy(directory), {}),
).resolves.toMatchObject({
ok: false,
error: {
_tag: "InvestigationWorkspaceError",
reason: "revision-conflict",
},
});
});
});
@@ -1,323 +0,0 @@
import { Client, InMemoryTransport } from "@modelcontextprotocol/client";
import { mkdir, rm, writeFile } from "node:fs/promises";
import { join } from "node:path";
import { expect, it } from "vitest";
import { z } from "zod";
import { createTestTempDirectory } from "../../fixtures/temporaryDirectory.js";
import { createTestBinarySession } from "../../fixtures/binarySession.js";
import type { BinarySession } from "../../../src/application/BinarySession.js";
import { FUNCTION_COMPARISON_EXAMPLE } from "../../../src/contracts/functionComparisonExample.js";
import { INVESTIGATION_EXAMPLES } from "../../../src/contracts/investigationExamples.js";
import {
FUNCTION_COMPARISON_EVIDENCE,
PROCESS_CAPTURE_RECONSTRUCTION,
PROCESS_CAPTURE_REFERENCE,
PROCESS_COMPARISON_EVIDENCE,
} from "../../../src/contracts/investigationExamples.js";
import { changedBehaviorResultSchema } from "../../../src/domain/changedBehavior.js";
import { createServer } from "../../../src/server/createServer.js";
import { observed } from "../../fixtures/analysisExecution.js";
import { readInvestigationWorkspace } from "../../../src/application/InvestigationWorkspaceStore.js";
import type { EvidenceFilePolicy } from "../../../src/domain/evidenceBundle.js";
import type { PermissionAuthority } from "../../../src/application/PermissionAuthority.js";
import { permissionAuthorityForRoot } from "../../fixtures/permissionAuthority.js";
it("never retains derived evidence after request cancellation", async () => {
const { session, server, client } = await connected();
const inputs = [
{
name: "find_changed_behavior",
arguments: INVESTIGATION_EXAMPLES.find_changed_behavior,
},
{
name: "build_call_path",
arguments: INVESTIGATION_EXAMPLES.build_call_path,
},
{
name: "correlate_static_and_runtime",
arguments: INVESTIGATION_EXAMPLES.correlate_static_and_runtime,
},
] as const;
for (const evidence of [
FUNCTION_COMPARISON_EXAMPLE.left,
FUNCTION_COMPARISON_EXAMPLE.right,
FUNCTION_COMPARISON_EVIDENCE,
PROCESS_CAPTURE_REFERENCE,
PROCESS_CAPTURE_RECONSTRUCTION,
PROCESS_COMPARISON_EVIDENCE,
])
expect(session.recordEvidence(evidence).ok).toBe(true);
const initialCount = session.exportEvidenceBundle().records.length;
try {
for (const input of inputs) {
const controller = new AbortController();
const request = client.callTool(input, {
signal: controller.signal,
onprogress: () => controller.abort(),
});
await expect(request).rejects.toThrow(/abort/iu);
}
await expect
.poll(() => session.exportEvidenceBundle().records.length)
.toBe(initialCount);
} finally {
await close(session, server, client);
}
}, 10_000);
it("runs and reuses a persistent cross-version workspace", async () => {
const directory = await createTestTempDirectory("rea-investigation-mcp-");
const left = join(directory, "left");
const right = join(directory, "right");
const workspace = join(directory, "workspace.json");
await Promise.all([mkdir(left), mkdir(right)]);
await Promise.all([
writeFile(join(left, "feature.txt"), "old\n"),
writeFile(join(right, "feature.txt"), "new\n"),
]);
const filePolicy = evidencePolicy(directory);
const { session, server, client } = await connected(filePolicy);
try {
const arguments_ = {
investigation_run: {
approved: true,
workspace_path: workspace,
workspace_name: "mcp-release-diff",
left_path: left,
right_path: right,
options: { page_size: 500, change_limit: 100 },
},
};
const first = await client.callTool({
name: "find_changed_behavior",
arguments: arguments_,
});
expect(first.isError).not.toBe(true);
const firstEvidence = sessionEvidence(session, first.structuredContent);
expect(firstEvidence.normalized_result).toMatchObject({
behavior_status: "unknown",
summary: { static_candidates: 2 },
investigation_run: { inventory_evidence_count: 2 },
});
expect(session.hasEvidence(firstEvidence.evidence_id)).toBe(true);
const loaded = await readInvestigationWorkspace(workspace, filePolicy);
expect(loaded).toMatchObject({
ok: true,
value: { revision: 3, runs: [{ status: "complete" }] },
});
expect(firstEvidence.normalized_result).toHaveProperty(
"investigation_run.workspace_id",
);
const second = await client.callTool({
name: "find_changed_behavior",
arguments: arguments_,
});
expect(second.isError, JSON.stringify(second)).not.toBe(true);
const secondEvidence = sessionEvidence(session, second.structuredContent);
expect(secondEvidence.evidence_id).toBe(firstEvidence.evidence_id);
expect(
await readInvestigationWorkspace(workspace, filePolicy),
).toMatchObject({ ok: true, value: { revision: 3 } });
} finally {
await close(session, server, client);
await rm(directory, { recursive: true, force: true });
}
});
it("replays a complete workspace without write authority", async () => {
const directory = await createTestTempDirectory("rea-investigation-read-");
const left = join(directory, "left");
const right = join(directory, "right");
const workspace = join(directory, "workspace.json");
await Promise.all([mkdir(left), mkdir(right)]);
await Promise.all([
writeFile(join(left, "feature.txt"), "old\n"),
writeFile(join(right, "feature.txt"), "new\n"),
]);
const filePolicy = evidencePolicy(directory);
const arguments_ = {
investigation_run: {
approved: true,
workspace_path: workspace,
workspace_name: "read-only-replay",
left_path: left,
right_path: right,
options: { page_size: 500, change_limit: 100 },
},
};
const writer = await investigationAuthority(directory, true);
const initial = await connected(filePolicy, writer);
try {
const created = await initial.client.callTool({
name: "find_changed_behavior",
arguments: arguments_,
});
expect(created.isError, JSON.stringify(created)).not.toBe(true);
} finally {
await close(initial.session, initial.server, initial.client);
}
const reader = await investigationAuthority(directory, false);
const replay = await connected(filePolicy, reader);
try {
const cached = await replay.client.callTool({
name: "find_changed_behavior",
arguments: arguments_,
});
expect(cached.isError, JSON.stringify(cached)).not.toBe(true);
expect(
await readInvestigationWorkspace(workspace, filePolicy),
).toMatchObject({ ok: true, value: { revision: 3 } });
await writeFile(join(right, "feature.txt"), "changed again\n");
const denied = await replay.client.callTool({
name: "find_changed_behavior",
arguments: arguments_,
});
expect(denied).toMatchObject({
isError: true,
structuredContent: {
error: {
code: "permission_required",
details: { capability: "investigation_workspace_write" },
},
},
});
} finally {
await close(replay.session, replay.server, replay.client);
await rm(directory, { recursive: true, force: true });
}
});
it("defers input permission for an explicit replay with deleted inputs", async () => {
const directory = await createTestTempDirectory("rea-investigation-replay-");
const left = join(directory, "left");
const right = join(directory, "right");
const workspace = join(directory, "workspace.json");
await Promise.all([mkdir(left), mkdir(right)]);
await Promise.all([
writeFile(join(left, "feature.txt"), "old\n"),
writeFile(join(right, "feature.txt"), "new\n"),
]);
const filePolicy = evidencePolicy(directory);
const investigationRun = {
approved: true,
workspace_path: workspace,
workspace_name: "selected-replay",
left_path: left,
right_path: right,
options: { page_size: 500, change_limit: 100 },
};
const writer = await investigationAuthority(directory, true);
const initial = await connected(filePolicy, writer);
let runId: string | undefined;
try {
const created = await initial.client.callTool({
name: "find_changed_behavior",
arguments: { investigation_run: investigationRun },
});
expect(created.isError, JSON.stringify(created)).not.toBe(true);
const evidence = sessionEvidence(
initial.session,
created.structuredContent,
);
const result = changedBehaviorResultSchema.parse(
evidence.normalized_result,
);
runId = result.investigation_run?.run_id;
} finally {
await close(initial.session, initial.server, initial.client);
}
if (runId === undefined) throw new Error("missing completed run ID");
await Promise.all([
rm(left, { recursive: true, force: true }),
rm(right, { recursive: true, force: true }),
]);
const workspaceOnly = await permissionAuthorityForRoot(
directory,
["investigation_workspace_read", "investigation_workspace_write"],
["investigation_workspace_read"],
);
const replay = await connected(filePolicy, workspaceOnly);
try {
const cached = await replay.client.callTool({
name: "find_changed_behavior",
arguments: {
investigation_run: {
...investigationRun,
replay_run_id: runId,
},
},
});
expect(cached.isError, JSON.stringify(cached)).not.toBe(true);
expect(
await readInvestigationWorkspace(workspace, filePolicy),
).toMatchObject({ ok: true, value: { revision: 3 } });
} finally {
await close(replay.session, replay.server, replay.client);
await rm(directory, { recursive: true, force: true });
}
});
const connected = async (
evidenceFilePolicy?: EvidenceFilePolicy,
permissionAuthority?: PermissionAuthority,
) => {
const session = createTestBinarySession(() => ({
health: () => Promise.resolve(),
execute: () => Promise.resolve(observed(null)),
close: () => Promise.resolve(),
}));
const server = createServer(session, session, {
...(evidenceFilePolicy === undefined ? {} : { evidenceFilePolicy }),
...(permissionAuthority === undefined ? {} : { permissionAuthority }),
});
const client = new Client({ name: "investigation-test", version: "1" });
const [clientTransport, serverTransport] =
InMemoryTransport.createLinkedPair();
await server.connect(serverTransport);
await client.connect(clientTransport);
return { session, server, client };
};
const evidencePolicy = (root: string): EvidenceFilePolicy => ({
roots: [root],
maxBytes: 64 * 1024 * 1024,
maxDepth: 64,
maxStringLength: 1024 * 1024,
maxNodes: 1_000_000,
});
const sessionEvidence = (session: BinarySession, value: unknown) => {
const parsed = z
.object({ evidence_id: z.string().regex(/^ev_[a-f0-9]{64}$/u) })
.parse(value);
const evidence = session.evidenceById(parsed.evidence_id);
if (evidence === undefined) throw new TypeError("Missing session Evidence");
return evidence;
};
const investigationAuthority = async (
root: string,
includeWrite: boolean,
): Promise<PermissionAuthority> =>
permissionAuthorityForRoot(
root,
["investigation_workspace_read", "investigation_workspace_write"],
[
"investigation_workspace_read",
...(includeWrite ? (["investigation_workspace_write"] as const) : []),
],
);
const close = async (
session: BinarySession,
server: Awaited<ReturnType<typeof createServer>>,
client: Client,
) => {
await Promise.allSettled([client.close(), server.close(), session.close()]);
};
@@ -1,95 +0,0 @@
import { writeFile } from "node:fs/promises";
import { join } from "node:path";
import { describe, expect, it } from "vitest";
import { createTestTempDirectory } from "../../fixtures/temporaryDirectory.js";
import { scanArtifactInventory } from "../../../src/application/ArtifactInventory.js";
import {
scanVersionInventories,
type VersionInventoryScanner,
} from "../../../src/application/CrossVersionInventory.js";
const LIMITS = {
maxEntries: 10,
maxTotalBytes: 1_024,
maxEntryBytes: 512,
maxCompressionRatio: 10,
maxDepth: 8,
maxPathBytes: 256,
} as const;
const INTEGRITY = {
mode: "fail",
} as const;
const deferred = <Value>() => {
let resolvePromise: (value: Value) => void = () => undefined;
let rejectPromise: (reason: unknown) => void = () => undefined;
const promise = new Promise<Value>((resolve, reject) => {
resolvePromise = resolve;
rejectPromise = reject;
});
return { promise, resolve: resolvePromise, reject: rejectPromise };
};
describe("cross-version inventory", () => {
it("starts both scans together and preserves left/right result order", async () => {
const root = await createTestTempDirectory("rea-version-inventory-");
const artifact = join(root, "artifact.js");
await writeFile(artifact, "export const value = 1;\n");
const snapshot = await scanArtifactInventory(artifact, LIMITS);
const left = deferred<typeof snapshot>();
const right = deferred<typeof snapshot>();
const calls: string[] = [];
const controller = new AbortController();
const scanner: VersionInventoryScanner = (...arguments_) => {
const [path, limits, options] = arguments_;
if (options === undefined) throw new Error("Expected scanner options");
calls.push(path);
expect(limits).toBe(LIMITS);
expect(options.signal).toBe(controller.signal);
expect(options.integrity).toBe(INTEGRITY);
return path === "left" ? left.promise : right.promise;
};
const result = scanVersionInventories(
{
leftPath: "left",
rightPath: "right",
limits: LIMITS,
signal: controller.signal,
integrity: INTEGRITY,
},
scanner,
);
expect(calls).toEqual(["left", "right"]);
right.resolve(snapshot);
left.resolve(snapshot);
await expect(result).resolves.toEqual({
left: snapshot,
right: snapshot,
});
});
it("rejects promptly when either parallel scan fails", async () => {
const left = deferred<never>();
const failure = new Error("right scan failed");
const scanner: VersionInventoryScanner = (path) =>
path === "left" ? left.promise : Promise.reject(failure);
await expect(
scanVersionInventories(
{
leftPath: "left",
rightPath: "right",
limits: LIMITS,
integrity: INTEGRITY,
},
scanner,
),
).rejects.toBe(failure);
});
});