Files
reverse-engineering-anything/src/application/process/ProcessScenarioPreparation.ts
T
morluto 271e375924 refactor(application): read the host from HostPlatform in the remaining application modules (#11124)
Scenario planning and plan Evidence take a required platform. Host file operations (target resolution, output trees, Windows spelling lookup, executable checks, native snapshot admission), the Linux distribution read and the process CLI adapter read HOST_PLATFORM explicitly. ByteEntropyService takes an optional platform seam, so its Windows casing test injects win32 instead of stubbing process.platform. src/application no longer reads process.platform or process.arch.
2026-11-15 22:03:14 +08:00

66 lines
2.7 KiB
TypeScript

import { AnalysisError } from "../../domain/analysisErrorBase.js";
import {
AnalysisCapabilityUnavailableError,
AnalysisInputError,
cancelledBy,
isAborted,
} from "../../domain/analysisErrorCore.js";
import type { ProcessScenario } from "../../domain/process/processScenario.js";
import { admitFilesystemObservationRoots } from "../../process/capture/FilesystemSnapshot.js";
import { processCaptureOwnershipUnavailableReason } from "../../process/capture/ProcessCaptureCapability.js";
import { createProcessSensitiveOutput } from "../../process/capture/ProcessSensitiveOutput.js";
import { resolveProcessScenarioRuntimePaths } from "../../process/capture/ProcessScenarioRuntimeValidation.js";
import { err, ok, type Result } from "../../domain/result.js";
/** A scenario whose executable and observation paths are resolved for execution. */
export type ResolvedProcessScenario = Omit<ProcessScenario, "dry_run">;
/**
* Resolve a scenario for a dry run. This applies the same executable, working
* directory, path and platform admission checks as a capture, then stops
* before any process is launched.
*/
export const prepareProcessScenarioPlan = async (
scenario: ProcessScenario,
environment: Readonly<Record<string, string | undefined>>,
platform: NodeJS.Platform,
signal?: AbortSignal,
): Promise<Result<ResolvedProcessScenario, AnalysisError>> => {
if (signal?.aborted === true)
return err(cancelledBy("capture_process_scenario", signal));
const ownershipReason = processCaptureOwnershipUnavailableReason(platform);
if (ownershipReason !== undefined)
return err(
new AnalysisCapabilityUnavailableError(
"rea-process",
"capture_process_scenario",
ownershipReason,
{ userMessage: ownershipReason },
),
);
try {
const { dry_run: _dryRun, ...resolved } =
await resolveProcessScenarioRuntimePaths(scenario, environment, platform);
if (isAborted(signal))
return err(cancelledBy("capture_process_scenario", signal));
const privacy = createProcessSensitiveOutput(scenario);
if (!privacy.ok) return privacy;
const admitted = await admitFilesystemObservationRoots(resolved, signal);
if (!admitted.ok)
return isAborted(signal)
? err(cancelledBy("capture_process_scenario", signal))
: admitted;
if (isAborted(signal))
return err(cancelledBy("capture_process_scenario", signal));
return ok(resolved);
} catch (cause: unknown) {
if (isAborted(signal))
return err(cancelledBy("capture_process_scenario", signal));
return err(
cause instanceof AnalysisError
? cause
: new AnalysisInputError("capture_process_scenario", { cause }, []),
);
}
};