OverviewFilesCommitsIssuesDocsReleasesPeers

knowledge-ratchet commits / ea76327

Update README for Phase 2 — complete system documentation
Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
ea763278b5d8f98aa92a1fe5966694160fd13bef
by plankatron <fleeky@gmail.com> · 2026-04-07T16:06:36.000Z

Changed files (1) +149 -106

README.md +149 -106
……@@ -1,143 +1,186 @@
11# Knowledge Ratchet
22
3A meta-learning system for Claude Code that extracts principles from user corrections, validates them retrospectively, and enforces them via hooks. Knowledge can only improve, never degrade.
4
5## The Problem
6
7You correct Claude. It saves a memory file. Next session, maybe it loads, maybe it doesn't. You correct it again. Knowledge leaks between sessions.
8
9## The Solution
10
11Every correction gets captured, grouped into patterns, crystallized into principles, and enforced through progressively stronger mechanisms. Principles live forever until contradicted.
3Self-improving quality measurement system for Claude Code. Extracts principles from corrections, detects failure loops, auto-generates prevention rules, and tracks intelligence improvement over time.
124
135## How It Works
146
157```
16YOU → correct Claude naturally
17 ↓
18CAPTURE → hooks detect corrections in real-time
19 ↓
20EXTRACT → group corrections by topic, find patterns
21 ↓
22VALIDATE → retrospective test: would principles prevent past corrections?
23 ↓
24PROMOTE → passing principles enter rules (always loaded)
25 ↓
26ENFORCE → hooks block violations, surface principles at moment of action
27 ↓
28REGRESS → weekly: are correction rates dropping?
29 ↓
30CONTRADICT → new correction conflicts with principle? ask the user
8Session starts
9 → Loop briefing: past failures + 221 learned rules for this project
10 → Preflight hook: blocks edits to danger files without reading first
11
12During session
13 → Realtime scorer: tracks corrections, approvals, tool calls per turn
14 → Loop detector: 2+ corrections → injects learned rules as context
15 → Status bar: ▬ 0.72 │ 3 approved 1 corrected │ 45 tools
16
17Session ends (2.7s pipeline)
18 → Sensor fusion: scores session with 16 per-turn + 5 session-level signals
19 → Meta-ratchet: adjusts sensor weights from predictive accuracy
20 → Loop extraction: finds correction→resolution patterns
21 → Rule extraction: auto-generates rules from resolved loops
22 → Project mapper: autodiscovers subsystems + danger levels
23 → Benchmarks: tracks novel/repeat ratio, density, resolution speed
24 → Dashboard: regenerates HTML visualization
25
26Next session
27 → Briefing includes new rules — each session makes the next one smarter
3128```
3229
33## Three Layers of Knowledge
30## Quick Start
3431
35```
36PRINCIPLES "Every storage decision must answer: what's the deletion story?"
37 ↑ extracted from patterns, prunes entire categories of bad decisions
38 |
39PATTERNS "Append-only structures grow without bound — plan GC from day one"
40 ↑ extracted from facts, predicts future corrections
41 |
42FACTS "Hyperdrive caused 56GB balloon in pear-git"
43 ↑ your direct corrections, the raw signal
44```
32```bash
33# Fast pipeline (runs automatically at session end)
34node pipeline.js
4535
46## How Corrections Become Constraints
36# Full batch analysis (includes LLM semantic classification)
37node analyze-all.js --limit=50
4738
48Principles migrate down through enforcement layers:
39# Dashboard (port 9077)
40node dashboard.js --serve
4941
50| Corrected... | Level | Mechanism |
51|---|---|---|
52| Once | Context | Principle in rules file (advisory) |
53| Twice | Reminder | Hook surfaces principle at moment of action |
54| 3+ times | Enforced | Hook blocks the action |
42# Benchmarks
43node benchmark-ratchet.js
44```
5545
56## Quick Start
46## Key Metrics
5747
58```bash
59# See current state
60node run.js status
48| Metric | Current | Target | What it means |
49|--------|---------|--------|---------------|
50| Novel correction rate | 87% | >90% | Old mistakes eliminated (higher = better) |
51| Correction density | 20.8% | <15% | Corrections per turn (lower = better) |
52| Loop resolution | 8.4 turns | <6 turns | How fast loops resolve (lower = better) |
53| Learned rules | 221 | growing | Auto-extracted from resolved loops |
6154
62# Run full pipeline
63node run.js full
55## Architecture
56
57### Two Pipelines
6458
65# Scan transcripts for corrections (LLM-augmented)
66node llm-analyzer.js --limit=50
59**Fast pipeline** (`pipeline.js`, 2.7s, runs at Stop hook):
60```
61Sensor Fusion → Meta-Ratchet → Loop Detector → Rule Extractor
62 → Project Mapper → Benchmarks → Dashboard
63```
6764
68# Deep sentiment analysis
69node analyze-transcripts.js
65**Full batch** (`analyze-all.js`, includes LLM calls):
66```
67LLM Analyzer → Regex Tuner → Sensor Fusion → Meta-Ratchet
68 → Coverage Map → Ratchet Pipeline → Deep Sentiment
7069```
7170
72## Pipeline Commands
71### Hooks
7372
74```bash
75node run.js full # Run everything
76node run.js extract # Extract patterns from feedback files
77node run.js validate # Retrospective split test
78node run.js regress # Score principles against recent corrections
79node run.js migrate # Check enforcement level escalation
80node run.js questions # List pending questions for user
81node run.js status # Show current state
73| Hook | Script | Mode | Purpose |
74|------|--------|------|---------|
75| SessionStart | loop-briefing.js | sync | Past loops + learned rules for current project |
76| SessionStart | realtime-score.js reset | sync | Reset session state |
77| UserPromptSubmit | loop-intervention.js | sync | Inject rules when loop detected |
78| UserPromptSubmit | detect-correction.js | async | Log corrections |
79| UserPromptSubmit | realtime-score.js prompt | async | Update live score |
80| PostToolUse | realtime-score.js tool | async | Track tool calls, errors, edits |
81| PreToolUse (Edit/Write) | preflight-review.js | sync | Block edits to danger files |
82| Stop | realtime-score.js stop | sync | Finalize session score |
83| Stop | pipeline.js | async | Run full pipeline |
84
85### Self-Improving Feedback Loops
86
87**1. Sensor weight learning** (meta-ratchet.js):
88```
89fuse-signals scores turns → meta-ratchet checks which sensors
90predicted corrections → adjusts weights → next run uses new weights
8291```
8392
84## Individual Tools
93**2. Pattern discovery** (tune-regex.js):
94```
95LLM classifies corrections → tune-regex finds which regex patterns
96match → discovers new patterns → fuse-signals uses tuned patterns
97```
8598
86```bash
87node scan-transcripts.js # Regex scan of session transcripts
88node llm-analyzer.js --limit=20 # LLM analysis of subtle corrections
89node llm-analyzer.js status # Check processing cursor
90node llm-analyzer.js reset # Reset cursor (reprocess everything)
91node contradiction-detector.js check "text" # Check for contradictions
92node contradiction-detector.js pending # List pending questions
93node promote.js list # Show principles and candidates
94node promote.js sync # Write principles to rules file
95node ratchet.js commit "message" # Git-guarded principle update
96node ratchet.js score # Current validation score
99**3. Rule accumulation** (rule-extractor.js):
100```
101loop resolves → rule-extractor captures what fixed it → rule saved
102→ next loop briefing includes the rule → prevents repeat
97103```
98104
99## Architecture
105## Files
100106
101107```
102108knowledge-ratchet/
103├── run.js # Pipeline orchestrator
104├── detect-correction.js # Hook: captures corrections in real-time
105├── preflight-review.js # Hook: blocks shared module edits with briefing
106├── reset-session.js # Hook: clears per-session state
107├── scan-transcripts.js # Regex scanner for session transcripts
108├── llm-analyzer.js # LLM-augmented semantic analysis (Haiku)
109├── analyze-transcripts.js # Deep sentiment and flow analysis
110├── extract-patterns.js # Groups feedback files into patterns/principles
111├── validate-principles.js # Retrospective split test
112├── contradiction-detector.js # Checks new corrections against principles
113├── promote.js # Moves candidates to active, syncs to rules
114├── regress.js # Scores principles by correction rate
115├── migrate-down.js # Tracks violations, escalates enforcement
116├── ratchet.js # Git-guarded principle commits
117├── active-principles.md # Current active principles
109├── pipeline.js # Fast pipeline (2.7s at session end)
110├── analyze-all.js # Full batch pipeline (7 steps)
111├── dashboard.js # HTML dashboard on port 9077
112│
113├── Measurement
114│ ├── fuse-signals.js # 16 sensors + 5 session signals → fused score
115│ ├── scan-sessions.js # Single-pass JSONL reader with results cache
116│ ├── realtime-score.js # Live session scoring via hooks
117│ ├── meta-ratchet.js # Auto-adjust sensor weights
118│ ├── tune-regex.js # LLM-driven regex pattern discovery
119│ └── coverage-map.js # Topic coverage + failure risk
120│
121├── Loop System
122│ ├── loop-detector.js # Extract correction loops from turn data
123│ ├── loop-intervention.js # Inject rules when loop forms (non-blocking)
124│ ├── loop-briefing.js # Session-start briefing with past loops
125│ └── rule-extractor.js # Auto-generate rules from resolutions
126│
127├── Prevention
128│ ├── preflight-review.js # Block edits to danger files
129│ ├── project-mapper.js # Autodiscover subsystems + danger levels
130│ └── detect-correction.js # Real-time correction capture
131│
132├── Phase 1 Pipeline
133│ ├── extract-patterns.js # Group corrections into patterns
134│ ├── validate-principles.js # Retrospective split test
135│ ├── contradiction-detector.js
136│ ├── promote.js # Candidate → active principle
137│ ├── regress.js # Score principles + fused quality trend
138│ ├── migrate-down.js # Escalate enforcement level
139│ ├── ratchet.js # Git-guarded principle commits
140│ └── run.js # Phase 1 pipeline orchestrator
141│
142├── Analysis
143│ ├── llm-analyzer.js # LLM semantic classification (Haiku)
144│ ├── scan-transcripts.js # Batch regex scanner
145│ ├── analyze-transcripts.js # Deep sentiment/flow analysis
146│ └── benchmark-ratchet.js # T3, R1, R2, R3, R4 benchmarks + history
147│
148├── active-principles.md # 5 validated principles
118149└── data/
119 ├── patterns.json # Extracted topic groups
120 ├── principles.json # Structured principles with metadata
121 └── validation-report.json # Test results
150 ├── fused-scores.json # Per-session fusion scores
151 ├── fused-turns-detail.json # Per-turn sensor data
152 ├── loops.json # 287 loops with corrections + resolutions
153 ├── learned-rules.json # 221 auto-extracted rules
154 ├── project-maps.json # Autodiscovered subsystem maps
155 ├── sensor-weights.json # Meta-ratchet learned weights
156 ├── tuned-patterns.json # LLM-discovered regex patterns
157 ├── benchmark-history.json # Daily benchmark snapshots
158 ├── coverage-map.json # Topic coverage + failure risk
159 ├── results-cache/ # Scored session cache (0.4s vs 10s)
160 └── dashboard.html # Generated visualization
122161```
123162
124## Design Decisions
163## Pear-Git Analysis (worst project: 58 loops)
164
165Top failure patterns:
166- "Do an audit / enter planning mode" — 68 corrections (33%)
167- "Stop" — 34 corrections (17%)
168- "Same error again" — 28 corrections (14%)
125169
126- **No decay**: Principles live forever until contradicted. Silence is confidence, not staleness.
127- **LLM over regex**: Regex catches obvious corrections. LLM catches "the mesh is invisible?" and "sounds like a timer to me." The LLM is the source of truth.
128- **Contradictions ask, don't auto-update**: When your thinking changes, the system asks you to confirm — it doesn't silently overwrite.
129- **Forward-only ratchet**: Principle changes are git-committed with validation. If the score drops, the commit is reverted.
130- **Dedup via cursor**: LLM analyzer tracks what it's already processed. No re-reading.
170Danger subsystems (auto-discovered):
171- `cli` — 51 files, 36 loops (⚠ high)
172- `server-core` — 9 files, 16 loops (⚠ high)
173- `p2p` — 13 files, 14 loops (⚠ high)
174- `routes` — 23 files, 13 loops (⚠ high)
131175
132## Validated Results
176## Design Decisions
133177
134- 30 feedback files analyzed
135- 5 principles extracted
136- 89% prevention rate on held-out test set
137- 1,704 data points from 108 session transcripts
138- LLM catches corrections regex misses (e.g., "you flipped x not y", "why are you advocating for polling")
178- **Context injection over blocking**: Loop intervention adds rules to the conversation, doesn't reject prompts. Claude reads rules AND responds to the user.
179- **Results cache**: Scored sessions cached by file mtime + weights hash. 140 files (1.1GB) → 0.4s scan.
180- **LLM is ground truth**: Regex has 97% precision but 25% recall. LLM catches the other 75% — subtle corrections like "hmm", "it is the same", "why are we doing X".
181- **Rules accumulate, never delete**: Each resolved loop generates a rule. Rules live forever unless superseded.
182- **Autodiscovery over hardcoding**: Project mapper scans file trees + require() graphs to find subsystems. Preflight hook reads the map, not a hardcoded list.
139183
140184## Inspired By
141185
142Karpathy's [autoresearch](https://github.com/karpathy/autoresearch) — automated experiment loops with a forward-only ratchet. Adapted from scalar metric optimization to domain knowledge improvement. The metric isn't val_bpb — it's "did the user have to correct me?"
143# trigger
186Karpathy's [autoresearch](https://github.com/karpathy/autoresearch) — forward-only ratchet applied to domain knowledge instead of scalar metrics. The metric isn't val_bpb — it's correction density and loop resolution speed.