SkillOpt: Agentic Skill Optimization via Reflective Training Loops

repository·main·Indexed 12 days ago

https://github.com/microsoft/skillopt

An executive strategy for self-evolving agent skills that treats skill documents as trainable parameters. It enables agents to improve performance through a structured optimization loop (rollout, reflect, aggregate, select, update, evaluate) without modifying LLM weights. Includes SkillOpt-Sleep, a deployment-time companion for local coding agents to automate the review and consolidation of recurring tasks into long-term memory and skills.

Tokens
81.2K
Snippets
147
Records
288
Agent score
93%

What's inside SkillOpt

  1. Overview of SkillOpt-Sleep

    main

    SkillOpt-Sleep is a deployment-time companion (currently in preview) designed to give local coding agents a nightly 'sleep cycle'. It automates the process of reviewing past agent sessions, replaying recurring tasks using your API budget, and consolidating learnings into validated long-term memory and skills.

    Key characteristics:

    • No weight training required: It does not require model fine-tuning.
    • Zero dependency: The skillopt_sleep/ engine is independent of the core skillopt/ package.
    • Review-driven: Improvements are staged for your review behind a validation gate before being adopted.
    • Workflow: It harvests transcripts (e.g., from Claude Code, Cursor, VS Code Copilot) $\rightarrow$ mines recurring tasks $\rightarrow$ replays tasks via a configured backend $\rightarrow$ consolidates via reflection and bounded edits $\rightarrow$ gates on held-out tasks $\rightarrow$ stages a proposal for user adoption.
  2. Understand the SkillOpt-Sleep OpenClaw reference adaptation

    main

    The skillopt-sleep directory in the plugins/openclaw path is a reference-only adaptation, not a production-ready integration. It is intended for developers to study or use as a template when porting a custom backend (like DeepSeek) to the SkillOpt-Sleep cycle within an OpenClaw environment.

    Warning: Do not run or schedule the files in this directory as-is. They contain machine-specific absolute paths, outdated staging logic, and non-portable backend registrations.

    To test the core SkillOpt-Sleep functionality safely without using this reference, use the supported shared CLI:

    python -m skillopt_sleep --backend mock
  3. Use SkillOpt-Sleep for agent self-evolution

    main

    SkillOpt-Sleep is a deployment-time companion that allows a Codex agent to undergo a 'sleep cycle' to self-improve. It reviews past local sessions, mines recurring tasks, re-runs them through a selected backend, and proposes improvements to skills (SKILL.md) and project memory (CLAUDE.md).

    Key Features:

    • Validation Gating: By default, changes are only accepted if they improve a held-out validation score.
    • Staging Workflow: Proposed changes are written to <project>/.skillopt-sleep/staging/<date>/ and do not affect live files until explicitly adopted.
    • No Model Training: It optimizes via skill and memory edits, not model-weight training.

    Important Note on Targets:

    • The engine does not write AGENTS.md.
    • To target a specific Codex skill, you must use the --target-skill-path flag (e.g., .agents/skills/<name>/SKILL.md).
    • To prevent updates to CLAUDE.md, set "evolve_memory": false in ~/.skillopt-sleep/config.json.
    bash
    # Example: Running a dry-run to see proposed improvements
    export SKILLOPT_SLEEP_REPO=/path/to/SkillOpt
    TARGET_SKILL=.agents/skills/example/SKILL.md
    bash "$SKILLOPT_SLEEP_REPO/plugins/run-sleep.sh" dry-run --project "$(pwd)" \
      --source codex --target-skill-path "$TARGET_SKILL" --backend mock
  4. Overview of SkillOpt Sleep (Claude Code Plugin)

    main

    What is SkillOpt Sleep?

    skillopt-sleep is a Claude Code plugin designed to provide a nightly sleep cycle for local Claude agents. It enables an agent to evolve its capabilities without model-weight training by consolidating short-term experiences into long-term memory and skills.

    Core Workflow

    1. Harvest: Extracts the day's Claude Code session transcripts from local storage (~/.claude).
    2. Mine: Converts transcripts into discrete task records with verifiable outcomes.
    3. Replay / "Dream": Re-runs those tasks offline using the user's existing API budget to simulate performance.
    4. Optimize: Runs the SkillOpt optimizer loop (reflect $\rightarrow$ bounded edit $\rightarrow$ held-out gate) to update text-based knowledge.
    5. Validate & Stage: Only changes that pass a validation gate are moved to a review staging area. The user must manually approve changes before they are adopted into live configuration files.

    Key Benefits

    • Weight-free evolution: Improves performance via text-based updates to CLAUDE.md and SKILL.md rather than fine-tuning.
    • Regression protection: Uses a validation gate to ensure nightly updates actually improve performance on held-out tasks.
    • Safety: Follows a "stage $\rightarrow$ approve $\rightarrow$ adopt" model, ensuring user configuration is never silently overwritten.
  5. What is a Skill Document?

    main
    A skill document is a Markdown file that acts as the 'prompt weights' for an agent. Instead of neural network parameters, it encodes task-specific knowledge, strategies, and instructions in natural language. SkillOpt iteratively optimizes this document through training to improve agent performance.
  6. What is SkillOpt-Sleep for Cursor?

    main

    SkillOpt-Sleep is a plugin that reviews recent local Cursor sessions to mine recurring tasks and propose bounded improvements to a project's Cursor skill.

    Key Characteristics:

    • Gated Proposals: A proposal is only accepted if it improves the held-out score.
    • Staged Workflow: Improvements are staged for review; nothing is applied to your live files until you explicitly run the adopt command.
    • No Model Training: It does not perform model-weight training; it evolves skills through task replay and reflection.
    • Safety: It uses a validation-gated engine to ensure proposed edits are beneficial.
  7. Validate optimizer JSON payloads

    main

    When using an LLM to merge or rank skill edits, you must validate the returned JSON before passing it back into SkillOpt to prevent silent failures.

    For edit payloads, verify:

    • The response is a JSON object.
    • edits is a non-empty list.
    • Every edit is an object.
    • Every edit has an allowed operation.
    • Required fields (e.g., content or target) are present for the specific operation.

    For ranking payloads, verify:

    • selected_indices exists.
    • Indices are integers and are unique.
    • Indices are within the candidate edit range.
    • The selected count does not exceed the edit budget.

    Error Handling: If validation fails, retry with a compact prompt containing the schema error. If retries fail, raise an explicit error.

  8. Select the correct library for Excel manipulation

    main

    When manipulating spreadsheets, choose your library based on the required outcome:

    Use caseLibraryNotes
    Preserve formulas, formatting, named rangesopenpyxlBest for structure-preserving read/write
    Bulk data transformation, aggregation, sortingpandasUse for logic, but write back with openpyxl to avoid data loss
    Simple cell read/writeopenpyxl

    CRITICAL WARNING: pandas.to_excel() silently destroys existing formulas and named ranges. If you are working with a spreadsheet that contains formulas, you must use openpyxl.save() to write the final result.

  9. The Meta-Skill Prompt Structure for Optimizer-Coaches

    main

    In the SkillOpt system, the meta_skill.md prompt defines the role of an optimizer-coach. The coach's purpose is to generate high-signal, optimizer-side memory that guides future optimizer calls in producing better skill edits.

    Input Data

    The coach receives four specific pieces of context:

    1. The previous epoch's last-step skill.
    2. The current epoch's last-step skill.
    3. A longitudinal comparison of the same sampled tasks performed under both skills.
    4. The previous optimizer meta skill (if available).

    Objective

    The goal is to produce a concise meta-skill that improves future optimizer behavior in specific stages: failure analysis, success analysis, patch merging, and edit ranking. The meta-skill should capture environment-specific patterns such as:

    • Effective vs. ineffective edit types.
    • Optimal levels of abstraction for rules.
    • Prioritized failure-repair patterns.
    • Regression risks to guard against.

    Output Format

    The coach must respond ONLY with a valid JSON object containing two keys:

    • reasoning: A brief reflection on which editing directions helped or hurt.
    • meta_skill_content: Compact, optimizer-side guidance for future edit generation and selection.

    Critical Constraints

    • Target Audience: Address the FUTURE OPTIMIZER, not the target agent.
    • Content Focus: Focus on editing strategies and skill updates, not task instructions.
    • Evidence-Based: Use evidence from the epoch comparison rather than generic advice.
    • Compactness: Prefer a few durable principles over verbose text.
    {
      "reasoning": "<brief reflection on what editing directions helped or hurt>",
      "meta_skill_content": "<compact optimizer-side guidance for future edit generation and selection>"
    }
  10. Data Privacy and Security Boundaries in SkillOpt-Sleep

    main

    When using SkillOpt-Sleep, it is critical to understand the data boundaries, especially when using real backends instead of the mock backend.

    General Security Warning

    Harvesting is local and read-only. However, a real backend sends truncated excerpts from harvested sessions and derived tasks to your selected model provider for mining, replay, judging, and reflection. Outbound prompts are not currently guaranteed to be secret-free.

    Recommended Workflow for Sensitive Projects:

    1. Harvest to a task file.
    2. Inspect and redact the file manually.
    3. Mark the file with "reviewed": true.
    4. Replay that file using a real backend.

    Source-Specific Data Handling

    • Cursor: Reads local user/assistant message text, explicit turn errors, and tool names. It excludes tool arguments, tool outputs, and non-message records. It uses cursor-agent for replays in an empty temporary workspace with project files denied.
    • VS Code Copilot: Reads local user-entered prompts, visible assistant Markdown, and tool names. It excludes system notifications, reasoning, tool inputs/outputs, rendered context, and account/model metadata.
    • Pi: Reads sessions from ~/.pi/agent/sessions. It retains user/assistant text, tool names, and lexical feedback. It excludes thinking, tool arguments, tool outputs, and images. The absolute project cwd from the session header is retained and may be sent to providers.

    Evidence Logging

    By default, each stateful night writes a local evidence.jsonl file under the project staging tree (or the Sleep state directory during dry-runs). This log contains best-effort-redacted, per-field-truncated copies of prompts and replies from the miner, replay, judge, and reflection stages.

    Configuration Options:

    • To disable evidence logging: Set "evidence_log": false.
    • To disable secret redaction (not recommended): Set "redact_secrets": false.
  11. Understand the SkillOpt mental model via the Deep Learning analogy

    main

    SkillOpt is architected to mirror the structure of training neural networks, where natural-language prompts are optimized instead of model weights. Understanding this mapping allows machine learning practitioners to apply familiar tuning intuitions to SkillOpt's hyperparameters and lifecycle.

    Core Mapping

    Deep Learning ConceptSkillOpt CounterpartDescription
    Model weightsSkill document (Markdown)The target being optimized
    Forward passRolloutTarget executes tasks using current skill
    Loss functionTask evaluatorScores task execution quality
    BackpropagationReflectOptimizer analyzes failures and generates edit patches
    GradientsEdit patchesProposed changes to the skill
    Gradient aggregationPatch aggregationMerging similar edits
    Gradient clippingEdit selectionCapping the maximum number of edits per step
    Learning ratelearning_rateMax number of edits applied per step
    LR schedulerlr_schedulerEdit-budget schedule (e.g., cosine, linear, constant, or autonomous)
    SGD stepSkill updateApplying selected patches to the document
    Validation setSelection splitGating mechanism that checks improvement before accepting
    Training stepStepA single cycle of: Rollout $\rightarrow$ Reflect $\rightarrow$ Update
    EpochEpochA full pass involving slow updates and meta memory
    MomentumSlow updateLongitudinal comparison at epoch boundaries
    Meta-learningMeta skillCross-epoch optimizer strategy memory
    Batch sizebatch_sizeNumber of tasks sampled per rollout
    Data parallelismanalyst_workersNumber of parallel reflection workers
    Training setTrain splitItems used for rollouts
    Test setTest splitHeld-out final evaluation
    CheckpointingSkill snapshotsSaved after each accepted step
    Transfer learningSeed skill / cross-benchmark initStarting from a pre-trained skill
  12. Task types and workflows in ALFWorld

    main

    The ALFWorld skill supports several distinct task patterns. Agents should follow these specific sequences:

    Pick & Place

    Goal: Put object X in/on receptacle Y. Workflow: Find X $\rightarrow$ take X $\rightarrow$ go to Y $\rightarrow$ put X in/on Y.

    Pick Two & Place

    Goal: Put two instances of X in/on Y. Workflow: Find X1 $\rightarrow$ take $\rightarrow$ place $\rightarrow$ find X2 $\rightarrow$ take $\rightarrow$ place. Note: Choose one destination receptacle instance once it is opened/usable and remember it as the target. Both objects must go into that same instance. If objects are split across different receptacles, consolidate them into the chosen target.

    Examine in Light

    Goal: Examine object X under a desklamp. Workflow: Find X $\rightarrow$ take X $\rightarrow$ find desklamp $\rightarrow$ use desklamp. Constraint: While holding X where a desklamp is visible, use the desklamp directly; do not attempt to place X on the lamp first.

    Transformation Tasks

    • Clean & Place: Find X $\rightarrow$ take X $\rightarrow$ go to sink $\rightarrow$ clean X $\rightarrow$ go to Y $\rightarrow$ put X.
    • Heat & Place: Find X $\rightarrow$ take X $\rightarrow$ go to microwave $\rightarrow$ heat X $\rightarrow$ go to Y $\rightarrow$ put X.
    • Cool & Place: Find X $\rightarrow$ take X $\rightarrow$ go to fridge $\rightarrow$ cool X $\rightarrow$ go to Y $\rightarrow$ put X.