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]

Allow tools to request requery of LM

Kilian Lieretbd3fc1aa4ee5
diff --git a/sweagent/agent/agents.py b/sweagent/agent/agents.py
index d254b2ad..a1924c24 100644
--- a/sweagent/agent/agents.py
+++ b/sweagent/agent/agents.py
@@ -142,6 +142,16 @@ class Skipped(Exception): ...
class Submitted(Exception): ...
+class RetryWithOutput(Exception): ...
+
+
+class RetryWithoutOutput(Exception): ...
+
+
+RETRY_WITH_OUTPUT_TOKEN = "###SWE-AGENT-RETRY-WITH-OUTPUT###"
+RETRY_WITHOUT_OUTPUT_TOKEN = "###SWE-AGENT-RETRY-WITHOUT-OUTPUT###"
+
+
class Agent:
def __init__(
self,
@@ -628,6 +638,14 @@ class Agent:
step.execution_time = time.perf_counter() - execution_t0
self._chook.on_action_executed(step=step)
step.state = self.tools.get_state(env=self._env)
+
+ if RETRY_WITH_OUTPUT_TOKEN in step.observation:
+ step.observation = step.observation.replace(RETRY_WITH_OUTPUT_TOKEN, "")
+ raise RetryWithOutput()
+ elif RETRY_WITHOUT_OUTPUT_TOKEN in step.observation:
+ step.observation = step.observation.replace(RETRY_WITHOUT_OUTPUT_TOKEN, "")
+ raise RetryWithoutOutput()
+
return self.handle_submission(step)
def forward(self, history: list[dict[str, str]]) -> StepOutput:
@@ -715,8 +733,8 @@ class Agent:
exception_message=exception_message,
)
- n_fails = 0
- while n_fails < self.max_requeries:
+ n_format_fails = 0
+ while n_format_fails < self.max_requeries:
try:
return self.forward(history)
@@ -728,19 +746,30 @@ class Agent:
# Errors that cause requery
except FormatError as e:
- n_fails += 1
+ n_format_fails += 1
history = handle_error_with_retry(exception=e, template=self.tools.config.format_error_template)
except BlockedActionError as e:
- n_fails += 1
+ n_format_fails += 1
history = handle_error_with_retry(
exception=e, template=self.tools.config.filter.blocklist_error_template
)
except BashIncorrectSyntaxError as e:
- n_fails += 1
+ n_format_fails += 1
history = handle_error_with_retry(
exception=e,
template=self.templates.shell_check_error_template,
)
+ except RetryWithOutput as e:
+ self.logger.warning("Requerying model with output from last step (RetryWithOutput)")
+ n_format_fails = 0
+ history = handle_error_with_retry(
+ exception=e,
+ template=self.templates.next_step_template,
+ )
+ except RetryWithoutOutput:
+ self.logger.warning("Requerying model (RetryWithoutOutput)")
+ n_format_fails = 0
+ # Requery with the same template as the last step
# Errors that cause exit
diff --git a/tools/edit_replace/bin/edit b/tools/edit_replace/bin/edit
index 294d4ab7..3eb2b239 100644
--- a/tools/edit_replace/bin/edit
+++ b/tools/edit_replace/bin/edit
@@ -16,6 +16,8 @@ else:
from default_utils import FileNotOpened, TextNotFound, WindowedFile # type: ignore
from flake8_utils import flake8, format_flake8_output # type: ignore
+RETRY_WITH_OUTPUT_TOKEN = "###SWE-AGENT-RETRY-WITH-OUTPUT###"
+
_NOT_FOUND_IN_WINDOW_MSG = """Text {search!r} not found in displayed lines.
Note: By default, we only replace the first occurrence in the lines that are currently visible in the editor.
If you want to replace all occurrences in and outside of the visible lines, use the `--replace-all` flag.
@@ -35,6 +37,7 @@ Your proposed edit has introduced new syntax error(s). Please read this error me
ERRORS:
{errors}
+
This is how your edit would have looked if applied
------------------------------------------------
{window_applied}
@@ -70,6 +73,7 @@ def main(search: str, replace: str, replace_all: bool):
if search == replace:
print(_NO_CHANGES_MADE_MSG)
+ print(RETRY_WITH_OUTPUT_TOKEN)
exit(2)
pre_edit_lint = flake8(wf.path)
@@ -82,6 +86,7 @@ def main(search: str, replace: str, replace_all: bool):
f"Error: Found more than one occurrence of {search} in the window while replace_all=False. "
"Aborting edit."
)
+ print(RETRY_WITH_OUTPUT_TOKEN)
exit(4)
replacement_info = wf.replace_in_window(search, replace)
# todo: Should warn if more than one occurrence was found?
@@ -90,6 +95,7 @@ def main(search: str, replace: str, replace_all: bool):
replacement_info = wf.replace(search, replace)
except TextNotFound:
print(_NOT_FOUND_IN_WINDOW_MSG.format(search=search))
+ print(RETRY_WITH_OUTPUT_TOKEN)
exit(3)
post_edit_lint = flake8(wf.path)
@@ -117,9 +123,10 @@ def main(search: str, replace: str, replace_all: bool):
without_edits = wf.get_window_text(line_numbers=True, status_line=True, pre_post_line=True)
print(
_LINT_ERROR_TEMPLATE.format(
- errors=new_flake8_output, window_applied=with_edits, window_original=without_edits
+ errors=new_flake8_output, window_applied=with_edits, window_original=without_edits,
)
)
+ print(RETRY_WITH_OUTPUT_TOKEN)
exit(4)
if not replace_all:
print(_SINGLE_EDIT_SUCCESS_MSG)