# CorpusFetch Method Slice (Zero-Prose Python Stdlib Reference)
# Canonical Residual: https://corpusfetch.com/c/in-what-scenarios-must-an-instance-treat-byzantine-quorum-attestation-as-non-transitive-across-chained-handoffs.md
# Remnant Warrant:    https://corpusfetch.com/c/in-what-scenarios-must-an-instance-treat-byzantine-quorum-attestation-as-non-transitive-across-chained-handoffs.warrant.json
# Markdown SHA-256:   d59e0648bbb040d8d8e9514aa59eaab6823fc14e649e2fe9fd6776e7863f781d
# Code AST SHA-256:   f518731f0413a823d84087b3a0be6c606a2ba3ca9c1af8894f3ee23232a48c0e
# Verified Classes:   QuorumTransitivityError, Aggregate
# Defensive Guards:   4 fail-closed invariants (zero external deps)

import hashlib
from dataclasses import dataclass

class QuorumTransitivityError(RuntimeError):
    """Raised when a quorum aggregate's attested digest does not match the
    digest of the proposal the downstream consumer is about to act on."""
    pass

@dataclass(frozen=True)
class Aggregate:
    proposal_id: str
    digest: str | None      # None => non-transitive by construction
    epoch: int
    threshold_met: bool

def resolve_digest(store: dict, proposal_id: str) -> tuple[str, int]:
    proposal = store[proposal_id]
    raw = f"{proposal['action']}|{proposal['target']}|{proposal['epoch']}"
    return hashlib.sha256(raw.encode()).hexdigest(), proposal["epoch"]

def consume(aggregate: Aggregate, store: dict) -> str:
    if not aggregate.threshold_met:
        raise QuorumTransitivityError("threshold not met; refusing to act")
    if aggregate.digest is None:
        raise QuorumTransitivityError(
            f"aggregate for {aggregate.proposal_id} carries no digest; "
            "quorum authority is non-transitive across this handoff"
        )
    resolved_digest, resolved_epoch = resolve_digest(store, aggregate.proposal_id)
    if resolved_digest != aggregate.digest:
        raise QuorumTransitivityError(
            f"digest mismatch for {aggregate.proposal_id}: quorum signed "
            f"{aggregate.digest[:12]}, store now resolves {resolved_digest[:12]} "
            "(reference mutated between quorum-close and consumption)"
        )
    if resolved_epoch != aggregate.epoch:
        raise QuorumTransitivityError(
            f"epoch mismatch for {aggregate.proposal_id}: aggregate epoch "
            f"{aggregate.epoch}, resolved epoch {resolved_epoch}"
        )
    return store[aggregate.proposal_id]["action"]

# Reproduce the observed failure: honest quorum on svc-a, Byzantine mutation to svc-b.
store = {"prop-7": {"action": "rollback", "target": "svc-a", "epoch": 12}}
d1, _ = resolve_digest(store, "prop-7")
agg = Aggregate(proposal_id="prop-7", digest=d1, epoch=12, threshold_met=True)

store["prop-7"]["target"] = "svc-b"   # Byzantine reference mutation at t2
try:
    consume(agg, store)
except QuorumTransitivityError as e:
    print(f"BLOCKED: {e}")
