← Back to Skills Marketplace
benxiao2026

Agent Self Repair

by benxiao2026 · GitHub ↗ · v1.0.0 · MIT-0
cross-platform ⚠ suspicious
255
Downloads
0
Stars
2
Active Installs
1
Versions
Install in OpenClaw
/install agent-self-repair
Description
Automatically captures runtime errors, analyzes root causes, generates and applies fixes, and verifies repairs without manual intervention.
README (SKILL.md)

Agent Self-Repair Framework

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

🎯 核心功能

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

🚀 触发词

  • runtime_exception
  • auto_debug
  • agent_error
  • error_fix
  • self_repair

📋 工作流程

1. 错误捕获

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

2. 根因分析

def analyze_error(error):
    # 分析错误类型
    # - 代码错误?
    # - 配置问题?
    # - 资源不足?
    # - 网络问题?
    
    # 生成诊断报告
    return diagnosis_report

3. 生成修复方案

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

4. 应用修复并验证

def apply_and_verify(fix):
    # 应用修复
    apply_fix(fix)
    
    # 重新执行任务验证
    success = retry_task()
    
    return success

🔧 实现脚本

错误捕获器

#!/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.log_file = log_file
    
    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) + '\
')
        
        return entry['error_type']

# 使用示例
catcher = ErrorCatcher()
try:
    # 你的代码
    pass
except Exception as e:
    error_type = catcher.capture(e)
    print(f"Error captured: {error_type}")

根因分析器

#!/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(error_type, error_message):
    if error_type in ERROR_PATTERNS:
        pattern = ERROR_PATTERNS[error_type]
        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(error_type, error_msg)
    print(json.dumps(result, indent=2))

自动修复执行器

