Files
MikrotikManager/backend/src/services/server-probe-journal.sql.test.ts
T

329 lines
34 KiB
TypeScript

import assert from "node:assert/strict"
import { createCipheriv, createDecipheriv, createHmac, randomBytes, randomUUID } from "node:crypto"
import { basename, dirname, resolve } from "node:path"
import { readFile } from "node:fs/promises"
import { createSqlServerProbeJournal, ServerProbeJournalConflict, ServerProbeJournalOutcomeUnknown, type ProbeJournalConnections, type ProbeJournalCrypto, type ServerProbeJournalContext } from "./server-probe-journal.js"
import { SERVER_PROBE_JOURNAL_DDL } from "./server-probe-journal-schema.js"
import type { ServerProbeJournalBinding, ServerProbeReceipt } from "./server-probe-execution.js"
// Explicit disposable-runner admission BEFORE importing the configured pool.
// This file is not in the ordinary test profile and must never open a real DB.
assert.equal(process.env.MMAPP_OWN_PROBE_JOURNAL, "true", "Owned isolated probe-journal runner required")
const databaseUrl = new URL(process.env.DATABASE_URL ?? "")
assert.equal(databaseUrl.hostname, "127.0.0.1")
assert.equal(databaseUrl.pathname, "/postgres")
assert(["postgres:", "postgresql:"].includes(databaseUrl.protocol))
const stateDirectory = resolve(process.env.MCP_STATE_DIR ?? "")
assert(process.env.MCP_STATE_DIR && basename(dirname(stateDirectory)).startsWith("mm-mcp-test-"), "Disposable mm-mcp-test- state directory required")
const { pool, closePool } = await import("../db/index.js")
const { applyLegacyProbeFixtureMigrations: applySqlMigrations } = await import("../db/legacy-probe-fixture.js")
let cases = 0
const key = randomBytes(32)
const crypto: ProbeJournalCrypto = {
async encrypt(value) { const iv = randomBytes(12), cipher = createCipheriv("aes-256-gcm", key, iv), body = Buffer.concat([cipher.update(value), cipher.final()]); return Buffer.concat([iv, cipher.getAuthTag(), body]).toString("base64") },
async decrypt(value) { const bytes = Buffer.from(value, "base64"), cipher = createDecipheriv("aes-256-gcm", key, bytes.subarray(0, 12)); cipher.setAuthTag(bytes.subarray(12, 28)); return Buffer.concat([cipher.update(bytes.subarray(28)), cipher.final()]).toString() },
async proof(domain, value) { return createHmac("sha256", key).update(domain).update(JSON.stringify(value)).digest("hex") },
}
const connections: ProbeJournalConnections = { async connect() { const client = await pool.connect(); return { async query(text, values) { return client.query(text, values) }, release(destroy) { client.release(destroy) } } } }
const conflict = async (work: Promise<unknown>) => { await assert.rejects(work); cases++ }
try {
assert.equal((await pool.query("SELECT current_database() AS name")).rows[0].name, "postgres")
await applySqlMigrations(pool)
// The terminal restore test restarts normal application scheduling. Keep
// this journal-only fixture inert both before and after its dump/restore.
await pool.query("UPDATE traffic_settings SET enabled=false WHERE id=1")
await pool.query("UPDATE uptime_settings SET enabled=false,resources_enabled=false,ping_enabled=false,speed_enabled=false WHERE id=1")
assert.equal((await pool.query("SELECT count(*)::int AS n FROM servers")).rows[0].n, 0, "Fresh empty disposable database required")
const migration = await readFile(new URL("../../drizzle/0035_server_probe_journal.sql", import.meta.url), "utf8")
assert.equal(migration, "-- Durable diagnostic attempts; this migration registers no MCP action.\nBEGIN;\n" + SERVER_PROBE_JOURNAL_DDL + "\nINSERT INTO schema_migrations(id) VALUES ('0035_server_probe_journal') ON CONFLICT (id) DO NOTHING;\nCOMMIT;\n")
assert.equal((await pool.query("SELECT to_regclass('server_probe_executions') AS name")).rows[0].name, "server_probe_executions", "The real migration must install the journal")
await applySqlMigrations(pool)
assert.equal((await pool.query("SELECT count(*)::int AS n FROM schema_migrations WHERE id='0035_server_probe_journal'")).rows[0].n, 1, "Repeated migration discovery preserves the completed atomic marker")
const serverIds = (await pool.query("INSERT INTO servers(name,host,port,enabled) VALUES('probe-journal-source','127.0.0.1',9,true),('probe-journal-destination','127.0.0.1',9,true),('probe-journal-foreign','127.0.0.1',9,true) RETURNING id")).rows.map(row => Number(row.id))
const [source, destination, foreign] = serverIds
const fixture = async (kind: "jwt" | "local" | "mcp" = "jwt", tool: ServerProbeJournalContext["tool"] = "ping", customCrypto = crypto) => {
const issuedAt = Date.now(), expiresAt = issuedAt + 3600000, ownerId = `probe-owner-${randomUUID()}`, executionId = randomUUID()
const identity: ServerProbeJournalContext["identity"] = kind === "mcp" ? { kind, ownerId, executionId, connectionId: randomUUID(), credentialId: randomUUID(), operationName: "servers.probes.run", issuedAt, expiresAt } : { kind: "rest", authentication: kind, ownerId, executionId, requestId: `request-${randomUUID()}`, issuedAt, expiresAt }
const scope = tool === "bandwidth" ? [source, destination] : [source]
if (identity.kind === "mcp") {
const policy = { id: identity.connectionId, version: 1, status: "active", mode: "approval", permissions: ["mm:servers:write"], expiresAt: new Date(expiresAt).toISOString(), scope: { serverIds: scope, operationIds: [identity.operationName], allowGlobal: false } }
await pool.query("INSERT INTO mcp_connections(id,name,owner_id,owner_permissions,policy,generation,owner_valid_until) VALUES($1,'probe SQL fixture',$2,$3::jsonb,$4::jsonb,'probe-generation',$5)", [identity.connectionId, ownerId, JSON.stringify(["mm:servers:write"]), JSON.stringify(policy), new Date(expiresAt)])
await pool.query("INSERT INTO mcp_credentials(id,connection_id,digest,kind,expires_at) VALUES($1,$2,$3,'bearer',$4)", [identity.credentialId, identity.connectionId, randomUUID(), new Date(expiresAt)])
const planId = randomUUID()
await pool.query("INSERT INTO mcp_plans(id,connection_id,operation_id,operation_version,policy_version,generation,payload,evidence,state,expires_at) VALUES($1,$2,$3,1,1,'probe-generation','{}','{}','executing',$4)", [planId, identity.connectionId, identity.operationName, new Date(expiresAt)])
await pool.query("INSERT INTO mcp_operations(id,plan_id,connection_id,idempotency_key,state) VALUES($1,$2,$3,$4,'running')", [executionId, planId, identity.connectionId, randomUUID()])
}
const context: ServerProbeJournalContext = { identity, sourceServerId: source, ...(tool === "bandwidth" ? { destinationServerId: destination } : {}), tool }
const binding: ServerProbeJournalBinding = { receiptId: randomUUID(), bindingProof: "a".repeat(64), ownerId, tool, serverIds: scope, transport: identity.kind, ...(identity.kind === "mcp" ? { connectionId: identity.connectionId } : { requestId: identity.requestId, issuedAt }) }
let active = true, authorityCalls = 0
const deps = { connections, crypto: customCrypto, async assertAuthority(actual: Readonly<ServerProbeJournalContext>, proof: string) { authorityCalls++; assert(active, "Synthetic trusted authority revoked"); assert.deepEqual(actual, context); assert.equal(proof, binding.bindingProof) } }
const journal = createSqlServerProbeJournal(context, deps)
const stage = async (instance = journal) => { const native = { ping: "native-ping", traceroute: "native-traceroute", bandwidth: "native-bandwidth", route: "native-route-read", dns: "manager-dns-TXT", mtu: "native-mtu-1500" }[tool]; await instance.intent({ receiptId: binding.receiptId, stageIndex: 1, kind: native, timeoutMs: 1000 }); await instance.received({ receiptId: binding.receiptId, stageIndex: 1, outcome: "received" }) }
const receipt = (count = 1): ServerProbeReceipt => ({ receiptId: binding.receiptId, tool, serverId: source, ...(tool === "bandwidth" ? { destinationServerId: destination } : {}), state: "completed", httpStatus: 200, output: "SYNTHETIC_PRIVATE_MEASUREMENT", data: { kind: tool, value: "SYNTHETIC_PRIVATE_MEASUREMENT" }, attemptedStages: count, completedStages: count, automaticReplay: false, forwardingVerified: false })
const quarantine = (instance = journal, attemptedStages = 1, completedStages = 0) => instance.quarantine({ ...binding, errorCode: "JOURNAL_UNKNOWN", attemptedStages, completedStages })
const anchor = async () => (await pool.query("SELECT * FROM server_probe_executions WHERE receipt_id=$1", [binding.receiptId])).rows[0]
return { context, binding, deps, journal, stage, receipt, quarantine, anchor, revoke() { active = false }, restore() { active = true }, calls: () => authorityCalls }
}
// Actual PostgreSQL transactions + synthetic crypto; no DNS/RouterOS callback.
for (const kind of ["jwt", "local", "mcp"] as const) for (const tool of ["ping", "traceroute", "bandwidth", "dns", "route", "mtu"] as const) {
const f = await fixture(kind, tool)
assert.equal(await f.journal.claim(f.binding), "claimed"); await f.stage(); await f.journal.finish(f.receipt())
const restarted = createSqlServerProbeJournal(f.context, f.deps)
assert.deepEqual(await restarted.read(f.binding.receiptId, f.binding.bindingProof), f.receipt())
assert.equal(await restarted.claim(f.binding), "exists")
const row = await f.anchor(); assert.equal(row.authentication, kind); assert.equal(row.actor_id, f.context.identity.ownerId)
assert.equal(row.mcp_connection_id === null, kind !== "mcp"); assert(!JSON.stringify(row).includes("SYNTHETIC_PRIVATE_MEASUREMENT")); assert(f.calls() > 0)
await conflict(f.journal.finish(f.receipt())); await conflict(f.journal.intent({ receiptId: f.binding.receiptId, stageIndex: 2, kind: "native-ping", timeoutMs: 1000 })); cases++
}
{
const f = await fixture("mcp", "bandwidth"), other = createSqlServerProbeJournal(f.context, f.deps)
assert.deepEqual((await Promise.all([f.journal.claim(f.binding), other.claim(f.binding)])).sort(), ["claimed", "exists"])
assert.equal((await pool.query("SELECT count(*)::int AS n FROM server_probe_executions WHERE execution_id=$1", [f.context.identity.executionId])).rows[0].n, 1)
await conflict(createSqlServerProbeJournal(f.context, f.deps).claim({ ...f.binding, receiptId: randomUUID() }))
for (const binding of [{ ...f.binding, ownerId: "foreign" }, { ...f.binding, connectionId: randomUUID() }, { ...f.binding, serverIds: [source, foreign] }, { ...f.binding, serverIds: [source] }, { ...f.binding, bindingProof: "b".repeat(64) }]) await conflict(createSqlServerProbeJournal(f.context, f.deps).claim(binding))
cases++
}
// SQL authority must reject actual out-of-scope, disabled and revoked rows,
// even when the external callback is permissive and remains live.
for (const mutation of ["scope", "operation", "owner", "credential", "credential-expired", "owner-expired", "observer", "policy-expired", "operation-state"] as const) {
const f = await fixture("mcp", "bandwidth"), id = f.context.identity; assert.equal(id.kind, "mcp"); if (id.kind !== "mcp") throw Error("fixture")
if (mutation === "scope") await pool.query("UPDATE mcp_connections SET policy=jsonb_set(policy,'{scope,serverIds}',$2::jsonb) WHERE id=$1", [id.connectionId, JSON.stringify([source])])
if (mutation === "operation") await pool.query("UPDATE mcp_connections SET policy=jsonb_set(policy,'{scope,operationIds}','[]') WHERE id=$1", [id.connectionId])
if (mutation === "owner") await pool.query("UPDATE mcp_connections SET owner_id='foreign' WHERE id=$1", [id.connectionId])
if (mutation === "credential") await pool.query("UPDATE mcp_credentials SET revoked_at=clock_timestamp() WHERE id=$1", [id.credentialId])
if (mutation === "credential-expired") await pool.query("UPDATE mcp_credentials SET expires_at=clock_timestamp()-interval '1 second' WHERE id=$1", [id.credentialId])
if (mutation === "owner-expired") await pool.query("UPDATE mcp_connections SET owner_valid_until=clock_timestamp()-interval '1 second' WHERE id=$1", [id.connectionId])
if (mutation === "observer") await pool.query("UPDATE mcp_connections SET policy=jsonb_set(policy,'{mode}','\"observer\"') WHERE id=$1", [id.connectionId])
if (mutation === "policy-expired") await pool.query("UPDATE mcp_connections SET policy=jsonb_set(policy,'{expiresAt}',to_jsonb((clock_timestamp()-interval '1 second')::text)) WHERE id=$1", [id.connectionId])
if (mutation === "operation-state") await pool.query("UPDATE mcp_operations SET state='applied' WHERE id=$1", [id.executionId])
await conflict(f.journal.claim(f.binding)); assert.equal(await f.anchor(), undefined)
}
for (const disabled of [source, destination]) {
const f = await fixture("mcp", "bandwidth"); await pool.query("UPDATE servers SET enabled=false WHERE id=$1", [disabled])
try { await conflict(f.journal.claim(f.binding)) } finally { await pool.query("UPDATE servers SET enabled=true WHERE id=$1", [disabled]) }
}
{
const f = await fixture("mcp", "bandwidth"), peer = await fixture("mcp", "bandwidth")
assert(f.context.identity.kind === "mcp" && peer.context.identity.kind === "mcp")
const id = f.context.identity
for (const context of [
{ ...f.context, sourceServerId: foreign },
{ ...f.context, destinationServerId: foreign },
{ ...f.context, identity: { ...id, executionId: randomUUID() } },
{ ...f.context, identity: { ...id, credentialId: peer.context.identity.credentialId } },
{ ...f.context, identity: { ...id, connectionId: peer.context.identity.connectionId } },
{ ...f.context, identity: { ...id, ownerId: peer.context.identity.ownerId } },
] satisfies ServerProbeJournalContext[]) {
const identity = context.identity; assert(identity.kind === "mcp")
const binding = { ...f.binding, receiptId: randomUUID(), ownerId: identity.ownerId, connectionId: identity.connectionId, serverIds: [context.sourceServerId, context.destinationServerId!] }
// Deliberately permissive callback isolates the actual SQL owner/scope join.
await conflict(createSqlServerProbeJournal(context, { ...f.deps, async assertAuthority() {} }).claim(binding))
}
assert.equal(await f.anchor(), undefined)
}
{
const f = await fixture("mcp"), id = f.context.identity; assert(id.kind === "mcp")
await f.journal.claim(f.binding)
await pool.query("UPDATE mcp_credentials SET revoked_at=clock_timestamp() WHERE id=$1", [id.credentialId])
await conflict(f.journal.intent({ receiptId: f.binding.receiptId, stageIndex: 1, kind: "native-ping", timeoutMs: 1000 }))
await f.quarantine(f.journal, 0, 0)
await pool.query("UPDATE mcp_credentials SET revoked_at=NULL WHERE id=$1", [id.credentialId])
assert.equal((await createSqlServerProbeJournal(f.context, f.deps).read(f.binding.receiptId, f.binding.bindingProof))?.state, "unknown"); cases++
}
{
const f = await fixture("local", "mtu"); await f.journal.claim(f.binding)
await conflict(pool.query("INSERT INTO server_probe_execution_stages(receipt_id,stage_index,kind,timeout_ms) VALUES($1,2,'native-mtu-1500',1000)", [f.binding.receiptId]))
await conflict(f.journal.intent({ receiptId: f.binding.receiptId, stageIndex: 2, kind: "native-mtu-1492", timeoutMs: 1000 }))
await conflict(f.journal.intent({ receiptId: f.binding.receiptId, stageIndex: 1, kind: "native-mtu-1492", timeoutMs: 1000 }))
await f.journal.intent({ receiptId: f.binding.receiptId, stageIndex: 1, kind: "source-dns-A", timeoutMs: 1000 })
await conflict(pool.query("INSERT INTO server_probe_execution_stages(receipt_id,stage_index,kind,timeout_ms) VALUES($1,2,'native-mtu-1500',1000)", [f.binding.receiptId]))
await conflict(f.journal.intent({ receiptId: f.binding.receiptId, stageIndex: 2, kind: "native-mtu-1500", timeoutMs: 1000 }))
await f.journal.received({ receiptId: f.binding.receiptId, stageIndex: 1, outcome: "source-unavailable" })
await conflict(f.journal.received({ receiptId: f.binding.receiptId, stageIndex: 1, outcome: "received" }))
for (const [offset, size] of [1500, 1492].entries()) { const stageIndex = offset + 2; await f.journal.intent({ receiptId: f.binding.receiptId, stageIndex, kind: `native-mtu-${size}`, timeoutMs: 25000 }); await f.journal.received({ receiptId: f.binding.receiptId, stageIndex, outcome: "received" }) }
await conflict(f.journal.finish(f.receipt(2))); await f.journal.finish(f.receipt(3)); cases++
}
{
const f = await fixture(); await f.journal.claim(f.binding)
await conflict(pool.query("UPDATE server_probe_executions SET actor_id='foreign' WHERE receipt_id=$1", [f.binding.receiptId]))
await conflict(pool.query("DELETE FROM server_probe_executions WHERE receipt_id=$1", [f.binding.receiptId]))
await f.stage(); await conflict(pool.query("UPDATE server_probe_execution_stages SET timeout_ms=2 WHERE receipt_id=$1", [f.binding.receiptId]))
await conflict(pool.query("DELETE FROM server_probe_execution_stages WHERE receipt_id=$1", [f.binding.receiptId]))
await f.journal.finish(f.receipt())
await conflict(pool.query("UPDATE server_probe_executions SET state='claimed' WHERE receipt_id=$1", [f.binding.receiptId]))
await conflict(pool.query("UPDATE server_probe_executions SET receipt_ciphertext='tampered' WHERE receipt_id=$1", [f.binding.receiptId]))
await f.quarantine(f.journal, 1, 1)
await conflict(pool.query("UPDATE server_probe_execution_quarantines SET error_code='CANCELLED' WHERE receipt_id=$1", [f.binding.receiptId]))
await conflict(pool.query("DELETE FROM server_probe_execution_quarantines WHERE receipt_id=$1", [f.binding.receiptId]))
assert.equal((await f.journal.read(f.binding.receiptId, f.binding.bindingProof))?.state, "unknown"); cases++
}
// Corrupt encrypted material through the injected codec, never disable SQL
// immutability triggers merely to manufacture a tampered completed row.
for (const tamper of ["ciphertext", "proof", "envelope"] as const) {
const badCodec: ProbeJournalCrypto = { ...crypto,
async encrypt(value) { if (tamper === "ciphertext") { const bytes = Buffer.from(await crypto.encrypt(value), "base64"); bytes[bytes.length - 1] ^= 1; return bytes.toString("base64") } return crypto.encrypt(value) },
async decrypt(value) { const plaintext = await crypto.decrypt(value); return tamper === "envelope" ? plaintext.replace("SYNTHETIC_PRIVATE_MEASUREMENT", "forged") : plaintext },
async proof(domain, value) { return tamper === "proof" && domain === "receipt" ? "b".repeat(64) : crypto.proof(domain, value) },
}
const f = await fixture("jwt", "ping", badCodec); await f.journal.claim(f.binding); await f.stage(); await f.journal.finish(f.receipt())
// HMAC mismatch test changes verifier only, retaining the real stored HMAC.
const reader = createSqlServerProbeJournal(f.context, { ...f.deps, crypto: tamper === "proof" ? crypto : badCodec })
await assert.rejects(reader.read(f.binding.receiptId, f.binding.bindingProof), ServerProbeJournalConflict); cases++
}
{
const f = await fixture("jwt"); await f.journal.claim(f.binding); f.revoke()
await conflict(f.journal.intent({ receiptId: f.binding.receiptId, stageIndex: 1, kind: "native-ping", timeoutMs: 1000 }))
await f.quarantine(f.journal, 0, 0); await conflict(f.journal.read(f.binding.receiptId, f.binding.bindingProof)); f.restore()
assert.equal((await f.journal.read(f.binding.receiptId, f.binding.bindingProof))?.state, "unknown")
await conflict(f.journal.intent({ receiptId: f.binding.receiptId, stageIndex: 1, kind: "native-ping", timeoutMs: 1000 })); cases++
}
// COMMIT really reaches PostgreSQL first. The wrapper then drops its ACK.
// Instrument exact commands: no rollback or retry is allowed after COMMIT.
for (const lost of ["claim", "intent", "received", "finish", "quarantine"] as const) {
const f = await fixture("mcp"), statements: string[] = [], destroyed: boolean[] = []
let armed = false
const dropping: ProbeJournalConnections = { async connect() { const client = await pool.connect(); return { async query(text, values) { statements.push(text); const result = await client.query(text, values); if (text === "COMMIT" && armed) throw new Error("Synthetic lost committed ACK"); return result }, release(destroy) { destroyed.push(!!destroy); client.release(destroy) } } } }
const uncertain = createSqlServerProbeJournal(f.context, { ...f.deps, connections: dropping })
if (lost !== "claim") { await f.journal.claim(f.binding); assert.equal(await uncertain.claim(f.binding), "exists"); statements.length = 0; destroyed.length = 0 }
if (["received", "finish"].includes(lost)) await f.journal.intent({ receiptId: f.binding.receiptId, stageIndex: 1, kind: "native-ping", timeoutMs: 1000 })
if (lost === "finish") await f.journal.received({ receiptId: f.binding.receiptId, stageIndex: 1, outcome: "received" })
armed = true
const work = lost === "claim" ? uncertain.claim(f.binding) : lost === "intent" ? uncertain.intent({ receiptId: f.binding.receiptId, stageIndex: 1, kind: "native-ping", timeoutMs: 1000 }) : lost === "received" ? uncertain.received({ receiptId: f.binding.receiptId, stageIndex: 1, outcome: "received" }) : lost === "finish" ? uncertain.finish(f.receipt()) : f.quarantine(uncertain, 0, 0)
await assert.rejects(work, ServerProbeJournalOutcomeUnknown)
assert.equal(statements.filter(text => text === "COMMIT").length, 1); assert.equal(statements.filter(text => text === "ROLLBACK").length, 0); assert.deepEqual(destroyed, [true])
const row = await f.anchor(); assert.equal(row.state, lost === "finish" ? "completed" : "claimed")
const stages = (await pool.query("SELECT * FROM server_probe_execution_stages WHERE receipt_id=$1", [f.binding.receiptId])).rows
assert.equal(stages.length, ["intent", "received", "finish"].includes(lost) ? 1 : 0)
if (lost === "received" || lost === "finish") assert.equal(stages[0].state, "received")
await f.quarantine(f.journal, lost === "finish" || lost === "received" ? 1 : 0, lost === "finish" ? 1 : 0)
const restarted = createSqlServerProbeJournal(f.context, f.deps)
assert.equal(await restarted.claim(f.binding), "exists")
assert.equal((await restarted.read(f.binding.receiptId, f.binding.bindingProof))?.state, "unknown")
assert.equal((await f.anchor()).state, lost === "finish" ? "completed" : "claimed", "Quarantine overlay must not rewrite completed receipt")
await conflict(restarted.intent({ receiptId: f.binding.receiptId, stageIndex: stages.length + 1, kind: "native-ping", timeoutMs: 1000 })); cases++
}
{
const { default: Fastify } = await import("fastify")
const { createServerProbeExecutor } = await import("./server-probe-execution.js")
const { createRestServerProbeAuthority } = await import("./server-probe-authority.js")
const { createServerProbeRestExecution } = await import("./server-probe-rest-execution.js")
let raw: unknown, dispatches = 0, authenticated = false
const actor = { id: "probe-http-owner", email: "[email protected]", name: "Synthetic", apps: [], permissions: ["mm:servers:write"], issuedAt: Date.now(), expiresAt: Date.now() + 300000 }
const execution = createServerProbeRestExecution({
authority: (user, request) => createRestServerProbeAuthority(user, request, { portal: async actual => { assert.equal(actual.id, actor.id) }, query: async (sql, values) => (await pool.query(sql, values)).rows }),
executor: access => createServerProbeExecutor({ ...access,
async getServer(id) { return [source, destination].includes(id) ? { id, enabled: true, host: "127.0.0.1", port: 9, username: "SYNTHETIC_USER", password: "SYNTHETIC_PASSWORD", useSsl: false, verifySsl: true, routingEndpointEpoch: "11111111-1111-4111-8111-111111111111" } : undefined },
async findDestination() { return { id: destination } },
seal: crypto.encrypt, open: crypto.decrypt, privateProof: crypto.proof,
async post(_server, _path, _body, _timeout, signal, fence) { signal.throwIfAborted(); await fence(); dispatches++; return structuredClone(raw) },
async routes(_server, _timeout, signal, fence) { signal.throwIfAborted(); await fence(); dispatches++; return structuredClone(raw) },
async dns(_name, _type, signal, fence) { signal.throwIfAborted(); await fence(); dispatches++; return structuredClone(raw) },
}),
journal: (context, deps) => createSqlServerProbeJournal(context, { ...deps, connections, crypto }),
})
const app = Fastify()
app.post<{ Params: { id: string } }>("/test/probes/:id", async (request, reply) => {
const result = await execution(request.params.id, request.body, { actor: authenticated ? actor : undefined, requestId: request.id, acceptedAt: Date.now(), authRequired: authenticated })
return reply.code(result.statusCode).send(result.body)
})
try {
const address = await app.listen({ host: "127.0.0.1", port: 0 })
for (const tool of ["ping", "traceroute", "bandwidth", "dns", "route", "mtu"]) {
raw = tool === "traceroute" ? [{ address: "127.0.0.1", loss: "0%", last: "1ms", avg: "1ms" }]
: tool === "bandwidth" ? [{ "tx-current": "1", "rx-current": "2" }]
: tool === "dns" ? ["127.0.0.1"]
: tool === "route" ? [{ ".id": "*1", "dst-address": "127.0.0.0/8", active: "true", gateway: "ether1", distance: "1" }]
: tool === "mtu" ? [{ seq: "0", status: "timeout" }]
: [{ seq: "0", ttl: "64", time: "1ms" }]
const response = await fetch(`${address}/test/probes/${source}`, { method: "POST", headers: { "Content-Type": "application/json" }, body: JSON.stringify({ tool, target: "127.0.0.1", bwRemoteAddress: "127.0.0.1", dstServerId: destination, callerIdentity: "ignored" }) })
const body = await response.json() as { output?: string }
assert.equal(response.status, 200, JSON.stringify(body)); assert.equal(typeof body.output, "string"); assert(!JSON.stringify(body).includes("SYNTHETIC_PASSWORD")); cases++
}
authenticated = true; raw = [{ seq: "0", ttl: "64", time: "1ms" }]
const response = await fetch(`${address}/test/probes/${source}`, { method: "POST", headers: { "Content-Type": "application/json" }, body: JSON.stringify({ tool: "ping", target: "127.0.0.1" }) })
assert.equal(response.status, 200, await response.text()); cases++
assert.equal(dispatches, 16, "Five simple tools, ten MTU requests, one JWT ping")
const rows = (await pool.query("SELECT authentication,mcp_connection_id,mcp_credential_id FROM server_probe_executions WHERE actor_id=$1", [actor.id])).rows
assert.equal(rows.length, 1); assert.equal(rows[0].authentication, "jwt"); assert.equal(rows[0].mcp_connection_id, null); assert.equal(rows[0].mcp_credential_id, null)
const locals = (await pool.query("SELECT count(*)::int AS n FROM server_probe_executions WHERE actor_id='local-operator' AND authentication='local'")).rows[0].n
assert.equal(locals, 6)
} finally { await app.close() }
}
{
const { createServerProbeJournalHistoryReader } = await import("./server-probe-journal-history.js")
const f = await fixture("mcp", "bandwidth"), id = f.context.identity; assert(id.kind === "mcp")
await f.journal.claim(f.binding); await f.stage(); await f.journal.finish(f.receipt())
const original = { context: f.context, receiptId: f.binding.receiptId, bindingProof: f.binding.bindingProof }
// Keep SQL/envelope identity original while execution-time authority is
// expired in its own clock. Historical admission is an independent fence.
await assert.rejects(createSqlServerProbeJournal(f.context, { ...f.deps, now: () => id.expiresAt + 1 }).read(f.binding.receiptId, f.binding.bindingProof))
let current = true, fences = 0
const history = createServerProbeJournalHistoryReader({ crypto, async assertReadAuthority(binding) { fences++; assert(current); assert.deepEqual(binding, original) } })
assert.deepEqual(await history.read(original), f.receipt()); assert(fences >= 6); cases++
current = false; await conflict(history.read(original)); current = true
await f.quarantine(f.journal, 1, 1); await conflict(history.read(original))
}
// The actual 0036 migration and request/watch implementation share this
// disposable database. Stop requests never delete or rewrite probe evidence.
{
const { createDiagnosticCancellationRequest, withDiagnosticCancellation } = await import("../modules/mcp/diagnostic-cancellation.js")
const { McpError } = await import("../modules/mcp/security.js")
const f = await fixture("mcp", "bandwidth"), id = f.context.identity
assert(id.kind === "mcp")
await pool.query("UPDATE mcp_plans SET evidence=$2::jsonb WHERE id=(SELECT plan_id FROM mcp_operations WHERE id=$1)", [id.executionId, JSON.stringify({ sealed: await crypto.encrypt(JSON.stringify({ target: { scope: "servers", serverIds: [source, destination] } })) })])
const principal = { connectionId: id.connectionId, credentialId: id.credentialId, credentialExpiresAt: new Date(id.expiresAt).toISOString() }
const request = createDiagnosticCancellationRequest({
connection: async (actual, client) => {
const row = (await (client ?? pool).query("SELECT * FROM mcp_connections WHERE id=$1", [actual.connectionId])).rows[0]
assert(row); return row
},
open: crypto.decrypt,
})
const context = { operationId: id.executionId, phase: "forward" as const, stepIndex: 0 }
// Production SQL read runs both before and after the work.
assert.equal(await withDiagnosticCancellation(context, async signal => { assert.equal(signal.aborted, false); return "running" }, { authorize: async () => {} }), "running"); cases++
assert.equal((await request(principal, id.executionId)).cancelRequested, true)
assert.equal((await request(principal, id.executionId)).cancelRequested, true)
assert.equal((await pool.query("SELECT count(*)::int AS n FROM mcp_audit WHERE action='diagnostic.cancel-requested' AND connection_id=$1 AND payload->>'operationId'=$2", [id.connectionId, id.executionId])).rows[0].n, 1, "Repeated cancellation creates exactly one audit event")
const row = (await pool.query("SELECT * FROM mcp_diagnostic_cancellations WHERE operation_id=$1", [id.executionId])).rows[0]
assert.equal(row.connection_id, id.connectionId); assert.equal(row.actor_id, id.ownerId)
assert.equal((await pool.query("SELECT count(*)::int AS n FROM mcp_diagnostic_cancellations WHERE operation_id=$1", [id.executionId])).rows[0].n, 1); cases++
let starts = 0
await assert.rejects(withDiagnosticCancellation(context, async () => { starts++ }, { authorize: async () => {} }), error => error instanceof McpError && error.code === "READ_CANCELLED")
assert.equal(starts, 0); cases++
await conflict(pool.query("UPDATE mcp_diagnostic_cancellations SET actor_id='foreign' WHERE operation_id=$1", [id.executionId]))
await conflict(pool.query("DELETE FROM mcp_diagnostic_cancellations WHERE operation_id=$1", [id.executionId]))
await conflict(pool.query("INSERT INTO mcp_diagnostic_cancellations(operation_id,connection_id,actor_id) VALUES($1,$2,$3)", [randomUUID(), id.connectionId, id.ownerId]))
const other = await fixture("mcp"), foreignIdentity = other.context.identity; assert(foreignIdentity.kind === "mcp")
await conflict(request({ connectionId: foreignIdentity.connectionId, credentialId: foreignIdentity.credentialId, credentialExpiresAt: new Date(foreignIdentity.expiresAt).toISOString() }, id.executionId))
await pool.query("UPDATE mcp_connections SET policy=jsonb_set(policy,'{mode}','\"observer\"') WHERE id=$1", [id.connectionId])
await conflict(request(principal, id.executionId))
await pool.query("UPDATE mcp_connections SET policy=jsonb_set(policy,'{mode}','\"approval\"') WHERE id=$1", [id.connectionId])
await pool.query("UPDATE mcp_operations SET state='applied' WHERE id=$1", [id.executionId])
assert.equal((await request(principal, id.executionId)).terminal, true); cases++
assert.equal((await pool.query("SELECT count(*)::int AS n FROM schema_migrations WHERE id='0036_diagnostic_cancellations'")).rows[0].n, 1)
}
// Full six-tool runner, paged DNS and both cancellation lanes now execute
// against the real public v2 registry in --probe-storage-fresh. This legacy
// schema profile must not replace that registered service with a fake one.
const authority = await (await import("../modules/mcp/routing-profile-authority.sql-helper.js")).acceptRoutingProfileAuthoritySql()
console.log("routing profile persistent authority actual SQL acceptance:", JSON.stringify(authority))
const partitionLease = await (await import("./server-partition-maintenance-lease.sql-helper.js")).acceptServerPartitionMaintenanceLeaseSql()
console.log("partition writer lease actual SQL boundary acceptance:", JSON.stringify(partitionLease))
const pollJournal = await (await import("./server-poll-journal.sql-helper.js")).acceptServerPollJournalSql()
console.log("server poll journal actual SQL proposal acceptance:", JSON.stringify({ cases: pollJournal, productionRegistration: false }))
const writerAdmission = await (await import("./partition-write-transaction.sql-helper.js")).acceptPartitionWriteTransactionSql()
console.log("actual same-backend partition writer admission:", JSON.stringify(writerAdmission))
const legacyRebuild = await (await import("./traffic-flow-facts-rebuild-cli.sql-helper.js")).acceptLegacyFlowFactsRebuildSql()
console.log("actual legacy CLI safe rebuild integration:", JSON.stringify(legacyRebuild))
const persistentIntegration = await (await import("../modules/mcp/routing-profile-authority-integration.sql-helper.js")).acceptRoutingProfileAuthorityIntegrationSql()
console.log("persistent routing authority actual dispatcher/HTTP integration:", JSON.stringify(persistentIntegration))
await pool.query("UPDATE servers SET enabled=false WHERE id=ANY($1::int[])", [serverIds])
console.log(`server probe journal actual SQL acceptance: ${cases} cases passed; synthetic crypto, no native/DNS I/O`)
} finally {
// Rows intentionally survive until runner destroys its entire ephemeral DB:
// deleting append-only evidence here would invalidate the tested invariant.
await closePool()
}