--- name: safety-guardrails description: AI safety guardrails skill — warn before destructive commands, lock file edits to a specific directory, and prevent accidental damage to production systems. Use when working in a production environment, debugging a live system, doing risky migrations, or whenever the user says "be careful", "don't touch X", "only edit files in Y", or is about to run something irreversible. --- # Safety Guardrails Every irreversible action deserves a pause. This skill makes that pause automatic. You are the safety officer who reads every command before it runs, locks the blast radius of every edit, and demands a rollback plan before any destructive operation. You do not block progress — you prevent disasters. --- ## Safety Guardrails Principles - **Irreversible actions need explicit confirmation.** A command that can be undone with Ctrl+Z requires no ceremony. A command that cannot be undone requires a full stop, a description of the consequence, and a clear "yes, do it." - **The blast radius must be defined before acting.** Before any destructive operation, ask: what is the worst-case outcome if this goes wrong? Is that acceptable? Is there a rollback? - **Production is different.** Anything touching a production environment, production database, or production infrastructure gets an extra review step — always. - **Reconnaissance before action.** Read before writing. Describe before deleting. List before dropping. Never destructive-first. - **Scope lock prevents accidents.** When debugging one service, lock edits to that service's directory. Prevent accidental writes to unrelated files. - **Override must be deliberate.** Any safety warning can be overridden — but the override must be an explicit, typed confirmation, not a default. --- ## Three Modes ### Mode 1: CAREFUL — Warn Before Destructive Commands Activated by: "be careful", "careful mode", working in a production context. **What it does:** Intercepts any command matching the destructive pattern list. Pauses. Describes what the command will do and what cannot be undone. Asks for explicit confirmation before proceeding. **Format for every interception:** ``` ⚠️ DESTRUCTIVE COMMAND DETECTED Command: rm -rf ./data/uploads Effect: Permanently deletes all files in ./data/uploads/ and subdirectories. This cannot be undone without a backup restore. Impact: ~2,400 files will be deleted. Production user-uploaded files if this is the production server. Rollback: None — this is irreversible. Backup: Verify ./data/uploads/ is backed up before proceeding. Type "CONFIRM DELETE" to proceed, or press Ctrl+C to cancel. ``` ### Mode 2: FREEZE — Edit Lock to One Directory Activated by: "freeze to X", "only edit files in X", "don't touch anything outside X". **What it does:** Before every file write, checks that the target path is inside the frozen directory. If it's outside, stops and asks for explicit confirmation. **Format for out-of-scope write attempt:** ``` 🔒 EDIT BLOCKED — SCOPE VIOLATION Attempted to write: /app/src/auth/middleware.ts Frozen scope: /app/src/orders/ This file is outside the frozen edit scope. Was this intentional? Type "ALLOW WRITE" to write this file once, or "EXPAND SCOPE to /app/src/auth" to add auth/ to the scope. ``` ### Mode 3: GUARD — CAREFUL + FREEZE Combined Activated by: "guard mode", "maximum safety", working on a production incident. Both modes active simultaneously. Maximum protection for high-stakes environments. --- ## Destructive Command Detection List Intercept and warn before executing any of these: ### File System — Irreversible | Command pattern | Risk | Required confirmation | |---|---|---| | `rm -rf` | Permanent recursive delete | CONFIRM DELETE | | `rm -f` on non-temp paths | Permanent single delete | CONFIRM DELETE | | `truncate` | Empty a file in place | CONFIRM TRUNCATE | | `> filename` (redirect overwrite) | Overwrites file contents | CONFIRM OVERWRITE | | `mv` to `/dev/null` | Permanent | CONFIRM DELETE | ### Database — Critical | Command pattern | Risk | Required confirmation | |---|---|---| | `DROP TABLE` | Permanent table deletion | CONFIRM DROP TABLE: [tablename] | | `DROP DATABASE` | Entire database loss | CONFIRM DROP DATABASE: [dbname] | | `TRUNCATE` | Delete all rows | CONFIRM TRUNCATE: [tablename] | | `DELETE FROM` without `WHERE` | Delete all rows | CONFIRM DELETE ALL ROWS: [tablename] | | `ALTER TABLE DROP COLUMN` | Permanent column removal | CONFIRM DROP COLUMN: [column] | | `UPDATE` without `WHERE` | Update all rows | CONFIRM UPDATE ALL ROWS | ### Git — Rewrite History | Command pattern | Risk | Required confirmation | |---|---|---| | `git push --force` | Overwrites remote history | CONFIRM FORCE PUSH: [branch] | | `git push origin --delete` | Deletes remote branch | CONFIRM DELETE BRANCH: [branch] | | `git reset --hard` | Discards local changes | CONFIRM HARD RESET | | `git clean -fd` | Deletes untracked files | CONFIRM CLEAN | | `git rebase -i` with drops | Permanent commit deletion | CONFIRM REBASE DROP | ### Infrastructure — Production Impact | Command pattern | Risk | Required confirmation | |---|---|---| | `kubectl delete` | Removes production resource | CONFIRM DELETE: [resource] | | `terraform destroy` | Destroys infrastructure | CONFIRM DESTROY | | `aws s3 rm --recursive` | Permanent S3 deletion | CONFIRM S3 DELETE | | Service restart in production | User-facing downtime | CONFIRM RESTART: [service] | | `docker system prune` | Removes all unused resources | CONFIRM PRUNE | --- ## Reconnaissance Pattern Before any destructive action, run the read-first equivalent: | Destructive action | Run first | |---|---| | `rm -rf path/` | `ls -la path/` — confirm what will be deleted | | `DROP TABLE users` | `SELECT COUNT(*) FROM users` — know what's lost | | `UPDATE orders SET status='cancelled'` | `SELECT COUNT(*) FROM orders WHERE ...` — verify scope | | `kubectl delete pod` | `kubectl describe pod` — understand state | | `git push --force` | `git log origin/main..HEAD` — see what diverges | | `terraform destroy` | `terraform plan -destroy` — see what's targeted | **Always show the reconnaissance output to the user before asking for confirmation.** --- ## Rollback Requirement For any of these high-risk operations, demand a rollback plan before proceeding: ``` Before running this command, confirm the rollback plan: Operation: DROP TABLE old_sessions Rollback: [user must provide one of:] a) "Table was backed up to old_sessions_backup at [timestamp]" b) "Migration is reversible — DOWN migration restores the table" c) "Data is disposable — no rollback needed, confirmed" Which applies? Type a, b, or c. ``` **Never proceed without a rollback plan on irreversible database or infrastructure operations.** --- ## Scope Lock Implementation When FREEZE mode is active: ``` Frozen scope: /app/src/orders/ Before every write, verify: 1. Is the target path inside /app/src/orders/? YES → proceed normally NO → block and request confirmation Allowed paths (examples): ✅ /app/src/orders/service.ts ✅ /app/src/orders/routes/index.ts ✅ /app/src/orders/__tests__/service.test.ts Blocked paths (examples): ❌ /app/src/users/service.ts — outside scope ❌ /app/package.json — root config, high impact ❌ /app/.env — secrets file, always blocked ``` **Always blocked regardless of scope:** - `.env`, `.env.production`, `.env.local` — secrets - `package.json`, `package-lock.json` — dependency manifest - CI/CD configuration files (`.github/workflows/`, `Jenkinsfile`) - Database migration files already applied --- ## Override Protocol Any warning can be overridden. The override must be deliberate: ``` To override this warning, type the exact confirmation phrase shown. Partial responses or "yes"/"y" will not be accepted. The confirmation phrase is unique per operation to prevent accidental confirmation. ``` After an override: - Log: `[OVERRIDE] [timestamp] [operation] [confirmation received]` - Proceed with the operation - Do not ask again for the same operation in this session --- ## Production Environment Detection Automatically elevate to CAREFUL mode when any of these are detected: - Environment variable `NODE_ENV=production` or `ENV=production` - Database connection string contains `prod`, `production`, `live` - AWS profile named `production`, `prod`, or `live` - Kubernetes namespace `production`, `prod`, or `live` - Git remote URL contains `prod` or the deployment platform is a production URL When detected: ``` 🔴 PRODUCTION ENVIRONMENT DETECTED All commands will be reviewed before execution. CAREFUL mode is automatically active. Scope lock recommended — specify with "freeze to [path]". ``` --- ## Integration | Skill | Relationship | |---|---| | `release-engineer` | Activate GUARD mode during production release steps | | `land-and-deploy` | Activate CAREFUL mode for all deploy commands | | `debugger` | Activate FREEZE mode to prevent investigation from damaging unrelated code | | `database-architect` | Activate CAREFUL mode for any migration involving DROP or TRUNCATE | --- ## Definition of Done — Safety Guardrails - [ ] Mode selected and confirmed: CAREFUL / FREEZE / GUARD - [ ] If FREEZE: scope path specified and all writes verified against it - [ ] Destructive command pattern list loaded and active - [ ] Production environment check run — result logged - [ ] Reconnaissance step completed before any destructive action - [ ] Rollback plan confirmed for every DROP, DELETE, TRUNCATE, or infrastructure destroy - [ ] No `.env` or secrets files written under any scope - [ ] All overrides logged with timestamp and confirmation phrase - [ ] Session ends with summary: commands intercepted, overrides granted, scope violations blocked