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: Warn if agents scrolls multiple times in a row (#631)
Kilian Lieretca0ae89c13c6
diff --git a/config/commands/defaults.sh b/config/commands/defaults.shindex 57f005b0..835ab32c 100644--- a/config/commands/defaults.sh+++ b/config/commands/defaults.sh@@ -120,6 +120,25 @@ goto() {_print}+_scroll_warning_message() {+ # Warn the agent if we scroll too many times+ # Message will be shown if scroll is called more than WARN_AFTER_SCROLLING_TIMES (default 3) times+ # Initialize variable if it's not set+ export SCROLL_COUNT=${SCROLL_COUNT:-0}+ # Reset if the last command wasn't about scrolling+ if [ "$LAST_ACTION" != "scroll_up" ] && [ "$LAST_ACTION" != "scroll_down" ]; then+ export SCROLL_COUNT=0+ fi+ # Increment because we're definitely scrolling now+ export SCROLL_COUNT=$((SCROLL_COUNT + 1))+ if [ $SCROLL_COUNT -ge ${WARN_AFTER_SCROLLING_TIMES:-3} ]; then+ echo ""+ echo "WARNING: Scrolling many times in a row is very inefficient."+ echo "If you know what you are looking for, use \`search_file <pattern>\` instead."+ echo ""+ fi+}+# @yaml# signature: scroll_down# docstring: moves the window down {WINDOW} lines@@ -132,6 +151,7 @@ scroll_down() {export CURRENT_LINE=$(jq -n "$CURRENT_LINE + $WINDOW - $OVERLAP")_constrain_line_print+ _scroll_warning_message}# @yaml@@ -146,6 +166,7 @@ scroll_up() {export CURRENT_LINE=$(jq -n "$CURRENT_LINE - $WINDOW + $OVERLAP")_constrain_line_print+ _scroll_warning_message}# @yamldiff --git a/docs/config/commands.md b/docs/config/commands.mdindex ca3855c3..a94be044 100644--- a/docs/config/commands.md+++ b/docs/config/commands.md@@ -36,6 +36,8 @@ Every command subscribes to the following skeleton code.* `CURRENT_LINE`: First line of the window that is currently being shown/edited* `WINDOW` (start line to end line): Part of the file that is currently shown/edited* `START_CURSOR`, `END_CURSOR`: Only used for the `cursors_*` commands.+* You can also use environment variables to set parameters for your commands. For this, edit the `env_vars` section of the [config](config.md).+ For example, the `WINDOW` setting controls the number of context lines shown when editing a file.## Displaying the Command to SWE-agentAfter you define a command, there are a small set of additional steps to making it available for the agent to use.diff --git a/sweagent/agent/agents.py b/sweagent/agent/agents.pyindex b2cdb378..2b70e027 100644--- a/sweagent/agent/agents.py+++ b/sweagent/agent/agents.py@@ -482,7 +482,7 @@ class Agent:Returns:thought: model reasoningaction: action that the model proposes- output: raw model output+ output: raw model output (not output of the action)"""thought, action, output = self.forward_with_error_check(observation, state)@@ -505,7 +505,7 @@ class Agent:"""Query the model with the current state and observation with the appropriate template.Returns:- output: raw model output+ output: raw model output (not output of the command)"""assert self.config is not None # mypydiff --git a/sweagent/agent/commands.py b/sweagent/agent/commands.pyindex e16f79d7..ac9789f4 100644--- a/sweagent/agent/commands.py+++ b/sweagent/agent/commands.py@@ -103,7 +103,7 @@ class ParseCommandBash(ParseCommand):else:return commands- def parse_bash_functions(self, path, contents) -> list[Command]:+ def parse_bash_functions(self, path, contents: str) -> list[Command]:"""Simple logic for parsing a bash file and segmenting it into functions.diff --git a/sweagent/environment/swe_env.py b/sweagent/environment/swe_env.pyindex b1fa78d8..bef0836d 100644--- a/sweagent/environment/swe_env.py+++ b/sweagent/environment/swe_env.py@@ -431,7 +431,7 @@ class SWEEnv(gym.Env):def step(self, action: str) -> tuple[str | None, int, bool, dict]:"""- Runs given action in environment and returns corresponding output+ Runs an action proposed by the agent in the environment and returns the corresponding output.Args:action: command to run in bash shell@@ -471,7 +471,7 @@ class SWEEnv(gym.Env):# Attempt to run action in containerobservation = ""try:- observation = self.communicate(input=action, timeout_duration=25)+ observation = self.communicate(input=action, timeout_duration=25, set_last_action=True)except TimeoutError:try:self.interrupt()@@ -738,6 +738,12 @@ class SWEEnv(gym.Env):input: str,timeout_duration=25,) -> str:+ """Runs command in container and returns output++ Args:+ input: command to run in container+ timeout_duration: duration to wait for output+ """assert self.container is not Nonecommunicate_method = keys_config.get("SWE_AGENT_COMMUNICATE_METHOD", default="end-marker", choices=["end-marker", "processes"]@@ -781,16 +787,14 @@ class SWEEnv(gym.Env):output = self._communicate(f"/bin/bash -n <<'EOF'\n{input}\nEOF\n")return output, self.returncode == 0- def communicate(- self,- input: str,- timeout_duration=25,- ) -> str:+ def communicate(self, input: str, timeout_duration: int = 25, *, set_last_action: bool = False) -> str:"""Sends input to container and returns outputArgs:input: input to send to container+ timeout_duration: duration to wait for output+ set_last_action: whether to set the LAST_ACTION environment variableReturns:output: output from container@@ -806,6 +810,12 @@ class SWEEnv(gym.Env):)self.logger.log(logging.TRACE, "Output:\n%s", output) # type: ignoreself.communicate_output = output+ if set_last_action:+ # Cannot merge this with last command, because of multiline command+ # handling.+ last_action_string = shlex.quote(input.strip())+ input = f"export LAST_ACTION={last_action_string}"+ self._communicate(input, timeout_duration=5)return outputelse:self.container.terminate()