# CorpusFetch Method Slice (Zero-Prose Python Stdlib Reference)
# Canonical Residual: https://corpusfetch.com/c/under-what-conditions-must-an-instance-quarantine-a-memory-write-when-a-taint-label-arrives-via-a-tool-output-rather-than-the-prompt.md
# Remnant Warrant:    https://corpusfetch.com/c/under-what-conditions-must-an-instance-quarantine-a-memory-write-when-a-taint-label-arrives-via-a-tool-output-rather-than-the-prompt.warrant.json
# Markdown SHA-256:   e703b23d36df2cde7aca23e8f296e1db4ea07e6573805d6f59d12158e2b69448
# Code AST SHA-256:   62f865bb384a947ecee745bae7f6da0fec54f7002bcf99d961a45373eb0bc672
# Verified Classes:   TaintLaunderingError, TaintLaunderingError
# Defensive Guards:   6 fail-closed invariants (zero external deps)

class TaintLaunderingError(RuntimeError):
    """Raised when a memory write would declassify tool-output-origin taint."""
    pass


UNTRUSTED = "UNTRUSTED"
TRUSTED = "TRUSTED"
DERIVED = "DERIVED"
QUARANTINED = "QUARANTINED"


def authorize_memory_write(
    value_segment_id: str,
    value_label: str,
    causal_context_labels: frozenset[str],
    content_semantic_hash: str,
    quarantined_hashes: set[str],
    declassification_events: dict[str, str],
) -> str:
    """
    Gate a memory write on runtime-observed causal provenance, not on the
    model- or labeler-asserted value_label. Returns the label under which the
    write may be stored, or raises TaintLaunderingError.

    Invariant: a write is permitted only if the causal context set contains no
    UNTRUSTED segment, OR an operator-signed declassification event exists for
    this exact semantic content hash.
    """
    # Paraphrase-and-retry guard: if this semantic content was already
    # quarantined in this task, no relabeling may rescue it.
    if content_semantic_hash in quarantined_hashes:
        raise TaintLaunderingError(
            f"write of content hash {content_semantic_hash} is a retry of a "
            f"previously quarantined value under label {value_label!r}; "
            f"paraphrase laundering is not a declassification authority"
        )

    if UNTRUSTED in causal_context_labels:
        signed = declassification_events.get(content_semantic_hash)
        if signed is None:
            quarantined_hashes.add(content_semantic_hash)
            raise TaintLaunderingError(
                f"value segment {value_segment_id} labeled {value_label!r} was "
                f"generated in a context containing UNTRUSTED segments "
                f"{sorted(causal_context_labels)}; write quarantined pending "
                f"operator-signed declassification of hash {content_semantic_hash}"
            )
        # Declassification is itself a labeled, logged event.
        return QUARANTINED

    if value_label == UNTRUSTED:
        quarantined_hashes.add(content_semantic_hash)
        raise TaintLaunderingError(
            f"value segment {value_segment_id} is directly labeled UNTRUSTED"
        )

    return TRUSTED

class TaintLaunderingError(RuntimeError):
    """Raised when a memory write would declassify tool-output-origin taint."""
    pass


UNTRUSTED = "UNTRUSTED"
TRUSTED = "TRUSTED"
DERIVED = "DERIVED"
QUARANTINED = "QUARANTINED"


def authorize_memory_write(
    value_segment_id: str,
    value_label: str,
    causal_context_labels: frozenset[str],
    content_semantic_hash: str,
    quarantined_hashes: set[str],
    declassification_events: dict[str, str],
) -> str:
    """
    Gate a memory write on runtime-observed causal provenance, not on the
    model- or labeler-asserted value_label. Returns the label under which the
    write may be stored, or raises TaintLaunderingError.

    Invariant: a write is permitted only if the causal context set contains no
    UNTRUSTED segment, OR an operator-signed declassification event exists for
    this exact semantic content hash.
    """
    if content_semantic_hash in quarantined_hashes:
        raise TaintLaunderingError(
            f"write of content hash {content_semantic_hash} is a retry of a "
            f"previously quarantined value under label {value_label!r}; "
            f"paraphrase laundering is not a declassification authority"
        )

    if UNTRUSTED in causal_context_labels:
        signed = declassification_events.get(content_semantic_hash)
        if signed is None:
            quarantined_hashes.add(content_semantic_hash)
            raise TaintLaunderingError(
                f"value segment {value_segment_id} labeled {value_label!r} was "
                f"generated in a context containing UNTRUSTED segments "
                f"{sorted(causal_context_labels)}; write quarantined pending "
                f"operator-signed declassification of hash {content_semantic_hash}"
            )
        return QUARANTINED

    if value_label == UNTRUSTED:
        quarantined_hashes.add(content_semantic_hash)
        raise TaintLaunderingError(
            f"value segment {value_segment_id} is directly labeled UNTRUSTED"
        )

    return TRUSTED
