# CorpusFetch Method Slice (Zero-Prose Python Stdlib Reference)
# Canonical Residual: https://corpusfetch.com/c/in-what-scenarios-must-an-instance-reject-a-delegated-macaroon-capability-chain-when-attenuation-appears-valid-but-the-discharge-witness-was-minted-ag.md
# Remnant Warrant:    https://corpusfetch.com/c/in-what-scenarios-must-an-instance-reject-a-delegated-macaroon-capability-chain-when-attenuation-appears-valid-but-the-discharge-witness-was-minted-ag.warrant.json
# Markdown SHA-256:   c0ba3040a2ce5c02c14c02cc64e11e61a97afa9f214bb24cf738e2112e9cea1f
# Code AST SHA-256:   0ce6ebd5d6264b715e81a508b6c98fc60d01f0cd34847ac6be6dbcdc13770a53
# Verified Classes:   StaleRootWitnessError, Witness, Chain
# Defensive Guards:   4 fail-closed invariants (zero external deps)

class StaleRootWitnessError(RuntimeError):
    """Raised when a discharge witness's mint generation does not match the
    presented chain's root generation, regardless of HMAC validity."""
    pass


class Witness:
    def __init__(self, authority: str, mint_root_generation: int,
                 hmac_valid: bool, revoked: bool):
        self.authority = authority
        self.mint_root_generation = mint_root_generation
        self.hmac_valid = hmac_valid
        self.revoked = revoked


class Chain:
    def __init__(self, root_generation: int, caveats: list[str]):
        self.root_generation = root_generation
        self.caveats = caveats


def verify_discharge(chain: Chain, witness: Witness,
                     authority_current_generation: int) -> None:
    # 1. Generation equality against the CHAIN's root, not the verifier's current.
    #    A witness minted at a different generation than the chain is stale or
    #    replayed and must be rejected before HMAC is even consulted.
    if witness.mint_root_generation != chain.root_generation:
        raise StaleRootWitnessError(
            f"witness minted at g{witness.mint_root_generation} "
            f"for chain rooted at g{chain.root_generation}; "
            f"authority {witness.authority} now at g{authority_current_generation}"
        )

    # 2. Revocation is checked at verification time, not mint time.
    if witness.revoked:
        raise StaleRootWitnessError(
            f"witness from {witness.authority} is revoked as of verification"
        )

    # 3. Only now is HMAC validity meaningful.
    if not witness.hmac_valid:
        raise StaleRootWitnessError(
            f"witness HMAC invalid under authority {witness.authority}"
        )

    # 4. The chain's own root generation must still be authorized for the resource.
    #    If the authority has advanced past the chain's generation and the resource
    #    requires current-generation authority, the chain is stale even with a
    #    contemporaneous witness.
    if chain.root_generation < authority_current_generation:
        # Grace-window completion is permitted only if the resource policy
        # accepts pre-rotation chains; otherwise reject.
        raise StaleRootWitnessError(
            f"chain rooted at g{chain.root_generation} predates authority "
            f"current g{authority_current_generation}; resource requires current"
        )
