Files
reverse-engineering-anything/src/cli/argumentErrors.ts
T
morlutoandCursor Agent b31fc5d5fb fix(cli): advertise domain constraints for selector and path options (#9181)
* fix(cli): advertise domain constraints for selector and path options

Android class, method, and mapping fields, ADB setting keys, and browser
endpoints used bare strings in --schema. They now reuse the domain fields,
so empty selectors, relative mapping paths, and malformed setting keys
fail in the advertised schema.

* fix(cli): keep selector limits when argument parsing rejects them

Empty browser and Electron selectors now fail in the CLI parser, so their
diagnostics are VALIDATION_ERROR with the one-character minimum. ADB
setting keys stay on the selector repair path, which restores maximum 256
and the identifier pattern.

* test(cli): assert empty-selector codes without frozen prose

Empty browser and Electron selectors still fail as VALIDATION_ERROR
too_small on the named field. The tests no longer copy Zod's sentence.

* test(cli): compare endpoint diagnostics with the domain schema

Browser and Inspector tests take custom issue text from the schema that
owns it, and empty capture comparison must name both missing inspection
fields.

* fix(cli): remove duplicate merged schema import

---------

Co-authored-by: Cursor Agent <cursoragent@cursor.com>
2026-11-07 16:17:09 +08:00

112 lines
4.0 KiB
TypeScript

import { Errors } from "incur";
import { z } from "zod";
import { projectInputIssues } from "../domain/inputIssueProjection.js";
import { encodeMarkdownTableCell } from "./cliOutputFormatter.js";
import type { CliInstance } from "./types.js";
import { visitRegisteredCommands } from "./visitRegisteredCommands.js";
const ARGUMENT_GUIDANCE =
"REA could not read the command arguments. Check the named argument and its expected type, then try again.";
const adbPackageCommands = new Set([
"pull-adb-package",
"inspect-adb-package",
"uninstall-adb-package",
"start-adb-app",
"stop-adb-app",
]);
const isAdbSelector = (command: string, field: string): boolean =>
(command.includes("-adb-") && field === "serial") ||
(adbPackageCommands.has(command) && field === "package") ||
(command === "list-adb-directory" && field === "device-path") ||
(command === "resolve-adb-packages" && field === "query") ||
(["pull-adb-file", "push-adb-file"].includes(command) &&
field === "device-path") ||
(command === "start-adb-activity" &&
["action", "component", "data-uri"].includes(field)) ||
(command === "read-adb-setting" && field === "key");
/** Preserve parser correction fields without embedding its serialized cause. */
export const presentCliArgumentErrors = (cli: CliInstance): void => {
visitRegisteredCommands(cli, (command, path) => {
command.middleware = [
async (_context, next) => {
try {
await next();
} catch (cause: unknown) {
if (!(cause instanceof Errors.ValidationError)) throw cause;
const failure = new Errors.ValidationError({
message: ARGUMENT_GUIDANCE,
fieldErrors: adbSelectorConstraints(path, cause),
});
// Incur's constructor appends cause.message as display details. Keep
// the original error separately, without copying it into the message.
Object.defineProperty(failure, "cause", { value: cause });
throw failure;
}
},
...(command.middleware ?? []),
];
});
};
/** Encode only at the final Markdown presentation boundary, after redaction. */
export const encodeCliArgumentErrors = (cli: CliInstance): void => {
visitRegisteredCommands(cli, (command) => {
command.middleware = [
async (context, next) => {
try {
await next();
} catch (error: unknown) {
if (
context.format === "md" &&
error instanceof Errors.ValidationError
) {
error.fieldErrors = error.fieldErrors.map((field) => ({
...field,
...(field.code === undefined
? {}
: { code: encodeMarkdownTableCell(field.code) }),
path: encodeMarkdownTableCell(field.path),
expected: encodeMarkdownTableCell(field.expected),
received: encodeMarkdownTableCell(field.received),
message: encodeMarkdownTableCell(field.message),
}));
}
throw error;
}
},
...(command.middleware ?? []),
];
});
};
/** Retain safe selector constraints dropped by Incur's parser projection. */
const adbSelectorConstraints = (
command: string,
error: Errors.ValidationError,
): Errors.FieldError[] => {
if (!command.includes("-adb-") || !(error.cause instanceof z.ZodError))
return error.fieldErrors;
return error.fieldErrors.map((field, index) => {
const issue =
error.cause instanceof z.ZodError ? error.cause.issues[index] : undefined;
if (
issue === undefined ||
issue.path.length !== 1 ||
issue.path[0] !== field.path ||
issue.code !== field.code ||
!isAdbSelector(command, field.path) ||
(issue.code !== "too_big" && issue.code !== "invalid_format")
)
return field;
const projected = projectInputIssues([issue], undefined)[0];
if (projected?.maximum !== undefined)
return { ...field, maximum: projected.maximum };
if (projected?.expected === "regex") return { ...field, expected: "regex" };
return field;
});
};