问题收集 → Git 定位 → 代码修复 → 测试 → 部署 | 多智能体分工 × 自定义 Skill 链 × 感知 - 执行 - 反思闭环 × 沙箱执行
5 个专业智能体各司其职,通过 OpenClaw 编排引擎实现高效协作,覆盖 Bug 修复全流程,每个智能体具备独立的"感知 - 执行 - 反思"能力
20+ 个可复用 Skills,支持灵活组合形成 Skill 链,实现能力复用和快速编排,降低开发成本,加速场景落地
每个智能体都具备完整的认知循环,能够感知环境状态、执行动作、反思结果并持续优化策略,实现自主进化
所有代码修复和测试在 Docker 沙箱中隔离执行,确保生产环境安全,支持快速回滚和资源回收
每个操作都有完整日志和分布式追踪,支持事后审计、问题回溯、责任认定,满足企业合规要求
标准化的流程设计和 Skill 封装,支持快速复制到其他项目和场景,降低部署成本和边际成本
┌─────────────────────────────────────────────────────────────────────────────────┐ │ 基于 OpenClaw 的 Bugfix Agent 架构 │ ├─────────────────────────────────────────────────────────────────────────────────┤ │ │ │ ┌─────────────────────────────────────────────────────────────────────────┐ │ │ │ 问题收集层 │ │ │ │ ┌──────────┐ ┌──────────┐ ┌──────────┐ ┌──────────┐ ┌──────────┐ │ │ │ │ │ Web │ │ Slack │ │ 钉钉 │ │ 邮件 │ │ 监控 │ │ │ │ │ │ 控制台 │ │ Bot │ │ Bot │ │ 系统 │ │ 系统 │ │ │ │ │ └──────────┘ └──────────┘ └──────────┘ └──────────┘ └──────────┘ │ │ │ └─────────────────────────────────────────────────────────────────────────┘ │ │ │ │ │ ┌─────────────────────────────────────────────────────────────────────────┐ │ │ │ OpenClaw 智能体编排层 │ │ │ │ ┌───────────────────────────────────────────────────────────────────┐ │ │ │ │ │ Coordinator Agent │ │ │ │ │ │ ┌─────────────────────────────────────────────────────────────┐ │ │ │ │ │ │ │ 工作流引擎 & 状态管理 & 决策引擎 │ │ │ │ │ │ │ └─────────────────────────────────────────────────────────────┘ │ │ │ │ │ └───────────────────────────────────────────────────────────────────┘ │ │ │ │ ┌────────────┐ ┌────────────┐ ┌────────────┐ ┌────────────┐ │ │ │ │ │ Collector │ │ Locator │ │ Fixer │ │ Tester │ │ │ │ │ │ Agent │ │ Agent │ │ Agent │ │ Agent │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ - 感知 │ │ - 感知 │ │ - 感知 │ │ - 感知 │ │ │ │ │ │ - 执行 │ │ - 执行 │ │ - 执行 │ │ - 执行 │ │ │ │ │ │ - 反思 │ │ - 反思 │ │ - 反思 │ │ - 反思 │ │ │ │ │ └────────────┘ └────────────┘ └────────────┘ └────────────┘ │ │ │ └─────────────────────────────────────────────────────────────────────────┘ │ │ │ │ │ ┌─────────────────────────────────────────────────────────────────────────┐ │ │ │ Skill 链层 │ │ │ │ ┌───────────────────────────────────────────────────────────────────┐ │ │ │ │ │ Skill Registry & Chain Engine │ │ │ │ │ │ 20+ Skills: receive_bug, git_blame, code_analyze, fix_generate │ │ │ │ │ │ run_tests, regression_check, canary_deploy... │ │ │ │ │ └───────────────────────────────────────────────────────────────────┘ │ │ │ └─────────────────────────────────────────────────────────────────────────┘ │ │ │ │ │ ┌─────────────────────────────────────────────────────────────────────────┐ │ │ │ 执行层 │ │ │ │ ┌──────────────┐ ┌──────────────┐ ┌──────────────┐ ┌──────────────┐│ │ │ │ │ Git │ │ Docker │ │ Jenkins │ │ K8s ││ │ │ │ │ │ │ Sandbox │ │ │ │ ││ │ │ │ │ - 代码仓库 │ │ - 沙箱环境 │ │ - CI/CD │ │ - 容器编排 ││ │ │ │ │ - 分支管理 │ │ - 隔离执行 │ │ - 流水线 │ │ - 服务部署 ││ │ │ │ │ - Blame │ │ - 资源限制 │ │ - 测试执行 │ │ - 灰度发布 ││ │ │ │ └──────────────┘ └──────────────┘ └──────────────┘ └──────────────┘│ │ │ └─────────────────────────────────────────────────────────────────────────┘ │ │ │ │ │ ┌─────────────────────────────────────────────────────────────────────────┐ │ │ │ 数据层 │ │ │ │ ┌──────────────┐ ┌──────────────┐ ┌──────────────┐ ┌──────────────┐│ │ │ │ │ PostgreSQL │ │ Redis │ │ Elasticsearch│ │ MinIO ││ │ │ │ │ - Bug 工单 │ │ - 缓存 │ │ - 日志检索 │ │ - 文件存储 ││ │ │ │ │ - 执行记录 │ │ - 会话 │ │ - 全文搜索 │ │ - 备份归档 ││ │ │ │ │ - 审计日志 │ │ - 分布式锁 │ │ - 数据分析 │ │ - 镜像存储 ││ │ │ │ └──────────────┘ └──────────────┘ └──────────────┘ └──────────────┘│ │ │ └─────────────────────────────────────────────────────────────────────────┘ │ │ │ └─────────────────────────────────────────────────────────────────────────────────┘
每个智能体专注单一职责,通过明确定义的接口协作,降低耦合度,提高可维护性和可测试性
每个智能体都具备"感知 - 执行 - 反思"能力,能够根据执行结果调整策略,实现持续优化和自主进化
所有代码执行在 Docker 沙箱中进行,与生产环境完全隔离,确保系统安全和数据隐私
每个操作都有详细日志和分布式追踪 ID,支持全链路追踪,满足企业合规要求和责任追溯
问题收集智能体
问题定位智能体
代码修复智能体
测试验证智能体
部署发布智能体
class BaseAgent(ABC): """智能体基类 - 所有智能体的抽象""" def __init__(self, name, skills, config): self.name = name self.skills = SkillRegistry(skills) self.config = config self.state = AgentState.IDLE self.metrics = AgentMetrics() @abstractmethod async def perceive(self, context): """感知:收集环境信息和状态""" pass @abstractmethod async def execute(self, perception): """执行:基于感知执行动作""" pass @abstractmethod async def reflect(self, result): """反思:评估执行结果,优化策略""" pass async def run_cycle(self, context): """运行完整的感知 - 执行 - 反思循环""" # 1. 感知 perception = await self.perceive(context) await self.metrics.record("perception", perception) # 2. 执行 result = await self.execute(perception) await self.metrics.record("execution", result) # 3. 反思 reflection = await self.reflect(result) await self.metrics.record("reflection", reflection) # 4. 更新策略 if reflection.should_optimize: await self.optimize_strategy(reflection) return result
class CollectorAgent(BaseAgent): """问题收集智能体 - 负责接收、分类、优先级排序 Bug 反馈""" async def perceive(self, context): """感知:从多渠道收集 Bug 信息""" bug_reports = [] # 从各渠道收集 for channel in ["web", "slack", "email", "monitor"]: reports = await self.skills.receive_bug.execute(channel) bug_reports.extend(reports) return {"bug_reports": bug_reports, "count": len(bug_reports)} async def execute(self, perception): """执行:分类、优先级排序、创建工单""" tickets = [] for report in perception["bug_reports"]: # 分类 category = await self.skills.classify.execute(report) # 优先级评估 priority = await self.skills.prioritize.execute( report, category ) # 创建工单 ticket = await self.skills.create_ticket.execute( report, category, priority ) tickets.append(ticket) return {"tickets": tickets, "count": len(tickets)} async def reflect(self, result): """反思:评估收集效果,优化分类规则""" reflection = {"should_optimize": False} for ticket in result["tickets"]: if ticket.category_confidence < 0.7: # 低置信度,需要优化分类模型 reflection["should_optimize"] = True await self.optimize_classifier(ticket) # 记录指标 await self.metrics.record("bug_collected", 1) return reflection
class LocatorAgent(BaseAgent): """问题定位智能体 - 负责 Git 定位、根因分析、责任归属""" async def perceive(self, context): """感知:收集错误信息、日志、堆栈""" error_info = { "stack_trace": context.ticket.stack_trace, "error_logs": await self.search_logs(context.ticket), "recent_changes": await self.get_recent_commits(context.ticket), "affected_files": context.ticket.affected_files } return error_info async def execute(self, perception): """执行:Git 定位、根因分析、责任归属""" # Git Blame 分析代码归属 ownership = await self.skills.git_blame.execute( perception["affected_files"] ) # 根因分析 root_cause = await self.skills.root_cause.execute( perception["stack_trace"], perception["error_logs"] ) # 影响范围分析 impact = await self.skills.impact_analysis.execute( root_cause.affected_module ) return LocalizationResult( root_cause=root_cause, ownership=ownership, impact=impact, confidence=root_cause.confidence ) async def reflect(self, result): """反思:评估定位准确性,优化分析模型""" reflection = {"should_optimize": False} if result.confidence < 0.8: # 低置信度,标记需要人工介入 reflection["should_optimize"] = True await self.flag_for_human_review(result) # 记录定位指标 await self.metrics.record("location_accuracy", result.confidence) return reflection
class BaseSkill(ABC): """Skill 基类 - 所有 Skills 的抽象""" def __init__(self, name, description): self.name = name self.description = description self.metrics = SkillMetrics() self.version = "1.0" @abstractmethod async def execute(self, **kwargs): """执行 Skill""" pass async def validate(self, input_data): """验证输入""" pass async def log(self, action, result): """记录审计日志""" await AuditLogger.log( skill=self.name, action=action, result=result, timestamp=datetime.utcnow() ) class SkillChain: """Skill 链 - 组合多个 Skills 形成复杂能力""" def __init__(self, skills): self.skills = skills self.execution_graph = ExecutionGraph() async def execute(self, input_data): """按依赖图执行 Skills""" results = {} # 拓扑排序确定执行顺序 ordered_skills = self.execution_graph.topological_sort() for skill in ordered_skills: # 获取依赖的输入 skill_input = self.get_dependencies(skill, results) # 执行 Skill result = await skill.execute(**skill_input) # 存储结果供后续 Skill 使用 results[skill.name] = result return results
| Skill 名称 | 所属 Agent | 功能描述 | 输入 | 输出 |
|---|---|---|---|---|
receive_bug |
Collector | 从多渠道接收 Bug 报告 | channel, data | BugReport |
classify |
Collector | 自动分类 Bug 类型 | BugReport | Category + Confidence |
prioritize |
Collector | 评估 Bug 优先级 | BugReport, Category | Priority (P0-P3) |
git_blame |
Locator | Git Blame 代码归属分析 | file_path, line_number | AuthorInfo |
analyze_log |
Locator | 日志分析与异常提取 | log_entries | ErrorPatterns |
root_cause |
Locator | 根因分析 | stack_trace, logs | RootCause + Confidence |
code_analyze |
Fixer | 代码理解与分析 | code_context | CodeAnalysis |
fix_generate |
Fixer | 生成修复方案 | RootCause, CodeAnalysis | FixPlan[] |
branch_create |
Fixer | 创建 Git 修复分支 | ticket_id | BranchName |
commit_code |
Fixer | 提交修复代码 | branch, fix, message | CommitHash |
run_unit_test |
Tester | 执行单元测试 | test_files | TestResult |
regression_test |
Tester | 回归测试验证 | fix_commit | RegressionReport |
canary_deploy |
Deployer | 灰度发布 | image, percentage | DeploymentStatus |
rollback |
Deployer | 自动回滚 | deployment_id | RollbackStatus |
# 定义完整的 Bug 修复 Skill 链 bugfix_chain = SkillChain([ # 阶段 1: 问题收集 ReceiveBugSkill(), ClassifyBugSkill(), PrioritizeBugSkill(), CreateTicketSkill(), # 阶段 2: 问题定位 GitBlameSkill(), AnalyzeLogSkill(), RootCauseSkill(), ImpactAnalysisSkill(), # 阶段 3: 代码修复 CodeAnalyzeSkill(), FixGenerateSkill(), BranchCreateSkill(), CommitCodeSkill(), # 阶段 4: 测试验证 RunUnitTestSkill(), RunIntegrationSkill(), RegressionTestSkill(), CoverageCheckSkill(), # 阶段 5: 部署发布 CodeReviewSkill(), CanaryDeploySkill(), MonitorSkill(), RollbackSkill() ]) # 执行 Skill 链 result = await bugfix_chain.execute(initial_bug_report)
负责智能体:Collector Agent
核心 Skills:receive_bug → classify → prioritize → create_ticket
输入:多渠道 Bug 报告
处理:分类、优先级评估、工单创建
输出:结构化 Bug 工单
负责智能体:Locator Agent
核心 Skills:git_blame → analyze_log → root_cause → impact_analysis
输入:Bug 工单、错误日志
处理:Git Blame、根因分析、影响评估
输出:定位报告、责任归属
负责智能体:Fixer Agent
核心 Skills:code_analyze → fix_generate → branch_create → commit_code
输入:定位报告、相关代码
处理:代码分析、修复生成、代码提交
输出:修复代码、Git 提交
负责智能体:Tester Agent
核心 Skills:run_unit_test → run_integration → regression_test → coverage_check
输入:修复代码、测试用例
处理:单元/集成/回归测试、覆盖率检查
输出:验证报告、测试覆盖率
负责智能体:Deployer Agent
核心 Skills:code_review → canary_deploy → monitor → rollback
输入:验证通过的修复
处理:代码审查、灰度发布、监控观察
输出:生产部署、监控指标
┌─────────────────────────────────────────────────────────────────┐
│ 沙箱管理层 │
│ ┌───────────────────────────────────────────────────────────┐ │
│ │ SandboxOrchestrator │ │
│ │ - 沙箱创建 - 资源分配 - 生命周期管理 - 清理回收 │ │
│ └───────────────────────────────────────────────────────────┘ │
│
▼
┌─────────────────────────────────────────────────────────────────┐
│ 沙箱实例层 │
│ ┌──────────────┐ ┌──────────────┐ ┌──────────────┐ │
│ │ Sandbox #1 │ │ Sandbox #2 │ │ Sandbox #3 │ │
│ │ │ │ │ │ │ │
│ │ - 代码执行 │ │ - 测试运行 │ │ - 集成验证 │ │
│ │ - 隔离环境 │ │ - 隔离环境 │ │ - 隔离环境 │ │
│ │ - 资源限制 │ │ - 资源限制 │ │ - 资源限制 │ │
│ └──────────────┘ └──────────────┘ └──────────────┘ │
│
▼
┌─────────────────────────────────────────────────────────────────┐
│ 资源隔离层 │
│ ┌──────────────┐ ┌──────────────┐ ┌──────────────┐ │
│ │ CPU Limit │ │ Memory Limit │ │ Network │ │
│ │ 2 Core │ │ 4GB │ │ Isolated │ │
│ └──────────────┘ └──────────────┘ └──────────────┘ │
│ ┌──────────────┐ ┌──────────────┐ ┌──────────────┐ │
│ │ Filesystem │ │ Process │ │ Timeout │ │
│ │ Read-Only │ │ Isolation │ │ 30min │ │
│ └──────────────┘ └──────────────┘ └──────────────┘ │
└─────────────────────────────────────────────────────────────────┘
class SandboxExecutor: """沙箱执行器 - 在隔离环境中执行代码""" def __init__(self, config): self.docker_client = docker.from_env() self.config = config self.sandboxes = {} async def create_sandbox(self, sandbox_id, requirements): """创建沙箱环境""" container = self.docker_client.containers.run( image="python:3.11-slim", name=f"bugfix-sandbox-{sandbox_id}", command="sleep infinity", detach=True, # 资源限制 cpu_quota=200000, # 2 Core mem_limit="4g", # 网络隔离 network="none", # 文件系统隔离 volumes={ "/tmp/sandbox/{sandbox_id}": { "bind": "/workspace", "mode": "rw" } }, # 安全配置 security_opt=["no-new-privileges:true"], cap_drop=["ALL"], read_only=True, tmpfs={"/tmp": "rw,noexec,nosuid,size=1g"} ) self.sandboxes[sandbox_id] = container return container async def execute_code(self, sandbox_id, code, timeout=1800): """在沙箱中执行代码""" container = self.sandboxes[sandbox_id] # 写入代码 await self.write_to_container(container, "/workspace/main.py", code) # 执行代码 (带超时) result = container.exec_run( cmd="python /workspace/main.py", demux=True, workdir="/workspace" ) # 收集输出 output = { "stdout": result.output[0].decode(), "stderr": result.output[1].decode(), "exit_code": result.exit_code } return output async def cleanup_sandbox(self, sandbox_id): """清理沙箱""" container = self.sandboxes.get(sandbox_id) if container: container.stop() container.remove() del self.sandboxes[sandbox_id]
class AuditLog: """审计日志数据结构""" def __init__(self): self.trace_id = uuid.uuid4() # 全链路追踪 ID self.timestamp = datetime.utcnow() self.agent_name = None self.skill_name = None self.action = None self.input_data = None self.output_data = None self.execution_time = None self.status = None self.error_message = None self.operator = None # AI Agent 或 人类 self.decision_reason = None # 决策依据 def to_dict(self): return { "trace_id": str(self.trace_id), "timestamp": self.timestamp.isoformat(), "agent": self.agent_name, "skill": self.skill_name, "action": self.action, "input": self.input_data, "output": self.output_data, "execution_time_ms": self.execution_time, "status": self.status, "error": self.error_message, "operator": self.operator, "decision_reason": self.decision_reason } class AuditLogger: """审计日志记录器""" @staticmethod async def log(**kwargs): """记录审计日志""" log_entry = AuditLog(**kwargs) # 写入数据库 await db.audit_logs.insert_one(log_entry.to_dict()) # 写入 Elasticsearch (用于检索) await es.index( index="audit-logs", body=log_entry.to_dict() ) @staticmethod async def query_by_trace_id(trace_id): """按追踪 ID 查询完整链路""" logs = await db.audit_logs.find( {"trace_id": trace_id} ).sort("timestamp", 1) return [log async for log in logs] @staticmethod async def generate_audit_report(ticket_id): """生成审计报告""" logs = await db.audit_logs.find( {"ticket_id": ticket_id} ) report = { "ticket_id": ticket_id, "total_actions": await logs.count(), "timeline": [log async for log in logs], "agents_involved": await logs.distinct("agent"), "total_execution_time": await calculate_total_time(logs), "success_rate": await calculate_success_rate(logs) } return report
| 查询类型 | API 端点 | 用途 |
|---|---|---|
| 全链路追踪 | GET /audit/trace/{trace_id} |
查看完整执行链路 |
| 按工单查询 | GET /audit/ticket/{ticket_id} |
查看某工单的所有操作 |
| 按 Agent 查询 | GET /audit/agent/{agent_name} |
查看某 Agent 的所有操作 |
| 错误查询 | GET /audit/errors?from=...&to=... |
查询特定时间段的错误 |
| 审计报告 | GET /audit/report/{ticket_id} |
生成完整审计报告 |