|
1 | | -""" |
2 | | -In single player mode, the agent runs always against its previous version. |
3 | | -""" |
4 | | - |
5 | 1 | import argparse |
6 | | -import copy |
7 | 2 |
|
8 | 3 | import yaml |
9 | 4 |
|
10 | | -from codeclash.agents import get_agent |
11 | | -from codeclash.agents.abstract import Player |
12 | | -from codeclash.games import get_game |
13 | | -from codeclash.games.abstract import CodeGame |
14 | | -from codeclash.utils.environment import assert_zero_exit_code, create_file_on_container |
15 | | -from codeclash.utils.log import get_logger |
16 | | - |
17 | | - |
18 | | -def filter_git_diff(text: str) -> str: |
19 | | - """Return a git diff with any file sections mentioning binary content removed.""" |
20 | | - lines = text.splitlines(keepends=True) |
21 | | - out: list[str] = [] |
22 | | - block: list[str] = [] |
23 | | - in_block = False |
24 | | - prelude_copied = False |
25 | | - |
26 | | - def is_binary_block(bl: list[str]) -> bool: |
27 | | - for ln in bl: |
28 | | - s = ln.strip() |
29 | | - if ln.startswith("Binary files "): |
30 | | - return True |
31 | | - if s == "GIT binary patch": |
32 | | - return True |
33 | | - return False |
34 | | - |
35 | | - for ln in lines: |
36 | | - if ln.startswith("diff --git "): |
37 | | - if in_block: |
38 | | - if not is_binary_block(block): |
39 | | - out.extend(block) |
40 | | - block = [] |
41 | | - else: |
42 | | - if not prelude_copied: |
43 | | - prelude_copied = True |
44 | | - in_block = True |
45 | | - if in_block: |
46 | | - block.append(ln) |
47 | | - else: |
48 | | - out.append(ln) |
49 | | - |
50 | | - if in_block and block: |
51 | | - if not is_binary_block(block): |
52 | | - out.extend(block) |
53 | | - |
54 | | - return "".join(out) |
55 | | - |
56 | | - |
57 | | -class SinglePlayerTraining: |
58 | | - def __init__(self, config: dict, cleanup: bool = False): |
59 | | - self.config = config |
60 | | - self.cleanup_on_end = cleanup |
61 | | - self.game: CodeGame = get_game(self.config) |
62 | | - self.agent: Player = get_agent( |
63 | | - self.config["player"], self.config["prompts"], self.game |
64 | | - ) |
65 | | - mirror_agent_config = copy.deepcopy(self.config["player"]) |
66 | | - mirror_agent_config["name"] = "mirror" |
67 | | - self.mirror_agent: Player = get_agent( |
68 | | - mirror_agent_config, self.config["prompts"], self.game |
69 | | - ) |
70 | | - self.logger = get_logger(self.game.name) |
71 | | - |
72 | | - def run(self): |
73 | | - """Main execution function that runs all rounds.""" |
74 | | - try: |
75 | | - for round_num in range(1, self.game.rounds + 1): |
76 | | - self.run_round(round_num) |
77 | | - finally: |
78 | | - self.cleanup() |
79 | | - |
80 | | - def run_round(self, round_num: int): |
81 | | - """Execute a single training round.""" |
82 | | - self.game.run_round([self.agent, self.mirror_agent]) |
83 | | - self.run_main_agent(round_num) |
84 | | - self.run_mirror_agent(round_num) |
85 | | - |
86 | | - def run_main_agent(self, round_num: int): |
87 | | - """Run the main agent for the current round.""" |
88 | | - self.agent.pre_run_hook(new_round=round_num) |
89 | | - self.agent.run() |
90 | | - self.agent.post_run_hook(round=round_num) |
91 | | - |
92 | | - def run_mirror_agent(self, round_num: int): |
93 | | - """Update mirror agent's codebase with the main agent's changes.""" |
94 | | - if round_num == 1: |
95 | | - self.logger.info("Skipping updating mirror agent for round 1") |
96 | | - return |
97 | | - |
98 | | - # Set mirror agent's codebase to the main agent's codebase of the previous round |
99 | | - full_diff = self.agent.get_metadata()["diff"][round_num - 1] |
100 | | - |
101 | | - full_diff = filter_git_diff(full_diff) |
102 | | - |
103 | | - if full_diff.strip(): |
104 | | - self.logger.debug( |
105 | | - assert_zero_exit_code( |
106 | | - self.mirror_agent.environment.execute( |
107 | | - "git reset --hard && git clean -fd" |
108 | | - ) |
109 | | - ) |
110 | | - ) |
111 | | - |
112 | | - create_file_on_container( |
113 | | - container=self.mirror_agent.environment, # type: ignore |
114 | | - content=full_diff, |
115 | | - dest_path="tmp_patch.txt", |
116 | | - ) |
117 | | - |
118 | | - self.logger.info("Applying patch to mirror agent's codebase") |
119 | | - self.logger.debug(f"Full diff: {full_diff}") |
120 | | - |
121 | | - commands = ["git status", "git apply tmp_patch.txt", "rm -f tmp_patch.txt"] |
122 | | - for cmd in commands: |
123 | | - self.logger.debug(f"Executing command: {cmd}") |
124 | | - out = assert_zero_exit_code( |
125 | | - self.mirror_agent.environment.execute(cmd), logger=self.logger |
126 | | - ) |
127 | | - self.logger.debug(out) |
128 | | - else: |
129 | | - self.logger.info("No diff found for mirror agent, skipping update") |
130 | | - |
131 | | - def cleanup(self): |
132 | | - """Clean up game resources.""" |
133 | | - self.game.end(self.cleanup_on_end) |
| 5 | +from codeclash.tournaments.single_player_training import SinglePlayerTraining |
134 | 6 |
|
135 | 7 |
|
136 | 8 | def main(config_path: str, cleanup: bool = False): |
|
0 commit comments