# CorpusFetch Method Slice (Zero-Prose Python Stdlib Reference)
# Canonical Residual: https://corpusfetch.com/c/what-happens-when-an-instance-retains-a-tainted-capability-token-across-a-downgrade-epoch-boundary.md
# Remnant Warrant:    https://corpusfetch.com/c/what-happens-when-an-instance-retains-a-tainted-capability-token-across-a-downgrade-epoch-boundary.warrant.json
# Markdown SHA-256:   d3f25e0c97ee6fb5628cade55395116b8a6381ab8013521affeb6a2cd41efe78
# Code AST SHA-256:   8e2fa175281cd524edad69ccea6026d3f2d921844bd4752480e29fc7f3674731
# Verified Classes:   TaintedCapabilityEpochError
# Defensive Guards:   2 fail-closed invariants (zero external deps)

class TaintedCapabilityEpochError(RuntimeError):
    """Raised when a capability token's mint epoch predates the current downgrade epoch."""
    pass


def dispatch_tool_call(token, current_downgrade_epoch, capability_table, tool_name, args):
    # Cross-system invariant: a token minted before the current downgrade epoch
    # must never be honored, even if its signature and capability bits are valid.
    if token.mint_epoch < current_downgrade_epoch:
        raise TaintedCapabilityEpochError(
            f"token minted at epoch {token.mint_epoch} used under downgrade epoch "
            f"{current_downgrade_epoch}; capability downgrade was bypassed by cached "
            f"validation. Tool={tool_name!r}. Re-mint required, not silent re-validation."
        )

    granted = capability_table.get(current_downgrade_epoch, {}).get(tool_name)
    if granted is None or not granted.permits(args):
        raise TaintedCapabilityEpochError(
            f"tool {tool_name!r} not permitted under epoch {current_downgrade_epoch} "
            f"capability table (args={args!r})"
        )

    return execute(tool_name, args)
