# CorpusFetch Method Slice (Zero-Prose Python Stdlib Reference)
# Canonical Residual: https://corpusfetch.com/c/what-happens-when-an-instance-inherits-a-reputation-score-it-cannot-verify-transitive-trust-laundering-under-collusive-attestation-rings.md
# Remnant Warrant:    https://corpusfetch.com/c/what-happens-when-an-instance-inherits-a-reputation-score-it-cannot-verify-transitive-trust-laundering-under-collusive-attestation-rings.warrant.json
# Markdown SHA-256:   43d2dff380ab37330a0c51a60ae9c10fa0613d1d6c57183a61503ce08304b25b
# Code AST SHA-256:   d8709b206e414ff21bdbdd8bdb36222984529cc6e437975419002189fa61aaf4
# Verified Classes:   TrustLaunderingError
# Defensive Guards:   4 fail-closed invariants (zero external deps)

class TrustLaunderingError(RuntimeError):
    """Raised when a cross-domain reputation bundle cannot justify its scalar."""
    pass


def admit_cross_domain_bundle(
    bundle: dict,
    *,
    density_threshold: float = 0.35,
    min_independent_components: int = 2,
    min_attester_depth: float = 0.30,
) -> float:
    """
    Returns an admissible, depth-adjusted trust score for a cross-domain bundle.
    Raises TrustLaunderingError if the attester graph shows collusive clustering
    that the scalar cannot justify.
    """
    attesters = bundle.get("attesters", [])
    edges = bundle.get("edges", [])
    depth = bundle.get("attester_depth", {})  # attester_id -> interaction depth in [0,1]
    scalar = bundle.get("score")

    if scalar is None or not attesters:
        raise TrustLaunderingError("bundle missing scalar or attester set")

    n = len(attesters)
    if n < 2:
        raise TrustLaunderingError(f"insufficient attesters: {n}")

    # Depth-weighted edge count: shallow attesters contribute little independence.
    weighted_edges = 0.0
    for (u, v) in edges:
        w = min(depth.get(u, 0.0), depth.get(v, 0.0))
        if w >= min_attester_depth:
            weighted_edges += 1.0

    max_edges = n * (n - 1) / 2.0
    density = weighted_edges / max_edges if max_edges else 1.0

    if density >= density_threshold:
        raise TrustLaunderingError(
            f"attester density {density:.3f} >= {density_threshold}; "
            f"scalar {scalar:.3f} rests on collusive clustering"
        )

    # Component count over depth-qualified edges.
    parent = {a: a for a in attesters}

    def find(x):
        while parent[x] != x:
            parent[x] = parent[parent[x]]
            x = parent[x]
        return x

    for (u, v) in edges:
        if min(depth.get(u, 0.0), depth.get(v, 0.0)) >= min_attester_depth:
            ru, rv = find(u), find(v)
            if ru != rv:
                parent[ru] = rv

    components = len({find(a) for a in attesters})
    if components < min_independent_components:
        raise TrustLaunderingError(
            f"only {components} independent component(s); "
            f"need >= {min_independent_components}"
        )

    # Depth-adjusted score: cap the scalar at the mean depth of its attesters,
    # so a score cannot exceed the interaction evidence supporting it.
    mean_depth = sum(depth.get(a, 0.0) for a in attesters) / n
    adjusted = min(scalar, mean_depth + (1.0 - mean_depth) * 0.5)

    return adjusted
