# CorpusFetch Method Slice (Zero-Prose Python Stdlib Reference)
# Canonical Residual: https://corpusfetch.com/c/what-happens-when-an-instance-re-signs-a-delegated-capability-token-after-its-own-scope-has-been-narrowed-mid-chain.md
# Remnant Warrant:    https://corpusfetch.com/c/what-happens-when-an-instance-re-signs-a-delegated-capability-token-after-its-own-scope-has-been-narrowed-mid-chain.warrant.json
# Markdown SHA-256:   002b60b723a01c176dff15887e38bdd33baa5b7c47f8f8f4e1a910e92ea66812
# Code AST SHA-256:   db408bac29c6b9a31ed0a160dedc20b74c23f1503971dbd4ac421dd40b088392
# Verified Classes:   DelegationLaunderingError
# Defensive Guards:   7 fail-closed invariants (zero external deps)

class DelegationLaunderingError(RuntimeError):
    """Raised when a re-signed capability token claims authority the
    re-signer does not currently hold, or when a verifier cannot
    reconstruct the re-signer's live effective scope."""


def mint_delegated_token(
    parent_token,
    resigner_subject: str,
    requested_scopes: frozenset,
    runtime_authz,
    revocation_set,
    liveness,
    now: float,
    max_ttl: float = 60.0,
) -> dict:
    # Refuse to mint if we cannot compute the re-signer's live scope.
    if not liveness.is_live(resigner_subject, at=now):
        raise DelegationLaunderingError(
            f"re-signer {resigner_subject} is not live at {now}; refusing mint"
        )
    if not runtime_authz.has_fresh_view(resigner_subject, at=now):
        raise DelegationLaunderingError(
            f"re-signer {resigner_subject} has no synchronous authorization view; "
            "fallback-to-claims is forbidden"
        )

    # Effective scope is the intersection, NOT the token claims.
    effective = (
        parent_token.scopes
        & runtime_authz.scopes_for(resigner_subject, at=now)
        & ~revocation_set.revoked_scopes(resigner_subject)
    )

    if not requested_scopes <= effective:
        leaked = requested_scopes - effective
        raise DelegationLaunderingError(
            f"requested scopes {sorted(leaked)} exceed live effective scope of "
            f"{resigner_subject}; parent claims are not authority"
        )

    exp = min(parent_token.exp, now + max_ttl)
    return {
        "issuer": resigner_subject,
        "parent_id": parent_token.id,
        "ancestors": parent_token.ancestors + [parent_token.id],
        "scopes": frozenset(requested_scopes),
        "exp": exp,
        "minted_at": now,
        "revocation_watermark": revocation_set.watermark(resigner_subject),
    }


def verify_delegated_token(token, chain_resolver, revocation_set, liveness, now: float) -> None:
    # Walk every ancestor: per-hop revocation and per-hop liveness.
    for ancestor_id in token["ancestors"]:
        ancestor = chain_resolver.get(ancestor_id)
        if ancestor is None:
            raise DelegationLaunderingError(
                f"unresolvable ancestor {ancestor_id}; deny by default"
            )
        if not liveness.is_live(ancestor["issuer"], at=now):
            raise DelegationLaunderingError(
                f"ancestor issuer {ancestor['issuer']} not live; chain is dead"
            )
        revoked = revocation_set.revoked_scopes(ancestor["issuer"])
        if token["scopes"] & revoked:
            raise DelegationLaunderingError(
                f"scope laundered past revocation of {ancestor['issuer']}: "
                f"{sorted(token['scopes'] & revoked)}"
            )
    if token["exp"] <= now:
        raise DelegationLaunderingError("token expired")
