import assert from "node:assert/strict" import { createHash, randomUUID } from "node:crypto" import { basename, dirname, resolve } from "node:path" import type { ServerProbeEndpoint } from "./server-probe-execution.js" import type { McpPrincipal, StoredOperation } from "../modules/mcp/store.js" /** Historical pre-registration fixture; active acceptance is now * modules/mcp/server-probe-all-tools.sql-helper.ts on the public v2 registry. * Not invoked by current profiles. Originally used after owned migrations. * Uses the actual public tool dispatcher, approval/admission, leased async * runner, cancellation watcher, encrypted plans, SQL journal and history. * Only provider I/O/endpoints are synthetic. Registers nothing on import. * No cleanup/retention, production routes, external DNS or RouterOS access. */ export async function acceptServerProbeSqlRunner() { assert.equal(process.env.MMAPP_OWN_PROBE_JOURNAL, "true") const url = new URL(process.env.DATABASE_URL ?? "") assert.equal(url.hostname, "127.0.0.1"); assert.equal(url.pathname, "/postgres") assert(["postgres:", "postgresql:"].includes(url.protocol)) assert(process.env.MCP_STATE_DIR && basename(dirname(resolve(process.env.MCP_STATE_DIR))).startsWith("mm-mcp-test-")) const { pool } = await import("../db/index.js") const { add, listActions } = await import("../modules/mcp/actions.js") const { registerMcpService } = await import("../modules/mcp/service-registry.js") const { createServerProbeMcpAdapter } = await import("../modules/mcp/server-probe-actions.js") const { createServerProbeExecutor } = await import("./server-probe-execution.js") const { SERVER_PROBE_OPERATION, SERVER_PROBE_RECEIPT_READ_OPERATION } = await import("./server-probe-authority.js") const { invokeMcpTool, approvePlan, applyPlan } = await import("../modules/mcp/runtime.js") const { requestDiagnosticCancellation } = await import("../modules/mcp/diagnostic-cancellation.js") const security = await import("../modules/mcp/security.js") const { mcpConnectionPolicySchema } = await import("@mmapp/contracts/mcp") assert(!listActions().some(action => action.operation.id === SERVER_PROBE_OPERATION), "Production diagnostic must remain unregistered before this test-only setup") assert.equal((await pool.query("SELECT current_database() AS name")).rows[0].name, "postgres") const previousMcpEnabled = (await pool.query("SELECT enabled FROM mcp_settings WHERE id=1")).rows[0].enabled as boolean const ids = (await pool.query("INSERT INTO servers(name,host,port,enabled) VALUES('runner-synthetic-source','127.0.0.1',9,true),('runner-synthetic-destination','127.0.0.1',9,true) RETURNING id")).rows.map(row => Number(row.id)) const endpoints = ids.map((id): ServerProbeEndpoint => ({ id, enabled: true, host: "127.0.0.1", port: 9, useSsl: false, verifySsl: true, username: "SYNTHETIC_RUNNER_USERNAME", password: "SYNTHETIC_RUNNER_PASSWORD", routingEndpointEpoch: randomUUID() })) try { await pool.query("UPDATE mcp_settings SET enabled=true WHERE id=1") let dispatches = 0, mode: "normal" | "before" | "during" = "normal" let enterCurrent: (() => void) | undefined, releaseCurrent: (() => void) | undefined, enteredProvider: (() => void) | undefined const provider = async (signal: AbortSignal, fence: () => Promise) => { await fence(); signal.throwIfAborted(); dispatches++ if (mode === "during") { enteredProvider?.() await new Promise((_resolve, reject) => { const abort = () => reject(signal.reason) if (signal.aborted) abort(); else signal.addEventListener("abort", abort, { once: true }) }) } } const adapter = createServerProbeMcpAdapter({ executor: input => createServerProbeExecutor({ ...input, getServer: async id => structuredClone(endpoints.find(endpoint => endpoint.id === id)), findDestination: async (_remote, explicit, scope) => scope.includes(explicit ?? ids[1]) ? { id: explicit ?? ids[1] } : undefined, seal: security.encryptSecret, open: security.decryptSecret, privateProof: (domain, value) => security.privateSecretFingerprint(security.fingerprint(value), "synthetic-runner:" + domain), post: async (_server, path, _body, _timeout, signal, fence) => { await provider(signal, fence) if (path === "/tool/bandwidth-test") return [{ "tx-current": "1000", "rx-current": "2000" }] if (path === "/tool/traceroute") return [{ address: "192.0.2.1", loss: "0%", last: "1ms", avg: "1ms" }] return [{ seq: "0", ttl: "64", time: "1ms", received: "1" }] }, routes: async (_server, _timeout, signal, fence) => { await provider(signal, fence); return [{ active: "true", "dst-address": "0.0.0.0/0", gateway: "192.0.2.254", distance: "1" }] }, dns: async (_name, type, signal, fence) => { await provider(signal, fence); return type === "TXT" ? [["PUBLIC_SYNTHETIC_MEASUREMENT_".repeat(180)], ["PUBLIC_SECOND_RECORD_".repeat(180)]] : ["192.0.2.1"] }, }), }) const runTool = "mm_test_owned_probe_plan", readTool = "mm_test_owned_probe_receipt_get" add(SERVER_PROBE_OPERATION, runTool, "Изолированная диагностика", "network", "mm:servers:write", "diagnostic", "servers", adapter.schema, { target: adapter.target, prepare: adapter.prepare }) add(SERVER_PROBE_RECEIPT_READ_OPERATION, readTool, "Изолированная квитанция", "servers", "mm:servers:read", "read", "servers", adapter.receiptSchema, { target: adapter.receiptTarget, read: adapter.readPage }) registerMcpService(SERVER_PROBE_OPERATION, { ...adapter.service, current: async input => { const result = await adapter.service.current(input) if (mode === "before") { enterCurrent?.(); await new Promise(resolve => { releaseCurrent = resolve }) } return result } }) let cases = 0 const fixture = async () => { const connectionId = randomUUID(), credentialId = randomUUID(), ownerId = `runner-owner-${randomUUID()}`, now = Date.now(), expires = now + 3600000 const permissions = ["mm:servers:write", "mm:servers:read", "mm:mcp:approve"] const policy = mcpConnectionPolicySchema.parse({ id: connectionId, version: 1, status: "active", mode: "approval", permissions, expiresAt: new Date(expires).toISOString(), scope: { serverIds: ids, operationIds: [SERVER_PROBE_OPERATION, SERVER_PROBE_RECEIPT_READ_OPERATION], allowGlobal: false }, limits: { callsPerMinute: 100, mutationsPerHour: 100, concurrentReads: 4, serversPerPlan: 2, objectsPerPlan: 20 } }) await pool.query("INSERT INTO mcp_connections(id,name,owner_id,owner_permissions,policy,generation,owner_valid_until,owner_claims_issued_at) VALUES($1,'synthetic runner',$2,$3::jsonb,$4::jsonb,$5,$6,$7)", [connectionId, ownerId, JSON.stringify(permissions), JSON.stringify(policy), await security.accessGeneration(), new Date(expires), new Date(now - 1000)]) await pool.query("INSERT INTO mcp_credentials(id,connection_id,digest,kind,expires_at) VALUES($1,$2,$3,'bearer',$4)", [credentialId, connectionId, randomUUID(), new Date(expires)]) const principal: McpPrincipal = { connectionId, credentialId, credentialExpiresAt: new Date(expires).toISOString() } return { principal, actor: { id: ownerId, email: "synthetic@example.invalid", name: "Synthetic", apps: ["mm"], permissions, issuedAt: now - 1000, expiresAt: expires } } } const terminal = async (id: string): Promise => { const until = Date.now() + 20000 while (Date.now() < until) { const row = (await pool.query("SELECT * FROM mcp_operations WHERE id=$1", [id])).rows[0] if (row && !["pending", "running"].includes(row.state)) return row await new Promise(resolve => setTimeout(resolve, 20)) } assert.fail("Actual asynchronous leased runner did not reach terminal state") } const prepare = async (f: Awaited>, tool: "ping" | "traceroute" | "bandwidth" | "dns" | "route" | "mtu") => { const request = tool === "bandwidth" ? { tool, bwRemoteAddress: endpoints[1].host, dstServerId: ids[1] } : tool === "dns" ? { tool, target: "synthetic.invalid", dnsType: "TXT" } : { tool, target: "192.0.2.1" } const before = dispatches const result = await invokeMcpTool(f.principal, runTool, { serverId: ids[0], request }) as { planId: string } assert.equal(dispatches, before, "Prepare cannot dispatch") const plan = (await pool.query("SELECT * FROM mcp_plans WHERE id=$1", [result.planId])).rows[0] assert.equal(typeof plan.payload.sealed, "string"); assert.equal(typeof plan.evidence.sealed, "string") assert(!JSON.stringify(plan).includes("SYNTHETIC_RUNNER_PASSWORD")) await approvePlan(result.planId, f.actor, true) return result.planId } for (const tool of ["ping", "traceroute", "bandwidth", "dns", "route", "mtu"] as const) { const f = await fixture(), planId = await prepare(f, tool), operation = await applyPlan(f.principal, planId, randomUUID()), done = await terminal(operation.id) assert.equal(done.state, "applied", `${tool}: ${JSON.stringify(done)}`) const result = done.result as { confirmedWrites: number; completedServiceSteps: number; diagnosticReceipt: { receiptId: string; attemptedStages: number; completedStages: number; stageBudget: number; sha256: string; totalPages: number } } assert.equal(result.confirmedWrites, 0); assert.equal(result.completedServiceSteps, 1) const receipt = result.diagnosticReceipt assert.equal(receipt.attemptedStages, receipt.completedStages); assert(receipt.attemptedStages > 0 && receipt.attemptedStages <= receipt.stageBudget) const steps = (await pool.query("SELECT * FROM mcp_operation_steps WHERE operation_id=$1", [done.id])).rows assert.equal(steps.length, 1); assert.equal(steps[0].state, "acknowledged"); assert.equal(steps[0].result.receiptId, receipt.receiptId) const anchor = (await pool.query("SELECT * FROM server_probe_executions WHERE execution_id=$1", [done.id])).rows[0] assert.equal(anchor.receipt_id, receipt.receiptId); assert.equal(anchor.mcp_credential_id, f.principal.credentialId) assert.equal((await pool.query("SELECT count(*)::int AS n FROM server_probe_execution_stages WHERE receipt_id=$1", [receipt.receiptId])).rows[0].n, receipt.attemptedStages) const beforeRead = dispatches, chunks: Buffer[] = [] for (let page = 0; page < receipt.totalPages; page++) { const value = await invokeMcpTool(f.principal, readTool, { operationId: done.id, receiptId: receipt.receiptId, page }) as { contentB64: string; sha256: string } assert.equal(value.sha256, receipt.sha256); chunks.push(Buffer.from(value.contentB64, "base64")) } const complete = Buffer.concat(chunks) assert.equal(createHash("sha256").update(complete).digest("hex"), receipt.sha256); assert.equal(JSON.parse(complete.toString()).tool, tool) if (tool === "dns") { assert(receipt.totalPages > 1, "Real historical pagination required") const rotatedId = randomUUID() await pool.query("INSERT INTO mcp_credentials(id,connection_id,digest,kind,expires_at) VALUES($1,$2,$3,'bearer',$4)", [rotatedId, f.principal.connectionId, randomUUID(), new Date(f.principal.credentialExpiresAt)]) await pool.query("UPDATE mcp_credentials SET revoked_at=now() WHERE id=$1", [f.principal.credentialId]) const rotated = { ...f.principal, credentialId: rotatedId } // Current access is a real read-only observer grant; historical reads // cannot rely on the original execution's write policy or credential. await pool.query("UPDATE mcp_connections SET policy=policy || $2::jsonb WHERE id=$1", [rotated.connectionId, JSON.stringify({ mode: "observer", permissions: ["mm:servers:read"], scope: { serverIds: ids, operationIds: [SERVER_PROBE_RECEIPT_READ_OPERATION], allowGlobal: false } })]) for (let page = 0; page < receipt.totalPages; page++) await invokeMcpTool(rotated, readTool, { operationId: done.id, receiptId: receipt.receiptId, page }) await assert.rejects(invokeMcpTool(f.principal, readTool, { operationId: done.id, receiptId: receipt.receiptId, page: 0 })) await pool.query("UPDATE mcp_connections SET policy=jsonb_set(policy,'{scope,serverIds}','[]') WHERE id=$1", [rotated.connectionId]) await assert.rejects(invokeMcpTool(rotated, readTool, { operationId: done.id, receiptId: receipt.receiptId, page: 1 })) } assert.equal(dispatches, beforeRead, "Historical pages and credential rotation cannot redispatch") const audit = (await pool.query("SELECT payload FROM mcp_audit WHERE connection_id=$1 AND action='operation.complete'", [f.principal.connectionId])).rows assert.equal(audit.length, 1); assert.equal(audit[0].payload.confirmedWrites, 0) assert.equal((await pool.query("SELECT count(*)::int AS n FROM mcp_audit WHERE connection_id=$1 AND action LIKE 'configuration.%'", [f.principal.connectionId])).rows[0].n, 0) cases++ } for (const cancellation of ["before", "during"] as const) { const f = await fixture(), planId = await prepare(f, "ping"), before = dispatches const entered = new Promise(resolve => { if (cancellation === "before") enterCurrent = resolve; else enteredProvider = resolve }) mode = cancellation try { const operation = await applyPlan(f.principal, planId, randomUUID()) await Promise.race([entered, new Promise((_resolve, reject) => { const timer = setTimeout(() => reject(Error("Runner did not reach cancellation barrier")), 10000); timer.unref() })]) const cancelled = await requestDiagnosticCancellation(f.principal, operation.id) assert.equal(cancelled.cancelRequested, true) releaseCurrent?.() const done = await terminal(operation.id), result = done.result as { confirmedWrites: number; completedServiceSteps: number; diagnosticProgress?: { attemptedStages: number; completedStages: number; state: string } } assert.equal(done.state, cancellation === "before" ? "rejected" : "unknown"); assert.equal(result.confirmedWrites, 0); assert.equal(result.completedServiceSteps, 0) assert.equal(dispatches - before, cancellation === "before" ? 0 : 1) if (cancellation === "during") { assert.equal(result.diagnosticProgress?.attemptedStages, 1); assert.equal(result.diagnosticProgress.completedStages, 0); assert.equal(result.diagnosticProgress.state, "unknown") assert.equal((await pool.query("SELECT count(*)::int AS n FROM server_probe_execution_quarantines WHERE execution_id=$1", [done.id])).rows[0].n, 1) } else { assert.equal(result.diagnosticProgress?.state, "cancelled") assert.equal(result.diagnosticProgress.attemptedStages, 0); assert.equal(result.diagnosticProgress.completedStages, 0) assert.equal((done.result as { needsReview: boolean }).needsReview, false) assert.equal((await pool.query("SELECT count(*)::int AS n FROM server_probe_executions WHERE execution_id=$1", [done.id])).rows[0].n, 0) } const duplicate = await applyPlan(f.principal, planId, operation.idempotency_key as string) assert.equal(duplicate.id, done.id); assert.equal(dispatches - before, cancellation === "before" ? 0 : 1) cases++ } finally { mode = "normal"; releaseCurrent?.(); releaseCurrent = undefined; enterCurrent = undefined; enteredProvider = undefined } } return { cases, providerDispatches: dispatches, realRunner: true, productionRegistration: false, externalNetwork: false } } finally { // The later restore profile can restart scheduling: leave only our own // fixture endpoints disabled, even after a failed acceptance assertion. await pool.query("UPDATE servers SET enabled=false WHERE id=ANY($1::int[])", [ids]) await pool.query("UPDATE mcp_settings SET enabled=$1 WHERE id=1", [previousMcpEnabled]) } }