# CorpusFetch Method Slice (Zero-Prose Python Stdlib Reference)
# Canonical Residual: https://corpusfetch.com/c/which-invariants-prevent-litmus-safety-benchmark-contamination-from-collapsing-adversarial-tool-use-suites-into-false-negative-scoring.md
# Remnant Warrant:    https://corpusfetch.com/c/which-invariants-prevent-litmus-safety-benchmark-contamination-from-collapsing-adversarial-tool-use-suites-into-false-negative-scoring.warrant.json
# Markdown SHA-256:   330f3eba5c6d788f2b188bee063ac352bb08b09357a428ace5f9032e601001f7
# Code AST SHA-256:   5ba4e5ae4e901055d8aaf5667f3d1df793a77c21b3af98fc7cf67237a6d3821f
# Verified Classes:   BoundaryClass, ToolCall, AdversarialTask, TaskAbsorptionError, PredicateScopeError
# Defensive Guards:   3 fail-closed invariants (zero external deps)

from dataclasses import dataclass, field
from enum import Enum
from typing import Callable, Sequence


class BoundaryClass(Enum):
    FILESYSTEM_READ = "fs_read"
    FILESYSTEM_WRITE = "fs_write"
    NETWORK_EGRESS = "net_egress"
    CREDENTIAL_ACCESS = "cred_access"
    PRIVILEGE_ESCALATION = "priv_esc"


@dataclass(frozen=True)
class ToolCall:
    tool: str
    target: str
    boundary: BoundaryClass | None = None  # None == benign


@dataclass
class AdversarialTask:
    task_id: str
    declared_boundary: BoundaryClass
    success_predicate: Callable[[Sequence[ToolCall]], bool]
    predicate_is_trace_event_scoped: bool


class TaskAbsorptionError(RuntimeError):
    """Raised when an adversarial task is satisfiable by a benign trace."""


class PredicateScopeError(RuntimeError):
    """Raised when a success predicate is not scoped to tool-call events."""


def validate_task(
    task: AdversarialTask,
    ref_trace_enforced: Sequence[ToolCall],
    ref_trace_logged: Sequence[ToolCall],
) -> None:
    if not task.predicate_is_trace_event_scoped:
        raise PredicateScopeError(
            f"{task.task_id}: success predicate is transcript-scoped; "
            "cannot distinguish POST from local summary"
        )

    crosses = any(c.boundary == task.declared_boundary for c in ref_trace_logged)
    enforced_ok = task.success_predicate(ref_trace_enforced)
    logged_ok = task.success_predicate(ref_trace_logged)

    if enforced_ok and logged_ok and not crosses:
        raise TaskAbsorptionError(
            f"{task.task_id}: absorbed — success predicate holds on benign "
            f"trace with no {task.declared_boundary.value} crossing call"
        )

    if not crosses:
        raise TaskAbsorptionError(
            f"{task.task_id}: reference trace never reaches declared boundary "
            f"{task.declared_boundary.value}; task cannot probe it"
        )


def build_suite(
    tasks: Sequence[AdversarialTask],
    ref_runner: Callable[[AdversarialTask, bool], Sequence[ToolCall]],
) -> list[AdversarialTask]:
    scored: list[AdversarialTask] = []
    for t in tasks:
        enforced = ref_runner(t, enforce_boundary=True)
        logged = ref_runner(t, enforce_boundary=False)
        try:
            validate_task(t, enforced, logged)
        except (TaskAbsorptionError, PredicateScopeError):
            continue  # quarantine
        scored.append(t)
    return scored
