Learning Capture System
The Learning Capture System automatically captures failed commands and their error output as learning documents. This helps build a personal knowledge base of common mistakes and their corrections.
Overview
When you run a command that fails, the Learning Capture System:
- Captures the command and error output
- Redacts any secrets (API keys, passwords, etc.)
- Stores the learning in Markdown format
- Indexes it for later search and retrieval
Architecture
Failed Command → Secret Redaction → Storage → Indexing → Search
↓
Auto-Suggest (future)Storage Locations
Learnings are stored in two locations:
Project-Specific (.terraphim/learnings/)
- Captured when you're in a project directory
- Shared with project-specific knowledge graph
- Priority location for queries
Global (~/.local/share/terraphim/learnings/)
- Fallback when not in a project
- Common patterns across all projects
- Always searched as secondary source
Usage
Capturing a Failed Command
This creates a Markdown file in your learnings directory with:
- The command that failed
- The error output (with secrets redacted)
- Exit code
- Timestamp and context
- Unique ID for reference
Listing Recent Learnings
# Show last 10 learnings (default)
# Show more
# Show global learnings
Output shows:
Recent learnings:
1. [P] git push -f (exit: 1)
2. [G] npm install (exit: 1)
3. [P] cargo build (exit: 101)[P] = Project-specific, [G] = Global
Querying Learnings
# Search by substring (default)
# Exact match
# Search global learnings
Automatic Capture with Hooks
The Learning Capture System can automatically capture failed commands via hooks.
PostToolUse Hook
Add to your Claude Code configuration to automatically capture failed Bash commands:
The hook:
- Only captures failed commands (non-zero exit)
- Automatically redacts secrets before storage
- Is fail-open - doesn't block if capture fails
- Works transparently in the background
Debug Mode
Enable debug output to see what's being captured:
Secret Redaction
Before storing, the system automatically redacts:
- AWS keys (
AKIA...) - API tokens (
sk-...,ghp_...,xoxb-...) - Connection strings (
postgresql://...,mysql://...) - Environment variables (
VAR=valuepatterns)
Example:
Before: postgresql://user:secret@localhost/db
After: postgresql://[REDACTED]@localhost/dbIgnored Commands
The following commands are automatically ignored (not captured):
cargo test*- Test commandsnpm test*- Test commandspytest*- Test commandsyarn test*- Test commands
This prevents test failures from cluttering your learnings.
Learning Document Format
Learnings are stored as Markdown files with YAML frontmatter:
remote: rejected
Future Features
Coming in future updates:
- Auto-suggest corrections - Query existing KG for suggested fixes
- Add corrections -
terraphim-agent learn correct <id> --correction "..." - Web UI - Browse and manage learnings visually
- Export/Import - Share learnings between machines
- Team sharing - Share common patterns with team
Configuration
The Learning Capture System uses sensible defaults but can be configured via environment:
# Enable debug output
# Custom directories (future feature)
Troubleshooting
Learnings not being captured
- Check that
terraphim-agentis in your PATH - Enable debug mode:
export TERRAPHIM_LEARN_DEBUG=true - Verify storage directory is writable
- Check if command matches ignore patterns
Hook not working
- Ensure hook script is executable:
chmod +x learning-capture.sh - Check Claude Code hook configuration
- Verify
terraphim-agentbinary exists
Storage location
Find where learnings are stored:
# Check project location
# Check global location
See Also
Appendix: Traceability Matrix
This matrix maps all documented examples to their implementation status and test evidence.
Manual Capture Examples
| # | Example | Command | Requirement | Test | Status |
|---|---------|---------|-------------|------|--------|
| 1 | Basic capture | learn capture 'git push -f' --error '...' | REQ-3.1: Capture failed commands | test_capture_failed_command | ✅ |
| 2 | NPM install error | learn capture 'npm install' --error 'EACCES...' | REQ-3.1: Capture failed commands | test_capture_failed_command | ✅ |
| 3 | Git status error | learn capture 'git status' --error 'fatal...' | REQ-3.1: Capture failed commands | test_capture_failed_command | ✅ |
| 4 | With exit code | --exit-code 128 | REQ-3.2: Store exit code | test_capture_failed_command | ✅ |
| 5 | With debug | --debug | REQ-5.1: Debug visibility | Manual test | ✅ |
List Examples
| # | Example | Command | Requirement | Test | Status |
|---|---------|---------|-------------|------|--------|
| 6 | List default | learn list | REQ-4.1: List learnings | test_list_learnings | ✅ |
| 7 | List recent | learn list --recent 5 | REQ-4.1: List learnings | test_list_learnings | ✅ |
| 8 | List global | learn list --global | REQ-2.2: Hybrid storage | Manual test | ✅ |
Query Examples
| # | Example | Command | Requirement | Test | Status |
|---|---------|---------|-------------|------|--------|
| 9 | Substring | learn query 'git' | REQ-4.2: Query learnings | test_list_learnings | ✅ |
| 10 | Exact | learn query '...' --exact | REQ-4.2: Query learnings | test_list_learnings | ✅ |
| 11 | Global | learn query 'npm' --global | REQ-2.2: Hybrid storage | Manual test | ✅ |
Ignored Commands (Anti-Patterns)
| # | Example | Command | Requirement | Test | Status |
|---|---------|---------|-------------|------|--------|
| 12 | cargo test | learn capture 'cargo test' ... | REQ-2.3: Ignore patterns | test_capture_ignores_test_commands | ✅ |
| 13 | npm test | learn capture 'npm test' ... | REQ-2.3: Ignore patterns | test_capture_ignores_test_commands | ✅ |
| 14 | pytest | learn capture 'pytest' ... | REQ-2.3: Ignore patterns | test_capture_ignores_test_commands | ✅ |
Secret Redaction
| # | Pattern | Redacted To | Requirement | Test | Status |
|---|---------|-------------|-------------|------|--------|
| 15 | AWS key | [AWS_KEY_REDACTED] | REQ-2.1: Auto-redaction | test_redact_aws_key | ✅ |
| 16 | Postgres | [REDACTED] | REQ-2.1: Auto-redaction | test_redact_connection_string | ✅ |
| 17 | OpenAI | [OPENAI_KEY_REDACTED] | REQ-2.1: Auto-redaction | test_redact_multiple_secrets | ✅ |
| 18 | GitHub | [GITHUB_TOKEN_REDACTED] | REQ-2.1: Auto-redaction | test_redact_multiple_secrets | ✅ |
| 19 | Slack | [SLACK_TOKEN_REDACTED] | REQ-2.1: Auto-redaction | test_redact_multiple_secrets | ✅ |
Integration
| # | Component | Requirement | Test | Status | |---|-----------|-------------|------|--------| | 20 | Hook script | REQ-5.1: Hook integration | test_learning_capture.sh | ✅ | | 21 | CLI subcommands | REQ-4.1-4.3: CLI | All CLI tests | ✅ | | 22 | Storage | REQ-2.2: Hybrid storage | test_storage_location_prefers_project | ✅ |
Summary
Total Examples: 22
Verified Working: 22 ✅ (100%)
Unit Tests: 15/15 passing ✅
Last Verified: 2026-02-15