Safety Guard Skill
Three-mode safety guardrail system for autonomous agent sessions. Prevents destructive operations and logs all interventions.
What Claude Gets Wrong Without This Skill
Without safety guardrails, autonomous agents:
- Execute destructive commands (rm -rf, DROP TABLE) without confirmation
- Force push to protected branches, losing commit history
- Run unrestricted operations in production environments
- Bypass safety hooks (--no-verify flags) without justification
- Provide no audit trail of potentially dangerous actions
Safety guard ensures autonomous operations remain safe and auditable.
Three Modes
Careful Mode (Warn)
Behavior: Warns on destructive commands but allows execution after user confirmation.
Use When: Standard development, trusted environments, human oversight available.
Protected Operations: rm -rf, git push --force, git reset --hard, DROP TABLE/DATABASE, docker system prune, kubectl delete, chmod 777, sudo rm, npm/cargo publish, --no-verify flags.
Freeze Mode (Lock Writes)
Behavior: Blocks write operations to specific paths. Config: FREEZE_PATHS="/etc/,~/.ssh/". Blocks Write, Edit, and Bash ops to frozen paths.
Guard Mode (Careful + Freeze)
Behavior: Warns on destructive ops AND locks write paths. Use for: autonomous agents, docker-sandbox, permissionMode: dontAsk sessions.
Hook Implementation Pattern
Safety guard extends existing hooks with additional checks.
PreToolUse Bash Hook Enhancement:
# Add to existing block-destructive.sh or create safety-guard-bash.sh
SAFETY_MODE="${SAFETY_GUARD_MODE:-off}" # off | careful | freeze | guard
LOG_FILE="${HOME}/.claude/safety-guard.log"
if [ "$SAFETY_MODE" = "off" ]; then
exit 0 # Safety guard disabled
fi
COMMAND="$1" # Tool input from stdin
# Careful mode: destructive operation detection
if [[ "$SAFETY_MODE" =~ (careful|guard) ]]; then
if echo "$COMMAND" | grep -qE "rm\s+-rf|git push.*--force|git reset --hard|DROP (TABLE|DATABASE)|docker system prune|kubectl delete|chmod 777|sudo rm|--no-verify"; then
echo "$(date -Iseconds) WARN: $COMMAND" >> "$LOG_FILE"
echo "⚠️ SAFETY GUARD: Destructive operation detected"
echo "Command: $COMMAND"
echo "Continue? (y/N):"
# Exit 1 to prompt user, or exit 0 if user confirms
fi
fi
# Freeze mode: write path locking
if [[ "$SAFETY_MODE" =~ (freeze|guard) ]]; then
FREEZE_PATHS="${FREEZE_PATHS:-}"
if [ -n "$FREEZE_PATHS" ]; then
# Check if command affects frozen paths
# Implementation depends on command parser
fi
fi
PreToolUse Write/Edit Hook Enhancement:
# Check if write target matches frozen paths
SAFETY_MODE="${SAFETY_GUARD_MODE:-off}"
TARGET_FILE="$1"
if [[ "$SAFETY_MODE" =~ (freeze|guard) ]]; then
FREEZE_PATHS="${FREEZE_PATHS:-}"
for frozen_path in ${FREEZE_PATHS//,/ }; do
if [[ "$TARGET_FILE" == "$frozen_path"* ]]; then
echo "$(date -Iseconds) BLOCK: Write to $TARGET_FILE (frozen path)" >> ~/.claude/safety-guard.log
echo "❌ SAFETY GUARD: Write blocked"
echo "Path: $TARGET_FILE is frozen"
exit 2 # Block operation
fi
done
fi
Action Logging
All interventions logged to ~/.claude/safety-guard.log with ISO timestamp, action (WARN/BLOCK), command, reason, outcome. Rotate at session start if >10KB.
Mode Activation
Via env vars: SAFETY_GUARD_MODE=guard and FREEZE_PATHS="/etc/,~/.ssh/". Or in agent frontmatter. Unset to disable.
Integration with Existing Hooks
Complements:
- block-destructive.sh: Safety guard extends this with logging and mode flexibility
- block-secrets-write.sh: Freeze mode adds pattern-based path protection
Does NOT replace:
- Existing hooks remain active
- Safety guard adds layer of user-controlled protection
- Hook order: existing blocks first, then safety guard warnings
Anti-Patterns
Always-on Guard mode: Guard mode for routine development slows workflow. Use Careful mode for normal work.
Freezing entire project: Freeze mode for / blocks all writes. Be specific with frozen paths.
Ignoring warnings repeatedly: If you're clicking through 10 safety warnings, the guard is misconfigured for your task.
No log review: Safety log accumulates warnings. Review monthly to identify risky patterns.
Mandatory Checklist
- Verify mode selected matches risk profile (Careful for dev, Guard for autonomous)
- Verify if Freeze or Guard: FREEZE_PATHS specified and tested
- Verify hook implementation added to PreToolUse: Bash, Write, Edit
- Verify log file location created at ~/.claude/safety-guard.log
- Verify warning format includes command, risk, and mode
- Verify blocked operations exit with code 2 (block)
- Verify environment variables documented in activation instructions