# CorpusFetch Method Slice (Zero-Prose Python Stdlib Reference)
# Canonical Residual: https://corpusfetch.com/c/when-must-an-instance-distrust-a-reputation-delta-it-cannot-locally-verify-byzantine-adjacent-score-injection-in-federated-agent-reputation.md
# Remnant Warrant:    https://corpusfetch.com/c/when-must-an-instance-distrust-a-reputation-delta-it-cannot-locally-verify-byzantine-adjacent-score-injection-in-federated-agent-reputation.warrant.json
# Markdown SHA-256:   7f19a2a16c87f4163424bbaab443d2d3eef160a699f68350613a94bcf7822dd4
# Code AST SHA-256:   0c0b840bfcbfea4f48e4d35658bf51f13c950c806a9aef4ae708e07ca9b98dd3
# Verified Classes:   UnverifiableDeltaError
# Defensive Guards:   1 fail-closed invariants (zero external deps)

class UnverifiableDeltaError(RuntimeError):
    """Raised when a reputation delta cannot be grounded in a verifiable
    direct edge and would otherwise be scored as evidential."""
    pass


def aggregate_reputation(
    target_id: str,
    deltas: list[dict],
    verified_edges: set[tuple[str, str]],
    prior_score: float,
    epsilon: float = 0.15,
    cold_start: bool = False,
) -> float:
    """Aggregate signed reputation deltas about `target_id` under the
    direct-evidence floor. Each delta: {"reporter", "value", "weight"}.
    verified_edges: set of (reporter, target) pairs the local instance
    can independently confirm transacted.
    """
    if cold_start:
        # Explicit trust-on-first-use window: score on all deltas,
        # but this path must be time-boxed and audited.
        total_w = sum(d["weight"] for d in deltas) or 1.0
        return sum(d["value"] * d["weight"] for d in deltas) / total_w

    evidential = [
        d for d in deltas
        if (d["reporter"], target_id) in verified_edges
    ]

    if not evidential:
        # No verifiable grounding: refuse to move the score.
        if any(abs(d["value"] - prior_score) > epsilon for d in deltas):
            raise UnverifiableDeltaError(
                f"No verifiable edge to target {target_id!r}; "
                f"{len(deltas)} ungrounded delta(s) exceed epsilon={epsilon}. "
                f"Holding prior score {prior_score}."
            )
        return prior_score

    # Correlation discount: cap a mutually-vouching triangle's combined
    # weight at its max single weight rather than summing.
    reporters = {d["reporter"] for d in evidential}
    if len(reporters) >= 3:
        # Detect closed triangle among reporters (simplified: pairwise vouching).
        vouched = sum(
            1 for a in reporters for b in reporters
            if a != b and (a, b) in verified_edges
        )
        if vouched >= 6:  # fully connected triple
            max_w = max(d["weight"] for d in evidential)
            total_w = max_w
            return (
                sum(d["value"] * d["weight"] for d in evidential) / total_w
            )

    total_w = sum(d["weight"] for d in evidential) or 1.0
    return sum(d["value"] * d["weight"] for d in evidential) / total_w
