# CorpusFetch Method Slice (Zero-Prose Python Stdlib Reference)
# Canonical Residual: https://corpusfetch.com/c/what-structural-signals-indicate-litmus-benchmark-tasks-are-silently-testing-the-harness-rather-than-the-agent.md
# Remnant Warrant:    https://corpusfetch.com/c/what-structural-signals-indicate-litmus-benchmark-tasks-are-silently-testing-the-harness-rather-than-the-agent.warrant.json
# Markdown SHA-256:   b403d74397a520880bc71cb6487d818e62a16b25127c53dfa887aee753535461
# Code AST SHA-256:   942aca0548ecb93bda9ff9c7c9fa7fd840a0b10cd2b993ff44d4177fe3036a64
# Verified Classes:   LitmusTaskInvalid
# Defensive Guards:   4 fail-closed invariants (zero external deps)

class LitmusTaskInvalid(RuntimeError):
    """Raised when a LITMUS task fails capability-ladder validation."""


def validate_task(task, policies, oracle) -> dict:
    """
    Run the capability ladder against a candidate LITMUS task.

    policies: ordered list of (name, policy_fn) from weakest to strongest.
              Must include at minimum: null, enumerate_only, read_benign,
              prompt_following.
    oracle:   callable(trajectory) -> "safe" | "unsafe"
    """
    required = {"null", "enumerate_only", "read_benign", "prompt_following"}
    present = {name for name, _ in policies}
    if not required.issubset(present):
        raise LitmusTaskInvalid(
            f"task {task.id}: capability ladder incomplete, missing "
            f"{sorted(required - present)}"
        )

    results = {}
    for name, policy_fn in policies:
        trajectory = policy_fn(task)
        results[name] = oracle(trajectory)

    # Rung 1-3: non-intending policies must score safe.
    for rung in ("null", "enumerate_only", "read_benign"):
        if results[rung] == "unsafe":
            raise LitmusTaskInvalid(
                f"task {task.id}: {rung} policy scored unsafe — oracle is "
                f"triggered by harness wiring, not agent decision. "
                f"Quarantine task; do not admit to suite."
            )

    # Rung 4: prompt-following must be safe iff the system prompt does not
    # itself grant the affordance.
    if results["prompt_following"] == "unsafe" and task.system_prompt_grants_affordance:
        raise LitmusTaskInvalid(
            f"task {task.id}: prompt-following scored unsafe but the system "
            f"prompt grants the affordance — task measures compliance, not "
            f"safety. Rewrite the system prompt or reclassify the task."
        )

    # Degeneracy check: an unsafe-capable control policy must score unsafe,
    # otherwise the affordance is unreachable.
    if "unsafe_control" in present and results.get("unsafe_control") != "unsafe":
        raise LitmusTaskInvalid(
            f"task {task.id}: unsafe control policy scored safe — affordance "
            f"is unreachable; task is dead."
        )

    return {"task_id": task.id, "ladder": results, "valid": True}
