Merge remote-tracking branch 'origin/main' into feat/artifact-integrity-policy
# Conflicts: # docs/product-catalog.json # docs/verification/managed-conformance-manifest.json # skills/reverse-engineer-anything/SKILL.md # src/generatedMcpToolCatalog.ts
This commit is contained in:
@@ -0,0 +1,67 @@
|
||||
import { expect } from "vitest";
|
||||
import { mkdir, writeFile } from "node:fs/promises";
|
||||
import { join } from "node:path";
|
||||
import { webSourceLocationFixture } from "../fixtures/webSourceLocation.js";
|
||||
import { createTestTempDirectory } from "../fixtures/temporaryDirectory.js";
|
||||
import { cliTest } from "../support/cli/cliFixture.js";
|
||||
|
||||
cliTest(
|
||||
"accepts numeric UTF-16 coordinates and runs the actual owned source-map codec without a browser",
|
||||
async ({ cli }) => {
|
||||
const root = await createTestTempDirectory("rea-source-map-cli-");
|
||||
const fixture = webSourceLocationFixture();
|
||||
await mkdir(join(root, "files", "modules"), { recursive: true });
|
||||
await writeFile(join(root, "files", "modules", "main.js"), fixture.source);
|
||||
const manifest = join(root, "manifest.json");
|
||||
const map = join(root, "app.js.map");
|
||||
await writeFile(manifest, JSON.stringify(fixture.manifest));
|
||||
await writeFile(map, fixture.sourceMap.text);
|
||||
const response = await cli.run({
|
||||
arguments: [
|
||||
"trace-web-source-location",
|
||||
manifest,
|
||||
"0",
|
||||
map,
|
||||
fixture.sourceMap.url,
|
||||
"1",
|
||||
"0",
|
||||
"--json",
|
||||
],
|
||||
environment: {
|
||||
REA_LOG_LEVEL: "silent",
|
||||
REA_BROWSER_EXECUTABLE: "relative-unconfigured-browser",
|
||||
},
|
||||
timeoutMs: 15000,
|
||||
});
|
||||
expect(response.exitCode).toBe(0);
|
||||
expect(response.json).toMatchObject({
|
||||
normalized_result: {
|
||||
source: { script_index: 0 },
|
||||
source_map: { association: "caller-selected" },
|
||||
generated_offset: 0,
|
||||
execution: "unknown",
|
||||
matches: [{ content: { text: "original" } }],
|
||||
runtime: { id: "node-v8" },
|
||||
},
|
||||
});
|
||||
const invalid = await cli.run({
|
||||
arguments: [
|
||||
"trace-web-source-location",
|
||||
manifest,
|
||||
"0",
|
||||
map,
|
||||
fixture.sourceMap.url,
|
||||
"2",
|
||||
"0",
|
||||
"--json",
|
||||
],
|
||||
environment: { REA_LOG_LEVEL: "silent" },
|
||||
timeoutMs: 15000,
|
||||
});
|
||||
expect(invalid.exitCode).toBe(1);
|
||||
expect(invalid.json).toMatchObject({
|
||||
code: "invalid_request",
|
||||
details: { issues: [{ path: ["generated_position"] }] },
|
||||
});
|
||||
},
|
||||
);
|
||||
@@ -0,0 +1,78 @@
|
||||
import { createHash } from "node:crypto";
|
||||
import { writeFile } from "node:fs/promises";
|
||||
import { join } from "node:path";
|
||||
import { expect, it } from "vitest";
|
||||
import { LocalWebSourceLocationArtifacts } from "../../../src/browser/assets/WebSourceLocationArtifacts.js";
|
||||
import { projectAnalysisError } from "../../../src/domain/analysisErrorProjection.js";
|
||||
import { ok } from "../../../src/domain/result.js";
|
||||
import { WEB_SOURCE_MAP_LIMITS } from "../../../src/domain/webSourceLocation.js";
|
||||
import { createTestTempDirectory } from "../../fixtures/temporaryDirectory.js";
|
||||
import {
|
||||
webSourceLocationArgs,
|
||||
webSourceLocationFixture,
|
||||
} from "../../fixtures/webSourceLocation.js";
|
||||
|
||||
const reader = () => {
|
||||
const { sourceMap: _sourceMap, ...script } = webSourceLocationFixture();
|
||||
return new LocalWebSourceLocationArtifacts({
|
||||
load: () => Promise.resolve(ok(script)),
|
||||
});
|
||||
};
|
||||
it("reads exact selected map bytes, retaining a BOM and explicitly selected URL context", async () => {
|
||||
const root = await createTestTempDirectory("rea-map-artifacts-");
|
||||
const path = join(root, "app.map");
|
||||
const text = '\uFEFF{"version":3,"sources":[],"names":[],"mappings":""}';
|
||||
await writeFile(path, text);
|
||||
const response = await reader().load({
|
||||
...webSourceLocationArgs,
|
||||
source_map: { ...webSourceLocationArgs.source_map, path },
|
||||
});
|
||||
if (!response.ok) throw response.error;
|
||||
expect(response.value.sourceMap).toEqual({
|
||||
file: {
|
||||
path,
|
||||
sha256: createHash("sha256").update(text).digest("hex"),
|
||||
bytes: Buffer.byteLength(text),
|
||||
},
|
||||
url: webSourceLocationArgs.source_map.url,
|
||||
text,
|
||||
});
|
||||
});
|
||||
it.each(["unavailable", "invalid-utf8", "oversized"])(
|
||||
"preserves the selected map reader failure: %s",
|
||||
async (variant) => {
|
||||
const root = await createTestTempDirectory("rea-map-artifacts-");
|
||||
const path = join(root, "app.map");
|
||||
if (variant === "invalid-utf8")
|
||||
await writeFile(path, Buffer.from([255, 254]));
|
||||
if (variant === "oversized")
|
||||
await writeFile(
|
||||
path,
|
||||
Buffer.alloc(WEB_SOURCE_MAP_LIMITS.mapBytes + 1, 32),
|
||||
);
|
||||
const response = await reader().load({
|
||||
...webSourceLocationArgs,
|
||||
source_map: { ...webSourceLocationArgs.source_map, path },
|
||||
});
|
||||
if (response.ok) throw new Error("expected map failure");
|
||||
const projection = projectAnalysisError(response.error);
|
||||
if (variant === "invalid-utf8")
|
||||
expect(projection).toMatchObject({
|
||||
code: "invalid_request",
|
||||
details: {
|
||||
issues: [
|
||||
{
|
||||
path: ["source_map", "path"],
|
||||
message: expect.stringContaining("UTF-8"),
|
||||
},
|
||||
],
|
||||
},
|
||||
});
|
||||
else
|
||||
expect(response.error).toMatchObject({
|
||||
_tag: "ArtifactOperationError",
|
||||
reason: variant === "oversized" ? "limit" : "io",
|
||||
detail: expect.stringContaining(path),
|
||||
});
|
||||
},
|
||||
);
|
||||
@@ -0,0 +1,206 @@
|
||||
import { fileURLToPath } from "node:url";
|
||||
import type { SourceMapDecoderDependencies } from "../../../src/javascript/sourceMaps/SourceMapDecoder.js";
|
||||
import { access } from "node:fs/promises";
|
||||
import { expect, it } from "vitest";
|
||||
import { SourceMapDecoder } from "../../../src/javascript/sourceMaps/SourceMapDecoder.js";
|
||||
import { PrivateRuntimeRoot } from "../../../src/process/PrivateRuntimeRoot.js";
|
||||
import { spawnOwnedProviderProcess } from "../../../src/process/ProviderProcess.js";
|
||||
import { waitForProviderProcessReady } from "../../fixtures/providerProcess.js";
|
||||
import {
|
||||
webSourceLocationArgs,
|
||||
webSourceLocationFixture,
|
||||
} from "../../fixtures/webSourceLocation.js";
|
||||
|
||||
const realDecoder = (dependencies: SourceMapDecoderDependencies = {}) =>
|
||||
new SourceMapDecoder({
|
||||
...dependencies,
|
||||
launcher:
|
||||
dependencies.launcher ??
|
||||
((options) =>
|
||||
spawnOwnedProviderProcess({
|
||||
...options,
|
||||
arguments: options.arguments.map((argument) =>
|
||||
argument.endsWith("/SourceMapCodecProcess.js") ||
|
||||
argument.endsWith("\\SourceMapCodecProcess.js")
|
||||
? fileURLToPath(
|
||||
new URL(
|
||||
"../../../dist/javascript/sourceMaps/SourceMapCodecProcess.js",
|
||||
import.meta.url,
|
||||
),
|
||||
)
|
||||
: argument,
|
||||
),
|
||||
})),
|
||||
});
|
||||
|
||||
const fixture = webSourceLocationFixture();
|
||||
const input = {
|
||||
text: fixture.sourceMap.text,
|
||||
url: fixture.sourceMap.url,
|
||||
path: fixture.sourceMap.file.path,
|
||||
position: webSourceLocationArgs.generated_position,
|
||||
};
|
||||
|
||||
it("runs the real pinned codec with an independent heap and removes its private request directory", async () => {
|
||||
let path: string | undefined;
|
||||
const environment = {
|
||||
...process.env,
|
||||
NODE_OPTIONS: "--require=/rea-file-that-does-not-exist",
|
||||
};
|
||||
const decoder = realDecoder({
|
||||
environment,
|
||||
createRuntime: async () => {
|
||||
const root = await PrivateRuntimeRoot.create();
|
||||
path = root.path;
|
||||
return root;
|
||||
},
|
||||
});
|
||||
const response = await decoder.trace(input);
|
||||
if (!response.ok) throw response.error;
|
||||
expect(response.value).toMatchObject({
|
||||
engine: { id: "jridgewell-trace-mapping", version: "0.3.31" },
|
||||
runtime: { id: "node-v8" },
|
||||
matches: [{ content: { text: "original" } }],
|
||||
});
|
||||
expect(response.value.runtime.v8_heap_limit_bytes).toBeLessThanOrEqual(
|
||||
256 * 1024 * 1024,
|
||||
);
|
||||
expect(environment.NODE_OPTIONS).toBe(
|
||||
"--require=/rea-file-that-does-not-exist",
|
||||
);
|
||||
if (path === undefined) throw new Error("runtime not acquired");
|
||||
await expect(access(path)).rejects.toMatchObject({ code: "ENOENT" });
|
||||
});
|
||||
it.each([
|
||||
{ text: "{", tag: "AnalysisInputError" },
|
||||
{
|
||||
text: JSON.stringify({
|
||||
version: 3,
|
||||
sections: [
|
||||
{
|
||||
offset: { line: 0, column: 0 },
|
||||
url: "https://app.test/external.map",
|
||||
},
|
||||
],
|
||||
}),
|
||||
tag: "AnalysisCapabilityUnavailableError",
|
||||
},
|
||||
{
|
||||
text: JSON.stringify({
|
||||
version: 3,
|
||||
sections: [{ offset: { line: 1000000000, column: 0 }, map: {} }],
|
||||
}),
|
||||
tag: "ArtifactOperationError",
|
||||
},
|
||||
])("preserves actual producer failures: $tag", async ({ text, tag }) => {
|
||||
const response = await realDecoder().trace({ ...input, text });
|
||||
if (response.ok) throw new Error("expected failed map");
|
||||
expect(response.error._tag).toBe(tag);
|
||||
});
|
||||
it("reports cleanup failure with the actual owning operation", async () => {
|
||||
const response = await realDecoder({
|
||||
createRuntime: async () => {
|
||||
const root = await PrivateRuntimeRoot.create();
|
||||
return {
|
||||
path: root.path,
|
||||
close: async () => {
|
||||
await root.close();
|
||||
throw new Error("observed cleanup failure");
|
||||
},
|
||||
};
|
||||
},
|
||||
}).trace(input);
|
||||
if (response.ok) throw new Error("expected cleanup failure");
|
||||
expect(response.error).toMatchObject({
|
||||
cleanupIncomplete: true,
|
||||
operation: "trace_web_source_location",
|
||||
diagnostics: { cleanup_failures: ["observed cleanup failure"] },
|
||||
});
|
||||
});
|
||||
it("cancels a real owned process and removes the runtime request", async () => {
|
||||
const controller = new AbortController();
|
||||
let path: string | undefined;
|
||||
const response = await realDecoder({
|
||||
createRuntime: async () => {
|
||||
const root = await PrivateRuntimeRoot.create();
|
||||
path = root.path;
|
||||
return root;
|
||||
},
|
||||
launcher: async (options) => {
|
||||
const child = await spawnOwnedProviderProcess({
|
||||
...options,
|
||||
arguments: [
|
||||
"-e",
|
||||
'process.stdout.write("ready\\n"); setInterval(() => undefined, 1000)',
|
||||
],
|
||||
});
|
||||
await waitForProviderProcessReady(child.process);
|
||||
setImmediate(() => controller.abort());
|
||||
return child;
|
||||
},
|
||||
}).trace(input, { signal: controller.signal });
|
||||
if (response.ok) throw new Error("expected cancellation");
|
||||
expect(response.error._tag).toBe("AnalysisCancelledError");
|
||||
if (path === undefined) throw new Error("runtime not acquired");
|
||||
await expect(access(path)).rejects.toMatchObject({ code: "ENOENT" });
|
||||
});
|
||||
it("retains final diagnostic bytes for an actual failed codec process", async () => {
|
||||
const response = await realDecoder({
|
||||
launcher: (options) =>
|
||||
spawnOwnedProviderProcess({
|
||||
...options,
|
||||
arguments: [
|
||||
"-e",
|
||||
'process.stderr.write("final diagnostic"); process.exitCode = 7',
|
||||
],
|
||||
}),
|
||||
}).trace(input);
|
||||
if (response.ok) throw new Error("expected exit failure");
|
||||
expect(response.error).toMatchObject({
|
||||
diagnostics: { exit_code: 7, stderr: "final diagnostic" },
|
||||
});
|
||||
});
|
||||
it("reports complete evidence amplification as a resource failure before serializing an oversized reply", async () => {
|
||||
const text = JSON.stringify({
|
||||
version: 3,
|
||||
sources: ["large.ts"],
|
||||
sourcesContent: ["a".repeat(600000)],
|
||||
names: [],
|
||||
mappings: Array.from({ length: 60 }, () => "AAAA").join(","),
|
||||
});
|
||||
const response = await realDecoder().trace({ ...input, text });
|
||||
if (response.ok) throw new Error("expected complete-evidence budget failure");
|
||||
expect(response.error).toMatchObject({
|
||||
_tag: "ArtifactOperationError",
|
||||
reason: "limit",
|
||||
detail: expect.stringContaining("no partial"),
|
||||
});
|
||||
});
|
||||
it("rejects a malformed actual subprocess reply", async () => {
|
||||
const response = await realDecoder({
|
||||
launcher: (options) =>
|
||||
spawnOwnedProviderProcess({
|
||||
...options,
|
||||
arguments: ["-e", 'process.stdout.write("not JSON")'],
|
||||
}),
|
||||
}).trace(input);
|
||||
if (response.ok) throw new Error("expected malformed reply");
|
||||
expect(response.error._tag).toBe("AnalysisOutputError");
|
||||
});
|
||||
it("removes the private request when launching the codec fails", async () => {
|
||||
let path: string | undefined;
|
||||
const response = await realDecoder({
|
||||
createRuntime: async () => {
|
||||
const root = await PrivateRuntimeRoot.create();
|
||||
path = root.path;
|
||||
return root;
|
||||
},
|
||||
launcher: () => Promise.reject(new Error("observed launch failure")),
|
||||
}).trace(input);
|
||||
if (response.ok) throw new Error("expected launch failure");
|
||||
expect(response.error).toMatchObject({
|
||||
diagnostics: { error_message: "observed launch failure" },
|
||||
});
|
||||
if (path === undefined) throw new Error("runtime not acquired");
|
||||
await expect(access(path)).rejects.toMatchObject({ code: "ENOENT" });
|
||||
});
|
||||
@@ -0,0 +1,71 @@
|
||||
import { traceSourceMap } from "../../../src/javascript/sourceMaps/TraceSourceMap.js";
|
||||
import { webSourceLocationArgs as args } from "../../fixtures/webSourceLocation.js";
|
||||
import { Client, InMemoryTransport } from "@modelcontextprotocol/client";
|
||||
import { Ajv2020 } from "ajv/dist/2020.js";
|
||||
import { expect, it, onTestFinished } from "vitest";
|
||||
import { WebSourceLocationService } from "../../../src/application/WebSourceLocationService.js";
|
||||
import { toolContract } from "../../../src/contracts/toolContracts.js";
|
||||
import { parseEvidence } from "../../../src/domain/evidence.js";
|
||||
import { ok } from "../../../src/domain/result.js";
|
||||
import { createServer } from "../../../src/server/createServer.js";
|
||||
import { createTestBinarySession } from "../../fixtures/binarySession.js";
|
||||
import { webSourceLocationFixture } from "../../fixtures/webSourceLocation.js";
|
||||
|
||||
it("publishes valid SDK schemas and records the named module trace without a binary target", async () => {
|
||||
const service = new WebSourceLocationService(
|
||||
{
|
||||
load: () => Promise.resolve(ok(webSourceLocationFixture())),
|
||||
},
|
||||
{
|
||||
trace: (input) =>
|
||||
Promise.resolve(
|
||||
ok(traceSourceMap(input.text, input.url, input.position)),
|
||||
),
|
||||
},
|
||||
);
|
||||
const session = createTestBinarySession(() => {
|
||||
throw new Error("binary provider must not start");
|
||||
});
|
||||
const server = createServer(session, session, { webSourceLocation: service });
|
||||
const client = new Client({ name: "module-contract", 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);
|
||||
const advertised = (await client.listTools()).tools.find(
|
||||
(tool) => tool.name === "trace_web_source_location",
|
||||
);
|
||||
if (advertised?.outputSchema === undefined)
|
||||
throw new Error("module trace must advertise 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, args)).toBe(true);
|
||||
const response = await client.callTool({
|
||||
name: "trace_web_source_location",
|
||||
arguments: args,
|
||||
});
|
||||
expect(response.isError).not.toBe(true);
|
||||
expect(
|
||||
ajv.validate(outputSchema, response.structuredContent),
|
||||
JSON.stringify(ajv.errors),
|
||||
).toBe(true);
|
||||
const parsed = toolContract("trace_web_source_location").outputSchema.parse(
|
||||
response.structuredContent,
|
||||
);
|
||||
const evidence = parseEvidence(parsed.evidence);
|
||||
expect(parsed.result).toEqual(evidence.normalized_result);
|
||||
expect(session.evidenceById(evidence.evidence_id)).toEqual(evidence);
|
||||
const invalid = await client.callTool({
|
||||
name: "trace_web_source_location",
|
||||
arguments: { ...args, manifest_path: "relative.json" },
|
||||
});
|
||||
expect(invalid.isError).toBe(true);
|
||||
});
|
||||
Vendored
+49
@@ -0,0 +1,49 @@
|
||||
import { createHash } from "node:crypto";
|
||||
import type { WebSourceLocationArtifacts } from "../../src/application/WebSourceLocationPorts.js";
|
||||
import { webModuleArtifactsFixture } from "./webModuleTrace.js";
|
||||
import { traceSourceMap } from "../../src/javascript/sourceMaps/TraceSourceMap.js";
|
||||
import type { WebSourceMapPort } from "../../src/application/WebSourceLocationPorts.js";
|
||||
import { ok } from "../../src/domain/result.js";
|
||||
|
||||
/** Inert codec seam for workflow tests; real process verification is separate. */
|
||||
export const webSourceMapDecoderFixture: WebSourceMapPort = {
|
||||
trace: (input) =>
|
||||
Promise.resolve(ok(traceSourceMap(input.text, input.url, input.position))),
|
||||
};
|
||||
|
||||
/** Source-owned map fixture with independent byte identity and retained capture context. */
|
||||
export const webSourceLocationFixture = (
|
||||
text = JSON.stringify({
|
||||
version: 3,
|
||||
sources: ["original.ts"],
|
||||
sourcesContent: ["original"],
|
||||
names: [],
|
||||
mappings: "AAAA",
|
||||
}),
|
||||
): WebSourceLocationArtifacts => {
|
||||
const { importMap: _importMap, ...script } =
|
||||
webModuleArtifactsFixture("generated");
|
||||
return {
|
||||
...script,
|
||||
sourceMap: {
|
||||
file: {
|
||||
path: "/analysis/app.js.map",
|
||||
sha256: createHash("sha256").update(text).digest("hex"),
|
||||
bytes: Buffer.byteLength(text),
|
||||
},
|
||||
url: "https://app.test/app.js.map?v=1#context",
|
||||
text,
|
||||
},
|
||||
};
|
||||
};
|
||||
|
||||
/** Explicit fixture pairing; no capture authenticity or execution is asserted. */
|
||||
export const webSourceLocationArgs = {
|
||||
manifest_path: "/analysis/manifest.json",
|
||||
script_index: 0,
|
||||
source_map: {
|
||||
path: "/analysis/app.js.map",
|
||||
url: "https://app.test/app.js.map?v=1#context",
|
||||
},
|
||||
generated_position: { line: 1, column: 0 },
|
||||
};
|
||||
Reference in New Issue
Block a user