refactor(rizin): share validated command requests across admission and execution

Resolve raw input at the application boundary and reuse its facts in both MCP admission paths. Keep cleanup, mutation retirement and execution ordering while retaining structured correction details for rejected commands.

Validation: 186 focused tests, source checks, docs conformance and duplication gate passed. The new compiled CLI regression fails on the main baseline. Provider process controls use deterministic fixtures; no real Rizin verification is claimed.
This commit is contained in:
morluto committed 2026-10-23 04:29:35 +08:00
1 parent 3e87889bfb
commit 46217c69bc
6 files changed
+102 -32

No files matched your search

@@ -23,6 +23,7 @@ import {
import { AnalysisOutputError } from "../../domain/analysisErrorCore.js";
import { ProviderAdapterError } from "../../domain/providerAdapterError.js";
import { createEvidence, type Evidence } from "../../domain/evidence.js";
import { analysisInputErrorFromIssues } from "../../domain/inputIssueProjection.js";
import type { JsonValue } from "../../domain/jsonValue.js";
import { err, ok, type Result } from "../../domain/result.js";
import {
@@ -30,6 +31,7 @@ import {
MAX_REVERSE_COMMAND_TIMEOUT_MS,
objdumpInputSchema,
rizinInputSchema,
type RizinInput,
} from "../../domain/reverseEngineering.js";
import type {
ReverseEngineeringCommandPort,
@@ -83,6 +85,23 @@ export type ReverseEngineeringServiceRuntimeOptions = Omit<
"commands" | "environment"
>;
/** Resolve one command for shared execution and adapter admission decisions. */
export const resolveRizinCommandRequest = (
input: unknown,
): Result<RizinInput, AnalysisInputError> => {
const parsed = rizinInputSchema.safeParse(input);
return parsed.success
? ok(parsed.data)
: err(
analysisInputErrorFromIssues(
"execute_rizin_command",
parsed.error.issues,
input,
{ cause: parsed.error },
),
);
};
/** Shared application workflow for caller-selected local analysis commands. */
export class ReverseEngineeringService {
readonly #environment: Readonly<NodeJS.ProcessEnv>;
@@ -234,18 +253,24 @@ export class ReverseEngineeringService {
rawInput: unknown,
options: { readonly signal?: AbortSignal } = {},
): Promise<Result<Evidence, AnalysisError>> {
const parsed = rizinInputSchema.safeParse(rawInput);
if (!parsed.success)
return Promise.resolve(
err(new AnalysisInputError("execute_rizin_command")),
);
const { command_timeout_ms: commandTimeoutMs, ...input } = parsed.data;
const parsed = resolveRizinCommandRequest(rawInput);
return parsed.ok
? this.executeRizinCommandValidated(parsed.value, options)
: Promise.resolve(parsed);
}
/** Execute owner-validated input after the adapter has admitted its target. */
executeRizinCommandValidated(
request: RizinInput,
options: { readonly signal?: AbortSignal } = {},
): Promise<Result<Evidence, AnalysisError>> {
const { command_timeout_ms: commandTimeoutMs, ...input } = request;
const command = this.#commands.rizin(input);
return this.#observe(
"execute_rizin_command",
command,
input.path,
commandTimeoutMs === undefined ? input : parsed.data,
commandTimeoutMs === undefined ? input : request,
options.signal,
commandTimeoutMs ?? DEFAULT_REVERSE_COMMAND_TIMEOUT_MS,
);
+1 -1
View File
@@ -90,4 +90,4 @@ export type ObjdumpSelection =
| z.input<typeof objdumpArtifactInputSchema>
| z.input<typeof objdumpDisassemblyInputSchema>
| z.input<typeof objdumpArchitecturesInputSchema>;
export type RizinInput = z.input<typeof rizinInputSchema>;
export type RizinInput = z.output<typeof rizinInputSchema>;
+14 -13
View File
@@ -6,7 +6,10 @@ import type {
} from "@modelcontextprotocol/server";
import type { Logger } from "pino";
import type { ReverseEngineeringService } from "../application/reverse/ReverseEngineeringService.js";
import {
resolveRizinCommandRequest,
type ReverseEngineeringService,
} from "../application/reverse/ReverseEngineeringService.js";
import type { EvidenceLookup } from "../application/EvidenceInputResolver.js";
import type { EvidenceWriter } from "../application/investigation/InvestigationRecordPort.js";
import type { Evidence } from "../domain/evidence.js";
@@ -16,7 +19,6 @@ import { logToolExecution } from "./toolLogging.js";
import { toolRegistrationOptions } from "./toolRegistrationOptions.js";
import type { ToolResultDelivery } from "./toolResult.js";
import type { WithAdmittedAnalysis } from "./analysisAdmission.js";
import { rizinInputSchema } from "../domain/reverseEngineering.js";
import { AnalysisOutputError } from "../domain/analysisErrorCore.js";
import type { AnalysisError } from "../domain/analysisErrorBase.js";
import type { Result } from "../domain/result.js";
@@ -81,18 +83,18 @@ export const registerReverseEngineeringTools = (
rizin.name,
toolRegistrationOptions(rizin),
async (input, context: ServerContext) => {
const parsed = rizinInputSchema.safeParse(input);
if (parsed.success && withAdmittedExternalMutation !== undefined) {
const parsed = resolveRizinCommandRequest(input);
if (parsed.ok && withAdmittedExternalMutation !== undefined) {
const cleanup = await service.retryCleanup(rizin.name);
if (!cleanup.ok)
return deliverFailure(cleanup.error, rizin, delivery, recordEvidence);
const admitted = await withAdmittedExternalMutation(
parsed.data.path,
parsed.value.path,
context.mcpReq.signal,
async () => {
invalidateSnapshotForExternalMutation?.();
return logToolExecution(logger, rizin.name, () =>
service.executeRizinCommand(parsed.data, {
service.executeRizinCommandValidated(parsed.value, {
signal: context.mcpReq.signal,
}),
);
@@ -143,15 +145,14 @@ export const registerReverseEngineeringTools = (
withAdmittedAnalysis,
delivery,
async () => {
if (
invalidateSnapshotForExternalMutation !== undefined &&
rizinInputSchema.safeParse(input).success
)
if (invalidateSnapshotForExternalMutation !== undefined && parsed.ok)
invalidateSnapshotForExternalMutation();
const result = await logToolExecution(logger, rizin.name, () =>
service.executeRizinCommand(input, {
signal: context.mcpReq.signal,
}),
parsed.ok
? service.executeRizinCommandValidated(parsed.value, {
signal: context.mcpReq.signal,
})
: Promise.resolve(parsed),
);
if (!result.ok)
return deliverFailure(