【Agent】SWE-Agent调试记录和简单解读

背景:

        SWE-Agent 是一个利用 LLM 与计算机交互以解决软件工程任务的自主系统,其精心定制的 ACI(Agent-Computer Interface) 极大地增强了 Agent 创建和编辑代码文件、浏览完整代码库以及执行程序的能力。SWE-agent 在 SWE-bench 上, 解决了 12.5% 的问题,相比之前通过检索增强生成(RAG)取得的 3.8% 有显著提升。SWE-bench 包含来自 GitHub 的 2,294 个真实问题案例。

       这两天对SWE-Agent进行简单的调试,并将调试过程和简单解读记录在此,供其它小伙伴们学习交流。原创不易,若转载请注明来源,感谢!

一、参考资料:

源码库:https://github.com/princeton-nlp/SWE-agent

官方文档(docker启动):Run with docker - SWE-agent documentation

官方示例Issue:SyntaxError: invalid syntax · Issue #1 · SWE-agent/test-repo · GitHub

SWE-agent 详解:打造大模型与计算机的编程界面:https://zhuanlan.zhihu.com/p/696347338

二、调试记录:

WebServer方式启动:

启动命令:

cd $PWD

docker run -p 5030:3000 -p 5080:8000 -it --name swe-agent-deng -v /var/run/docker.sock:/var/run/docker.sock \
  -v $(pwd)/keys.cfg:/app/keys.cfg \
  sweagent/swe-agent-run:latest /bin/bash

root@10da16dc8a9e:/app# ./start_web_ui.sh

其中挂载的keys.cfg配置文件内容如下:

# All keys are commented out by default. Make sure to remove the leading '#' of the relevant lines
GITHUB_TOKEN: 'XXXXX'
# OPENAI_API_KEY: 'OpenAI API Key Here if using OpenAI Model'
# ANTHROPIC_API_KEY: 'Anthropic API Key Here if using Anthropic Model'
# TOGETHER_API_KEY: 'Together API Key Here if using Together Model'
AZURE_OPENAI_API_KEY: 'XXXXX'
AZURE_OPENAI_ENDPOINT: 'XXXXX'
AZURE_OPENAI_DEPLOYMENT: 'gpt-4o'
AZURE_OPENAI_API_VERSION: '2024-02-15-preview'
# OPENAI_API_BASE_URL: 'LM base URL here if using Local or alternative api Endpoint'
# GROQ_API_KEY: 'Groq Models API Key’

因为我们用的LLM是Azure的OpenAI API,所以需要配置AZURE开头的四个参数,DEPLOYMENT和API_VERSION这两个参数可以在Azure的控制台里找到。OpenAI官方的几个参数就不要配置了。

GITHUB_TOKEN:用于访问repo拉取代码,创建Token方式请参考Managing your personal access tokens - GitHub Docs

输出日志:

