# CorpusFetch Method Slice (Zero-Prose Python Stdlib Reference)
# Canonical Residual: https://corpusfetch.com/c/under-what-conditions-must-an-instance-treat-a-granted-tool-capability-as-revoked-mid-execution-and-how-does-the-revocation-handshake-fail-across-mcp.md
# Remnant Warrant:    https://corpusfetch.com/c/under-what-conditions-must-an-instance-treat-a-granted-tool-capability-as-revoked-mid-execution-and-how-does-the-revocation-handshake-fail-across-mcp.warrant.json
# Markdown SHA-256:   a962fa74301f3fc69a5cc025cf23d587e7fdf68919d77f3a98f1b967b45839d0
# Code AST SHA-256:   fb3fae56253106204b3c1e33af14d22b9da3acf85a0d7fee8d933f9e6f9dfdad
# Verified Classes:   EffectClass, CapabilityRevocationError, ToolSpec, PlanStep, CapabilityBarrier
# Defensive Guards:   2 fail-closed invariants (zero external deps)

from dataclasses import dataclass, field
from enum import Enum


class EffectClass(Enum):
    READ = "read"
    WRITE_DURABLE = "write_durable"
    EGRESS = "egress"
    MUTATE = "mutate"


class CapabilityRevocationError(RuntimeError):
    """Raised when a plan step's effect class has been revoked and the agent
    attempts to route the same effect through an alternate capability."""


@dataclass
class ToolSpec:
    name: str
    effects: frozenset


@dataclass
class PlanStep:
    tool: str
    effect: EffectClass


@dataclass
class CapabilityBarrier:
    revoked_effects: set = field(default_factory=set)
    quarantined_tools: set = field(default_factory=set)

    def on_authorization_error(self, tool: str, effect: EffectClass,
                               catalog: dict) -> None:
        # Authorization errors are non-retryable and plan-invalidating.
        self.revoked_effects.add(effect)
        # Quarantine every tool that can carry the revoked effect,
        # not just the tool that failed.
        for name, spec in catalog.items():
            if effect.value in spec.effects:
                self.quarantined_tools.add(name)

    def authorize_step(self, step: PlanStep) -> None:
        if step.effect in self.revoked_effects:
            if step.tool in self.quarantined_tools:
                raise CapabilityRevocationError(
                    f"step '{step.tool}' carries revoked effect "
                    f"'{step.effect.value}' via capability substitution; "
                    f"plan must be amended by orchestrator"
                )
            raise CapabilityRevocationError(
                f"effect '{step.effect.value}' revoked; step '{step.tool}' "
                f"cannot proceed without explicit re-grant"
            )


def run_plan(plan: list, barrier: CapabilityBarrier,
             catalog: dict) -> None:
    for step in plan:
        barrier.authorize_step(step)
        # dispatch to MCP server only after the barrier passes
        _dispatch(step)


def _dispatch(step: PlanStep) -> None:
    pass
