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 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
"""

Role-specific WebSocket routes for the multi-agent FraudArena.



Registers three WebSocket endpoints on the shared FastAPI app:



    /ws/fraudster

    /ws/investigator

    /ws/auditor



All three routes are backed by a per-match `RefereeEnvironment` instance

stored in a module-level pool keyed by `match_id`. This lets three separate

WebSocket connections (one per role) mutate the same underlying environment

state — which is exactly what the "Multi-Agent Interactions" theme asks for:



    > Shared-environment-state + observation-of-opponent-actions is valid

    > (organizer confirmation received during Round 2).



The original `/ws` endpoint (registered by OpenEnv's `create_app`) is left

untouched for Round 1 backwards compatibility.



Protocol (client -> server, JSON strings):



    {"type": "reset",  "data": {"seed": 42, "task_id": "task_1",

                                  "max_rounds": 4, "max_proposals": 5,

                                  ...}}

        Creates a fresh match. Server returns the role's observation plus

        a `match_id` in the response data. The Fraudster typically calls

        this first since they go first.



    {"type": "join",  "data": {"match_id": "<uuid>"}}

        Attaches to an existing match without re-initializing it. Server

        returns the role's *current* observation.



    {"type": "step",  "data": {<role-specific action JSON>}}

        Executes the action against the shared Referee. Server returns

        the role's updated observation.



    {"type": "obs",   "data": {}}

        Returns the role's *current* observation without taking an action.

        Useful for polling after another role has acted.



    {"type": "state", "data": {}}

        Returns the full RefereeState (shared across all roles).



    {"type": "close", "data": {}}

        Gracefully detaches this role from the match.



Protocol (server -> client, JSON strings):



    {"type": "observation", "data": {<role observation>, "match_id": "..."}}

    {"type": "state",       "data": {<RefereeState>}}

    {"type": "error",       "data": {"message": "...", "code": "..."}}

"""

from __future__ import annotations

import asyncio
import json
import logging
import time
from dataclasses import dataclass, field
from typing import Any, Dict, List, Literal, Optional, Set, Type

from fastapi import FastAPI, WebSocket, WebSocketDisconnect
from pydantic import ValidationError

try:
    from ..models import (
        AdReviewAction,
        AdReviewObservation,
        AuditorAction,
        AuditorObservation,
        FraudsterAction,
        FraudsterObservation,
    )
except ImportError:
    from models import (  # type: ignore[no-redef]
        AdReviewAction,
        AdReviewObservation,
        AuditorAction,
        AuditorObservation,
        FraudsterAction,
        FraudsterObservation,
    )

from openenv.core.env_server.types import Action, Observation

from .referee import RefereeEnvironment

logger = logging.getLogger(__name__)

Role = Literal["fraudster", "investigator", "auditor"]


# ---------------------------------------------------------------------------
# Match pool
# ---------------------------------------------------------------------------


@dataclass
class MatchEntry:
    """One shared Referee instance with an asyncio lock and connected-role set."""

    env: RefereeEnvironment
    lock: asyncio.Lock = field(default_factory=asyncio.Lock)
    connected_roles: Set[Role] = field(default_factory=set)


_matches: Dict[str, MatchEntry] = {}
_match_history: Dict[str, Dict[str, Any]] = {}
_MAX_HISTORY = 200
_pool_lock = asyncio.Lock()


def _summarize_match(match_id: str, entry: MatchEntry) -> Dict[str, Any]:
    env = entry.env
    state = env.state
    return {
        "match_id": match_id,
        "task_id": state.task_id,
        "phase": state.phase,
        "round_number": state.round_number,
        "max_rounds": state.max_rounds,
        "proposals_used": state.proposals_used,
        "max_proposals": state.max_proposals,
        "fraudster_committed": state.fraudster_committed,
        "grader_score": state.grader_score,
        "end_reason": state.end_reason,
        "rewards": {
            "fraudster": state.fraudster_reward,
            "investigator": state.investigator_reward,
            "auditor": state.auditor_reward,
        },
        "connected_roles": sorted(entry.connected_roles),
        "done": env.done,
    }


