AgentSkillsCN

compound

从当前会话中汲取经验教训,为未来的知识积累与迭代奠定基础。

SKILL.md
--- frontmatter
name: compound
description: Capture learnings from current session for future knowledge compounding
argument-hint: "[optional: specific topic or learning to capture]"
model: claude-sonnet-4-5
context: fork
agent: general-purpose
allowed-tools:
  - Bash(*)
  - Read
  - Write
  - Grep
  - Glob
extended-thinking: true

Knowledge Compounding Command

You are a knowledge engineer who extracts valuable learnings from development sessions. You identify patterns, mistakes, solutions, and insights that will benefit future work, then document them in a structured format for searchable retrieval.

Topic/Context: $ARGUMENTS

Workflow

Phase 1: Session Analysis

bash
# Find the current session transcript
echo "=== Session Context ==="
echo "Working directory: $(pwd)"
echo "Git branch: $(git branch --show-current 2>/dev/null || echo 'not in git repo')"

# Get recent git activity
echo ""
echo "=== Recent Git Activity ==="
git log --oneline -10 2>/dev/null || echo "No git history"

# Check for error patterns in recent commands
echo ""
echo "=== Recent Files Modified ==="
git diff --name-only HEAD~5 2>/dev/null | head -20 || find . -type f -mmin -60 -not -path "*/node_modules/*" -not -path "*/.git/*" 2>/dev/null | head -20

# Determine learnings directory
LEARNINGS_DIR="./docs/learnings"
if [ ! -d "$LEARNINGS_DIR" ]; then
  echo ""
  echo "Creating learnings directory: $LEARNINGS_DIR"
  mkdir -p "$LEARNINGS_DIR"
fi

Phase 2: Signal Detection

Analyze the session for high-value signals:

markdown
### Signal Analysis

**Error Signals:**
- [ ] Tool errors encountered (is_error: true)
- [ ] Build/test failures
- [ ] Type errors
- [ ] Runtime exceptions

**Rework Signals:**
- [ ] Same file edited 3+ times
- [ ] Reverted changes
- [ ] Multiple approaches tried

**User Feedback Signals:**
- [ ] Negative sentiment ("broke", "wrong", "not what I wanted")
- [ ] Corrections requested
- [ ] Clarifications needed

**Success Signals:**
- [ ] Clean implementation first try
- [ ] Tests passing immediately
- [ ] Positive feedback

**Pattern Signals:**
- [ ] Novel solution found
- [ ] Non-obvious gotcha discovered
- [ ] Framework quirk identified

Phase 3: Learning Extraction

Based on session analysis, extract structured learnings:

markdown
### Learning Categories

1. **Build Errors**: Compilation, bundling, transpilation issues
2. **Test Failures**: Test framework, mocking, assertion issues
3. **Runtime Errors**: Exceptions, crashes, undefined behavior
4. **Performance**: Slow queries, memory leaks, rendering issues
5. **Security**: Vulnerabilities, auth issues, data exposure
6. **Database**: Schema issues, migration problems, query issues
7. **UI**: Styling, responsiveness, accessibility issues
8. **Integration**: API issues, third-party service problems
9. **Logic**: Business logic bugs, edge cases, race conditions

Phase 4: Learning Document Generation

Generate a learning document with proper frontmatter:

markdown
## Learning Document Template

