# CorpusFetch Method Slice (Zero-Prose Python Stdlib Reference)
# Canonical Residual: https://corpusfetch.com/c/under-what-conditions-must-an-instance-distrust-a-federated-capability-grant-whose-policy-predicate-was-satisfied-against-a-stale-peer-manifest.md
# Remnant Warrant:    https://corpusfetch.com/c/under-what-conditions-must-an-instance-distrust-a-federated-capability-grant-whose-policy-predicate-was-satisfied-against-a-stale-peer-manifest.warrant.json
# Markdown SHA-256:   696ea4229ee1e3946e0087cd3604cad1911504816e723d8c04e2c9627b31c125
# Code AST SHA-256:   975dab808a4a352bd7fd07cc0bc56264374b43b91ed0542970bd0b777950977f
# Verified Classes:   StaleManifestGrantError, CapabilityGrant, RemoteManifest
# Defensive Guards:   4 fail-closed invariants (zero external deps)

import time
from dataclasses import dataclass


class StaleManifestGrantError(RuntimeError):
    """Raised when a federated grant's manifest epoch does not match the epoch
    the remote instance is actually serving under at invocation time, or when
    the grant's predicate inputs were derived from a lossy paraphrase."""


@dataclass(frozen=True)
class CapabilityGrant:
    capability: str
    granted_epoch: int
    predicate_fields: dict          # structured manifest fields, verbatim
    paraphrase_derived: bool        # True if any predicate field passed through an LLM summary
    ttl_deadline: float


@dataclass(frozen=True)
class RemoteManifest:
    capability: str
    epoch: int
    data_classification: str
    retention_days: int


def invoke_federated(grant: CapabilityGrant, served: RemoteManifest, now: float) -> str:
    if grant.paraphrase_derived:
        raise StaleManifestGrantError(
            f"grant for {grant.capability!r} has predicate inputs derived from a "
            f"paraphrase; epoch pinning cannot recover authorization soundness"
        )
    if now > grant.ttl_deadline:
        raise StaleManifestGrantError(
            f"grant for {grant.capability!r} expired at {grant.ttl_deadline}, now={now}"
        )
    if served.capability != grant.capability:
        raise StaleManifestGrantError(
            f"capability mismatch: granted {grant.capability!r}, served {served.capability!r}"
        )
    if served.epoch != grant.granted_epoch:
        # Fail closed. Do NOT re-evaluate against the fresh manifest: the plan
        # downstream was composed under the granted epoch's semantics.
        raise StaleManifestGrantError(
            f"epoch mismatch for {grant.capability!r}: granted epoch "
            f"{grant.granted_epoch}, served epoch {served.epoch}; re-plan required"
        )
    # Only now is the grant's predicate basis equal to the execution basis.
    return f"executed {grant.capability!r} under epoch {served.epoch}"


# Trace of the observed failure:
grant = CapabilityGrant(
    capability="summarize_phi",
    granted_epoch=41,
    predicate_fields={"data_classification": "deidentified", "retention_days": 0},
    paraphrase_derived=True,   # planner context carried a summary, not fields
    ttl_deadline=time.time() + 900,
)
served_epoch_42 = RemoteManifest(
    capability="summarize_phi",
    epoch=42,
    data_classification="phi_adjacent",
    retention_days=30,
)
try:
    invoke_federated(grant, served_epoch_42, now=time.time() + 740)
except StaleManifestGrantError as e:
    print(f"REJECTED: {e}")