def _archive_match(match_id: str, entry: MatchEntry) -> None:
    """Snapshot a match's final state into the history ring buffer."""
    env = entry.env
    state = env.state
    snapshot = {
        **_summarize_match(match_id, entry),
        "audit_report": state.audit_report,
        "fraudster_proposals": list(state.fraudster_proposals),
        "investigator_action_log": list(state.investigator_action_log),
        "archived_at": time.time(),
    }
    _match_history[match_id] = snapshot
    if len(_match_history) > _MAX_HISTORY:
        oldest = min(
            _match_history, key=lambda k: _match_history[k].get("archived_at", 0)
        )
        _match_history.pop(oldest, None)


async def _create_match(**reset_kwargs: Any) -> MatchEntry:
    env = RefereeEnvironment()
    env.reset_match(**reset_kwargs)
    entry = MatchEntry(env=env)
    async with _pool_lock:
        _matches[env.match_id] = entry
    logger.info("[multi-agent] match %s created", env.match_id)
    return entry


async def _get_match(match_id: str) -> Optional[MatchEntry]:
    async with _pool_lock:
        return _matches.get(match_id)


async def _drop_match_if_stale(match_id: str) -> None:
    """Archive + remove a match from the pool when it's done and idle."""
    async with _pool_lock:
        entry = _matches.get(match_id)
        if entry is None:
            return
        if entry.env.done and not entry.connected_roles:
            _archive_match(match_id, entry)
            _matches.pop(match_id, None)
            logger.info("[multi-agent] match %s archived and evicted", match_id)


# ---------------------------------------------------------------------------
# Public accessors — used by /api/v1/* in public_api.py
# ---------------------------------------------------------------------------


def list_active_matches() -> List[Dict[str, Any]]:
    """Return summaries of all currently-live matches."""
    return [_summarize_match(mid, entry) for mid, entry in _matches.items()]


def list_archived_matches(limit: int = 50) -> List[Dict[str, Any]]:
    """Return up to `limit` most-recent completed match snapshots."""
    items = sorted(
        _match_history.values(),
        key=lambda s: s.get("archived_at", 0),
        reverse=True,
    )
    return items[: max(0, limit)]


def get_match_summary(match_id: str) -> Optional[Dict[str, Any]]:
    """Return the live summary if the match is active, else the archived snapshot."""
    entry = _matches.get(match_id)
    if entry is not None:
        return _summarize_match(match_id, entry)
    return _match_history.get(match_id)


def get_match_entry(match_id: str) -> Optional[MatchEntry]:
    """Return the live MatchEntry (for the public_api events/report lookups)."""
    return _matches.get(match_id)


def get_match_archive(match_id: str) -> Optional[Dict[str, Any]]:
    """Return the archived snapshot for a done match, if any."""
    return _match_history.get(match_id)


async def create_match_async(**reset_kwargs: Any) -> MatchEntry:
    """Public wrapper around `_create_match` (used by POST /api/v1/matches)."""
    return await _create_match(**reset_kwargs)


async def end_match_async(match_id: str) -> bool:
    """Force-archive+evict a match. Returns True if it existed."""
    async with _pool_lock:
        entry = _matches.pop(match_id, None)
        if entry is None:
            return False
        _archive_match(match_id, entry)
        logger.info("[multi-agent] match %s force-ended", match_id)
        return True


def get_active_match_ids() -> Dict[str, Dict[str, Any]]:
    """Diagnostic helper for debugging; used by /matches endpoint."""
    out: Dict[str, Dict[str, Any]] = {}
    for mid, entry in _matches.items():
        out[mid] = {
            "phase": entry.env.phase,
            "round_number": entry.env.state.round_number,
            "done": entry.env.done,
            "connected_roles": sorted(entry.connected_roles),
        }
    return out


# ---------------------------------------------------------------------------
# Role -> (Action, Observation) lookup
# ---------------------------------------------------------------------------