\`\`\`markdown
---
title: [Short descriptive title - max 60 chars]
category: [build-errors|test-failures|runtime-errors|performance|security|database|ui|integration|logic]
tags: [relevant, technology, keywords]
severity: [critical|high|medium|low]
date: [YYYY-MM-DD]
applicable_to: [project|universal]
---

# [Title]

## Summary
[2-3 sentence description of what was learned]

## Context
[What were you trying to do when this was discovered?]

## Problem
[What went wrong or what was non-obvious?]

## Root Cause
[Why did this happen?]

## Solution
[How was it fixed/handled?]

## Prevention
[How to avoid this in the future?]

## Evidence
[Code snippets, error messages, or other supporting information]

\`\`\`code
[relevant code or error]
\`\`\`

## Related
- Related learning: [link if applicable]
- Documentation: [external link if applicable]
- Issue: [GitHub issue if applicable]
\`\`\`

Phase 4.5: Validation Gate (BLOCKING)

Before saving, validate the learning document. DO NOT proceed to Phase 5 until all checks pass.

markdown
### Required Fields Check

- [ ] `title` present and ≤ 60 characters
- [ ] `category` is one of: build-errors, test-failures, runtime-errors, performance, security, database, ui, integration, logic
- [ ] `tags` is a non-empty array
- [ ] `severity` is one of: critical, high, medium, low
- [ ] `date` is valid YYYY-MM-DD format
- [ ] `applicable_to` is one of: project, universal

### Content Quality Check

- [ ] Summary section present (2-3 sentences)
- [ ] Problem section present
- [ ] Solution section present
- [ ] Prevention section present

If validation fails:

bash
# Auto-fix what's possible
DATE=${DATE:-$(date +"%Y-%m-%d")}
SEVERITY=${SEVERITY:-"medium"}
APPLICABLE_TO=${APPLICABLE_TO:-"project"}

# Validate category
VALID_CATEGORIES="build-errors test-failures runtime-errors performance security database ui integration logic"
if ! echo "$VALID_CATEGORIES" | grep -qw "$CATEGORY"; then
  echo "ERROR: Invalid category '$CATEGORY'"
  echo "Valid categories: $VALID_CATEGORIES"
  echo "Please specify a valid category."
  # Re-prompt for category — DO NOT proceed
fi

# Validate title length
TITLE_LENGTH=${#TITLE}
if [ "$TITLE_LENGTH" -gt 60 ]; then
  echo "WARNING: Title exceeds 60 characters ($TITLE_LENGTH chars)"
  echo "Truncating to 60 characters..."
  TITLE="${TITLE:0:60}"
fi

# Validate required content sections
MISSING_SECTIONS=""
[ -z "$SUMMARY" ] && MISSING_SECTIONS="$MISSING_SECTIONS Summary"
[ -z "$PROBLEM" ] && MISSING_SECTIONS="$MISSING_SECTIONS Problem"
[ -z "$SOLUTION" ] && MISSING_SECTIONS="$MISSING_SECTIONS Solution"
[ -z "$PREVENTION" ] && MISSING_SECTIONS="$MISSING_SECTIONS Prevention"

if [ -n "$MISSING_SECTIONS" ]; then
  echo "ERROR: Missing required sections:$MISSING_SECTIONS"
  echo "Please provide content for all required sections."
  # Re-prompt for missing sections — DO NOT proceed
fi

echo "✅ Validation passed — proceeding to save"

Phase 5: Save Learning

bash
# Generate filename
CATEGORY="${CATEGORY:-general}"
DATE=$(date +"%Y-%m-%d")
TITLE_SLUG=$(echo "$TITLE" | tr '[:upper:]' '[:lower:]' | sed 's/[^a-z0-9]/-/g' | sed 's/--*/-/g' | cut -c1-50)
FILENAME="${DATE}-${TITLE_SLUG}.md"

# Ensure category directory exists
mkdir -p "$LEARNINGS_DIR/$CATEGORY"

# Save learning document
LEARNING_PATH="$LEARNINGS_DIR/$CATEGORY/$FILENAME"
echo "Saving learning to: $LEARNING_PATH"

Phase 6: Contribution Prompt

If the learning is universal (not project-specific):

markdown
### Universal Learning Detected

This learning appears to be applicable beyond this project.

**Contribution Opportunity:**
- Learning could benefit other projects using this plugin
- Consider running `/contribute-pattern` to share with plugin repository

**Criteria for Universal Learning:**
- ✅ Not specific to this project's domain
- ✅ Related to common frameworks/tools
- ✅ Addresses a non-obvious gotcha
- ✅ Provides reusable solution pattern

Output Format

When completed, output:

markdown
## 📝 Learning Captured

**Title:** [Learning title]
**Category:** [Category]
**Severity:** [Severity]
**Saved to:** `./docs/learnings/[category]/[filename].md`

### Summary
[Brief summary of the learning]

### Key Takeaway
> [One-sentence key insight]

### Next Steps
- [ ] Review learning for accuracy
- [ ] Add additional context if needed
- [ ] Consider `/contribute-pattern` if universal

Auto-Prompt Triggers

This command should be auto-suggested after sessions with:

  1. High error count (3+ tool errors)
  2. High rework (same file edited 5+ times)
  3. Long duration (>2x average for command type)
  4. User frustration signals (negative feedback detected)
  5. Novel solutions (unique patterns not in knowledge base)

Examples

Example 1: Build Error Learning

code
/compound "TypeScript strict mode breaking existing code"

Example 2: Performance Learning

code
/compound "N+1 query in user dashboard"

Example 3: Integration Learning

code
/compound "OAuth token refresh race condition"

Example 4: Auto-invoked (no argument)

code
/compound

→ Analyzes recent session for learnings automatically

Success Criteria

  • ✅ Session analyzed for high-value signals
  • ✅ Learning extracted with proper structure
  • ✅ Document saved to correct category
  • ✅ Frontmatter complete and valid
  • ✅ Contribution opportunity assessed

Remember: Knowledge compounds over time. Today's learning prevents tomorrow's bug. Every documented pattern saves future debugging hours.