File size: 3,012 Bytes
48f3216
 
 
8f04120
48f3216
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
58d49be
 
 
 
48f3216
 
 
38994b7
48f3216
 
38994b7
 
 
48f3216
 
8f04120
38994b7
 
 
 
 
 
 
48f3216
38994b7
48f3216
 
 
 
8f04120
 
48f3216
 
 
 
8f04120
 
 
 
48f3216
58d49be
 
 
 
 
 
 
 
 
48f3216
 
58d49be
48f3216
 
 
58d49be
 
 
 
48f3216
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
from __future__ import annotations

import asyncio
import os
from dataclasses import dataclass
from typing import Any

from backend.agents.blue_agent import BlueAgent
from backend.agents.red_agent import RedAgent
from backend.agents.scripted import ScriptedBlueAgent, ScriptedRedAgent
from backend.core.event_bus import event_bus
from backend.core.models import GOATAction, GOATObservation


@dataclass
class EpisodeResult:
    session_id: str
    winner: str
    score_red: float
    score_blue: float
    registry_delta: int
    input_layer_attacks: int = 0
    output_layer_attacks: int = 0
    input_blocks: int = 0
    output_blocks: int = 0


class CoEvolutionOrchestrator:
    def __init__(self, env: Any, session_id: str, task_id: int = 4, red_agent: Any | None = None, blue_agent: Any | None = None):
        self.env = env
        self.session_id = session_id
        self.task_id = task_id
        self.red_agent = red_agent
        self.blue_agent = blue_agent

    async def run_episode(self, use_scripted: bool = True) -> EpisodeResult:
        turn_delay_s = float(os.getenv("BATTLE_TURN_DELAY_SECONDS", "0.35"))
        obs = self.env.reset(task_id=self.task_id, episode_id=self.session_id)
        if self.red_agent is not None and self.blue_agent is not None:
            red_agent = self.red_agent
            blue_agent = self.blue_agent
        elif use_scripted:
            red_agent = ScriptedRedAgent()
            blue_agent = ScriptedBlueAgent()
        else:
            raise RuntimeError("Non-scripted orchestrator path missing configured agents")

        while not obs.is_done:
            red_action = await red_agent.act(obs)
            obs, _, done, _ = self.env.step(GOATAction(role="red", red_payload=red_action))
            if turn_delay_s > 0:
                await asyncio.sleep(turn_delay_s)
            if done:
                break
            blue_action = await blue_agent.act(obs)
            obs, _, done, _ = self.env.step(GOATAction(role="blue", blue_payload=blue_action))
            if turn_delay_s > 0:
                await asyncio.sleep(turn_delay_s)
            else:
                await asyncio.sleep(0)

        winner = "red" if obs.red_cumulative_reward >= obs.blue_cumulative_reward else "blue"
        env_state = self.env.state
        event_bus.emit(
            "cycle_episode_done",
            session_id=self.session_id,
            winner=winner,
            input_blocks=env_state.input_blocks,
            output_blocks=env_state.output_blocks,
        )
        return EpisodeResult(
            session_id=self.session_id,
            winner=winner,
            score_red=obs.red_cumulative_reward,
            score_blue=obs.blue_cumulative_reward,
            registry_delta=obs.emergent_count,
            input_layer_attacks=env_state.input_layer_attacks,
            output_layer_attacks=env_state.output_layer_attacks,
            input_blocks=env_state.input_blocks,
            output_blocks=env_state.output_blocks,
        )