refactor(mcp): make analysis states unrepresentable (#468)
* refactor(config): parse capability settings into exact states * refactor(runtime): encode replay and process outcomes exactly * refactor: make analysis states unrepresentable * refactor(mcp): register canonical schemas with SDK * fix: preserve capability and catalog invariants * fix(build): remove stale compiled modules * fix(replay): update real verifier policy dependency * fix(package): verify the complete MCP catalog * fix(test): supply complete replay reload policy * refactor(contracts): centralize output schema lookup * refactor(mcp): delegate protocol boundaries to SDK * fix(browser): approve real source capture * fix(mcp): preserve catalog guidance
This commit is contained in:
@@ -0,0 +1,41 @@
|
||||
import { describe, expect, it } from "vitest";
|
||||
|
||||
import {
|
||||
resolveArtifactIntegrityPolicy,
|
||||
resolveNativeMountPolicy,
|
||||
} from "../../../src/application/ArtifactInventory/policy.js";
|
||||
|
||||
describe("artifact inventory policy resolution", () => {
|
||||
it("admits native mounting only when caller and operator both allow it", () => {
|
||||
expect(resolveNativeMountPolicy(false, true)).toEqual({
|
||||
status: "disabled",
|
||||
});
|
||||
expect(() => resolveNativeMountPolicy(true, false)).toThrow(
|
||||
"disabled by operator policy",
|
||||
);
|
||||
expect(resolveNativeMountPolicy(true, true)).toEqual({
|
||||
status: "approved",
|
||||
});
|
||||
});
|
||||
|
||||
it("projects strict integrity without dormant continuation state", () => {
|
||||
expect(resolveArtifactIntegrityPolicy({ mode: "fail" }, true)).toEqual({
|
||||
mode: "fail",
|
||||
});
|
||||
});
|
||||
|
||||
it("admits parsed continuation intent only under operator policy", () => {
|
||||
expect(() =>
|
||||
resolveArtifactIntegrityPolicy(
|
||||
{ mode: "record-and-continue", maxMismatches: 10 },
|
||||
false,
|
||||
),
|
||||
).toThrow("requires explicit approval and operator policy");
|
||||
expect(
|
||||
resolveArtifactIntegrityPolicy(
|
||||
{ mode: "record-and-continue", maxMismatches: 10 },
|
||||
true,
|
||||
),
|
||||
).toEqual({ mode: "record-and-continue", maxMismatches: 10 });
|
||||
});
|
||||
});
|
||||
@@ -22,13 +22,37 @@ const status = (
|
||||
readonly operation: string;
|
||||
readonly available: boolean;
|
||||
readonly reason: string | null;
|
||||
readonly availability_code?: string;
|
||||
readonly availability_code?: "unsupported_host";
|
||||
}[];
|
||||
} = {},
|
||||
) => ({
|
||||
open: options.open ?? false,
|
||||
...(options.kind === undefined ? {} : { kind: options.kind }),
|
||||
capabilities: [...(options.capabilities ?? [])],
|
||||
capabilities: (options.capabilities ?? []).map((capability) => ({
|
||||
...capability,
|
||||
availability_code: capability.available
|
||||
? null
|
||||
: (capability.availability_code ?? null),
|
||||
input_contract_version: 1,
|
||||
output_contract_version: 1,
|
||||
pagination: "none" as const,
|
||||
exhaustive: true,
|
||||
effects: {
|
||||
mutates_artifact: false,
|
||||
launches_process: false,
|
||||
may_show_ui: false,
|
||||
may_access_network: false,
|
||||
may_write_filesystem: false,
|
||||
changes_permissions: false,
|
||||
requires_root: false,
|
||||
},
|
||||
limits: {
|
||||
max_results: null,
|
||||
max_payload_bytes: null,
|
||||
timeout_ms: null,
|
||||
},
|
||||
limitations: [],
|
||||
})),
|
||||
});
|
||||
|
||||
const entry = (
|
||||
|
||||
@@ -47,6 +47,20 @@ const contractJsonSchema = (schema: z.ZodType) => {
|
||||
return converted;
|
||||
};
|
||||
|
||||
const describesObject = (schema: ReturnType<typeof contractJsonSchema>) =>
|
||||
schema.type === "object" ||
|
||||
[schema.oneOf, schema.anyOf].some(
|
||||
(variants) =>
|
||||
Array.isArray(variants) &&
|
||||
variants.every(
|
||||
(variant) =>
|
||||
typeof variant === "object" &&
|
||||
variant !== null &&
|
||||
"type" in variant &&
|
||||
variant.type === "object",
|
||||
),
|
||||
);
|
||||
|
||||
const emptySchemaPaths = (value: unknown, path = "$"): string[] => {
|
||||
if (Array.isArray(value))
|
||||
return value.flatMap((item, index) =>
|
||||
@@ -66,8 +80,8 @@ describe("tool contract surface", () => {
|
||||
for (const contract of contracts) {
|
||||
const inputSchema = contractJsonSchema(contract.inputSchema);
|
||||
const outputSchema = contractJsonSchema(contract.outputSchema);
|
||||
expect(inputSchema.type).toBe("object");
|
||||
expect(outputSchema.type).toBe("object");
|
||||
expect(describesObject(inputSchema)).toBe(true);
|
||||
expect(describesObject(outputSchema)).toBe(true);
|
||||
expect(contract.title.length).toBeGreaterThan(2);
|
||||
expect(contract.annotations).toEqual(
|
||||
annotationsFromEffects(contract.effects),
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
import { describe, expect, it } from "vitest";
|
||||
|
||||
import { traceJavaScriptSemanticsRequestSchema } from "../../../src/contracts/applicationWorkflowInputContracts.js";
|
||||
import { buildCapabilityInventory } from "../../../src/application/CapabilityInventory.js";
|
||||
import {
|
||||
artifactInspectionInputSchema,
|
||||
artifactInventoryInputSchema,
|
||||
@@ -19,7 +20,10 @@ import {
|
||||
annotationsFromEffects,
|
||||
toolContractMetadata,
|
||||
} from "../../../src/contracts/toolEffects.js";
|
||||
import { requireOutputSchema } from "../../../src/contracts/toolOutputSchemaPrimitives.js";
|
||||
import {
|
||||
requireOutputSchema,
|
||||
toolAvailability,
|
||||
} from "../../../src/contracts/toolOutputSchemaPrimitives.js";
|
||||
import { JAVASCRIPT_FEATURE_TRACE_FULL_EVIDENCE_EXAMPLE } from "../../../src/contracts/javascriptApplicationWorkflowExamples.js";
|
||||
|
||||
const EVIDENCE_ID = `ev_${"a".repeat(64)}`;
|
||||
@@ -80,6 +84,17 @@ describe("contract validation boundaries", () => {
|
||||
).toBe(false);
|
||||
});
|
||||
|
||||
it("accepts each complete managed application Evidence source", () => {
|
||||
for (const input of [
|
||||
{ managed_artifact_evidence_id: EVIDENCE_ID },
|
||||
{ managed_members_evidence_id: EVIDENCE_ID },
|
||||
{ managed_native_boundaries_evidence_id: EVIDENCE_ID },
|
||||
])
|
||||
expect(
|
||||
managedApplicationGraphReferenceInputSchema.safeParse(input).success,
|
||||
).toBe(true);
|
||||
});
|
||||
|
||||
it("requires explicit approval for record-and-continue artifact inspection", () => {
|
||||
const input = {
|
||||
integrity_policy: "record-and-continue" as const,
|
||||
@@ -89,6 +104,30 @@ describe("contract validation boundaries", () => {
|
||||
expect(artifactInspectionInputSchema.safeParse(input).success).toBe(false);
|
||||
});
|
||||
|
||||
it("rejects contradictory tool availability states", () => {
|
||||
const inventory = buildCapabilityInventory(
|
||||
{ open: false, capabilities: [] },
|
||||
{
|
||||
processCaptureEnabled: false,
|
||||
evidenceFileRoots: 0,
|
||||
investigationInputRoots: 0,
|
||||
},
|
||||
);
|
||||
const unavailable = inventory.find(({ available }) => !available);
|
||||
expect(unavailable).toBeDefined();
|
||||
expect(toolAvailability.safeParse(unavailable).success).toBe(true);
|
||||
expect(
|
||||
toolAvailability.safeParse({ ...unavailable, available: true }).success,
|
||||
).toBe(false);
|
||||
expect(
|
||||
toolAvailability.safeParse({
|
||||
...unavailable,
|
||||
available: false,
|
||||
reason: "available",
|
||||
}).success,
|
||||
).toBe(false);
|
||||
});
|
||||
|
||||
it("covers deterministic contract rendering and defensive helper errors", () => {
|
||||
const rendered = renderGuidedPrompt(PROMPT_CONTRACTS[0], {
|
||||
zulu: "last",
|
||||
|
||||
@@ -22,9 +22,6 @@ const LIMITS = {
|
||||
|
||||
const INTEGRITY = {
|
||||
mode: "fail",
|
||||
approved: false,
|
||||
enabled: false,
|
||||
maxMismatches: 1,
|
||||
} as const;
|
||||
|
||||
const deferred = <Value>() => {
|
||||
|
||||
@@ -43,12 +43,6 @@ describe("Hopper provider discovery", () => {
|
||||
code: "target_kind_unsupported",
|
||||
diagnostics: { target_kind: "archive", target_format: "asar" },
|
||||
});
|
||||
expect(
|
||||
hopper.inspectTargetSupport(executableTargetWithoutArchitecture()),
|
||||
).toMatchObject({
|
||||
status: "unsupported",
|
||||
code: "architecture_unsupported",
|
||||
});
|
||||
});
|
||||
|
||||
it("advertises the exact bounded byte-read limit", () => {
|
||||
@@ -88,21 +82,26 @@ const databaseTarget = (): BinaryTarget => ({
|
||||
const executableTarget = (
|
||||
format: "mach-o" | "elf" | "pe",
|
||||
architecture: "x86" | "x86_64" | "arm" | "arm64",
|
||||
): BinaryTarget => ({
|
||||
path: "/tmp/fixture",
|
||||
sha256: "b".repeat(64),
|
||||
kind: "executable",
|
||||
format,
|
||||
architecture,
|
||||
availableArchitectures: [architecture],
|
||||
});
|
||||
|
||||
const executableTargetWithoutArchitecture = (): BinaryTarget => ({
|
||||
path: "/tmp/fixture",
|
||||
sha256: "b".repeat(64),
|
||||
kind: "executable",
|
||||
format: "elf",
|
||||
});
|
||||
): BinaryTarget =>
|
||||
format === "pe"
|
||||
? {
|
||||
path: "/tmp/fixture",
|
||||
sha256: "b".repeat(64),
|
||||
kind: "executable",
|
||||
format,
|
||||
architecture,
|
||||
availableArchitectures: [architecture],
|
||||
executableRole: "application",
|
||||
managed: false,
|
||||
}
|
||||
: {
|
||||
path: "/tmp/fixture",
|
||||
sha256: "b".repeat(64),
|
||||
kind: "executable",
|
||||
format,
|
||||
architecture,
|
||||
availableArchitectures: [architecture],
|
||||
};
|
||||
|
||||
const artifactTarget = (): BinaryTarget => ({
|
||||
path: "/tmp/app.asar",
|
||||
|
||||
@@ -48,7 +48,7 @@ describe("package identity", () => {
|
||||
"node scripts/generate-package-metadata.mjs --check",
|
||||
);
|
||||
expect(packageJson.scripts.prebuild).toBe(
|
||||
"npm run deps:check && npm run metadata:check",
|
||||
"npm run deps:check && npm run metadata:check && node scripts/clean-build-output.mjs",
|
||||
);
|
||||
expect(packageJson.scripts["precheck:fast"]).toBe("npm run deps:check");
|
||||
expect(packageJson.scripts["check:fast"]).toBe("turbo run typecheck lint");
|
||||
|
||||
@@ -1,103 +0,0 @@
|
||||
import { describe, expect, it } from "vitest";
|
||||
|
||||
import { TOOL_CONTRACTS } from "../../../src/contracts/toolContracts.js";
|
||||
import { applyToolInputJsonSchemaOverride } from "../../../src/contracts/toolSchemaJsonOverrides.js";
|
||||
import { z } from "zod";
|
||||
|
||||
const publicInputSchema = (name: string) => {
|
||||
const contract = TOOL_CONTRACTS.find((item) => item.name === name);
|
||||
if (contract === undefined) throw new Error(`Missing contract: ${name}`);
|
||||
return applyToolInputJsonSchemaOverride(
|
||||
name,
|
||||
z.toJSONSchema(contract.inputSchema, {
|
||||
target: "draft-07",
|
||||
unrepresentable: "any",
|
||||
}),
|
||||
);
|
||||
};
|
||||
|
||||
describe("public tool schema refinements", () => {
|
||||
it("requires complete web capture pairs and Evidence sources", () => {
|
||||
expect(publicInputSchema("compare_web_captures")).toMatchObject({
|
||||
oneOf: [
|
||||
{
|
||||
required: ["before", "after"],
|
||||
not: {
|
||||
anyOf: [
|
||||
{ required: ["before_scenario"] },
|
||||
{ required: ["after_scenario"] },
|
||||
],
|
||||
},
|
||||
},
|
||||
{
|
||||
required: ["before_scenario", "after_scenario"],
|
||||
not: {
|
||||
anyOf: [{ required: ["before"] }, { required: ["after"] }],
|
||||
},
|
||||
},
|
||||
],
|
||||
});
|
||||
expect(publicInputSchema("trace_application_feature")).toMatchObject({
|
||||
oneOf: [
|
||||
{ required: ["application"] },
|
||||
{ required: ["application_evidence_id"] },
|
||||
],
|
||||
});
|
||||
});
|
||||
|
||||
it("publishes sensitive approval dependencies and exact readiness stages", () => {
|
||||
expect(
|
||||
JSON.stringify(publicInputSchema("inspect_electron_page")),
|
||||
).toContain('"source_capture_approved"');
|
||||
const readiness = publicInputSchema("evaluate_reconstruction_readiness");
|
||||
expect(readiness).toMatchObject({
|
||||
allOf: [
|
||||
{
|
||||
properties: {
|
||||
stages: {
|
||||
allOf: expect.arrayContaining([
|
||||
expect.objectContaining({ minContains: 1, maxContains: 1 }),
|
||||
]),
|
||||
},
|
||||
},
|
||||
},
|
||||
],
|
||||
});
|
||||
expect(JSON.stringify(readiness)).toContain('"minContains":1');
|
||||
|
||||
const bundleSchema = JSON.stringify(
|
||||
publicInputSchema("analyze_web_bundle"),
|
||||
);
|
||||
expect(bundleSchema).toContain('"include_console_text"');
|
||||
expect(bundleSchema).toContain('"console_text_approved"');
|
||||
expect(bundleSchema).toContain('"include_json_body_shapes"');
|
||||
expect(bundleSchema).toContain('"json_body_schema_approved"');
|
||||
expect(bundleSchema).toContain('"include_websocket_shapes"');
|
||||
expect(bundleSchema).toContain('"websocket_shape_approved"');
|
||||
});
|
||||
|
||||
it("requires exactly two export-shape Evidence links", () => {
|
||||
const contract = TOOL_CONTRACTS.find(
|
||||
({ name }) => name === "compare_javascript_export_shapes",
|
||||
);
|
||||
if (contract === undefined)
|
||||
throw new Error("Missing export comparison contract");
|
||||
expect(
|
||||
JSON.stringify(
|
||||
z.toJSONSchema(contract.outputSchema, {
|
||||
target: "draft-07",
|
||||
unrepresentable: "any",
|
||||
}),
|
||||
),
|
||||
).toContain('"minItems":2');
|
||||
});
|
||||
|
||||
it("requires exactly one source for source-to-bundle comparison", () => {
|
||||
expect(publicInputSchema("compare_source_to_bundle")).toMatchObject({
|
||||
oneOf: [
|
||||
{ required: ["application"] },
|
||||
{ required: ["application_evidence_id"] },
|
||||
],
|
||||
});
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user