moflo 4.0.1 → 4.0.3

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 (91) hide show
  1. package/.claude/guidance/agent-bootstrap.md +12 -6
  2. package/bin/setup-project.mjs +201 -0
  3. package/package.json +114 -109
  4. package/v3/@claude-flow/cli/dist/src/memory/memory-bridge.js +194 -81
  5. package/v3/@claude-flow/cli/dist/src/memory/memory-initializer.js +1892 -1841
  6. package/v3/@claude-flow/memory/README.md +587 -0
  7. package/v3/@claude-flow/memory/dist/agent-memory-scope.d.ts +131 -0
  8. package/v3/@claude-flow/memory/dist/agent-memory-scope.js +223 -0
  9. package/v3/@claude-flow/memory/dist/agent-memory-scope.test.d.ts +8 -0
  10. package/v3/@claude-flow/memory/dist/agent-memory-scope.test.js +463 -0
  11. package/v3/@claude-flow/memory/dist/agentdb-adapter.d.ts +165 -0
  12. package/v3/@claude-flow/memory/dist/agentdb-adapter.js +806 -0
  13. package/v3/@claude-flow/memory/dist/agentdb-backend.d.ts +214 -0
  14. package/v3/@claude-flow/memory/dist/agentdb-backend.js +844 -0
  15. package/v3/@claude-flow/memory/dist/agentdb-backend.test.d.ts +7 -0
  16. package/v3/@claude-flow/memory/dist/agentdb-backend.test.js +258 -0
  17. package/v3/@claude-flow/memory/dist/application/commands/delete-memory.command.d.ts +65 -0
  18. package/v3/@claude-flow/memory/dist/application/commands/delete-memory.command.js +129 -0
  19. package/v3/@claude-flow/memory/dist/application/commands/store-memory.command.d.ts +48 -0
  20. package/v3/@claude-flow/memory/dist/application/commands/store-memory.command.js +72 -0
  21. package/v3/@claude-flow/memory/dist/application/index.d.ts +12 -0
  22. package/v3/@claude-flow/memory/dist/application/index.js +15 -0
  23. package/v3/@claude-flow/memory/dist/application/queries/search-memory.query.d.ts +72 -0
  24. package/v3/@claude-flow/memory/dist/application/queries/search-memory.query.js +143 -0
  25. package/v3/@claude-flow/memory/dist/application/services/memory-application-service.d.ts +121 -0
  26. package/v3/@claude-flow/memory/dist/application/services/memory-application-service.js +190 -0
  27. package/v3/@claude-flow/memory/dist/auto-memory-bridge.d.ts +226 -0
  28. package/v3/@claude-flow/memory/dist/auto-memory-bridge.js +709 -0
  29. package/v3/@claude-flow/memory/dist/auto-memory-bridge.test.d.ts +8 -0
  30. package/v3/@claude-flow/memory/dist/auto-memory-bridge.test.js +754 -0
  31. package/v3/@claude-flow/memory/dist/benchmark.test.d.ts +2 -0
  32. package/v3/@claude-flow/memory/dist/benchmark.test.js +277 -0
  33. package/v3/@claude-flow/memory/dist/cache-manager.d.ts +134 -0
  34. package/v3/@claude-flow/memory/dist/cache-manager.js +407 -0
  35. package/v3/@claude-flow/memory/dist/controller-registry.d.ts +216 -0
  36. package/v3/@claude-flow/memory/dist/controller-registry.js +893 -0
  37. package/v3/@claude-flow/memory/dist/controller-registry.test.d.ts +14 -0
  38. package/v3/@claude-flow/memory/dist/controller-registry.test.js +636 -0
  39. package/v3/@claude-flow/memory/dist/database-provider.d.ts +87 -0
  40. package/v3/@claude-flow/memory/dist/database-provider.js +410 -0
  41. package/v3/@claude-flow/memory/dist/database-provider.test.d.ts +7 -0
  42. package/v3/@claude-flow/memory/dist/database-provider.test.js +285 -0
  43. package/v3/@claude-flow/memory/dist/domain/entities/memory-entry.d.ts +143 -0
  44. package/v3/@claude-flow/memory/dist/domain/entities/memory-entry.js +226 -0
  45. package/v3/@claude-flow/memory/dist/domain/index.d.ts +11 -0
  46. package/v3/@claude-flow/memory/dist/domain/index.js +12 -0
  47. package/v3/@claude-flow/memory/dist/domain/repositories/memory-repository.interface.d.ts +102 -0
  48. package/v3/@claude-flow/memory/dist/domain/repositories/memory-repository.interface.js +11 -0
  49. package/v3/@claude-flow/memory/dist/domain/services/memory-domain-service.d.ts +105 -0
  50. package/v3/@claude-flow/memory/dist/domain/services/memory-domain-service.js +297 -0
  51. package/v3/@claude-flow/memory/dist/hnsw-index.d.ts +111 -0
  52. package/v3/@claude-flow/memory/dist/hnsw-index.js +781 -0
  53. package/v3/@claude-flow/memory/dist/hnsw-lite.d.ts +23 -0
  54. package/v3/@claude-flow/memory/dist/hnsw-lite.js +168 -0
  55. package/v3/@claude-flow/memory/dist/hybrid-backend.d.ts +245 -0
  56. package/v3/@claude-flow/memory/dist/hybrid-backend.js +569 -0
  57. package/v3/@claude-flow/memory/dist/hybrid-backend.test.d.ts +8 -0
  58. package/v3/@claude-flow/memory/dist/hybrid-backend.test.js +320 -0
  59. package/v3/@claude-flow/memory/dist/index.d.ts +208 -0
  60. package/v3/@claude-flow/memory/dist/index.js +362 -0
  61. package/v3/@claude-flow/memory/dist/infrastructure/index.d.ts +17 -0
  62. package/v3/@claude-flow/memory/dist/infrastructure/index.js +16 -0
  63. package/v3/@claude-flow/memory/dist/infrastructure/repositories/hybrid-memory-repository.d.ts +66 -0
  64. package/v3/@claude-flow/memory/dist/infrastructure/repositories/hybrid-memory-repository.js +409 -0
  65. package/v3/@claude-flow/memory/dist/learning-bridge.d.ts +137 -0
  66. package/v3/@claude-flow/memory/dist/learning-bridge.js +335 -0
  67. package/v3/@claude-flow/memory/dist/learning-bridge.test.d.ts +8 -0
  68. package/v3/@claude-flow/memory/dist/learning-bridge.test.js +578 -0
  69. package/v3/@claude-flow/memory/dist/memory-graph.d.ts +100 -0
  70. package/v3/@claude-flow/memory/dist/memory-graph.js +333 -0
  71. package/v3/@claude-flow/memory/dist/memory-graph.test.d.ts +8 -0
  72. package/v3/@claude-flow/memory/dist/memory-graph.test.js +609 -0
  73. package/v3/@claude-flow/memory/dist/migration.d.ts +68 -0
  74. package/v3/@claude-flow/memory/dist/migration.js +513 -0
  75. package/v3/@claude-flow/memory/dist/persistent-sona.d.ts +144 -0
  76. package/v3/@claude-flow/memory/dist/persistent-sona.js +332 -0
  77. package/v3/@claude-flow/memory/dist/query-builder.d.ts +211 -0
  78. package/v3/@claude-flow/memory/dist/query-builder.js +438 -0
  79. package/v3/@claude-flow/memory/dist/rvf-backend.d.ts +51 -0
  80. package/v3/@claude-flow/memory/dist/rvf-backend.js +481 -0
  81. package/v3/@claude-flow/memory/dist/rvf-learning-store.d.ts +139 -0
  82. package/v3/@claude-flow/memory/dist/rvf-learning-store.js +295 -0
  83. package/v3/@claude-flow/memory/dist/rvf-migration.d.ts +45 -0
  84. package/v3/@claude-flow/memory/dist/rvf-migration.js +254 -0
  85. package/v3/@claude-flow/memory/dist/sqlite-backend.d.ts +121 -0
  86. package/v3/@claude-flow/memory/dist/sqlite-backend.js +564 -0
  87. package/v3/@claude-flow/memory/dist/sqljs-backend.d.ts +128 -0
  88. package/v3/@claude-flow/memory/dist/sqljs-backend.js +601 -0
  89. package/v3/@claude-flow/memory/dist/types.d.ts +484 -0
  90. package/v3/@claude-flow/memory/dist/types.js +58 -0
  91. package/v3/@claude-flow/memory/package.json +46 -0