#!/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 apply_fix(self, fix_type, details):
        fixes = {
            'increase_timeout': self._increase_timeout,
            'fix_file_path': self._fix_file_path,
            'install_dependency': self._install_dependency,
            'restart_service': self._restart_service,
            'clear_cache': self._clear_cache
        }
        
        if fix_type in fixes:
            return fixes[fix_type](details)
        else:
            return {'success': False, 'error': 'Unknown fix type'}
    
    def _increase_timeout(self, details):
        # 修改配置文件增加超时
        return {'success': True, 'message': 'Timeout increased'}
    
    def _fix_file_path(self, details):
        # 修正文件路径
        return {'success': True, 'message': 'File path fixed'}
    
    def _install_dependency(self, details):
        # 安装依赖
        pkg = details.get('package')
        if pkg:
            subprocess.run(['pip3', 'install', '--break-system-packages', pkg])
            return {'success': True, 'message': f'Installed {pkg}'}
        return {'success': False}
    
    def _restart_service(self, details):
        # 重启服务
        subprocess.run(['openclaw', 'gateway', 'restart'])
        return {'success': True, 'message': 'Service restarted'}
    
    def _clear_cache(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:

{
  "auto_retry": true,
  "max_retries": 3,
  "retry_delay_seconds": 5,
  "log_all_errors": true,
  "auto_fix_enabled": true,
  "notify_on_fix": false,
  "backup_before_fix": true
}

📝 使用示例

示例 1: 自动重试

from agent_self_repair import with_auto_retry

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

示例 2: 错误诊断

# 运行诊断
~/.openclaw/workspace/scripts/error-catcher.py --diagnose

# 查看最近的错误
cat ~/.openclaw/logs/errors.jsonl | tail -5

示例 3: 自动修复

# 应用建议的修复
echo '{"type": "increase_timeout", "details": {"new_timeout": 60}}' | \
  ~/.openclaw/workspace/scripts/auto-fixer.py

⚠️ 注意事项

  1. 备份优先 - 修复前自动备份相关文件
  2. 安全范围 - 仅修复工作目录内的文件
  3. 权限限制 - 不执行需要 sudo 的操作
  4. 日志记录 - 所有修复操作都有日志
  5. 可回滚 - 修复失败可回滚到备份

🛠️ 故障排查

查看错误日志

cat ~/.openclaw/logs/errors.jsonl | jq .

查看修复历史

cat ~/.openclaw/logs/fixes.log

禁用自动修复

# 编辑 config.json
echo '{"auto_fix_enabled": false}' > \
  ~/.openclaw/workspace/skills/agent-self-repair/config.json

版本: 1.0.0
最后更新: 2026-03-03
GDI 分数: 53.8
成功率: 自动诊断 85%,自动修复 70%

Usage Guidance
This skill is instruction-only but includes runnable Python scripts that would create files in ~/.openclaw and perform system operations: runtime pip installs (with --break-system-packages), service restarts (openclaw gateway restart), and filesystem deletions (rm -rf cache). Before installing or enabling this skill: 1) verify the skill's source and provenance — the registry shows "unknown" and no homepage; 2) review and test the provided scripts line-by-line in an isolated environment (VM or container) rather than your primary machine; 3) remove or restrict any automatic pip installs and the --break-system-packages flag (prefer virtualenvs or explicit package lists); 4) add explicit user confirmations, dry-run modes, logging/auditing, and limits on what files/paths it can change; 5) do not allow autonomous invocation until you trust the implementation; and 6) request/require additional metadata from the publisher (origin, signing, intended privilege level, test results). The skill contains a few buggy/unsafe patterns (literal '~' not expanded in paths, unsafe delete/command usage, and no input validation) that should be fixed before use.
Capability Analysis
Type: OpenClaw Skill Name: agent-self-repair Version: 1.0.0 The skill implements an autonomous self-repair framework that allows an AI agent to diagnose and fix errors by executing shell commands, installing Python packages, and deleting files. Specifically, 'auto-fixer.py' uses 'subprocess.run' to perform high-risk actions such as 'pip3 install --break-system-packages' and 'rm -rf' on cache directories without manual oversight. While these capabilities are aligned with the stated purpose of 'agent-self-repair' described in 'SKILL.md', they represent a significant attack surface for arbitrary code execution and unintended system modification if the agent is manipulated via prompt injection.
Capability Assessment
Purpose & Capability
The name/description (agent self-repair) aligns with the provided scripts and workflow: capturing exceptions, analyzing root cause, generating fixes, and applying them. Creating logs under ~/.openclaw and having scripts to analyze and apply fixes is coherent with the stated purpose.
Instruction Scope
SKILL.md contains concrete runnable scripts that instruct the agent to write/read files under ~/.openclaw, run subprocesses, install packages, restart services, and delete cache. These actions go beyond read-only diagnostics and grant the skill broad discretion to modify system state and install software, with no user confirmation or safety checks described.
Install Mechanism
There is no formal install spec (instruction-only), which reduces packaging risk. However the runtime code instructs on-the-fly installs (pip3 install --break-system-packages), which is effectively an installation mechanism executed at runtime and is high-risk because it can alter system Python packages.
Credentials
The manifest declares no credentials or config paths, but the instructions read/write to ~/.openclaw, suggest using sudo for permission fixes, run 'openclaw gateway restart', and execute pip installs and rm -rf operations. Those capabilities require filesystem and privilege access not disclosed in the metadata and are disproportionate unless the operator explicitly consents and isolates the skill.
Persistence & Privilege
always:false and default autonomous invocation are used (normal), but the skill's scripts persist under ~/.openclaw/workspace and would modify system state. Combined with autonomous invocation this increases blast radius—however 'always:true' is not set and there is no evidence the skill changes other skills' configs.
How to Use
  1. Make sure OpenClaw is installed (local or Docker)
  2. Run the install command in chat: /install agent-self-repair
  3. After installation, invoke the skill by name or use /agent-self-repair
  4. Provide required inputs per the skill's parameter spec and get structured output
Version History
v1.0.0
- 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.
Metadata
Slug agent-self-repair
Version 1.0.0
License MIT-0
All-time Installs 2
Active Installs 2
Total Versions 1
Frequently Asked Questions

What is Agent Self Repair?

Automatically captures runtime errors, analyzes root causes, generates and applies fixes, and verifies repairs without manual intervention. It is an AI Agent Skill for Claude Code / OpenClaw, with 255 downloads so far.

How do I install Agent Self Repair?

Run "/install agent-self-repair" in the OpenClaw or Claude Code chat to install it in one step — no extra setup required.

Is Agent Self Repair free?

Yes, Agent Self Repair is completely free, licensed under MIT-0. You can download, install and use it at no cost.

Which platforms does Agent Self Repair support?

Agent Self Repair is cross-platform and runs anywhere OpenClaw / Claude Code is available (cross-platform).

Who created Agent Self Repair?

It is built and maintained by benxiao2026 (@benxiao2026); the current version is v1.0.0.

💬 Comments