fix(session): require an open target for session-bound native and artifact tools
binary_session reported native and artifact operations available whenever their provider declared them, although these operations run on the active target: in a closed session every call failed with target_unavailable, and Mach-O-only operations stayed available with a ZIP or ELF target open. Report target_required for every session-bound contract kind in a closed session, and target_unsupported for Mach-O-only operations unless the open target is a Mach-O executable. A host limitation is reported before target selection, since opening a target cannot fix it; composed operations keep deciding from their requirements. Fixes #1166 Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
co-authored by
Claude Opus 5.5
parent
de5bfec79f
commit
cf77d073fc
@@ -1,9 +1,13 @@
|
||||
import { readFile } from "node:fs/promises";
|
||||
import { readFile, writeFile } from "node:fs/promises";
|
||||
import { join } from "node:path";
|
||||
|
||||
import { Client, InMemoryTransport } from "@modelcontextprotocol/client";
|
||||
import { describe, expect, it } from "vitest";
|
||||
import { z } from "zod";
|
||||
|
||||
import { createTestBinarySession } from "../../fixtures/binarySession.js";
|
||||
import { createTestTempDirectory } from "../../fixtures/temporaryDirectory.js";
|
||||
import { machoImage } from "../../../src/artifacts/apple/MachoImage.fixture.js";
|
||||
import {
|
||||
CATALOG_IDENTITY,
|
||||
CLI_COMMAND_NAMES,
|
||||
@@ -21,11 +25,17 @@ import type {
|
||||
CapabilityDescriptor,
|
||||
} from "../../../src/application/AnalysisProvider.js";
|
||||
|
||||
const availabilityProvider = (): AnalysisProvider => {
|
||||
const availabilityProvider = (
|
||||
operations: readonly CapabilityDescriptor["operation"][] = [
|
||||
"current_address",
|
||||
],
|
||||
): AnalysisProvider => {
|
||||
const identity = { id: "fixture", name: "Fixture", version: "1" };
|
||||
const capability: CapabilityDescriptor = {
|
||||
const capability = (
|
||||
operation: CapabilityDescriptor["operation"],
|
||||
): CapabilityDescriptor => ({
|
||||
provider: identity,
|
||||
operation: "current_address",
|
||||
operation,
|
||||
available: true,
|
||||
reason: null,
|
||||
effects: {
|
||||
@@ -38,10 +48,10 @@ const availabilityProvider = (): AnalysisProvider => {
|
||||
requiresRoot: false,
|
||||
},
|
||||
limitations: [],
|
||||
};
|
||||
});
|
||||
return {
|
||||
identity: () => identity,
|
||||
capabilities: () => [capability],
|
||||
capabilities: () => operations.map(capability),
|
||||
createClient: () => ({
|
||||
health: () => Promise.resolve(),
|
||||
execute: () => Promise.resolve(observed(null)),
|
||||
@@ -248,6 +258,101 @@ describe("live server identity over MCP", () => {
|
||||
}, 10_000);
|
||||
});
|
||||
|
||||
describe("active-target availability over MCP", () => {
|
||||
it("advertises active-target tools only when a matching target is open", async () => {
|
||||
const session = createTestBinarySession(
|
||||
availabilityProvider(["inspect_macho", "inspect_artifact"]),
|
||||
);
|
||||
const server = createServer(session, session);
|
||||
const client = new Client({ name: "availability-test", version: "1" });
|
||||
const [clientTransport, serverTransport] =
|
||||
InMemoryTransport.createLinkedPair();
|
||||
const directory = await createTestTempDirectory("rea-target-availability-");
|
||||
const executable = join(directory, "tool");
|
||||
const plist = join(directory, "Info.plist");
|
||||
await writeFile(executable, machoImage({}));
|
||||
await writeFile(plist, PLIST_XML);
|
||||
try {
|
||||
await server.connect(serverTransport);
|
||||
await client.connect(clientTransport);
|
||||
const listed = await toolNames(client);
|
||||
expect(await activeTargetReasons(client)).toEqual({
|
||||
inspect_macho: "target_required",
|
||||
inspect_artifact: "target_required",
|
||||
inspect_managed_artifact: "available",
|
||||
});
|
||||
const call = await client.callTool({
|
||||
name: "inspect_macho",
|
||||
arguments: {},
|
||||
});
|
||||
expect(call.structuredContent).toMatchObject({
|
||||
error: { code: "target_unavailable" },
|
||||
});
|
||||
await openTarget(client, plist);
|
||||
expect(await activeTargetReasons(client)).toEqual({
|
||||
inspect_macho: "target_unsupported",
|
||||
inspect_artifact: "available",
|
||||
inspect_managed_artifact: "available",
|
||||
});
|
||||
await client.callTool({ name: "close_binary", arguments: {} });
|
||||
await openTarget(client, executable);
|
||||
expect(await activeTargetReasons(client)).toEqual({
|
||||
inspect_macho: "available",
|
||||
inspect_artifact: "available",
|
||||
inspect_managed_artifact: "available",
|
||||
});
|
||||
await client.callTool({ name: "close_binary", arguments: {} });
|
||||
expect((await activeTargetReasons(client)).inspect_macho).toBe(
|
||||
"target_required",
|
||||
);
|
||||
expect(await toolNames(client)).toEqual(listed);
|
||||
} finally {
|
||||
await Promise.allSettled([
|
||||
client.close(),
|
||||
server.close(),
|
||||
session.close(),
|
||||
]);
|
||||
}
|
||||
}, 10_000);
|
||||
});
|
||||
|
||||
const PLIST_XML =
|
||||
'<?xml version="1.0" encoding="UTF-8"?><plist version="1.0"><dict><key>CFBundleName</key><string>Fixture</string></dict></plist>';
|
||||
|
||||
const toolNames = async (client: Client): Promise<string[]> =>
|
||||
(await client.listTools()).tools.map(({ name }) => name);
|
||||
|
||||
const openTarget = async (client: Client, path: string): Promise<void> => {
|
||||
const opened = await client.callTool({
|
||||
name: "open_binary",
|
||||
arguments: { path },
|
||||
});
|
||||
expect(opened.isError ?? false).toBe(false);
|
||||
};
|
||||
|
||||
const activeTargetReasons = async (
|
||||
client: Client,
|
||||
): Promise<Record<string, unknown>> => {
|
||||
const status = await client.callTool({
|
||||
name: "binary_session",
|
||||
arguments: {},
|
||||
});
|
||||
const availability = z
|
||||
.object({
|
||||
result: z.object({
|
||||
tool_availability: z.array(
|
||||
z.object({ name: z.string(), reason: z.string() }).loose(),
|
||||
),
|
||||
}),
|
||||
})
|
||||
.parse(status.structuredContent).result.tool_availability;
|
||||
return Object.fromEntries(
|
||||
["inspect_macho", "inspect_artifact", "inspect_managed_artifact"].map(
|
||||
(name) => [name, availability.find((tool) => tool.name === name)?.reason],
|
||||
),
|
||||
);
|
||||
};
|
||||
|
||||
const assertLiveIdentity = async (client: Client): Promise<void> => {
|
||||
const identity = await client.callTool({
|
||||
name: "binary_session",
|
||||
|
||||
Reference in New Issue
Block a user