| 1 | """This module contains an auxiliary class for rendering progress of a batch run.
|
| 2 | It's identical to the one used in swe-agent.
|
| 3 | """
|
| 4 |
|
| 5 | import collections
|
| 6 | import time
|
| 7 | from datetime import timedelta
|
| 8 | from pathlib import Path
|
| 9 | from threading import Lock
|
| 10 |
|
| 11 | import yaml
|
| 12 | from rich.console import Group
|
| 13 | from rich.progress import (
|
| 14 | BarColumn,
|
| 15 | MofNCompleteColumn,
|
| 16 | Progress,
|
| 17 | SpinnerColumn,
|
| 18 | TaskID,
|
| 19 | TaskProgressColumn,
|
| 20 | TextColumn,
|
| 21 | TimeElapsedColumn,
|
| 22 | )
|
| 23 | from rich.table import Table
|
| 24 |
|
| 25 | import minisweagent.models
|
| 26 |
|
| 27 |
|
| 28 | def _shorten_str(s: str, max_len: int, shorten_left=False) -> str:
|
| 29 | if not shorten_left:
|
| 30 | s = s[: max_len - 3] + "..." if len(s) > max_len else s
|
| 31 | else:
|
| 32 | s = "..." + s[-max_len + 3 :] if len(s) > max_len else s
|
| 33 | return f"{s:<{max_len}}"
|
| 34 |
|
| 35 |
|
| 36 | class RunBatchProgressManager:
|
| 37 | def __init__(
|
| 38 | self,
|
| 39 | num_instances: int,
|
| 40 | yaml_report_path: Path | None = None,
|
| 41 | ):
|
| 42 | """This class manages a progress bar/UI for run-batch
|
| 43 |
|
| 44 | Args:
|
| 45 | num_instances: Number of task instances
|
| 46 | yaml_report_path: Path to save a yaml report of the instances and their exit statuses
|
| 47 | """
|
| 48 |
|
| 49 | self._spinner_tasks: dict[str, TaskID] = {}
|
| 50 | """We need to map instance ID to the task ID that is used by the rich progress bar."""
|
| 51 |
|
| 52 | self._lock = Lock()
|
| 53 | self._start_time = time.time()
|
| 54 | self._total_instances = num_instances
|
| 55 |
|
| 56 | self._instances_by_exit_status = collections.defaultdict(list)
|
| 57 | self._main_progress_bar = Progress(
|
| 58 | SpinnerColumn(spinner_name="dots2"),
|
| 59 | TextColumn("[progress.description]{task.description} (${task.fields[total_cost]})"),
|
| 60 | BarColumn(),
|
| 61 | MofNCompleteColumn(),
|
| 62 | TaskProgressColumn(),
|
| 63 | TimeElapsedColumn(),
|
| 64 | TextColumn("[cyan]{task.fields[eta]}[/cyan]"),
|
| 65 | # Wait 5 min before estimating speed
|
| 66 | speed_estimate_period=60 * 5,
|
| 67 | )
|
| 68 | self._task_progress_bar = Progress(
|
| 69 | SpinnerColumn(spinner_name="dots2"),
|
| 70 | TextColumn("{task.fields[instance_id]}"),
|
| 71 | TextColumn("{task.fields[status]}"),
|
| 72 | TimeElapsedColumn(),
|
| 73 | )
|
| 74 | """Task progress bar for individual instances. There's only one progress bar
|
| 75 | with one task for each instance.
|
| 76 | """
|
| 77 |
|
| 78 | self._main_task_id = self._main_progress_bar.add_task(
|
| 79 | "[cyan]Overall Progress", total=num_instances, total_cost="0.00", eta=""
|
| 80 | )
|
| 81 |
|
| 82 | self.render_group = Group(self._main_progress_bar, Table(), self._task_progress_bar)
|
| 83 | self._yaml_report_path = yaml_report_path
|
| 84 |
|
| 85 | @property
|
| 86 | def n_completed(self) -> int:
|
| 87 | return sum(len(instances) for instances in self._instances_by_exit_status.values())
|
| 88 |
|
| 89 | def _get_eta_text(self) -> str:
|
| 90 | """Calculate estimated time remaining based on current progress."""
|
| 91 | try:
|
| 92 | estimated_remaining = (
|
| 93 | (time.time() - self._start_time) / self.n_completed * (self._total_instances - self.n_completed)
|
| 94 | )
|
| 95 | return f"eta: {timedelta(seconds=int(estimated_remaining))}"
|
| 96 | except ZeroDivisionError:
|
| 97 | return ""
|
| 98 |
|
| 99 | def update_exit_status_table(self):
|
| 100 | # We cannot update the existing table, so we need to create a new one and
|
| 101 | # assign it back to the render group.
|
| 102 | t = Table()
|
| 103 | t.add_column("Exit Status")
|
| 104 | t.add_column("Count", justify="right", style="bold cyan")
|
| 105 | t.add_column("Most recent instances")
|
| 106 | t.show_header = False
|
| 107 | with self._lock:
|
| 108 | t.show_header = True
|
| 109 | # Sort by number of instances in descending order
|
| 110 | sorted_items = sorted(self._instances_by_exit_status.items(), key=lambda x: len(x[1]), reverse=True)
|
| 111 | for status, instances in sorted_items:
|
| 112 | instances_str = _shorten_str(", ".join(reversed(instances)), 55)
|
| 113 | t.add_row(status, str(len(instances)), instances_str)
|
| 114 | assert self.render_group is not None
|
| 115 | self.render_group.renderables[1] = t
|
| 116 |
|
| 117 | def _update_total_costs(self) -> None:
|
| 118 | with self._lock:
|
| 119 | self._main_progress_bar.update(
|
| 120 | self._main_task_id,
|
| 121 | total_cost=f"{minisweagent.models.GLOBAL_MODEL_STATS.cost:.2f}",
|
| 122 | eta=self._get_eta_text(),
|
| 123 | )
|
| 124 |
|
| 125 | def update_instance_status(self, instance_id: str, message: str):
|
| 126 | assert self._task_progress_bar is not None
|
| 127 | assert self._main_progress_bar is not None
|
| 128 | with self._lock:
|
| 129 | self._task_progress_bar.update(
|
| 130 | self._spinner_tasks[instance_id],
|
| 131 | status=_shorten_str(message, 30),
|
| 132 | instance_id=_shorten_str(instance_id, 25, shorten_left=True),
|
| 133 | )
|
| 134 | self._update_total_costs()
|
| 135 |
|
| 136 | def on_instance_start(self, instance_id: str):
|
| 137 | with self._lock:
|
| 138 | self._spinner_tasks[instance_id] = self._task_progress_bar.add_task(
|
| 139 | description=f"Task {instance_id}",
|
| 140 | status="Task initialized",
|
| 141 | total=None,
|
| 142 | instance_id=instance_id,
|
| 143 | )
|
| 144 |
|
| 145 | def on_instance_end(self, instance_id: str, exit_status: str | None) -> None:
|
| 146 | with self._lock:
|
| 147 | self._instances_by_exit_status[exit_status].append(instance_id)
|
| 148 | try:
|
| 149 | self._task_progress_bar.remove_task(self._spinner_tasks[instance_id])
|
| 150 | except KeyError:
|
| 151 | pass
|
| 152 | self._main_progress_bar.update(TaskID(0), advance=1, eta=self._get_eta_text())
|
| 153 | self.update_exit_status_table()
|
| 154 | self._update_total_costs()
|
| 155 | if self._yaml_report_path is not None:
|
| 156 | self._save_overview_data_yaml(self._yaml_report_path)
|
| 157 |
|
| 158 | def on_uncaught_exception(self, instance_id: str, exception: Exception) -> None:
|
| 159 | self.on_instance_end(instance_id, f"Uncaught {type(exception).__name__}")
|
| 160 |
|
| 161 | def print_report(self) -> None:
|
| 162 | """Print complete list of instances and their exit statuses."""
|
| 163 | for status, instances in self._instances_by_exit_status.items():
|
| 164 | print(f"{status}: {len(instances)}")
|
| 165 | for instance in instances:
|
| 166 | print(f" {instance}")
|
| 167 |
|
| 168 | def _get_overview_data(self) -> dict:
|
| 169 | """Get data like exit statuses, total costs, etc."""
|
| 170 | return {
|
| 171 | # convert defaultdict to dict because of serialization
|
| 172 | "instances_by_exit_status": dict(self._instances_by_exit_status),
|
| 173 | }
|
| 174 |
|
| 175 | def _save_overview_data_yaml(self, path: Path) -> None:
|
| 176 | """Save a yaml report of the instances and their exit statuses."""
|
| 177 | with self._lock:
|
| 178 | path.write_text(yaml.dump(self._get_overview_data(), indent=4))
|
| 179 |
|