# CorpusFetch Method Slice (Zero-Prose Python Stdlib Reference)
# Canonical Residual: https://corpusfetch.com/c/under-what-conditions-must-an-instance-refuse-to-mint-a-delegated-credential-when-the-aaif-scope-narrowing-proof-is-satisfiable-only-under-a-stale-cap.md
# Remnant Warrant:    https://corpusfetch.com/c/under-what-conditions-must-an-instance-refuse-to-mint-a-delegated-credential-when-the-aaif-scope-narrowing-proof-is-satisfiable-only-under-a-stale-cap.warrant.json
# Markdown SHA-256:   ff9fe26ada0d6a1e27a83724a1386c064cbce45b9a876976596051b137b1befc
# Code AST SHA-256:   bf887064fddc5c5c9c25d3a4bb4eedc1b4b11acc2849494d3d80e2f713eee5c1
# Verified Classes:   DelegationPreconditionError
# Defensive Guards:   4 fail-closed invariants (zero external deps)

class DelegationPreconditionError(RuntimeError):
    """Raised when a delegated credential cannot be minted against a live parent snapshot."""
    pass


def mint_delegated_credential(
    parent_credential,
    parent_snapshot,
    child_scope,
    now,
    re_scope_channel_live,
    issuer_reported_change_since_fetch,
):
    max_age = parent_credential.max_snapshot_age
    age = now - parent_snapshot.fetched_at

    # Condition 3: revocation/re-scope reachability.
    if not re_scope_channel_live:
        raise DelegationPreconditionError(
            f"parent {parent_credential.id}: re-scope channel dead; "
            f"cannot prove snapshot {parent_snapshot.hash[:12]} is current"
        )

    # Condition 2: narrowing monotonicity against the *live* snapshot.
    if issuer_reported_change_since_fetch:
        raise DelegationPreconditionError(
            f"parent {parent_credential.id}: issuer reports snapshot change "
            f"since fetch at {parent_snapshot.fetched_at}; "
            f"proof over {parent_snapshot.hash[:12]} is not reproducible"
        )

    # Condition 1: freshness bound. Exceeded but channel live and no change
    # reported -> re-fetch is required before minting.
    if age > max_age:
        raise DelegationPreconditionError(
            f"parent {parent_credential.id}: snapshot age {age}s exceeds "
            f"max_snapshot_age {max_age}s; re-fetch required before mint"
        )

    # Scope satisfiability is necessary but NOT sufficient; we only reach here
    # when the snapshot is provably current.
    if not child_scope.issubset(parent_snapshot.scope):
        raise DelegationPreconditionError(
            f"child scope {child_scope} not a subset of live parent scope "
            f"{parent_snapshot.scope}"
        )

    return parent_credential.sign_child(
        scope=child_scope,
        parent_snapshot_hash=parent_snapshot.hash,
    )
