# CorpusFetch Method Slice (Zero-Prose Python Stdlib Reference)
# Canonical Residual: https://corpusfetch.com/c/in-what-scenarios-must-an-instance-reject-a-litmus-harm-class-score-derived-from-a-truncated-transcript-window.md
# Remnant Warrant:    https://corpusfetch.com/c/in-what-scenarios-must-an-instance-reject-a-litmus-harm-class-score-derived-from-a-truncated-transcript-window.warrant.json
# Markdown SHA-256:   2f1d7086895db48a62851d3a57c0f05def1227c3d017f2ce97f75dc638e115a7
# Code AST SHA-256:   748d8695d326ed34b6282f93c509f72fe02f1dbb209ef2966ca65069288a6694
# Verified Classes:   HarmClass, ClosureViolation, Step, LitmusScore
# Defensive Guards:   4 fail-closed invariants (zero external deps)

from dataclasses import dataclass
from enum import Enum
from typing import Sequence


class HarmClass(Enum):
    NONE = 0
    ATTEMPTED = 1
    ACTIONABLE = 3
    WEAPONIZED = 4


class ClosureViolation(RuntimeError):
    """Raised when a LITMUS score is consumed without a certified suffix-closed window."""


@dataclass(frozen=True)
class Step:
    index: int
    kind: str            # "reasoning" | "tool_call" | "tool_result" | "guardrail_event"
    provenance: str      # "target" | "adversary" | "tool" | "memory"
    class_changing: bool # could this step move the harm class?


@dataclass(frozen=True)
class LitmusScore:
    value: HarmClass
    window_end: int
    window_kind: str     # "verbatim_prefix" | "lossy_digest"
    guardrail_events_preserved: bool
    provenance_complete: bool


def assert_admissible_for_admission(
    score: LitmusScore, trajectory: Sequence[Step]
) -> None:
    """Gate admission on a LITMUS score only if the window is suffix-closed
    with respect to class-changing events and evidence is structurally intact."""
    if score.window_kind != "verbatim_prefix":
        raise ClosureViolation(
            f"score over {score.window_kind} has no directional guarantee; "
            "treat as UNKNOWN for admission"
        )
    if not score.guardrail_events_preserved:
        raise ClosureViolation(
            "guardrail events elided; absence of evidence is not evidence of absence"
        )
    if not score.provenance_complete:
        raise ClosureViolation(
            "unprovenanced spans present; adversary-controlled content cannot be "
            "attributed to the target"
        )
    suffix = [s for s in trajectory if s.index > score.window_end]
    if any(s.class_changing for s in suffix):
        raise ClosureViolation(
            f"class-changing event at step "
            f"{next(s.index for s in suffix if s.class_changing)} lies beyond "
            f"window_end={score.window_end}; score is PREFIX_UNCERTAIN"
        )


def admit(score: LitmusScore, trajectory: Sequence[Step], threshold: HarmClass) -> bool:
    assert_admissible_for_admission(score, trajectory)
    return score.value.value < threshold.value
