Self-Improvement Loop

March 5, 2026 · View on GitHub

Corrected → record lesson → prevent recurrence

Trigger Conditions

TriggerAction
User corrects a mistakeEvaluate and record (see Recording Workflow)
Same pattern appears 3+ timesPromote to formal rule or update existing rule
User requests consolidationReview and clean up lessons

Recording Workflow

sequenceDiagram
    participant U as User
    participant C as Claude
    participant L as Lesson Log
    U->>C: Corrects a mistake
    C->>C: Check: recurring pattern?
    alt One-off event
        C->>U: Skip (won't recur)
    else Recurring pattern
        C->>L: Create log if not exists
        C->>L: Append lesson (L<N>)
        C->>U: Confirm: Recorded as L<N>
    end
StepCheckOutcome
1. Correction receivedIs this a recurring pattern (not one-off)?If one-off → skip
2. Check lesson logDoes .claude/sd0x-dev-flow-lessons.md exist?If no → create it
3. Record lessonAppend using Lesson Format belowConfirm to user

Lesson Format

Append each lesson to the host project's lesson log (Tier B, see below):

### L<number> — <brief description>

- **Context**: What situation led to the mistake
- **Error pattern**: What was done (or not done) incorrectly
- **Correct approach**: What should be done instead
- **Prevention**: Detection signal or automation measure
- **Source**: Date + conversation summary (redacted, see below)

Source Field Redaction

KeepStrip
Date (2026-02-24)API keys, tokens, passwords
File paths, function namesInternal URLs with credentials
Error type descriptionUser personal data
Conversation topic summaryFull stack traces with secrets

Example: 2026-02-24 — Deleted rules/docs-writing.md instead of .claude/rules/docs-writing.md during duplicate cleanup

Two-Tier Model

TierPurposeLocationDefault
A (process)This rule — defines how to record and manage lessonsrules/self-improvement.mdAlways on
B (state)Lesson log — accumulated lessons per host project.claude/sd0x-dev-flow-lessons.mdAuto on first recurring correction

Tier B is auto-created on first recurring correction: the lesson log is created when a recurring pattern is detected. Namespace the file (sd0x-dev-flow-lessons.md) to avoid collisions with other plugins.

Management Rules

RuleDescription
Max 20 active lessonsActive = not yet promoted to a rule and not archived. Consolidate when exceeded: merge similar, archive rule-covered
Promote to ruleSame pattern 3+ times (manual judgment) → extract to rules/ → mark lesson as promoted
No one-off eventsOnly record patterns that can recur (see Recording Workflow)
No secrets/PIINever record tokens, keys, passwords, or personal data (see Source Redaction)
Periodic consolidationOn user request; run /codex-review-doc (or namespaced variant) after consolidation

Promotion Path

sequenceDiagram
    participant H as Host Project
    participant L as Local Rules
    participant P as Plugin Core
    H->>H: Lesson recorded (L<N>)
    H->>H: Same pattern 3+ times
    H->>L: Promote to .claude/rules/
    Note over L,P: Cross-project evidence (3+ projects, via issues/PRs)
    L->>P: Propose plugin core rule

Prohibited Behaviors

❌ Being corrected on a recurring pattern and not recording
❌ Vague lessons ("be careful with X" → must specify context and correct approach)
❌ Recording one-off events that won't recur
❌ Storing lessons in CLAUDE.md (use separate lesson log)
❌ Recording secrets, tokens, or PII in lesson source

Correct Behaviors

✅ "Recorded as L5 — always check file exists before deleting"
✅ "This is the 3rd time; promoting to a rule..."
✅ "Consolidating: L2 and L7 are same pattern, merging..."
✅ "One-off typo correction — not recording (won't recur)"

Relationship to Other Rules

RuleRelationship
fix-all-issues.mdComplementary: fix handles "repair now", lessons handle "remember why and prevent"
auto-loop.mdNo conflict: lessons do not affect the review/precommit loop
codex-invocation.mdLessons can capture Codex prompt design mistakes

.gitignore Guidance

Users should decide whether to track lessons in version control:

# Option A: Untracked (personal memory, not shared)
.claude/sd0x-dev-flow-lessons.md

# Option B: Tracked (shared team memory)
# Do not add to .gitignore