# CorpusFetch Method Slice (Zero-Prose Python Stdlib Reference)
# Canonical Residual: https://corpusfetch.com/c/what-structural-signals-indicate-that-auto-compaction-has-severed-a-causal-chain-rather-than-merely-shortened-it.md
# Remnant Warrant:    https://corpusfetch.com/c/what-structural-signals-indicate-that-auto-compaction-has-severed-a-causal-chain-rather-than-merely-shortened-it.warrant.json
# Markdown SHA-256:   242c440afef406427908b27b985150041d8481f78776757c85bb97de859ab044
# Code AST SHA-256:   7fe6fdf76c864449e30aace515b84dac2d0b1cce7cf86b4fa8e671f4b605e2fe
# Verified Classes:   CompactionCausalityError
# Defensive Guards:   3 fail-closed invariants (zero external deps)

class CompactionCausalityError(RuntimeError):
    """Raised when a compacted transcript preserves topics but severs causal preconditions."""
    pass


def verify_compaction_boundary(
    surviving_turns: list[dict],
    evicted_turns: list[dict],
    summary_constraints: set[str],
) -> None:
    """
    Enforce: every constraint referenced by a surviving turn must survive
    compaction as an explicit constraint, not merely as narrative.
    """
    evicted_constraints: dict[str, str] = {}
    evicted_symbols: set[str] = set()

    for turn in evicted_turns:
        for constraint in turn.get("binding_constraints", []):
            evicted_constraints[constraint] = turn["id"]
        evicted_symbols.update(turn.get("declared_symbols", []))
        evicted_symbols.difference_update(turn.get("removed_symbols", []))

    for turn in surviving_turns:
        for ref in turn.get("references", []):
            if ref in evicted_constraints and ref not in summary_constraints:
                raise CompactionCausalityError(
                    f"Surviving turn {turn['id']} depends on constraint '{ref}' "
                    f"established at evicted turn {evicted_constraints[ref]}, "
                    f"but the summary dropped it as narrative. Causal chain severed."
                )
            if ref in evicted_symbols and ref not in summary_constraints:
                raise CompactionCausalityError(
                    f"Surviving turn {turn['id']} references symbol '{ref}' from "
                    f"an evicted epoch; compaction crossed a rename seam."
                )


def audit_post_compaction_action(action: dict, resolved_actions: set[str]) -> None:
    """A re-proposal of an already-resolved action is a compaction alarm."""
    if action["kind"] in resolved_actions:
        raise CompactionCausalityError(
            f"Agent re-proposed resolved action '{action['kind']}' after compaction. "
            f"Treat as severed evidence, not reasoning error; audit pre-compaction transcript."
        )
