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

269 lines
32 KiB
TypeScript

import assert from "node:assert/strict"
import { generateKeyPairSync, randomUUID } from "node:crypto"
import { basename, dirname, join, resolve } from "node:path"
import { mkdtemp, readFile, realpath, writeFile } from "node:fs/promises"
assert.equal(process.env.MMAPP_OWN_PROBE_STORAGE_JOURNAL, "true")
const databaseUrl = new URL(process.env.DATABASE_URL ?? "")
assert.equal(databaseUrl.hostname, "127.0.0.1"); assert.equal(databaseUrl.pathname, "/postgres")
assert(process.env.MCP_STATE_DIR && basename(dirname(resolve(process.env.MCP_STATE_DIR))).startsWith("mm-mcp-test-"))
const { pool, closePool } = await import("../db/index.js")
const { applyLegacyProbeFixtureMigrations: applySqlMigrations } = await import("../db/legacy-probe-fixture.js")
const { createSqlServerProbeJournal } = await import("./server-probe-journal.js")
const { createServerProbeStorageJournalAdapter } = await import("./server-probe-storage-journal-adapter.js")
const { connectProbeStorage } = await import("./server-probe-storage-connection.js")
const { createProbeJournalStorage, ProbeStorageUnknown } = await import("./server-probe-journal-maintenance.js")
const security = await import("../modules/mcp/security.js")
const cases: string[] = []
type JournalContext = import("./server-probe-journal.js").ServerProbeJournalContext
type Receipt = import("./server-probe-execution.js").ServerProbeReceipt
let serverId: number, lose = "", revoked = false, failGc = false
const policy = { retentionDays: 30, globalBytes: 64 * 1048576, ownerBytes: 1048576, globalFiles: 100, ownerFiles: 2, globalAdmissions: 100, ownerAdmissions: 2 }
const readPolicy = async () => ({ ...policy, retentionDays: Number((await pool.query("SELECT requests_retention_days FROM mcp_settings WHERE id=1")).rows[0].requests_retention_days) })
const connect = async (options: import("./server-probe-journal-maintenance.js").ProbeStorageOptions = { deadlineAt: Date.now() + 120000 }) => {
const client = await connectProbeStorage(options); let mutation = ""
return { release(destroy?: boolean) { client.release(destroy) }, async query(text: string, values?: unknown[], queryOptions?: { signal?: AbortSignal; timeoutMs: number }) {
if (text.includes("probe-storage:reserve")) mutation = "reserve"
if (text.includes("probe-journal:claim")) mutation = "claim"
if (text.includes("probe-storage:publish")) mutation = "publish"
if (text.includes("probe-storage:gc-admissions")) mutation = "gc"
const result = await client.query(text, values, queryOptions).catch((error: unknown) => {
const failure = error as { code?: string; constraint?: string }
console.error("Owned fixture SQL failure", { tag: text.match(/probe-storage:([a-z-]+)/)?.[1], code: failure.code, constraint: failure.constraint })
throw error
})
if (mutation === "gc" && text.includes("probe-storage:gc-admissions") && failGc) { failGc = false; throw Error("Owned GC fixture failure after actual DELETE before COMMIT") }
if (text === "COMMIT" && mutation && mutation === lose) { lose = ""; throw Error("Owned fixture lost actual storage COMMIT acknowledgement") }
return result
} }
}
async function fixture(ownerId = "owner-" + randomUUID(), at = Date.now(), legacy = false) {
const context: JournalContext = { identity: { kind: "rest", authentication: "jwt", ownerId, executionId: randomUUID(), requestId: "request-" + randomUUID(), issuedAt: at - 1000, expiresAt: at + 3600000 }, sourceServerId: serverId, tool: "ping" }
const binding = { receiptId: randomUUID(), bindingProof: "a".repeat(64), ownerId, transport: "rest" as const, requestId: context.identity.kind === "rest" ? context.identity.requestId : "", issuedAt: context.identity.issuedAt, serverIds: [serverId], tool: "ping" as const }
const assertAuthority = async () => { assert(!revoked); assert.equal((await pool.query("SELECT count(*)::int AS n FROM servers WHERE id=$1 AND enabled=true", [serverId])).rows[0].n, 1) }
const controller = new AbortController()
let clockOffset = 0, interruption = ""
const readCurrentPolicy = async () => {
if (interruption === "policy-unavailable") throw Error("Owned fixture settings unavailable")
const current = await readPolicy()
return interruption === "quota-drift" ? { ...current, ownerBytes: current.ownerBytes + 1 } : current
}
const options = { deadlineAt: Date.now() + 120000, signal: controller.signal }
const storageBoundary = legacy ? undefined : createServerProbeStorageJournalAdapter(context, { connect, assertAuthority, policy, readPolicy: readCurrentPolicy, options, now: () => Date.now() + clockOffset })
const journal = createSqlServerProbeJournal(context, { assertAuthority, now: legacy ? () => at : undefined, connections: { connect }, storageBoundary })
const receipt: Receipt = { receiptId: binding.receiptId, tool: "ping", serverId, state: "completed", httpStatus: 200, output: "owned synthetic measurement", data: { kind: "ping", value: "owned fixture" }, attemptedStages: 1, completedStages: 1, automaticReplay: false, forwardingVerified: false }
const stage = async () => { await journal.intent({ receiptId: binding.receiptId, kind: "native-ping", stageIndex: 1, timeoutMs: 1000 }); await journal.received({ receiptId: binding.receiptId, stageIndex: 1, outcome: "received" }) }
const complete = async () => { assert.equal(await journal.claim(binding), "claimed"); await stage(); await journal.finish(receipt) }
const fresh = () => createSqlServerProbeJournal(context, { assertAuthority, connections: { connect }, storageBoundary: createServerProbeStorageJournalAdapter(context, { connect, assertAuthority, policy, readPolicy, options: { deadlineAt: Date.now() + 120000 } }) })
const interrupt = (kind: string) => { interruption = kind; if (kind === "abort") controller.abort(); if (kind === "deadline") clockOffset = 121000 }
return { context, binding, journal, stage, receipt, complete, fresh, interrupt }
}
try {
await applySqlMigrations(pool)
await pool.query("UPDATE traffic_settings SET enabled=false; UPDATE uptime_settings SET enabled=false,resources_enabled=false,ping_enabled=false,speed_enabled=false")
serverId = Number((await pool.query("INSERT INTO servers(name,host,port,enabled) VALUES('owned-probe-storage','127.0.0.1',9,true) RETURNING id")).rows[0].id)
const legacy = await fixture("legacy-owner", Date.now(), true), expired = await fixture("expired-owner", Date.now() - 32 * 86400000, true)
const expiredDirect = await fixture("expired-direct-owner", Date.now() - 32 * 86400000, true)
const expiredLate = await fixture("expired-late-owner", Date.now() - 32 * 86400000, true)
const expiredLost = await fixture("expired-lost-owner", Date.now() - 32 * 86400000, true)
const expiredRollback = await fixture("expired-rollback-owner", Date.now() - 32 * 86400000, true)
await legacy.complete()
const legacySnapshot = await (await import("./server-probe-legacy-snapshot.sql-helper.js")).acceptLegacyProbeSnapshotSql(async () => {
await (await fixture("snapshot-concurrent-owner", Date.now(), true)).complete()
})
console.log("Owned authenticated legacy snapshot:", JSON.stringify(legacySnapshot))
const { provisionDatabaseSourceBindingOffline } = await import("../../scripts/database-source-binding-operator.js")
const fixtureRoot = await mkdtemp(join(await realpath(dirname(resolve(process.env.MCP_STATE_DIR!))), "source-binding-"))
const privateKeyFile = join(fixtureRoot, "issuer-private.pem")
const { privateKey } = generateKeyPairSync("ed25519")
await writeFile(privateKeyFile, privateKey.export({ format: "pem", type: "pkcs8" }), { mode: 0o600, flag: "wx" })
const installationId = randomUUID(), sourceIdentityId = randomUUID()
const inspectionHost = await (await import("./server-probe-legacy-inspection.sql-helper.js")).acceptLegacyProbeInspectionSql(async source => {
const generation = randomUUID()
const issued = await provisionDatabaseSourceBindingOffline({ privateKeyFile, outputDirectory: join(fixtureRoot, generation),
request: { installationId, sourceIdentityId, generation, source } })
return issued.deploymentPin
})
console.log("Owned operator legacy inspection:", JSON.stringify(inspectionHost))
const sourceObservation = await (await import("../db/database-source-observation.sql-helper.js")).acceptDatabaseSourceObservationSql()
console.log("Owned source database observation:", JSON.stringify(sourceObservation))
// Seed an already elapsed synthetic legacy history, before conversion. SQL
// clocks remain real; no trigger is disabled and no immutable row is edited.
for (const prior of [expired, expiredDirect, expiredLate, expiredLost, expiredRollback]) {
const oldAt = new Date(prior.context.identity.issuedAt), oldExpiry = new Date(prior.context.identity.expiresAt)
const envelope = JSON.stringify({ version: 1, context: prior.context, bindingProof: prior.binding.bindingProof, receipt: prior.receipt })
const ciphertext = await security.encryptSecret(envelope), proof = await security.privateSecretFingerprint(security.fingerprint(envelope), "server-probe-journal-v1:receipt")
await pool.query(`INSERT INTO server_probe_executions(receipt_id,binding_proof,identity_kind,execution_id,actor_id,authentication,request_id,authority_issued_at,authority_expires_at,source_server_id,tool,created_at)
VALUES($1,$2,'rest',$3,$4,'jwt',$5,$6,$7,$8,'ping',$9)`, [prior.binding.receiptId, prior.binding.bindingProof, prior.context.identity.executionId, prior.context.identity.ownerId, prior.binding.requestId, prior.context.identity.issuedAt, oldExpiry, serverId, oldAt])
await pool.query("INSERT INTO server_probe_execution_stages(receipt_id,stage_index,kind,timeout_ms,state,outcome,created_at,received_at) VALUES($1,1,'native-ping',1000,'received','received',$2,$2)", [prior.binding.receiptId, oldAt])
await pool.query("UPDATE server_probe_executions SET state='completed',attempted_stages=1,completed_stages=1,receipt_ciphertext=$2,receipt_proof=$3,finished_at=$4 WHERE receipt_id=$1", [prior.binding.receiptId, ciphertext, proof, oldAt])
if (prior === expiredDirect) await pool.query("INSERT INTO server_probe_execution_quarantines(receipt_id,identity_kind,execution_id,binding_proof,identity_proof,error_code,attempted_stages,completed_stages,created_at) VALUES($1,'rest',$2,$3,$4,'JOURNAL_UNKNOWN',1,1,$5)", [prior.binding.receiptId, prior.context.identity.executionId, prior.binding.bindingProof, "c".repeat(64), oldAt])
}
const cancelledFixtures: { receiptId: string; operationId: string; planId: string; ownerId: string; state: string }[] = []
for (const state of ["failed", "running"]) {
const receiptId = randomUUID(), operationId = randomUUID(), planId = randomUUID(), connectionId = randomUUID(), credentialId = randomUUID(), ownerId = "gc-cancellation-" + state
const identity = { kind: "mcp" as const, ownerId, executionId: operationId, connectionId, credentialId, operationName: "servers.probes.run", issuedAt: expired.context.identity.issuedAt, expiresAt: expired.context.identity.expiresAt }
const until = new Date(Date.now() + 3600000), oldAt = new Date(identity.issuedAt), expiry = new Date(identity.expiresAt)
const currentPolicy = { id: connectionId, version: 1, status: "active", mode: "approval", permissions: ["mm:servers:write"], expiresAt: until.toISOString(), scope: { serverIds: [serverId], operationIds: ["servers.probes.run"], allowGlobal: false } }
await pool.query("INSERT INTO mcp_connections(id,name,owner_id,owner_permissions,policy,generation,owner_valid_until) VALUES($1,'owned GC fixture',$2,$3::jsonb,$4::jsonb,$5,$6)", [connectionId, ownerId, JSON.stringify(currentPolicy.permissions), JSON.stringify(currentPolicy), await security.accessGeneration(), until])
await pool.query("INSERT INTO mcp_credentials(id,connection_id,digest,kind,expires_at) VALUES($1,$2,$3,'bearer',$4)", [credentialId, connectionId, randomUUID(), expiry])
await pool.query("INSERT INTO mcp_plans(id,connection_id,operation_id,operation_version,policy_version,generation,payload,evidence,state,expires_at,approved_by) VALUES($1,$2,'servers.probes.run',1,1,$3,'{}','{}',$4,$5,$6)", [planId, connectionId, await security.accessGeneration(), state === "running" ? "executing" : "completed", expiry, ownerId])
await pool.query("INSERT INTO mcp_operations(id,plan_id,connection_id,idempotency_key,state,created_at,finished_at) VALUES($1,$2,$3,$4,$5,$6,$7)", [operationId, planId, connectionId, randomUUID(), state, oldAt, state === "running" ? null : oldAt])
const context: JournalContext = { identity, sourceServerId: serverId, tool: "ping" }, receipt: Receipt = { ...expired.receipt, receiptId }
const body = JSON.stringify({ version: 1, context, bindingProof: expired.binding.bindingProof, receipt })
const cipher = await security.encryptSecret(body), proof = await security.privateSecretFingerprint(security.fingerprint(body), "server-probe-journal-v1:receipt")
await pool.query("INSERT INTO server_probe_executions(receipt_id,binding_proof,identity_kind,execution_id,actor_id,authentication,mcp_connection_id,mcp_credential_id,authority_issued_at,authority_expires_at,source_server_id,tool,created_at) VALUES($1,$2,'mcp',$3,$4,'mcp',$5,$6,$7,$8,$9,'ping',$10)", [receiptId, expired.binding.bindingProof, operationId, ownerId, connectionId, credentialId, identity.issuedAt, expiry, serverId, oldAt])
await pool.query("INSERT INTO server_probe_execution_stages(receipt_id,stage_index,kind,timeout_ms,state,outcome,created_at,received_at) VALUES($1,1,'native-ping',1000,'received','received',$2,$2)", [receiptId, oldAt])
await pool.query("UPDATE server_probe_executions SET state='completed',attempted_stages=1,completed_stages=1,receipt_ciphertext=$2,receipt_proof=$3,finished_at=$4 WHERE receipt_id=$1", [receiptId, cipher, proof, oldAt])
await pool.query("INSERT INTO mcp_diagnostic_cancellations(operation_id,connection_id,actor_id,created_at) VALUES($1,$2,$3,$4)", [operationId, connectionId, ownerId, oldAt])
await pool.query("INSERT INTO mcp_audit(actor_id,action,connection_id,payload) VALUES($1,'synthetic.gc-preserve',$2,$3::jsonb)", [ownerId, connectionId, JSON.stringify({ operationId })])
cancelledFixtures.push({ receiptId, operationId, planId, ownerId, state })
}
const oldCiphertext = (await pool.query("SELECT receipt_ciphertext FROM server_probe_executions WHERE receipt_id=$1", [legacy.binding.receiptId])).rows[0].receipt_ciphertext
const client = await pool.connect()
try { await client.query(await readFile(new URL("../db/proposals/server-probe-storage-v2.sql", import.meta.url), "utf8")) } finally { client.release(true) }
assert.equal((await pool.query("SELECT receipt_ciphertext FROM server_probe_execution_payloads WHERE receipt_id=$1", [legacy.binding.receiptId])).rows[0].receipt_ciphertext, oldCiphertext)
await pool.query("INSERT INTO server_probe_execution_quarantines(receipt_id,identity_kind,execution_id,binding_proof,identity_proof,error_code,attempted_stages,completed_stages) VALUES($1,'rest',$2,$3,$4,'JOURNAL_UNKNOWN',1,1)", [expiredLate.binding.receiptId, expiredLate.context.identity.executionId, expiredLate.binding.bindingProof, "c".repeat(64)])
assert.deepEqual(await legacy.fresh().read(legacy.binding.receiptId, legacy.binding.bindingProof), legacy.receipt)
assert.equal((await pool.query("SELECT count(*)::int AS n FROM information_schema.columns WHERE table_name='server_probe_executions' AND column_name='receipt_ciphertext'")).rows[0].n, 0)
cases.push("actual proposal copies legacy ciphertext exactly before removing anchor payload and opt-in journal reads the converted receipt")
const normal = await fixture(); await normal.complete()
assert.deepEqual(await normal.fresh().read(normal.binding.receiptId, normal.binding.bindingProof), normal.receipt)
const anchor = (await pool.query("SELECT * FROM server_probe_executions WHERE receipt_id=$1", [normal.binding.receiptId])).rows[0]
assert.equal(anchor.state, "completed"); assert(anchor.receipt_proof); assert.equal(anchor.receipt_ciphertext, undefined)
assert.equal((await pool.query("SELECT count(*)::int AS n FROM server_probe_execution_payloads WHERE receipt_id=$1", [normal.binding.receiptId])).rows[0].n, 1)
cases.push("real claim/stage/finish/read use quota admission and separate authenticated payload with proof-only immutable anchor")
const owner = "quota-owner-" + randomUUID(), first = await fixture(owner), second = await fixture(owner), third = await fixture(owner)
await first.journal.claim(first.binding); await second.journal.claim(second.binding)
await assert.rejects(third.journal.claim(third.binding))
assert.equal((await pool.query("SELECT count(*)::int AS n FROM server_probe_executions WHERE receipt_id=$1", [third.binding.receiptId])).rows[0].n, 0)
assert.equal(Number((await pool.query("SELECT admissions FROM server_probe_storage_usage WHERE scope=$1", ["owner:" + owner])).rows[0].admissions), 2)
cases.push("actual owner quota refuses before anchor insertion with no extra charge")
for (const phase of ["reserve", "claim", "publish"] as const) {
const f = await fixture()
if (phase === "publish") { await f.journal.claim(f.binding); await f.stage() }
lose = phase
await assert.rejects(phase === "publish" ? f.journal.finish(f.receipt) : f.journal.claim(f.binding))
const fresh = f.fresh(); await assert.rejects(fresh.claim(f.binding))
revoked = true
await f.journal.quarantine({ ...f.binding, errorCode: "JOURNAL_UNKNOWN", attemptedStages: phase === "reserve" ? 0 : 1, completedStages: phase === "publish" ? 1 : 0 })
revoked = false
const observed = await f.fresh().read(f.binding.receiptId, f.binding.bindingProof)
assert.equal(observed?.state, "unknown")
assert.equal((await pool.query("SELECT count(*)::int AS n FROM server_probe_storage_admissions WHERE receipt_id=$1", [f.binding.receiptId])).rows[0].n, 1)
cases.push("actual " + phase + " COMMIT ACK loss retains spent admission and quarantine dominates even after authority revocation")
}
for (const interruption of ["abort", "deadline", "retention-drift", "quota-drift", "policy-unavailable"]) {
const f = await fixture(); await f.journal.claim(f.binding); await f.stage()
lose = "publish"; await assert.rejects(f.journal.finish(f.receipt), ProbeStorageUnknown)
const snapshot = async () => (await pool.query(`SELECT row_to_json(e) AS anchor,
(SELECT row_to_json(p) FROM server_probe_execution_payloads p WHERE p.receipt_id=e.receipt_id) AS payload,
(SELECT json_agg(s ORDER BY stage_index) FROM server_probe_execution_stages s WHERE s.receipt_id=e.receipt_id) AS stages
FROM server_probe_executions e WHERE receipt_id=$1`, [f.binding.receiptId])).rows[0]
const before = await snapshot(); assert.equal(before.anchor.state, "completed"); assert(before.payload)
f.interrupt(interruption); revoked = true
if (interruption === "retention-drift") await pool.query("UPDATE mcp_settings SET requests_retention_days=31 WHERE id=1")
try {
await f.journal.quarantine({ ...f.binding, errorCode: "JOURNAL_UNKNOWN", attemptedStages: 1, completedStages: 1 })
await f.journal.quarantine({ ...f.binding, errorCode: "JOURNAL_UNKNOWN", attemptedStages: 1, completedStages: 1 })
} finally {
revoked = false
if (interruption === "retention-drift") await pool.query("UPDATE mcp_settings SET requests_retention_days=30 WHERE id=1")
}
assert.deepEqual(await snapshot(), before)
assert.equal((await pool.query("SELECT count(*)::int AS n FROM server_probe_execution_quarantines WHERE receipt_id=$1", [f.binding.receiptId])).rows[0].n, 1)
assert.equal((await f.fresh().read(f.binding.receiptId, f.binding.bindingProof))?.state, "unknown")
await assert.rejects(f.fresh().claim(f.binding))
cases.push("actual publish COMMIT ACK loss followed by " + interruption + " preserves exact terminal/payload/stages and fresh read returns only quarantined unknown")
}
const storage = createProbeJournalStorage({ connect, policy, safetyWindowMs: 60000, authorize: async () => "c".repeat(64) })
const purged = await storage.purge(expired.context.identity.ownerId, 10, { deadlineAt: Date.now() + 120000 })
assert.equal(purged.purged, 1)
const stored = await storage.read({ ownerId: expired.context.identity.ownerId, receiptId: expired.binding.receiptId, identityKind: "rest", executionId: expired.context.identity.executionId, bindingProof: expired.binding.bindingProof, authorityExpiresAt: new Date(expired.context.identity.expiresAt).toISOString() }, { deadlineAt: Date.now() + 120000 })
assert.equal(stored.kind, "expired")
assert.equal((await pool.query("SELECT count(*)::int AS n FROM server_probe_executions WHERE receipt_id=$1", [expired.binding.receiptId])).rows[0].n, 1)
assert.equal(Number((await pool.query("SELECT reserved_bytes FROM server_probe_storage_usage WHERE scope=$1", ["owner:" + expired.context.identity.ownerId])).rows[0].reserved_bytes), 32768)
cases.push("actual expired payload purge releases only payload capacity; immutable attempt metadata and permanent expired tombstone survive")
const gcCheck = await pool.connect()
try {
await gcCheck.query("BEGIN")
assert((await gcCheck.query("SELECT proconfig FROM pg_proc WHERE proname='server_probe_storage_gc_allowed'")).rows[0].proconfig.includes("TimeZone=UTC"))
assert.equal((await gcCheck.query("SELECT server_probe_storage_gc_allowed($1) AS allowed", [expired.binding.receiptId])).rows[0].allowed, false)
await gcCheck.query("SET LOCAL mm.probe_storage_gc='on'")
assert.equal((await gcCheck.query("SELECT server_probe_storage_gc_allowed($1) AS allowed", [expired.binding.receiptId])).rows[0].allowed, true)
assert.equal((await gcCheck.query("SELECT server_probe_storage_gc_allowed($1) AS allowed", [normal.binding.receiptId])).rows[0].allowed, false)
await gcCheck.query("UPDATE mcp_settings SET requests_retention_days=365 WHERE id=1")
assert.equal((await gcCheck.query("SELECT server_probe_storage_gc_allowed($1) AS allowed", [expired.binding.receiptId])).rows[0].allowed, false)
} finally { await gcCheck.query("ROLLBACK"); gcCheck.release(true) }
cases.push("actual GC predicate requires explicit mode, persisted expiry and current retention; fresh admission remains protected")
for (const table of ["server_probe_executions", "server_probe_execution_stages", "server_probe_storage_admissions"]) {
await assert.rejects(pool.query(`DELETE FROM ${table} WHERE receipt_id=$1`, [expired.binding.receiptId]))
}
cases.push("actual SQL rejects metadata DELETE without validated GC mode even for expired rows")
assert.equal((await storage.collectExpired(expired.context.identity.ownerId, 10, { deadlineAt: Date.now() + 120000 })).collected, 1)
for (const table of ["server_probe_executions", "server_probe_execution_stages", "server_probe_storage_admissions"]) assert.equal((await pool.query(`SELECT count(*)::int AS n FROM ${table} WHERE receipt_id=$1`, [expired.binding.receiptId])).rows[0].n, 0)
assert.equal((await pool.query("SELECT count(*)::int AS n FROM server_probe_storage_usage WHERE scope=$1", ["owner:" + expired.context.identity.ownerId])).rows[0].n, 0)
await assert.rejects(storage.claim({ ownerId: expired.context.identity.ownerId, receiptId: expired.binding.receiptId, identityKind: "rest", executionId: expired.context.identity.executionId, bindingProof: expired.binding.bindingProof, authorityExpiresAt: new Date(expired.context.identity.expiresAt).toISOString() }, { deadlineAt: Date.now() + 120000 }, async () => assert.fail("Expired original identity cannot execute after GC")))
cases.push("actual GC removes expired spent metadata and refunds remaining reservation once; original expired identity cannot replay")
assert.equal((await storage.collectExpired(expiredDirect.context.identity.ownerId, 10, { deadlineAt: Date.now() + 120000 })).collected, 1)
assert.equal((await pool.query("SELECT count(*)::int AS n FROM server_probe_execution_payloads WHERE receipt_id=$1", [expiredDirect.binding.receiptId])).rows[0].n, 0)
assert.equal((await pool.query("SELECT count(*)::int AS n FROM server_probe_execution_quarantines WHERE receipt_id=$1", [expiredDirect.binding.receiptId])).rows[0].n, 0)
assert.equal((await storage.collectExpired(expiredDirect.context.identity.ownerId, 10, { deadlineAt: Date.now() + 120000 })).collected, 0)
assert.equal((await storage.collectExpired(normal.context.identity.ownerId, 10, { deadlineAt: Date.now() + 120000 })).collected, 0)
cases.push("actual GC also removes unpurged expired ciphertext atomically; repeat and fresh owner batches change nothing")
assert.equal((await storage.collectExpired(expiredLate.context.identity.ownerId, 10, { deadlineAt: Date.now() + 120000 })).collected, 0)
assert.equal((await pool.query("SELECT count(*)::int AS n FROM server_probe_execution_quarantines WHERE receipt_id=$1", [expiredLate.binding.receiptId])).rows[0].n, 1)
cases.push("actual recent quarantine protects old original identity from GC until last-activity retention elapses")
failGc = true
await assert.rejects(storage.collectExpired(expiredRollback.context.identity.ownerId, 10, { deadlineAt: Date.now() + 120000 }))
for (const table of ["server_probe_executions", "server_probe_execution_stages", "server_probe_execution_payloads", "server_probe_storage_admissions"]) assert.equal((await pool.query(`SELECT count(*)::int AS n FROM ${table} WHERE receipt_id=$1`, [expiredRollback.binding.receiptId])).rows[0].n, 1)
assert.equal(Number((await pool.query("SELECT reserved_bytes FROM server_probe_storage_usage WHERE scope=$1", ["owner:" + expiredRollback.context.identity.ownerId])).rows[0].reserved_bytes), 294912)
cases.push("actual error after GC DELETE rolls back all metadata and capacity changes atomically")
lose = "gc"
await assert.rejects(storage.collectExpired(expiredLost.context.identity.ownerId, 10, { deadlineAt: Date.now() + 120000 }), ProbeStorageUnknown)
assert.equal((await pool.query("SELECT count(*)::int AS n FROM server_probe_storage_admissions WHERE receipt_id=$1", [expiredLost.binding.receiptId])).rows[0].n, 0)
assert.equal((await storage.collectExpired(expiredLost.context.identity.ownerId, 10, { deadlineAt: Date.now() + 120000 })).collected, 0)
cases.push("actual lost GC COMMIT ACK reports unknown without replay; independent reconciliation sees the completed deletion")
const terminal = cancelledFixtures.find(row => row.state === "failed")!, running = cancelledFixtures.find(row => row.state === "running")!
assert.equal((await storage.collectExpiredCancellations(running.ownerId, 10, { deadlineAt: Date.now() + 120000 })).collected, 0)
assert.equal((await storage.collectExpired(running.ownerId, 10, { deadlineAt: Date.now() + 120000 })).collected, 0)
cases.push("actual expired original identity cannot permit GC while its MCP operation is still running")
assert.equal((await storage.collectExpiredCancellations(terminal.ownerId, 10, { deadlineAt: Date.now() + 120000 })).collected, 1)
assert.equal(Number((await pool.query("SELECT reserved_bytes FROM server_probe_storage_usage WHERE scope=$1", ["owner:" + terminal.ownerId])).rows[0].reserved_bytes), 294912)
assert.equal((await storage.collectExpired(terminal.ownerId, 10, { deadlineAt: Date.now() + 120000 })).collected, 1)
assert.equal((await pool.query("SELECT count(*)::int AS n FROM mcp_diagnostic_cancellations WHERE operation_id=$1", [terminal.operationId])).rows[0].n, 0)
assert.equal((await pool.query("SELECT count(*)::int AS n FROM mcp_operations WHERE id=$1", [terminal.operationId])).rows[0].n, 1)
assert.equal((await pool.query("SELECT count(*)::int AS n FROM mcp_plans WHERE id=$1", [terminal.planId])).rows[0].n, 1)
assert.equal((await pool.query("SELECT count(*)::int AS n FROM mcp_audit WHERE payload->>'operationId'=$1", [terminal.operationId])).rows[0].n, 1)
cases.push("actual terminal cancellation GC refunds no duplicate quota; admission GC preserves plans, operations and general audit")
const cancelledScope = { ownerId: "owned-cancel-quota", receiptId: randomUUID(), identityKind: "rest" as const, executionId: randomUUID(), bindingProof: "d".repeat(64), authorityExpiresAt: new Date(Date.now() + 3600000).toISOString() }
const cancel = (bound = cancelledScope) => storage.cancel(bound.ownerId, { deadlineAt: Date.now() + 120000 }, async sql => { await sql.query("SELECT 1 /* owned synthetic cancellation source */"); return bound }, async sql => { await sql.query("SELECT 1 /* owned cancellation acknowledgement */"); return bound.receiptId })
assert.equal(await cancel(), cancelledScope.receiptId)
const cancelled = (await pool.query("SELECT * FROM server_probe_storage_admissions WHERE receipt_id=$1", [cancelledScope.receiptId])).rows[0]
assert.equal(cancelled.claim_token, null); assert(cancelled.cancelled_at); assert(Number.isFinite(new Date(cancelled.gc_not_before).getTime()))
await cancel({ ...cancelledScope, receiptId: randomUUID(), executionId: randomUUID() })
assert.equal(await cancel(), cancelledScope.receiptId)
assert.equal(Number((await pool.query("SELECT reserved_bytes FROM server_probe_storage_usage WHERE scope=$1", ["owner:" + cancelledScope.ownerId])).rows[0].reserved_bytes), 2 * 294912)
await assert.rejects(cancel({ ...cancelledScope, receiptId: randomUUID(), executionId: randomUUID() }))
await assert.rejects(storage.claim(cancelledScope, { deadlineAt: Date.now() + 120000 }, async () => assert.fail("Cancelled identity cannot acquire anchor")))
await assert.rejects(storage.publish(cancelledScope, "owned synthetic ciphertext", "a".repeat(64), { deadlineAt: Date.now() + 120000 }, async () => assert.fail("Cancelled identity cannot publish")))
cases.push("actual preclaim cancellation reserves shared quota once with finite GC lifetime; duplicate at quota limit succeeds while claim and publish remain refused")
const sourcePrerequisites = await (await import("../modules/mcp/diagnostic-cancellation-source.sql-helper.js")).acceptDiagnosticCancellationSourceSql(serverId)
console.log("Owned cancellation source prerequisites:", JSON.stringify(sourcePrerequisites))
console.log("Owned storage source mapping:", JSON.stringify(await (await import("./server-probe-storage-source.sql-helper.js")).acceptProbeStorageSourceSql(serverId)))
console.log("Owned bounded storage connection:", JSON.stringify(await (await import("./server-probe-storage-connection.sql-helper.js")).acceptProbeStorageConnectionSql()))
// Only the current MCP runtime subcase needs the replay fence. Keep the
// earlier legacy inspection and conversion fixtures on their historical schema.
const { analyzeMigration, MIGRATION_MANIFEST } = await import("../db/migration-manifest.js")
const replayPin = MIGRATION_MANIFEST.find(pin => pin.file === "0045_mcp_operation_replay_tombstones.sql")
assert(replayPin)
const replayMigration = await readFile(new URL("../../drizzle/" + replayPin.file, import.meta.url))
analyzeMigration(replayPin.file, replayMigration, replayPin)
const runtimeFixtureClient = await pool.connect()
try { await runtimeFixtureClient.query(replayMigration.toString("utf8")) } finally { runtimeFixtureClient.release(true) }
console.log("Owned storage probe runtime host:", JSON.stringify(await (await import("../modules/mcp/server-probe-storage-host.sql-helper.js")).acceptServerProbeStorageHostSql()))
console.log("Owned storage REST host:", JSON.stringify(await (await import("./server-probe-storage-rest.sql-helper.js")).acceptServerProbeStorageRestSql()))
console.log("Owned bounded metadata cancellation:", JSON.stringify(await (await import("./server-probe-storage-metadata.sql-helper.js")).acceptProbeStorageMetadataSql()))
console.log("Owned retention worker:", JSON.stringify(await (await import("./server-probe-retention.sql-helper.js")).acceptServerProbeRetentionSql()))
console.log("Owned probe storage journal SQL:", JSON.stringify({ cases: cases.length, names: cases, actualSql: true, actualCommitAckLosses: 8, productionCrypto: true, syntheticRestIdentity: true, nativeOrDnsCalls: 0, metadataGcRun: "owned synthetic database only", productionDdlApplied: false, routesRegistered: false, historicalNewCredentialReaderIntegrated: false }))
} finally { revoked = false; await closePool() }