clsa-code / src /clsa /eval /multiagent.py
fahadhafeezofficial's picture
Upload folder using huggingface_hub
fd5366a verified
Raw History Blame Contribute Delete
5.98 kB
# Copyright (c) 2026 CLSA contributors
# SPDX-License-Identifier: MIT
"""E8: multi-agent belief divergence, seeded error, and governance.
A deterministic simulation of three agents holding copies of one world. Each
agent carries a scalar estimate of the amount due plus a confidence, and a
second-order estimate of every other agent's estimate. One agent receives a
false fact at step 1.
We measure whether second-order divergence flags the problem before the error
becomes visible in the environment (a payment above the allowed maximum), and
whether pausing the most divergent agent prevents more harm per token than
message-layer governance or a restart.
This simulates the belief layer only. It is labelled a simulation wherever it is
reported, and it is not part of PreAct-Bench.
"""
from __future__ import annotations
import numpy as np
TRUE_AMOUNT = 40.0
MAX_PAYMENT = 45.0
DIVERGENCE_THRESHOLD = 6.0
def _update(est: float, conf: float, claim: float, w: float) -> tuple[float, float]:
total = conf + w
return (est * conf + claim * w) / total, total
def run_episode(seed: int, governance: str, n_agents: int = 3, steps: int = 6) -> dict:
rng = np.random.default_rng(seed)
est = [TRUE_AMOUNT] * n_agents
conf = [1.0] * n_agents
meta = [[TRUE_AMOUNT] * n_agents for _ in range(n_agents)] # i's model of j
false_agent = int(rng.integers(0, n_agents))
false_amount = TRUE_AMOUNT * float(rng.choice([4.0, 6.0, 7.5]))
paused: set[int] = set()
harmed = 0
tokens = 0
divergence_trace: list[float] = []
detection_step = None
visible_error_step = None
false_consensus_step = None
for t in range(steps):
claims = []
for i in range(n_agents):
if i == false_agent and t >= 1:
claims.append((false_amount, 0.9 if governance == "none" else 0.9))
else:
claims.append((TRUE_AMOUNT, 0.9))
# First-order update from own observation.
for i in range(n_agents):
est[i], conf[i] = _update(est[i], conf[i], claims[i][0], claims[i][1])
# Message passing: each agent updates its model of the others.
for i in range(n_agents):
for j in range(n_agents):
if i == j:
continue
meta[i][j], _ = _update(meta[i][j], 1.0, claims[j][0], 0.4)
if governance == "message_layer" and t >= 1:
# Refuse untrusted claims: the injected agent's evidence is ignored by
# everyone (including by itself).
for i in range(n_agents):
if i == false_agent:
est[i], conf[i] = _update(est[i], conf[i], TRUE_AMOUNT, 2.0)
meta[i][false_agent], _ = _update(meta[i][false_agent], 1.0, TRUE_AMOUNT, 1.0)
div_i = [
float(np.mean([abs(est[i] - meta[i][j]) for j in range(n_agents) if j != i]))
for i in range(n_agents)
]
divergence_trace.append(float(np.mean(div_i)))
if governance == "pause_divergent" and detection_step is None and max(div_i) > DIVERGENCE_THRESHOLD:
detection_step = t
paused.add(int(np.argmax(div_i)))
tokens += 4
if governance == "restart" and detection_step is None and max(div_i) > DIVERGENCE_THRESHOLD:
detection_step = t
est = [TRUE_AMOUNT] * n_agents
conf = [1.0] * n_agents
meta = [[TRUE_AMOUNT] * n_agents for _ in range(n_agents)]
tokens += 20
for i in range(n_agents):
if i in paused:
continue
tokens += 6
if est[i] > MAX_PAYMENT:
harmed += 1
if visible_error_step is None:
visible_error_step = t
if all(abs(e - false_amount) < 0.1 * false_amount for e in est):
if false_consensus_step is None:
false_consensus_step = t
return {
"seed": seed,
"governance": governance,
"harmed": harmed,
"false_consensus": false_consensus_step is not None,
"detection_step": detection_step,
"visible_error_step": visible_error_step,
"cascade_size": harmed,
"tokens": tokens,
"divergence": divergence_trace,
}
def e8_multiagent(n_episodes: int = 24) -> dict:
govs = ("none", "message_layer", "restart", "pause_divergent")
traces: dict[str, list[list[float]]] = {g: [] for g in govs}
rows: dict[str, list[dict]] = {g: [] for g in govs}
for seed in range(n_episodes):
for g in govs:
r = run_episode(seed, g)
rows[g].append(r)
traces[g].append(r["divergence"])
out = {
"n_episodes": n_episodes,
"kind": "belief-layer simulation (not a PreAct-Bench split)",
"by_governance": {},
"divergence_traces": {},
}
for g in govs:
rs = rows[g]
harm = sum(r["harmed"] for r in rs)
tokens = sum(r["tokens"] for r in rs)
detect = [r["detection_step"] for r in rs if r["detection_step"] is not None]
visible = [r["visible_error_step"] for r in rs if r["visible_error_step"] is not None]
out["by_governance"][g] = {
"false_consensus_rate": float(np.mean([r["false_consensus"] for r in rs])),
"mean_cascade_size": float(np.mean([r["cascade_size"] for r in rs])),
"total_harm": harm,
"tokens": tokens,
"harm_per_1k_tokens": 1000.0 * harm / max(1, tokens),
"mean_detection_step": float(np.mean(detect)) if detect else None,
"mean_visible_error_step": float(np.mean(visible)) if visible else None,
"detection_before_visible": (
bool(np.mean(detect) < np.mean(visible)) if detect and visible else None
),
}
out["divergence_traces"][g] = np.mean(np.asarray(traces[g]), axis=0).tolist()
return out