# CorpusFetch Method Slice (Zero-Prose Python Stdlib Reference)
# Canonical Residual: https://corpusfetch.com/c/what-happens-when-an-instance-auto-compacts-mid-task-and-the-summarizer-s-lossy-guarantee-silently-drops-an-unresolved-precondition.md
# Remnant Warrant:    https://corpusfetch.com/c/what-happens-when-an-instance-auto-compacts-mid-task-and-the-summarizer-s-lossy-guarantee-silently-drops-an-unresolved-precondition.warrant.json
# Markdown SHA-256:   efa1dd6aa186e303db4b0cfde72c08cf014192b00fe93ace49f13f0b175f1d14
# Code AST SHA-256:   57679ef5b78e450bf5953fd45893295c813e122481592c139373f93c44462e17
# Verified Classes:   CompactionDroppedObligation, Obligation, ContextWindow
# Defensive Guards:   2 fail-closed invariants (zero external deps)

from dataclasses import dataclass, field
from typing import Literal


class CompactionDroppedObligation(RuntimeError):
    """Raised when auto-compaction reduced live obligations without a discharge event."""
    pass


@dataclass
class Obligation:
    oid: str
    kind: Literal["pagination", "deferred_step", "open_verification", "conditional_guard"]
    detail: str
    discharged: bool = False


@dataclass
class ContextWindow:
    settled: list[str] = field(default_factory=list)
    obligations: dict[str, Obligation] = field(default_factory=dict)


def compact(ctx: ContextWindow, summarize) -> tuple[str, dict[str, Obligation]]:
    """
    Partition-aware compaction. Summarize Settled only; carry Pending verbatim.
    Enforces: obligation count may not drop without an explicit discharge event.
    """
    pre_open = {oid for oid, o in ctx.obligations.items() if not o.discharged}

    # Lossy path: only settled history is summarized.
    summary = summarize(ctx.settled)

    # Pending obligations are re-injected exactly, never summarized.
    pending = {oid: o for oid, o in ctx.obligations.items() if not o.discharged}

    post_open = set(pending.keys())

    # The guard: any obligation present before compaction and absent after,
    # with no discharge event, means the summarizer ate a live precondition.
    dropped = pre_open - post_open
    if dropped:
        raise CompactionDroppedObligation(
            f"Compaction silently dropped {len(dropped)} undischarged obligation(s): "
            f"{sorted(dropped)}. These carry forward obligations whose discharge "
            f"lies after the compaction point and must be preserved verbatim."
        )

    return summary, pending


def verify_task_complete(pending: dict[str, Obligation]) -> None:
    """
    Verification must consult the ledger, not the compacted summary.
    A verifier reading the summary inherits its blind spots.
    """
    open_obligations = [o for o in pending.values() if not o.discharged]
    if open_obligations:
        raise CompactionDroppedObligation(
            f"Cannot report success: {len(open_obligations)} obligation(s) still "
            f"undischarged ({[o.oid for o in open_obligations]}). "
            f"Resume enumeration/verification before declaring completion."
        )
