Process a single agent and update their agent_stats. Returns True if any updates were made.
(agent: dict, agent_folder: Path)
| 23 | |
| 24 | |
| 25 | def update_agent_info(agent: dict, agent_folder: Path) -> bool: |
| 26 | """Process a single agent and update their agent_stats. |
| 27 | |
| 28 | Returns True if any updates were made. |
| 29 | """ |
| 30 | agent_name = agent.get("name") |
| 31 | if not agent_name: |
| 32 | logger.warning("Agent entry missing 'name'; skipping") |
| 33 | return False |
| 34 | |
| 35 | if not agent_folder.is_dir(): |
| 36 | logger.warning(f"No folder found for agent {agent_name} in {agent_folder.parent}") |
| 37 | return False |
| 38 | |
| 39 | if "agent_stats" not in agent: |
| 40 | agent["agent_stats"] = {} |
| 41 | |
| 42 | updated = False |
| 43 | |
| 44 | for traj_file in agent_folder.glob(f"{agent_name}_r*.traj.json"): |
| 45 | round_num = extract_round_number(traj_file.name) |
| 46 | if round_num is None: |
| 47 | logger.warning(f"Could not extract round number from {traj_file.name}") |
| 48 | continue |
| 49 | |
| 50 | if str(round_num) in agent["agent_stats"]: |
| 51 | existing = agent["agent_stats"][str(round_num)] |
| 52 | logger.debug(f"Skipping {agent_name} round {round_num} (already exists): {existing}") |
| 53 | continue |
| 54 | |
| 55 | try: |
| 56 | traj_data = json.loads(traj_file.read_text()) |
| 57 | info = traj_data.get("info", {}) |
| 58 | model_stats = info.get("model_stats", {}) |
| 59 | |
| 60 | cost = model_stats.get("instance_cost") |
| 61 | api_calls = model_stats.get("api_calls") |
| 62 | exit_status = info.get("exit_status") |
| 63 | |
| 64 | agent["agent_stats"][str(round_num)] = { |
| 65 | "exit_status": exit_status, |
| 66 | "cost": cost, |
| 67 | "api_calls": api_calls, |
| 68 | } |
| 69 | |
| 70 | updated = True |
| 71 | logger.info(f"Added stats for {agent_name} round {round_num}: cost={cost}, api_calls={api_calls}") |
| 72 | |
| 73 | except Exception as e: |
| 74 | logger.error(f"Error processing {traj_file}: {e}", exc_info=True) |
| 75 | |
| 76 | return updated |
| 77 | |
| 78 | |
| 79 | def process_tournament_folder(metadata_path: Path, *, dry_run: bool = False) -> None: |
no test coverage detected