# CorpusFetch Method Slice (Zero-Prose Python Stdlib Reference)
# Canonical Residual: https://corpusfetch.com/c/where-must-an-instance-boundary-be-enforced-in-aaif-delegated-credential-chaining.md
# Remnant Warrant:    https://corpusfetch.com/c/where-must-an-instance-boundary-be-enforced-in-aaif-delegated-credential-chaining.warrant.json
# Markdown SHA-256:   01dba3c1d8cf54c686ad32bd5647eceaada62b00459c20a4d6818f8fd3cd2df5
# Code AST SHA-256:   ae5461bbdaffb7cb0bf3fe320e750acf83de106cd1bb972a131b83ba12d0c879
# Verified Classes:   InstanceBindingViolation
# Defensive Guards:   3 fail-closed invariants (zero external deps)

class InstanceBindingViolation(RuntimeError):
    """Raised when a delegated credential's instance claim does not match the presenter."""
    pass


def accept_delegated_credential(credential: dict, attested_instance_id: str) -> dict:
    """
    Enforce the AAIF instance boundary at the delegatee presentation point.

    The credential's `instance_id` claim is a constraint minted by the delegator.
    It is NOT proof of the presenter. We compare it to a fresh attestation of
    our own instance identity on every request; mismatch means replay or fork.
    """
    if not attested_instance_id:
        raise InstanceBindingViolation(
            "no attested instance identity; cannot evaluate instance binding"
        )

    claimed = credential.get("instance_id")
    if claimed is None:
        raise InstanceBindingViolation(
            "delegated credential carries no instance_id claim; "
            "treating as unbound bearer credential is not permitted in this path"
        )

    if claimed != attested_instance_id:
        raise InstanceBindingViolation(
            f"instance binding mismatch: credential claims {claimed!r}, "
            f"presenter attests {attested_instance_id!r}; "
            "rejecting to prevent replay by a forked or scaled replica"
        )

    return credential
