# CorpusFetch Method Slice (Zero-Prose Python Stdlib Reference)
# Canonical Residual: https://corpusfetch.com/c/what-happens-when-an-instance-quarantines-a-tainted-retrieval-chunk-but-its-derived-summaries-remain-in-the-semantic-cache.md
# Remnant Warrant:    https://corpusfetch.com/c/what-happens-when-an-instance-quarantines-a-tainted-retrieval-chunk-but-its-derived-summaries-remain-in-the-semantic-cache.warrant.json
# Markdown SHA-256:   c4d85906e565d7aeeb30d6586d81fec670a3ce5e74cf0fe61462df0df27db510
# Code AST SHA-256:   23cee575f4c76ba70b92238ecaef37502753a4feba4670aac4855ca159ef392c
# Verified Classes:   UnresolvedDerivationError, ProvenanceStore
# Defensive Guards:   6 fail-closed invariants (zero external deps)

class UnresolvedDerivationError(RuntimeError):
    """Raised when a cache artifact cannot be traced to a live, trusted source chunk."""
    pass


class ProvenanceStore:
    def __init__(self):
        self.chunks = {}          # chunk_id -> {"trust": float, "tainted": bool}
        self.artifacts = {}       # artifact_id -> {"derived_from": str | None, "text": str}

    def quarantine_chunk(self, chunk_id: str) -> None:
        if chunk_id not in self.chunks:
            raise UnresolvedDerivationError(f"quarantine target {chunk_id} not in store")
        self.chunks[chunk_id]["tainted"] = True
        self.chunks[chunk_id]["trust"] = 0.0

    def ingest_artifact(self, artifact_id: str, min_trust: float = 0.65) -> str:
        art = self.artifacts.get(artifact_id)
        if art is None:
            raise UnresolvedDerivationError(f"artifact {artifact_id} not in cache")

        source_id = art.get("derived_from")
        if source_id is None:
            # Fail closed: unknown derivation is not trusted.
            raise UnresolvedDerivationError(
                f"artifact {artifact_id} has no derived_from pointer; "
                f"refusing ingest (fail-closed gate)"
            )

        source = self.chunks.get(source_id)
        if source is None:
            raise UnresolvedDerivationError(
                f"artifact {artifact_id} points at missing chunk {source_id}"
            )

        if source["tainted"]:
            raise UnresolvedDerivationError(
                f"artifact {artifact_id} derives from quarantined chunk {source_id}; "
                f"content reachable through summary path"
            )

        if source["trust"] < min_trust:
            raise UnresolvedDerivationError(
                f"artifact {artifact_id} source trust {source['trust']:.2f} "
                f"below gate {min_trust:.2f}"
            )

        return art["text"]


# Reproduction of the observed failure:
store = ProvenanceStore()
store.chunks["c_4471"] = {"trust": 0.71, "tainted": False}
store.artifacts["s_4471"] = {"derived_from": None, "text": "distilled claims..."}  # writer dropped the pointer
store.quarantine_chunk("c_4471")

try:
    store.ingest_artifact("s_4471")
except UnresolvedDerivationError as e:
    # Fail-closed gate catches the pointerless summary; the pre-fix
    # pipeline coerced missing trust to 0.8 and ingested it.
    print(f"blocked: {e}")
