Files
MikrotikManager/backend/src/modules/mcp/native-conservation-acceptance.ts
T
DenozordecandClaude Opus 5.5 73af143a53 fix(test): перевести оставшиеся перехваты аудита на транзакционный клиент
Устаревшие тесты, не дефект продукта. Аудиты configuration.intent и
configuration.acknowledged пишутся через requestAccountingWrite на клиенте
из pool.connect(), поэтому перехваты pool.query в Legacy OSPF, журнале
событий, IPsec peer update, IPsec native write и native conservation не
срабатывали. Фикстур configuration-audit-commit получил общий примитив
hookConfigurationAudits (после COMMIT намерения / перед подтверждением);
afterConfigurationIntentCommit и loseConfigurationAcknowledgement построены
на нём. Ожидаемые итоги (unknown/partial, без записи и повтора) не менялись;
перехват afterVerified в native conservation остаётся на pool.query, так как
runtime обновляет шаги через пул.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
2026-11-15 22:10:01 +07:00

571 lines
41 KiB
TypeScript

import assert from "node:assert/strict"
import { randomUUID } from "node:crypto"
import { createServer } from "node:http"
import { setTimeout as delay } from "node:timers/promises"
import { mcpConnectionCreateSchema } from "@mmapp/contracts/mcp"
import { pool } from "../../db/index.js"
import type { AuthUser } from "../../lib/permissions.js"
import { managedComment } from "../../managed-markers.js"
import { updateServer } from "../servers/service/servers-service.js"
import type { ChangeEvidence } from "./action-types.js"
import { findAction } from "./actions.js"
import { readRosSnapshot } from "./resources.js"
import { approvePlan, applyPlan, invokeMcpTool } from "./runtime.js"
import { decryptSecret, encryptSecret } from "./security.js"
import { authenticateMcpToken, createMcpConnection, setConnectionStatus } from "./store.js"
import { hookConfigurationAudits } from "../../test/fixtures/configuration-audit-commit.js"
/** Owned SQL + loopback only. Models projected native configuration, not a CHR
* forwarding test, opaque-field proof, or proof of real native side effects. */
export async function acceptNativeConservation(actor: AuthUser) {
const serverId = 198
const filterPath = "/ip/firewall/filter", rulePath = "/routing/rule"
const routePaths = ["/ip/route", "/ipv6/route"], settingsPaths = ["/ip/settings", "/ipv6/settings"]
const base = () => ["a", "b", "c"].map(id => ({ ".id": `*${id}`, chain: "fixture", action: "accept", comment: id, disabled: "true" }))
const ruleBase = () => ["a", "b", "c"].map(id => ({ ".id": `*${id}`, action: "lookup", table: "main", comment: id, disabled: "true" }))
let catalogs: Record<string, Record<string, string>[]> = {}, mode = "normal", writes = 0, next = 0, polls = 0
let connectionId: string | undefined
let afterIntent: (() => Promise<void>) | undefined, afterVerified: (() => Promise<void>) | undefined
let afterRouteRead: (() => Promise<void>) | undefined
const query = pool.query.bind(pool)
const commands: Array<{ method: string; path: string }> = []
const readPaths = new Set<string>()
const reset = () => {
mode = "normal"
catalogs = { [filterPath]: base(), [rulePath]: ruleBase(), "/routing/table": [{ ".id": "*main", name: "main", fib: "true", disabled: "false" }], "/ip/settings": [{ "rp-filter": "no", "ipv4-multipath-hash-policy": "l3" }], "/ip/route": [] }
}
reset()
const router = createServer(async (req, res) => {
const path = decodeURIComponent((req.url ?? "").replace(/^\/rest/, ""))
res.setHeader("content-type", "application/json")
if (req.method === "GET") {
polls++; readPaths.add(path)
const body = JSON.stringify(catalogs[path] ?? [])
if (path === "/ip/route" && afterRouteRead) {
const hook = afterRouteRead
afterRouteRead = undefined
await hook()
}
res.end(body)
return
}
const parent = [filterPath, rulePath, ...routePaths, ...settingsPaths].find(value => path === value || path.startsWith(`${value}/`))
if (!parent) { res.statusCode = 404; res.end("{}"); return }
const singleton = settingsPaths.includes(parent)
const set = singleton && path === parent + "/set" && req.method === "POST"
const rowWrite = !singleton && (req.method === "PUT" ? path === parent : ["PATCH", "DELETE"].includes(req.method ?? "") && path.startsWith(parent + "/"))
if (!set && !rowWrite) { res.statusCode = 400; res.end("{}"); return }
let text = ""; for await (const chunk of req) text += chunk
const fields = text ? JSON.parse(text) as Record<string, string> : {}
if (set) {
const keys = parent === "/ip/settings" ? ["rp-filter", "ipv4-multipath-hash-policy"] : ["multipath-hash-policy"]
if (!fields || !Object.keys(fields).length || Object.entries(fields).some(([key, value]) => !keys.includes(key) || typeof value !== "string")) { res.statusCode = 400; res.end("{}"); return }
}
const rows = catalogs[parent], id = req.method === "PUT" ? `*new${++next}` : path.slice(parent.length + 1)
writes++; commands.push({ method: req.method!, path })
if (mode !== "ignored") {
if (req.method === "PUT") rows.push({ ".id": id, ...fields })
if (set) Object.assign(rows[0], fields)
if (req.method === "PATCH") Object.assign(rows.find(row => row[".id"] === id)!, fields)
if (req.method === "DELETE") catalogs[parent] = rows.filter(row => row[".id"] !== id)
}
const after = catalogs[parent]
if (routePaths.includes(parent)) for (const row of after) row.active = Number(row.distance) === Math.min(...after.map(item => Number(item.distance))) ? "true" : "false"
if (mode === "two-new") after.push({ ".id": `*extra${next}`, ...fields })
if (mode === "collateral-update") after.find(row => row[".id"] === "*b")!.comment = "external"
if (mode === "collateral-remove") catalogs[parent] = after.filter(row => row[".id"] !== "*b")
if (mode === "duplicate-id") after.push(structuredClone(after[0]))
if (mode === "missing-id") delete (after[0] as Record<string, string | undefined>)[".id"]
if (mode === "reorder") after.reverse()
if (mode === "lost") { req.socket.destroy(); return }
// Model both acknowledged empty-body and RouterOS command responses.
if (set) {
if (parent === "/ip/settings") { res.writeHead(204); res.end() }
else res.end("[]")
return
}
res.end(JSON.stringify(req.method === "PUT" ? { ".id": mode === "ack-existing" ? "*a" : id, ...fields } : {}))
})
await new Promise<void>(resolve => router.listen(0, "127.0.0.1", resolve))
const wait = async (id: string) => {
for (const deadline = Date.now() + 45000; Date.now() < deadline;) {
const row = (await query("SELECT state,verification,error_code FROM mcp_operations WHERE id=$1", [id])).rows[0]
if (!["pending", "running"].includes(row.state)) return row
await delay(25)
}
throw Error("Conservation operation remains live; never replay")
}
// Hooks run AFTER durable intent / verified-step SQL, not at plan creation.
// Intent audits commit on transaction clients, so that hook wraps pool.connect.
const restoreAudits = hookConfigurationAudits({ afterIntentCommit: async payload => {
if (payload.serverId === serverId && afterIntent) { const hook = afterIntent; afterIntent = undefined; await hook() }
} })
pool.query =(async (...args: unknown[]) => {
const value = await (query as (...values: unknown[]) => Promise<unknown>)(...args)
const sql = args[0], params = args[1] as unknown[] | undefined
if (typeof sql === "string" && sql.startsWith("UPDATE mcp_operation_steps SET state=$3") && params?.[2] === "verified" && afterVerified) {
const hook = afterVerified; afterVerified = undefined; await hook()
}
return value
}) as typeof pool.query
try {
assert.equal((await query("SELECT count(*)::int AS n FROM servers WHERE id=$1", [serverId])).rows[0].n, 0)
await query("INSERT INTO servers(id,name,host,port,use_ssl,password) VALUES($1,'native-conservation','127.0.0.1',$2,false,'fixture-only')", [serverId, (router.address() as { port: number }).port])
const access = await createMcpConnection(mcpConnectionCreateSchema.parse({ name: "Native conservation", mode: "approval", expiresAt: new Date(Date.now() + 1800000).toISOString(), permissions: ["mm:network:read", "mm:network:write"], scope: { serverIds: [serverId], operationIds: ["ros.ip_filter.create", "ros.ip_filter.update", "ros.ip_filter.delete", "ros.routing_rule.update", "ros.ipv4_route.update", "ros.ip_settings.update", "ros.ipv6_settings.update", "routing.ecmp.update", "routing.rpf.update", "operations.recover", "operations.reconcile", "operations.read"], allowGlobal: false }, limits: { callsPerMinute: 1000, mutationsPerHour: 1000 } }), actor)
connectionId = access.connection.id
let principal = await authenticateMcpToken(access.token)
const prepare = (action: "create" | "update" | "delete", resource = "ip_filter") => invokeMcpTool(principal, `mm_${resource}_${action}_plan`, { serverId, ...(action === "create" ? {} : { id: "*a" }), fields: action === "create" ? { chain: "fixture", action: "accept", comment: "new", disabled: true } : action === "update" ? { comment: "changed" } : {} }) as Promise<{ planId: string }>
const evidence = async (id: string) => JSON.parse(await decryptSecret((await query("SELECT evidence FROM mcp_plans WHERE id=$1", [id])).rows[0].evidence.sealed)) as ChangeEvidence
const replace = async (id: string, value: ChangeEvidence) => { await query("UPDATE mcp_plans SET evidence=$2::jsonb WHERE id=$1", [id, JSON.stringify({ sealed: await encryptSecret(JSON.stringify(value)) })]) }
const apply = async (id: string) => { await approvePlan(id, actor, true); const key = randomUUID(), operation = await applyPlan(principal, id, key); return { key, operation, ...await wait(operation.id) } }
const noReplay = async (id: string, result: Awaited<ReturnType<typeof apply>>, count: number) => {
const replay = await applyPlan(principal, id, result.key)
assert.equal(replay.id, result.operation.id); assert.equal(writes, count)
assert.equal((await applyPlan(principal, id, randomUUID())).id, result.operation.id)
assert.equal(writes, count)
await assert.rejects(invokeMcpTool(principal, "mm_operation_recovery_plan", { operationId: result.operation.id }))
assert.equal(writes, count)
}
for (const action of ["create", "update", "delete"] as const) {
reset(); const plan = await prepare(action), before = writes, beforePolls = polls, result = await apply(plan.planId)
assert.equal(result.state, "applied", `normal ${action}`); assert.equal(result.verification, "passed"); assert.equal(writes, before + 1)
const sealed = await evidence(plan.planId)
assert.equal(polls - beforePolls, new Set(sealed.snapshots.map(snapshot => snapshot.resource)).size * 4 + 1, `normal ${action}: one sweep per execution boundary and changed-catalog readback`)
const observed = await invokeMcpTool(principal, "mm_operation_reconcile", { operationId: result.operation.id }) as { automaticRetry: boolean }
assert.equal(observed.automaticRetry, false); assert.equal(writes, before + 1)
if (action !== "delete") {
const inverse = await invokeMcpTool(principal, "mm_operation_recovery_plan", { operationId: result.operation.id }) as { planId: string }
assert.equal((await apply(inverse.planId)).state, "applied")
assert.deepEqual(catalogs[filterPath], base(), `exact ${action} inverse`)
} else {
// Generic DELETE has no safe inverse: a new native ID is not the old identity.
await assert.rejects(invokeMcpTool(principal, "mm_operation_recovery_plan", { operationId: result.operation.id }))
assert.deepEqual(catalogs[filterPath], base().slice(1))
}
}
reset()
const finalDrift = await prepare("update"), beforeFinalDrift = writes
afterVerified = async () => { catalogs[filterPath][1].comment = "external-before-terminal" }
try {
const result = await apply(finalDrift.planId)
assert.equal(afterVerified, undefined)
assert.equal(result.error_code, "STATE_CONFLICT")
assert.notEqual(result.state, "applied")
assert.equal(writes, beforeFinalDrift + 1)
await noReplay(finalDrift.planId, result, writes)
} finally {
afterVerified = undefined
}
for (const action of ["create", "update", "delete"] as const) {
const faults = ["ignored", "lost", "collateral-update", "collateral-remove", "duplicate-id", "missing-id", "reorder", ...(action === "create" ? ["ack-existing", "two-new"] : [])]
for (const fault of faults) {
reset(); const plan = await prepare(action), before = writes; mode = fault
const result = await apply(plan.planId)
assert.notEqual(result.state, "applied", `${action}/${fault}`); assert.notEqual(result.verification, "passed", `${action}/${fault}`)
assert.equal(writes, before + 1, `${action}/${fault}: one diagnostic dispatch`)
await noReplay(plan.planId, result, writes)
assert.equal((await query("SELECT count(*)::int AS n FROM mcp_operation_steps WHERE operation_id=$1 AND state='verified'", [result.operation.id])).rows[0].n, 0)
}
}
for (const fault of ["duplicate-id", "missing-id"]) {
reset(); const before = writes
if (fault === "duplicate-id") catalogs[filterPath].push(structuredClone(catalogs[filterPath][0]))
else delete (catalogs[filterPath][0] as Record<string, string | undefined>)[".id"]
// Either preparation rejects an invalid catalog, or execution rejects
// the sealed invalid snapshot before its first native dispatch.
const plan = await prepare("update").catch(() => undefined)
if (plan) { const result = await apply(plan.planId); assert.notEqual(result.state, "applied"); assert.notEqual(result.verification, "passed") }
assert.equal(writes, before, `${fault} in original snapshot: zero writes`)
}
reset(); const reorderedRule = await prepare("update", "routing_rule"); mode = "reorder"
const ruleResult = await apply(reorderedRule.planId)
assert.notEqual(ruleResult.state, "applied"); assert.notEqual(ruleResult.verification, "passed")
await noReplay(reorderedRule.planId, ruleResult, writes)
// Explicit sealed fixture: two valid native PATCHes share the real initial
// full-catalog snapshot. We do NOT fabricate an already-mutated prefix.
reset(); const multi = await prepare("update"), multiEvidence = await evidence(multi.planId)
const first = multiEvidence.steps[0]; assert.equal(first.kind, "ros")
if (first.kind !== "ros") throw Error("Expected native step")
multiEvidence.steps.push({ kind: "ros", step: { ...first.step, id: "*b", fields: { comment: "second" } } })
multiEvidence.objects.push({ kind: "ip_filter", id: "*b", serverId }); multiEvidence.mutationCost = 2
multiEvidence.warnings.push("Acceptance fixture: sealed two-step batch with full original snapshot")
await replace(multi.planId, multiEvidence)
const beforeMulti = writes, commandOffset = commands.length
afterVerified = async () => { catalogs[filterPath][1].comment = "external-before-second-intent" }
const stopped = await apply(multi.planId)
assert.equal(afterVerified, undefined, "first write must actually be verified before drift")
assert.notEqual(stopped.state, "applied"); assert.notEqual(stopped.verification, "passed"); assert.equal(writes, beforeMulti + 1)
assert.deepEqual(commands.slice(commandOffset), [{ method: "PATCH", path: `${filterPath}/*a` }])
assert.deepEqual((await query("SELECT step_index,state FROM mcp_operation_steps WHERE operation_id=$1 ORDER BY step_index", [stopped.operation.id])).rows, [{ step_index: 0, state: "verified" }])
await noReplay(multi.planId, stopped, writes)
for (const drift of ["source", "epoch", "dependency", "protection", "policy"] as const) {
reset(); const plan = await prepare("update"), sealed = await evidence(plan.planId)
// A legitimate read dependency, sealed BEFORE dispatch. The unchanged
// table is part of the prefix and must be checked after native intent.
if (drift === "dependency") {
sealed.snapshots.push(await readRosSnapshot(serverId, "routing_table"))
sealed.objects.push({ kind: "routing_table", id: "*main", serverId })
await replace(plan.planId, sealed)
}
const protection = randomUUID(), before = writes
let pollsAtIntent: number | undefined
afterIntent = async () => {
pollsAtIntent = polls
if (drift === "source") await query("UPDATE servers SET password='late-fixture-source' WHERE id=$1", [serverId])
if (drift === "epoch") {
const previous = (await query("SELECT routing_endpoint_epoch FROM servers WHERE id=$1", [serverId])).rows[0].routing_endpoint_epoch
await query("UPDATE servers SET password='epoch-aba-fixture' WHERE id=$1", [serverId])
await query("UPDATE servers SET password='fixture-only' WHERE id=$1", [serverId])
assert.notEqual((await query("SELECT routing_endpoint_epoch FROM servers WHERE id=$1", [serverId])).rows[0].routing_endpoint_epoch, previous, "A -> B -> A must rotate the actual SQL endpoint epoch")
}
if (drift === "dependency") catalogs["/routing/table"][0].fib = "false"
if (drift === "protection") await query("INSERT INTO mcp_protections(id,object_ref,created_by) VALUES($1,$2::jsonb,$3)", [protection, JSON.stringify({ kind: "ip_filter", id: "*a", serverId, protection: "deny" }), actor.id])
if (drift === "policy") await query("UPDATE mcp_connections SET policy=jsonb_set(policy,'{version}',to_jsonb((policy->>'version')::int+1)) WHERE id=$1", [connectionId])
}
try {
const result = await apply(plan.planId)
console.log("Late native fence:", drift, { state: result.state, verification: result.verification, errorCode: result.error_code, dispatched: writes - before })
assert.equal(afterIntent, undefined, `${drift}: durable native intent hook reached`)
assert.notEqual(result.state, "applied", `${drift}: late intent must stop execution`); assert.notEqual(result.verification, "passed"); assert.equal(writes, before, `${drift}: zero dispatches`)
if (drift === "source" || drift === "epoch") assert.equal(polls, pollsAtIntent, `${drift}: new source refused before native GET`)
await noReplay(plan.planId, result, writes)
} finally {
afterIntent = undefined
await query("DELETE FROM mcp_protections WHERE id=$1", [protection])
await query("UPDATE servers SET password='fixture-only' WHERE id=$1", [serverId])
principal = await authenticateMcpToken(access.token)
}
}
reset(); const obsolete = await prepare("update")
const currentVersion = findAction("mm_ip_filter_update_plan").operation.version
assert.ok(currentVersion > 1)
await query("UPDATE mcp_plans SET operation_version=$2 WHERE id=$1", [obsolete.planId, currentVersion - 1])
await approvePlan(obsolete.planId, actor, true)
const beforeObsolete = writes
await assert.rejects(applyPlan(principal, obsolete.planId, randomUUID()), /устарели/)
assert.equal(writes, beforeObsolete)
reset()
const routeBaseline = [0, 1].map(index => ({ ".id": `*route${index}`, "dst-address": "192.0.2.0/24", "routing-table": "main", gateway: `198.18.0.${index + 1}`, "immediate-gw": `198.18.0.${index + 1}%fixture`, scope: "0", distance: String((index + 1) * 10), active: index === 0 ? "true" : "false", disabled: "false", dynamic: "false", comment: managedComment("conservation") }))
catalogs["/ip/route"] = structuredClone(routeBaseline)
const originalSettings = structuredClone(catalogs["/ip/settings"])
const ecmpArgs = { serverId, family: "ipv4", routeIds: ["*route0", "*route1"], distance: 10, hashPolicy: "l4" }
const beforeEcmp = writes
const observer = await createMcpConnection(mcpConnectionCreateSchema.parse({ name: "ECMP interface-only preflight", mode: "observer", expiresAt: new Date(Date.now() + 1800000).toISOString(), permissions: ["mm:network:read"], scope: { serverIds: [serverId], operationIds: ["routing.ecmp.preflight.read"], allowGlobal: false } }), actor)
try {
const reader = await authenticateMcpToken(observer.token)
const { distance, hashPolicy, ...preflightInput } = ecmpArgs
catalogs["/ip/firewall/connection/tracking"] = [{ enabled: "auto" }]
catalogs["/ip/route"] = routeBaseline.map((row, index) => ({ ...row, gateway: `gre${index}`, "immediate-gw": `gre${index}` }))
catalogs["/interface"] = [0, 1].map(index => ({ ".id": `*gre${index}`, name: `gre${index}` }))
const before = polls
const result = await invokeMcpTool(reader, "mm_ecmp_preflight_get", preflightInput) as { configurationObserved: boolean; networkValidation: string; sources: { interfaces: { count: number } } }
assert.equal(result.configurationObserved, true)
assert.equal(result.networkValidation, "not-performed")
assert.equal(result.sources.interfaces.count, 2)
assert.equal(polls - before, 11)
assert.equal(writes, beforeEcmp)
} finally {
await query("DELETE FROM mcp_connections WHERE id=$1", [observer.connection.id])
catalogs["/ip/route"] = structuredClone(routeBaseline)
}
catalogs["/ip/route"] = routeBaseline.map((row, index) => ({ ...row, gateway: `gre${index}`, "immediate-gw": `gre${index}` }))
const namedHopPlan = await invokeMcpTool(principal, "mm_ecmp_plan", ecmpArgs) as { planId: string; changes: unknown }
assert.ok(!JSON.stringify(namedHopPlan.changes).includes("nextHopInterface"))
const inventory = structuredClone(catalogs["/interface"])
afterIntent = async () => { catalogs["/interface"][0][".id"] = "*replacement" }
const replacedHop = await apply(namedHopPlan.planId)
assert.equal(afterIntent, undefined)
assert.equal(replacedHop.state, "rejected")
assert.equal(replacedHop.error_code, "STATE_CONFLICT")
assert.equal(writes, beforeEcmp)
catalogs["/interface"] = inventory
catalogs["/ip/route"] = structuredClone(routeBaseline)
console.log("ECMP interface-only next-hop: same-name replacement after intent refused before PATCH; native identity stays private")
afterRouteRead = async () => { catalogs["/ip/route"][1].distance = "25" }
await assert.rejects(invokeMcpTool(principal, "mm_ecmp_plan", ecmpArgs), { code: "STATE_CONFLICT" })
assert.equal(afterRouteRead, undefined)
catalogs["/ip/route"] = structuredClone(routeBaseline)
afterRouteRead = async () => {
await query("UPDATE servers SET password='source-aba-fixture' WHERE id=$1", [serverId])
await query("UPDATE servers SET password='fixture-only' WHERE id=$1", [serverId])
}
await assert.rejects(invokeMcpTool(principal, "mm_ecmp_plan", ecmpArgs), { code: "STATE_CONFLICT" })
assert.equal(afterRouteRead, undefined)
assert.equal(writes, beforeEcmp)
catalogs["/ip/route"][1]["immediate-gw"] = routeBaseline[0]["immediate-gw"]
await assert.rejects(invokeMcpTool(principal, "mm_ecmp_plan", ecmpArgs), { code: "STATE_CONFLICT" })
assert.equal(writes, beforeEcmp)
catalogs["/ip/route"] = structuredClone(routeBaseline)
const fibTable = catalogs["/routing/table"]
for (const rows of [
[{ ...fibTable[0], fib: "false" }],
[{ ...fibTable[0], fib: "" }],
[{ ".id": "*main", name: "main" }],
[{ ...fibTable[0], disabled: "true" }],
[{ ...fibTable[0], invalid: "true" }],
[...fibTable, { ...fibTable[0], ".id": "*duplicate" }],
]) {
catalogs["/routing/table"] = rows
await assert.rejects(invokeMcpTool(principal, "mm_ecmp_plan", ecmpArgs), { code: "STATE_CONFLICT" })
assert.equal(writes, beforeEcmp)
}
catalogs["/routing/table"] = fibTable
const staleFib = await invokeMcpTool(principal, "mm_ecmp_plan", ecmpArgs) as { planId: string }
await approvePlan(staleFib.planId, actor, true)
catalogs["/routing/table"] = [{ ...fibTable[0], fib: "false" }]
assert.equal((await wait((await applyPlan(principal, staleFib.planId, randomUUID())).id)).state, "rejected")
assert.equal(writes, beforeEcmp)
catalogs["/routing/table"] = fibTable
console.log("ECMP FIB SQL/HTTP: inactive, unconfirmed and ambiguous tables refused; drift rejected before native write")
const equal = await invokeMcpTool(principal, "mm_ecmp_plan", ecmpArgs) as { planId: string }
const beforeEqual: number = writes, equalResult = await apply(equal.planId)
assert.equal(equalResult.state, "applied"); assert.equal(writes, beforeEqual + 3)
assert.deepEqual(commands.at(-1), { method: "POST", path: "/ip/settings/set" })
assert.equal(catalogs["/ip/settings"][0]["ipv4-multipath-hash-policy"], "l4")
const unequal = await invokeMcpTool(principal, "mm_operation_recovery_plan", { operationId: equalResult.operation.id }) as { planId: string }
assert.equal((await apply(unequal.planId)).state, "applied"); assert.equal(writes, beforeEqual + 6)
assert.deepEqual(commands[beforeEqual + 3], { method: "POST", path: "/ip/settings/set" })
assert.deepEqual(catalogs["/ip/route"], routeBaseline); assert.deepEqual(catalogs["/ip/settings"], originalSettings)
const lateEcmp = await invokeMcpTool(principal, "mm_ecmp_plan", ecmpArgs) as { planId: string }
afterIntent = async () => { catalogs["/ip/route"][1]["immediate-gw"] = "" }
const lateEcmpResult = await apply(lateEcmp.planId)
assert.equal(afterIntent, undefined)
assert.equal(lateEcmpResult.state, "rejected")
assert.equal(lateEcmpResult.error_code, "STATE_CONFLICT")
assert.equal(writes, beforeEqual + 6)
catalogs["/ip/route"] = structuredClone(routeBaseline)
const sourceChanged = await invokeMcpTool(principal, "mm_ecmp_plan", ecmpArgs) as { planId: string }
let lateRouteReads = 0
const rotateDuringHealthRead = async () => {
if (++lateRouteReads === 1) {
// The prefix reads the configuration first; the next GET checks the live next-hop.
afterRouteRead = rotateDuringHealthRead
return
}
await assert.rejects(updateServer(serverId, { password: "concurrent-update-fixture" }), { statusCode: 409 })
await query("UPDATE servers SET password='health-source-aba-fixture' WHERE id=$1", [serverId])
await query("UPDATE servers SET password='fixture-only' WHERE id=$1", [serverId])
}
afterIntent = async () => { afterRouteRead = rotateDuringHealthRead }
const staleHealthSource = await apply(sourceChanged.planId)
assert.equal(lateRouteReads, 2)
assert.equal(staleHealthSource.state, "rejected")
assert.equal(staleHealthSource.error_code, "STATE_CONFLICT")
assert.equal(writes, beforeEqual + 6)
const beforeNextStep = await invokeMcpTool(principal, "mm_ecmp_plan", ecmpArgs) as { planId: string }
afterVerified = async () => { catalogs["/ip/route"][1]["immediate-gw"] = "" }
const partialEcmp = await apply(beforeNextStep.planId)
assert.equal(partialEcmp.state, "partial")
assert.equal(writes, beforeEqual + 7)
for (const [result, confirmedWrites] of [[lateEcmpResult, 0], [partialEcmp, 1]] as const) {
const stored = await invokeMcpTool(principal, "mm_operation_get", { operationId: result.operation.id }) as {
state: string; result: { failedStepDispatched: boolean; confirmedWrites: number; needsReview: boolean }
}
assert.equal(stored.state, result.state)
assert.equal(stored.result.failedStepDispatched, false)
assert.equal(stored.result.confirmedWrites, confirmedWrites)
assert.equal(stored.result.needsReview, confirmedWrites > 0)
const { rows: [stop] } = await pool.query("SELECT payload FROM mcp_audit WHERE action='operation.stop' AND payload->>'operationId'=$1", [result.operation.id])
assert.equal(stop.payload.failedStepDispatched, false)
}
catalogs["/ip/route"] = structuredClone(routeBaseline)
const collapsed = await invokeMcpTool(principal, "mm_ecmp_plan", { ...ecmpArgs, hashPolicy: undefined }) as { planId: string }
afterVerified = async () => {
afterVerified = async () => { catalogs["/ip/route"][1]["immediate-gw"] = routeBaseline[0]["immediate-gw"] }
}
const collapsedResult = await apply(collapsed.planId)
assert.equal(collapsedResult.state, "partial")
assert.equal(collapsedResult.verification, "failed")
assert.equal(writes, beforeEqual + 9)
assert.equal(afterVerified, undefined)
catalogs["/ip/route"] = structuredClone(routeBaseline)
console.log("ECMP SQL/HTTP: shared adjacency refuses preparation and post-write collapse fails verification")
const v6Baseline = routeBaseline.map((row, index) => ({
...row, "dst-address": "2001:db8::/64", gateway: `2001:db8:1::${index + 1}`,
"immediate-gw": `fe80::${index + 1}%fixture`,
}))
catalogs["/ipv6/route"] = structuredClone(v6Baseline)
const v6Settings = [{ "multipath-hash-policy": "l3" }]
catalogs["/ipv6/settings"] = structuredClone(v6Settings)
const v6Mangle = "/ipv6/firewall/mangle"
catalogs[v6Mangle] = [{ ".id": "*v6mark", action: "mark-routing", disabled: "false", "new-routing-mark": "NEVER_NATIVE_POLICY_MARK", bytes: "0", packets: "0" }]
const v6Args = { ...ecmpArgs, family: "ipv6" }
readPaths.clear()
for (const [path, field] of [[v6Mangle, "new-routing-mark"], [rulePath, "min-prefix"]]) {
const stale = await invokeMcpTool(principal, "mm_ecmp_plan", v6Args) as { planId: string; changes: { snapshots: Record<string, unknown>[] } }
const policy = stale.changes.snapshots.find(snapshot => snapshot.resource === "routing_policy:ipv6_mangle")
assert.deepEqual(policy, { serverId, resource: "routing_policy:ipv6_mangle", rowCount: 1 })
assert.ok(!JSON.stringify(stale).includes("NEVER_NATIVE_POLICY_MARK"))
await approvePlan(stale.planId, actor, true)
const original = catalogs[path], before = writes
catalogs[path] = [{ ...original[0], [field]: "17" }, ...original.slice(1)]
assert.equal((await wait((await applyPlan(principal, stale.planId, randomUUID())).id)).state, "rejected")
assert.equal(writes, before)
catalogs[path] = original
}
const v6Plan = await invokeMcpTool(principal, "mm_ecmp_plan", v6Args) as { planId: string }
catalogs[v6Mangle][0].bytes = "1500"
catalogs[v6Mangle][0].packets = "1"
const v6Result = await apply(v6Plan.planId)
assert.equal(v6Result.state, "applied")
assert.deepEqual(commands.at(-1), { method: "POST", path: "/ipv6/settings/set" })
assert.ok(catalogs["/ipv6/route"].every(row => row.active === "true" && row.distance === "10"))
assert.equal(catalogs["/ipv6/settings"][0]["multipath-hash-policy"], "l4")
const inverseArgs = { operationId: v6Result.operation.id }
catalogs[v6Mangle][0]["new-routing-mark"] = "external-mark"
await assert.rejects(invokeMcpTool(principal, "mm_operation_recovery_plan", inverseArgs), { code: "STATE_CONFLICT" })
catalogs[v6Mangle][0]["new-routing-mark"] = "NEVER_NATIVE_POLICY_MARK"
const v6Inverse = await invokeMcpTool(principal, "mm_operation_recovery_plan", inverseArgs) as { planId: string }
assert.ok(!JSON.stringify(v6Inverse).includes("NEVER_NATIVE_POLICY_MARK"))
const v6InverseOffset = commands.length
assert.equal((await apply(v6Inverse.planId)).state, "applied")
assert.deepEqual(commands[v6InverseOffset], { method: "POST", path: "/ipv6/settings/set" })
assert.deepEqual(catalogs["/ipv6/route"], v6Baseline)
assert.deepEqual(catalogs["/ipv6/settings"], v6Settings)
assert.ok(![...readPaths].some(path => path === "/ip/settings" || path.startsWith("/ip/firewall/")))
console.log("ECMP IPv6 SQL/HTTP: family-specific reads, policy drift refusal, route/hash apply and inverse")
const rpfArgs = { serverId, mode: "strict", acknowledgeAsymmetry: true }
const manglePath = "/ip/firewall/mangle"
const beforeRpf = writes, beforeIpv6 = polls
await assert.rejects(invokeMcpTool(principal, "mm_rpf_plan", { ...rpfArgs, family: "ipv6" }), { code: "UNSUPPORTED_CAPABILITY" })
assert.equal(polls, beforeIpv6)
catalogs[rulePath][0].disabled = "false"
await assert.rejects(invokeMcpTool(principal, "mm_rpf_plan", rpfArgs), { code: "STATE_CONFLICT" })
const strictSettings = { serverId, fields: { "rp-filter": "strict", "ipv4-multipath-hash-policy": "l4" } }
await assert.rejects(invokeMcpTool(principal, "mm_ip_settings_update_plan", strictSettings), { code: "STATE_CONFLICT" })
catalogs[rulePath][0].disabled = "true"
catalogs[manglePath] = [{ ".id": "*mark", chain: "prerouting", action: "mark-routing", disabled: "false", "new-routing-mark": "fixture-mark" }]
await assert.rejects(invokeMcpTool(principal, "mm_rpf_plan", rpfArgs), { code: "STATE_CONFLICT" })
const loose = await invokeMcpTool(principal, "mm_rpf_plan", { ...rpfArgs, mode: "loose" }) as { changes: { warnings: string[] } }
assert.ok(loose.changes.warnings.some(warning => warning.includes("policy routing")))
catalogs[manglePath][0].disabled = "true"
const strict = await invokeMcpTool(principal, "mm_rpf_plan", rpfArgs) as { planId: string }
const genericStrict = await invokeMcpTool(principal, "mm_ip_settings_update_plan", strictSettings) as { planId: string }
await approvePlan(strict.planId, actor, true)
await approvePlan(genericStrict.planId, actor, true)
catalogs[manglePath][0]["new-routing-mark"] = "changed-after-approval"
const staleRpf = await applyPlan(principal, strict.planId, randomUUID())
assert.equal((await wait(staleRpf.id)).state, "rejected")
assert.equal((await wait((await applyPlan(principal, genericStrict.planId, randomUUID())).id)).state, "rejected")
assert.equal(writes, beforeRpf)
catalogs[manglePath] = []
catalogs["/ip/settings"][0]["rp-filter"] = "loose"
const disableRpf = await invokeMcpTool(principal, "mm_rpf_plan", { ...rpfArgs, mode: "disabled" }) as { planId: string }
assert.equal((await apply(disableRpf.planId)).state, "applied")
assert.deepEqual(catalogs["/ip/settings"], originalSettings)
catalogs["/ip/settings"][0]["rp-filter"] = "strict"
const leaveStrict = await invokeMcpTool(principal, "mm_rpf_plan", { ...rpfArgs, mode: "no" }) as { planId: string }
const leftStrict = await apply(leaveStrict.planId)
assert.equal(leftStrict.state, "applied")
const inverseRpfArgs = { operationId: leftStrict.operation.id }
const beforeInverseRpf = writes
catalogs[manglePath] = [{ ".id": "*late-mark", action: "mark-routing", disabled: "false" }]
await assert.rejects(invokeMcpTool(principal, "mm_operation_recovery_plan", inverseRpfArgs), { code: "STATE_CONFLICT" })
catalogs[manglePath] = []
const legacyRpf = await invokeMcpTool(principal, "mm_operation_recovery_plan", inverseRpfArgs) as { planId: string }
const legacyRpfEvidence = await evidence(legacyRpf.planId)
legacyRpfEvidence.snapshots = legacyRpfEvidence.snapshots.filter(snapshot => !snapshot.resource.startsWith("routing_policy:"))
await replace(legacyRpf.planId, legacyRpfEvidence)
await approvePlan(legacyRpf.planId, actor, true)
await assert.rejects(applyPlan(principal, legacyRpf.planId, randomUUID()), { code: "PLAN_EXPIRED" })
const staleInverseRpf = await invokeMcpTool(principal, "mm_operation_recovery_plan", inverseRpfArgs) as { planId: string }
await approvePlan(staleInverseRpf.planId, actor, true)
catalogs[manglePath] = [{ ".id": "*late-mark", action: "mark-routing", disabled: "false" }]
assert.equal((await wait((await applyPlan(principal, staleInverseRpf.planId, randomUUID())).id)).state, "rejected")
assert.equal(writes, beforeInverseRpf)
catalogs[manglePath] = []
const safeInverseRpf = await invokeMcpTool(principal, "mm_operation_recovery_plan", inverseRpfArgs) as { planId: string }
assert.equal((await apply(safeInverseRpf.planId)).state, "applied")
assert.equal(catalogs["/ip/settings"][0]["rp-filter"], "strict")
catalogs["/ip/settings"] = structuredClone(originalSettings)
console.log("RPF inverse SQL/HTTP: active policy, legacy plans and post-approval drift refused; exact strict restoration under verified policy")
console.log("RPF SQL/HTTP: explicit IPv6 refusal, strict policy admission and drift, loose warning, disabled alias")
for (const [tool, args, path] of [
["mm_rpf_plan", { serverId, mode: "loose", acknowledgeAsymmetry: true }, "/ip/settings"],
["mm_ip_settings_update_plan", { serverId, fields: { "rp-filter": "loose" } }, "/ip/settings"],
["mm_ipv6_settings_update_plan", { serverId, fields: { "multipath-hash-policy": "l4" } }, "/ipv6/settings"],
["mm_ipv4_route_update_plan", { serverId, id: "*route0", fields: { scope: 30 } }, "/ip/route"],
] as const) {
const baseline = structuredClone(catalogs[path]), commandOffset = commands.length
const forward = await invokeMcpTool(principal, tool, args) as { planId: string }, result = await apply(forward.planId)
assert.equal(result.state, "applied", tool)
const inverse = await invokeMcpTool(principal, "mm_operation_recovery_plan", { operationId: result.operation.id }) as { planId: string }
assert.equal((await apply(inverse.planId)).state, "applied", tool)
assert.deepEqual(catalogs[path], baseline, `${tool}: exact singleton/zero inverse`)
if (settingsPaths.includes(path)) assert.deepEqual(commands.slice(commandOffset), [
{ method: "POST", path: path + "/set" }, { method: "POST", path: path + "/set" },
])
}
for (const fault of ["ignored", "lost"]) {
const forward = await invokeMcpTool(principal, "mm_ip_settings_update_plan", { serverId, fields: { "rp-filter": "loose" } }) as { planId: string }
const commandOffset = commands.length
mode = fault
try {
const result = await apply(forward.planId)
assert.notEqual(result.state, "applied")
assert.notEqual(result.verification, "passed")
assert.deepEqual(commands.slice(commandOffset), [{ method: "POST", path: "/ip/settings/set" }])
const observed = await invokeMcpTool(principal, "mm_operation_reconcile", { operationId: result.operation.id }) as { automaticRetry: boolean }
assert.equal(observed.automaticRetry, false)
await noReplay(forward.planId, result, commandOffset + 1)
} finally {
mode = "normal"
catalogs["/ip/settings"] = structuredClone(originalSettings)
}
}
catalogs[filterPath] = base().map(row => ({ ...row, comment: "", log: "false" }))
const booleanBaseline = structuredClone(catalogs[filterPath])
const booleanPlan = await invokeMcpTool(principal, "mm_ip_filter_update_plan", { serverId, id: "*a", fields: { comment: "changed", log: true } }) as { planId: string }
const booleanResult = await apply(booleanPlan.planId); assert.equal(booleanResult.state, "applied")
const booleanInverse = await invokeMcpTool(principal, "mm_operation_recovery_plan", { operationId: booleanResult.operation.id }) as { planId: string }
assert.equal((await apply(booleanInverse.planId)).state, "applied"); assert.deepEqual(catalogs[filterPath], booleanBaseline)
catalogs["/ip/route"] = routeBaseline.map((row, index) => ({ ...row, gateway: `gre${index}`, "immediate-gw": `gre${index}` }))
const stableHopPlan = await invokeMcpTool(principal, "mm_ecmp_plan", { ...ecmpArgs, hashPolicy: undefined }) as { planId: string }
const beforeStableHops = writes
const stableHops = await apply(stableHopPlan.planId)
assert.equal(stableHops.state, "applied")
assert.equal(writes, beforeStableHops + 2)
assert.ok(catalogs["/ip/route"].every(row => row.distance === "10" && row.active === "true"))
console.log("ECMP interface-only next-hop: unchanged interface IDs verified before both writes and after apply")
const namedHopInverseArgs = { operationId: stableHops.operation.id }
const plansBeforeReplacement = (await query("SELECT count(*)::int AS count FROM mcp_plans WHERE connection_id=$1", [connectionId])).rows[0].count
catalogs["/interface"][0][".id"] = "*replaced-before-preparation"
await assert.rejects(invokeMcpTool(principal, "mm_operation_recovery_plan", namedHopInverseArgs), { code: "STATE_CONFLICT" })
assert.equal((await query("SELECT count(*)::int AS count FROM mcp_plans WHERE connection_id=$1", [connectionId])).rows[0].count, plansBeforeReplacement)
catalogs["/interface"][0][".id"] = "*gre0"
const oldHopInverse = await invokeMcpTool(principal, "mm_operation_recovery_plan", namedHopInverseArgs) as { planId: string }
const oldHopEvidence = await evidence(oldHopInverse.planId)
delete oldHopEvidence.healthChecks
await replace(oldHopInverse.planId, oldHopEvidence)
const beforeInverseHops = writes
const oldHopResult = await apply(oldHopInverse.planId)
assert.equal(oldHopResult.state, "rejected")
assert.equal(oldHopResult.error_code, "PLAN_EXPIRED")
assert.equal(writes, beforeInverseHops)
const staleHopInverse = await invokeMcpTool(principal, "mm_operation_recovery_plan", namedHopInverseArgs) as { planId: string; changes: unknown }
assert.ok(!JSON.stringify(staleHopInverse.changes).includes("nextHopInterface"))
afterIntent = async () => { catalogs["/interface"][0][".id"] = "*inverse-replacement" }
const refusedInverse = await apply(staleHopInverse.planId)
assert.equal(afterIntent, undefined)
assert.equal(refusedInverse.state, "rejected")
assert.equal(refusedInverse.error_code, "STATE_CONFLICT")
assert.equal(writes, beforeInverseHops)
catalogs["/interface"][0][".id"] = "*gre0"
const safeHopInverse = await invokeMcpTool(principal, "mm_operation_recovery_plan", namedHopInverseArgs) as { planId: string }
assert.equal((await apply(safeHopInverse.planId)).state, "applied")
assert.equal(writes, beforeInverseHops + 2)
assert.deepEqual(catalogs["/ip/route"].map(row => row.distance), ["10", "20"])
console.log("ECMP inverse: replaced interface refused before PATCH; intact interface restores unequal costs")
console.log("Native multi-catalog inverse SQL/HTTP: two route PATCHes plus singleton hash policy, exact reverse prefix, RPF/raw singleton, numeric zero, false and empty-comment originals preserved")
console.log("Native conservation SQL/HTTP: C/U/D and safe inverses; assigned-ID, membership, field and order failures never VERIFIED/replayed; honest two-step prefix stops before second intent; late source/dependency/protection/policy fence zero writes; stale operation revision refused. No CHR/opaque-field/forwarding claim.")
} finally {
pool.query = query as typeof pool.query; restoreAudits()
if (connectionId) await setConnectionStatus(connectionId, "paused", actor)
await query("DELETE FROM mcp_protections WHERE object_ref->>'serverId'=$1", [String(serverId)])
await query("UPDATE servers SET enabled=false WHERE id=$1", [serverId])
await new Promise<void>((resolve, reject) => router.close(error => error ? reject(error) : resolve()))
}
}