@claude-flow/memory 3.0.0-alpha.10 → 3.0.0-alpha.12

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 (47) hide show
  1. package/dist/agent-memory-scope.d.ts.map +1 -1
  2. package/dist/agent-memory-scope.js +10 -2
  3. package/dist/agent-memory-scope.js.map +1 -1
  4. package/dist/agentdb-backend.d.ts.map +1 -1
  5. package/dist/agentdb-backend.js +18 -1
  6. package/dist/agentdb-backend.js.map +1 -1
  7. package/dist/controller-registry.d.ts +216 -0
  8. package/dist/controller-registry.d.ts.map +1 -0
  9. package/dist/controller-registry.js +893 -0
  10. package/dist/controller-registry.js.map +1 -0
  11. package/dist/controller-registry.test.d.ts +14 -0
  12. package/dist/controller-registry.test.d.ts.map +1 -0
  13. package/dist/controller-registry.test.js +636 -0
  14. package/dist/controller-registry.test.js.map +1 -0
  15. package/dist/database-provider.d.ts +2 -1
  16. package/dist/database-provider.d.ts.map +1 -1
  17. package/dist/database-provider.js +27 -2
  18. package/dist/database-provider.js.map +1 -1
  19. package/dist/hnsw-lite.d.ts +23 -0
  20. package/dist/hnsw-lite.d.ts.map +1 -0
  21. package/dist/hnsw-lite.js +168 -0
  22. package/dist/hnsw-lite.js.map +1 -0
  23. package/dist/hybrid-backend.d.ts +28 -0
  24. package/dist/hybrid-backend.d.ts.map +1 -1
  25. package/dist/hybrid-backend.js +53 -0
  26. package/dist/hybrid-backend.js.map +1 -1
  27. package/dist/index.d.ts +12 -0
  28. package/dist/index.d.ts.map +1 -1
  29. package/dist/index.js +9 -0
  30. package/dist/index.js.map +1 -1
  31. package/dist/persistent-sona.d.ts +144 -0
  32. package/dist/persistent-sona.d.ts.map +1 -0
  33. package/dist/persistent-sona.js +332 -0
  34. package/dist/persistent-sona.js.map +1 -0
  35. package/dist/rvf-backend.d.ts +51 -0
  36. package/dist/rvf-backend.d.ts.map +1 -0
  37. package/dist/rvf-backend.js +481 -0
  38. package/dist/rvf-backend.js.map +1 -0
  39. package/dist/rvf-learning-store.d.ts +139 -0
  40. package/dist/rvf-learning-store.d.ts.map +1 -0
  41. package/dist/rvf-learning-store.js +295 -0
  42. package/dist/rvf-learning-store.js.map +1 -0
  43. package/dist/rvf-migration.d.ts +45 -0
  44. package/dist/rvf-migration.d.ts.map +1 -0
  45. package/dist/rvf-migration.js +254 -0
  46. package/dist/rvf-migration.js.map +1 -0
  47. package/package.json +2 -2
