Files
MikrotikManager/backend/src/modules/mcp/routing-community-public.ts
T

61 lines
4.2 KiB
TypeScript

import type { ChangeEvidence } from "./action-types.js"
import { isRoutingPolicySnapshot } from "./resources.js"
const record = (value: unknown): Record<string, unknown> => value && typeof value === "object" && !Array.isArray(value) ? value as Record<string, unknown> : {}
const rules = (value: unknown) => Array.isArray(value) ? value.map(item => {
const rule = record(item)
return { community: rule.community, action: rule.action ?? "route", gateway: rule.gateway, gatewayTunnelId: rule.gatewayTunnelId }
}) : []
/** Native snapshots remain encrypted execution evidence. Public review contains
* only the exact managed routing change, without scripts, comments or seals.
* The unkeyed snapshot fingerprint is also private: comments can contain low
* entropy confidential values that would otherwise permit offline guesses. */
export function publicRoutingCommunityChanges(evidence: ChangeEvidence) {
return {
target: evidence.target, objects: evidence.objects,
snapshots: evidence.snapshots.map(snapshot => ({ serverId: snapshot.serverId, resource: snapshot.resource, rowCount: snapshot.rows.length })),
before: rules(evidence.before),
steps: evidence.steps.map(step => {
if (step.kind !== "service" || !["routing.community", "routing.community.scoped", "routing.community.scoped.recover"].includes(step.service)) throw new Error("Unsupported public Route AI change")
const profile = record(step.input.profile)
return { kind: step.kind, service: step.service, input: {
serverId: step.input.serverId, candidateId: step.input.candidateId,
community: profile.community, table: profile.table, probeVrf: profile.probeVrf,
rules: rules(step.input.rules),
} }
}),
requiresHuman: evidence.requiresHuman, mutationCost: evidence.mutationCost, validUntil: evidence.validUntil,
managementChannels: evidence.managementChannels?.map(channel => ({ channelId: channel.channelId, serverId: channel.serverId, expiresAt: channel.expiresAt })),
warnings: evidence.warnings,
}
}
/** Service recovery has the same private native bodies as its forward plan. */
export function publicMcpChangeEvidence(operationId: string, evidence: ChangeEvidence) {
const scopedRecovery = operationId === "operations.recover" && evidence.steps.length === 1 && evidence.steps[0].kind === "service" && evidence.steps[0].service === "routing.community.scoped.recover"
if (operationId === "routing.profiles.save" || operationId === "routing.controller.update") return {...evidence,steps:evidence.steps.map(step=>{if(step.kind!=="service")return step;const {baseline:_privateBaseline,...input}=step.input;return {...step,input}})}
if (operationId === "routing.community.switch" || scopedRecovery) return publicRoutingCommunityChanges(evidence)
if (evidence.healthChecks?.some(check => check.nextHopInterface)) evidence = { ...evidence, healthChecks: evidence.healthChecks.map(({ nextHopInterface: _privateInterface, ...check }) => check) }
if (!evidence.snapshots.some(snapshot => isRoutingPolicySnapshot(snapshot.resource))) return evidence
return { ...evidence, snapshots: evidence.snapshots.map(snapshot => isRoutingPolicySnapshot(snapshot.resource)
? { serverId: snapshot.serverId, resource: snapshot.resource, rowCount: snapshot.rows.length }
: snapshot) }
}
/** Public controller DTO is explicit. Private baseline/native proofs, seal
* lineage and future internal fields never leave profile list or plan review. */
export function publicRoutingControllerState(raw:unknown) {
if(!raw||typeof raw!=="object"||Array.isArray(raw))return {}
const value=raw as Record<string,unknown>,result:Record<string,unknown>={}
for(const key of ["pendingCandidateId","observationKey","lastSwitchAt","operationId","pausedReason","failureKey","failingCandidateId",
"planId","candidateId","expectedCandidateId","reason","checkedAt","probedAt"]){
if(typeof value[key]==="string")result[key]=value[key]
}
for(const key of ["consecutive","failureCount"]){
if(typeof value[key]==="number"&&Number.isSafeInteger(value[key])&&Number(value[key])>=0)result[key]=value[key]
}
if(Array.isArray(value.switches)&&value.switches.length<=2048&&value.switches.every(at=>typeof at==="string"))result.switches=[...value.switches]
return result
}