@claude-flow/memory 3.0.0-alpha.2 → 3.0.0-alpha.7
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/README.md +356 -18
- package/dist/agent-memory-scope.d.ts +131 -0
- package/dist/agent-memory-scope.d.ts.map +1 -0
- package/dist/agent-memory-scope.js +215 -0
- package/dist/agent-memory-scope.js.map +1 -0
- package/dist/agent-memory-scope.test.d.ts +8 -0
- package/dist/agent-memory-scope.test.d.ts.map +1 -0
- package/dist/agent-memory-scope.test.js +463 -0
- package/dist/agent-memory-scope.test.js.map +1 -0
- package/dist/agentdb-adapter.d.ts +22 -3
- package/dist/agentdb-adapter.d.ts.map +1 -1
- package/dist/agentdb-adapter.js +135 -8
- package/dist/agentdb-adapter.js.map +1 -1
- package/dist/auto-memory-bridge.d.ts +226 -0
- package/dist/auto-memory-bridge.d.ts.map +1 -0
- package/dist/auto-memory-bridge.js +709 -0
- package/dist/auto-memory-bridge.js.map +1 -0
- package/dist/auto-memory-bridge.test.d.ts +8 -0
- package/dist/auto-memory-bridge.test.d.ts.map +1 -0
- package/dist/auto-memory-bridge.test.js +754 -0
- package/dist/auto-memory-bridge.test.js.map +1 -0
- package/dist/benchmark.test.d.ts +2 -0
- package/dist/benchmark.test.d.ts.map +1 -0
- package/dist/benchmark.test.js +277 -0
- package/dist/benchmark.test.js.map +1 -0
- package/dist/index.d.ts +8 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +8 -0
- package/dist/index.js.map +1 -1
- package/dist/learning-bridge.d.ts +137 -0
- package/dist/learning-bridge.d.ts.map +1 -0
- package/dist/learning-bridge.js +335 -0
- package/dist/learning-bridge.js.map +1 -0
- package/dist/learning-bridge.test.d.ts +8 -0
- package/dist/learning-bridge.test.d.ts.map +1 -0
- package/dist/learning-bridge.test.js +578 -0
- package/dist/learning-bridge.test.js.map +1 -0
- package/dist/memory-graph.d.ts +100 -0
- package/dist/memory-graph.d.ts.map +1 -0
- package/dist/memory-graph.js +333 -0
- package/dist/memory-graph.js.map +1 -0
- package/dist/memory-graph.test.d.ts +8 -0
- package/dist/memory-graph.test.d.ts.map +1 -0
- package/dist/memory-graph.test.js +609 -0
- package/dist/memory-graph.test.js.map +1 -0
- package/dist/types.d.ts +3 -0
- package/dist/types.d.ts.map +1 -1
- package/package.json +15 -4
- package/.agentic-flow/intelligence.json +0 -16
- package/__tests__/coverage/base.css +0 -224
- package/__tests__/coverage/block-navigation.js +0 -87
- package/__tests__/coverage/coverage-final.json +0 -19
- package/__tests__/coverage/favicon.png +0 -0
- package/__tests__/coverage/index.html +0 -206
- package/__tests__/coverage/lcov-report/base.css +0 -224
- package/__tests__/coverage/lcov-report/block-navigation.js +0 -87
- package/__tests__/coverage/lcov-report/favicon.png +0 -0
- package/__tests__/coverage/lcov-report/index.html +0 -206
- package/__tests__/coverage/lcov-report/prettify.css +0 -1
- package/__tests__/coverage/lcov-report/prettify.js +0 -2
- package/__tests__/coverage/lcov-report/sort-arrow-sprite.png +0 -0
- package/__tests__/coverage/lcov-report/sorter.js +0 -210
- package/__tests__/coverage/lcov-report/src/agentdb-adapter.ts.html +0 -2737
- package/__tests__/coverage/lcov-report/src/agentdb-backend.ts.html +0 -3130
- package/__tests__/coverage/lcov-report/src/application/commands/delete-memory.command.ts.html +0 -601
- package/__tests__/coverage/lcov-report/src/application/commands/index.html +0 -131
- package/__tests__/coverage/lcov-report/src/application/commands/store-memory.command.ts.html +0 -394
- package/__tests__/coverage/lcov-report/src/application/queries/index.html +0 -116
- package/__tests__/coverage/lcov-report/src/application/queries/search-memory.query.ts.html +0 -796
- package/__tests__/coverage/lcov-report/src/application/services/index.html +0 -116
- package/__tests__/coverage/lcov-report/src/application/services/memory-application-service.ts.html +0 -793
- package/__tests__/coverage/lcov-report/src/cache-manager.ts.html +0 -1633
- package/__tests__/coverage/lcov-report/src/database-provider.ts.html +0 -1618
- package/__tests__/coverage/lcov-report/src/domain/entities/index.html +0 -116
- package/__tests__/coverage/lcov-report/src/domain/entities/memory-entry.ts.html +0 -952
- package/__tests__/coverage/lcov-report/src/domain/services/index.html +0 -116
- package/__tests__/coverage/lcov-report/src/domain/services/memory-domain-service.ts.html +0 -1294
- package/__tests__/coverage/lcov-report/src/hnsw-index.ts.html +0 -3124
- package/__tests__/coverage/lcov-report/src/hybrid-backend.ts.html +0 -2167
- package/__tests__/coverage/lcov-report/src/index.html +0 -266
- package/__tests__/coverage/lcov-report/src/infrastructure/repositories/hybrid-memory-repository.ts.html +0 -1633
- package/__tests__/coverage/lcov-report/src/infrastructure/repositories/index.html +0 -116
- package/__tests__/coverage/lcov-report/src/migration.ts.html +0 -2092
- package/__tests__/coverage/lcov-report/src/query-builder.ts.html +0 -1711
- package/__tests__/coverage/lcov-report/src/sqlite-backend.ts.html +0 -2281
- package/__tests__/coverage/lcov-report/src/sqljs-backend.ts.html +0 -2374
- package/__tests__/coverage/lcov-report/src/types.ts.html +0 -2266
- package/__tests__/coverage/lcov.info +0 -10238
- package/__tests__/coverage/prettify.css +0 -1
- package/__tests__/coverage/prettify.js +0 -2
- package/__tests__/coverage/sort-arrow-sprite.png +0 -0
- package/__tests__/coverage/sorter.js +0 -210
- package/__tests__/coverage/src/agentdb-adapter.ts.html +0 -2737
- package/__tests__/coverage/src/agentdb-backend.ts.html +0 -3130
- package/__tests__/coverage/src/application/commands/delete-memory.command.ts.html +0 -601
- package/__tests__/coverage/src/application/commands/index.html +0 -131
- package/__tests__/coverage/src/application/commands/store-memory.command.ts.html +0 -394
- package/__tests__/coverage/src/application/queries/index.html +0 -116
- package/__tests__/coverage/src/application/queries/search-memory.query.ts.html +0 -796
- package/__tests__/coverage/src/application/services/index.html +0 -116
- package/__tests__/coverage/src/application/services/memory-application-service.ts.html +0 -793
- package/__tests__/coverage/src/cache-manager.ts.html +0 -1633
- package/__tests__/coverage/src/database-provider.ts.html +0 -1618
- package/__tests__/coverage/src/domain/entities/index.html +0 -116
- package/__tests__/coverage/src/domain/entities/memory-entry.ts.html +0 -952
- package/__tests__/coverage/src/domain/services/index.html +0 -116
- package/__tests__/coverage/src/domain/services/memory-domain-service.ts.html +0 -1294
- package/__tests__/coverage/src/hnsw-index.ts.html +0 -3124
- package/__tests__/coverage/src/hybrid-backend.ts.html +0 -2167
- package/__tests__/coverage/src/index.html +0 -266
- package/__tests__/coverage/src/infrastructure/repositories/hybrid-memory-repository.ts.html +0 -1633
- package/__tests__/coverage/src/infrastructure/repositories/index.html +0 -116
- package/__tests__/coverage/src/migration.ts.html +0 -2092
- package/__tests__/coverage/src/query-builder.ts.html +0 -1711
- package/__tests__/coverage/src/sqlite-backend.ts.html +0 -2281
- package/__tests__/coverage/src/sqljs-backend.ts.html +0 -2374
- package/__tests__/coverage/src/types.ts.html +0 -2266
- package/benchmarks/cache-hit-rate.bench.ts +0 -535
- package/benchmarks/hnsw-indexing.bench.ts +0 -552
- package/benchmarks/memory-write.bench.ts +0 -469
- package/benchmarks/vector-search.bench.ts +0 -449
- package/docs/AGENTDB-INTEGRATION.md +0 -388
- package/docs/CROSS_PLATFORM.md +0 -505
- package/docs/WINDOWS_SUPPORT.md +0 -422
- package/examples/agentdb-example.ts +0 -345
- package/examples/cross-platform-usage.ts +0 -326
- package/framework/benchmark.ts +0 -112
- package/src/agentdb-adapter.ts +0 -884
- package/src/agentdb-backend.test.ts +0 -339
- package/src/agentdb-backend.ts +0 -1016
- package/src/application/commands/delete-memory.command.ts +0 -172
- package/src/application/commands/store-memory.command.ts +0 -103
- package/src/application/index.ts +0 -36
- package/src/application/queries/search-memory.query.ts +0 -237
- package/src/application/services/memory-application-service.ts +0 -236
- package/src/cache-manager.ts +0 -516
- package/src/database-provider.test.ts +0 -364
- package/src/database-provider.ts +0 -511
- package/src/domain/entities/memory-entry.ts +0 -289
- package/src/domain/index.ts +0 -35
- package/src/domain/repositories/memory-repository.interface.ts +0 -120
- package/src/domain/services/memory-domain-service.ts +0 -403
- package/src/hnsw-index.ts +0 -1013
- package/src/hybrid-backend.test.ts +0 -399
- package/src/hybrid-backend.ts +0 -694
- package/src/index.ts +0 -515
- package/src/infrastructure/index.ts +0 -23
- package/src/infrastructure/repositories/hybrid-memory-repository.ts +0 -516
- package/src/migration.ts +0 -669
- package/src/query-builder.ts +0 -542
- package/src/sqlite-backend.ts +0 -732
- package/src/sqljs-backend.ts +0 -763
- package/src/types.ts +0 -727
- package/tmp.json +0 -0
- package/tsconfig.json +0 -9
- package/tsconfig.tsbuildinfo +0 -1
- package/verify-cross-platform.ts +0 -170
package/README.md
CHANGED
|
@@ -6,12 +6,16 @@
|
|
|
6
6
|
[](https://www.typescriptlang.org/)
|
|
7
7
|
[](https://github.com/ruvnet/claude-flow)
|
|
8
8
|
|
|
9
|
-
> High-performance memory module for Claude Flow V3 - AgentDB unification, HNSW indexing, vector search, and hybrid SQLite+AgentDB backend (ADR-009).
|
|
9
|
+
> High-performance memory module for Claude Flow V3 - AgentDB unification, HNSW indexing, vector search, self-learning knowledge graph, and hybrid SQLite+AgentDB backend (ADR-009).
|
|
10
10
|
|
|
11
11
|
## Features
|
|
12
12
|
|
|
13
13
|
- **150x-12,500x Faster Search** - HNSW (Hierarchical Navigable Small World) vector index for ultra-fast similarity search
|
|
14
14
|
- **Hybrid Backend** - SQLite for structured data + AgentDB for vectors (ADR-009)
|
|
15
|
+
- **Auto Memory Bridge** - Bidirectional sync between Claude Code auto memory and AgentDB (ADR-048)
|
|
16
|
+
- **Self-Learning** - LearningBridge connects insights to SONA/ReasoningBank neural pipeline (ADR-049)
|
|
17
|
+
- **Knowledge Graph** - PageRank + label propagation community detection over memory entries (ADR-049)
|
|
18
|
+
- **Agent-Scoped Memory** - 3-scope agent memory (project/local/user) with cross-agent knowledge transfer (ADR-049)
|
|
15
19
|
- **Vector Quantization** - Binary, scalar, and product quantization for 4-32x memory reduction
|
|
16
20
|
- **Multiple Distance Metrics** - Cosine, Euclidean, dot product, and Manhattan distance
|
|
17
21
|
- **Query Builder** - Fluent API for building complex memory queries
|
|
@@ -188,16 +192,6 @@ await migration.execute({
|
|
|
188
192
|
});
|
|
189
193
|
```
|
|
190
194
|
|
|
191
|
-
## Performance Benchmarks
|
|
192
|
-
|
|
193
|
-
| Operation | V2 Performance | V3 Performance | Improvement |
|
|
194
|
-
|-----------|---------------|----------------|-------------|
|
|
195
|
-
| Vector Search | 150ms | <1ms | **150x** |
|
|
196
|
-
| Bulk Insert | 500ms | 5ms | **100x** |
|
|
197
|
-
| Memory Write | 50ms | <5ms | **10x** |
|
|
198
|
-
| Cache Hit | 5ms | <0.1ms | **50x** |
|
|
199
|
-
| Index Build | 10s | 800ms | **12.5x** |
|
|
200
|
-
|
|
201
195
|
## Quantization Options
|
|
202
196
|
|
|
203
197
|
```typescript
|
|
@@ -220,16 +214,358 @@ const productIndex = new HNSWIndex({
|
|
|
220
214
|
});
|
|
221
215
|
```
|
|
222
216
|
|
|
217
|
+
## Auto Memory Bridge (ADR-048)
|
|
218
|
+
|
|
219
|
+
Bidirectional sync between Claude Code's [auto memory](https://code.claude.com/docs/en/memory) files and AgentDB. Auto memory is a persistent directory (`~/.claude/projects/<project>/memory/`) where Claude writes learnings as markdown. `MEMORY.md` (first 200 lines) is loaded into the system prompt; topic files are read on demand.
|
|
220
|
+
|
|
221
|
+
### Quick Start
|
|
222
|
+
|
|
223
|
+
```typescript
|
|
224
|
+
import { AutoMemoryBridge } from '@claude-flow/memory';
|
|
225
|
+
|
|
226
|
+
const bridge = new AutoMemoryBridge(memoryBackend, {
|
|
227
|
+
workingDir: '/workspaces/my-project',
|
|
228
|
+
syncMode: 'on-session-end', // 'on-write' | 'on-session-end' | 'periodic'
|
|
229
|
+
pruneStrategy: 'confidence-weighted', // 'confidence-weighted' | 'fifo' | 'lru'
|
|
230
|
+
});
|
|
231
|
+
|
|
232
|
+
// Record an insight (stores in AgentDB + optionally writes to files)
|
|
233
|
+
await bridge.recordInsight({
|
|
234
|
+
category: 'debugging',
|
|
235
|
+
summary: 'HNSW index requires initialization before search',
|
|
236
|
+
source: 'agent:tester',
|
|
237
|
+
confidence: 0.95,
|
|
238
|
+
});
|
|
239
|
+
|
|
240
|
+
// Sync buffered insights to auto memory files
|
|
241
|
+
const syncResult = await bridge.syncToAutoMemory();
|
|
242
|
+
|
|
243
|
+
// Import existing auto memory files into AgentDB (on session start)
|
|
244
|
+
const importResult = await bridge.importFromAutoMemory();
|
|
245
|
+
|
|
246
|
+
// Curate MEMORY.md index (stays under 200-line limit)
|
|
247
|
+
await bridge.curateIndex();
|
|
248
|
+
|
|
249
|
+
// Check status
|
|
250
|
+
const status = bridge.getStatus();
|
|
251
|
+
```
|
|
252
|
+
|
|
253
|
+
### Sync Modes
|
|
254
|
+
|
|
255
|
+
| Mode | Behavior |
|
|
256
|
+
|------|----------|
|
|
257
|
+
| `on-write` | Writes to files immediately on `recordInsight()` |
|
|
258
|
+
| `on-session-end` | Buffers insights, flushes on `syncToAutoMemory()` |
|
|
259
|
+
| `periodic` | Auto-syncs on a configurable interval |
|
|
260
|
+
|
|
261
|
+
### Insight Categories
|
|
262
|
+
|
|
263
|
+
| Category | Topic File | Description |
|
|
264
|
+
|----------|-----------|-------------|
|
|
265
|
+
| `project-patterns` | `patterns.md` | Code patterns and conventions |
|
|
266
|
+
| `debugging` | `debugging.md` | Bug fixes and debugging insights |
|
|
267
|
+
| `architecture` | `architecture.md` | Design decisions and module relationships |
|
|
268
|
+
| `performance` | `performance.md` | Benchmarks and optimization results |
|
|
269
|
+
| `security` | `security.md` | Security findings and CVE notes |
|
|
270
|
+
| `preferences` | `preferences.md` | User and project preferences |
|
|
271
|
+
| `swarm-results` | `swarm-results.md` | Multi-agent swarm outcomes |
|
|
272
|
+
|
|
273
|
+
### Key Optimizations
|
|
274
|
+
|
|
275
|
+
- **Batch import** - `bulkInsert()` instead of individual `store()` calls
|
|
276
|
+
- **Pre-fetched hashes** - Single query for content-hash dedup during import
|
|
277
|
+
- **Async I/O** - `node:fs/promises` for non-blocking writes
|
|
278
|
+
- **Exact dedup** - `hasSummaryLine()` uses bullet-prefix matching, not substring
|
|
279
|
+
- **O(1) sync tracking** - `syncedInsightKeys` Set prevents double-write race
|
|
280
|
+
- **Prune-before-build** - Avoids O(n^2) index rebuild loop
|
|
281
|
+
|
|
282
|
+
### Utility Functions
|
|
283
|
+
|
|
284
|
+
```typescript
|
|
285
|
+
import {
|
|
286
|
+
resolveAutoMemoryDir, // Derive auto memory path from working dir
|
|
287
|
+
findGitRoot, // Walk up to find .git root
|
|
288
|
+
parseMarkdownEntries, // Parse ## headings into structured entries
|
|
289
|
+
extractSummaries, // Extract bullet summaries, strip metadata
|
|
290
|
+
formatInsightLine, // Format insight as markdown bullet
|
|
291
|
+
hashContent, // SHA-256 truncated to 16 hex chars
|
|
292
|
+
pruneTopicFile, // Keep topic files under line limit
|
|
293
|
+
hasSummaryLine, // Exact bullet-prefix dedup check
|
|
294
|
+
} from '@claude-flow/memory';
|
|
295
|
+
```
|
|
296
|
+
|
|
297
|
+
### Types
|
|
298
|
+
|
|
299
|
+
```typescript
|
|
300
|
+
import type {
|
|
301
|
+
AutoMemoryBridgeConfig,
|
|
302
|
+
MemoryInsight,
|
|
303
|
+
InsightCategory,
|
|
304
|
+
SyncDirection,
|
|
305
|
+
SyncMode,
|
|
306
|
+
PruneStrategy,
|
|
307
|
+
SyncResult,
|
|
308
|
+
ImportResult,
|
|
309
|
+
} from '@claude-flow/memory';
|
|
310
|
+
```
|
|
311
|
+
|
|
312
|
+
## Self-Learning Bridge (ADR-049)
|
|
313
|
+
|
|
314
|
+
Connects insights to the `@claude-flow/neural` learning pipeline. When neural is unavailable, all operations degrade to no-ops.
|
|
315
|
+
|
|
316
|
+
### Quick Start
|
|
317
|
+
|
|
318
|
+
```typescript
|
|
319
|
+
import { AutoMemoryBridge, LearningBridge } from '@claude-flow/memory';
|
|
320
|
+
|
|
321
|
+
const bridge = new AutoMemoryBridge(backend, {
|
|
322
|
+
workingDir: '/workspaces/my-project',
|
|
323
|
+
learning: {
|
|
324
|
+
sonaMode: 'balanced',
|
|
325
|
+
confidenceDecayRate: 0.005, // Per-hour decay
|
|
326
|
+
accessBoostAmount: 0.03, // Boost per access
|
|
327
|
+
consolidationThreshold: 10, // Min insights before consolidation
|
|
328
|
+
},
|
|
329
|
+
});
|
|
330
|
+
|
|
331
|
+
// Insights now trigger learning trajectories automatically
|
|
332
|
+
await bridge.recordInsight({
|
|
333
|
+
category: 'debugging',
|
|
334
|
+
summary: 'Connection pool exhaustion on high load',
|
|
335
|
+
source: 'agent:tester',
|
|
336
|
+
confidence: 0.9,
|
|
337
|
+
});
|
|
338
|
+
|
|
339
|
+
// Consolidation runs JUDGE/DISTILL/CONSOLIDATE pipeline
|
|
340
|
+
await bridge.syncToAutoMemory(); // Calls consolidate() first
|
|
341
|
+
```
|
|
342
|
+
|
|
343
|
+
### Standalone Usage
|
|
344
|
+
|
|
345
|
+
```typescript
|
|
346
|
+
import { LearningBridge } from '@claude-flow/memory';
|
|
347
|
+
|
|
348
|
+
const lb = new LearningBridge(backend, {
|
|
349
|
+
// Optional: inject neural loader for custom setups
|
|
350
|
+
neuralLoader: async () => {
|
|
351
|
+
const { NeuralLearningSystem } = await import('@claude-flow/neural');
|
|
352
|
+
return new NeuralLearningSystem();
|
|
353
|
+
},
|
|
354
|
+
});
|
|
355
|
+
|
|
356
|
+
// Boost confidence when insight is accessed
|
|
357
|
+
await lb.onInsightAccessed('entry-123'); // +0.03 confidence
|
|
358
|
+
|
|
359
|
+
// Apply time-based decay
|
|
360
|
+
const decayed = await lb.decayConfidences('default'); // -0.005/hour
|
|
361
|
+
|
|
362
|
+
// Find similar patterns via ReasoningBank
|
|
363
|
+
const patterns = await lb.findSimilarPatterns('connection pooling');
|
|
364
|
+
|
|
365
|
+
// Get learning statistics
|
|
366
|
+
const stats = lb.getStats();
|
|
367
|
+
// { totalTrajectories, activeTrajectories, completedTrajectories,
|
|
368
|
+
// totalConsolidations, accessBoosts, ... }
|
|
369
|
+
```
|
|
370
|
+
|
|
371
|
+
### Confidence Lifecycle
|
|
372
|
+
|
|
373
|
+
| Event | Effect | Range |
|
|
374
|
+
|-------|--------|-------|
|
|
375
|
+
| Insight recorded | Initial confidence from source | 0.1 - 1.0 |
|
|
376
|
+
| Insight accessed | +0.03 per access | Capped at 1.0 |
|
|
377
|
+
| Time decay | -0.005 per hour since last access | Floored at 0.1 |
|
|
378
|
+
| Consolidation | Neural pipeline may adjust | 0.1 - 1.0 |
|
|
379
|
+
|
|
380
|
+
## Knowledge Graph (ADR-049)
|
|
381
|
+
|
|
382
|
+
Pure TypeScript knowledge graph with PageRank and community detection. No external graph libraries required.
|
|
383
|
+
|
|
384
|
+
### Quick Start
|
|
385
|
+
|
|
386
|
+
```typescript
|
|
387
|
+
import { AutoMemoryBridge, MemoryGraph } from '@claude-flow/memory';
|
|
388
|
+
|
|
389
|
+
const bridge = new AutoMemoryBridge(backend, {
|
|
390
|
+
workingDir: '/workspaces/my-project',
|
|
391
|
+
graph: {
|
|
392
|
+
similarityThreshold: 0.8,
|
|
393
|
+
pageRankDamping: 0.85,
|
|
394
|
+
maxNodes: 5000,
|
|
395
|
+
},
|
|
396
|
+
});
|
|
397
|
+
|
|
398
|
+
// Graph builds automatically on import
|
|
399
|
+
await bridge.importFromAutoMemory();
|
|
400
|
+
|
|
401
|
+
// Curation uses PageRank to prioritize influential insights
|
|
402
|
+
await bridge.curateIndex();
|
|
403
|
+
```
|
|
404
|
+
|
|
405
|
+
### Standalone Usage
|
|
406
|
+
|
|
407
|
+
```typescript
|
|
408
|
+
import { MemoryGraph } from '@claude-flow/memory';
|
|
409
|
+
|
|
410
|
+
const graph = new MemoryGraph({
|
|
411
|
+
pageRankDamping: 0.85,
|
|
412
|
+
pageRankIterations: 50,
|
|
413
|
+
pageRankConvergence: 1e-6,
|
|
414
|
+
maxNodes: 5000,
|
|
415
|
+
});
|
|
416
|
+
|
|
417
|
+
// Build from backend entries
|
|
418
|
+
await graph.buildFromBackend(backend, 'my-namespace');
|
|
419
|
+
|
|
420
|
+
// Or build manually
|
|
421
|
+
graph.addNode(entry);
|
|
422
|
+
graph.addEdge('entry-1', 'entry-2', 'reference', 1.0);
|
|
423
|
+
graph.addEdge('entry-1', 'entry-3', 'similar', 0.9);
|
|
424
|
+
|
|
425
|
+
// Compute PageRank (power iteration)
|
|
426
|
+
const ranks = graph.computePageRank();
|
|
427
|
+
|
|
428
|
+
// Detect communities (label propagation)
|
|
429
|
+
const communities = graph.detectCommunities();
|
|
430
|
+
|
|
431
|
+
// Graph-aware ranking: blend vector score + PageRank
|
|
432
|
+
const ranked = graph.rankWithGraph(searchResults, 0.7);
|
|
433
|
+
// alpha=0.7 means 70% vector score + 30% PageRank
|
|
434
|
+
|
|
435
|
+
// Get most influential insights for MEMORY.md
|
|
436
|
+
const topNodes = graph.getTopNodes(20);
|
|
437
|
+
|
|
438
|
+
// BFS traversal for related insights
|
|
439
|
+
const neighbors = graph.getNeighbors('entry-1', 2); // depth=2
|
|
440
|
+
```
|
|
441
|
+
|
|
442
|
+
### Edge Types
|
|
443
|
+
|
|
444
|
+
| Type | Source | Description |
|
|
445
|
+
|------|--------|-------------|
|
|
446
|
+
| `reference` | `MemoryEntry.references` | Explicit cross-references between entries |
|
|
447
|
+
| `similar` | HNSW search | Auto-created when similarity > threshold |
|
|
448
|
+
| `temporal` | Timestamps | Entries created in same time window |
|
|
449
|
+
| `co-accessed` | Access patterns | Entries frequently accessed together |
|
|
450
|
+
| `causal` | Learning pipeline | Cause-effect relationships |
|
|
451
|
+
|
|
452
|
+
### Performance
|
|
453
|
+
|
|
454
|
+
| Operation | Result | Target |
|
|
455
|
+
|-----------|--------|--------|
|
|
456
|
+
| Graph build (1k nodes) | 2.78 ms | <200 ms |
|
|
457
|
+
| PageRank (1k nodes) | 12.21 ms | <100 ms |
|
|
458
|
+
| Community detection (1k) | 19.62 ms | — |
|
|
459
|
+
| `rankWithGraph(10)` | 0.006 ms | — |
|
|
460
|
+
| `getTopNodes(20)` | 0.308 ms | — |
|
|
461
|
+
| `getNeighbors(d=2)` | 0.005 ms | — |
|
|
462
|
+
|
|
463
|
+
## Agent-Scoped Memory (ADR-049)
|
|
464
|
+
|
|
465
|
+
Maps Claude Code's 3-scope agent memory directories for per-agent knowledge isolation and cross-agent transfer.
|
|
466
|
+
|
|
467
|
+
### Quick Start
|
|
468
|
+
|
|
469
|
+
```typescript
|
|
470
|
+
import { createAgentBridge, transferKnowledge } from '@claude-flow/memory';
|
|
471
|
+
|
|
472
|
+
// Create a bridge for a specific agent scope
|
|
473
|
+
const agentBridge = createAgentBridge(backend, {
|
|
474
|
+
agentName: 'my-coder',
|
|
475
|
+
scope: 'project', // 'project' | 'local' | 'user'
|
|
476
|
+
workingDir: '/workspaces/my-project',
|
|
477
|
+
});
|
|
478
|
+
|
|
479
|
+
// Record insights scoped to this agent
|
|
480
|
+
await agentBridge.recordInsight({
|
|
481
|
+
category: 'debugging',
|
|
482
|
+
summary: 'Use connection pooling for DB calls',
|
|
483
|
+
source: 'agent:my-coder',
|
|
484
|
+
confidence: 0.95,
|
|
485
|
+
});
|
|
486
|
+
|
|
487
|
+
// Transfer high-confidence insights between agents
|
|
488
|
+
const result = await transferKnowledge(sourceBackend, targetBridge, {
|
|
489
|
+
sourceNamespace: 'learnings',
|
|
490
|
+
minConfidence: 0.8, // Only transfer confident insights
|
|
491
|
+
maxEntries: 20,
|
|
492
|
+
categories: ['debugging', 'architecture'],
|
|
493
|
+
});
|
|
494
|
+
// { transferred: 15, skipped: 5 }
|
|
495
|
+
```
|
|
496
|
+
|
|
497
|
+
### Scope Paths
|
|
498
|
+
|
|
499
|
+
| Scope | Directory | Use Case |
|
|
500
|
+
|-------|-----------|----------|
|
|
501
|
+
| `project` | `<gitRoot>/.claude/agent-memory/<agent>/` | Project-specific learnings |
|
|
502
|
+
| `local` | `<gitRoot>/.claude/agent-memory-local/<agent>/` | Machine-local data |
|
|
503
|
+
| `user` | `~/.claude/agent-memory/<agent>/` | Cross-project user knowledge |
|
|
504
|
+
|
|
505
|
+
### Utilities
|
|
506
|
+
|
|
507
|
+
```typescript
|
|
508
|
+
import {
|
|
509
|
+
resolveAgentMemoryDir, // Get scope directory path
|
|
510
|
+
createAgentBridge, // Create scoped AutoMemoryBridge
|
|
511
|
+
transferKnowledge, // Cross-agent knowledge sharing
|
|
512
|
+
listAgentScopes, // Discover existing agent scopes
|
|
513
|
+
} from '@claude-flow/memory';
|
|
514
|
+
|
|
515
|
+
// Resolve path for an agent scope
|
|
516
|
+
const dir = resolveAgentMemoryDir('my-agent', 'project');
|
|
517
|
+
// → /workspaces/my-project/.claude/agent-memory/my-agent/
|
|
518
|
+
|
|
519
|
+
// List all agent scopes in a directory
|
|
520
|
+
const scopes = await listAgentScopes('/workspaces/my-project');
|
|
521
|
+
// [{ agentName: 'coder', scope: 'project', path: '...' }, ...]
|
|
522
|
+
```
|
|
523
|
+
|
|
524
|
+
## Performance Benchmarks
|
|
525
|
+
|
|
526
|
+
| Operation | V2 Performance | V3 Performance | Improvement |
|
|
527
|
+
|-----------|---------------|----------------|-------------|
|
|
528
|
+
| Vector Search | 150ms | <1ms | **150x** |
|
|
529
|
+
| Bulk Insert | 500ms | 5ms | **100x** |
|
|
530
|
+
| Memory Write | 50ms | <5ms | **10x** |
|
|
531
|
+
| Cache Hit | 5ms | <0.1ms | **50x** |
|
|
532
|
+
| Index Build | 10s | 800ms | **12.5x** |
|
|
533
|
+
|
|
534
|
+
### ADR-049 Benchmarks
|
|
535
|
+
|
|
536
|
+
| Operation | Actual | Target | Headroom |
|
|
537
|
+
|-----------|--------|--------|----------|
|
|
538
|
+
| Graph build (1k nodes) | 2.78 ms | <200 ms | **71.9x** |
|
|
539
|
+
| PageRank (1k nodes) | 12.21 ms | <100 ms | **8.2x** |
|
|
540
|
+
| Insight recording | 0.12 ms/each | <5 ms/each | **41.0x** |
|
|
541
|
+
| Consolidation | 0.26 ms | <500 ms | **1,955x** |
|
|
542
|
+
| Confidence decay (1k) | 0.23 ms | <50 ms | **215x** |
|
|
543
|
+
| Knowledge transfer | 1.25 ms | <100 ms | **80.0x** |
|
|
544
|
+
|
|
223
545
|
## TypeScript Types
|
|
224
546
|
|
|
225
547
|
```typescript
|
|
226
548
|
import type {
|
|
227
|
-
|
|
228
|
-
HNSWStats,
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
549
|
+
// Core
|
|
550
|
+
HNSWConfig, HNSWStats, SearchResult, MemoryEntry,
|
|
551
|
+
QuantizationConfig, DistanceMetric,
|
|
552
|
+
|
|
553
|
+
// Auto Memory Bridge (ADR-048)
|
|
554
|
+
AutoMemoryBridgeConfig, MemoryInsight, InsightCategory,
|
|
555
|
+
SyncDirection, SyncMode, PruneStrategy,
|
|
556
|
+
SyncResult, ImportResult,
|
|
557
|
+
|
|
558
|
+
// Learning Bridge (ADR-049)
|
|
559
|
+
LearningBridgeConfig, LearningStats,
|
|
560
|
+
ConsolidateResult, PatternMatch,
|
|
561
|
+
|
|
562
|
+
// Knowledge Graph (ADR-049)
|
|
563
|
+
MemoryGraphConfig, GraphNode, GraphEdge,
|
|
564
|
+
GraphStats, RankedResult, EdgeType,
|
|
565
|
+
|
|
566
|
+
// Agent Scope (ADR-049)
|
|
567
|
+
AgentMemoryScope, AgentScopedConfig,
|
|
568
|
+
TransferOptions, TransferResult,
|
|
233
569
|
} from '@claude-flow/memory';
|
|
234
570
|
```
|
|
235
571
|
|
|
@@ -238,11 +574,13 @@ import type {
|
|
|
238
574
|
- `agentdb` - Vector database engine
|
|
239
575
|
- `better-sqlite3` - SQLite driver (native)
|
|
240
576
|
- `sql.js` - SQLite driver (WASM fallback)
|
|
577
|
+
- `@claude-flow/neural` - **Optional peer dependency** for self-learning (graceful fallback when unavailable)
|
|
241
578
|
|
|
242
579
|
## Related Packages
|
|
243
580
|
|
|
244
|
-
- [@claude-flow/neural](../neural) - Neural learning integration
|
|
581
|
+
- [@claude-flow/neural](../neural) - Neural learning integration (SONA, ReasoningBank, EWC++)
|
|
245
582
|
- [@claude-flow/shared](../shared) - Shared types and utilities
|
|
583
|
+
- [@claude-flow/hooks](../hooks) - Session lifecycle hooks for auto memory sync
|
|
246
584
|
|
|
247
585
|
## License
|
|
248
586
|
|
|
@@ -0,0 +1,131 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Agent-Scoped Memory - Support for Claude Code's 3-scope agent memory directories
|
|
3
|
+
*
|
|
4
|
+
* Claude Code organizes agent memory into three scopes:
|
|
5
|
+
* - **project**: Shared across all collaborators (checked into git)
|
|
6
|
+
* - **local**: Machine-specific, not shared (gitignored)
|
|
7
|
+
* - **user**: Global per-user, spans all projects
|
|
8
|
+
*
|
|
9
|
+
* Each scope stores agent-specific memory in a named subdirectory,
|
|
10
|
+
* enabling isolated yet transferable knowledge between agents.
|
|
11
|
+
*
|
|
12
|
+
* @module @claude-flow/memory/agent-memory-scope
|
|
13
|
+
*/
|
|
14
|
+
import type { IMemoryBackend } from './types.js';
|
|
15
|
+
import { AutoMemoryBridge } from './auto-memory-bridge.js';
|
|
16
|
+
import type { AutoMemoryBridgeConfig, InsightCategory } from './auto-memory-bridge.js';
|
|
17
|
+
/** Claude Code's 3-scope agent memory system */
|
|
18
|
+
export type AgentMemoryScope = 'project' | 'local' | 'user';
|
|
19
|
+
/** Configuration for agent-scoped memory bridge */
|
|
20
|
+
export interface AgentScopedConfig extends AutoMemoryBridgeConfig {
|
|
21
|
+
/** Agent name (used in directory path) */
|
|
22
|
+
agentName: string;
|
|
23
|
+
/** Memory scope */
|
|
24
|
+
scope: AgentMemoryScope;
|
|
25
|
+
}
|
|
26
|
+
/** Options for knowledge transfer between agents */
|
|
27
|
+
export interface TransferOptions {
|
|
28
|
+
/** Source namespace to transfer from */
|
|
29
|
+
sourceNamespace: string;
|
|
30
|
+
/** Minimum confidence to include (default: 0.8) */
|
|
31
|
+
minConfidence?: number;
|
|
32
|
+
/** Maximum entries to transfer (default: 20) */
|
|
33
|
+
maxEntries?: number;
|
|
34
|
+
/** Filter by categories */
|
|
35
|
+
categories?: InsightCategory[];
|
|
36
|
+
}
|
|
37
|
+
/** Result of a knowledge transfer */
|
|
38
|
+
export interface TransferResult {
|
|
39
|
+
/** Number of entries transferred */
|
|
40
|
+
transferred: number;
|
|
41
|
+
/** Number of entries skipped (below threshold or duplicate) */
|
|
42
|
+
skipped: number;
|
|
43
|
+
}
|
|
44
|
+
/**
|
|
45
|
+
* Resolve the agent memory directory for a given agent name, scope, and working directory.
|
|
46
|
+
*
|
|
47
|
+
* Path resolution matches Claude Code binary behavior:
|
|
48
|
+
* ```
|
|
49
|
+
* project: <gitRoot>/.claude/agent-memory/<agentName>/
|
|
50
|
+
* local: <gitRoot>/.claude/agent-memory-local/<agentName>/
|
|
51
|
+
* user: ~/.claude/agent-memory/<agentName>/
|
|
52
|
+
* ```
|
|
53
|
+
*
|
|
54
|
+
* Agent names are sanitized to prevent path traversal attacks.
|
|
55
|
+
*
|
|
56
|
+
* @param agentName - The agent identifier
|
|
57
|
+
* @param scope - Memory scope: project, local, or user
|
|
58
|
+
* @param workingDir - Working directory for git root detection (defaults to cwd)
|
|
59
|
+
* @returns Absolute path to the agent's memory directory
|
|
60
|
+
*/
|
|
61
|
+
export declare function resolveAgentMemoryDir(agentName: string, scope: AgentMemoryScope, workingDir?: string): string;
|
|
62
|
+
/**
|
|
63
|
+
* Create an AutoMemoryBridge configured for a specific agent scope.
|
|
64
|
+
*
|
|
65
|
+
* This is the primary factory for creating scoped bridges. It resolves
|
|
66
|
+
* the correct memory directory based on agent name and scope, then
|
|
67
|
+
* delegates to AutoMemoryBridge for the actual sync logic.
|
|
68
|
+
*
|
|
69
|
+
* @param backend - The AgentDB memory backend
|
|
70
|
+
* @param config - Agent-scoped configuration
|
|
71
|
+
* @returns A configured AutoMemoryBridge instance
|
|
72
|
+
*
|
|
73
|
+
* @example
|
|
74
|
+
* ```typescript
|
|
75
|
+
* const bridge = createAgentBridge(backend, {
|
|
76
|
+
* agentName: 'coder',
|
|
77
|
+
* scope: 'project',
|
|
78
|
+
* syncMode: 'on-write',
|
|
79
|
+
* });
|
|
80
|
+
* await bridge.recordInsight({ ... });
|
|
81
|
+
* ```
|
|
82
|
+
*/
|
|
83
|
+
export declare function createAgentBridge(backend: IMemoryBackend, config: AgentScopedConfig): AutoMemoryBridge;
|
|
84
|
+
/**
|
|
85
|
+
* Transfer knowledge from a source backend namespace into a target bridge.
|
|
86
|
+
*
|
|
87
|
+
* Queries high-confidence entries from the source and records them as
|
|
88
|
+
* insights in the target bridge. Useful for cross-agent knowledge sharing
|
|
89
|
+
* or promoting learnings from one scope to another.
|
|
90
|
+
*
|
|
91
|
+
* @param sourceBackend - Backend to query entries from
|
|
92
|
+
* @param targetBridge - Bridge to record insights into
|
|
93
|
+
* @param options - Transfer options (namespace, filters, limits)
|
|
94
|
+
* @returns Transfer result with counts of transferred and skipped entries
|
|
95
|
+
*
|
|
96
|
+
* @example
|
|
97
|
+
* ```typescript
|
|
98
|
+
* const result = await transferKnowledge(sourceBackend, targetBridge, {
|
|
99
|
+
* sourceNamespace: 'learnings',
|
|
100
|
+
* minConfidence: 0.9,
|
|
101
|
+
* maxEntries: 10,
|
|
102
|
+
* categories: ['architecture', 'security'],
|
|
103
|
+
* });
|
|
104
|
+
* console.log(`Transferred ${result.transferred}, skipped ${result.skipped}`);
|
|
105
|
+
* ```
|
|
106
|
+
*/
|
|
107
|
+
export declare function transferKnowledge(sourceBackend: IMemoryBackend, targetBridge: AutoMemoryBridge, options: TransferOptions): Promise<TransferResult>;
|
|
108
|
+
/**
|
|
109
|
+
* List all agent scopes and their agents for the current project.
|
|
110
|
+
*
|
|
111
|
+
* Scans the three scope directories (project, local, user) and returns
|
|
112
|
+
* the agent names found in each. Useful for discovery and diagnostics.
|
|
113
|
+
*
|
|
114
|
+
* @param workingDir - Working directory for git root detection (defaults to cwd)
|
|
115
|
+
* @returns Array of scope/agents pairs
|
|
116
|
+
*
|
|
117
|
+
* @example
|
|
118
|
+
* ```typescript
|
|
119
|
+
* const scopes = listAgentScopes('/workspaces/my-project');
|
|
120
|
+
* // [
|
|
121
|
+
* // { scope: 'project', agents: ['coder', 'tester'] },
|
|
122
|
+
* // { scope: 'local', agents: ['researcher'] },
|
|
123
|
+
* // { scope: 'user', agents: ['planner'] },
|
|
124
|
+
* // ]
|
|
125
|
+
* ```
|
|
126
|
+
*/
|
|
127
|
+
export declare function listAgentScopes(workingDir?: string): Array<{
|
|
128
|
+
scope: AgentMemoryScope;
|
|
129
|
+
agents: string[];
|
|
130
|
+
}>;
|
|
131
|
+
//# sourceMappingURL=agent-memory-scope.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"agent-memory-scope.d.ts","sourceRoot":"","sources":["../src/agent-memory-scope.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;GAYG;AAIH,OAAO,KAAK,EAAE,cAAc,EAAe,MAAM,YAAY,CAAC;AAC9D,OAAO,EAAE,gBAAgB,EAAE,MAAM,yBAAyB,CAAC;AAC3D,OAAO,KAAK,EACV,sBAAsB,EAEtB,eAAe,EAChB,MAAM,yBAAyB,CAAC;AAIjC,gDAAgD;AAChD,MAAM,MAAM,gBAAgB,GAAG,SAAS,GAAG,OAAO,GAAG,MAAM,CAAC;AAE5D,mDAAmD;AACnD,MAAM,WAAW,iBAAkB,SAAQ,sBAAsB;IAC/D,0CAA0C;IAC1C,SAAS,EAAE,MAAM,CAAC;IAClB,mBAAmB;IACnB,KAAK,EAAE,gBAAgB,CAAC;CACzB;AAED,oDAAoD;AACpD,MAAM,WAAW,eAAe;IAC9B,wCAAwC;IACxC,eAAe,EAAE,MAAM,CAAC;IACxB,mDAAmD;IACnD,aAAa,CAAC,EAAE,MAAM,CAAC;IACvB,gDAAgD;IAChD,UAAU,CAAC,EAAE,MAAM,CAAC;IACpB,2BAA2B;IAC3B,UAAU,CAAC,EAAE,eAAe,EAAE,CAAC;CAChC;AAED,qCAAqC;AACrC,MAAM,WAAW,cAAc;IAC7B,oCAAoC;IACpC,WAAW,EAAE,MAAM,CAAC;IACpB,+DAA+D;IAC/D,OAAO,EAAE,MAAM,CAAC;CACjB;AA2CD;;;;;;;;;;;;;;;;GAgBG;AACH,wBAAgB,qBAAqB,CACnC,SAAS,EAAE,MAAM,EACjB,KAAK,EAAE,gBAAgB,EACvB,UAAU,CAAC,EAAE,MAAM,GAClB,MAAM,CAoBR;AAED;;;;;;;;;;;;;;;;;;;;GAoBG;AACH,wBAAgB,iBAAiB,CAC/B,OAAO,EAAE,cAAc,EACvB,MAAM,EAAE,iBAAiB,GACxB,gBAAgB,CAWlB;AAED;;;;;;;;;;;;;;;;;;;;;;GAsBG;AACH,wBAAsB,iBAAiB,CACrC,aAAa,EAAE,cAAc,EAC7B,YAAY,EAAE,gBAAgB,EAC9B,OAAO,EAAE,eAAe,GACvB,OAAO,CAAC,cAAc,CAAC,CAwDzB;AAED;;;;;;;;;;;;;;;;;;GAkBG;AACH,wBAAgB,eAAe,CAC7B,UAAU,CAAC,EAAE,MAAM,GAClB,KAAK,CAAC;IAAE,KAAK,EAAE,gBAAgB,CAAC;IAAC,MAAM,EAAE,MAAM,EAAE,CAAA;CAAE,CAAC,CAetD"}
|