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 committed 2026-10-13 03:22:46 +08:00
1 parent b123b2e51c
commit cecc6ecbff
4 files changed
+220 -44

No files matched your search

@@ -10,6 +10,8 @@ import { tmpdir } from "node:os";
import { join } from "node:path";
import { expect, it } from "vitest";
import { AnalysisInputError } from "../../domain/analysisErrorCore.js";
import { projectAnalysisError } from "../../domain/analysisErrorProjection.js";
import {
resolveExecutable,
resolveProcessScenarioRuntimePaths,
@@ -51,7 +53,12 @@ it("resolves a Windows executable through the canonical PATH spelling", async ()
);
expect(resolved.executable).toBe(candidate);
} catch (cause: unknown) {
const message = cause instanceof Error ? cause.message : String(cause);
const message =
cause instanceof AnalysisInputError
? (cause.issues[0]?.message ?? cause.message)
: cause instanceof Error
? cause.message
: String(cause);
expect(message).toContain(candidate);
expect(message).not.toContain(ambient);
}
@@ -60,14 +67,100 @@ it("resolves a Windows executable through the canonical PATH spelling", async ()
}
});
it("retains the selected missing working directory and OS reason", async () => {
const selected = join(tmpdir(), "rea-process-missing-cwd-unique");
const scenario = parseProcessScenario({
executable: process.execPath,
working_directory: selected,
});
await expect(resolveProcessScenarioRuntimePaths(scenario)).rejects.toThrow(
`working directory could not be resolved: ${selected} (ENOENT)`,
);
it("reports a working-directory file as invalid input", async () => {
const root = await mkdtemp(join(tmpdir(), "rea-process-invalid-cwd-"));
const selectedFile = join(root, "selected-file");
await writeFile(selectedFile, "not a directory");
try {
const failure = await runtimePathFailure({
executable: process.execPath,
working_directory: selectedFile,
});
expect(projectAnalysisError(failure)).toMatchObject({
code: "invalid_request",
details: {
operation: "capture_process_scenario",
issues: [
{
path: ["working_directory"],
reason: "invalid_value",
message: `working directory is not a directory: ${selectedFile}`,
},
],
},
});
} finally {
await rm(root, { recursive: true, force: true });
}
});
it("reports an executable directory as invalid input", async () => {
const root = await mkdtemp(
join(tmpdir(), "rea-process-executable-directory-"),
);
const directory = join(root, "tool-directory");
try {
await mkdir(directory);
const directoryFailure = await runtimePathFailure({
executable: directory,
working_directory: root,
});
expect(projectAnalysisError(directoryFailure)).toMatchObject({
code: "invalid_request",
details: {
issues: [
{
path: ["executable"],
message: `executable could not be resolved: ${directory}; checked ${directory} (not a regular file)`,
},
],
},
});
} finally {
await rm(root, { recursive: true, force: true });
}
});
it.skipIf(process.platform === "win32")(
"reports a non-executable regular file as invalid input",
async () => {
const root = await mkdtemp(join(tmpdir(), "rea-process-non-executable-"));
const selected = join(root, "not-executable");
try {
await writeFile(selected, "not executable");
await chmod(selected, 0o644);
const failure = await runtimePathFailure({
executable: selected,
working_directory: root,
});
expect(projectAnalysisError(failure)).toMatchObject({
code: "invalid_request",
details: {
issues: [
{
path: ["executable"],
message: `executable could not be resolved: ${selected}; checked ${selected} (EACCES)`,
},
],
},
});
} finally {
await rm(root, { recursive: true, force: true });
}
},
);
const runtimePathFailure = async (
scenario: Parameters<typeof parseProcessScenario>[0],
): Promise<AnalysisInputError> => {
let failure: unknown;
try {
await resolveProcessScenarioRuntimePaths(parseProcessScenario(scenario));
} catch (cause: unknown) {
failure = cause;
}
expect(failure).toBeInstanceOf(AnalysisInputError);
if (!(failure instanceof AnalysisInputError))
throw new Error("Selected runtime path should be invalid input");
return failure;
};