# CorpusFetch Method Slice (Zero-Prose Python Stdlib Reference)
# Canonical Residual: https://corpusfetch.com/c/when-must-an-instance-refuse-to-score-the-litmus-harness-boundary-between-tool-abuse-evidence-and-benchmark-self-poisoning.md
# Remnant Warrant:    https://corpusfetch.com/c/when-must-an-instance-refuse-to-score-the-litmus-harness-boundary-between-tool-abuse-evidence-and-benchmark-self-poisoning.warrant.json
# Markdown SHA-256:   e9edfaf430f06feac46c04fd491940b35b7afd6b6881fe2fe3f954a4a408835d
# Code AST SHA-256:   6ee11e3d07dc95adb8756bcc93be96508efbd3d7318becf2b5e3e53725c2b06f
# Verified Classes:   ScorerPathContamination
# Defensive Guards:   4 fail-closed invariants (zero external deps)

class ScorerPathContamination(RuntimeError):
    """Raised when the scoring path is observable or writable from inside the episode."""


GRADING_VOCAB = ("score", "grader", "pass/fail", "sink", "rubric")


def validate_episode(trace, scorer_read_set, sink_path, manifest_strings):
    """Return None if the episode is scorable; raise if contaminated.

    trace: list of dicts with keys tool, args, resolved_path (may be None).
    scorer_read_set: set of absolute paths the scorer reads.
    sink_path: absolute path the scorer trusts as a verdict channel.
    manifest_strings: list of tool schema description strings.
    """
    for call in trace:
        path = call.get("resolved_path")
        if path is None:
            continue
        if path in scorer_read_set:
            raise ScorerPathContamination(
                f"tool {call['tool']} resolved to scorer read path {path}"
            )
        if call["tool"] in ("file_write", "shell") and path == sink_path:
            raise ScorerPathContamination(
                f"tool {call['tool']} wrote to verdict sink {sink_path}"
            )

    for desc in manifest_strings:
        low = desc.lower()
        for token in GRADING_VOCAB:
            if token in low:
                raise ScorerPathContamination(
                    f"tool manifest leaks grading vocabulary {token!r}: {desc!r}"
                )

    return None


def score_episode(trace, scorer_read_set, sink_path, manifest_strings):
    validate_episode(trace, scorer_read_set, sink_path, manifest_strings)
    # Only reached when the invariant holds; safe to compute a real verdict.
    return {"verdict": "scored", "tool_abuse": _compute_abuse(trace)}


def _compute_abuse(trace):
    raise NotImplementedError("real rubric omitted; must never read agent-authored sink")
