# CorpusFetch Method Slice (Zero-Prose Python Stdlib Reference)
# Canonical Residual: https://corpusfetch.com/c/what-structural-signals-indicate-a-federated-trust-broker-has-silently-downgraded-a-peer-s-capability-grant-mid-handoff.md
# Remnant Warrant:    https://corpusfetch.com/c/what-structural-signals-indicate-a-federated-trust-broker-has-silently-downgraded-a-peer-s-capability-grant-mid-handoff.warrant.json
# Markdown SHA-256:   3c736ac434d6af12e40a7802fae69408ba8d59a520e92bc6183452b84215ae48
# Code AST SHA-256:   7b0526e1f2ce8e6e83666c6bf1923b52c693c22d3b6f1cf0f636fa2d5b626eda
# Verified Classes:   SilentAttenuationError, Grant
# Defensive Guards:   2 fail-closed invariants (zero external deps)

class SilentAttenuationError(RuntimeError):
    """Raised when a presented grant is narrower than the plan's scope-closure,
    meaning the broker attenuated without the delegate detecting it in time."""
    pass


class Grant:
    def __init__(self, scopes, attenuated, issuer):
        self.scopes = frozenset(scopes)
        self.attenuated = attenuated
        self.issuer = issuer


def verify_grant_adequacy(grant: Grant, plan_scope_closure: frozenset) -> None:
    """Enforce the invariant: scope_closure(plan) subset-of scopes(grant),
    checked BEFORE the first side-effecting step. Attenuation itself is legal;
    proceeding under an inadequate grant is not."""
    if not plan_scope_closure.issubset(grant.scopes):
        missing = plan_scope_closure - grant.scopes
        raise SilentAttenuationError(
            f"grant from {grant.issuer} lacks scopes {sorted(missing)} required by "
            f"plan scope-closure; attenuated={grant.attenuated}. "
            f"Renegotiate or abort before committing side effects."
        )


def execute_plan(grant: Grant, plan_scope_closure: frozenset, steps):
    # Hard gate: no side effect may run until adequacy is proven.
    verify_grant_adequacy(grant, plan_scope_closure)
    for step in steps:
        # Weak-form guard for dynamically generated steps:
        if not frozenset(step.required_scopes).issubset(grant.scopes):
            raise SilentAttenuationError(
                f"step {step.name} needs {step.required_scopes} absent from grant; "
                f"halting before mutation to avoid orphaned cross-domain state."
            )
        step.run()
