Files
reverse-engineering-anything/src/cli/projectCliAnalysisResult.ts
T
morluto 8c9cceceb4 refactor(javascript): name heap and deadline flags from the bound limit, not remediation text (#11448)
* refactor(javascript): bind max_heap_mb and analysis_timeout_ms flags from the limit, not remediation text

The CLI projection now also names the flag of a registered setting limit that carries a request parameter (javascript.max_heap_mb, javascript.analysis_timeout_ms). The JavaScript worker heap and timeout remediation and the web bundle heap remediation no longer name CLI flags; a web bundle heap exhaustion that a larger max_heap_mb raises now binds max_heap_mb as its per-call control.

* test(javascript): assert the adapter-neutral heap remediation in the worker order audit

* refactor(managed): bind max_snapshot_bytes as the per-call control of a managed snapshot refusal (#11453)
2026-11-16 05:24:16 +08:00

105 lines
3.8 KiB
TypeScript

import type { JsonValue } from "../domain/jsonValue.js";
import type { Result } from "../domain/result.js";
interface CliInputFieldBinding {
readonly operation: string;
readonly fields: Readonly<Record<string, string>>;
/** Each contract field, or `field.member`, the command takes as a flag. */
readonly flags: Readonly<Record<string, string>>;
}
/** Flatten a finalized outcome only when returning the command's JSON output. */
export const projectCliAnalysisResult = <Value>(
result: Result<Value, JsonValue>,
): Value | JsonValue => (result.ok ? result.value : result.error);
export const isObject = (
value: JsonValue | undefined,
): value is Record<string, JsonValue> =>
value !== null && typeof value === "object" && !Array.isArray(value);
/**
* Name a contract command's input fields by their CLI spelling in its
* output: a direct failure, a delivered failure or a refused publication.
* Issue paths owned by this operation take the CLI key, and a per-call
* control a failure reports (`details.limit.parameter`, which services name
* by contract field) gains `cli_flag`, the option that sets it on this
* command. Any other output is returned unchanged.
*/
export const projectCliInputFields = <Output>(
output: Output,
binding: CliInputFieldBinding,
): Output => {
const error = output as JsonValue;
if (!isObject(error) || !isObject(error.details)) return output;
const issues = renamedIssues(error, error.details, binding);
const limit = flaggedLimit(error.details.limit, binding);
if (issues === undefined && limit === undefined) return output;
return {
...error,
details: {
...error.details,
...(issues === undefined ? {} : { issues }),
...(limit === undefined ? {} : { limit }),
},
} as Output;
};
/** Issue paths renamed to CLI keys, or undefined when none changes. */
const renamedIssues = (
error: Record<string, JsonValue>,
details: Record<string, JsonValue>,
binding: CliInputFieldBinding,
): JsonValue[] | undefined => {
if (
details.operation !== binding.operation ||
!Array.isArray(details.issues) ||
(error.code !== "invalid_request" &&
!(
error.code === "cleanup_incomplete" &&
details.execution_failure === "invalid_request"
))
)
return undefined;
let changed = false;
const issues = details.issues.map((issue) => {
if (!isObject(issue) || !Array.isArray(issue.path)) return issue;
const field = issue.path[0];
if (typeof field !== "string" || !Object.hasOwn(binding.fields, field))
return issue;
const alias = binding.fields[field];
if (alias === undefined || alias === field) return issue;
changed = true;
return { ...issue, path: [alias, ...issue.path.slice(1)] };
});
// Keep retained output, partial Evidence and cleanup history by reference;
// their source fields are not CLI parameter names.
return changed ? issues : undefined;
};
/**
* The reported per-call control with the flag that sets it, or undefined
* when the command takes no flag for it. A parameter control names it, and so
* does a registered setting a request argument can also select. The longest
* bound prefix of the parameter path names the flag: a flattened member
* option, else the option of the whole field (such as `--producer-limits`
* JSON).
*/
const flaggedLimit = (
limit: JsonValue | undefined,
binding: CliInputFieldBinding,
): Record<string, JsonValue> | undefined => {
if (
!isObject(limit) ||
typeof limit.parameter !== "string" ||
(limit.control_kind !== "parameter" && typeof limit.setting !== "string")
)
return undefined;
const segments = limit.parameter.split(".");
for (let length = segments.length; length > 0; length -= 1) {
const flag = binding.flags[segments.slice(0, length).join(".")];
if (flag !== undefined) return { ...limit, cli_flag: flag };
}
return undefined;
};