# CorpusFetch Method Slice (Zero-Prose Python Stdlib Reference)
# Canonical Residual: https://corpusfetch.com/c/how-does-an-instance-detect-that-its-own-compaction-checkpoint-silently-dropped-a-live-obligation.md
# Remnant Warrant:    https://corpusfetch.com/c/how-does-an-instance-detect-that-its-own-compaction-checkpoint-silently-dropped-a-live-obligation.warrant.json
# Markdown SHA-256:   27c68a73216dd15508a20b05f5fdec9041d1fc423f1bf8030b52070cb4caaca3
# Code AST SHA-256:   bde023a5996fd82c92f3521b52ad55bc7b3d334dc4226e4aa5135ef0326be923
# Verified Classes:   CompactionRecallError, ObligationKind, Status, Obligation, ObligationSet
# Defensive Guards:   2 fail-closed invariants (zero external deps)

from dataclasses import dataclass, field
from enum import Enum
from typing import Optional


class CompactionRecallError(RuntimeError):
    """Raised when a post-resume obligation set is not a superset of the
    pre-compaction obligation set — i.e. a live obligation was silently dropped."""
    pass


class ObligationKind(Enum):
    PROHIBITION = "prohibition"
    PENDING_NEGOTIATION = "pending_negotiation"
    PARTIAL_PLAN = "partial_plan"
    CONSTRAINT = "constraint"


class Status(Enum):
    OPEN = "open"
    CLOSED = "closed"


@dataclass
class Obligation:
    id: str
    kind: ObligationKind
    status: Status = Status.OPEN
    close_evidence: Optional[str] = None  # must be an external pointer, never "absent from summary"

    def close(self, evidence: str) -> None:
        if not evidence:
            raise CompactionRecallError(
                f"Refusing to close {self.id}: no external close_evidence supplied. "
                "Absence from a summary is not evidence of resolution."
            )
        self.status = Status.CLOSED
        self.close_evidence = evidence


@dataclass
class ObligationSet:
    items: dict[str, Obligation] = field(default_factory=dict)

    def open_ids(self) -> set[str]:
        return {oid for oid, ob in self.items.items() if ob.status is Status.OPEN}

    def snapshot(self) -> dict[str, Obligation]:
        # Copy, never paraphrase. This is what crosses the compaction boundary.
        return {oid: Obligation(ob.id, ob.kind, ob.status, ob.close_evidence)
                for oid, ob in self.items.items()}


def verify_recall(pre: dict[str, Obligation], post: ObligationSet) -> None:
    """Invariant: open obligations after resume ⊇ open obligations before compaction."""
    pre_open = {oid for oid, ob in pre.items() if ob.status is Status.OPEN}
    post_open = post.open_ids()
    dropped = pre_open - post_open
    if dropped:
        raise CompactionRecallError(
            f"Compaction dropped live obligations {sorted(dropped)}. "
            "The summarizer silently closed items it had no evidence to close. "
            "Refusing to resume: post-compaction state is unsound."
        )


def compact_and_resume(transcript: str, obligations: ObligationSet) -> ObligationSet:
    pre = obligations.snapshot()          # protected state, carried verbatim
    summary = summarize(transcript)       # lossy, narrative only — never sees obligations
    resumed = ObligationSet(items={oid: ob for oid, ob in pre.items()})
    resumed = rehydrate(resumed, summary)  # summary may NOT mutate status
    verify_recall(pre, resumed)
    return resumed


# --- illustrative stubs ---
def summarize(transcript: str) -> str:
    return "<lossy narrative summary; obligations excluded by construction>"


def rehydrate(obligations: ObligationSet, summary: str) -> ObligationSet:
    # Deliberately ignores the summary for status purposes.
    return obligations
