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]

Update tests for tool bundles

carlosejimenez6eee22c762d7
diff --git a/config/default_from_url.yaml b/config/default_from_url.yaml
index dbdd9dd4..526ba492 100644
--- a/config/default_from_url.yaml
+++ b/config/default_from_url.yaml
@@ -89,29 +89,17 @@ agent:
env_variables:
WINDOW: 100
OVERLAP: 2
- CURRENT_LINE: 0
- CURRENT_FILE: ''
- 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_function:
- type: thought_action
+ bundles:
+ - tools/defaults
+ - tools/search
+ - tools/edit_linting
+ - tools/submit
+ - tools/state
parse_command:
type: detailed
- state_command:
- name: state
- code: |
- state() {
- local working_dir="$PWD";
- if [ -z "$CURRENT_FILE" ]; then
- echo '{"open_file": "n/a", "working_dir": "'$working_dir'"}';
- else
- echo '{"open_file": "'$(realpath "$CURRENT_FILE")'", "working_dir": "'$working_dir'"}';
- fi
- };
+ state_command: _state
+ parse_function:
+ type: xml_thought_action
history_processor:
type: last_n_observations
n: 5
diff --git a/config/default_xml.yaml b/config/default_xml.yaml
index 2f857ff3..d728cfee 100644
--- a/config/default_xml.yaml
+++ b/config/default_xml.yaml
@@ -1,105 +1,96 @@
-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.
- <command>
- ls -a
- </command>
-
- 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-$
-put_demos_in_history: true
-state_command:
- name: state
- code: |
- state() {
- local working_dir="$PWD";
- if [ -z "$CURRENT_FILE" ]; then
- echo '{"open_file": "n/a", "working_dir": "'$working_dir'"}';
- else
- echo '{"open_file": "'$(realpath "$CURRENT_FILE")'", "working_dir": "'$working_dir'"}';
- fi
- };
-parse_function: XMLThoughtActionParser
-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__xml_sys-env_window100__t-0.20__p-0.95__c-2.00__install-1/marshmallow-code__marshmallow-1867.traj
+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.
+ <command>
+ ls -a
+ </command>
+
+ 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-$
+ put_demos_in_history: true
+ tools:
+ env_variables:
+ WINDOW: 100
+ OVERLAP: 2
+ bundles:
+ - tools/defaults
+ - tools/search
+ - tools/edit_linting
+ - tools/submit
+ - tools/state
+ parse_command:
+ type: detailed
+ state_command: _state
+ parse_function:
+ type: xml_thought_action
+ history_processor:
+ type: last_n_observations
+ n: 5
+ demonstrations:
+ - trajectories/demonstrations/replay__marshmallow-code__marshmallow-1867__xml_sys-env_window100__t-0.20__p-0.95__c-2.00__install-1/marshmallow-code__marshmallow-1867.traj
diff --git a/sweagent/tools/tools.py b/sweagent/tools/tools.py
index 5feca373..5d9f2771 100644
--- a/sweagent/tools/tools.py
+++ b/sweagent/tools/tools.py
@@ -227,7 +227,7 @@ class ToolHandler:
return {}
output = env.communicate(self.config.state_command, check=True).strip()
if not output:
- self.logger.warning("State command %s returned empty output", self.config.state_command.name)
+ self.logger.warning("State command %s returned empty output", self.config.state_command)
return {}
try:
state = json.loads(output)
diff --git a/tests/test_commands.py b/tests/test_commands.py
index 876b4a14..88d83e11 100644
--- a/tests/test_commands.py
+++ b/tests/test_commands.py
@@ -1,11 +1,9 @@
from __future__ import annotations
-import sys
+from tests.utils import make_python_tool_importable
-sys.path.append("config/commands")
-
-
-from _split_string import Flake8Error, _update_previous_errors, format_flake8_output
+make_python_tool_importable("tools/edit_linting/bin/_split_string", "split_string")
+from split_string import Flake8Error, _update_previous_errors, format_flake8_output
def test_partition_flake8_line():
diff --git a/tests/test_data/trajectories/gpt4__swe-agent-test-repo__default_from_url__t-0.00__p-0.95__c-3.00__install-1/args.yaml b/tests/test_data/trajectories/gpt4__swe-agent-test-repo__default_from_url__t-0.00__p-0.95__c-3.00__install-1/args.yaml
index f80221e0..d155d36e 100644
--- a/tests/test_data/trajectories/gpt4__swe-agent-test-repo__default_from_url__t-0.00__p-0.95__c-3.00__install-1/args.yaml
+++ b/tests/test_data/trajectories/gpt4__swe-agent-test-repo__default_from_url__t-0.00__p-0.95__c-3.00__install-1/args.yaml
@@ -445,21 +445,7 @@ agent:
parse_command: {}
parse_function: {}
put_demos_in_history: false
- state_command:
- arguments: null
- code: |
- state() {
- local working_dir="$PWD";
- if [ -z $CURRENT_FILE ]; then
- echo '{"open_file": "n/a", "working_dir": "'$working_dir'"}';
- else
- echo '{"open_file": "'$(realpath $CURRENT_FILE)'", "working_dir": "'$working_dir'"}';
- fi
- };
- docstring: null
- end_name: null
- name: state
- signature: null
+ state_command: _state
strategy_template: null
submit_command: submit
subroutine_types: []
diff --git a/tests/test_data/trajectories/gpt4__swe-agent__test-repo__default_from_url__t-0.00__p-0.95__c-3.00__install-1/args.yaml b/tests/test_data/trajectories/gpt4__swe-agent__test-repo__default_from_url__t-0.00__p-0.95__c-3.00__install-1/args.yaml
index ab65af14..0fde8375 100644
--- a/tests/test_data/trajectories/gpt4__swe-agent__test-repo__default_from_url__t-0.00__p-0.95__c-3.00__install-1/args.yaml
+++ b/tests/test_data/trajectories/gpt4__swe-agent__test-repo__default_from_url__t-0.00__p-0.95__c-3.00__install-1/args.yaml
@@ -444,21 +444,7 @@ agent:
parse_command: {}
parse_function: {}
put_demos_in_history: false
- state_command:
- arguments: null
- code: |
- state() {
- local working_dir="$PWD";
- if [ -z $CURRENT_FILE ]; then
- echo '{"open_file": "n/a", "working_dir": "'$working_dir'"}';
- else
- echo '{"open_file": "'$(realpath $CURRENT_FILE)'", "working_dir": "'$working_dir'"}';
- fi
- };
- docstring: null
- end_name: null
- name: state
- signature: null
+ state_command: _state
strategy_template: null
submit_command: submit
subroutine_types: []
diff --git a/tests/test_data/trajectories/gpt4__swe-bench-dev-easy_first_only__default__t-0.00__p-0.95__c-3.00__install-1/args.yaml b/tests/test_data/trajectories/gpt4__swe-bench-dev-easy_first_only__default__t-0.00__p-0.95__c-3.00__install-1/args.yaml
index 6362216f..a0369b0f 100644
--- a/tests/test_data/trajectories/gpt4__swe-bench-dev-easy_first_only__default__t-0.00__p-0.95__c-3.00__install-1/args.yaml
+++ b/tests/test_data/trajectories/gpt4__swe-bench-dev-easy_first_only__default__t-0.00__p-0.95__c-3.00__install-1/args.yaml
@@ -443,21 +443,7 @@ agent:
parse_command: {}
parse_function: {}
put_demos_in_history: false
- state_command:
- arguments: null
- code: |
- state() {
- local working_dir="$PWD";
- if [ -z $CURRENT_FILE ]; then
- echo '{"open_file": "n/a", "working_dir": "'$working_dir'"}';
- else
- echo '{"open_file": "'$(realpath $CURRENT_FILE)'", "working_dir": "'$working_dir'"}';
- fi
- };
- docstring: null
- end_name: null
- name: state
- signature: null
+ state_command: _state
strategy_template: null
submit_command: submit
subroutine_types: []
diff --git a/tests/utils.py b/tests/utils.py
new file mode 100644
index 00000000..bd9c7f9a
--- /dev/null
+++ b/tests/utils.py
@@ -0,0 +1,29 @@
+import importlib.machinery
+import importlib.util
+import sys
+from pathlib import Path
+
+
+def make_python_tool_importable(file_path: str | Path, module_name: str | None = None) -> None:
+ """Import a Python executable script and add it to sys.modules.
+
+ Args:
+ file_path: Path to the executable script (relative or absolute)
+ module_name: Name of the module to import the script as. If None, the filename stem is used.
+ """
+ # Convert to absolute path if needed
+ abs_path = Path(file_path).resolve()
+ module_name = Path(file_path).stem if module_name is None else module_name
+
+ if module_name in sys.modules:
+ return # Module already imported
+
+ loader = importlib.machinery.SourceFileLoader(module_name, abs_path)
+ spec = importlib.util.spec_from_file_location(module_name, abs_path, loader=loader)
+ if spec is None or spec.loader is None:
+ msg = f"Could not load module spec for {file_path}"
+ raise ImportError(msg)
+
+ module = importlib.util.module_from_spec(spec)
+ sys.modules[module_name] = module
+ spec.loader.exec_module(module)