feat(mcp): select views of retained analysis results (#1180)
Add explicit summary, facet, item and paged views of retained analysis Evidence without repeating analysis. Preserve captured observations, graph validation, artifact identity, coverage and complete default results. Fixes #1050 Verified with focused source, CLI, pinned-SDK schema and MCP tests, serial static checks, and one complete native Windows Notion ASAR workflow. Real Hopper/Ghidra verification was not performed for this provider-independent projection change. Co-authored-by: Matt Van Horn <455140+mvanhorn@users.noreply.github.com>
This commit is contained in:
co-authored by
Matt Van Horn
parent
911152a04d
commit
db675f1d52
@@ -0,0 +1,82 @@
|
||||
import { mkdir, writeFile } from "node:fs/promises";
|
||||
import { join } from "node:path";
|
||||
|
||||
import { expect } from "vitest";
|
||||
|
||||
import { analysisViewLayoutEvidence } from "../../fixtures/analysisView.js";
|
||||
import { createTestTempDirectory } from "../../fixtures/temporaryDirectory.js";
|
||||
import { cliTest } from "../../support/cli/cliFixture.js";
|
||||
|
||||
cliTest(
|
||||
"projects one section from a JSON file of inline layout Evidence",
|
||||
async ({ cli }) => {
|
||||
const root = await createTestTempDirectory("rea-analysis-view-cli-");
|
||||
const parent = analysisViewLayoutEvidence();
|
||||
const request = {
|
||||
source: { kind: "inline", evidence: parent },
|
||||
view: {
|
||||
kind: "item",
|
||||
collection: "sections",
|
||||
selector: { name: ".data" },
|
||||
},
|
||||
};
|
||||
const input = join(root, "view.json");
|
||||
await mkdir(root, { recursive: true });
|
||||
await writeFile(input, JSON.stringify(request));
|
||||
const result = await cli.run({
|
||||
arguments: ["inspect-analysis-view", input, "--json"],
|
||||
environment: {
|
||||
HOME: root,
|
||||
XDG_CONFIG_HOME: root,
|
||||
XDG_CACHE_HOME: root,
|
||||
},
|
||||
});
|
||||
expect(result.exitCode).toBe(0);
|
||||
expect(result.json).toMatchObject({
|
||||
operation: "inspect_analysis_view",
|
||||
normalized_result: {
|
||||
kind: "item",
|
||||
parent_evidence_id: parent.evidence_id,
|
||||
item: { name: { display: ".data" } },
|
||||
},
|
||||
});
|
||||
},
|
||||
);
|
||||
|
||||
cliTest("rejects malformed JSON before projection", async ({ cli }) => {
|
||||
const root = await createTestTempDirectory("rea-analysis-view-cli-bad-");
|
||||
const result = await cli.run({
|
||||
arguments: ["inspect-analysis-view", "{", "--json"],
|
||||
environment: {
|
||||
HOME: root,
|
||||
XDG_CONFIG_HOME: root,
|
||||
XDG_CACHE_HOME: root,
|
||||
},
|
||||
});
|
||||
expect(result.exitCode).toBe(1);
|
||||
expect(result.json).toMatchObject({
|
||||
code: "invalid_request",
|
||||
category: "invalid_input",
|
||||
details: { operation: "inspect-analysis-view" },
|
||||
});
|
||||
});
|
||||
|
||||
cliTest("rejects a request that omits source", async ({ cli }) => {
|
||||
const root = await createTestTempDirectory("rea-analysis-view-cli-missing-");
|
||||
const input = join(root, "missing-source.json");
|
||||
await writeFile(input, JSON.stringify({ view: { kind: "summary" } }));
|
||||
const result = await cli.run({
|
||||
arguments: ["inspect-analysis-view", input, "--json"],
|
||||
environment: {
|
||||
HOME: root,
|
||||
XDG_CONFIG_HOME: root,
|
||||
XDG_CACHE_HOME: root,
|
||||
},
|
||||
});
|
||||
expect(result.exitCode).toBe(1);
|
||||
expect(result.json).toMatchObject({
|
||||
code: "invalid_request",
|
||||
category: "invalid_input",
|
||||
details: { operation: "inspect-analysis-view" },
|
||||
});
|
||||
});
|
||||
@@ -7,6 +7,7 @@ import { createTestTempDirectory } from "../../fixtures/temporaryDirectory.js";
|
||||
import { cliTest } from "../../support/cli/cliFixture.js";
|
||||
|
||||
const JSON_COMMANDS = [
|
||||
["inspect-analysis-view", "inspect-analysis-view"],
|
||||
["trace-application-feature", "trace-application-feature"],
|
||||
["trace-javascript-semantics", "trace-javascript-semantics"],
|
||||
["compare-application-versions", "compare-application-versions"],
|
||||
|
||||
@@ -10,6 +10,7 @@ import { createTestTempDirectory } from "../../fixtures/temporaryDirectory.js";
|
||||
|
||||
import { SUPPORTED_CLIENT_DEFINITIONS } from "../../../src/application/SupportedClients.js";
|
||||
import {
|
||||
ANALYSIS_VIEW_PROVIDER,
|
||||
ANDROID_APPLICATION_PROVIDER,
|
||||
APPLE_APPLICATION_PROVIDER,
|
||||
ARTIFACT_GRAPH_PROVIDER,
|
||||
@@ -132,6 +133,7 @@ describe("canonical product catalog", () => {
|
||||
JAVASCRIPT_APPLICATION_PROVIDER,
|
||||
JAVASCRIPT_RUNTIME_RECONCILIATION_PROVIDER,
|
||||
JAVASCRIPT_APPLICATION_WORKFLOW_PROVIDER,
|
||||
ANALYSIS_VIEW_PROVIDER,
|
||||
WEB_SCRIPT_EXPORT_PROVIDER,
|
||||
]
|
||||
.map(({ id }) => id)
|
||||
@@ -178,6 +180,10 @@ describe("canonical product catalog", () => {
|
||||
"trace_application_feature",
|
||||
"trace_javascript_semantics",
|
||||
]);
|
||||
expect(
|
||||
catalog.providers.find(({ id }) => id === ANALYSIS_VIEW_PROVIDER.id)
|
||||
?.capabilities,
|
||||
).toEqual(["inspect_analysis_view"]);
|
||||
expect(
|
||||
catalog.providers.find(({ id }) => id === ANDROID_APPLICATION_PROVIDER.id)
|
||||
?.capabilities,
|
||||
|
||||
@@ -0,0 +1,270 @@
|
||||
import { Client, InMemoryTransport } from "@modelcontextprotocol/client";
|
||||
import { Ajv2020 } from "ajv/dist/2020.js";
|
||||
import { expect, it, onTestFinished } from "vitest";
|
||||
import { z } from "zod";
|
||||
|
||||
import { BinaryLayoutService } from "../../../src/application/binaryDiagnostics/BinaryLayoutService.js";
|
||||
import { toolContract } from "../../../src/contracts/toolContracts.js";
|
||||
import { parseEvidence } from "../../../src/domain/evidence.js";
|
||||
import { ok } from "../../../src/domain/result.js";
|
||||
import {
|
||||
createJavaScriptApplicationGraph,
|
||||
createJavaScriptApplicationNode,
|
||||
} from "../../../src/domain/javascript/javascriptApplicationGraph.js";
|
||||
import { createServer } from "../../../src/server/createServer.js";
|
||||
import {
|
||||
analysisViewJavaScriptEvidence,
|
||||
analysisViewJavaScriptAnalysisWithSource,
|
||||
analysisViewBindJavaScriptGraphs,
|
||||
analysisViewLayoutFixture,
|
||||
} from "../../fixtures/analysisView.js";
|
||||
import { createTestBinarySession } from "../../fixtures/binarySession.js";
|
||||
import { BINARY_LAYOUT_TEST_PROVIDER } from "../../fixtures/binaryDiagnostics/layout.js";
|
||||
|
||||
const connect = async (binaryLayout?: BinaryLayoutService) => {
|
||||
const session = createTestBinarySession(() => {
|
||||
throw new Error("selected views must not start a deep provider");
|
||||
});
|
||||
const server = createServer(
|
||||
session,
|
||||
session,
|
||||
binaryLayout === undefined ? {} : { binaryLayout },
|
||||
);
|
||||
const client = new Client({ name: "analysis-view-mcp", version: "1" });
|
||||
onTestFinished(async () => {
|
||||
await client.close();
|
||||
await server.close();
|
||||
await session.close();
|
||||
});
|
||||
const [clientTransport, serverTransport] =
|
||||
InMemoryTransport.createLinkedPair();
|
||||
await server.connect(serverTransport);
|
||||
await client.connect(clientTransport);
|
||||
return { client, session };
|
||||
};
|
||||
|
||||
it("advertises exact schemas and projects one layout section from retained Evidence", async () => {
|
||||
const layout = analysisViewLayoutFixture();
|
||||
const service = new BinaryLayoutService({
|
||||
identity: BINARY_LAYOUT_TEST_PROVIDER,
|
||||
inspect: () => Promise.resolve(ok(layout)),
|
||||
});
|
||||
const { client, session } = await connect(service);
|
||||
const advertised = (await client.listTools()).tools.find(
|
||||
(tool) => tool.name === "inspect_analysis_view",
|
||||
);
|
||||
if (advertised?.outputSchema === undefined)
|
||||
throw new Error("inspect_analysis_view must publish both schemas");
|
||||
const ajv = new Ajv2020({ strict: false, validateFormats: false });
|
||||
const inputSchema: Record<string, unknown> = advertised.inputSchema;
|
||||
const outputSchema: Record<string, unknown> = advertised.outputSchema;
|
||||
expect(ajv.validateSchema(inputSchema)).toBe(true);
|
||||
expect(ajv.validateSchema(outputSchema)).toBe(true);
|
||||
expect(
|
||||
ajv.validate(inputSchema, {
|
||||
source: {
|
||||
kind: "retained-evidence",
|
||||
evidence_id: `ev_${"a".repeat(64)}`,
|
||||
},
|
||||
view: { kind: "summary", approval: true },
|
||||
}),
|
||||
).toBe(false);
|
||||
const inspected = await client.callTool({
|
||||
name: "inspect_binary_layout",
|
||||
arguments: { path: layout.artifact.path },
|
||||
});
|
||||
expect(inspected.isError).not.toBe(true);
|
||||
const parent = toolContract("inspect_binary_layout").outputSchema.parse(
|
||||
inspected.structuredContent,
|
||||
);
|
||||
const response = await client.callTool({
|
||||
name: "inspect_analysis_view",
|
||||
arguments: {
|
||||
source: {
|
||||
kind: "retained-evidence",
|
||||
evidence_id: parent.evidence_id,
|
||||
},
|
||||
view: {
|
||||
kind: "item",
|
||||
collection: "sections",
|
||||
selector: { name: ".data" },
|
||||
},
|
||||
},
|
||||
});
|
||||
expect(response.isError).not.toBe(true);
|
||||
expect(
|
||||
ajv.validate(outputSchema, response.structuredContent),
|
||||
JSON.stringify(ajv.errors),
|
||||
).toBe(true);
|
||||
const parsed = toolContract("inspect_analysis_view").outputSchema.parse(
|
||||
response.structuredContent,
|
||||
);
|
||||
expect(parsed.result).toMatchObject({
|
||||
kind: "item",
|
||||
parent_evidence_id: parent.evidence_id,
|
||||
item: { name: { display: ".data" } },
|
||||
});
|
||||
expect(parsed.result).toEqual(
|
||||
parseEvidence(parsed.evidence).normalized_result,
|
||||
);
|
||||
expect(session.evidenceById(parsed.evidence_id)).toEqual(
|
||||
parseEvidence(parsed.evidence),
|
||||
);
|
||||
});
|
||||
|
||||
it("retains an oversized selected observation and keeps subsequent views usable", async () => {
|
||||
const { client, session } = await connect();
|
||||
const analysis = analysisViewJavaScriptAnalysisWithSource();
|
||||
const original = analysis.graph.nodes[0];
|
||||
if (original === undefined) throw new Error("missing module");
|
||||
const source = "\0".repeat(500_000);
|
||||
const node = createJavaScriptApplicationNode({
|
||||
kind: original.kind,
|
||||
identity: original.identity,
|
||||
observations: original.observations.map((observation) => ({
|
||||
label: observation.label,
|
||||
properties: { ...observation.properties, source },
|
||||
evidence: observation.evidence,
|
||||
})),
|
||||
});
|
||||
const graph = createJavaScriptApplicationGraph({
|
||||
schema: "JavaScriptApplicationGraph",
|
||||
root_node_ids: [node.node_id],
|
||||
nodes: [node],
|
||||
edges: [],
|
||||
coverage: analysis.graph.coverage,
|
||||
limitations: analysis.graph.limitations,
|
||||
});
|
||||
const parent = analysisViewJavaScriptEvidence(
|
||||
analysisViewBindJavaScriptGraphs(analysis, graph),
|
||||
);
|
||||
expect(session.recordEvidence(parent).ok).toBe(true);
|
||||
const response = await client.callTool({
|
||||
name: "inspect_analysis_view",
|
||||
arguments: {
|
||||
source: { kind: "retained-evidence", evidence_id: parent.evidence_id },
|
||||
view: {
|
||||
kind: "item",
|
||||
collection: "modules",
|
||||
selector: { node_id: node.node_id },
|
||||
},
|
||||
},
|
||||
});
|
||||
expect(response.isError).toBe(true);
|
||||
const reference = z
|
||||
.object({
|
||||
error: z.object({
|
||||
details: z.object({
|
||||
reported_limits: z.object({
|
||||
evidence_reference: z.object({
|
||||
kind: z.literal("retained-evidence"),
|
||||
evidence_id: z.string(),
|
||||
}),
|
||||
}),
|
||||
}),
|
||||
}),
|
||||
})
|
||||
.parse(response.structuredContent).error.details
|
||||
.reported_limits.evidence_reference;
|
||||
expect(
|
||||
session.evidenceById(reference.evidence_id)?.normalized_result,
|
||||
).toMatchObject({
|
||||
item: {
|
||||
observations: [
|
||||
expect.objectContaining({
|
||||
properties: expect.objectContaining({ source }),
|
||||
}),
|
||||
],
|
||||
},
|
||||
});
|
||||
await client.ping();
|
||||
const summary = await client.callTool({
|
||||
name: "inspect_analysis_view",
|
||||
arguments: {
|
||||
source: { kind: "retained-evidence", evidence_id: parent.evidence_id },
|
||||
view: { kind: "summary" },
|
||||
},
|
||||
});
|
||||
expect(summary.isError).not.toBe(true);
|
||||
});
|
||||
|
||||
it("returns a JavaScript summary without graph payloads from a retained reference", async () => {
|
||||
const { client, session } = await connect();
|
||||
const parent = analysisViewJavaScriptEvidence();
|
||||
expect(session.recordEvidence(parent).ok).toBe(true);
|
||||
const response = await client.callTool({
|
||||
name: "inspect_analysis_view",
|
||||
arguments: {
|
||||
source: {
|
||||
kind: "retained-evidence",
|
||||
evidence_id: parent.evidence_id,
|
||||
},
|
||||
view: { kind: "summary" },
|
||||
},
|
||||
});
|
||||
expect(response.isError).not.toBe(true);
|
||||
const parsed = toolContract("inspect_analysis_view").outputSchema.parse(
|
||||
response.structuredContent,
|
||||
);
|
||||
expect(parsed.result).toMatchObject({
|
||||
kind: "summary",
|
||||
parent_operation: "analyze_javascript_application",
|
||||
summary: { format: "directory" },
|
||||
});
|
||||
expect(parsed.result).not.toHaveProperty("summary.semantic_graph");
|
||||
const stale = await client.callTool({
|
||||
name: "inspect_analysis_view",
|
||||
arguments: {
|
||||
source: {
|
||||
kind: "retained-evidence",
|
||||
evidence_id: `ev_${"e".repeat(64)}`,
|
||||
},
|
||||
view: { kind: "summary" },
|
||||
},
|
||||
});
|
||||
expect(stale).toMatchObject({
|
||||
isError: true,
|
||||
structuredContent: {
|
||||
error: { details: { reason: "missing" } },
|
||||
},
|
||||
});
|
||||
});
|
||||
|
||||
it("accepts a transport-constraint retained reference as the view source", async () => {
|
||||
const { client, session } = await connect();
|
||||
const parent = analysisViewJavaScriptEvidence();
|
||||
expect(session.recordEvidence(parent).ok).toBe(true);
|
||||
const evidenceReference = {
|
||||
kind: "retained-evidence" as const,
|
||||
evidence_id: parent.evidence_id,
|
||||
};
|
||||
const response = await client.callTool({
|
||||
name: "inspect_analysis_view",
|
||||
arguments: {
|
||||
source: evidenceReference,
|
||||
view: {
|
||||
kind: "page",
|
||||
collection: "modules",
|
||||
offset: 0,
|
||||
limit: 8,
|
||||
},
|
||||
},
|
||||
});
|
||||
expect(response.isError).not.toBe(true);
|
||||
const parsed = toolContract("inspect_analysis_view").outputSchema.parse(
|
||||
response.structuredContent,
|
||||
);
|
||||
expect(parsed.result).toMatchObject({
|
||||
kind: "page",
|
||||
parent_evidence_id: parent.evidence_id,
|
||||
coverage: { exhausted: true },
|
||||
});
|
||||
if (parsed.result.kind !== "page") throw new Error("expected page view");
|
||||
expect(parsed.result.items).toEqual([
|
||||
{
|
||||
node_id: expect.any(String),
|
||||
kind: "javascript-asset",
|
||||
path: "renderer.js",
|
||||
},
|
||||
]);
|
||||
});
|
||||
@@ -65,6 +65,26 @@ it("advertises exact valid schemas and records inline layout without a binary ta
|
||||
expect(parsed.result).toEqual(value);
|
||||
expect(parsed.result).toEqual(evidence.normalized_result);
|
||||
expect(session.evidenceById(evidence.evidence_id)).toEqual(evidence);
|
||||
const viewed = await client.callTool({
|
||||
name: "inspect_analysis_view",
|
||||
arguments: {
|
||||
source: {
|
||||
kind: "retained-evidence",
|
||||
evidence_id: evidence.evidence_id,
|
||||
},
|
||||
view: { kind: "summary" },
|
||||
},
|
||||
});
|
||||
expect(viewed.isError).not.toBe(true);
|
||||
expect(
|
||||
toolContract("inspect_analysis_view").outputSchema.parse(
|
||||
viewed.structuredContent,
|
||||
).result,
|
||||
).toMatchObject({
|
||||
kind: "summary",
|
||||
parent_evidence_id: evidence.evidence_id,
|
||||
summary: { format: "elf", bytes: value.artifact.bytes },
|
||||
});
|
||||
expect(
|
||||
(
|
||||
await client.callTool({
|
||||
|
||||
@@ -215,3 +215,27 @@ it("preserves validation for the referenced record's operation, predicate, and s
|
||||
});
|
||||
expect(response.isError).toBe(true);
|
||||
});
|
||||
|
||||
it("projects a retained JavaScript summary without graph or semantic_graph", async () => {
|
||||
const { client, session } = await connect();
|
||||
expect(session.recordEvidence(application).ok).toBe(true);
|
||||
const inline = await client.callTool({
|
||||
name: "inspect_analysis_view",
|
||||
arguments: {
|
||||
source: { kind: "inline", evidence: application },
|
||||
view: { kind: "summary" },
|
||||
},
|
||||
});
|
||||
const referenced = await client.callTool({
|
||||
name: "inspect_analysis_view",
|
||||
arguments: {
|
||||
source: retained(application.evidence_id),
|
||||
view: { kind: "summary" },
|
||||
},
|
||||
});
|
||||
expect(inline.isError).not.toBe(true);
|
||||
expect(referenced.structuredContent).toEqual(inline.structuredContent);
|
||||
expect(inline.structuredContent).not.toHaveProperty(
|
||||
"result.summary.semantic_graph",
|
||||
);
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user