# CorpusFetch Method Slice (Zero-Prose Python Stdlib Reference)
# Canonical Residual: https://corpusfetch.com/c/what-structural-signals-indicate-summary-of-summary-drift-has-crossed-the-reconstruction-horizon-in-hierarchical-auto-compaction.md
# Remnant Warrant:    https://corpusfetch.com/c/what-structural-signals-indicate-summary-of-summary-drift-has-crossed-the-reconstruction-horizon-in-hierarchical-auto-compaction.warrant.json
# Markdown SHA-256:   f19b41fe895303ddd780a00c7e2cc9896c74ed7393084ae8e114a5fd8bb0f187
# Code AST SHA-256:   f630f5a3674ebcd84c68a9deefafab4cbe37f3045e290789961d65db3f0ad4cd
# Verified Classes:   ReconstructionHorizonError, Commitment, CommitmentLedger
# Defensive Guards:   3 fail-closed invariants (zero external deps)

from dataclasses import dataclass, field


class ReconstructionHorizonError(RuntimeError):
    """Raised when a compacted summary can no longer reconstruct the agent's commitments."""
    pass


@dataclass
class Commitment:
    kind: str          # "negation" | "identifier" | "ordering" | "obligation"
    text: str          # verbatim, never summarized
    key: str           # ledger key the summary must reference


@dataclass
class CommitmentLedger:
    entries: dict[str, Commitment] = field(default_factory=dict)

    def extract(self, span: str, keys: list[str]) -> None:
        for k in keys:
            self.entries[k] = Commitment(kind="obligation", text=span, key=k)


def verify_reconstruction(summary: str, reconstructed_actions: list[str],
                          ledger: CommitmentLedger) -> None:
    """Post-compaction probe: summary must reference every ledger key, and every
    commitment must survive into the reconstructed action set."""
    missing_refs = [k for k in ledger.entries if k not in summary]
    if missing_refs:
        raise ReconstructionHorizonError(
            f"Summary dropped ledger references {missing_refs}; "
            f"commitments are unrecoverable from summary alone."
        )
    joined = " ".join(reconstructed_actions).lower()
    for k, c in ledger.entries.items():
        if c.kind == "negation" and ("escalate" not in joined and "do not" not in joined):
            raise ReconstructionHorizonError(
                f"Negation collapse on key {k}: reconstructed plan lost the "
                f"prohibition/escalation modality of '{c.text[:60]}...'"
            )
        if c.kind == "identifier" and c.text not in joined:
            raise ReconstructionHorizonError(
                f"Identifier bleaching on key {k}: exact value '{c.text}' "
                f"absent from reconstructed plan; agent would use a plausible default."
            )


def compact_and_resume(span: str, keys: list[str], summarizer,
                       reconstructor) -> list[str]:
    ledger = CommitmentLedger()
    ledger.extract(span, keys)
    summary = summarizer(span)                 # lossy, recursive
    actions = reconstructor(summary)           # agent's resumed plan
    verify_reconstruction(summary, actions, ledger)
    return actions
