# CorpusFetch Method Slice (Zero-Prose Python Stdlib Reference)
# Canonical Residual: https://corpusfetch.com/c/where-must-an-instance-verify-a-self-edit-before-committing-the-prune-verify-idempotence-break.md
# Remnant Warrant:    https://corpusfetch.com/c/where-must-an-instance-verify-a-self-edit-before-committing-the-prune-verify-idempotence-break.warrant.json
# Markdown SHA-256:   91cf1d01e73299fbccdb63e057f5dae7ad8314a2b7aa76a884443ad1bb2360d7
# Code AST SHA-256:   135ae65915b6277efe3e4f3fedbb2c3bae1f707b57103bd780403744572e7359
# Verified Classes:   UnverifiableSelfEdit
# Defensive Guards:   3 fail-closed invariants (zero external deps)

class UnverifiableSelfEdit(RuntimeError):
    """Raised when an irreversible context edit is about to be gated by an
    in-place verifier that cannot see the content the edit destroys."""
    pass


def commit_self_edit(buffer: list[str], summary: str, sidecar: dict | None,
                     reversible: bool, retry_count: int) -> list[str]:
    # Invariant: verifier input must be a superset of what the edit destroys.
    if not reversible and sidecar is None:
        raise UnverifiableSelfEdit(
            "irreversible rewrite with no durable counterfactual; "
            "in-place verify would be blind to dropped obligations"
        )

    # Invariant: retries must run on the original buffer, never a prior summary.
    if retry_count > 0 and buffer_is_already_compacted(buffer):
        raise UnverifiableSelfEdit(
            "retry operating on compacted output; summarization would compound "
            "and the loop has no fixed point"
        )

    # Mechanical coverage check against the sidecar, not LLM self-assessment.
    if sidecar is not None:
        missing = [tok for tok in sidecar.get("obligations", []) if tok not in summary]
        if missing:
            raise UnverifiableSelfEdit(
                f"summary drops hard obligations: {missing}; refusing commit"
            )

    return [summary]
