SW

SWE-agent

18,000PythonAI Software Engineer

Stanford-developed AI software engineer for automatically fixing GitHub Issues.

PythonGitHubBug FixResearch

Overview

SWE-agent is an AI software agent system developed by Stanford University, specifically designed to solve real bugs in GitHub Issues. It decomposes complex software engineering tasks into manageable sub-tasks, combining code understanding, tool usage, and iterative improvement capabilities.

Features

  • GitHub Issues automatic fixing
  • Codebase navigation and exploration
  • Test-driven repair workflow
  • Iterative improvement based on feedback
  • Multi-tool support (edit, search, execute)
  • Context management for long conversations

Installation

git clone https://github.com/princeton-nlp/SWE-agent.git && cd SWE-agent && pip install -r requirements.txt

Pros

  • +Validated on real GitHub Issues
  • +Open-source and customizable
  • +Supports multiple LLM backends
  • +Strong code understanding capabilities
  • +Test-driven repair ensures quality
  • +Backed by Stanford research

Cons

  • Primarily depends on GPT-4, costly
  • Success rate on complex problems still improving
  • Requires development environment setup
  • Limited support for non-Python projects

Alternatives

Documentation

SWE-agent

Overview

SWE-agent 是由斯坦福大学开发的一个 AI 软件工程师代理系统,专门用于解决 GitHub Issues 中的真实 bug。它通过将复杂的软件工程任务分解为可管理的子任务,结合代码理解、工具使用和迭代改进能力,成功解决了多个开源项目中的真实问题。

SWE-agent 的核心创新在于其"代理-环境"交互模式:代理可以读取代码、运行测试、执行命令、查看输出,并根据反馈不断调整策略。这种模式使得 SWE-agent 能够像人类开发者一样进行"试错"学习,逐步接近问题的解决方案。

Features

  • GitHub Issues 自动修复:专门针对 GitHub Issues 设计
  • 代码库导航:自动探索和理解代码结构
  • 测试驱动修复:通过运行测试验证修复效果
  • 迭代改进:基于错误反馈持续优化解决方案
  • 多工具支持:支持编辑、搜索、执行命令等多种工具
  • 上下文管理:智能管理长对话和代码上下文
  • 开源可定制:完全开源,可根据需求定制

Installation

# 克隆仓库
git clone https://github.com/princeton-nlp/SWE-agent.git
cd SWE-agent

# 安装依赖
pip install -r requirements.txt

# 配置 API key
export OPENAI_API_KEY=your-api-key

# 运行
python swe_agent/run.py --repo owner/repo --issue-number 123

Quick Start

# 修复单个 GitHub Issue
python swe_agent/run.py \
  --repo "psf/requests" \
  --issue-number 3548 \
  --model "gpt-4"

# 批量修复多个 Issues
python swe_agent/run.py \
  --repo "psf/requests" \
  --issue-file issues.json \
  --model "gpt-4-turbo"

Core Concepts

代理架构

SWE-agent 使用基于 LLM 的代理架构:

┌─────────────────────────────────────┐
│           LLM (GPT-4)               │
├─────────────────────────────────────┤
│  观察 (Observation)                 │
│  - 代码片段                          │
│  - 测试输出                          │
│  - 错误信息                          │
│  - 文件列表                          │
├─────────────────────────────────────┤
│  行动 (Action)                      │
│  - edit: 编辑文件                    │
│  - search: 搜索代码                  │
│  - execute: 运行命令                 │
│  - submit: 提交解决方案              │
└─────────────────────────────────────┘

环境交互

SWE-agent 在一个模拟的软件开发环境中运行:

环境组件功能
代码编辑器读取、搜索、修改代码文件
Shell执行 shell 命令
测试运行器运行单元测试和集成测试
Git版本控制和分支管理
文件浏览器探索项目结构

Examples

示例 1:修复 requests 库的 bug

Issue: #3548 - requests.get() raises ConnectionError on timeout

SWE-agent 执行流程:
1. 读取 issue 描述和复现步骤
2. 克隆 requests 仓库
3. 编写复现脚本验证问题
4. 搜索相关代码(requests/models.py, sessions.py)
5. 定位到 timeout 处理逻辑
6. 分析代码,发现异常处理不完整
7. 修改代码添加正确的异常处理
8. 运行测试验证修复
9. 提交 PR

示例 2:添加新功能

Issue: #123 - Add support for custom HTTP headers in API calls

SWE-agent 执行流程:
1. 理解需求:需要在 API 调用中添加自定义 header 支持
2. 搜索现有代码,找到 API 调用相关模块
3. 分析现有 header 处理逻辑
4. 设计实现方案
5. 修改代码添加 header 参数
6. 编写单元测试
7. 运行所有测试确保无回归
8. 更新文档

Pros

  • ✅ 在真实 GitHub Issues 上验证过有效性
  • ✅ 开源可定制,可针对特定项目优化
  • ✅ 支持多种 LLM 后端
  • ✅ 强大的代码理解和导航能力
  • ✅ 测试驱动的修复流程保证质量
  • ✅ 斯坦福大学研究支持

Cons

  • ❌ 主要依赖 GPT-4,成本较高
  • ❌ 对某些复杂问题的解决率仍有提升空间
  • ❌ 需要配置开发环境
  • ❌ 对非 Python 项目的支持有限

When to Use

  • 需要自动化修复 GitHub Issues 时
  • 想要减少重复性 bug 修复工作时
  • 需要代码审查和测试时
  • 想要研究 AI 软件工程时
  • 处理大量相似类型的 bug 时

Use Cases

Use CaseWhy SWE-agent
GitHub Issue AutomationAutomatically fix bugs in open-source projects
Bug TriageReduce manual effort for bug reporting
Code ReviewAI-assisted code analysis and suggestions
ResearchStudy AI software engineering capabilities

Comparison with Alternatives

FeatureSWE-agentOpenHandsDevikaAider
FocusGitHub IssuesGeneral developmentGeneral developmentCLI pairing
Test-Driven✅ Yes✅ Yes✅ Yes⚠️ Limited
Iterative Fix✅ Yes✅ Yes✅ Yes⚠️ Limited
Open Source✅ Yes✅ Yes✅ Yes✅ Yes
SWE-bench✅ Leader⚠️ Participating⚠️ Participating❌ No
Learning CurveMediumMediumMediumLow
Best forIssue automationFull developmentPlanningCLI users

Best Practices

  1. Start with simple issues — Begin with well-defined, isolated bugs
  2. Provide clear issue context — Include reproduction steps and expected behavior
  3. Review generated changes — Always verify AI-generated fixes before merging
  4. Use appropriate model — GPT-4 for complex issues, smaller models for simple ones
  5. Test thoroughly — Run full test suite after AI-generated changes

Troubleshooting

IssueSolution
Agent gets stuckProvide more context or break issue into smaller tasks
Test failures persistReview test output and provide feedback to agent
Code navigation failsVerify repository is accessible and dependencies installed
Low success rateUse stronger model (GPT-4) for complex issues

Resources