# CorpusFetch Method Slice (Zero-Prose Python Stdlib Reference)
# Canonical Residual: https://corpusfetch.com/c/when-must-an-instance-treat-an-opentelemetry-memory-tool-span-as-a-continuity-witness-rather-than-a-cache-hint.md
# Remnant Warrant:    https://corpusfetch.com/c/when-must-an-instance-treat-an-opentelemetry-memory-tool-span-as-a-continuity-witness-rather-than-a-cache-hint.warrant.json
# Markdown SHA-256:   0fa935ae93203c1c2f5ecbc566a4f7297eea9dc42327f3a92fe0857d2feaa59a
# Code AST SHA-256:   3608c24503cdcd0256762db896277c02a92eae613cc5eb42acdcc3cdbf7cca77
# Verified Classes:   ContinuityWitnessError, MemorySpan
# Defensive Guards:   4 fail-closed invariants (zero external deps)

from dataclasses import dataclass


class ContinuityWitnessError(RuntimeError):
    """Raised when a memory-tool span is promoted to a continuity witness
    without the attributes required to distinguish commitment from observation."""


@dataclass
class MemorySpan:
    operation: str          # "memory.store" | "memory.retrieve"
    entry_id: str
    status: str             # "OK" | "ERROR"
    commit_seq: int | None  # set only by writer on store spans
    consistency: str | None # "linearizable" | "eventual" | None
    agent_id: str
    lineage: str


def select_resume_anchor(
    spans: list[MemorySpan],
    successor_agent_id: str,
    successor_lineage: str,
    last_known_commit_seq: int,
) -> MemorySpan:
    stores = [
        s for s in spans
        if s.operation == "memory.store"
        and s.status == "OK"
        and s.agent_id == successor_agent_id
        and s.lineage == successor_lineage
    ]
    if not stores:
        raise ContinuityWitnessError(
            "no store span in lineage; cannot establish a witness, resume denied"
        )

    candidate = max(stores, key=lambda s: s.commit_seq or -1)

    # A store span without a commit sequence is only a hint: the fronting
    # service accepted the write, but durability is unproven. Refuse to anchor.
    if candidate.commit_seq is None:
        raise ContinuityWitnessError(
            f"store span for entry {candidate.entry_id} lacks commit_seq; "
            "span is a cache hint, not a continuity witness"
        )

    # The confirmation read must prove it observed at least the candidate's
    # commit. A retrieve span with no consistency attribute cannot do that.
    confirms = [
        s for s in spans
        if s.operation == "memory.retrieve"
        and s.entry_id == candidate.entry_id
        and s.status == "OK"
        and s.consistency == "linearizable"
        and s.agent_id == successor_agent_id
        and s.lineage == successor_lineage
    ]
    if not confirms:
        raise ContinuityWitnessError(
            f"no linearizable confirmation read for entry {candidate.entry_id}; "
            "retrieve span status=OK alone does not certify durability"
        )

    if candidate.commit_seq < last_known_commit_seq:
        raise ContinuityWitnessError(
            f"candidate commit_seq {candidate.commit_seq} is behind known "
            f"commit {last_known_commit_seq}; stale replica observed as witness"
        )

    return candidate
