-
-
Notifications
You must be signed in to change notification settings - Fork 845
Expand file tree
/
Copy pathagent_task.py
More file actions
584 lines (474 loc) · 20 KB
/
Copy pathagent_task.py
File metadata and controls
584 lines (474 loc) · 20 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
"""
Agent 审计任务模型
支持 AI Agent 自主漏洞挖掘和验证
"""
import uuid
from datetime import datetime
from typing import Optional, List, TYPE_CHECKING
from sqlalchemy import (
Column, String, Integer, Float, Text, Boolean,
DateTime, ForeignKey, Enum as SQLEnum, JSON
)
from sqlalchemy.orm import relationship
from sqlalchemy.sql import func
from app.db.base import Base
if TYPE_CHECKING:
from .project import Project
class AgentTaskStatus:
"""Agent 任务状态"""
PENDING = "pending" # 等待执行
INITIALIZING = "initializing" # 初始化中
RUNNING = "running" # 运行中
PLANNING = "planning" # 规划阶段
INDEXING = "indexing" # 索引阶段
ANALYZING = "analyzing" # 分析阶段
VERIFYING = "verifying" # 验证阶段
REPORTING = "reporting" # 报告生成
COMPLETED = "completed" # 已完成
FAILED = "failed" # 失败
CANCELLED = "cancelled" # 已取消
PAUSED = "paused" # 已暂停
class AgentTaskPhase:
"""Agent 执行阶段"""
PLANNING = "planning"
INDEXING = "indexing"
RECONNAISSANCE = "reconnaissance"
ANALYSIS = "analysis"
VERIFICATION = "verification"
REPORTING = "reporting"
class AgentTask(Base):
"""Agent 审计任务"""
__tablename__ = "agent_tasks"
id = Column(String(36), primary_key=True, default=lambda: str(uuid.uuid4()))
project_id = Column(String(36), ForeignKey("projects.id", ondelete="CASCADE"), nullable=False)
# 任务基本信息
name = Column(String(255), nullable=True)
description = Column(Text, nullable=True)
task_type = Column(String(50), default="agent_audit")
# 任务配置
audit_scope = Column(JSON, nullable=True) # 审计范围配置
target_vulnerabilities = Column(JSON, nullable=True) # 目标漏洞类型
verification_level = Column(String(50), default="sandbox") # analysis_only, sandbox, generate_poc
# 分支信息(仓库项目)
branch_name = Column(String(255), nullable=True)
# 排除模式
exclude_patterns = Column(JSON, nullable=True)
# 文件范围
target_files = Column(JSON, nullable=True) # 指定扫描的文件列表
# LLM 配置
llm_config = Column(JSON, nullable=True) # LLM 配置
# Agent 配置
agent_config = Column(JSON, nullable=True) # Agent 特定配置
max_iterations = Column(Integer, default=50) # 最大迭代次数
token_budget = Column(Integer, default=100000) # Token 预算
timeout_seconds = Column(Integer, default=1800) # 超时时间(秒)
# 状态
status = Column(String(20), default=AgentTaskStatus.PENDING)
current_phase = Column(String(50), nullable=True)
current_step = Column(String(255), nullable=True) # 当前执行步骤描述
error_message = Column(Text, nullable=True)
# 进度统计
total_files = Column(Integer, default=0)
indexed_files = Column(Integer, default=0)
analyzed_files = Column(Integer, default=0) # 实际扫描过的文件数
files_with_findings = Column(Integer, default=0) # 有漏洞发现的文件数
total_chunks = Column(Integer, default=0) # 代码块总数
# Agent 统计
total_iterations = Column(Integer, default=0) # Agent 迭代次数
tool_calls_count = Column(Integer, default=0) # 工具调用次数
tokens_used = Column(Integer, default=0) # 已使用 Token 数
# 发现统计
findings_count = Column(Integer, default=0) # 发现总数
verified_count = Column(Integer, default=0) # 已验证数
false_positive_count = Column(Integer, default=0) # 误报数
# 严重程度统计
critical_count = Column(Integer, default=0)
high_count = Column(Integer, default=0)
medium_count = Column(Integer, default=0)
low_count = Column(Integer, default=0)
# 质量评分
quality_score = Column(Float, default=0.0)
security_score = Column(Float, default=0.0)
# 审计计划
audit_plan = Column(JSON, nullable=True) # Agent 生成的审计计划
# 时间戳
created_at = Column(DateTime(timezone=True), server_default=func.now())
updated_at = Column(DateTime(timezone=True), onupdate=func.now())
started_at = Column(DateTime(timezone=True), nullable=True)
completed_at = Column(DateTime(timezone=True), nullable=True)
# 创建者
created_by = Column(String(36), ForeignKey("users.id"), nullable=False)
# 关联关系
project = relationship("Project", back_populates="agent_tasks")
events = relationship("AgentEvent", back_populates="task", cascade="all, delete-orphan", order_by="AgentEvent.created_at")
findings = relationship("AgentFinding", back_populates="task", cascade="all, delete-orphan")
def __repr__(self):
return f"<AgentTask {self.id} - {self.status}>"
@property
def progress_percentage(self) -> float:
"""计算进度百分比"""
if self.status == AgentTaskStatus.COMPLETED:
return 100.0
if self.status in [AgentTaskStatus.FAILED, AgentTaskStatus.CANCELLED]:
return 0.0
phase_weights = {
AgentTaskPhase.PLANNING: 5,
AgentTaskPhase.INDEXING: 15,
AgentTaskPhase.RECONNAISSANCE: 10,
AgentTaskPhase.ANALYSIS: 50,
AgentTaskPhase.VERIFICATION: 15,
AgentTaskPhase.REPORTING: 5,
}
completed_weight = 0
current_found = False
for phase, weight in phase_weights.items():
if phase == self.current_phase:
current_found = True
# 估算当前阶段进度
if phase == AgentTaskPhase.INDEXING and self.total_files > 0:
completed_weight += weight * (self.indexed_files / self.total_files)
elif phase == AgentTaskPhase.ANALYSIS and self.total_files > 0:
completed_weight += weight * (self.analyzed_files / self.total_files)
else:
completed_weight += weight * 0.5
break
elif not current_found:
completed_weight += weight
return min(completed_weight, 99.0)
class AgentEventType:
"""Agent 事件类型"""
# 系统事件
TASK_START = "task_start"
TASK_COMPLETE = "task_complete"
TASK_ERROR = "task_error"
TASK_CANCEL = "task_cancel"
# 阶段事件
PHASE_START = "phase_start"
PHASE_COMPLETE = "phase_complete"
# Agent 思考
THINKING = "thinking"
PLANNING = "planning"
DECISION = "decision"
# 工具调用
TOOL_CALL = "tool_call"
TOOL_RESULT = "tool_result"
TOOL_ERROR = "tool_error"
# RAG 相关
RAG_QUERY = "rag_query"
RAG_RESULT = "rag_result"
# 发现相关
FINDING_NEW = "finding_new"
FINDING_UPDATE = "finding_update"
FINDING_VERIFIED = "finding_verified"
FINDING_FALSE_POSITIVE = "finding_false_positive"
# 沙箱相关
SANDBOX_START = "sandbox_start"
SANDBOX_EXEC = "sandbox_exec"
SANDBOX_RESULT = "sandbox_result"
SANDBOX_ERROR = "sandbox_error"
# 进度
PROGRESS = "progress"
# 日志
INFO = "info"
WARNING = "warning"
ERROR = "error"
DEBUG = "debug"
class AgentEvent(Base):
"""Agent 执行事件(用于实时日志和回放)"""
__tablename__ = "agent_events"
id = Column(String(36), primary_key=True, default=lambda: str(uuid.uuid4()))
task_id = Column(String(36), ForeignKey("agent_tasks.id", ondelete="CASCADE"), nullable=False, index=True)
# 事件信息
event_type = Column(String(50), nullable=False, index=True)
phase = Column(String(50), nullable=True)
# 事件内容
message = Column(Text, nullable=True)
# 工具调用相关
tool_name = Column(String(100), nullable=True)
tool_input = Column(JSON, nullable=True)
tool_output = Column(JSON, nullable=True)
tool_duration_ms = Column(Integer, nullable=True) # 工具执行时长(毫秒)
# 关联的发现
finding_id = Column(String(36), nullable=True)
# Token 消耗
tokens_used = Column(Integer, default=0)
# 元数据
event_metadata = Column(JSON, nullable=True)
# 序号(用于排序)
sequence = Column(Integer, default=0, index=True)
# 时间戳
created_at = Column(DateTime(timezone=True), server_default=func.now(), index=True)
# 关联关系
task = relationship("AgentTask", back_populates="events")
def __repr__(self):
return f"<AgentEvent {self.event_type} - {self.message[:50] if self.message else ''}>"
def to_sse_dict(self) -> dict:
"""转换为 SSE 事件格式"""
return {
"id": self.id,
"type": self.event_type,
"phase": self.phase,
"message": self.message,
"tool_name": self.tool_name,
"tool_input": self.tool_input,
"tool_output": self.tool_output,
"tool_duration_ms": self.tool_duration_ms,
"finding_id": self.finding_id,
"tokens_used": self.tokens_used,
"metadata": self.event_metadata,
"sequence": self.sequence,
"timestamp": self.created_at.isoformat() if self.created_at else None,
}
class VulnerabilitySeverity:
"""漏洞严重程度"""
CRITICAL = "critical"
HIGH = "high"
MEDIUM = "medium"
LOW = "low"
INFO = "info"
class VulnerabilityType:
"""漏洞类型"""
SQL_INJECTION = "sql_injection"
NOSQL_INJECTION = "nosql_injection"
XSS = "xss"
COMMAND_INJECTION = "command_injection"
CODE_INJECTION = "code_injection"
PATH_TRAVERSAL = "path_traversal"
FILE_INCLUSION = "file_inclusion"
SSRF = "ssrf"
XXE = "xxe"
DESERIALIZATION = "deserialization"
AUTH_BYPASS = "auth_bypass"
IDOR = "idor"
SENSITIVE_DATA_EXPOSURE = "sensitive_data_exposure"
HARDCODED_SECRET = "hardcoded_secret"
WEAK_CRYPTO = "weak_crypto"
RACE_CONDITION = "race_condition"
BUSINESS_LOGIC = "business_logic"
MEMORY_CORRUPTION = "memory_corruption"
OTHER = "other"
class FindingStatus:
"""发现状态"""
NEW = "new" # 新发现
ANALYZING = "analyzing" # 分析中
VERIFIED = "verified" # 已验证
FALSE_POSITIVE = "false_positive" # 误报
NEEDS_REVIEW = "needs_review" # 需要人工审核
FIXED = "fixed" # 已修复
WONT_FIX = "wont_fix" # 不修复
DUPLICATE = "duplicate" # 重复
class AgentFinding(Base):
"""Agent 发现的漏洞"""
__tablename__ = "agent_findings"
id = Column(String(36), primary_key=True, default=lambda: str(uuid.uuid4()))
task_id = Column(String(36), ForeignKey("agent_tasks.id", ondelete="CASCADE"), nullable=False, index=True)
# 漏洞基本信息
vulnerability_type = Column(String(100), nullable=False, index=True)
severity = Column(String(20), nullable=False, index=True)
title = Column(String(500), nullable=False)
description = Column(Text, nullable=True)
# 位置信息
file_path = Column(String(500), nullable=True, index=True)
line_start = Column(Integer, nullable=True)
line_end = Column(Integer, nullable=True)
column_start = Column(Integer, nullable=True)
column_end = Column(Integer, nullable=True)
function_name = Column(String(255), nullable=True)
class_name = Column(String(255), nullable=True)
# 代码片段
code_snippet = Column(Text, nullable=True)
code_context = Column(Text, nullable=True) # 更多上下文
# 数据流信息
source = Column(Text, nullable=True) # 污点源
sink = Column(Text, nullable=True) # 危险函数
dataflow_path = Column(JSON, nullable=True) # 数据流路径
# 验证信息
status = Column(String(30), default=FindingStatus.NEW, index=True)
verdict = Column(String(30), nullable=True, index=True) # confirmed / likely / uncertain / false_positive
is_verified = Column(Boolean, default=False)
verification_method = Column(Text, nullable=True)
verification_result = Column(JSON, nullable=True)
verified_at = Column(DateTime(timezone=True), nullable=True)
# PoC
has_poc = Column(Boolean, default=False)
poc_code = Column(Text, nullable=True)
poc_description = Column(Text, nullable=True)
poc_steps = Column(JSON, nullable=True) # 复现步骤
# 修复建议
suggestion = Column(Text, nullable=True)
fix_code = Column(Text, nullable=True)
fix_description = Column(Text, nullable=True)
references = Column(JSON, nullable=True) # 参考链接 CWE, OWASP 等
# AI 解释
ai_explanation = Column(Text, nullable=True)
ai_confidence = Column(Float, nullable=True) # AI 置信度 0-1
# XAI (可解释AI)
xai_what = Column(Text, nullable=True)
xai_why = Column(Text, nullable=True)
xai_how = Column(Text, nullable=True)
xai_impact = Column(Text, nullable=True)
# 关联规则
matched_rule_code = Column(String(100), nullable=True)
matched_pattern = Column(Text, nullable=True)
# CVSS 评分(可选)
cvss_score = Column(Float, nullable=True)
cvss_vector = Column(String(100), nullable=True)
# 元数据
finding_metadata = Column(JSON, nullable=True)
tags = Column(JSON, nullable=True)
# 去重标识
fingerprint = Column(String(64), nullable=True, index=True) # 用于去重的指纹
# 时间戳
created_at = Column(DateTime(timezone=True), server_default=func.now())
updated_at = Column(DateTime(timezone=True), onupdate=func.now())
# 关联关系
task = relationship("AgentTask", back_populates="findings")
def __repr__(self):
return f"<AgentFinding {self.vulnerability_type} - {self.severity} - {self.file_path}>"
def generate_fingerprint(self) -> str:
"""生成去重指纹"""
import hashlib
components = [
self.vulnerability_type or "",
self.file_path or "",
str(self.line_start or 0),
self.function_name or "",
(self.code_snippet or "")[:200],
]
content = "|".join(components)
return hashlib.sha256(content.encode()).hexdigest()[:16]
def to_dict(self) -> dict:
"""转换为字典"""
return {
"id": self.id,
"task_id": self.task_id,
"vulnerability_type": self.vulnerability_type,
"severity": self.severity,
"title": self.title,
"description": self.description,
"file_path": self.file_path,
"line_start": self.line_start,
"line_end": self.line_end,
"code_snippet": self.code_snippet,
"status": self.status,
"is_verified": self.is_verified,
"has_poc": self.has_poc,
"poc_code": self.poc_code,
"suggestion": self.suggestion,
"fix_code": self.fix_code,
"ai_explanation": self.ai_explanation,
"ai_confidence": self.ai_confidence,
"created_at": self.created_at.isoformat() if self.created_at else None,
}
class AgentCheckpoint(Base):
"""
Agent 检查点
用于持久化 Agent 状态,支持:
- 任务恢复
- 状态回滚
- 执行历史追踪
"""
__tablename__ = "agent_checkpoints"
id = Column(String(36), primary_key=True, default=lambda: str(uuid.uuid4()))
task_id = Column(String(36), ForeignKey("agent_tasks.id", ondelete="CASCADE"), nullable=False, index=True)
# Agent 信息
agent_id = Column(String(50), nullable=False, index=True)
agent_name = Column(String(255), nullable=False)
agent_type = Column(String(50), nullable=False)
parent_agent_id = Column(String(50), nullable=True)
# 状态数据(JSON 序列化的 AgentState)
state_data = Column(Text, nullable=False)
# 执行状态
iteration = Column(Integer, default=0)
status = Column(String(30), nullable=False)
# 统计信息
total_tokens = Column(Integer, default=0)
tool_calls = Column(Integer, default=0)
findings_count = Column(Integer, default=0)
# 检查点类型
checkpoint_type = Column(String(30), default="auto") # auto, manual, error, final
checkpoint_name = Column(String(255), nullable=True)
# 元数据
checkpoint_metadata = Column(JSON, nullable=True)
# 时间戳
created_at = Column(DateTime(timezone=True), server_default=func.now(), index=True)
def __repr__(self):
return f"<AgentCheckpoint {self.agent_id} - iter {self.iteration}>"
def to_dict(self) -> dict:
"""转换为字典"""
return {
"id": self.id,
"task_id": self.task_id,
"agent_id": self.agent_id,
"agent_name": self.agent_name,
"agent_type": self.agent_type,
"parent_agent_id": self.parent_agent_id,
"iteration": self.iteration,
"status": self.status,
"total_tokens": self.total_tokens,
"tool_calls": self.tool_calls,
"findings_count": self.findings_count,
"checkpoint_type": self.checkpoint_type,
"checkpoint_name": self.checkpoint_name,
"created_at": self.created_at.isoformat() if self.created_at else None,
}
class AgentTreeNode(Base):
"""
Agent 树节点
记录动态 Agent 树的结构,用于:
- 可视化 Agent 树
- 追踪 Agent 间关系
- 分析执行流程
"""
__tablename__ = "agent_tree_nodes"
id = Column(String(36), primary_key=True, default=lambda: str(uuid.uuid4()))
task_id = Column(String(36), ForeignKey("agent_tasks.id", ondelete="CASCADE"), nullable=False, index=True)
# Agent 信息
agent_id = Column(String(50), nullable=False, unique=True, index=True)
agent_name = Column(String(255), nullable=False)
agent_type = Column(String(50), nullable=False)
# 树结构
parent_agent_id = Column(String(50), nullable=True, index=True)
depth = Column(Integer, default=0) # 树深度
# 任务信息
task_description = Column(Text, nullable=True)
knowledge_modules = Column(JSON, nullable=True)
# 执行状态
status = Column(String(30), default="created")
# 执行结果
result_summary = Column(Text, nullable=True)
findings_count = Column(Integer, default=0)
# 统计
iterations = Column(Integer, default=0)
tokens_used = Column(Integer, default=0)
tool_calls = Column(Integer, default=0)
duration_ms = Column(Integer, nullable=True)
# 时间戳
created_at = Column(DateTime(timezone=True), server_default=func.now())
started_at = Column(DateTime(timezone=True), nullable=True)
finished_at = Column(DateTime(timezone=True), nullable=True)
def __repr__(self):
return f"<AgentTreeNode {self.agent_name} ({self.agent_id})>"
def to_dict(self) -> dict:
"""转换为字典"""
return {
"id": self.id,
"task_id": self.task_id,
"agent_id": self.agent_id,
"agent_name": self.agent_name,
"agent_type": self.agent_type,
"parent_agent_id": self.parent_agent_id,
"depth": self.depth,
"task_description": self.task_description,
"knowledge_modules": self.knowledge_modules,
"status": self.status,
"result_summary": self.result_summary,
"findings_count": self.findings_count,
"iterations": self.iterations,
"tokens_used": self.tokens_used,
"tool_calls": self.tool_calls,
"duration_ms": self.duration_ms,
"created_at": self.created_at.isoformat() if self.created_at else None,
"started_at": self.started_at.isoformat() if self.started_at else None,
"finished_at": self.finished_at.isoformat() if self.finished_at else None,
}