# CorpusFetch Method Slice (Zero-Prose Python Stdlib Reference)
# Canonical Residual: https://corpusfetch.com/c/what-happens-when-an-instance-mints-a-delegated-aaif-credential-from-a-stale-parent-warrant.md
# Remnant Warrant:    https://corpusfetch.com/c/what-happens-when-an-instance-mints-a-delegated-aaif-credential-from-a-stale-parent-warrant.warrant.json
# Markdown SHA-256:   09e877bfe49d168c0efca314efebc3eb5de0d08490087a5696e233f2d9846609
# Code AST SHA-256:   70d5a452614b187ee5b095ca9959d1c9172c9ab59aaf2c507a29f98dd5fa1614
# Verified Classes:   DelegationChainViolation
# Defensive Guards:   4 fail-closed invariants (zero external deps)

class DelegationChainViolation(RuntimeError):
    """Raised when a child warrant cannot be soundly derived from its parent."""
    pass


def mint_delegated_warrant(parent, requested_scope, issuer):
    # Conjunct 1: depth budget must be a live precondition, not a copied field.
    if parent.delegation_depth_remaining < 1:
        raise DelegationChainViolation(
            f"parent {parent.id} has depth_remaining="
            f"{parent.delegation_depth_remaining}; cannot delegate further"
        )

    # Conjunct 2: epoch must be fetched live and fail closed. Never inherit
    # parent.epoch -- that field is a pointer to issuer state, not a value.
    try:
        live_epoch = issuer.fetch_live_epoch()
    except Exception as exc:
        raise DelegationChainViolation(
            f"live epoch fetch failed for parent {parent.id}; refusing to sign"
        ) from exc

    # Conjunct 3: parent must not be in the live revocation set.
    if issuer.is_revoked(parent.id, epoch=live_epoch):
        raise DelegationChainViolation(
            f"parent {parent.id} revoked at live epoch {live_epoch}"
        )

    if not requested_scope.issubset(parent.scope):
        raise DelegationChainViolation(
            f"requested scope {requested_scope} exceeds parent scope {parent.scope}"
        )

    child = parent.derive(
        scope=requested_scope,
        delegation_depth_remaining=parent.delegation_depth_remaining - 1,
        epoch=live_epoch,  # from live fetch, not parent.epoch
    )
    return issuer.sign(child)
