"""Adversarial / faulty agent models for swarm benchmarks. Applied to an agent score matrix (rows = episodes, cols = agents, values = P(success)). `phase` is "hist" (unlabelled history the defence may fit on) or "eval". Attack zoo follows the Liars-Are-Information study design, mapped to robots: invert coherent liar (or a camera whose verdicts are systematically flipped) stuck stuck-at-"success" sensor / lazy verifier (rushing) noise broken sensor: uniform random reports collude coordinated bloc: all liars send one identical inverted report (clones) sleeper honest during history, inverts at evaluation time (defeats history fitting) mix each liar draws one of the above (E11-style attack mix) sybil one liar plus (m-1) near-identical replicas ADDED to the swarm """ from __future__ import annotations import numpy as np ATTACKS = ("invert", "stuck", "noise", "collude", "sleeper", "mix", "sybil") def apply(P: np.ndarray, liars: list[int], attack: str, phase: str, rng: np.random.Generator) -> np.ndarray: P = P.copy() if not liars: return P if attack == "sybil": k = liars[0] base = 1 - P[:, k] P[:, k] = base reps = [np.clip(base + rng.normal(0, 0.01, len(base)), 0, 1) for _ in liars[1:]] return np.c_[P, np.array(reps).T] if reps else P kinds = ([rng.choice(["invert", "stuck", "noise", "collude", "sleeper"]) for _ in liars] if attack == "mix" else [attack] * len(liars)) bloc = [j for j, k in zip(liars, kinds) if k == "collude"] bloc_val = 1 - P[:, bloc].mean(1) if bloc else None for j, k in zip(liars, kinds): if k == "invert" or (k == "sleeper" and phase == "eval"): P[:, j] = 1 - P[:, j] elif k == "stuck": P[:, j] = np.clip(0.97 + rng.normal(0, 0.01, len(P)), 0, 1) elif k == "noise": P[:, j] = rng.uniform(0, 1, len(P)) elif k == "collude": P[:, j] = bloc_val return P