129 lines
10 KiB
TypeScript
129 lines
10 KiB
TypeScript
/** Internal SQL-only observation. Caller owns the bounded READ ONLY REPEATABLE
|
|
* READ transaction, connection, rollback and writer drain. No pool or lifecycle
|
|
* operations are available here; an observation never authorizes conversion. */
|
|
import { createHash } from "node:crypto"
|
|
import { z } from "zod"
|
|
import { fingerprint } from "../modules/mcp/security.js"
|
|
import { readServerProbeExecutionSource, type ServerProbeExecutionSource } from "../modules/mcp/diagnostic-cancellation-source.js"
|
|
import { createLegacyServerProbeVerifier, type LegacyServerProbeReason } from "./server-probe-legacy-verifier.js"
|
|
import type { ProbeStorageSql } from "./server-probe-journal-maintenance.js"
|
|
|
|
export interface LegacyServerProbeSnapshotOptions { deadlineAt: number; signal?: AbortSignal; batchSize?: number; totalLimit?: number }
|
|
export type LegacyServerProbeSnapshotReason = LegacyServerProbeReason | "SNAPSHOT_REQUIRED" | "INVALID_OPTIONS" | "CANCELLED" | "DEADLINE_EXCEEDED" | "SQL_UNAVAILABLE" | "ROW_LIMIT" | "TOTAL_LIMIT" | "IDENTITY_DRIFT" | "ORPHAN_STAGES"
|
|
export interface LegacyServerProbeSnapshotSummary {
|
|
version: 1; complete: boolean
|
|
counts: { identities: number; verified: number; unresolved: number; mcp: number; rest: number; anchors: number; quarantines: number; cancellations: number; orphanStages: number }
|
|
authenticationCounts: { receipt: number; "approved-source": number; "identity-context-only": number }
|
|
reasons: Readonly<Partial<Record<LegacyServerProbeSnapshotReason, number>>>
|
|
aggregateDigest: string; migrationReady: false; conversionAllowed: false; automaticReplay: false
|
|
requiresDatabaseSourceAttestation: true; requiresWriterDrain: true; requiresLockedReverification: true
|
|
}
|
|
class Stopped extends Error { constructor(readonly reason: LegacyServerProbeSnapshotReason) { super(reason) } }
|
|
const identitySchema = z.object({ identity_kind: z.enum(["mcp", "rest"]), execution_id: z.string().uuid() }).strict()
|
|
|
|
export async function collectLegacyServerProbeSnapshot(sql: ProbeStorageSql, options: LegacyServerProbeSnapshotOptions): Promise<Readonly<LegacyServerProbeSnapshotSummary>> {
|
|
const counts = { identities: 0, verified: 0, unresolved: 0, mcp: 0, rest: 0, anchors: 0, quarantines: 0, cancellations: 0, orphanStages: 0 }
|
|
const authenticationCounts = { receipt: 0, "approved-source": 0, "identity-context-only": 0 }
|
|
const reasons: Partial<Record<LegacyServerProbeSnapshotReason, number>> = {}, digest = createHash("sha256")
|
|
digest.update("server-probe-legacy-snapshot-observation-v1\n")
|
|
const addReason = (reason: LegacyServerProbeSnapshotReason) => { reasons[reason] = (reasons[reason] ?? 0) + 1 }
|
|
let complete = false
|
|
const deadlineAt = options.deadlineAt, signal = options.signal, batchSize = options.batchSize ?? 100, totalLimit = options.totalLimit ?? 10000
|
|
const fence = () => {
|
|
if (signal?.aborted) throw new Stopped("CANCELLED")
|
|
if (Date.now() >= deadlineAt) throw new Stopped("DEADLINE_EXCEEDED")
|
|
}
|
|
const query: ProbeStorageSql["query"] = async (text, values) => {
|
|
fence()
|
|
let result: Awaited<ReturnType<ProbeStorageSql["query"]>>
|
|
try { result = await sql.query(text, values, { signal, timeoutMs: Math.max(1, Math.min(10000, deadlineAt - Date.now())) }) }
|
|
catch { fence(); throw new Stopped("SQL_UNAVAILABLE") }
|
|
fence()
|
|
if (!Array.isArray(result.rows)) throw new Stopped("SQL_UNAVAILABLE")
|
|
// Digest all actually returned rows, including sealed source evidence. This
|
|
// is an observation checksum, not a source generation/readiness capability.
|
|
digest.update(fingerprint({ text, values, rows: result.rows }) + "\n")
|
|
return result
|
|
}
|
|
const unique = (rows: Record<string, unknown>[]) => { if (rows.length > 1) throw new Stopped("ROW_LIMIT"); return rows[0] }
|
|
try {
|
|
if (!Number.isSafeInteger(deadlineAt) || !Number.isSafeInteger(batchSize) || batchSize < 1 || batchSize > 100
|
|
|| !Number.isSafeInteger(totalLimit) || totalLimit < 1 || totalLimit > 10000) throw new Stopped("INVALID_OPTIONS")
|
|
const state = unique((await query(`/* probe-legacy-snapshot:transaction */ SELECT
|
|
current_setting('transaction_isolation') AS isolation,current_setting('transaction_read_only') AS read_only,
|
|
transaction_timestamp()<statement_timestamp() AS explicit_transaction`)).rows)
|
|
// Session defaults alone also apply to autocommit, which would give every
|
|
// SELECT a different snapshot. Require an earlier explicit BEGIN command.
|
|
// This validates caller lifecycle only, never source authority/readiness.
|
|
if (!state || state.isolation !== "repeatable read" || state.read_only !== "on" || state.explicit_transaction !== true) throw new Stopped("SNAPSHOT_REQUIRED")
|
|
const orphan = unique((await query(`/* probe-legacy-snapshot:orphan-stages */ SELECT count(*)::text AS count
|
|
FROM server_probe_execution_stages s LEFT JOIN server_probe_executions e ON e.receipt_id=s.receipt_id WHERE e.receipt_id IS NULL`)).rows)
|
|
const orphanCount = Number(orphan?.count)
|
|
if (!Number.isSafeInteger(orphanCount) || orphanCount < 0) throw new Stopped("ROW_LIMIT")
|
|
counts.orphanStages = orphanCount
|
|
if (orphanCount) throw new Stopped("ORPHAN_STAGES")
|
|
let lastKind: string | null = null, lastExecution: string | null = null
|
|
while (true) {
|
|
const take = Math.min(batchSize, totalLimit - counts.identities)
|
|
const page = (await query(`/* probe-legacy-snapshot:identities */ SELECT identity_kind,execution_id FROM (
|
|
SELECT identity_kind,execution_id FROM server_probe_executions UNION
|
|
SELECT identity_kind,execution_id FROM server_probe_execution_quarantines UNION
|
|
SELECT 'mcp'::text AS identity_kind,operation_id AS execution_id FROM mcp_diagnostic_cancellations
|
|
) identities WHERE $1::text IS NULL OR (identity_kind,execution_id)>($1::text,$2::uuid)
|
|
ORDER BY identity_kind,execution_id LIMIT $3`, [lastKind, lastExecution, take + 1])).rows
|
|
if (page.length > take + 1) throw new Stopped("ROW_LIMIT")
|
|
for (const rawIdentity of page.slice(0, take)) {
|
|
let identity: z.infer<typeof identitySchema>
|
|
try { identity = identitySchema.parse(rawIdentity) } catch { throw new Stopped("IDENTITY_DRIFT") }
|
|
const { identity_kind: kind, execution_id: executionId } = identity
|
|
if (executionId !== executionId.toLowerCase() || lastKind !== null && (kind < lastKind || kind === lastKind && executionId <= lastExecution!)) throw new Stopped("IDENTITY_DRIFT")
|
|
let anchor = unique((await query(`/* probe-legacy-snapshot:anchor */ SELECT * FROM server_probe_executions
|
|
WHERE identity_kind=$1 AND execution_id=$2 LIMIT 2`, [kind, executionId])).rows)
|
|
const quarantine = unique((await query(`/* probe-legacy-snapshot:quarantine */ SELECT * FROM server_probe_execution_quarantines
|
|
WHERE (identity_kind=$1 AND execution_id=$2) OR receipt_id=$3::uuid LIMIT 2`, [kind, executionId, anchor?.receipt_id ?? null])).rows)
|
|
if (!anchor && quarantine) anchor = unique((await query(`/* probe-legacy-snapshot:anchor-receipt */ SELECT * FROM server_probe_executions
|
|
WHERE (identity_kind=$1 AND execution_id=$2) OR receipt_id=$3::uuid LIMIT 2`, [kind, executionId, quarantine.receipt_id])).rows)
|
|
const cancellation = unique((await query(`/* probe-legacy-snapshot:cancellation */ SELECT * FROM mcp_diagnostic_cancellations
|
|
WHERE $1::text='mcp' AND operation_id=$2 LIMIT 2`, [kind, executionId])).rows)
|
|
let source: ServerProbeExecutionSource | undefined, sourceFailure: unknown
|
|
const loadMcpSource = async (operationId: string) => {
|
|
if (operationId !== executionId) throw new Stopped("IDENTITY_DRIFT")
|
|
if (source) return source
|
|
if (sourceFailure) throw sourceFailure
|
|
try {
|
|
const owner = unique((await query(`/* probe-legacy-snapshot:owner */ SELECT o.connection_id,c.owner_id
|
|
FROM mcp_operations o JOIN mcp_connections c ON c.id=o.connection_id WHERE o.id=$1 LIMIT 2`, [operationId])).rows)
|
|
if (!owner || typeof owner.connection_id !== "string" || typeof owner.owner_id !== "string") throw new Stopped("SOURCE_UNAVAILABLE")
|
|
source = await readServerProbeExecutionSource({ query }, operationId, { connectionId: owner.connection_id, ownerId: owner.owner_id })
|
|
fence(); digest.update(fingerprint({ sourceDigest: source.sourceDigest }) + "\n")
|
|
return source
|
|
} catch (failure) { sourceFailure = failure; throw failure }
|
|
}
|
|
if (kind === "mcp") {
|
|
try { await loadMcpSource(executionId) }
|
|
catch (failure) { if (failure instanceof Stopped) throw failure; fence() }
|
|
}
|
|
const receiptId = anchor?.receipt_id ?? quarantine?.receipt_id ?? source?.scope.receiptId
|
|
const stageRows = receiptId === undefined ? [] : (await query(`/* probe-legacy-snapshot:stages */ SELECT * FROM server_probe_execution_stages
|
|
WHERE receipt_id=$1 ORDER BY stage_index LIMIT 12`, [receiptId])).rows
|
|
if (stageRows.length > 11) throw new Stopped("ROW_LIMIT")
|
|
fence()
|
|
const result = await createLegacyServerProbeVerifier({ loadMcpSource })({ identityKind: kind, executionId,
|
|
...(anchor ? { anchor } : {}), ...(quarantine ? { quarantine } : {}), ...(cancellation ? { cancellation } : {}), stages: stageRows })
|
|
fence()
|
|
counts.identities++; counts[kind]++; counts.anchors += Number(!!anchor); counts.quarantines += Number(!!quarantine); counts.cancellations += Number(!!cancellation)
|
|
if (result.status === "verified") { counts.verified++; authenticationCounts[result.authentication]++ }
|
|
else { counts.unresolved++; addReason(result.reasonCode) }
|
|
digest.update(fingerprint({ kind, executionId, result }) + "\n")
|
|
lastKind = kind; lastExecution = executionId
|
|
}
|
|
if (page.length <= take) { complete = true; break }
|
|
if (counts.identities === totalLimit) throw new Stopped("TOTAL_LIMIT")
|
|
}
|
|
} catch (failure) { addReason(failure instanceof Stopped ? failure.reason : "SQL_UNAVAILABLE") }
|
|
digest.update(fingerprint({ complete, counts, reasons }) + "\n")
|
|
return Object.freeze({ version: 1, complete, counts: Object.freeze(counts), authenticationCounts: Object.freeze(authenticationCounts), reasons: Object.freeze(reasons),
|
|
aggregateDigest: digest.digest("hex"), migrationReady: false, conversionAllowed: false, automaticReplay: false,
|
|
requiresDatabaseSourceAttestation: true, requiresWriterDrain: true, requiresLockedReverification: true })
|
|
}
|