153 lines
14 KiB
TypeScript
153 lines
14 KiB
TypeScript
import assert from "node:assert/strict"
|
|
import { randomUUID } from "node:crypto"
|
|
import { setTimeout as delay } from "node:timers/promises"
|
|
import { SignJWT } from "jose"
|
|
import type { FastifyInstance } from "fastify"
|
|
import { mcpConnectionCreateSchema } from "@mmapp/contracts/mcp"
|
|
import { pool } from "../db/index.js"
|
|
import { env } from "../config.js"
|
|
import type { AuthUser } from "../lib/permissions.js"
|
|
import { createMcpConnection, authenticateMcpToken, setConnectionStatus } from "../modules/mcp/store.js"
|
|
import { invokeMcpTool, approvePlan, applyPlan, loadPlan } from "../modules/mcp/runtime.js"
|
|
import { decryptSecret } from "../modules/mcp/security.js"
|
|
import { appendClientEventRows, appendEvent, appendEvents, prepareEventRows } from "../modules/events/service/events-service.js"
|
|
import { PARTITION_WRITER_ADVISORY_KEY } from "../services/partition-write-transaction.js"
|
|
export async function verifyMcpEventsBatch(actor: AuthUser, app: FastifyInstance) {
|
|
const owner = { ...actor, id: "events-batch-owner", permissions: [...new Set([...actor.permissions, "mm:dashboard:read"])] }
|
|
const ids = (await pool.query("SELECT id FROM servers ORDER BY id")).rows.map(row => Number(row.id))
|
|
const issue = (serverIds = ids) => createMcpConnection(mcpConnectionCreateSchema.parse({ name: "Event journal fixture", mode: "autonomous", permissions: ["mm:dashboard:read"], scope: { serverIds, operationIds: ["events.batch.append", "operations.global.reconcile"], allowGlobal: true }, expiresAt: new Date(Date.now() + 1800000).toISOString(), limits: { callsPerMinute: 240, mutationsPerHour: 100, serversPerPlan: 1000, objectsPerPlan: 10000 } }), owner)
|
|
const issued = await issue(), principal = await authenticateMcpToken(issued.token), allIds: string[] = [], protections: string[] = []
|
|
const originalQuery = pool.query.bind(pool)
|
|
let afterIntent: (() => Promise<void>) | undefined
|
|
let loseAcknowledgement = false
|
|
pool.query = (async (...args: unknown[]) => {
|
|
const result = await (originalQuery as (...args: unknown[]) => Promise<unknown>)(...args)
|
|
if (typeof args[0] === "string" && args[0].includes("INSERT INTO mcp_audit") && (args[1] as unknown[] | undefined)?.[1] === "configuration.intent" && afterIntent) { const hook = afterIntent; afterIntent = undefined; await hook() }
|
|
if (typeof args[0] === "string" && args[0].includes("INSERT INTO mcp_audit") && (args[1] as unknown[] | undefined)?.[1] === "configuration.acknowledged" && loseAcknowledgement) { loseAcknowledgement = false; throw Error("Owned acknowledgement loss") }
|
|
return result
|
|
}) as typeof pool.query
|
|
const event = { level: "warning" as const, eventType: " fixture." + randomUUID() + " ", sourceModule: "system" as const, title: " review title ", message: " review message ", entityType: " server ", entityId: " 1 ", payload: { zero: 0, enabled: false, token: "NEVER_EVENT_PAYLOAD_TOKEN", nested: { text: "fixture" } }, createdAt: "2026-10-24T03:00:00+03:00" }
|
|
const internal = { ...event, eventType: "owned-internal-" + randomUUID() }
|
|
const prepare = async () => {
|
|
const result = await invokeMcpTool(principal, "mm_events_batch_append_plan", { events: [event, { ...event, level: "info", entityType: undefined, entityId: undefined, payload: undefined, createdAt: undefined }] }) as { planId: string; state: string }
|
|
assert.equal(result.state, "awaiting-approval", "Autonomous policy still requires human approval")
|
|
const plan = await loadPlan(result.planId), evidence = JSON.parse(await decryptSecret(plan.evidence.sealed)), input = evidence.steps[0].input
|
|
const rows = JSON.parse(await decryptSecret(input.batchCiphertext)); allIds.push(...rows.map((row: { id: string }) => row.id))
|
|
assert.equal((await pool.query("SELECT count(*)::int AS n FROM events WHERE id=ANY($1::text[])", [rows.map((row: { id: string }) => row.id)])).rows[0].n, 0)
|
|
await approvePlan(result.planId, owner, true)
|
|
return { ...result, input, rows }
|
|
}
|
|
const execute = async (planId: string) => {
|
|
const operation = await applyPlan(principal, planId, randomUUID())
|
|
for (let attempt = 0; attempt < 500; attempt++) { const row = (await pool.query("SELECT state FROM mcp_operations WHERE id=$1", [operation.id])).rows[0]; if (!["pending", "running"].includes(row.state)) return { ...operation, state: row.state }; await delay(20) }
|
|
throw Error("Owned event batch operation did not settle")
|
|
}
|
|
const reconcile = async (operationId: string) => (await invokeMcpTool(principal, "mm_global_operation_reconcile", { operationId }) as { results: Array<{ matchesDesiredState: boolean; configurationMatchesDesired: boolean }> }).results[0]
|
|
try {
|
|
const [designed] = prepareEventRows([{ ...event, eventType: "owned-admission-fixture" }]); allIds.push(designed.id)
|
|
const blocker = await pool.connect()
|
|
let write: Promise<void> | undefined
|
|
try {
|
|
await blocker.query("SELECT pg_advisory_lock($1::bigint)", [PARTITION_WRITER_ADVISORY_KEY])
|
|
let checked = false
|
|
write = Promise.allSettled([
|
|
appendClientEventRows([designed], async () => { checked = true }),
|
|
appendEvent(internal),
|
|
appendEvents([internal, internal]),
|
|
]).then(results => {
|
|
for (const result of results) if (result.status === "rejected") throw result.reason
|
|
})
|
|
void write.catch(() => {})
|
|
let waiting = false
|
|
for (let attempt = 0; attempt < 150 && !waiting; attempt++) {
|
|
waiting = (await pool.query(`SELECT 1 FROM pg_locks WHERE locktype='advisory' AND NOT granted
|
|
AND mode='ShareLock' AND classid=0 AND objid=$1::oid`, [PARTITION_WRITER_ADVISORY_KEY])).rowCount === 3
|
|
if (!waiting) await delay(20)
|
|
}
|
|
const prematureCallback = checked
|
|
const storedWhileLocked = (await pool.query("SELECT 1 FROM events WHERE id=$1", [designed.id])).rowCount
|
|
const internalWhileLocked = (await pool.query("SELECT 1 FROM events WHERE event_type=$1", [internal.eventType])).rowCount
|
|
await blocker.query("SELECT pg_advisory_unlock($1::bigint)", [PARTITION_WRITER_ADVISORY_KEY])
|
|
await write
|
|
assert(waiting, "Client, internal single and internal batch writers must wait for common SQL admission")
|
|
assert.equal(prematureCallback, false)
|
|
assert.equal(storedWhileLocked, 0)
|
|
assert.equal(internalWhileLocked, 0)
|
|
assert.equal(checked, true)
|
|
assert.equal((await pool.query("SELECT 1 FROM events WHERE id=$1", [designed.id])).rowCount, 1)
|
|
assert.equal((await pool.query("SELECT 1 FROM events WHERE event_type=$1", [internal.eventType])).rowCount, 3)
|
|
const [refused] = prepareEventRows([event]); allIds.push(refused.id)
|
|
const refusal = Error("Owned post-admission callback refusal")
|
|
await assert.rejects(appendClientEventRows([refused], async () => { throw refusal }), error => error === refusal)
|
|
assert.equal((await pool.query("SELECT 1 FROM events WHERE id=$1", [refused.id])).rowCount, 0)
|
|
} finally {
|
|
blocker.release(true)
|
|
await write?.catch(() => {})
|
|
}
|
|
const jwt = await new SignJWT({ sub: owner.id, apps: ["mm"], permissions: owner.permissions }).setProtectedHeader({ alg: "HS256" }).setIssuer(env.authIssuer).setExpirationTime("1h").sign(new TextEncoder().encode(env.jwtSecret))
|
|
const rest = await app.inject({ method: "POST", url: "/api/events/batch", headers: { authorization: "Bearer " + jwt }, payload: { events: [event] } })
|
|
assert.equal(rest.statusCode, 201, rest.body); assert.deepEqual(rest.json(), { ok: true, inserted: 1 })
|
|
const baseline = (await pool.query("SELECT * FROM events WHERE event_type=$1", [event.eventType.trim()])).rows[0]; allIds.push(baseline.id)
|
|
const prepared = await prepare(), applied = await execute(prepared.planId)
|
|
assert.equal(applied.state, "applied")
|
|
const saved = (await pool.query("SELECT * FROM events WHERE id=ANY($1::text[])", [prepared.rows.map((row: { id: string }) => row.id)])).rows
|
|
const first = saved.find(row => row.id === prepared.rows[0].id)
|
|
assert.deepEqual({ ...first, id: baseline.id }, baseline, "Shared REST/MCP preserves all supplied fields and timestamp semantics")
|
|
assert.equal((await reconcile(applied.id)).matchesDesiredState, true)
|
|
const portal = await app.inject({ method: "POST", url: `/api/mcp/operations/${applied.id}/reconcile`, headers: { authorization: "Bearer " + jwt }, payload: {} })
|
|
assert.equal(portal.statusCode, 200, portal.body); assert.equal(portal.json().results[0].matchesDesiredState, true)
|
|
await pool.query("UPDATE mcp_operation_steps SET state='applied' WHERE operation_id=$1", [applied.id])
|
|
const unverified = await reconcile(applied.id); assert.equal(unverified.configurationMatchesDesired, true); assert.equal(unverified.matchesDesiredState, false)
|
|
await pool.query("UPDATE mcp_operation_steps SET state='verified' WHERE operation_id=$1", [applied.id])
|
|
assert.equal((await reconcile(applied.id)).matchesDesiredState, true)
|
|
await pool.query("UPDATE events SET title='collateral change' WHERE id=$1", [first.id]); assert.equal((await reconcile(applied.id)).matchesDesiredState, false)
|
|
assert.equal((await applyPlan(principal, prepared.planId, randomUUID())).id, applied.id)
|
|
const lost = await prepare(); loseAcknowledgement = true
|
|
const unknown = await execute(lost.planId); assert.equal(unknown.state, "unknown"); assert.equal(loseAcknowledgement, false)
|
|
const lostComparison = await reconcile(unknown.id); assert.equal(lostComparison.configurationMatchesDesired, true); assert.equal(lostComparison.matchesDesiredState, false)
|
|
assert.equal((await applyPlan(principal, lost.planId, randomUUID())).id, unknown.id)
|
|
assert.equal((await pool.query("SELECT count(*)::int AS n FROM events WHERE id=ANY($1::text[])", [lost.rows.map((row: { id: string }) => row.id)])).rows[0].n, 2)
|
|
await assert.rejects(invokeMcpTool(principal, "mm_events_batch_append_plan", { events: [{ ...event, id: randomUUID() }] }))
|
|
await assert.rejects(invokeMcpTool(principal, "mm_events_batch_append_plan", { events: [{ ...event, message: "я".repeat(40000) }] }))
|
|
if (ids.length) {
|
|
const narrow = await issue(ids.slice(1))
|
|
try {
|
|
const limited = await authenticateMcpToken(narrow.token)
|
|
await assert.rejects(invokeMcpTool(limited, "mm_events_batch_append_plan", { events: [event] }))
|
|
await assert.rejects(invokeMcpTool(limited, "mm_global_operation_reconcile", { operationId: applied.id }))
|
|
}
|
|
finally { await setConnectionStatus(narrow.connection.id, "paused", owner) }
|
|
}
|
|
const foreign = await issue()
|
|
try { await assert.rejects(invokeMcpTool(await authenticateMcpToken(foreign.token), "mm_global_operation_reconcile", { operationId: applied.id })) }
|
|
finally { await setConnectionStatus(foreign.connection.id, "paused", owner) }
|
|
const savedPolicy = (await pool.query("SELECT policy FROM mcp_connections WHERE id=$1", [issued.connection.id])).rows[0].policy
|
|
await pool.query("UPDATE mcp_connections SET policy=jsonb_set(policy,'{limits,objectsPerPlan}',$2::jsonb) WHERE id=$1", [issued.connection.id, JSON.stringify(ids.length + 2)])
|
|
try { await assert.rejects(invokeMcpTool(principal, "mm_events_batch_append_plan", { events: [event, event] }), (error: { code?: string }) => error.code === "PLAN_LIMIT_EXCEEDED") }
|
|
finally { await pool.query("UPDATE mcp_connections SET policy=$2::jsonb WHERE id=$1", [issued.connection.id, JSON.stringify(savedPolicy)]) }
|
|
await pool.query("UPDATE mcp_connections SET policy=jsonb_set(policy,'{scope,operationIds}',$2::jsonb) WHERE id=$1", [issued.connection.id, JSON.stringify(savedPolicy.scope.operationIds.filter((id: string) => id !== "events.batch.append"))])
|
|
try { await assert.rejects(invokeMcpTool(principal, "mm_global_operation_reconcile", { operationId: applied.id }), (error: { code?: string }) => error.code === "PERMISSION_DENIED") }
|
|
finally { await pool.query("UPDATE mcp_connections SET policy=$2::jsonb WHERE id=$1", [issued.connection.id, JSON.stringify(savedPolicy)]) }
|
|
for (const scenario of ["protection", "event-protection", "pause"] as const) {
|
|
const staged = await prepare()
|
|
afterIntent = async () => {
|
|
if (scenario === "pause") await setConnectionStatus(issued.connection.id, "paused", owner)
|
|
else { const id = randomUUID(); protections.push(id); await pool.query("INSERT INTO mcp_protections(id,object_ref,created_by) VALUES($1,$2::jsonb,$3)", [id, JSON.stringify(scenario === "event-protection" ? { kind: "event", id: staged.rows[0].id, protection: "deny" } : { kind: "events-journal", id: "global", protection: "deny" }), owner.id]) }
|
|
}
|
|
assert.notEqual((await execute(staged.planId)).state, "applied")
|
|
assert.equal((await pool.query("SELECT count(*)::int AS n FROM events WHERE id=ANY($1::text[])", [staged.rows.map((row: { id: string }) => row.id)])).rows[0].n, 0)
|
|
assert.equal(afterIntent, undefined)
|
|
await pool.query("DELETE FROM mcp_protections WHERE id=ANY($1::uuid[])", [protections])
|
|
await setConnectionStatus(issued.connection.id, "active", owner)
|
|
}
|
|
const checkpoints = (await pool.query("SELECT intended_state,result FROM mcp_operation_steps WHERE operation_id IN(SELECT id FROM mcp_operations WHERE connection_id=$1)", [issued.connection.id])).rows
|
|
assert.ok(!JSON.stringify(checkpoints).includes("NEVER_EVENT_PAYLOAD_TOKEN")); assert.ok(!JSON.stringify(checkpoints).includes("review message"))
|
|
console.log("✓ Event batch SQL+REST: full fields/trim/time/payload parity, atomic stable UUID batch, human approval/full global scope, per-event object budget, JSON byte/depth bounds, exact VERIFIED read-only global/portal reconcile, cross-connection/narrow/original-action denial, lost acknowledgement unknown, tamper/no replay and after-intent pause/protection fences; journal entries only, no notifications")
|
|
} finally {
|
|
pool.query = originalQuery; await setConnectionStatus(issued.connection.id, "paused", owner)
|
|
await pool.query("DELETE FROM mcp_protections WHERE id=ANY($1::uuid[])", [protections])
|
|
await pool.query("DELETE FROM events WHERE id=ANY($1::text[])", [allIds])
|
|
await pool.query("DELETE FROM events WHERE event_type=$1", [internal.eventType])
|
|
}
|
|
}
|