Files
MikrotikManager/backend/src/services/server-probe-retention.ts
T

116 lines
7.0 KiB
TypeScript

/** Fixed background maintenance. No caller-selected owner, policy, SQL or native
* executor; expiry predicates and quota refunds remain in the approved store. */
import { fingerprint } from "../modules/mcp/security.js"
import { PROBE_STORAGE_LIMITS } from "../modules/mcp/diagnostic-cancellation-storage.js"
import { connectProbeStorage } from "./server-probe-storage-connection.js"
import { createProbeJournalStorage, ProbeStorageRefused, type ProbeStorageConnection, type ProbeStorageOptions } from "./server-probe-journal-maintenance.js"
import { sharedDatabaseGate } from "./shared-database-gate.js"
import { captureServerProbeTerminalProof } from "./server-probe-operation-retirement.js"
/** Independent from the expiry transaction. A busy, malformed, or uncertain
* source cannot extend retention of its ciphertext or reuse a checkout. */
async function captureBeforeExpiry() {
const deadlineAt = Date.now() + 2200
let candidates: Record<string, unknown>[]
try {
const sql = await connectProbeStorage({ deadlineAt: Math.min(deadlineAt, Date.now()+500) })
let read = false
try {
candidates = (await sql.query(`/* probe-retention:capture-candidates */
SELECT a.receipt_id,o.id AS operation_id,o.connection_id
FROM public.server_probe_storage_admissions a
JOIN public.server_probe_execution_payloads b USING(receipt_id)
JOIN public.mcp_operations o ON o.id=a.execution_id
JOIN public.mcp_plans p ON p.id=o.plan_id AND p.connection_id=o.connection_id
JOIN public.mcp_connections c ON c.id=o.connection_id AND c.owner_id=a.owner_id
WHERE a.identity_kind='mcp' AND a.purged_at IS NULL AND a.cancelled_at IS NULL AND a.claim_token IS NULL
AND a.claim_attempted_at IS NOT NULL AND a.authority_expires_at<clock_timestamp()
AND a.gc_not_before<clock_timestamp()+interval '1 day'
AND o.state='applied' AND o.verification='inconclusive' AND o.error_code IS NULL
AND isfinite(o.finished_at) AND o.finished_at<clock_timestamp()
AND p.operation_id='servers.probes.run' AND p.operation_version=1 AND p.state='completed'
AND isfinite(p.expires_at) AND p.expires_at<clock_timestamp()
AND NOT EXISTS(SELECT 1 FROM public.server_probe_terminal_proofs f WHERE f.operation_id=o.id OR f.receipt_id=a.receipt_id)
AND NOT EXISTS(SELECT 1 FROM public.server_probe_retired_sources r WHERE r.operation_id=o.id OR r.receipt_id=a.receipt_id)
ORDER BY a.gc_not_before,a.receipt_id LIMIT 8`, [], {timeoutMs:450})).rows
read = true
} finally { sql.release(!read) }
} catch { return }
for (const row of candidates) {
if (Date.now() >= deadlineAt || candidates.length > 8) break
if (typeof row.receipt_id !== "string" || typeof row.operation_id !== "string" || typeof row.connection_id !== "string") continue
try {
await captureServerProbeTerminalProof({ receiptId:row.receipt_id, operationId:row.operation_id, connectionId:row.connection_id },
Math.min(deadlineAt, Date.now()+550))
} catch { /* Never hold the payload or retry a capture in this window. */ }
}
}
function createRetention(connect: typeof connectProbeStorage, preCapture?: () => Promise<void>) {
let running = false, cursor = ""
return async () => {
if (running) return { skipped: true, owners: 0, collected: 0 }
running = true
try {
return await sharedDatabaseGate.request(async () => {
await preCapture?.()
const options: ProbeStorageOptions = { deadlineAt: Date.now() + 20000 }
const physical = await connect(options)
let active = true, broken = false, released = false, currentOwner = ""
const destroy = () => { broken = true; if (!released) { released = true; physical.release(true) } }
const fence = () => { if (!active || broken || Date.now() >= options.deadlineAt) throw new ProbeStorageRefused() }
const query: ProbeStorageConnection["query"] = async (text, values, queryOptions) => {
fence()
try {
const result = await physical.query(text, values, { timeoutMs: Math.max(1, Math.min(10000, options.deadlineAt - Date.now(), queryOptions?.timeoutMs ?? 10000)) })
fence(); return result
} catch { destroy(); throw new ProbeStorageRefused() }
}
const logical: ProbeStorageConnection = { query, release(unsafe) { if (unsafe) destroy() } }
const retention = async () => {
const rows = (await query("/* probe-retention:settings */ SELECT requests_retention_days FROM mcp_settings WHERE id=1")).rows
const days = rows[0]?.requests_retention_days
if (rows.length !== 1 || typeof days !== "number" || !Number.isInteger(days) || days < 1 || days > 365) throw new ProbeStorageRefused()
return days
}
try {
const days = await retention()
const storage = createProbeJournalStorage({ connect: async () => logical,
policy: { ...PROBE_STORAGE_LIMITS, retentionDays: days }, safetyWindowMs: 60000,
authorize: async (ownerId, operation, scope) => {
fence()
if (!currentOwner || ownerId !== currentOwner || operation !== "purge" || scope) throw new ProbeStorageRefused()
const currentDays = await retention()
if (currentDays !== days) throw new ProbeStorageRefused()
return fingerprint({ format: "server-probe-retention-v1", ownerId, retentionDays: days })
} })
let owners = (await query(`/* probe-retention:owners */ SELECT DISTINCT owner_id FROM server_probe_storage_admissions
WHERE owner_id>$1 ORDER BY owner_id LIMIT 16`, [cursor])).rows
if (!owners.length && cursor) {
cursor = ""
owners = (await query(`/* probe-retention:owners */ SELECT DISTINCT owner_id FROM server_probe_storage_admissions
WHERE owner_id>$1 ORDER BY owner_id LIMIT 16`, [cursor])).rows
}
if (owners.length > 16 || owners.some(row => typeof row.owner_id !== "string" || !row.owner_id || row.owner_id.length > 200)
|| new Set(owners.map(row => row.owner_id)).size !== owners.length) throw new ProbeStorageRefused()
let collected = 0, completedOwners = 0
for (const row of owners) {
fence(); currentOwner = row.owner_id as string
const result = await storage.collectExpired(currentOwner, 25, options)
collected += result.collected; completedOwners++; cursor = currentOwner
}
return { skipped: false, owners: completedOwners, collected }
} finally {
active = false
// A failed/uncertain maintenance round never reuses its backend.
if (!released) { released = true; physical.release(broken) }
}
})
} finally { running = false }
}
}
export const retainServerProbeStorage = createRetention(connectProbeStorage, captureBeforeExpiry)
/** Trusted owned-test composition only; never an HTTP/MCP parameter. */
export const createServerProbeRetentionForTests = createRetention