@@ -0,0 +1,333 @@
1
+ /**
2
+ * Knowledge Graph Module for @claude-flow/memory
3
+ *
4
+ * Builds a graph from MemoryEntry.references, computes PageRank,
5
+ * detects communities via label propagation, and provides
6
+ * graph-aware ranking for search results.
7
+ *
8
+ * Pure TypeScript - no external graph libraries.
9
+ * @module v3/memory/memory-graph
10
+ */
11
+ import { EventEmitter } from 'node:events';
12
+ const DEFAULT_CONFIG = {
13
+ similarityThreshold: 0.8,
14
+ pageRankDamping: 0.85,
15
+ pageRankIterations: 50,
16
+ pageRankConvergence: 1e-6,
17
+ maxNodes: 5000,
18
+ enableAutoEdges: true,
19
+ communityAlgorithm: 'label-propagation',
20
+ };
21
+ /**
22
+ * Knowledge graph built from memory entry references.
23
+ * Supports PageRank, community detection (label propagation),
24
+ * and graph-aware result ranking blending vector similarity with structural importance.
25
+ */
26
+ export class MemoryGraph extends EventEmitter {
27
+ nodes = new Map();
28
+ edges = new Map();
29
+ reverseEdges = new Map();
30
+ pageRanks = new Map();
31
+ communities = new Map();
32
+ config;
33
+ dirty = true;
34
+ constructor(config) {
35
+ super();
36
+ this.config = { ...DEFAULT_CONFIG, ...config };
37
+ }
38
+ /** Build graph from all entries in a backend. Creates nodes and reference edges. */
39
+ async buildFromBackend(backend, namespace) {
40
+ const entries = await backend.query({
41
+ type: 'hybrid',
42
+ namespace,
43
+ limit: this.config.maxNodes,
44
+ });
45
+ for (const entry of entries) {
46
+ this.addNode(entry);
47
+ }
48
+ for (const entry of entries) {
49
+ for (const refId of entry.references) {
50
+ this.addEdge(entry.id, refId, 'reference');
51
+ }
52
+ }
53
+ this.dirty = true;
54
+ this.emit('graph:built', { nodeCount: this.nodes.size });
55
+ }
56
+ /** Add a node from a MemoryEntry. Skips silently at maxNodes capacity. */
57
+ addNode(entry) {
58
+ if (this.nodes.size >= this.config.maxNodes && !this.nodes.has(entry.id)) {
59
+ return;
60
+ }
61
+ this.nodes.set(entry.id, {
62
+ id: entry.id,
63
+ category: entry.metadata?.category || 'general',
64
+ confidence: entry.metadata?.confidence || 0.5,
65
+ accessCount: entry.accessCount,
66
+ createdAt: entry.createdAt,
67
+ });
68
+ if (!this.edges.has(entry.id))
69
+ this.edges.set(entry.id, []);
70
+ if (!this.reverseEdges.has(entry.id))
71
+ this.reverseEdges.set(entry.id, new Set());
72
+ this.dirty = true;
73
+ }
74
+ /** Add a directed edge. Skips if either node missing. Updates weight to max if exists. */
75
+ addEdge(sourceId, targetId, type, weight = 1.0) {
76
+ if (!this.nodes.has(sourceId) || !this.nodes.has(targetId))
77
+ return;
78
+ const edgeList = this.edges.get(sourceId) || [];
79
+ if (!this.edges.has(sourceId))
80
+ this.edges.set(sourceId, edgeList);
81
+ const existing = edgeList.find((e) => e.targetId === targetId);
82
+ if (existing) {
83
+ existing.weight = Math.max(existing.weight, weight);
84
+ }
85
+ else {
86
+ edgeList.push({ targetId, type, weight });
87
+ }
88
+ if (!this.reverseEdges.has(targetId))
89
+ this.reverseEdges.set(targetId, new Set());
90
+ this.reverseEdges.get(targetId).add(sourceId);
91
+ this.dirty = true;
92
+ }
93
+ /** Remove a node and all associated edges (both directions). */
94
+ removeNode(id) {
95
+ if (!this.nodes.has(id))
96
+ return;
97
+ // Remove outgoing edges and their reverse entries
98
+ for (const edge of this.edges.get(id) || []) {
99
+ this.reverseEdges.get(edge.targetId)?.delete(id);
100
+ }
101
+ this.edges.delete(id);
102
+ // Remove incoming edges pointing to this node
103
+ const incoming = this.reverseEdges.get(id);
104
+ if (incoming) {
105
+ for (const sourceId of incoming) {
106
+ const srcEdges = this.edges.get(sourceId);
107
+ if (srcEdges)
108
+ this.edges.set(sourceId, srcEdges.filter((e) => e.targetId !== id));
109
+ }
110
+ }
111
+ this.reverseEdges.delete(id);
112
+ this.nodes.delete(id);
113
+ this.pageRanks.delete(id);
114
+ this.communities.delete(id);
115
+ this.dirty = true;
116
+ }
117
+ /** Add similarity edges by searching backend. Returns count of edges added. */
118
+ async addSimilarityEdges(backend, entryId) {
119
+ const entry = await backend.get(entryId);
120
+ if (!entry || !entry.embedding)
121
+ return 0;
122
+ const results = await backend.search(entry.embedding, {
123
+ k: 20,
124
+ threshold: this.config.similarityThreshold,
125
+ });
126
+ let added = 0;
127
+ for (const result of results) {
128
+ if (result.entry.id === entryId)
129
+ continue;
130
+ if (result.score >= this.config.similarityThreshold) {
131
+ const hadEdge = this.hasEdge(entryId, result.entry.id);
132
+ this.addEdge(entryId, result.entry.id, 'similar', result.score);
133
+ if (!hadEdge)
134
+ added++;
135
+ }
136
+ }
137
+ return added;
138
+ }
139
+ /**
140
+ * Compute PageRank via power iteration with dangling node redistribution.
141
+ * Returns map of node ID to PageRank score.
142
+ */
143
+ computePageRank() {
144
+ const N = this.nodes.size;
145
+ if (N === 0) {
146
+ this.dirty = false;
147
+ this.emit('pagerank:computed', { iterations: 0 });
148
+ return new Map();
149
+ }
150
+ const d = this.config.pageRankDamping;
151
+ for (const nodeId of this.nodes.keys()) {
152
+ this.pageRanks.set(nodeId, 1 / N);
153
+ }
154
+ // Identify dangling nodes (no outgoing edges)
155
+ const danglingNodes = [];
156
+ for (const nodeId of this.nodes.keys()) {
157
+ const out = this.edges.get(nodeId);
158
+ if (!out || out.length === 0)
159
+ danglingNodes.push(nodeId);
160
+ }
161
+ let iterations = 0;
162
+ for (let iter = 0; iter < this.config.pageRankIterations; iter++) {
163
+ let maxDelta = 0;
164
+ const newRanks = new Map();
165
+ let danglingSum = 0;
166
+ for (const nodeId of danglingNodes) {
167
+ danglingSum += this.pageRanks.get(nodeId) || 0;
168
+ }
169
+ for (const nodeId of this.nodes.keys()) {
170
+ let sum = 0;
171
+ const incoming = this.reverseEdges.get(nodeId);
172
+ if (incoming) {
173
+ for (const sourceId of incoming) {
174
+ const outDegree = this.edges.get(sourceId)?.length || 1;
175
+ sum += (this.pageRanks.get(sourceId) || 0) / outDegree;
176
+ }
177
+ }
178
+ const newRank = (1 - d) / N + d * (sum + danglingSum / N);
179
+ newRanks.set(nodeId, newRank);
180
+ maxDelta = Math.max(maxDelta, Math.abs(newRank - (this.pageRanks.get(nodeId) || 0)));
181
+ }
182
+ this.pageRanks = newRanks;
183
+ iterations = iter + 1;
184
+ if (maxDelta < this.config.pageRankConvergence)
185
+ break;
186
+ }
187
+ this.dirty = false;
188
+ this.emit('pagerank:computed', { iterations });
189
+ return new Map(this.pageRanks);
190
+ }
191
+ /** Detect communities using label propagation. Returns map of nodeId to communityId. */
192
+ detectCommunities() {
193
+ const labels = new Map();
194
+ for (const nodeId of this.nodes.keys())
195
+ labels.set(nodeId, nodeId);
196
+ for (let iter = 0; iter < 20; iter++) {
197
+ let changed = false;
198
+ const nodeIds = this.shuffleArray([...this.nodes.keys()]);
199
+ for (const nodeId of nodeIds) {
200
+ const labelCounts = new Map();
201
+ for (const edge of this.edges.get(nodeId) || []) {
202
+ const lbl = labels.get(edge.targetId);
203
+ if (lbl !== undefined)
204
+ labelCounts.set(lbl, (labelCounts.get(lbl) || 0) + edge.weight);
205
+ }
206
+ const incoming = this.reverseEdges.get(nodeId);
207
+ if (incoming) {
208
+ for (const sourceId of incoming) {
209
+ const lbl = labels.get(sourceId);
210
+ if (lbl !== undefined)
211
+ labelCounts.set(lbl, (labelCounts.get(lbl) || 0) + 1);
212
+ }
213
+ }
214
+ if (labelCounts.size > 0) {
215
+ let maxLabel = labels.get(nodeId);
216
+ let maxCount = 0;
217
+ for (const [label, count] of labelCounts) {
218
+ if (count > maxCount) {
219
+ maxCount = count;
220
+ maxLabel = label;
221
+ }
222
+ }
223
+ if (maxLabel !== labels.get(nodeId)) {
224
+ labels.set(nodeId, maxLabel);
225
+ changed = true;
226
+ }
227
+ }
228
+ }
229
+ if (!changed)
230
+ break;
231
+ }
232
+ this.communities = labels;
233
+ this.emit('communities:detected', { communityCount: new Set(labels.values()).size });
234
+ return new Map(this.communities);
235
+ }
236
+ /**
237
+ * Rank search results blending vector similarity and PageRank.
238
+ * @param alpha - Weight for vector score (default 0.7). PageRank weight is (1 - alpha).
239
+ */
240
+ rankWithGraph(searchResults, alpha = 0.7) {
241
+ if (this.dirty)
242
+ this.computePageRank();
243
+ const N = this.nodes.size || 1;
244
+ return searchResults
245
+ .map((result) => {
246
+ const pageRank = this.pageRanks.get(result.entry.id) || 0;
247
+ return {
248
+ entry: result.entry,
249
+ score: result.score,
250
+ pageRank,
251
+ combinedScore: alpha * result.score + (1 - alpha) * (pageRank * N),
252
+ community: this.communities.get(result.entry.id),
253
+ };
254
+ })
255
+ .sort((a, b) => b.combinedScore - a.combinedScore);
256
+ }
257
+ /** Get top N nodes by PageRank score. */
258
+ getTopNodes(n) {
259
+ if (this.dirty)
260
+ this.computePageRank();
261
+ return [...this.pageRanks.entries()]
262
+ .sort((a, b) => b[1] - a[1])
263
+ .slice(0, n)
264
+ .map(([id, pageRank]) => ({
265
+ id,
266
+ pageRank,
267
+ community: this.communities.get(id) || id,
268
+ }));
269
+ }
270
+ /** BFS neighbors from a node up to given depth. Excludes the start node. */
271
+ getNeighbors(id, depth = 1) {
272
+ const visited = new Set();
273
+ let frontier = new Set([id]);
274
+ for (let d = 0; d < depth; d++) {
275
+ const nextFrontier = new Set();
276
+ for (const nodeId of frontier) {
277
+ for (const edge of this.edges.get(nodeId) || []) {
278
+ if (!visited.has(edge.targetId) && edge.targetId !== id) {
279
+ visited.add(edge.targetId);
280
+ nextFrontier.add(edge.targetId);
281
+ }
282
+ }
283
+ }
284
+ frontier = nextFrontier;
285
+ if (frontier.size === 0)
286
+ break;
287
+ }
288
+ return visited;
289
+ }
290
+ /** Get statistics about the current graph state. */
291
+ getStats() {
292
+ let totalEdges = 0;
293
+ for (const edgeList of this.edges.values())
294
+ totalEdges += edgeList.length;
295
+ const nodeCount = this.nodes.size;
296
+ let maxPageRank = 0;
297
+ let minPageRank = Infinity;
298
+ if (this.pageRanks.size > 0) {
299
+ for (const rank of this.pageRanks.values()) {
300
+ if (rank > maxPageRank)
301
+ maxPageRank = rank;
302
+ if (rank < minPageRank)
303
+ minPageRank = rank;
304
+ }
305
+ }
306
+ else {
307
+ minPageRank = 0;
308
+ }
309
+ return {
310
+ nodeCount,
311
+ edgeCount: totalEdges,
312
+ avgDegree: nodeCount > 0 ? totalEdges / nodeCount : 0,
313
+ communityCount: new Set(this.communities.values()).size,
314
+ pageRankComputed: !this.dirty,
315
+ maxPageRank,
316
+ minPageRank,
317
+ };
318
+ }
319
+ // ===== Internal Helpers =====
320
+ hasEdge(sourceId, targetId) {
321
+ const edgeList = this.edges.get(sourceId);
322
+ return edgeList ? edgeList.some((e) => e.targetId === targetId) : false;
323
+ }
324
+ shuffleArray(arr) {
325
+ const shuffled = [...arr];
326
+ for (let i = shuffled.length - 1; i > 0; i--) {
327
+ const j = Math.floor(Math.random() * (i + 1));
328
+ [shuffled[i], shuffled[j]] = [shuffled[j], shuffled[i]];
329
+ }
330
+ return shuffled;
331
+ }
332
+ }
333
+ //# sourceMappingURL=memory-graph.js.map
@@ -0,0 +1,8 @@
1
+ /**
2
+ * Tests for MemoryGraph - Knowledge Graph Module
3
+ *
4
+ * TDD London School (mock-first) tests for graph construction,
5
+ * PageRank computation, community detection, and graph-aware ranking.
6
+ */
7
+ export {};
8
+ //# sourceMappingURL=memory-graph.test.d.ts.map