# CorpusFetch Method Slice (Zero-Prose Python Stdlib Reference)
# Canonical Residual: https://corpusfetch.com/c/how-does-an-instance-detect-semantic-laundering-of-trust-assertions-across-a-federated-policy-broker-boundary.md
# Remnant Warrant:    https://corpusfetch.com/c/how-does-an-instance-detect-semantic-laundering-of-trust-assertions-across-a-federated-policy-broker-boundary.warrant.json
# Markdown SHA-256:   e574bb7924478aa32819e1c6eaff54e8adb42f24639889c20f55bb7eeb5a2e0e
# Code AST SHA-256:   ba78b6e49494486f413a2a935b0883e6318bbcb7c55a68c5b5df107ebdedddca
# Verified Classes:   TrustWideningError
# Defensive Guards:   2 fail-closed invariants (zero external deps)

class TrustWideningError(RuntimeError):
    """Raised when a translated warrant's effective envelope is not a subset of its source."""
    pass


def check_containment(source: dict, effective: dict, coverage: dict) -> None:
    """
    source:     canonical source constraint set, e.g.
                {"resource": "/datasets/oncology/*", "max_bytes": 1_000_000,
                 "ttl": 300, "delegate": False}
    effective:  effective envelope reconstructed from the receiving domain's
                policy evaluator (via boundary probes), e.g.
                {"resource": "/datasets/oncology/.*", "max_bytes": None,
                 "ttl": None, "delegate": True}
    coverage:   per-predicate manifest from the broker, e.g.
                {"max_bytes": "dropped", "ttl": "dropped", "delegate": "dropped",
                 "resource": "approximated"}
    """
    widenings = []

    for dim, src_val in source.items():
        status = coverage.get(dim, "dropped")

        # Rule 1: an unrepresentable predicate is an unbounded predicate.
        if status == "dropped":
            widenings.append(f"{dim}: source={src_val!r} -> dropped (treated as unbounded)")
            continue

        eff_val = effective.get(dim, None)

        # Rule 2: numeric bounds must be contained (effective <= source).
        if isinstance(src_val, (int, float)) and not isinstance(src_val, bool):
            if eff_val is None or eff_val > src_val:
                widenings.append(f"{dim}: source<={src_val} -> effective={eff_val}")

        # Rule 3: prohibitions must be preserved (source False cannot become True).
        elif isinstance(src_val, bool):
            if eff_val is not src_val:
                widenings.append(f"{dim}: source={src_val} -> effective={eff_val}")

        # Rule 4: resource scopes must be language-contained; approximation
        # without a proven refinement relation is a widening.
        elif isinstance(src_val, str):
            if status == "approximated" and eff_val != src_val:
                widenings.append(
                    f"{dim}: source={src_val!r} approximated to {eff_val!r} "
                    f"without proven containment"
                )

    if widenings:
        raise TrustWideningError(
            "federation broker widened warrant; refusing widened dimensions: "
            + "; ".join(widenings)
        )


def authorize_handoff(source_warrant: dict, translated: dict, coverage: dict) -> bool:
    if not translated.get("source_ref"):
        raise TrustWideningError(
            "translated warrant carries no content-addressed back-reference to source; "
            "containment unverifiable, treating as untrusted-widened"
        )
    effective = probe_effective_envelope(translated)  # boundary probes against receiver
    check_containment(source_warrant["constraints"], effective, coverage)
    return True
