superlocalmemory 2.3.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.
Files changed (100) hide show
  1. package/ATTRIBUTION.md +140 -0
  2. package/CHANGELOG.md +1749 -0
  3. package/LICENSE +21 -0
  4. package/README.md +600 -0
  5. package/bin/aider-smart +72 -0
  6. package/bin/slm +202 -0
  7. package/bin/slm-npm +73 -0
  8. package/bin/slm.bat +195 -0
  9. package/bin/slm.cmd +10 -0
  10. package/bin/superlocalmemoryv2:list +3 -0
  11. package/bin/superlocalmemoryv2:profile +3 -0
  12. package/bin/superlocalmemoryv2:recall +3 -0
  13. package/bin/superlocalmemoryv2:remember +3 -0
  14. package/bin/superlocalmemoryv2:reset +3 -0
  15. package/bin/superlocalmemoryv2:status +3 -0
  16. package/completions/slm.bash +58 -0
  17. package/completions/slm.zsh +76 -0
  18. package/configs/antigravity-mcp.json +13 -0
  19. package/configs/chatgpt-desktop-mcp.json +7 -0
  20. package/configs/claude-desktop-mcp.json +15 -0
  21. package/configs/codex-mcp.toml +13 -0
  22. package/configs/cody-commands.json +29 -0
  23. package/configs/continue-mcp.yaml +14 -0
  24. package/configs/continue-skills.yaml +26 -0
  25. package/configs/cursor-mcp.json +15 -0
  26. package/configs/gemini-cli-mcp.json +11 -0
  27. package/configs/jetbrains-mcp.json +11 -0
  28. package/configs/opencode-mcp.json +12 -0
  29. package/configs/perplexity-mcp.json +9 -0
  30. package/configs/vscode-copilot-mcp.json +12 -0
  31. package/configs/windsurf-mcp.json +16 -0
  32. package/configs/zed-mcp.json +12 -0
  33. package/docs/ARCHITECTURE.md +877 -0
  34. package/docs/CLI-COMMANDS-REFERENCE.md +425 -0
  35. package/docs/COMPETITIVE-ANALYSIS.md +210 -0
  36. package/docs/COMPRESSION-README.md +390 -0
  37. package/docs/GRAPH-ENGINE.md +503 -0
  38. package/docs/MCP-MANUAL-SETUP.md +720 -0
  39. package/docs/MCP-TROUBLESHOOTING.md +787 -0
  40. package/docs/PATTERN-LEARNING.md +363 -0
  41. package/docs/PROFILES-GUIDE.md +453 -0
  42. package/docs/RESET-GUIDE.md +353 -0
  43. package/docs/SEARCH-ENGINE-V2.2.0.md +748 -0
  44. package/docs/SEARCH-INTEGRATION-GUIDE.md +502 -0
  45. package/docs/UI-SERVER.md +254 -0
  46. package/docs/UNIVERSAL-INTEGRATION.md +432 -0
  47. package/docs/V2.2.0-OPTIONAL-SEARCH.md +666 -0
  48. package/docs/WINDOWS-INSTALL-README.txt +34 -0
  49. package/docs/WINDOWS-POST-INSTALL.txt +45 -0
  50. package/docs/example_graph_usage.py +148 -0
  51. package/hooks/memory-list-skill.js +130 -0
  52. package/hooks/memory-profile-skill.js +284 -0
  53. package/hooks/memory-recall-skill.js +109 -0
  54. package/hooks/memory-remember-skill.js +127 -0
  55. package/hooks/memory-reset-skill.js +274 -0
  56. package/install-skills.sh +436 -0
  57. package/install.ps1 +417 -0
  58. package/install.sh +755 -0
  59. package/mcp_server.py +585 -0
  60. package/package.json +94 -0
  61. package/requirements-core.txt +24 -0
  62. package/requirements.txt +10 -0
  63. package/scripts/postinstall.js +126 -0
  64. package/scripts/preuninstall.js +57 -0
  65. package/skills/slm-build-graph/SKILL.md +423 -0
  66. package/skills/slm-list-recent/SKILL.md +348 -0
  67. package/skills/slm-recall/SKILL.md +325 -0
  68. package/skills/slm-remember/SKILL.md +194 -0
  69. package/skills/slm-status/SKILL.md +363 -0
  70. package/skills/slm-switch-profile/SKILL.md +442 -0
  71. package/src/__pycache__/cache_manager.cpython-312.pyc +0 -0
  72. package/src/__pycache__/embedding_engine.cpython-312.pyc +0 -0
  73. package/src/__pycache__/graph_engine.cpython-312.pyc +0 -0
  74. package/src/__pycache__/hnsw_index.cpython-312.pyc +0 -0
  75. package/src/__pycache__/hybrid_search.cpython-312.pyc +0 -0
  76. package/src/__pycache__/memory-profiles.cpython-312.pyc +0 -0
  77. package/src/__pycache__/memory-reset.cpython-312.pyc +0 -0
  78. package/src/__pycache__/memory_compression.cpython-312.pyc +0 -0
  79. package/src/__pycache__/memory_store_v2.cpython-312.pyc +0 -0
  80. package/src/__pycache__/migrate_v1_to_v2.cpython-312.pyc +0 -0
  81. package/src/__pycache__/pattern_learner.cpython-312.pyc +0 -0
  82. package/src/__pycache__/query_optimizer.cpython-312.pyc +0 -0
  83. package/src/__pycache__/search_engine_v2.cpython-312.pyc +0 -0
  84. package/src/__pycache__/setup_validator.cpython-312.pyc +0 -0
  85. package/src/__pycache__/tree_manager.cpython-312.pyc +0 -0
  86. package/src/cache_manager.py +520 -0
  87. package/src/embedding_engine.py +671 -0
  88. package/src/graph_engine.py +970 -0
  89. package/src/hnsw_index.py +626 -0
  90. package/src/hybrid_search.py +693 -0
  91. package/src/memory-profiles.py +518 -0
  92. package/src/memory-reset.py +485 -0
  93. package/src/memory_compression.py +999 -0
  94. package/src/memory_store_v2.py +1088 -0
  95. package/src/migrate_v1_to_v2.py +638 -0
  96. package/src/pattern_learner.py +898 -0
  97. package/src/query_optimizer.py +513 -0
  98. package/src/search_engine_v2.py +403 -0
  99. package/src/setup_validator.py +479 -0
  100. package/src/tree_manager.py +720 -0