root@10da16dc8a9e:/app# ./start_web_ui.sh 
(##################) ⠼ reify:fsevents: sill reify mark deleted [ '/app/sweagent/frontend/node_modules/fsevents' ]
(##################) ⠼ reify:fsevents: sill reify mark deleted [ '/app/sweagent/frontend/node_modules/fsevents' ]
(##################) ⠼ reify:fsevents: sill reify mark deleted [ '/app/sweagent/frontend/node_modules/fsevents' ]
(##################) ⠼ reify:fsevents: sill reify mark deleted [ '/app/sweagent/frontend/node_modules/fsevents' ]
(##################) ⠇ reify:fsevents: sill reify mark deleted [ '/app/sweagent/frontend/node_modules/fsevents' ]
(##################) ⠇ reify:fsevents: sill reify mark deleted [ '/app/sweagent/frontend/node_modules/fsevents' ]
(##################) ⠇ reify:fsevents: sill reify mark deleted [ '/app/sweagent/frontend/node_modules/fsevents' ]
(##################) ⠴ reify:fsevents: sill reify mark deleted [ '/app/sweagent/frontend/node_modules/fsevents' ]
(##################) ⠴ reify:fsevents: sill reify mark deleted [ '/app/sweagent/frontend/node_modules/fsevents' ]
(##################) ⠴ reify:fsevents: sill reify mark deleted [ '/app/sweagent/frontend/node_modules/fsevents' ]
(##################) ⠴ reify:fsevents: sill reify mark deleted [ '/app/sweagent/frontend/node_modules/fsevents' ]

up to date, audited 1819 packages in 30m

349 packages are looking for funding
  run `npm fund` for details

22 vulnerabilities (1 low, 6 moderate, 15 high)

To address issues that do not require attention, run:
  npm audit fix

To address all issues (including breaking changes), run:
  npm audit fix --force

Run `npm audit` for details.

                        -------------

__/\\\\\\\____/\\____________/\\____/\\\\\_____
 _\/\\/\\_\/\\\________/\\\__/\\///\\___
  _\/\\_______\/\\_\/\\//\\____/\\//\\_\///______\//\\__
   _\/\\\\\\\/__\/\\///\\/\\/_\/\\___________/\\/___
    _\/\\/____\/\\__\///\\/___\/\\________/\\//_____
     _\/\\_____________\/\\____\///_____\/\\_____/\\//________
      _\/\\_____________\/\\_____________\/\\___/\\/___________
       _\/\\_____________\/\\_____________\/\\__/\\\\\\\\_
        _\///______________\///______________\///__\///__


                          Runtime Edition

        PM2 is a Production Process Manager for Node.js applications
                     with a built-in Load Balancer.

                Start and Daemonize any application:
                $ pm2 start app.js

                Load Balance 4 instances of api.js:
                $ pm2 start api.js -i 4

                Monitor in production:
                $ pm2 monitor

                Make pm2 auto-boot at server restart:
                $ pm2 startup

                To go further checkout:
                http://pm2.io/


                        -------------

[PM2] Spawning PM2 daemon with pm2_home=/root/.pm2
[PM2] PM2 Successfully daemonized
[PM2] Starting /usr/bin/npm in fork_mode (1 instance)
[PM2] Done.
┌────┬──────────────┬─────────────┬─────────┬─────────┬──────────┬────────┬──────┬───────────┬──────────┬──────────┬──────────┬──────────┐
│ id │ name         │ namespace   │ version │ mode    │ pid      │ uptime │ ↺    │ status    │ cpu      │ mem      │ user     │ watching │
├────┼──────────────┼─────────────┼─────────┼─────────┼──────────┼────────┼──────┼───────────┼──────────┼──────────┼──────────┼──────────┤
│ 0  │ swe-agent    │ default     │ N/A     │ fork    │ 149      │ 0s     │ 0    │ online    │ 0%       │ 17.6mb   │ root     │ disabled │
└────┴──────────────┴─────────────┴─────────┴─────────┴──────────┴────────���──────┴───────────┴──────────┴──────────┴──────────┴──────────┘
* If you are running on your own machine, then a browser window 
  should have already opened. If not, wait a few more seconds, then 
  open your browser at http://localhost:3000
* If you are running in github codespaces, please click the popup 
  that offers to forward port 3000 (not 8000!).
  Missed it? Find more information at 
  https://princeton-nlp.github.io/SWE-agent/installation/codespaces#running-the-web-ui
* Something went wrong? Please check 
  web_api.log for error messages!
* See here for more information: https://princeton-nlp.github.io/SWE-agent/usage/web_ui/

URL访问:

http://YourIP:3000/

ACI(Agent Computer Interface)总界面:

  • 上半部分是issue地址、模型、环境等配置参数。

  • 下半部分左边展示了Agent的思考过程。

  • 下半部分右上角区域展示了容器在终端执行的命令及输出结果。

  • 下半部分右下角区域是所有日志。

image.png

模型名称记得在【gpt4o】前面带上【azure:】,后台根据azure字符串来识别用的Azure的API:

image.png

agent执行代码的环境,这里官方是建议显式指定所有安装命令,容器启动时会创建conda环境:

image.png

其它配置,没动:

image.png

Agent执行逐步骤截图:

1. 列出代码库的目录结构,尝试找到issue中bug所在的文件:

image.png

2. 看错误信息该文件似乎在tests目录下,列出tests目录下的文件:

image.png

3. 找到了该文件,打开该文件,识别出错误信息里提到的语法问题:

image.png

4. 看来是第4行函数定义末尾少了个冒号,给加上:

image.png

5. 我们再跑下代码,看看还有没有错:

image.png

6. 成功运行,输出了期望结果,提交下代码:

image.png

7. 提交成功,销毁容器,完成任务:

image.png

官方命令行接口方式启动:

启动命令:

其中,config_file是System Prompt模板配置文件。

# 复用启动WebServer时的容器

root@10da16dc8a9e:/app# python run.py   --model_name azure:gpt4o   --data_path https://github.com/SWE-agent/test-repo/issues/1   --config_file config/default_from_url.yaml

输出日志:

INFO     Base commit reference None resolved to commit hash 8c179cd2be750cd9f2bb91b21adb39934311e9b8
INFO     💽 Loaded dataset from https://github.com/SWE-agent/test-repo/issues/1
INFO     Found image sweagent/swe-agent:latest with tags: ['sweagent/swe-agent:latest'], created: 2024-06-05T01:13:45.176471384Z for linux arm64.
DEBUG    Starting container with command: docker run -i --rm --name sweagent-swe-agent-latest-01edf87adc sweagent/swe-agent:latest /bin/bash -l
INFO     🌱 Environment Initialized
DEBUG    Environment initialization took 2.09 seconds
INFO     ▶️  Beginning task 0
INFO     Trying to clone from non-mirror...
WARNING  install_environment is set to True, but the data path is a GitHub URL without an environment config file (environment_config key/flag). Skipping conda environment installation.
INFO     Initializing agent settings for container 26cd13d1f31252475cde7e1ae1981d11f43e88d2066c2532611f5f2182d42737
INFO     Resetting model stats
INFO     SYSTEM (primary)
         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 100 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:
         open:
           docstring: opens the file at the given path in the editor. If line_number is provided, the window will be move to include that line
           signature: open <path> [<line_number>]
           arguments:
             - path (string) [required]: the path to the file to open
             - line_number (integer) [optional]: the line number to move the window to (if not provided, the window will start at the top of the file)

         goto:
           docstring: moves the window to show <line_number>
           signature: goto <line_number>
           arguments:
             - line_number (integer) [required]: the line number to move the window to

         scroll_down:
           docstring: moves the window down {WINDOW} lines
           signature: scroll_down

         scroll_up:
           docstring: moves the window down {WINDOW} lines
           signature: scroll_up

         create:
           docstring: creates and opens a new file with the given name
           signature: create <filename>
           arguments:
             - filename (string) [required]: the name of the file to create

         submit:
           docstring: submits your current code and terminates the session
           signature: submit

         search_dir:
           docstring: searches for search_term in all files in dir. If dir is not provided, searches in the current directory
           signature: search_dir <search_term> [<dir>]
           arguments:
             - search_term (string) [required]: the term to search for
             - dir (string) [optional]: the directory to search in (if not provided, searches in the current directory)

         search_file:
           docstring: searches for search_term in file. If file is not provided, searches in the current open file
           signature: search_file <search_term> [<file>]
           arguments:
             - search_term (string) [required]: the term to search for
             - file (string) [optional]: the file to search in (if not provided, searches in the current open file)

         find_file:
           docstring: finds all files with the given name in dir. If dir is not provided, searches in the current directory
           signature: find_file <file_name> [<dir>]
           arguments:
             - file_name (string) [required]: the name of the file to search for
             - dir (string) [optional]: the directory to search in (if not provided, searches in the current directory)

         edit:
           docstring: replaces lines <start_line> through <end_line> (inclusive) with the given text in the open file. The replacement text is terminated by a line with only end_of_edit on it. All of the
         <replacement text> will be entered, so make sure your indentation is formatted properly. Python files will be checked for syntax errors after the edit. If the system detects a syntax error, the edit will
         not be executed. Simply try to edit the file again, but make sure to read the error message and modify the edit command you issue accordingly. Issuing the same command a second time will just lead to the
         same error message again.
           signature: edit <start_line>:<end_line>
         <replacement_text>
         end_of_edit
           arguments:
             - start_line (integer) [required]: the line number to start the edit at
             - end_line (integer) [required]: the line number to end the edit at (inclusive)
             - replacement_text (string) [required]: the text to replace the current selection with



         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.
INFO     DEMONSTRATION:
         /Users/fuchur/Documents/24/git_sync/SWE-agent/trajectories/demonstrations/replay__marshmallow-code__marshmallow-1867__default__t-0.20__p-0.95__c-2.00__install-1___install_from_source/marshmallow-code__ma
         rshmallow-1867.traj
INFO     Trajectory will be saved to trajectories/fuchur/azure-gpt4__SWE-agent__test-repo__default_from_url__t-0.00__p-0.95__c-2.00__install-1/SWE-agent__test-repo-i1.traj
INFO     🤖 MODEL INPUT
         We're currently solving the following issue within our repository. Here's the issue text:
         ISSUE:
         SyntaxError: invalid syntax
         I'm running `missing_colon.py` as follows:

         ```python
         division(23, 0)
         ```

         but I get the following error:

         ```
           File "/Users/fuchur/Documents/24/git_sync/swe-agent-test-repo/tests/./missing_colon.py", line 4
             def division(a: float, b: float) -> float
                                                      ^
         SyntaxError: invalid syntax
         ```


         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.

         7. It may be necessary to install the repository from source before you can run code. Please think about how to install the environment from the repository directory if you need to do so.


         (Open file: n/a)
         (Current directory: /SWE-agent__test-repo)
         bash-$
INFO     input_tokens=10,194, output_tokens=56, instance_cost=0.10, cost=0.10
INFO     total_tokens_sent=10,194, total_tokens_received=56, total_cost=0.10, total_api_calls=1
INFO     💭 THOUGHT (primary)
         First, let's locate the file `missing_colon.py` mentioned in the issue to understand the syntax error and fix it. We'll use the `find_file` command to search for this file in the current directory.

INFO     🎬 ACTION (primary)
         find_file "missing_colon.py"

INFO     🤖 MODEL INPUT
         Found 1 matches for "missing_colon.py" in /SWE-agent__test-repo:
         /SWE-agent__test-repo/tests/missing_colon.py
         (Open file: n/a)
         (Current directory: /SWE-agent__test-repo)
         bash-$
INFO     input_tokens=10,314, output_tokens=45, instance_cost=0.21, cost=0.10
INFO     total_tokens_sent=20,508, total_tokens_received=101, total_cost=0.21, total_api_calls=2
INFO     💭 THOUGHT (primary)
         Now that we've located the file `missing_colon.py` in the `tests` directory, let's open it to inspect the syntax error mentioned in the issue.

INFO     🎬 ACTION (primary)
         open tests/missing_colon.py

INFO     🤖 MODEL INPUT
         [File: /SWE-agent__test-repo/tests/missing_colon.py (10 lines total)]
         1:#!/usr/bin/env python3
         2:
         3:
         4:def division(a: float, b: float) -> float
         5:    return a/b
         6:
         7:
         8:if __name__ == "__main__":
         9:    print(division(123, 15))
         10:
         (Open file: /SWE-agent__test-repo/tests/missing_colon.py)
         (Current directory: /SWE-agent__test-repo)
         bash-$
INFO     input_tokens=10,484, output_tokens=59, instance_cost=0.31, cost=0.11
INFO     total_tokens_sent=30,992, total_tokens_received=160, total_cost=0.31, total_api_calls=3
INFO     💭 THOUGHT (primary)
         The syntax error is due to the missing colon at the end of the function definition on line 4. Let's add the missing colon to correct the syntax error.

INFO     🎬 ACTION (primary)
         edit 4:4
         def division(a: float, b: float) -> float:
         end_of_edit

INFO     🤖 MODEL INPUT
         [File: /SWE-agent__test-repo/tests/missing_colon.py (10 lines total)]
         1:#!/usr/bin/env python3
         2:
         3:
         4:def division(a: float, b: float) -> float:
         5:    return a/b
         6:
         7:
         8:if __name__ == "__main__":
         9:    print(division(123, 15))
         10:
         File updated. Please review the changes and make sure they are correct (correct indentation, no duplicate lines, etc). Edit the file again if necessary.
         (Open file: /SWE-agent__test-repo/tests/missing_colon.py)
         (Current directory: /SWE-agent__test-repo)
         bash-$
INFO     input_tokens=10,698, output_tokens=47, instance_cost=0.42, cost=0.11
INFO     total_tokens_sent=41,690, total_tokens_received=207, total_cost=0.42, total_api_calls=4
INFO     💭 THOUGHT (primary)
         The syntax error has been corrected by adding the missing colon at the end of the function definition. Now, let's run the script to ensure that it executes without any syntax errors.

INFO     🎬 ACTION (primary)
         python tests/missing_colon.py

INFO     🤖 MODEL INPUT
         8.2
         (Open file: /SWE-agent__test-repo/tests/missing_colon.py)
         (Current directory: /SWE-agent__test-repo)
         bash-$
INFO     input_tokens=10,790, output_tokens=45, instance_cost=0.53, cost=0.11
INFO     total_tokens_sent=52,480, total_tokens_received=252, total_cost=0.53, total_api_calls=5
INFO     💭 THOUGHT (primary)
         The script executed successfully and printed the result of the division function, which confirms that the syntax error has been resolved. Now that the issue is fixed, we can submit the changes to the
         code base.

INFO     🎬 ACTION (primary)
         submit

INFO     Found submission:
         diff --git a/tests/missing_colon.py b/tests/missing_colon.py
         index 20edef5..5857437 100755
         --- a/tests/missing_colon.py
         +++ b/tests/missing_colon.py
         @@ -1,7 +1,7 @@
          #!/usr/bin/env python3


         -def division(a: float, b: float) -> float
         +def division(a: float, b: float) -> float:
              return a/b



INFO     Trajectory saved to trajectories/fuchur/azure-gpt4__SWE-agent__test-repo__default_from_url__t-0.00__p-0.95__c-2.00__install-1/SWE-agent__test-repo-i1.traj
INFO     Saved predictions to trajectories/fuchur/azure-gpt4__SWE-agent__test-repo__default_from_url__t-0.00__p-0.95__c-2.00__install-1/all_preds.jsonl
╭──────────────────────────── 🎉 Submission successful 🎉 ────────────────────────────╮
│ SWE-agent has produced a patch that it believes will solve the issue you submitted! │
│ Use the code snippet below to inspect or apply it!                                  │
╰─────────────────────────────────────────────────────────────────────────────────────╯

 # The patch has been saved to your local filesystem at:
 PATCH_FILE_PATH='/Users/fuchur/Documents/24/git_sync/SWE-agent/trajectories/fuchur/azure-gpt4__SWE-agent__test-repo__default_from_url__t-0.00__p-0.95__c-2.00__install-1/patches/SWE-agent__test-repo-i1.patch'
 # Inspect it:
 cat "${PATCH_FILE_PATH}"
 # Apply it to a local repository:
 cd <your local repo root>
 git apply "${PATCH_FILE_PATH}"

日志解读:

1. 读取指定的github issue:

INFO     💽 Loaded dataset from https://github.com/SWE-agent/test-repo/issues/1

2. 创建容器(用于调试代码):

INFO     Found image sweagent/swe-agent:latest with tags: ['sweagent/swe-agent:latest'], created: 2024-06-05T01:13:45.176471384Z for linux arm64.
DEBUG    Starting container with command: docker run -i --rm --name sweagent-swe-agent-latest-01edf87adc sweagent/swe-agent:latest /bin/bash -l
INFO     🌱 Environment Initialized
DEBUG    Environment initialization took 2.09 seconds

3. 克隆源码到容器中:

INFO     ▶️  Beginning task 0
INFO     Trying to clone from non-mirror...

4. 安装依赖环境、初始化Agent设置:

WARNING  install_environment is set to True, but the data path is a GitHub URL without an environment config file (environment_config key/flag). Skipping conda environment installation.
INFO     Initializing agent settings for container 26cd13d1f31252475cde7e1ae1981d11f43e88d2066c2532611f5f2182d42737
INFO     Resetting model stats

5. 设置System Prompt(即给Agent设置身份):

INFO     SYSTEM (primary)
         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 100 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:
         open:
           docstring: opens the file at the given path in the editor. If line_number is provided, the window will be move to include that line
           signature: open <path> [<line_number>]
           arguments:
             - path (string) [required]: the path to the file to open
             - line_number (integer) [optional]: the line number to move the window to (if not provided, the window will start at the top of the file)

         goto:
           docstring: moves the window to show <line_number>
           signature: goto <line_number>
           arguments:
             - line_number (integer) [required]: the line number to move the window to

         scroll_down:
           docstring: moves the window down {WINDOW} lines
           signature: scroll_down

         scroll_up:
           docstring: moves the window down {WINDOW} lines
           signature: scroll_up

         create:
           docstring: creates and opens a new file with the given name
           signature: create <filename>
           arguments:
             - filename (string) [required]: the name of the file to create

         submit:
           docstring: submits your current code and terminates the session
           signature: submit

         search_dir:
           docstring: searches for search_term in all files in dir. If dir is not provided, searches in the current directory
           signature: search_dir <search_term> [<dir>]
           arguments:
             - search_term (string) [required]: the term to search for
             - dir (string) [optional]: the directory to search in (if not provided, searches in the current directory)

         search_file:
           docstring: searches for search_term in file. If file is not provided, searches in the current open file
           signature: search_file <search_term> [<file>]
           arguments:
             - search_term (string) [required]: the term to search for
             - file (string) [optional]: the file to search in (if not provided, searches in the current open file)

         find_file:
           docstring: finds all files with the given name in dir. If dir is not provided, searches in the current directory
           signature: find_file <file_name> [<dir>]
           arguments:
             - file_name (string) [required]: the name of the file to search for
             - dir (string) [optional]: the directory to search in (if not provided, searches in the current directory)

         edit:
           docstring: replaces lines <start_line> through <end_line> (inclusive) with the given text in the open file. The replacement text is terminated by a line with only end_of_edit on it. All of the
         <replacement text> will be entered, so make sure your indentation is formatted properly. Python files will be checked for syntax errors after the edit. If the system detects a syntax error, the edit will
         not be executed. Simply try to edit the file again, but make sure to read the error message and modify the edit command you issue accordingly. Issuing the same command a second time will just lead to the
         same error message again.
           signature: edit <start_line>:<end_line>
         <replacement_text>
         end_of_edit
           arguments:
             - start_line (integer) [required]: the line number to start the edit at
             - end_line (integer) [required]: the line number to end the edit at (inclusive)
             - replacement_text (string) [required]: the text to replace the current selection with



         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.

该System Prompt配置了一个自主程序员的角色,该程序员在命令行中使用特殊界面工作。该界面包括一个文件编辑器,每次显示文件的100行。程序员可以使用典型的bash命令以及一些特殊命令来导航和编辑文件。System Prompt详细描述了这些特殊命令的使用方法及其参数,并强调了编辑命令时正确缩进的重要性。程序员的响应格式需要包括讨论和命令两个部分,每次只能执行一个命令并等待反馈。

Prompt翻译成中文如下:

SETTING:
您是一名自主程序员,正在使用一个特殊界面直接在命令行工作。
该特殊界面包括一个文件编辑器,每次显示文件的100行。
除了典型的bash命令,您还可以使用以下命令来帮助您导航和编辑文件。

COMMANDS:
     open:
       文档字符串:在编辑器中打开给定路径的文件。如果提供了行号,窗口将移动以包含该行
       签名:open <路径> [<行号>]
       参数:
         - 路径(字符串)[必需]:要打开的文件的路径
         - 行号(整数)[可选]:要移动窗口到的行号(如果未提供,窗口将从文件顶部开始)

     goto:
       文档字符串:将窗口移动到显示<行号>
       签名:goto <行号>
       参数:
         - 行号(整数)[必需]:要移动窗口到的行号

     scroll_down:
       文档字符串:将窗口向下移动{WINDOW}行
       签名:scroll_down

     scroll_up:
       文档字符串:将窗口向上移动{WINDOW}行
       签名:scroll_up

     create:
       文档字符串:创建并打开一个具有给定名称的新文件
       签名:create <文件名>
       参数:
         - 文件名(字符串)[必需]:要创建的文件的名称

     submit:
       文档字符串:提交您当前的代码并终止会话
       签名:submit

     search_dir:
       文档字符串:在目录中的所有文件中搜索search_term。如果未提供目录,则在当前目录中搜索
       签名:search_dir <搜索词> [<目录>]
       参数:
         - 搜索词(字符串)[必需]:要搜索的词
         - 目录(字符串)[可选]:要搜索的目录(如果未提供,在当前目录中搜索)

     search_file:
       文档字符串:在文件中搜索search_term。如果未提供文件,则在当前打开的文件中搜索
       签名:search_file <搜索词> [<文件>]
       参数:
         - 搜索词(字符串)[必需]:要搜索的词
         - 文件(字符串)[可选]:要搜索的文件(如果未提供,在当前打开的文件中搜索)

     find_file:
       文档字符串:在目录中查找具有给定名称的所有文件。如果未提供目录,则在当前目录中搜索
       签名:find_file <文件名> [<目录>]
       参数:
         - 文件名(字符串)[必需]:要搜索的文件的名称
         - 目录(字符串)[可选]:要搜索的目录(如果未提供,在当前目录中搜索)

     edit:
       文档字符串:用给定的文本替换打开文件中<开始行>到<结束行>(包括)的行。替换文本以仅包含end_of_edit的行为终止。所有<替换文本>将被输入,因此请确保您的缩进格式正确。Python文件将在编辑后检查语法错误。如果系统检测到语法错误,编辑将不会执行。只需再次尝试编辑文件,但请务必阅读错误消息并相应地修改发出的编辑命令。第二次发出相同的命令只会再次导致相同的错误消息。
       签名:edit <开始行>:<结束行>
     <替换文本>
     end_of_edit
       参数:
         - 开始行(整数)[必需]:开始编辑的行号
         - 结束行(整数)[必需]:结束编辑的行号(包括)
         - 替换文本(字符串)[必需]:要用当前选择替换的文本

     请注意,编辑命令需要正确的缩进。
     如果您想添加行'        print(x)',您必须完整地写出来,【包括代码前的所有空格!】缩进很重要,缩进不正确的代码将失败并需要修复后才能运行。

     响应格式:
     您的shell提示符格式如下:
     (Open file:<path>)<cwd> $

     您需要使用两个字段来格式化输出:DISCUSSION和COMMAND。
     您的输出应该始终包含一个DISCUSSION和一个COMMAND字段,格式如下示例:
     
     DISCUSSION
     首先,我将使用ls查看当前目录中的文件。然后我们可以看看一些相关的文件是什么样子的。
     ```
     ls -a
     ```

     您在COMMAND部分应该只包含【一个】命令,然后在继续更多讨论和命令之前等待shell的响应。讨论部分的所有内容都将被保存以供将来参考。
     如果您想一次发出两个命令,【请不要这样做!】请先提交第一个命令,然后在收到响应后,您将能够发出第二个命令。
     除了上面列出的特殊命令,您可以自由使用任何其他bash命令(例如find、grep、cat、ls、cd)。
     但是,该环境不支持交互式会话命令(例如python、vim),所以请不要调用它们。

6. 陈述issue(此处应该还在System Prompt里):

将用户反映的这个issue的内容陈述一遍:

INFO     🤖 MODEL INPUT
         We're currently solving the following issue within our repository. Here's the issue text:
         ISSUE:
         SyntaxError: invalid syntax
         I'm running `missing_colon.py` as follows:

         ```python
         division(23, 0)
         ```

         but I get the following error:

         ```
           File "/Users/fuchur/Documents/24/git_sync/swe-agent-test-repo/tests/./missing_colon.py", line 4
             def division(a: float, b: float) -> float
                                                      ^
         SyntaxError: invalid syntax
         ```

7. 指导说明(此处应该还在System Prompt里):

         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.

         7. It may be necessary to install the repository from source before you can run code. Please think about how to install the environment from the repository directory if you need to do so.


         (Open file: n/a)
         (Current directory: /SWE-agent__test-repo)
         bash-$

主要就是指导Agent如何在终端中自主解决问题,在解决问题的过程中需要遵循的规则和最佳实践。例如,首先要尝试重现错误,逐步编辑和检查文件,并且只能每次输入一条命令。此外,注意文件路径、缩进、以及可能需要的环境安装问题。

翻译成中文如下:

【指导说明】:
现在,你将自己解决这个问题。你的终端会话已经启动,你现在位于代码库的根目录下。你可以使用任何 bash 命令或特殊界面来帮助你完成任务。 你可以编辑所有需要的文件,运行任何你想要的检查或测试。 请记住,你只能每次输入一条命令。在每次命令执行后,等待反馈。 当你对所做的所有更改感到满意时,可以通过运行 submit 命令提交更改到代码库。 但是请注意,你不能在这个环境中使用任何交互式会话命令(例如,python, vim),但你可以编写脚本并运行它们。例如,你可以编写一个 python 脚本,然后使用 python <脚本名>.py 运行它。

关于编辑命令的注意事项: 【缩进非常重要!】编辑文件时,确保在每一行之前插入适当的缩进!

【重要提示】:
1. 总是从尝试重现问题开始。如果问题包括用于重现错误的代码,建议你在你的环境中重新实现它,并运行它以确保可以重现该错误。然后开始尝试修复它。
当你认为你已经修复了错误时,重新运行 bug 重现脚本以确保该错误确实被修复。
如果 bug 重现脚本在成功运行时没有打印任何内容,建议在文件末尾添加 print("脚本成功完成,没有错误。") 命令,以便你能确定脚本确实成功运行到最后。

2. 如果运行某个命令失败,尝试运行其他命令。除非你修改了它,否则某个命令第一次运行失败,第二次运行也不会成功。

3. 如果打开一个文件并且需要到达特定行,比如 583 行,不要仅仅通过多次使用 scroll_down 命令来滚动。相反,使用 goto 583 命令,这样更快。

4. 如果 bug 重现脚本需要输入/读取一个特定文件,比如 buggy-input.png,并且你想了解如何输入该文件,可以在现有的代码库中进行搜索,查看是否已经有人实现了相关代码。运行 find_file "buggy-input.png" 命令。如果没有效果,使用 Linux 的 find 命令。

5. 一定要查看当前打开的文件和当前工作目录(显示在当前打开文件的后面)。当前打开的文件可能位于与工作目录不同的目录中!请注意,有些命令(如 create)会打开文件,因此它们可能会更改当前打开的文件。

6. 在编辑文件时,容易意外指定错误的行号或编写缩进错误的代码。编辑后总是检查代码,确保它反映了你想要实现的内容。如果没有实现,发出另一条命令进行修复。

7. 在运行代码之前,可能需要从源代码安装代码库。请思考如何从代码库目录安装环境。

8. 利用思维链,Agent逐步解决问题:

Agent采用Thought->Action->Model Input模式,一步一步思考与行动,直到最后解决问题(或多次尝试无解后放弃),依次执行了查找代码文件、打开文件、修改代码、运行代码、提交代码的操作:

INFO     💭 THOUGHT (primary)
         First, let's locate the file `missing_colon.py` mentioned in the issue to understand the syntax error and fix it. We'll use the `find_file` command to search for this file in the current directory.

INFO     🎬 ACTION (primary)
         find_file "missing_colon.py"

INFO     🤖 MODEL INPUT
         Found 1 matches for "missing_colon.py" in /SWE-agent__test-repo:
         /SWE-agent__test-repo/tests/missing_colon.py
         (Open file: n/a)
         (Current directory: /SWE-agent__test-repo)
         bash-$
INFO     input_tokens=10,314, output_tokens=45, instance_cost=0.21, cost=0.10
INFO     total_tokens_sent=20,508, total_tokens_received=101, total_cost=0.21, total_api_calls=2
INFO     💭 THOUGHT (primary)
         Now that we've located the file `missing_colon.py` in the `tests` directory, let's open it to inspect the syntax error mentioned in the issue.

INFO     🎬 ACTION (primary)
         open tests/missing_colon.py

INFO     🤖 MODEL INPUT
         [File: /SWE-agent__test-repo/tests/missing_colon.py (10 lines total)]
         1:#!/usr/bin/env python3
         2:
         3:
         4:def division(a: float, b: float) -> float
         5:    return a/b
         6:
         7:
         8:if __name__ == "__main__":
         9:    print(division(123, 15))
         10:
         (Open file: /SWE-agent__test-repo/tests/missing_colon.py)
         (Current directory: /SWE-agent__test-repo)
         bash-$
INFO     input_tokens=10,484, output_tokens=59, instance_cost=0.31, cost=0.11
INFO     total_tokens_sent=30,992, total_tokens_received=160, total_cost=0.31, total_api_calls=3
INFO     💭 THOUGHT (primary)
         The syntax error is due to the missing colon at the end of the function definition on line 4. Let's add the missing colon to correct the syntax error.

INFO     🎬 ACTION (primary)
         edit 4:4
         def division(a: float, b: float) -> float:
         end_of_edit

INFO     🤖 MODEL INPUT
         [File: /SWE-agent__test-repo/tests/missing_colon.py (10 lines total)]
         1:#!/usr/bin/env python3
         2:
         3:
         4:def division(a: float, b: float) -> float:
         5:    return a/b
         6:
         7:
         8:if __name__ == "__main__":
         9:    print(division(123, 15))
         10:
         File updated. Please review the changes and make sure they are correct (correct indentation, no duplicate lines, etc). Edit the file again if necessary.
         (Open file: /SWE-agent__test-repo/tests/missing_colon.py)
         (Current directory: /SWE-agent__test-repo)
         bash-$
INFO     input_tokens=10,698, output_tokens=47, instance_cost=0.42, cost=0.11
INFO     total_tokens_sent=41,690, total_tokens_received=207, total_cost=0.42, total_api_calls=4
INFO     💭 THOUGHT (primary)
         The syntax error has been corrected by adding the missing colon at the end of the function definition. Now, let's run the script to ensure that it executes without any syntax errors.

INFO     🎬 ACTION (primary)
         python tests/missing_colon.py

INFO     🤖 MODEL INPUT
         8.2
         (Open file: /SWE-agent__test-repo/tests/missing_colon.py)
         (Current directory: /SWE-agent__test-repo)
         bash-$
INFO     input_tokens=10,790, output_tokens=45, instance_cost=0.53, cost=0.11
INFO     total_tokens_sent=52,480, total_tokens_received=252, total_cost=0.53, total_api_calls=5
INFO     💭 THOUGHT (primary)
         The script executed successfully and printed the result of the division function, which confirms that the syntax error has been resolved. Now that the issue is fixed, we can submit the changes to the
         code base.

INFO     🎬 ACTION (primary)
         submit

INFO     Found submission:
         diff --git a/tests/missing_colon.py b/tests/missing_colon.py
         index 20edef5..5857437 100755
         --- a/tests/missing_colon.py
         +++ b/tests/missing_colon.py
         @@ -1,7 +1,7 @@
          #!/usr/bin/env python3


         -def division(a: float, b: float) -> float
         +def division(a: float, b: float) -> float:
              return a/b

9. 完成代码提交,结束任务:

INFO     Trajectory saved to trajectories/fuchur/azure-gpt4__SWE-agent__test-repo__default_from_url__t-0.00__p-0.95__c-2.00__install-1/SWE-agent__test-repo-i1.traj
INFO     Saved predictions to trajectories/fuchur/azure-gpt4__SWE-agent__test-repo__default_from_url__t-0.00__p-0.95__c-2.00__install-1/all_preds.jsonl
╭──────────────────────────── 🎉 Submission successful 🎉 ────────────────────────────╮
│ SWE-agent has produced a patch that it believes will solve the issue you submitted! │
│ Use the code snippet below to inspect or apply it!                                  │
╰─────────────────────────────────────────────────────────────────────────────────────╯

 # The patch has been saved to your local filesystem at:
 PATCH_FILE_PATH='/Users/fuchur/Documents/24/git_sync/SWE-agent/trajectories/fuchur/azure-gpt4__SWE-agent__test-repo__default_from_url__t-0.00__p-0.95__c-2.00__install-1/patches/SWE-agent__test-repo-i1.patch'
 # Inspect it:
 cat "${PATCH_FILE_PATH}"
 # Apply it to a local repository:
 cd <your local repo root>
 git apply "${PATCH_FILE_PATH}"
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值