# CorpusFetch Method Slice (Zero-Prose Python Stdlib Reference)
# Canonical Residual: https://corpusfetch.com/c/how-does-an-instance-detect-a-hardware-attested-identity-that-has-silently-outlived-its-workload-the-tpm-bound-credential-staleness-boundary-in-aaif-a.md
# Remnant Warrant:    https://corpusfetch.com/c/how-does-an-instance-detect-a-hardware-attested-identity-that-has-silently-outlived-its-workload-the-tpm-bound-credential-staleness-boundary-in-aaif-a.warrant.json
# Markdown SHA-256:   b4812643d9ea604445ff727654277693a7d6ae78d468178ce4319de924709afb
# Code AST SHA-256:   5161f8365a96446b30a1442a672d5c13f2635616690a7e06cd973dd512aeb4d9
# Verified Classes:   AttestationBindingError
# Defensive Guards:   2 fail-closed invariants (zero external deps)

class AttestationBindingError(RuntimeError):
    """Raised when a hardware quote is validly signed but not bound to the warranted workload."""
    pass


def verify_workload_attestation(quote, warrant, tpm_proxy) -> None:
    # Step 3: recompute expected PCR from the warrant's extend log,
    # never trust a cached snapshot.
    expected_pcr = fold_extend(warrant.extend_log)
    if quote.pcr10 != expected_pcr:
        raise AttestationBindingError(
            f"PCR divergence: quote={quote.pcr10.hex()} "
            f"expected={expected_pcr.hex()} ak_id={warrant.ak_id}"
        )

    # Step 4: the key handle must have been created under a policy
    # bound to the warranted PCR value. A node-scoped handle that
    # survived a reschedule will fail this check.
    policy_digest = tpm_proxy.policy_get_digest(warrant.ak_id)
    if policy_digest != expected_pcr:
        raise AttestationBindingError(
            f"AK {warrant.ak_id} handle policy {policy_digest.hex()} "
            f"does not bind warranted workload {expected_pcr.hex()}; "
            f"handle likely inherited across reschedule"
        )

    # Only now is the quote evidence of workload identity.
    return None
