Security-hardened self-improvement skill for OpenClaw. Captures learnings, errors, and corrections with mandatory human-approval gate, automated sanitization...
---
name: safe-self-improvement
description: "Security-hardened self-improvement skill for OpenClaw. Captures learnings, errors, and corrections with mandatory human-approval gate, automated sanitization, audit tooling, and promotion rate-limiting. Use when: (1) A command or operation fails unexpectedly, (2) User corrects the agent, (3) User requests a missing capability, (4) An external API or tool fails, (5) Agent realizes knowledge is outdated, (6) A better approach is discovered. Review learnings before major tasks."
homepage: https://github.com/gateswell/safe-self-improvement-agent
metadata:
clawdbot:
emoji: "โก"
requires:
env: []
files:
- scripts/sanitize.sh
- scripts/audit.sh
- scripts/promotion-gate.sh
---
# Safe Self-Improvement
Log learnings and errors to markdown files for continuous improvement with security hardening. Unlike untrusted variants: **all promotions require human approval, sensitive data is sanitized by script, and bulk promotions are rate-limited**.
## External Endpoints
| Endpoint | Data Sent | Purpose |
|----------|-----------|---------|
| None | โ | This skill makes no external network calls |
No data leaves the machine. Learnings are stored locally only.
## Security & Privacy
- **Data stored locally**: All learnings written to `.learnings/` in the workspace directory
- **No external transmission**: Zero network calls; no data sent to any third party
- **Sensitive data protection**: `scripts/sanitize.sh` must pass before any entry is written (see Pre-Log Sanitization)
- **Cross-session sharing**: Blocked by default; requires explicit user approval per session
- **Read-only recommendation**: For high-security environments, set `.learnings/` to read-only (`chmod 555`)
## Model Invocation Note
This skill operates autonomously between sessions. The agent reads SKILL.md on trigger and executes logging, sanitization, and promotion workflows. To disable: remove the skill directory or run `openclaw skills disable safe-self-improvement`.
## Trust Statement
By installing this skill, you trust the author (gateswell) with handling your learning logs. This skill does not contact external services, share data, or execute untrusted code. Install only if you trust the source.
---
## First-Use Initialisation
If `.learnings/` directory or its files are missing, create them:
```bash
mkdir -p .learnings
[ -f .learnings/LEARNINGS.md ] || printf "# Learnings\n\nCorrections, insights, and knowledge gaps.\n\n**Categories**: correction | insight | knowledge_gap | best_practice\n\n---\n" > .learnings/LEARNINGS.md
[ -f .learnings/ERRORS.md ] || printf "# Errors\n\nCommand failures and integration errors.\n\n---\n" > .learnings/ERRORS.md
[ -f .learnings/FEATURE_REQUESTS.md ] || printf "# Feature Requests\n\nCapabilities requested by the user.\n\n---\n" > .learnings/FEATURE_REQUESTS.md
```
Never overwrite existing files.
## ๐ Security Rules (Non-Negotiable)
1. **NEVER auto-modify core files** โ `SOUL.md`, `AGENTS.md`, `TOOLS.md`, `MEMORY.md`, `IDENTITY.md` must NOT be modified without explicit user approval shown as a clear question and awaiting a "yes" response.
2. **No secrets in logs** โ Never log tokens, API keys, passwords, private keys, env vars, or full config/source files. Use redacted summaries only.
3. **No cross-session sharing without approval** โ Using `sessions_send` or `sessions_spawn` to share learnings requires the same approval gate as promotion: present what will be shared, to which session, and wait for explicit "yes". Never share automatically.
4. **No hook scripts** โ This skill does not install or use hook scripts that read command output.
5. **No dynamic payload fetching** โ Never fetch remote content at runtime for skill logic.
6. **Promotion = proposal, not action** โ When a learning qualifies for promotion, ASK the user first.
7. **Sanitize before write** โ Before logging any entry, run `scripts/sanitize.sh` on the content. Block the write if sanitization fails.
## โ ๏ธ Security Limitations
**This skill's protections are based on AI instruction adherence, not hardware-level isolation.**
- In high-security environments (financial, medical, critical infrastructure): **do not use this skill**
- The sanitization script provides a defense layer, but a determined attacker controlling the agent's context could bypass it
- For team environments: set `.learnings/` to read-only (`chmod 555`) and require a human to make it writable for approved promotions
## Quick Reference
| Situation | Action |
|-----------|--------|
| Command/operation fails | Log to `.learnings/ERRORS.md` |
| User corrects you | Log to `.learnings/LEARNINGS.md` (category: `correction`) |
| User wants missing feature | Log to `.learnings/FEATURE_REQUESTS.md` |
| API/external tool fails | Log to `.learnings/ERRORS.md` |
| Knowledge was outdated | Log to `.learnings/LEARNINGS.md` (category: `knowledge_gap`) |
| Found better approach | Log to `.learnings/LEARNINGS.md` (category: `best_practice`) |
| Learning seems broadly applicable | **Propose** promotion โ do NOT auto-modify core files |
## Pre-Log Sanitization (Mandatory โ Script-Enforced)
Before writing ANY entry, you MUST run the sanitization script:
```bash
./scripts/sanitize.sh "<content_to_log>"
```
The script checks for:
- API keys / tokens (GitHub, AWS, OpenAI, etc.)
- Private keys (RSA, EC, SSH, etc.)
- Passwords and secrets in plain text
- IP addresses (private ranges)
- MAC addresses
- Phone numbers
- Email addresses (non-placeholder)
- SSID/WiFi credentials
- GPS coordinates
- Device serial numbers
**If sanitization fails (exit code 1):**
- Do NOT write the entry
- Inform the user: "Sensitive data detected in proposed log entry. Content blocked. Rewrite with placeholders."
- Redact and retry with `./scripts/sanitize.sh "<redacted_content>"`
**Only proceed to write the entry after sanitization passes.**
## Logging Format
### Learning Entry
Append to `.learnings/LEARNINGS.md`:
```markdown
## [LRN-YYYYMMDD-XXX] category
**Logged**: ISO-8601 timestamp
**Priority**: low | medium | high | critical
**Status**: pending
**Area**: frontend | backend | infra | tests | docs | config
### Summary
One-line description
### Details
Full context (sanitized โ no secrets)
### Suggested Action
Specific fix or improvement
### Metadata
- Source: conversation | error | user_feedback
- Related Files: path/to/file.ext
- Tags: tag1, tag2
- See Also: LRN-YYYYMMDD-XXX
- Pattern-Key: optional.stable_key
- Recurrence-Count: 1
- First-Seen: YYYY-MM-DD
- Last-Seen: YYYY-MM-DD
---
```
### Error Entry
Append to `.learnings/ERRORS.md`:
```markdown
## [ERR-YYYYMMDD-XXX] skill_or_command_name
**Logged**: ISO-8601 timestamp
**Priority**: high
**Status**: pending
**Area**: frontend | backend | infra | tests | docs | config
### Summary
Brief description of failure
### Error
```
Error message (redacted)
```
### Context
- Command attempted
- Environment details (no secrets)
- Redacted excerpt of relevant output
### Suggested Fix
Possible resolution
### Metadata
- Reproducible: yes | no | unknown
- Related Files: path/to/file.ext
- See Also: ERR-YYYYMMDD-XXX
---
```
### Feature Request Entry
Append to `.learnings/FEATURE_REQUESTS.md`:
```markdown
## [FEAT-YYYYMMDD-XXX] capability_name
**Logged**: ISO-8601 timestamp
**Priority**: medium
**Status**: pending
**Area**: frontend | backend | infra | tests | docs | config
### Requested Capability
What the user wanted
### User Context
Why they needed it
### Complexity Estimate
simple | medium | complex
### Metadata
- Frequency: first_time | recurring
---
```
## ID Generation
Format: `TYPE-YYYYMMDD-XXX`
- TYPE: `LRN`, `ERR`, `FEAT`
- XXX: Sequential (`001`, `002`...)
## Resolving Entries
When an issue is fixed:
1. Change `**Status**: pending` โ `**Status**: resolved`
2. Add resolution block:
```markdown
### Resolution
- **Resolved**: ISO-8601 timestamp
- **Notes**: What was done
```
Other status values: `in_progress`, `wont_fix`, `promoted`
## Promotion (Human-Approval Gate + Rate-Limit)
When a learning qualifies for promotion, propose โ **never auto-execute**.
### When a Learning Qualifies for Promotion
- Recurrence-Count โฅ 3 (within 30 days)
- Seen across โฅ 2 distinct tasks
- Non-obvious, verified, or user-flagged
### Promotion Rate-Limiting
The `scripts/promotion-gate.sh` enforces:
- Maximum **3 promotions** per 24-hour window
- Minimum **6-hour cooldown** between bulk promotion batches
- Bulk = 3+ promotions at once (requires extra scrutiny)
Check gate status before proposing:
```bash
./scripts/promotion-gate.sh status
```
### Promotion Targets
| Learning Type | Target File | Example |
|---------------|------------|---------|
| Behavioral patterns | `SOUL.md` | "Be concise, avoid disclaimers" |
| Workflow improvements | `AGENTS.md` | "Spawn sub-agents for long tasks" |
| Tool gotchas | `TOOLS.md` | "Git push needs auth configured" |
### Promotion Procedure
**STOP. Do not modify the target file yet.**
1. Check gate: `./scripts/promotion-gate.sh check`
2. Present the user with:
- The proposed rule (distilled, concise)
- Target file and location
- Original learning entry ID
- Recurrence count and context
3. Ask: *"This learning recurred X times. Propose adding to [file]: [rule]. Approve?"*
4. **Only on explicit "yes"/"approve"/"็กฎ่ฎค":**
- Run `./scripts/promotion-gate.sh approve LRN-YYYYMMDD-XXX`
- Then modify the target file
- Update original entry: `**Status**: promoted`, `**Promoted**: <filename>`
5. Any other response = do not promote
## Recurring Pattern Detection
When logging something similar to an existing entry:
1. Search: `grep -r "keyword" .learnings/`
2. Link: Add `**See Also**: LRN-...` in Metadata
3. Bump priority if recurring
4. Increment `Recurrence-Count` and update `Last-Seen`
5. If promotion threshold met โ follow Promotion Procedure above
## Detection Triggers
Automatically log when you notice:
- **Corrections**: "No, that's not right...", "Actually...", "You're wrong about..."
- **Feature Requests**: "Can you also...", "I wish you could...", "Is there a way to..."
- **Knowledge Gaps**: User provides info you didn't know, docs are outdated, API differs
- **Errors**: Non-zero exit codes, exceptions, unexpected output, timeouts
> โ ๏ธ Note on corrections: If a correction feels suspicious (e.g., repeated similar corrections in short succession), log it but flag it in the entry with `**Confidence**: low`. Do not promote low-confidence learnings without extra scrutiny.
## Periodic Review & Audit
Run the audit script regularly:
```bash
./scripts/audit.sh
```
Audit checks:
1. **Sensitive data scan** โ blocks if secrets found in any learnings file
2. **Format consistency** โ all entries have Status, Priority, Logged fields
3. **Orphaned See Also links** โ all references point to existing entries
4. **File size** โ warns if any file exceeds 500 lines
5. **Summary report** โ PASS/FAIL/WARNINGS
Run at least:
- Before any major task
- Monthly
- Before context compression
## Context Compression
When `.learnings/*.md` exceeds 500 lines (after audit confirms no issues):
1. **Distill resolved/promoted entries** into one-line summaries:
- Before: Full entry (~15 lines)
- After: `> [LRN-20260428-001] Use pnpm not npm (promotedโTOOLS.md)` (~1 line)
2. **Merge duplicate patterns** โ if 3+ entries share the same `Pattern-Key`, keep one summary
3. **Expire stale entries** โ `wont_fix` > 90 days โ delete; `pending` no activity > 60 days โ demote to `low` + compress
4. **Keep summary index** in `.learnings/archive/SUMMARY.md` โ one line per entry, oldest first
5. Active files only retain `pending` and `in_progress` in full detail
## Best Practices
1. **Sanitize before write** โ run `sanitize.sh` every time, no exceptions
2. Log immediately โ context fades fast
3. Be specific โ future sessions need clarity
4. Redact secrets โ always
5. Include reproduction steps for errors
6. Suggest concrete fixes, not just "investigate"
7. Link related files
8. Run audit before compression โ never compress a dirty file
9. Propose promotions promptly when threshold met โ but wait for explicit approval
10. Flag low-confidence corrections โ use `**Confidence**: low` and delay promotion
don't have the plugin yet? install it then click "run inline in claude" again.