SWE-agent takes a GitHub issue and tries to automatically fix it, using your LM of choice. It can also be employed for offensive cybersecurity or competitive coding challenges. [NeurIPS 2024]
Feat(inspector cli): Show gold patches
Kilian Lieret626555b1b900
diff --git a/sweagent/run/batch_instances.py b/sweagent/run/batch_instances.pyindex 80a46e18..8b329625 100644--- a/sweagent/run/batch_instances.py+++ b/sweagent/run/batch_instances.py@@ -1,11 +1,9 @@-import jsonimport randomimport refrom abc import ABC, abstractmethodfrom pathlib import Pathfrom typing import Any, Literal-import yamlfrom pydantic import BaseModel, Fieldfrom swerex.deployment.config import (DeploymentConfig,@@ -18,6 +16,7 @@ from typing_extensions import Selffrom sweagent.agent.problem_statement import ProblemStatementConfig, TextProblemStatementfrom sweagent.environment.repo import GithubRepoConfig, LocalRepoConfig, PreExistingRepoConfigfrom sweagent.environment.swe_env import EnvironmentConfig+from sweagent.utils.files import load_filefrom sweagent.utils.log import get_loggerlogger = get_logger("swea-config", emoji="🔧")@@ -30,24 +29,6 @@ class AbstractInstanceSource(ABC):def get_instance_configs(self) -> list[EnvironmentConfig]: ...-def _load_file(path: Path) -> Any:- """Load files based on their extension."""- if not path.exists():- raise FileNotFoundError(path)- if path.is_dir():- from datasets import load_from_disk-- return load_from_disk(path)- if path.suffix == ".json":- return json.loads(path.read_text())- if path.suffix == ".jsonl":- return [json.loads(line) for line in path.read_text().splitlines() if line.strip()]- if path.suffix == ".yaml":- return yaml.safe_load(path.read_text())- msg = f"Unsupported file extension: {path.suffix}"- raise NotImplementedError(msg)--class BatchInstance(BaseModel):"""A single instance in a batch of instances.This specifies both the environment configuration and the problem statement.@@ -199,7 +180,7 @@ class InstancesFromFile(BaseModel, AbstractInstanceSource):"""Discriminator for (de)serialization/CLI. Do not change."""def get_instance_configs(self) -> list[BatchInstance]:- instance_dicts = _load_file(self.path)+ instance_dicts = load_file(self.path)simple_instances = [SimpleBatchInstance.model_validate(instance_dict) for instance_dict in instance_dicts]instances = [instance.to_full_batch_instance(self.deployment) for instance in simple_instances]return _filter_batch_items(instances, filter_=self.filter, slice_=self.slice, shuffle=self.shuffle)@@ -320,7 +301,7 @@ class ExpertInstancesFromFile(BaseModel, AbstractInstanceSource):"""Discriminator for (de)serialization/CLI. Do not change."""def get_instance_configs(self) -> list[BatchInstance]:- instance_dicts = _load_file(self.path)+ instance_dicts = load_file(self.path)instances = [BatchInstance.model_validate(instance_dict) for instance_dict in instance_dicts]return _filter_batch_items(instances, filter_=self.filter, slice_=self.slice, shuffle=self.shuffle)diff --git a/sweagent/run/inspector_cli.py b/sweagent/run/inspector_cli.pyindex 761a951c..51af3e35 100644--- a/sweagent/run/inspector_cli.py+++ b/sweagent/run/inspector_cli.py@@ -14,10 +14,16 @@ from textual.containers import Container, Vertical, VerticalScrollfrom textual.screen import ModalScreenfrom textual.widgets import Footer, Header, Input, ListItem, ListView, Static+from sweagent.utils.files import load_filefrom sweagent.utils.serialization import _yaml_serialization_with_linebreaksdef _move_items_top(d: dict, keys: list[str]) -> dict:+ """Reorder items in a dictionary.++ The first keys will be those specified in `keys`, the rest will+ be in the same order as in the original dictionary.+ """new_d = {}for key in keys:if key in d:@@ -39,18 +45,22 @@ class TrajectoryViewer(Static):Binding("k,up", "scroll_up", "Scroll up"),]- def __init__(self, path: Path, title: str, overview_stats: dict):+ def __init__(self, path: Path, title: str, overview_stats: dict, *, gold_patch: str | None = None):+ """View a single trajectory."""super().__init__()self.current_index = -1self.trajectory = json.loads(path.read_text())self.show_full = Falseself.title = titleself.overview_stats = overview_stats+ self.gold_patch = gold_patch- def load_trajectory(self, path: Path, title: str, overview_stats: dict):+ def load_trajectory(self, path: Path, title: str, overview_stats: dict, *, gold_patch: str | None = None):+ """Load a new trajectory and update the viewer."""print("Loading", path)self.trajectory = json.loads(path.read_text())self.title = title+ self.gold_patch = gold_patchself.update_content()def compose(self) -> ComposeResult:@@ -64,7 +74,7 @@ class TrajectoryViewer(Static):def n_steps(self) -> int:return len(self.trajectory["trajectory"])- def _show_full(self, item: dict) -> None:+ def _show_step_yaml(self, item: dict) -> None:"""Show full yaml of trajectory item"""content_str = _yaml_serialization_with_linebreaks(_move_items_top(item, ["thought", "action", "observation", "response", "execution_time"])@@ -74,7 +84,7 @@ class TrajectoryViewer(Static):content.update(syntax) # type: ignoreself.app.sub_title = f"{self.title} - Step {self.current_index + 1}/{self.n_steps} - Full View"- def _show_overview(self, item: dict) -> None:+ def _show_step_simple(self, item: dict) -> None:# Simplified view - show action and observation as plain textthought = item.get("thought", "")action = item.get("action", "")@@ -89,7 +99,8 @@ class TrajectoryViewer(Static):def _show_info(self):info = copy.deepcopy(self.trajectory["info"])info["result"] = self.overview_stats["result"]- info = _move_items_top(info, ["result", "exit_status", "model_stats", "submission"])+ info["gold_patch"] = self.gold_patch+ info = _move_items_top(info, ["result", "exit_status", "model_stats", "submission", "gold_patch"])syntax = Syntax(_yaml_serialization_with_linebreaks(info), "yaml", theme="monokai", word_wrap=True)content = self.query_one("#content")content.update(syntax) # type: ignore@@ -104,9 +115,9 @@ class TrajectoryViewer(Static):item = self.trajectory["trajectory"][self.current_index]if self.show_full:- return self._show_full(item)+ return self._show_step_yaml(item)- return self._show_overview(item)+ return self._show_step_simple(item)def action_next_item(self) -> None:if self.current_index < self.n_steps:@@ -330,7 +341,7 @@ class TrajectoryInspectorApp(App):}"""- def __init__(self, input_path: str | Path):+ def __init__(self, input_path: str | Path, data_path: Path | None = None):super().__init__()self.input_path = Path(input_path)if not self.input_path.exists():@@ -343,6 +354,12 @@ class TrajectoryInspectorApp(App):self.trajectory_index = 0self.overview_stats = collections.defaultdict(dict)self._build_overview_stats()+ self._data = load_file(data_path)++ def get_gold_patch(self, instance_id: str) -> str | None:+ if self._data is None:+ return None+ return self._data.get(instance_id, {}).get("patch", None)def _build_overview_stats(self):results_path = self.input_path / "results.json"@@ -386,6 +403,7 @@ class TrajectoryInspectorApp(App):self.available_traj_paths[self.trajectory_index],self._get_viewer_title(self.trajectory_index),self.overview_stats[instance_id],+ gold_patch=self.get_gold_patch(instance_id),)def _get_available_trajs(self) -> list[Path]:@@ -444,6 +462,7 @@ def main(args: list[str] | None = None):default=os.getcwd(),nargs="?",)+ parser.add_argument("-d", "--data_path", type=Path, help="Path to the data file to load gold patches from")parsed_args = parser.parse_args(args)app = TrajectoryInspectorApp(parsed_args.trajectory_path)diff --git a/sweagent/utils/files.py b/sweagent/utils/files.pynew file mode 100644index 00000000..e10f868e--- /dev/null+++ b/sweagent/utils/files.py@@ -0,0 +1,25 @@+import json+from pathlib import Path+from typing import Any++import yaml+++def load_file(path: Path | None) -> Any:+ """Load files based on their extension."""+ if path is None:+ return None+ if not path.exists():+ raise FileNotFoundError(path)+ if path.is_dir():+ from datasets import load_from_disk++ return load_from_disk(path)+ if path.suffix == ".json":+ return json.loads(path.read_text())+ if path.suffix == ".jsonl":+ return [json.loads(line) for line in path.read_text().splitlines() if line.strip()]+ if path.suffix == ".yaml":+ return yaml.safe_load(path.read_text())+ msg = f"Unsupported file extension: {path.suffix}"+ raise NotImplementedError(msg)