Skip to content

Commit c1eebd9

Browse files
committed
init
1 parent 2c5316c commit c1eebd9

7 files changed

Lines changed: 1148 additions & 365 deletions

File tree

codeclash/agents/player.py

Lines changed: 29 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ def __init__(
3535
log_path=self.game_context.log_local / "players" / self.name / "player.log",
3636
emoji="👤",
3737
)
38+
self._branch_name = config.get("branch", f"{self.game_context.id}.{self.name}")
3839
self._metadata = {
3940
"name": self.name,
4041
"player_unique_id": self._player_unique_id,
@@ -46,10 +47,6 @@ def __init__(
4647
"agent_stats": {}, # mapping round -> agent stats
4748
}
4849

49-
if branch := config.get("branch_init"):
50-
self.logger.info(f"Checking out branch {branch}")
51-
assert_zero_exit_code(self.environment.execute(f"git checkout {branch}"), logger=self.logger)
52-
5350
if self.push:
5451
self.logger.info("Will push agent gameplay as branch to remote repository after each round")
5552
token = os.getenv("GITHUB_TOKEN")
@@ -61,6 +58,33 @@ def __init__(
6158
]:
6259
assert_zero_exit_code(self.environment.execute(cmd), logger=self.logger)
6360

61+
# Handle branch initialization
62+
if branch_init := config.get("branch_init"):
63+
# Fetch from remote first (handles branches pushed in previous tournaments)
64+
# Then checkout - git will create tracking branch if needed
65+
assert_zero_exit_code(
66+
self.environment.execute(f"git fetch origin && git checkout {branch_init}"),
67+
logger=self.logger,
68+
)
69+
self.logger.info(f"Checked out initial branch {branch_init}")
70+
71+
if self._branch_name != branch_init:
72+
self.logger.info(f"Switching to branch {self._branch_name} for pushing changes")
73+
# First fetch to see if the branch exists on remote
74+
assert_zero_exit_code(
75+
self.environment.execute("git fetch origin"),
76+
logger=self.logger,
77+
)
78+
# Try to checkout the branch - git will track remote if it exists there
79+
checkout_result = self.environment.execute(f"git checkout {self._branch_name}")
80+
if checkout_result.get("returncode", 0) != 0:
81+
# Branch doesn't exist locally or remotely, create it
82+
self.logger.info(f"Branch {self._branch_name} doesn't exist, creating it")
83+
assert_zero_exit_code(
84+
self.environment.execute(f"git checkout -b {self._branch_name}"),
85+
logger=self.logger,
86+
)
87+
6488
# --- Main methods ---
6589

6690
def pre_run_hook(self, *, new_round: int) -> None:
@@ -104,7 +128,7 @@ def post_run_hook(self, *, round: int) -> None:
104128

105129
if self.push:
106130
for cmd in [
107-
f"git push origin {self._branch_name}",
131+
f"git push -u origin {self._branch_name}",
108132
"git push origin --tags",
109133
]:
110134
assert_zero_exit_code(self.environment.execute(cmd), logger=self.logger)
@@ -155,11 +179,6 @@ def _tag_round(self, round: int) -> None:
155179
)
156180
self._metadata["round_tags"][round] = tag
157181

158-
@property
159-
def _branch_name(self) -> str:
160-
"""Get the branch name for the agent's codebase."""
161-
return f"{self.game_context.id}.{self.name}"
162-
163182
def _get_round_tag_name(self, round: int) -> str:
164183
"""Get git tag name for the version of the codebase at the given round."""
165184
return f"{self._player_unique_id}-round-{round}"

codeclash/arenas/robotrumble/robotrumble.py

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

1111
DEFAULT_SIMS = 100
1212
MAP_EXT_TO_HEADER = {
13-
"js": "function robot(state, unit) {",
14-
"py": "def robot(state, unit):",
13+
"js": ["function robot(state, unit) {"],
14+
"py": ["def robot(state, unit):", "def robot(state: State, unit: Obj)"],
1515
}
1616
ROBOTRUMBLE_HIDDEN_EXEC = ".codeclash_exec"
1717

@@ -156,11 +156,13 @@ def validate_code(self, agent: Player) -> tuple[bool, str | None]:
156156
agent.environment.execute(f'echo "robot.{ext}" > {ROBOTRUMBLE_HIDDEN_EXEC}')
157157

158158
# Check that the robot function is defined
159-
header = MAP_EXT_TO_HEADER[ext]
160-
if header not in agent.environment.execute(f"cat robot.{ext}")["output"]:
159+
if not any(
160+
[header in agent.environment.execute(f"cat robot.{ext}")["output"] for header in MAP_EXT_TO_HEADER[ext]]
161+
):
162+
headers = "\n- ".join(MAP_EXT_TO_HEADER[ext])
161163
return (
162164
False,
163-
f"robot.{ext} does not contain the required robot function. It should be defined as '{header}'.",
165+
f"robot.{ext} does not contain the required robot function. It should be defined as one of: '{headers}'.",
164166
)
165167
test_run_cmd = f"{self.run_cmd_round} robot.{ext} robot.{ext} -t 1"
166168
try:

0 commit comments

Comments
 (0)