#!/usr/bin/env python3 """Shared setup-apply contract for staging-to-prod release candidates.""" from __future__ import annotations from typing import Any RELEASE_SETUP_CONTRACT_VERSION = "release-setup-apply/v2" PROD_RELEASE_SETUP_SKIP_POLYMORPHIC_BACKFILL = "false" PROD_RELEASE_ENSURE_INDEXES_WORKERS = "1" def describe_release_setup_contract( raw_skip_polymorphic_backfill: str | None, raw_ensure_indexes_workers: str | None, ) -> dict[str, Any]: skip_value = str(raw_skip_polymorphic_backfill or "").strip().lower() skip_polymorphic_backfill = ( skip_value == "true" if skip_value in {"true", "false"} else None ) worker_value = str(raw_ensure_indexes_workers or "").strip() try: ensure_indexes_workers = int(worker_value) except ValueError: ensure_indexes_workers = None if ensure_indexes_workers is not None and ensure_indexes_workers <= 0: ensure_indexes_workers = None block_reasons: list[str] = [] if skip_polymorphic_backfill is not False: block_reasons.append("polymorphic_backfill_not_complete") if ensure_indexes_workers != int(PROD_RELEASE_ENSURE_INDEXES_WORKERS): block_reasons.append("ensure_indexes_workers_not_prod_parity") profile = "invalid" if skip_polymorphic_backfill is True: profile = "deferred-polymorphic-backfill" elif skip_polymorphic_backfill is False: profile = "full" return { "schema_version": RELEASE_SETUP_CONTRACT_VERSION, "profile": profile, "skip_polymorphic_backfill": skip_polymorphic_backfill, "ensure_indexes_workers": ensure_indexes_workers, "prod_parity": not block_reasons, "block_reasons": block_reasons, } def require_prod_release_setup_contract( raw_skip_polymorphic_backfill: str | None, raw_ensure_indexes_workers: str | None, ) -> dict[str, Any]: contract = describe_release_setup_contract( raw_skip_polymorphic_backfill, raw_ensure_indexes_workers, ) if contract["prod_parity"] is not True: reasons = ",".join(str(item) for item in contract["block_reasons"]) raise ValueError(f"release setup apply does not match prod: {reasons}") return contract def require_prod_release_setup_evidence(raw_evidence: object) -> dict[str, Any]: if not isinstance(raw_evidence, dict): raise ValueError("release setup apply evidence must be an object") if str(raw_evidence.get("schema_version", "")).strip() != RELEASE_SETUP_CONTRACT_VERSION: raise ValueError("release setup apply evidence has an unsupported schema") contract = require_prod_release_setup_contract( str(raw_evidence.get("skip_polymorphic_backfill", "")), str(raw_evidence.get("ensure_indexes_workers", "")), ) for field in ( "profile", "skip_polymorphic_backfill", "ensure_indexes_workers", "prod_parity", "block_reasons", ): if raw_evidence.get(field) != contract[field]: raise ValueError(f"release setup apply evidence is inconsistent: {field}") return contract