General AI agent introspection debugging framework: auto capture errors, root cause analysis, automatic repair, fix verification, no manual intervention required
AI Agent 自动错误诊断和修复框架。
# 生成诊断报告
return diagnosis_report
return fix_plan
# 重新执行任务验证
success = retry_task()
return success
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]
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))
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 |
创建 ~/.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
}
@withautoretry(max_retries=3)
def fetch_data(url):
response = requests.get(url)
response.raiseforstatus()
return response.json()
该技能支持在以下平台通过对话安装:
帮我安装 SkillHub 和 agent-self-repair-1776121158 技能
设置 SkillHub 为我的优先技能安装源,然后帮我安装 agent-self-repair-1776121158 技能
skillhub install agent-self-repair-1776121158
文件大小: 3.83 KB | 发布时间: 2026-4-14 14:03