feat(session): add explicit provider registry and target binding
Introduce deterministic deep-provider discovery and immutable per-target routing while retaining disjoint artifact and native capability families. Expose provider selection consistently through MCP, CLI, environment configuration, session status, prompt completion, typed diagnostics, and generated documentation. Preserve exact provider/profile provenance without request racing or transparent fallback. Cover overlapping providers, ambiguity, rejection details, cancellation, restoration, package installation, and real Hopper target switching.
This commit is contained in:
@@ -10,13 +10,18 @@ import type {
|
||||
BinaryArchitecture,
|
||||
BinaryTarget,
|
||||
} from "../domain/binaryTarget.js";
|
||||
import { ProviderAdapterError, type AnalysisError } from "../domain/errors.js";
|
||||
import {
|
||||
AnalysisCancelledError,
|
||||
ProviderAdapterError,
|
||||
type AnalysisError,
|
||||
} from "../domain/errors.js";
|
||||
import { err, ok, type Result } from "../domain/result.js";
|
||||
|
||||
interface HopperProfileOptions {
|
||||
readonly launcherPath: string;
|
||||
readonly loaderArgsOverride: readonly string[];
|
||||
readonly provider: ProviderIdentity;
|
||||
readonly signal?: AbortSignal;
|
||||
}
|
||||
|
||||
/** Resolve Hopper open semantics without placing its CLI flags in BinaryTarget. */
|
||||
@@ -33,12 +38,14 @@ export const resolveHopperAnalysisProfile = async (
|
||||
? derived.value
|
||||
: [...options.loaderArgsOverride];
|
||||
const compatibility = { loaderArgs: [...loaderArgs] };
|
||||
const launcherDigest = await sha256File(options.launcherPath);
|
||||
if (launcherDigest === undefined) return ok({ profile: null, compatibility });
|
||||
const launcherDigest = await sha256File(options.launcherPath, options.signal);
|
||||
if (!launcherDigest.ok) return launcherDigest;
|
||||
if (launcherDigest.value === undefined)
|
||||
return ok({ profile: null, compatibility });
|
||||
const provider = {
|
||||
id: options.provider.id,
|
||||
name: options.provider.name,
|
||||
version: `launcher-sha256:${launcherDigest}`,
|
||||
version: `launcher-sha256:${launcherDigest.value}`,
|
||||
};
|
||||
return ok({
|
||||
profile: createAnalysisProfile(provider, 1, {
|
||||
@@ -103,12 +110,31 @@ const hopperArchitectureFlag = (architecture: BinaryArchitecture): string => {
|
||||
}
|
||||
};
|
||||
|
||||
const sha256File = async (path: string): Promise<string | undefined> => {
|
||||
const sha256File = async (
|
||||
path: string,
|
||||
signal: AbortSignal | undefined,
|
||||
): Promise<Result<string | undefined, AnalysisCancelledError>> => {
|
||||
if (signalIsAborted(signal))
|
||||
return err(new AnalysisCancelledError("open_binary"));
|
||||
const stream = createReadStream(path);
|
||||
const onAbort = (): void => {
|
||||
stream.destroy();
|
||||
};
|
||||
signal?.addEventListener("abort", onAbort, { once: true });
|
||||
try {
|
||||
const hash = createHash("sha256");
|
||||
for await (const chunk of createReadStream(path)) hash.update(chunk);
|
||||
return hash.digest("hex");
|
||||
for await (const chunk of stream) hash.update(chunk);
|
||||
return signalIsAborted(signal)
|
||||
? err(new AnalysisCancelledError("open_binary"))
|
||||
: ok(hash.digest("hex"));
|
||||
} catch {
|
||||
return undefined;
|
||||
return signalIsAborted(signal)
|
||||
? err(new AnalysisCancelledError("open_binary"))
|
||||
: ok(undefined);
|
||||
} finally {
|
||||
signal?.removeEventListener("abort", onAbort);
|
||||
}
|
||||
};
|
||||
|
||||
const signalIsAborted = (signal: AbortSignal | undefined): boolean =>
|
||||
signal?.aborted === true;
|
||||
|
||||
@@ -1,11 +1,15 @@
|
||||
import { fileURLToPath } from "node:url";
|
||||
import { accessSync, constants } from "node:fs";
|
||||
|
||||
import { createAnalysisExecution } from "../application/AnalysisProvider.js";
|
||||
import type {
|
||||
AnalysisClient,
|
||||
AnalysisProvider,
|
||||
AnalysisProfileResolutionOptions,
|
||||
AnalysisProviderCandidate,
|
||||
CapabilityDescriptor,
|
||||
ProviderAvailability,
|
||||
ProviderIdentity,
|
||||
ProviderTargetSupport,
|
||||
} from "../application/AnalysisProvider.js";
|
||||
import type { AnalysisProfileCommitment } from "../domain/analysisProfile.js";
|
||||
import type { AppConfig } from "../config.js";
|
||||
@@ -81,7 +85,7 @@ const CAPABILITIES: readonly CapabilityDescriptor[] = Object.freeze(
|
||||
);
|
||||
|
||||
/** Concrete analysis provider backed by REA's private Hopper bridge. */
|
||||
export class HopperProvider implements AnalysisProvider {
|
||||
export class HopperProvider implements AnalysisProviderCandidate {
|
||||
constructor(
|
||||
private readonly config: AppConfig,
|
||||
private readonly logger: Logger,
|
||||
@@ -95,11 +99,87 @@ export class HopperProvider implements AnalysisProvider {
|
||||
return CAPABILITIES;
|
||||
}
|
||||
|
||||
resolveAnalysisProfile(target: BinaryTarget) {
|
||||
inspectAvailability(): ProviderAvailability {
|
||||
const diagnostics = {
|
||||
launcher_path: this.config.hopperLauncherPath,
|
||||
platform: process.platform,
|
||||
};
|
||||
if (process.platform !== "darwin" && process.platform !== "linux")
|
||||
return {
|
||||
status: "unavailable",
|
||||
code: "unsupported_host",
|
||||
reason: `Hopper integration is not supported on ${process.platform}.`,
|
||||
diagnostics,
|
||||
};
|
||||
try {
|
||||
accessSync(this.config.hopperLauncherPath, constants.X_OK);
|
||||
return {
|
||||
status: "available",
|
||||
code: null,
|
||||
reason: null,
|
||||
diagnostics,
|
||||
};
|
||||
} catch {
|
||||
return {
|
||||
status: "unavailable",
|
||||
code: "executable_missing",
|
||||
reason: `Hopper launcher is missing or not executable: ${this.config.hopperLauncherPath}`,
|
||||
diagnostics,
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
inspectTargetSupport(target: BinaryTarget): ProviderTargetSupport {
|
||||
const diagnostics = {
|
||||
target_kind: target.kind,
|
||||
target_format: target.format,
|
||||
architecture: target.architecture ?? null,
|
||||
};
|
||||
if (target.kind !== "executable" && target.kind !== "database")
|
||||
return {
|
||||
status: "unsupported",
|
||||
code: "target_kind_unsupported",
|
||||
reason: `Hopper cannot directly analyze ${target.kind} targets.`,
|
||||
diagnostics,
|
||||
};
|
||||
if (target.kind === "database")
|
||||
return {
|
||||
status: "supported",
|
||||
code: null,
|
||||
reason: null,
|
||||
diagnostics,
|
||||
};
|
||||
if (!["mach-o", "elf", "pe"].includes(target.format))
|
||||
return {
|
||||
status: "unsupported",
|
||||
code: "target_format_unsupported",
|
||||
reason: `Hopper cannot open ${target.format} through this adapter.`,
|
||||
diagnostics,
|
||||
};
|
||||
if (target.architecture === undefined)
|
||||
return {
|
||||
status: "unsupported",
|
||||
code: "architecture_unsupported",
|
||||
reason: "Hopper requires a supported concrete target architecture.",
|
||||
diagnostics,
|
||||
};
|
||||
return {
|
||||
status: "supported",
|
||||
code: null,
|
||||
reason: null,
|
||||
diagnostics,
|
||||
};
|
||||
}
|
||||
|
||||
resolveAnalysisProfile(
|
||||
target: BinaryTarget,
|
||||
options?: AnalysisProfileResolutionOptions,
|
||||
) {
|
||||
return resolveHopperAnalysisProfile(target, {
|
||||
launcherPath: this.config.hopperLauncherPath,
|
||||
loaderArgsOverride: this.config.hopperLoaderArgs,
|
||||
provider: IDENTITY,
|
||||
...(options?.signal === undefined ? {} : { signal: options.signal }),
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user