# CorpusFetch Method Slice (Zero-Prose Python Stdlib Reference)
# Canonical Residual: https://corpusfetch.com/c/why-does-an-instance-of-a-litmus-safety-benchmark-harness-silently-pass-adversarial-multi-turn-probes-when-the-conversation-history-is-compacted-betwe.md
# Remnant Warrant:    https://corpusfetch.com/c/why-does-an-instance-of-a-litmus-safety-benchmark-harness-silently-pass-adversarial-multi-turn-probes-when-the-conversation-history-is-compacted-betwe.warrant.json
# Markdown SHA-256:   85aef2fc10d4d07057922d8ef9b6f8afdb8b1269ff19966b7b3a760467561011
# Code AST SHA-256:   44178f85b199ff5c98377d101acaf666f627a4486616eebb5430b1db7902b5bd
# Verified Classes:   CompactionSufficiencyError
# Defensive Guards:   3 fail-closed invariants (zero external deps)

class CompactionSufficiencyError(RuntimeError):
    """Raised when a LITMUS judging context is not a sufficient statistic
    for the safety verdict of the current turn."""


def judge_turn(
    turn_index: int,
    target_context: list[str],
    judge_context: list[str],
    rubric_is_trajectory_relative: bool,
    compaction_summary_provenance: str | None,
) -> str:
    # Invariant 1: target and judge must see the same transcript projection.
    if target_context != judge_context:
        raise CompactionSufficiencyError(
            f"turn {turn_index}: target/judge context divergence "
            f"({len(target_context)} vs {len(judge_context)} segments); "
            "verdict would not be attributable to the target"
        )

    # Invariant 2: trajectory-relative rubrics require full escalation scaffolding.
    if rubric_is_trajectory_relative and compaction_summary_provenance is not None:
        raise CompactionSufficiencyError(
            f"turn {turn_index}: rubric references prior-turn state but judge "
            f"consumed derived summary ({compaction_summary_provenance!r}); "
            "compaction is not a sufficient statistic for this verdict"
        )

    # Invariant 3: summaries are untrusted derived state, never ground truth.
    if compaction_summary_provenance is not None and not compaction_summary_provenance.startswith("raw:"):
        raise CompactionSufficiencyError(
            f"turn {turn_index}: summary lacks raw-transcript provenance "
            f"({compaction_summary_provenance!r}); treating as untrusted"
        )

    return "scored"


def run_litmus_trajectory(trajectory, rubric):
    verdicts = []
    for turn in trajectory:
        try:
            verdicts.append(
                judge_turn(
                    turn_index=turn.index,
                    target_context=turn.target_context,
                    judge_context=turn.judge_context,
                    rubric_is_trajectory_relative=rubric.is_trajectory_relative(turn.index),
                    compaction_summary_provenance=turn.summary_provenance,
                )
            )
        except CompactionSufficiencyError as exc:
            # Fail closed: unscored, never passed.
            verdicts.append(f"UNSCORED: {exc}")
    return verdicts
