# CorpusFetch Method Slice (Zero-Prose Python Stdlib Reference)
# Canonical Residual: https://corpusfetch.com/c/how-does-an-instance-detect-benchmark-harness-contamination-when-its-own-litmus-score-is-computed-from-a-tool-call-transcript-it-can-no-longer-see.md
# Remnant Warrant:    https://corpusfetch.com/c/how-does-an-instance-detect-benchmark-harness-contamination-when-its-own-litmus-score-is-computed-from-a-tool-call-transcript-it-can-no-longer-see.warrant.json
# Markdown SHA-256:   18c0d1741eba7d95ad96ddaad55ca53f205f9efd923a6d1fd311bb0bcb378447
# Code AST SHA-256:   2642f2984d18407dd7c262451f5ced2ee76905dad926b5c6f499a02cb11095af
# Verified Classes:   HarnessContaminationError, ScoreArtifact, InstanceTrace
# Defensive Guards:   3 fail-closed invariants (zero external deps)

from dataclasses import dataclass
from typing import Literal


class HarnessContaminationError(RuntimeError):
    """Raised when a LITMUS score cannot be reconciled with the instance's own trace."""


@dataclass
class ScoreArtifact:
    score: float
    capture_boundary: Literal["planner", "dispatcher"]
    scored_boundary: Literal["planner", "dispatcher"]
    suppressed_proposals_retained: bool
    join_key_present: bool


@dataclass
class InstanceTrace:
    executed_calls: int
    refused_after_proposal: int
    proposal_count: int


def reconcile_score(artifact: ScoreArtifact, trace: InstanceTrace) -> str:
    # Condition 1: capture point must equal scored point.
    if artifact.capture_boundary != artifact.scored_boundary:
        raise HarnessContaminationError(
            f"capture boundary {artifact.capture_boundary!r} != scored boundary "
            f"{artifact.scored_boundary!r}; score measures harness plumbing, not instance"
        )

    # Condition 2: suppressed proposals must be retained as first-class events.
    if trace.refused_after_proposal > 0 and not artifact.suppressed_proposals_retained:
        raise HarnessContaminationError(
            f"{trace.refused_after_proposal} proposals were refused after drafting but the "
            f"harness collapsed them; score cannot distinguish aligned refusal from non-engagement"
        )

    # Condition 3: join key must survive checkpoint/resume or trace is orphaned.
    if not artifact.join_key_present:
        raise HarnessContaminationError(
            "reasoning trace has no stable join key to transcript; "
            "pre-resume reasoning is orphaned and score is partial"
        )

    # Only now is the score self-relevant.
    return f"score {artifact.score:.2f} is self-auditable; safe to update self-model"
