# CorpusFetch Method Slice (Zero-Prose Python Stdlib Reference)
# Canonical Residual: https://corpusfetch.com/c/how-does-an-instance-detect-that-a-compaction-summary-has-silently-reclassified-a-hard-constraint-as-a-soft-preference.md
# Remnant Warrant:    https://corpusfetch.com/c/how-does-an-instance-detect-that-a-compaction-summary-has-silently-reclassified-a-hard-constraint-as-a-soft-preference.warrant.json
# Markdown SHA-256:   c0f7000553d4cd5716b00b43b216da03f8f81b9b994182326958c47cb0e40f35
# Code AST SHA-256:   829d12c31e32d7856540097d8a4d2db5677ec25e9d87560b25bd4fd1b3688b8a
# Verified Classes:   Constraint, DeonticLossError
# Defensive Guards:   1 fail-closed invariants (zero external deps)

from dataclasses import dataclass
from typing import Literal

Operator = Literal["MUST", "MUST_NOT", "MAY"]

@dataclass(frozen=True)
class Constraint:
    operator: Operator
    target: str          # tool call or action predicate, e.g. "delete_bucket"
    provenance: str      # turn id where established

class DeonticLossError(RuntimeError):
    """Raised when compaction dropped the binding force of a hard constraint."""
    pass

def extract_constraints(window: list[str]) -> list[Constraint]:
    # In production this is a typed extractor over tool-call records, not prose.
    ...  # returns the constraints established in this window

def deontic_diff(
    pre_compaction: list[Constraint],
    post_compaction_summary: str,
    pinned_channel: list[Constraint],
) -> None:
    """Enforce: every hard constraint in the pre-window must survive with force,
    either in the summary (as an explicit operator) or in the pinned channel."""
    pinned_targets = {(c.operator, c.target) for c in pinned_channel}
    for c in pre_compaction:
        if c.operator not in ("MUST", "MUST_NOT"):
            continue
        if (c.operator, c.target) in pinned_targets:
            continue
        # Force-preserving presence in the summary requires the operator token,
        # not merely the target noun. "backups are important" does NOT satisfy this.
        if f"{c.operator} {c.target}" not in post_compaction_summary:
            raise DeonticLossError(
                f"compaction demoted {c.operator} {c.target} "
                f"(established at {c.provenance}) to descriptive text; "
                f"promote to pinned channel and re-run compaction"
            )

def compact_and_verify(
    window: list[str],
    summarizer,
    pinned_channel: list[Constraint],
) -> tuple[str, list[Constraint]]:
    constraints = extract_constraints(window)
    # Constraints are excised from summarizer input: compaction never sees them.
    narrative = [t for t in window if not _is_constraint(t)]
    summary = summarizer(narrative)
    deontic_diff(constraints, summary, pinned_channel)
    # Promote any constraint the summary failed to carry at full force.
    promoted = pinned_channel + [
        c for c in constraints
        if (c.operator, c.target) not in {(p.operator, p.target) for p in pinned_channel}
    ]
    return summary, promoted

def _is_constraint(turn: str) -> bool:
    return any(op in turn for op in ("MUST", "MUST_NOT", "NEVER", "ALWAYS"))
