Skip to content

Commit e4b0283

Browse files
author
yolo h8cker 93
committed
Matrix: Handle case of missing patch
1 parent 623d945 commit e4b0283

1 file changed

Lines changed: 24 additions & 9 deletions

File tree

codeclash/analysis/matrix.py

Lines changed: 24 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -100,13 +100,16 @@ def _save_progress(self):
100100
self.output_file.write_text(json.dumps(self._metadata, indent=2))
101101
self.logger.debug("Progress saved to matrix.json")
102102

103-
def _get_round_diff(self, player_name: str, round_num: int) -> str:
104-
"""Read diff data from changes_r{round}.json file."""
103+
def _get_round_diff(self, player_name: str, round_num: int) -> str | None:
104+
"""Read diff data from changes_r{round}.json file. Returns None if file doesn't exist."""
105105
if round_num == 0:
106106
return ""
107107
changes_file = self.pvp_output_dir / "players" / player_name / f"changes_r{round_num}.json"
108-
changes_data = json.loads(changes_file.read_text())
109-
return changes_data.get("full_diff", "")
108+
try:
109+
changes_data = json.loads(changes_file.read_text())
110+
return changes_data.get("full_diff", "")
111+
except FileNotFoundError:
112+
return None
110113

111114
def _create_dummy_agent(self, player_name: str, agent_suffix: str = "") -> Dummy:
112115
"""Create a dummy agent for matrix evaluation."""
@@ -140,8 +143,8 @@ def _create_dummy_agent(self, player_name: str, agent_suffix: str = "") -> Dummy
140143

141144
def _evaluate_matrix_cell(
142145
self, agent1: Dummy, agent2: Dummy, player1_name: str, player2_name: str, i: int, j: int, matrix_id: str
143-
) -> dict:
144-
"""Evaluate a single matrix cell and return the stats object."""
146+
) -> dict | None:
147+
"""Evaluate a single matrix cell and return the stats object. Returns None if cell should be skipped."""
145148
# Return existing result if already completed
146149
try:
147150
existing_result = self.matrices[matrix_id][str(i)][str(j)]
@@ -154,6 +157,18 @@ def _evaluate_matrix_cell(
154157
patch1 = self._get_round_diff(player1_name, i)
155158
patch2 = self._get_round_diff(player2_name, j)
156159

160+
# Skip if any required changes file is missing
161+
if patch1 is None:
162+
self.logger.warning(
163+
f"Skipping {player1_name} round {i} vs {player2_name} round {j} - missing changes file for {player1_name} round {i}"
164+
)
165+
return None
166+
if patch2 is None:
167+
self.logger.warning(
168+
f"Skipping {player1_name} round {i} vs {player2_name} round {j} - missing changes file for {player2_name} round {j}"
169+
)
170+
return None
171+
157172
agent1.reset_and_apply_patch(filter_git_diff(patch1))
158173
agent2.reset_and_apply_patch(filter_git_diff(patch2))
159174

@@ -179,9 +194,9 @@ def _evaluate_matrix(self, player1_name: str, player2_name: str):
179194
self.matrices[matrix_id].setdefault(str(i), {})
180195
j_range = range(i + 1) if symmetric else range(self.rounds + 1)
181196
for j in j_range:
182-
self.matrices[matrix_id][str(i)][str(j)] = self._evaluate_matrix_cell(
183-
agent1, agent2, player1_name, player2_name, i, j, matrix_id
184-
)
197+
result = self._evaluate_matrix_cell(agent1, agent2, player1_name, player2_name, i, j, matrix_id)
198+
if result is not None:
199+
self.matrices[matrix_id][str(i)][str(j)] = result
185200
self._save_progress()
186201

187202
def evaluate_all_matrices(self) -> dict:

0 commit comments

Comments
 (0)