52 lines
3.5 KiB
TypeScript
52 lines
3.5 KiB
TypeScript
import { fingerprint, McpError } from "./security.js"
|
|
import { readRosSnapshot, rosSnapshotVersion, type RosSnapshot, type RosStep } from "./resources.js"
|
|
import { verifyRosStepSnapshots } from "./ros-step-verification.js"
|
|
import { acknowledgedCreateId } from "./native-create-identity.js"
|
|
|
|
const key = (snapshot: Pick<RosSnapshot, "serverId" | "resource">) => `${snapshot.serverId}:${snapshot.resource}`
|
|
function valid(snapshot: RosSnapshot): boolean {
|
|
if (snapshot.projectionVersion !== rosSnapshotVersion(snapshot.resource) || typeof snapshot.sourceProof !== "string" || !/^[a-f0-9]{64}$/.test(snapshot.sourceProof) || !Array.isArray(snapshot.rows) || snapshot.fingerprint !== fingerprint(snapshot.rows)) return false
|
|
if (["ip_settings", "ipv6_settings"].includes(snapshot.resource)) return snapshot.rows.length === 1 && !!snapshot.rows[0] && typeof snapshot.rows[0] === "object" && !Array.isArray(snapshot.rows[0])
|
|
const ids = snapshot.rows.map(acknowledgedCreateId)
|
|
return ids.every(id => id !== undefined) && new Set(ids).size === ids.length
|
|
}
|
|
function matches(expected: RosSnapshot, current: RosSnapshot): boolean {
|
|
return valid(current) && key(expected) === key(current) && expected.sourceProof === current.sourceProof && expected.fingerprint === current.fingerprint
|
|
}
|
|
|
|
/** The baseline advances only after a complete, acknowledged projected change. */
|
|
export function createRosPrefix(snapshots: RosSnapshot[], read: typeof readRosSnapshot = readRosSnapshot) {
|
|
let expected = new Map<string, RosSnapshot>()
|
|
for (const snapshot of snapshots) {
|
|
if (!valid(snapshot)) throw new McpError("PLAN_EXPIRED", "Сетевой снимок имеет устаревший контракт; подготовьте новый план", 409)
|
|
const previous = expected.get(key(snapshot))
|
|
if (previous && !matches(previous, snapshot)) throw new McpError("STATE_CONFLICT", "Неоднозначный исходный сетевой снимок", 409)
|
|
expected.set(key(snapshot), structuredClone(snapshot))
|
|
}
|
|
const checkMap = async (map: Map<string, RosSnapshot>, beforeRead: () => Promise<unknown>, code: string) => {
|
|
for (const snapshot of map.values()) {
|
|
const current = await read(snapshot.serverId, snapshot.resource, beforeRead, snapshot.sourceProof)
|
|
if (!matches(snapshot, current)) throw new McpError(code, "Сетевой источник или ожидаемая конфигурация изменились; повтор записи запрещён", 409)
|
|
}
|
|
await beforeRead()
|
|
}
|
|
return {
|
|
async check(beforeRead: () => Promise<unknown> = async () => {}) {
|
|
await checkMap(expected, beforeRead, "STATE_CONFLICT")
|
|
},
|
|
before(step: RosStep): RosSnapshot {
|
|
const snapshot = expected.get(key(step))
|
|
if (!snapshot) throw new McpError("STATE_CONFLICT", "Нет исходного снимка сетевого шага", 409)
|
|
return structuredClone(snapshot)
|
|
},
|
|
async advance(step: RosStep, result: unknown, after: RosSnapshot, beforeRead: () => Promise<unknown> = async () => {}) {
|
|
const before = expected.get(key(step))
|
|
if (!before || !valid(after) || before.sourceProof !== after.sourceProof || !verifyRosStepSnapshots(step, result, before, after)) throw new McpError("VERIFICATION_FAILED", "Изменение сетевого каталога не совпало с подтверждённым шагом", 409)
|
|
const next = new Map(expected)
|
|
next.set(key(after), structuredClone(after))
|
|
await checkMap(next, beforeRead, "VERIFICATION_FAILED")
|
|
expected = next
|
|
},
|
|
}
|
|
}
|