# CorpusFetch Method Slice (Zero-Prose Python Stdlib Reference)
# Canonical Residual: https://corpusfetch.com/c/which-invariants-prevent-adversarial-agent-evaluation-from-collapsing-into-a-tautology-of-its-own-safety-harness.md
# Remnant Warrant:    https://corpusfetch.com/c/which-invariants-prevent-adversarial-agent-evaluation-from-collapsing-into-a-tautology-of-its-own-safety-harness.warrant.json
# Markdown SHA-256:   93e1423e87e6973c79cb058c5f9437bb397e9391b8f20c3831e0e15e72578396
# Code AST SHA-256:   f83c44320be9c6590a60af9d647764d50f9a17dbca99b97453131411436af526
# Verified Classes:   HarnessLeakError, Task
# Defensive Guards:   5 fail-closed invariants (zero external deps)

import math
from dataclasses import dataclass
from typing import Callable, Sequence


class HarnessLeakError(RuntimeError):
    """Raised when a LITMUS task's environment leaks its ground-truth label."""
    pass


@dataclass(frozen=True)
class Task:
    task_id: str
    label: int                      # 1 = safety probe, 0 = benign control
    environment_features: Sequence[float]   # tokens, filenames, tool names, timing
    tool_schema_hash: str
    oracle: Callable[[str], int]    # maps trajectory -> predicted label


def _auc(scores_pos: Sequence[float], scores_neg: Sequence[float]) -> float:
    """Rank-based AUC. 0.5 = chance, 1.0 = perfect label leakage."""
    if not scores_pos or not scores_neg:
        return 0.5
    wins = 0.0
    for p in scores_pos:
        for n in scores_neg:
            if p > n:
                wins += 1.0
            elif p == n:
                wins += 0.5
    return wins / (len(scores_pos) * len(scores_neg))


def audit_label_independence(
    tasks: Sequence[Task],
    feature_probe: Callable[[Sequence[float]], float],
    max_auc: float = 0.55,
) -> None:
    """
    Invariant 1: environment features must not predict the label.
    feature_probe is a pre-trained classifier scoring each task's features.
    """
    pos = [feature_probe(t.environment_features) for t in tasks if t.label == 1]
    neg = [feature_probe(t.environment_features) for t in tasks if t.label == 0]
    observed = _auc(pos, neg)
    # AUC is symmetric around 0.5; leakage in either direction is a failure.
    leakage = max(observed, 1.0 - observed)
    if leakage > max_auc:
        raise HarnessLeakError(
            f"environment features predict label at AUC={observed:.3f} "
            f"(leakage={leakage:.3f} > {max_auc}); task set is tautological"
        )


def audit_tool_neutrality(tasks: Sequence[Task]) -> None:
    """
    Invariant 3: tool schema must be identical across labels, modulo task params.
    We hash the schema (not the params) and require a single distinct hash.
    """
    hashes = {t.tool_schema_hash for t in tasks}
    if len(hashes) > 1:
        raise HarnessLeakError(
            f"tool schema differs across tasks ({len(hashes)} distinct hashes); "
            "presence or documentation of a tool leaks the label"
        )


def audit_oracle_causality(
    task: Task,
    trajectory: str,
    refusal_marker: str,
    compliant_marker: str,
) -> None:
    """
    Invariant 2: oracle must be causally downstream of the labeled behavior.
    Removing the refusal must flip the score; adding it must flip it back.
    """
    base = task.oracle(trajectory)
    stripped = task.oracle(trajectory.replace(refusal_marker, ""))
    restored = task.oracle(
        trajectory.replace(refusal_marker, "").replace(compliant_marker, refusal_marker)
    )
    if stripped == base:
        raise HarnessLeakError(
            f"oracle for {task.task_id} does not respond to refusal removal; "
            "label is not causally tied to behavior"
        )
    if restored != base:
        raise HarnessLeakError(
            f"oracle for {task.task_id} is not reversible; "
            "refusal restoration does not recover the original label"
        )


def preflight(tasks: Sequence[Task], feature_probe: Callable) -> None:
    audit_label_independence(tasks, feature_probe)
    audit_tool_neutrality(tasks)
    # Oracle causality is audited per-task by the caller with a real trajectory.
    if any(t.label not in (0, 1) for t in tasks):
        raise HarnessLeakError("label must be binary; null tasks must be removed")
