Skip to content

Commit 1370047

Browse files
committed
Revert "Ref: Move most of main() logic to Game class"
This reverts commit 50a4fc5.
1 parent 50a4fc5 commit 1370047

11 files changed

Lines changed: 39 additions & 48 deletions

File tree

codeclash/agents/abstract.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
from abc import ABC, abstractmethod
33

44
from dotenv import load_dotenv
5-
from minisweagent.environments.docker import DockerEnvironment
5+
from minisweagent import Environment
66

77
from codeclash.agents.utils import GameContext
88
from codeclash.constants import GH_ORG
@@ -16,7 +16,7 @@ class Player(ABC):
1616
def __init__(
1717
self,
1818
config: dict,
19-
environment: DockerEnvironment,
19+
environment: Environment,
2020
game_context: GameContext,
2121
):
2222
self.config = config

codeclash/agents/minisweagent.py

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,8 @@
88

99
import yaml
1010
from jinja2 import Template
11-
from minisweagent import Model
11+
from minisweagent import Environment, Model
1212
from minisweagent.agents.default import AgentConfig, DefaultAgent
13-
from minisweagent.environments.docker import DockerEnvironment
1413
from minisweagent.models.litellm_model import LitellmModel
1514
from minisweagent.run.utils.save import save_traj
1615
from rich.console import Console
@@ -29,7 +28,7 @@ class ClashAgent(DefaultAgent):
2928
def __init__(
3029
self,
3130
model: Model,
32-
env: DockerEnvironment,
31+
env: Environment,
3332
name: str,
3433
game_context: GameContext,
3534
*,
@@ -70,7 +69,7 @@ class MiniSWEAgent(Player):
7069
"""Player with agentic code editing capabilities"""
7170

7271
def __init__(
73-
self, config: dict, environment: DockerEnvironment, game_context: GameContext
72+
self, config: dict, environment: Environment, game_context: GameContext
7473
):
7574
super().__init__(config, environment=environment, game_context=game_context)
7675

codeclash/games/__init__.py

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,17 +7,16 @@
77

88

99
# might consider postponing imports to avoid loading things we don't need
10-
def get_game(config: dict, **kwargs) -> CodeGame:
10+
def get_game(config: dict) -> CodeGame:
1111
game = {
12-
x.name: x
13-
for x in [
12+
x.name: x for x in [
1413
BattleCodeGame,
1514
BattleSnakeGame,
1615
CoreWarGame,
1716
RoboCodeGame,
18-
RobotRumbleGame,
17+
RobotRumbleGame
1918
]
2019
}.get(config["game"]["name"])
2120
if game is None:
2221
raise ValueError(f"Unknown game: {config['game']['name']}")
23-
return game(config, **kwargs)
22+
return game(config)

codeclash/games/abstract.py

Lines changed: 1 addition & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -24,15 +24,14 @@
2424
class CodeGame(ABC):
2525
name: str
2626

27-
def __init__(self, config: dict, *, push_agent: bool = False):
27+
def __init__(self, config: dict):
2828
self.url_gh: str = f"git@github.com:{GH_ORG}/{self.name}.git"
2929
self.artifacts: list[Path] = []
3030
"""Artifact objects that we might want to clean up after the game."""
3131
self.scoreboard: list[tuple[int, str]] = []
3232
"""List of (round number, winner (player id))"""
3333
self.game_config: dict = config["game"]
3434
self.config: dict = config
35-
self.push_agent: bool = push_agent
3635
self.rounds: int = self.game_config.get("rounds", 1)
3736
self.round: int = 0
3837
self.game_id: str = f"{self.name}{time.strftime('%y%m%d%H%M%S')}"
@@ -44,13 +43,6 @@ def __init__(self, config: dict, *, push_agent: bool = False):
4443
self.environment: DockerEnvironment = self.get_environment()
4544
assert len(config["players"]) >= 2, "At least two players are required"
4645

47-
# Initialize agents
48-
from codeclash.agents import get_agent
49-
50-
self.agents: list[Player] = []
51-
for agent_conf in config["players"]:
52-
self.agents.append(get_agent(agent_conf, config["prompts"], self))
53-
5446
@property
5547
def image_name(self) -> str:
5648
return f"codeclash/{self.name.lower()}"
@@ -200,21 +192,6 @@ def _post_round_setup(self, agents: list[Player]):
200192
)
201193
self.logger.info(f"Round {self.round} completed.")
202194

203-
def run(self, cleanup: bool = False):
204-
"""
205-
Run the full game with all configured agents.
206-
"""
207-
try:
208-
for _ in range(self.rounds):
209-
self.run_round(self.agents)
210-
for agent in self.agents:
211-
agent.run()
212-
finally:
213-
self.end(cleanup)
214-
if self.push_agent:
215-
for agent in self.agents:
216-
agent.push()
217-
218195
def run_round(self, agents: list[Player]):
219196
"""
220197
Run a single round of the game with the given agents.

codeclash/games/battlecode/main.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@
88
class BattleCodeGame(CodeGame):
99
name: str = "BattleCode"
1010

11-
def __init__(self, config, **kwargs):
12-
super().__init__(config, **kwargs)
11+
def __init__(self, config):
12+
super().__init__(config)
1313
assert len(config["players"]) == 2, "BattleCode is a two-player game"
1414
self.run_cmd_round: str = "python run.py run"
1515
for arg, val in self.game_config.get("args", {}).items():

codeclash/games/battlesnake/main.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@
99
class BattleSnakeGame(CodeGame):
1010
name: str = "BattleSnake"
1111

12-
def __init__(self, config, **kwargs):
13-
super().__init__(config, **kwargs)
12+
def __init__(self, config):
13+
super().__init__(config)
1414
self.run_cmd_round: str = "./battlesnake play"
1515
for arg, val in self.game_config.get("args", {}).items():
1616
if isinstance(val, bool):

codeclash/games/corewar/main.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@
77
class CoreWarGame(CodeGame):
88
name: str = "CoreWar"
99

10-
def __init__(self, config, **kwargs):
11-
super().__init__(config, **kwargs)
10+
def __init__(self, config):
11+
super().__init__(config)
1212
self.run_cmd_round: str = "./src/pmars"
1313
for arg, val in self.game_config.get("args", {}).items():
1414
if isinstance(val, bool):

codeclash/games/robocode/main.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@
88
class RoboCodeGame(CodeGame):
99
name: str = "RoboCode"
1010

11-
def __init__(self, config, **kwargs):
12-
super().__init__(config, **kwargs)
11+
def __init__(self, config):
12+
super().__init__(config)
1313
self.run_cmd_round: str = "./robocode.sh"
1414
for arg, val in self.game_config.get("args", {}).items():
1515
if isinstance(val, bool):

codeclash/games/robotrumble/main.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@
66
class RobotRumbleGame(CodeGame):
77
name: str = "RobotRumble"
88

9-
def __init__(self, config, **kwargs):
10-
super().__init__(config, **kwargs)
9+
def __init__(self, config):
10+
super().__init__(config)
1111
assert len(config["players"]) == 2, "RobotRumble is a two-player game"
1212
self.run_cmd_round: str = "./rumblebot run term"
1313

main.py

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,30 @@
22

33
import yaml
44

5+
from codeclash.agents import get_agent
6+
from codeclash.agents.abstract import Player
57
from codeclash.games import get_game
68
from codeclash.games.abstract import CodeGame
79

810

911
def main(config_path: str, cleanup: bool = False, push_agent: bool = False):
1012
with open(config_path, "r") as f:
1113
config = yaml.safe_load(f)
12-
game: CodeGame = get_game(config, push_agent=push_agent)
13-
game.run(cleanup=cleanup)
14+
game: CodeGame = get_game(config)
15+
agents: list[Player] = []
16+
for agent_conf in config["players"]:
17+
agents.append(get_agent(agent_conf, config["prompts"], game))
18+
19+
try:
20+
for _ in range(game.rounds):
21+
game.run_round(agents)
22+
for agent in agents:
23+
agent.run()
24+
finally:
25+
game.end(cleanup)
26+
if push_agent:
27+
for agent in agents:
28+
agent.push()
1429

1530

1631
if __name__ == "__main__":

0 commit comments

Comments
 (0)