refactor(binary): move production composition outside application workflows (#965)
This commit is contained in:
@@ -3,7 +3,8 @@ import { parseConfig } from "../config.js";
|
||||
import type { JsonValue } from "../domain/jsonValue.js";
|
||||
import { EnhancedTools } from "./EnhancedTools.js";
|
||||
import { executeFunctionAnalysisEvidence } from "./FunctionAnalysisEvidence.js";
|
||||
import { createBinarySession, createManagedBinarySession } from "./runtime.js";
|
||||
import type { DirectAnalysisDependencies } from "./DirectAnalysisDependencies.js";
|
||||
import type { BinarySession } from "./binary/BinarySession.js";
|
||||
import { silentLogger, type Logger } from "../logger.js";
|
||||
import { createEvidence } from "../domain/evidence.js";
|
||||
import type { Evidence } from "../domain/evidence.js";
|
||||
@@ -38,11 +39,6 @@ import {
|
||||
import type { AnalysisProviderSelector } from "../contracts/providerSelection.js";
|
||||
import { artifactInspectionResultSchema } from "../domain/artifactInspection.js";
|
||||
|
||||
export {
|
||||
runCapabilityStatus,
|
||||
runProviderStatus,
|
||||
} from "./DirectAnalysisStatus.js";
|
||||
|
||||
type DirectAnalysisTool =
|
||||
| "annotate_native_function"
|
||||
| "inspect_native_load_image"
|
||||
@@ -70,6 +66,7 @@ type DirectAnalysisTool =
|
||||
* retain a target or provider client for a subsequent command.
|
||||
*/
|
||||
export const runDirectAnalysis = async (
|
||||
dependencies: DirectAnalysisDependencies,
|
||||
path: string,
|
||||
tool: DirectAnalysisTool,
|
||||
arguments_: Readonly<Record<string, JsonValue>>,
|
||||
@@ -82,7 +79,7 @@ export const runDirectAnalysis = async (
|
||||
} = {},
|
||||
): Promise<JsonValue> =>
|
||||
withProcessCancellation(options.signal, (signal) =>
|
||||
runAnalysis(path, tool, arguments_, {
|
||||
runAnalysis(dependencies, path, tool, arguments_, {
|
||||
logger: options.logger ?? silentLogger,
|
||||
snapshotPath: options.snapshotPath,
|
||||
signal,
|
||||
@@ -97,6 +94,7 @@ export const runDirectAnalysis = async (
|
||||
|
||||
/** Execute one provider-native semantic operation with atomic provenance. */
|
||||
export const runProviderAnalysis = async (
|
||||
dependencies: DirectAnalysisDependencies,
|
||||
...[path, tool, arguments_, logger = silentLogger, signal]: readonly [
|
||||
path: string,
|
||||
tool: NativeToolName | ArtifactAnalysisOperation | ManagedToolName,
|
||||
@@ -106,9 +104,9 @@ export const runProviderAnalysis = async (
|
||||
]
|
||||
): Promise<JsonValue> =>
|
||||
isManagedToolName(tool)
|
||||
? runManagedProviderAnalysis(path, tool, signal)
|
||||
? runManagedProviderAnalysis(dependencies, path, tool, signal)
|
||||
: withProcessCancellation(signal, (operationSignal) =>
|
||||
runAnalysis(path, tool, arguments_, {
|
||||
runAnalysis(dependencies, path, tool, arguments_, {
|
||||
logger,
|
||||
snapshotPath: undefined,
|
||||
signal: operationSignal,
|
||||
@@ -117,12 +115,13 @@ export const runProviderAnalysis = async (
|
||||
|
||||
/** Execute managed metadata inspection in an isolated managed-only session. */
|
||||
export const runManagedProviderExecution = async (
|
||||
dependencies: DirectAnalysisDependencies,
|
||||
path: string,
|
||||
tool: ManagedToolName,
|
||||
signal?: AbortSignal,
|
||||
): Promise<Result<AnalysisExecution, AnalysisError>> =>
|
||||
withProcessCancellation(signal, async (operationSignal) => {
|
||||
const session = createManagedBinarySession();
|
||||
const session = dependencies.createManagedBinarySession();
|
||||
try {
|
||||
const opened = await session.open(path, { signal: operationSignal });
|
||||
if (!opened.ok) return opened;
|
||||
@@ -133,11 +132,17 @@ export const runManagedProviderExecution = async (
|
||||
});
|
||||
|
||||
const runManagedProviderAnalysis = async (
|
||||
dependencies: DirectAnalysisDependencies,
|
||||
path: string,
|
||||
tool: ManagedToolName,
|
||||
signal?: AbortSignal,
|
||||
): Promise<JsonValue> => {
|
||||
const execution = await runManagedProviderExecution(path, tool, signal);
|
||||
const execution = await runManagedProviderExecution(
|
||||
dependencies,
|
||||
path,
|
||||
tool,
|
||||
signal,
|
||||
);
|
||||
if (!execution.ok) return cliError(execution.error);
|
||||
const value = execution.value;
|
||||
return createEvidence(value.subject ?? undefined, value.provider, {
|
||||
@@ -151,6 +156,7 @@ const runManagedProviderAnalysis = async (
|
||||
};
|
||||
|
||||
const runAnalysis = async (
|
||||
dependencies: DirectAnalysisDependencies,
|
||||
path: string,
|
||||
tool:
|
||||
| NativeToolName
|
||||
@@ -177,7 +183,7 @@ const runAnalysis = async (
|
||||
const { logger, signal, snapshotPath } = options;
|
||||
const config = parseConfig(options.environment ?? process.env);
|
||||
if (!config.ok) return cliError(config.error);
|
||||
const session = createBinarySession(config.value, logger);
|
||||
const session = dependencies.createBinarySession(config.value, logger);
|
||||
try {
|
||||
const prepared = await prepareSnapshot({
|
||||
path,
|
||||
@@ -247,13 +253,13 @@ const runAnalysis = async (
|
||||
};
|
||||
|
||||
const canReplayDirectTool = (
|
||||
session: ReturnType<typeof createBinarySession>,
|
||||
tool: Parameters<typeof runAnalysis>[1],
|
||||
session: BinarySession,
|
||||
tool: Parameters<typeof runAnalysis>[2],
|
||||
): boolean =>
|
||||
tool !== "trace_native_ui_action" && session.allowsSnapshotReplay(tool);
|
||||
|
||||
const executeAnalysisTool = async (input: {
|
||||
readonly session: ReturnType<typeof createBinarySession>;
|
||||
readonly session: BinarySession;
|
||||
readonly openedTarget: BinaryTarget;
|
||||
readonly tool:
|
||||
| NativeToolName
|
||||
@@ -372,7 +378,7 @@ const prepareSnapshot = async (options: {
|
||||
};
|
||||
|
||||
const replayProviderFor = (
|
||||
session: ReturnType<typeof createBinarySession>,
|
||||
session: BinarySession,
|
||||
tool:
|
||||
| NativeToolName
|
||||
| ArtifactAnalysisOperation
|
||||
@@ -384,7 +390,7 @@ const replayProviderFor = (
|
||||
: session.providerIdentity(tool);
|
||||
|
||||
const analysisProfileForEvidence = (
|
||||
session: ReturnType<typeof createBinarySession>,
|
||||
session: BinarySession,
|
||||
tool:
|
||||
| NativeToolName
|
||||
| ArtifactAnalysisOperation
|
||||
|
||||
@@ -0,0 +1,12 @@
|
||||
import type { AppConfig } from "../config.js";
|
||||
import type { Logger } from "../logger.js";
|
||||
import type { BinarySession } from "./binary/BinarySession.js";
|
||||
|
||||
/** Existing one-shot session factories supplied by the production boundary. */
|
||||
export interface DirectAnalysisDependencies {
|
||||
readonly createBinarySession: (
|
||||
config: AppConfig,
|
||||
logger: Logger,
|
||||
) => BinarySession;
|
||||
readonly createManagedBinarySession: () => BinarySession;
|
||||
}
|
||||
@@ -3,15 +3,16 @@ import { projectAnalysisError } from "../domain/analysisErrorProjection.js";
|
||||
import { jsonObjectSchema, type JsonValue } from "../domain/jsonValue.js";
|
||||
import { createServerIdentity } from "../serverIdentity.js";
|
||||
import { silentLogger, type Logger } from "../logger.js";
|
||||
import { createBinarySession } from "./runtime.js";
|
||||
import type { DirectAnalysisDependencies } from "./DirectAnalysisDependencies.js";
|
||||
|
||||
const runSessionStatus = async (
|
||||
dependencies: Pick<DirectAnalysisDependencies, "createBinarySession">,
|
||||
logger: Logger = silentLogger,
|
||||
environment: Readonly<Record<string, string | undefined>> = process.env,
|
||||
): Promise<JsonValue> => {
|
||||
const config = parseConfig(environment);
|
||||
if (!config.ok) return { error: projectAnalysisError(config.error) };
|
||||
const session = createBinarySession(config.value, logger);
|
||||
const session = dependencies.createBinarySession(config.value, logger);
|
||||
try {
|
||||
return {
|
||||
...jsonObjectSchema.parse(session.status()),
|
||||
@@ -26,12 +27,14 @@ const runSessionStatus = async (
|
||||
|
||||
/** List binary-session provider candidates and auxiliary operation availability. */
|
||||
export const runProviderStatus = (
|
||||
dependencies: Pick<DirectAnalysisDependencies, "createBinarySession">,
|
||||
logger: Logger = silentLogger,
|
||||
environment: Readonly<Record<string, string | undefined>> = process.env,
|
||||
) => runSessionStatus(logger, environment);
|
||||
) => runSessionStatus(dependencies, logger, environment);
|
||||
|
||||
/** List binary-session operation descriptors; this is not the full MCP catalog. */
|
||||
export const runCapabilityStatus = (
|
||||
dependencies: Pick<DirectAnalysisDependencies, "createBinarySession">,
|
||||
logger: Logger = silentLogger,
|
||||
environment: Readonly<Record<string, string | undefined>> = process.env,
|
||||
) => runSessionStatus(logger, environment);
|
||||
) => runSessionStatus(dependencies, logger, environment);
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
import { z } from "incur";
|
||||
|
||||
import { runProviderAnalysis } from "../application/DirectAnalysis.js";
|
||||
import { runProviderAnalysis } from "../composition/directAnalysis.js";
|
||||
import { createArtifactExtractionDestination } from "../application/ArtifactExtractionDestination.js";
|
||||
import { logCliCommand } from "../cliLogging.js";
|
||||
import { CLI_COMMANDS } from "../cliCommandNames.js";
|
||||
|
||||
@@ -2,7 +2,7 @@ import { z } from "incur";
|
||||
import { stat } from "node:fs/promises";
|
||||
import { resolve } from "node:path";
|
||||
|
||||
import { runDirectAnalysis } from "../application/DirectAnalysis.js";
|
||||
import { runDirectAnalysis } from "../composition/directAnalysis.js";
|
||||
import { CLI_COMMANDS } from "../cliCommandNames.js";
|
||||
import { logCliCommand } from "../cliLogging.js";
|
||||
import type { Logger } from "../logger.js";
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
import { z } from "incur";
|
||||
|
||||
import { runDirectAnalysis } from "../application/DirectAnalysis.js";
|
||||
import { runDirectAnalysis } from "../composition/directAnalysis.js";
|
||||
import { CLI_COMMANDS } from "../cliCommandNames.js";
|
||||
import { logCliCommand } from "../cliLogging.js";
|
||||
import type { Logger } from "../logger.js";
|
||||
|
||||
@@ -3,7 +3,7 @@ import { z } from "incur";
|
||||
import { compareManagedMemberPaths } from "../application/managed/ManagedMemberComparisonService.js";
|
||||
import { verifyManagedNativeBoundariesEvidence } from "../application/managed/ManagedNativeVerificationService.js";
|
||||
import { importManagedReconstructionEvidence } from "../application/managed/ManagedReconstructionService.js";
|
||||
import { runProviderAnalysis } from "../application/DirectAnalysis.js";
|
||||
import { runProviderAnalysis } from "../composition/directAnalysis.js";
|
||||
import { parseCliJsonInput } from "../cliJsonInput.js";
|
||||
import { projectAnalysisError } from "../domain/analysisErrorProjection.js";
|
||||
import { CLI_COMMANDS } from "../cliCommandNames.js";
|
||||
|
||||
@@ -5,7 +5,7 @@ import {
|
||||
runCapabilityStatus,
|
||||
runProviderAnalysis,
|
||||
runProviderStatus,
|
||||
} from "../application/DirectAnalysis.js";
|
||||
} from "../composition/directAnalysis.js";
|
||||
import { importReferenceSource } from "../application/ReferenceSourceImport.js";
|
||||
import { projectReferenceSourceImportError } from "../application/ReferenceSourceImportTypes.js";
|
||||
import { parseConfig } from "../config.js";
|
||||
|
||||
@@ -7,7 +7,7 @@ import { describe, expect, it } from "vitest";
|
||||
import { createCli } from "./cli.js";
|
||||
import { createSystemDoctorHost } from "./doctorRuntime.js";
|
||||
import { captureProcessScenarioFile } from "./application/ProcessCli.js";
|
||||
import { runCapabilityStatus } from "./application/DirectAnalysisStatus.js";
|
||||
import { runCapabilityStatus } from "./composition/directAnalysis.js";
|
||||
import { probeProcessCaptureCapability } from "./application/ProcessHarness.js";
|
||||
import { parseEvidence } from "./domain/evidence.js";
|
||||
import { parseProcessCapture } from "./domain/processCapture.js";
|
||||
|
||||
@@ -1,15 +1,15 @@
|
||||
import type { AppConfig } from "../config.js";
|
||||
import type { BinarySession } from "./binary/BinarySession.js";
|
||||
import type { BinarySession } from "../application/binary/BinarySession.js";
|
||||
import { HopperProvider } from "../hopper/HopperProvider.js";
|
||||
import { GhidraProvider } from "../ghidra/GhidraProvider.js";
|
||||
import { IdaProvider } from "../ida/IdaProvider.js";
|
||||
import { silentLogger, type Logger } from "../logger.js";
|
||||
import { auxiliaryAnalysisProviderDeclarations } from "../composition/auxiliaryAnalysisProviders.js";
|
||||
import { AnalysisProviderRegistry } from "./binary/AnalysisProviderRegistry.js";
|
||||
import { composeBinarySession } from "./binary/BinarySessionComposition.js";
|
||||
import { LazyAnalysisProvider } from "./binary/LazyAnalysisProvider.js";
|
||||
import { auxiliaryAnalysisProviderDeclarations } from "./auxiliaryAnalysisProviders.js";
|
||||
import { AnalysisProviderRegistry } from "../application/binary/AnalysisProviderRegistry.js";
|
||||
import { composeBinarySession } from "../application/binary/BinarySessionComposition.js";
|
||||
import { LazyAnalysisProvider } from "../application/binary/LazyAnalysisProvider.js";
|
||||
import { ManagedStaticProvider } from "../dotnet/ManagedStaticProvider.js";
|
||||
import { SessionProviderRouter } from "./binary/SessionProviderRouter.js";
|
||||
import { SessionProviderRouter } from "../application/binary/SessionProviderRouter.js";
|
||||
|
||||
/**
|
||||
* Compose the target-switching runtime shared directly by CLI and MCP adapters.
|
||||
@@ -0,0 +1,46 @@
|
||||
import {
|
||||
runDirectAnalysis as executeDirectAnalysis,
|
||||
runProviderAnalysis as executeProviderAnalysis,
|
||||
runManagedProviderExecution as executeManagedProvider,
|
||||
} from "../application/DirectAnalysis.js";
|
||||
import {
|
||||
runCapabilityStatus as executeCapabilityStatus,
|
||||
runProviderStatus as executeProviderStatus,
|
||||
} from "../application/DirectAnalysisStatus.js";
|
||||
import type { DirectAnalysisDependencies } from "../application/DirectAnalysisDependencies.js";
|
||||
import { createBinarySession, createManagedBinarySession } from "./binary.js";
|
||||
|
||||
const dependencies: DirectAnalysisDependencies = {
|
||||
createBinarySession,
|
||||
createManagedBinarySession,
|
||||
};
|
||||
|
||||
/** Run one isolated binary operation through the production session factory. */
|
||||
export const runDirectAnalysis = executeDirectAnalysis.bind(
|
||||
undefined,
|
||||
dependencies,
|
||||
);
|
||||
|
||||
/** Run one isolated native, artifact or managed operation. */
|
||||
export const runProviderAnalysis = executeProviderAnalysis.bind(
|
||||
undefined,
|
||||
dependencies,
|
||||
);
|
||||
|
||||
/** Execute managed metadata without constructing native provider candidates. */
|
||||
export const runManagedProviderExecution = executeManagedProvider.bind(
|
||||
undefined,
|
||||
dependencies,
|
||||
);
|
||||
|
||||
/** Report production provider candidates without opening a target. */
|
||||
export const runProviderStatus = executeProviderStatus.bind(
|
||||
undefined,
|
||||
dependencies,
|
||||
);
|
||||
|
||||
/** Report production session operations without opening a target. */
|
||||
export const runCapabilityStatus = executeCapabilityStatus.bind(
|
||||
undefined,
|
||||
dependencies,
|
||||
);
|
||||
+1
-1
@@ -6,7 +6,7 @@ import { realpathSync } from "node:fs";
|
||||
import { serveStdio } from "@modelcontextprotocol/server/stdio";
|
||||
|
||||
import { parseConfig } from "./config.js";
|
||||
import { createBinarySession } from "./application/runtime.js";
|
||||
import { createBinarySession } from "./composition/binary.js";
|
||||
import { createLogger } from "./logger.js";
|
||||
import { projectAnalysisError } from "./domain/analysisErrorProjection.js";
|
||||
import type { RuntimeDependencies } from "./main/types.js";
|
||||
|
||||
@@ -9,7 +9,7 @@ import { AnalysisInputError } from "../domain/analysisErrorCore.js";
|
||||
import { err } from "../domain/result.js";
|
||||
import type { Logger } from "../logger.js";
|
||||
import { registerEvidenceTools } from "./registerEvidenceTools.js";
|
||||
import { runManagedProviderExecution } from "../application/DirectAnalysis.js";
|
||||
import { runManagedProviderExecution } from "../composition/directAnalysis.js";
|
||||
import { isManagedToolName } from "../contracts/managed/managedToolContracts.js";
|
||||
|
||||
/** Register execution-free managed PE/CLI inspection. */
|
||||
|
||||
Reference in New Issue
Block a user