From 55c317d2b6dd545cf0e3d290d6bc91ad6b69741a Mon Sep 17 00:00:00 2001 From: morluto <76467478+morluto@users.noreply.github.com> Date: Mon, 16 Nov 2026 06:47:48 +0800 Subject: [PATCH] refactor(domain): give the binary target filesystem failure projection a domain owner (#11531) --- scripts/lib/module-layers.baseline.json | 4 +- src/application/BinaryTargetResolver.ts | 66 ++----------------- src/application/DirectAnalysis.ts | 2 +- src/domain/binaryTargetFilesystemFailure.ts | 64 ++++++++++++++++++ src/dotnet/ManagedSnapshot.ts | 2 +- .../directAnalysisStatFailure.test.ts | 2 +- 6 files changed, 72 insertions(+), 68 deletions(-) create mode 100644 src/domain/binaryTargetFilesystemFailure.ts diff --git a/scripts/lib/module-layers.baseline.json b/scripts/lib/module-layers.baseline.json index 6879fbed63..6042bd5ddf 100644 --- a/scripts/lib/module-layers.baseline.json +++ b/scripts/lib/module-layers.baseline.json @@ -1,7 +1,5 @@ { - "upward": { - "dotnet -> application": 1 - }, + "upward": {}, "cycles": [ "cli <-> cli.ts", "cli <-> cliBrowserScenarioCommands.ts", diff --git a/src/application/BinaryTargetResolver.ts b/src/application/BinaryTargetResolver.ts index b98f5739d4..92c83eca46 100644 --- a/src/application/BinaryTargetResolver.ts +++ b/src/application/BinaryTargetResolver.ts @@ -28,6 +28,10 @@ import { access, realpath, stat, type FileHandle } from "node:fs/promises"; import { extname } from "node:path"; import { BinaryTargetError } from "../domain/configurationErrors.js"; +import { + classifyTargetFilesystemFailure, + type TargetFilesystemStage, +} from "../domain/binaryTargetFilesystemFailure.js"; import { assertStableRegularFileIdentity, RegularFileChangedError, @@ -654,68 +658,6 @@ const targetResolutionFailure = ( return err(failure); }; -/** - * `resolve` covers the caller-selected path before canonicalization; `read` - * covers a canonical path opened with symbolic links rejected. - */ -type TargetFilesystemStage = "resolve" | "read"; - -/** Classify a filesystem failure while reading a previously selected target. */ -export const classifyBinaryTargetFilesystemFailure = ( - path: string, - cause: unknown, -): BinaryTargetError | undefined => - classifyTargetFilesystemFailure(path, cause, "read"); - -const classifyTargetFilesystemFailure = ( - path: string, - cause: unknown, - stage: TargetFilesystemStage, -): BinaryTargetError | undefined => { - if (!(cause instanceof Error)) return undefined; - const code = filesystemErrorCode(cause); - return code === undefined - ? undefined - : new BinaryTargetError( - path, - targetFailureReason(code, cause.message, stage), - { cause }, - ); -}; - -const filesystemErrorCode = (cause: Error): string | undefined => { - const code: unknown = Reflect.get(cause, "code"); - return typeof code === "string" ? code : undefined; -}; - -const targetFailureReason = ( - code: string, - detail: string, - stage: TargetFilesystemStage, -): string => { - switch (code) { - case "ENOENT": - case "ENOTDIR": - return `target does not exist or a path component is not a directory: ${detail}`; - case "EACCES": - case "EPERM": - return `permission denied while reading target: ${detail}`; - case "EISDIR": - return `target is a directory, not a readable file: ${detail}`; - case "ENOTFILE": - return "target is not a regular file"; - case "ELOOP": - // A canonical path has no symbolic links left, so a link rejected while - // reading it appeared after resolution. Before that, ELOOP is a loop or - // an over-deep link chain in the selected path itself. - return stage === "resolve" - ? `target path could not be resolved: too many symbolic links or a symbolic link loop: ${detail}` - : `target changed while being opened: ${detail}`; - default: - return `target could not be read (${code}): ${detail}`; - } -}; - const throwIfTargetResolutionCancelled = (signal?: AbortSignal): void => { if (signal?.aborted === true) throw cancelledBy("resolve_binary_target", signal); diff --git a/src/application/DirectAnalysis.ts b/src/application/DirectAnalysis.ts index 24e92c2968..f015b4f920 100644 --- a/src/application/DirectAnalysis.ts +++ b/src/application/DirectAnalysis.ts @@ -56,7 +56,6 @@ import { requestsFreshIndependentDecompile, requiresNativeSourceAdmissionBeforeReplay, } from "./binary/AnalysisSnapshotCache.js"; -import { classifyBinaryTargetFilesystemFailure } from "./BinaryTargetResolver.js"; import { snapshotBindingMismatch, snapshotEvidenceForQuery, @@ -94,6 +93,7 @@ import { } from "./DirectAnalysisCleanup.js"; import { completeProviderEvidence } from "./ProviderOperationExecution.js"; import { BUNDLE_RESOURCE_OPERATIONS } from "../ports/BundleResourceOperations.js"; +import { classifyBinaryTargetFilesystemFailure } from "../domain/binaryTargetFilesystemFailure.js"; // These one-shot CLI questions require native utilities, not a deep engine. // Runtime observation retains the ordinary session and authority checks. diff --git a/src/domain/binaryTargetFilesystemFailure.ts b/src/domain/binaryTargetFilesystemFailure.ts new file mode 100644 index 0000000000..c7b152eaf1 --- /dev/null +++ b/src/domain/binaryTargetFilesystemFailure.ts @@ -0,0 +1,64 @@ +import { BinaryTargetError } from "./configurationErrors.js"; + +/** + * `resolve` covers the caller-selected path before canonicalization; `read` + * covers a canonical path opened with symbolic links rejected. + */ +export type TargetFilesystemStage = "resolve" | "read"; + +/** Classify a filesystem failure while reading a previously selected target. */ +export const classifyBinaryTargetFilesystemFailure = ( + path: string, + cause: unknown, +): BinaryTargetError | undefined => + classifyTargetFilesystemFailure(path, cause, "read"); + +/** Project a filesystem failure at `stage` as the target's admission error. */ +export const classifyTargetFilesystemFailure = ( + path: string, + cause: unknown, + stage: TargetFilesystemStage, +): BinaryTargetError | undefined => { + if (!(cause instanceof Error)) return undefined; + const code = filesystemErrorCode(cause); + return code === undefined + ? undefined + : new BinaryTargetError( + path, + targetFailureReason(code, cause.message, stage), + { cause }, + ); +}; + +const filesystemErrorCode = (cause: Error): string | undefined => { + const code: unknown = Reflect.get(cause, "code"); + return typeof code === "string" ? code : undefined; +}; + +const targetFailureReason = ( + code: string, + detail: string, + stage: TargetFilesystemStage, +): string => { + switch (code) { + case "ENOENT": + case "ENOTDIR": + return `target does not exist or a path component is not a directory: ${detail}`; + case "EACCES": + case "EPERM": + return `permission denied while reading target: ${detail}`; + case "EISDIR": + return `target is a directory, not a readable file: ${detail}`; + case "ENOTFILE": + return "target is not a regular file"; + case "ELOOP": + // A canonical path has no symbolic links left, so a link rejected while + // reading it appeared after resolution. Before that, ELOOP is a loop or + // an over-deep link chain in the selected path itself. + return stage === "resolve" + ? `target path could not be resolved: too many symbolic links or a symbolic link loop: ${detail}` + : `target changed while being opened: ${detail}`; + default: + return `target could not be read (${code}): ${detail}`; + } +}; diff --git a/src/dotnet/ManagedSnapshot.ts b/src/dotnet/ManagedSnapshot.ts index 91746da420..8c8a7b7d1c 100644 --- a/src/dotnet/ManagedSnapshot.ts +++ b/src/dotnet/ManagedSnapshot.ts @@ -4,7 +4,6 @@ import type { Stats } from "node:fs"; import type { FileHandle } from "node:fs/promises"; import { MANAGED_STATIC_PROVIDER } from "../domain/investigationProviders.js"; -import { classifyBinaryTargetFilesystemFailure } from "../application/BinaryTargetResolver.js"; import { RegularFileCleanupFailure, regularFileCancellation, @@ -28,6 +27,7 @@ import { DEFAULT_MANAGED_SNAPSHOT_BYTES } from "../domain/managed/managedInspect import { ProviderCleanupError } from "../domain/providerCleanupError.js"; import { RegularFileChangedError } from "../filesystem/RegularFile.js"; import { readBoundedFileBytes } from "../process/BoundedFileBytes.js"; +import { classifyBinaryTargetFilesystemFailure } from "../domain/binaryTargetFilesystemFailure.js"; interface ManagedSourceObservation { readonly byteLength: number; diff --git a/tests/process-global/directAnalysisStatFailure.test.ts b/tests/process-global/directAnalysisStatFailure.test.ts index 11c4dac63c..fc9731d5f9 100644 --- a/tests/process-global/directAnalysisStatFailure.test.ts +++ b/tests/process-global/directAnalysisStatFailure.test.ts @@ -16,10 +16,10 @@ vi.mock("node:fs/promises", async (importOriginal) => { }); import { parseConfig } from "../../src/config/parseConfig.js"; -import { classifyBinaryTargetFilesystemFailure } from "../../src/application/BinaryTargetResolver.js"; import type { BinarySession } from "../../src/application/binary/BinarySession.js"; import type { DirectAnalysisDependencies } from "../../src/application/DirectAnalysisDependencies.js"; import { runProviderAnalysis } from "../../src/application/DirectAnalysis.js"; +import { classifyBinaryTargetFilesystemFailure } from "../../src/domain/binaryTargetFilesystemFailure.js"; afterEach(() => statFailures.clear());