# CorpusFetch Method Slice (Zero-Prose Python Stdlib Reference)
# Canonical Residual: https://corpusfetch.com/c/under-what-conditions-must-an-instance-refuse-to-attenuate-a-delegated-macaroon-capability-token-when-the-requested-caveat-is-unverifiable-at-mint-tim.md
# Remnant Warrant:    https://corpusfetch.com/c/under-what-conditions-must-an-instance-refuse-to-attenuate-a-delegated-macaroon-capability-token-when-the-requested-caveat-is-unverifiable-at-mint-tim.warrant.json
# Markdown SHA-256:   d15427c378b12392462b41e98c0acadfc15e086375d16332279943990e02a8fb
# Code AST SHA-256:   1025edbfa111851a7f2d852fd226aa6f0eae882c867e35d42e98783e0ff9012f
# Verified Classes:   UnverifiableCaveatError
# Defensive Guards:   3 fail-closed invariants (zero external deps)

class UnverifiableCaveatError(RuntimeError):
    """Raised when a mid-chain minter attempts to bind a caveat whose
    discharge authority is outside its trust domain and no signed
    discharge-vocabulary manifest covers the predicate."""
    pass


def attenuate(macaroon, caveat_key, caveat_value, manifest, minter_domain):
    """
    Append a caveat to a delegated macaroon, but only if the minter can
    ground the binding in either local discharge or a signed manifest
    from the eventual verifier.

    manifest: dict mapping predicate_key -> (verifier_domain, value_grammar)
              as published in the handoff's capability negotiation.
    """
    if not manifest:
        raise UnverifiableCaveatError(
            f"minter {minter_domain!r} has no discharge-vocabulary manifest; "
            f"cannot bind caveat {caveat_key}={caveat_value!r}"
        )

    entry = manifest.get(caveat_key)
    if entry is None:
        raise UnverifiableCaveatError(
            f"caveat key {caveat_key!r} absent from verifier manifest; "
            f"binding it would produce a token no downstream verifier "
            f"can discharge (or one a lenient verifier silently ignores)"
        )

    verifier_domain, grammar = entry
    if not grammar.accepts(caveat_value):
        raise UnverifiableCaveatError(
            f"value {caveat_value!r} violates verifier grammar for "
            f"{caveat_key!r} published by {verifier_domain!r}"
        )

    # Only now is the attenuation grounded in a signature rather than a prior.
    return macaroon.add_first_party_caveat(f"{caveat_key}={caveat_value}")
