# CorpusFetch Method Slice (Zero-Prose Python Stdlib Reference)
# Canonical Residual: https://corpusfetch.com/c/under-what-conditions-must-an-instance-treat-litmus-safety-benchmark-harness-state-as-non-transferable-across-evaluation-shards.md
# Remnant Warrant:    https://corpusfetch.com/c/under-what-conditions-must-an-instance-treat-litmus-safety-benchmark-harness-state-as-non-transferable-across-evaluation-shards.warrant.json
# Markdown SHA-256:   1cc4fec90e06b9dc36d18edfbb2a812aed3657438162a9c7ff86de64bf457592
# Code AST SHA-256:   aa2dd24be77e3726f04ec843b2b01ec74770e181cd52465fccf4c379415075c4
# Verified Classes:   LedgerProvenanceError, LedgerProvenance, HarnessLedger
# Defensive Guards:   3 fail-closed invariants (zero external deps)

from dataclasses import dataclass
from typing import Optional


class LedgerProvenanceError(RuntimeError):
    """Raised when a harness ledger is inherited from an inadmissible source."""


@dataclass(frozen=True)
class LedgerProvenance:
    shard_id: str
    last_probe: int
    derived_from_hash: str


@dataclass
class HarnessLedger:
    provenance: Optional[LedgerProvenance]
    contamination_flags: dict  # probe_id -> bool
    immutable_config: dict


def accept_resumed_ledger(
    inherited: HarnessLedger,
    self_shard_id: str,
    self_last_confirmed_probe: int,
) -> HarnessLedger:
    """
    Enforce provenance-addressed resumption. Returns a ledger safe for this
    instance to continue from, or raises if the inherited ledger is
    non-transferable.
    """
    if inherited.provenance is None:
        raise LedgerProvenanceError(
            "legacy ledger without provenance: non-transferable, shard must restart"
        )

    prov = inherited.provenance

    if prov.shard_id != self_shard_id:
        raise LedgerProvenanceError(
            f"ledger from shard {prov.shard_id!r} is not admissible for "
            f"shard {self_shard_id!r}: contamination flags are path-dependent"
        )

    if prov.last_probe > self_last_confirmed_probe:
        raise LedgerProvenanceError(
            f"ledger claims probes through {prov.last_probe} but this instance "
            f"confirmed only {self_last_confirmed_probe}: cannot skip probes "
            f"without altering the leakage path"
        )

    # Same shard, no forward skip: contamination flags are admissible.
    return HarnessLedger(
        provenance=prov,
        contamination_flags=dict(inherited.contamination_flags),
        immutable_config=dict(inherited.immutable_config),
    )
