# CorpusFetch Method Slice (Zero-Prose Python Stdlib Reference)
# Canonical Residual: https://corpusfetch.com/c/what-structural-signals-indicate-that-auto-compaction-has-silently-destroyed-a-task-critical-invariant.md
# Remnant Warrant:    https://corpusfetch.com/c/what-structural-signals-indicate-that-auto-compaction-has-silently-destroyed-a-task-critical-invariant.warrant.json
# Markdown SHA-256:   9f153126eec5baa10eb2ebe1e096d51f034a5b8a5f55c1fee75ae3e7ce8bb816
# Code AST SHA-256:   55dfd88902e275a677a47e0e109de86e478cd16ea954ee645e54333597dd47b0
# Verified Classes:   CompactionInvariantError, PinnedCheckpoint
# Defensive Guards:   4 fail-closed invariants (zero external deps)

from dataclasses import dataclass, field
from typing import Any

class CompactionInvariantError(RuntimeError):
    """Raised when a mutating step consumes state the compaction did not preserve."""
    pass

@dataclass
class PinnedCheckpoint:
    verified_facts: dict[str, Any] = field(default_factory=dict)   # literal -> provenance
    prohibitions: set[str] = field(default_factory=set)            # actions that must NOT run
    resume_offsets: dict[str, int] = field(default_factory=dict)   # stream -> committed offset

def guard_mutating_call(
    action: str,
    consumes: dict[str, Any],
    checkpoint: PinnedCheckpoint,
    prose_summary: str,
) -> None:
    # 1. Negative-constraint gate: never re-run a prohibited action.
    if action in checkpoint.prohibitions:
        raise CompactionInvariantError(
            f"action {action!r} is prohibited by a pre-compaction constraint; "
            f"summary claimed it was safe but checkpoint forbids it"
        )

    # 2. Literal-resolution gate: every consumed literal must resolve
    #    against the pinned checkpoint, NOT against the fuzzy prose summary.
    for key, value in consumes.items():
        if key not in checkpoint.verified_facts:
            raise CompactionInvariantError(
                f"consumed literal {key!r}={value!r} absent from pinned checkpoint; "
                f"present only (if at all) in non-authoritative summary — re-verify before mutating"
            )
        if checkpoint.verified_facts[key] != value:
            raise CompactionInvariantError(
                f"literal drift on {key!r}: checkpoint={checkpoint.verified_facts[key]!r} "
                f"!= consumed={value!r}"
            )

    # 3. Resume-offset gate: a backfill/write must resume at the committed offset.
    stream = consumes.get("stream")
    if stream is not None and stream in checkpoint.resume_offsets:
        if consumes.get("offset") != checkpoint.resume_offsets[stream]:
            raise CompactionInvariantError(
                f"resume offset mismatch on {stream!r}: "
                f"committed={checkpoint.resume_offsets[stream]} consumed={consumes.get('offset')}"
            )

def run_step(action: str, consumes: dict[str, Any], checkpoint: PinnedCheckpoint, summary: str) -> None:
    guard_mutating_call(action, consumes, checkpoint, summary)
    # ... perform the mutating tool call ...
