mirror of
https://github.com/morluto/rea.git
synced 2026-12-04 22:11:12 +00:00
Replace bare toThrow() across the MCP, CLI and other boundary tests, the Hopper and fixture conformance verifiers, the evaluation guards and the process-global tests with the SDK cancellation code and reason, schema issue paths, tagged error reasons, cleanup facts, errno codes and verifier messages each case is meant to refuse.
182 lines
6.7 KiB
TypeScript
182 lines
6.7 KiB
TypeScript
import { unknownTarget } from "../../../src/domain/targetIdentity.js";
|
|
import { Client, InMemoryTransport } from "@modelcontextprotocol/client";
|
|
import type { CallToolResult } from "@modelcontextprotocol/server";
|
|
import { access, mkdir, rm, writeFile } from "node:fs/promises";
|
|
import { join } from "node:path";
|
|
import { expect, it, vi } from "vitest";
|
|
import { createAnalysisExecution } from "../../../src/ports/AnalysisProvider.js";
|
|
import { BinarySession } from "../../../src/application/binary/BinarySession.js";
|
|
import { InvestigationRecords } from "../../../src/application/investigation/InvestigationRecords.js";
|
|
import { createEvidence } from "../../../src/domain/evidence.js";
|
|
import { ProviderCleanupError } from "../../../src/domain/providerCleanupError.js";
|
|
import { err, ok } from "../../../src/domain/result.js";
|
|
import { createServer } from "../../../src/server/createServer.js";
|
|
import { createTestProviderRouter } from "../../fixtures/binarySession.js";
|
|
import { createTestTempDirectory } from "../../fixtures/temporaryDirectory.js";
|
|
import { parseMcpToolError } from "../../fixtures/mcpToolError.js";
|
|
import { phaseGate } from "../../support/lifecycleHarness.js";
|
|
import { releaseOwnedCleanup } from "../../support/cleanupRelease.js";
|
|
import { createMcpSchemaValidation } from "../../fixtures/mcpSchemaValidation.js";
|
|
|
|
const expectRecoveredBatch = (response: CallToolResult, owned: string) => {
|
|
expect(parseMcpToolError(response).error).toMatchObject({
|
|
code: "cleanup_incomplete",
|
|
details: {
|
|
resources: [owned],
|
|
partial_observation: {
|
|
evidence: {
|
|
normalized_result: { items: [{ status: "ok" }], total: 1 },
|
|
},
|
|
},
|
|
secondary_failures: expect.arrayContaining([
|
|
{
|
|
code: "resource_constraint",
|
|
category: "resource_constraint",
|
|
message: expect.any(String),
|
|
retryable: true,
|
|
retry_class: "same_call",
|
|
remediation: expect.any(Object),
|
|
details: expect.objectContaining({ operation: "record_evidence" }),
|
|
},
|
|
]),
|
|
},
|
|
});
|
|
};
|
|
|
|
it("keeps a batch cleanup owner and its completed prefix after source retention fails", async () => {
|
|
const root = await createTestTempDirectory("rea-batch-owned-cleanup-");
|
|
const target = join(root, "fixture.com"),
|
|
owned = join(root, "owned");
|
|
await writeFile(target, Buffer.from([0xb8, 0, 0x4c, 0xcd, 0x21]));
|
|
const provider = { id: "fixture", name: "Fixture", version: "1" };
|
|
const source = createEvidence(unknownTarget("not-observed"), provider, {
|
|
operation: "health",
|
|
parameters: {},
|
|
result: "source",
|
|
});
|
|
const failure = new ProviderCleanupError(provider.id, [owned], {
|
|
reason: "controlled cleanup failure",
|
|
});
|
|
let count = 0,
|
|
pending = false,
|
|
allow = false,
|
|
closes = 0;
|
|
const finalCleanup = phaseGate();
|
|
const session = new BinarySession(
|
|
createTestProviderRouter(() => ({
|
|
execute: async (operation) => {
|
|
if (operation === "procedure_pseudo_code" && ++count === 2) {
|
|
await mkdir(owned);
|
|
pending = true;
|
|
return err(failure);
|
|
}
|
|
return ok(
|
|
createAnalysisExecution(
|
|
operation === "procedure_address"
|
|
? "0x100"
|
|
: operation === "address_name"
|
|
? "example"
|
|
: operation === "health"
|
|
? null
|
|
: "int example(void) { return 1; }",
|
|
provider,
|
|
),
|
|
);
|
|
},
|
|
close: async () => {
|
|
closes++;
|
|
if (!pending) return ok(null);
|
|
if (closes === 2) {
|
|
await finalCleanup.hold();
|
|
return err(failure);
|
|
}
|
|
if (!allow) return err(failure);
|
|
await rm(owned, { recursive: true });
|
|
pending = false;
|
|
releaseOwnedCleanup(failure);
|
|
return ok(null);
|
|
},
|
|
})),
|
|
new InvestigationRecords({ maximumRepresentationBytes: 0 }),
|
|
);
|
|
const admit = session.withAdmittedAnalysis.bind(session);
|
|
vi.spyOn(session, "withAdmittedAnalysis").mockImplementation(
|
|
(operation, signal, run) =>
|
|
admit(operation, signal, (analysis) =>
|
|
run({
|
|
...analysis,
|
|
execute: async (...args) => {
|
|
const result = await analysis.execute(...args);
|
|
if (args[0] === "procedure_pseudo_code" && result.ok)
|
|
return {
|
|
...result,
|
|
value: { ...result.value, sourceEvidence: [source] },
|
|
};
|
|
return result;
|
|
},
|
|
}),
|
|
),
|
|
);
|
|
const server = createServer({ kind: "session", session });
|
|
const client = new Client(
|
|
{ name: "batch-owned-cleanup", version: "1" },
|
|
{ jsonSchemaValidator: createMcpSchemaValidation() },
|
|
);
|
|
const [ct, st] = InMemoryTransport.createLinkedPair();
|
|
try {
|
|
await server.connect(st);
|
|
await client.connect(ct);
|
|
const call = (name: string, args: Record<string, unknown> = {}) =>
|
|
client.callTool({ name, arguments: args }, { timeout: 120000 });
|
|
expect(
|
|
(await call("open_binary", { path: target, format: "dos-com" })).isError,
|
|
).not.toBe(true);
|
|
const response = await call("batch_decompile", {
|
|
addresses: ["first", "second"],
|
|
});
|
|
expectRecoveredBatch(response, owned);
|
|
expect(pending).toBe(true);
|
|
expect(failure.cleanupIncomplete).toBe(true);
|
|
await expect(access(owned)).resolves.toBeUndefined();
|
|
const close1 = await call("close_binary");
|
|
expect(close1.isError).toBe(true);
|
|
expect(parseMcpToolError(close1).error).toMatchObject({
|
|
code: "cleanup_incomplete",
|
|
recovery: { kind: "retry_cleanup", operation: "close_binary" },
|
|
details: { resources: [owned] },
|
|
});
|
|
expect(pending).toBe(true);
|
|
const controller = new AbortController();
|
|
const cancelledClose = client.callTool(
|
|
{ name: "close_binary", arguments: {} },
|
|
{ signal: controller.signal, timeout: 120000 },
|
|
);
|
|
try {
|
|
await finalCleanup.reached(cancelledClose, "final cleanup");
|
|
controller.abort();
|
|
await expect(cancelledClose).rejects.toMatchObject({
|
|
code: "REQUEST_TIMEOUT",
|
|
message: expect.stringContaining("AbortError"),
|
|
});
|
|
} finally {
|
|
controller.abort();
|
|
finalCleanup.release();
|
|
await Promise.allSettled([cancelledClose]);
|
|
}
|
|
allow = true;
|
|
const close2 = await call("close_binary");
|
|
expect(close2.isError).not.toBe(true);
|
|
expect(pending).toBe(false);
|
|
expect(failure.cleanupIncomplete).toBe(false);
|
|
expect(closes).toBe(3);
|
|
await expect(access(owned)).rejects.toMatchObject({ code: "ENOENT" });
|
|
} finally {
|
|
allow = true;
|
|
finalCleanup.release();
|
|
await client.close();
|
|
await server.close();
|
|
await session.close();
|
|
vi.restoreAllMocks();
|
|
}
|
|
});
|