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: Improve help for union types and more (#849)
Kilian Lieret0b2c6924a792
diff --git a/docs/faq.md b/docs/faq.mdindex a31ad406..88130100 100644--- a/docs/faq.md+++ b/docs/faq.md@@ -22,7 +22,7 @@ But you can always execute SWE-agent in the cloud.> I got a very long error message about various configuration options not working. What's up?This is probably because of union types.-See [this section](#union-types) for more information, but the short version is that some options (e.g., the repository or problem statement) can be specified in multiple ways, so we try every option until we find the one that works based on your inputs.+See [this section](usage/cl_tutorial.md#union-types) for more information, but the short version is that some options (e.g., the repository or problem statement) can be specified in multiple ways, so we try every option until we find the one that works based on your inputs.If none of them work, we throw an error which then tells you why we cannot initialize any of the types, so this will get somewhat long and confusing.{% include-markdown "_footer.md" %}\ No newline at end of filediff --git a/docs/usage/cl_tutorial.md b/docs/usage/cl_tutorial.mdindex b783c6aa..a8073334 100644--- a/docs/usage/cl_tutorial.md+++ b/docs/usage/cl_tutorial.md@@ -169,6 +169,20 @@ However, you can also explicitly specify the type of problem statement you wantIf everything works correctly, we try to initialize every option until we find the one that works based on your inputs (for example stopping at `TextProblemStatement` if you provided a `--problem_statement.text`).However, if none of them work, we throw an error which then tells you why we cannot initialize any of the types (so it will tell you that `github_url` is required for `GithubIssue`, even though you might not even have tried to work on a GitHub issue).+ <details>Example union type errors+ <summary>Example union type errors</summary>++ This is the output of running++ ```bash+ sweagent run --problem_statement.path="test" --problem_statement.github_url="asdf"+ ```++ ```+ --8<-- "docs/usage/union_type_error.txt"+ ```+ </details>+If you want to read more about how this works, check out the [pydantic docs](https://docs.pydantic.dev/latest/concepts/unions/).## Specifying the repositorydiff --git a/docs/usage/union_type_error.txt b/docs/usage/union_type_error.txtnew file mode 100644index 00000000..2c2acb4f--- /dev/null+++ b/docs/usage/union_type_error.txt@@ -0,0 +1,40 @@+╭───────────────────────────────────────────────────────────────────────────────────────────────────────────────────╮+│ Validation error │+│ │+│ The following errors are raised by Pydantic, trying to instantiate the configuration based on │+│ the merged configuration dictionary (see above). │+│ │+│ Every new indented block corresponds to a different error from Pydantic. │+│ The first line of each block is the attribute that failed validation, the following lines are the error messages. │+│ │+│ If you see many lines of errors, there are probably different ways to instantiate the same object (a union type). │+│ For example, there are different deployments with different options each. Pydantic is then trying │+│ one after the other and reporting the failures for each of them. │+│ │+│ 8 validation errors for RunSingleConfig │+│ agent.model │+│ Field required }, input_type=dict] │+│ For further information visit https://errors.pydantic.dev/2.9/v/missing │+│ problem_statement.TextProblemStatement.text │+│ Field required │+│ For further information visit https://errors.pydantic.dev/2.9/v/missing │+│ problem_statement.TextProblemStatement.path │+│ Extra inputs are not permitted │+│ For further information visit https://errors.pydantic.dev/2.9/v/extra_forbidden │+│ problem_statement.TextProblemStatement.github_url │+│ Extra inputs are not permitted │+│ For further information visit https://errors.pydantic.dev/2.9/v/extra_forbidden │+│ problem_statement.GithubIssue.path │+│ Extra inputs are not permitted │+│ For further information visit https://errors.pydantic.dev/2.9/v/extra_forbidden │+│ problem_statement.EmptyProblemStatement.path │+│ Extra inputs are not permitted │+│ For further information visit https://errors.pydantic.dev/2.9/v/extra_forbidden │+│ problem_statement.EmptyProblemStatement.github_url │+│ Extra inputs are not permitted │+│ For further information visit https://errors.pydantic.dev/2.9/v/extra_forbidden │+│ problem_statement.FileProblemStatement.github_url │+│ Extra inputs are not permitted │+│ For further information visit https://errors.pydantic.dev/2.9/v/extra_forbidden │+╰───────────────────────────────────────────────────────────────────────────────────────────────────────────────────╯+diff --git a/sweagent/run/common.py b/sweagent/run/common.pyindex 6e94a2cf..e588bb02 100644--- a/sweagent/run/common.py+++ b/sweagent/run/common.py@@ -3,6 +3,7 @@import jsonimport sysfrom argparse import ArgumentParser+from collections import defaultdictfrom pathlib import Pathfrom types import UnionTypefrom typing import Any@@ -54,6 +55,7 @@ The first line of each block is the attribute that failed validation, the followIf you see many lines of errors, there are probably different ways to instantiate the same object (a union type).For example, there are different deployments with different options each. Pydantic is then tryingone after the other and reporting the failures for each of them.+More on union types: [link=https://swe-agent.com/latest/usage/cl_tutorial/#union-types]https://swe-agent.com/latest/usage/cl_tutorial/#union-types[/link]"""_SETTING_ERROR_HINTS = """@@ -108,6 +110,48 @@ class ConfigHelper:return "\n\n".join(lines)+def _nested_dict():+ """Helper function to create nested dictionaries."""+ return defaultdict(_nested_dict)+++def _parse_args_to_nested_dict(args):+ """Parse the command-line arguments into a nested dictionary."""+ result = _nested_dict()++ i = 0+ while i < len(args):+ arg = args[i]+ if not arg.startswith("--"):+ i += 1+ continue++ # Handle --key=value format+ if "=" in arg:+ key, value = arg[2:].split("=", 1)+ # Handle --key value format+ else:+ key = arg[2:]+ i += 1+ if i >= len(args):+ break+ value = args[i]++ # Convert value to int if possible+ value = int(value) if value.isdigit() else value++ # Build nested dict structure+ keys = key.split(".")+ current = result+ for k in keys[:-1]:+ current = current[k]+ current[keys[-1]] = value++ i += 1++ return result++# todo: Parameterize type hintsclass BasicCLI:def __init__(self, config_type: type[BaseSettings], *, default_settings: bool = True, help_text: str | None = None):@@ -216,6 +260,9 @@ class BasicCLI:else:config_merged = {}+ # For informational purposes, we also merge in the command line options+ cl_options_dict = _parse_args_to_nested_dict(remaining_args)+# >>> Step 4: Bring together remaining arguments and the merged config to initialize the config object# This is done by CliApp.run from pydantic-settings@@ -224,11 +271,25 @@ class BasicCLI:except ValidationError as e:rich_print(Panel.fit(- "[red][bold]Merged configuration dictionary\n[/bold]"+ "[red][bold]Configuration from config files\n[/bold]""This is all the configuration that was provided from defaults, --config, and CLI arguments[/red]\n\n"+ yaml.dump(_shorten_strings(config_merged))))+ rich_print(+ Panel.fit(+ "[red][bold]Configuration from CLI arguments\n[/bold]"+ "This is all the configuration that was provided from the command line arguments[/red]\n\n"+ + yaml.dump(_shorten_strings(cl_options_dict))+ )+ )+ rich_print(+ Panel.fit(+ "[red][bold]Merged configuration\n[/bold]"+ "This is the merged configuration that was used to instantiate the config object[/red]\n\n"+ + yaml.dump(_shorten_strings(config_merged | cl_options_dict))+ )+ )rich_print(Panel.fit("[red][bold]Validation error[/bold]\n" + _VALIDATION_ERROR_HELP_TEXT + "[/red]\n" + str(e),