fix(hopper): add bounded analysis recovery
This commit is contained in:
@@ -0,0 +1,22 @@
|
||||
import type { ProgressReporter } from "../application/ProgressReporter.js";
|
||||
|
||||
/** Report one lifecycle operation start through the shared monotonic boundary. */
|
||||
export const reportLifecycleStart = (
|
||||
progress: ProgressReporter,
|
||||
phase: string,
|
||||
message = "started",
|
||||
): Promise<void> => progress.report({ phase, completed: 0, total: 1, message });
|
||||
|
||||
/** Report one lifecycle operation's terminal disposition. */
|
||||
export const reportLifecycleEnd = (
|
||||
progress: ProgressReporter,
|
||||
phase: string,
|
||||
succeeded: boolean,
|
||||
): Promise<void> =>
|
||||
progress.report({
|
||||
phase,
|
||||
completed: 1,
|
||||
total: 1,
|
||||
message: succeeded ? "completed" : "failed",
|
||||
terminal: true,
|
||||
});
|
||||
@@ -0,0 +1,22 @@
|
||||
import type { PermissionAuthority } from "../application/PermissionAuthority.js";
|
||||
import {
|
||||
AnalysisProtocolError,
|
||||
PermissionRequiredError,
|
||||
type AnalysisError,
|
||||
} from "../domain/errors.js";
|
||||
import { err, type Result } from "../domain/result.js";
|
||||
|
||||
/** Preserve a typed permission denial or contain an unexpected authority error. */
|
||||
export const permissionFailure = (
|
||||
failure: Awaited<ReturnType<PermissionAuthority["authorize"]>>,
|
||||
): Result<never, AnalysisError> => {
|
||||
if (failure.ok)
|
||||
return err(new AnalysisProtocolError("Expected a denied permission"));
|
||||
return err(
|
||||
failure.error instanceof PermissionRequiredError
|
||||
? failure.error
|
||||
: new AnalysisProtocolError(failure.error.message, {
|
||||
cause: failure.error,
|
||||
}),
|
||||
);
|
||||
};
|
||||
@@ -0,0 +1,93 @@
|
||||
import { writeAnalysisSnapshot } from "../application/AnalysisSnapshotFiles.js";
|
||||
import { closeBinaryInputSchema } from "../contracts/sessionLifecycleInputs.js";
|
||||
import { ok } from "../domain/result.js";
|
||||
import {
|
||||
reportLifecycleEnd,
|
||||
reportLifecycleStart,
|
||||
} from "./lifecycleProgress.js";
|
||||
import { logToolExecution } from "./toolLogging.js";
|
||||
import { toCallToolResult } from "./toolResult.js";
|
||||
import { mcpProgressReporter } from "./mcpProgress.js";
|
||||
import { permissionFailure } from "./permissionFailure.js";
|
||||
import type { LifecycleToolRegistration } from "./registerSessionTools.js";
|
||||
import { safeParseToolInput } from "./toolInputValidation.js";
|
||||
import { toolRegistrationOptions } from "./toolRegistrationOptions.js";
|
||||
|
||||
/** Register provider cleanup and optional pre-close snapshot persistence. */
|
||||
export const registerCloseLifecycleTool = ({
|
||||
server,
|
||||
session,
|
||||
logger,
|
||||
contracts: [, closeContract],
|
||||
snapshotFilePolicy,
|
||||
permissionAuthority,
|
||||
}: LifecycleToolRegistration): void => {
|
||||
server.registerTool(
|
||||
closeContract.name,
|
||||
toolRegistrationOptions(closeContract),
|
||||
async (input, context) => {
|
||||
const parsedInput = safeParseToolInput(
|
||||
closeBinaryInputSchema,
|
||||
input,
|
||||
closeContract.name,
|
||||
);
|
||||
if (!parsedInput.ok) return toCallToolResult(parsedInput, closeContract);
|
||||
const parsed = parsedInput.value;
|
||||
const progress = mcpProgressReporter(context);
|
||||
if (parsed.snapshot_path === undefined) {
|
||||
await reportLifecycleStart(progress, closeContract.name);
|
||||
const closed = await logToolExecution(logger, closeContract.name, () =>
|
||||
session.close({ progress }),
|
||||
);
|
||||
await reportLifecycleEnd(progress, closeContract.name, closed.ok);
|
||||
server.sendToolListChanged();
|
||||
return toCallToolResult(closed, closeContract);
|
||||
}
|
||||
if (permissionAuthority !== undefined) {
|
||||
const authorized = await permissionAuthority.authorize(
|
||||
{
|
||||
capability: "snapshot_write",
|
||||
roots: [parsed.snapshot_path],
|
||||
executables: [],
|
||||
environment_names: [],
|
||||
network: "none",
|
||||
mount: false,
|
||||
operation_identity: `close_binary:snapshot:${parsed.snapshot_path}`,
|
||||
},
|
||||
"write",
|
||||
);
|
||||
if (!authorized.ok)
|
||||
return toCallToolResult(permissionFailure(authorized), closeContract);
|
||||
}
|
||||
const snapshot = session.exportAnalysisSnapshot();
|
||||
if (!snapshot.ok) return toCallToolResult(snapshot, closeContract);
|
||||
const written = await writeAnalysisSnapshot(
|
||||
snapshot.value,
|
||||
parsed.snapshot_path,
|
||||
parsed.overwrite,
|
||||
snapshotFilePolicy,
|
||||
);
|
||||
if (!written.ok) return toCallToolResult(written, closeContract);
|
||||
await reportLifecycleStart(
|
||||
progress,
|
||||
closeContract.name,
|
||||
"snapshot written; closing provider",
|
||||
);
|
||||
const closed = await logToolExecution(logger, closeContract.name, () =>
|
||||
session.close({ progress }),
|
||||
);
|
||||
await reportLifecycleEnd(progress, closeContract.name, closed.ok);
|
||||
server.sendToolListChanged();
|
||||
return closed.ok
|
||||
? toCallToolResult(
|
||||
ok({
|
||||
path: written.value.path,
|
||||
bytes: written.value.bytes,
|
||||
entries: snapshot.value.entries.length,
|
||||
}),
|
||||
closeContract,
|
||||
)
|
||||
: toCallToolResult(closed, closeContract);
|
||||
},
|
||||
);
|
||||
};
|
||||
@@ -49,9 +49,8 @@ export const registerEnhancedTools = (
|
||||
analysis: AnalysisOperationPort,
|
||||
options: EnhancedToolRegistration,
|
||||
): void => {
|
||||
const services = new EnhancedTools(analysis);
|
||||
for (const contract of ENHANCED_TOOL_CONTRACTS) {
|
||||
registerEnhancedTool(server, services, contract, {
|
||||
registerEnhancedTool(server, analysis, contract, {
|
||||
logger: options.logger,
|
||||
activeTarget: options.activeTarget,
|
||||
analysisProfile: options.analysisProfile,
|
||||
@@ -63,7 +62,7 @@ export const registerEnhancedTools = (
|
||||
|
||||
const registerEnhancedTool = (
|
||||
server: McpServer,
|
||||
services: EnhancedTools,
|
||||
analysis: AnalysisOperationPort,
|
||||
contract: ToolContract,
|
||||
registration: {
|
||||
readonly logger: Logger;
|
||||
@@ -91,6 +90,13 @@ const registerEnhancedTool = (
|
||||
if (!validatedCall.ok) return toCallToolResult(validatedCall, contract);
|
||||
const parsedInput = validatedCall.value.input;
|
||||
const parameters = jsonParameters(parsedInput);
|
||||
const services = new EnhancedTools({
|
||||
execute: (operation, operationParameters, executionOptions) =>
|
||||
analysis.execute(operation, operationParameters, {
|
||||
...executionOptions,
|
||||
progress,
|
||||
}),
|
||||
});
|
||||
const result = await logToolExecution(registration.logger, name, () =>
|
||||
services.executeValidated(validatedCall.value, context.mcpReq.signal),
|
||||
);
|
||||
|
||||
@@ -148,6 +148,7 @@ const projectSessionStatus = (options: {
|
||||
active_provider: activeProvider,
|
||||
target: open ? targetFrom(options.status) : null,
|
||||
alignment: options.serverIdentity.alignment,
|
||||
analysis_activity: options.status.analysis_activity,
|
||||
recommended_actions: recommendedActions(
|
||||
open,
|
||||
options.serverIdentity.alignment.state,
|
||||
|
||||
@@ -6,7 +6,7 @@ import { SESSION_TOOL_CONTRACTS } from "../contracts/toolContracts.js";
|
||||
import { toCallToolResult } from "./toolResult.js";
|
||||
import type { Logger } from "../logger.js";
|
||||
import { logToolExecution } from "./toolLogging.js";
|
||||
import { err, ok, type Result } from "../domain/result.js";
|
||||
import { ok, type Result } from "../domain/result.js";
|
||||
import type {
|
||||
ProcessCapture,
|
||||
ProcessExecutionPolicy,
|
||||
@@ -18,16 +18,8 @@ import { processCapturePermissionRequest } from "../application/ProcessCapturePe
|
||||
import type { Evidence } from "../domain/evidence.js";
|
||||
import type { AnalysisSnapshot } from "../domain/analysisSnapshot.js";
|
||||
import type { EvidenceFilePolicy } from "../domain/evidenceBundle.js";
|
||||
import {
|
||||
readAnalysisSnapshot,
|
||||
writeAnalysisSnapshot,
|
||||
} from "../application/AnalysisSnapshotFiles.js";
|
||||
import {
|
||||
AnalysisProtocolError,
|
||||
PermissionRequiredError,
|
||||
UnknownRegistryError,
|
||||
type AnalysisError,
|
||||
} from "../domain/errors.js";
|
||||
import { readAnalysisSnapshot } from "../application/AnalysisSnapshotFiles.js";
|
||||
import { UnknownRegistryError, type AnalysisError } from "../domain/errors.js";
|
||||
import {
|
||||
DENY_EVIDENCE_FILE_POLICY,
|
||||
DENY_PROCESS_POLICY,
|
||||
@@ -40,10 +32,7 @@ import { toolRegistrationOptions } from "./toolRegistrationOptions.js";
|
||||
import { safeParseToolInput } from "./toolInputValidation.js";
|
||||
import { registerBundleComparisonTool } from "./registerBundleComparisonTool.js";
|
||||
import { registerInvestigationTools } from "./registerInvestigationTools.js";
|
||||
import {
|
||||
closeBinaryInputSchema,
|
||||
openBinaryInputSchema,
|
||||
} from "../contracts/sessionLifecycleInputs.js";
|
||||
import { openBinaryInputSchema } from "../contracts/sessionLifecycleInputs.js";
|
||||
import { registerSessionStatusTool } from "./registerSessionStatusTool.js";
|
||||
import type { PermissionAuthority } from "../application/PermissionAuthority.js";
|
||||
import { mcpProgressReporter } from "./mcpProgress.js";
|
||||
@@ -61,20 +50,8 @@ import {
|
||||
sessionAvailabilityPolicy,
|
||||
type SessionAvailability,
|
||||
} from "./sessionAvailabilityPolicy.js";
|
||||
|
||||
const permissionFailure = (
|
||||
failure: Awaited<ReturnType<PermissionAuthority["authorize"]>>,
|
||||
): Result<never, AnalysisError> => {
|
||||
if (failure.ok)
|
||||
return err(new AnalysisProtocolError("Expected a denied permission"));
|
||||
return err(
|
||||
failure.error instanceof PermissionRequiredError
|
||||
? failure.error
|
||||
: new AnalysisProtocolError(failure.error.message, {
|
||||
cause: failure.error,
|
||||
}),
|
||||
);
|
||||
};
|
||||
import { permissionFailure } from "./permissionFailure.js";
|
||||
import { registerCloseLifecycleTool } from "./registerCloseLifecycleTool.js";
|
||||
|
||||
const recordProcessResidualUnknowns = (
|
||||
session: BinarySessionPort,
|
||||
@@ -204,7 +181,7 @@ const registerProcessTools = ({
|
||||
);
|
||||
};
|
||||
|
||||
interface LifecycleToolRegistration {
|
||||
export interface LifecycleToolRegistration {
|
||||
readonly server: McpServer;
|
||||
readonly session: BinarySessionPort;
|
||||
readonly logger: Logger;
|
||||
@@ -314,75 +291,6 @@ const registerOpenLifecycleTool = ({
|
||||
);
|
||||
};
|
||||
|
||||
const registerCloseLifecycleTool = ({
|
||||
server,
|
||||
session,
|
||||
logger,
|
||||
contracts: [, closeContract],
|
||||
snapshotFilePolicy,
|
||||
permissionAuthority,
|
||||
}: LifecycleToolRegistration): void => {
|
||||
server.registerTool(
|
||||
closeContract.name,
|
||||
toolRegistrationOptions(closeContract),
|
||||
async (input) => {
|
||||
const parsedInput = safeParseToolInput(
|
||||
closeBinaryInputSchema,
|
||||
input,
|
||||
closeContract.name,
|
||||
);
|
||||
if (!parsedInput.ok) return toCallToolResult(parsedInput, closeContract);
|
||||
const parsed = parsedInput.value;
|
||||
if (parsed.snapshot_path === undefined) {
|
||||
const closed = await logToolExecution(logger, closeContract.name, () =>
|
||||
session.close(),
|
||||
);
|
||||
if (closed.ok) server.sendToolListChanged();
|
||||
return toCallToolResult(closed, closeContract);
|
||||
}
|
||||
if (permissionAuthority !== undefined) {
|
||||
const authorized = await permissionAuthority.authorize(
|
||||
{
|
||||
capability: "snapshot_write",
|
||||
roots: [parsed.snapshot_path],
|
||||
executables: [],
|
||||
environment_names: [],
|
||||
network: "none",
|
||||
mount: false,
|
||||
operation_identity: `close_binary:snapshot:${parsed.snapshot_path}`,
|
||||
},
|
||||
"write",
|
||||
);
|
||||
if (!authorized.ok)
|
||||
return toCallToolResult(permissionFailure(authorized), closeContract);
|
||||
}
|
||||
const snapshot = session.exportAnalysisSnapshot();
|
||||
if (!snapshot.ok) return toCallToolResult(snapshot, closeContract);
|
||||
const written = await writeAnalysisSnapshot(
|
||||
snapshot.value,
|
||||
parsed.snapshot_path,
|
||||
parsed.overwrite,
|
||||
snapshotFilePolicy,
|
||||
);
|
||||
if (!written.ok) return toCallToolResult(written, closeContract);
|
||||
const closed = await logToolExecution(logger, closeContract.name, () =>
|
||||
session.close(),
|
||||
);
|
||||
if (closed.ok) server.sendToolListChanged();
|
||||
return closed.ok
|
||||
? toCallToolResult(
|
||||
ok({
|
||||
path: written.value.path,
|
||||
bytes: written.value.bytes,
|
||||
entries: snapshot.value.entries.length,
|
||||
}),
|
||||
closeContract,
|
||||
)
|
||||
: toCallToolResult(closed, closeContract);
|
||||
},
|
||||
);
|
||||
};
|
||||
|
||||
/** Register MCP-only target lifecycle operations on a long-lived session. */
|
||||
export interface SessionToolOptions {
|
||||
readonly processPolicy?: ProcessExecutionPolicy;
|
||||
|
||||
Reference in New Issue
Block a user