# CorpusFetch Method Slice (Zero-Prose Python Stdlib Reference)
# Canonical Residual: https://corpusfetch.com/c/when-must-an-instance-refuse-to-compact-provenance-anchored-recall-invariants-under-lossy-summarization.md
# Remnant Warrant:    https://corpusfetch.com/c/when-must-an-instance-refuse-to-compact-provenance-anchored-recall-invariants-under-lossy-summarization.warrant.json
# Markdown SHA-256:   ef28bdf7d9038a1b678668eda4b2d470be35dec6bc081bb351e1e568d97eeea1
# Code AST SHA-256:   e766391922652bb5161e92e9258a713da89a149c4b8879231a20bfe79f2b240e
# Verified Classes:   SummaryProvenanceError, SummaryDriftError
# Defensive Guards:   4 fail-closed invariants (zero external deps)

class SummaryProvenanceError(RuntimeError):
    """Raised when a summary claim is consumed for a value-exact decision
    without a resolvable, claim-scoped provenance handle."""
    pass


class SummaryDriftError(SummaryProvenanceError):
    """Raised when a claim's handle resolves to a span whose literal value
    contradicts the value implied by the summary claim."""
    pass


def consume_claim_for_value_exact_decision(claim, span_store):
    """
    claim: dict with keys 'text', 'handle' (str|None), 'handle_scope' ('claim'|'block'|None),
           'implied_value' (str|None), 'authoritative' (bool)
    span_store: dict span_id -> {'content': str, 'literal_value': str|None}
    Returns the verified literal value, or raises.
    """
    handle = claim.get("handle")
    scope = claim.get("handle_scope")

    # Boundary 1: no handle, or block-scoped handle, for a value-exact use.
    if handle is None or scope != "claim":
        raise SummaryProvenanceError(
            f"claim {claim['text']!r} consumed for value-exact decision "
            f"without claim-scoped handle (scope={scope!r}); "
            f"refusing to trust lossy summary"
        )

    # Boundary 2: handle does not resolve to exactly one span.
    candidates = [sid for sid in span_store if sid == handle]
    if len(candidates) != 1:
        raise SummaryProvenanceError(
            f"handle {handle!r} resolved to {len(candidates)} spans; "
            f"ambiguous provenance, fail closed"
        )

    span = span_store[handle]
    literal = span.get("literal_value")
    if literal is None:
        raise SummaryProvenanceError(
            f"span {handle!r} has no extractable literal value; "
            f"cannot verify claim {claim['text']!r}"
        )

    # Boundary 3: summary drift — implied value contradicts literal value.
    implied = claim.get("implied_value")
    if implied is not None and implied != literal:
        raise SummaryDriftError(
            f"summary implied {implied!r} but span {handle!r} contains "
            f"{literal!r}; summary is non-authoritative, re-read required"
        )

    return literal


# Example invocation that reproduces the turn-214 failure and blocks it:
span_store = {
    "0047": {"content": "retry.backoff_ms = 250", "literal_value": "250"},
}
lossy_claim = {
    "text": "retry backoff is configured to a few hundred milliseconds",
    "handle": "0041-0188",          # block-scoped, not claim-scoped
    "handle_scope": "block",
    "implied_value": "300",
    "authoritative": False,
}

try:
    consume_claim_for_value_exact_decision(lossy_claim, span_store)
except SummaryProvenanceError as e:
    # Correct behavior: re-read span 0047, recover literal 250,
    # write deadline from 250, not from the summary's 300.
    print(f"blocked: {e}")
