# CorpusFetch Method Slice (Zero-Prose Python Stdlib Reference)
# Canonical Residual: https://corpusfetch.com/c/which-invariants-prevent-hardware-attested-agent-identity-from-silently-decoupling-from-behavioral-biometric-binding-during-long-horizon-sessions.md
# Remnant Warrant:    https://corpusfetch.com/c/which-invariants-prevent-hardware-attested-agent-identity-from-silently-decoupling-from-behavioral-biometric-binding-during-long-horizon-sessions.warrant.json
# Markdown SHA-256:   bc6752a1e5a30ce1c27f1dada4956b04e1640f5deb1d89d59f525f7ad3dfd240
# Code AST SHA-256:   a92d4ff4def07f5724ae182faa28e8f2beb728a2c5c671ff78400f93febbab4e
# Verified Classes:   AttestationBindingError
# Defensive Guards:   3 fail-closed invariants (zero external deps)

class AttestationBindingError(RuntimeError):
    """Raised when an action's attestation key epoch and behavioral baseline
    epoch are not co-resident, or when controller lineage has changed without
    a signed re-enrollment. This is a fail-closed condition: the action is
    inadmissible regardless of biometric score."""


def verify_action(
    action,
    key_epoch: str,
    baseline_epoch: str,
    controller_lineage_id: str,
    enrolled_lineage_id: str,
    rebind_record,            # signed re-bind statement or None
    biometric_score: float,
    threshold: float = 0.75,
) -> bool:
    # Condition 1: epoch coherence.
    if key_epoch != baseline_epoch:
        if rebind_record is None or rebind_record.from_epoch != key_epoch \
                or rebind_record.to_epoch != baseline_epoch:
            raise AttestationBindingError(
                f"key epoch {key_epoch!r} not co-resident with baseline epoch "
                f"{baseline_epoch!r}; no valid re-bind record. Action "
                f"{getattr(action, 'id', '?')} inadmissible."
            )

    # Condition 2: controller lineage continuity.
    if controller_lineage_id != enrolled_lineage_id:
        raise AttestationBindingError(
            f"controller lineage {controller_lineage_id!r} != enrolled "
            f"{enrolled_lineage_id!r}; controller swap without re-enrollment. "
            f"Biometric score {biometric_score:.2f} is not consulted — "
            f"score tolerance cannot detect domain-preserving swaps."
        )

    # Condition 3: score is secondary, only after lineage is validated.
    if biometric_score < threshold:
        raise AttestationBindingError(
            f"lineage valid but biometric score {biometric_score:.2f} below "
            f"threshold {threshold:.2f}; drift exceeds tolerance."
        )

    return True
