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 <noreply@anthropic.com>
This commit is contained in:
Kaoru0822
2026-10-09 02:33:16 +00:00
co-authored by Claude Opus 5.5
parent b34bdcdb28
commit eacf8461da
2 changed files with 41 additions and 16 deletions
+14 -8
View File
@@ -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<ArtifactExtractionResult> => {
// 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<ArtifactExtractionInput, "inputPath" | "inputFormat">,
): Promise<boolean> => {
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<ArtifactExtractionInput, "inputPath" | "inputFormat">,
): Promise<ArtifactReader> => {
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);
@@ -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();
});
});