File size: 5,975 Bytes
fd5366a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
# 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