🔒 Security: - 修复 API Key 泄露,移除 platform_db.json 明文存储 - 限制 CORS 允许来源,不再全开放 - 添加路径校验防止目录穿越攻击 - Git 命令使用列表传参防止注入 - os.killpg 添加异常处理 🐛 Bug Fixes: - 生产环境禁用 Flask debug 模式 - 修复 SSE 连接泄漏(useTaskLogs + ReposView) - 修复 fetch 静默吞错误,添加 console.error 日志 - 补全 _run_ui_fix 函数签名 - 修复 .coveragerc source 配置 - _load_db 添加文件锁和 Queue maxsize - SSE log_queues 使用 WeakValueDictionary 🔧 Refactor: - BaseAPI → APIClient 统一命名 - 删除后端死代码和重复 import - 删除 business_backup 备份代码 - 删除 conftest_metrics 死代码 📝 Docs: - 新增 CHANGELOG.md 和 CHANGELIST.md - 更新 QUICKSTART.md 添加安全注意事项 - 更新 CONFIG.md 添加安全加固和性能优化
243 lines
5.7 KiB
Markdown
243 lines
5.7 KiB
Markdown
# TestAgent 配置文档
|
||
|
||
## 一、config.yaml
|
||
|
||
```yaml
|
||
agent:
|
||
name: "TestAgent"
|
||
model: "glm-5.2" # LLM 模型名称
|
||
provider: "openai" # OpenAI 兼容
|
||
base_url: "https://ai-api.d-robotics.cc/v1"
|
||
temperature: 0.3
|
||
max_tokens: 16384
|
||
|
||
git:
|
||
default_branch: "main"
|
||
clone_depth: 50 # git clone 深度
|
||
max_file_size_kb: 500
|
||
|
||
analysis:
|
||
supported_languages: # 支持的语言
|
||
- python
|
||
- typescript
|
||
- javascript
|
||
- go
|
||
- java
|
||
ignore_patterns: # 忽略的文件/目录
|
||
- "**/node_modules/**"
|
||
- "**/__pycache__/**"
|
||
- "**/.git/**"
|
||
- "**/dist/**"
|
||
- "**/build/**"
|
||
- "**/vendor/**"
|
||
- "**/*.min.js"
|
||
- "**/*.bundle.js"
|
||
|
||
test_generation:
|
||
output_dir: "tests" # 测试输出目录
|
||
domain_layer_dir: "tests/domain"
|
||
infrastructure_layer_dir: "tests/infrastructure"
|
||
session_domain_dir: "tests/session"
|
||
smoke_marker: "smoke"
|
||
integration_marker: "integration"
|
||
regression_marker: "regression"
|
||
e2e_marker: "e2e"
|
||
metrics_gate: # 指标门禁
|
||
min_coverage: 60 # 覆盖率 ≥ 60%
|
||
min_pass_rate: 95 # 通过率 ≥ 95%
|
||
|
||
backend:
|
||
host: "0.0.0.0"
|
||
port: 5002
|
||
existing_tests_root: "./tests"
|
||
|
||
plugins:
|
||
enabled:
|
||
- pylint_analyzer # 代码静态分析
|
||
- test_runner # 测试运行 + 门禁
|
||
|
||
logging:
|
||
level: "info"
|
||
format: "%(asctime)s [%(levelname)s] %(name)s: %(message)s"
|
||
```
|
||
|
||
---
|
||
|
||
## 二、.env 环境变量
|
||
|
||
```bash
|
||
# LLM API 配置(固定,页面无需输入)
|
||
OPENAI_API_KEY=sk-your-api-key-here
|
||
OPENAI_BASE_URL=https://ai-api.d-robotics.cc/v1
|
||
|
||
# 测试平台登录凭证
|
||
TEST_USERNAME=admin&tsting # 地瓜机器人平台账号
|
||
TEST_PASSWORD=12345678 # 对应密码
|
||
```
|
||
|
||
**可选环境变量:**
|
||
|
||
| 变量 | 说明 | 默认值 |
|
||
|------|------|--------|
|
||
| `DEEPSEEK_API_KEY` | DeepSeek API Key(兼容模式) | - |
|
||
| `ANTHROPIC_API_KEY` | Anthropic API Key | - |
|
||
| `HEADLESS` | 浏览器无头模式 | `false` |
|
||
|
||
---
|
||
|
||
## 三、Skills 知识库配置
|
||
|
||
知识库位于 `skills/` 目录,每个 Skill 是一个子目录,包含 `SKILL.md` 文件。
|
||
|
||
### 3.1 TDD 方法论 (`skills/tdd-methodology/SKILL.md`)
|
||
- 红-绿-重构循环
|
||
- FIRST 原则(Fast/Independent/Repeatable/Self-Validating/Timely)
|
||
- AAA 模式(Arrange-Act-Assert)
|
||
- 测试命名规范
|
||
- 测试替身(Stub/Mock/Fake/Spy/Dummy)
|
||
|
||
### 3.2 DDD 模式 (`skills/ddd-patterns/SKILL.md`)
|
||
- 限界上下文划分
|
||
- 聚合根/实体/值对象/领域服务/仓储
|
||
- 测试目录结构规范
|
||
- 领域层 vs 基础设施层隔离
|
||
- Session 领域测试
|
||
|
||
### 3.3 测试框架规范 (`skills/test-framework/SKILL.md`)
|
||
- 测试分层(冒烟/集成/系统/回归/指标)
|
||
- 测试标记规范
|
||
- 指标门禁断言
|
||
- CI/CD 流水线链
|
||
- 敏捷迭代规范
|
||
|
||
### 3.4 多语言模式 (`skills/language-patterns/SKILL.md`)
|
||
- Python (pytest + Mock)
|
||
- TypeScript (Jest)
|
||
- Go (testing)
|
||
- 测试数据管理
|
||
|
||
---
|
||
|
||
## 四、插件配置
|
||
|
||
### 4.1 pylint_analyzer
|
||
- 触发时机:`pre_analysis`
|
||
- 功能:行长度检测、TODO/FIXME 标记、print 语句检测、复杂度评估
|
||
|
||
### 4.2 test_runner
|
||
- 触发时机:`post_generation`
|
||
- 功能:运行生成的测试、解析结果、指标门禁检查
|
||
|
||
---
|
||
|
||
## 五、前端配置
|
||
|
||
### 5.1 vite.config.ts
|
||
```typescript
|
||
export default defineConfig({
|
||
plugins: [react(), tailwindcss()],
|
||
server: {
|
||
host: '0.0.0.0',
|
||
port: 5174,
|
||
proxy: {
|
||
'/api': 'http://127.0.0.1:5002',
|
||
'/artifacts': 'http://127.0.0.1:5002',
|
||
},
|
||
},
|
||
})
|
||
```
|
||
|
||
### 5.2 主题配置
|
||
- 暗色/亮色模式切换
|
||
- CSS 自定义属性 14 个主题变量
|
||
- 存储在 `localStorage`,默认跟随系统偏好
|
||
|
||
---
|
||
|
||
## 六、数据清理策略
|
||
|
||
- 任务超过 100 个时自动清除最旧 50 个
|
||
- 清理时同步删除关联的截图和报告文件
|
||
- `platform_db.json` 每次写入时自动清理
|
||
|
||
---
|
||
|
||
## 七、安全加固配置
|
||
|
||
### 7.1 CORS 配置
|
||
|
||
生产环境应限制允许的来源,不要开放 `origins='*'`:
|
||
|
||
```python
|
||
# backend/app.py
|
||
from flask_cors import CORS
|
||
|
||
# 生产环境配置
|
||
CORS(app, origins=["https://your-domain.com"])
|
||
|
||
# 开发环境配置(仅允许本地)
|
||
CORS(app, origins=["http://localhost:5174", "http://127.0.0.1:5174"])
|
||
```
|
||
|
||
### 7.2 路径安全
|
||
|
||
所有涉及文件路径的接口都应进行路径校验:
|
||
|
||
```python
|
||
import os
|
||
|
||
def safe_path(base_dir: str, filename: str) -> str:
|
||
"""防止路径穿越攻击"""
|
||
full_path = os.path.join(base_dir, filename)
|
||
real_path = os.path.realpath(full_path)
|
||
if not real_path.startswith(os.path.realpath(base_dir)):
|
||
raise ValueError("Invalid path: directory traversal detected")
|
||
return real_path
|
||
```
|
||
|
||
### 7.3 Git 命令安全
|
||
|
||
使用列表传参而非字符串拼接,防止命令注入:
|
||
|
||
```python
|
||
import subprocess
|
||
|
||
# ✅ 安全:使用列表传参
|
||
subprocess.run(["git", "clone", "--depth", "1", repo_url, target_dir], check=True)
|
||
|
||
# ❌ 危险:字符串拼接
|
||
# os.system(f"git clone {repo_url} {target_dir}")
|
||
```
|
||
|
||
### 7.4 SSE 连接管理
|
||
|
||
前端 SSE 连接应在组件卸载时正确清理:
|
||
|
||
```typescript
|
||
// 使用 useRef 跟踪 EventSource
|
||
const esRef = useRef<EventSource | null>(null);
|
||
|
||
useEffect(() => {
|
||
return () => {
|
||
// 组件卸载时关闭连接
|
||
if (esRef.current) {
|
||
esRef.current.close();
|
||
esRef.current = null;
|
||
}
|
||
};
|
||
}, []);
|
||
```
|
||
|
||
---
|
||
|
||
## 八、性能优化建议
|
||
|
||
### 8.1 前端
|
||
- 使用 `React.memo` 优化列表渲染
|
||
- SSE 日志限制保留 200 条,避免内存无限增长
|
||
- 图片懒加载
|
||
|
||
### 8.2 后端
|
||
- 数据库操作使用文件锁,避免并发写入冲突
|
||
- Queue 设置 maxsize,防止内存泄漏
|
||
- 定期清理过期任务和日志文件 |