Files
MikrotikManager/backend/src/services/server-poll-terminal-compaction.ts
T

132 lines
10 KiB
TypeScript

/** Existing-worker maintenance only. Acknowledged one-shot claim precedes the
* sole destructive transaction. A restart never resumes a claimed full row. */
import { randomUUID } from "node:crypto"
import { sharedDatabaseGate } from "./shared-database-gate.js"
import { connectProbeStorage } from "./server-probe-storage-connection.js"
import { admitPollStorageWriter, createPollMaintenanceWriter, runPollStorageTransaction, PollStorageRefused, type PollStorageDependencies } from "./server-poll-storage.js"
import { assertServerPollArchiveAccounting } from "./server-poll-archive-accounting.js"
import { createServerPollTerminalArchive, openServerPollTerminalArchive, ServerPollArchiveRefused, type ServerPollTerminalArchive } from "./server-poll-terminal-archive.js"
const fail: () => never = () => { throw new PollStorageRefused() }
const uuid = /^[a-f\d]{8}-[a-f\d]{4}-[1-8][a-f\d]{3}-[89ab][a-f\d]{3}-[a-f\d]{12}$/i
const production: PollStorageDependencies = { connect: connectProbeStorage, now: Date.now }
// A slow skipped prefix must not consume every tick's bounded scan window.
const scanCursors = new WeakMap<PollStorageDependencies, { expiresAt: string; receiptId: string }>()
const CANDIDATE_LIMIT = 128
const CANDIDATES = `SELECT a.receipt_id,a.authority_expires_at::text AS authority_expires_at FROM server_poll_storage_admissions a
JOIN mcp_operations o ON o.id=a.execution_id AND o.connection_id=a.connection_id
JOIN mcp_plans p ON p.id=o.plan_id AND p.connection_id=o.connection_id
JOIN mcp_operation_steps s ON s.operation_id=o.id AND s.step_index=0
WHERE a.storage_state='full' AND a.compaction_claim IS NULL AND a.identity_kind='mcp'
AND o.state='applied' AND o.verification='passed' AND o.error_code IS NULL AND o.finished_at IS NOT NULL
AND p.operation_id='servers.poll' AND p.operation_version=1 AND p.state='completed' AND p.approved_by IS NOT NULL
AND s.state='verified' AND s.finished_at IS NOT NULL
AND GREATEST(a.authority_expires_at,p.expires_at,o.finished_at,s.finished_at)+a.safety_ms*interval '1 millisecond'<clock_timestamp()
AND NOT EXISTS(SELECT 1 FROM server_poll_quarantines q WHERE q.receipt_id=a.receipt_id)
AND ($1::timestamptz IS NULL OR (a.authority_expires_at,a.receipt_id)>($1::timestamptz,$2::uuid))
ORDER BY a.authority_expires_at,a.receipt_id LIMIT ${CANDIDATE_LIMIT}`
/** Fixed column projection shared with the bounded history reader. Retention
* metadata belongs to the immutable admission; digest/length are ACTUAL SQL. */
export const SERVER_POLL_ARCHIVE_READ_SQL = `SELECT r.*,
to_char(r.original_activity_at AT TIME ZONE 'UTC','YYYY-MM-DD"T"HH24:MI:SS.US"Z"') AS original_activity_at,
to_char(r.original_expires_at AT TIME ZONE 'UTC','YYYY-MM-DD"T"HH24:MI:SS.US"Z"') AS original_expires_at,
a.retention_days,a.safety_ms,pg_catalog.encode(pg_catalog.sha256(pg_catalog.convert_to(r.ciphertext,'UTF8')),'hex') AS ciphertext_sha256,
octet_length(r.ciphertext) AS actual_ciphertext_bytes FROM server_poll_archives r
JOIN server_poll_storage_admissions a USING(receipt_id) WHERE r.receipt_id=$1 AND a.storage_state='compacted' LIMIT 2`
function insert(archive: ServerPollTerminalArchive) {
// No caller JSON selects columns, table, SQL or identity. SQL owns the entire
// archive→old-children deletion→tombstone→exact-charge transition.
const names = ["receipt_id", "version", "execution_id", "connection_id", "owner_id", "binding_proof", "context_proof", "source_digest", "source_sha256", "terminal_digest",
"original_activity_at", "original_expires_at", "decoded_bytes", "compressed_bytes", "ciphertext", "ciphertext_bytes", "archive_proof"] as const
return { text: `INSERT INTO server_poll_archives(${names.join(",")}) VALUES(${names.map((_name, index) => "$" + (index + 1)).join(",")}) RETURNING receipt_id`,
values: names.map(name => archive[name]) }
}
async function compact(deps:PollStorageDependencies){
const firstDeadline=deps.now()+20000,writer=createPollMaintenanceWriter(deps,firstDeadline+20000)
try{return await compactOnWriter(writer,firstDeadline,deps)}finally{writer.destroy()}
}
async function compactOnWriter(writer:ReturnType<typeof createPollMaintenanceWriter>,firstDeadline:number,sourceDeps:PollStorageDependencies) {
const {deps}=writer
let scanned = 0, skipped = 0
const claimed = await runPollStorageTransaction(deps, { deadlineAt: firstDeadline }, async () => {}, async sql => {
const previous = scanCursors.get(sourceDeps)
const candidates = (await sql.query(CANDIDATES, [previous?.expiresAt ?? null, previous?.receiptId ?? null])).rows
if (candidates.length > CANDIDATE_LIMIT) fail()
// Idle ticks do not hash the complete retained history. An allocation or
// destructive transition still authenticates it under this same lock.
if (!candidates.length) { scanCursors.delete(sourceDeps); return undefined }
await assertServerPollArchiveAccounting(sql)
for (const candidate of candidates) {
if (deps.now() >= firstDeadline - 2000) break
if (typeof candidate.receipt_id !== "string" || !uuid.test(candidate.receipt_id) || typeof candidate.authority_expires_at !== "string") fail()
scanCursors.set(sourceDeps, { expiresAt: candidate.authority_expires_at, receiptId: candidate.receipt_id })
scanned++
// The SQL function takes the exact operation/journal locks without
// waiting on a current connection owner. NULL is an ineligible/busy row.
const rows = (await sql.query("SELECT server_poll_storage_source_payload($1::uuid)::text AS source_text,clock_timestamp() AS observed_at", [candidate.receipt_id])).rows
if (rows.length !== 1) fail()
const text = rows[0].source_text
if (text === null) { skipped++; continue }
if (typeof text !== "string") fail()
const observedAt = new Date(rows[0].observed_at as string | Date).getTime()
try {
// Source proofs, full audit and encoded size pass BEFORE consuming the
// one-shot claim. This preflight publishes nothing and frees nothing.
if (!await createServerPollTerminalArchive(text, observedAt)) { skipped++; continue }
} catch (error) {
if (!(error instanceof ServerPollArchiveRefused)) throw error
skipped++; continue
}
const token = randomUUID()
const changed = (await sql.query("UPDATE server_poll_storage_admissions SET compaction_claim=$2::uuid,compaction_claimed_at=clock_timestamp() WHERE receipt_id=$1::uuid AND storage_state='full' AND compaction_claim IS NULL RETURNING receipt_id", [candidate.receipt_id, token])).rows
if (changed.length !== 1 || changed[0].receipt_id !== candidate.receipt_id) fail()
scanCursors.delete(sourceDeps)
return { receiptId: candidate.receipt_id, token }
}
if (scanned === candidates.length && candidates.length < CANDIDATE_LIMIT) scanCursors.delete(sourceDeps)
return undefined
}, async sql => {
if(!await writer.tryAcquire(sql))return {deferred:undefined}
await admitPollStorageWriter(sql,firstDeadline)
})
if (!claimed){await writer.finish(firstDeadline);return { scanned, compacted: 0, skipped, automaticReplay: false as const }}
// This branch is reached ONLY after the separate claim COMMIT was ACKed.
// Any loss after here leaves either a complete archive or a charged spent
// full row. Fresh workers select compaction_claim IS NULL and never replay it.
const secondDeadline = deps.now() + 20000
const compacted = await runPollStorageTransaction(deps, { deadlineAt: secondDeadline }, async () => {}, async sql => {
await assertServerPollArchiveAccounting(sql)
const admission = (await sql.query("SELECT receipt_id FROM server_poll_storage_admissions WHERE receipt_id=$1::uuid AND storage_state='full' AND compaction_claim=$2::uuid AND compaction_claimed_at IS NOT NULL AND compaction_claim_xid IS NOT NULL FOR UPDATE", [claimed.receiptId, claimed.token])).rows
if (admission.length !== 1) fail()
const rows = (await sql.query("SELECT server_poll_storage_source_payload($1::uuid)::text AS source_text,clock_timestamp() AS observed_at", [claimed.receiptId])).rows
if (rows.length !== 1 || typeof rows[0].source_text !== "string") fail()
const text = rows[0].source_text, now = new Date(rows[0].observed_at as string | Date).getTime()
const archive = await createServerPollTerminalArchive(text, now)
if (!archive) return 0 // Oversize retains the FULL charge and spent claim.
if (archive.receipt_id !== claimed.receiptId) fail()
const command = insert(archive), inserted = (await sql.query(command.text, command.values)).rows
if (inserted.length !== 1 || inserted[0].receipt_id !== claimed.receiptId) fail()
const published = (await sql.query(SERVER_POLL_ARCHIVE_READ_SQL, [claimed.receiptId])).rows
if (published.length !== 1 || published[0].actual_ciphertext_bytes !== archive.ciphertext_bytes) fail()
// Authenticate the persisted representation before commit. A refusal rolls
// back both the archive insert and its trigger's journal/charge transition.
const authenticated = await openServerPollTerminalArchive(published[0], now)
if (authenticated.sourceText !== text || authenticated.metadata.receipt_id !== claimed.receiptId) fail()
await sql.query("INSERT INTO mcp_audit(actor_id,action,connection_id,payload) VALUES($1,$2,$3,$4::jsonb)",
["mcp-worker", "poll.history.compacted", archive.connection_id, JSON.stringify({ operationId: archive.execution_id,
receiptId: archive.receipt_id, archiveBytes: archive.ciphertext_bytes,
sourceJournalRows: authenticated.source.journal.intents.length + authenticated.source.journal.acks.length + 2,
originalRetentionDays: archive.retention_days, automaticReplay: false })])
await assertServerPollArchiveAccounting(sql)
return 1
}, sql => admitPollStorageWriter(sql, secondDeadline))
await writer.finish(secondDeadline)
return { scanned, compacted, skipped: skipped + (compacted ? 0 : 1), automaticReplay: false as const }
}
export const compactCompletedServerPollStorage = () => sharedDatabaseGate.request(() => compact(production))
/** Actual owned-SQL fault injection only; not a registered maintenance API. */
export const compactCompletedServerPollStorageForTests = (deps: PollStorageDependencies) => sharedDatabaseGate.request(() => compact(deps))