Skip to content

Commit f3586d2

Browse files
committed
Fix: Don't duplicate information into metadata; add branch name
1 parent 756e866 commit f3586d2

2 files changed

Lines changed: 21 additions & 17 deletions

File tree

codeclash/agents/player.py

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -39,10 +39,11 @@ def __init__(
3939
self._metadata = {
4040
"name": self.name,
4141
"player_unique_id": self._player_unique_id,
42-
"diff": {0: ""}, # mapping round -> diff
4342
"created_timestamp": int(time.time()),
4443
"config": self.config,
4544
"initial_commit_hash": self._get_commit_hash(),
45+
"branch_name": self._branch_name,
46+
"round_tags": {}, # mapping round -> tag
4647
}
4748

4849
if self.push:
@@ -64,17 +65,24 @@ def pre_run_hook(self, *, new_round: int) -> None:
6465
self._tag_round(0)
6566
self.game_context.round = new_round
6667

67-
def _write_changes_to_file(self, *, round: int, incremental_diff: str, modified_files: dict[str, str]) -> None:
68-
"""Write incremental changes to a JSON file in players/{name}/changes_r{round}.json"""
68+
def _write_changes_to_file(self, *, round: int) -> None:
69+
"""Write all changes to a JSON file in players/{name}/changes_r{round}.json"""
6970
if round == 0:
7071
return # No changes for round 0
7172

73+
# Generate all diffs and extract modified files
74+
raw_diff = self._get_round_diff(round)
75+
filtered_diff = filter_git_diff(raw_diff)
76+
incremental_diff = self._get_round_diff(round, incremental=True)
77+
modified_files = self._extract_modified_files_from_diff(filtered_diff)
78+
7279
player_dir = self.game_context.log_local / "players" / self.name
7380
player_dir.mkdir(parents=True, exist_ok=True)
7481

7582
changes_file = player_dir / f"changes_r{round}.json"
7683
changes_data = {
7784
"round": round,
85+
"full_diff": raw_diff,
7886
"incremental_diff": incremental_diff,
7987
"modified_files": modified_files,
8088
"timestamp": int(time.time()),
@@ -86,14 +94,9 @@ def _write_changes_to_file(self, *, round: int, incremental_diff: str, modified_
8694
def post_run_hook(self, *, round: int) -> None:
8795
"""Should be called after we called the run method."""
8896
self._commit()
89-
raw_diff = self._get_round_diff(round)
90-
filtered_diff = filter_git_diff(raw_diff)
91-
self._metadata["diff"][round] = raw_diff
9297

93-
# Write incremental changes to separate JSON file
94-
incremental_diff = self._get_round_diff(round, incremental=True)
95-
modified_files = self._extract_modified_files_from_diff(filtered_diff)
96-
self._write_changes_to_file(round=round, incremental_diff=incremental_diff, modified_files=modified_files)
98+
# Write all changes to separate JSON file
99+
self._write_changes_to_file(round=round)
97100

98101
if self.push:
99102
for cmd in [
@@ -144,10 +147,12 @@ def reset_and_apply_patch(self, patch: str, *, base_commit: str = "", filter_pat
144147

145148
def _tag_round(self, round: int) -> None:
146149
"""Git tag the codebase at the given round."""
150+
tag = self._get_round_tag_name(round)
147151
assert_zero_exit_code(
148-
self.environment.execute(f"git tag -a {self._get_round_tag_name(round)} -m 'Round {round} Update'"),
152+
self.environment.execute(f"git tag -a {tag} -m 'Round {round} Update'"),
149153
logger=self.logger,
150154
)
155+
self._metadata["round_tags"][round] = tag
151156

152157
@property
153158
def _branch_name(self) -> str:

codeclash/viewer/app.py

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -357,35 +357,34 @@ def parse_trajectory(self, player_name: str, round_num: int) -> TrajectoryInfo |
357357
info = data.get("info", {})
358358
model_stats = info.get("model_stats", {})
359359

360-
# Get diff data from player metadata and changes file
360+
# Get diff data from changes file (preferred) or fall back to metadata
361361
diff = None
362362
incremental_diff = None
363363
modified_files = None
364364
diff_by_files = {}
365365
incremental_diff_by_files = {}
366366

367-
if player_name in self._player_metadata:
368-
player_meta = self._player_metadata[player_name]
369-
diff = player_meta.get("diff", {}).get(str(round_num), "")
370-
371-
# Try to read incremental changes from separate JSON file
367+
# Try to read all changes from separate JSON file first
372368
changes_file = player_dir / f"changes_r{round_num}.json"
373369
if changes_file.exists():
374370
try:
375371
changes_data = json.loads(changes_file.read_text())
372+
diff = changes_data.get("full_diff", "")
376373
incremental_diff = changes_data.get("incremental_diff", "")
377374
modified_files = changes_data.get("modified_files", {})
378375
except (json.JSONDecodeError, KeyError):
379376
# Fall back to metadata if changes file is corrupted
380377
if player_name in self._player_metadata:
381378
player_meta = self._player_metadata[player_name]
379+
diff = player_meta.get("diff", {}).get(str(round_num), "")
382380
incremental_diff = player_meta.get("incremental_diff", {}).get(str(round_num), "")
383381
modified_files = player_meta.get("modified_files", {}).get(str(round_num), {})
384382
else:
385383
# todo: Legacy: Remove this at some point after we have migrated
386384
# Fall back to metadata if changes file doesn't exist
387385
if player_name in self._player_metadata:
388386
player_meta = self._player_metadata[player_name]
387+
diff = player_meta.get("diff", {}).get(str(round_num), "")
389388
incremental_diff = player_meta.get("incremental_diff", {}).get(str(round_num), "")
390389
modified_files = player_meta.get("modified_files", {}).get(str(round_num), {})
391390

0 commit comments

Comments
 (0)