Files
reverse-engineering-anything/src/providers/artifactResourceFailure.ts
T
morluto 5f718ce30e refactor(kernel): give error-message, byte-hex and SHA-256 hex helpers one owner in a new src/kernel layer (#11542)
* refactor(kernel): give error-to-message text one owner in a new src/kernel layer

* fix(kernel): keep the regex worker's own error text inside its isolated source

* refactor(kernel): exclude copies that run in another realm from the error-message guard by name

The regex worker's source string and the page.evaluate fetch in PlaywrightScenarioRequest cannot import errorMessage. The guard now names them, so the counted baseline does not invite a later slice to rewrite them.

* refactor(kernel): migrate the long tail of inline error-message copies to errorMessage (#11552)

Every provider and adapter folder with six or fewer copies now calls errorMessage from src/kernel. Local helpers whose whole body was the copy (failureMessage, gdbErrorMessage, message, describe, reasonOf, recoveryFailureMessage and four local errorMessage definitions) are removed and their call sites call the owner.

Refs #11548

* refactor(kernel): give byte-to-hex formatting one owner in src/kernel (#11560)

byteToHex returns the two lowercase hexadecimal digits of one byte. All eight inline copies (native, contracts, domain firmware and desktop, pcap) call it, keeping their own 0x, % or \x notation, and a counted guard with an empty baseline keeps new copies out.

Refs #11548

* refactor(kernel): give one-shot SHA-256 hex digests one owner in src/kernel (#11562)

sha256Hex digests one in-memory value. A counted guard records the one-shot createHash("sha256").update(x).digest("hex") chains (115 in 101 files) and the first slice migrates domain, contracts and process (35 copies), removing local wrappers (sha256, sha256Text, hash) and the exported sha256Text. Incremental and streamed hashing is not counted.

Refs #11548

* refactor(kernel): migrate the application layer's inline error-message copies to errorMessage (#11563)

All 69 copies in src/application call errorMessage from src/kernel. Local helpers whose whole body was the copy (errorDetail, causeReason and five local errorMessage definitions) are removed and their call sites call the owner.

Refs #11548

* refactor(kernel): migrate the native provider's inline error-message copies to errorMessage (#11567)

All 49 copies in src/native call errorMessage from src/kernel. The local errorReason helpers in LldbCallTracer and LldbRetainedObservations, whose whole body was the copy, are removed and their call sites call the owner.

Refs #11548

* refactor(kernel): migrate the remaining inline error-message copies to errorMessage (#11569)
2026-11-16 07:05:30 +08:00

141 lines
4.3 KiB
TypeScript

import {
ArtifactReaderFailure,
type readStableArtifact,
stableArtifactInputRecovery,
} from "../resources/index.js";
import { AnalysisInputError } from "../domain/analysisErrorCore.js";
import type { JsonValue } from "../domain/jsonValue.js";
import { ProviderAdapterError } from "../domain/providerAdapterError.js";
import {
ArtifactAnalysisResources,
type ArtifactResourceFailureContext,
} from "../process/ArtifactAnalysisResources.js";
import type { readLinuxResourceLimits } from "../process/LinuxResourceLimits.js";
import {
classifyProviderFailure,
type ProviderFailureDialect,
} from "./classifyProviderFailure.js";
import { errorMessage } from "../kernel/errorMessage.js";
/**
* The dialect for a failure the artifact resources observed: a selected input
* read, a post-run identity read, or the resources' own descriptors. Each is
* built from the typed pieces those resources own.
*/
export const artifactResourceDialect = (
resources: ArtifactAnalysisResources,
context: ArtifactResourceFailureContext,
): ProviderFailureDialect => {
const { operation } = context;
const { providerId } = resources;
const observedOutput =
context.kind === "selected-input"
? context.input.observedOutput
: context.kind === "integrity"
? context.capturedOutput
: undefined;
const captured =
observedOutput === undefined ? {} : { capturedOutput: observedOutput };
const shared = {
providerId,
operation,
readCleanup: (
read: Parameters<ArtifactAnalysisResources["readCleanupFailure"]>[0],
primary: Parameters<ArtifactAnalysisResources["readCleanupFailure"]>[1],
) => resources.readCleanupFailure(read, primary, observedOutput, operation),
observedOutput,
};
switch (context.kind) {
case "selected-input": {
const { input } = context;
return {
...shared,
file: {
path: input.path,
description: "artifact",
fileSize: resources.selectedFileSize(input, operation),
retainCompletedIdentity: true,
},
unclassified: (cause, details) =>
new AnalysisInputError(
operation,
{
...details,
cause,
...(cause instanceof ArtifactReaderFailure
? stableArtifactInputRecovery(cause)
: {}),
},
[
{
path: ["path"],
reason: "invalid_value",
message: errorMessage(cause),
},
],
),
};
}
case "integrity":
return {
...shared,
recognise: (cause, details) =>
cause instanceof ArtifactReaderFailure
? resources.integrityChanged(
cause,
context.path,
{ ...details, ...captured, cause },
operation,
)
: undefined,
unclassified: (cause, details) =>
new ProviderAdapterError(providerId, operation, {
...details,
...captured,
cause,
diagnostics: {
stage: "integrity",
resource_path: context.path,
reason: errorMessage(cause),
},
}),
};
case "resources":
return {
...shared,
unclassified: (cause, details) =>
new ProviderAdapterError(providerId, operation, {
...details,
cause,
diagnostics: { stage: "artifact-resources", reason: String(cause) },
}),
};
}
};
/** Artifact resources whose observed failures classify through the shared owner. */
export const createArtifactAnalysisResources = (
providerId: string,
operation: string,
readArtifact: typeof readStableArtifact,
readKernelLimits: typeof readLinuxResourceLimits,
capacity: {
readonly message: string;
readonly details: Readonly<Record<string, JsonValue>>;
},
): ArtifactAnalysisResources => {
const resources: ArtifactAnalysisResources = new ArtifactAnalysisResources(
providerId,
operation,
readArtifact,
readKernelLimits,
capacity,
(cause, context) =>
classifyProviderFailure(
cause,
artifactResourceDialect(resources, context),
),
);
return resources;
};