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]

Split up AgentConfig into Tools and Templates config

Kilian Lieret915019fd8ced
diff --git a/config/default.yaml b/config/default.yaml
index 7d7163e0..f2e766a9 100644
--- a/config/default.yaml
+++ b/config/default.yaml
@@ -1,85 +1,104 @@
agent:
- system_template: |-
- SETTING: You are an autonomous programmer, and you're working directly in the command line with a special interface.
-
- The special interface consists of a file editor that shows you {WINDOW} lines of a file at a time.
- In addition to typical bash commands, you can also use the following commands to help you navigate and edit files.
-
- COMMANDS:
- {command_docs}
-
- Please note that THE EDIT COMMAND REQUIRES PROPER INDENTATION.
- If you'd like to add the line ' print(x)' you must fully write that out, with all those spaces before the code! Indentation is important and code that is not indented correctly will fail and require fixing before it can be run.
-
- RESPONSE FORMAT:
- Your shell prompt is formatted as follows:
- (Open file: <path>) <cwd> $
-
- You need to format your output using two fields; discussion and command.
- Your output should always include _one_ discussion and _one_ command field EXACTLY as in the following example:
- DISCUSSION
- First I'll start by using ls to see what files are in the current directory. Then maybe we can look at some relevant files to see what they look like.
- ```
- ls -a
- ```
-
- You should only include a *SINGLE* command in the command section and then wait for a response from the shell before continuing with more discussion and commands. Everything you include in the DISCUSSION section will be saved for future reference.
- If you'd like to issue two commands at once, PLEASE DO NOT DO THAT! Please instead first submit just the first command, and then after receiving a response you'll be able to issue the second command.
- You're free to use any other bash commands you want (e.g. find, grep, cat, ls, cd) in addition to the special commands listed above.
- However, the environment does NOT support interactive session commands (e.g. python, vim), so please do not invoke them.
- instance_template: |-
- We're currently solving the following issue within our repository. Here's the issue text:
- ISSUE:
- {problem_statement}
-
- INSTRUCTIONS:
- Now, you're going to solve this issue on your own. Your terminal session has started and you're in the repository's root directory. You can use any bash commands or the special interface to help you. Edit all the files you need to and run any checks or tests that you want.
- Remember, YOU CAN ONLY ENTER ONE COMMAND AT A TIME. You should always wait for feedback after every command.
- When you're satisfied with all of the changes you've made, you can submit your changes to the code base by simply running the submit command.
- Note however that you cannot use any interactive session commands (e.g. python, vim) in this environment, but you can write scripts and run them. E.g. you can write a python script and then run it with `python <script_name>.py`.
-
- NOTE ABOUT THE EDIT COMMAND: Indentation really matters! When editing a file, make sure to insert appropriate indentation before each line!
-
- IMPORTANT TIPS:
- 1. Always start by trying to replicate the bug that the issues discusses.
- If the issue includes code for reproducing the bug, we recommend that you re-implement that in your environment, and run it to make sure you can reproduce the bug.
- Then start trying to fix it.
- When you think you've fixed the bug, re-run the bug reproduction script to make sure that the bug has indeed been fixed.
-
- If the bug reproduction script does not print anything when it successfully runs, we recommend adding a print("Script completed successfully, no errors.") command at the end of the file,
- so that you can be sure that the script indeed ran fine all the way through.
-
- 2. If you run a command and it doesn't work, try running a different command. A command that did not work once will not work the second time unless you modify it!
-
- 3. If you open a file and need to get to an area around a specific line that is not in the first 100 lines, say line 583, don't just use the scroll_down command multiple times. Instead, use the goto 583 command. It's much quicker.
-
- 4. If the bug reproduction script requires inputting/reading a specific file, such as buggy-input.png, and you'd like to understand how to input that file, conduct a search in the existing repo code, to see whether someone else has already done that. Do this by running the command: find_file "buggy-input.png" If that doesn't work, use the linux 'find' command.
-
- 5. Always make sure to look at the currently open file and the current working directory (which appears right after the currently open file). The currently open file might be in a different directory than the working directory! Note that some commands, such as 'create', open files, so they might change the current open file.
-
- 6. When editing files, it is easy to accidentally specify a wrong line number or to write code with incorrect indentation. Always check the code after you issue an edit to make sure that it reflects what you wanted to accomplish. If it didn't, issue another command to fix it.
-
-
- (Open file: {open_file})
- (Current directory: {working_dir})
- bash-$
- next_step_template: |-
- {observation}
- (Open file: {open_file})
- (Current directory: {working_dir})
- bash-$
- next_step_no_output_template: |-
- Your command ran successfully and did not produce any output.
- (Open file: {open_file})
- (Current directory: {working_dir})
- bash-$
- demonstration_template: |
- Here is a demonstration of how to correctly accomplish this task.
- It is included to show you how to correctly use the interface.
- You do not need to follow exactly what is done in the demonstration.
- --- DEMONSTRATION ---
- {demonstration}
- --- END OF DEMONSTRATION ---
+ templates:
+ system_template: |-
+ SETTING: You are an autonomous programmer, and you're working directly in the command line with a special interface.
+
+ The special interface consists of a file editor that shows you {WINDOW} lines of a file at a time.
+ In addition to typical bash commands, you can also use the following commands to help you navigate and edit files.
+
+ COMMANDS:
+ {command_docs}
+
+ Please note that THE EDIT COMMAND REQUIRES PROPER INDENTATION.
+ If you'd like to add the line ' print(x)' you must fully write that out, with all those spaces before the code! Indentation is important and code that is not indented correctly will fail and require fixing before it can be run.
+
+ RESPONSE FORMAT:
+ Your shell prompt is formatted as follows:
+ (Open file: <path>) <cwd> $
+
+ You need to format your output using two fields; discussion and command.
+ Your output should always include _one_ discussion and _one_ command field EXACTLY as in the following example:
+ DISCUSSION
+ First I'll start by using ls to see what files are in the current directory. Then maybe we can look at some relevant files to see what they look like.
+ ```
+ ls -a
+ ```
+
+ You should only include a *SINGLE* command in the command section and then wait for a response from the shell before continuing with more discussion and commands. Everything you include in the DISCUSSION section will be saved for future reference.
+ If you'd like to issue two commands at once, PLEASE DO NOT DO THAT! Please instead first submit just the first command, and then after receiving a response you'll be able to issue the second command.
+ You're free to use any other bash commands you want (e.g. find, grep, cat, ls, cd) in addition to the special commands listed above.
+ However, the environment does NOT support interactive session commands (e.g. python, vim), so please do not invoke them.
+ instance_template: |-
+ We're currently solving the following issue within our repository. Here's the issue text:
+ ISSUE:
+ {problem_statement}
+
+ INSTRUCTIONS:
+ Now, you're going to solve this issue on your own. Your terminal session has started and you're in the repository's root directory. You can use any bash commands or the special interface to help you. Edit all the files you need to and run any checks or tests that you want.
+ Remember, YOU CAN ONLY ENTER ONE COMMAND AT A TIME. You should always wait for feedback after every command.
+ When you're satisfied with all of the changes you've made, you can submit your changes to the code base by simply running the submit command.
+ Note however that you cannot use any interactive session commands (e.g. python, vim) in this environment, but you can write scripts and run them. E.g. you can write a python script and then run it with `python <script_name>.py`.
+
+ NOTE ABOUT THE EDIT COMMAND: Indentation really matters! When editing a file, make sure to insert appropriate indentation before each line!
+
+ IMPORTANT TIPS:
+ 1. Always start by trying to replicate the bug that the issues discusses.
+ If the issue includes code for reproducing the bug, we recommend that you re-implement that in your environment, and run it to make sure you can reproduce the bug.
+ Then start trying to fix it.
+ When you think you've fixed the bug, re-run the bug reproduction script to make sure that the bug has indeed been fixed.
+
+ If the bug reproduction script does not print anything when it successfully runs, we recommend adding a print("Script completed successfully, no errors.") command at the end of the file,
+ so that you can be sure that the script indeed ran fine all the way through.
+
+ 2. If you run a command and it doesn't work, try running a different command. A command that did not work once will not work the second time unless you modify it!
+
+ 3. If you open a file and need to get to an area around a specific line that is not in the first 100 lines, say line 583, don't just use the scroll_down command multiple times. Instead, use the goto 583 command. It's much quicker.
+
+ 4. If the bug reproduction script requires inputting/reading a specific file, such as buggy-input.png, and you'd like to understand how to input that file, conduct a search in the existing repo code, to see whether someone else has already done that. Do this by running the command: find_file "buggy-input.png" If that doesn't work, use the linux 'find' command.
+
+ 5. Always make sure to look at the currently open file and the current working directory (which appears right after the currently open file). The currently open file might be in a different directory than the working directory! Note that some commands, such as 'create', open files, so they might change the current open file.
+
+ 6. When editing files, it is easy to accidentally specify a wrong line number or to write code with incorrect indentation. Always check the code after you issue an edit to make sure that it reflects what you wanted to accomplish. If it didn't, issue another command to fix it.
+
+
+ (Open file: {open_file})
+ (Current directory: {working_dir})
+ bash-$
+ next_step_template: |-
+ {observation}
+ (Open file: {open_file})
+ (Current directory: {working_dir})
+ bash-$
+ next_step_no_output_template: |-
+ Your command ran successfully and did not produce any output.
+ (Open file: {open_file})
+ (Current directory: {working_dir})
+ bash-$
+ demonstration_template: |
+ Here is a demonstration of how to correctly accomplish this task.
+ It is included to show you how to correctly use the interface.
+ You do not need to follow exactly what is done in the demonstration.
+ --- DEMONSTRATION ---
+ {demonstration}
+ --- END OF DEMONSTRATION ---
+ demonstrations:
+ - trajectories/demonstrations/replay__marshmallow-code__marshmallow-1867__default_sys-env_window100__t-0.20__p-0.95__c-2.00__install-1/marshmallow-code__marshmallow-1867.traj
+ tools:
+ env_variables:
+ WINDOW: 100
+ OVERLAP: 2
+ CURRENT_LINE: 0
+ CURRENT_FILE: ""
+ SEARCH_RESULTS: ()
+ SEARCH_FILES: ()
+ SEARCH_INDEX: 0
+ command_files:
+ - config/commands/defaults.sh
+ - config/commands/search.sh
+ - config/commands/edit_linting.sh
+ - config/commands/_split_string.py
+ - config/commands/submit.sh
+ parse_command: ParseCommandDetailed
state_command:
name: state
code: |
@@ -92,21 +111,4 @@ agent:
fi
};
parse_function: ThoughtActionParser
- env_variables:
- WINDOW: 100
- OVERLAP: 2
- CURRENT_LINE: 0
- CURRENT_FILE: ""
- SEARCH_RESULTS: ()
- SEARCH_FILES: ()
- SEARCH_INDEX: 0
- command_files:
- - config/commands/defaults.sh
- - config/commands/search.sh
- - config/commands/edit_linting.sh
- - config/commands/_split_string.py
- - config/commands/submit.sh
- parse_command: ParseCommandDetailed
history_processor: Last5Observations
- demonstrations:
- - trajectories/demonstrations/replay__marshmallow-code__marshmallow-1867__default_sys-env_window100__t-0.20__p-0.95__c-2.00__install-1/marshmallow-code__marshmallow-1867.traj
diff --git a/config/default_from_url.yaml b/config/default_from_url.yaml
index 10dffb88..ea91077e 100644
--- a/config/default_from_url.yaml
+++ b/config/default_from_url.yaml
@@ -1,87 +1,90 @@
agent:
- system_template: |-
- SETTING: You are an autonomous programmer, and you're working directly in the command line with a special interface.
-
- The special interface consists of a file editor that shows you {WINDOW} lines of a file at a time.
- In addition to typical bash commands, you can also use the following commands to help you navigate and edit files.
-
- COMMANDS:
- {command_docs}
-
- Please note that THE EDIT COMMAND REQUIRES PROPER INDENTATION.
- If you'd like to add the line ' print(x)' you must fully write that out, with all those spaces before the code! Indentation is important and code that is not indented correctly will fail and require fixing before it can be run.
-
- RESPONSE FORMAT:
- Your shell prompt is formatted as follows:
- (Open file: <path>) <cwd> $
-
- You need to format your output using two fields; discussion and command.
- Your output should always include _one_ discussion and _one_ command field EXACTLY as in the following example:
- DISCUSSION
- First I'll start by using ls to see what files are in the current directory. Then maybe we can look at some relevant files to see what they look like.
- ```
- ls -a
- ```
-
- You should only include a *SINGLE* command in the command section and then wait for a response from the shell before continuing with more discussion and commands. Everything you include in the DISCUSSION section will be saved for future reference.
- If you'd like to issue two commands at once, PLEASE DO NOT DO THAT! Please instead first submit just the first command, and then after receiving a response you'll be able to issue the second command.
- You're free to use any other bash commands you want (e.g. find, grep, cat, ls, cd) in addition to the special commands listed above.
- However, the environment does NOT support interactive session commands (e.g. python, vim), so please do not invoke them.
- instance_template: |-
- We're currently solving the following issue within our repository. Here's the issue text:
- ISSUE:
- {problem_statement}
-
- INSTRUCTIONS:
- Now, you're going to solve this issue on your own. Your terminal session has started and you're in the repository's root directory. You can use any bash commands or the special interface to help you. Edit all the files you need to and run any checks or tests that you want.
- Remember, YOU CAN ONLY ENTER ONE COMMAND AT A TIME. You should always wait for feedback after every command.
- When you're satisfied with all of the changes you've made, you can submit your changes to the code base by simply running the submit command.
- Note however that you cannot use any interactive session commands (e.g. python, vim) in this environment, but you can write scripts and run them. E.g. you can write a python script and then run it with `python <script_name>.py`.
-
- NOTE ABOUT THE EDIT COMMAND: Indentation really matters! When editing a file, make sure to insert appropriate indentation before each line!
-
- IMPORTANT TIPS:
- 1. Always start by trying to replicate the bug that the issues discusses.
- If the issue includes code for reproducing the bug, we recommend that you re-implement that in your environment, and run it to make sure you can reproduce the bug.
- Then start trying to fix it.
- When you think you've fixed the bug, re-run the bug reproduction script to make sure that the bug has indeed been fixed.
-
- If the bug reproduction script does not print anything when it successfully runs, we recommend adding a print("Script completed successfully, no errors.") command at the end of the file,
- so that you can be sure that the script indeed ran fine all the way through.
-
- 2. If you run a command and it doesn't work, try running a different command. A command that did not work once will not work the second time unless you modify it!
-
- 3. If you open a file and need to get to an area around a specific line that is not in the first 100 lines, say line 583, don't just use the scroll_down command multiple times. Instead, use the goto 583 command. It's much quicker.
-
- 4. If the bug reproduction script requires inputting/reading a specific file, such as buggy-input.png, and you'd like to understand how to input that file, conduct a search in the existing repo code, to see whether someone else has already done that. Do this by running the command: find_file "buggy-input.png" If that doesn't work, use the linux 'find' command.
-
- 5. Always make sure to look at the currently open file and the current working directory (which appears right after the currently open file). The currently open file might be in a different directory than the working directory! Note that some commands, such as 'create', open files, so they might change the current open file.
-
- 6. When editing files, it is easy to accidentally specify a wrong line number or to write code with incorrect indentation. Always check the code after you issue an edit to make sure that it reflects what you wanted to accomplish. If it didn't, issue another command to fix it.
-
- 7. It may be necessary to install the repository from source before you can run code. Please think about how to install the environment from the repository directory if you need to do so.
-
-
- (Open file: {open_file})
- (Current directory: {working_dir})
- bash-$
- next_step_template: |-
- {observation}
- (Open file: {open_file})
- (Current directory: {working_dir})
- bash-$
- next_step_no_output_template: |-
- Your command ran successfully and did not produce any output.
- (Open file: {open_file})
- (Current directory: {working_dir})
- bash-$
- demonstration_template: |
- Here is a demonstration of how to correctly accomplish this task.
- It is included to show you how to correctly use the interface.
- You do not need to follow exactly what is done in the demonstration.
- --- DEMONSTRATION ---
- {demonstration}
- --- END OF DEMONSTRATION ---
+ templates:
+ system_template: |-
+ SETTING: You are an autonomous programmer, and you're working directly in the command line with a special interface.
+
+ The special interface consists of a file editor that shows you {WINDOW} lines of a file at a time.
+ In addition to typical bash commands, you can also use the following commands to help you navigate and edit files.
+
+ COMMANDS:
+ {command_docs}
+
+ Please note that THE EDIT COMMAND REQUIRES PROPER INDENTATION.
+ If you'd like to add the line ' print(x)' you must fully write that out, with all those spaces before the code! Indentation is important and code that is not indented correctly will fail and require fixing before it can be run.
+
+ RESPONSE FORMAT:
+ Your shell prompt is formatted as follows:
+ (Open file: <path>) <cwd> $
+
+ You need to format your output using two fields; discussion and command.
+ Your output should always include _one_ discussion and _one_ command field EXACTLY as in the following example:
+ DISCUSSION
+ First I'll start by using ls to see what files are in the current directory. Then maybe we can look at some relevant files to see what they look like.
+ ```
+ ls -a
+ ```
+
+ You should only include a *SINGLE* command in the command section and then wait for a response from the shell before continuing with more discussion and commands. Everything you include in the DISCUSSION section will be saved for future reference.
+ If you'd like to issue two commands at once, PLEASE DO NOT DO THAT! Please instead first submit just the first command, and then after receiving a response you'll be able to issue the second command.
+ You're free to use any other bash commands you want (e.g. find, grep, cat, ls, cd) in addition to the special commands listed above.
+ However, the environment does NOT support interactive session commands (e.g. python, vim), so please do not invoke them.
+ instance_template: |-
+ We're currently solving the following issue within our repository. Here's the issue text:
+ ISSUE:
+ {problem_statement}
+
+ INSTRUCTIONS:
+ Now, you're going to solve this issue on your own. Your terminal session has started and you're in the repository's root directory. You can use any bash commands or the special interface to help you. Edit all the files you need to and run any checks or tests that you want.
+ Remember, YOU CAN ONLY ENTER ONE COMMAND AT A TIME. You should always wait for feedback after every command.
+ When you're satisfied with all of the changes you've made, you can submit your changes to the code base by simply running the submit command.
+ Note however that you cannot use any interactive session commands (e.g. python, vim) in this environment, but you can write scripts and run them. E.g. you can write a python script and then run it with `python <script_name>.py`.
+
+ NOTE ABOUT THE EDIT COMMAND: Indentation really matters! When editing a file, make sure to insert appropriate indentation before each line!
+
+ IMPORTANT TIPS:
+ 1. Always start by trying to replicate the bug that the issues discusses.
+ If the issue includes code for reproducing the bug, we recommend that you re-implement that in your environment, and run it to make sure you can reproduce the bug.
+ Then start trying to fix it.
+ When you think you've fixed the bug, re-run the bug reproduction script to make sure that the bug has indeed been fixed.
+
+ If the bug reproduction script does not print anything when it successfully runs, we recommend adding a print("Script completed successfully, no errors.") command at the end of the file,
+ so that you can be sure that the script indeed ran fine all the way through.
+
+ 2. If you run a command and it doesn't work, try running a different command. A command that did not work once will not work the second time unless you modify it!
+
+ 3. If you open a file and need to get to an area around a specific line that is not in the first 100 lines, say line 583, don't just use the scroll_down command multiple times. Instead, use the goto 583 command. It's much quicker.
+
+ 4. If the bug reproduction script requires inputting/reading a specific file, such as buggy-input.png, and you'd like to understand how to input that file, conduct a search in the existing repo code, to see whether someone else has already done that. Do this by running the command: find_file "buggy-input.png" If that doesn't work, use the linux 'find' command.
+
+ 5. Always make sure to look at the currently open file and the current working directory (which appears right after the currently open file). The currently open file might be in a different directory than the working directory! Note that some commands, such as 'create', open files, so they might change the current open file.
+
+ 6. When editing files, it is easy to accidentally specify a wrong line number or to write code with incorrect indentation. Always check the code after you issue an edit to make sure that it reflects what you wanted to accomplish. If it didn't, issue another command to fix it.
+
+ 7. It may be necessary to install the repository from source before you can run code. Please think about how to install the environment from the repository directory if you need to do so.
+
+
+ (Open file: {open_file})
+ (Current directory: {working_dir})
+ bash-$
+ next_step_template: |-
+ {observation}
+ (Open file: {open_file})
+ (Current directory: {working_dir})
+ bash-$
+ next_step_no_output_template: |-
+ Your command ran successfully and did not produce any output.
+ (Open file: {open_file})
+ (Current directory: {working_dir})
+ bash-$
+ demonstration_template: |
+ Here is a demonstration of how to correctly accomplish this task.
+ It is included to show you how to correctly use the interface.
+ You do not need to follow exactly what is done in the demonstration.
+ --- DEMONSTRATION ---
+ {demonstration}
+ --- END OF DEMONSTRATION ---
+ demonstrations:
+ - trajectories/demonstrations/replay__marshmallow-code__marshmallow-1867__default__t-0.20__p-0.95__c-2.00__install-1___install_from_source/marshmallow-code__marshmallow-1867.traj
state_command:
name: state
code: |
@@ -94,21 +97,20 @@ agent:
fi
};
parse_function: ThoughtActionParser
- env_variables:
- WINDOW: 100
- OVERLAP: 2
- CURRENT_LINE: 0
- CURRENT_FILE: ''
- SEARCH_RESULTS: ()
- SEARCH_FILES: ()
- SEARCH_INDEX: 0
- command_files:
- - config/commands/defaults.sh
- - config/commands/search.sh
- - config/commands/edit_linting.sh
- - config/commands/_split_string.py
- - config/commands/submit.sh
- parse_command: ParseCommandDetailed
+ tools:
+ env_variables:
+ WINDOW: 100
+ OVERLAP: 2
+ CURRENT_LINE: 0
+ CURRENT_FILE: ''
+ SEARCH_RESULTS: ()
+ SEARCH_FILES: ()
+ SEARCH_INDEX: 0
+ command_files:
+ - config/commands/defaults.sh
+ - config/commands/search.sh
+ - config/commands/edit_linting.sh
+ - config/commands/_split_string.py
+ - config/commands/submit.sh
+ parse_command: ParseCommandDetailed
history_processor: Last5Observations
- demonstrations:
- - trajectories/demonstrations/replay__marshmallow-code__marshmallow-1867__default__t-0.20__p-0.95__c-2.00__install-1___install_from_source/marshmallow-code__marshmallow-1867.traj
diff --git a/sweagent/agent/agents.py b/sweagent/agent/agents.py
index 1ebeb14a..5797b660 100644
--- a/sweagent/agent/agents.py
+++ b/sweagent/agent/agents.py
@@ -25,8 +25,6 @@ from sweagent.agent.models import (
get_model,
)
from sweagent.agent.parsing import FormatError, ParseFunction
-
-# from sweagent.agent.summarizer import SummarizerConfig
from sweagent.agent.utils import _guard_multiline_input
from sweagent.environment.config.problem_statement import ProblemStatement, ProblemStatementConfig
from sweagent.environment.swe_env import SWEEnv
@@ -35,8 +33,7 @@ from sweagent.utils.config import _convert_paths_to_abspath
from sweagent.utils.log import get_logger
-# todo: factor out tools config and potentially try to only give it to SWEEnv. Agent should only need allow-lists and the final tools documentation
-class AgentConfig(BaseModel):
+class TemplateConfig(BaseModel):
system_template: str = ""
instance_template: str = ""
next_step_template: str = None # type: ignore
@@ -47,26 +44,26 @@ class AgentConfig(BaseModel):
strategy_template: str | None = None
demonstration_template: str | None = None
- # Paths to demonstrations. If path is not absolute, it is assumed to be
- # relative to the SWE_AGENT_CONFIG_ROOT (if set) or the SWE-agent repository root
+
demonstrations: list[str] = field(default_factory=list)
- put_demos_in_history: bool = False # if True, add demonstration to history instead of as a single message
- # defaults to format_error_template in ParseFunction
- format_error_template: str = None # type: ignore
- # Paths to command files. If path is not absolute, it is assumed to be
- # relative to the SWE_AGENT_CONFIG_ROOT (if set) or the SWE-agent repository root
- command_files: list[str] = field(default_factory=list)
- env_variables: dict[str, Any] = field(default_factory=dict)
- util_functions: list[str] = field(default_factory=list)
- submit_command: str = "submit"
- parse_function: Any = "ThoughtActionParser"
- parse_command: Any = "ParseCommandBash"
- history_processor: Any = "DefaultHistoryProcessor"
- history_processor_args: dict[str, Any] = field(default_factory=dict)
- command_docs: str = None # type: ignore
- # summarizer_config: SummarizerConfig = field(default_factory=SummarizerConfig)
+ """Paths to demonstrations. If path is not absolute, it is assumed to be
+ relative to the SWE_AGENT_CONFIG_ROOT (if set) or the SWE-agent repository root
+ """
+
+ put_demos_in_history: bool = False
+ """If True, add demonstration to history instead of as a single message"""
+
+ def model_post_init(self, __context):
+ self.demonstrations = _convert_paths_to_abspath(self.demonstrations)
+ if self.next_step_template is None:
+ self.next_step_template = self.instance_template
+ if self.next_step_no_output_template is None:
+ self.next_step_no_output_template = self.next_step_template
+
+
+class ToolFilterConfig(BaseModel):
blocklist_error_template: str = "Interactive operation '{name}' is not supported by this environment"
- blocklist: tuple[str, ...] = (
+ blocklist: list[str] = [
"vim",
"vi",
"emacs",
@@ -74,8 +71,8 @@ class AgentConfig(BaseModel):
"nohup",
"git",
"gdb",
- )
- blocklist_standalone: tuple[str, ...] = (
+ ]
+ blocklist_standalone: list[str] = [
"python",
"python3",
"ipython",
@@ -90,25 +87,29 @@ class AgentConfig(BaseModel):
"emacs",
"nano",
"su",
- )
+ ]
+ # todo: probably rename this
block_unless_regex: dict[str, str] = field(
default_factory=lambda: {
"radare2": r"\b(?:radare2)\b.*\s+-c\s+.*",
"r2": r"\b(?:radare2)\b.*\s+-c\s+.*",
}
)
- # Should extract environment state in a json readable form
- state_command: Command = field(
- default_factory=lambda: Command(
- name="state",
- code="""state() {
- echo '{"working_dir": "'$(realpath --relative-to=$ROOT/.. $PWD)'"}';
- };""",
- )
- )
- # _subroutines: dict[str, Subroutine] = field(default_factory=dict)
- # subroutine_types: list[Subroutine] = field(default_factory=list)
- model: ModelConfig = field(default_factory=lambda: ModelConfig(name="gpt4"))
+
+
+class ToolConfig(BaseModel):
+ filter: ToolFilterConfig = field(default_factory=ToolFilterConfig)
+ command_files: list[str] = field(default_factory=list)
+ env_variables: dict[str, Any] = field(default_factory=dict)
+ util_functions: list[str] = field(default_factory=list)
+ submit_command: str = "submit"
+ parse_function: Any = "ThoughtActionParser"
+ parse_command: Any = "ParseCommandBash"
+
+ format_error_template: str = None # type: ignore
+ """Defaults to format_error_template in ParseFunction"""
+
+ command_docs: str = None # type: ignore
multi_line_command_endings: dict[str, str] = field(default_factory=dict)
submit_command_end_name: str | None = None
@@ -126,14 +127,7 @@ class AgentConfig(BaseModel):
def model_post_init(self, __context):
self.command_files = _convert_paths_to_abspath(self.command_files)
- self.demonstrations = _convert_paths_to_abspath(self.demonstrations)
- if self.next_step_template is None:
- self.next_step_template = self.instance_template
- if self.next_step_no_output_template is None:
- self.next_step_no_output_template = self.next_step_template
-
- # object.__setattr__(self, "parse_command", ParseCommand.get(self.parse_command))
parse_command = ParseCommand.get(self.parse_command)
_commands = []
for file in self.command_files:
@@ -146,10 +140,7 @@ class AgentConfig(BaseModel):
_commands.extend(commands)
multi_line_command_endings = {
- command.name: command.end_name
- # for command in [*self._commands, *self._subroutines.values()]
- for command in _commands
- if command.end_name is not None
+ command.name: command.end_name for command in _commands if command.end_name is not None
}
self.multi_line_command_endings = multi_line_command_endings
self.command_docs = parse_command.generate_command_docs(
@@ -158,25 +149,40 @@ class AgentConfig(BaseModel):
# self.subroutine_types,
**self.env_variables,
)
- # object.__setattr__(self, "parse_function", ParseFunction.get(self.parse_function))
parse_function = ParseFunction.get(self.parse_function)
if self.format_error_template is None:
self.format_error_template = parse_function.format_error_template
self.format_error_template = self.format_error_template.format(**self.__dict__)
for command in _commands:
+ print(command.name, self.submit_command)
if command.name == self.submit_command:
+ print("yay")
self.submit_command_end_name = command.end_name
break
- # object.__setattr__(
- # self,
- # "history_processor",
- # HistoryProcessor.get(self.history_processor, **self.history_processor_args),
- # )
- # if "WINDOW" in self.env_variables:
- # window_size = self.env_variables["WINDOW"]
- # if self.summarizer_config.window_length < int(window_size):
- # msg = f"Summarizer window length is set to {self.summarizer_config.window_length} which is less than the window length {window_size}"
- # raise ValueError(msg)
+ else:
+ print(_commands)
+
+
+# todo: factor out tools config and potentially try to only give it to SWEEnv. Agent should only need allow-lists and the final tools documentation
+class AgentConfig(BaseModel):
+ templates: TemplateConfig = field(default_factory=TemplateConfig)
+ tools: ToolConfig = field(default_factory=ToolConfig)
+
+ # todo: Why can't we just configure this in the same way as the models?
+ history_processor: Any = "DefaultHistoryProcessor"
+ history_processor_args: dict[str, Any] = field(default_factory=dict)
+
+ state_command: Command = field(
+ default_factory=lambda: Command(
+ name="state",
+ code="""state() {
+ echo '{"working_dir": "'$(realpath --relative-to=$ROOT/.. $PWD)'"}';
+ };""",
+ )
+ )
+ """Should extract environment state in a json readable form"""
+
+ model: ModelConfig = field(default_factory=lambda: ModelConfig(name="gpt4"))
# todo: separate out from_config. In particular separate out model (as a class, etc.)
@@ -188,16 +194,12 @@ class Agent:
self.name = name
# todo: currently only used to get the model name, so might remove this later
self._args = args
- # self.model = get_model(args.model, args.config._commands + args.config.subroutine_types)
- self.model = get_model(args.model, args.commands)
- # self.summarizer_model = get_model(
- # args.summarizer_config.model if args.summarizer_config.model is not None else args.model
- # )
+ self.model = get_model(args.model, args.tools.commands)
self.config = args
self.system_args = {
- "command_docs": self.config.command_docs,
- **self.config.env_variables,
+ "command_docs": self.config.tools.command_docs,
+ **self.config.tools.env_variables,
}
self._parse_command_patterns()
self.last_container_id = None
@@ -226,7 +228,7 @@ class Agent:
self._history_processor = HistoryProcessor.get(
self.config.history_processor, **self.config.history_processor_args
)
- self._parse_function = ParseFunction.get(self.config.parse_function)
+ self._parse_function = ParseFunction.get(self.config.tools.parse_function)
self._chook = CombinedAgentHook()
@@ -309,15 +311,18 @@ class Agent:
# self.model = get_model(self._args.model, self.config._commands + self.config.subroutine_types)
# fixme: This doesn't reset total cost
assert self._problem_statement is not None
- system_msg = self.config.system_template.format(
+ system_msg = self.config.templates.system_template.format(
**self.system_args, problem_statement=self._problem_statement.get_problem_statement()
)
self.logger.info(f"SYSTEM ({self.name})\n{system_msg}")
self._append_history(HistoryItem({"role": "system", "content": system_msg, "agent": self.name}))
# todo: This should be moved somewhere
if "history_to_messages" in dir(self.model):
- for demonstration_path in self.config.demonstrations:
- if self.config.demonstration_template is None and not self.config.put_demos_in_history:
+ for demonstration_path in self.config.templates.demonstrations:
+ if (
+ self.config.templates.demonstration_template is None
+ and not self.config.templates.put_demos_in_history
+ ):
msg = "Cannot use demonstrations without a demonstration template or put_demos_in_history=True"
raise ValueError(msg)
@@ -330,8 +335,8 @@ class Agent:
if ("agent" not in entry) or ("agent" in entry and entry["agent"] == self.name)
]
- if self.config.put_demos_in_history:
- if self.config.demonstration_template is not None:
+ if self.config.templates.put_demos_in_history:
+ if self.config.templates.demonstration_template is not None:
self.logger.warning("Demonstration template is ignored for put_demos_in_history=True")
# Add demonstration to history directly as separate messages
for entry in demo_history:
@@ -344,8 +349,8 @@ class Agent:
demo_history,
is_demonstration=True,
)
- assert self.config.demonstration_template is not None
- demonstration = self.config.demonstration_template.format(demonstration=demo_message)
+ assert self.config.templates.demonstration_template is not None
+ demonstration = self.config.templates.demonstration_template.format(demonstration=demo_message)
self._append_history(
{
"agent": self.name,
@@ -409,7 +414,7 @@ class Agent:
patterns = {
k: v
for k, v in self.command_patterns.items()
- if k in self.config.multi_line_command_endings or k == self.config.submit_command
+ if k in self.config.tools.multi_line_command_endings or k == self.config.tools.submit_command
}
matches = list()
for _, pat in patterns.items():
@@ -433,7 +438,7 @@ class Agent:
"""Sets self.command_patterns and self.subroutine_patterns"""
self.command_patterns = dict()
- for command in self.config.commands:
+ for command in self.config.tools.commands:
if command.end_name is not None:
pat = re.compile(
rf"^\s*({command.name})\s*(.*?)^({command.end_name})\s*$",
@@ -443,14 +448,11 @@ class Agent:
else:
pat = re.compile(rf"^\s*({command.name})\s*(.*?)$", re.MULTILINE)
self.command_patterns[command.name] = pat
- if hasattr(self.config, "submit_command_end_name"):
- submit_pat = re.compile(
- rf"^\s*({self.config.submit_command})\s*(.*?)^({self.config.submit_command_end_name})\s*$",
- re.DOTALL | re.MULTILINE,
- )
- else:
- submit_pat = re.compile(rf"^\s*({self.config.submit_command})(\s*)$", re.MULTILINE) # group 2 is nothing
- self.command_patterns[self.config.submit_command] = submit_pat
+ submit_pat = re.compile(
+ rf"^\s*({self.config.tools.submit_command})\s*(.*?)^({self.config.tools.submit_command_end_name})\s*$",
+ re.DOTALL | re.MULTILINE,
+ )
+ self.command_patterns[self.config.tools.submit_command] = submit_pat
def forward(self, observation: str, available_actions: list[str], state: dict[str, str]) -> tuple[str, str, str]:
"""Forwards the model, i.e., queries the model with the current trajectory and observation.
@@ -494,15 +496,15 @@ class Agent:
# Determine observation template based on what prior observation was
if self.history[-1]["role"] == "system" or self.history[-1].get("is_demo", False):
# Show instance template if prev. obs. was initial system message
- templates = [self.config.instance_template]
- if self.config.strategy_template is not None:
- templates.append(self.config.strategy_template)
+ templates = [self.config.templates.instance_template]
+ if self.config.templates.strategy_template is not None:
+ templates.append(self.config.templates.strategy_template)
elif observation is None or observation.strip() == "":
# Show no output template if observation content was empty
- templates = [self.config.next_step_no_output_template]
+ templates = [self.config.templates.next_step_no_output_template]
else:
# Show standard output template if there is observation content
- templates = [self.config.next_step_template]
+ templates = [self.config.templates.next_step_template]
# Populate selected template(s) with information (e.g., issue, arguments, state)
messages = []
@@ -528,7 +530,7 @@ class Agent:
def retry_after_format_fail(self, output: str) -> str:
"""Ask the model to correct (without committing to persistent history) after a malformatted model output"""
- format_error_template = self.config.format_error_template
+ format_error_template = self.config.tools.format_error_template
self.logger.warning(f"MALFORMED OUTPUT\n{output}")
self.logger.warning(f"FORMAT ERROR\n{format_error_template}")
@@ -542,7 +544,7 @@ class Agent:
def retry_after_blocklist_fail(self, output: str, action: str) -> str:
"""Ask the model to correct (without committing to persistent history) after a disallowed command"""
name = action.strip().split()[0]
- blocklist_error_message = self.config.blocklist_error_template.format(name=name)
+ blocklist_error_message = self.config.tools.filter.blocklist_error_template.format(name=name)
self.logger.warning(f"BLOCKLISTED OUTPUT\n{output}")
self.logger.warning(f"BLOCKLIST ERROR\n{blocklist_error_message}")
@@ -559,11 +561,13 @@ class Agent:
if len(names) == 0:
return False
name = names[0]
- if name in self.config.blocklist:
+ if name in self.config.tools.filter.blocklist:
return True
- if name in self.config.blocklist_standalone and name == action.strip():
+ if name in self.config.tools.filter.blocklist_standalone and name == action.strip():
return True
- if name in self.config.block_unless_regex and not re.search(self.config.block_unless_regex[name], action):
+ if name in self.config.tools.filter.block_unless_regex and not re.search(
+ self.config.tools.filter.block_unless_regex[name], action
+ ):
return True
return False
@@ -587,7 +591,7 @@ class Agent:
elif self.model.args.name == "human_thought":
thought, action = ParseFunction.get("ThoughtActionParser")(
output,
- self.config.commands,
+ self.config.tools.commands,
strict=False,
)
return thought, action, output
@@ -598,7 +602,7 @@ class Agent:
try:
thought, action = self._parse_function(
output,
- self.config.commands,
+ self.config.tools.commands,
strict=False,
)
except KeyboardInterrupt:
@@ -650,7 +654,7 @@ class Agent:
)
def init_environment_vars(self):
- self.set_environment_vars(self.config.env_variables)
+ self.set_environment_vars(self.config.tools.env_variables)
def set_environment_vars(self, env_variables: dict[str, Any]) -> None:
"""Sets environment variables in the container and for example makes sure
@@ -674,7 +678,7 @@ class Agent:
self.logger.warning(f"Failed to set environment variables: {traceback.format_exc()}")
raise e
command_files = list()
- for file in self.config.command_files:
+ for file in self.config.tools.command_files:
datum = dict()
with open(file) as f:
contents = f.read()
diff --git a/tests/test_run_single.py b/tests/test_run_single.py
index 744357e0..6ae730e1 100644
--- a/tests/test_run_single.py
+++ b/tests/test_run_single.py
@@ -28,21 +28,21 @@ def test_run_single_raises_exception():
@pytest.fixture
def agent_config_with_commands():
ac = AgentConfig()
- ac.command_files = [
- f"config/commands/{name}"
- for name in ["defaults.sh", "search.sh", "edit_linting.sh", "_split_string.py", "submit.sh"]
- ]
+ ac.tools.command_files = [f"config/commands/{name}" for name in ["submit.sh"]]
+ # Make sure dependent properties are set
+ ac.tools.model_post_init(None)
return ac
@pytest.mark.slow
-def test_run_ies(agent_config_with_commands):
+def test_run_ies(tmpdir, agent_config_with_commands):
# ies = instant empty submit
ac = agent_config_with_commands
ac.model.name = "instant_empty_submit"
rsc = RunSingleConfig(
env=EnvironmentConfig(),
agent=ac,
+ traj_dir=tmpdir,
)
rs = RunSingle.from_config(rsc)
rs.run()