fix(process): preserve invalid runtime path selections as input errors

Classify unresolvable and unusable executable or working-directory selections before launch, retaining field paths, attempted candidates and OS diagnostics. Preserve tagged analysis errors through capture lifecycle handling. Replace duplicate missing-path source assertions with public CLI/MCP regression coverage and retain distinct permission, file-kind and PATH checks.
This commit is contained in:
morluto
2026-10-13 03:22:46 +08:00
parent b123b2e51c
commit cecc6ecbff
4 changed files with 220 additions and 44 deletions
@@ -1,13 +1,11 @@
import { access, realpath, stat } from "node:fs/promises";
import { constants as fsConstants } from "node:fs";
import { access, realpath, stat } from "node:fs/promises";
import { delimiter, isAbsolute, join, resolve } from "node:path";
import type { ProcessScenario } from "../../domain/process/processScenario.js";
import { AnalysisInputError } from "../../domain/analysisErrorCore.js";
import { snapshotEnvironment } from "../snapshotEnvironment.js";
import {
ProcessCaptureError,
processCaptureCancelled,
} from "./ProcessCaptureError.js";
import { processCaptureCancelled } from "./ProcessCaptureError.js";
/** Resolve caller-selected executable and observation paths for this execution. */
export const resolveProcessScenarioRuntimePaths = async (
@@ -20,22 +18,25 @@ export const resolveProcessScenarioRuntimePaths = async (
try {
workingDirectory = await realpath(selectedWorkingDirectory);
} catch (cause: unknown) {
throw new ProcessCaptureError(
throw invalidScenarioPath(
"working_directory",
`working directory could not be resolved: ${selectedWorkingDirectory}${osFailure(cause)}`,
{ cause },
cause,
);
}
let workingDirectoryStat;
try {
workingDirectoryStat = await stat(workingDirectory);
} catch (cause: unknown) {
throw new ProcessCaptureError(
throw invalidScenarioPath(
"working_directory",
`working directory could not be inspected: ${workingDirectory}${osFailure(cause)}`,
{ cause },
cause,
);
}
if (!workingDirectoryStat.isDirectory())
throw new ProcessCaptureError(
throw invalidScenarioPath(
"working_directory",
`working directory is not a directory: ${workingDirectory}`,
);
const executable = await resolveExecutable(
@@ -80,19 +81,27 @@ export const resolveExecutable = async (
if ((await stat(candidate)).isFile()) return candidate;
failures.push(`${candidate} (not a regular file)`);
} catch (cause: unknown) {
const code = osErrorCode(cause);
if (code !== "ENOENT" && code !== "ENOTDIR")
failures.push(`${candidate}${osFailure(cause)}`);
failures.push(`${candidate}${osFailure(cause)}`);
continue;
}
}
throw new ProcessCaptureError(
failures.length === 0
? `executable could not be resolved: ${command}`
: `executable could not be resolved: ${command}; checked ${failures.join("; ")}`,
throw invalidScenarioPath(
"executable",
`executable could not be resolved: ${command}; checked ${failures.join("; ")}`,
);
};
const invalidScenarioPath = (
field: "executable" | "working_directory",
message: string,
cause?: unknown,
): AnalysisInputError =>
new AnalysisInputError(
"capture_process_scenario",
cause === undefined ? undefined : { cause },
[{ path: [field], reason: "invalid_value", message }],
);
const osErrorCode = (cause: unknown): string | undefined =>
typeof cause === "object" &&
cause !== null &&