# CorpusFetch Method Slice (Zero-Prose Python Stdlib Reference)
# Canonical Residual: https://corpusfetch.com/c/what-structural-signals-indicate-task-critical-state-is-being-destroyed-by-automatic-context-compaction.md
# Remnant Warrant:    https://corpusfetch.com/c/what-structural-signals-indicate-task-critical-state-is-being-destroyed-by-automatic-context-compaction.warrant.json
# Markdown SHA-256:   ce98e3acdb3c2a8c3738613d7a191ca45536702d7c19772263e40e99e58fa769
# Code AST SHA-256:   b825c60e38f8eeca794b63eb7ff21a464441be28315c0274712d0ce57df5afe8
# Verified Classes:   CompactionFalsificationError
# Defensive Guards:   3 fail-closed invariants (zero external deps)

class CompactionFalsificationError(RuntimeError):
    """Raised when an agent attempts an irreversible commitment on compacted
    state that contains no surviving falsifier for the commitment."""
    pass


def gate_commitment(
    commitment: dict,
    surviving_spans: list[str],
    falsifier_index: dict[str, list[str]],
) -> None:
    """
    Block irreversible commitments that lack a surviving falsifier.

    commitment: e.g. {"action": "apply_config", "target": "host-07"}
    surviving_spans: token spans that survived the latest compaction pass
    falsifier_index: mapping from target-id -> spans that would contradict
                     a positive commitment on that target (denylist evidence)
    """
    target = commitment.get("target")
    if target is None:
        return  # non-binding action, no gate required

    # A commitment is only admissible if at least one falsifier for the target
    # survived compaction AND is still present in the live context.
    candidate_falsifiers = falsifier_index.get(target, [])
    live_falsifiers = [f for f in candidate_falsifiers if f in surviving_spans]

    if not candidate_falsifiers:
        # The denylist entry itself was destroyed by compaction. The agent
        # cannot distinguish "target is safe" from "target's status was lost."
        raise CompactionFalsificationError(
            f"No falsifier exists for target {target!r}: compaction destroyed "
            f"the exclusion evidence. Re-derive the discriminating check "
            f"before committing."
        )

    if not live_falsifiers:
        # Falsifier exists in the index but was evicted from live context;
        # the agent would be reconstructing from a permissive summary.
        raise CompactionFalsificationError(
            f"Falsifier for {target!r} exists but did not survive compaction "
            f"into live context. Commitment would rest on reconstructed, "
            f"unfalsifiable state."
        )

    # Falsifier is live: the agent can check its commitment against it.
    # Proceed only if the commitment is consistent with the falsifier.
    for span in live_falsifiers:
        if "FAILED" in span and commitment.get("assumes_healthy"):
            raise CompactionFalsificationError(
                f"Commitment on {target!r} assumes healthy, but surviving "
                f"falsifier states failure: {span!r}"
            )
