mirror of
https://github.com/morluto/rea.git
synced 2026-12-03 21:46:01 +00:00
* fix(errors)!: derive retryable from the producer's declared retry class `retryable` means the unchanged request may succeed, but it was computed downstream as an OR of a code table and five class rules. Every error classification now declares a retry class (same_call, change_first, never, unknown_outcome); `retryable` is `same_call` after one incomplete-cleanup check, and the RETRYABLE_CODES table is deleted. Producer corrections: - update_unknown revision conflicts are change_first and report details.current_revision; duplicate record_unknown reports details.existing_unknown_id. - close_gdb_session's explicit-disposition refusal is change_first. - Per-call artifact reader limits are change_first (never when the limit cannot rise). - An exited or unreachable Hopper bridge, and a Hopper start that another REA session owns, are change_first. - File-descriptor exhaustion (EMFILE/ENFILE) outside process launch is occupied host capacity (same_call) for private workspaces, model and GPU readers and JavaScript application reads. - apktool JVM native-thread exhaustion follows the shared capacity default. BREAKING CHANGE: these refusals now report `retryable: false` (descriptor pressure now reports `retryable: true` as `resource_constraint`). Callers that repeated them unchanged should follow the remediation first: re-read the unknown and retry with details.current_revision, dispose GDB inferiors, raise the reported reader limit, or restart the Hopper provider. * fix(errors): require producers to name recovery for conforming refusals A refusal whose arguments match the input schema was presented as a schema error whenever its producer left out remediation, and many producers did. AnalysisInputError's options are now a union: a conforming refusal must supply both its outcome (userMessage) and next step (remediationAction), so the compiler lists every producer; only contract violations may fall back to the schema guidance. Producers fixed: unknown Frida session and script, decompile_all output-directory state, the duplicate unknown build computer factory (collapsed), stale Cutter sessions, Mach-O architecture selection, output roots that already exist, selected files the host refuses, Ghidra lookups the bridge could not resolve, YARA path refusals, unresolved native symbol names, apt key files and withheld sign-in state. A missing selected APK is now target_unavailable like every other selected-file reader. The capability fallback "Choose another analysis or target" is deleted; a capability refusal without its own recovery names details.reason and binary_session. Refs #15312 * fix(errors): classify caller selections and target bytes by their real failure Several refusals reported a code that reads as corruption or a crash: - a missing unknown_id and a missing Evidence reference now report invalid_request instead of execution_failure and evidence_integrity_mismatch; - close_binary with snapshot_path in a resource-only session reports invalid_request instead of evidence_integrity_mismatch; - ADB push onto an existing device path and pulls into an occupied output directory are conforming invalid_request refusals naming overwrite or output_directory, not unreadable_output; - damaged APK ZIP, signing-block, signer and central-directory structures are malformed targets, not invalid input; - NuGet and VSIX bytes without a ZIP signature are unrecognized, not malformed; - a historical source graph file whose entries contradict its root commitment is an integrity failure, not invalid input; - a CDP protocol failure states what happened instead of the unclassified fallback message. Refs #15319 * feat(errors): expose the producer's retry class as retry_class Every error projection now carries retry_class (same_call, change_first, never or unknown_outcome) beside retryable, which stays true exactly for same_call. The field is additive: the error schema accepts errors retained by earlier releases without it. Refs #15323 * fix(errors): address review of the folded error-contract PRs Keep a provider's own capability message as its remediation; the binary_session fallback applies only to a bare declared reason. Delete the unused conformingInputRefusal helper. Compose both retry fields for a refused publication whose cleanup is incomplete. Report descriptor exhaustion from a JavaScript directory opendir as occupied capacity. Let an exited Hopper startup attempt without a setup failure code retry unchanged. Declare retry for the Ghidra superseded checkpoint and unknown IDA readiness. Redact ambiguous selector candidates. Give a Ghidra document miss its own recovery. Drop an unchanged-files claim the journal repair cannot make. Name each withheld sign-in refusal's step. Hash the source graph commitment before the cancellation yield. Keep the browser protocol_error message neutral. Add tests for mutation-audit survivors C3, C5, P2, P4, P8, H1, H4 and H5. * fix(errors): keep a missing Evidence reference an integrity failure and state only the observed commitment mismatch * test(desktop): assert the unknown build computer refusal's remediation, not its message The refusal now carries its next step in remediation.action instead of the message, so the closed-computer checks assert the exact code, message and remediation facts. * fix(errors): keep producer retry choices apart from cleanup and fixed ceilings A capture's retryable flag no longer folds in pending cleanup, so the projection applies the cleanup rule once and a transient capture with pending cleanup reports change_first instead of never. A release-fixed artifact allowance reports never, and a stale admitted session identity reports change_first (reopen, then retry); both have catalogue rows. The bare capability-unavailable remediation names binary_session only for a binary-analysis session, so standalone bridges such as Cutter are pointed at their own reason. * test(vsix): expect a non-ZIP selection to be refused as unrecognized This PR reports bytes without a ZIP signature as another format (unrecognized), not a damaged package; the exact-reason assertion added on main still expected malformed. * test(delivery): expect the capacity refusal's retry class in delivered failure facts The exact failure facts asserted on main predate the projection's retry_class; occupied capacity reports same_call. * fix(errors): mark a sent Hopper cancellation unknown and missing Evidence recoverable A cancellation Hopper already received may still apply, so its retry class is unknown_outcome instead of same_call; an unsent cancellation keeps same_call. A missing EvidenceReferenceError is change_first, since importing the referenced records lets the same request succeed; other reference failures stay never. Both have catalogue rows. * fix(hopper): ask for changed launch arguments before retrying an unencodable launch A launcher path or argument that cannot be encoded as an AppleScript literal meets the same pre-launch guard on every attempt, so the start refusal is change_first and its retry_action is unknown; a start failure without a declared reason stays same_call. * fix(errors): keep a one-shot cleanup failure's retry class with its cleanup state A one-shot reply that reports cleanup_incomplete spread the primary or cleanup projection and overrode only retryable, so a timed-out read whose close also failed carried retry_class same_call. The reply now reads its retry class from the typed composed failure, and never offers the same call before cleanup settles. * test(evtx): expect an unreadable selected log to be refused with its own recovery This PR treats a well-formed path the host refused as a regular file as conforming input with its own remediation; the evtx boundary test still expected a schema-recovery refusal. * test(javascript): expect a broken source-graph commitment as an integrity failure This PR reports a well-formed historical source graph whose entries disagree with its root_sha256 as evidence_integrity_mismatch, not an input issue; the exact refusal assertion added on main still expected invalid_request.
1586 lines
53 KiB
TypeScript
1586 lines
53 KiB
TypeScript
import { describe, expect, it } from "vitest";
|
|
import {
|
|
AnalysisCapabilityUnavailableError,
|
|
AnalysisInputError,
|
|
AnalysisOutputError,
|
|
AnalysisCancelledError,
|
|
AnalysisTimeoutError,
|
|
AnalysisResourceConstraintError,
|
|
AnalysisArtifactChangedError,
|
|
type AnalysisInputIssue,
|
|
unsupportedTarget,
|
|
parameterFileSizeConstraint,
|
|
} from "./analysisErrorCore.js";
|
|
import { ArtifactOperationError } from "./artifactOperationError.js";
|
|
import {
|
|
BinaryTargetError,
|
|
ConfigurationError,
|
|
} from "./configurationErrors.js";
|
|
import { BrowserObservationError } from "./browserObservationError.js";
|
|
import { EvidenceFileError, EvidenceIntegrityError } from "./evidenceErrors.js";
|
|
import { ProviderAdapterError } from "./providerAdapterError.js";
|
|
import { UnknownRegistryError } from "./unknownRegistryError.js";
|
|
import {
|
|
childFailureDiagnostics,
|
|
projectAnalysisError,
|
|
projectAnalysisErrorWithoutPartialObservation,
|
|
} from "./analysisErrorProjection.js";
|
|
import {
|
|
analysisErrorRemediationAction,
|
|
analysisErrorUserMessage,
|
|
} from "./analysisErrorPresentation.js";
|
|
import { ProviderSelectionError } from "./providerSelectionError.js";
|
|
import { unknownFridaSession } from "./fridaInputErrors.js";
|
|
import { analysisErrorProjectionSchema } from "./analysisErrorProjectionSchema.js";
|
|
import { nativeCallPartialObservationSchema } from "./native/nativeCallPartialObservation.js";
|
|
import { analysisSettingLimit } from "./analysisSettingDefinitions.js";
|
|
import { redactExplicitFailure } from "./explicitSensitiveFailure.js";
|
|
import { redactExplicitText } from "./explicitSensitiveValues.js";
|
|
import { analysisErrorWithCleanupFailure } from "./analysisErrorCleanup.js";
|
|
import {
|
|
AnalysisError,
|
|
ProviderOwnedAnalysisError,
|
|
providerErrorTag,
|
|
type AnalysisCallerLifetime,
|
|
type AnalysisErrorOwnPresentation,
|
|
} from "./analysisErrorBase.js";
|
|
import { ProviderCleanupError } from "./providerCleanupError.js";
|
|
import { releaseOwnedCleanup } from "../../tests/support/cleanupRelease.js";
|
|
|
|
it("projects nested workflow-publication recovery for the actual caller lifetime", () => {
|
|
const child = new UnknownRegistryError(
|
|
"limit",
|
|
).retainWorkflowUnknownPublication({
|
|
evidence_id: `ev_${"a".repeat(64)}`,
|
|
evidence_retention: "retained",
|
|
total_questions: 3,
|
|
created_questions: 1,
|
|
existing_questions: 0,
|
|
next_question_index: 1,
|
|
});
|
|
const error = new AnalysisCancelledError(
|
|
"trace_feature",
|
|
childFailureDiagnostics({ child: { error: child } }),
|
|
);
|
|
const continuing = projectAnalysisError(error);
|
|
const invocation = projectAnalysisError(error, "invocation");
|
|
const childOf = (value: typeof continuing) => {
|
|
const diagnostics = value.details?.diagnostics;
|
|
if (
|
|
typeof diagnostics !== "object" ||
|
|
diagnostics === null ||
|
|
Array.isArray(diagnostics)
|
|
)
|
|
throw new Error("Expected child diagnostics");
|
|
return analysisErrorProjectionSchema.parse(diagnostics.child);
|
|
};
|
|
expect(
|
|
childOf(continuing).details?.workflow_unknown_publication,
|
|
).toMatchObject({
|
|
evidence_retention: "retained",
|
|
created_questions: 1,
|
|
next_question_index: 1,
|
|
});
|
|
expect(childOf(continuing).remediation.action).toContain(
|
|
"get_evidence_bundle",
|
|
);
|
|
expect(
|
|
childOf(invocation).details?.workflow_unknown_publication,
|
|
).toMatchObject({
|
|
evidence_retention: "invocation-only",
|
|
created_questions: 1,
|
|
next_question_index: 1,
|
|
});
|
|
expect(childOf(invocation).remediation.action).not.toContain(
|
|
"get_evidence_bundle",
|
|
);
|
|
});
|
|
|
|
describe("agent recovery metadata", () => {
|
|
it("distinguishes deterministic duplicate identities from stale update revisions", () => {
|
|
const unknownId = `unk_${"1".repeat(64)}`;
|
|
const duplicate = projectAnalysisError(
|
|
new UnknownRegistryError("already-exists", {
|
|
existingUnknownId: unknownId,
|
|
}),
|
|
);
|
|
expect(duplicate).toMatchObject({
|
|
code: "invalid_request",
|
|
category: "invalid_input",
|
|
retryable: false,
|
|
details: { reason: "already-exists", existing_unknown_id: unknownId },
|
|
});
|
|
expect(analysisErrorProjectionSchema.parse(duplicate)).toEqual(duplicate);
|
|
// A stale expected_revision cannot succeed unchanged: the caller re-reads
|
|
// the reported revision first.
|
|
const conflict = projectAnalysisError(
|
|
new UnknownRegistryError("revision-conflict", { currentRevision: 3 }),
|
|
);
|
|
expect(conflict).toMatchObject({
|
|
code: "revision_conflict",
|
|
retryable: false,
|
|
message:
|
|
"The unknown's stored revision is 3, which does not match expected_revision.",
|
|
remediation: {
|
|
action:
|
|
"Retry update_unknown with expected_revision 3 after reviewing the current state with list_unknowns.",
|
|
},
|
|
details: { reason: "revision-conflict", current_revision: 3 },
|
|
});
|
|
expect(analysisErrorProjectionSchema.parse(conflict)).toEqual(conflict);
|
|
});
|
|
|
|
it.each(["history_gap", "revision_chain"] as const)(
|
|
"preserves %s as an integrity failure with producer facts",
|
|
(kind) => {
|
|
const constraint = { kind, unknown_id: `unk_${"0".repeat(64)}` };
|
|
const projected = projectAnalysisError(
|
|
new UnknownRegistryError(constraint, {
|
|
cause: new Error("Unrelated private diagnostic"),
|
|
}),
|
|
);
|
|
expect(projected).toMatchObject({
|
|
code: "evidence_integrity_mismatch",
|
|
retryable: false,
|
|
details: { reason: "integrity", constraint },
|
|
});
|
|
expect(projected.message).toContain(constraint.unknown_id);
|
|
expect(JSON.stringify(projected)).not.toContain(
|
|
"Unrelated private diagnostic",
|
|
);
|
|
expect(analysisErrorProjectionSchema.parse(projected)).toEqual(projected);
|
|
},
|
|
);
|
|
|
|
it("names the input contract and preserves the rejected argument", () => {
|
|
const issue = {
|
|
path: ["view", "kind"],
|
|
reason: "invalid_value",
|
|
expected: ["summary", "module"],
|
|
} satisfies AnalysisInputIssue;
|
|
const projected = projectAnalysisError(
|
|
new AnalysisInputError("inspect_analysis_view", undefined, [issue]),
|
|
);
|
|
|
|
expect(projected).toMatchObject({
|
|
code: "invalid_request",
|
|
retryable: false,
|
|
details: { operation: "inspect_analysis_view", issues: [issue] },
|
|
recovery: {
|
|
kind: "inspect_input_contract",
|
|
operation: "inspect_analysis_view",
|
|
},
|
|
remediation: { action: expect.stringContaining("inspect_analysis_view") },
|
|
});
|
|
expect(projected.remediation.action).toContain("tools/list");
|
|
expect(analysisErrorProjectionSchema.parse(projected)).toEqual(projected);
|
|
});
|
|
|
|
it("omits schema recovery when a conforming request lacks a target fact", () => {
|
|
const issue = {
|
|
path: ["address"],
|
|
reason: "invalid_value",
|
|
message: "Address 0x2000 is in uninitialized memory block .bss",
|
|
observed: { address: "0x2000", file_mapping: "uninitialized" },
|
|
} as const;
|
|
const projected = projectAnalysisError(
|
|
new AnalysisInputError(
|
|
"address_to_file_offset",
|
|
{
|
|
userMessage: "No exact original-file offset exists.",
|
|
remediationAction: "Select an address inside a file-backed mapping.",
|
|
conformsToInputContract: true,
|
|
},
|
|
[issue],
|
|
),
|
|
);
|
|
|
|
expect(projected).toEqual({
|
|
code: "invalid_request",
|
|
category: "invalid_input",
|
|
message: "No exact original-file offset exists.",
|
|
retryable: false,
|
|
retry_class: "change_first",
|
|
remediation: {
|
|
action: "Select an address inside a file-backed mapping.",
|
|
},
|
|
details: { operation: "address_to_file_offset", issues: [issue] },
|
|
});
|
|
expect(analysisErrorProjectionSchema.parse(projected)).toEqual(projected);
|
|
});
|
|
|
|
it("keeps a conforming refusal's own recovery through redaction, never the schema's", () => {
|
|
const refusal = unknownFridaSession("load_frida_script", "fixture-session");
|
|
for (const error of [
|
|
refusal,
|
|
redactExplicitFailure(refusal, ["fixture-session"]),
|
|
]) {
|
|
const projected = projectAnalysisError(error);
|
|
expect(projected).not.toHaveProperty("recovery");
|
|
expect(projected.remediation.action).toMatch(
|
|
/^Use a session_id that start_frida_session returned/u,
|
|
);
|
|
expect(projected.remediation.action).not.toMatch(/schema|tools\/list/u);
|
|
expect(projected.message).not.toContain("Analysis input is invalid");
|
|
}
|
|
});
|
|
|
|
it("separates timeout-state inspection from client deadline recovery", () => {
|
|
const projected = projectAnalysisError(
|
|
new AnalysisTimeoutError("inspect_binary_layout", 2_500),
|
|
);
|
|
|
|
expect(projected).toMatchObject({
|
|
code: "provider_timeout",
|
|
retryable: true,
|
|
recovery: {
|
|
kind: "inspect_timeout_state",
|
|
operation: "inspect_binary_layout",
|
|
timeout_ms: 2_500,
|
|
},
|
|
});
|
|
expect(analysisErrorProjectionSchema.parse(projected)).toEqual(projected);
|
|
});
|
|
});
|
|
|
|
describe("capability failure retry disposition", () => {
|
|
it.each([
|
|
{
|
|
name: "a capability without a retry disposition uses the fallback",
|
|
options: {},
|
|
category: "unsupported_provider",
|
|
retryable: false,
|
|
action: "Choose a supported target.",
|
|
message: undefined,
|
|
},
|
|
{
|
|
name: "an explicit refusal keeps its producer guidance",
|
|
options: {
|
|
remediationAction: "Use the documented host workflow.",
|
|
userMessage: "Host capability unavailable.",
|
|
},
|
|
category: "unsupported_provider",
|
|
retryable: false,
|
|
action: "Use the documented host workflow.",
|
|
message: "Host capability unavailable.",
|
|
},
|
|
{
|
|
name: "a transient failure can retry without switching targets",
|
|
options: { retry: "same_call" },
|
|
category: "unavailable",
|
|
retryable: true,
|
|
action: expect.stringContaining("retry the same analysis"),
|
|
message: "Required runtime controls are unavailable for this attempt.",
|
|
},
|
|
{
|
|
name: "a refusal the caller must act on first is not retryable unchanged",
|
|
options: {
|
|
retry: "change_first",
|
|
remediationAction:
|
|
"Choose an explicit disposition, then retry closing.",
|
|
userMessage: "Close requires an explicit disposition.",
|
|
},
|
|
category: "unavailable",
|
|
retryable: false,
|
|
action: "Choose an explicit disposition, then retry closing.",
|
|
message: "Close requires an explicit disposition.",
|
|
},
|
|
] as const)("$name", ({ options, retryable, category, action, message }) => {
|
|
const reason = "The owned runtime did not meet its requirements.";
|
|
const projected = projectAnalysisError(
|
|
new AnalysisCapabilityUnavailableError("fixture", "close", reason, {
|
|
fallbackRemediationAction: "Choose a supported target.",
|
|
...options,
|
|
cause: new Error("private internal cause"),
|
|
}),
|
|
);
|
|
|
|
expect(projected).toMatchObject({
|
|
code: "capability_unavailable",
|
|
category,
|
|
retryable,
|
|
remediation: { action },
|
|
details: { provider_id: "fixture", operation: "close", reason },
|
|
});
|
|
if (message !== undefined) expect(projected.message).toBe(message);
|
|
expect(JSON.stringify(projected)).not.toContain("private internal cause");
|
|
expect(analysisErrorProjectionSchema.parse(projected)).toEqual(projected);
|
|
});
|
|
|
|
it("retains cleanup ownership before allowing a transient operation to retry", () => {
|
|
expect(
|
|
projectAnalysisError(
|
|
new AnalysisCapabilityUnavailableError(
|
|
"fixture-provider",
|
|
"inspect",
|
|
"temporary failure",
|
|
{
|
|
retry: "same_call",
|
|
cleanup: {
|
|
reason: "owned handle is still live",
|
|
resources: ["native-owner"],
|
|
},
|
|
},
|
|
),
|
|
),
|
|
).toMatchObject({
|
|
code: "cleanup_incomplete",
|
|
retryable: false,
|
|
details: {
|
|
cleanup: "incomplete",
|
|
resources: ["native-owner"],
|
|
execution_failure: "capability_unavailable",
|
|
},
|
|
});
|
|
});
|
|
});
|
|
|
|
it("projects a published file receipt alongside retained cleanup failure", () => {
|
|
const projected = projectAnalysisError(
|
|
new EvidenceFileError("write", "io", {
|
|
path: "/tmp/evidence.json",
|
|
published: { path: "/tmp/evidence.json", bytes: 42 },
|
|
cleanup: {
|
|
reason: "staging directory is still owned",
|
|
resources: ["/tmp/.rea-write-abc"],
|
|
},
|
|
cause: new Error("cleanup incomplete"),
|
|
}),
|
|
);
|
|
expect(projected).toMatchObject({
|
|
code: "cleanup_incomplete",
|
|
message: expect.stringMatching(/published.*staging cleanup.*incomplete/iu),
|
|
remediation: {
|
|
action: expect.stringContaining("details.published_path"),
|
|
},
|
|
details: {
|
|
operation: "write",
|
|
path: "/tmp/evidence.json",
|
|
published: true,
|
|
published_path: "/tmp/evidence.json",
|
|
bytes: 42,
|
|
resources: ["/tmp/.rea-write-abc"],
|
|
},
|
|
});
|
|
expect(analysisErrorProjectionSchema.safeParse(projected).success).toBe(true);
|
|
});
|
|
|
|
const nativeCallPartialObservation = nativeCallPartialObservationSchema.parse({
|
|
kind: "native-call-observation",
|
|
target: {
|
|
path: "/tmp/selected-app",
|
|
sha256: "a".repeat(64),
|
|
architecture: "arm64",
|
|
arguments: [],
|
|
environment: {},
|
|
working_directory: null,
|
|
},
|
|
process: {
|
|
pid: 4242,
|
|
stdout: {
|
|
text: "partial stdout",
|
|
bytes: 13,
|
|
truncated: false,
|
|
complete: false,
|
|
},
|
|
stderr: null,
|
|
other_stops: [],
|
|
},
|
|
debugger: { version: null },
|
|
events: [],
|
|
coverage: { status: "partial", reason: "cancelled" },
|
|
limitations: ["Observation ended before the requested window completed."],
|
|
});
|
|
|
|
const retainedOutput = {
|
|
stdout: "selected output\u0000",
|
|
stderr: "upstream warning",
|
|
truncated: true,
|
|
};
|
|
|
|
it("distinguishes unsupported target formats from unavailable provider capabilities", () => {
|
|
const projected = projectAnalysisError(
|
|
unsupportedTarget(
|
|
"inspect_evm_interface",
|
|
"/selected/runtime.hex",
|
|
"evm-bytecode",
|
|
"EOF-style EF00 container is unsupported.",
|
|
{ capturedOutput: retainedOutput },
|
|
),
|
|
);
|
|
expect(projected).toMatchObject({
|
|
code: "unsupported_target",
|
|
category: "unsupported_target",
|
|
retryable: false,
|
|
details: {
|
|
operation: "inspect_evm_interface",
|
|
path: "/selected/runtime.hex",
|
|
reason: "EOF-style EF00 container is unsupported.",
|
|
captured_output: retainedOutput,
|
|
},
|
|
});
|
|
expect(projected.message).toContain("EF00");
|
|
expect(projected.remediation.action).toContain("Choose another operation");
|
|
expect(projected.remediation.action).not.toContain("doctor");
|
|
expect(analysisErrorProjectionSchema.safeParse(projected).success).toBe(true);
|
|
});
|
|
|
|
it("keeps a refusing provider's recovery on a target selection failure", () => {
|
|
const selection = {
|
|
reason: "target_unsupported" as const,
|
|
requestedProviderId: "ghidra",
|
|
candidateIds: ["ghidra"],
|
|
userMessage: "Unsupported target: the configured extension is x86-64 only.",
|
|
};
|
|
expect(
|
|
projectAnalysisError(
|
|
new ProviderSelectionError({
|
|
...selection,
|
|
remediationAction: "Omit the extension setting for this target.",
|
|
}),
|
|
),
|
|
).toMatchObject({
|
|
code: "capability_unavailable",
|
|
message: selection.userMessage,
|
|
remediation: { action: "Omit the extension setting for this target." },
|
|
details: { selection_reason: "target_unsupported" },
|
|
});
|
|
expect(
|
|
projectAnalysisError(new ProviderSelectionError(selection)).remediation
|
|
.action,
|
|
).toBe(selection.userMessage);
|
|
});
|
|
|
|
it.each([false, true])(
|
|
"marks occupied capacity retryable only while cleanup is confirmed: %s",
|
|
(cleanupIncomplete) => {
|
|
const projected = projectAnalysisError(
|
|
new AnalysisResourceConstraintError(
|
|
"inspect_sqlite_database",
|
|
"capacity",
|
|
"Inspection slots are occupied",
|
|
{ max_concurrent_inspections: 2 },
|
|
cleanupIncomplete
|
|
? {
|
|
cleanup: {
|
|
reason: "Snapshot removal denied",
|
|
resources: ["/private/snapshot"],
|
|
},
|
|
}
|
|
: undefined,
|
|
),
|
|
);
|
|
expect(projected.retryable).toBe(!cleanupIncomplete);
|
|
expect(projected.code).toBe(
|
|
cleanupIncomplete ? "cleanup_incomplete" : "resource_constraint",
|
|
);
|
|
expect(projected.details).toMatchObject({
|
|
resource: "capacity",
|
|
reported_limits: { max_concurrent_inspections: 2 },
|
|
});
|
|
expect(projected.remediation.action).toContain("Wait");
|
|
expect(analysisErrorProjectionSchema.safeParse(projected).success).toBe(
|
|
true,
|
|
);
|
|
},
|
|
);
|
|
|
|
it.each([
|
|
["memory", null],
|
|
["memory", { address_space_bytes: 67108864 }],
|
|
["cpu", null],
|
|
["cpu", { cpu_seconds: 1 }],
|
|
["file-size", null],
|
|
["file-size", { file_size_bytes: 1024 }],
|
|
["transport", { result_budget_bytes: 10485760 }],
|
|
] as const)(
|
|
"projects reported %s constraints with observed or unknown limits: %j",
|
|
(resource, limits) => {
|
|
const projected = projectAnalysisError(
|
|
new AnalysisResourceConstraintError(
|
|
"inspect_binary_layout",
|
|
resource,
|
|
resource === "memory"
|
|
? "Memory allocation failed; exact cause unknown."
|
|
: "Observed SIGXCPU; exact signal cause unknown.",
|
|
limits,
|
|
{ capturedOutput: retainedOutput },
|
|
),
|
|
);
|
|
expect(projected).toMatchObject({
|
|
code: "resource_constraint",
|
|
category: "resource_constraint",
|
|
retryable: false,
|
|
details: {
|
|
resource,
|
|
reported_limits: limits,
|
|
captured_output: retainedOutput,
|
|
},
|
|
});
|
|
expect(analysisErrorProjectionSchema.safeParse(projected).success).toBe(
|
|
true,
|
|
);
|
|
},
|
|
);
|
|
it.each([
|
|
new AnalysisInputError("inspect", { capturedOutput: retainedOutput }),
|
|
new AnalysisOutputError("inspect", "invalid reply", {
|
|
capturedOutput: retainedOutput,
|
|
}),
|
|
new AnalysisCapabilityUnavailableError(
|
|
"provider",
|
|
"inspect",
|
|
"unsupported profile",
|
|
{ capturedOutput: retainedOutput },
|
|
),
|
|
new AnalysisCancelledError("inspect", { capturedOutput: retainedOutput }),
|
|
new AnalysisTimeoutError("inspect", 30, { capturedOutput: retainedOutput }),
|
|
new ProviderSelectionError({
|
|
operation: "inspect",
|
|
reason: "provider_unavailable",
|
|
requestedProviderId: "provider",
|
|
candidateIds: [],
|
|
capturedOutput: retainedOutput,
|
|
}),
|
|
])(
|
|
"retains captured output without changing the typed primary failure: $._tag",
|
|
(failure) => {
|
|
expect(projectAnalysisError(failure).details?.captured_output).toEqual(
|
|
retainedOutput,
|
|
);
|
|
},
|
|
);
|
|
|
|
it.each([
|
|
new AnalysisCancelledError("observe_native_calls", {
|
|
cleanup: {
|
|
reason: "target process termination could not be verified",
|
|
resources: ["native-target:4242"],
|
|
},
|
|
}),
|
|
new AnalysisTimeoutError("observe_native_calls", 65_000, {
|
|
cleanup: {
|
|
reason: "target process identity was unavailable",
|
|
resources: ["native-target:4242"],
|
|
},
|
|
}),
|
|
])("retains incomplete cleanup for lifecycle failures: $._tag", (failure) => {
|
|
expect(projectAnalysisError(failure)).toMatchObject({
|
|
code: "cleanup_incomplete",
|
|
details: {
|
|
cleanup: "incomplete",
|
|
cleanup_reason: expect.any(String),
|
|
resources: ["native-target:4242"],
|
|
},
|
|
});
|
|
});
|
|
|
|
it("retains the original failure tag and code when cleanup is incomplete", () => {
|
|
const error = new EvidenceIntegrityError(
|
|
"The launched native target did not match its selected digest",
|
|
{
|
|
cleanup: {
|
|
reason: "target survivor identity could not be verified",
|
|
resources: ["native-target:4242"],
|
|
},
|
|
},
|
|
);
|
|
expect(error._tag).toBe("EvidenceIntegrityError");
|
|
expect(projectAnalysisError(error)).toMatchObject({
|
|
code: "cleanup_incomplete",
|
|
category: "integrity_mismatch",
|
|
details: {
|
|
cleanup: "incomplete",
|
|
cleanup_reason: "target survivor identity could not be verified",
|
|
resources: ["native-target:4242"],
|
|
execution_failure: "evidence_integrity_mismatch",
|
|
},
|
|
});
|
|
});
|
|
|
|
it("projects native partial observations through healthy cancellation failures", () => {
|
|
const projected = projectAnalysisError(
|
|
new AnalysisCancelledError("observe_native_calls", {
|
|
partialObservation: nativeCallPartialObservation,
|
|
}),
|
|
);
|
|
|
|
expect(projected).toMatchObject({
|
|
code: "cancelled",
|
|
details: {
|
|
operation: "observe_native_calls",
|
|
cleanup: "complete",
|
|
partial_observation: nativeCallPartialObservation,
|
|
},
|
|
});
|
|
expect(analysisErrorProjectionSchema.safeParse(projected).success).toBe(true);
|
|
});
|
|
|
|
it("retains native partial observations and cleanup uncertainty on provider failures", () => {
|
|
const projected = projectAnalysisError(
|
|
new ProviderAdapterError("native-lldb", "observe_native_calls", {
|
|
partialObservation: nativeCallPartialObservation,
|
|
cleanup: {
|
|
reason: "target process termination could not be verified",
|
|
resources: ["native-target:4242"],
|
|
},
|
|
}),
|
|
);
|
|
|
|
expect(projected).toMatchObject({
|
|
code: "cleanup_incomplete",
|
|
details: {
|
|
provider_id: "native-lldb",
|
|
operation: "observe_native_calls",
|
|
cleanup: "incomplete",
|
|
cleanup_reason: "target process termination could not be verified",
|
|
resources: ["native-target:4242"],
|
|
execution_failure: "execution_failure",
|
|
partial_observation: nativeCallPartialObservation,
|
|
},
|
|
});
|
|
expect(analysisErrorProjectionSchema.safeParse(projected).success).toBe(true);
|
|
});
|
|
|
|
describe("analysis error projection: artifact reader limits", () => {
|
|
const limited = (canIncrease: boolean) =>
|
|
new ArtifactOperationError("inventory_artifact", "limit", undefined, "", {
|
|
readerLimit: {
|
|
parameter: "max_entries",
|
|
effective_value: 10,
|
|
unit: "count",
|
|
minimum: 1,
|
|
maximum: canIncrease ? 100 : 10,
|
|
applies_at: "next_operation",
|
|
agent_editable: true,
|
|
can_increase: canIncrease,
|
|
update_mode: "parameter",
|
|
pending_restart: false,
|
|
},
|
|
});
|
|
|
|
it("asks for a raised limit first, and never a same-call retry", () => {
|
|
expect(projectAnalysisError(limited(true))).toMatchObject({
|
|
retryable: false,
|
|
retry_class: "change_first",
|
|
});
|
|
expect(projectAnalysisError(limited(false))).toMatchObject({
|
|
retryable: false,
|
|
retry_class: "never",
|
|
});
|
|
const composed = analysisErrorWithCleanupFailure(
|
|
limited(true),
|
|
new ProviderCleanupError(
|
|
"artifact",
|
|
["/tmp/reader"],
|
|
{ reason: "close failed" },
|
|
{ operation: "inventory_artifact" },
|
|
),
|
|
"inventory_artifact",
|
|
);
|
|
expect(projectAnalysisError(composed)).toMatchObject({
|
|
code: "cleanup_incomplete",
|
|
retryable: false,
|
|
});
|
|
releaseOwnedCleanup(composed);
|
|
});
|
|
});
|
|
|
|
describe("analysis error projection: browser provider failures", () => {
|
|
it("reports a protocol failure the caller's cancellation caused as cancelled", () => {
|
|
expect(
|
|
projectAnalysisError(
|
|
new BrowserObservationError(
|
|
"capture_browser_scenario",
|
|
"protocol_error",
|
|
{ cause: new AnalysisCancelledError("capture_browser_scenario") },
|
|
),
|
|
),
|
|
).toMatchObject({ code: "cancelled", category: "cancelled" });
|
|
});
|
|
|
|
it("explains the service-worker policy refusal and names its recovery", () => {
|
|
const projected = projectAnalysisError(
|
|
new BrowserObservationError(
|
|
"capture_browser_scenario",
|
|
"service_worker_blocked",
|
|
),
|
|
);
|
|
|
|
expect(projected).toMatchObject({
|
|
code: "target_unavailable",
|
|
category: "unavailable",
|
|
retryable: false,
|
|
message: expect.stringContaining("service-worker policy is block"),
|
|
remediation: {
|
|
action: expect.stringContaining("environment.service_workers to allow"),
|
|
},
|
|
details: {
|
|
operation: "capture_browser_scenario",
|
|
reason: "service_worker_blocked",
|
|
},
|
|
});
|
|
});
|
|
|
|
it("projects the primary browser failure alongside incomplete cleanup", () => {
|
|
const projected = projectAnalysisError(
|
|
new BrowserObservationError(
|
|
"observe_javascript_runtime",
|
|
"cleanup_failed",
|
|
{
|
|
cause: new AggregateError([
|
|
new BrowserObservationError(
|
|
"observe_javascript_runtime",
|
|
"target_changed",
|
|
),
|
|
new Error("socket close failed"),
|
|
]),
|
|
},
|
|
),
|
|
);
|
|
|
|
expect(projected).toMatchObject({
|
|
code: "cleanup_incomplete",
|
|
details: {
|
|
operation: "observe_javascript_runtime",
|
|
reason: "cleanup_failed",
|
|
cleanup: "incomplete",
|
|
resources: ["browser_transport"],
|
|
primary_reason: "target_changed",
|
|
},
|
|
});
|
|
});
|
|
|
|
it("keeps producer facts when a browser failure is attributed to another operation", () => {
|
|
const partial = { kind: "browser-test-partial" } as never;
|
|
const original = new BrowserObservationError(
|
|
"inspect_web_page",
|
|
"cleanup_failed",
|
|
{
|
|
detail: "Target page-1 closed while REA released its session.",
|
|
remediationAction: "Release session page-1, then retry.",
|
|
cleanup: { reason: "socket close failed", resources: ["page-1"] },
|
|
diagnostics: { endpoint: "loopback" },
|
|
partialObservation: partial,
|
|
cause: new AggregateError([
|
|
new BrowserObservationError("inspect_web_page", "target_changed"),
|
|
new Error("socket close failed"),
|
|
]),
|
|
},
|
|
);
|
|
const relabelled = original.forOperation("capture_web_screenshot");
|
|
|
|
expect(relabelled).toMatchObject({
|
|
operation: "capture_web_screenshot",
|
|
reason: "cleanup_failed",
|
|
cause: original.cause,
|
|
diagnostics: { endpoint: "loopback" },
|
|
partialObservation: partial,
|
|
});
|
|
const { details, ...projected } = projectAnalysisError(relabelled);
|
|
const { details: originalDetails, ...originalProjected } =
|
|
projectAnalysisError(original);
|
|
expect(projected).toEqual(originalProjected);
|
|
expect(projected).toMatchObject({
|
|
code: "cleanup_incomplete",
|
|
message: "Target page-1 closed while REA released its session.",
|
|
remediation: { action: "Release session page-1, then retry." },
|
|
});
|
|
expect(details).toEqual({
|
|
...originalDetails,
|
|
operation: "capture_web_screenshot",
|
|
});
|
|
expect(details).toMatchObject({
|
|
resources: ["page-1"],
|
|
primary_reason: "target_changed",
|
|
});
|
|
});
|
|
|
|
it("derives default browser guidance for the attributed operation", () => {
|
|
const original = new BrowserObservationError(
|
|
"inspect_web_page",
|
|
"target_not_found",
|
|
);
|
|
expect(original.forOperation("inspect_web_page")).toBe(original);
|
|
|
|
const relabelled = original.forOperation("discover_webmcp_tools");
|
|
const projected = projectAnalysisError(relabelled);
|
|
expect(projected).toMatchObject({
|
|
code: "target_unavailable",
|
|
message: expect.stringMatching(/^discover_webmcp_tools could not find/),
|
|
details: {
|
|
operation: "discover_webmcp_tools",
|
|
reason: "target_not_found",
|
|
},
|
|
});
|
|
expect(projected.message).not.toContain("inspect_web_page");
|
|
expect(
|
|
projectAnalysisError(
|
|
new BrowserObservationError("inspect_web_page", "timeout").forOperation(
|
|
"observe_web_session",
|
|
),
|
|
),
|
|
).toMatchObject({
|
|
code: projectAnalysisError(
|
|
new BrowserObservationError("observe_web_session", "timeout"),
|
|
).code,
|
|
details: { operation: "observe_web_session", reason: "timeout" },
|
|
});
|
|
});
|
|
});
|
|
|
|
describe("analysis error projection: detached provider diagnostics", () => {
|
|
it.each(["adapter", "cancelled", "timeout"])(
|
|
"preserves detached, actionable diagnostics for %s errors",
|
|
(kind) => {
|
|
const diagnostics = {
|
|
runtime_root: "/tmp/rea-ghidra-fixture",
|
|
profile_digest: "a".repeat(64),
|
|
exit_code: 1,
|
|
};
|
|
const error =
|
|
kind === "adapter"
|
|
? new ProviderAdapterError("ghidra", "health", { diagnostics })
|
|
: kind === "cancelled"
|
|
? new AnalysisCancelledError("health", { diagnostics })
|
|
: new AnalysisTimeoutError("health", 250, { diagnostics });
|
|
|
|
expect(error.diagnostics).not.toBe(diagnostics);
|
|
expect(projectAnalysisError(error)).toMatchObject({
|
|
details: {
|
|
...(kind === "adapter" ? { provider_id: "ghidra" } : {}),
|
|
operation: "health",
|
|
diagnostics,
|
|
},
|
|
});
|
|
diagnostics.exit_code = 2;
|
|
expect(error.diagnostics?.exit_code).toBe(1);
|
|
},
|
|
);
|
|
});
|
|
|
|
describe("analysis error projection: unclassified failures", () => {
|
|
it.each([
|
|
["hopper", true],
|
|
["ghidra", true],
|
|
["ida", true],
|
|
["jadx", true],
|
|
["kuna", true],
|
|
["firmware", false],
|
|
["native-macos", false],
|
|
["rizin", false],
|
|
["rea-cdp-browser", false],
|
|
["rea", false],
|
|
] as const)(
|
|
"projects a %s failure (doctor support: %s)",
|
|
(providerId, doctor) => {
|
|
const projected = projectAnalysisError(
|
|
new ProviderAdapterError(providerId, "open_binary"),
|
|
);
|
|
|
|
expect(projected).toMatchObject({
|
|
code: "execution_failure",
|
|
details: { provider_id: providerId, operation: "open_binary" },
|
|
});
|
|
expect(projected.message).toContain(
|
|
`${providerId} provider could not complete open_binary`,
|
|
);
|
|
expect(projected.message).toContain("did not classify the failure");
|
|
expect(projected.message).not.toContain("doctor");
|
|
if (doctor)
|
|
expect(projected.remediation.action).toContain(
|
|
`\`rea doctor --provider ${providerId}\``,
|
|
);
|
|
else expect(projected.remediation.action).not.toContain("doctor");
|
|
},
|
|
);
|
|
|
|
it("keeps an authored provider message as the recovery", () => {
|
|
const projected = projectAnalysisError(
|
|
new ProviderAdapterError("firmware", "inspect", {
|
|
userMessage: "Select a smaller firmware image.",
|
|
}),
|
|
);
|
|
|
|
expect(projected.remediation.action).toBe(
|
|
"Select a smaller firmware image.",
|
|
);
|
|
});
|
|
});
|
|
|
|
describe("analysis error projection: caller contract", () => {
|
|
it("preserves the reported output constraint without exposing its internal cause", () => {
|
|
const projected = projectAnalysisError(
|
|
new AnalysisOutputError(
|
|
"extract_firmware",
|
|
"Report exceeded the declared byte budget",
|
|
{ cause: new Error("private internal cause") },
|
|
),
|
|
);
|
|
expect(projected).toMatchObject({
|
|
code: "unreadable_output",
|
|
details: {
|
|
operation: "extract_firmware",
|
|
reason: "Report exceeded the declared byte budget",
|
|
},
|
|
});
|
|
expect(JSON.stringify(projected)).not.toContain("private internal cause");
|
|
expect(projected.message).not.toContain("doctor");
|
|
expect(projected.remediation.action).not.toContain("doctor");
|
|
});
|
|
it("maps representative failures without exposing causes", () => {
|
|
const secretCause = new Error("secret-token");
|
|
const projected = [
|
|
projectAnalysisError(
|
|
new AnalysisInputError("overview", { cause: secretCause }),
|
|
),
|
|
projectAnalysisError(
|
|
new AnalysisCapabilityUnavailableError("fixture", "overview", "absent"),
|
|
),
|
|
projectAnalysisError(
|
|
new BinaryTargetError("/local/targets/app", "invalid", {
|
|
cause: secretCause,
|
|
}),
|
|
),
|
|
];
|
|
|
|
expect(projected.map(({ code }) => code)).toEqual([
|
|
"invalid_request",
|
|
"capability_unavailable",
|
|
"target_unavailable",
|
|
]);
|
|
expect(projected[1]).toMatchObject({
|
|
category: "unsupported_provider",
|
|
message: expect.stringMatching(/unavailable.*target/iu),
|
|
details: {
|
|
provider_id: "fixture",
|
|
operation: "overview",
|
|
reason: "absent",
|
|
},
|
|
});
|
|
expect(projected[2]).toMatchObject({
|
|
details: { path: "/local/targets/app" },
|
|
});
|
|
expect(JSON.stringify(projected)).not.toContain("secret-token");
|
|
});
|
|
|
|
it.each(["EACCES", "EPERM"] as const)(
|
|
"reports a target the host refused to read (%s) as access_denied",
|
|
(systemCode) => {
|
|
const denied = Object.assign(new Error("denied"), { code: systemCode });
|
|
const reason = `permission denied while reading target: ${systemCode}`;
|
|
expect(
|
|
projectAnalysisError(
|
|
new BinaryTargetError("/local/targets/app", reason, {
|
|
cause: denied,
|
|
}),
|
|
),
|
|
).toMatchObject({
|
|
code: "access_denied",
|
|
category: "unavailable",
|
|
retryable: false,
|
|
remediation: {
|
|
action: expect.stringMatching(
|
|
/read access.*selected path.*readable local file/iu,
|
|
),
|
|
},
|
|
details: {
|
|
path: "/local/targets/app",
|
|
reason,
|
|
system_code: systemCode,
|
|
boundary: "filesystem-read",
|
|
},
|
|
});
|
|
expect(
|
|
projectAnalysisError(
|
|
new BinaryTargetError("/local/targets/app", "missing", {
|
|
cause: Object.assign(new Error("missing"), { code: "ENOENT" }),
|
|
}),
|
|
),
|
|
).toMatchObject({ code: "target_unavailable" });
|
|
},
|
|
);
|
|
|
|
it("preserves exact artifact-integrity coordinates", () => {
|
|
expect(
|
|
projectAnalysisError(
|
|
new ArtifactOperationError("inventory_artifact", "integrity", {
|
|
logicalPath: "main.js",
|
|
declaredSha256: "a".repeat(64),
|
|
calculatedSha256: "b".repeat(64),
|
|
unpacked: true,
|
|
}),
|
|
),
|
|
).toMatchObject({
|
|
category: "integrity_mismatch",
|
|
details: {
|
|
logical_path: "main.js",
|
|
declared_sha256: "a".repeat(64),
|
|
calculated_sha256: "b".repeat(64),
|
|
unpacked: true,
|
|
},
|
|
});
|
|
});
|
|
|
|
it("describes a debugging-protocol failure instead of an unclassified one", () => {
|
|
const projected = projectAnalysisError(
|
|
new BrowserObservationError("inspect_web_page", "protocol_error"),
|
|
);
|
|
expect(projected).toMatchObject({
|
|
code: "unreadable_output",
|
|
details: { reason: "protocol_error" },
|
|
});
|
|
expect(projected.message).toMatch(/debugging-protocol exchange/u);
|
|
// The reason also covers REA's own protocol handling.
|
|
expect(projected.message).not.toMatch(/endpoint/u);
|
|
expect(projected.message).not.toContain("did not classify");
|
|
});
|
|
|
|
it("gives missing residual unknowns lookup-specific remediation", () => {
|
|
expect(
|
|
projectAnalysisError(new UnknownRegistryError("not-found")),
|
|
).toMatchObject({
|
|
// An unknown_id the session does not hold is the caller's selection.
|
|
code: "invalid_request",
|
|
category: "invalid_input",
|
|
message: expect.stringMatching(
|
|
/residual unknown.*does not exist.*unknown_id/iu,
|
|
),
|
|
remediation: {
|
|
action: expect.stringMatching(/unknown_id.*session.*retry/iu),
|
|
},
|
|
details: { reason: "not-found" },
|
|
});
|
|
});
|
|
});
|
|
|
|
describe("analysis error projection: evidence files", () => {
|
|
it.each([
|
|
["read", "missing", "invalid_request", "invalid_input"],
|
|
["read", "not-file", "invalid_request", "invalid_input"],
|
|
["write", "missing", "invalid_request", "invalid_input"],
|
|
["write", "exists", "invalid_request", "invalid_input"],
|
|
["write", "path-limit", "resource_constraint", "resource_constraint"],
|
|
[
|
|
"read",
|
|
"invalid-json",
|
|
"evidence_integrity_mismatch",
|
|
"execution_failure",
|
|
],
|
|
["read", "io", "execution_failure", "execution_failure"],
|
|
] as const)(
|
|
"classifies a %s %s evidence file by who can correct it",
|
|
(operation, reason, code, category) => {
|
|
const projected = projectAnalysisError(
|
|
new EvidenceFileError(operation, reason, { path: "/selected.json" }),
|
|
);
|
|
expect(projected).toMatchObject({
|
|
code,
|
|
category,
|
|
details: { operation, reason, path: "/selected.json" },
|
|
});
|
|
expect(analysisErrorProjectionSchema.parse(projected)).toEqual(projected);
|
|
},
|
|
);
|
|
});
|
|
|
|
it("retains the primary failure and both cleanup reports after a secondary failure", () => {
|
|
const callerReason = { caller: "stop" };
|
|
const primary = new AnalysisCancelledError("execute_rizin_command", {
|
|
cause: callerReason,
|
|
capturedOutput: {
|
|
stdout: "partial",
|
|
stderr: "diagnostic",
|
|
truncated: false,
|
|
},
|
|
cleanup: { reason: "writer remains", resources: ["writer"] },
|
|
});
|
|
const secondary = new AnalysisOutputError("open_binary", "cannot rebind", {
|
|
cleanup: { reason: "provider remains", resources: ["provider", "writer"] },
|
|
});
|
|
const enriched = primary.retainSecondaryFailure(secondary, {
|
|
rebind_error: projectAnalysisError(secondary),
|
|
});
|
|
expect(enriched).toBe(primary);
|
|
expect(enriched._tag).toBe("AnalysisCancelledError");
|
|
expect(enriched.cause).toBe(callerReason);
|
|
expect(enriched.secondaryFailures).toEqual([secondary]);
|
|
expect(projectAnalysisError(enriched)).toMatchObject({
|
|
code: "cleanup_incomplete",
|
|
details: {
|
|
execution_failure: "cancelled",
|
|
resources: ["writer"],
|
|
cleanup_reason: "writer remains",
|
|
captured_output: { stdout: "partial", stderr: "diagnostic" },
|
|
diagnostics: { rebind_error: { code: "cleanup_incomplete" } },
|
|
secondary_failures: [
|
|
{
|
|
code: "cleanup_incomplete",
|
|
details: {
|
|
operation: "open_binary",
|
|
reason: "cannot rebind",
|
|
resources: ["provider", "writer"],
|
|
cleanup_reason: "provider remains",
|
|
execution_failure: "unreadable_output",
|
|
},
|
|
},
|
|
],
|
|
},
|
|
});
|
|
|
|
releaseOwnedCleanup(secondary);
|
|
const later = projectAnalysisError(enriched);
|
|
expect(later).toMatchObject({
|
|
code: "cleanup_incomplete",
|
|
details: {
|
|
resources: ["writer"],
|
|
diagnostics: { rebind_error: { code: "cleanup_incomplete" } },
|
|
},
|
|
});
|
|
expect(later.details?.secondary_failures).toEqual([
|
|
expect.objectContaining({
|
|
code: "unreadable_output",
|
|
details: { operation: "open_binary", reason: "cannot rebind" },
|
|
}),
|
|
]);
|
|
});
|
|
|
|
it("retains secondary failures when the primary error has no cause", () => {
|
|
const primary = new AnalysisCancelledError("execute_rizin_command");
|
|
const secondary = new AnalysisOutputError("open_binary", "cannot rebind");
|
|
|
|
primary.retainSecondaryFailure(secondary, {});
|
|
|
|
expect(primary.cause).toBeUndefined();
|
|
expect(primary.secondaryFailures).toEqual([secondary]);
|
|
});
|
|
|
|
it("does not offer entry-policy retries for integrity refusals without a declared-versus-observed entry contradiction", () => {
|
|
const native = projectAnalysisError(
|
|
new ArtifactOperationError(
|
|
"inspect_artifact",
|
|
"integrity",
|
|
undefined,
|
|
"hdiutil verify failed: invalid checksum",
|
|
),
|
|
);
|
|
expect(native.category).toBe("integrity_mismatch");
|
|
expect(native.details).toMatchObject({
|
|
reason: "integrity",
|
|
detail: "hdiutil verify failed: invalid checksum",
|
|
});
|
|
expect(native.message).not.toContain("record-and-continue");
|
|
expect(native.remediation.action).not.toContain("record-and-continue");
|
|
});
|
|
|
|
it.each([undefined, false, true])(
|
|
"projects whether a refused request was sent (%s) and omits an unknown value",
|
|
(requestSent) => {
|
|
const details = projectAnalysisError(
|
|
new AnalysisResourceConstraintError(
|
|
"cutter_command",
|
|
"transport",
|
|
"Cutter request exceeds its frame limit",
|
|
{ max_request_bytes: 8192 },
|
|
requestSent === undefined ? {} : { requestSent },
|
|
),
|
|
).details;
|
|
if (requestSent === undefined)
|
|
expect(details).not.toHaveProperty("request_sent");
|
|
else expect(details).toMatchObject({ request_sent: requestSent });
|
|
},
|
|
);
|
|
|
|
it("preserves ordinary configuration recovery and projects a producer-owned session repair", () => {
|
|
const settings = [{ setting: "REA_EXAMPLE", constraint: "must be positive" }];
|
|
const original = projectAnalysisError(
|
|
new ConfigurationError("Invalid setting", { settings }),
|
|
);
|
|
const repairing = projectAnalysisError(
|
|
new ConfigurationError("Invalid setting", {
|
|
settings,
|
|
remediationAction: "Repair the running session, then retry.",
|
|
}),
|
|
);
|
|
expect(original.remediation.action).toMatch(
|
|
/correct.*REA_EXAMPLE.*environment.*shell.*MCP.*registration.*rerun/iu,
|
|
);
|
|
expect(repairing).toEqual({
|
|
...original,
|
|
remediation: { action: "Repair the running session, then retry." },
|
|
});
|
|
expect(repairing.code).toBe("configuration_invalid");
|
|
});
|
|
|
|
const continuingOnly = {
|
|
userMessage: "The fixture session remains usable.",
|
|
remediationAction: "Update the fixture runtime setting on this session.",
|
|
};
|
|
|
|
describe("analysis error projection: caller lifetime", () => {
|
|
it.each([
|
|
new AnalysisTimeoutError("procedure_pseudo_code", 5000, {
|
|
userMessage: "Only this request stopped.",
|
|
remediationAction: "Rerun with a longer launch deadline.",
|
|
continuingCallerGuidance: continuingOnly,
|
|
}),
|
|
new ProviderAdapterError("fixture", "procedure_pseudo_code", {
|
|
userMessage: "Only this request stopped.",
|
|
remediationAction: "Rerun with a longer launch deadline.",
|
|
continuingCallerGuidance: continuingOnly,
|
|
}),
|
|
new AnalysisResourceConstraintError(
|
|
"search_strings",
|
|
"cpu",
|
|
"Only this request stopped.",
|
|
null,
|
|
{
|
|
remediationAction: "Rerun with a longer launch deadline.",
|
|
continuingCallerGuidance: continuingOnly,
|
|
},
|
|
),
|
|
])(
|
|
"states continuing-session guidance only to a continuing caller ($_tag)",
|
|
(error) => {
|
|
expect(projectAnalysisError(error)).toMatchObject({
|
|
message:
|
|
"Only this request stopped. The fixture session remains usable.",
|
|
remediation: {
|
|
action: "Update the fixture runtime setting on this session.",
|
|
},
|
|
});
|
|
const invocation = projectAnalysisError(error, "invocation");
|
|
expect(invocation).toMatchObject({
|
|
code: projectAnalysisError(error).code,
|
|
message: "Only this request stopped.",
|
|
remediation: { action: "Rerun with a longer launch deadline." },
|
|
});
|
|
expect(analysisErrorUserMessage(error, "invocation")).toBe(
|
|
"Only this request stopped.",
|
|
);
|
|
expect(analysisErrorRemediationAction(error, "invocation")).toBe(
|
|
"Rerun with a longer launch deadline.",
|
|
);
|
|
},
|
|
);
|
|
|
|
it("re-scopes a limit for a finished invocation and keeps its enforced facts", () => {
|
|
const limit = analysisSettingLimit(
|
|
"regex.matching_deadline_ms",
|
|
250,
|
|
"setting",
|
|
);
|
|
const error = new AnalysisResourceConstraintError(
|
|
"search_strings",
|
|
"cpu",
|
|
"Regex matching exceeded its deadline.",
|
|
null,
|
|
{ limit },
|
|
);
|
|
expect(projectAnalysisError(error).details?.limit).toEqual(limit);
|
|
expect(projectAnalysisError(error, "invocation").details?.limit).toEqual({
|
|
setting: "regex.matching_deadline_ms",
|
|
effective_value: 250,
|
|
source: "setting",
|
|
unit: "ms",
|
|
minimum: limit.minimum,
|
|
maximum: limit.maximum,
|
|
agent_editable: true,
|
|
update_mode: "runtime",
|
|
pending_restart: false,
|
|
applies_at: "next_invocation",
|
|
scope: "rea-invocation",
|
|
environment: "REA_REGEX_MATCHING_DEADLINE_MS",
|
|
});
|
|
// The error keeps its runtime facts for other projections.
|
|
expect(error.limit).toEqual(limit);
|
|
});
|
|
|
|
it("names a per-call parameter, not an environment, when a setting has no launch seed", () => {
|
|
const error = new AnalysisTimeoutError("inspect_wasm_artifact", 100, {
|
|
limit: analysisSettingLimit("wasm.command_timeout_ms", 100, "request"),
|
|
});
|
|
expect(projectAnalysisError(error, "invocation").details?.limit).toEqual(
|
|
expect.objectContaining({
|
|
setting: "wasm.command_timeout_ms",
|
|
source: "request",
|
|
update_mode: "parameter",
|
|
parameter: "command_timeout_ms",
|
|
applies_at: "next_invocation",
|
|
scope: "rea-invocation",
|
|
}),
|
|
);
|
|
expect(
|
|
analysisSettingLimit("wasm.command_timeout_ms", 100, "runtime"),
|
|
).toMatchObject({ source: "runtime", update_mode: "runtime" });
|
|
expect(
|
|
analysisSettingLimit("javascript.max_heap_mb", 128, "request"),
|
|
).toMatchObject({
|
|
source: "request",
|
|
update_mode: "parameter",
|
|
parameter: "max_heap_mb",
|
|
});
|
|
expect(
|
|
analysisSettingLimit("javascript.analysis_timeout_ms", 1, "default"),
|
|
).toMatchObject({ source: "default", update_mode: "runtime" });
|
|
expect(
|
|
projectAnalysisError(error, "invocation").details?.limit,
|
|
).not.toHaveProperty("environment");
|
|
});
|
|
});
|
|
|
|
describe("analysis error projection: composed failures for a caller lifetime", () => {
|
|
it("projects a composed cleanup failure's children for the caller and keeps their history", () => {
|
|
const limit = analysisSettingLimit(
|
|
"regex.matching_deadline_ms",
|
|
250,
|
|
"setting",
|
|
);
|
|
const primary = new AnalysisTimeoutError("search_strings", 250, {
|
|
limit,
|
|
userMessage: "Only this request stopped.",
|
|
remediationAction: "Rerun with a longer launch deadline.",
|
|
continuingCallerGuidance: continuingOnly,
|
|
});
|
|
const cleanup = new ProviderCleanupError(
|
|
"fixture",
|
|
["fixture-process"],
|
|
{ reason: "exit unconfirmed" },
|
|
{ remediationAction: "Close the retained fixture process." },
|
|
);
|
|
const composed = analysisErrorWithCleanupFailure(
|
|
primary,
|
|
cleanup,
|
|
).bindCleanupDependencies([primary, cleanup]);
|
|
const child = (lifetime: AnalysisCallerLifetime) =>
|
|
projectAnalysisError(composed, lifetime).details;
|
|
expect(child("invocation")).toMatchObject({
|
|
limit: { scope: "rea-invocation" },
|
|
diagnostics: {
|
|
primary_error: projectAnalysisError(primary, "invocation"),
|
|
cleanup_error: { code: "cleanup_incomplete" },
|
|
},
|
|
});
|
|
expect(child("continuing")).toMatchObject({
|
|
limit: { scope: "rea-runtime" },
|
|
diagnostics: { primary_error: projectAnalysisError(primary) },
|
|
});
|
|
expect(projectAnalysisError(composed, "invocation").message).toBe(
|
|
"Only this request stopped.",
|
|
);
|
|
expect(projectAnalysisError(composed).message).toBe(
|
|
`Only this request stopped. ${continuingOnly.userMessage}`,
|
|
);
|
|
// After cleanup resolves, history keeps the composition-time snapshot.
|
|
releaseOwnedCleanup(cleanup);
|
|
for (const lifetime of ["continuing", "invocation"] as const)
|
|
expect(child(lifetime)?.cleanup_history).toMatchObject({
|
|
primary_error: projectAnalysisError(primary, lifetime),
|
|
cleanup_error: { code: "cleanup_incomplete" },
|
|
});
|
|
});
|
|
|
|
it("keeps caller lifetime for a secondary failure embedded in child diagnostics", () => {
|
|
const primary = new AnalysisInputError("read_bytes", undefined, []);
|
|
const secondary = new AnalysisTimeoutError("search_strings", 250, {
|
|
limit: analysisSettingLimit("regex.matching_deadline_ms", 250, "setting"),
|
|
continuingCallerGuidance: continuingOnly,
|
|
});
|
|
primary.retainSecondaryFailure(secondary);
|
|
const history = childFailureDiagnostics({
|
|
primary_error: { error: primary },
|
|
});
|
|
expect(history.diagnostics?.primary_error).toEqual(
|
|
projectAnalysisError(primary, "continuing"),
|
|
);
|
|
expect(history.invocationDiagnostics?.primary_error).toEqual(
|
|
projectAnalysisError(primary, "invocation"),
|
|
);
|
|
});
|
|
|
|
it("records one lifetime form when no child depends on the caller", () => {
|
|
const composed = analysisErrorWithCleanupFailure(
|
|
new AnalysisInputError("read_bytes", undefined, [
|
|
{ path: ["address"], reason: "invalid_format" },
|
|
]),
|
|
new ProviderCleanupError("fixture", ["fixture-process"], {}),
|
|
);
|
|
expect(composed.invocationDiagnostics).toBeUndefined();
|
|
expect(projectAnalysisError(composed, "invocation").details).toEqual(
|
|
projectAnalysisError(composed).details,
|
|
);
|
|
});
|
|
|
|
it("keeps the named integrity check when redaction rebuilds an artifact change", () => {
|
|
const error = new AnalysisArtifactChangedError(
|
|
"inspect_artifact",
|
|
"/target/secret-token.bin",
|
|
"member changed",
|
|
{ integrityCheck: "member-identity" },
|
|
);
|
|
const redacted = redactExplicitFailure(error, ["secret-token"]);
|
|
expect(redacted).not.toBe(error);
|
|
expect(projectAnalysisError(redacted)).toMatchObject({
|
|
code: "artifact_changed",
|
|
details: { integrity_check: "member-identity" },
|
|
});
|
|
});
|
|
|
|
it("keeps continuing guidance through redaction and redacts its text", () => {
|
|
const guidance = {
|
|
userMessage: "The session for secret-token remains usable.",
|
|
remediationAction: "Retry secret-token on this session.",
|
|
};
|
|
const error = new AnalysisTimeoutError("procedure_pseudo_code", 5000, {
|
|
userMessage: "Only this request stopped.",
|
|
remediationAction: "Rerun with a longer launch deadline.",
|
|
continuingCallerGuidance: guidance,
|
|
});
|
|
const redact = (text: string) => redactExplicitText(text, ["secret-token"]);
|
|
const redacted = redactExplicitFailure(error, ["secret-token"]);
|
|
expect(projectAnalysisError(redacted)).toMatchObject({
|
|
message: `Only this request stopped. ${redact(guidance.userMessage)}`,
|
|
remediation: { action: redact(guidance.remediationAction) },
|
|
});
|
|
expect(JSON.stringify(projectAnalysisError(redacted))).not.toContain(
|
|
"secret-token",
|
|
);
|
|
expect(projectAnalysisError(redacted, "invocation")).toMatchObject({
|
|
message: "Only this request stopped.",
|
|
remediation: { action: "Rerun with a longer launch deadline." },
|
|
});
|
|
});
|
|
});
|
|
|
|
describe("analysis error projection: provider-owned errors", () => {
|
|
class ExampleProviderError extends ProviderOwnedAnalysisError {
|
|
readonly _tag = providerErrorTag("ExampleProviderError");
|
|
constructor() {
|
|
super("Example provider stopped with internal state");
|
|
}
|
|
override ownPresentation(): AnalysisErrorOwnPresentation {
|
|
return {
|
|
code: "provider_unavailable",
|
|
category: "unavailable",
|
|
retry: "same_call",
|
|
message: "The example provider stopped.",
|
|
remediation: "Restart the example provider, then retry.",
|
|
details: { stage: "launch" },
|
|
};
|
|
}
|
|
}
|
|
|
|
it("projects a tag outside the domain set through the error's own presentation", () => {
|
|
const projection = projectAnalysisError(new ExampleProviderError());
|
|
expect(projection).toMatchObject({
|
|
code: "provider_unavailable",
|
|
category: "unavailable",
|
|
message: "The example provider stopped.",
|
|
remediation: { action: "Restart the example provider, then retry." },
|
|
details: { stage: "launch" },
|
|
});
|
|
expect(analysisErrorProjectionSchema.parse(projection)).toEqual(projection);
|
|
});
|
|
|
|
it("refuses an error whose tag is neither a domain tag nor provider-owned", () => {
|
|
class StrayError extends AnalysisError {
|
|
readonly _tag = "StrayError" as unknown as AnalysisError["_tag"];
|
|
}
|
|
expect(() => projectAnalysisError(new StrayError("stray"))).toThrow(
|
|
"Unknown analysis error tag",
|
|
);
|
|
});
|
|
});
|
|
|
|
describe("analysis error projection: composite argument observations", () => {
|
|
const left = { kind: "retained-evidence", evidence_id: "left" } as const;
|
|
const right = { kind: "retained-evidence", evidence_id: "right" } as const;
|
|
const own = { kind: "retained-evidence", evidence_id: "comparison" } as const;
|
|
|
|
it("keeps each argument's observation beside the failure's own partial", () => {
|
|
const failure = new AnalysisCancelledError("compare_managed_members", {
|
|
partialObservation: own,
|
|
})
|
|
.retainArgumentObservation("left", left)
|
|
.retainArgumentObservation("right", right)
|
|
.retainArgumentObservation("left", right);
|
|
|
|
expect(projectAnalysisError(failure).details).toMatchObject({
|
|
partial_observation: own,
|
|
argument_observations: [
|
|
{ argument: "left", observation: left },
|
|
{ argument: "right", observation: right },
|
|
],
|
|
});
|
|
expect(
|
|
projectAnalysisErrorWithoutPartialObservation(failure).details,
|
|
).not.toHaveProperty("argument_observations");
|
|
expect(
|
|
projectAnalysisError(
|
|
new AnalysisCancelledError("compare_managed_members"),
|
|
).details,
|
|
).not.toHaveProperty("argument_observations");
|
|
});
|
|
|
|
it("reads an inner failure's argument observations through cleanup before and after it settles", () => {
|
|
const primary = new AnalysisInputError(
|
|
"compare_managed_members",
|
|
).retainArgumentObservation("left", left);
|
|
const cleanup = new ProviderCleanupError("fixture", ["fixture-file"], {});
|
|
const composed = analysisErrorWithCleanupFailure(
|
|
primary,
|
|
cleanup,
|
|
).bindCleanupDependencies([cleanup]);
|
|
const expected = [{ argument: "left", observation: left }];
|
|
|
|
expect(projectAnalysisError(composed).details).toMatchObject({
|
|
cleanup: "incomplete",
|
|
argument_observations: expected,
|
|
});
|
|
expect(
|
|
projectAnalysisErrorWithoutPartialObservation(composed).details,
|
|
).not.toHaveProperty("argument_observations");
|
|
|
|
releaseOwnedCleanup(cleanup);
|
|
expect(
|
|
projectAnalysisError(composed).details?.argument_observations,
|
|
).toEqual(expected);
|
|
});
|
|
});
|
|
|
|
it("distinguishes file budgets from a size beyond the parameter maximum", () => {
|
|
const limit = {
|
|
control_kind: "parameter",
|
|
parameter: "max_file_bytes",
|
|
effective_value: 4,
|
|
minimum: 1,
|
|
maximum: 8,
|
|
default_value: 8,
|
|
unit: "bytes",
|
|
applies_at: "next_call",
|
|
} as const;
|
|
const bounded = parameterFileSizeConstraint(
|
|
"inspect_evtx_archive",
|
|
"File is too large",
|
|
limit,
|
|
8n,
|
|
);
|
|
expect(bounded.limit).toMatchObject({ required_value: 8 });
|
|
expect(bounded.fixedAllowance).toBeUndefined();
|
|
const tooLarge = parameterFileSizeConstraint(
|
|
"inspect_evtx_archive",
|
|
"File is too large",
|
|
limit,
|
|
9n,
|
|
);
|
|
expect(tooLarge.limit).toBeUndefined();
|
|
expect(tooLarge.fixedAllowance).toMatchObject({
|
|
kind: "input",
|
|
limit: 8,
|
|
unit: "bytes",
|
|
});
|
|
expect(projectAnalysisError(tooLarge).remediation.action).toContain(
|
|
"complete artifact",
|
|
);
|
|
});
|