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]

Enh: Add autocorrect suggestions for run_single (#854)

Kilian Liereta2bfef63b491
diff --git a/sweagent/run/common.py b/sweagent/run/common.py
index e588bb02..53cced97 100644
--- a/sweagent/run/common.py
+++ b/sweagent/run/common.py
@@ -4,6 +4,7 @@ import json
import sys
from argparse import ArgumentParser
from collections import defaultdict
+from collections.abc import Callable
from pathlib import Path
from types import UnionType
from typing import Any
@@ -68,6 +69,35 @@ Run `sweagent <subcommand> --help` for usage examples.
"""
+class AutoCorrectSuggestion:
+ def __init__(
+ self, original: str, alternative: str = "", *, condition: Callable | None = None, help: str | None = None
+ ):
+ self.original = original
+ self.alternative = alternative
+ self.condition = condition
+ self.help = help
+ if self.help and self.alternative:
+ msg = "Cannot set both help and alternative"
+ raise ValueError(msg)
+
+ def show(self, args: list[str]) -> bool:
+ no_equal = []
+ for arg in args:
+ if "=" in arg:
+ no_equal.extend(arg.split("="))
+ else:
+ no_equal.append(arg)
+ if self.condition is not None:
+ return self.condition(no_equal)
+ return f"--{self.original}" in no_equal
+
+ def format(self) -> str:
+ if self.help:
+ return self.help
+ return f"You wrote [red]--{self.original}[/red]. Did you mean [green]--{self.alternative}[/green]?"
+
+
class ConfigHelper:
"""Produce easy-to-read help text from pydantic setting objects."""
@@ -171,6 +201,20 @@ class BasicCLI:
self.logger = get_logger("swea-cli", emoji="🔧")
self.help_text = help_text
+ def maybe_show_auto_correct(self, args: list[str]):
+ auto_correct = []
+ if hasattr(self.arg_type, "_get_auto_correct"):
+ for ac in self.arg_type._get_auto_correct(): # type: ignore
+ if ac.show(args):
+ auto_correct.append(ac)
+ if auto_correct:
+ rich_print(
+ Panel.fit(
+ "[red][bold]Auto-correct suggestions[/bold][/red]\n\n"
+ + "\n".join(ac.format() for ac in auto_correct),
+ )
+ )
+
def get_config(self, args: list[str] | None = None) -> BaseSettings:
"""Get the configuration object from defaults and command arguments."""
@@ -295,10 +339,12 @@ class BasicCLI:
"[red][bold]Validation error[/bold]\n" + _VALIDATION_ERROR_HELP_TEXT + "[/red]\n" + str(e),
)
)
+ self.maybe_show_auto_correct(remaining_args)
msg = "Invalid configuration. Please check the above output."
raise RuntimeError(msg) from None
except SettingsError as e:
rich_print(Panel.fit("[red][bold]SettingsError[/bold][/red]\n\n" + str(e) + "\n\n" + _SETTING_ERROR_HINTS))
+ self.maybe_show_auto_correct(remaining_args)
msg = "Invalid command line arguments. Please check the above output in the box."
raise RuntimeError(msg) from None
diff --git a/sweagent/run/run_single.py b/sweagent/run/run_single.py
index b1b9c6e4..3c7abd9b 100644
--- a/sweagent/run/run_single.py
+++ b/sweagent/run/run_single.py
@@ -43,6 +43,7 @@ from sweagent.agent.problem_statement import (
ProblemStatementConfig,
)
from sweagent.environment.swe_env import EnvironmentConfig, SWEEnv
+from sweagent.run.common import AutoCorrectSuggestion as ACS
from sweagent.run.common import BasicCLI, ConfigHelper, save_predictions
from sweagent.run.hooks.abstract import CombinedRunHooks, RunHook
from sweagent.run.hooks.apply_patch import SaveApplyPatchHook
@@ -89,6 +90,24 @@ class RunSingleConfig(BaseSettings, cli_implicit_flags=False):
config_file = config_file.stem
self.output_dir = Path.cwd() / "trajectories" / user_id / f"{config_file}__{model_id}___{problem_id}"
+ @classmethod
+ def _get_auto_correct(cls) -> list[ACS]:
+ return [
+ ACS("model", "agent.model.name"),
+ ACS("agent.model", "agent.model.name"),
+ ACS("per_instance_cost_limit", "agent.model.per_instance_cost_limit"),
+ ACS("model.per_instance_cost_limit", "agent.model.per_instance_cost_limit"),
+ ACS("config_file", "config"),
+ ACS(
+ "data_path",
+ help="--data_path is no longer support for SWE-A 1.0. Please check the tutorial and use one of the --problem_statement options, e.g., --problem_statement.github_url.",
+ ),
+ ACS(
+ "repo_path",
+ help="--repo_path is no longer support for SWE-A 1.0. Please check the tutorial and use one of the --env.repo options, e.g., --env.repo.github_url.",
+ ),
+ ]
+
class RunSingle:
def __init__(