@@ -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 } "
0 commit comments