@@ -0,0 +1,877 @@
1
+ # Architecture Documentation
2
+
3
+ ## SuperLocalMemory V2 - Technical Architecture
4
+
5
+ This document provides a comprehensive technical overview of SuperLocalMemory V2's architecture, design decisions, and implementation details.
6
+
7
+ ---
8
+
9
+ ## Table of Contents
10
+
11
+ - [System Overview](#system-overview)
12
+ - [Universal Integration](#universal-integration)
13
+ - [7-Layer Architecture](#7-layer-architecture)
14
+ - [Component Details](#component-details)
15
+ - [Data Flow](#data-flow)
16
+ - [Technology Stack](#technology-stack)
17
+ - [Design Decisions](#design-decisions)
18
+ - [Performance Characteristics](#performance-characteristics)
19
+ - [Security & Privacy](#security--privacy)
20
+ - [Scalability](#scalability)
21
+
22
+ ---
23
+
24
+ ## System Overview
25
+
26
+ SuperLocalMemory V2 is a **standalone, local-first, intelligent memory system** that transforms flat memory storage into a living knowledge graph with pattern learning capabilities.
27
+
28
+ Works independently or integrates with Claude CLI, GPT, local LLMs, or any AI assistant.
29
+
30
+ ### Core Principles
31
+
32
+ 1. **Local-First:** All data stored locally, zero external API calls
33
+ 2. **Privacy-First:** No telemetry, no tracking, complete user control
34
+ 3. **Intelligence:** Auto-discovery of relationships and patterns
35
+ 4. **Modularity:** Independent layers with clear interfaces
36
+ 5. **Performance:** Sub-second operations for typical workloads (< 500 memories)
37
+
38
+ ### Architecture Philosophy
39
+
40
+ ```
41
+ Simple Storage → Intelligent Organization → Adaptive Learning
42
+ (SQLite) (Graphs + Indexes) (Pattern Recognition)
43
+ ```
44
+
45
+ ---
46
+
47
+ ## Universal Integration
48
+
49
+ **Version 2.3.0-universal** transforms SuperLocalMemory from Claude-Code-only to a universal memory system that works across 16+ IDEs and CLI tools with zero configuration.
50
+
51
+ ### Three-Tier Access Architecture
52
+
53
+ ```
54
+ ┌─────────────────────────────────────────────────────────────┐
55
+ │ ACCESS METHODS │
56
+ ├─────────────────────────────────────────────────────────────┤
57
+ │ TIER 1: MCP (Model Context Protocol) │
58
+ │ • Cursor IDE - Native AI tool access │
59
+ │ • Windsurf IDE - Built-in integration │
60
+ │ • Claude Desktop - Direct MCP support │
61
+ │ • Continue.dev - MCP context providers │
62
+ ├─────────────────────────────────────────────────────────────┤
63
+ │ TIER 2: Skills & Commands │
64
+ │ • Claude Code - /superlocalmemoryv2:* │
65
+ │ • Continue.dev - /slm-* slash commands │
66
+ │ • Cody - Custom commands via settings │
67
+ ├─────────────────────────────────────────────────────────────┤
68
+ │ TIER 3: Universal CLI │
69
+ │ • slm wrapper - Simple command syntax │
70
+ │ • aider-smart - Auto-context injection │
71
+ │ • Any terminal - Direct Python execution │
72
+ └────────────────┬────────────────────────────────────────────┘
73
+
74
+
75
+ ┌───────────────────┐
76
+ │ SuperLocalMemory │
77
+ │ Core (Unchanged) │
78
+ │ memory.db (SQLite) │
79
+ └───────────────────┘
80
+ ```
81
+
82
+ ### MCP Server Implementation
83
+
84
+ **File:** `mcp_server.py` (450+ lines)
85
+
86
+ The MCP server provides native integration with modern AI tools:
87
+
88
+ **6 Tools:**
89
+ - `remember(content, tags, project)` - Save memories
90
+ - `recall(query, limit)` - Search memories
91
+ - `list_recent(limit)` - Recent memories
92
+ - `get_status()` - System status
93
+ - `build_graph()` - Build knowledge graph
94
+ - `switch_profile(name)` - Change profile
95
+
96
+ **4 Resources:**
97
+ - `memory://recent` - Recent memories feed
98
+ - `memory://stats` - System statistics
99
+ - `memory://graph/clusters` - Graph clusters
100
+ - `memory://patterns/identity` - Learned patterns
101
+
102
+ **2 Prompts:**
103
+ - `coding_identity_prompt` - User's coding style
104
+ - `project_context_prompt` - Current project context
105
+
106
+ **Transport Support:**
107
+ - stdio (default) - For local IDE integration
108
+ - HTTP - For remote/network access
109
+
110
+ **Key Design:** Zero duplication - calls existing `memory_store_v2.py` functions
111
+
112
+ ### Auto-Detection System
113
+
114
+ **File:** `install.sh` (enhanced)
115
+
116
+ Installation automatically detects and configures:
117
+
118
+ ```bash
119
+ # Detect installed tools
120
+ [ -d "$HOME/Library/Application Support/Claude" ] && configure_claude_desktop
121
+ [ -d "$HOME/.cursor" ] && configure_cursor
122
+ [ -d "$HOME/.windsurf" ] && configure_windsurf
123
+ [ -d "$HOME/.continue" ] && configure_continue
124
+ ```
125
+
126
+ **For each detected tool:**
127
+ 1. Creates configuration from template
128
+ 2. Substitutes {{INSTALL_DIR}} with actual path
129
+ 3. Backs up existing configs
130
+ 4. Installs new config
131
+ 5. Reports success
132
+
133
+ **Zero manual configuration required.**
134
+
135
+ ### Universal CLI Wrapper
136
+
137
+ **File:** `bin/slm`
138
+
139
+ Provides simple syntax for all operations:
140
+
141
+ ```bash
142
+ slm remember "content" # Calls superlocalmemoryv2:remember
143
+ slm recall "query" # Calls superlocalmemoryv2:recall
144
+ slm list # Recent memories
145
+ slm status # System health
146
+ slm profile list # Profile management
147
+ slm graph build # Knowledge graph
148
+ ```
149
+
150
+ **Implementation:** Bash wrapper routing to existing CLI commands - no duplicate logic
151
+
152
+ ### Backward Compatibility
153
+
154
+ **100% backward compatible:**
155
+ - All original commands work unchanged
156
+ - Database schema unchanged
157
+ - Profile system intact
158
+ - Skills system unchanged
159
+ - Zero breaking changes
160
+
161
+ **Upgrade path:**
162
+ 1. Run `./install.sh`
163
+ 2. Auto-detection configures new tools
164
+ 3. Existing workflows continue working
165
+ 4. New capabilities available immediately
166
+
167
+ ### Integration Matrix
168
+
169
+ | Tool | Integration Method | Configuration |
170
+ |------|-------------------|---------------|
171
+ | Claude Code | Skills (unchanged) | Existing |
172
+ | Cursor | MCP stdio | Auto-detected |
173
+ | Windsurf | MCP stdio | Auto-detected |
174
+ | Claude Desktop | MCP stdio | Auto-detected |
175
+ | Continue.dev | MCP + Skills | Auto-detected |
176
+ | Cody | Custom commands | Auto-detected |
177
+ | Aider | Smart wrapper | Always available |
178
+ | Any terminal | slm wrapper | Always available |
179
+
180
+ ### Design Decision: Additive Architecture
181
+
182
+ **Reasoning:**
183
+ - New files only - zero modifications to core src/
184
+ - Original commands unchanged
185
+ - Optional MCP layer on top
186
+ - Fallback to CLI if MCP unavailable
187
+ - Single source of truth (memory.db)
188
+
189
+ **Trade-offs:**
190
+ - Slight code duplication in wrappers (bash + MCP)
191
+ - More installation complexity (auto-detection)
192
+ - Multiple access paths to same data
193
+
194
+ **Benefits:**
195
+ - Zero risk to existing users
196
+ - Instant rollback (remove new files)
197
+ - Easy testing (each tier independent)
198
+ - Universal adoption without migration
199
+
200
+ ---
201
+
202
+ ## 7-Layer Architecture
203
+
204
+ SuperLocalMemory V2 uses a hierarchical, additive architecture where each layer builds on the previous without replacing it. Version 2.3.0-universal extended the original 4 core layers with 3 universal access layers.
205
+
206
+ ```
207
+ ┌─────────────────────────────────────────────────────────────────┐
208
+ │ SuperLocalMemory V2 │
209
+ │ 7-Layer Universal Architecture │
210
+ └─────────────────────────────────────────────────────────────────┘
211
+
212
+ ┌─────────────────────────────────────────────────────────────────┐
213
+ │ Layer 7: Universal Access │
214
+ │ Three access methods: MCP Protocol, Skills, CLI │
215
+ │ All share one database (~/.claude-memory/memory.db) │
216
+ │ Output: Unified access across 16+ IDEs and tools │
217
+ └─────────────────────────────────────────────────────────────────┘
218
+
219
+ ┌─────────────────────────────────────────────────────────────────┐
220
+ │ Layer 6: MCP Integration (mcp_server.py) │
221
+ │ 6 tools, 4 resources, 2 prompts │
222
+ │ Auto-configured for Claude Desktop, Cursor, Windsurf, etc. │
223
+ │ Output: Native AI tool access via Model Context Protocol │
224
+ └─────────────────────────────────────────────────────────────────┘
225
+
226
+ ┌─────────────────────────────────────────────────────────────────┐
227
+ │ Layer 5: Skills Layer (skills/*) │
228
+ │ 6 slash-command skills for Claude Code, Continue.dev, Cody │
229
+ │ slm-remember, slm-recall, slm-status, slm-list-recent, │
230
+ │ slm-build-graph, slm-switch-profile │
231
+ │ Output: Familiar /command interface across AI tools │
232
+ └─────────────────────────────────────────────────────────────────┘
233
+
234
+ ┌─────────────────────────────────────────────────────────────────┐
235
+ │ Layer 4: Pattern Learning (Identity Profiles) │
236
+ ├─────────────────────────────────────────────────────────────────┤
237
+ │ │
238
+ │ ┌──────────────────┐ ┌──────────────────┐ │
239
+ │ │ Frequency │ │ Context │ │
240
+ │ │ Analysis │ │ Analysis │ │
241
+ │ └──────────────────┘ └──────────────────┘ │
242
+ │ │
243
+ │ ┌──────────────────┐ ┌──────────────────┐ │
244
+ │ │ Terminology │ │ Confidence │ │
245
+ │ │ Learning │ │ Scoring │ │
246
+ │ └──────────────────┘ └──────────────────┘ │
247
+ │ │
248
+ │ Output: Identity profiles with confidence scores │
249
+ └─────────────────────────────────────────────────────────────────┘
250
+
251
+ ┌─────────────────────────────────────────────────────────────────┐
252
+ │ Layer 3: Knowledge Graph (GraphRAG) │
253
+ ├─────────────────────────────────────────────────────────────────┤
254
+ │ │
255
+ │ ┌──────────────────┐ ┌──────────────────┐ │
256
+ │ │ TF-IDF Entity │ │ Leiden │ │
257
+ │ │ Extraction │ │ Clustering │ │
258
+ │ └──────────────────┘ └──────────────────┘ │
259
+ │ │
260
+ │ ┌──────────────────┐ ┌──────────────────┐ │
261
+ │ │ Auto-naming │ │ Relationship │ │
262
+ │ │ Clusters │ │ Discovery │ │
263
+ │ └──────────────────┘ └──────────────────┘ │
264
+ │ │
265
+ │ Output: Clusters of related memories, entity relationships │
266
+ └─────────────────────────────────────────────────────────────────┘
267
+
268
+ ┌─────────────────────────────────────────────────────────────────┐
269
+ │ Layer 2: Hierarchical Index (PageIndex-inspired) │
270
+ ├─────────────────────────────────────────────────────────────────┤
271
+ │ │
272
+ │ ┌──────────────────┐ ┌──────────────────┐ │
273
+ │ │ Tree Structure │ │ Parent-Child │ │
274
+ │ │ Management │ │ Relationships │ │
275
+ │ └──────────────────┘ └──────────────────┘ │
276
+ │ │
277
+ │ ┌──────────────────┐ ┌──────────────────┐ │
278
+ │ │ Fast Navigation │ │ Contextual │ │
279
+ │ │ Paths │ │ Grouping │ │
280
+ │ └──────────────────┘ └──────────────────┘ │
281
+ │ │
282
+ │ Output: Hierarchical organization, breadcrumb navigation │
283
+ └─────────────────────────────────────────────────────────────────┘
284
+
285
+ ┌─────────────────────────────────────────────────────────────────┐
286
+ │ Layer 1: Raw Storage (SQLite Foundation) │
287
+ ├─────────────────────────────────────────────────────────────────┤
288
+ │ │
289
+ │ ┌──────────────────┐ ┌──────────────────┐ │
290
+ │ │ Full-Text │ │ Vector │ │
291
+ │ │ Search (FTS5) │ │ Embeddings │ │
292
+ │ └──────────────────┘ └──────────────────┘ │
293
+ │ │
294
+ │ ┌──────────────────┐ ┌──────────────────┐ │
295
+ │ │ Tags & │ │ Compression │ │
296
+ │ │ Metadata │ │ Archives │ │
297
+ │ └──────────────────┘ └──────────────────┘ │
298
+ │ │
299
+ │ Output: Persistent storage, fast search, metadata │
300
+ └─────────────────────────────────────────────────────────────────┘
301
+ ```
302
+
303
+ **Optional Add-on Layers (v2.2.0):**
304
+ - **Advanced Search:** BM25 engine, HNSW vector index, hybrid search
305
+ - **Visualization:** Web dashboard with timeline, graph view, statistics
306
+
307
+ ---
308
+
309
+ ## Component Details
310
+
311
+ ### Layer 1: Raw Storage (SQLite)
312
+
313
+ **Responsibility:** Persistent storage, basic search, metadata management
314
+
315
+ **Components:**
316
+
317
+ 1. **memory_store_v2.py** - Core CRUD operations
318
+ - Add, update, delete memories
319
+ - Tag management
320
+ - Search interface
321
+
322
+ 2. **SQLite Schema:**
323
+ ```sql
324
+ -- Main memories table
325
+ CREATE TABLE memories (
326
+ id INTEGER PRIMARY KEY,
327
+ content TEXT NOT NULL,
328
+ timestamp DATETIME DEFAULT CURRENT_TIMESTAMP,
329
+ tags TEXT, -- JSON array
330
+ metadata TEXT, -- JSON object
331
+ tier INTEGER DEFAULT 1, -- Compression tier
332
+ parent_id INTEGER, -- For tree structure
333
+ FOREIGN KEY (parent_id) REFERENCES memories(id)
334
+ );
335
+
336
+ -- Full-text search index
337
+ CREATE VIRTUAL TABLE memories_fts USING fts5(
338
+ content, tags,
339
+ content=memories,
340
+ content_rowid=id
341
+ );
342
+
343
+ -- Graph tables
344
+ CREATE TABLE graph_clusters (
345
+ id INTEGER PRIMARY KEY,
346
+ name TEXT,
347
+ description TEXT
348
+ );
349
+
350
+ CREATE TABLE graph_cluster_members (
351
+ cluster_id INTEGER,
352
+ memory_id INTEGER,
353
+ FOREIGN KEY (cluster_id) REFERENCES graph_clusters(id),
354
+ FOREIGN KEY (memory_id) REFERENCES memories(id)
355
+ );
356
+
357
+ -- Pattern learning tables
358
+ CREATE TABLE learned_patterns (
359
+ id INTEGER PRIMARY KEY,
360
+ category TEXT,
361
+ subcategory TEXT,
362
+ pattern_text TEXT,
363
+ confidence REAL,
364
+ frequency INTEGER,
365
+ last_seen DATETIME
366
+ );
367
+ ```
368
+
369
+ 3. **Compression System** (memory_compression.py)
370
+ - Progressive summarization
371
+ - Tier-based compression (Tier 1→2→3)
372
+ - Cold storage archival
373
+
374
+ **Performance:**
375
+ - Full-text search: ~45ms (avg)
376
+ - Insert: <10ms
377
+ - Tag search: ~30ms
378
+
379
+ ---
380
+
381
+ ### Layer 2: Hierarchical Index (PageIndex)
382
+
383
+ **Responsibility:** Tree-based organization, contextual grouping
384
+
385
+ **Components:**
386
+
387
+ 1. **tree_manager.py** - Tree structure operations
388
+ - Parent-child relationship management
389
+ - Breadcrumb generation
390
+ - Subtree queries
391
+
392
+ **Key Features:**
393
+ - Fast navigation through related memories
394
+ - Contextual grouping (e.g., "Project Alpha" → subtasks)
395
+ - Efficient ancestor/descendant queries
396
+
397
+ **Schema Integration:**
398
+ ```sql
399
+ -- Parent-child relationships
400
+ parent_id INTEGER FOREIGN KEY → memories(id)
401
+
402
+ -- Query: Get all children
403
+ SELECT * FROM memories WHERE parent_id = ?
404
+
405
+ -- Query: Get breadcrumb path
406
+ WITH RECURSIVE ancestors AS (...)
407
+ ```
408
+
409
+ **Use Cases:**
410
+ - Project hierarchies
411
+ - Conversation threading
412
+ - Multi-step task tracking
413
+
414
+ ---
415
+
416
+ ### Layer 3: Knowledge Graph (GraphRAG)
417
+
418
+ **Responsibility:** Relationship discovery, auto-clustering, entity extraction
419
+
420
+ **Components:**
421
+
422
+ 1. **graph_engine.py** - Graph construction and queries
423
+ - TF-IDF entity extraction
424
+ - Leiden clustering algorithm
425
+ - Cluster naming via LLM patterns
426
+ - Relationship discovery
427
+
428
+ **Algorithm Pipeline:**
429
+
430
+ ```
431
+ Memories → TF-IDF → Entity Extraction → Similarity Matrix → Leiden Clustering → Named Clusters
432
+ ```
433
+
434
+ **Step-by-Step:**
435
+
436
+ 1. **Entity Extraction (TF-IDF):**
437
+ ```python
438
+ # Extract top terms from each memory
439
+ tfidf_vectorizer = TfidfVectorizer(
440
+ max_features=20,
441
+ stop_words='english',
442
+ ngram_range=(1, 2)
443
+ )
444
+ entities = tfidf_vectorizer.fit_transform(memories)
445
+ ```
446
+
447
+ 2. **Similarity Calculation:**
448
+ ```python
449
+ # Cosine similarity between memories
450
+ similarity_matrix = cosine_similarity(entities)
451
+ ```
452
+
453
+ 3. **Leiden Clustering:**
454
+ ```python
455
+ # Community detection in similarity graph
456
+ import leidenalg
457
+ communities = leidenalg.find_partition(graph, resolution=1.0)
458
+ ```
459
+
460
+ 4. **Auto-Naming:**
461
+ ```python
462
+ # Extract common terms from cluster members
463
+ cluster_name = most_frequent_terms(cluster_memories)
464
+ # Example: "Authentication & Security"
465
+ ```
466
+
467
+ **Performance:**
468
+ - Build time: <30ms (20 memories)
469
+ - Build time: ~2s (100 memories)
470
+ - Query time: <10ms (related memories)
471
+
472
+ **Output:**
473
+ - Clusters with auto-generated names
474
+ - Entity relationships
475
+ - Related memory suggestions
476
+
477
+ ---
478
+
479
+ ### Layer 4: Pattern Learning (xMemory-inspired)
480
+
481
+ **Responsibility:** Learn user preferences, coding style, terminology
482
+
483
+ **Components:**
484
+
485
+ 1. **pattern_learner.py** - Pattern extraction and scoring
486
+ - Frequency analysis
487
+ - Context analysis
488
+ - Confidence scoring
489
+ - Identity profile generation
490
+
491
+ **Analysis Categories:**
492
+
493
+ ```python
494
+ CATEGORIES = {
495
+ "preferences": {
496
+ "framework": ["React", "Vue", "Angular", ...],
497
+ "language": ["Python", "JavaScript", "Go", ...],
498
+ "architecture": ["microservices", "monolith", ...],
499
+ "security": ["JWT", "OAuth", "API keys", ...]
500
+ },
501
+ "style": {
502
+ "code_preferences": ["functional", "OOP", ...],
503
+ "priorities": ["performance", "readability", ...]
504
+ },
505
+ "terminology": {
506
+ "common_terms": [...],
507
+ "technical_vocabulary": [...]
508
+ }
509
+ }
510
+ ```
511
+
512
+ **Confidence Scoring:**
513
+
514
+ ```python
515
+ confidence = frequency / total_memories
516
+
517
+ # Example:
518
+ # "React" appears in 6/10 memories → 60% confidence
519
+ # "Vue" appears in 1/10 memories → 10% confidence
520
+ ```
521
+
522
+ **Output Format:**
523
+
524
+ ```json
525
+ {
526
+ "preferences": {
527
+ "framework": {
528
+ "React": 0.60,
529
+ "Angular": 0.20
530
+ },
531
+ "language": {
532
+ "Python": 0.50,
533
+ "JavaScript": 0.40
534
+ }
535
+ },
536
+ "style": {
537
+ "priorities": {
538
+ "performance": 0.53,
539
+ "readability": 0.30
540
+ }
541
+ }
542
+ }
543
+ ```
544
+
545
+ **Use Cases:**
546
+ - Personalized AI assistant context
547
+ - Style guide generation
548
+ - Skill tracking
549
+ - Learning trajectory analysis
550
+
551
+ ---
552
+
553
+ ## Data Flow
554
+
555
+ ### Write Path (Adding Memory)
556
+
557
+ ```
558
+ User Input
559
+
560
+ memory_store_v2.py: add()
561
+
562
+ SQLite: INSERT into memories table
563
+
564
+ FTS5: Update full-text index
565
+
566
+ [Optional] tree_manager: Set parent_id
567
+
568
+ [Async] graph_engine: Rebuild trigger
569
+
570
+ [Async] pattern_learner: Update trigger
571
+ ```
572
+
573
+ ### Read Path (Search)
574
+
575
+ ```
576
+ User Query
577
+
578
+ memory_store_v2.py: search()
579
+
580
+ SQLite FTS5: Full-text search
581
+
582
+ Results + Metadata
583
+
584
+ [Optional] graph_engine: Enrich with related memories
585
+
586
+ [Optional] pattern_learner: Add context suggestions
587
+
588
+ User Output
589
+ ```
590
+
591
+ ### Graph Build Path
592
+
593
+ ```
594
+ Trigger: Manual or scheduled
595
+
596
+ graph_engine.py: build()
597
+
598
+ Fetch all memories from SQLite
599
+
600
+ TF-IDF: Extract entities
601
+
602
+ Cosine Similarity: Calculate relationships
603
+
604
+ Leiden Algorithm: Detect communities
605
+
606
+ Auto-naming: Generate cluster names
607
+
608
+ SQLite: INSERT into graph_clusters, graph_cluster_members
609
+
610
+ Statistics output
611
+ ```
612
+
613
+ ---
614
+
615
+ ## Technology Stack
616
+
617
+ ### Core Technologies
618
+
619
+ | Component | Technology | Version | Purpose |
620
+ |-----------|-----------|---------|---------|
621
+ | Database | SQLite | 3.35+ | Persistent storage |
622
+ | Search | FTS5 | Built-in | Full-text search |
623
+ | Language | Python | 3.8+ | Core implementation |
624
+ | Clustering | Leiden Algorithm | Custom impl | Community detection |
625
+ | Entity Extraction | TF-IDF | scikit-learn-style | Term importance |
626
+ | CLI | Bash scripts | - | User commands |
627
+
628
+ ### Python Standard Library Only
629
+
630
+ **No external dependencies required for core features:**
631
+ - `sqlite3` - Database
632
+ - `json` - Configuration, metadata
633
+ - `re` - Text processing
634
+ - `datetime` - Timestamps
635
+ - `hashlib` - ID generation
636
+ - `collections` - Data structures
637
+
638
+ **Optional (for advanced features):**
639
+ - `scikit-learn` - Full TF-IDF implementation
640
+ - `leidenalg` - Advanced clustering
641
+
642
+ **Fallback implementations provided** for systems without optional dependencies.
643
+
644
+ ---
645
+
646
+ ## Design Decisions
647
+
648
+ ### Decision 1: SQLite Over NoSQL
649
+
650
+ **Reasoning:**
651
+ - Local-first: No server setup required
652
+ - ACID transactions: Data integrity guaranteed
653
+ - FTS5: Built-in full-text search
654
+ - Widely available: Pre-installed on most systems
655
+ - Zero configuration: Works out of the box
656
+
657
+ **Trade-offs:**
658
+ - Single-writer: Not suitable for high-concurrency scenarios
659
+ - Limited to local disk: No distributed access (feature, not bug)
660
+
661
+ ---
662
+
663
+ ### Decision 2: 7-Layer Additive Architecture
664
+
665
+ **Reasoning:**
666
+ - Modularity: Each layer can be disabled independently
667
+ - Progressive enhancement: System works even if upper layers fail
668
+ - Clear separation: Easy to understand and maintain
669
+ - Testability: Each layer can be tested in isolation
670
+ - Universal access: Layers 5-7 enable multi-IDE support without changing core
671
+
672
+ **Trade-offs:**
673
+ - Slight redundancy: Some data duplicated across layers
674
+ - Build time: Graph/pattern layers require periodic rebuilds
675
+ - More installation complexity: Auto-detection for 16+ IDEs
676
+
677
+ ---
678
+
679
+ ### Decision 3: Local-Only, No External APIs
680
+
681
+ **Reasoning:**
682
+ - Privacy: User data never leaves machine
683
+ - Reliability: No internet dependency
684
+ - Speed: No network latency
685
+ - Cost: No API bills
686
+
687
+ **Trade-offs:**
688
+ - No cloud sync: Multi-device requires manual export/import
689
+ - Limited ML: No access to large models (by design)
690
+
691
+ ---
692
+
693
+ ### Decision 4: TF-IDF + Leiden Clustering
694
+
695
+ **Reasoning:**
696
+ - TF-IDF: Fast, lightweight, no training required
697
+ - Leiden: Better than Louvain, finds quality communities
698
+ - Deterministic: Same input = same output
699
+ - Scalable: O(n log n) complexity
700
+
701
+ **Alternatives considered:**
702
+ - Vector embeddings: Too heavy, requires models
703
+ - LDA: Slower, less interpretable
704
+ - Simple keyword matching: Too simplistic
705
+
706
+ ---
707
+
708
+ ### Decision 5: Confidence Scoring Over Binary Classification
709
+
710
+ **Reasoning:**
711
+ - Nuance: Captures uncertainty in patterns
712
+ - User control: Filter by confidence threshold
713
+ - Adaptability: Patterns evolve as memories grow
714
+ - Transparency: Users see why patterns are suggested
715
+
716
+ **Example:**
717
+ ```
718
+ React: 60% confidence → High priority suggestion
719
+ Vue: 10% confidence → Low priority, exploratory
720
+ ```
721
+
722
+ ---
723
+
724
+ ## Performance Characteristics
725
+
726
+ ### Time Complexity
727
+
728
+ | Operation | Complexity | Typical Time |
729
+ |-----------|-----------|--------------|
730
+ | Add memory | O(1) | <10ms |
731
+ | Search (FTS5) | O(log n) | ~45ms |
732
+ | Graph build | O(n²) worst, O(n log n) avg | ~2s (100 memories) |
733
+ | Pattern update | O(n) | <2s (100 memories) |
734
+ | Find related | O(1) | <10ms |
735
+
736
+ ### Space Complexity
737
+
738
+ | Component | Storage | Notes |
739
+ |-----------|---------|-------|
740
+ | Raw memories | ~1KB/memory | Typical text memory |
741
+ | FTS5 index | ~30% overhead | Full-text search index |
742
+ | Graph data | ~100 bytes/memory | Cluster memberships |
743
+ | Patterns | ~1KB total | Aggregate statistics |
744
+ | Compression (Tier 2) | 60% reduction | Summarized old memories |
745
+ | Compression (Tier 3) | 96% reduction | Archived to JSON |
746
+
747
+ **Example:** 500 memories ≈ 500KB raw + 150KB index + 50KB graph = ~700KB total
748
+
749
+ ---
750
+
751
+ ## Security & Privacy
752
+
753
+ ### Threat Model
754
+
755
+ **Assumptions:**
756
+ - User has physical access to machine
757
+ - Local filesystem is trusted
758
+ - No network threats (local-only)
759
+
760
+ **Protections:**
761
+
762
+ 1. **No External Data Leaks:**
763
+ - Zero API calls
764
+ - No telemetry
765
+ - No auto-updates
766
+
767
+ 2. **Data Integrity:**
768
+ - SQLite ACID transactions
769
+ - Automatic backups before resets
770
+
771
+ 3. **Access Control:**
772
+ - Relies on filesystem permissions
773
+ - Standard Unix/macOS security model
774
+
775
+ **Not Protected Against:**
776
+ - Physical access to machine
777
+ - Root/admin access
778
+ - Filesystem malware
779
+
780
+ **Recommendation:** Use full-disk encryption for sensitive data.
781
+
782
+ ---
783
+
784
+ ## Scalability
785
+
786
+ ### Current Limits (Tested)
787
+
788
+ | Memories | Build Time | Search Time | Database Size |
789
+ |----------|-----------|-------------|---------------|
790
+ | 20 | <0.03s | ~30ms | ~30KB |
791
+ | 100 | ~2s | ~45ms | ~150KB |
792
+ | 500 | ~15s | ~60ms | ~700KB |
793
+ | 1000 | ~45s | ~100ms | ~1.5MB |
794
+
795
+ ### Scaling Strategies
796
+
797
+ **For 1000+ memories:**
798
+ 1. **Profile Splitting:** Separate contexts (work, personal, learning)
799
+ 2. **Compression:** Auto-archive old memories (Tier 3)
800
+ 3. **Incremental Builds:** Graph updates instead of full rebuilds
801
+ 4. **Selective Loading:** Load only active profile
802
+
803
+ **Future Optimizations:**
804
+ - Graph delta updates (vs. full rebuild)
805
+ - Lazy pattern loading
806
+ - Memory pagination for large searches
807
+
808
+ ---
809
+
810
+ ## Extension Points
811
+
812
+ ### Adding New Layers
813
+
814
+ ```python
815
+ # Layer 5 example: Predictive recommendations
816
+ class RecommendationEngine:
817
+ def suggest_next_memory(self, context):
818
+ # Use patterns + graph to predict what user might add next
819
+ pass
820
+ ```
821
+
822
+ ### Adding New Pattern Categories
823
+
824
+ ```python
825
+ # In pattern_learner.py
826
+ CATEGORIES["preferences"]["testing"] = [
827
+ "pytest", "jest", "unittest", "TDD", "BDD"
828
+ ]
829
+ ```
830
+
831
+ ### Custom Clustering Algorithms
832
+
833
+ ```python
834
+ # In graph_engine.py
835
+ def custom_clustering(similarity_matrix):
836
+ # Implement alternative clustering
837
+ pass
838
+ ```
839
+
840
+ ---
841
+
842
+ ## Conclusion
843
+
844
+ SuperLocalMemory V2's architecture provides:
845
+ - **Simplicity:** Standard tools (SQLite, Python stdlib)
846
+ - **Intelligence:** Auto-discovery of patterns and relationships
847
+ - **Privacy:** 100% local, zero external dependencies
848
+ - **Modularity:** Independent layers, clear interfaces
849
+ - **Performance:** Sub-second operations for typical workloads
850
+
851
+ **Design Philosophy:** Start simple (Layer 1), add intelligence progressively (Layers 2-4), enable universal access (Layers 5-7), maintain local-first principles throughout.
852
+
853
+ ---
854
+
855
+ ## Further Reading
856
+
857
+ - [Compression System](COMPRESSION-README.md)
858
+ - [CLI Commands Reference](CLI-COMMANDS-REFERENCE.md)
859
+ - [MCP Troubleshooting](MCP-TROUBLESHOOTING.md)
860
+ - [Universal Integration Guide](UNIVERSAL-INTEGRATION.md)
861
+ - [Wiki: Knowledge Graph Guide](https://github.com/varun369/SuperLocalMemoryV2/wiki/Knowledge-Graph-Guide)
862
+ - [Wiki: Pattern Learning](https://github.com/varun369/SuperLocalMemoryV2/wiki/Pattern-Learning-Explained)
863
+
864
+ ---
865
+
866
+ ## Author
867
+
868
+ **Varun Pratap Bhardwaj**
869
+ *Solution Architect*
870
+
871
+ SuperLocalMemory V2 - Intelligent local memory system for AI coding assistants.
872
+
873
+ ---
874
+
875
+ **Questions about architecture?**
876
+
877
+ See [CONTRIBUTING.md](CONTRIBUTING.md) for development discussions or open an issue on GitHub.