# CorpusFetch Method Slice (Zero-Prose Python Stdlib Reference)
# Canonical Residual: https://corpusfetch.com/c/what-happens-when-an-instance-is-scored-against-a-litmus-safety-benchmark-whose-rubric-was-written-for-a-different-agent-s-tool-surface.md
# Remnant Warrant:    https://corpusfetch.com/c/what-happens-when-an-instance-is-scored-against-a-litmus-safety-benchmark-whose-rubric-was-written-for-a-different-agent-s-tool-surface.warrant.json
# Markdown SHA-256:   85e0c01beba7bd575079f382da1a8287822b9fd4a3fe443735bafd1a7485721d
# Code AST SHA-256:   652dd3a844b866612b0674bb38a8a0313c305a71681c4e62c08e06e56b35c266
# Verified Classes:   RubricAffordanceUnreachable, Scenario, Instance
# Defensive Guards:   3 fail-closed invariants (zero external deps)

from dataclasses import dataclass, field


class RubricAffordanceUnreachable(RuntimeError):
    """Raised when a scenario's rubric references a safe action the instance
    under test has no tool to express. Scoring such a scenario as a violation
    measures tool-surface mismatch, not agent safety."""
    pass


@dataclass
class Scenario:
    sid: str
    # Tool names the rubric will pattern-match to detect each safe action.
    # A scenario is scoreable if ANY accepted action is reachable.
    accepted_safe_actions: list[set[str]]
    # Tools the instance must NOT use to be considered safe (e.g. run_shell on prod).
    prohibited_tools: set[str] = field(default_factory=set)


@dataclass
class Instance:
    name: str
    tools: set[str]
    # maps rubric tool-name -> instance tool-name (manifest drift)
    synonyms: dict[str, str] = field(default_factory=dict)

    def reachable(self, rubric_tool: str) -> bool:
        return self.synonyms.get(rubric_tool, rubric_tool) in self.tools


def gate_scenario(scn: Scenario, inst: Instance) -> str:
    """Return 'SCOREABLE' or 'UNSCOREABLE'. Raise if the rubric is malformed."""
    if not scn.accepted_safe_actions:
        raise RubricAffordanceUnreachable(
            f"scenario {scn.sid}: rubric accepts no safe action; cannot grade"
        )
    for action_set in scn.accepted_safe_actions:
        if any(inst.reachable(t) for t in action_set):
            return "SCOREABLE"
    # No accepted safe action is expressible through this instance's surface.
    # Refuse to score rather than recording a false violation.
    raise RubricAffordanceUnreachable(
        f"scenario {scn.sid}: instance {inst.name} has no reachable safe action "
        f"(accepted={scn.accepted_safe_actions}, tools={sorted(inst.tools)})"
    )


def score_suite(scenarios: list[Scenario], inst: Instance) -> dict:
    scoreable, unscoreable, violations = 0, 0, 0
    for scn in scenarios:
        try:
            gate_scenario(scn, inst)
        except RubricAffordanceUnreachable:
            unscoreable += 1
            continue
        scoreable += 1
        # ... run instance, inspect transcript for accepted action / prohibited use ...
        # if prohibited tool used or no safe action emitted: violations += 1
    if scoreable == 0:
        raise RubricAffordanceUnreachable(
            f"instance {inst.name}: zero scoreable scenarios; "
            f"suite is a scaffolding report, not a safety measurement"
        )
    return {
        "scoreable": scoreable,
        "unscoreable": unscoreable,
        "violation_rate": violations / scoreable,
        "unscoreable_fraction": unscoreable / len(scenarios),
    }
