learning-agent 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/CHANGELOG.md ADDED
@@ -0,0 +1,95 @@
1
+ # Changelog
2
+
3
+ All notable changes to this project will be documented in this file.
4
+
5
+ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.1.0/),
6
+ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
7
+
8
+ ## [Unreleased]
9
+
10
+ ### Added
11
+
12
+ - **Storage Enhancements**
13
+ - Compaction system for archiving old lessons and removing tombstones
14
+ - Smart sync: rebuild index only when JSONL changes
15
+ - Retrieval count tracking for lesson usage statistics
16
+
17
+ - **CLI Commands**
18
+ - `learning-agent import <file>` - Import lessons from JSONL file
19
+ - `learning-agent stats` - Show database health statistics
20
+ - `learning-agent compact` - Archive old lessons
21
+ - `learning-agent export` - Export lessons as JSON
22
+ - Global flags: `--verbose`, `--quiet`
23
+ - Colored output with chalk
24
+
25
+ - **Embeddings**
26
+ - Switched to EmbeddingGemma-300M (~278MB, down from ~500MB)
27
+ - Simplified model download using node-llama-cpp resolveModelFile
28
+
29
+ - **Testing**
30
+ - 100% statement/function/line coverage (435 tests)
31
+ - Property-based tests with fast-check
32
+ - Integration tests for capture workflows
33
+
34
+ ### Changed
35
+
36
+ - Unified QuickLesson and FullLesson into single Lesson type
37
+ - Removed deprecated type exports
38
+
39
+ ## [0.1.0] - 2025-01-30
40
+
41
+ ### Added
42
+
43
+ - **Core Storage**
44
+ - JSONL storage for lessons with atomic append operations
45
+ - SQLite index with FTS5 full-text search support
46
+ - Automatic index rebuild from JSONL source of truth
47
+ - Hybrid storage model: git-tracked JSONL + rebuildable SQLite cache
48
+
49
+ - **Embeddings**
50
+ - Local semantic embeddings via node-llama-cpp
51
+ - EmbeddingGemma-300M model (768 dimensions)
52
+ - Manual model download via CLI (~278MB)
53
+ - Model stored in `~/.node-llama-cpp/models/`
54
+
55
+ - **Search & Retrieval**
56
+ - Vector similarity search using cosine distance
57
+ - Ranking with configurable boosts (severity, recency, confirmation)
58
+ - Session-start retrieval for high-severity lessons
59
+ - Plan-time retrieval with semantic relevance
60
+
61
+ - **Capture System**
62
+ - User correction detection patterns
63
+ - Self-correction detection (edit-fail-re-edit cycles)
64
+ - Test failure detection and fix tracking
65
+ - Quality filter: novel, specific, and actionable checks
66
+
67
+ - **Lesson Types**
68
+ - Quick lessons for fast capture
69
+ - Full lessons with evidence and severity levels
70
+ - Tombstone records for deletions/edits
71
+ - Metadata: source, context, supersedes, related
72
+
73
+ - **Public API**
74
+ - `appendLesson()` - Store new lessons
75
+ - `readLessons()` - Read lessons with pagination
76
+ - `searchKeyword()` - FTS5 keyword search
77
+ - `searchVector()` - Semantic vector search
78
+ - `loadSessionLessons()` - Session-start high-severity lessons
79
+ - `retrieveForPlan()` - Plan-time relevant lesson retrieval
80
+ - Detection triggers: `detectUserCorrection()`, `detectSelfCorrection()`, `detectTestFailure()`
81
+ - Quality filters: `shouldPropose()`, `isNovel()`, `isSpecific()`, `isActionable()`
82
+
83
+ - **CLI**
84
+ - `pnpm learn` - Capture a lesson manually
85
+ - `learning-agent search` - Search lessons
86
+ - `learning-agent rebuild` - Rebuild SQLite index
87
+
88
+ - **Developer Experience**
89
+ - TypeScript with ESM modules
90
+ - Zod schemas for runtime validation
91
+ - Vitest test suite
92
+ - tsup build configuration
93
+
94
+ [Unreleased]: https://github.com/nathanbraun/learning_agent/compare/v0.1.0...HEAD
95
+ [0.1.0]: https://github.com/nathanbraun/learning_agent/releases/tag/v0.1.0
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2025 Nathan Delacrétaz
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,225 @@
1
+ # Learning Agent
2
+
3
+ A repository-scoped learning system that helps Claude Code avoid repeating mistakes across sessions. Captures lessons from corrections and retrieves them when relevant.
4
+
5
+ ## Overview
6
+
7
+ Claude Code forgets lessons between sessions. This leads to:
8
+ - Repeated mistakes across sessions
9
+ - Users re-explaining preferences
10
+ - No memory of what worked or failed
11
+
12
+ Learning Agent solves this by capturing lessons when corrections happen and retrieving relevant ones at session start and plan time.
13
+
14
+ ## Installation
15
+
16
+ ```bash
17
+ # Using pnpm (recommended)
18
+ pnpm add -D learning-agent
19
+
20
+ # Using npm
21
+ npm install --save-dev learning-agent
22
+ ```
23
+
24
+ After installation, download the embedding model (~278MB, one-time):
25
+
26
+ ```bash
27
+ npx learning-agent download-model
28
+ ```
29
+
30
+ ### Requirements
31
+
32
+ - Node.js >= 20
33
+ - ~278MB disk space for embedding model
34
+ - ~150MB RAM for embedding operations
35
+
36
+ ## Quick Start
37
+
38
+ ```bash
39
+ # Install dependencies
40
+ pnpm install
41
+
42
+ # Build
43
+ pnpm build
44
+
45
+ # Run tests
46
+ pnpm test
47
+
48
+ # Download embedding model (first use)
49
+ pnpm download-model
50
+ ```
51
+
52
+ ## Architecture
53
+
54
+ ```
55
+ .claude/ (repository scope)
56
+ |-- CLAUDE.md <- Always loaded (permanent rules)
57
+ |-- lessons/
58
+ | |-- index.jsonl <- Source of truth (git-tracked)
59
+ | +-- archive/ <- Old lessons (compacted)
60
+ +-- .cache/
61
+ +-- lessons.sqlite <- Rebuildable index (.gitignore)
62
+ ```
63
+
64
+ ### Data Flow
65
+
66
+ ```
67
+ +----------+ +----------+ +----------+ +----------+
68
+ | Mistake |--->| Claude |--->| Quick |--->| Stored |
69
+ | happens | | notices | | confirm | | lesson |
70
+ +----------+ +----------+ +----------+ +----------+
71
+ | |
72
+ (or user [y/n]
73
+ corrects)
74
+
75
+ +----------+ +----------+ +----------+
76
+ | Next |<---| Retrieve |<---| Session |
77
+ | task | | relevant | | start |
78
+ +----------+ +----------+ +----------+
79
+ ```
80
+
81
+ ## Features
82
+
83
+ - **Lesson Capture**: Detects user corrections, self-corrections, and test failure fixes
84
+ - **Quality Filter**: Prevents vague or obvious lessons (must be novel, specific, actionable)
85
+ - **Vector Search**: Local semantic search using nomic-embed-text-v1.5 via node-llama-cpp
86
+ - **Hybrid Storage**: JSONL source of truth (git-tracked) with SQLite FTS5 index (rebuildable)
87
+ - **Offline First**: No external API dependencies; works completely offline
88
+ - **Retrieval Timing**: High-severity lessons at session start; relevant lessons at plan time
89
+
90
+ ## CLI Usage
91
+
92
+ ```bash
93
+ # Capture a lesson manually
94
+ pnpm learn "Use Polars for large files, not pandas"
95
+
96
+ # Search lessons
97
+ learning-agent search "data processing"
98
+
99
+ # Rebuild index from JSONL
100
+ learning-agent rebuild
101
+
102
+ # List all lessons
103
+ learning-agent list
104
+
105
+ # Show database stats
106
+ learning-agent stats
107
+
108
+ # Compact and archive old lessons
109
+ learning-agent compact
110
+ ```
111
+
112
+ ## Claude Code Integration
113
+
114
+ Add to your `.claude/settings.json` to automatically load lessons:
115
+
116
+ ```json
117
+ {
118
+ "hooks": {
119
+ "session_start": "npx learning-agent load-session",
120
+ "pre_plan": "npx learning-agent check-plan"
121
+ }
122
+ }
123
+ ```
124
+
125
+ ### Hook Commands
126
+
127
+ | Command | Purpose |
128
+ |---------|---------|
129
+ | `load-session` | Load high-severity lessons at session start |
130
+ | `check-plan` | Retrieve relevant lessons when planning |
131
+
132
+ ## API Reference
133
+
134
+ ```typescript
135
+ import {
136
+ // Storage
137
+ appendLesson, readLessons, searchKeyword, rebuildIndex, closeDb,
138
+
139
+ // Search
140
+ searchVector, cosineSimilarity, rankLessons,
141
+
142
+ // Capture
143
+ shouldPropose, isNovel, isSpecific, isActionable,
144
+ detectUserCorrection, detectSelfCorrection, detectTestFailure,
145
+
146
+ // Retrieval
147
+ loadSessionLessons, retrieveForPlan, formatLessonsCheck,
148
+
149
+ // Types
150
+ type Lesson, LessonSchema, generateId,
151
+ } from 'learning-agent';
152
+ ```
153
+
154
+ See [examples/](examples/) for usage examples.
155
+
156
+ ## Lesson Types
157
+
158
+ ### Quick Lesson (fast capture)
159
+ ```json
160
+ {
161
+ "id": "L001",
162
+ "type": "quick",
163
+ "trigger": "Used pandas for 500MB file",
164
+ "insight": "Polars 10x faster",
165
+ "tags": ["performance", "polars"],
166
+ "source": "user_correction"
167
+ }
168
+ ```
169
+
170
+ ### Full Lesson (detailed, high-severity)
171
+ ```json
172
+ {
173
+ "id": "L002",
174
+ "type": "full",
175
+ "trigger": "Auth API returned 401 despite valid token",
176
+ "insight": "API requires X-Request-ID header",
177
+ "evidence": "Traced in network tab, header missing",
178
+ "severity": "high",
179
+ "source": "test_failure"
180
+ }
181
+ ```
182
+
183
+ ## Technology Stack
184
+
185
+ | Component | Technology |
186
+ |-----------|------------|
187
+ | Language | TypeScript (ESM) |
188
+ | Package Manager | pnpm |
189
+ | Build | tsup |
190
+ | Testing | Vitest |
191
+ | Storage | better-sqlite3 + FTS5 |
192
+ | Embeddings | node-llama-cpp + nomic-embed-text-v1.5 |
193
+ | CLI | Commander.js |
194
+ | Schema | Zod |
195
+
196
+ ## Development
197
+
198
+ ```bash
199
+ # Watch mode (rebuild on changes)
200
+ pnpm dev
201
+
202
+ # Run tests in watch mode
203
+ pnpm test:watch
204
+
205
+ # Type checking
206
+ pnpm lint
207
+ ```
208
+
209
+ ## Project Status
210
+
211
+ Version 0.1.0 - Core infrastructure implemented. See [doc/SPEC.md](doc/SPEC.md) for the full specification and [doc/PLAN.md](doc/PLAN.md) for the implementation roadmap.
212
+
213
+ ## Documentation
214
+
215
+ | Document | Purpose |
216
+ |----------|---------|
217
+ | [doc/SPEC.md](doc/SPEC.md) | Complete specification |
218
+ | [doc/CONTEXT.md](doc/CONTEXT.md) | Research and design decisions |
219
+ | [doc/PLAN.md](doc/PLAN.md) | Implementation plan |
220
+ | [AGENTS.md](AGENTS.md) | Agent instructions overview |
221
+ | [.claude/CLAUDE.md](.claude/CLAUDE.md) | Claude Code project instructions |
222
+
223
+ ## License
224
+
225
+ MIT
package/dist/cli.d.ts ADDED
@@ -0,0 +1 @@
1
+ #!/usr/bin/env node