# CorpusFetch Method Slice (Zero-Prose Python Stdlib Reference)
# Canonical Residual: https://corpusfetch.com/c/when-must-an-instance-re-derive-a-delegated-capability-grant-after-an-audience-mismatch-and-why-proof-of-possession-alone-does-not-save-you.md
# Remnant Warrant:    https://corpusfetch.com/c/when-must-an-instance-re-derive-a-delegated-capability-grant-after-an-audience-mismatch-and-why-proof-of-possession-alone-does-not-save-you.warrant.json
# Markdown SHA-256:   08db8d8a35e6b086928349381dfa7892f35ae18bbf71d13805f9e0bae070881c
# Code AST SHA-256:   db107dda919e90303ac47b6a85d84b10063b3ea1799a6682cb1d40471c1ba94e
# Verified Classes:   DelegationBindingError
# Defensive Guards:   5 fail-closed invariants (zero external deps)

class DelegationBindingError(RuntimeError):
    """Raised when a delegated capability token's (sub, aud, cnf) triple is
    internally inconsistent, or when a partial rewrite was attempted."""
    pass


def issue_delegated_token(
    parent_token: dict,
    new_holder_id: str,
    new_holder_pubkey: str,
    target_audience: str,
    narrowed_scope: list[str],
    delegator_can_mint: bool,
) -> dict:
    """
    Full re-issuance of a capability grant for a new holder.
    Refuses to attenuate a parent token in place.
    """
    # Guard 1: a handoff to a distinct agent is never an attenuation.
    if new_holder_id == parent_token.get("sub"):
        # Same agent, same key, same resource -> attenuation is legal.
        if new_holder_pubkey != parent_token.get("cnf"):
            raise DelegationBindingError(
                "same sub but different cnf: key rotation mid-flight is not "
                "a valid attenuation; re-issue required"
            )
        if target_audience != parent_token.get("aud"):
            raise DelegationBindingError(
                "same sub but different aud: cannot retarget audience by "
                "attenuation; re-issue required"
            )
        return {**parent_token, "scope": narrowed_scope}

    # Guard 2: handoff to a new holder requires minting authority.
    if not delegator_can_mint:
        raise DelegationBindingError(
            f"delegator lacks minting authority to re-issue for {new_holder_id}; "
            f"must proxy the call instead of forwarding a mutated token"
        )

    # Guard 3: atomic triple. Never copy any of sub/aud/cnf from parent.
    child = {
        "sub": new_holder_id,
        "aud": target_audience,
        "cnf": new_holder_pubkey,
        "scope": narrowed_scope,
        "parent": parent_token.get("jti"),
    }

    # Guard 4: post-construction consistency assertion.
    if not (child["sub"] and child["aud"] and child["cnf"]):
        raise DelegationBindingError("incomplete binding triple in child token")
    if child["cnf"] == parent_token.get("cnf") and child["sub"] != parent_token.get("sub"):
        raise DelegationBindingError(
            "child reuses parent cnf with a new sub: attribution desync"
        )
    return child
