* perf(native): add opt-in Mach-O summary delivery and retained pages inspect_macho returns every import and export inline. For VS Code's Electron Framework one MCP call carries 2.5 MB of text (about 828k o200k tokens), and the same again as structuredContent; the MCP response budget admits it. Agents asking for slice identity, UUID or dependency counts paid that cost on every first call. Add the established opt-in detail selection: omitted or complete keeps the existing complete result and Evidence identity, while summary retains the complete Evidence and returns slice identity with per-collection returned/total/exhaustive counts (956 tokens). inspect_analysis_view kind macho pages the original rows of any collection or the command provenance, repeating the producer's own total and exhaustive flag so page exhaustion is never read as absence. Delivery detail never reaches the native tools or the source Evidence ID: registerEvidenceTools gains a parametersForEvidence hook, so the summary's parent is the same Evidence as a complete call. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * fix(mcp): refuse unretainable summary delivery before analysis A server without Evidence retention (a fixed analysis source) ran the complete inspect_macho or inspect_artifact analysis for detail summary and only then refused it in summarizeRetainedAnalysis, paying the very cost summary delivery exists to avoid. registerEvidenceTools gains a preflight hook; both registrations check retention there, as the WASM, EVM, managed, pcap and capture handlers already do before dispatch. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * fix(native): keep whole rows in Mach-O architecture and segment pages The page item union listed plain-object row arrays, and zod returns the first union member that parses while dropping unknown keys. Entrypoint rows declare only file_offset, so every architecture and segment row (which also carry a numeric file_offset) matched it and lost its name, CPU fields, permissions and sections. Rows now use exact key sets, and imports/exports share one member. The MCP test pages every facet of a real /bin/ls and compares it with the unparsed complete rows. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> --------- Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
217 lines
7.7 KiB
TypeScript
217 lines
7.7 KiB
TypeScript
import { silentLogger } from "../../../src/logger.js";
|
|
import { copyFile, writeFile } from "node:fs/promises";
|
|
import { join } from "node:path";
|
|
|
|
import { describe, expect } from "vitest";
|
|
import { z } from "zod";
|
|
|
|
import { createBinarySession } from "../../../src/composition/binary.js";
|
|
import { parseConfig } from "../../../src/config/parseConfig.js";
|
|
import { inspectMachoSchema } from "../../../src/domain/native/nativeInspection.js";
|
|
import { createServer } from "../../../src/server/createServer.js";
|
|
import { thinMach } from "../../../src/domain/binaryTarget.fixture.js";
|
|
import { createTestBinarySession } from "../../fixtures/binarySession.js";
|
|
import { observed } from "../../fixtures/analysisExecution.js";
|
|
import { ok } from "../../../src/domain/result.js";
|
|
import { parseMcpToolError } from "../../fixtures/mcpToolError.js";
|
|
import { createTestTempDirectory } from "../../fixtures/temporaryDirectory.js";
|
|
import { mcpTest } from "../../support/mcp/mcpFixture.js";
|
|
|
|
const evidenceOf = <Schema extends z.ZodType>(schema: Schema) =>
|
|
z.object({
|
|
evidence_id: z.string(),
|
|
parameters: z.record(z.string(), z.unknown()),
|
|
normalized_result: schema,
|
|
});
|
|
const countSchema = z.object({
|
|
returned: z.number(),
|
|
total: z.number().nullable(),
|
|
exhaustive: z.boolean(),
|
|
});
|
|
const summarySchema = z.object({
|
|
kind: z.literal("summary"),
|
|
parent_evidence_id: z.string(),
|
|
summary: z.object({
|
|
uuid: z.string().nullable(),
|
|
collections: z.record(z.string(), countSchema),
|
|
provenance_count: z.number(),
|
|
}),
|
|
});
|
|
const pageSchema = z.object({
|
|
kind: z.literal("macho"),
|
|
item: z.array(z.unknown()),
|
|
coverage: z.object({ next_offset: z.number().nullable() }),
|
|
});
|
|
|
|
describe.skipIf(process.platform !== "darwin")(
|
|
"Mach-O summary delivery through MCP",
|
|
() => {
|
|
mcpTest(
|
|
"retains complete inspect_macho Evidence and pages its exact rows",
|
|
async ({ mcp, onTestFinished }) => {
|
|
const target = join(
|
|
await createTestTempDirectory("rea-macho-summary-mcp-"),
|
|
"ls",
|
|
);
|
|
await copyFile("/bin/ls", target);
|
|
const configured = parseConfig({ REA_ANALYSIS_PROVIDER: "auto" });
|
|
if (!configured.ok) throw configured.error;
|
|
const session = createBinarySession(
|
|
configured.value,
|
|
silentLogger,
|
|
process.env,
|
|
);
|
|
onTestFinished(async () => {
|
|
await session.close();
|
|
});
|
|
const client = await mcp.connect(
|
|
createServer({ kind: "session", session }),
|
|
);
|
|
const opened = await client.callTool({
|
|
name: "open_binary",
|
|
arguments: { path: target },
|
|
});
|
|
expect(opened.isError, JSON.stringify(opened.content)).not.toBe(true);
|
|
|
|
const complete = await client.callTool({
|
|
name: "inspect_macho",
|
|
arguments: {},
|
|
});
|
|
expect(complete.isError, JSON.stringify(complete.content)).not.toBe(
|
|
true,
|
|
);
|
|
const full = evidenceOf(inspectMachoSchema).parse(
|
|
complete.structuredContent,
|
|
);
|
|
// Omitted detail keeps the default Evidence parameters unchanged.
|
|
expect(full.parameters).toEqual({});
|
|
|
|
const summarized = await client.callTool({
|
|
name: "inspect_macho",
|
|
arguments: { detail: "summary" },
|
|
});
|
|
expect(summarized.isError, JSON.stringify(summarized.content)).not.toBe(
|
|
true,
|
|
);
|
|
const summary = evidenceOf(summarySchema).parse(
|
|
summarized.structuredContent,
|
|
).normalized_result;
|
|
// Delivery detail never reaches the tools or the source Evidence ID.
|
|
expect(summary.parent_evidence_id).toBe(full.evidence_id);
|
|
const report = full.normalized_result;
|
|
expect(summary.summary.uuid).toBe(report.uuid);
|
|
expect(summary.summary.provenance_count).toBe(report.provenance.length);
|
|
for (const name of ["imports", "exports", "dependencies"] as const)
|
|
expect(summary.summary.collections[name]).toEqual({
|
|
returned: report[name].items.length,
|
|
total: report[name].total,
|
|
exhaustive: report[name].exhaustive,
|
|
});
|
|
expect(JSON.stringify(summarized.structuredContent)).not.toContain(
|
|
report.imports.items[0]?.name ?? "_",
|
|
);
|
|
|
|
// Every facet must reassemble the unparsed complete rows exactly.
|
|
const raw = z
|
|
.object({ normalized_result: z.record(z.string(), z.unknown()) })
|
|
.parse(complete.structuredContent).normalized_result;
|
|
const facets = [
|
|
"entrypoints",
|
|
"architectures",
|
|
"build_metadata",
|
|
"load_commands",
|
|
"dependencies",
|
|
"imports",
|
|
"exports",
|
|
"segments",
|
|
"provenance",
|
|
] as const;
|
|
for (const facet of facets) {
|
|
const rows: unknown[] = [];
|
|
let offset: number | null = 0;
|
|
while (offset !== null) {
|
|
const paged = await client.callTool({
|
|
name: "inspect_analysis_view",
|
|
arguments: {
|
|
source: {
|
|
kind: "retained-evidence",
|
|
evidence_id: summary.parent_evidence_id,
|
|
},
|
|
view: { kind: "macho", facet, offset, limit: 16 },
|
|
},
|
|
});
|
|
expect(paged.isError, JSON.stringify(paged.content)).not.toBe(true);
|
|
const page = evidenceOf(pageSchema).parse(
|
|
paged.structuredContent,
|
|
).normalized_result;
|
|
rows.push(...page.item);
|
|
offset = page.coverage.next_offset;
|
|
}
|
|
expect(rows, facet).toEqual(
|
|
facet === "provenance"
|
|
? raw.provenance
|
|
: z.object({ items: z.array(z.unknown()) }).parse(raw[facet])
|
|
.items,
|
|
);
|
|
}
|
|
|
|
const wrongKind = await client.callTool({
|
|
name: "inspect_analysis_view",
|
|
arguments: {
|
|
source: {
|
|
kind: "retained-evidence",
|
|
evidence_id: summary.parent_evidence_id,
|
|
},
|
|
view: { kind: "wasm", facet: "exports" },
|
|
},
|
|
});
|
|
expect(wrongKind.isError).toBe(true);
|
|
expect(JSON.stringify(wrongKind.content)).toContain(
|
|
"Mach-O inspection supports summary and macho views.",
|
|
);
|
|
},
|
|
);
|
|
},
|
|
);
|
|
|
|
mcpTest(
|
|
"refuses summary delivery before analysis when the server retains no Evidence",
|
|
async ({ mcp, onTestFinished }) => {
|
|
const target = join(
|
|
await createTestTempDirectory("rea-summary-preflight-"),
|
|
"tool",
|
|
);
|
|
await writeFile(target, thinMach(0xfeedfacf, 0x0100000c));
|
|
const executed: string[] = [];
|
|
const session = createTestBinarySession(() => ({
|
|
execute: (operation) => {
|
|
executed.push(operation);
|
|
return Promise.resolve(observed(null));
|
|
},
|
|
close: () => Promise.resolve(ok(null)),
|
|
}));
|
|
onTestFinished(async () => {
|
|
await session.close();
|
|
});
|
|
const opened = await session.open(target);
|
|
if (!opened.ok) throw opened.error;
|
|
const client = await mcp.connect(
|
|
createServer({ kind: "fixed", analysis: session }),
|
|
);
|
|
const refused = [];
|
|
for (const name of ["inspect_macho", "inspect_artifact"])
|
|
refused.push(
|
|
await client.callTool({ name, arguments: { detail: "summary" } }),
|
|
);
|
|
// Neither analysis ran only to be discarded by the delivery refusal.
|
|
expect(executed.filter((operation) => operation !== "health")).toEqual([]);
|
|
for (const result of refused)
|
|
expect(parseMcpToolError(result)).toMatchObject({
|
|
error: {
|
|
code: "capability_unavailable",
|
|
message: expect.stringContaining("does not retain Evidence"),
|
|
},
|
|
});
|
|
},
|
|
);
|