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 defaults tools

carlosejimenez54a50eb1c115
diff --git a/tools/cursors_defaults/lib/cursors_defaults_utils.sh b/tools/cursors_defaults/lib/cursors_defaults_utils.sh
new file mode 100644
index 00000000..5b2e83e3
--- /dev/null
+++ b/tools/cursors_defaults/lib/cursors_defaults_utils.sh
@@ -0,0 +1,119 @@
+#!/usr/bin/env bash
+
+_reset_cursors() {
+ _write_env START_CURSOR 1
+ _write_env END_CURSOR 1
+}
+
+_constrain_cursors() {
+ # constrain the cursors to be within the bounds of the file [0, total_lines+1]
+ local CURRENT_FILE=$(_read_env CURRENT_FILE)
+ local CURRENT_LINE=$(_read_env CURRENT_LINE)
+ local WINDOW=$(_read_env WINDOW)
+ local OVERLAP=$(_read_env OVERLAP)
+ local START_CURSOR=$(_read_env START_CURSOR)
+ local END_CURSOR=$(_read_env END_CURSOR)
+
+ local total_lines=$(awk 'END {print NR}' "$CURRENT_FILE")
+ total_lines=$((total_lines < 1 ? 1 : total_lines)) # if the file is empty, set total_lines to 1
+ local start_line=$((CURRENT_LINE - WINDOW / 2))
+ local end_line=$((CURRENT_LINE + WINDOW / 2))
+ start_line=$((start_line < 1 ? 1 : start_line))
+ end_line=$((end_line > total_lines ? total_lines : end_line))
+ local warning_string=""
+ if [ "$START_CURSOR" -lt "$start_line" ]; then
+ warning_string+="START_CURSOR moved to $start_line\n"
+ START_CURSOR=$start_line
+ elif [ "$START_CURSOR" -gt "$end_line" ]; then
+ START_CURSOR=$end_line
+ warning_string+="START_CURSOR moved to $end_line\n"
+ fi
+ if [ "$END_CURSOR" -lt "$start_line" ]; then
+ warning_string+="END_CURSOR moved to $start_line\n"
+ END_CURSOR=$start_line
+ elif [ "$END_CURSOR" -gt "$end_line" ]; then
+ warning_string+="END_CURSOR moved to $end_line\n"
+ END_CURSOR=$end_line
+ fi
+ echo "$warning_string"
+ echo $START_CURSOR $END_CURSOR
+ _write_env START_CURSOR $START_CURSOR
+ _write_env END_CURSOR $END_CURSOR
+}
+
+_print() {
+ local CURRENT_FILE=$(_read_env CURRENT_FILE)
+ local CURRENT_LINE=$(_read_env CURRENT_LINE)
+ local WINDOW=$(_read_env WINDOW)
+ local OVERLAP=$(_read_env OVERLAP)
+ local START_CURSOR=$(_read_env START_CURSOR)
+ local END_CURSOR=$(_read_env END_CURSOR)
+
+ local cursor_warning=$(_constrain_cursors)
+ local cursor_values=$(echo "$cursor_warning" | tail -n 1)
+ cursor_warning=$(echo "$cursor_warning" | head -n -1)
+ _write_env START_CURSOR $(echo "$cursor_values" | awk '{print $1}')
+ _write_env END_CURSOR $(echo "$cursor_values" | awk '{print $2}')
+ local total_lines=$(awk 'END {print NR}' $CURRENT_FILE)
+ echo "[File: $(realpath "$CURRENT_FILE") ($total_lines lines total)]"
+ local start_line=$((CURRENT_LINE - WINDOW / 2))
+ local end_line=$((CURRENT_LINE + WINDOW / 2))
+ start_line=$((start_line < 1 ? 1 : start_line))
+ end_line=$((end_line > total_lines ? total_lines : end_line))
+ local lines=()
+ local i=0
+ while IFS= read -r line; do
+ lines[i++]="$line"
+ done < <(awk -v start="$start_line" -v end="$end_line" 'NR>=start && NR<=end {print}' "$CURRENT_FILE")
+ local num_lines=${#lines[@]}
+ if [ $start_line -gt 1 ]; then
+ echo "($((start_line - 1)) more lines above)"
+ fi
+ for ((i=0; i<num_lines+1; i++)) do
+ local line_number=$((start_line + i))
+ if [ $line_number -eq $START_CURSOR ]
+ then
+ echo $START_CURSOR_MARK
+ fi
+ # if i in [0, num_lines-1] then print the line number and the line
+ if [ $i -ge 0 ] && [ $i -lt $num_lines ]
+ then
+ echo "$line_number:${lines[i]}"
+ fi
+ if [ $line_number -eq $END_CURSOR ]
+ then
+ echo $END_CURSOR_MARK
+ fi
+ done
+ lines_below=$((total_lines - start_line - num_lines))
+ lines_below=$((lines_below > 0 ? lines_below : 0))
+ if [ $lines_below -gt 0 ]; then
+ echo "($lines_below more lines below)"
+ fi
+ if [ -n "$cursor_warning" ]; then
+ echo -e "$cursor_warning"
+ fi
+}
+
+_constrain_line() {
+ local CURRENT_FILE=$(_read_env CURRENT_FILE)
+ local CURRENT_LINE=$(_read_env CURRENT_LINE)
+ local WINDOW=$(_read_env WINDOW)
+
+ if [ -z "$CURRENT_FILE" ]
+ then
+ echo "No file open. Use the open command first."
+ return
+ fi
+ local max_line=$(awk 'END {print NR}' "$CURRENT_FILE")
+ local half_window=$((WINDOW / 2))
+ # First ensure line number isn't too large (min of current_line and max_line - half_window)
+ if ((CURRENT_LINE > max_line - half_window)); then
+ CURRENT_LINE=$((max_line - half_window))
+ fi
+ # Then ensure line number isn't too small (max of current_line and half_window)
+ if ((CURRENT_LINE < half_window)); then
+ CURRENT_LINE=$((half_window))
+ fi
+ _write_env CURRENT_LINE $CURRENT_LINE
+}
\ No newline at end of file
diff --git a/tools/debug/lib/utils.sh b/tools/debug/lib/utils.sh
new file mode 100644
index 00000000..60a6dabb
--- /dev/null
+++ b/tools/debug/lib/utils.sh
@@ -0,0 +1,3 @@
+_debug_command() {
+ echo "<<INTERACTIVE||$@||INTERACTIVE>>"
+}
\ No newline at end of file
diff --git a/tools/defaults/bin/goto b/tools/defaults/bin/goto
index 8a9329ce..6d84a929 100644
--- a/tools/defaults/bin/goto
+++ b/tools/defaults/bin/goto
@@ -32,8 +32,10 @@ main() {
echo "Error: <line> must be less than or equal to $max_line"
exit 1
fi
- local OFFSET=$(jq -n "$WINDOW/6" | jq 'floor')
- _write_env CURRENT_LINE $(jq -n "[$1 + $WINDOW/2 - $OFFSET, 1] | max | floor") || exit 1
+ local OFFSET=$((WINDOW / 6))
+ local line_number=$(( $1 + WINDOW / 2 - OFFSET ))
+ line_number=$(( line_number > 1 ? line_number : 1 )) # Ensure line_number is at least 1
+ _write_env CURRENT_LINE $line_number || exit 1
_constrain_line || exit 1
_print || exit 1
}
diff --git a/tools/defaults/bin/open b/tools/defaults/bin/open
index f8c4dbcb..500b4b32 100644
--- a/tools/defaults/bin/open
+++ b/tools/defaults/bin/open
@@ -29,17 +29,18 @@ main() {
if [ $2 -gt $max_line ]; then
echo "Warning: <line_number> ($2) is greater than the number of lines in the file ($max_line)"
echo "Warning: Setting <line_number> to $max_line"
- local line_number=$(jq -n "$max_line") # Set line number to max if greater than max
+ local line_number=$max_line # Set line number to max if greater than max
elif [ $2 -lt 1 ]; then
echo "Warning: <line_number> ($2) is less than 1"
echo "Warning: Setting <line_number> to 1"
- local line_number=$(jq -n "1") # Set line number to 1 if less than 1
+ local line_number=1 # Set line number to 1 if less than 1
else
- local OFFSET=$(jq -n "$WINDOW/6" | jq 'floor')
- local line_number=$(jq -n "[$2 + $WINDOW/2 - $OFFSET, 1] | max | floor")
+ local OFFSET=$((WINDOW / 6))
+ local line_number=$(( $2 + WINDOW / 2 - OFFSET ))
+ line_number=$(( line_number > 1 ? line_number : 1 )) # Ensure line_number is at least 1
fi
else
- local line_number=$(jq -n "$WINDOW/2") # Set default line number if not provided
+ local line_number=$((WINDOW / 2)) # Set default line number if not provided
fi
_write_env CURRENT_FILE $(realpath "$1") || exit 1
diff --git a/tools/defaults/bin/scroll_down b/tools/defaults/bin/scroll_down
index 934a7980..52be8bec 100644
--- a/tools/defaults/bin/scroll_down
+++ b/tools/defaults/bin/scroll_down
@@ -13,7 +13,7 @@ main() {
echo "No file open. Use the open command first."
exit 1
fi
- _write_env CURRENT_LINE $(jq -n "$CURRENT_LINE + $WINDOW - $OVERLAP") || exit 1
+ _write_env CURRENT_LINE $((CURRENT_LINE + WINDOW - OVERLAP)) || exit 1
_constrain_line || exit 1
_print || exit 1
_scroll_warning_message || exit 1
diff --git a/tools/defaults/bin/scroll_up b/tools/defaults/bin/scroll_up
index 779d4c35..5c6efafb 100644
--- a/tools/defaults/bin/scroll_up
+++ b/tools/defaults/bin/scroll_up
@@ -13,7 +13,7 @@ main() {
echo "No file open. Use the open command first."
exit 1
fi
- _write_env CURRENT_LINE $(jq -n "$CURRENT_LINE - $WINDOW + $OVERLAP") || exit 1
+ _write_env CURRENT_LINE $((CURRENT_LINE - WINDOW + OVERLAP)) || exit 1
_constrain_line || exit 1
_print || exit 1
_scroll_warning_message || exit 1
diff --git a/tools/defaults/install.sh b/tools/defaults/install.sh
index e6387a97..58044980 100644
--- a/tools/defaults/install.sh
+++ b/tools/defaults/install.sh
@@ -13,4 +13,4 @@ _write_env "CURRENT_LINE" "${CURRENT_LINE:-0}"
_write_env "CURRENT_FILE" "${CURRENT_FILE:-}"
# install jq
-apt-get update && apt-get install -y jq
+# apt-get update && apt-get install -y jq
diff --git a/tools/defaults/lib/utils.sh b/tools/defaults/lib/utils.sh
index e664c68c..a5029de2 100644
--- a/tools/defaults/lib/utils.sh
+++ b/tools/defaults/lib/utils.sh
@@ -5,21 +5,27 @@ _print() {
local total_lines=$(awk 'END {print NR}' "$CURRENT_FILE")
echo "[File: $(realpath "$CURRENT_FILE") ($total_lines lines total)]"
- lines_above=$(jq -n "$CURRENT_LINE - $WINDOW/2" | jq '[0, .] | max | floor')
- lines_below=$(jq -n "$total_lines - $CURRENT_LINE - $WINDOW/2" | jq '[0, .] | max | round')
+ lines_above=$((CURRENT_LINE - WINDOW / 2 - 1))
+ lines_above=$((lines_above < 0 ? 0 : lines_above))
if [ $lines_above -gt 0 ]; then
echo "($lines_above more lines above)"
fi
- cat "$CURRENT_FILE" | grep -n $ | head -n $(jq -n "[$CURRENT_LINE + $WINDOW/2, $WINDOW/2] | max | floor") | tail -n $(jq -n "$WINDOW")
+ start_line=$((CURRENT_LINE - WINDOW / 2))
+ start_line=$((start_line < 1 ? 1 : start_line))
+ end_line=$((CURRENT_LINE + WINDOW / 2))
+ end_line=$((end_line > total_lines ? total_lines : end_line))
+ sed -n "$start_line,$end_line p" "$CURRENT_FILE" | nl -v $start_line -s':' -b a -n ln -w1
+ lines_below=$((total_lines - CURRENT_LINE - WINDOW / 2))
+ lines_below=$((lines_below < 0 ? 0 : lines_below))
if [ $lines_below -gt 0 ]; then
echo "($lines_below more lines below)"
fi
}
_constrain_line() {
- local CURRENT_FILE=$(_read_env "CURRENT_FILE")
- local CURRENT_LINE=$(_read_env "CURRENT_LINE")
- local WINDOW=$(_read_env "WINDOW")
+ local CURRENT_FILE=$(_read_env CURRENT_FILE)
+ local CURRENT_LINE=$(_read_env CURRENT_LINE)
+ local WINDOW=$(_read_env WINDOW)
if [ -z "$CURRENT_FILE" ]
then
@@ -27,8 +33,33 @@ _constrain_line() {
return
fi
local max_line=$(awk 'END {print NR}' "$CURRENT_FILE")
- local half_window=$(jq -n "$WINDOW/2" | jq 'floor')
- export CURRENT_LINE=$(jq -n "[$CURRENT_LINE, $max_line - $half_window] | min")
- export CURRENT_LINE=$(jq -n "[$CURRENT_LINE, $half_window] | max")
- _write_env "CURRENT_LINE" "$CURRENT_LINE"
+ local half_window=$((WINDOW / 2))
+ # First ensure line number isn't too large (min of current_line and max_line - half_window)
+ if ((CURRENT_LINE > max_line - half_window)); then
+ CURRENT_LINE=$((max_line - half_window))
+ fi
+ # Then ensure line number isn't too small (max of current_line and half_window)
+ if ((CURRENT_LINE < half_window)); then
+ CURRENT_LINE=$((half_window))
+ fi
+ _write_env CURRENT_LINE $CURRENT_LINE
+}
+
+_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
+ local SCROLL_COUNT=$(_read_env SCROLL_COUNT 0)
+ # Reset if the last command wasn't about scrolling
+ if [ "$LAST_ACTION" != "scroll_up" ] && [ "$LAST_ACTION" != "scroll_down" ]; then
+ _write_env SCROLL_COUNT 0
+ fi
+ # Increment because we're definitely scrolling now
+ _write_env 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
}
\ No newline at end of file
diff --git a/tools/server_connection/lib/utils.sh b/tools/server_connection/lib/utils.sh
new file mode 100644
index 00000000..c91db336
--- /dev/null
+++ b/tools/server_connection/lib/utils.sh
@@ -0,0 +1,3 @@
+_connect_command() {
+ echo "<<INTERACTIVE||$@||INTERACTIVE>>"
+}
\ No newline at end of file