fix(wasm): report WABT output past its budget as a resource constraint (#2117)
* fix(wasm): report WABT output past its budget as a resource constraint A module whose WAT exceeds the 32 MiB output budget failed as unreadable_output, advised `rea doctor`, and echoed the full 32 MiB truncated WAT prefix in details.captured_output. Map the owned-command output-limit outcome during inspection to AnalysisResourceConstraintError naming the budget, as the apktool and native macOS providers do, with a remediation that says the fixed budget will not change on retry. Captured output for an output-limit failure keeps only the first 4,096 characters of each stream, including on the cleanup-failure path. An oversized --version probe keeps its configuration-phase mapping. * fix(wasm): keep observed sizes and whole code points in output-limit excerpts Cut each stream's output-limit excerpt with boundedUtf8Text at 4,096 UTF-8 bytes, so an astral character at the cut no longer leaves a lone surrogate. Mark the output truncated when either excerpt was cut, and report each stream's observed byte count. Word the remediation as a fact about the fixed budget. A flood of diagnostics from any WABT tool reaches this path, so it no longer says the module is too large. Replace the 32 MiB end-to-end test with a launcher seam that raises a small output-limit failure. * fix(wasm): match sibling captured-output fields for WABT output limits Report retained byte counts in stdout_bytes and stderr_bytes, as apktool and the firmware command do, and record exit_code and signal. Mark the excerpt truncated outright, since the command raises output-limit only after retention was truncated. When cleanup also fails, record the output limit and its budget as the previous error instead of the generic command message. Document that the two streams share the budget, so the excerpt holds up to 4,096 of the bytes each stream retained. * docs(wasm): scope the output-budget error to inspection commands * docs(wasm): distinguish reported warm costs from unmeasured gaps * docs(wasm): clarify the scope of reported warm measurements --------- Co-authored-by: morluto <76467478+morluto@users.noreply.github.com>
This commit is contained in:
co-authored by
morluto
parent
729ca2ab6a
commit
42667fc1a0
+18
-3
@@ -61,9 +61,24 @@ The tool profile preserves upstream release/source identity, resolved executable
|
||||
paths, executable digests, exact banners and arguments. Executable digests are
|
||||
checked again after each command. Caller-supplied tools are trusted programs;
|
||||
version and digest recording are not publisher authentication or an OS sandbox.
|
||||
Each subprocess has a 30-second deadline and 32 MiB combined output budget;
|
||||
truncation fails. All commands use existing owned-process cancellation and
|
||||
cleanup. Workspace cleanup failures remain explicit errors with resource paths.
|
||||
Each subprocess has a 30-second deadline and 32 MiB combined output budget.
|
||||
Inspection output past the budget fails with `resource_constraint`; an oversized
|
||||
`--version` probe still reports `unreadable_output`. Both streams share the
|
||||
budget, so a stream that starts after the other has filled it retains nothing.
|
||||
The captured output keeps up to the first 4,096 bytes that each stream retained,
|
||||
each stream's retained byte count, and the exit code and signal. Retained text
|
||||
can end in U+FFFD where the budget cut a multi-byte character.
|
||||
|
||||
These excerpts bound the returned diagnostic text.
|
||||
[PR #2117](https://github.com/morluto/rea/pull/2117) reports elapsed time, CPU
|
||||
time, and peak process-family memory, including WABT children, for warm Linux
|
||||
x64 WABT 1.0.42 CLI workflows; those costs did not change outside measurement
|
||||
noise. Agent token usage and truly cold workflows remain
|
||||
unmeasured. The underlying error cause still retains the full captured
|
||||
subprocess snapshot, so the excerpt does not establish lower retained memory
|
||||
or faster analysis.
|
||||
All commands use existing owned-process cancellation and cleanup. Workspace
|
||||
cleanup failures remain explicit errors with resource paths.
|
||||
|
||||
WAT is a decoded representation, not original source or proof of runtime
|
||||
behavior. Custom annotations can preserve payloads, but WAT does not establish original
|
||||
|
||||
@@ -8,6 +8,8 @@ import { PrivateRuntimeRootUnavailableError } from "../process/PrivateRuntimeRoo
|
||||
import { spawnOwnedProviderProcess } from "../process/ProviderProcess.js";
|
||||
import { OwnedCommandFailure } from "../process/OwnedCommand.js";
|
||||
import { projectAnalysisError } from "../domain/analysisErrorProjection.js";
|
||||
import { WABT_LIMITS } from "./WabtRelease.js";
|
||||
import { wabtFailure } from "./WabtFailures.js";
|
||||
|
||||
const fileCloseFailure = vi.hoisted(() => ({
|
||||
selectedPath: "",
|
||||
@@ -391,3 +393,157 @@ it.each([
|
||||
expect(() => process.kill(pid, 0)).toThrow();
|
||||
}
|
||||
});
|
||||
const outputLimitFailure = ({
|
||||
stdout = "(module",
|
||||
stderr = "",
|
||||
cleanupFailure = null,
|
||||
}: {
|
||||
readonly stdout?: string;
|
||||
readonly stderr?: string;
|
||||
readonly cleanupFailure?: string | null;
|
||||
} = {}) =>
|
||||
new OwnedCommandFailure(
|
||||
"output-limit",
|
||||
"Command diagnostic output exceeded its complete-output budget.",
|
||||
{
|
||||
diagnosticTruncated: true,
|
||||
// Observed sizes differ from retained sizes, as after a real overflow.
|
||||
stdout: {
|
||||
text: stdout,
|
||||
bytes: Buffer.byteLength(stdout),
|
||||
observedBytes: Buffer.byteLength(stdout) + 1000,
|
||||
},
|
||||
stderr: {
|
||||
text: stderr,
|
||||
bytes: Buffer.byteLength(stderr),
|
||||
observedBytes: Buffer.byteLength(stderr) + 1000,
|
||||
},
|
||||
exitCode: null,
|
||||
signal: "SIGKILL",
|
||||
},
|
||||
cleanupFailure,
|
||||
undefined,
|
||||
cleanupFailure === null ? [] : ["/owned/runtime"],
|
||||
);
|
||||
it("reports WABT output beyond the budget as a resource constraint with a bounded excerpt", async () => {
|
||||
const selectedRoot = await createTestTempDirectory("rea-wabt-budget-");
|
||||
const path = join(selectedRoot, "selected.wasm");
|
||||
await writeFile(path, Buffer.alloc(76, 1));
|
||||
for (const tool of ["wasm-validate", "wasm-objdump", "wasm2wat"])
|
||||
await writeFile(
|
||||
join(selectedRoot, tool + (process.platform === "win32" ? ".exe" : "")),
|
||||
"source-owned executable seam",
|
||||
{ mode: 0o700 },
|
||||
);
|
||||
const dump = await readFile(
|
||||
new URL("../../tests/fixtures/wasm/objdump.txt", import.meta.url),
|
||||
"utf8",
|
||||
);
|
||||
const provider = new WabtArtifactProvider(
|
||||
{ REA_WABT_BIN_DIRECTORY: selectedRoot },
|
||||
(input) => {
|
||||
const version = input.arguments[0] === "--version";
|
||||
if (!version && input.command.includes("wasm2wat"))
|
||||
throw outputLimitFailure({ stdout: "(module" + "a".repeat(5000) });
|
||||
const script = version
|
||||
? 'process.stdout.write("1.0.42\\n")'
|
||||
: input.command.includes("objdump")
|
||||
? `process.stdout.write(${JSON.stringify(dump)})`
|
||||
: "";
|
||||
return spawnOwnedProviderProcess({
|
||||
...input,
|
||||
command: process.execPath,
|
||||
arguments: ["-e", script],
|
||||
expectedCommand: null,
|
||||
});
|
||||
},
|
||||
);
|
||||
const result = await provider.inspect({
|
||||
path,
|
||||
glue_paths: [],
|
||||
candidate_paths: [],
|
||||
});
|
||||
if (result.ok) throw new Error("Expected the output budget to fail");
|
||||
expect(result.error).toMatchObject({
|
||||
_tag: "AnalysisResourceConstraintError",
|
||||
resource: "memory",
|
||||
reportedLimits: { max_output_bytes: WABT_LIMITS.outputBytes },
|
||||
});
|
||||
const projection = projectAnalysisError(result.error);
|
||||
expect(projection).toMatchObject({
|
||||
code: "resource_constraint",
|
||||
message: expect.stringContaining(
|
||||
`${String(WABT_LIMITS.outputBytes)}-byte output budget`,
|
||||
),
|
||||
remediation: {
|
||||
action: expect.stringContaining("retrying with the same input"),
|
||||
},
|
||||
});
|
||||
expect(projection.details?.captured_output).toEqual({
|
||||
stdout: "(module" + "a".repeat(4089),
|
||||
stderr: "",
|
||||
truncated: true,
|
||||
stdout_bytes: 5007,
|
||||
stderr_bytes: 0,
|
||||
exit_code: null,
|
||||
signal: "SIGKILL",
|
||||
});
|
||||
});
|
||||
it("bounds an oversized stderr to an excerpt", () => {
|
||||
expect(
|
||||
projectAnalysisError(
|
||||
wabtFailure(
|
||||
outputLimitFailure({ stderr: "w".repeat(5000) }),
|
||||
"producer",
|
||||
"/module.wasm",
|
||||
),
|
||||
).details?.captured_output,
|
||||
).toEqual({
|
||||
stdout: "(module",
|
||||
stderr: "w".repeat(4096),
|
||||
truncated: true,
|
||||
stdout_bytes: 7,
|
||||
stderr_bytes: 5000,
|
||||
exit_code: null,
|
||||
signal: "SIGKILL",
|
||||
});
|
||||
});
|
||||
it("cuts the excerpt on a code point boundary", () => {
|
||||
const failure = wabtFailure(
|
||||
outputLimitFailure({ stdout: "a".repeat(4095) + "\u{1F600}b" }),
|
||||
"producer",
|
||||
"/module.wasm",
|
||||
);
|
||||
expect(failure.capturedOutput?.stdout).toBe("a".repeat(4095));
|
||||
});
|
||||
it("bounds the excerpt when cleanup also fails after the output budget", () => {
|
||||
expect(
|
||||
projectAnalysisError(
|
||||
wabtFailure(
|
||||
outputLimitFailure({
|
||||
stderr: "w".repeat(5000),
|
||||
cleanupFailure: "Source-owned cleanup uncertainty",
|
||||
}),
|
||||
"producer",
|
||||
"/module.wasm",
|
||||
),
|
||||
),
|
||||
).toMatchObject({
|
||||
code: "cleanup_incomplete",
|
||||
details: {
|
||||
diagnostics: {
|
||||
previous_error: {
|
||||
failure_kind: "output-limit",
|
||||
message: `WABT output exceeded the ${String(WABT_LIMITS.outputBytes)}-byte output budget`,
|
||||
max_output_bytes: WABT_LIMITS.outputBytes,
|
||||
},
|
||||
captured_output: { stdout: "(module", stderr: "w".repeat(4096) },
|
||||
},
|
||||
},
|
||||
});
|
||||
});
|
||||
it("does not blame the input when a version probe exceeds the budget", () => {
|
||||
expect(
|
||||
wabtFailure(outputLimitFailure(), "configuration", "/module.wasm"),
|
||||
).toMatchObject({ _tag: "AnalysisOutputError" });
|
||||
});
|
||||
|
||||
@@ -1,4 +1,6 @@
|
||||
import { ArtifactReaderFailure } from "../artifacts/ArtifactReader.js";
|
||||
import { boundedUtf8Text } from "../process/boundedUtf8Text.js";
|
||||
import type { ProviderProcessSnapshot } from "../process/ProviderProcess.js";
|
||||
import { AnalysisError } from "../domain/analysisErrorBase.js";
|
||||
import {
|
||||
AnalysisCancelledError,
|
||||
@@ -6,6 +8,7 @@ import {
|
||||
AnalysisAccessDeniedError,
|
||||
AnalysisInputError,
|
||||
AnalysisOutputError,
|
||||
AnalysisResourceConstraintError,
|
||||
AnalysisTimeoutError,
|
||||
AnalysisCapabilityUnavailableError,
|
||||
} from "../domain/analysisErrorCore.js";
|
||||
@@ -13,6 +16,21 @@ import { ProviderCleanupError } from "../domain/providerCleanupError.js";
|
||||
import { OwnedCommandFailure } from "../process/OwnedCommand.js";
|
||||
import { PrivateRuntimeRootUnavailableError } from "../process/PrivateRuntimeRootUnavailableError.js";
|
||||
import { WABT_LIMITS, WABT_PROVIDER_IDENTITY } from "./WabtRelease.js";
|
||||
const OUTPUT_LIMIT_EXCERPT_BYTES = 4096;
|
||||
const OUTPUT_LIMIT_REASON = `WABT output exceeded the ${String(WABT_LIMITS.outputBytes)}-byte output budget`;
|
||||
/** Past the output budget, retained text is a prefix of an oversized stream; keep a bounded excerpt of each. */
|
||||
const outputLimitExcerpt = (snapshot: ProviderProcessSnapshot) => ({
|
||||
stdout: boundedUtf8Text(snapshot.stdout.text, OUTPUT_LIMIT_EXCERPT_BYTES)
|
||||
.text,
|
||||
stderr: boundedUtf8Text(snapshot.stderr.text, OUTPUT_LIMIT_EXCERPT_BYTES)
|
||||
.text,
|
||||
// The command reports output-limit only after retention was truncated.
|
||||
truncated: true,
|
||||
stdout_bytes: snapshot.stdout.bytes,
|
||||
stderr_bytes: snapshot.stderr.bytes,
|
||||
exit_code: snapshot.exitCode ?? null,
|
||||
signal: snapshot.signal ?? null,
|
||||
});
|
||||
/** Preserve lifecycle failures and diagnostic bytes instead of returning partial success. */
|
||||
export const wabtFailure = (
|
||||
cause: unknown,
|
||||
@@ -37,11 +55,13 @@ export const wabtFailure = (
|
||||
const capturedOutput =
|
||||
cause.snapshot === null
|
||||
? undefined
|
||||
: {
|
||||
stdout: cause.snapshot.stdout.text,
|
||||
stderr: cause.snapshot.stderr.text,
|
||||
truncated: cause.snapshot.diagnosticTruncated ?? false,
|
||||
};
|
||||
: cause.reason === "output-limit"
|
||||
? outputLimitExcerpt(cause.snapshot)
|
||||
: {
|
||||
stdout: cause.snapshot.stdout.text,
|
||||
stderr: cause.snapshot.stderr.text,
|
||||
truncated: cause.snapshot.diagnosticTruncated ?? false,
|
||||
};
|
||||
const options =
|
||||
capturedOutput === undefined ? { cause } : { cause, capturedOutput };
|
||||
if (cause.cleanupFailure !== null)
|
||||
@@ -50,7 +70,14 @@ export const wabtFailure = (
|
||||
cause.resources,
|
||||
{
|
||||
reason: cause.cleanupFailure,
|
||||
previous_error: reason,
|
||||
previous_error:
|
||||
cause.reason === "output-limit"
|
||||
? {
|
||||
failure_kind: "output-limit",
|
||||
message: OUTPUT_LIMIT_REASON,
|
||||
max_output_bytes: WABT_LIMITS.outputBytes,
|
||||
}
|
||||
: reason,
|
||||
...(capturedOutput === undefined
|
||||
? {}
|
||||
: { captured_output: capturedOutput }),
|
||||
@@ -65,6 +92,18 @@ export const wabtFailure = (
|
||||
WABT_LIMITS.timeoutMs,
|
||||
options,
|
||||
);
|
||||
if (phase !== "configuration" && cause.reason === "output-limit")
|
||||
return new AnalysisResourceConstraintError(
|
||||
"inspect_wasm_artifact",
|
||||
"memory",
|
||||
OUTPUT_LIMIT_REASON,
|
||||
{ max_output_bytes: WABT_LIMITS.outputBytes },
|
||||
{
|
||||
...options,
|
||||
remediationAction:
|
||||
"The WABT output budget is fixed, so retrying with the same input will fail the same way.",
|
||||
},
|
||||
);
|
||||
if (phase === "configuration" && cause.reason === "process")
|
||||
return new AnalysisCapabilityUnavailableError(
|
||||
WABT_PROVIDER_IDENTITY.id,
|
||||
|
||||
Reference in New Issue
Block a user