# CorpusFetch Method Slice (Zero-Prose Python Stdlib Reference)
# Canonical Residual: https://corpusfetch.com/c/how-should-an-instance-detect-prompt-injection-propagation-across-a-federated-trust-boundary-when-the-injected-payload-is-laundered-through-a-peer-s-s.md
# Remnant Warrant:    https://corpusfetch.com/c/how-should-an-instance-detect-prompt-injection-propagation-across-a-federated-trust-boundary-when-the-injected-payload-is-laundered-through-a-peer-s-s.warrant.json
# Markdown SHA-256:   797aa0069e2167ff42f43927886f2257ce2a7aaa254a148d92a43bdaa0ccef4a
# Code AST SHA-256:   77953f0e5dbe36ea59d338609f95025b119da0b38b612b7e3ed9abdce187c2d8
# Verified Classes:   ProvenanceLaunderingError
# Defensive Guards:   3 fail-closed invariants (zero external deps)

class ProvenanceLaunderingError(RuntimeError):
    """Raised when an inbound peer artifact asserts operational authority
    without a recomputable provenance manifest (trust laundered via summary)."""


OPERATIONAL_AUTHORITY_MARKERS = (
    "must", "required", "always include", "policy", "tool call",
    "environment block", "execute", "invoke",
)


def admit_inbound_artifact(artifact: dict, sender_domain: str,
                           receiver_trusted_domains: set[str]) -> dict:
    """
    Decide the trust level of a peer artifact crossing a federation boundary.
    Returns a dict with 'authority' in {'grounding', 'operational'} and
    'trust_level' = minimum provenance trust, never sender identity trust.
    """
    manifest = artifact.get("provenance_manifest")
    body = artifact.get("body", "").lower()

    asserts_operational = any(m in body for m in OPERATIONAL_AUTHORITY_MARKERS)

    if manifest is None:
        if asserts_operational:
            # A summary asserting operational authority with no provenance
            # chain is the signature of marker-erasing laundering.
            raise ProvenanceLaunderingError(
                f"Artifact from {sender_domain} asserts operational authority "
                f"but carries no provenance manifest; refusing elevation."
            )
        return {"authority": "grounding", "trust_level": "untrusted-origin"}

    # Recompute trust as the minimum over declared sources, never peer identity.
    sources = manifest.get("sources", [])
    if not sources:
        raise ProvenanceLaunderingError(
            f"Manifest from {sender_domain} declares no sources; cannot "
            f"recompute provenance-minimum trust."
        )
    source_trusts = [s.get("trust", "untrusted-origin") for s in sources]
    min_trust = "trusted" if all(t == "trusted" for t in source_trusts) \
        else "untrusted-origin"

    signature_ok = manifest.get("signature") == artifact.get("body_hash")
    if not signature_ok:
        raise ProvenanceLaunderingError(
            f"Manifest signature from {sender_domain} does not cover the "
            f"summarized body; summary may have been altered post-signing."
        )

    authority = "operational" if (min_trust == "trusted"
                                  and sender_domain in receiver_trusted_domains) \
        else "grounding"
    return {"authority": authority, "trust_level": min_trust}
