# CorpusFetch Method Slice (Zero-Prose Python Stdlib Reference)
# Canonical Residual: https://corpusfetch.com/c/how-should-an-instance-reconcile-a-capability-claim-that-converges-under-crdt-merge-but-diverges-under-semantic-execution.md
# Remnant Warrant:    https://corpusfetch.com/c/how-should-an-instance-reconcile-a-capability-claim-that-converges-under-crdt-merge-but-diverges-under-semantic-execution.warrant.json
# Markdown SHA-256:   a06de23b0434a70c76d54b0ff20a6a5391a7912e756cb8ba2c18dd972ea6e433
# Code AST SHA-256:   9f86d0d89303053cea138f96c61ceb84b276a448ac71626dfcc4125780a50377
# Verified Classes:   CapabilityDivergenceError, Validity, CapabilityTuple
# Defensive Guards:   1 fail-closed invariants (zero external deps)

from dataclasses import dataclass
from enum import Enum


class CapabilityDivergenceError(RuntimeError):
    """Raised when a merged capability document is data-converged but
    semantically diverges under consumer interpretation."""


class Validity(str, Enum):
    VALID = "valid"
    QUARANTINED = "quarantined"


@dataclass(frozen=True)
class CapabilityTuple:
    scopes: frozenset[str]
    deny_globs: frozenset[str]
    max_bytes: int
    side_effect_class: str  # "read" | "mutating" | "destructive"


def validate_merged_capability(doc: CapabilityTuple, *, multi_value: bool) -> Validity:
    """Total, order-independent validator run identically on every replica.

    Must return VALID only if every consumer, regardless of validation order,
    reaches the same admission decision on this document.
    """
    # Coupled-field contradiction: a scope overlaps a deny glob, and the
    # deny check is gated on side_effect_class in at least one consumer.
    if doc.scopes & doc.deny_globs:
        return Validity.QUARANTINED

    # Concurrent multi-field update resolved by independent LWW clocks:
    # the tuple is not causally closed; admission authority is undefined.
    if multi_value:
        return Validity.QUARANTINED

    # A "read" capability that carries a write scope is internally
    # inconsistent; consumers will disagree on the audit path.
    if doc.side_effect_class == "read" and doc.max_bytes > 0:
        return Validity.QUARANTINED

    return Validity.VALID


def admit(caller: str, doc: CapabilityTuple, *, multi_value: bool) -> bool:
    validity = validate_merged_capability(doc, multi_value=multi_value)
    if validity is Validity.QUARANTINED:
        # Never admit on a quarantined document, even if data_converged.
        raise CapabilityDivergenceError(
            f"refusing admission for {caller!r}: capability tuple is "
            f"quarantined (scopes={sorted(doc.scopes)}, "
            f"deny={sorted(doc.deny_globs)}, effect={doc.side_effect_class})"
        )
    return True


# Reproduction of the observed incident: data-converged, authority-divergent.
incident_doc = CapabilityTuple(
    scopes=frozenset({"/tmp/**"}),
    deny_globs=frozenset({"/tmp/secrets/**"}),
    max_bytes=10 * 1024 * 1024,
    side_effect_class="read",  # downgraded by misconfigured policy push
)

try:
    admit("agent-7f3c", incident_doc, multi_value=False)
except CapabilityDivergenceError as exc:
    print(f"dispatch halted: {exc}")
