Files
morluto 6ac87697e7 fix(errors)!: make retry_class the only retry field of a target failure (#15450)
* fix(errors)!: make retry_class the only retry field of a target failure

A target failure reported two retry fields: its own `retry` hint
(`none | backoff | after_change`) and the `retry_class` every error
carries. They mapped one to one, so readers had to reconcile both.
`retry_class` is now the single owner: the target-failure table declares
it directly, the projection no longer emits `retry`, the error schema no
longer accepts it, and the CLI recovery rewrite reads `retry_class`.

BREAKING CHANGE: target failures no longer include `retry`, and the
error schema rejects it. Read `retry_class` instead: `backoff` is
`same_call`, `after_change` is `change_first` and `none` is `never`.

* test(errors): readers reject the removed target-failure retry field
2026-12-01 10:36:15 -05:00

218 lines
6.9 KiB
TypeScript

import { describe, expect, it } from "vitest";
import { Ajv2020 } from "ajv/dist/2020.js";
import { ProcessCaptureError } from "../../../src/process/capture/ProcessCaptureError.js";
import { analysisErrorJsonSchema } from "../../../src/contracts/analysisErrorJsonSchema.js";
import { analysisErrorProjectionSchema } from "../../../src/domain/analysisErrorProjectionSchema.js";
import { ArtifactOperationError } from "../../../src/domain/artifactOperationError.js";
import { EvidenceFileError } from "../../../src/domain/evidenceErrors.js";
import { HopperRemoteError } from "../../../src/hopper/HopperErrors.js";
import { UnknownRegistryError } from "../../../src/domain/unknownRegistryError.js";
import { projectAnalysisError } from "../../../src/domain/analysisErrorProjection.js";
import { BrowserObservationError } from "../../../src/domain/browserObservationError.js";
import {
AnalysisCancelledError,
AnalysisTimeoutError,
unsupportedTarget,
} from "../../../src/domain/analysisErrorCore.js";
import type { AnalysisError } from "../../../src/domain/analysisErrorBase.js";
it.each([
{ declaredSha256: "a".repeat(64), calculatedSha256: "b".repeat(64) },
{
algorithm: "md5" as const,
declaredMd5: "a".repeat(32),
calculatedMd5: "b".repeat(32),
},
])("retains partial observations on integrity errors", (digests) => {
const error = new ArtifactOperationError(
"trace_web_module_imports",
"integrity",
{
logicalPath: "modules/main.js",
unpacked: false,
...digests,
},
);
error.retainPartialObservation({
kind: "web-artifact-acquisition",
analysis: "not-performed",
files: [],
});
const projected = projectAnalysisError(error);
expect(projected.details?.partial_observation).toEqual({
kind: "web-artifact-acquisition",
analysis: "not-performed",
files: [],
});
expect(analysisErrorProjectionSchema.parse(projected)).toEqual(projected);
const validate = new Ajv2020({ strict: false }).compile(
analysisErrorJsonSchema,
);
expect(validate(projected), JSON.stringify(validate.errors)).toBe(true);
});
it("rejects the removed target-failure retry field in every reader", () => {
const projected = projectAnalysisError(
unsupportedTarget(
"inspect_elf_notes",
"/tmp/t",
"elf",
"not an ELF variant this reader decodes",
),
);
const validate = new Ajv2020({ strict: false }).compile(
analysisErrorJsonSchema,
);
// The projection itself is valid in both readers.
expect(analysisErrorProjectionSchema.safeParse(projected).success).toBe(true);
expect(validate(projected), JSON.stringify(validate.errors)).toBe(true);
// retry_class owns retry semantics; the old field is not accepted back.
const withRetry = { ...projected, retry: "none" };
expect(analysisErrorProjectionSchema.safeParse(withRetry).success).toBe(
false,
);
expect(validate(withRetry)).toBe(false);
});
describe("analysis error projection contract", () => {
it("keeps cleanup failure distinct from unreadable collected output", () => {
const projected = projectAnalysisError(
new BrowserObservationError(
"capture_electron_scenario",
"cleanup_failed",
{
cause: new Error("lineage could not be inspected"),
cleanup: {
reason: "process table permission denied",
resources: ["owned_process_group"],
},
capturedOutput: {
stdout: "completed observation",
stderr: "",
truncated: false,
},
},
),
);
expect(projected).toMatchObject({
code: "cleanup_incomplete",
details: {
cleanup_reason: "process table permission denied",
execution_failure: "execution_failure",
captured_output: { stdout: "completed observation", truncated: false },
},
});
expect(projected.details).not.toHaveProperty("primary_reason");
expect(analysisErrorProjectionSchema.safeParse(projected).success).toBe(
true,
);
});
it.each([
[new AnalysisCancelledError("capture_electron_scenario"), "cancelled"],
[new AnalysisTimeoutError("capture_electron_scenario", 10), "timeout"],
[
new BrowserObservationError("capture_electron_scenario", "disconnected"),
"disconnected",
],
] as const)(
"preserves direct and aggregated primary failures: %s",
(primary, reason) => {
for (const cause of [
primary,
new AggregateError([primary, new Error("close failed")]),
]) {
const projected = projectAnalysisError(
new BrowserObservationError(
"capture_electron_scenario",
"cleanup_failed",
{
cause,
cleanup: {
reason: "close failed",
resources: ["owned_process_group"],
},
},
),
);
expect(projected).toMatchObject({
code: "cleanup_incomplete",
details: { primary_reason: reason, cleanup_reason: "close failed" },
});
expect(analysisErrorProjectionSchema.safeParse(projected).success).toBe(
true,
);
}
},
);
it("accepts every closed error-reason variant", () => {
const variants: AnalysisError[] = [
...(
[
"cancelled",
"integrity",
"limit",
"path",
"unavailable",
"io",
] as const
).map(
(reason) => new ArtifactOperationError("inventory_artifact", reason),
),
...(
[
"not-file",
"exists",
"invalid-json",
"missing",
"path-limit",
"io",
] as const
).map((reason) => new EvidenceFileError("read", reason)),
...(
[
"not-found",
"already-exists",
"revision-conflict",
"invalid-transition",
"integrity",
"limit",
] as const
).map((reason) => new UnknownRegistryError(reason)),
...(["capture_failed", "cleanup_incomplete", "cancelled"] as const).map(
(reason) =>
new ProcessCaptureError("API_TOKEN=selected capture diagnostic", {
reason,
}),
),
...(
[
"remote",
"authorization",
"invalid_request",
"bridge_exception",
] as const
).map(
(diagnostic) =>
new HopperRemoteError(9, "safe", { diagnosticType: diagnostic }),
),
];
for (const variant of variants) {
const projected = projectAnalysisError(variant);
expect(
analysisErrorProjectionSchema.safeParse(projected),
JSON.stringify({ variant, projected }),
).toMatchObject({ success: true });
expect(projected.code).toMatch(/^[a-z][a-z0-9_]*$/u);
if (variant instanceof ProcessCaptureError)
expect(projected).toMatchObject({
message: variant.message,
details: { execution_failure: variant.message },
});
}
});
});