From eacf8461dad6bab11a3d5438e6db001a10f33a9b Mon Sep 17 00:00:00 2001 From: Kaoru0822 <208281322+kaoru0822-kitauji@users.noreply.github.com> Date: Fri, 9 Oct 2026 02:33:16 +0000 Subject: [PATCH] fix(artifacts): keep cancellation and the caller path in extraction refusals Two follow-ups to #1161 from its post-merge review: - The new format preflight ran before scanArtifactInventory, which was the first cancellation check. An already-cancelled extraction of a DMG or PKG therefore reported unsupported_target instead of cancellation. Check the signal before the preflight. - The unsupported-format and encrypted-entry refusals reported the realpath. When the caller selected the target through a symlink, junction, or other alias, the diagnostic named a different path. Report the caller's input path; I/O still uses the canonical path. Co-Authored-By: Claude Opus 5.5 --- .../extraction/ArtifactExtraction.ts | 22 +++++++----- .../artifactProvider.extraction.test.ts | 35 ++++++++++++++----- 2 files changed, 41 insertions(+), 16 deletions(-) diff --git a/src/artifacts/extraction/ArtifactExtraction.ts b/src/artifacts/extraction/ArtifactExtraction.ts index 4cd226831..3d2ae4eee 100644 --- a/src/artifacts/extraction/ArtifactExtraction.ts +++ b/src/artifacts/extraction/ArtifactExtraction.ts @@ -11,6 +11,7 @@ import { type ArtifactEntry, type ArtifactReader, } from "../ArtifactReader.js"; +import { abortIfNeeded } from "../ArtifactHash.js"; import { DirectoryArtifactReader } from "../DirectoryArtifactReader.js"; import { SafeOutputTree } from "../SafeOutputTree.js"; import { ZipArtifactReader } from "../ZipArtifactReader.js"; @@ -38,8 +39,10 @@ export const extractArtifact = async ( input: ArtifactExtractionInput, signal?: AbortSignal, ): Promise => { + // Cancellation wins over every later refusal, including unsupported formats. + abortIfNeeded(signal); const sourcePath = await realpath(input.inputPath); - await requireExtractableFormat(sourcePath, input.inputFormat); + await requireExtractableFormat(sourcePath, input); const snapshot = await scanArtifactInventory(sourcePath, { signal, }); @@ -72,7 +75,7 @@ export const extractArtifact = async ( if (occurrence.encrypted) throw new AnalysisUnsupportedTargetError( "extract_artifact", - sourcePath, + input.inputPath, `Archive entry ${occurrence.logical_path} is encrypted; REA does not decrypt archive entries, so the archive cannot be extracted completely`, ); if ( @@ -130,7 +133,7 @@ const materializeSelection = async ({ const byPath = new Map( selected.map((item) => [item.occurrence.logical_path, item]), ); - const reader = await createReader(sourcePath, input.inputFormat); + const reader = await createReader(sourcePath, input); const output = await SafeOutputTree.create(input.outputRoot); let readerClosed = false; const extracted: ExtractedOccurrence[] = []; @@ -258,12 +261,14 @@ const isZipFormat = ( /** * Refuse a format without an extraction reader before inventory work starts. - * The target kind, not the host, is unsupported by extraction. + * The target kind, not the host, is unsupported by extraction. The error keeps + * the caller's spelling of the path; I/O uses the canonical path. */ const requireExtractableFormat = async ( path: string, - format: BinaryTarget["format"], + input: Pick, ): Promise => { + const format = input.inputFormat; const directory = (await lstat(path)).isDirectory(); if ( !directory && @@ -273,7 +278,7 @@ const requireExtractableFormat = async ( ) throw new AnalysisUnsupportedTargetError( "extract_artifact", - path, + input.inputPath, `Artifact format has no extraction reader: ${format}`, ); return directory; @@ -281,9 +286,10 @@ const requireExtractableFormat = async ( const createReader = async ( path: string, - format: BinaryTarget["format"], + input: Pick, ): Promise => { - if (await requireExtractableFormat(path, format)) + const format = input.inputFormat; + if (await requireExtractableFormat(path, input)) return new DirectoryArtifactReader(path); if (format === "asar") return new AsarArtifactReader(path); if (isZipFormat(format)) return new ZipArtifactReader(path, format); diff --git a/tests/boundary/providers/native/artifactProvider.extraction.test.ts b/tests/boundary/providers/native/artifactProvider.extraction.test.ts index 509a34798..038b863a7 100644 --- a/tests/boundary/providers/native/artifactProvider.extraction.test.ts +++ b/tests/boundary/providers/native/artifactProvider.extraction.test.ts @@ -1,4 +1,11 @@ -import { access, mkdir, readFile, readdir, writeFile } from "node:fs/promises"; +import { + access, + mkdir, + readFile, + readdir, + symlink, + writeFile, +} from "node:fs/promises"; import { join } from "node:path"; import { describe, expect, it } from "vitest"; @@ -137,21 +144,33 @@ describe("artifact extraction", () => { const root = await createTestTempDirectory("rea-extract-dmg-"); const image = join(root, "Image.dmg"); await writeFile(image, "not mounted"); + // A caller-specific alias must be the path the refusal reports. + const alias = join(root, "Alias.dmg"); + await symlink(image, alias); const output = join(root, "output"); - const result = await new ArtifactProvider() - .createClient(target(image, "dmg")) - .execute( - "extract_artifact", - artifactExtractionExecutionSchema.parse({ output_root: output }), - ); - expect(result).toMatchObject({ + const extract = (signal?: AbortSignal) => + new ArtifactProvider() + .createClient(target(alias, "dmg")) + .execute( + "extract_artifact", + artifactExtractionExecutionSchema.parse({ output_root: output }), + signal === undefined ? undefined : { signal }, + ); + expect(await extract()).toMatchObject({ ok: false, error: { _tag: "AnalysisUnsupportedTargetError", operation: "extract_artifact", + path: alias, reason: "Artifact format has no extraction reader: dmg", }, }); + const controller = new AbortController(); + controller.abort(); + expect(await extract(controller.signal)).toMatchObject({ + ok: false, + error: { _tag: "ArtifactOperationError", reason: "cancelled" }, + }); await expect(access(output)).rejects.toThrow(); }); });