* fix(wasm): attribute invalid paths to their selected fields * test(wasm): assert the exact rejected selection issues --------- Co-authored-by: maxazure <405711+maxazure@users.noreply.github.com>
106 lines
3.7 KiB
TypeScript
106 lines
3.7 KiB
TypeScript
import { isAbsolute } from "node:path";
|
|
import type { ExecutionOptions } from "../AnalysisProvider.js";
|
|
import type { WasmArtifactPort } from "./WasmArtifactPort.js";
|
|
import type { AnalysisError } from "../../domain/analysisErrorBase.js";
|
|
import {
|
|
AnalysisCancelledError,
|
|
AnalysisInputError,
|
|
AnalysisOutputError,
|
|
type AnalysisInputIssue,
|
|
} from "../../domain/analysisErrorCore.js";
|
|
import { createEvidence, type Evidence } from "../../domain/evidence.js";
|
|
import {
|
|
inspectWasmArtifactInputSchema,
|
|
wasmArtifactSchema,
|
|
wasmArtifactMatchesSubject,
|
|
} from "../../domain/wasm/wasmArtifact.js";
|
|
import { analysisInputErrorFromIssues } from "../../domain/inputIssueProjection.js";
|
|
import { err, ok, type Result } from "../../domain/result.js";
|
|
|
|
const OPERATION = "inspect_wasm_artifact";
|
|
|
|
/** Shared artifact validation and typed inline Evidence for CLI/MCP. */
|
|
export class WasmArtifactService {
|
|
constructor(readonly provider: WasmArtifactPort) {}
|
|
|
|
/** Join provider cleanup during shutdown or an explicit retry. */
|
|
close(): Promise<void> {
|
|
return this.provider.close();
|
|
}
|
|
|
|
/** Preserve exact selected artifact identity and validated upstream observations. */
|
|
async inspect(
|
|
rawInput: unknown,
|
|
options?: ExecutionOptions,
|
|
): Promise<Result<Evidence, AnalysisError>> {
|
|
if (options?.signal?.aborted)
|
|
return err(new AnalysisCancelledError(OPERATION));
|
|
const input = inspectWasmArtifactInputSchema.safeParse(rawInput);
|
|
if (!input.success)
|
|
return err(
|
|
analysisInputErrorFromIssues(OPERATION, input.error.issues, rawInput, {
|
|
cause: input.error,
|
|
}),
|
|
);
|
|
const pathIssues = [
|
|
{ value: input.data.path, path: ["path"] },
|
|
...input.data.glue_paths.map((value, index) => ({
|
|
value,
|
|
path: ["glue_paths", index],
|
|
})),
|
|
...input.data.candidate_paths.map((value, index) => ({
|
|
value,
|
|
path: ["candidate_paths", index],
|
|
})),
|
|
].flatMap<AnalysisInputIssue>(({ value, path }) =>
|
|
isAbsolute(value)
|
|
? []
|
|
: [
|
|
{
|
|
path,
|
|
reason: "invalid_format",
|
|
message: "Expected an absolute filesystem path on this host.",
|
|
},
|
|
],
|
|
);
|
|
if (pathIssues.length > 0)
|
|
return err(new AnalysisInputError(OPERATION, undefined, pathIssues));
|
|
const inspected = await this.provider.inspect(input.data, options);
|
|
if (!inspected.ok) return inspected;
|
|
if (options?.signal?.aborted)
|
|
return err(new AnalysisCancelledError(OPERATION));
|
|
const execution = inspected.value;
|
|
const report = wasmArtifactSchema.safeParse(execution.result);
|
|
if (
|
|
!report.success ||
|
|
report.data.artifact.path !== input.data.path ||
|
|
!wasmArtifactMatchesSubject(report.data.artifact, execution.subject) ||
|
|
JSON.stringify(report.data.glue.map((item) => item.artifact.path)) !==
|
|
JSON.stringify(input.data.glue_paths) ||
|
|
JSON.stringify(report.data.candidates.map((item) => item.path)) !==
|
|
JSON.stringify([
|
|
...new Set([input.data.path, ...input.data.candidate_paths]),
|
|
])
|
|
)
|
|
return err(
|
|
new AnalysisOutputError(
|
|
OPERATION,
|
|
"WABT adapter returned malformed data or changed the selected artifact identity.",
|
|
),
|
|
);
|
|
return ok(
|
|
createEvidence(execution.subject, execution.provider, {
|
|
operation: OPERATION,
|
|
parameters: input.data,
|
|
result: report.data,
|
|
...(execution.rawResult === null
|
|
? {}
|
|
: { rawResult: execution.rawResult }),
|
|
confidence: "observed",
|
|
locations: execution.locations,
|
|
limitations: execution.limitations,
|
|
}),
|
|
);
|
|
}
|
|
}
|