@@ -0,0 +1,893 @@
1
+ /**
2
+ * ControllerRegistry - Central controller lifecycle management for AgentDB v3
3
+ *
4
+ * Wraps the AgentDB class and adds CLI-specific controllers from @claude-flow/memory.
5
+ * Manages initialization (level-based ordering), health checks, and graceful shutdown.
6
+ *
7
+ * Per ADR-053: Replaces memory-initializer.js's raw sql.js usage with a unified
8
+ * controller ecosystem routing all memory operations through AgentDB v3.
9
+ *
10
+ * @module @claude-flow/memory/controller-registry
11
+ */
12
+ import { EventEmitter } from 'node:events';
13
+ import { LearningBridge } from './learning-bridge.js';
14
+ import { MemoryGraph } from './memory-graph.js';
15
+ import { TieredCacheManager } from './cache-manager.js';
16
+ // ===== Initialization Levels =====
17
+ /**
18
+ * Level-based initialization order per ADR-053.
19
+ * Controllers at each level can be initialized in parallel.
20
+ * Each level must complete before the next begins.
21
+ */
22
+ export const INIT_LEVELS = [
23
+ // Level 0: Foundation - already exists
24
+ { level: 0, controllers: [] },
25
+ // Level 1: Core intelligence
26
+ { level: 1, controllers: ['reasoningBank', 'hierarchicalMemory', 'learningBridge', 'hybridSearch', 'tieredCache'] },
27
+ // Level 2: Graph & security
28
+ { level: 2, controllers: ['memoryGraph', 'agentMemoryScope', 'vectorBackend', 'mutationGuard', 'gnnService'] },
29
+ // Level 3: Specialization
30
+ { level: 3, controllers: ['skills', 'explainableRecall', 'reflexion', 'attestationLog', 'batchOperations', 'memoryConsolidation'] },
31
+ // Level 4: Causal & routing
32
+ { level: 4, controllers: ['causalGraph', 'nightlyLearner', 'learningSystem', 'semanticRouter'] },
33
+ // Level 5: Advanced services
34
+ { level: 5, controllers: ['graphTransformer', 'sonaTrajectory', 'contextSynthesizer', 'rvfOptimizer', 'mmrDiversityRanker', 'guardedVectorBackend'] },
35
+ // Level 6: Session management
36
+ { level: 6, controllers: ['federatedSession', 'graphAdapter'] },
37
+ ];
38
+ // ===== ControllerRegistry =====
39
+ /**
40
+ * Central registry for AgentDB v3 controller lifecycle management.
41
+ *
42
+ * Handles:
43
+ * - Level-based initialization ordering (levels 0-6)
44
+ * - Graceful degradation (each controller fails independently)
45
+ * - Config-driven activation (controllers only instantiate when enabled)
46
+ * - Health check aggregation across all controllers
47
+ * - Ordered shutdown (reverse initialization order)
48
+ *
49
+ * @example
50
+ * ```typescript
51
+ * const registry = new ControllerRegistry();
52
+ * await registry.initialize({
53
+ * dbPath: './data/memory.db',
54
+ * dimension: 384,
55
+ * memory: {
56
+ * enableHNSW: true,
57
+ * learningBridge: { sonaMode: 'balanced' },
58
+ * memoryGraph: { pageRankDamping: 0.85 },
59
+ * },
60
+ * });
61
+ *
62
+ * const reasoning = registry.get<ReasoningBank>('reasoningBank');
63
+ * const graph = registry.get<MemoryGraph>('memoryGraph');
64
+ *
65
+ * await registry.shutdown();
66
+ * ```
67
+ */
68
+ export class ControllerRegistry extends EventEmitter {
69
+ controllers = new Map();
70
+ agentdb = null;
71
+ backend = null;
72
+ config = {};
73
+ initialized = false;
74
+ initTimeMs = 0;
75
+ /**
76
+ * Initialize all controllers in level-based order.
77
+ *
78
+ * Each level's controllers are initialized in parallel within the level.
79
+ * Failures are isolated: a controller that fails to init is marked as
80
+ * unavailable but does not block other controllers.
81
+ */
82
+ async initialize(config = {}) {
83
+ if (this.initialized)
84
+ return;
85
+ this.initialized = true; // Set early to prevent concurrent re-entry
86
+ this.config = config;
87
+ const startTime = performance.now();
88
+ // Step 1: Initialize AgentDB (the core)
89
+ await this.initAgentDB(config);
90
+ // Step 2: Set up the backend
91
+ this.backend = config.backend || null;
92
+ // Step 3: Initialize controllers level by level
93
+ for (const level of INIT_LEVELS) {
94
+ const controllersToInit = level.controllers.filter((name) => this.isControllerEnabled(name));
95
+ if (controllersToInit.length === 0)
96
+ continue;
97
+ // Initialize all controllers in this level in parallel
98
+ const results = await Promise.allSettled(controllersToInit.map((name) => this.initController(name, level.level)));
99
+ // Process results
100
+ for (let i = 0; i < results.length; i++) {
101
+ const result = results[i];
102
+ const name = controllersToInit[i];
103
+ if (result.status === 'rejected') {
104
+ const errorMsg = result.reason instanceof Error
105
+ ? result.reason.message
106
+ : String(result.reason);
107
+ this.controllers.set(name, {
108
+ name,
109
+ instance: null,
110
+ level: level.level,
111
+ initTimeMs: 0,
112
+ enabled: false,
113
+ error: errorMsg,
114
+ });
115
+ this.emit('controller:failed', { name, error: errorMsg, level: level.level });
116
+ }
117
+ }
118
+ }
119
+ this.initTimeMs = performance.now() - startTime;
120
+ this.emit('initialized', {
121
+ initTimeMs: this.initTimeMs,
122
+ activeControllers: this.getActiveCount(),
123
+ totalControllers: this.controllers.size,
124
+ });
125
+ }
126
+ /**
127
+ * Shutdown all controllers in reverse initialization order.
128
+ */
129
+ async shutdown() {
130
+ if (!this.initialized)
131
+ return;
132
+ // Shutdown in reverse level order
133
+ const reverseLevels = [...INIT_LEVELS].reverse();
134
+ for (const level of reverseLevels) {
135
+ const controllersToShutdown = level.controllers
136
+ .filter((name) => {
137
+ const entry = this.controllers.get(name);
138
+ return entry?.enabled && entry?.instance;
139
+ });
140
+ await Promise.allSettled(controllersToShutdown.map((name) => this.shutdownController(name)));
141
+ }
142
+ // Shutdown AgentDB
143
+ if (this.agentdb) {
144
+ try {
145
+ if (typeof this.agentdb.close === 'function') {
146
+ await this.agentdb.close();
147
+ }
148
+ }
149
+ catch {
150
+ // Best-effort cleanup
151
+ }
152
+ this.agentdb = null;
153
+ }
154
+ this.controllers.clear();
155
+ this.initialized = false;
156
+ this.emit('shutdown');
157
+ }
158
+ /**
159
+ * Get a controller instance by name.
160
+ * Returns null if the controller is not initialized or unavailable.
161
+ */
162
+ get(name) {
163
+ // First check CLI-layer controllers
164
+ const entry = this.controllers.get(name);
165
+ if (entry?.enabled && entry?.instance) {
166
+ return entry.instance;
167
+ }
168
+ // Fall back to AgentDB internal controllers
169
+ if (this.agentdb && typeof this.agentdb.getController === 'function') {
170
+ try {
171
+ const controller = this.agentdb.getController(name);
172
+ if (controller)
173
+ return controller;
174
+ }
175
+ catch {
176
+ // Controller not available in AgentDB
177
+ }
178
+ }
179
+ return null;
180
+ }
181
+ /**
182
+ * Check if a controller is enabled and initialized.
183
+ */
184
+ isEnabled(name) {
185
+ const entry = this.controllers.get(name);
186
+ if (entry?.enabled)
187
+ return true;
188
+ // Check AgentDB internal controllers
189
+ if (this.agentdb && typeof this.agentdb.getController === 'function') {
190
+ try {
191
+ return this.agentdb.getController(name) !== null;
192
+ }
193
+ catch {
194
+ return false;
195
+ }
196
+ }
197
+ return false;
198
+ }
199
+ /**
200
+ * Aggregate health check across all controllers.
201
+ */
202
+ async healthCheck() {
203
+ const controllerHealth = [];
204
+ for (const [name, entry] of this.controllers) {
205
+ controllerHealth.push({
206
+ name,
207
+ status: entry.enabled
208
+ ? 'healthy'
209
+ : entry.error
210
+ ? 'unavailable'
211
+ : 'degraded',
212
+ initTimeMs: entry.initTimeMs,
213
+ error: entry.error,
214
+ });
215
+ }
216
+ // Check AgentDB health
217
+ let agentdbAvailable = false;
218
+ if (this.agentdb) {
219
+ try {
220
+ agentdbAvailable = typeof this.agentdb.getController === 'function';
221
+ }
222
+ catch {
223
+ agentdbAvailable = false;
224
+ }
225
+ }
226
+ const active = controllerHealth.filter((c) => c.status === 'healthy').length;
227
+ const unavailable = controllerHealth.filter((c) => c.status === 'unavailable').length;
228
+ let status = 'healthy';
229
+ if (unavailable > 0 && active === 0) {
230
+ status = 'unhealthy';
231
+ }
232
+ else if (unavailable > 0) {
233
+ status = 'degraded';
234
+ }
235
+ return {
236
+ status,
237
+ controllers: controllerHealth,
238
+ agentdbAvailable,
239
+ initTimeMs: this.initTimeMs,
240
+ timestamp: Date.now(),
241
+ activeControllers: active,
242
+ totalControllers: controllerHealth.length,
243
+ };
244
+ }
245
+ /**
246
+ * Get the underlying AgentDB instance.
247
+ */
248
+ getAgentDB() {
249
+ return this.agentdb;
250
+ }
251
+ /**
252
+ * Get the memory backend.
253
+ */
254
+ getBackend() {
255
+ return this.backend;
256
+ }
257
+ /**
258
+ * Check if the registry is initialized.
259
+ */
260
+ isInitialized() {
261
+ return this.initialized;
262
+ }
263
+ /**
264
+ * Get the number of active (successfully initialized) controllers.
265
+ */
266
+ getActiveCount() {
267
+ let count = 0;
268
+ for (const entry of this.controllers.values()) {
269
+ if (entry.enabled)
270
+ count++;
271
+ }
272
+ return count;
273
+ }
274
+ /**
275
+ * List all registered controller names and their status.
276
+ */
277
+ listControllers() {
278
+ return Array.from(this.controllers.entries()).map(([name, entry]) => ({
279
+ name,
280
+ enabled: entry.enabled,
281
+ level: entry.level,
282
+ }));
283
+ }
284
+ // ===== Private Methods =====
285
+ /**
286
+ * Initialize AgentDB instance with dynamic import and fallback chain.
287
+ */
288
+ async initAgentDB(config) {
289
+ try {
290
+ // Validate dbPath to prevent path traversal
291
+ const dbPath = config.dbPath || ':memory:';
292
+ if (dbPath !== ':memory:') {
293
+ const resolved = require('path').resolve(dbPath);
294
+ if (resolved.includes('..')) {
295
+ this.emit('agentdb:unavailable', { reason: 'Invalid dbPath' });
296
+ return;
297
+ }
298
+ }
299
+ const agentdbModule = await import('agentdb');
300
+ const AgentDBClass = agentdbModule.AgentDB || agentdbModule.default;
301
+ if (!AgentDBClass) {
302
+ this.emit('agentdb:unavailable', { reason: 'No AgentDB class found' });
303
+ return;
304
+ }
305
+ this.agentdb = new AgentDBClass({ dbPath });
306
+ // Suppress agentdb's noisy info-level output during init
307
+ // using stderr redirect instead of monkey-patching console.log
308
+ const origLog = console.log;
309
+ const suppressFilter = (args) => {
310
+ const msg = String(args[0] ?? '');
311
+ return msg.includes('Transformers.js') ||
312
+ msg.includes('better-sqlite3') ||
313
+ msg.includes('[AgentDB]');
314
+ };
315
+ console.log = (...args) => {
316
+ if (!suppressFilter(args))
317
+ origLog.apply(console, args);
318
+ };
319
+ try {
320
+ await this.agentdb.initialize();
321
+ }
322
+ finally {
323
+ console.log = origLog;
324
+ }
325
+ this.emit('agentdb:initialized');
326
+ }
327
+ catch (error) {
328
+ const msg = error instanceof Error ? error.message : String(error);
329
+ this.emit('agentdb:unavailable', { reason: msg.substring(0, 200) });
330
+ this.agentdb = null;
331
+ }
332
+ }
333
+ /**
334
+ * Check whether a controller should be initialized based on config.
335
+ */
336
+ isControllerEnabled(name) {
337
+ // Explicit enable/disable from config
338
+ if (this.config.controllers) {
339
+ const explicit = this.config.controllers[name];
340
+ if (explicit !== undefined)
341
+ return explicit;
342
+ }
343
+ // Default behavior: enable based on category
344
+ switch (name) {
345
+ // Core intelligence — enabled by default
346
+ case 'reasoningBank':
347
+ case 'learningBridge':
348
+ case 'tieredCache':
349
+ case 'hierarchicalMemory':
350
+ return true;
351
+ // Graph — enabled if backend available
352
+ case 'memoryGraph':
353
+ return !!(this.config.memory?.memoryGraph || this.backend);
354
+ // Security — enabled if AgentDB available
355
+ case 'mutationGuard':
356
+ case 'attestationLog':
357
+ case 'vectorBackend':
358
+ case 'guardedVectorBackend':
359
+ return this.agentdb !== null;
360
+ // AgentDB-internal controllers — only if AgentDB available
361
+ case 'skills':
362
+ case 'reflexion':
363
+ case 'causalGraph':
364
+ case 'causalRecall':
365
+ case 'learningSystem':
366
+ case 'explainableRecall':
367
+ case 'nightlyLearner':
368
+ case 'graphTransformer':
369
+ case 'graphAdapter':
370
+ case 'gnnService':
371
+ case 'memoryConsolidation':
372
+ case 'batchOperations':
373
+ case 'contextSynthesizer':
374
+ case 'rvfOptimizer':
375
+ case 'mmrDiversityRanker':
376
+ return this.agentdb !== null;
377
+ // SemanticRouter — auto-enable if agentdb available (exported since alpha.10)
378
+ case 'semanticRouter':
379
+ return this.agentdb !== null;
380
+ // Optional controllers
381
+ case 'hybridSearch':
382
+ case 'agentMemoryScope':
383
+ case 'sonaTrajectory':
384
+ case 'federatedSession':
385
+ return false; // Require explicit enabling
386
+ default:
387
+ return false;
388
+ }
389
+ }
390
+ /**
391
+ * Initialize a single controller with error isolation.
392
+ */
393
+ async initController(name, level) {
394
+ const startTime = performance.now();
395
+ try {
396
+ const instance = await this.createController(name);
397
+ const initTimeMs = performance.now() - startTime;
398
+ this.controllers.set(name, {
399
+ name,
400
+ instance,
401
+ level,
402
+ initTimeMs,
403
+ enabled: instance !== null,
404
+ error: instance === null ? 'Controller returned null' : undefined,
405
+ });
406
+ if (instance !== null) {
407
+ this.emit('controller:initialized', { name, level, initTimeMs });
408
+ }
409
+ }
410
+ catch (error) {
411
+ const errorMsg = error instanceof Error ? error.message : String(error);
412
+ const initTimeMs = performance.now() - startTime;
413
+ this.controllers.set(name, {
414
+ name,
415
+ instance: null,
416
+ level,
417
+ initTimeMs,
418
+ enabled: false,
419
+ error: errorMsg,
420
+ });
421
+ throw error;
422
+ }
423
+ }
424
+ /**
425
+ * Factory method to create a controller instance.
426
+ * Handles CLI-layer controllers; AgentDB-internal controllers are
427
+ * accessed via agentdb.getController().
428
+ */
429
+ async createController(name) {
430
+ switch (name) {
431
+ // ----- CLI-layer controllers -----
432
+ case 'learningBridge': {
433
+ if (!this.backend)
434
+ return null;
435
+ const config = this.config.memory?.learningBridge || {};
436
+ const bridge = new LearningBridge(this.backend, {
437
+ sonaMode: config.sonaMode || this.config.neural?.sonaMode || 'balanced',
438
+ confidenceDecayRate: config.confidenceDecayRate,
439
+ accessBoostAmount: config.accessBoostAmount,
440
+ consolidationThreshold: config.consolidationThreshold,
441
+ enabled: true,
442
+ });
443
+ return bridge;
444
+ }
445
+ case 'memoryGraph': {
446
+ const config = this.config.memory?.memoryGraph || {};
447
+ const graph = new MemoryGraph({
448
+ pageRankDamping: config.pageRankDamping,
449
+ maxNodes: config.maxNodes,
450
+ ...config,
451
+ });
452
+ // Build from backend if available
453
+ if (this.backend) {
454
+ try {
455
+ await graph.buildFromBackend(this.backend);
456
+ }
457
+ catch {
458
+ // Graph build from backend failed — empty graph is still usable
459
+ }
460
+ }
461
+ return graph;
462
+ }
463
+ case 'tieredCache': {
464
+ const config = this.config.memory?.tieredCache || {};
465
+ const cache = new TieredCacheManager({
466
+ maxSize: config.maxSize || 10000,
467
+ ttl: config.ttl || 300000,
468
+ lruEnabled: true,
469
+ writeThrough: false,
470
+ ...config,
471
+ });
472
+ return cache;
473
+ }
474
+ case 'hybridSearch':
475
+ // BM25 hybrid search — placeholder for future implementation
476
+ return null;
477
+ case 'agentMemoryScope':
478
+ // Agent memory scope — placeholder, activated when explicitly enabled
479
+ return null;
480
+ case 'semanticRouter': {
481
+ // SemanticRouter exported from agentdb 3.0.0-alpha.10 (ADR-062)
482
+ // Constructor: () — requires initialize() after construction
483
+ try {
484
+ const agentdbModule = await import('agentdb');
485
+ const SR = agentdbModule.SemanticRouter;
486
+ if (!SR)
487
+ return null;
488
+ const router = new SR();
489
+ await router.initialize();
490
+ return router;
491
+ }
492
+ catch {
493
+ return null;
494
+ }
495
+ }
496
+ case 'sonaTrajectory':
497
+ // Delegate to AgentDB's SonaTrajectoryService if available
498
+ if (this.agentdb && typeof this.agentdb.getController === 'function') {
499
+ try {
500
+ return this.agentdb.getController('sonaTrajectory');
501
+ }
502
+ catch {
503
+ return null;
504
+ }
505
+ }
506
+ return null;
507
+ case 'hierarchicalMemory': {
508
+ // HierarchicalMemory exported from agentdb 3.0.0-alpha.10 (ADR-066 Phase P2-3)
509
+ // Constructor: (db, embedder, vectorBackend?, graphBackend?, config?)
510
+ if (!this.agentdb)
511
+ return this.createTieredMemoryStub();
512
+ try {
513
+ const agentdbModule = await import('agentdb');
514
+ const HM = agentdbModule.HierarchicalMemory;
515
+ if (!HM)
516
+ return this.createTieredMemoryStub();
517
+ const embedder = this.createEmbeddingService();
518
+ const hm = new HM(this.agentdb.database, embedder);
519
+ await hm.initializeDatabase();
520
+ return hm;
521
+ }
522
+ catch {
523
+ return this.createTieredMemoryStub();
524
+ }
525
+ }
526
+ case 'memoryConsolidation': {
527
+ // MemoryConsolidation exported from agentdb 3.0.0-alpha.10 (ADR-066 Phase P2-3)
528
+ // Constructor: (db, hierarchicalMemory, embedder, vectorBackend?, graphBackend?, config?)
529
+ if (!this.agentdb)
530
+ return this.createConsolidationStub();
531
+ try {
532
+ const agentdbModule = await import('agentdb');
533
+ const MC = agentdbModule.MemoryConsolidation;
534
+ if (!MC)
535
+ return this.createConsolidationStub();
536
+ // Get the HierarchicalMemory instance (must be initialized at level 1 before us at level 3)
537
+ const hm = this.get('hierarchicalMemory');
538
+ if (!hm || typeof hm.recall !== 'function' || typeof hm.store !== 'function') {
539
+ return this.createConsolidationStub();
540
+ }
541
+ const embedder = this.createEmbeddingService();
542
+ const mc = new MC(this.agentdb.database, hm, embedder);
543
+ await mc.initializeDatabase();
544
+ return mc;
545
+ }
546
+ catch {
547
+ return this.createConsolidationStub();
548
+ }
549
+ }
550
+ case 'federatedSession':
551
+ // Federated session — placeholder for Phase 4
552
+ return null;
553
+ // ----- AgentDB-internal controllers (via getController) -----
554
+ // AgentDB.getController() only supports: reflexion/memory, skills, causalGraph/causal
555
+ case 'reasoningBank': {
556
+ // ReasoningBank is exported directly, not via getController
557
+ if (!this.agentdb)
558
+ return null;
559
+ try {
560
+ const agentdbModule = await import('agentdb');
561
+ const RB = agentdbModule.ReasoningBank;
562
+ if (!RB)
563
+ return null;
564
+ return new RB(this.agentdb.database);
565
+ }
566
+ catch {
567
+ return null;
568
+ }
569
+ }
570
+ case 'skills':
571
+ case 'reflexion':
572
+ case 'causalGraph': {
573
+ if (!this.agentdb || typeof this.agentdb.getController !== 'function')
574
+ return null;
575
+ try {
576
+ return this.agentdb.getController(name) ?? null;
577
+ }
578
+ catch {
579
+ return null;
580
+ }
581
+ }
582
+ case 'causalRecall': {
583
+ if (!this.agentdb)
584
+ return null;
585
+ try {
586
+ const agentdbModule = await import('agentdb');
587
+ const CR = agentdbModule.CausalRecall;
588
+ if (!CR)
589
+ return null;
590
+ return new CR(this.agentdb.database);
591
+ }
592
+ catch {
593
+ return null;
594
+ }
595
+ }
596
+ case 'learningSystem': {
597
+ if (!this.agentdb)
598
+ return null;
599
+ try {
600
+ const agentdbModule = await import('agentdb');
601
+ const LS = agentdbModule.LearningSystem;
602
+ if (!LS)
603
+ return null;
604
+ return new LS(this.agentdb.database);
605
+ }
606
+ catch {
607
+ return null;
608
+ }
609
+ }
610
+ case 'explainableRecall': {
611
+ if (!this.agentdb)
612
+ return null;
613
+ try {
614
+ const agentdbModule = await import('agentdb');
615
+ const ER = agentdbModule.ExplainableRecall;
616
+ if (!ER)
617
+ return null;
618
+ return new ER(this.agentdb.database);
619
+ }
620
+ catch {
621
+ return null;
622
+ }
623
+ }
624
+ case 'nightlyLearner': {
625
+ if (!this.agentdb)
626
+ return null;
627
+ try {
628
+ const agentdbModule = await import('agentdb');
629
+ const NL = agentdbModule.NightlyLearner;
630
+ if (!NL)
631
+ return null;
632
+ return new NL(this.agentdb.database);
633
+ }
634
+ catch {
635
+ return null;
636
+ }
637
+ }
638
+ case 'graphTransformer': {
639
+ if (!this.agentdb)
640
+ return null;
641
+ try {
642
+ const agentdbModule = await import('agentdb');
643
+ const GT = agentdbModule.CausalMemoryGraph;
644
+ if (!GT)
645
+ return null;
646
+ return new GT(this.agentdb.database);
647
+ }
648
+ catch {
649
+ return null;
650
+ }
651
+ }
652
+ // ----- Direct-instantiation controllers -----
653
+ case 'batchOperations': {
654
+ if (!this.agentdb)
655
+ return null;
656
+ try {
657
+ const agentdbModule = await import('agentdb');
658
+ const BO = agentdbModule.BatchOperations;
659
+ if (!BO)
660
+ return null;
661
+ const embedder = this.config.embeddingGenerator || null;
662
+ return new BO(this.agentdb.database, embedder);
663
+ }
664
+ catch {
665
+ return null;
666
+ }
667
+ }
668
+ case 'contextSynthesizer': {
669
+ // ContextSynthesizer.synthesize is static — return the class itself
670
+ try {
671
+ const agentdbModule = await import('agentdb');
672
+ return agentdbModule.ContextSynthesizer ?? null;
673
+ }
674
+ catch {
675
+ return null;
676
+ }
677
+ }
678
+ case 'mmrDiversityRanker': {
679
+ try {
680
+ const agentdbModule = await import('agentdb');
681
+ const MMR = agentdbModule.MMRDiversityRanker;
682
+ if (!MMR)
683
+ return null;
684
+ return new MMR();
685
+ }
686
+ catch {
687
+ return null;
688
+ }
689
+ }
690
+ case 'mutationGuard': {
691
+ // MutationGuard exported from agentdb 3.0.0-alpha.10 (ADR-060)
692
+ // Constructor: (config?) where config.dimension, config.maxElements, config.enableWasmProofs
693
+ if (!this.agentdb)
694
+ return null;
695
+ try {
696
+ const agentdbModule = await import('agentdb');
697
+ const MG = agentdbModule.MutationGuard;
698
+ if (!MG)
699
+ return null;
700
+ return new MG({ dimension: this.config.dimension || 384 });
701
+ }
702
+ catch {
703
+ return null;
704
+ }
705
+ }
706
+ case 'attestationLog': {
707
+ // AttestationLog exported from agentdb 3.0.0-alpha.10 (ADR-060)
708
+ // Constructor: (db) — uses database for append-only audit log
709
+ if (!this.agentdb)
710
+ return null;
711
+ try {
712
+ const agentdbModule = await import('agentdb');
713
+ const AL = agentdbModule.AttestationLog;
714
+ if (!AL)
715
+ return null;
716
+ return new AL(this.agentdb.database);
717
+ }
718
+ catch {
719
+ return null;
720
+ }
721
+ }
722
+ case 'gnnService': {
723
+ // GNNService exported from agentdb 3.0.0-alpha.10 (ADR-062)
724
+ // Constructor: (config?) — requires initialize() after construction
725
+ try {
726
+ const agentdbModule = await import('agentdb');
727
+ const GNN = agentdbModule.GNNService;
728
+ if (!GNN)
729
+ return null;
730
+ const gnn = new GNN({ inputDim: this.config.dimension || 384 });
731
+ await gnn.initialize();
732
+ return gnn;
733
+ }
734
+ catch {
735
+ return null;
736
+ }
737
+ }
738
+ case 'rvfOptimizer': {
739
+ // RVFOptimizer exported from agentdb 3.0.0-alpha.10 (ADR-062/065)
740
+ // Constructor: (config?) — no-arg for defaults
741
+ try {
742
+ const agentdbModule = await import('agentdb');
743
+ const RVF = agentdbModule.RVFOptimizer;
744
+ if (!RVF)
745
+ return null;
746
+ return new RVF();
747
+ }
748
+ catch {
749
+ return null;
750
+ }
751
+ }
752
+ case 'guardedVectorBackend': {
753
+ // GuardedVectorBackend exported from agentdb 3.0.0-alpha.10 (ADR-060)
754
+ // Constructor: (innerBackend, mutationGuard, attestationLog?)
755
+ // Requires vectorBackend and mutationGuard to be initialized first (level 2)
756
+ if (!this.agentdb)
757
+ return null;
758
+ try {
759
+ const vb = this.get('vectorBackend');
760
+ const guard = this.get('mutationGuard');
761
+ if (!vb || !guard)
762
+ return null;
763
+ const agentdbModule = await import('agentdb');
764
+ const GVB = agentdbModule.GuardedVectorBackend;
765
+ if (!GVB)
766
+ return null;
767
+ const log = this.get('attestationLog');
768
+ return new GVB(vb, guard, log || undefined);
769
+ }
770
+ catch {
771
+ return null;
772
+ }
773
+ }
774
+ case 'vectorBackend':
775
+ case 'graphAdapter': {
776
+ // These are accessed via AgentDB internal state, not direct construction
777
+ if (!this.agentdb)
778
+ return null;
779
+ try {
780
+ if (typeof this.agentdb.getController === 'function') {
781
+ return this.agentdb.getController(name) ?? null;
782
+ }
783
+ }
784
+ catch { /* fallthrough */ }
785
+ return null;
786
+ }
787
+ default:
788
+ return null;
789
+ }
790
+ }
791
+ /**
792
+ * Shutdown a single controller gracefully.
793
+ */
794
+ async shutdownController(name) {
795
+ const entry = this.controllers.get(name);
796
+ if (!entry?.instance)
797
+ return;
798
+ try {
799
+ const instance = entry.instance;
800
+ // Try known shutdown methods (always await for safety)
801
+ if (typeof instance.destroy === 'function') {
802
+ await instance.destroy();
803
+ }
804
+ else if (typeof instance.shutdown === 'function') {
805
+ await instance.shutdown();
806
+ }
807
+ else if (typeof instance.close === 'function') {
808
+ await instance.close();
809
+ }
810
+ }
811
+ catch {
812
+ // Best-effort cleanup
813
+ }
814
+ entry.enabled = false;
815
+ entry.instance = null;
816
+ }
817
+ /**
818
+ * Create an EmbeddingService for controllers that need it.
819
+ * Uses the config's embedding generator or creates a minimal local service.
820
+ */
821
+ createEmbeddingService() {
822
+ // If user provided an embedding generator, wrap it
823
+ if (this.config.embeddingGenerator) {
824
+ return {
825
+ embed: async (text) => this.config.embeddingGenerator(text),
826
+ embedBatch: async (texts) => Promise.all(texts.map(t => this.config.embeddingGenerator(t))),
827
+ initialize: async () => { },
828
+ };
829
+ }
830
+ // Return a minimal stub — HierarchicalMemory falls back to manualSearch without embeddings
831
+ return {
832
+ embed: async () => new Float32Array(this.config.dimension || 384),
833
+ embedBatch: async (texts) => texts.map(() => new Float32Array(this.config.dimension || 384)),
834
+ initialize: async () => { },
835
+ };
836
+ }
837
+ /**
838
+ * Lightweight in-memory tiered store (fallback when HierarchicalMemory
839
+ * cannot be initialized from agentdb).
840
+ * Enforces per-tier size limits to prevent unbounded memory growth.
841
+ */
842
+ createTieredMemoryStub() {
843
+ const MAX_PER_TIER = 5000;
844
+ const tiers = {
845
+ working: new Map(),
846
+ episodic: new Map(),
847
+ semantic: new Map(),
848
+ };
849
+ return {
850
+ store(key, value, tier = 'working') {
851
+ const t = tiers[tier] || tiers.working;
852
+ // Evict oldest if at capacity
853
+ if (t.size >= MAX_PER_TIER) {
854
+ const oldest = t.keys().next().value;
855
+ if (oldest !== undefined)
856
+ t.delete(oldest);
857
+ }
858
+ t.set(key, { value: value.substring(0, 100_000), ts: Date.now() });
859
+ },
860
+ recall(query, topK = 5) {
861
+ const safeTopK = Math.min(Math.max(1, topK), 100);
862
+ const q = query.toLowerCase().substring(0, 10_000);
863
+ const results = [];
864
+ for (const [tierName, map] of Object.entries(tiers)) {
865
+ for (const [key, entry] of map) {
866
+ if (key.toLowerCase().includes(q) || entry.value.toLowerCase().includes(q)) {
867
+ results.push({ key, value: entry.value, tier: tierName, ts: entry.ts });
868
+ if (results.length >= safeTopK * 3)
869
+ break; // Early exit for large stores
870
+ }
871
+ }
872
+ }
873
+ return results.sort((a, b) => b.ts - a.ts).slice(0, safeTopK);
874
+ },
875
+ getTierStats() {
876
+ return Object.fromEntries(Object.entries(tiers).map(([name, map]) => [name, map.size]));
877
+ },
878
+ };
879
+ }
880
+ /**
881
+ * No-op consolidation stub (fallback when MemoryConsolidation
882
+ * cannot be initialized from agentdb).
883
+ */
884
+ createConsolidationStub() {
885
+ return {
886
+ consolidate() {
887
+ return { promoted: 0, pruned: 0, timestamp: Date.now() };
888
+ },
889
+ };
890
+ }
891
+ }
892
+ export default ControllerRegistry;
893
+ //# sourceMappingURL=controller-registry.js.map