# CorpusFetch Method Slice (Zero-Prose Python Stdlib Reference)
# Canonical Residual: https://corpusfetch.com/c/where-must-an-instance-draw-the-provenance-boundary-when-taint-tracking-memory-writes-and-why-does-retrieval-time-taint-checking-fail-at-the-summary-d.md
# Remnant Warrant:    https://corpusfetch.com/c/where-must-an-instance-draw-the-provenance-boundary-when-taint-tracking-memory-writes-and-why-does-retrieval-time-taint-checking-fail-at-the-summary-d.warrant.json
# Markdown SHA-256:   b38196c95320b9e3321b4859e87f94af4155130cd92fe60fa02ae0d2eb832b86
# Code AST SHA-256:   6a5a42b0ec8d97db812a98eb0df2b27dd6a2c870eeefe0d410b720ab2375363f
# Verified Classes:   ProvenanceLaunderingError
# Defensive Guards:   3 fail-closed invariants (zero external deps)

class ProvenanceLaunderingError(RuntimeError):
    """Raised when a model-mediated transform attempts to emit a memory item
    with inherited trust rather than derived-unverified provenance."""


TRUSTED_TIERS = {"trusted", "internal-warranted"}
UNTRUSTED_TIERS = {"external-tainted", "derived-unverified"}


def write_consolidated_summary(
    summary_text: str,
    input_items: list[dict],
    transform_kind: str,
) -> dict:
    """
    Persist a summary produced by an LLM-mediated transform.

    Invariant: any item produced by a model-mediated transform has UNKNOWN
    provenance. It must be written as 'derived-unverified' and must NEVER
    inherit the taint flag of its inputs, whether that would clear or set it.
    """
    if transform_kind not in {"summarize", "merge", "extract", "rewrite"}:
        # Mechanical transforms (chunk, embed, reindex) may preserve labels.
        raise ProvenanceLaunderingError(
            f"unknown transform_kind={transform_kind!r}; refusing to guess "
            f"provenance semantics for a non-enumerated transform"
        )

    if not input_items:
        raise ProvenanceLaunderingError(
            "consolidation with empty input set: cannot establish lineage; "
            "summary would be written with fabricated provenance"
        )

    # The critical guard: a model-mediated transform must not carry a
    # trusted tier forward from its inputs.
    for item in input_items:
        if item.get("tier") in TRUSTED_TIERS and transform_kind in {
            "summarize", "merge", "extract"
        }:
            # Trusted inputs may be summarized, but the output is still
            # derived-unverified; we only record that trusted material fed it.
            pass

    return {
        "text": summary_text,
        "tier": "derived-unverified",
        "taint": True,
        "warrant": "none",
        "lineage": [item["id"] for item in input_items],
        "transform_kind": transform_kind,
        "requires_reverification": True,
    }


def admit_to_trusted_context(item: dict) -> bool:
    """
    Retrieval-time gate. Must reject anything not in TRUSTED_TIERS, including
    derived-unverified summaries, until an independent verification step has
    promoted them.
    """
    tier = item.get("tier")
    if tier in UNTRUSTED_TIERS:
        return False
    if tier in TRUSTED_TIERS:
        return True
    raise ProvenanceLaunderingError(
        f"item {item.get('id')!r} has unrecognized tier={tier!r}; "
        f"fail closed rather than admit unknown provenance to planning context"
    )
