(
self,
config: dict,
environment: DockerEnvironment,
game_context: GameContext,
)
| 18 | |
| 19 | class Player(ABC): |
| 20 | def __init__( |
| 21 | self, |
| 22 | config: dict, |
| 23 | environment: DockerEnvironment, |
| 24 | game_context: GameContext, |
| 25 | ) -> None: |
| 26 | self.config = config |
| 27 | self.name = config["name"] |
| 28 | self._player_unique_id = str(uuid.uuid4()) |
| 29 | """Unique ID that doesn't clash even across multiple games. Used for git tags.""" |
| 30 | self.environment = environment |
| 31 | self.game_context = game_context |
| 32 | self.push = config.get("push", False) |
| 33 | self.logger = get_logger( |
| 34 | self.name, |
| 35 | log_path=self.game_context.log_local / "players" / self.name / "player.log", |
| 36 | emoji="👤", |
| 37 | ) |
| 38 | self._branch_name = config.get("branch", f"{self.game_context.id}.{self.name}") |
| 39 | self._metadata = { |
| 40 | "name": self.name, |
| 41 | "player_unique_id": self._player_unique_id, |
| 42 | "created_timestamp": int(time.time()), |
| 43 | "config": self.config, |
| 44 | "initial_commit_hash": self._get_commit_hash(), |
| 45 | "branch_name": self._branch_name, |
| 46 | "round_tags": {}, # mapping round -> tag |
| 47 | "agent_stats": {}, # mapping round -> agent stats |
| 48 | } |
| 49 | |
| 50 | if self.push: |
| 51 | self.logger.info("Will push agent gameplay as branch to remote repository after each round") |
| 52 | token = os.getenv("GITHUB_TOKEN") |
| 53 | if not token: |
| 54 | raise ValueError("GITHUB_TOKEN environment variable is required") |
| 55 | for cmd in [ |
| 56 | "git remote remove origin", |
| 57 | f"git remote add origin https://x-access-token:{token}@github.com/{GH_ORG}/{self.game_context.name}.git", |
| 58 | ]: |
| 59 | assert_zero_exit_code(self.environment.execute(cmd), logger=self.logger) |
| 60 | |
| 61 | # Handle branch initialization |
| 62 | if branch_init := config.get("branch_init"): |
| 63 | # Fetch, then check out the initial branch (creating a tracking branch if needed). |
| 64 | assert_zero_exit_code( |
| 65 | self.environment.execute(f"git fetch origin && git checkout {branch_init}"), |
| 66 | logger=self.logger, |
| 67 | ) |
| 68 | self.logger.info(f"Checked out initial branch {branch_init}") |
| 69 | |
| 70 | if self._branch_name != branch_init: |
| 71 | self.logger.info(f"Switching to branch {self._branch_name} for pushing changes") |
| 72 | if branch_init: |
| 73 | # Start the push branch at branch_init. get_environment() pre-created a |
| 74 | # same-named branch at the default branch; a plain checkout would revert the |
| 75 | # working tree to it, so use -B to re-point it at the current HEAD. |
| 76 | assert_zero_exit_code( |
| 77 | self.environment.execute(f"git checkout -B {self._branch_name}"), |
nothing calls this directly
no test coverage detected