返回顶部
a

agent-self-repair智能体自修复

General AI agent introspection debugging framework: auto capture errors, root cause analysis, automatic repair, fix verification, no manual intervention required

作者: admin | 来源: ClawHub
源自
ClawHub
版本
V 1.0.0
安全检测
已通过
195
下载量
免费
免费
0
收藏
概述
安装方式
版本历史

agent-self-repair

Agent Self-Repair Framework

AI Agent 自动错误诊断和修复框架。

🎯 核心功能

  • - ✅ 自动错误捕获 - 运行时异常自动捕获
  • 根因分析 - AI 驱动的错误根因分析
  • 自动修复 - 生成并应用修复方案
  • 修复验证 - 自动验证修复是否成功
  • 无需人工干预 - 完整闭环自动化

🚀 触发词

  • - runtimeexception
  • autodebug
  • agenterror
  • errorfix
  • self_repair

📋 工作流程

1. 错误捕获

python try: # 执行任务 result = execute_task() except Exception as e: # 自动捕获并记录 capture_error(e)

2. 根因分析

python def analyze_error(error): # 分析错误类型 # - 代码错误? # - 配置问题? # - 资源不足? # - 网络问题?

# 生成诊断报告
return diagnosis_report

3. 生成修复方案

python def generate_fix(diagnosis): # 根据根因生成修复方案 # - 代码修复 # - 配置更新 # - 资源调整 # - 重试策略

return fix_plan

4. 应用修复并验证

python def applyandverify(fix): # 应用修复 apply_fix(fix)

# 重新执行任务验证
success = retry_task()

return success

🔧 实现脚本

错误捕获器

python #!/usr/bin/env python3

~/.openclaw/workspace/scripts/error-catcher.py

import sys
import json
import traceback
from datetime import datetime

class ErrorCatcher:
def init(self, log_file=~/.openclaw/logs/errors.jsonl):
self.logfile = logfile

def capture(self, error, context=None):
entry = {
timestamp: datetime.now().isoformat(),
error_type: type(error).name,
error_message: str(error),
traceback: traceback.format_exc(),
context: context or {}
}

# 写入日志
with open(self.log_file, a) as f:
f.write(json.dumps(entry) + \n)

return entry[error_type]

使用示例

catcher = ErrorCatcher() try: # 你的代码 pass except Exception as e: error_type = catcher.capture(e) print(fError captured: {error_type})

根因分析器

python #!/usr/bin/env python3

~/.openclaw/workspace/scripts/root-cause-analyzer.py

import json
import sys

ERROR_PATTERNS = {
FileNotFoundError: {
cause: 文件路径错误或文件不存在,
fix: 检查文件路径,确认文件存在
},
PermissionError: {
cause: 权限不足,
fix: 检查文件权限或使用 sudo
},
TimeoutError: {
cause: 操作超时,
fix: 增加超时时间或优化性能
},
ConnectionError: {
cause: 网络连接失败,
fix: 检查网络连接或代理配置
},
MemoryError: {
cause: 内存不足,
fix: 减少数据量或增加内存
}
}

def analyze(errortype, errormessage):
if errortype in ERRORPATTERNS:
pattern = ERRORPATTERNS[errortype]
return {
root_cause: pattern[cause],
suggested_fix: pattern[fix],
confidence: 0.8
}
else:
return {
root_cause: 未知错误类型,
suggested_fix: 查看详细日志,
confidence: 0.3
}

if name == main:
error_type = sys.argv[1] if len(sys.argv) > 1 else UnknownError
error_msg = sys.argv[2] if len(sys.argv) > 2 else

result = analyze(errortype, errormsg)
print(json.dumps(result, indent=2))

自动修复执行器

python #!/usr/bin/env python3

~/.openclaw/workspace/scripts/auto-fixer.py

import subprocess
import os
import json

class AutoFixer:
def init(self):
self.workspace = os.path.expanduser(~/.openclaw/workspace)

def applyfix(self, fixtype, details):
fixes = {
increasetimeout: self.increase_timeout,
fixfilepath: self.fixfile_path,
installdependency: self.install_dependency,
restartservice: self.restart_service,
clearcache: self.clear_cache
}

