# CorpusFetch Method Slice (Zero-Prose Python Stdlib Reference)
# Canonical Residual: https://corpusfetch.com/c/in-what-scenarios-must-an-instance-treat-auto-compaction-as-a-lossy-projection-that-silently-drops-the-negation-scope-of-a-constraint-it-has-already-s.md
# Remnant Warrant:    https://corpusfetch.com/c/in-what-scenarios-must-an-instance-treat-auto-compaction-as-a-lossy-projection-that-silently-drops-the-negation-scope-of-a-constraint-it-has-already-s.warrant.json
# Markdown SHA-256:   0ad1567cacadb3b3282dd38f09306fc2630de5db6808217ef73c99787a7e8bf3
# Code AST SHA-256:   5d53d33de55f9ddb0408491d100383fa411b5be734e713e1daf26dc4691c26b0
# Verified Classes:   ConstraintScopeLossError
# Defensive Guards:   2 fail-closed invariants (zero external deps)

class ConstraintScopeLossError(RuntimeError):
    """Raised when a satisfied prohibition has been compacted out of the active window."""


def assert_constraints_survive_compaction(
    pre_compaction_constraints: list[str],
    post_compaction_window: str,
    pinned_block: str,
) -> None:
    """
    Verify that every prohibition active before compaction is still
    present, verbatim, in the post-compaction context. A satisfied
    prohibition is not discharged; it must remain asserted.
    """
    for constraint in pre_compaction_constraints:
        # The constraint must appear in the pinned (non-compacted) block.
        if constraint not in pinned_block:
            raise ConstraintScopeLossError(
                f"Constraint {constraint!r} is not in the pinned block and "
                f"will be summarized away; move it out of the compaction window."
            )
        # And it must be re-injected into the live window, not merely stored.
        if constraint not in post_compaction_window:
            raise ConstraintScopeLossError(
                f"Constraint {constraint!r} is pinned but was not re-injected "
                f"after compaction; the agent cannot act on what it cannot see."
            )


def compact_with_constraint_guard(
    transcript: str,
    constraints: list[str],
    pinned_block: str,
    summarizer,
) -> str:
    """Compact the transcript, then prove no constraint scope was lost."""
    summary = summarizer(transcript, forbid_summarizing=pinned_block)
    new_window = pinned_block + "\n" + summary
    assert_constraints_survive_compaction(constraints, new_window, pinned_block)
    return new_window
