Files
reverse-engineering-anything/tests/boundary/cli/projectCliAnalysisResult.test.ts
T
morluto 0035d8edb3 feat(errors): name the fields a failure's recovery selects so the CLI renders their flags (#11644)
* feat(errors): name the fields a failure's recovery selects so the CLI renders their flags

AnalysisError gains related { parameters, operations } (contract fields and operation names), projected as details.related_parameters / related_operations and kept through the domain re-attribution helpers. The CLI projection adds details.related_cli_flags for its own command's fields. inspect_plist's missing-default failure and inspect_sqlite_database's column-evaluation failure name fields only in their text.

* test(sqlite): check MCP omits CLI flags on the failure that names related fields
2026-11-15 20:54:50 -05:00

186 lines
6.3 KiB
TypeScript

import { expect, it } from "vitest";
import {
AnalysisInputError,
AnalysisResourceConstraintError,
reattributedResourceConstraint,
} from "../../../src/domain/analysisErrorCore.js";
import { analysisErrorWithCleanupFailure } from "../../../src/domain/analysisErrorCleanup.js";
import { projectAnalysisError } from "../../../src/domain/analysisErrorProjection.js";
import { freezeJsonSnapshot } from "../../../src/domain/immutableJson.js";
import { ownedJsonObjectSchema } from "../../../src/domain/jsonValue.js";
import { ProviderCleanupError } from "../../../src/domain/providerCleanupError.js";
import { err, ok } from "../../../src/domain/result.js";
import {
projectCliAnalysisResult,
projectCliInputFields,
} from "../../../src/cli/projectCliAnalysisResult.js";
const binding = {
operation: "inspect_keyed_archive",
fields: { path: "archive" },
flags: {},
};
it("projects bound input fields without changing canonical facts or nested identities", () => {
const original = freezeJsonSnapshot(
projectAnalysisError(
new AnalysisInputError("inspect_keyed_archive", undefined, [
{
path: ["path", "path", 0],
reason: "invalid_format",
observed: { path: "verbatim source" },
},
{ path: ["root"], reason: "invalid_value", expected: ["main"] },
]),
),
);
const mapped = projectCliInputFields(original, binding);
expect(mapped).toEqual({
...original,
details: {
...original.details,
issues: [
{
path: ["archive", "path", 0],
reason: "invalid_format",
observed: { path: "verbatim source" },
},
{ path: ["root"], reason: "invalid_value", expected: ["main"] },
],
},
});
expect(original.details?.issues).toMatchObject([
{ path: ["path", "path", 0] },
{ path: ["root"] },
]);
const details = ownedJsonObjectSchema.parse(
ownedJsonObjectSchema.parse(mapped).details,
);
const mappedIssues = details.issues;
const originalIssues = original.details?.issues;
if (!Array.isArray(mappedIssues) || !Array.isArray(originalIssues))
throw new Error("Expected projected input issues");
expect(mappedIssues[1]).toBe(originalIssues[1]);
expect(ownedJsonObjectSchema.parse(mappedIssues[0]).observed).toBe(
ownedJsonObjectSchema.parse(originalIssues[0]).observed,
);
});
it("keeps cleanup history and retained output while binding the actionable primary issue", () => {
const primary = new AnalysisInputError(
"inspect_keyed_archive",
{
capturedOutput: {
stdout: "retained source output",
stderr: "",
truncated: false,
},
},
[{ path: ["path"], reason: "invalid_value" }],
);
const cleanup = new ProviderCleanupError(
"fixture",
["owned-input"],
{},
{
cleanup: { reason: "close denied", resources: ["owned-input"] },
},
);
const original = freezeJsonSnapshot(
projectAnalysisError(analysisErrorWithCleanupFailure(primary, cleanup)),
);
expect(original.code).toBe("cleanup_incomplete");
const mapped = projectCliInputFields(original, binding);
expect(mapped).toMatchObject({
code: "cleanup_incomplete",
details: {
execution_failure: "invalid_request",
issues: [{ path: ["archive"], reason: "invalid_value" }],
diagnostics: {
primary_error: { details: { issues: [{ path: ["path"] }] } },
},
captured_output: { stdout: "retained source output" },
},
});
expect(mapped).toEqual({
...original,
details: {
...original.details,
issues: [{ path: ["archive"], reason: "invalid_value" }],
},
});
const details = ownedJsonObjectSchema.parse(
ownedJsonObjectSchema.parse(mapped).details,
);
expect(details.diagnostics).toBe(original.details?.diagnostics);
expect(details.captured_output).toBe(original.details?.captured_output);
});
it("passes through successful data, unbound operations and unknown error values", () => {
const value = { path: "caller-selected data" };
expect(projectCliInputFields(value, binding)).toBe(value);
const targetError = projectAnalysisError(
new AnalysisInputError("open_binary", undefined, [
{ path: ["path"], reason: "invalid_format" },
]),
);
expect(projectCliInputFields(targetError, binding)).toBe(targetError);
const unbound = projectAnalysisError(
new AnalysisInputError("inspect_keyed_archive", undefined, [
{ path: ["root"], reason: "invalid_value" },
]),
);
expect(projectCliInputFields(unbound, binding)).toBe(unbound);
expect(projectCliAnalysisResult(err(targetError))).toBe(targetError);
expect(projectCliAnalysisResult(ok(value))).toBe(value);
const unknown = {
code: "future_failure",
details: {
operation: "inspect_keyed_archive",
issues: [{ path: ["path"] }],
},
};
expect(projectCliInputFields(unknown, binding)).toBe(unknown);
});
it("names related fields and operations by contract identity and renders only this command's flags", () => {
const constraint = new AnalysisResourceConstraintError(
"inspect_sqlite_database",
"memory",
"fixture limit",
null,
{
related: {
parameters: ["columns", "table", "row_limit"],
operations: ["inspect_artifact"],
},
},
);
// Re-attribution keeps the inputs the recovery names, as it keeps `limit`.
const projected = projectAnalysisError(
reattributedResourceConstraint("inspect_sqlite_database", constraint),
);
expect(projected.details).toMatchObject({
related_parameters: ["columns", "table", "row_limit"],
related_operations: ["inspect_artifact"],
});
const sqlite = {
operation: "inspect_sqlite_database",
fields: {},
flags: { columns: "--column", table: "--table" },
};
// A field the command takes no flag for is left to the contract name.
expect(projectCliInputFields(projected, sqlite)).toMatchObject({
details: { related_cli_flags: { columns: "--column", table: "--table" } },
});
// Another command's failure keeps its fields for that command to render.
expect(
projectCliInputFields(projected, { ...sqlite, operation: "inspect_plist" }),
).toBe(projected);
// An error that names nothing projects no related fields.
expect(
projectAnalysisError(new AnalysisInputError("inspect_plist")).details ?? {},
).not.toHaveProperty("related_parameters");
});