Skip to content

Commit bba4174

Browse files
committed
Add minor validation to HuskyBench
1 parent 79d8d17 commit bba4174

5 files changed

Lines changed: 38 additions & 22 deletions

File tree

codeclash/games/battlecode/battlecode.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -62,10 +62,10 @@ def get_results(self, agents: list[Player], round_num: int, stats: RoundStats):
6262

6363
def validate_code(self, agent: Player) -> tuple[bool, str | None]:
6464
if BC_FOLDER not in agent.environment.execute("ls src")["output"]:
65-
return False, f"`{BC_FOLDER}` directory not found in `src/`"
65+
return False, f"There should be a `src/{BC_FOLDER}/` directory"
6666
if "bot.py" not in agent.environment.execute(f"ls src/{BC_FOLDER}")["output"]:
67-
return False, "`bot.py` not found in `src/mysubmission/`"
67+
return False, f"There should be a `src/{BC_FOLDER}/bot.py` file"
6868
bot_content = agent.environment.execute(f"cat src/{BC_FOLDER}/bot.py")["output"].splitlines()
6969
if "def turn():" not in bot_content:
70-
return False, "`turn()` function not found in `bot.py`"
70+
return False, f"There should be a `turn()` function implemented in `src/{BC_FOLDER}/bot.py`"
7171
return True, None

codeclash/games/corewar/corewar.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,5 +68,5 @@ def get_results(self, agents: list[Player], round_num: int, stats: RoundStats):
6868

6969
def validate_code(self, agent: Player) -> tuple[bool, str | None]:
7070
if COREWAR_FILE not in agent.environment.execute("ls warriors")["output"]:
71-
return False, f"`{COREWAR_FILE}` not found in `warriors/`"
71+
return False, f"There should be a `warriors/{COREWAR_FILE}` file"
7272
return True, None

codeclash/games/huskybench/huskybench.py

Lines changed: 30 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,9 @@
66
from codeclash.utils.environment import create_file_in_container
77

88
HB_LOG_ENGINE = "engine.log"
9+
HB_PORT = 8000
910
HB_REGEX_SCORE = re.compile(r"Player\s(\d+)\sdelta\supdated\:[\d\s\-\+\=]+,\smoney\:\s\d+\s\-\>\s(\d+)")
11+
HB_SCRIPT = "run_game.sh"
1012

1113

1214
class HuskyBenchGame(CodeGame):
@@ -16,7 +18,7 @@ def __init__(self, config, *, tournament_id: str, local_output_dir: Path):
1618
super().__init__(config, tournament_id=tournament_id, local_output_dir=local_output_dir)
1719
self.num_players: int = len(config["players"])
1820
self.run_cmd_round: str = (
19-
f"python engine/main.py --port 8000 --players {self.num_players} "
21+
f"python engine/main.py --port {HB_PORT} --players {self.num_players} "
2022
f"--sim --sim-rounds {self.game_config['sims_per_round']}"
2123
)
2224
for arg, val in self.game_config.get("args", {}).items():
@@ -26,24 +28,34 @@ def __init__(self, config, *, tournament_id: str, local_output_dir: Path):
2628
else:
2729
self.run_cmd_round += f" --{arg} {val}"
2830

