MCPcopy Create free account
hub / github.com/CodeClash-ai/CodeClash / __init__

Method __init__

codeclash/agents/player.py:20–88  ·  view source on GitHub ↗
(
        self,
        config: dict,
        environment: DockerEnvironment,
        game_context: GameContext,
    )

Source from the content-addressed store, hash-verified

18
19class 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 # Force the branch push (used on ladder resume, to overwrite an interrupted rung's partial
34 # pushes). Safe here because a run's push branch has a single writer.
35 self._force_push = config.get("force_push", False)
36 self.logger = get_logger(
37 self.name,
38 log_path=self.game_context.log_local / "players" / self.name / "player.log",
39 emoji="👤",
40 )
41 self._branch_name = config.get("branch", f"{self.game_context.id}.{self.name}")
42 self._metadata = {
43 "name": self.name,
44 "player_unique_id": self._player_unique_id,
45 "created_timestamp": int(time.time()),
46 "config": self.config,
47 "initial_commit_hash": self._get_commit_hash(),
48 "branch_name": self._branch_name,
49 "round_tags": {}, # mapping round -> tag
50 "agent_stats": {}, # mapping round -> agent stats
51 }
52
53 if self.push:
54 self.logger.info("Will push agent gameplay as branch to remote repository after each round")
55 token = os.getenv("GITHUB_TOKEN")
56 if not token:
57 raise ValueError("GITHUB_TOKEN environment variable is required")
58 for cmd in [
59 "git remote remove origin",
60 f"git remote add origin https://x-access-token:{token}@github.com/{GH_ORG}/{self.game_context.name}.git",
61 ]:
62 assert_zero_exit_code(self.environment.execute(cmd), logger=self.logger)
63
64 # Handle branch initialization
65 if branch_init := config.get("branch_init"):
66 # Fetch, then check out the initial branch (creating a tracking branch if needed).
67 assert_zero_exit_code(
68 self.environment.execute(f"git fetch origin && git checkout {branch_init}"),
69 logger=self.logger,
70 )
71 self.logger.info(f"Checked out initial branch {branch_init}")
72
73 if self._branch_name != branch_init:
74 self.logger.info(f"Switching to branch {self._branch_name} for pushing changes")
75 if branch_init:
76 # Start the push branch at branch_init. get_environment() pre-created a
77 # same-named branch at the default branch; a plain checkout would revert the

Callers

nothing calls this directly

Calls 5

_get_commit_hashMethod · 0.95
get_loggerFunction · 0.90
assert_zero_exit_codeFunction · 0.90
getMethod · 0.80
executeMethod · 0.45

Tested by

no test coverage detected