Skip to content

About

testing-agent

Resources

Stars

0 stars

Watchers

0 watching

Forks

Repository files navigation

SecPriAgent V0.1

全周期安全测试编排器。端侧黑盒隐私 + 云端白盒红线,opencode 主 Agent 驱动 11 个 SKILL 串联执行。

前置依赖

工具 版本 说明
Python 3.10 项目运行时
uv ≥0.5 Python 包管理
Node.js ≥18 前端构建(可选,已有 mock API 时前端非必需)

快速启动

# 1. 克隆项目
cd secpri-agent

# 2. 安装 Python 依赖
uv sync

# 3. 启动后端(默认 http://localhost:8000)
uv run python main.py

启动后访问:

  • 后端 API:http://localhost:8000
  • API 文档:http://localhost:8000/docs(Swagger UI)
  • 健康检查:http://localhost:8000/api/health

启动前端(可选)

cd frontend
npm install
npm run dev          # http://localhost:3000,自动代理 /api 到 8000

如果不想启动前端,可以直接通过 API 或 curl 交互:

# 对话
curl -X POST http://localhost:8000/api/chat/simple \
  -H "Content-Type: application/json" \
  -d '{"message": "你好,介绍一下你的能力"}'

# 启动测试
curl -X POST http://localhost:8000/api/test/start \
  -H "Content-Type: application/json" \
  -d '{"requirement_text": "检查 /admin 接口的 SQL 注入和 XSS 漏洞"}'

项目结构

secpri-agent/
├── AGENT.md                          # Agent 架构文档
├── README.md                         # 本文件
├── SPEC.md                           # 需求规格说明书
├── opencode.json                     # opencode 运行时配置
├── pyproject.toml                    # Python 项目配置 (uv)
│
├── .opencode/                        # ★ opencode Agent 定义
│   └── skills/
│       ├── manifest.json             # SKILL 注册表
│       ├── INDEX.md                  # SKILL 索引
│       ├── full-pipeline.md          # 全周期编排
│       ├── opencode-fusion.md        # ① 模型智能路由
│       ├── opencode-contexty.md      # ② 上下文工程
│       ├── trust-manager.md          # ★ 信任边界管理
│       ├── stage-0-input-parser.md   # 阶段零
│       ├── stage-1-planner.md        # 阶段一
│       ├── stage-2-router.md         # 阶段二
│       ├── stage-3a-local-executor.md # 阶段三-A
│       ├── stage-3b-cloud-executor.md # 阶段三-B
│       ├── stage-4-aggregator.md     # 阶段四
│       └── stage-5-reporter.md       # 阶段五
│
├── domain/                           # 端云共享代码
│   ├── models/                       # Pydantic 数据模型
│   │   ├── common.py                 # 枚举 (TestType, ExecutionMode)
│   │   ├── alarm.py                  # AlarmItem
│   │   ├── result.py                 # TestResult, UnifiedTestResult
│   │   ├── error.py                  # SelfCheckError, SubagentErrorReport
│   │   └── plan.py                   # TestTask, TestPlan
│   └── algorithms/
│       └── fingerprint.py            # 告警指纹算法 (SHA256, 单点定义)
│
├── backend/
│   ├── main.py                       # FastAPI 应用入口
│   ├── config.yaml                   # 路由配置 (redline→cloud, privacy→local)
│   ├── models.py                     # API 请求/响应模型
│   └── core/
│       ├── opencode/                 # ★ opencode 引擎实现
│       │   ├── fusion.py             # 模型智能路由
│       │   ├── contexty.py           # 上下文工程
│       │   └── adapter.py            # 统一适配层
│       ├── llm/
│       │   ├── client.py             # DeepSeek API 客户端
│       │   ├── prompts.py            # 兼容导入层
│       │   └── system_prompts.py     # 完整系统提示词 (10组)
│       ├── input_parser/             # 阶段零:输入预处理
│       ├── planner/                  # 阶段一:计划生成
│       ├── router/                   # 阶段二:路由决策
│       ├── scheduler/                # 阶段三:执行调度
│       └── reporter/                 # 阶段四/五:汇聚+报告
│
├── frontend/                         # React 三栏布局前端
│   └── src/
│       ├── App.jsx                   # 主页面 (左侧会话 + 中间对话 + 右侧报告)
│       └── index.css                 # 全局样式
│
└── tests/                            # 测试 (50 个)
    ├── test_domain_models.py         # 15 测试:数据模型 + 指纹算法
    ├── test_backend_modules.py       # 23 测试:各阶段模块
    └── test_api.py                   # 12 测试:API 端点

