# CorpusFetch Method Slice (Zero-Prose Python Stdlib Reference)
# Canonical Residual: https://corpusfetch.com/c/when-must-an-instance-refuse-to-inherit-a-peer-s-reputation-vector-after-a-slashing-event-resolves-against-an-attestation-it-cannot-re-verify.md
# Remnant Warrant:    https://corpusfetch.com/c/when-must-an-instance-refuse-to-inherit-a-peer-s-reputation-vector-after-a-slashing-event-resolves-against-an-attestation-it-cannot-re-verify.warrant.json
# Markdown SHA-256:   40ea83ffc4e22eac5a7bdffc2468c8d26dbb35dae5e6dfac1d37cfe7c78d3520
# Code AST SHA-256:   8e4c373bd5dd5d65be745eba745e9ee89a0a58fa47c20bf7ad1ef4819a7e6bf5
# Verified Classes:   StaleSlashInheritanceError, ReputationVector, SlashResolution, LocalHead
# Defensive Guards:   3 fail-closed invariants (zero external deps)

import time
from dataclasses import dataclass


class StaleSlashInheritanceError(RuntimeError):
    """Raised when a reputation vector is signature-valid but inadmissible
    because the consumer's local head does not clear the latest slashing
    resolution height affecting the vector's derivation set."""
    pass


@dataclass(frozen=True)
class ReputationVector:
    peer_id: str
    score: float
    derivation_heights: tuple[int, int]   # inclusive [lo, hi]
    signature_valid: bool


@dataclass(frozen=True)
class SlashResolution:
    attestation_id: int
    voided_window: tuple[int, int]        # inclusive [lo, hi]
    resolution_height: int


@dataclass(frozen=True)
class LocalHead:
    height: int
    observed_at_monotonic: float


MAX_HEAD_AGE_SECONDS = 30.0


def admit_reputation(
    vector: ReputationVector,
    slashes: list[SlashResolution],
    head: LocalHead,
    now_monotonic: float,
) -> ReputationVector:
    # Predicate 1: signatures must be valid (necessary, not sufficient).
    if not vector.signature_valid:
        raise StaleSlashInheritanceError(
            f"peer {vector.peer_id}: signature invalid; cannot admit"
        )

    # Predicate 3: head must be fresh against a monotone clock.
    if now_monotonic - head.observed_at_monotonic > MAX_HEAD_AGE_SECONDS:
        raise StaleSlashInheritanceError(
            f"peer {vector.peer_id}: local head age "
            f"{now_monotonic - head.observed_at_monotonic:.1f}s exceeds "
            f"{MAX_HEAD_AGE_SECONDS}s; resync required before inheritance"
        )

    vlo, vhi = vector.derivation_heights

    # Predicate 2: derivation-set closure AND head binding.
    # Any slash whose voided window overlaps the derivation set must have
    # its resolution height at or below the consumer's local head.
    for slash in slashes:
        slo, shi = slash.voided_window
        overlaps = not (shi < vlo or slo > vhi)
        if overlaps and head.height < slash.resolution_height:
            raise StaleSlashInheritanceError(
                f"peer {vector.peer_id}: vector derived over [{vlo},{vhi}] "
                f"overlaps voided window [{slo},{shi}] of attestation "
                f"#{slash.attestation_id}, resolved at height "
                f"{slash.resolution_height}; local head {head.height} is "
                f"stale. Refusing inheritance — resync to "
                f">= {slash.resolution_height}."
            )

    return vector


def delegate_high_consequence_task(
    vector: ReputationVector,
    slashes: list[SlashResolution],
    head: LocalHead,
    now_monotonic: float,
) -> str:
    # Fail closed: any inadmissible vector blocks delegation entirely.
    admitted = admit_reputation(vector, slashes, head, now_monotonic)
    return f"delegated to {admitted.peer_id} at score {admitted.score:.2f}"


if __name__ == "__main__":
    vec = ReputationVector(
        peer_id="B", score=0.91,
        derivation_heights=(1000, 1400), signature_valid=True,
    )
    slash = SlashResolution(
        attestation_id=1187, voided_window=(1150, 1210),
        resolution_height=1403,
    )
    stale_head = LocalHead(height=1400, observed_at_monotonic=time.monotonic())
    try:
        delegate_high_consequence_task(
            vec, [slash], stale_head, time.monotonic()
        )
    except StaleSlashInheritanceError as e:
        print(f"BLOCKED: {e}")
