fix: 修复35个安全与功能缺陷,补全知识进化/数字孪生/行为采集模块

## 安全修复 (12项)
- Webhook接口添加全局Token认证,过滤敏感请求头
- 修复JWT Base64 padding公式,防止签名验证绕过
- 数据库密码/飞书Token从源码移除,改为环境变量
- 工作流引擎添加路径遍历防护 (_resolve_safe_path)
- eval()添加模板长度上限检查
- 审批API添加认证依赖
- 前端v-html增强XSS转义,console.log仅开发模式输出
- 500错误不再暴露内部异常详情

## Agent运行时修复 (7项)
- 删除_inject_knowledge_context中未定义db变量的finally块
- 工具执行添加try/except保护,异常不崩溃Agent
- LLM重试计入budget计数器
- self_review异常时passed=False
- max_iterations截断标记success=False
- 工具参数JSON解析失败时记录警告日志
- run()开始时重置_llm_invocations计数器

## 配置与基础设施
- DEBUG默认False,SQL_ECHO独立配置项
- init_db()补全13个缺失模型导入
- 新增WEBHOOK_AUTH_TOKEN/SQL_ECHO配置项
- 新增.env.example模板文件

## 前端修复 (12项)
- 登录改用URLSearchParams替代FormData
- 401拦截器通过Pinia store统一清理状态
- SSE流超时从60s延长至300s
- final/error事件时清除streamTimeout
- localStorage聊天记录添加24h TTL
- safeParseArgCount替代模板中裸JSON.parse
- fetchUser 401时同时清除user对象

## 新增模块
- 知识进化: knowledge_extractor/retriever/tasks
- 数字孪生: shadow_executor/comparison模型
- 行为采集: behavior_middleware/collector/fingerprint_engine
- 代码审查: code_review_agent/document_review_agent
- 反馈学习: feedback_learner
- 瓶颈检测/优化引擎/成本估算/需求估算
- 速率限制器 (rate_limiter)
- Alembic迁移 015-020

## 文档
- 商业化落地计划
- 8篇docs文档 (架构/API/部署/开发/贡献等)
- Docker Compose生产配置

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
renjianbo
2026-05-10 19:50:20 +08:00
parent f79dc0b3c6
commit ab1589921a
77 changed files with 9442 additions and 265 deletions

View File

