mirror of
https://github.com/morluto/rea.git
synced 2026-12-03 09:34:20 +00:00
* fix(plist): rethrow internal invariant violations from plist consumers KeyedArchiveReader reported any non-limit decode failure as a format failure, InterfaceBuilderAnalysis listed it as an invalid archive, ReleaseExposureReader fell back to scanning raw bytes, and the native plist parser reported it as provider output. An InternalInvariantError is a REA defect, not evidence about the input, so each consumer now rethrows it. The native parser's binary path now shares its one cause projection instead of a duplicate. * fix(native): rethrow internal invariant violations from the native output projection nativeOutputFailure wrapped any producer failure as AnalysisOutputError, so a REA defect thrown while reading a plist or dispatching a command was reported as native output parsing. It now rethrows InternalInvariantError, which reaches the provider's adapter-failure path with the invariant as its cause. * fix(artifacts): rethrow internal invariant violations from the shared artifact failure mapper translateArtifactFailure turned every unrecognised cause into an ArtifactOperationError with reason io, so the keyed-archive and Interface Builder rethrows still reached users as artifact read failures. The shared mapper now rethrows InternalInvariantError, and the deferred client reports it as a ProviderAdapterError. Provider-level tests drive both operations through that client. * refactor(dotnet): report managed reader defects through invariantViolation (#11030) * refactor(dotnet): report managed reader defects through invariantViolation The managed readers threw plain TypeError and RangeError for states their callers rule out: an allowance or method page outside the input schema, a missing snapshot after identity admission, and a missing CLI header after the caller already returned for it. These are REA defects, not malformed targets, so they throw invariantViolation. The two CLI re-checks are gone: the callers pass the CLI header they already narrowed. The dotnet parser-plain-throw baseline drops from 6 to 0. * refactor(providers): refuse unusable tool paths and record marker mismatches without throwing to their own catch (#11031) * refactor(firmware): refuse unusable tool paths without throwing to its own catch resolveFirmwareCommand threw a plain TypeError for a configured engine or limiter path that is not a regular file, only to catch it in the next line and wrap it as an unavailable capability. A helper now returns why a path cannot run, and each caller builds the one refusal directly. Refusal reasons are unchanged; the directory tests now pin them exactly. The firmware parser-plain-throw baseline drops from 2 to 0. * refactor(pwndbg): refuse unusable debugger paths without throwing to its own catch inspectPwndbgCore threw plain Errors for a relative or wrong-kind configured path only to catch them and wrap them as an unavailable capability. A helper now returns why a path cannot be used, with any host error as its cause, and the loop builds the one refusal. Cancellation between host checks still propagates. Refusal reasons are unchanged. The PwndbgCoreContext parser-plain-throw baseline entry is removed. * refactor(pwntools): record a mismatched failure marker without throwing to its own catch readPwntoolsFailureEvidence threw a plain Error when the private bridge marker disagreed with the exit status, only to catch it and record its message as the marker failure. The mismatch is now recorded directly. The recorded evidence is unchanged; a unit test pins both the matching and the mismatched marker. The PwntoolsResourceLimits parser-plain-throw baseline entry is removed. * fix(android): report an unobserved Java probe as a REA defect, not a Java refusal (#11032) * fix(android): report an unobserved Java probe as a REA defect, not a Java refusal When runOwnedCommand resolved without observing the completed run, the probe threw a plain Error that its catch then classified as a Java runtime refusal, telling the caller to review their launcher. The state is a broken internal assumption, so the probe throws invariantViolation and projects it as a jadx adapter failure. The JadxJavaProbe parser-plain-throw baseline entry is removed. * refactor(native): report native provider defects through invariantViolation (#11033) * refactor(native): report native provider defects through invariantViolation Native providers threw plain Error and TypeError for states their own earlier steps rule out: a client with no target, a measured slice with no selection, a read longer than requested, a decoder command without its runtime, an LLDB root released before its children, and an IPS integer fact from another reader. These are REA defects, so they throw invariantViolation. NativeUiInterruptionControl no longer needs a throw: recording the deadline interruption returns the interruption its error is built from. Six native parser-plain-throw baseline entries drop. * refactor(native): report malformed lipo, swift-demangle and UI helper output through ParseFailure (#11034) * refactor(native): report malformed lipo and swift-demangle output through ParseFailure The lipo and swift-demangle output parsers threw plain TypeError for output that breaks their framing, and the lipo result classifier caught every TypeError as malformed output, so a host TypeError from a REA defect was reported as lipo output. Both parsers now throw malformedInput with their own format, and the lipo classifier accepts ParseFailureError instead of TypeError. Messages and the consumers' output and format failure categories are unchanged. Four native parser-plain-throw baseline entries drop. * refactor(native): report the UI helper's non-JSON output through ParseFailure The native UI helper runtime threw a plain Error when the helper's stdout was not JSON. It now throws malformedInput with its own format and the JSON failure as cause. The observation boundary still reports it as a native-macos adapter failure with the same reason. The NativeUiHelperRuntime parser-plain-throw baseline entry is removed. * refactor(verify): keep process-lifecycle owners out of the parser plain-throw guard (#11035) * refactor(verify): keep process-lifecycle owners out of the parser plain-throw guard JadxWorkspaceRoot and PwntoolsDecoder sit in parser folders but own a private helper process; their remaining plain throws report the helper's startup, handshake and cleanup outcomes, not parse faults. The guard now names them as process-lifecycle owners and leaves them out, so their five baseline entries drop without inventing parse errors for process failures. * refactor(parsers): report the remaining artifact, il2cpp, model and domain defects through invariantViolation (#11036) * refactor(parsers): report the remaining artifact, il2cpp, model and domain defects through invariantViolation Nine plain Error, TypeError and RangeError throws in parser folders reported broken internal assumptions or caller contracts (an uninitialized ZIP or DMG reader, a missing scan result, a non-byte stream chunk, an invalid traversal or marshal limit, a header count or linking fact the reader itself should have produced, a misordered Safetensors budget, an append after fingerprinting). None is an input fault, and no catch site depends on their class. They now throw invariantViolation with the same messages, and their parser-plain-throw baseline entries drop. * refactor(evidence): refuse an earlier Evidence format through ParseFailure (#11037) * refactor(evidence): refuse an earlier Evidence format through ParseFailure UnsupportedEvidenceFormatError restated the ParseFailure algebra as its own TypeError subclass. The format owner now throws unsupportedInput for the evidence-bundle format at the evidence_format field and exports the predicate its consumers use. The bundle reader's unsupported-format kind and message are unchanged. The snapshot reader surfaced only TypeError messages, so it now also surfaces the format refusal and keeps its recreate guidance; a boundary test pins that. * refactor(native): report Mach-O reader and inspection defects through invariantViolation (#11042) The Mach-O inspection plan reads the lipo, nm and dyld_info captures it just ran, the dispatch metadata reader is called only for Mach-O executables, and the command-name reader takes limits its callers derive. A missing capture, a non-Mach-O target or a malformed limit is a REA defect, not an input fault, so these six sites throw invariantViolation with their existing messages instead of TypeError. No catch site depends on their class. Their parser-plain-throw baseline entries drop. * fix(artifacts): join the Interface Builder cleanup failure to a catalog defect When catalog work failed while the concurrent Interface Builder reader failed to close, analyzeAppleAssetCatalogs built the primary through translateArtifactFailure before joining cleanup. That mapper now rethrows an InternalInvariantError, so a defect skipped the join and the reader's retained resources were lost. The defect now becomes the artifact graph provider's adapter failure at that site and joins the cleanup failure as before.
558 lines
20 KiB
TypeScript
558 lines
20 KiB
TypeScript
import {
|
|
processInspectionCancellationError,
|
|
retainProcessInspectionCleanup,
|
|
} from "../providers/OwnedCommandCleanup.js";
|
|
import type { ArtifactResourceScope } from "../artifacts/ArtifactResourceScope.js";
|
|
import {
|
|
selectArtifactCommandPath,
|
|
type ArtifactCommandPath,
|
|
} from "../process/ArtifactCommandPath.js";
|
|
import { ArtifactReaderFailure } from "../artifacts/ArtifactReader.js";
|
|
import {
|
|
FIRMWARE_TOOL_REQUIREMENTS,
|
|
type FirmwareEngineName,
|
|
} from "../contracts/firmware/firmwareToolRequirements.js";
|
|
import { randomUUID } from "node:crypto";
|
|
import { access, stat } from "node:fs/promises";
|
|
import { constants } from "node:fs";
|
|
import { isAbsolute } from "node:path";
|
|
import {
|
|
AnalysisCapabilityUnavailableError,
|
|
AnalysisOutputError,
|
|
AnalysisTimeoutError,
|
|
} from "../domain/analysisErrorCore.js";
|
|
import { classifyProviderFailure } from "../providers/classifyProviderFailure.js";
|
|
import { ProviderAdapterError } from "../domain/providerAdapterError.js";
|
|
import { ProviderCleanupError } from "../domain/providerCleanupError.js";
|
|
import type {
|
|
AnalysisError,
|
|
AnalysisCapturedOutput,
|
|
} from "../domain/analysisErrorBase.js";
|
|
import { projectAnalysisErrorWithoutCapturedOutput } from "../domain/analysisErrorProjection.js";
|
|
import type { FirmwareDeadline } from "./FirmwareDeadline.js";
|
|
import {
|
|
firmwareFailureDialect,
|
|
firmwareLimitRecovery,
|
|
firmwarePhaseInterruption,
|
|
firmwareProcessLimitRecovery,
|
|
firmwareSignalLimitError,
|
|
} from "./FirmwareFailures.js";
|
|
import { prlimitArgumentVector } from "../process/LinuxResourceLimits.js";
|
|
import { cleanupOwnedProcessGroup } from "../process/ProcessOwnership.js";
|
|
import {
|
|
spawnOwnedProviderProcess,
|
|
ProviderProcessSupervisor,
|
|
type OwnedProviderProcessSpawnOptions,
|
|
type ProviderProcessSnapshot,
|
|
type SpawnedOwnedProviderProcess,
|
|
} from "../process/ProviderProcess.js";
|
|
import { FIRMWARE_LIMITS } from "./FirmwareRelease.js";
|
|
import {
|
|
FirmwareFileAccess,
|
|
inventoryFirmwareOutput,
|
|
} from "./FirmwareFiles.js";
|
|
|
|
/** Injectable owned launcher for production protocol and lifecycle tests. */
|
|
export type FirmwareLauncher = (
|
|
options: OwnedProviderProcessSpawnOptions,
|
|
) => Promise<SpawnedOwnedProviderProcess>;
|
|
|
|
/** Captured tool output for one finished firmware command, including byte coverage. */
|
|
const capturedFirmwareOutput = (
|
|
snapshot: ProviderProcessSnapshot,
|
|
): AnalysisCapturedOutput => ({
|
|
stdout: snapshot.stdout.text,
|
|
stderr: snapshot.stderr.text,
|
|
stdout_bytes: snapshot.stdout.bytes,
|
|
stderr_bytes: snapshot.stderr.bytes,
|
|
exit_code: snapshot.exitCode ?? null,
|
|
signal: snapshot.signal ?? null,
|
|
truncated: snapshot.diagnosticTruncated === true,
|
|
});
|
|
|
|
/** Refuse an unmet host or tool requirement, which another target cannot satisfy. */
|
|
export const firmwareToolUnavailable = (
|
|
engine: string,
|
|
operation: string,
|
|
reason: string,
|
|
): AnalysisCapabilityUnavailableError =>
|
|
new AnalysisCapabilityUnavailableError(engine, operation, reason, {
|
|
userMessage: reason,
|
|
});
|
|
|
|
/** Firmware engines run only on Linux; discovery and execution share this check. */
|
|
export const firmwareHostSupported = (platform: NodeJS.Platform): boolean =>
|
|
platform === "linux";
|
|
|
|
const failureReason = (cause: unknown): string =>
|
|
cause instanceof Error ? cause.message : String(cause);
|
|
|
|
/**
|
|
* Why a configured tool path cannot run, or undefined when `accessPath` grants
|
|
* `mode` and `statPath` is a regular file.
|
|
*/
|
|
const executableRegularFileRefusal = async (
|
|
accessPath: string,
|
|
statPath: string,
|
|
mode: number,
|
|
): Promise<string | undefined> => {
|
|
try {
|
|
await access(accessPath, mode);
|
|
return (await stat(statPath)).isFile()
|
|
? undefined
|
|
: "Configured path is not a regular file";
|
|
} catch (cause: unknown) {
|
|
return failureReason(cause);
|
|
}
|
|
};
|
|
|
|
/** Resolve explicitly supplied executable paths; never install or search for engines. */
|
|
export const resolveFirmwareCommand = async (
|
|
environment: Readonly<Record<string, string | undefined>>,
|
|
engine: FirmwareEngineName,
|
|
operation: string,
|
|
files: FirmwareFileAccess,
|
|
signal?: AbortSignal,
|
|
) => {
|
|
const variable = FIRMWARE_TOOL_REQUIREMENTS[engine].commandEnvironment;
|
|
const configured = environment[variable];
|
|
if (
|
|
!firmwareHostSupported(process.platform) ||
|
|
configured === undefined ||
|
|
!isAbsolute(configured)
|
|
)
|
|
throw firmwareToolUnavailable(
|
|
engine,
|
|
operation,
|
|
`This integration requires Linux and an absolute ${variable} path to the caller-supplied tool`,
|
|
);
|
|
const selection = await selectArtifactCommandPath(configured, signal).catch(
|
|
(cause: unknown) => {
|
|
if (cause instanceof ArtifactReaderFailure) throw cause;
|
|
throw firmwareToolUnavailable(
|
|
engine,
|
|
operation,
|
|
`${variable} is unavailable: ${failureReason(cause)}`,
|
|
);
|
|
},
|
|
);
|
|
const command = selection.invocationPath;
|
|
const engineUnavailable = (reason: string) =>
|
|
firmwareToolUnavailable(
|
|
engine,
|
|
operation,
|
|
`${variable} is not a readable executable regular file: ${command}: ${reason}`,
|
|
);
|
|
const engineRefusal = await executableRegularFileRefusal(
|
|
command,
|
|
command,
|
|
constants.R_OK | constants.X_OK,
|
|
);
|
|
if (engineRefusal !== undefined) throw engineUnavailable(engineRefusal);
|
|
let sha256: string;
|
|
try {
|
|
sha256 = await files.hash(selection.resolvedPath, signal);
|
|
} catch (cause: unknown) {
|
|
if (cause instanceof ArtifactReaderFailure) throw cause;
|
|
throw engineUnavailable(failureReason(cause));
|
|
}
|
|
const limiter =
|
|
environment.REA_FIRMWARE_PRLIMIT_COMMAND ?? "/usr/bin/prlimit";
|
|
if (!isAbsolute(limiter))
|
|
throw firmwareToolUnavailable(
|
|
engine,
|
|
operation,
|
|
"REA_FIRMWARE_PRLIMIT_COMMAND must be absolute",
|
|
);
|
|
const limiterUnavailable = (reason: string) =>
|
|
firmwareToolUnavailable(
|
|
engine,
|
|
operation,
|
|
`Resource limiter is not an executable regular file: ${limiter}: ${reason}; provide util-linux prlimit`,
|
|
);
|
|
let limiterSelection: ArtifactCommandPath;
|
|
try {
|
|
limiterSelection = await selectArtifactCommandPath(limiter, signal);
|
|
} catch (cause: unknown) {
|
|
if (cause instanceof ArtifactReaderFailure) throw cause;
|
|
throw limiterUnavailable(failureReason(cause));
|
|
}
|
|
const limiterRefusal = await executableRegularFileRefusal(
|
|
limiterSelection.resolvedPath,
|
|
limiter,
|
|
constants.X_OK,
|
|
);
|
|
if (limiterRefusal !== undefined) throw limiterUnavailable(limiterRefusal);
|
|
return {
|
|
command: selection,
|
|
limiter: limiterSelection,
|
|
sha256,
|
|
fileSizeCeiling: await files.fileSizeCeiling(signal),
|
|
};
|
|
};
|
|
|
|
/**
|
|
* The RLIMIT_FSIZE one producer run gets. It covers every producer file,
|
|
* including mandatory reports and logs; outputBudget inventory still governs
|
|
* aggregate payload. One byte above the largest budget makes a file the kernel
|
|
* stops at the limit exceed that budget instead of passing as complete. An
|
|
* inherited ceiling can remove that byte, so inventory also rejects files
|
|
* that reach the returned limit.
|
|
*/
|
|
export const firmwareFileSizeLimit = (
|
|
fileSizeCeiling: number,
|
|
stagingBytes?: number,
|
|
): number =>
|
|
Math.min(
|
|
fileSizeCeiling,
|
|
Math.max(
|
|
stagingBytes ?? 0,
|
|
FIRMWARE_LIMITS.reportBytes,
|
|
FIRMWARE_LIMITS.diagnosticBytes,
|
|
) + 1,
|
|
);
|
|
|
|
/**
|
|
* Whether a larger max_output_bytes would raise the per-run file limit. It is
|
|
* false when the report and diagnostic allowances or the inherited ceiling,
|
|
* including its last byte of headroom, set that limit instead.
|
|
*/
|
|
const firmwareFileSizeAdjustable = (
|
|
fileSizeCeiling: number,
|
|
stagingBytes?: number,
|
|
): boolean =>
|
|
stagingBytes !== undefined &&
|
|
firmwareFileSizeLimit(fileSizeCeiling, stagingBytes + 1) >
|
|
firmwareFileSizeLimit(fileSizeCeiling, stagingBytes);
|
|
|
|
/** Execute one bounded provider command and verify its owned descendants have stopped. */
|
|
export const runFirmwareCommand = async (context: {
|
|
engine: string;
|
|
operation: string;
|
|
command: string;
|
|
limiter: string;
|
|
/** Observed inherited soft/hard file policy; never raised by metadata headroom. */
|
|
fileSizeCeiling: number;
|
|
args: readonly string[];
|
|
cwd: string;
|
|
environment: Readonly<Record<string, string | undefined>>;
|
|
signal: AbortSignal;
|
|
deadline: FirmwareDeadline;
|
|
launcher?: FirmwareLauncher;
|
|
resources: ArtifactResourceScope;
|
|
outputBudget?: { root: string; bytes: number; entries: number };
|
|
acceptReportedExtractionFailure?: boolean;
|
|
retainCleanup(supervisor: ProviderProcessSupervisor): void;
|
|
}) => {
|
|
const { engine, operation, signal } = context;
|
|
const dialect = firmwareFailureDialect({
|
|
engine,
|
|
operation,
|
|
phase: { signal, deadline: context.deadline },
|
|
});
|
|
const fileSizeBytes = firmwareFileSizeLimit(
|
|
context.fileSizeCeiling,
|
|
context.outputBudget?.bytes,
|
|
);
|
|
const fileSizeAdjustable = firmwareFileSizeAdjustable(
|
|
context.fileSizeCeiling,
|
|
context.outputBudget?.bytes,
|
|
);
|
|
const checkInterruption = () => {
|
|
const failure = firmwarePhaseInterruption(
|
|
operation,
|
|
signal,
|
|
context.deadline,
|
|
);
|
|
if (failure !== undefined) throw failure;
|
|
};
|
|
checkInterruption();
|
|
const runId = `rea-firmware-${randomUUID()}`;
|
|
const spawned = await (context.launcher ?? spawnOwnedProviderProcess)({
|
|
command: context.limiter,
|
|
arguments: [
|
|
...prlimitArgumentVector(
|
|
{
|
|
address_space_bytes: FIRMWARE_LIMITS.addressSpaceBytes,
|
|
file_bytes: fileSizeBytes,
|
|
cpu_seconds: FIRMWARE_LIMITS.cpuSeconds,
|
|
},
|
|
context.command,
|
|
),
|
|
...context.args,
|
|
],
|
|
expectedCommand: null,
|
|
runId,
|
|
cwd: context.cwd,
|
|
hostEnvironment: context.environment,
|
|
signal,
|
|
env: {
|
|
TMPDIR: context.cwd,
|
|
OMP_NUM_THREADS: "1",
|
|
OPENBLAS_NUM_THREADS: "1",
|
|
RAYON_NUM_THREADS: "1",
|
|
NO_COLOR: "1",
|
|
},
|
|
}).catch((cause: unknown) => {
|
|
const cancellation = processInspectionCancellationError(cause, operation);
|
|
// The phase deadline reports its own expiry; any other inspection
|
|
// cancellation keeps its ownership-inspection origin.
|
|
const interruption =
|
|
cancellation === undefined
|
|
? undefined
|
|
: firmwarePhaseInterruption(operation, signal, context.deadline);
|
|
const primary =
|
|
cancellation === undefined
|
|
? classifyProviderFailure(cause, dialect)
|
|
: interruption instanceof AnalysisTimeoutError
|
|
? interruption
|
|
: cancellation;
|
|
throw retainProcessInspectionCleanup(
|
|
cause,
|
|
primary,
|
|
context.resources,
|
|
operation,
|
|
);
|
|
});
|
|
let failure: AnalysisError | undefined;
|
|
const isFailedExit = (code: number | null | undefined) =>
|
|
code !== undefined &&
|
|
code !== 0 &&
|
|
!(context.acceptReportedExtractionFailure === true && code === 1);
|
|
const completedFailure = (snapshot: ProviderProcessSnapshot) => {
|
|
if (isFailedExit(snapshot.exitCode)) {
|
|
const signalLimit = firmwareSignalLimitError(
|
|
operation,
|
|
snapshot.signal,
|
|
fileSizeBytes,
|
|
fileSizeAdjustable,
|
|
capturedFirmwareOutput(snapshot),
|
|
);
|
|
if (signalLimit !== undefined) return signalLimit;
|
|
return new ProviderAdapterError(engine, operation, {
|
|
diagnostics: {
|
|
reason:
|
|
"Tool failed; inspect exit status and stderr for format, dependency, sandbox or resource-limit failures",
|
|
exit_code: snapshot.exitCode ?? null,
|
|
signal: snapshot.signal ?? null,
|
|
stdout: snapshot.stdout.text,
|
|
stderr: snapshot.stderr.text,
|
|
stdout_bytes: snapshot.stdout.bytes,
|
|
stderr_bytes: snapshot.stderr.bytes,
|
|
truncated: snapshot.diagnosticTruncated === true,
|
|
},
|
|
remediationAction: `Inspect exit status and stderr for format, dependency, sandbox, or resource-limit failures. ${firmwareProcessLimitRecovery(fileSizeBytes, fileSizeAdjustable)} A format or dependency failure is not repaired by changing analysis_timeout_ms.`,
|
|
});
|
|
}
|
|
return undefined;
|
|
};
|
|
let completedFailureObserved = false;
|
|
let diagnosticTruncated = false;
|
|
const supervisor = new ProviderProcessSupervisor(
|
|
{
|
|
...spawned,
|
|
ownsProcessLifetime: true,
|
|
cleanup:
|
|
spawned.cleanup ?? (() => cleanupOwnedProcessGroup(spawned.ownership)),
|
|
},
|
|
{
|
|
maxDiagnosticBytes: FIRMWARE_LIMITS.diagnosticBytes,
|
|
onDiagnostic: (event) => {
|
|
if (event.type === "output" && event.truncated === true)
|
|
diagnosticTruncated = true;
|
|
// Root exit and inherited-pipe closure are different observations.
|
|
// Retain a completed failure before drain or cleanup can be interrupted,
|
|
// while cancellation already present at exit keeps its precedence.
|
|
if (
|
|
event.type === "exit" &&
|
|
failure === undefined &&
|
|
!diagnosticTruncated &&
|
|
firmwarePhaseInterruption(operation, signal, context.deadline) ===
|
|
undefined
|
|
)
|
|
completedFailureObserved = isFailedExit(event.code);
|
|
},
|
|
},
|
|
);
|
|
const checkDiagnostics = () => {
|
|
if (diagnosticTruncated) {
|
|
const snapshot = supervisor.snapshot();
|
|
throw new AnalysisOutputError(
|
|
operation,
|
|
`Tool diagnostics exceeded the ${FIRMWARE_LIMITS.diagnosticBytes}-byte capture budget`,
|
|
{
|
|
capturedOutput: {
|
|
stdout: snapshot.stdout.text,
|
|
stderr: snapshot.stderr.text,
|
|
truncated: true,
|
|
},
|
|
userMessage: `Firmware diagnostics exceeded the ${String(FIRMWARE_LIMITS.diagnosticBytes)}-byte capture allowance. This release has no runtime override.`,
|
|
remediationAction: firmwareLimitRecovery(
|
|
`The firmware diagnostic capture allowance is ${String(FIRMWARE_LIMITS.diagnosticBytes)} bytes`,
|
|
),
|
|
},
|
|
);
|
|
}
|
|
};
|
|
try {
|
|
while (!(await supervisor.waitForOutputClose(100))) {
|
|
checkDiagnostics();
|
|
checkInterruption();
|
|
if (context.outputBudget !== undefined)
|
|
await inventoryFirmwareOutput(
|
|
context.outputBudget.root,
|
|
context.outputBudget.bytes,
|
|
context.outputBudget.entries,
|
|
fileSizeBytes,
|
|
operation,
|
|
signal,
|
|
context.deadline,
|
|
);
|
|
}
|
|
checkDiagnostics();
|
|
checkInterruption();
|
|
} catch (cause: unknown) {
|
|
failure ??= classifyProviderFailure(cause, dialect);
|
|
}
|
|
const stopped = await supervisor.stop();
|
|
// Cleanup is an awaited part of this phase. Preserve a prior observed failure,
|
|
// otherwise retain an interruption that arrived while stopping owned resources.
|
|
failure ??= firmwarePhaseInterruption(operation, signal, context.deadline);
|
|
const snapshot = supervisor.snapshot();
|
|
// Capture diagnostics only after owned cleanup has drained the inherited pipes.
|
|
if (completedFailureObserved) failure = completedFailure(snapshot);
|
|
if (stopped.status === "incomplete") {
|
|
context.retainCleanup(supervisor);
|
|
const previous =
|
|
failure === undefined
|
|
? undefined
|
|
: projectAnalysisErrorWithoutCapturedOutput(failure);
|
|
// Retain typed recovery history, but give captured output exactly one owner.
|
|
throw new ProviderCleanupError(
|
|
engine,
|
|
[runId, context.cwd],
|
|
{
|
|
reason: stopped.reason,
|
|
leader_pid: spawned.ownership.leaderPid,
|
|
process_group_id: spawned.ownership.processGroupId,
|
|
previous_error: previous ?? null,
|
|
},
|
|
{
|
|
operation,
|
|
cleanupPrimary: failure,
|
|
capturedOutput: capturedFirmwareOutput(snapshot),
|
|
},
|
|
);
|
|
}
|
|
if (failure !== undefined) throw failure;
|
|
return {
|
|
stdout: snapshot.stdout,
|
|
stderr: snapshot.stderr,
|
|
exit_code: snapshot.exitCode,
|
|
signal: snapshot.signal ?? null,
|
|
command: context.command,
|
|
args: [...context.args],
|
|
resource_limiter: context.limiter,
|
|
};
|
|
};
|
|
|
|
/**
|
|
* Consume the report a completed command was asked to write. A successful exit
|
|
* status is not analysis Evidence by itself: an absent, empty or malformed report
|
|
* fails with the observed report state and command facts, without retrying or
|
|
* substituting an empty result.
|
|
*/
|
|
export const readFirmwareCommandReport = async (context: {
|
|
path: string;
|
|
engine: string;
|
|
operation: string;
|
|
execution: Awaited<ReturnType<typeof runFirmwareCommand>>;
|
|
files: FirmwareFileAccess;
|
|
signal?: AbortSignal;
|
|
deadline?: FirmwareDeadline;
|
|
}): Promise<unknown> => {
|
|
try {
|
|
return await context.files.report(
|
|
context.path,
|
|
context.operation,
|
|
context.signal,
|
|
);
|
|
} catch (cause: unknown) {
|
|
if (cause instanceof ArtifactReaderFailure) {
|
|
const { engine, operation, execution, deadline } = context;
|
|
const signal = context.signal ?? deadline?.signal;
|
|
const failure = classifyProviderFailure(
|
|
cause,
|
|
firmwareFailureDialect({
|
|
engine,
|
|
operation,
|
|
phase:
|
|
deadline === undefined || signal === undefined
|
|
? undefined
|
|
: { signal, deadline },
|
|
}),
|
|
);
|
|
if (cause.partialObservation !== undefined)
|
|
failure.retainPartialObservation(cause.partialObservation);
|
|
throw failure.retainCapturedOutput({
|
|
stdout: execution.stdout.text,
|
|
stderr: execution.stderr.text,
|
|
stdout_bytes: execution.stdout.bytes,
|
|
stderr_bytes: execution.stderr.bytes,
|
|
exit_code: execution.exit_code ?? null,
|
|
signal: execution.signal,
|
|
truncated: false,
|
|
});
|
|
}
|
|
const { engine, operation, execution } = context;
|
|
const reportFailure =
|
|
cause instanceof AnalysisOutputError
|
|
? cause.reason
|
|
: `Report could not be read: ${cause instanceof Error ? cause.message : String(cause)}`;
|
|
const diagnostics = {
|
|
...(cause instanceof AnalysisOutputError ? cause.diagnostics : {}),
|
|
report_path: context.path,
|
|
report_failure: reportFailure,
|
|
command: execution.command,
|
|
args: execution.args,
|
|
resource_limiter: execution.resource_limiter,
|
|
exit_code: execution.exit_code ?? null,
|
|
signal: execution.signal,
|
|
stdout: execution.stdout.text,
|
|
stderr: execution.stderr.text,
|
|
stdout_bytes: execution.stdout.bytes,
|
|
stderr_bytes: execution.stderr.bytes,
|
|
};
|
|
if (execution.exit_code === 1)
|
|
throw new ProviderAdapterError(engine, operation, {
|
|
cause,
|
|
diagnostics: {
|
|
reason:
|
|
"Tool exited 1 without a usable analysis report; inspect stderr for startup, sandbox or dependency failures",
|
|
...diagnostics,
|
|
},
|
|
});
|
|
const oversized =
|
|
cause instanceof AnalysisOutputError &&
|
|
cause.diagnostics?.report_state === "oversized";
|
|
const reportFacts =
|
|
"Review diagnostics.report_state, the exit status and captured stdout/stderr. REA did not retry or substitute an empty result. diagnostics.command and diagnostics.args record what ran; their workspace paths are REA-private and removed after the attempt, so they cannot be replayed as-is.";
|
|
const reportAllowance = firmwareLimitRecovery(
|
|
`The firmware report allowance is ${String(FIRMWARE_LIMITS.reportBytes)} bytes`,
|
|
);
|
|
const observed = `${engine} exited successfully but left no usable report (${reportFailure}), so ${operation} produced no Evidence. A successful exit status is not an empty or complete result.`;
|
|
throw new AnalysisOutputError(
|
|
operation,
|
|
`${engine} exited ${String(execution.exit_code)} without a usable report: ${reportFailure}`,
|
|
{
|
|
cause,
|
|
diagnostics,
|
|
userMessage: oversized ? `${observed} ${reportAllowance}` : observed,
|
|
remediationAction: oversized
|
|
? `${reportFacts} ${reportAllowance} \`rea doctor\` checks configuration only and cannot recover this report.`
|
|
: `${reportFacts} Rerun the same REA request to check whether this is intermittent; each attempt uses a fresh workspace. If it recurs, copy the selected input bytes into a new directory and run diagnostics.command with the same flags, replacing every private path with your copy and a new report path, under the documented firmware resource limits; confirm that it writes the requested report. \`rea doctor\` checks configuration only and cannot recover this report.`,
|
|
},
|
|
);
|
|
}
|
|
};
|