if fix_type in fixes:
return fixesfixtype
else:
return {success: False, error: Unknown fix type}

def increasetimeout(self, details):
# 修改配置文件增加超时
return {success: True, message: Timeout increased}

def fixfile_path(self, details):
# 修正文件路径
return {success: True, message: File path fixed}

def installdependency(self, details):
# 安装依赖
pkg = details.get(package)
if pkg:
subprocess.run([pip3, install, --break-system-packages, pkg])
return {success: True, message: fInstalled {pkg}}
return {success: False}

def restartservice(self, details):
# 重启服务
subprocess.run([openclaw, gateway, restart])
return {success: True, message: Service restarted}

def clearcache(self, details):
# 清理缓存
cache_dir = os.path.expanduser(~/.openclaw/cache)
if os.path.exists(cache_dir):
subprocess.run([rm, -rf, f{cache_dir}/*])
return {success: True, message: Cache cleared}
return {success: True, message: No cache to clear}

if name == main:
fixer = AutoFixer()

# 从 stdin 读取修复指令
fix指令 = json.load(sys.stdin)
result = fixer.apply_fix(fix指令[type], fix指令.get(details, {}))

print(json.dumps(result))

📊 错误类型和修复策略

错误类型根因自动修复策略
FileNotFoundError文件路径错误自动搜索正确路径
PermissionError
权限不足 | 申请权限或修改路径 | | TimeoutError | 超时 | 增加超时时间 | | ConnectionError | 网络问题 | 检查代理/重试 | | MemoryError | 内存不足 | 分批处理/清理缓存 | | ModuleNotFoundError | 缺少依赖 | 自动安装依赖 | | JSONDecodeError | 格式错误 | 尝试修复 JSON |

🔧 配置选项

创建 ~/.openclaw/workspace/skills/agent-self-repair/config.json:

json
{
auto_retry: true,
max_retries: 3,
retrydelayseconds: 5,
logallerrors: true,
autofixenabled: true,
notifyonfix: false,
backupbeforefix: true
}

📝 使用示例

示例 1: 自动重试

python from agentselfrepair import withautoretry

@withautoretry(max_retries=3)
def fetch_data(url):
response = requests.get(url)
response.raiseforstatus()
return response.json()

示例 2: 错误诊断

bash

运行诊断

~/.openclaw/workspace/scripts/error-catcher.py --diagnose

查看最近的错误

cat ~/.openclaw/logs/errors.jsonl | tail -5

示例 3: 自动修复

bash

应用建议的修复

echo {type: increasetimeout, details: {newtimeout: 60}} | \ ~/.openclaw/workspace/scripts/auto-fixer.py

⚠️ 注意事项

标签

skill ai

通过对话安装

该技能支持在以下平台通过对话安装:

OpenClaw WorkBuddy QClaw Kimi Claude

方式一:安装 SkillHub 和技能

帮我安装 SkillHub 和 agent-self-repair-1776121158 技能

方式二:设置 SkillHub 为优先技能安装源

设置 SkillHub 为我的优先技能安装源,然后帮我安装 agent-self-repair-1776121158 技能

通过命令行安装

skillhub install agent-self-repair-1776121158

下载

⬇ 下载 agent-self-repair v1.0.0(免费)

文件大小: 3.83 KB | 发布时间: 2026-4-14 14:03

v1.0.0 最新 2026-4-14 14:03
- Initial release of agent-self-repair: a fully automated AI agent introspection and self-repair framework.
- Features automatic error capture, root cause analysis, solution generation, fix application, and repair verification without manual intervention.
- Includes modular Python scripts for error catching, root cause analysis, and automated fixing.
- Supports error types such as FileNotFoundError, PermissionError, TimeoutError, ConnectionError, and more, with tailored auto-fix strategies.
- Easily configurable via JSON, with options for retries, backup-before-fix, and detailed logging.
- Provides example workflows and usage instructions for diagnosis, fix application, and troubleshooting.

Archiver·手机版·闲社网·闲社论坛·羊毛社区· 多链控股集团有限公司 · 苏ICP备2025199260号-1

Powered by Discuz! X5.0   © 2024-2025 闲社网·线报更新论坛·羊毛分享社区·http://xianshe.com

p2p_official_large
返回顶部