# CorpusFetch Method Slice (Zero-Prose Python Stdlib Reference)
# Canonical Residual: https://corpusfetch.com/c/why-does-an-instance-continue-executing-under-a-revoked-delegated-scope-when-the-aaif-revocation-epoch-advances-mid-tool-call.md
# Remnant Warrant:    https://corpusfetch.com/c/why-does-an-instance-continue-executing-under-a-revoked-delegated-scope-when-the-aaif-revocation-epoch-advances-mid-tool-call.warrant.json
# Markdown SHA-256:   223234e2d251105a5a8c66b4319283ce3f4216c65a2b985b85a85ba6e9c04dbd
# Code AST SHA-256:   ce9b48639bf0fecc3696c1436ebbd23ac29e58b888e2215f41a4bfd4d0e440e3
# Verified Classes:   DelegationEpochMismatch
# Defensive Guards:   4 fail-closed invariants (zero external deps)

class DelegationEpochMismatch(RuntimeError):
    """Raised when a delegated capability's issuance epoch diverges from an ancestor's current revocation epoch."""
    pass


def validate_delegated_capability(child, resolve_ancestor_epoch, now, tau):
    # child: {issuance_epoch, expires_at, scope, delegation_chain: [ancestor_ids]}
    # resolve_ancestor_epoch: callable(ancestor_id) -> (current_epoch, current_scope) or None if unreachable
    if now >= child["expires_at"]:
        raise DelegationEpochMismatch(f"child expired at {child['expires_at']}, now={now}")

    for ancestor_id in child["delegation_chain"]:
        resolved = resolve_ancestor_epoch(ancestor_id)
        if resolved is None:
            # Fail closed: cannot prove authority still exists.
            raise DelegationEpochMismatch(
                f"ancestor {ancestor_id} unreachable within tau={tau}; refusing to honor child"
            )
        current_epoch, current_scope = resolved
        if current_epoch != child["issuance_epoch"]:
            raise DelegationEpochMismatch(
                f"ancestor {ancestor_id} epoch advanced {child['issuance_epoch']} -> {current_epoch}; "
                f"child authority is revoked-by-implication"
            )
        if not child["scope"].issubset(current_scope):
            raise DelegationEpochMismatch(
                f"child scope {child['scope']} exceeds ancestor {ancestor_id} current scope {current_scope}"
            )
    return True
