Skip to content

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: sweagent remove-unfinished subcommand

Kilian Lieretf14e9173403b
diff --git a/sweagent/run/remove_unfinished.py b/sweagent/run/remove_unfinished.py
new file mode 100644
index 00000000..3f9be664
--- /dev/null
+++ b/sweagent/run/remove_unfinished.py
@@ -0,0 +1,63 @@
+"""Remove unfinished trajectories."""
+
+import argparse
+import shutil
+from pathlib import Path
+
+from sweagent.utils.files import load_file
+from sweagent.utils.log import get_logger
+
+logger = get_logger("remove_unfinished")
+
+
+def remove_unfinished(base_dir: Path, dry_run: bool = True) -> None:
+ """Remove unfinished trajectories."""
+ to_remove = []
+ for directory in base_dir.iterdir():
+ if not directory.is_dir():
+ continue
+ if "__" not in directory.name:
+ continue
+ trajs = list(directory.glob("*.traj"))
+ if not trajs:
+ logger.info("No trajectories found in %s", directory)
+ continue
+ if len(trajs) > 1:
+ logger.warning("Found multiple trajectories in %s. Skipping.", directory)
+ continue
+ try:
+ traj = load_file(trajs[0])
+ except Exception as e:
+ logger.warning("Error loading trajectory %s: %s. Adding to remove list.", trajs[0], e)
+ to_remove.append(directory)
+ continue
+ submission = traj.get("info", {}).get("submission", None)
+ if submission is None:
+ logger.warning("No submission found in %s. Adding to remove list.", directory)
+ to_remove.append(directory)
+ continue
+ if dry_run:
+ logger.info("Would remove %d unfinished trajectories.", len(to_remove))
+ for directory in to_remove:
+ logger.info(directory)
+ else:
+ for directory in to_remove:
+ logger.info("Removing %s", directory)
+ shutil.rmtree(directory)
+
+
+def get_cli_parser() -> argparse.ArgumentParser:
+ parser = argparse.ArgumentParser(description=__doc__)
+ parser.add_argument("--base-dir", type=Path, help="Base directory")
+ parser.add_argument("--remove", action="store_true", help="Remove unfinished trajectories")
+ return parser
+
+
+def run_from_cli(args: list[str] | None = None) -> None:
+ cli_parser = get_cli_parser()
+ cli_args = cli_parser.parse_args(args)
+ remove_unfinished(cli_args.base_dir, cli_args.remove)
+
+
+if __name__ == "__main__":
+ run_from_cli()
diff --git a/sweagent/run/run.py b/sweagent/run/run.py
index 94222564..3168a278 100644
--- a/sweagent/run/run.py
+++ b/sweagent/run/run.py
@@ -24,6 +24,7 @@ Display usage instructions for a specific command:
This can be useful to fill in environment output when creating demonstrations.
[bold][green]traj-to-demo[/green][/bold]: Convert a trajectory file to an easy to edit demo file.
[bold][green]run-api[/green][/bold]: Run swe-agent as a backend for a GUI
+[bold][green]remove-unfinished[/green][/bold] or [bold][green]ru[/green][/bold]: Remove unfinished trajectories
"""
import argparse
@@ -52,6 +53,8 @@ def get_cli():
"extract-pred",
"compare-runs",
"cr",
+ "remove-unfinished",
+ "ru",
],
nargs="?",
)
@@ -118,6 +121,10 @@ def main(args: list[str] | None = None):
from sweagent.run.compare_runs import run_from_cli as compare_runs_main
compare_runs_main(remaining_args)
+ elif command in ["remove-unfinished", "ru"]:
+ from sweagent.run.remove_unfinished import run_from_cli as remove_unfinished_main
+
+ remove_unfinished_main(remaining_args)
else:
msg = f"Unknown command: {command}"
raise ValueError(msg)