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]

Config with binary reviewer and at least 2 tries

Kilian Lieret6200d57b41e7
diff --git a/config/default_retry_10_0_0.yaml b/config/default_retry_10_0_0.yaml
deleted file mode 100644
index 5fd08894..00000000
--- a/config/default_retry_10_0_0.yaml
+++ /dev/null
@@ -1,142 +0,0 @@
-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:
- {issue}
-
- 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 ---
-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: 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
-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
-review_loop_config:
- review_loop_classname: ReviewLoop
- reviewer_classname: Reviewer
- reviewer_config:
- system_template: |
- You are checking whether a problem in a code base has been resolved.
-
- You will be presented with
-
- INPUTS
-
- **INPUT 1**: Problem statement: A GitHub issue that describes an issue
- **INPUT 2**: Code: Relevant lines from a file
-
- TASK
-
- Determine if the problem persists.
-
- If you believe the problem has been resolved, answer "success". If you believe the problem persists, answer "fail".
- instance_template: |
- INPUT 1: Problem statement
-
- {issue}
-
-
- INPUT 2: Code
-
- ```
- {edited_files70}
- ```
- max_samples: 10
- min_draws: 0
- max_accepted_draws: 0
diff --git a/config/default_retry_5_0_0.yaml b/config/default_retry_5_0_0.yaml
index 9a6ad9ce..8c56d828 100644
--- a/config/default_retry_5_0_0.yaml
+++ b/config/default_retry_5_0_0.yaml
@@ -126,6 +126,8 @@ review_loop_config:
Determine if the problem persists.
If you believe the problem has been resolved, answer "success". If you believe the problem persists, answer "fail".
+
+ THE LAST LINE OF YOUR RESPONSE MUST BE EITHER "SUCCESS" OR "FAIL".
instance_template: |
INPUT 1: Problem statement
diff --git a/config/default_retry_5_0_0_no_exit_cost.yaml b/config/default_retry_5_2_0_choo.yaml
similarity index 56%
rename from config/default_retry_5_0_0_no_exit_cost.yaml
rename to config/default_retry_5_2_0_choo.yaml
index 055a10e2..0381c217 100644
--- a/config/default_retry_5_0_0_no_exit_cost.yaml
+++ b/config/default_retry_5_2_0_choo.yaml
@@ -126,6 +126,8 @@ review_loop_config:
Determine if the problem persists.
If you believe the problem has been resolved, answer "success". If you believe the problem persists, answer "fail".
+
+ THE LAST LINE OF YOUR RESPONSE MUST BE EITHER "SUCCESS" OR "FAIL".
instance_template: |
INPUT 1: Problem statement
@@ -137,7 +139,129 @@ review_loop_config:
```
{edited_files70}
```
- reject_exit_cost: False
+ binary_reviewer_classname: BinaryReviewer
+ binary_reviewer_config:
+ system_template: |
+ You are comparing two attempts to fix a problem in a code base to determine which one resolves the problem better.
+
+ You will be presented with
+
+ 1. INPUTS
+
+ **INPUT 1: Problem statement**: A GitHub issue that describes an issue
+ **INPUT 2: Investigation 1**: History of how the first developer tried to fix the problem
+ **INPUT 3: Investigation 2**: History of how the second developer tried to fix the problem
+
+ 2. INPUT FORMATS
+
+ 2.1 The investigation format
+
+ The investigation is structured as a series of actions ("Step {i} action: "), where each action is a thought followed by a command in triple-backticks.
+ For example:
+
+ Investigation 1 Step 1 action:
+ We have found a bug in the code. Let's try to reproduce it.
+ ```
+ create reproduce.py
+ ```
+
+ Investigation 1 Step 1 output:
+ reproduce.py created.
+
+ After the command, there is a description of the output of the command ("Step {i} output: ").
+ The most important actions are
+
+ - `open {file}`: This action opens the file {file}. The output of the command shows part of the content of the file.
+ - `edit {from}:{to}`, followed by the content to replace lines {from} to {to}. This action always affects the currently
+ opened file. The output of the command is the content of the file after the edit.
+ - `python {file}`: This action executes the Python script {file}. The output of the command is the output of the script.
+ - `submit`: This action submits the code for review. The output of the command is the result of the submission.
+ The format of the output is a patch file/git diff (described in the next section)
+
+ 2.2 The patch/git diff format
+
+ The patch/git diff format is formatted as follows:
+
+ 2.1.1 File Paths: Each diff begins with two lines indicating the file paths of the old and new files.
+
+ ```
+ --- a/path/to/file
+ +++ b/path/to/file
+ ```
+
+ 2.1.2 Hunks: The changes within the files are divided into “hunks.” Each hunk shows the line numbers affected and the actual changes. The hunk header looks like this:
+
+ ```
+ @@ -start,line_count +start,line_count @@
+ ```
+ The -start,line_count refers to the starting line number and the number of lines in the old file.
+ The +start,line_count refers to the starting line number and the number of lines in the new file.
+
+ 2.1.3 Changes:
+
+ Removed Lines: Lines removed from the file are prefixed with a -.
+ Added Lines: Lines added to the file are prefixed with a +.
+ Unchanged Lines: Lines that remain unchanged around the changes are shown for context and are prefixed with a space.
+
+ 3. TASK
+
+ The last line of your reply MUST be "first" or "second", depending on which patch resolves the problem.
+
+ Compare the two investigations (INPUT 2 and INPUT 3) and determine which one is better based on how they address
+ the following points:
+
+ - The history should attempt to reproduce the problem if it was a bug, for example by creating a `reproduce.py` file.
+ Make sure that the problem reproduced is the same as the problem described in INPUT 1.
+ - Fixing the isuee: If the problem was reproduced, the edits should fix the problem without modifying
+ the reproduction script.
+ - Any attempt to fix the issue should not introduce new issues or remove important checks that might serve other purposes.
+ For example, when trying to fix a case where an exception is raised, the fix should not remove all of the exception handling,
+ but understand why it was raised in this specific scenario and fix the root cause. If the logic is significantly simplified
+ or shortened, you should be extra suspicious.
+ - Generally, the less steps and the fewer API calls are needed to fix the problem, the better the solution is.
+ Investigation 1 used {model_stats_api_calls1} API calls to fix the problem. Investigation 2 used {model_stats_api_calls2} API calls to fix the problem.
+ - Make sure that the explanation of the actions is clear and concise and that they make sense in the context of the previous
+ steps, actions, and outputs. You are actively looking for inconsistencies.
+ - The explanation of the actions must match the actual code changes.
+
+ Based on these questions, determine which patch is the better solution.
+
+ If INPUT 2 is the better solution, answer "first"
+ If INPUT 3 is the better solution, answer "second"
+
+ If you cannot determine which solution is better, answer "first".
+
+ YOU MUST DECIDE. THE LAST LINE OF YOUR REPLY MUST BE "first" OR "second".
+
+ instance_template: |
+ ==========================
+ INPUT 1: Problem statement
+ ==========================
+
+ {issue}
+
+ ========================
+ INPUT 2: Investigation 1
+ ========================
+
+ Investigation 1 used {model_stats_api_calls1} API calls to fix the problem. The investigation is as follows:
+
+ {traj1}
+
+ ========================
+ INPUT 3: Investigation 2
+ ========================
+
+ Investigation 2 used {model_stats_api_calls2} API calls to fix the problem. The investigation is as follows:
+
+ {traj2}
+ traj_only_show_last_n_output: 3
+ traj_item_template: |
+ Investigation {i_traj} Step {i_step}:
+ {response}
+
+ Investigation {i_traj} Step {i_step} output:
+ {observation}
max_samples: 5
- min_draws: 0
+ min_draws: 2
max_accepted_draws: 0
diff --git a/sweagent/agent/reviewer.py b/sweagent/agent/reviewer.py
index 4ccd20fa..4c16a69d 100644
--- a/sweagent/agent/reviewer.py
+++ b/sweagent/agent/reviewer.py
@@ -146,6 +146,9 @@ class BinaryReviewerConfig(FrozenSerializable):
traj_filter: list[str] = field(default_factory=list)
#: Format of the trajectory item
traj_item_template: str = "Model: {response}\n\nObservation: {observation}"
+ traj_output_filter: list[str] = field(default_factory=list)
+ traj_filter_failed_edits: bool = False
+ traj_only_show_last_n_output: int = 0
@dataclass(frozen=True)
@@ -177,7 +180,6 @@ class ReviewLoopConfig(FrozenSerializable):
# --- IMPLEMENTATIONS ---
-# fixme: Reviewer should insta-reject on exit_cost
class Reviewer(AbstractReviewer):
LOG_PREFIX = "🧑‍⚖️ Reviewer: "
@@ -241,12 +243,14 @@ class TrajectoryFormatter:
traj_output_filter: list[str] | None = None,
traj_item_template: str = "Model: {response}\n\nObservation: {observation}",
filter_failed_edits: bool = False,
+ only_show_last_n_output: int = 0,
):
"""Formats trajectories for the use in prompts"""
self._traj_filter = traj_filter or []
self._traj_output_filter = traj_output_filter or []
self._traj_item_template = traj_item_template
self._filter_failed_edits = filter_failed_edits
+ self._only_show_last_n_output = only_show_last_n_output
def _include_step(self, item: TrajectoryStep) -> bool:
action = item["action"].strip()
@@ -257,16 +261,18 @@ class TrajectoryFormatter:
return False
return True
- def _include_step_output(self, item: TrajectoryStep) -> bool:
+ def _include_step_output(self, item: TrajectoryStep, i_step: int, n_steps: int) -> bool:
+ if self._only_show_last_n_output > 0 and i_step < n_steps - self._only_show_last_n_output:
+ return False
action = item["action"].strip()
for f in self._traj_output_filter:
if action.startswith(f):
return False
return True
- def _format_trajectory_step(self, step: TrajectoryStep, i_step: int, i_traj: int = 1) -> str:
+ def _format_trajectory_step(self, step: TrajectoryStep, i_step: int, *, n_steps: int, i_traj: int = 1) -> str:
step = copy.deepcopy(step)
- if not self._include_step_output(step):
+ if not self._include_step_output(step, i_step, n_steps=n_steps):
step["observation"] = "[Output omitted]"
return self._traj_item_template.format(
**step,
@@ -277,7 +283,10 @@ class TrajectoryFormatter:
def format_trajectory(self, trajectory: Trajectory, i_traj: int = 1) -> str:
traj_messages = [step for step in trajectory if self._include_step(step)]
return "\n\n".join(
- [self._format_trajectory_step(step, i_step, i_traj=i_traj) for i_step, step in enumerate(traj_messages)]
+ [
+ self._format_trajectory_step(step, i_step, i_traj=i_traj, n_steps=len(traj_messages))
+ for i_step, step in enumerate(traj_messages)
+ ]
)
@@ -290,6 +299,9 @@ class BinaryReviewer(AbstractBinaryReviewer):
self._traj_formatter = TrajectoryFormatter(
traj_filter=config.traj_filter,
traj_item_template=config.traj_item_template,
+ traj_output_filter=config.traj_output_filter,
+ filter_failed_edits=config.traj_filter_failed_edits,
+ only_show_last_n_output=config.traj_only_show_last_n_output,
)
def format_messages(self, instance: INSTANCE_TYPE, sub1: ReviewSubmission, sub2: ReviewSubmission):
diff --git a/sweagent/types.py b/sweagent/types.py
index 562bafaa..6d0a0127 100644
--- a/sweagent/types.py
+++ b/sweagent/types.py
@@ -66,6 +66,9 @@ class ReviewSubmission:
for k, v in self.info.items():
if isinstance(v, str):
out[f"{k}{suffix}"] = v
+ elif isinstance(v, dict):
+ for k2, v2 in v.items():
+ out[f"{k}_{k2}{suffix}"] = v2
return out