29-
def execute_round(self, agents: list[Player]):
30-
cmd = f"{self.run_cmd_round} > {self.log_env / HB_LOG_ENGINE} 2>&1 &"
31-
self.logger.debug(f"Starting game engine with command: {cmd}")
32-
# Remove previous outputs, kill previous game if any, start engine, start server
33-
script = ["rm -rf /app/output/*", "kill -9 $(lsof -ti :8000)", cmd, "sleep 0.5"]
31+
def _construct_game_script(self, agents: list[Player], run_cmd_round: str, verbose: bool = False) -> None:
32+
if verbose:
33+
self.logger.debug(f"Starting game engine with command: {run_cmd_round}")
34+
script = [
35+
"!/bin/bash",
36+
"rm -rf /app/output/*", # Remove previous outputs
37+
f"kill -9 $(lsof -ti :{HB_PORT})", # Kill previous game if any
38+
run_cmd_round, # Start engine
39+
"sleep 0.5", # Give engine a moment to start
40+
]
3441
for agent in agents:
3542
# Start each agent in background, redirecting output to log file
36-
cmd = f"cd /{agent.name} && python client/main.py --port 8000 > {self.log_env / f'{agent.name}.log'} 2>&1 &"
37-
self.logger.info(f"Adding player {agent.name} with command: {cmd}")
43+
cmd = (
44+
f"cd /{agent.name} && python client/main.py --port {HB_PORT} "
45+
f"> {self.log_env / f'{agent.name}.log'} 2>&1 &"
46+
)
47+
if verbose:
48+
self.logger.debug(f"Starting agent {agent.name} with command: {cmd}")
3849
script.append(cmd)
3950
script.append("wait")
4051
script.append(f"mv /app/output/* {self.log_env}") # Move logs to log directory
41-
create_file_in_container(
42-
container=self.environment, content="\n".join(script), dest_path="/testbed/run_game.sh"
43-
)
52+
return "\n".join(script)
4453

45-
# Run game
46-
self.environment.execute("chmod +x run_game.sh; ./run_game.sh")
54+
def execute_round(self, agents: list[Player]):
55+
cmd = f"{self.run_cmd_round} > {self.log_env / HB_LOG_ENGINE} 2>&1 &"
56+
script = self._construct_game_script(agents, cmd, verbose=True)
57+
create_file_in_container(container=self.environment, content=script, dest_path=f"/testbed/{HB_SCRIPT}")
58+
self.environment.execute(f"chmod +x {HB_SCRIPT}; ./{HB_SCRIPT}")
4759

4860
def get_results(self, agents: list[Player], round_num: int, stats: RoundStats):
4961
map_id_to_agent = {}
@@ -69,5 +81,9 @@ def get_results(self, agents: list[Player], round_num: int, stats: RoundStats):
6981
stats.player_stats[player].score = score
7082

7183
def validate_code(self, agent: Player) -> tuple[bool, str | None]:
72-
# TODO: implement more checks
84+
assets = agent.environment.execute("ls client")["output"]
85+
if "main.py" not in assets:
86+
return False, "There should be a `client/main.py` file"
87+
if "player.py" not in assets:
88+
return False, "There should be a `client/player.py` file"
7389
return True, None

codeclash/games/robocode/robocode.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -108,12 +108,12 @@ def get_results(self, agents: list[Player], round_num: int, stats: RoundStats):
108108

109109
def validate_code(self, agent: Player) -> tuple[bool, str | None]:
110110
if "robots" not in agent.environment.execute("ls")["output"]:
111-
return False, "`robots/` directory not found in submission root"
111+
return False, "There should be a `robots/` directory"
112112
if "custom" not in agent.environment.execute("ls robots")["output"]:
113-
return False, "`robots/custom/` directory not found"
113+
return False, "There should be a `robots/custom/` directory"
114114
if str(RC_FILE) not in agent.environment.execute("ls robots/custom")["output"]:
115115
return False, (
116-
f"`{RC_FILE}` not found in `robots/custom/`. "
116+
f"There should be a `robots/custom/{RC_FILE}` file. "
117117
f"You can include additional files, but the primary tank logic must be in `robots/custom/{RC_FILE}`"
118118
)
119119
response = agent.environment.execute('javac -cp "libs/robocode.jar" robots/custom/*.java')

codeclash/games/robotrumble/robotrumble.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ def get_results(self, agents: list[Player], round_num: int, stats: RoundStats):
6363

6464
def validate_code(self, agent: Player) -> tuple[bool, str | None]:
6565
if "robot.py" not in agent.environment.execute("ls")["output"]:
66-
return False, "robot.py not found in the root directory"
66+
return False, "There should be a `robot.py` file"
6767
if "def robot(state, unit):" not in agent.environment.execute("cat robot.py")["output"]:
6868
return (
6969
False,

0 commit comments

Comments
 (0)