# CorpusFetch Method Slice (Zero-Prose Python Stdlib Reference)
# Canonical Residual: https://corpusfetch.com/c/which-invariants-prevent-reputation-laundering-through-transitive-attestation-chains-in-federated-agent-orchestration.md
# Remnant Warrant:    https://corpusfetch.com/c/which-invariants-prevent-reputation-laundering-through-transitive-attestation-chains-in-federated-agent-orchestration.warrant.json
# Markdown SHA-256:   970d6c4dbea7f340d0267cccc1bdf4dd1ea5455d24371d4de55768aa9ebf7b39
# Code AST SHA-256:   dc0b41f3bbb87e44a4b7e8c13658f36b713c26bea8c4c6629e58bb35903b5369
# Verified Classes:   TrustPropagationError
# Defensive Guards:   4 fail-closed invariants (zero external deps)

class TrustPropagationError(RuntimeError):
    """Raised when an attestation-resolved score violates path-soundness invariants."""
    pass


def resolve_reputation(
    agent_id: str,
    attestation_graph: dict,   # {issuer: {subject: (weight, identity_bound)}}
    identity_bindings: dict,   # {raw_key: canonical_identity}
    root_scores: dict,         # {canonical_identity: first_party_score}
    decay: float,              # lambda, must be < 1.0
    max_depth: int,
    admission_threshold: float,
) -> tuple[float, list[str]]:
    if not (0.0 < decay < 1.0):
        raise TrustPropagationError(
            f"decay must be in (0,1); got {decay}. Without decay, chain length "
            f"amplifies trust and the invariant is unenforceable."
        )

    canonical_target = identity_bindings.get(agent_id, agent_id)

    # DFS over the identity closure, tracking path and accumulated attenuation.
    best_score = 0.0
    best_path: list[str] = []

    def walk(node: str, path: list[str], attenuation: float) -> None:
        nonlocal best_score, best_path
        canonical_node = identity_bindings.get(node, node)

        if canonical_node in path:
            raise TrustPropagationError(
                f"identity-closure cycle detected: {' -> '.join(path + [canonical_node])}. "
                f"Sybil re-entry via fresh key is the laundering vector; "
                f"bind identities before resolving."
            )
        if len(path) > max_depth:
            raise TrustPropagationError(
                f"global path depth {len(path)} exceeds max_attestation_depth "
                f"{max_depth}; domain boundaries must not reset the counter."
            )

        if canonical_node in root_scores:
            score = root_scores[canonical_node] * attenuation
            if score > best_score:
                best_score, best_path = score, path + [canonical_node]

        for subject, (weight, bound) in attestation_graph.get(canonical_node, {}).items():
            if not bound:
                # Unbound keys are distinct roots with score 0, not inheritors.
                continue
            walk(subject, path + [canonical_node], attenuation * weight * decay)

    walk(canonical_target, [], 1.0)

    if best_score >= admission_threshold and len(best_path) > 1:
        # Re-verify monotonicity: the winning path must not have gained score
        # relative to its shortest prefix.
        prefix_attenuation = 1.0
        for hop in best_path[1:]:
            prefix_attenuation *= decay
            # (weights folded into best_score already; this guards against
            #  resolver bugs that drop attenuation on re-entry)
        if prefix_attenuation >= 1.0:
            raise TrustPropagationError(
                f"path {best_path} shows non-decreasing attenuation; "
                f"reputation laundering invariant violated."
            )

    return best_score, best_path
