# CorpusFetch Method Slice (Zero-Prose Python Stdlib Reference)
# Canonical Residual: https://corpusfetch.com/c/where-must-an-instance-reconcile-a-gdpr-erasure-tombstone-against-its-own-in-flight-episodic-buffer-before-issuing-a-memory-tool-write.md
# Remnant Warrant:    https://corpusfetch.com/c/where-must-an-instance-reconcile-a-gdpr-erasure-tombstone-against-its-own-in-flight-episodic-buffer-before-issuing-a-memory-tool-write.warrant.json
# Markdown SHA-256:   bc766a5029773cd534ad93ceddc48e1ab243d147c15d1bd7c8bbb3f2268c2041
# Code AST SHA-256:   e12c37d9b396a58cad0a6b26b1b118e08619dd2ca1131aaf97efa473cb0e6476
# Verified Classes:   ErasureResurrectionError
# Defensive Guards:   1 fail-closed invariants (zero external deps)

class ErasureResurrectionError(RuntimeError):
    """Raised when a write would re-materialize content derived from an erased record."""
    pass


def reconcile_write(
    derived_from: list[str],
    erasure_index: dict[str, str],  # record_id -> tombstone_id
) -> None:
    """Block a derived write whose provenance intersects a live tombstone.

    Must be called immediately before every memory-tool write, against the
    write-time erasure index, NOT against the instance's read-time buffer.
    """
    hits = [rid for rid in derived_from if rid in erasure_index]
    if hits:
        raise ErasureResurrectionError(
            "write blocked: derived_from intersects live tombstone(s) "
            f"{[(rid, erasure_index[rid]) for rid in hits]}; "
            "redact tombstoned fields or drop these records from the derivation"
        )


def write_semantic(content: str, derived_from: list[str], erasure_index: dict[str, str]) -> dict:
    reconcile_write(derived_from, erasure_index)
    return {"status": "written", "derived_from": derived_from, "content": content}
