mirror of
https://github.com/morluto/rea.git
synced 2026-11-22 14:35:45 +00:00
* 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)
219 lines
7.7 KiB
TypeScript
219 lines
7.7 KiB
TypeScript
import { capturedProviderOutput } from "../process/capturedProviderOutput.js";
|
|
import { randomUUID } from "node:crypto";
|
|
import { rm } from "node:fs/promises";
|
|
|
|
import {
|
|
type ArtifactResourceScope,
|
|
AnalysisCleanupLease,
|
|
} from "../resources/index.js";
|
|
import { AnalysisError } from "../domain/analysisErrorBase.js";
|
|
import { analysisErrorWithCancellation } from "../domain/analysisErrorCancellation.js";
|
|
import {
|
|
retainProcessInspectionCleanup,
|
|
processInspectionCancellationError,
|
|
} from "../providers/OwnedCommandCleanup.js";
|
|
import {
|
|
OwnedCommandFailure,
|
|
runOwnedCommand,
|
|
} from "../process/OwnedCommand.js";
|
|
import { DarwinProcessOwnershipInspectionError } from "../process/DarwinProcessRunTokenReader.js";
|
|
import {
|
|
AnalysisOutputError,
|
|
cancelledBy,
|
|
carriedReason,
|
|
} from "../domain/analysisErrorCore.js";
|
|
import { errorMessage } from "../kernel/errorMessage.js";
|
|
|
|
/** Run a setup command with ownership of its entire process family and partial output. */
|
|
export const runDesktopBuildOwnedCommand = async (input: {
|
|
readonly command: string;
|
|
readonly arguments: readonly string[];
|
|
readonly environment: Readonly<NodeJS.ProcessEnv>;
|
|
readonly timeoutMs: number;
|
|
readonly maxOutputBytes: number;
|
|
readonly resources: ArtifactResourceScope;
|
|
readonly partialPath?: string;
|
|
readonly workspace?: string;
|
|
readonly signal?: AbortSignal;
|
|
}): Promise<void> => {
|
|
let retained = false;
|
|
let cleanupReason: string | undefined;
|
|
const paths = [input.partialPath, input.workspace].filter(
|
|
(path): path is string => path !== undefined,
|
|
);
|
|
const remainingPaths = new Set(paths);
|
|
const removeOwnedPaths = async () => {
|
|
const attempted = [...remainingPaths];
|
|
const outcomes = await Promise.allSettled(
|
|
attempted.map((path) =>
|
|
rm(path, { recursive: path === input.workspace, force: true }),
|
|
),
|
|
);
|
|
outcomes.forEach((outcome, index) => {
|
|
if (outcome.status === "fulfilled")
|
|
remainingPaths.delete(attempted[index]!);
|
|
});
|
|
const failures = outcomes.flatMap((outcome) =>
|
|
outcome.status === "rejected" ? [outcome.reason] : [],
|
|
);
|
|
if (failures.length > 0)
|
|
throw new AggregateError(
|
|
failures,
|
|
"Could not remove setup command outputs",
|
|
);
|
|
};
|
|
try {
|
|
await runOwnedCommand(
|
|
{
|
|
command: input.command,
|
|
arguments: input.arguments,
|
|
runId: randomUUID(),
|
|
expectedCommand: null,
|
|
hostEnvironment: input.environment,
|
|
sweepTokenOwnedProcesses: true,
|
|
},
|
|
{ timeoutMs: input.timeoutMs, diagnosticBytes: input.maxOutputBytes },
|
|
{
|
|
signal: input.signal,
|
|
retainCleanup: (owner, result) => {
|
|
retained = true;
|
|
cleanupReason = result.reason;
|
|
const process = `setup process ${owner.launch.ownership?.runId ?? "unknown"}`;
|
|
let processPending = true;
|
|
input.resources.retain({
|
|
kind: "lease",
|
|
resource:
|
|
input.partialPath ??
|
|
`setup process ${owner.launch.ownership?.runId ?? "unknown"}`,
|
|
lease: new AnalysisCleanupLease(
|
|
async () => {
|
|
if (processPending) {
|
|
const stopped = await owner.stop();
|
|
if (stopped.status === "incomplete")
|
|
throw new Error(stopped.reason);
|
|
processPending = false;
|
|
}
|
|
await removeOwnedPaths();
|
|
},
|
|
{
|
|
get cleanupResources() {
|
|
return [
|
|
...remainingPaths,
|
|
...(processPending ? [process] : []),
|
|
];
|
|
},
|
|
},
|
|
),
|
|
});
|
|
},
|
|
},
|
|
);
|
|
if (retained)
|
|
throw new Error(
|
|
`Setup command cleanup incomplete: ${cleanupReason ?? "unknown reason"}`,
|
|
);
|
|
} catch (cause: unknown) {
|
|
let failure: unknown = cause;
|
|
if (!retained && paths.length > 0) {
|
|
try {
|
|
await removeOwnedPaths();
|
|
} catch (cleanup: unknown) {
|
|
input.resources.retain({
|
|
kind: "lease",
|
|
resource: paths[0]!,
|
|
lease: new AnalysisCleanupLease(removeOwnedPaths, {
|
|
get cleanupResources() {
|
|
return [...remainingPaths];
|
|
},
|
|
}),
|
|
});
|
|
failure = new AggregateError(
|
|
[cause, cleanup],
|
|
"Setup command failed and its partial output could not be removed",
|
|
);
|
|
}
|
|
}
|
|
if (retained)
|
|
failure = new Error(
|
|
`${desktopBuildOwnedCommandReason(cause)}; cleanup is incomplete: ${cleanupReason ?? "unknown reason"}`,
|
|
{ cause },
|
|
);
|
|
const command = cause instanceof OwnedCommandFailure ? cause : undefined;
|
|
const preparation = command?.preparationFailure ?? cause;
|
|
const output =
|
|
command?.snapshot == null
|
|
? preparation instanceof DarwinProcessOwnershipInspectionError
|
|
? preparation.capturedOutput
|
|
: undefined
|
|
: capturedProviderOutput(command.snapshot);
|
|
const reason = desktopBuildOwnedCommandReason(failure);
|
|
const physical =
|
|
failure instanceof AnalysisError
|
|
? failure
|
|
: new AnalysisOutputError("setup", reason, {
|
|
cause: failure,
|
|
...(command === undefined
|
|
? {}
|
|
: { diagnostics: { command_failure: command.reason } }),
|
|
...(output === undefined ? {} : { capturedOutput: output }),
|
|
});
|
|
const inspectionCancellation =
|
|
command === undefined || command.preparationFailure !== undefined
|
|
? processInspectionCancellationError(cause, "setup")
|
|
: undefined;
|
|
let primary: AnalysisError = physical;
|
|
if (command?.reason === "cancelled" && command.cancellation !== undefined) {
|
|
primary =
|
|
inspectionCancellation ??
|
|
cancelledBy("setup", carriedReason(command.cancellation.reason), {
|
|
diagnostics: {
|
|
setup_failure: reason,
|
|
command_failure: command.reason,
|
|
},
|
|
...(output === undefined ? {} : { capturedOutput: output }),
|
|
});
|
|
if (failure !== cause) primary.retainSecondaryFailure(physical);
|
|
} else if (inspectionCancellation !== undefined) {
|
|
primary = inspectionCancellation;
|
|
}
|
|
// Executed command cleanup is already retained with its partial outputs
|
|
// by retainCleanup above. Only preparation owners transfer here.
|
|
primary =
|
|
command === undefined || command.preparationFailure !== undefined
|
|
? retainProcessInspectionCleanup(
|
|
command?.preparationFailure ?? cause,
|
|
primary,
|
|
input.resources,
|
|
"setup",
|
|
)
|
|
: command.cancellation === undefined
|
|
? primary
|
|
: analysisErrorWithCancellation(
|
|
primary,
|
|
cancelledBy("setup", carriedReason(command.cancellation.reason)),
|
|
);
|
|
// This finalization checkpoint records a later caller cancellation without
|
|
// reclassifying the established physical command or cleanup outcome.
|
|
if (input.signal?.aborted)
|
|
primary = analysisErrorWithCancellation(
|
|
primary,
|
|
cancelledBy("setup", input.signal),
|
|
);
|
|
throw primary;
|
|
}
|
|
};
|
|
|
|
/** Keep the bounded command's stderr with its failure reason. */
|
|
export const desktopBuildOwnedCommandReason = (cause: unknown): string => {
|
|
const command = cause instanceof OwnedCommandFailure ? cause : undefined;
|
|
const preparation = command?.preparationFailure ?? cause;
|
|
const stderr =
|
|
command?.snapshot?.stderr.text.trim() ??
|
|
(preparation instanceof DarwinProcessOwnershipInspectionError
|
|
? preparation.capturedOutput?.stderr.trim()
|
|
: undefined) ??
|
|
"";
|
|
const message = errorMessage(cause);
|
|
return stderr === "" ? message : `${message}: ${stderr.slice(-2000)}`;
|
|
};
|