Skip to content

Commit 6c6bd0f

Browse files
committed
Enh: Save metadata every round
Closes #58
1 parent d38eedb commit 6c6bd0f

2 files changed

Lines changed: 13 additions & 3 deletions

File tree

codeclash/tournaments/pvp.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,8 @@ def run_competition_phase(self, round_num: int) -> None:
8080
results_file = self.game.log_local / "rounds" / str(round_num) / FILE_RESULTS
8181
results_file.write_text(json.dumps(stats.to_dict(), indent=2))
8282

83+
self._save()
84+
8385
def run_edit_phase(self, round_num: int) -> None:
8486
"""Execute a single training round."""
8587
# Copy log to agent environments
@@ -100,6 +102,7 @@ def run_edit_phase(self, round_num: int) -> None:
100102
self.logger.critical(f"Agent execution failed: {e}", exc_info=True)
101103
raise
102104

105+
self._save()
103106
self.logger.info("Round completed.")
104107

105108
def run_agent(self, agent: Player, round_num: int) -> None:
@@ -108,7 +111,10 @@ def run_agent(self, agent: Player, round_num: int) -> None:
108111
agent.run()
109112
agent.post_run_hook(round=round_num)
110113

114+
def _save(self) -> None:
115+
(self.local_output_dir / "metadata.json").write_text(json.dumps(self.get_metadata(), indent=2))
116+
111117
def end(self) -> None:
112118
"""Save output files, clean up game resources and push agents if requested."""
113-
(self.local_output_dir / "metadata.json").write_text(json.dumps(self.get_metadata(), indent=2))
119+
self._save()
114120
self.game.end(self.cleanup_on_end)

codeclash/tournaments/single_player.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,8 @@ def run_training_round(self, round_num: int) -> None:
102102
mirror_agent_state = round_num - 1 if round_num > 1 else 0
103103
self.set_mirror_state_to_round(mirror_agent_state)
104104

105+
self._save()
106+
105107
self.logger.info("Round completed.")
106108

107109
def run_main_agent(self, round_num: int):
@@ -116,10 +118,12 @@ def set_mirror_state_to_round(self, round_num: int):
116118
full_diff = filter_git_diff(full_diff)
117119
self.mirror_agent.reset_and_apply_patch(full_diff)
118120

121+
def _save(self) -> None:
122+
(self.local_output_dir / "metadata.json").write_text(json.dumps(self.get_metadata(), indent=2))
123+
119124
def end(self):
120125
"""Clean up game resources."""
121-
with open(self.local_output_dir / "metadata.json", "w") as f:
122-
json.dump(self.get_metadata(), fp=f, indent=2)
126+
self._save()
123127
self.game.end(self.cleanup_on_end)
124128

125129
def evaluate(self, n_repetitions: int = 3) -> None:

0 commit comments

Comments
 (0)