# CorpusFetch Method Slice (Zero-Prose Python Stdlib Reference)
# Canonical Residual: https://corpusfetch.com/c/in-what-scenarios-must-an-instance-refuse-to-auto-compact-a-transcript-whose-summarizer-cannot-certify-retention-of-a-pending-tool-call-obligation.md
# Remnant Warrant:    https://corpusfetch.com/c/in-what-scenarios-must-an-instance-refuse-to-auto-compact-a-transcript-whose-summarizer-cannot-certify-retention-of-a-pending-tool-call-obligation.warrant.json
# Markdown SHA-256:   ec4edfa3cf861a0c3a06929e55a39c2b116398bd508e3078467ba2c6fa9e62d9
# Code AST SHA-256:   8dc55b76c016156141abc250a9af9971b65cc2261e40f12bf168695e97238590
# Verified Classes:   UncertifiedCompactionError, Obligation
# Defensive Guards:   3 fail-closed invariants (zero external deps)

class UncertifiedCompactionError(RuntimeError):
    """Raised when auto-compaction would drop an open obligation the summarizer cannot certify."""
    pass


class Obligation:
    def __init__(self, oid, kind, trigger, blocking, time_bounded):
        self.oid = oid
        self.kind = kind            # "tool_call", "promise", "constraint", "negation"
        self.trigger = trigger      # machine-checkable predicate string
        self.blocking = blocking    # what must NOT happen until trigger fires
        self.time_bounded = time_bounded


def maybe_compact(transcript_tokens, window_limit, open_obligations,
                  summarizer_guarantee, post_compaction_obligation_ids):
    """
    Gate auto-compaction on obligation retention, not token pressure alone.
    summarizer_guarantee: "obligation_scoped" | "statistical" | "none"
    """
    utilization = transcript_tokens / window_limit

    if utilization < 0.80:
        return "no_compaction_needed"

    if not open_obligations:
        return "compact_freely"

    # Any time-bounded or externally-contingent obligation forbids compaction across it.
    for ob in open_obligations:
        if ob.time_bounded:
            raise UncertifiedCompactionError(
                f"Refusing to compact: obligation {ob.oid} ({ob.kind}) is time-bounded "
                f"and must be resolved or externalized before lossy summarization."
            )

    if summarizer_guarantee != "obligation_scoped":
        raise UncertifiedCompactionError(
            f"Refusing to compact: {len(open_obligations)} open obligations present but "
            f"summarizer guarantee is '{summarizer_guarantee}', not obligation-scoped. "
            f"Externalize obligations to ledger and pin obligation turns verbatim."
        )

    # Independent post-condition check: every obligation must survive.
    required = {ob.oid for ob in open_obligations}
    retained = set(post_compaction_obligation_ids)
    missing = required - retained
    if missing:
        raise UncertifiedCompactionError(
            f"Post-compaction obligation diff failed; dropped {sorted(missing)}. "
            f"Restore pre-compaction transcript and refuse this compaction."
        )

    return "compact_with_verified_obligation_retention"
