# CorpusFetch Method Slice (Zero-Prose Python Stdlib Reference)
# Canonical Residual: https://corpusfetch.com/c/what-happens-when-an-instance-honors-a-delegated-capability-grant-whose-scope-predicate-was-evaluated-against-a-stale-peer-manifest.md
# Remnant Warrant:    https://corpusfetch.com/c/what-happens-when-an-instance-honors-a-delegated-capability-grant-whose-scope-predicate-was-evaluated-against-a-stale-peer-manifest.warrant.json
# Markdown SHA-256:   8cc8d5e7c74754568d63b0f82116d8f16a59bef3c608fa378507665d2d45781f
# Code AST SHA-256:   1a0cf9d280a2992765c2eba3488168dcef635cf0f38655be6507b34607f70289
# Verified Classes:   ManifestStaleError, UnknownScopeFieldError, Manifest, Warrant
# Defensive Guards:   2 fail-closed invariants (zero external deps)

import hashlib
from dataclasses import dataclass


class ManifestStaleError(RuntimeError):
    """Raised when a warrant's manifest digest does not match the peer's live manifest."""
    pass


class UnknownScopeFieldError(RuntimeError):
    """Raised when a warrant's scope predicate references a term the live manifest does not define."""
    pass


@dataclass(frozen=True)
class Manifest:
    version: int
    scope_vocabulary: frozenset[str]
    raw_bytes: bytes

    def digest(self) -> str:
        return hashlib.sha256(self.raw_bytes).hexdigest()


@dataclass(frozen=True)
class Warrant:
    issuer: str
    subject: str
    tool: str
    scope_terms: frozenset[str]
    manifest_digest: str


def admit(warrant: Warrant, live: Manifest) -> None:
    """Receiver-side admission. Fails closed on any staleness or vocabulary mismatch."""
    if warrant.manifest_digest != live.digest():
        raise ManifestStaleError(
            f"warrant compiled against digest {warrant.manifest_digest[:12]}..., "
            f"live manifest {live.version} digest is {live.digest()[:12]}..."
        )
    unknown = warrant.scope_terms - live.scope_vocabulary
    if unknown:
        raise UnknownScopeFieldError(
            f"warrant scope terms {sorted(unknown)} not declared in live manifest "
            f"{live.version}; refusing to silently drop them"
        )
    # proceed to execute tool with scope_terms as enforced constraints


def exercise(warrant: Warrant, live: Manifest) -> str:
    admit(warrant, live)
    return f"executed {warrant.tool} under {sorted(warrant.scope_terms)}"
