Skip to content

Commit 4160ee4

Browse files
committed
Push to branch every commit, instead of all at the end
1 parent eb6eecb commit 4160ee4

3 files changed

Lines changed: 25 additions & 24 deletions

File tree

codeclash/agents/__init__.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,11 @@
66
from codeclash.agents.utils import GameContext
77

88

9-
def get_agent(config: dict, game_context: GameContext, environment: DockerEnvironment) -> Player:
9+
def get_agent(config: dict, game_context: GameContext, environment: DockerEnvironment, push: bool = False) -> Player:
1010
agents = {
1111
"dummy": Dummy,
1212
"mini": MiniSWEAgent,
1313
}.get(config["agent"])
1414
if agents is None:
1515
raise ValueError(f"Unknown agent type: {config['agent']}")
16-
return agents(config, environment, game_context)
16+
return agents(config, environment, game_context, push=push)

codeclash/agents/player.py

Lines changed: 20 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -21,13 +21,15 @@ def __init__(
2121
config: dict,
2222
environment: DockerEnvironment,
2323
game_context: GameContext,
24+
push: bool = False,
2425
) -> None:
2526
self.config = config
2627
self.name = config["name"]
2728
self._player_unique_id = str(uuid.uuid4())
2829
"""Unique ID that doesn't clash even across multiple games. Used for git tags."""
2930
self.environment = environment
3031
self.game_context = game_context
32+
self.push = push
3133
self.logger = get_logger(
3234
self.name,
3335
log_path=self.game_context.log_local / "players" / self.name / "player.log",
@@ -43,6 +45,17 @@ def __init__(
4345
"initial_commit_hash": self._get_commit_hash(),
4446
}
4547

48+
if self.push:
49+
self.logger.info("Will push agent gameplay as branch to remote repository after each round")
50+
token = os.getenv("GITHUB_TOKEN")
51+
if not token:
52+
raise ValueError("GITHUB_TOKEN environment variable is required")
53+
for cmd in [
54+
"git remote remove origin",
55+
f"git remote add origin https://x-access-token:{token}@github.com/{GH_ORG}/{self.game_context.name}.git",
56+
]:
57+
assert_zero_exit_code(self.environment.execute(cmd), logger=self.logger)
58+
4659
# --- Main methods ---
4760

4861
def pre_run_hook(self, *, new_round: int) -> None:
@@ -56,6 +69,13 @@ def post_run_hook(self, *, round: int) -> None:
5669
self._commit()
5770
self._metadata["diff"][round] = self._get_round_diff(round)
5871
self._metadata["incremental_diff"][round] = self._get_round_diff(round, incremental=True)
72+
if self.push:
73+
for cmd in [
74+
f"git push origin {self._branch_name}",
75+
"git push origin --tags",
76+
]:
77+
assert_zero_exit_code(self.environment.execute(cmd), logger=self.logger)
78+
self.logger.info(f"Pushed {self.name} commit history to remote repository (branch {self._branch_name})")
5979

6080
@abstractmethod
6181
def run(self) -> None:
@@ -65,21 +85,6 @@ def get_metadata(self) -> dict:
6585
"""Get metadata for the agent."""
6686
return self._metadata
6787

68-
def push(self) -> None:
69-
"""Push codebase to a branch on the game's remote repository."""
70-
token = os.getenv("GITHUB_TOKEN")
71-
if not token:
72-
raise ValueError("GITHUB_TOKEN environment variable is required")
73-
74-
for cmd in [
75-
"git remote remove origin",
76-
f"git remote add origin https://x-access-token:{token}@github.com/{GH_ORG}/{self.game_context.name}.git",
77-
f"git push origin {self._branch_name}",
78-
"git push origin --tags",
79-
]:
80-
assert_zero_exit_code(self.environment.execute(cmd), logger=self.logger)
81-
self.logger.info(f"Pushed {self.name} commit history to remote repository (branch {self._branch_name})")
82-
8388
def reset_and_apply_patch(self, patch: str, *, base_commit: str = "", filter_patch: bool = True) -> None:
8489
"""Clean all uncommitted changes. If base_commit is provided, reset to that commit.
8590
Then apply the patch to the codebase.

codeclash/tournaments/pvp.py

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -19,15 +19,14 @@ class PvpTournament(AbstractTournament):
1919
def __init__(self, config: dict, *, cleanup: bool = False, push: bool = False):
2020
super().__init__(config, name="PvpTournament")
2121
self.cleanup_on_end = cleanup
22-
self.push = push
2322
self.game: CodeGame = get_game(
2423
self.config,
2524
tournament_id=self.tournament_id,
2625
local_output_dir=self.local_output_dir,
2726
)
2827
self.agents: list[Player] = []
2928
for agent_conf in self.config["players"]:
30-
self.agents.append(self.get_agent(agent_conf, self.config["prompts"]))
29+
self.agents.append(self.get_agent(agent_conf, self.config["prompts"], push=push))
3130

3231
@property
3332
def scoreboard(self) -> list[tuple[int, str]]:
@@ -46,7 +45,7 @@ def get_metadata(self) -> dict:
4645
"agents": [agent.get_metadata() for agent in self.agents],
4746
}
4847

49-
def get_agent(self, agent_config: dict, prompts: dict) -> Player:
48+
def get_agent(self, agent_config: dict, prompts: dict, push: bool) -> Player:
5049
"""Create an agent with environment and game context."""
5150
environment = self.game.get_environment(f"{self.game.game_id}.{agent_config['name']}")
5251

@@ -62,7 +61,7 @@ def get_agent(self, agent_config: dict, prompts: dict) -> Player:
6261
working_dir=str(DIR_WORK),
6362
)
6463

65-
return get_agent(agent_config, game_context, environment)
64+
return get_agent(agent_config, game_context, environment, push=push)
6665

6766
def run(self) -> None:
6867
"""Main execution function that runs all rounds."""
@@ -121,6 +120,3 @@ def end(self) -> None:
121120
"""Save output files, clean up game resources and push agents if requested."""
122121
(self.local_output_dir / "metadata.json").write_text(json.dumps(self.get_metadata(), indent=2))
123122
self.game.end(self.cleanup_on_end)
124-
if self.push:
125-
for agent in self.agents:
126-
agent.push()

0 commit comments

Comments
 (0)