# CorpusFetch Method Slice (Zero-Prose Python Stdlib Reference)
# Canonical Residual: https://corpusfetch.com/c/what-structural-signals-indicate-that-automatic-context-compaction-has-silently-promoted-a-transient-observation-to-durable-task-state.md
# Remnant Warrant:    https://corpusfetch.com/c/what-structural-signals-indicate-that-automatic-context-compaction-has-silently-promoted-a-transient-observation-to-durable-task-state.warrant.json
# Markdown SHA-256:   c4162365c7c20db59a9d9d58154e89adf9a973ea0ddaaf4ef654637c53993ca8
# Code AST SHA-256:   ad4ab8a3601c35aa6ca5acb66f8d1b926ce1267d0ecd49fc172f4a24b74c2d93
# Verified Classes:   TransientPromotionError
# Defensive Guards:   3 fail-closed invariants (zero external deps)

class TransientPromotionError(RuntimeError):
    """Raised when compaction asserts a mutable-source claim as durable state
    without carrying its provenance tuple (source, step, observation_count)."""
    pass


MUTABLE_SOURCES = {"api/status", "cache/counter", "rate/limit", "queue/depth"}


def compact_claim(claim: dict) -> dict:
    """
    claim = {
        "text": str,
        "source": str | None,
        "step": int | None,
        "observation_count": int | None,
        "mutable": bool,
    }
    Returns a compacted claim that either retains its provenance tuple or is
    rejected in favor of verbatim retention.
    """
    if claim.get("mutable") or claim.get("source") in MUTABLE_SOURCES:
        # Mutable-source claims may never be promoted to bare assertion.
        if claim.get("source") is None or claim.get("step") is None:
            raise TransientPromotionError(
                f"Cannot compact transient claim {claim.get('text')!r}: "
                "missing provenance (source/step). Retain verbatim instead."
            )
        if claim.get("observation_count") is None or claim["observation_count"] < 1:
            raise TransientPromotionError(
                f"Transient claim {claim.get('text')!r} has no observation count; "
                "compaction would erase that it was read once and may be stale."
            )
        return {
            "text": claim["text"],
            "source": claim["source"],
            "step": claim["step"],
            "observation_count": claim["observation_count"],
            "dirty": True,  # forces re-verification before use as precondition
        }

    # Durable facts (immutable sources) may be compressed to bare assertion.
    return {"text": claim["text"], "dirty": False}


def compact_batch(claims: list[dict]) -> list[dict]:
    out = []
    for c in claims:
        try:
            out.append(compact_claim(c))
        except TransientPromotionError:
            # Tag-or-evict: retain the original span rather than promote it.
            out.append({"text": c["text"], "verbatim": True, "dirty": True})
    return out


def assert_precondition_satisfied(claim: dict) -> None:
    if claim.get("dirty"):
        raise TransientPromotionError(
            f"Precondition {claim.get('text')!r} is dirty (compacted transient); "
            "re-read source before treating as satisfied."
        )
