fix: report malformed application evidence as input errors

This commit is contained in:
morluto
2026-10-10 03:52:35 +08:00
parent 55a46f7c71
commit 218c13160e
15 changed files with 613 additions and 165 deletions
@@ -313,7 +313,7 @@ describe("evidence bundle filesystem adapter", () => {
error: {
_tag: "EvidenceIntegrityError",
userMessage: expect.stringContaining(
"Evidence semantic identifier does not match its record",
"at records.0.evidence_id: Evidence semantic identifier does not match its record",
),
},
});
@@ -320,7 +320,7 @@ describe("complete comparison projections", () => {
});
describe("JavaScript application workflow error diagnostics", () => {
it("returns safe explicit Evidence constraints from graph workflows", async () => {
it("identifies the inconsistent Evidence subject in graph workflows", async () => {
const root = await createTestTempDirectory(
"rea-application-workflow-diagnostics-",
);
@@ -328,10 +328,10 @@ describe("JavaScript application workflow error diagnostics", () => {
const evidence = await analyzeFixture(root);
const subject = evidence.subject;
if (subject === null) throw new Error("Analysis Evidence subject missing");
const secretPath = "/private/token=do-not-return";
const inconsistentPath = "/different/application.js";
const inconsistent = {
...evidence,
subject: { ...subject, local_path: secretPath },
subject: { ...subject, local_path: inconsistentPath },
};
const result = traceApplicationFeatureEvidence({
@@ -346,11 +346,10 @@ describe("JavaScript application workflow error diagnostics", () => {
expect(result.error.issues).toContainEqual(
expect.objectContaining({
reason: "invalid_value",
message:
"JavaScript application Evidence subject does not match its result",
path: ["application", "subject"],
message: expect.stringContaining("subject must match"),
}),
);
expect(JSON.stringify(result.error.issues)).not.toContain(secretPath);
});
it("preserves schema-authored Evidence validation messages", async () => {
@@ -232,7 +232,9 @@ const analyzeReturn = async (root: string, properties: string) => {
await writeFixtureFiles(root, { "parser.mjs": `${prefix}${expression});\n` });
const analyzed = await analyzeJavaScriptApplication({ input_path: root });
if (!analyzed.ok) throw analyzed.error;
const source = parseApplicationGraphEvidence(analyzed.value);
const parsed = parseApplicationGraphEvidence(analyzed.value);
if (!parsed.ok) throw new Error("Analysis Evidence must parse");
const source = parsed.value;
const observation = source.graph.nodes
.flatMap(({ observations }) => observations)
.find(
@@ -938,7 +938,9 @@ const analyzeGraph = async (root: string) => {
input_path: root,
});
if (!result.ok) throw result.error;
return parseApplicationGraphEvidence(result.value);
const parsed = parseApplicationGraphEvidence(result.value);
if (!parsed.ok) throw new Error("Analysis Evidence must parse");
return parsed.value;
};
const analyzeSources = async (
@@ -15,7 +15,9 @@ const analyze = async (root: string) => {
format: "directory",
});
if (!result.ok) throw result.error;
const authenticated = parseApplicationGraphEvidence(result.value);
const parsed = parseApplicationGraphEvidence(result.value);
if (!parsed.ok) throw new Error("Analysis Evidence must parse");
const authenticated = parsed.value;
return {
graph: authenticated.graph,
result: javascriptApplicationAnalysisResultSchema.parse(
@@ -668,9 +668,16 @@ it("rejects static application Evidence whose subject path disagrees with its re
},
};
expect(() => parseApplicationGraphEvidence(inconsistent)).toThrow(
"JavaScript application Evidence subject does not match its result",
);
const parsed = parseApplicationGraphEvidence(inconsistent);
expect(parsed.ok).toBe(false);
if (!parsed.ok)
expect(parsed.error.issues).toContainEqual(
expect.objectContaining({
path: ["subject"],
reason: "invalid_value",
message: expect.stringContaining("subject must match"),
}),
);
});
const compareTrees = async (previous: string, current: string) => {
@@ -682,9 +689,11 @@ const compareTrees = async (previous: string, current: string) => {
if (!reference.ok) throw new Error(reference.error.message);
const analyzed = await analyzeJavaScriptApplication({ input_path: current });
if (!analyzed.ok) throw analyzed.error;
const application = parseApplicationGraphEvidence(
const parsedApplication = parseApplicationGraphEvidence(
JSON.parse(JSON.stringify(analyzed.value)),
);
if (!parsedApplication.ok) throw new Error("Analysis Evidence must parse");
const application = parsedApplication.value;
const comparison = compareSourceToBundle({
reference: reference.value,
application: {
@@ -0,0 +1,221 @@
import { mkdir, writeFile } from "node:fs/promises";
import { join } from "node:path";
import { expect, it } from "vitest";
import { Ajv2020 } from "ajv/dist/2020.js";
import { createApplicationMcpHarness } from "../fixtures/applicationMcpHarness.js";
import { parseMcpToolError } from "../fixtures/mcpToolError.js";
import { createTestTempDirectory } from "../fixtures/temporaryDirectory.js";
import { createCli } from "../../src/cli.js";
import { createEvidence, parseEvidence } from "../../src/domain/evidence.js";
import type { JsonValue } from "../../src/domain/jsonValue.js";
import { analysisCliErrorEnvelopeSchema } from "../../src/contracts/errorSchemas.js";
import { toolContract } from "../../src/contracts/toolContracts.js";
import { javaScriptSemanticTraceResultSchema } from "../../src/domain/javascript/javascriptSemanticTraceSchemas.js";
import { resolveJavaScriptSemanticEvidence } from "../../src/domain/javascript/javascriptSemanticGraph.js";
const SEMANTIC_GRAPH_ID = `jsrg_${"0".repeat(64)}`;
it("reports invalid nested semantic graph Evidence as caller input through CLI and SDK MCP", async () => {
const root = await createTestTempDirectory("rea-semantic-trace-input-");
await mkdir(join(root, "src"));
await writeFile(
join(root, "src", "main.js"),
"export function parse(value) { return JSON.parse(value); }\n",
);
const cli = createCli({});
const analyzed = await serveCli(cli, [
"analyze-javascript-application",
root,
"--json",
]);
expect(analyzed.exitCode).toBe(0);
const sourceEvidence = parseEvidence(JSON.parse(analyzed.stdout));
const normalizedResult = structuredClone(sourceEvidence.normalized_result);
if (!isJsonObject(normalizedResult))
throw new Error("CLI analysis result must be an object");
const semanticGraph = normalizedResult.semantic_graph;
if (!isJsonObject(semanticGraph))
throw new Error("CLI analysis must include a semantic graph");
const firstNode = semanticGraph.nodes;
if (!Array.isArray(firstNode) || !isJsonObject(firstNode[0]))
throw new Error("CLI semantic graph must include a node");
const seedNodeId = firstNode[0].node_id;
if (typeof seedNodeId !== "string")
throw new Error("CLI semantic graph node must have an identifier");
semanticGraph.graph_id = SEMANTIC_GRAPH_ID;
const subject = sourceEvidence.subject;
if (subject === null) throw new Error("CLI Evidence must include a subject");
const malformedEvidence = createEvidence(
{
path: subject.local_path,
sha256: subject.digest.sha256,
format: subject.format,
...(subject.architecture === null
? {}
: { architecture: subject.architecture }),
},
sourceEvidence.provider,
{
predicateType: sourceEvidence.predicate_type,
operation: sourceEvidence.operation,
parameters: sourceEvidence.parameters,
result: normalizedResult,
confidence: sourceEvidence.confidence,
authority: sourceEvidence.authority,
},
);
expect(malformedEvidence.evidence_id).not.toBe(sourceEvidence.evidence_id);
const arguments_ = {
application: malformedEvidence,
query: {
seed: { kind: "semantic-node", node_id: seedNodeId },
direction: "backward-provenance",
},
};
const cliTrace = await serveCli(cli, [
"trace-javascript-semantics",
JSON.stringify(arguments_),
"--json",
]);
const cliEnvelope = analysisCliErrorEnvelopeSchema.parse(
JSON.parse(cliTrace.stdout),
);
const { error: _label, ...cliError } = cliEnvelope;
expect(cliError).toMatchObject({
code: "invalid_request",
category: "invalid_input",
details: {
issues: [
{
path: [
"application",
"normalized_result",
"semantic_graph",
"graph_id",
],
reason: "invalid_value",
},
],
},
});
const harness = await createApplicationMcpHarness();
try {
await assertSuccessfulTraceParity(
cli,
harness,
sourceEvidence,
arguments_.query,
);
const mcpTrace = await harness.client.callTool({
name: "trace_javascript_semantics",
arguments: arguments_,
});
const { error: mcpError } = parseMcpToolError(mcpTrace);
expect(mcpError).toEqual(cliError);
} finally {
await harness.close();
}
});
const assertSuccessfulTraceParity = async (
cli: ReturnType<typeof createCli>,
harness: Awaited<ReturnType<typeof createApplicationMcpHarness>>,
application: ReturnType<typeof parseEvidence>,
query: { readonly seed: unknown; readonly direction: string },
): Promise<void> => {
const arguments_ = { application, query };
const cliResponse = await serveCli(cli, [
"trace-javascript-semantics",
JSON.stringify(arguments_),
"--json",
]);
const cliEvidence = parseEvidence(JSON.parse(cliResponse.stdout));
const cliResult = javaScriptSemanticTraceResultSchema.parse(
cliEvidence.normalized_result,
);
assertTraceContextsResolve(cliResult);
const mcpResponse = await harness.client.callTool({
name: "trace_javascript_semantics",
arguments: arguments_,
});
expect(mcpResponse.isError).not.toBe(true);
const { tools } = await harness.client.listTools();
const advertised = tools.find(
({ name }) => name === "trace_javascript_semantics",
);
if (advertised?.outputSchema === undefined)
throw new Error("Semantic trace SDK output schema is missing");
const ajv = new Ajv2020({ strict: false, validateFormats: false });
const outputSchema: Record<string, unknown> = advertised.outputSchema;
expect(ajv.validateSchema(outputSchema)).toBe(true);
expect(ajv.validate(outputSchema, mcpResponse.structuredContent)).toBe(true);
const mcpEvidence = toolContract(
"trace_javascript_semantics",
).outputSchema.parse(mcpResponse.structuredContent);
expect(mcpEvidence.normalized_result).toEqual(cliResult);
expect(mcpEvidence).toEqual(cliEvidence);
};
const assertTraceContextsResolve = (
result: ReturnType<typeof javaScriptSemanticTraceResultSchema.parse>,
): void => {
const contexts = new Map(
result.evidence_contexts.map((context) => [context.context_id, context]),
);
const references = [
...result.nodes.map(({ evidence }) => evidence),
...result.relations.map(({ evidence }) => evidence),
...result.unknowns.map(({ evidence }) => evidence),
];
const referencedIds = new Set<string>();
for (const reference of references) {
const context = contexts.get(reference.context_id);
expect(context, reference.context_id).toBeDefined();
if (context === undefined) continue;
referencedIds.add(reference.context_id);
expect(
resolveJavaScriptSemanticEvidence(
{ evidence_contexts: result.evidence_contexts },
reference,
),
).toMatchObject({
...Object.fromEntries(
Object.entries(context).filter(([key]) => key !== "context_id"),
),
location: reference.location,
});
}
expect([...referencedIds].toSorted()).toEqual(
[...contexts.keys()].toSorted(),
);
expect(contexts.size).toBeGreaterThan(0);
};
const serveCli = async (
cli: ReturnType<typeof createCli>,
arguments_: readonly string[],
): Promise<{ readonly stdout: string; readonly exitCode: number }> => {
let stdout = "";
let exitCode = 0;
await cli.serve([...arguments_], {
env: {},
exit: (code) => {
exitCode = code;
},
stdout: (text) => {
stdout += text;
},
});
return { stdout, exitCode };
};
const isJsonObject = (value: unknown): value is Record<string, JsonValue> =>
typeof value === "object" && value !== null && !Array.isArray(value);
@@ -175,7 +175,7 @@ it("names the side and constraint when Evidence is not a usable capture", async
error: {
code: "evidence_integrity_mismatch",
message: expect.stringContaining(
"The right Evidence failed validation (Evidence semantic identifier does not match its record)",
"The right Evidence failed validation (at evidence_id: Evidence semantic identifier does not match its record)",
),
},
});