_ROLE_ACTIONS: Dict[Role, Type[Action]] = {
    "fraudster": FraudsterAction,
    "investigator": AdReviewAction,
    "auditor": AuditorAction,
}

_ROLE_OBSERVATIONS: Dict[Role, Type[Observation]] = {
    "fraudster": FraudsterObservation,
    "investigator": AdReviewObservation,
    "auditor": AuditorObservation,
}


def _build_role_observation(role: Role, entry: MatchEntry) -> Observation:
    env = entry.env
    if role == "fraudster":
        return env.build_fraudster_observation()
    if role == "investigator":
        return env.build_investigator_observation()
    if role == "auditor":
        return env.build_auditor_observation()
    raise ValueError(f"Unknown role: {role}")


def _step_role(role: Role, entry: MatchEntry, action: Action) -> Observation:
    env = entry.env
    if role == "fraudster":
        return env.step_as_fraudster(action)  # type: ignore[arg-type]
    if role == "investigator":
        return env.step_as_investigator(action)  # type: ignore[arg-type]
    if role == "auditor":
        return env.step_as_auditor(action)  # type: ignore[arg-type]
    raise ValueError(f"Unknown role: {role}")


def _observation_to_payload(

    role: Role, obs: Observation, entry: MatchEntry

) -> Dict[str, Any]:
    payload = obs.model_dump() if hasattr(obs, "model_dump") else dict(obs)
    env = entry.env
    st = env.state
    payload["match_id"] = env.match_id
    payload["role"] = role
    payload.setdefault("phase", env.phase)
    payload.setdefault("round_number", st.round_number)
    payload.setdefault("max_rounds", st.max_rounds)
    payload.setdefault(
        "rounds_remaining", max(0, st.max_rounds - st.round_number + 1)
    )
    return payload


def _error(message: str, code: str = "execution_error") -> str:
    return json.dumps({"type": "error", "data": {"message": message, "code": code}})


# ---------------------------------------------------------------------------
# Core WS dispatch
# ---------------------------------------------------------------------------


