Files
reverse-engineering-anything/src/application/binary/sessionAdmission.test.ts
T
morluto 402ce20b80 fix(session): declare, admit and advertise the open-target prerequisite through one owner (#15356)
* fix(contracts): declare the open-target prerequisite once in availability

Replace the requiresActiveTarget boolean with an "active-target" value of the
availability declaration. 18 contracts that read a required caller path, all
declared standalone or host-restricted or named inventory rules, no longer
claim to need an open target.

* fix(session): admit tools through one session evaluator shared with binary_session

binary_session availability and call-time dispatch now read one admission
evaluator for active-target tools and external mutations. Resource-only
lane refusals and Ghidra or resource-only external-mutation refusals carry
the open_binary or close_binary recovery that binary_session reports,
instead of a generic capability or unreadable-output error. Packet
capture, Wakaru, firmware and mitmproxy refuse an unsupported host before
reading tool configuration.

* fix(mcp): advertise the open-target prerequisite and name open_binary on first contact

Each active-target tool's advertised description ends with one sentence
derived from its availability declaration. When the SDK refuses such a
tool's arguments while the session would also refuse it, the
invalid_request remediation names the session recovery first, read from
the same admission evaluator as binary_session and dispatch.

* fix(mcp): refuse composed workflows as a whole and keep scoped recovery and catalog identity in step

A composed workflow such as batch_decompile now refuses with the
advertised open_binary or close_binary recovery before it runs, instead of
reporting a batch of failed children. Malformed arguments on a connection
that does not advertise session tools lead with the scoped restart
recovery, and catalog digests cover the advertised prerequisite sentence.
The prerequisite test reads union alternatives.
2026-12-01 03:55:48 +00:00

116 lines
4.1 KiB
TypeScript

import { writeFile } from "node:fs/promises";
import { join } from "node:path";
import { describe, expect, it, vi } from "vitest";
import { createTestTempDirectory } from "../../../tests/fixtures/temporaryDirectory.js";
import { TOOL_CONTRACTS } from "../../contracts/toolContracts.js";
import { analysisErrorRemediationAction } from "../../domain/analysisErrorPresentation.js";
import { projectAnalysisError } from "../../domain/analysisErrorProjection.js";
import { ok } from "../../domain/result.js";
import {
createAnalysisExecution,
type AnalysisClient,
type AnalysisOperation,
} from "../../ports/AnalysisProvider.js";
import { buildCapabilityInventory } from "../CapabilityInventory.js";
import { InvestigationRecords } from "../investigation/InvestigationRecords.js";
import { AnalysisProviderRegistry } from "./AnalysisProviderRegistry.js";
import { BinarySession } from "./BinarySession.js";
import { SessionProviderRouter } from "./SessionProviderRouter.js";
const identity = {
id: "bundle-resource-fixture",
name: "Fixture",
version: "1",
};
const resourceClient: AnalysisClient = {
execute: async (operation) =>
ok(createAnalysisExecution({ operation, target_sha256: null }, identity)),
close: async () => ok(null),
};
const createSession = () =>
new BinarySession(
SessionProviderRouter.selectable(new AnalysisProviderRegistry([]), []),
new InvestigationRecords(),
{ createClient: () => ({ client: resourceClient, capabilities: [] }) },
);
const openResources = async (session: BinarySession): Promise<void> => {
const root = await createTestTempDirectory("rea-session-admission-");
await writeFile(join(root, "Info.plist"), "<plist><dict/></plist>");
const opened = await session.openBundleResources(root);
if (!opened.ok) throw opened.error;
};
const activeTargetTools = TOOL_CONTRACTS.filter(
(contract) =>
"availability" in contract && contract.availability === "active-target",
).map(({ name }) => name);
/**
* Each active-target tool's call-time refusal carries the remediation that
* binary_session advertises for it, in every session lane.
*/
describe("session admission parity", () => {
it.each([
["no target", async () => {}],
["a resource-only session", openResources],
] as const)(
"refuses each active-target tool with its advertised remediation in %s",
async (_lane, prepare) => {
const session = createSession();
try {
await prepare(session);
const inventory = new Map(
buildCapabilityInventory(await session.status(), {
processCaptureEnabled: false,
}).map((tool) => [tool.name, tool]),
);
for (const name of activeTargetTools) {
const advertised = inventory.get(name);
const called = await session.execute(name as AnalysisOperation, {});
if (advertised?.available === true) {
expect(called.ok, name).toBe(true);
continue;
}
if (called.ok) throw new Error(`${name} was admitted`);
expect(advertised, name).toMatchObject({
reason: expect.stringMatching(/^target_(required|unsupported)$/u),
});
expect(analysisErrorRemediationAction(called.error), name).toBe(
advertised?.remediation,
);
}
} finally {
await session.close();
}
},
);
it("refuses an external mutation beside a resource-only session and names close_binary", async () => {
const session = createSession();
try {
await openResources(session);
const mutate = vi.fn(async () => ok("mutated"));
const admitted = await session.withAdmittedExternalMutation(
undefined,
undefined,
mutate,
(_value, error) => error,
);
if (admitted.ok) throw new Error("external mutation was admitted");
expect(mutate).not.toHaveBeenCalled();
expect(projectAnalysisError(admitted.error)).toMatchObject({
code: "capability_unavailable",
remediation: {
action: expect.stringContaining("Call close_binary"),
},
});
} finally {
await session.close();
}
});
});