Skip to content

zzszzs-maker/RepoPilot-CodeRepair-Agent

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

1 Commit
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

RepoPilot:测试驱动代码修复智能体

RepoPilot: LLM-based Test-Driven Code Repair Agent

RepoPilot 是一个面向 Python 项目的代码修复 Agent。用户上传 .zip 项目后,系统会自动运行 pytest,分析失败测试,读取相关源码,生成最小补丁,重新运行测试,并在前端展示修复前后结果、补丁 diff、修复后的文件预览、下载链接和完整 Agent 执行轨迹。

当前版本适合本地演示和可信代码测试。因为系统会运行上传项目中的 pytest,公开部署前需要加入 Docker 沙箱、资源限制和网络隔离。

功能特性

  • 中文 Streamlit 前端页面
  • 上传 .zip Python 项目
  • 自动运行修复前 / 修复后 pytest
  • ReAct-style Agent 工具调用循环
  • edit_file(path, old_text, new_text) 局部补丁编辑
  • 规则 Reflection + LLM Reflection
  • 规则 Replanning + LLM Replanning
  • classic 手写循环运行时 + LangGraph 图式运行时
  • 自动生成 unified diff
  • 前端展示问题诊断与修复说明
  • 前端展示修复后的文件内容
  • 前端下载修复后的项目 zip
  • 前端下载 patch.diff
  • JSON / Markdown trajectory logging
  • 10 个 seeded Python bug benchmark
  • 支持评估和 ablation 脚本

项目结构

RepoPilot/
├── app.py                         # 中文 Streamlit Web Demo
├── agent/                         # Agent Core
│   ├── main.py                    # classic ReAct loop
│   ├── graph_agent.py             # LangGraph runtime
│   ├── tools.py                   # list_files/read_file/search_code/edit_file/run_tests
│   ├── llm.py                     # mock + real LLM action decision
│   ├── prompts.py                 # prompt construction
│   ├── planner.py                 # planning + LLM replanning
│   ├── reflector.py               # reflection + LLM reflection
│   ├── memory.py                  # reflection memory
│   ├── trace.py                   # trajectory logging
│   └── evaluate.py                # benchmark evaluation
├── backend/
│   ├── workspace.py               # zip extraction and workspace management
│   ├── diff_utils.py              # unified diff generation
│   └── service.py                 # repair-job service + runtime switch
├── benchmark/                     # generated seeded bugs and zip files
├── scripts/
│   ├── create_benchmark.py
│   ├── check_benchmark.py
│   ├── smoke_test_backend.py
│   └── summarize_reports.py
├── reports/
├── runs/
└── requirements.txt

安装

pip install -r requirements.txt

依赖包括:

pytest
openai
streamlit
langgraph

快速开始:Mock 模式

Mock 模式不需要 API Key,默认可以稳定修复 bug_001_divide_zero

python scripts/create_benchmark.py
python scripts/smoke_test_backend.py --runtime classic

期望看到:

Initial Passed: False
Final Passed: True
Modified Files: ['calculator.py']
Patch Diff Path: runs/<job_id>/patch.diff
Repaired Zip Path: runs/<job_id>/repaired_project.zip

测试 LangGraph 运行时:

python scripts/smoke_test_backend.py --runtime langgraph

如果提示缺少 langgraph,请运行:

pip install langgraph

启动中文前端

streamlit run app.py

打开页面后,默认设置为:

Agent 运行时:classic
使用 Mock LLM:开启
启用 Reflection:开启
启用 Planning:开启

上传示例:

benchmark/bug_001_divide_zero.zip

点击:

开始运行 RepoPilot

前端会展示:

  1. 问题诊断与修复说明
    • 哪里出了问题
    • 为什么出问题
    • Agent 如何修改
    • 修复后是否通过测试
  2. 测试结果:修复前
  3. 补丁 Diff
  4. 测试结果:修复后
  5. 修复后结果
    • 修改过的文件
    • 修复后文件预览
    • 下载修复后的项目 zip
    • 下载 patch.diff
  6. Agent 执行轨迹

真实 LLM 模式

OpenAI API:

export OPENAI_API_KEY="your_key"
export OPENAI_MODEL="gpt-4o-mini"
streamlit run app.py

OpenAI-compatible 本地服务:

export OPENAI_API_KEY="dummy"
export OPENAI_BASE_URL="http://localhost:8000/v1"
export OPENAI_MODEL="your-model-name"
streamlit run app.py

然后在侧边栏关闭:

使用 Mock LLM

可选打开:

启用 LLM Reflection
启用 LLM Replanning

运行结果保存在哪里?

每次运行会生成一个 job:

runs/{job_id}/
├── upload/                     # 上传的 zip
├── repo/                       # 修复后的项目
├── before/                     # 修复前快照
├── patch.diff                  # 生成的补丁文件
├── repaired_project.zip        # 修复后的项目压缩包
├── repair_report.md            # 问题诊断与修复说明
├── traces/
│   ├── {job_id}.json
│   └── {job_id}.md
└── reflection_memory.json

前端底部也会显示本地路径。

Benchmark 评估

生成 benchmark:

python scripts/create_benchmark.py
python scripts/check_benchmark.py

运行完整 Agent:

python -m agent.evaluate \
  --benchmark benchmark \
  --output reports/eval_full.json \
  --trace_dir reports/traces/full \
  --max_steps 12

运行 LLM Reflection + LLM Replanning:

python -m agent.evaluate \
  --benchmark benchmark \
  --output reports/eval_llm_reflect_replan.json \
  --trace_dir reports/traces/llm_reflect_replan \
  --max_steps 12 \
  --use_llm_reflection \
  --use_llm_replanning

消融实验

ReAct only:

python -m agent.evaluate \
  --benchmark benchmark \
  --output reports/eval_react_only.json \
  --trace_dir reports/traces/react_only \
  --max_steps 12 \
  --disable_reflection \
  --disable_planning

Full Agent:

python -m agent.evaluate \
  --benchmark benchmark \
  --output reports/eval_full.json \
  --trace_dir reports/traces/full \
  --max_steps 12

汇总结果:

python scripts/summarize_reports.py

输出:

reports/ablation_table.md
reports/ablation_table.csv

安全说明

RepoPilot 会执行上传项目中的 pytest,因此会运行用户代码。当前版本只建议本地运行和可信代码测试。公开部署前应加入:

  • Docker 沙箱
  • CPU / 内存限制
  • 网络隔离
  • 文件系统隔离
  • 更严格的上传大小和文件类型限制
  • 每个 job 独立状态管理

简历描述参考

RepoPilot: LLM-based Test-Driven Code Repair Agent

- Built a deployable code repair assistant that accepts Python project zip files, runs pytest, diagnoses failures, generates minimal patches, and verifies fixes automatically.
- Implemented a ReAct-style agent with structured tool calling, safe patch editing, planning, reflection memory, LLM reflection, LLM replanning, and trajectory logging.
- Developed dual runtimes: a classic hand-written ReAct loop and a LangGraph-based state graph runtime.
- Built a Chinese Streamlit interface for project upload, patch diff visualization, repaired file preview, downloadable repaired project zip, pytest results, and agent trajectory inspection.
- Designed a seeded Python bug benchmark and evaluation scripts for repair success rate, average tool steps, invalid actions, and unsafe edit attempts.

About

LLM-based test-driven code repair agent with ReAct, reflection, replanning, LangGraph runtime, and Streamlit UI.

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

No releases published

Packages

 
 
 

Contributors

Languages