fix(providers): report provider-selection diagnostics when the selected provider refuses its profile (#4103)

* fix(providers): report provider-selection diagnostics when the selected provider refuses its profile

An explicitly selected provider that refuses the target during profile resolution now yields the target_unsupported selection error with its candidates and rejections, keeping the refusal's message and, when present, its specific recovery.

Fixes #3930

* fix(providers): keep refusal evidence and cleanup ownership on selection failures

A refusal with unconfirmed cleanup is returned unchanged; otherwise the selection failure carries the refusal's captured output, diagnostics, partial observation and cause. Registry tests cover the copied recovery and the cleanup guard.
This commit is contained in:
pablodiazjorge authored and GitHub committed 2026-10-23 02:31:23 +08:00
1 parent 3f35eacad2
commit 02e2c9cd7f
6 files changed
+248 -11

No files matched your search

@@ -15,6 +15,7 @@ import { createAnalysisProfile } from "../../domain/analysisProfile.js";
import type { BinaryTarget } from "../../domain/binaryTargetTypes.js";
import { ProviderAdapterError } from "../../domain/providerAdapterError.js";
import { ProviderCleanupError } from "../../domain/providerCleanupError.js";
import { AnalysisUnsupportedTargetError } from "../../domain/analysisErrorCore.js";
import { ProviderSelectionError } from "../../domain/providerSelectionError.js";
import { projectAnalysisError } from "../../domain/analysisErrorProjection.js";
import { AnalysisCancelledError } from "../../domain/analysisErrorCore.js";
@@ -607,6 +608,73 @@ const capability = (provider: ProviderIdentity): CapabilityDescriptor => ({
limitations: [],
});
describe("analysis provider registry: explicit profile refusals", () => {
const refusing = (refusal: AnalysisUnsupportedTargetError) => ({
...candidate("ghidra").provider,
resolveAnalysisProfile: () => Promise.resolve(err(refusal)),
});
it("keeps a refusing provider's reason, recovery and evidence in the selection failure", async () => {
const capturedOutput = {
stdout: "",
stderr: "x86-64 only",
truncated: false,
};
const refusal = new AnalysisUnsupportedTargetError(
"resolve_analysis_profile",
DATABASE_TARGET.path,
"The configured extension supports x86-64 only.",
{
remediationAction: "Omit the extension setting for this target.",
capturedOutput,
diagnostics: { extension: "fixture" },
},
);
const selected = await new AnalysisProviderRegistry([
refusing(refusal),
]).select(DATABASE_TARGET, "ghidra");
if (selected.ok) throw new Error("expected a profile refusal");
expect(selected.error).toBeInstanceOf(ProviderSelectionError);
expect(selected.error.cause).toBe(refusal);
expect(projectAnalysisError(selected.error)).toMatchObject({
code: "capability_unavailable",
message: refusal.message,
remediation: { action: "Omit the extension setting for this target." },
details: {
selection_reason: "target_unsupported",
requested_provider_id: "ghidra",
candidate_ids: ["ghidra"],
rejections: [
{
provider_id: "ghidra",
code: "target_format_unsupported",
reason: "The configured extension supports x86-64 only.",
},
],
captured_output: capturedOutput,
diagnostics: { extension: "fixture" },
},
});
});
it("returns a refusal with unconfirmed cleanup unchanged", async () => {
const refusal = new AnalysisUnsupportedTargetError(
"resolve_analysis_profile",
DATABASE_TARGET.path,
"The configured extension supports x86-64 only.",
{ cleanup: { reason: "probe close failed", resources: ["/tmp/probe"] } },
);
const selected = await new AnalysisProviderRegistry([
refusing(refusal),
]).select(DATABASE_TARGET, "ghidra");
if (selected.ok) throw new Error("expected a profile refusal");
expect(selected.error).toBe(refusal);
expect(projectAnalysisError(selected.error)).toMatchObject({
code: "cleanup_incomplete",
details: { cleanup: "incomplete", resources: ["/tmp/probe"] },
});
});
});
describe("profile resolution terminal facts", () => {
it.each([false, true])(
"preserves a physical profile failure across late caller abort (throws: %s)",
@@ -14,10 +14,14 @@ import {
} from "../../contracts/providerSelection.js";
import type { AnalysisProfileCommitment } from "../../domain/analysisProfile.js";
import type { BinaryTarget } from "../../domain/binaryTargetTypes.js";
import { AnalysisCancelledError } from "../../domain/analysisErrorCore.js";
import {
AnalysisCancelledError,
AnalysisUnsupportedTargetError,
} from "../../domain/analysisErrorCore.js";
import { AnalysisError } from "../../domain/analysisErrorBase.js";
import {
ProviderSelectionError,
type ProviderSelectionErrorOptions,
type ProviderSelectionFailureReason,
type ProviderSelectionRejection,
} from "../../domain/providerSelectionError.js";
@@ -593,9 +597,29 @@ export class AnalysisProviderRegistry {
);
if (!evaluation.ok) return evaluation;
const evaluated = evaluation.value;
if (evaluated.profileError !== undefined)
return err(evaluated.profileError);
const statuses = replaceCandidate(baseline, evaluated.status);
const refusal = evaluated.profileError;
// A selected provider refusing the target is a selection outcome that keeps the
// refusal's reason, recovery and evidence; unconfirmed cleanup stays with the refusal.
if (
refusal instanceof AnalysisUnsupportedTargetError &&
!refusal.cleanupIncomplete
)
return err(
selectionError("target_unsupported", providerId, statuses, {
refusal: {
userMessage: refusal.message,
...(refusal.remediationAction === undefined
? {}
: { remediationAction: refusal.remediationAction }),
capturedOutput: refusal.capturedOutput,
diagnostics: refusal.diagnostics,
partialObservation: refusal.partialObservation,
cause: refusal,
},
}),
);
if (refusal !== undefined) return err(refusal);
if (evaluated.profile === undefined)
return err(selectionError("provider_unavailable", providerId, statuses));
return ok(
@@ -818,9 +842,19 @@ const selectionError = (
options: {
readonly operation?: string;
readonly rejectionCandidates?: readonly AnalysisProviderCandidateStatus[];
readonly refusal?: Pick<
ProviderSelectionErrorOptions,
| "userMessage"
| "remediationAction"
| "capturedOutput"
| "diagnostics"
| "partialObservation"
| "cause"
>;
} = {},
): ProviderSelectionError =>
new ProviderSelectionError({
...options.refusal,
operation: options.operation ?? "open_binary",
reason,
requestedProviderId,
+5
View File
@@ -70,6 +70,11 @@ export const analysisErrorRemediationAction = (
return "Review details.reason, then retry the same analysis after the condition clears.";
if (error instanceof EvidenceFileError && error.published !== undefined)
return "Use the published file at details.published_path and resolve the reported staging cleanup failure.";
if (
error instanceof ProviderSelectionError &&
error.remediationAction !== undefined
)
return error.remediationAction;
if (error instanceof AnalysisSnapshotMismatchError)
return "Run analysis without this snapshot, then save a fresh snapshot using the intended artifact, provider, and analysis profile.";
if (error instanceof AnalysisUnsupportedTargetError)
@@ -282,6 +282,32 @@ it("distinguishes unsupported target formats from unavailable provider capabilit
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) => {
+16 -8
View File
@@ -2,6 +2,7 @@ import type { JsonValue } from "./jsonValue.js";
import {
AnalysisError,
type AnalysisCapturedOutput,
type AnalysisErrorOptions,
} from "./analysisErrorBase.js";
/** Stable failure classes for target-bound deep-provider selection. */
@@ -21,9 +22,14 @@ export interface ProviderSelectionRejection {
}
/** Complete typed context for one failed deep-provider selection. */
export interface ProviderSelectionErrorOptions {
readonly capturedOutput?: AnalysisCapturedOutput;
export interface ProviderSelectionErrorOptions extends Pick<
AnalysisErrorOptions,
"cause" | "diagnostics" | "partialObservation"
> {
readonly capturedOutput?: AnalysisCapturedOutput | undefined;
readonly userMessage?: string;
/** Recovery known to the provider that refused the target, when it has one. */
readonly remediationAction?: string;
readonly operation?: string;
readonly reason: ProviderSelectionFailureReason;
readonly requestedProviderId: string;
@@ -39,16 +45,17 @@ export class ProviderSelectionError extends AnalysisError {
readonly requestedProviderId: string;
readonly candidateIds: readonly string[];
readonly rejections: readonly ProviderSelectionRejection[];
readonly remediationAction: string | undefined;
override readonly userMessage: string;
constructor(options: ProviderSelectionErrorOptions) {
const operation = options.operation ?? "open_binary";
super(
providerSelectionDiagnostic(options),
options.capturedOutput === undefined
? undefined
: { capturedOutput: options.capturedOutput },
);
super(providerSelectionDiagnostic(options), {
capturedOutput: options.capturedOutput,
diagnostics: options.diagnostics,
partialObservation: options.partialObservation,
...(options.cause === undefined ? {} : { cause: options.cause }),
});
this.operation = operation;
this.reason = options.reason;
this.requestedProviderId = options.requestedProviderId;
@@ -57,6 +64,7 @@ export class ProviderSelectionError extends AnalysisError {
...rejection,
diagnostics: structuredClone(rejection.diagnostics),
}));
this.remediationAction = options.remediationAction;
this.userMessage =
options.userMessage ?? providerSelectionUserMessage(options);
}