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.txtPros
- +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 Case | Why SWE-agent |
|---|---|
| GitHub Issue Automation | Automatically fix bugs in open-source projects |
| Bug Triage | Reduce manual effort for bug reporting |
| Code Review | AI-assisted code analysis and suggestions |
| Research | Study AI software engineering capabilities |
Comparison with Alternatives
| Feature | SWE-agent | OpenHands | Devika | Aider |
|---|---|---|---|---|
| Focus | GitHub Issues | General development | General development | CLI 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 Curve | Medium | Medium | Medium | Low |
| Best for | Issue automation | Full development | Planning | CLI users |
Best Practices
- Start with simple issues — Begin with well-defined, isolated bugs
- Provide clear issue context — Include reproduction steps and expected behavior
- Review generated changes — Always verify AI-generated fixes before merging
- Use appropriate model — GPT-4 for complex issues, smaller models for simple ones
- Test thoroughly — Run full test suite after AI-generated changes
Troubleshooting
| Issue | Solution |
|---|---|
| Agent gets stuck | Provide more context or break issue into smaller tasks |
| Test failures persist | Review test output and provide feedback to agent |
| Code navigation fails | Verify repository is accessible and dependencies installed |
| Low success rate | Use stronger model (GPT-4) for complex issues |