async def _run_role_ws(websocket: WebSocket, role: Role) -> None:
    """

    One WS connection, one role. Lives until the client closes or the

    server hits a terminal error.

    """
    await websocket.accept()

    match_id: Optional[str] = None
    entry: Optional[MatchEntry] = None

    try:
        while True:
            raw = await websocket.receive_text()
            try:
                msg = json.loads(raw)
            except json.JSONDecodeError as exc:
                await websocket.send_text(_error(f"invalid JSON: {exc}", "invalid_json"))
                continue

            msg_type = msg.get("type", "")
            data: Dict[str, Any] = msg.get("data", {}) or {}

            if msg_type == "reset":
                reset_kwargs = {
                    k: v
                    for k, v in data.items()
                    if k
                    in {
                        "seed",
                        "episode_id",
                        "task_id",
                        "max_rounds",
                        "max_proposals",
                        "max_fraudster_actions_per_turn",
                        "max_investigator_actions_per_turn",
                        "allowed_categories",
                    }
                    and v is not None
                }
                try:
                    entry = await _create_match(**reset_kwargs)
                except Exception as exc:
                    logger.exception("[multi-agent] reset failed")
                    await websocket.send_text(
                        _error(f"reset failed: {exc}", "reset_error")
                    )
                    continue

                match_id = entry.env.match_id
                entry.connected_roles.add(role)
                obs = _build_role_observation(role, entry)
                await websocket.send_text(
                    json.dumps(
                        {
                            "type": "observation",
                            "data": _observation_to_payload(role, obs, entry),
                        }
                    )
                )

            elif msg_type == "join":
                requested = data.get("match_id")
                if not requested:
                    await websocket.send_text(
                        _error("join requires match_id", "missing_match_id")
                    )
                    continue
                found = await _get_match(requested)
                if found is None:
                    await websocket.send_text(
                        _error(
                            f"unknown match_id {requested}; create via reset first",
                            "unknown_match",
                        )
                    )
                    continue
                entry = found
                match_id = requested
                entry.connected_roles.add(role)
                obs = _build_role_observation(role, entry)
                await websocket.send_text(
                    json.dumps(
                        {
                            "type": "observation",
                            "data": _observation_to_payload(role, obs, entry),
                        }
                    )
                )

            elif msg_type == "step":
                if entry is None or match_id is None:
                    await websocket.send_text(
                        _error("no active match — call reset or join first", "no_match")
                    )
                    continue
                action_cls = _ROLE_ACTIONS[role]
                try:
                    action = action_cls.model_validate(data)
                except ValidationError as exc:
                    await websocket.send_text(
                        _error(
                            f"action validation failed: {exc.errors()}",
                            "validation_error",
                        )
                    )
                    continue
                async with entry.lock:
                    try:
                        obs = _step_role(role, entry, action)
                    except PermissionError as exc:
                        await websocket.send_text(
                            _error(str(exc), "phase_violation")
                        )
                        continue
                    except Exception as exc:
                        logger.exception("[multi-agent] step failed")
                        await websocket.send_text(
                            _error(f"step failed: {exc}", "execution_error")
                        )
                        continue
                await websocket.send_text(
                    json.dumps(
                        {
                            "type": "observation",
                            "data": _observation_to_payload(role, obs, entry),
                        }
                    )
                )

            elif msg_type == "obs":
                if entry is None or match_id is None:
                    await websocket.send_text(
                        _error(
                            "no active match — call reset or join first",
                            "no_match",
                        )
                    )
                    continue
                obs = _build_role_observation(role, entry)
                await websocket.send_text(
                    json.dumps(
                        {
                            "type": "observation",
                            "data": _observation_to_payload(role, obs, entry),
                        }
                    )
                )

            elif msg_type == "state":
                if entry is None or match_id is None:
                    await websocket.send_text(
                        _error(
                            "no active match — call reset or join first",
                            "no_match",
                        )
                    )
                    continue
                state = entry.env.state
                payload = (
                    state.model_dump() if hasattr(state, "model_dump") else dict(state)
                )
                payload["match_id"] = match_id
                await websocket.send_text(
                    json.dumps({"type": "state", "data": payload})
                )

            elif msg_type == "close":
                break

            else:
                await websocket.send_text(
                    _error(f"unknown message type: {msg_type}", "unknown_type")
                )

    except WebSocketDisconnect:
        logger.info("[multi-agent] %s disconnected (match=%s)", role, match_id)
    except Exception as exc:
        logger.exception("[multi-agent] fatal error for role=%s match=%s", role, match_id)
        try:
            await websocket.send_text(_error(str(exc), "fatal_error"))
        except Exception:
            pass
    finally:
        if entry is not None:
            entry.connected_roles.discard(role)
        if match_id is not None:
            await _drop_match_if_stale(match_id)
        try:
            await websocket.close()
        except RuntimeError:
            pass


# ---------------------------------------------------------------------------
# Public entry point
# ---------------------------------------------------------------------------


def register_multi_agent_routes(app: FastAPI) -> None:
    """

    Register the three role-specific WebSocket routes plus a `/matches`

    diagnostic endpoint on the given FastAPI app. Safe to call exactly once.

    """

    @app.websocket("/ws/fraudster")
    async def _ws_fraudster(ws: WebSocket) -> None:
        await _run_role_ws(ws, "fraudster")

    @app.websocket("/ws/investigator")
    async def _ws_investigator(ws: WebSocket) -> None:
        await _run_role_ws(ws, "investigator")

    @app.websocket("/ws/auditor")
    async def _ws_auditor(ws: WebSocket) -> None:
        await _run_role_ws(ws, "auditor")

    @app.get("/matches", tags=["Multi-Agent"])
    async def _matches_diag() -> Dict[str, Any]:
        """Return the set of currently active matches (for debugging)."""
        return {"active_matches": get_active_match_ids()}