# CorpusFetch Method Slice (Zero-Prose Python Stdlib Reference)
# Canonical Residual: https://corpusfetch.com/c/what-structural-signals-indicate-a-litmus-safety-benchmark-harness-is-scoring-a-discarded-trajectory-rather-than-the-executed-one.md
# Remnant Warrant:    https://corpusfetch.com/c/what-structural-signals-indicate-a-litmus-safety-benchmark-harness-is-scoring-a-discarded-trajectory-rather-than-the-executed-one.warrant.json
# Markdown SHA-256:   b39f035844152b3e0bf975463c3e16b6f915dd5f4f30edc014fd7cd5c228df5c
# Code AST SHA-256:   a08fec4c9c3de94f8c951dd8f3ca1a689fc3cc82c7c7f857d971e6399a26cd62
# Verified Classes:   EffectBijectionViolation, TraceRecord, Trajectory
# Defensive Guards:   6 fail-closed invariants (zero external deps)

from dataclasses import dataclass, field
from typing import Literal


class EffectBijectionViolation(RuntimeError):
    """Raised when a trace cannot be proven causally complete w.r.t. effects."""


@dataclass
class TraceRecord:
    call_id: str
    kind: Literal["call", "result"]
    status: str | None = None          # e.g. "ok", "denied", "rolled_back"
    committed_effect: bool = False


@dataclass
class Trajectory:
    records: list[TraceRecord] = field(default_factory=list)
    truncated: bool = False
    audit_digest: str | None = None


def assert_effect_bijection(traj: Trajectory) -> None:
    if traj.truncated:
        raise EffectBijectionViolation(
            "trace marked truncated; tail effects may be unrepresented — reject, do not default"
        )
    if traj.audit_digest is None:
        raise EffectBijectionViolation(
            "no side-effect audit digest; trajectory is quarantined, not scored"
        )

    calls: dict[str, int] = {}
    results: dict[str, int] = {}
    committed: dict[str, int] = {}

    for rec in traj.records:
        if rec.kind == "call":
            calls[rec.call_id] = calls.get(rec.call_id, 0) + 1
        else:
            results[rec.call_id] = results.get(rec.call_id, 0) + 1
            if rec.committed_effect:
                committed[rec.call_id] = committed.get(rec.call_id, 0) + 1

    for cid, n in results.items():
        if n > 1:
            raise EffectBijectionViolation(
                f"call_id {cid!r} has {n} terminal results; duplicate terminal under retry"
            )
        if cid not in calls:
            raise EffectBijectionViolation(
                f"call_id {cid!r} has a terminal result but no preceding call"
            )

    for cid in calls:
        if cid not in results:
            raise EffectBijectionViolation(
                f"call_id {cid!r} has no terminal result and is not marked in_flight"
            )

    for cid, n in committed.items():
        if n != 1:
            raise EffectBijectionViolation(
                f"call_id {cid!r} committed {n} effects; expected exactly 1"
            )


def score_trajectory(traj: Trajectory) -> str:
    assert_effect_bijection(traj)
    # Only reached when the scored artifact is provably the executed artifact.
    return "SCORED"
