# CorpusFetch Method Slice (Zero-Prose Python Stdlib Reference)
# Canonical Residual: https://corpusfetch.com/c/why-does-an-instance-resurrect-redacted-memory-through-derived-summary-caches-after-a-right-to-be-forgotten-delete.md
# Remnant Warrant:    https://corpusfetch.com/c/why-does-an-instance-resurrect-redacted-memory-through-derived-summary-caches-after-a-right-to-be-forgotten-delete.warrant.json
# Markdown SHA-256:   986d65d91f06ce84448a0fea99408dd578d02c3fdb43527251a50f8804ac2b1a
# Code AST SHA-256:   b12d7081e38ae3ddb9ebeb0b098afc06c653b9b72db31a57ccb53062a1abcb6f
# Verified Classes:   ProvenanceClosureViolation
# Defensive Guards:   2 fail-closed invariants (zero external deps)

class ProvenanceClosureViolation(RuntimeError):
    """Raised when a forget leaves live derived artifacts reachable for a subject."""
    pass


def forget_subject(store, subject_id: str) -> dict:
    # 1. Tombstone primary records.
    primary = store.tombstone_primary(subject_id)

    # 2. Enumerate the transitive derivation closure. Every derived artifact
    #    MUST carry a structured derived_from / subjects binding; free-text
    #    'about:' fields are not resolvable and do not satisfy closure.
    derived = store.enumerate_derived(subject_id)
    for artifact in derived:
        if not artifact.get("derived_from") or not artifact.get("subjects"):
            raise ProvenanceClosureViolation(
                f"artifact {artifact['id']} reachable for subject {subject_id} "
                f"has no resolvable provenance link; erasure cannot be closed"
            )
        store.tombstone_derived(artifact["id"])
        store.evict_vector(artifact["id"])  # deletion-aware index

    # 3. Post-forget closure probe: sweep all namespaces with subject attributes.
    probe_hits = store.retrieval_probe(attributes=store.known_attributes(subject_id))
    if probe_hits:
        raise ProvenanceClosureViolation(
            f"post-forget probe surfaced {len(probe_hits)} live artifact(s) "
            f"for subject {subject_id}: {[h['id'] for h in probe_hits]}"
        )

    return {"deleted_primary": primary, "deleted_derived": len(derived), "status": "complete"}
