Skip to content

Commit 2cd8b37

Browse files
committed
assert_zero_exit_code helper function
1 parent eaf86b2 commit 2cd8b37

4 files changed

Lines changed: 22 additions & 14 deletions

File tree

codeclash/agents/abstract.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
from minisweagent import Environment
77

88
from codeclash.constants import GH_ORG
9+
from codeclash.utils.environment import assert_zero_exit_code
910

1011
load_dotenv()
1112

@@ -30,7 +31,7 @@ def commit(self):
3031
"git add -A",
3132
f"git commit --allow-empty -m 'Round {self.round}/{rounds} Update'",
3233
]:
33-
self.environment.execute(cmd)
34+
assert_zero_exit_code(self.environment.execute(cmd))
3435
print(f"Committed changes for {self.name} for round {self.round}/{rounds}")
3536

3637
def push(self):
@@ -46,7 +47,7 @@ def push(self):
4647
f"git remote add origin https://x-access-token:{token}@github.com/{GH_ORG}/{self.name}.git",
4748
"git push -u origin main",
4849
]:
49-
self.environment.execute(cmd)
50+
assert_zero_exit_code(self.environment.execute(cmd))
5051

5152
@abstractmethod
5253
def run(self):

codeclash/games/abstract.py

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010

1111
from codeclash.constants import DIR_LOGS, DIR_WORK, GH_ORG
1212
from codeclash.games.utils import copy_between_containers
13+
from codeclash.utils.environment import assert_zero_exit_code
1314

1415

1516
class CodeGame(ABC):
@@ -86,10 +87,7 @@ def get_environment(self) -> DockerEnvironment:
8687
"git add -A",
8788
"git commit -m 'init'",
8889
]:
89-
out = environment.execute(cmd)
90-
if out.get("returncode", 0) != 0:
91-
msg = f"Failed to execute command: {cmd}. Output so far:\n{out.get('output')}"
92-
raise RuntimeError(msg)
90+
assert_zero_exit_code(environment.execute(cmd))
9391
return environment
9492

9593
def _pre_round_setup(self, agents: list[Any]):
@@ -107,8 +105,8 @@ def _pre_round_setup(self, agents: list[Any]):
107105
)
108106

109107
# Ensure the log path + file exists
110-
self.environment.execute(f"mkdir -p {self.log_path}")
111-
self.environment.execute(f"touch {self.round_log_path}")
108+
assert_zero_exit_code(self.environment.execute(f"mkdir -p {self.log_path}"))
109+
assert_zero_exit_code(self.environment.execute(f"touch {self.round_log_path}"))
112110

113111
@abstractmethod
114112
def determine_winner(self, agents: list[Any]) -> Any:

codeclash/games/battlesnake/main.py

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
from typing import Any
44

55
from codeclash.games.abstract import CodeGame
6+
from codeclash.utils.environment import assert_zero_exit_code
67

78

89
class BattleSnakeGame(CodeGame):
@@ -19,7 +20,9 @@ def __init__(self, config):
1920
self.run_cmd_round += f" --{arg} {val}"
2021

2122
def determine_winner(self, agents: list[Any]):
22-
response = self.environment.execute(f"tail -1 {self.round_log_path}")
23+
response = assert_zero_exit_code(
24+
self.environment.execute(f"tail -1 {self.round_log_path}")
25+
)
2326
winner = json.loads(response["output"].strip("\n"))["winnerName"]
2427
self.scoreboard.append((self.round, winner))
2528

@@ -39,12 +42,14 @@ def execute_round(self, agents: list[Any]):
3942
cmd = " ".join(cmd)
4043
print(f"Running command: {cmd}")
4144

45+
# todo: should probably keep output somewhere?
4246
try:
43-
response = self.environment.execute(
44-
f"{self.run_cmd_round} {cmd}",
45-
cwd=f"{self.environment.config.cwd}/game",
47+
assert_zero_exit_code(
48+
self.environment.execute(
49+
f"{self.run_cmd_round} {cmd}",
50+
cwd=f"{self.environment.config.cwd}/game",
51+
)
4652
)
47-
assert response["returncode"] == 0, response
4853
finally:
4954
# Kill all python servers when done
5055
self.environment.execute("pkill -f 'python main.py' || true")

codeclash/games/utils.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44

55
from minisweagent.environments.docker import DockerEnvironment
66

7+
from codeclash.utils.environment import assert_zero_exit_code
8+
79

810
def copy_between_containers(
911
src_container: DockerEnvironment,
@@ -33,7 +35,9 @@ def copy_between_containers(
3335
)
3436

3537
# Ensure destination folder exists
36-
dest_container.execute(f"mkdir -p {Path(dest_path).parent}")
38+
assert_zero_exit_code(
39+
dest_container.execute(f"mkdir -p {Path(dest_path).parent}")
40+
)
3741

3842
# Copy from temporary local directory to destination container
3943
cmd_dest = [

0 commit comments

Comments
 (0)