Files
reverse-engineering-anything/src/wasm/WabtFailures.test.ts
T
morluto 8cad335fe1 test(wabt): consolidate provider failure and output checks (#11885)
* test(wabt): consolidate provider failure and output checks

* test(wabt): assert the structured command deadline
2026-11-18 07:02:58 +08:00

379 lines
12 KiB
TypeScript

import { spawn } from "node:child_process";
import { expect, it } from "vitest";
import { ArtifactReaderFailure } from "../resources/ArtifactReader.js";
import { projectAnalysisError } from "../domain/analysisErrorProjection.js";
import { wabtFailureDialect } from "./WabtFailures.js";
import { classifyProviderFailure } from "../providers/classifyProviderFailure.js";
import { WASM_RESOURCE_DEFAULTS } from "../domain/wasm/wasmArtifact.js";
import { OwnedCommandFailure } from "../process/OwnedCommand.js";
import { UnclassifiedProviderFailure } from "../domain/unclassifiedProviderFailure.js";
/** Classify a raw failure the way the provider does, through WABT's dialect. */
const wabtFailure = (
cause: unknown,
...dialect: Parameters<typeof wabtFailureDialect>
) => classifyProviderFailure(cause, wabtFailureDialect(...dialect));
it.each(["configuration", "artifact"] as const)(
"keeps host allocation failure distinct from %s readiness and byte admission",
(phase) => {
const cause = Object.assign(new Error("Allocation failed"), {
code: "ENOMEM",
});
const failure = projectAnalysisError(
wabtFailure(cause, phase, "/module.wasm"),
);
expect(failure).toMatchObject({
code: "resource_constraint",
retryable: false,
details: { resource: "memory" },
});
expect(failure.remediation.action).not.toContain("REA_WABT_BIN_DIRECTORY");
},
);
it.each(["configuration", "producer"] as const)(
"retains timeout recovery under %s cleanup failure without making cleanup retryable",
(phase) => {
const cause = new OwnedCommandFailure(
"timeout",
"Command deadline elapsed.",
null,
"owned stop unconfirmed",
undefined,
["pid:owned-fixture"],
);
const failure = wabtFailure(cause, phase, "/module.wasm", {
commandTimeoutMs: 1234,
});
// The typed timeout stays the cleanup primary, so its control is the
// composite's own limit.
expect(failure.cleanupPrimary?.cause).toBe(cause);
const projected = projectAnalysisError(failure);
expect(projected).toMatchObject({
code: "cleanup_incomplete",
retryable: false,
details: {
resources: ["pid:owned-fixture"],
limit: {
setting: "wasm.command_timeout_ms",
effective_value: 1234,
parameter: "command_timeout_ms",
update_mode: "runtime",
pending_restart: false,
},
diagnostics: {
primary_error: {
code: "provider_timeout",
details: { timeout_ms: 1234 },
},
},
},
});
expect(projected.recovery).toBeUndefined();
},
);
it("retains timeout command output once, including when cleanup wraps its recovery", () => {
for (const cleanup of [null, "stop unconfirmed"]) {
const cause = new OwnedCommandFailure(
"timeout",
"deadline",
{
stdout: { text: "partial-owned-output", bytes: 20, observedBytes: 20 },
stderr: { text: "", bytes: 0, observedBytes: 0 },
exitCode: null,
signal: null,
},
cleanup,
);
const projected = projectAnalysisError(
wabtFailure(cause, "producer", "/module.wasm", {
commandTimeoutMs: 1234,
}),
);
expect(
JSON.stringify(projected).split("partial-owned-output"),
).toHaveLength(2);
expect(projected.code).toBe(
cleanup === null ? "provider_timeout" : "cleanup_incomplete",
);
}
});
it.each([1234, 2_147_483_647])(
"reports the actual %i ms command budget without an unsupported higher timer",
(commandTimeoutMs) => {
const projected = projectAnalysisError(
wabtFailure(
new OwnedCommandFailure("timeout", "deadline"),
"producer",
"/module.wasm",
{ commandTimeoutMs },
),
);
expect(projected).toMatchObject({
code: "provider_timeout",
details: {
timeout_ms: commandTimeoutMs,
limit: {
setting: "wasm.command_timeout_ms",
effective_value: commandTimeoutMs,
minimum: 1,
maximum: 2_147_483_647,
unit: "ms",
parameter: "command_timeout_ms",
applies_at: "next_operation",
agent_editable: true,
update_mode: "runtime",
pending_restart: false,
},
},
recovery: { kind: "inspect_timeout_state", timeout_ms: commandTimeoutMs },
});
expect(
projectAnalysisError(
wabtFailure(
new OwnedCommandFailure("timeout", "deadline"),
"producer",
"/module.wasm",
{ commandTimeoutMs, commandTimeoutSource: "request" },
),
),
).toMatchObject({
details: {
limit: {
source: "request",
update_mode: "parameter",
effective_value: commandTimeoutMs,
},
},
});
},
);
it.each([
{ reason: "timeout" as const, cleanup: null, code: "provider_timeout" },
{ reason: "cancelled" as const, cleanup: null, code: "cancelled" },
{
reason: "process" as const,
cleanup: "owned stop failed",
code: "cleanup_incomplete",
},
])(
"retains version-probe facts without changing $reason cleanup=$cleanup",
({ reason, cleanup, code }) => {
const callerReason = { source: "version probe caller" };
const cause = new OwnedCommandFailure(
reason,
"version command failed",
null,
cleanup,
reason === "cancelled"
? { cancellation: { reason: callerReason } }
: undefined,
);
const probe = {
configuration_key: "REA_WABT_BIN_DIRECTORY" as const,
executable_path: "/tools/wasm2wat",
tool: "wasm2wat",
expected_version: "1.0.42" as const,
accepted_releases: "WABT >=1.0.42 <1.1.0 (verified with 1.0.42)",
};
const failure = wabtFailure(cause, "configuration", "/selected.wasm", {
commandTimeoutMs: 1234,
versionProbe: probe,
});
expect(failure.cleanupIncomplete).toBe(cleanup !== null);
expect((failure.cleanupPrimary ?? failure).cause).toBe(
reason === "cancelled" ? callerReason : cause,
);
expect(projectAnalysisError(failure)).toMatchObject({
code,
details: {
diagnostics:
cleanup === null
? probe
: { primary_error: { details: { diagnostics: probe } } },
},
});
},
);
it.each([
{ code: "EACCES", action: "read and execute access" },
{
code: "ENOENT",
action: "contains wasm-validate, wasm-objdump and wasm2wat",
},
{ code: "ENOTDIR", action: "directory, not an individual executable" },
])(
"explains $code configuration recovery without blaming the WASM input",
({ code, action }) => {
const failure = wabtFailure(
Object.assign(new Error(`tool admission failed: ${code}`), { code }),
"configuration",
"/selected.wasm",
);
const projected = projectAnalysisError(failure);
expect(projected).toMatchObject({
code: "capability_unavailable",
retryable: false,
remediation: { action: expect.stringContaining(action) },
});
expect(projected.remediation.action).toContain("REA_WABT_BIN_DIRECTORY");
expect(projected.remediation.action).toContain("restart");
expect(projected.remediation.action).toContain("Do not repeat");
},
);
it.each([false, true])(
"preserves the file-size boundary with cleanup=%s when an input exceeds its allowance",
(cleanup) => {
const cause = new ArtifactReaderFailure(
"limit",
"Selected artifact exceeds its byte allowance",
{
boundary: "file-size",
...(cleanup
? {
cleanup: { reason: "close denied", resources: ["selected.wasm"] },
}
: {}),
},
);
const projected = projectAnalysisError(
wabtFailure(cause, "artifact", "selected.wasm"),
);
expect(projected).toMatchObject({
code: cleanup ? "cleanup_incomplete" : "resource_constraint",
category: "resource_constraint",
retryable: false,
details: {
resource: "file-size",
reported_limits: {
maximum_input_bytes: WASM_RESOURCE_DEFAULTS.inputBytes,
},
},
});
if (!cleanup) {
expect(projected.remediation.action).toContain("max_input_bytes");
expect(projected.remediation.action).not.toContain("evidence reply");
expect(projected.remediation.action).not.toContain("--");
expect(projected.details?.limit).toMatchObject({
control_kind: "parameter",
parameter: "max_input_bytes",
effective_value: WASM_RESOURCE_DEFAULTS.inputBytes,
default_value: WASM_RESOURCE_DEFAULTS.inputBytes,
});
}
expect(
wabtFailure(cause, "artifact", "selected.wasm").cleanupResources,
).toEqual(cleanup ? ["selected.wasm"] : []);
},
);
it.each([null, { stopped: "caller" }])(
"keeps the command-owned cancellation reason as the direct typed cause: %j",
(reason) => {
const cause = new OwnedCommandFailure(
"cancelled",
"cancelled",
null,
null,
{ cancellation: { reason } },
);
const error = wabtFailure(cause, "producer", "/module.wasm");
expect(error._tag).toBe("AnalysisCancelledError");
expect(error.cause).toBe(reason);
},
);
it("reports a WABT tool ended by an inherited file-size limit as a resource constraint", () => {
const cause = new OwnedCommandFailure("process", "signaled", {
stdout: { text: "", bytes: 0, observedBytes: 0 },
stderr: { text: "", bytes: 0, observedBytes: 0 },
exitCode: null,
signal: "SIGXFSZ",
});
expect(
projectAnalysisError(wabtFailure(cause, "producer", "/module.wasm")),
).toMatchObject({
code: "resource_constraint",
details: { resource: "file-size" },
});
});
it("reports a WABT tool the host refused to start as unavailable", async () => {
const refused = await new Promise<unknown>((resolve) => {
spawn("/nonexistent-wabt/wasm-objdump").on("error", resolve);
});
expect(
projectAnalysisError(wabtFailure(refused, "producer", "/module.wasm")),
).toMatchObject({ code: "capability_unavailable" });
});
it("reports an unrecognised WABT producer exit with the examined outcome", () => {
const cause = new OwnedCommandFailure("process", "exited with code 2", {
stdout: { text: "", bytes: 0, observedBytes: 0 },
stderr: { text: "unexpected", bytes: 10, observedBytes: 10 },
exitCode: 2,
signal: null,
});
const failure = wabtFailure(cause, "producer", "/module.wasm");
expect(failure).toBeInstanceOf(UnclassifiedProviderFailure);
expect(failure).toMatchObject({
providerId: "wabt-artifact",
examined: { failure_kind: "exited", exit_code: 2 },
capturedOutput: { stderr: "unexpected" },
});
expect(projectAnalysisError(failure).code).toBe("execution_failure");
});
it("keeps setup guidance when a version probe's output cannot be collected", () => {
const cause = new OwnedCommandFailure(
"process",
"stdout stream failed",
{
stdout: { text: "", bytes: 0, observedBytes: 0 },
stderr: { text: "", bytes: 0, observedBytes: 0 },
exitCode: 0,
signal: null,
},
null,
{ collectionFailure: true },
);
const projected = projectAnalysisError(
wabtFailure(cause, "configuration", "/module.wasm"),
);
expect(projected).toMatchObject({ code: "capability_unavailable" });
expect(projected.remediation.action).toContain("REA_WABT_BIN_DIRECTORY");
});
it.each([
{ phase: "configuration", hasEarlierOutput: true },
{ phase: "artifact", hasEarlierOutput: false },
{ phase: "producer", hasEarlierOutput: true },
] as const)(
"keeps caller cancellation and observed output from a $phase-phase read",
({ phase, hasEarlierOutput }) => {
const reason = { stopped: "caller" };
const observedOutput = hasEarlierOutput
? { stdout: "earlier", stderr: "", truncated: false }
: undefined;
const failure = wabtFailure(
new ArtifactReaderFailure("cancelled", "Artifact read cancelled", {
cause: reason,
}),
phase,
"/selected.wasm",
{ commandTimeoutMs: 1234, observedOutput },
);
expect(failure).toMatchObject({
_tag: "AnalysisCancelledError",
cause: reason,
});
expect(failure.capturedOutput).toEqual(observedOutput);
},
);