|
| 1 | +# Code Review Agent — 自动代码评审 Agent |
| 2 | + |
| 3 | +基于 tRPC-Agent-Python 框架构建的自动代码评审 Agent,集成 Skills、沙箱执行、数据库存储、Filter 治理和监控审计能力,可对 git diff 进行自动化审查并输出结构化报告。 |
| 4 | + |
| 5 | +## 环境要求 |
| 6 | + |
| 7 | +- Python >= 3.10 |
| 8 | +- 依赖:见 `requirements.txt` 或 `pyproject.toml` |
| 9 | + |
| 10 | +## 安装 |
| 11 | + |
| 12 | +```bash |
| 13 | +# 从项目根目录安装 |
| 14 | +pip install -e . |
| 15 | + |
| 16 | +# 或直接运行(依赖已安装的情况下) |
| 17 | +cd examples/skills_code_review_agent |
| 18 | +``` |
| 19 | + |
| 20 | +## 快速开始 |
| 21 | + |
| 22 | +### 使用测试样本运行(dry-run 模式,无需 API Key) |
| 23 | + |
| 24 | +```bash |
| 25 | +cd examples/skills_code_review_agent |
| 26 | + |
| 27 | +# 运行无问题样本 |
| 28 | +python dry_run.py --fixture 01_clean |
| 29 | + |
| 30 | +# 运行安全漏洞样本 |
| 31 | +python dry_run.py --fixture 02_security_leak |
| 32 | + |
| 33 | +# 运行全部 8 条样本 |
| 34 | +for f in 01 02 03 04 05 06 07 08; do |
| 35 | + python dry_run.py --fixture "${f}_clean" |
| 36 | +done |
| 37 | +``` |
| 38 | + |
| 39 | +### 审查本地 diff 文件 |
| 40 | + |
| 41 | +```bash |
| 42 | +python review_agent.py --diff-file /path/to/changes.diff |
| 43 | +``` |
| 44 | + |
| 45 | +### 审查 git 工作区变更 |
| 46 | + |
| 47 | +```bash |
| 48 | +python review_agent.py --repo-path /path/to/repo |
| 49 | +``` |
| 50 | + |
| 51 | +### 列出可用样本 |
| 52 | + |
| 53 | +```bash |
| 54 | +python review_agent.py --list-fixtures |
| 55 | +``` |
| 56 | + |
| 57 | +## 输出 |
| 58 | + |
| 59 | +- `review_report.json` — 结构化 JSON 报告 |
| 60 | +- `review_report.md` — 可读 Markdown 报告 |
| 61 | + |
| 62 | +## 测试样本 |
| 63 | + |
| 64 | +| 样本 | 场景 | 预期 | |
| 65 | +|------|------|------| |
| 66 | +| `01_clean.py.diff` | 无问题代码 | 空 findings | |
| 67 | +| `02_security_leak.py.diff` | 硬编码密钥/密码 | severity=high | |
| 68 | +| `03_async_resource_leak.py.diff` | 未关闭 aiohttp ClientSession | 资源泄漏 | |
| 69 | +| `04_db_connection_leak.py.diff` | 数据库连接未释放 | 连接泄漏 | |
| 70 | +| `05_test_missing.py.diff` | 新增函数无测试 | warning | |
| 71 | +| `06_duplicate_finding.py.diff` | 同一问题重复出现 | 去重后只报 1 条 | |
| 72 | +| `07_sandbox_failure.py.diff` | 沙箱执行超时/失败 | 任务不崩溃 | |
| 73 | +| `08_secret_masking.py.diff` | 多种敏感信息 | 全部脱敏 | |
| 74 | + |
| 75 | +## 项目结构 |
| 76 | + |
| 77 | +``` |
| 78 | +examples/skills_code_review_agent/ |
| 79 | +├── review_agent.py # 主入口 |
| 80 | +├── dry_run.py # 干跑模式 |
| 81 | +├── config.py # 配置管理 |
| 82 | +├── cli.py # CLI 参数解析 |
| 83 | +├── models.py # 数据模型 |
| 84 | +├── diff_parser.py # diff 解析器 |
| 85 | +├── sandbox.py # 沙箱执行器 |
| 86 | +├── secret_masker.py # 敏感信息脱敏 |
| 87 | +├── filters.py # Filter 治理 |
| 88 | +├── filter_chain.py # Filter 编排链 |
| 89 | +├── deduper.py # 去重降噪 |
| 90 | +├── report_generator.py # 报告生成 |
| 91 | +├── monitor.py # 监控审计 |
| 92 | +├── db/ # 数据库层 |
| 93 | +│ ├── schema.sql |
| 94 | +│ ├── storage.py |
| 95 | +│ └── init_db.py |
| 96 | +├── skills/code-review/ # CR Skill |
| 97 | +│ ├── SKILL.md |
| 98 | +│ ├── rules/ # 规则文档 |
| 99 | +│ └── scripts/ # 沙箱脚本 |
| 100 | +└── fixtures/ # 测试样本 |
| 101 | + ├── 01_clean.py.diff |
| 102 | + └── ... |
| 103 | +``` |
0 commit comments