From 093af33e5c1d7e3812d7ee80d4fa629445be3c47 Mon Sep 17 00:00:00 2001 From: morluto <76467478+morluto@users.noreply.github.com> Date: Sun, 22 Nov 2026 10:03:55 -0500 Subject: [PATCH] perf(wasm): stream digest-only inspection reads (#13440) --- src/wasm/WabtArtifactProvider.test.ts | 113 ++++++++++++++------------ src/wasm/WabtArtifactProvider.ts | 52 ++++++------ 2 files changed, 90 insertions(+), 75 deletions(-) diff --git a/src/wasm/WabtArtifactProvider.test.ts b/src/wasm/WabtArtifactProvider.test.ts index 220763a4bf..eb74fb99c0 100644 --- a/src/wasm/WabtArtifactProvider.test.ts +++ b/src/wasm/WabtArtifactProvider.test.ts @@ -1680,6 +1680,7 @@ it.each([ validation: "rejected-by-selected-profile", }, { stage: "candidate admission", mode: "success", validation: "unknown" }, + { stage: "candidate finalization", mode: "success", validation: "unknown" }, { stage: "glue decoding", mode: "success", validation: "unknown" }, { stage: "glue finalization", mode: "success", validation: "unknown" }, { stage: "producer output", mode: "malformed-wat", validation: "valid" }, @@ -1691,7 +1692,17 @@ it.each([ const fixture = await createWabtArtifactFixture(); const candidate = join(fixture.selectedRoot, "candidate.wasm"); const gluePath = join(fixture.selectedRoot, "glue.js"); - const candidateBytes = Buffer.from("independent candidate bytes"); + const candidateFailure = stage.startsWith("candidate"); + const closingPath = + stage === "candidate finalization" + ? candidate + : stage === "glue finalization" + ? gluePath + : undefined; + const candidateBytes = + stage === "candidate finalization" + ? Buffer.alloc(1024 * 1024 + 29, 0xa5) + : Buffer.from("independent candidate bytes"); await writeFile(candidate, candidateBytes); const missing = join(fixture.selectedRoot, "missing.wasm"); const glueSource = 'fetch("./selected.wasm");'; @@ -1706,8 +1717,8 @@ it.each([ controller: new AbortController(), }), ); - if (stage === "glue finalization") { - fileCloseFailure.selectedPath = gluePath; + if (closingPath !== undefined) { + fileCloseFailure.selectedPath = closingPath; fileCloseFailure.attempts = 0; fileCloseFailure.failFirst = true; } @@ -1719,34 +1730,25 @@ it.each([ stage === "candidate admission" ? [fixture.path, candidate, candidate, missing, missing] : [candidate], - glue_paths: stage === "candidate admission" ? [] : [gluePath], + glue_paths: candidateFailure ? [] : [gluePath], }, { commandTimeoutMs: 42000 }, ); - if (!result.ok) { - expect(result.error.partialObservation).toMatchObject({ - parameters: expect.not.objectContaining({ - command_timeout_ms: expect.anything(), - }), - normalized_result: { tool_profile: { timeout_ms: 42000 } }, - }); - } if (result.ok) throw new Error("Expected the selected failure stage"); expect(result.error._tag).toBe( - validation === "rejected-by-selected-profile" - ? "AnalysisInputError" - : validation === "unknown" - ? "AnalysisInputError" - : "AnalysisOutputError", + validation === "valid" ? "AnalysisOutputError" : "AnalysisInputError", ); expect(result.error.partialObservation).toMatchObject({ + parameters: expect.not.objectContaining({ + command_timeout_ms: expect.anything(), + }), normalized_result: { artifact: { path: fixture.path, sha256: createHash("sha256").update(fixture.bytes).digest("hex"), }, validation, - tool_profile: { release: "1.0.42" }, + tool_profile: { release: "1.0.42", timeout_ms: 42000 }, candidates: [ { path: fixture.path }, { @@ -1755,34 +1757,33 @@ it.each([ bytes: candidateBytes.length, }, ], - glue: - stage === "candidate admission" - ? [] - : [ - { - artifact: { - path: gluePath, - sha256: createHash("sha256") - .update(glueBytes) - .digest("hex"), - }, - parse_status: - stage === "glue finalization" - ? "not-performed" - : stage === "glue decoding" - ? "failed" - : "complete", - references: - stage === "glue decoding" || stage === "glue finalization" - ? [] - : [ - { - value: "./selected.wasm", - candidate_paths: [fixture.path], - }, - ], + glue: candidateFailure + ? [] + : [ + { + artifact: { + path: gluePath, + sha256: createHash("sha256") + .update(glueBytes) + .digest("hex"), }, - ], + parse_status: + stage === "glue finalization" + ? "not-performed" + : stage === "glue decoding" + ? "failed" + : "complete", + references: + stage === "glue decoding" || stage === "glue finalization" + ? [] + : [ + { + value: "./selected.wasm", + candidate_paths: [fixture.path], + }, + ], + }, + ], }, }); if (validation === "rejected-by-selected-profile") @@ -1792,19 +1793,31 @@ it.each([ stderr: "module.wasm: invalid bytes\n", }, }); - if (stage === "candidate admission") + if (candidateFailure) expect(projectAnalysisError(result.error)).toMatchObject({ - code: "invalid_request", - details: { issues: [{ path: ["candidate_paths", 3] }] }, + code: + stage === "candidate finalization" + ? "cleanup_incomplete" + : "invalid_request", + details: { + issues: [ + { + path: [ + "candidate_paths", + stage === "candidate finalization" ? 0 : 3, + ], + }, + ], + }, }); else if (validation === "unknown") expect(result.error).toMatchObject({ issues: [{ path: ["glue_paths", 0] }], }); - if (stage === "glue finalization") { + if (closingPath !== undefined) { expect(result.error).toMatchObject({ cleanupIncomplete: true, - cleanupResources: [gluePath], + cleanupResources: [closingPath], }); expect(fileCloseFailure.attempts).toBe(1); await provider.close(); diff --git a/src/wasm/WabtArtifactProvider.ts b/src/wasm/WabtArtifactProvider.ts index 8ffb27accd..b4a73219ac 100644 --- a/src/wasm/WabtArtifactProvider.ts +++ b/src/wasm/WabtArtifactProvider.ts @@ -17,7 +17,9 @@ import { createAnalysisExecution } from "../ports/AnalysisProvider.js"; import { classifyProviderFailure } from "../providers/classifyProviderFailure.js"; import { completedStableArtifact, + completedStableArtifactStream, readStableArtifact, + streamStableArtifact, type StableArtifactSnapshot, ArtifactResourceScope, ArtifactResourceRetryFailure, @@ -243,6 +245,14 @@ export class WabtArtifactProvider implements WasmArtifactPort { throw cause; } }; + const readIdentity = (path: string, maximumBytes: number) => + streamStableArtifact(path, maximumBytes, { + retainBytes: () => 0, + onChunk: () => {}, + chunkBytes: 1024 * 1024, + signal: options?.signal, + resourceScope: this.#resources, + }); const commandTimeoutMs = input.command_timeout_ms ?? options?.commandTimeoutMs ?? @@ -283,13 +293,7 @@ export class WabtArtifactProvider implements WasmArtifactPort { ); const path = selection.resolvedPath; await access(path, constants.X_OK); - const binary = await readStableArtifact( - path, - limits.toolBytes, - options?.signal, - undefined, - this.#resources, - ); + const binary = await readIdentity(path, limits.toolBytes); commands.push({ tool, path, @@ -312,13 +316,7 @@ export class WabtArtifactProvider implements WasmArtifactPort { let snapshotHash: string | undefined; const verifySnapshot = async () => { if (snapshotHash === undefined) return; - const bytes = await readStableArtifact( - snapshotPath, - limits.inputBytes, - options?.signal, - undefined, - this.#resources, - ); + const bytes = await readIdentity(snapshotPath, limits.inputBytes); if (bytes.sha256 !== snapshotHash) throw new AnalysisArtifactChangedError( OPERATION, @@ -380,13 +378,7 @@ export class WabtArtifactProvider implements WasmArtifactPort { ...(options?.signal === undefined ? {} : { signal: options.signal }), capturedOutput, }); - const current = await readStableArtifact( - command.path, - limits.toolBytes, - options?.signal, - undefined, - this.#resources, - ); + const current = await readIdentity(command.path, limits.toolBytes); if (current.sha256 !== command.sha256) throw new AnalysisOutputError( OPERATION, @@ -466,13 +458,23 @@ export class WabtArtifactProvider implements WasmArtifactPort { if (seenCandidates.has(path)) continue; seenCandidates.add(path); artifactInput = { path, field: ["candidate_paths", index] }; - await readSelectedInput(path, (snapshot) => { + try { + const candidate = await readIdentity(path, limits.inputBytes); candidates.push({ path, - sha256: snapshot.sha256, - bytes: snapshot.bytes.length, + sha256: candidate.sha256, + bytes: candidate.size, }); - }); + } catch (cause: unknown) { + const completed = completedStableArtifactStream(cause); + if (completed !== undefined) + candidates.push({ + path, + sha256: completed.sha256, + bytes: completed.size, + }); + throw cause; + } } for (const [index, path] of input.glue_paths.entries()) { artifactInput = { path, field: ["glue_paths", index] };