Files
MikrotikManager/backend/src/modules/mcp/service-native-progress.ts
T

13 lines
1.2 KiB
TypeScript

import { McpError } from "./security.js"
/** Service steps can contain several native commands. Preserve that progress
* separately from the runner's count of completed top-level steps. Never copy
* arbitrary error data, native payloads, or unbounded counters into the audit. */
export function serviceNativeProgress(error: unknown, serviceStep: boolean, mutationCost: number | undefined) {
if (!serviceStep || !(error instanceof McpError) || !["MUTATION_OUTCOME_UNKNOWN", "VERIFICATION_FAILED"].includes(error.code) || !Number.isSafeInteger(mutationCost) || mutationCost! < 0) return undefined
const progress = error as McpError & { commandCount?: number; confirmedWrites?: number; completedCommands?: number }
const { commandCount, confirmedWrites, completedCommands } = progress
if (![commandCount, confirmedWrites, completedCommands].every(value => Number.isSafeInteger(value) && value! >= 0) || commandCount! > Math.min(mutationCost!, 10000) || confirmedWrites! > commandCount! || completedCommands! > confirmedWrites!) return undefined
return { commandCount: commandCount!, acknowledgedWrites: confirmedWrites!, verifiedCommands: completedCommands!, automaticReplay: false }
}