import assert from "node:assert/strict" import { createHmac, randomUUID } from "node:crypto" import { lstat, mkdir, realpath } from "node:fs/promises" import { tmpdir } from "node:os" import { basename, dirname, isAbsolute, resolve } from "node:path" import type { Pool } from "pg" import type { BandwidthBinding, BandwidthJournal, BandwidthScope, BandwidthTransition } from "./bandwidth-admission.js" import type { ProbeStorageConnection } from "./server-probe-journal-maintenance.js" /** Owned fixture only. All DB/private module imports happen after this guard. */ export function assertOwnedBandwidthSql(): void { assert.equal(process.env.MMAPP_OWN_BANDWIDTH_ADMISSION, "true") const url = new URL(process.env.DATABASE_URL ?? "") assert(["postgres:", "postgresql:"].includes(url.protocol)) assert.equal(url.hostname, "127.0.0.1") assert.equal(url.username, "mcp_test") assert(url.password.length > 0) assert(Number.isSafeInteger(Number(url.port)) && Number(url.port) > 0 && Number(url.port) <= 65535) assert.equal(url.search, ""); assert.equal(url.hash, "") assert.equal(url.pathname, "/postgres") assert(process.env.MCP_STATE_DIR && isAbsolute(process.env.MCP_STATE_DIR)) assert.equal(basename(resolve(process.env.MCP_STATE_DIR)), "state") assert(basename(dirname(resolve(process.env.MCP_STATE_DIR))).startsWith("mm-mcp-test-")) } /** Create only the literal state child of the runner's existing private temp * directory. Never recursively create a path supplied by environment data. */ export async function prepareOwnedBandwidthState(): Promise { assertOwnedBandwidthSql() const state = resolve(process.env.MCP_STATE_DIR!), parent = dirname(state) const canonicalTmp = await realpath(tmpdir()), parentStat = await lstat(parent) assert(parentStat.isDirectory() && !parentStat.isSymbolicLink()) const canonicalParent = await realpath(parent) assert.equal(dirname(canonicalParent), canonicalTmp) assert.match(basename(canonicalParent), /^mm-mcp-test-[A-Za-z0-9_-]+$/) try { await mkdir(state, { mode: 0o700 }) } catch (error) { if ((error as NodeJS.ErrnoException).code !== "EEXIST") throw error } const stateStat = await lstat(state) assert(stateStat.isDirectory() && !stateStat.isSymbolicLink()) const canonicalState = await realpath(state) assert.equal(basename(canonicalState), "state") assert.equal(dirname(canonicalState), canonicalParent) } export function canonical(value: unknown): string { return JSON.stringify(value, (_key, item: unknown) => item && typeof item === "object" && !Array.isArray(item) ? Object.fromEntries(Object.entries(item).sort(([a], [b]) => a.localeCompare(b))) : item) } export const fixtureMac = (value: unknown): string => createHmac("sha256", "owned-bandwidth-synthetic-fixture-key-v1").update(canonical(value)).digest("hex") export const scope = (milliseconds = 10000): BandwidthScope => ({ deadlineAt: Date.now() + milliseconds }) export function fixtureBinding(source = "node-A", destination = "node-B", extra: Partial = {}): BandwidthBinding { const core = { attemptId: randomUUID(), callerKind: "rest-speed" as const, operationId: randomUUID(), source: { canonicalNodeId: source, endpointProof: fixtureMac(["endpoint", source]) }, destination: { canonicalNodeId: destination, endpointProof: fixtureMac(["endpoint", destination]) }, authorityProof: fixtureMac("authority"), authorityGeneration: "owned-authority-v1", controlGeneration: "owned-control-v1", expiresAt: Date.now() + 120000, durationSec: 2147483647, requestProof: fixtureMac("request"), commandProof: fixtureMac("command"), ...extra } return { ...core, bindingProof: fixtureMac(core) } } export const completionProof = (binding: BandwidthBinding): string => fixtureMac({ binding: binding.bindingProof, terminal: "done testing" }) export interface Harness { journal: BandwidthJournal register(binding: BandwidthBinding): void markNoSend(binding: BandwidthBinding): void fault(kind: "lost-commit" | "rollback-before-commit"): void lastPid(): number committedLosses(): number } export async function createOwnedBandwidthHarness(ownedPool?: Pool): Promise { assertOwnedBandwidthSql() const { pool } = await import("../db/index.js") const targetPool = ownedPool ?? pool const { createProbeStorageConnectionConnector } = await import("./server-probe-storage-connection.js") const { createBandwidthAdmissionSqlJournal } = await import("./bandwidth-admission-storage.js") const allowed = new Map(), noSend = new Set() let nextFault = "", pid = 0, losses = 0 const connector = createProbeStorageConnectionConnector(async () => { const raw = await targetPool.connect() pid = Number((await raw.query("SELECT pg_backend_pid() AS pid")).rows[0].pid) return raw }) const connect = async (window: BandwidthScope): Promise => { const owned = await connector(window) const fault = nextFault; nextFault = "" return { release: destroy => owned.release(destroy), async query(sql, values, options) { const commit = /^COMMIT\s*;?$/i.test(sql.trim()) if (commit && fault === "rollback-before-commit") throw Error("Owned failure BEFORE actual COMMIT") const result = await owned.query(sql, values, options) if (commit && fault === "lost-commit") { losses++; throw Error("Owned lost ACK AFTER actual COMMIT") } return result }, } } const dependencies = { connect, now: Date.now, authorize: async (binding: BandwidthBinding, window: BandwidthScope) => { assert(Date.now() < window.deadlineAt); window.signal?.throwIfAborted() assert.equal(allowed.get(binding.attemptId), binding.bindingProof) }, verifyEvidence: async (transition: BandwidthTransition, binding: BandwidthBinding, evidence: unknown, window: BandwidthScope) => { assert(Date.now() < window.deadlineAt); window.signal?.throwIfAborted() assert.equal(allowed.get(binding.attemptId), binding.bindingProof) if (transition === "complete") assert.deepEqual(evidence, { terminalProof: completionProof(binding) }) if (transition === "release-before-dispatch") { assert(noSend.has(binding.attemptId), "Only fixture-owned non-dispatched attempts may release") assert.equal((evidence as { proof?: string }).proof, "private-no-send") } }, eventProof: async (event: unknown) => fixtureMac(event), } // One physical SQL journal instance owns one local reserve ACK. The harness // multiplexes independent attempts without teaching fresh instances adoption. const journals = new Map() const owned = (binding: BandwidthBinding) => { let value = journals.get(binding.attemptId) if (!value) { value = createBandwidthAdmissionSqlJournal(dependencies); journals.set(binding.attemptId, value) } return value } const journal: BandwidthJournal = { reserve: (binding, window) => owned(binding).reserve(binding, window), arm: (binding, window) => owned(binding).arm(binding, window), complete: (binding, evidence, window) => owned(binding).complete(binding, evidence, window), releaseBeforeDispatch: (binding, evidence, window) => owned(binding).releaseBeforeDispatch(binding, evidence, window), quarantine: (binding, window) => owned(binding).quarantine(binding, window), } return { journal, register: binding => { allowed.set(binding.attemptId, binding.bindingProof) }, markNoSend: binding => { noSend.add(binding.attemptId) }, fault: kind => { nextFault = kind }, lastPid: () => pid, committedLosses: () => losses } } if (process.env.MMAPP_BANDWIDTH_CHILD === "true") { assertOwnedBandwidthSql() const harness = await createOwnedBandwidthHarness() const { closePool, pool } = await import("../db/index.js") pool.on("error", () => { /* Intentional owned pg_terminate_backend fixtures. */ }) process.on("message", (message: { id: number; command: string; binding?: BandwidthBinding }) => { void (async () => { if (message.command === "close") { await closePool(); process.send?.({ id: message.id, ok: true }); process.disconnect(); return } const binding = message.binding! harness.register(binding) try { const result = message.command === "reserve" ? await harness.journal.reserve(binding, scope()) : message.command === "arm" ? await harness.journal.arm(binding, scope()) : message.command === "complete" ? await harness.journal.complete(binding, { terminalProof: completionProof(binding) }, scope()) : await harness.journal.quarantine(binding, scope()) process.send?.({ id: message.id, ok: true, result, pid: harness.lastPid(), processId: process.pid }) } catch { process.send?.({ id: message.id, ok: false, processId: process.pid }) } })().catch(() => { process.exitCode = 1; process.disconnect() }) }) process.send?.({ ready: true, processId: process.pid }) }