← 返回
AI智能 中文

ClawGuard

System-level watchdog for OpenClaw gateway restarts and sub-agent task PIDs. Monitors registered PIDs and optional log/directory freshness. Auto-reverts conf...
系统级看门狗,监控 OpenClaw 网关重启及子代理任务 PID,检测已注册 PID 与可选日志/目录新鲜度,自动回滚配置。
camopel
AI智能 clawhub v1.1.0 1 版本 100000 Key: 无需
★ 0
Stars
📥 811
下载
💾 14
安装
1
版本
#latest

概述

ClawGuard — Task & Gateway Watchdog

A lightweight service that monitors registered events:

  1. Sub-agent task PIDs — if PID dies → notify and remove. If log/dir stale → alert and remove.
  2. Gateway restarts — if restart fails → revert config backups (newest to oldest) → retry → notify.

ClawGuard only monitors what is explicitly registered. It does not auto-discover.

Install

cd <skill-dir>
bash scripts/install.sh

Installs:

  • Daemon: systemd user service (Linux) or launchd agent (macOS) — Restart=always, auto-starts on boot
  • CLI: claw-guard in ~/.local/bin/
  • Data: ~/.openclaw/workspace/tools/claw-guard/

OpenClaw Integration (Recommended)

1. Auto-register gateway restarts

Add ExecStartPre to your gateway service so every restart (manual, crash, or Restart=always) is automatically registered:

# ~/.config/systemd/user/openclaw-gateway.service
[Service]
ExecStartPre=/home/<user>/.local/bin/claw-guard register-restart
ExecStart=...

Then reload: systemctl --user daemon-reload

Now every gateway restart automatically:

  • Snapshots the current config (rotates up to 5 backups)
  • Watches for the gateway to come back
  • If it fails → reverts config backups newest-to-oldest → notifies default channel

No manual claw-guard register-restart needed — systemd handles it.

2. Add task execution rules to AGENTS.md

Add these rules so the agent always registers its work:

## Task Execution Rules (MANDATORY)

### Sub-agent requirement
- **Any exec/tool call that might take >5s → sub-agent** (`sessions_spawn`).
  Main agent stays responsive.
- **Complex or unpredictable tasks → always sub-agent.** Even if they might
  be fast. If you can't guarantee it won't block, delegate it.
- **Only run in main agent** if certain it won't block I/O (quick file reads,
  short `grep`, `git status`, `claw-guard status`, etc.)

### ClawGuard registration (MANDATORY for all sub-agents)
Every sub-agent and background process **must** be registered:

claw-guard register --id "" --pid \

--target "" \

--log "/path/to/logfile" --timeout 180 \

--command ""

- `--target`: same channel/room where the user asked for the task
- `--log` and `--timeout`: optional but recommended for long tasks
- If PID dies → claw-guard notifies the target channel and removes the entry
- If log goes stale → claw-guard notifies and removes

### Gateway restarts
- **Never restart the gateway while tasks are running** — it kills all sub-agents
- Gateway service has `ExecStartPre=claw-guard register-restart` — automatic
- No manual registration needed for restarts

3. How it works end-to-end

Sub-agent task flow:

  1. User requests a long-running task
  2. Agent spawns sub-agent → gets PID
  3. Agent runs: claw-guard register --id "task-name" --pid $PID --target "room:..." --command "..."
  4. If PID dies → claw-guard notifies the target channel → agent confirms result with user
  5. If log goes stale → claw-guard alerts → agent investigates

Gateway restart flow:

  1. Gateway restarts (manual, crash, or auto)
  2. ExecStartPre runs claw-guard register-restart → config backed up
  3. Gateway starts successfully → claw-guard logs ✅ Gateway restart succeeded → watch cleared
  4. Gateway fails to start → claw-guard tries config backups → notifies default channel

CLI Reference

Register a task

claw-guard register --id "benchmark-q8" --pid 12345 \
  --target "room:!abc:server" \
  --log "/path/to/task.log" --timeout 180 \
  --command "python3 benchmark.py"

# Or watch a directory for new file creation:
claw-guard register --id "export-gguf" --pid 12345 \
  --target "room:!abc:server" \
  --watch-dir "/path/to/output/" --timeout 300 \
  --command "export_gguf.py"
FlagRequiredDescription
-----------------------------
--idyesUnique task identifier
--pidyesProcess ID to watch
--targetyesNotification target (see formats below)
--lognoLog file path — checks mtime only
--watch-dirnoDirectory — checks newest file mtime
--timeoutnoStale threshold in seconds (default: 180)
--commandnoDescription included in notifications

Register a gateway restart

claw-guard register-restart [--target "room:!abc:server"]

No --target needed — sends to OpenClaw's default channel. Pass --target to override.

Manage

claw-guard status          # Show tasks, restart watch, config backups
claw-guard remove --id X   # Remove a task
claw-guard clear-done      # Remove completed/gone tasks

Behavior

Check cycle (every 15s)

  1. Gateway restart: if registered and gateway not active after 30s → revert + retry + notify
  2. PID check: if PID gone → notify target → remove entry
  3. Log/dir freshness: if mtime exceeds timeout → notify target → remove entry

Deduplication

After notifying, the registered entry is removed from the registry. Once removed, it can't fire again. No dedup tracking needed.

Restart / reboot behavior

On service restart or system reboot:

  • All registered tasks are cleared — nothing carries over
  • Config backups persist on disk (only thing that survives)

This is by design: after a reboot, all monitored processes are gone anyway. The agent must re-register any new tasks.

Notification Targets

Any format openclaw message send --target accepts:

  • room:!roomId:server (Matrix)
  • telegram:chatid
  • discord:#channel
  • slack:#channel

Gateway restart alerts with no --target are sent without a target flag, letting OpenClaw route to the default channel.

Design Principles

  • Registration-based, not auto-discovery — only watches what's explicitly registered
  • Notify once, then remove — no duplicate alerts, no stale state
  • In-memory state — registry clears on service restart (clean slate)
  • Disk persistence only for config backups — the only thing worth keeping across restarts
  • Cross-platform — Linux (systemd) and macOS (launchd)
  • Minimal overhead — ~7MB RAM, negligible CPU

版本历史

共 1 个版本

  • v1.1.0 当前
    2026-03-29 23:37 安全 安全

安全检测

腾讯云安全 (Keen)

安全,无风险
查看报告

腾讯云安全 (Sanbu)

安全,无风险
查看报告

🔗 相关推荐

ai-intelligence

ontology

oswalpalash
类型化知识图谱,用于结构化智能体记忆与可组合技能。支持创建/查询实体(人员、项目、任务、事件、文档)及关联...
★ 712 📥 243,832
data-analysis

ddgs-search

camopel
通过ddgs CLI免费提供多引擎网页搜索(支持DuckDuckGo、Google、Bing等)及arXiv API搜索,无需API密钥。适用于用户需要搜索网络或学术文献时。
★ 3 📥 1,626
ai-intelligence

Self-Improving + Proactive Agent

ivangdavila
自我反思+自我批评+自我学习+自组织记忆。智能体评估自身工作、发现错误并持续改进。
★ 1,358 📥 318,370