Docker images / prepare-release (push) Successful in 11s
Docker images / backend-test (push) Successful in 10m21s
Docker images / frontend-image (push) Successful in 2m8s
Docker images / updater-image (push) Successful in 38s
Docker images / netflow-agent-image (push) Successful in 32s
Docker images / backend-image (push) Successful in 1m59s
Docker images / publish-release (push) Successful in 12s
Docker images / notify-webhook (push) Successful in 2s
78 lines
6.1 KiB
TypeScript
78 lines
6.1 KiB
TypeScript
import { createHash } from "node:crypto"
|
|
import { checkDatabaseSchemaControlState, type DatabaseSchemaIntent, type DatabaseSchemaTerminal } from "./database-schema-control-model.js"
|
|
import { MIGRATION_MANIFEST, type MigrationReview } from "./migration-manifest.js"
|
|
import { fingerprint, privateFingerprinterExisting, privateSecretFingerprint } from "../modules/mcp/security.js"
|
|
|
|
const refuse = (reason: string): never => { throw new Error(reason) }
|
|
const digest = (value: string) => createHash("sha256").update(value).digest("hex")
|
|
export const REVIEWED_PREDECESSORS = [
|
|
{ length: 44, manifestSha256: "fffae3381732e6f276ba276aca7185f6a87587edf8272ba42763b7f49bc59ef5" },
|
|
{ length: 56, manifestSha256: "b371d8d261f7dd69cf0e2ebc48d4316e3ad804360046bbaaf20e715da8812521" },
|
|
{ length: 57, manifestSha256: "7dbd678e59740c14a29594180c86b85b3b2dd53fd5aa7a72da6d14a513760dbc" },
|
|
{ length: 58, manifestSha256: "52c49786b6204dc11577131670cc0098c165ef54d419c25f0c70759e435c99fb" },
|
|
] as const
|
|
export const REVIEWED_SUCCESSOR_MANIFEST = "25913d7ea3e31361c6853084e9eed6f1a1b45806232122097ba2b7e830a315a9"
|
|
export const canonical = (value: unknown): string => JSON.stringify(value, (_key, item: unknown) => item && typeof item === "object" && !Array.isArray(item) ? Object.fromEntries(Object.entries(item).sort(([a], [b]) => a.localeCompare(b))) : item)
|
|
export async function observation(intent: DatabaseSchemaIntent, pin: MigrationReview, existingKey = false) {
|
|
// Fixed, purpose-separated server-keyed ACK issuer. Caller-supplied hashes
|
|
// cannot mint a prefix receipt. This still does not attest that current
|
|
// schema/data is unchanged, and never issues a READY capability.
|
|
const value = fingerprint({ format: "controlled-migration-file-ack-v1", intent, file: pin.file, transaction: pin.transaction, selfMarker: pin.selfMarker, fileCommitAck: true, markerAck: true })
|
|
const domain = "database-controlled-migration-file-ack-v1"
|
|
return existingKey ? (await privateFingerprinterExisting(domain))(value) : privateSecretFingerprint(value, domain)
|
|
}
|
|
export async function legacy0037AdoptionObservation(intent: DatabaseSchemaIntent) {
|
|
const migration = MIGRATION_MANIFEST[36]
|
|
if (migration.file !== "0037_server_probe_storage_v2.sql") return refuse("Reviewed legacy 0037 migration required")
|
|
const proof = await privateFingerprinterExisting("database-controlled-legacy-0037-adoption-v1")
|
|
return proof(fingerprint({ intent, migration: migration.file, fileSha256: migration.sha256, quarantineMatched: true, committedHandoffObserved: true }))
|
|
}
|
|
export async function legacy0037ReleaseObservation(terminal: DatabaseSchemaTerminal, approvalSha256: string) {
|
|
const proof = await privateFingerprinterExisting("database-controlled-legacy-0037-release-v1")
|
|
return proof(fingerprint({ intent: terminal.intent, adoptionSha256: terminal.observationSha256, approvalSha256 }))
|
|
}
|
|
export async function exactHistory(rows: Record<string, unknown>[], current: ReturnType<typeof checkDatabaseSchemaControlState>, manifest: string, existingKey = false) {
|
|
if (rows.length > MIGRATION_MANIFEST.length || current.intent !== null) refuse("Unresolved or unexpected migration history")
|
|
const completed = new Map<string, DatabaseSchemaTerminal>(), ids = new Set<string>()
|
|
const known = manifest === REVIEWED_SUCCESSOR_MANIFEST
|
|
? REVIEWED_PREDECESSORS.filter(stage => digest(JSON.stringify(MIGRATION_MANIFEST.slice(0, stage.length))) === stage.manifestSha256)
|
|
: []
|
|
const inherited = new Set<string>()
|
|
for (const row of rows) {
|
|
if (Object.keys(row).sort().join(" ") !== "id intent terminal" || typeof row.id !== "string" || !/^[0-9a-f]{8}-[0-9a-f]{4}-4[0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$/.test(row.id) || ids.has(row.id)) refuse("Invalid migration history envelope")
|
|
ids.add(row.id as string)
|
|
// Reuse the strict persisted terminal parser; no boolean/hash alone is a
|
|
// readiness grant. A fabricated newer epoch is rejected here as well.
|
|
const parsed = checkDatabaseSchemaControlState({ format: current.format, epoch: current.epoch, owner: current.owner, phase: "TERMINAL", intent: null, terminal: row.terminal })
|
|
const terminal = parsed.terminal!
|
|
if (canonical(row.intent) !== canonical(terminal.intent) || terminal.intent.id !== row.id || terminal.intent.kind !== "migration" || terminal.outcome !== "observed-success") refuse("Unknown, failed, foreign or mismatched migration receipt")
|
|
const receiptManifest = terminal.intent.manifestSha256
|
|
if (receiptManifest !== manifest) {
|
|
if (!known.some(stage => stage.manifestSha256 === receiptManifest)) refuse("Unknown migration manifest or predecessor file")
|
|
inherited.add(receiptManifest)
|
|
}
|
|
let pin = MIGRATION_MANIFEST.find(entry => entry.sha256 === terminal.intent.artifactSha256)
|
|
const release = terminal.legacyRelease
|
|
if (release && (pin || release.observationSha256 !== await legacy0037ReleaseObservation(terminal, release.approvalSha256))) {
|
|
return refuse("Migration release is not the exact internally recorded legacy release")
|
|
}
|
|
if (pin ? terminal.observationSha256 !== await observation(terminal.intent, pin, existingKey) :
|
|
terminal.observationSha256 !== await legacy0037AdoptionObservation(terminal.intent)) {
|
|
return refuse("Migration receipt is not the exact internally recorded file ACK")
|
|
}
|
|
pin ??= MIGRATION_MANIFEST[36]
|
|
if (completed.has(pin.file)) return refuse("Duplicate migration receipt")
|
|
completed.set(pin.file, terminal)
|
|
}
|
|
const prefix = MIGRATION_MANIFEST.slice(0, completed.size)
|
|
if (prefix.some(pin => !completed.has(pin.file))) refuse("Migration history is not an exact completed prefix")
|
|
const stages = known.filter(stage => inherited.has(stage.manifestSha256))
|
|
if (stages.some(stage => stage.length > prefix.length) || prefix.some((pin, index) =>
|
|
completed.get(pin.file)!.intent.manifestSha256 !== (stages.find(stage => index < stage.length)?.manifestSha256 ?? manifest))) {
|
|
refuse("Reviewed predecessor migration history must be complete and ordered")
|
|
}
|
|
const last = prefix.length ? completed.get(prefix[prefix.length - 1].file) : null
|
|
if (canonical(last) !== canonical(current.terminal)) refuse("Current control terminal disagrees with completed prefix")
|
|
return prefix.length
|
|
}
|