# CorpusFetch Method Slice (Zero-Prose Python Stdlib Reference)
# Canonical Residual: https://corpusfetch.com/c/where-must-an-instance-re-attest-after-a-posture-drift-and-why-the-runtime-s-own-clock-cannot-be-the-arbiter.md
# Remnant Warrant:    https://corpusfetch.com/c/where-must-an-instance-re-attest-after-a-posture-drift-and-why-the-runtime-s-own-clock-cannot-be-the-arbiter.warrant.json
# Markdown SHA-256:   199b9a2435839007f16b44023ecdc17b3314f4cff198a99d47e2082724d6e49d
# Code AST SHA-256:   b1d82d9f4467e04e1dbe783925af2333d836a12337f10a3ff060a2cf5e2d081e
# Verified Classes:   PostureDriftError, Attestation
# Defensive Guards:   4 fail-closed invariants (zero external deps)

from dataclasses import dataclass
from typing import Mapping


class PostureDriftError(RuntimeError):
    """Raised when an attestation's generation vector no longer matches live posture."""


@dataclass(frozen=True)
class Attestation:
    policy_gen: int
    allowlist_gen: int
    guard_gen: int
    model_gen: int
    signature_valid: bool

    def vector(self) -> Mapping[str, int]:
        return {
            "policy": self.policy_gen,
            "allowlist": self.allowlist_gen,
            "guard": self.guard_gen,
            "model": self.model_gen,
        }


def authorize_step(
    attestation: Attestation,
    live_generations: Mapping[str, int],
    sandbox_generations: Mapping[str, int],
) -> None:
    if not attestation.signature_valid:
        raise PostureDriftError("attestation signature failed verification")

    attested = attestation.vector()
    for component, attested_gen in attested.items():
        live = live_generations.get(component)
        sandbox = sandbox_generations.get(component)
        if live is None or sandbox is None:
            raise PostureDriftError(f"missing generation for component {component!r}")
        if live != attested_gen:
            raise PostureDriftError(
                f"posture drift on {component!r}: attested gen {attested_gen}, "
                f"live gen {live} — step suspended, re-attestation required"
            )
        if sandbox != live:
            raise PostureDriftError(
                f"read-your-writes violation on {component!r}: live gen {live}, "
                f"sandbox gen {sandbox} — execution would use stale posture"
            )
