277 lines
21 KiB
TypeScript
277 lines
21 KiB
TypeScript
import assert from "node:assert/strict"
|
|
import { readFileSync } from "node:fs"
|
|
import { randomUUID } from "node:crypto"
|
|
import { test } from "node:test"
|
|
import { runInNewContext } from "node:vm"
|
|
import ts from "typescript"
|
|
import { z } from "zod"
|
|
import * as model from "./server-poll-storage-model.js"
|
|
import * as errorProvenance from "./server-poll-error-provenance.js"
|
|
import { admitPartitionWriter, PARTITION_WRITER_ADVISORY_KEY } from "./partition-write-transaction.js"
|
|
import type { PollStorageDependencies } from "./server-poll-storage.js"
|
|
|
|
/** Actual compactor and transaction dispatcher, explicit SQL/codec/accounting
|
|
* doubles. These tests establish control flow, NOT SQL triggers, cryptographic
|
|
* provenance, native execution or physical PostgreSQL durability. */
|
|
const compile = (name: string) => ts.transpileModule(readFileSync(new URL(name, import.meta.url), "utf8"), {
|
|
compilerOptions: { module: ts.ModuleKind.CommonJS, target: ts.ScriptTarget.ES2022 },
|
|
}).outputText
|
|
const storageCode = compile("./server-poll-storage.ts"), compactorCode = compile("./server-poll-terminal-compaction.ts")
|
|
const ACCOUNTING = "SELECT component_accounting"
|
|
type Row = Record<string, unknown>
|
|
type State = { claim: string | null; archive: Row | null; audits: Row[] }
|
|
type Command = { tx: number; text: string; values: unknown[] }
|
|
type Api = { compactCompletedServerPollStorageForTests(deps: PollStorageDependencies): Promise<{ scanned: number; compacted: number; skipped: number; automaticReplay: false }> }
|
|
function load(code: string, modules: Record<string, unknown>) {
|
|
const exports: Record<string, unknown> = {}
|
|
runInNewContext(code, { exports, require(name: string) { assert(Object.hasOwn(modules, name), "Unapproved production import: " + name); return modules[name] },
|
|
Date, Error, Promise, Object, Math, Number, String, JSON, RegExp, setTimeout, clearTimeout, structuredClone }, { filename: "poll-compaction-component.js" })
|
|
return exports
|
|
}
|
|
function fixture(quarantined = false) {
|
|
const now = Date.now(), receiptId = "ffffffff-ffff-4fff-8fff-ffffffffffff", operationId = randomUUID(), connectionId = randomUUID()
|
|
const earlierReceipts = Array.from({ length: 128 }, (_, index) => `00000000-0000-4000-8000-${String(index).padStart(12, "0")}`)
|
|
const authorityExpiresAt = "2026-10-01 00:00:00.123456+00"
|
|
let clock = now, skippedPrefix: "slow" | "full-page" | undefined, removedReceiptId: string | undefined
|
|
let state: State = { claim: null, archive: null, audits: [] }, connects = 0, transactions=0, commits = 0, createCalls = 0, openCalls = 0, accountCalls = 0
|
|
let eligible = true, sourceMissing = false, accountingFails = false, preflight: "valid" | "oversize" | "invalid" = "valid", secondOversize = false
|
|
let lostCommit = 0, commitPersists = true, badPublishedSize = false, badPublishedText = false, badPublishedId = false, auditFails = false, writer: boolean|Error=true
|
|
const commands: Command[] = [], releases: { tx: number; unsafe: boolean }[] = [], events: string[] = []
|
|
class ArchiveRefused extends Error {}
|
|
const forbiddenConnector = async (): Promise<never> => { throw Error("Production connector must never run") }
|
|
const accounting = async (sql: { query(text: string): Promise<unknown> }) => { accountCalls++; await sql.query(ACCOUNTING); if (accountingFails) throw new model.PollStorageRefused() }
|
|
const storage = load(storageCode, {
|
|
"node:crypto": { randomUUID }, zod: { z }, "./server-probe-storage-connection.js": { connectProbeStorage: forbiddenConnector },
|
|
"./server-poll-storage-model.js": model, "./server-poll-archive-accounting.js": { assertServerPollArchiveAccounting: accounting },
|
|
"./server-poll-error-provenance.js": errorProvenance,
|
|
"./partition-write-transaction.js": { admitPartitionWriter, PARTITION_WRITER_ADVISORY_KEY },
|
|
"../db/database-restore-quarantine.js": {
|
|
databaseRestoreQuarantineEnabled: () => quarantined,
|
|
assertDatabaseRestoreQuarantineOpen: async () => { throw new Error("Owned restore quarantine") },
|
|
},
|
|
})
|
|
const archives = new Map<string, string>()
|
|
const api = load(compactorCode, {
|
|
"node:crypto": { randomUUID }, "./shared-database-gate.js": { sharedDatabaseGate: { request: (work: () => unknown) => work() } },
|
|
"./server-probe-storage-connection.js": { connectProbeStorage: forbiddenConnector }, "./server-poll-storage.js": storage,
|
|
"./server-poll-archive-accounting.js": { assertServerPollArchiveAccounting: accounting },
|
|
"./server-poll-terminal-archive.js": {
|
|
ServerPollArchiveRefused: ArchiveRefused,
|
|
async createServerPollTerminalArchive(text: string, observedAt: number) {
|
|
createCalls++; events.push("create:" + createCalls); assert.equal(observedAt, now)
|
|
if (createCalls === 1 && preflight === "invalid") throw new ArchiveRefused()
|
|
if (createCalls === 1 && preflight === "oversize" || createCalls === 2 && secondOversize) return undefined
|
|
const ciphertext = "component-ciphertext-" + createCalls; archives.set(ciphertext, text)
|
|
return { version: 1, receipt_id: receiptId, execution_id: operationId, connection_id: connectionId, owner_id: "component-owner",
|
|
binding_proof: "binding-double", context_proof: "context-double", source_digest: "source-double", source_sha256: "sha-double", terminal_digest: "terminal-double",
|
|
original_activity_at: "2026-10-01T00:00:00.123456Z", original_expires_at: "2026-10-01T00:00:00.123456Z", decoded_bytes: text.length,
|
|
compressed_bytes: 30, ciphertext, ciphertext_bytes: ciphertext.length, archive_proof: "archive-double", retention_days: 30 }
|
|
},
|
|
async openServerPollTerminalArchive(archive: Row) {
|
|
openCalls++; events.push("open:" + openCalls)
|
|
assert(archives.has(String(archive.ciphertext)))
|
|
return { sourceText: badPublishedText ? "altered" : archives.get(String(archive.ciphertext)), metadata: { receipt_id: badPublishedId ? operationId : receiptId },
|
|
source: { journal: { intents: [{}, {}], acks: [{}, {}] } } }
|
|
},
|
|
},
|
|
}) as unknown as Api
|
|
const deps: PollStorageDependencies = { now: () => clock, connect: async options => {
|
|
const owner = ++connects; let tx=0, transaction: State | undefined, closed = false
|
|
assert.equal(options.deadlineAt, clock + 40000)
|
|
return { async query(text, values = [], queryOptions) {
|
|
values = Array.from(values) // Normalize the VM caller's array realm only.
|
|
assert(!closed); assert(queryOptions && queryOptions.timeoutMs >= 1 && queryOptions.timeoutMs <= 5000)
|
|
if(text==="BEGIN")tx=++transactions
|
|
commands.push({ tx, text, values }); events.push(tx + ":" + text)
|
|
if (text === "BEGIN") { transaction = structuredClone(state); return { rows: [] } }
|
|
if (text === "ROLLBACK") { transaction = undefined; return { rows: [] } }
|
|
if (text.includes("pg_advisory_unlock_shared")) {assert.equal(transaction,undefined);return {rows:[{released:true}]}}
|
|
assert(transaction)
|
|
if (text.includes("pg_try_advisory_lock_shared")) {if(writer instanceof Error)throw writer;return {rows:[{acquired:writer}]}}
|
|
if (text === "COMMIT") {
|
|
commits++; if (commits !== lostCommit || commitPersists) state = structuredClone(transaction)
|
|
transaction = undefined
|
|
if (commits === lostCommit) throw Error("PRIVATE_DRIVER_COMMIT_ACK_LOST")
|
|
return { rows: [] }
|
|
}
|
|
if (text.includes("current_setting('lock_timeout')")) return { rows: [{ lock_timeout: "0" }] }
|
|
if (text.startsWith("SELECT set_config(") || text.startsWith("SELECT pg_catalog.set_config(") || text.startsWith("SET LOCAL ") || text.includes("pg_advisory_xact_lock") || text === ACCOUNTING) return { rows: [] }
|
|
if (text.startsWith("SELECT a.receipt_id,") && text.includes("FROM server_poll_storage_admissions a")) {
|
|
assert(text.includes("a.compaction_claim IS NULL")); assert(text.includes("a.storage_state='full'"))
|
|
const ids = earlierReceipts.slice(0, skippedPrefix === "slow" ? 5 : skippedPrefix === "full-page" ? 128 : 0)
|
|
if (eligible && !transaction.claim && !transaction.archive) ids.push(receiptId)
|
|
if (values[0] != null) {
|
|
assert(text.includes("(a.authority_expires_at,a.receipt_id)>($1::timestamptz,$2::uuid)"))
|
|
assert.deepEqual(values, [authorityExpiresAt, values[1]])
|
|
}
|
|
return { rows: ids.filter(id => id !== removedReceiptId && (values[0] == null || id > String(values[1]))).slice(0, 128)
|
|
.map(receipt_id => ({ receipt_id, authority_expires_at: authorityExpiresAt })) }
|
|
}
|
|
if (text.startsWith("SELECT server_poll_storage_source_payload(")) {
|
|
if (typeof values[0] === "string" && earlierReceipts.includes(values[0])) {
|
|
if (skippedPrefix === "slow") clock += 4500
|
|
return { rows: [{ source_text: null }] }
|
|
}
|
|
assert.deepEqual(values, [receiptId]); return { rows: [{ source_text: sourceMissing ? null : JSON.stringify({ receiptId, claim: transaction.claim }), observed_at: new Date(now) }] }
|
|
}
|
|
if (text.startsWith("UPDATE server_poll_storage_admissions SET compaction_claim=")) {
|
|
assert.equal(transaction.claim, null); assert.equal(values[0], receiptId); assert.equal(typeof values[1], "string")
|
|
transaction.claim = String(values[1]); return { rows: [{ receipt_id: receiptId }] }
|
|
}
|
|
if (text.startsWith("SELECT receipt_id FROM server_poll_storage_admissions")) {
|
|
assert.deepEqual(values, [receiptId, transaction.claim]); assert(transaction.claim)
|
|
assert(text.includes("compaction_claim_xid IS NOT NULL FOR UPDATE")); return { rows: [{ receipt_id: receiptId }] }
|
|
}
|
|
if (text.startsWith("INSERT INTO server_poll_archives(")) {
|
|
const columns = text.slice(text.indexOf("(") + 1, text.indexOf(")")).split(",")
|
|
transaction.archive = Object.fromEntries(columns.map((column, index) => [column, values[index]]))
|
|
assert(transaction.claim); return { rows: [{ receipt_id: receiptId }] }
|
|
}
|
|
if (text.startsWith("SELECT r.*,")) {
|
|
assert(transaction.archive); assert(text.includes("pg_catalog.sha256(pg_catalog.convert_to(r.ciphertext,'UTF8'))")); assert(text.includes("octet_length(r.ciphertext)"))
|
|
assert(text.includes('HH24:MI:SS.US"Z"')); assert.deepEqual(values, [receiptId])
|
|
return { rows: [{ ...transaction.archive, actual_ciphertext_bytes: Number(transaction.archive.ciphertext_bytes) + (badPublishedSize ? 1 : 0) }] }
|
|
}
|
|
if (text.startsWith("INSERT INTO mcp_audit(")) {
|
|
if (auditFails) throw Error("PRIVATE_DRIVER_AUDIT_FAILURE")
|
|
assert.equal(values[1], "poll.history.compacted"); transaction.audits.push({ actor: values[0], connection: values[2], payload: JSON.parse(String(values[3])) }); return { rows: [] }
|
|
}
|
|
throw Error("Unapproved fixture SQL: " + text)
|
|
}, release(unsafe = false) { assert(!closed); closed = true; transaction = undefined; releases.push({ tx:owner, unsafe }) } }
|
|
} }
|
|
return { run: () => api.compactCompletedServerPollStorageForTests(deps), commands, releases, events, state: () => structuredClone(state),
|
|
counts: () => ({ connects, commits, createCalls, openCalls, accountCalls }), Unknown: storage.PollStorageUnknown as new () => Error,setWriter:(value:boolean|Error)=>{writer=value},
|
|
configure(options: { eligible?: boolean; sourceMissing?: boolean; accountingFails?: boolean; preflight?: typeof preflight; secondOversize?: boolean; lostCommit?: number; commitPersists?: boolean; badPublishedSize?: boolean; badPublishedText?: boolean; badPublishedId?: boolean; auditFails?: boolean; skippedPrefix?: typeof skippedPrefix; removeLastSlowReceipt?: boolean }) {
|
|
eligible = options.eligible ?? eligible; sourceMissing = options.sourceMissing ?? sourceMissing; accountingFails = options.accountingFails ?? accountingFails
|
|
preflight = options.preflight ?? preflight; secondOversize = options.secondOversize ?? secondOversize; lostCommit = options.lostCommit ?? lostCommit
|
|
commitPersists = options.commitPersists ?? commitPersists; badPublishedSize = options.badPublishedSize ?? badPublishedSize; badPublishedText = options.badPublishedText ?? badPublishedText; auditFails = options.auditFails ?? auditFails
|
|
badPublishedId = options.badPublishedId ?? badPublishedId
|
|
skippedPrefix = options.skippedPrefix ?? skippedPrefix
|
|
if (options.removeLastSlowReceipt) removedReceiptId = earlierReceipts[3]
|
|
} }
|
|
}
|
|
const claimed = (f: ReturnType<typeof fixture>) => f.commands.filter(command => command.text.startsWith("UPDATE server_poll_storage_admissions"))
|
|
const published = (f: ReturnType<typeof fixture>) => f.commands.filter(command => command.text.startsWith("INSERT INTO server_poll_archives"))
|
|
|
|
test("idle tick skips accounting and codec/key/native dependencies", async () => {
|
|
const f = fixture(); f.configure({ eligible: false })
|
|
assert.equal((await f.run()).compacted, 0)
|
|
assert.deepEqual(f.counts(), { connects: 1, commits: 1, createCalls: 0, openCalls: 0, accountCalls: 0 })
|
|
assert.equal(claimed(f).length, 0); assert.equal(published(f).length, 0)
|
|
})
|
|
|
|
test("only a definite shared session lock miss defers before POLL storage, and SQL failures remain errors",async()=>{
|
|
const blocked=fixture();blocked.setWriter(false)
|
|
assert.deepEqual({...await blocked.run()},{scanned:0,compacted:0,skipped:0,automaticReplay:false})
|
|
assert.deepEqual(blocked.commands.map(command=>command.text),["BEGIN","SELECT pg_catalog.pg_try_advisory_lock_shared($1::bigint) AS acquired","COMMIT"])
|
|
assert.deepEqual(blocked.releases,[{tx:1,unsafe:false}])
|
|
const unexpected=fixture();unexpected.setWriter(Error("owned unexpected provider failure"))
|
|
await assert.rejects(unexpected.run(),model.PollStorageRefused)
|
|
assert.deepEqual(unexpected.releases,[{tx:1,unsafe:true}])
|
|
assert.equal(claimed(unexpected).length,0)
|
|
})
|
|
|
|
test("quarantine refusal discards the POLL session holding the partition lock", async () => {
|
|
const f = fixture(true)
|
|
await assert.rejects(f.run(), model.PollStorageRefused)
|
|
assert.equal(f.counts().commits, 0)
|
|
assert.deepEqual(f.releases, [{ tx: 1, unsafe: true }])
|
|
assert.equal(claimed(f).length, 0)
|
|
assert.equal(published(f).length, 0)
|
|
})
|
|
|
|
test("ineligible SQL source, oversize and invalid preflight skip without consuming claim", async () => {
|
|
for (const options of [{ sourceMissing: true }, { preflight: "oversize" as const }, { preflight: "invalid" as const }]) {
|
|
const f = fixture(); f.configure(options); const result = await f.run()
|
|
assert.equal(result.compacted, 0); assert.equal(result.skipped, 1)
|
|
assert.equal(claimed(f).length, 0); assert.equal(published(f).length, 0); assert.equal(f.counts().connects, 1)
|
|
assert.equal(f.state().claim, null); assert.equal(f.counts().accountCalls, 1)
|
|
}
|
|
})
|
|
|
|
test("slow skipped candidates cannot consume successive scan windows ahead of an eligible row", async () => {
|
|
const f = fixture(); f.configure({ skippedPrefix: "slow" })
|
|
assert.deepEqual({ ...await f.run() }, { scanned: 4, compacted: 0, skipped: 4, automaticReplay: false })
|
|
assert.equal(claimed(f).length, 0)
|
|
f.configure({ removeLastSlowReceipt: true })
|
|
assert.deepEqual({ ...await f.run() }, { scanned: 2, compacted: 1, skipped: 1, automaticReplay: false })
|
|
assert.equal(claimed(f).length, 1)
|
|
})
|
|
|
|
test("a full unclaimable SQL page advances to the next timestamp/receipt tuple", async () => {
|
|
// The SQL paging double deliberately exceeds the current 128 FULL-row accounting cap.
|
|
const f = fixture(); f.configure({ skippedPrefix: "full-page" })
|
|
assert.deepEqual({ ...await f.run() }, { scanned: 128, compacted: 0, skipped: 128, automaticReplay: false })
|
|
assert.equal(claimed(f).length, 0)
|
|
assert.deepEqual({ ...await f.run() }, { scanned: 1, compacted: 1, skipped: 0, automaticReplay: false })
|
|
assert.equal(claimed(f).length, 1)
|
|
})
|
|
|
|
test("failed accounting never consumes claim or runs codec", async () => {
|
|
const f = fixture(); f.configure({ accountingFails: true }); await assert.rejects(f.run(), model.PollStorageRefused)
|
|
assert.equal(claimed(f).length, 0); assert.equal(f.counts().createCalls, 0)
|
|
assert.equal(f.commands.filter(command => command.text === "ROLLBACK").length, 1)
|
|
})
|
|
|
|
test("phase1 COMMIT ACK loss never starts phase2 or inserts archive, for either database outcome", async () => {
|
|
for (const commitPersists of [false, true]) {
|
|
const f = fixture(); f.configure({ lostCommit: 1, commitPersists })
|
|
await assert.rejects(f.run(), error => error instanceof f.Unknown && !error.message.includes("PRIVATE_DRIVER"))
|
|
assert.equal(f.counts().connects, 1); assert.equal(f.counts().commits, 1); assert.equal(published(f).length, 0)
|
|
assert(!f.commands.some(command => command.text === "ROLLBACK")); assert.equal(!!f.state().claim, commitPersists)
|
|
assert.equal(f.state().archive, null); assert.deepEqual(f.releases, [{ tx: 1, unsafe: true }])
|
|
}
|
|
})
|
|
|
|
test("normal two acknowledged COMMITs publish exact readback and audit in the same transaction", async () => {
|
|
const f = fixture(), result = await f.run()
|
|
assert.equal(result.compacted, 1); assert.equal(result.automaticReplay, false)
|
|
assert.deepEqual(f.counts(), { connects: 1, commits: 2, createCalls: 2, openCalls: 1, accountCalls: 3 })
|
|
const firstCommit = f.events.indexOf("1:COMMIT"), secondBegin = f.events.indexOf("2:BEGIN")
|
|
assert(firstCommit >= 0 && firstCommit < secondBegin)
|
|
for (const tx of [1, 2]) {
|
|
const commands = f.commands.filter(command => command.tx === tx), lock = commands.findIndex(command => command.text.includes("pg_advisory_xact_lock"))
|
|
assert(lock >= 0 && lock < commands.findIndex(command => command.text === ACCOUNTING))
|
|
assert(commands.findIndex(command => command.text === ACCOUNTING) < commands.findIndex(command => /^(?:UPDATE server_poll_storage_admissions|INSERT INTO server_poll_archives)/.test(command.text)))
|
|
}
|
|
const second = f.commands.filter(command => command.tx === 2).map(command => command.text)
|
|
const insert = second.findIndex(text => text.startsWith("INSERT INTO server_poll_archives")), read = second.findIndex(text => text.startsWith("SELECT r.*,")), audit = second.findIndex(text => text.startsWith("INSERT INTO mcp_audit"))
|
|
assert(insert < read && read < audit && audit < second.lastIndexOf(ACCOUNTING) && second.lastIndexOf(ACCOUNTING) < second.indexOf("COMMIT"))
|
|
const opened = f.events.indexOf("open:1")
|
|
assert(f.events.findIndex(event => event.startsWith("2:SELECT r.*,")) < opened &&
|
|
opened < f.events.findIndex(event => event.startsWith("2:INSERT INTO mcp_audit")))
|
|
assert.equal(f.state().audits.length, 1); assert.deepEqual(f.state().audits[0].payload, { operationId: f.state().archive!.execution_id, receiptId: f.state().archive!.receipt_id,
|
|
archiveBytes: f.state().archive!.ciphertext_bytes, sourceJournalRows: 6, originalRetentionDays: 30, automaticReplay: false })
|
|
assert.deepEqual(f.releases, [{ tx: 1, unsafe: false }])
|
|
})
|
|
|
|
test("phase2 COMMIT ACK loss leaves spent full or complete archive, never rollback or automatic resume", async () => {
|
|
for (const commitPersists of [false, true]) {
|
|
const f = fixture(); f.configure({ lostCommit: 2, commitPersists })
|
|
await assert.rejects(f.run(), f.Unknown)
|
|
assert(f.state().claim); assert.equal(!!f.state().archive, commitPersists); assert.equal(f.state().audits.length, commitPersists ? 1 : 0)
|
|
assert(!f.commands.some(command => command.text === "ROLLBACK")); assert.equal(published(f).length, 1)
|
|
assert.deepEqual(f.releases, [{ tx: 1, unsafe: true }])
|
|
const counts = f.counts(); const next = await f.run()
|
|
assert.equal(next.scanned, 0); assert.equal(next.compacted, 0); assert.equal(claimed(f).length, 1); assert.equal(published(f).length, 1)
|
|
assert.equal(f.counts().createCalls, counts.createCalls); assert.equal(f.counts().accountCalls, counts.accountCalls)
|
|
}
|
|
})
|
|
|
|
test("phase2 size growth preserves spent full charge and subsequent candidate exclusion", async () => {
|
|
const f = fixture(); f.configure({ secondOversize: true })
|
|
assert.equal((await f.run()).skipped, 1); assert(f.state().claim); assert.equal(f.state().archive, null)
|
|
assert.equal(published(f).length, 0); assert.equal((await f.run()).scanned, 0); assert.equal(claimed(f).length, 1)
|
|
assert(!f.commands.some(command => /^(?:DELETE|UPDATE server_poll_storage_usage)/.test(command.text)))
|
|
})
|
|
|
|
test("published size, text or identity mismatch or audit failure rolls back archive but keeps acknowledged claim", async () => {
|
|
for (const options of [{ badPublishedSize: true }, { badPublishedText: true }, { badPublishedId: true }, { auditFails: true }]) {
|
|
const f = fixture(); f.configure(options); await assert.rejects(f.run(), model.PollStorageRefused)
|
|
assert(f.state().claim); assert.equal(f.state().archive, null); assert.equal(f.state().audits.length, 0)
|
|
assert.equal(f.commands.filter(command => command.tx === 2 && command.text === "ROLLBACK").length, 1)
|
|
assert.equal(f.commands.filter(command => command.tx === 2 && command.text === "COMMIT").length, 0)
|
|
assert.equal((await f.run()).scanned, 0); assert.equal(published(f).length, 1)
|
|
}
|
|
})
|