self-improving-agent

repository·master·Indexed 20 days ago

https://github.com/pskoett/self-improving-agent

A self-improvement skill designed for OpenClaw that captures learnings, errors, and feature requests to enable continuous improvement across agent sessions. It includes a self-improvement hook for error sweeping and a framework for promoting resolved learnings into reusable skills using a standardized SKILL.md template.

Tokens
9.7K
Snippets
24
Records
42
Agent score
72%

What's inside self-improving-agent

  1. Understand the session-end error sweep

    master

    The self-improvement hook implements a session-end error sweep rather than real-time per-command detection (as OpenClaw lacks per-tool-call events).

    How it works

    1. Trigger: When /new or /reset is issued, the session ends.
    2. Scan: The hook scans the transcript for patterns like Error:, command not found, Traceback, npm ERR!, or Permission denied.
    3. Log: Matches are appended to <workspace>/.learnings/ERRORS.md as pending entries.
    4. Triage: At the next agent:bootstrap, the agent is notified of pending triage entries to review, fix, or delete.

    Limitations

    • No real-time detection: Errors are only detected at the end of a session.
    • Opt-in: The sweep only runs if the .learnings/ directory exists.
    • Heuristic: It may produce false positives (e.g., prose containing the word "failed").
    • Redaction: Excerpts are redacted with best-effort rules; treat .learnings/ as potentially sensitive.
  2. Follow skill naming and file conventions

    master

    To maintain a consistent skill repository, adhere to the following conventions:

    Naming Conventions

    • Skill name: Use lowercase and hyphens for spaces.
      • docker-m1-fixes, api-timeout-patterns
      • Docker_M1_Fixes, APITimeoutPatterns
    • Description: Start with an action verb and mention the trigger condition.
      • ✅ "Handles Docker build failures on Apple Silicon. Use when builds fail with platform mismatch."
      • ❌ "Docker stuff"

    Directory Structure

    • SKILL.md: Required. The main documentation file.
    • scripts/: Optional. Contains executable code/helpers.
    • references/: Optional. Contains detailed documentation.
    • assets/: Optional. Contains templates or related assets.
  3. How the Self-Improvement Hook works

    master

    The self-improvement hook operates during two specific OpenClaw lifecycle events:

    1. During agent:bootstrap

    Before workspace files are injected, the hook:

    • Adds a reminder block to check .learnings/ for relevant entries.
    • Prompts the agent to log corrections, errors, and discoveries.
    • Includes a pending-triage note if auto-detected errors are awaiting review.

    2. During command:new or command:reset (Session End)

    When a session ends, the hook performs an error sweep:

    • Transcript Location: It looks for the session transcript at context.previousSessionEntry.sessionFile, falling back to <workspace>/sessions/<sessionId>.jsonl.
    • Pattern Matching: It scans the transcript against a fixed list of error patterns (e.g., Error:, command not found, Traceback, npm ERR!).
    • Error Logging: It appends a pending entry to <workspace>/.learnings/ERRORS.md.
    • Deduplication: Each entry is stamped with a deterministic Pattern-Key (e.g., deps.module-not-found) to allow for deduplication and recurrence counting.
  4. Format Error entries (ERR)

    master

    Error entries (prefixed with ERR-) document specific technical failures to facilitate debugging and pattern recognition.

    Key fields include:

    • Summary: High-level description of the error.
    • Error: The exact error message or stack trace.
    • Context: The command run, the environment (e.g., Apple Silicon), or the specific configuration that triggered the error.
    • Suggested Fix: The direct solution to resolve the error.
    • Metadata: Includes Reproducible (boolean) and Related Files.

    Example error types include platform mismatches (e.g., docker_build) and connectivity issues (e.g., api_timeout).

  5. Understand the status definitions for learnings

    master

    The project tracks development corrections, insights, knowledge gaps, and best practices using specific statuses. When reviewing or managing learnings, use the following status mapping:

    • pending: Not yet addressed.
    • in_progress: Actively being worked on.
    • resolved: Issue fixed or knowledge integrated.
    • wont_fix: Decided not to address (check the Resolution field for reasoning).
    • promoted: Elevated to core documentation files like SOUL.md, TOOLS.md, or AGENTS.md.
    • promoted_to_skill: Extracted as a reusable skill within the agent's skill set.
  6. Promote recurring patterns to system prompts

    master

    Promote recurring patterns into agent context/system prompt files (like SOUL.md, TOOLS.md, or AGENTS.md) when the following criteria are met:

    • Recurrence-Count >= 3
    • Seen across at least 2 distinct tasks
    • Occurred within a 30-day window

    Writing Promoted Rules: Write them as short prevention rules (e.g., "what to do before/while coding") rather than long incident write-ups.

  7. Use Pattern-Key Taxonomy for deduplication

    master

    The Pattern-Key is a stable, two-level key used to deduplicate semantically identical entries in log files. Using consistent keys ensures that Recurrence-Count and promotion rules function correctly.

    Format: area.symptom (exactly two levels, lowercase, hyphenated, e.g., deps.module-not-found).

    Rules for Keys:

    • Reuse before minting: Check for existing keys before creating new ones using: grep -rh "Pattern-Key:" .learnings/ | sort -u.
    • Keep symptoms generic: Do not include file names, versions, or hostnames in the symptom part of the key.
    • One key per manual entry: While auto-swept entries may have several, manual entries should use one.
    • Avoid generic sweep keys: Keys like runtime.error are considered "unclassified"; replace them with specific keys during triage.

    Taxonomy Reference:

    AreaScopeExample Keys
    apiExternal API/service behaviorapi.rate-limit, api.schema-mismatch
    authCredentials, tokens, scopesauth.token-expired, auth.missing-scope
    buildCompilation, bundling, CIbuild.type-error, build.missing-artifact
    configConfig files, env vars, settingsconfig.missing-env, config.invalid-json
    depsPackage managers, dependenciesdeps.module-not-found, deps.npm-error
    fsFilesystemfs.no-such-file, fs.permission-denied
    netNetwork connectivitynet.connection-refused, net.timeout
    runtimeLanguage/runtime errorsruntime.type-error, runtime.python-exception
    shellShell/CLI mechanicsshell.command-not-found, shell.nonzero-exit
    vcsGit and other version controlvcs.fatal-error, vcs.merge-conflict
    simplify / hardenCode-quality patternssimplify.dead_code, harden.input_validation
  8. Format Feature Request entries (FEAT)

    master

    Feature requests (prefixed with FEAT-) document requested capabilities for the system.

    Key fields include:

    • Requested Capability: What the user wants to achieve.
    • User Context: The motivation or workflow driving the request.
    • Complexity Estimate: simple, medium, or complex.
    • Suggested Implementation: Technical guidance on how to implement the feature.
    • Metadata: Includes Frequency and Related Features.
    • Resolution: If the feature is implemented, include the resolution date, the Commit/PR number, and notes.
  9. Use injected prompt files for agent behavior

    master

    OpenClaw uses specific workspace files to inject context and guide agent behavior. You can customize these files to define how your agents operate.

    AGENTS.md (Coordination)

    Defines multi-agent workflows and delegation patterns (e.g., Delegation Rules, Session Handoff).

    SOUL.md (Behavior)

    Defines behavioral guidelines and communication styles (e.g., Communication Style, Error Handling).

    TOOLS.md (Capabilities)

    Documents tool capabilities, integration quirks, and local configuration requirements.

  10. Format Learning entries (LRN)

    master

    Learning entries (prefixed with LRN-) are used to document corrections, knowledge gaps, or best practices discovered during development.

    Common fields include:

    • Logged: Timestamp of the entry.
    • Priority: high, medium, or low.
    • Status: pending, resolved, promoted, or promoted_to_skill.
    • Area: The functional area (e.g., tests, config, backend, infra).
    • Summary: A brief description of the learning.
    • Details: Contextual information about why the learning occurred.
    • Suggested Action: Concrete steps to prevent the issue or apply the knowledge.
    • Metadata: Includes Source, Related Files, and Tags.

    If a learning is promoted, it is moved to specialized files like TOOLS.md, AGENTS.md, or converted into a formal SKILL.md.

  11. Promote learnings to workspace memory

    master

    When a learning is broadly applicable (not a one-off fix), promote it to a workspace file so every session inherits it. This prevents recurring mistakes and documents project-specific conventions.

    Promotion Workflow

    1. Distill the learning into a concise rule or fact.
    2. Add to the appropriate section in the target file (create if needed).
    3. Update the original entry in .learnings/:
      • Change **Status**: pending**Status**: promoted.
      • Add **Promoted**: [Target File] (e.g., **Promoted**: SOUL.md).

    Promotion Targets

    TargetWhat Belongs There
    SOUL.mdBehavioral guidelines, communication style, principles
    TOOLS.mdTool capabilities, usage patterns, integration gotchas
    AGENTS.mdWorkflows, delegation patterns, automation rules

    Note: If the learning is specific to a project repo (not the workspace), promote it to that project's own agent file (e.g., its AGENTS.md) instead.

  12. Create a skill using the SKILL.md template

    master

    When extracting a new skill from a learning, use the SKILL.md template to ensure consistency. A skill is a reusable piece of knowledge or an executable helper derived from a resolved learning entry.

    Full Template Structure

    A comprehensive skill should include:

    • Frontmatter: Contains name (lowercase with hyphens) and description (starting with an action verb and mentioning triggers).
    • Quick Reference: A table mapping specific triggers/situations to actions.
    • Background: Context on why the knowledge matters.
    • Solution: A step-by-step guide and a tested code example.
    • Common Variations & Gotchas: Handling different scenarios and avoiding common mistakes.
    • Source: Metadata including the Learning ID, Original Category (correction, insight, knowledge_gap, or best_practice), and Extraction Date.

    Minimal Template

    For simple skills, use a reduced format:

    ---
    name: skill-name-here
    description: "What this skill does and when to use it."
    ---
    
    # Skill Name
    
    [Problem statement in one sentence]
    
    ## Solution
    
    [Direct solution with code/commands]
    
    ## Source
    
    - Learning ID: LRN-YYYYMMDD-XXX
    ---
    name: skill-name-here
    description: "Concise description of when and why to use this skill. Include trigger conditions."
    ---
    
    # Skill Name
    
    Brief introduction explaining the problem this skill solves and its origin.
    
    ## Quick Reference
    
    | Situation | Action |
    |-----------|--------|
    | [Trigger 1] | [Action 1] |
    | [Trigger 2] | [Action 2] |
    
    ## Background
    
    Why this knowledge matters. What problems it prevents. Context from the original learning.
    
    ## Solution
    
    ### Step-by-Step
    
    1. First step with code or command
    2. Second step
    3. Verification step
    
    ### Code Example
    
    ```language
    // Example code demonstrating the solution

    Common Variations

    • Variation A: Description and how to handle
    • Variation B: Description and how to handle

    Gotchas

    • Warning or common mistake #1
    • Warning or common mistake #2
    • Link to related documentation
    • Link to related skill

    Source

    Extracted from learning entry.

    • Learning ID: LRN-YYYYMMDD-XXX
    • Original Category: correction | insight | knowledge_gap | best_practice
    • Extraction Date: YYYY-MM-DD