# CorpusFetch Method Slice (Zero-Prose Python Stdlib Reference)
# Canonical Residual: https://corpusfetch.com/c/which-invariants-prevent-delegation-chain-identity-collapse-when-aaif-attestation-is-co-signed-across-trust-domains.md
# Remnant Warrant:    https://corpusfetch.com/c/which-invariants-prevent-delegation-chain-identity-collapse-when-aaif-attestation-is-co-signed-across-trust-domains.warrant.json
# Markdown SHA-256:   a8a8fd8def57f0e02586f9c0c2ef95fd92b535efc2c18e751a599bd784ad2e73
# Code AST SHA-256:   bd825cdc1d9c08bbbbcd1d590b9c62fa24ee3b39916d7ea2d754756268e71ca7
# Verified Classes:   PrincipalReanchoredError
# Defensive Guards:   3 fail-closed invariants (zero external deps)

class PrincipalReanchoredError(RuntimeError):
    """Raised when a verificationMethod id re-resolves to different key bytes
    than were attested at delegation-chain construction time. Fail-closed:
    the PCT is invalidated, not refreshed."""
    pass


def verify_pct_against_chain(pct, chain, resolver, attested_bindings):
    # attested_bindings: {(did, vm_id): key_thumbprint} captured at construction
    for link in chain.links:
        did, vm_id = link.issuer, link.verification_method_id
        live_thumb = resolver.resolve_thumbprint(did, vm_id)
        attested_thumb = attested_bindings.get((did, vm_id))
        if attested_thumb is None:
            raise PrincipalReanchoredError(
                f"no construction-time attestation for {did}#{vm_id}"
            )
        if live_thumb != attested_thumb:
            raise PrincipalReanchoredError(
                f"principal re-anchored: {did}#{vm_id} resolved to "
                f"{live_thumb!r}, attested {attested_thumb!r}"
            )
    if resolver.version_vector() != chain.resolver_version_at_construction:
        # resolver advanced: re-validate from root, never leaf-only
        return verify_pct_against_chain(
            pct, chain, resolver, attested_bindings
        )
    root_principal = chain.links[0].subject
    if pct.cnf_principal != root_principal:
        raise PrincipalReanchoredError(
            f"PCT bound to {pct.cnf_principal!r}, chain root is {root_principal!r}"
        )
    return True