@@ -9,12 +9,13 @@
- 推荐端口:
- 前端:`3001`
- 后端 API`8037`
- 后端 API`8037`(若被占用,一键启动会自动改用备用 **`8041`**
- Redis`6379`
- **一键脚本目录**:均在仓库内 `scripts\startup\`,在根目录 `D:\aaa\aiagent` 下执行时路径为 `.\scripts\startup\*.ps1`(勿写成根目录下的 `.\start_aiagent.ps1`)。
- `backend/.env` 必须与实际 Redis 端口一致:
- 推荐:`REDIS_URL=redis://localhost:6379/0`
- 任何 `.env` / 依赖 / 工具代码变更后,至少重启:
- API + Celery`restart_backend_celery.ps1`
- API + Celery`scripts\startup\restart_backend_celery.ps1`
- 若出现 `timeout of 30000ms exceeded`,优先检查:
1) Redis 是否可连
2) Celery Worker 是否在跑
@@ -33,47 +34,73 @@ cd D:\aaa\aiagent
### 1.1 一键启动(全套)
```powershell
powershell -ExecutionPolicy Bypass -File .\start_aiagent.ps1
powershell -ExecutionPolicy Bypass -File .\scripts\startup\start_aiagent.ps1
```
默认目标:
- 前端:`http://localhost:3001`
- 后端文档:`http://127.0.0.1:8037/docs`
- 后端文档:优先 `http://127.0.0.1:8037/docs`;若脚本提示已切到 **8041**,则打开 `http://127.0.0.1:8041/docs`
- Redis`127.0.0.1:6379`
一键启动会为每个进程新开 PowerShell 窗口(含 **Celery Worker****Celery Beat**)。若控制台一闪退出,请到该窗口里看报错。
### 1.2 一键停止(全套)
```powershell
powershell -ExecutionPolicy Bypass -File .\stop_aiagent.ps1
powershell -ExecutionPolicy Bypass -File .\scripts\startup\stop_aiagent.ps1
```
### 1.3 仅重启后端 + Celery最常用
```powershell
powershell -ExecutionPolicy Bypass -File .\restart_backend_celery.ps1
powershell -ExecutionPolicy Bypass -File .\scripts\startup\restart_backend_celery.ps1
```
适用场景:改了 `.env`、Python 依赖、内置工具实现、Agent 执行逻辑。
说明:该脚本**固定**把 API 起在 **8037**。若本机 8037 被其它程序或异常状态占用,请先 `stop` 全套或释放端口后再执行。
### 1.4 仅重启前端(不动 Redis / 后端)
适用:只改了前端代码或 Vite 配置,需要刷新 dev 进程。
1. 结束占用 **3001** 的进程(任务管理器结束对应 `node`/`pnpm`,或用资源监视器按端口查 PID 后结束)。
2. 确认当前 API 端口(一般为 **8037**;若全套启动时曾提示改用 **8041**,则与之一致)。
3. 新开 PowerShell
```powershell
cd D:\aaa\aiagent\frontend
$env:AIAGENT_API_PROXY='http://127.0.0.1:8037' # 若 API 在 8041改为 ...8041
pnpm dev --port 3001
```
浏览器请优先用 **`http://localhost:3001`** 访问Vite 常只绑 `[::1]`,用 `127.0.0.1:3001` 可能连不上,属正常现象)。
---
## 2. 标准“重启服务器”流程(推荐照抄)
```powershell
cd D:\aaa\aiagent
powershell -ExecutionPolicy Bypass -File .\stop_aiagent.ps1
powershell -ExecutionPolicy Bypass -File .\start_aiagent.ps1
powershell -ExecutionPolicy Bypass -File .\scripts\startup\stop_aiagent.ps1
powershell -ExecutionPolicy Bypass -File .\scripts\startup\start_aiagent.ps1
```
完成后立刻验证:
`start` 输出中出现 **`Port 8037 is occupied, switching to 8041`**,则本次 API 在 **8041**,下文健康检查与文档地址请改用 **8041**(前端新窗口里的 `AIAGENT_API_PROXY` 已指向该端口)。
完成后立刻验证PowerShell 中 `curl` 多为 `Invoke-WebRequest` 别名,建议用 **`curl.exe`**
```powershell
netstat -ano | findstr :6379
netstat -ano | findstr :8037
netstat -ano | findstr :8041
netstat -ano | findstr :3001
curl http://127.0.0.1:8037/health
curl.exe -s -o NUL -w "%{http_code}" http://127.0.0.1:8037/health
curl.exe -s -o NUL -w "%{http_code}" http://127.0.0.1:8041/health
```
若 API 实际在 **8041**,刚启动后 **1020 秒内** `/health` 可能仍超时,属 uvicorn 拉起过程;前端 `3001` 可先就绪。
---
## 3. 本次故障复盘(学生作业管理助手超时)
@@ -96,7 +123,7 @@ curl http://127.0.0.1:8037/health
1.`backend/.env` 改为:
- `REDIS_URL=redis://localhost:6379/0`
2. 执行:
- `powershell -ExecutionPolicy Bypass -File .\restart_backend_celery.ps1`
- `powershell -ExecutionPolicy Bypass -File .\scripts\startup\restart_backend_celery.ps1`
3. 验证:
- `6379/8037/3001` 监听正常
- `/health` 返回 `200`
@@ -127,7 +154,7 @@ Set-ExecutionPolicy -Scope Process Bypass
### 4.3 一键脚本不可用时的手动拉起(应急)
3~4 个终端
**5** 个终端(与一键启动对齐;若暂不需要定时任务可省略 Beat
1) Redis
```powershell
@@ -156,6 +183,28 @@ $env:AIAGENT_API_PROXY='http://127.0.0.1:8037'
pnpm dev --port 3001
```
5) Celery Beat与一键 `start_aiagent.ps1` 对齐;定时任务依赖此项)
```powershell
cd D:\aaa\aiagent\backend
.\venv\Scripts\Activate.ps1
python -m celery -A app.core.celery_app beat --loglevel=info
```
### 4.4 8037「占用」与 netstat 幽灵 PID实践经验
部分 Windows 环境会出现:
- `start_aiagent.ps1` 提示 **8037 被占用**,自动改用 **8041**
- `netstat` 仍显示 `0.0.0.0:8037 LISTENING` 且带某一 **PID**,但 **`tasklist` 查不到该 PID**(或无法 `Stop-Process`)。
此时以 **`start` 脚本打印的实际端口为准**(多为 **8041**),用 **`http://127.0.0.1:8041/docs`** 与对应 `/health` 验证即可。若必须清理 **8037**,可本机执行 `netsh interface ipv4 show excludedportrange protocol=tcp` 查看是否与 Hyper-V / 动态端口保留冲突,或重启 Windows 后再全套启动。
### 4.5 `stop` 已正常但 `start` 报「8037 与 8041 均占用」
曾出现:`stop` 对后端端口打印 **SKIP**,端口检查却仍显示 **LISTEN**,随后 `start` 抛出双端口占用。根因是旧版 `stop_aiagent.ps1`**`ForEach-Object` 内误用 `return`**(会从整个函数提前返回)以及内层 **`$pid` 变量名与 PowerShell 只读自动变量冲突**,导致未真正枚举监听 PID。
**当前仓库内 `scripts\startup\stop_aiagent.ps1` 已按上述问题修复**;若你本地脚本被回退或拷贝了旧版,请与仓库版本对齐后再执行全套重启。
---
## 5. OCR上传图片识别必查项
@@ -180,10 +229,11 @@ cd D:\aaa\aiagent\backend
## 6. 访问地址
- 前端:`http://localhost:3001`
- 后端 API`http://127.0.0.1:8037`
- 后端文档:`http://127.0.0.1:8037/docs`
- 健康检查:`http://127.0.0.1:8037/health`
- 前端:`http://localhost:3001`(推荐用 **localhost**,避免与 `[::1]` 绑定不一致)
- 后端 API(默认)`http://127.0.0.1:8037`
- 后端文档(默认)`http://127.0.0.1:8037/docs`
- 健康检查(默认)`http://127.0.0.1:8037/health`
- 若一键启动提示改用 **8041**:将上述三项中的端口改为 **8041** 即可。
---