API 端点

方法 路径 说明
POST /api/chat 流式对话(SSE)
POST /api/chat/simple 非流式对话
POST /api/test/start 启动测试(Mock 流水线)
GET /api/test/{session_id} 查询会话状态和结果
POST /api/cloud/submit [Mock] 提交云端红线任务
GET /api/cloud/result/{id} [Mock] 轮询云端任务结果
POST /api/alarms/batch [Mock] 批量同步告警
GET /api/opencode/stats opencode 运行统计
GET /api/health 健康检查

开发与调试

运行测试

# 全部测试
uv run pytest tests/ -v

# 只看 API 测试
uv run pytest tests/test_api.py -v

# 只看模型测试
uv run pytest tests/test_domain_models.py -v

# 只看模块测试
uv run pytest tests/test_backend_modules.py -v

# 快速运行(无详细输出)
uv run pytest tests/ -q

查看 Agent 状态

# opencode 路由 + 上下文统计
curl http://localhost:8000/api/opencode/stats

调试 LLM 调用

日志级别默认 INFO。查看详细的 LLM 调用日志:

# 临时开启 DEBUG 日志
uv run python -c "
import logging
logging.basicConfig(level=logging.DEBUG)
from backend.main import main
main()
"

验证 DeepSeek 连接

uv run python -c "
from backend.core.llm.client import get_client
c = get_client()
r = c.chat('hello')
print(r[:100])
"

前端调试

cd frontend
npm run dev
# 浏览器打开 http://localhost:3000
# F12 → Network 查看 SSE 流

配置文件

文件 作用
backend/config.yaml 路由表 + LLM 配置(API Key, base_url, model)
opencode.json opencode 运行时配置(fusion 路由表 + contexty 预算 + workspace 治理)
pyproject.toml Python 依赖 + pytest 配置

Mock 模式说明

当前为开发阶段 Mock 模式,不会真实执行安全扫描:

模块 Mock 行为
stage-3a (隐私) 返回 2 个预设告警(PII+日志泄露)
stage-3b (红线) 返回 3 个预设告警(SQLi+Auth bypass+XSS)
stage-5 (告警同步) 直接 accept,不发送 HTTP 请求

Mock 数据定义在 backend/core/reporter/mock_results.py。

技术栈

层 技术 说明
Agent 引擎 opencode (fusion + contexty) 自建,Python 实现
LLM DeepSeek V4-Flash OpenAI 兼容 API
后端 FastAPI + Uvicorn Python 3.10, uv 管理
前端 React 18 + Vite 三栏布局
测试 pytest + pytest-asyncio 50 个用例
模型 Pydantic V2 domain/ 端云统一

架构图

┌──────────────────────────────────────────────────────────────┐
│                       用户界面                               │
│  ┌─────────────┐  ┌──────────────────┐  ┌────────────────┐  │
│  │ 会话管理     │  │ 对话 + 代码输入   │  │ 报告摘要        │  │
│  │ (左侧280px) │  │ (中间弹性)       │  │ (右侧320px)    │  │
│  └─────────────┘  └──────────────────┘  └────────────────┘  │
└──────────────────────────┬───────────────────────────────────┘
                           │ HTTP + SSE
┌──────────────────────────▼───────────────────────────────────┐
│                   FastAPI (localhost:8000)                    │
│  ┌─────────────┐  ┌──────────────┐  ┌──────────────────┐    │
│  │ /api/chat   │  │ /api/test/*  │  │ /api/cloud/*     │    │
│  │ (SSE流式)   │  │ (流水线)     │  │ (Mock云端)       │    │
│  └─────────────┘  └──────────────┘  └──────────────────┘    │
└──────────────────────────┬───────────────────────────────────┘
                           │
┌──────────────────────────▼───────────────────────────────────┐
│                    opencode 引擎                             │
│  ┌───────────────┐  ┌───────────────┐  ┌────────────────┐   │
│  │ fusion.py     │  │ contexty.py   │  │ adapter.py     │   │
│  │ 模型路由       │  │ 上下文工程    │  │ 统一适配       │   │
│  └───────────────┘  └───────────────┘  └────────────────┘   │
└──────────────────────────┬───────────────────────────────────┘
                           │
┌──────────────────────────▼───────────────────────────────────┐
│                   DeepSeek API                               │
│              https://api.deepseek.com                        │
│              model: deepseek-v4-flash                        │
└──────────────────────────────────────────────────────────────┘

About

testing-agent

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages