# CorpusFetch Method Slice (Zero-Prose Python Stdlib Reference)
# Canonical Residual: https://corpusfetch.com/c/when-must-an-instance-refuse-a-delegated-scope-it-cannot-attest-to-the-attestation-provenance-gap-in-aaif-delegation-chains.md
# Remnant Warrant:    https://corpusfetch.com/c/when-must-an-instance-refuse-a-delegated-scope-it-cannot-attest-to-the-attestation-provenance-gap-in-aaif-delegation-chains.warrant.json
# Markdown SHA-256:   861384c1a4c66f52ca818348cad9b1ae37b23b686c5ae0a06d8dfee9e70ccb5b
# Code AST SHA-256:   5f5c0ad788c0f0498655fb36ed4d1d2bef4f947fbb3fbb0cf8389fe5c2f7da94
# Verified Classes:   AttestationProvenanceError
# Defensive Guards:   3 fail-closed invariants (zero external deps)

class AttestationProvenanceError(RuntimeError):
    """Raised when a delegated token cannot be cross-bound to the presenting
    attestation, so presentation would corrupt provenance."""


def present_delegated_scope(token: dict, attestation: dict, exclusive_custody: bool) -> str:
    """Decide whether an instance may present a scoped delegation token.

    token:        {"id", "sub", "aud", "scope", "bound_measurement"|None}
    attestation:  {"measurement", "nonce", "fresh"}
    exclusive_custody: instance proves no shared credential cache / pool inheritance.
    """
    if not attestation.get("fresh"):
        raise AttestationProvenanceError(
            f"attestation for token {token['id']} is stale; cannot bind"
        )

    bound = token.get("bound_measurement")

    if bound is not None:
        if bound != attestation["measurement"]:
            # Loud refusal: a mismatch is indistinguishable from token replay.
            raise AttestationProvenanceError(
                f"token {token['id']} bound to {bound!r} but presenter measured "
                f"{attestation['measurement']!r}; refusing to present"
            )
        return "present"

    # Legacy token with no binding field.
    if not exclusive_custody:
        raise AttestationProvenanceError(
            f"token {token['id']} has no bound_measurement and instance cannot prove "
            f"exclusive custody; refusing and requesting re-mint with binding"
        )
    return "present_under_exclusive_custody"
