claude-cortex 1.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +291 -0
- package/dist/api/events.d.ts +134 -0
- package/dist/api/events.d.ts.map +1 -0
- package/dist/api/events.js +73 -0
- package/dist/api/events.js.map +1 -0
- package/dist/api/visualization-server.d.ts +11 -0
- package/dist/api/visualization-server.d.ts.map +1 -0
- package/dist/api/visualization-server.js +653 -0
- package/dist/api/visualization-server.js.map +1 -0
- package/dist/context/project-context.d.ts +57 -0
- package/dist/context/project-context.d.ts.map +1 -0
- package/dist/context/project-context.js +135 -0
- package/dist/context/project-context.js.map +1 -0
- package/dist/database/init.d.ts +49 -0
- package/dist/database/init.d.ts.map +1 -0
- package/dist/database/init.js +336 -0
- package/dist/database/init.js.map +1 -0
- package/dist/embeddings/generator.d.ts +20 -0
- package/dist/embeddings/generator.d.ts.map +1 -0
- package/dist/embeddings/generator.js +77 -0
- package/dist/embeddings/generator.js.map +1 -0
- package/dist/embeddings/index.d.ts +2 -0
- package/dist/embeddings/index.d.ts.map +1 -0
- package/dist/embeddings/index.js +2 -0
- package/dist/embeddings/index.js.map +1 -0
- package/dist/errors.d.ts +74 -0
- package/dist/errors.d.ts.map +1 -0
- package/dist/errors.js +131 -0
- package/dist/errors.js.map +1 -0
- package/dist/index.d.ts +16 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +83 -0
- package/dist/index.js.map +1 -0
- package/dist/memory/activation.d.ts +69 -0
- package/dist/memory/activation.d.ts.map +1 -0
- package/dist/memory/activation.js +168 -0
- package/dist/memory/activation.js.map +1 -0
- package/dist/memory/consolidate.d.ts +96 -0
- package/dist/memory/consolidate.d.ts.map +1 -0
- package/dist/memory/consolidate.js +400 -0
- package/dist/memory/consolidate.js.map +1 -0
- package/dist/memory/contradiction.d.ts +69 -0
- package/dist/memory/contradiction.d.ts.map +1 -0
- package/dist/memory/contradiction.js +286 -0
- package/dist/memory/contradiction.js.map +1 -0
- package/dist/memory/decay.d.ts +62 -0
- package/dist/memory/decay.d.ts.map +1 -0
- package/dist/memory/decay.js +184 -0
- package/dist/memory/decay.js.map +1 -0
- package/dist/memory/salience.d.ts +36 -0
- package/dist/memory/salience.d.ts.map +1 -0
- package/dist/memory/salience.js +200 -0
- package/dist/memory/salience.js.map +1 -0
- package/dist/memory/similarity.d.ts +57 -0
- package/dist/memory/similarity.d.ts.map +1 -0
- package/dist/memory/similarity.js +114 -0
- package/dist/memory/similarity.js.map +1 -0
- package/dist/memory/store.d.ts +170 -0
- package/dist/memory/store.d.ts.map +1 -0
- package/dist/memory/store.js +973 -0
- package/dist/memory/store.js.map +1 -0
- package/dist/memory/types.d.ts +91 -0
- package/dist/memory/types.d.ts.map +1 -0
- package/dist/memory/types.js +30 -0
- package/dist/memory/types.js.map +1 -0
- package/dist/server.d.ts +12 -0
- package/dist/server.d.ts.map +1 -0
- package/dist/server.js +466 -0
- package/dist/server.js.map +1 -0
- package/dist/tools/context.d.ts +135 -0
- package/dist/tools/context.d.ts.map +1 -0
- package/dist/tools/context.js +273 -0
- package/dist/tools/context.js.map +1 -0
- package/dist/tools/forget.d.ts +53 -0
- package/dist/tools/forget.d.ts.map +1 -0
- package/dist/tools/forget.js +179 -0
- package/dist/tools/forget.js.map +1 -0
- package/dist/tools/recall.d.ts +74 -0
- package/dist/tools/recall.d.ts.map +1 -0
- package/dist/tools/recall.js +140 -0
- package/dist/tools/recall.js.map +1 -0
- package/dist/tools/remember.d.ts +65 -0
- package/dist/tools/remember.d.ts.map +1 -0
- package/dist/tools/remember.js +147 -0
- package/dist/tools/remember.js.map +1 -0
- package/dist/worker/brain-worker.d.ts +100 -0
- package/dist/worker/brain-worker.d.ts.map +1 -0
- package/dist/worker/brain-worker.js +261 -0
- package/dist/worker/brain-worker.js.map +1 -0
- package/dist/worker/link-discovery.d.ts +47 -0
- package/dist/worker/link-discovery.d.ts.map +1 -0
- package/dist/worker/link-discovery.js +103 -0
- package/dist/worker/link-discovery.js.map +1 -0
- package/dist/worker/predictive-consolidation.d.ts +46 -0
- package/dist/worker/predictive-consolidation.d.ts.map +1 -0
- package/dist/worker/predictive-consolidation.js +110 -0
- package/dist/worker/predictive-consolidation.js.map +1 -0
- package/dist/worker/types.d.ts +91 -0
- package/dist/worker/types.d.ts.map +1 -0
- package/dist/worker/types.js +22 -0
- package/dist/worker/types.js.map +1 -0
- package/package.json +59 -0
|
@@ -0,0 +1,261 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Brain Worker
|
|
3
|
+
*
|
|
4
|
+
* Phase 4 Organic Brain Feature
|
|
5
|
+
*
|
|
6
|
+
* Background worker that performs brain-like maintenance operations:
|
|
7
|
+
* - Light tick (5 min): Prune activation cache, check predictive consolidation
|
|
8
|
+
* - Medium tick (30 min): Discover missing links, scan for contradictions
|
|
9
|
+
*
|
|
10
|
+
* This transforms the memory system from reactive to continuously organic.
|
|
11
|
+
*/
|
|
12
|
+
import { DEFAULT_WORKER_CONFIG, } from './types.js';
|
|
13
|
+
import { pruneActivationCache } from '../memory/activation.js';
|
|
14
|
+
import { getMemoryStats } from '../memory/store.js';
|
|
15
|
+
import { consolidate } from '../memory/consolidate.js';
|
|
16
|
+
import { detectContradictions, linkContradictions, } from '../memory/contradiction.js';
|
|
17
|
+
import { discoverMissingLinks, findUnlinkedMemories } from './link-discovery.js';
|
|
18
|
+
import { shouldTriggerPredictiveConsolidation } from './predictive-consolidation.js';
|
|
19
|
+
import { emitWorkerLightTick, emitWorkerMediumTick, emitPredictiveConsolidation, } from '../api/events.js';
|
|
20
|
+
/**
|
|
21
|
+
* Brain Worker Class
|
|
22
|
+
*
|
|
23
|
+
* Manages background processing timers and operations.
|
|
24
|
+
* Designed to be started by the visualization server and run continuously.
|
|
25
|
+
*/
|
|
26
|
+
export class BrainWorker {
|
|
27
|
+
lightTimer = null;
|
|
28
|
+
mediumTimer = null;
|
|
29
|
+
isRunning = false;
|
|
30
|
+
config;
|
|
31
|
+
// Statistics tracking
|
|
32
|
+
stats = {
|
|
33
|
+
lightTicks: 0,
|
|
34
|
+
mediumTicks: 0,
|
|
35
|
+
consolidations: 0,
|
|
36
|
+
};
|
|
37
|
+
// Timestamps
|
|
38
|
+
lastLightTick = null;
|
|
39
|
+
lastMediumTick = null;
|
|
40
|
+
lastConsolidation = null;
|
|
41
|
+
/**
|
|
42
|
+
* Create a new BrainWorker
|
|
43
|
+
*
|
|
44
|
+
* @param config - Partial configuration to override defaults
|
|
45
|
+
*/
|
|
46
|
+
constructor(config = {}) {
|
|
47
|
+
this.config = { ...DEFAULT_WORKER_CONFIG, ...config };
|
|
48
|
+
}
|
|
49
|
+
/**
|
|
50
|
+
* Start the background worker
|
|
51
|
+
* Sets up interval timers for light and medium ticks
|
|
52
|
+
*/
|
|
53
|
+
start() {
|
|
54
|
+
if (this.isRunning) {
|
|
55
|
+
console.log('[BrainWorker] Already running');
|
|
56
|
+
return;
|
|
57
|
+
}
|
|
58
|
+
this.isRunning = true;
|
|
59
|
+
console.log('[BrainWorker] Starting background worker');
|
|
60
|
+
console.log(`[BrainWorker] Light tick interval: ${this.config.lightTickIntervalMs / 1000}s`);
|
|
61
|
+
console.log(`[BrainWorker] Medium tick interval: ${this.config.mediumTickIntervalMs / 1000}s`);
|
|
62
|
+
// Light tick every 5 minutes (by default)
|
|
63
|
+
this.lightTimer = setInterval(() => this.lightTick(), this.config.lightTickIntervalMs);
|
|
64
|
+
// Medium tick every 30 minutes (by default)
|
|
65
|
+
this.mediumTimer = setInterval(() => this.mediumTick(), this.config.mediumTickIntervalMs);
|
|
66
|
+
// Run initial light tick after a short delay (10 seconds)
|
|
67
|
+
// This allows the server to fully initialize first
|
|
68
|
+
setTimeout(() => {
|
|
69
|
+
if (this.isRunning) {
|
|
70
|
+
this.lightTick();
|
|
71
|
+
}
|
|
72
|
+
}, 10000);
|
|
73
|
+
}
|
|
74
|
+
/**
|
|
75
|
+
* Stop the background worker
|
|
76
|
+
* Clears all interval timers
|
|
77
|
+
*/
|
|
78
|
+
stop() {
|
|
79
|
+
if (!this.isRunning) {
|
|
80
|
+
console.log('[BrainWorker] Not running');
|
|
81
|
+
return;
|
|
82
|
+
}
|
|
83
|
+
this.isRunning = false;
|
|
84
|
+
if (this.lightTimer) {
|
|
85
|
+
clearInterval(this.lightTimer);
|
|
86
|
+
this.lightTimer = null;
|
|
87
|
+
}
|
|
88
|
+
if (this.mediumTimer) {
|
|
89
|
+
clearInterval(this.mediumTimer);
|
|
90
|
+
this.mediumTimer = null;
|
|
91
|
+
}
|
|
92
|
+
console.log('[BrainWorker] Stopped');
|
|
93
|
+
}
|
|
94
|
+
/**
|
|
95
|
+
* Check if the worker is currently running
|
|
96
|
+
*/
|
|
97
|
+
isActive() {
|
|
98
|
+
return this.isRunning;
|
|
99
|
+
}
|
|
100
|
+
/**
|
|
101
|
+
* Light tick - runs every 5 minutes
|
|
102
|
+
*
|
|
103
|
+
* Operations:
|
|
104
|
+
* 1. Prune stale activation cache entries
|
|
105
|
+
* 2. Check if predictive consolidation should run
|
|
106
|
+
*/
|
|
107
|
+
async lightTick() {
|
|
108
|
+
const result = {
|
|
109
|
+
activationsPruned: 0,
|
|
110
|
+
predictiveConsolidation: null,
|
|
111
|
+
timestamp: new Date(),
|
|
112
|
+
};
|
|
113
|
+
try {
|
|
114
|
+
// 1. Prune activation cache
|
|
115
|
+
result.activationsPruned = pruneActivationCache();
|
|
116
|
+
// 2. Check if predictive consolidation is needed
|
|
117
|
+
const stats = getMemoryStats();
|
|
118
|
+
const decision = shouldTriggerPredictiveConsolidation(stats, this.config);
|
|
119
|
+
if (decision.shouldRun) {
|
|
120
|
+
console.log(`[BrainWorker] Predictive consolidation triggered: ${decision.reason}`);
|
|
121
|
+
result.predictiveConsolidation = consolidate();
|
|
122
|
+
this.lastConsolidation = new Date();
|
|
123
|
+
this.stats.consolidations++;
|
|
124
|
+
// Emit event for dashboard
|
|
125
|
+
emitPredictiveConsolidation({
|
|
126
|
+
trigger: decision.reason,
|
|
127
|
+
urgency: decision.urgency,
|
|
128
|
+
result: result.predictiveConsolidation,
|
|
129
|
+
});
|
|
130
|
+
}
|
|
131
|
+
// Update stats
|
|
132
|
+
this.lastLightTick = result.timestamp;
|
|
133
|
+
this.stats.lightTicks++;
|
|
134
|
+
// Emit light tick event
|
|
135
|
+
emitWorkerLightTick(result);
|
|
136
|
+
// Log summary
|
|
137
|
+
if (result.activationsPruned > 0 || result.predictiveConsolidation) {
|
|
138
|
+
console.log(`[BrainWorker] Light tick: pruned ${result.activationsPruned} activations` +
|
|
139
|
+
(result.predictiveConsolidation
|
|
140
|
+
? `, consolidated ${result.predictiveConsolidation.consolidated}`
|
|
141
|
+
: ''));
|
|
142
|
+
}
|
|
143
|
+
}
|
|
144
|
+
catch (e) {
|
|
145
|
+
console.error('[BrainWorker] Light tick failed:', e);
|
|
146
|
+
}
|
|
147
|
+
return result;
|
|
148
|
+
}
|
|
149
|
+
/**
|
|
150
|
+
* Medium tick - runs every 30 minutes
|
|
151
|
+
*
|
|
152
|
+
* Operations:
|
|
153
|
+
* 1. Discover and create missing links
|
|
154
|
+
* 2. Scan for contradictions
|
|
155
|
+
*/
|
|
156
|
+
async mediumTick() {
|
|
157
|
+
const result = {
|
|
158
|
+
linksDiscovered: 0,
|
|
159
|
+
contradictionsFound: 0,
|
|
160
|
+
contradictionsLinked: 0,
|
|
161
|
+
memoriesScanned: 0,
|
|
162
|
+
timestamp: new Date(),
|
|
163
|
+
};
|
|
164
|
+
try {
|
|
165
|
+
// 1. Link discovery - find unlinked memories and create relationships
|
|
166
|
+
const unlinked = findUnlinkedMemories(this.config.maxLinksPerCycle);
|
|
167
|
+
result.memoriesScanned = unlinked.length;
|
|
168
|
+
result.linksDiscovered = discoverMissingLinks(this.config.maxLinksPerCycle);
|
|
169
|
+
// 2. Contradiction scan
|
|
170
|
+
const contradictions = detectContradictions({
|
|
171
|
+
minScore: 0.5,
|
|
172
|
+
limit: this.config.contradictionScanLimit,
|
|
173
|
+
});
|
|
174
|
+
result.contradictionsFound = contradictions.length;
|
|
175
|
+
result.contradictionsLinked = linkContradictions(contradictions);
|
|
176
|
+
// Update stats
|
|
177
|
+
this.lastMediumTick = result.timestamp;
|
|
178
|
+
this.stats.mediumTicks++;
|
|
179
|
+
// Emit medium tick event
|
|
180
|
+
emitWorkerMediumTick(result);
|
|
181
|
+
// Log summary
|
|
182
|
+
console.log(`[BrainWorker] Medium tick: scanned ${result.memoriesScanned} memories, ` +
|
|
183
|
+
`discovered ${result.linksDiscovered} links, ` +
|
|
184
|
+
`found ${result.contradictionsFound} contradictions`);
|
|
185
|
+
}
|
|
186
|
+
catch (e) {
|
|
187
|
+
console.error('[BrainWorker] Medium tick failed:', e);
|
|
188
|
+
}
|
|
189
|
+
return result;
|
|
190
|
+
}
|
|
191
|
+
/**
|
|
192
|
+
* Get current worker status
|
|
193
|
+
*/
|
|
194
|
+
getStatus() {
|
|
195
|
+
return {
|
|
196
|
+
isRunning: this.isRunning,
|
|
197
|
+
lastLightTick: this.lastLightTick,
|
|
198
|
+
lastMediumTick: this.lastMediumTick,
|
|
199
|
+
lastConsolidation: this.lastConsolidation,
|
|
200
|
+
stats: { ...this.stats },
|
|
201
|
+
};
|
|
202
|
+
}
|
|
203
|
+
/**
|
|
204
|
+
* Get current configuration
|
|
205
|
+
*/
|
|
206
|
+
getConfig() {
|
|
207
|
+
return { ...this.config };
|
|
208
|
+
}
|
|
209
|
+
/**
|
|
210
|
+
* Update configuration
|
|
211
|
+
* Note: Changes won't affect running timers until restart
|
|
212
|
+
*/
|
|
213
|
+
updateConfig(config) {
|
|
214
|
+
this.config = { ...this.config, ...config };
|
|
215
|
+
}
|
|
216
|
+
// Manual triggers for testing and API endpoints
|
|
217
|
+
/**
|
|
218
|
+
* Manually trigger a light tick
|
|
219
|
+
* Useful for testing or immediate cache pruning
|
|
220
|
+
*/
|
|
221
|
+
triggerLightTick() {
|
|
222
|
+
return this.lightTick();
|
|
223
|
+
}
|
|
224
|
+
/**
|
|
225
|
+
* Manually trigger a medium tick
|
|
226
|
+
* Useful for testing or immediate link discovery
|
|
227
|
+
*/
|
|
228
|
+
triggerMediumTick() {
|
|
229
|
+
return this.mediumTick();
|
|
230
|
+
}
|
|
231
|
+
}
|
|
232
|
+
// Default singleton instance (optional - server can create its own)
|
|
233
|
+
let defaultWorker = null;
|
|
234
|
+
/**
|
|
235
|
+
* Get or create the default worker instance
|
|
236
|
+
*/
|
|
237
|
+
export function getDefaultWorker() {
|
|
238
|
+
if (!defaultWorker) {
|
|
239
|
+
defaultWorker = new BrainWorker();
|
|
240
|
+
}
|
|
241
|
+
return defaultWorker;
|
|
242
|
+
}
|
|
243
|
+
/**
|
|
244
|
+
* Start the default worker if not already running
|
|
245
|
+
*/
|
|
246
|
+
export function startDefaultWorker() {
|
|
247
|
+
const worker = getDefaultWorker();
|
|
248
|
+
if (!worker.isActive()) {
|
|
249
|
+
worker.start();
|
|
250
|
+
}
|
|
251
|
+
return worker;
|
|
252
|
+
}
|
|
253
|
+
/**
|
|
254
|
+
* Stop the default worker
|
|
255
|
+
*/
|
|
256
|
+
export function stopDefaultWorker() {
|
|
257
|
+
if (defaultWorker) {
|
|
258
|
+
defaultWorker.stop();
|
|
259
|
+
}
|
|
260
|
+
}
|
|
261
|
+
//# sourceMappingURL=brain-worker.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"brain-worker.js","sourceRoot":"","sources":["../../src/worker/brain-worker.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;GAUG;AAEH,OAAO,EAEL,qBAAqB,GAItB,MAAM,YAAY,CAAC;AACpB,OAAO,EAAE,oBAAoB,EAAE,MAAM,yBAAyB,CAAC;AAC/D,OAAO,EAAE,cAAc,EAAE,MAAM,oBAAoB,CAAC;AACpD,OAAO,EAAE,WAAW,EAAE,MAAM,0BAA0B,CAAC;AACvD,OAAO,EACL,oBAAoB,EACpB,kBAAkB,GACnB,MAAM,4BAA4B,CAAC;AACpC,OAAO,EAAE,oBAAoB,EAAE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IACvB,CAAC;AACH,CAAC"}
|
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Link Discovery Module
|
|
3
|
+
*
|
|
4
|
+
* Phase 4 Organic Brain Feature
|
|
5
|
+
*
|
|
6
|
+
* Finds memories that have no links and discovers relationships
|
|
7
|
+
* for them during background worker cycles.
|
|
8
|
+
*/
|
|
9
|
+
import { Memory } from '../memory/types.js';
|
|
10
|
+
/**
|
|
11
|
+
* Find memories that have no outgoing links
|
|
12
|
+
* These are candidates for relationship discovery
|
|
13
|
+
*
|
|
14
|
+
* @param limit - Maximum number of memories to return
|
|
15
|
+
* @returns Array of Memory objects with no outgoing links
|
|
16
|
+
*/
|
|
17
|
+
export declare function findUnlinkedMemories(limit?: number): Memory[];
|
|
18
|
+
/**
|
|
19
|
+
* Find memories with few links (under-linked)
|
|
20
|
+
* These might benefit from additional connections
|
|
21
|
+
*
|
|
22
|
+
* @param maxLinks - Maximum number of existing links to qualify
|
|
23
|
+
* @param limit - Maximum number of memories to return
|
|
24
|
+
* @returns Array of Memory objects with fewer than maxLinks links
|
|
25
|
+
*/
|
|
26
|
+
export declare function findUnderlinkedMemories(maxLinks?: number, limit?: number): Memory[];
|
|
27
|
+
/**
|
|
28
|
+
* Discover and create links for unlinked memories
|
|
29
|
+
* Called during medium tick to build the knowledge graph organically
|
|
30
|
+
*
|
|
31
|
+
* @param limit - Maximum number of memories to process per cycle
|
|
32
|
+
* @returns Number of new links created
|
|
33
|
+
*/
|
|
34
|
+
export declare function discoverMissingLinks(limit?: number): number;
|
|
35
|
+
/**
|
|
36
|
+
* Get statistics about memory link coverage
|
|
37
|
+
* Useful for dashboard visualization
|
|
38
|
+
*
|
|
39
|
+
* @returns Object with link coverage stats
|
|
40
|
+
*/
|
|
41
|
+
export declare function getLinkCoverageStats(): {
|
|
42
|
+
totalMemories: number;
|
|
43
|
+
memoriesWithLinks: number;
|
|
44
|
+
memoriesWithoutLinks: number;
|
|
45
|
+
averageLinksPerMemory: number;
|
|
46
|
+
};
|
|
47
|
+
//# sourceMappingURL=link-discovery.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"link-discovery.d.ts","sourceRoot":"","sources":["../../src/worker/link-discovery.ts"],"names":[],"mappings":"AAAA;;;;;;;GAOG;AAQH,OAAO,EAAE,MAAM,EAAE,MAAM,oBAAoB,CAAC;AAE5C;;;;;;GAMG;AACH,wBAAgB,oBAAoB,CAAC,KAAK,GAAE,MAAW,GAAG,MAAM,EAAE,CAcjE;AAED;;;;;;;GAOG;AACH,wBAAgB,uBAAuB,CACrC,QAAQ,GAAE,MAAU,EACpB,KAAK,GAAE,MAAW,GACjB,MAAM,EAAE,CAeV;AAED;;;;;;GAMG;AACH,wBAAgB,oBAAoB,CAAC,KAAK,GAAE,MAAW,GAAG,MAAM,CA8B/D;AAED;;;;;GAKG;AACH,wBAAgB,oBAAoB,IAAI;IACtC,aAAa,EAAE,MAAM,CAAC;IACtB,iBAAiB,EAAE,MAAM,CAAC;IAC1B,oBAAoB,EAAE,MAAM,CAAC;IAC7B,qBAAqB,EAAE,MAAM,CAAC;CAC/B,CAuBA"}
|
|
@@ -0,0 +1,103 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Link Discovery Module
|
|
3
|
+
*
|
|
4
|
+
* Phase 4 Organic Brain Feature
|
|
5
|
+
*
|
|
6
|
+
* Finds memories that have no links and discovers relationships
|
|
7
|
+
* for them during background worker cycles.
|
|
8
|
+
*/
|
|
9
|
+
import { getDatabase } from '../database/init.js';
|
|
10
|
+
import { rowToMemory, detectRelationships, createMemoryLink, } from '../memory/store.js';
|
|
11
|
+
/**
|
|
12
|
+
* Find memories that have no outgoing links
|
|
13
|
+
* These are candidates for relationship discovery
|
|
14
|
+
*
|
|
15
|
+
* @param limit - Maximum number of memories to return
|
|
16
|
+
* @returns Array of Memory objects with no outgoing links
|
|
17
|
+
*/
|
|
18
|
+
export function findUnlinkedMemories(limit = 10) {
|
|
19
|
+
const db = getDatabase();
|
|
20
|
+
// Find memories that are not the source of any link
|
|
21
|
+
// Prioritize by salience (important memories should be linked first)
|
|
22
|
+
const rows = db.prepare(`
|
|
23
|
+
SELECT m.* FROM memories m
|
|
24
|
+
LEFT JOIN memory_links ml ON m.id = ml.source_id
|
|
25
|
+
WHERE ml.id IS NULL
|
|
26
|
+
ORDER BY m.salience DESC, m.last_accessed DESC
|
|
27
|
+
LIMIT ?
|
|
28
|
+
`).all(limit);
|
|
29
|
+
return rows.map(rowToMemory);
|
|
30
|
+
}
|
|
31
|
+
/**
|
|
32
|
+
* Find memories with few links (under-linked)
|
|
33
|
+
* These might benefit from additional connections
|
|
34
|
+
*
|
|
35
|
+
* @param maxLinks - Maximum number of existing links to qualify
|
|
36
|
+
* @param limit - Maximum number of memories to return
|
|
37
|
+
* @returns Array of Memory objects with fewer than maxLinks links
|
|
38
|
+
*/
|
|
39
|
+
export function findUnderlinkedMemories(maxLinks = 2, limit = 10) {
|
|
40
|
+
const db = getDatabase();
|
|
41
|
+
// Find memories with fewer than maxLinks total links (as source or target)
|
|
42
|
+
const rows = db.prepare(`
|
|
43
|
+
SELECT m.*, COUNT(ml.id) as link_count
|
|
44
|
+
FROM memories m
|
|
45
|
+
LEFT JOIN memory_links ml ON m.id = ml.source_id OR m.id = ml.target_id
|
|
46
|
+
GROUP BY m.id
|
|
47
|
+
HAVING link_count < ?
|
|
48
|
+
ORDER BY m.salience DESC
|
|
49
|
+
LIMIT ?
|
|
50
|
+
`).all(maxLinks, limit);
|
|
51
|
+
return rows.map(rowToMemory);
|
|
52
|
+
}
|
|
53
|
+
/**
|
|
54
|
+
* Discover and create links for unlinked memories
|
|
55
|
+
* Called during medium tick to build the knowledge graph organically
|
|
56
|
+
*
|
|
57
|
+
* @param limit - Maximum number of memories to process per cycle
|
|
58
|
+
* @returns Number of new links created
|
|
59
|
+
*/
|
|
60
|
+
export function discoverMissingLinks(limit = 10) {
|
|
61
|
+
const unlinked = findUnlinkedMemories(limit);
|
|
62
|
+
let linksCreated = 0;
|
|
63
|
+
for (const memory of unlinked) {
|
|
64
|
+
try {
|
|
65
|
+
// Find up to 3 related memories
|
|
66
|
+
const relationships = detectRelationships(memory, 3);
|
|
67
|
+
for (const rel of relationships) {
|
|
68
|
+
const link = createMemoryLink(memory.id, rel.targetId, rel.relationship, rel.strength);
|
|
69
|
+
if (link) {
|
|
70
|
+
linksCreated++;
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
catch (e) {
|
|
75
|
+
// Log but don't stop - continue with other memories
|
|
76
|
+
console.error(`[BrainWorker] Link discovery failed for memory ${memory.id}:`, e);
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
return linksCreated;
|
|
80
|
+
}
|
|
81
|
+
/**
|
|
82
|
+
* Get statistics about memory link coverage
|
|
83
|
+
* Useful for dashboard visualization
|
|
84
|
+
*
|
|
85
|
+
* @returns Object with link coverage stats
|
|
86
|
+
*/
|
|
87
|
+
export function getLinkCoverageStats() {
|
|
88
|
+
const db = getDatabase();
|
|
89
|
+
const totalMemories = db.prepare('SELECT COUNT(*) as count FROM memories').get().count;
|
|
90
|
+
const memoriesWithLinks = db.prepare(`
|
|
91
|
+
SELECT COUNT(DISTINCT source_id) as count FROM memory_links
|
|
92
|
+
`).get().count;
|
|
93
|
+
const totalLinks = db.prepare('SELECT COUNT(*) as count FROM memory_links').get().count;
|
|
94
|
+
return {
|
|
95
|
+
totalMemories,
|
|
96
|
+
memoriesWithLinks,
|
|
97
|
+
memoriesWithoutLinks: totalMemories - memoriesWithLinks,
|
|
98
|
+
averageLinksPerMemory: totalMemories > 0
|
|
99
|
+
? totalLinks / totalMemories
|
|
100
|
+
: 0,
|
|
101
|
+
};
|
|
102
|
+
}
|
|
103
|
+
//# sourceMappingURL=link-discovery.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"link-discovery.js","sourceRoot":"","sources":["../../src/worker/link-discovery.ts"],"names":[],"mappings":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AG,CAAC,YAAY,EAChB,GAAG,CAAC,QAAQ,CACb,CAAC;gBACF,IAAI,IAAI,EAAE,CAAC;oBACT,YAAY,EAAE,CAAC;gBACjB,CAAC;YACH,CAAC;QACH,CAAC;QAAC,OAAO,CAAC,EAAE,CAAC;YACX,oDAAoD;YACpD,OAAO,CAAC,KAAK,CACX,kDAAkD,MAAM,CAAC,EAAE,GAAG,EAC9D,CAAC,CACF,CAAC;QACJ,CAAC;IACH,CAAC;IAED,OAAO,YAAY,CAAC;AACtB,CAAC;AAED;;;;;GAKG;AACH,MAAM,UAAU,oBAAoB;IAMlC,MAAM,EAAE,GAAG,WAAW,EAAE,CAAC;IAEzB,MAAM,aAAa,GAAI,EAAE,CAAC,OAAO,CAC/B,wCAAwC,CACzC,CAAC,GAAG,EAAwB,CAAC,KAAK,CAAC;IAEpC,MAAM,iBAAiB,GAAI,EAAE,CAAC,OAAO,CAAC;;GAErC,CAAC,CAAC,GAAG,EAAwB,CAAC,KAAK,CAAC;IAErC,MAAM,UAAU,GAAI,EAAE,CAAC,OAAO,CAC5B,4CAA4C,CAC7C,CAAC,GAAG,EAAwB,CAAC,KAAK,CAAC;IAEpC,OAAO;QACL,aAAa;QACb,iBAAiB;QACjB,oBAAoB,EAAE,aAAa,GAAG,iBAAiB;QACvD,qBAAqB,EAAE,aAAa,GAAG,CAAC;YACtC,CAAC,CAAC,UAAU,GAAG,aAAa;YAC5B,CAAC,CAAC,CAAC;KACN,CAAC;AACJ,CAAC"}
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Predictive Consolidation Module
|
|
3
|
+
*
|
|
4
|
+
* Phase 4 Organic Brain Feature
|
|
5
|
+
*
|
|
6
|
+
* Determines when to run consolidation early based on memory pressure
|
|
7
|
+
* and activity patterns, rather than waiting for the 4-hour cycle.
|
|
8
|
+
*/
|
|
9
|
+
import { WorkerConfig, PredictiveDecision } from './types.js';
|
|
10
|
+
/**
|
|
11
|
+
* Memory statistics type (matches getMemoryStats return type)
|
|
12
|
+
*/
|
|
13
|
+
export interface MemoryStats {
|
|
14
|
+
total: number;
|
|
15
|
+
shortTerm: number;
|
|
16
|
+
longTerm: number;
|
|
17
|
+
episodic: number;
|
|
18
|
+
byCategory: Record<string, number>;
|
|
19
|
+
averageSalience: number;
|
|
20
|
+
}
|
|
21
|
+
/**
|
|
22
|
+
* Determine if consolidation should run early
|
|
23
|
+
* More intelligent than just waiting for the 4-hour cycle
|
|
24
|
+
*
|
|
25
|
+
* @param stats - Current memory statistics
|
|
26
|
+
* @param config - Worker configuration
|
|
27
|
+
* @returns Decision object with shouldRun, reason, and urgency
|
|
28
|
+
*/
|
|
29
|
+
export declare function shouldTriggerPredictiveConsolidation(stats: MemoryStats, config: WorkerConfig): PredictiveDecision;
|
|
30
|
+
/**
|
|
31
|
+
* Get detailed memory pressure information
|
|
32
|
+
* Useful for dashboard visualization and debugging
|
|
33
|
+
*
|
|
34
|
+
* @param stats - Current memory statistics
|
|
35
|
+
* @param config - Worker configuration
|
|
36
|
+
* @returns Object with detailed pressure metrics
|
|
37
|
+
*/
|
|
38
|
+
export declare function getMemoryPressure(stats: MemoryStats, config: WorkerConfig): {
|
|
39
|
+
stmFullness: number;
|
|
40
|
+
totalFullness: number;
|
|
41
|
+
ltmFullness: number;
|
|
42
|
+
isWarning: boolean;
|
|
43
|
+
isCritical: boolean;
|
|
44
|
+
recommendation: string;
|
|
45
|
+
};
|
|
46
|
+
//# sourceMappingURL=predictive-consolidation.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"predictive-consolidation.d.ts","sourceRoot":"","sources":["../../src/worker/predictive-consolidation.ts"],"names":[],"mappings":"AAAA;;;;;;;GAOG;AAGH,OAAO,EAAE,YAAY,EAAE,kBAAkB,EAAE,MAAM,YAAY,CAAC;AAE9D;;GAEG;AACH,MAAM,WAAW,WAAW;IAC1B,KAAK,EAAE,MAAM,CAAC;IACd,SAAS,EAAE,MAAM,CAAC;IAClB,QAAQ,EAAE,MAAM,CAAC;IACjB,QAAQ,EAAE,MAAM,CAAC;IACjB,UAAU,EAAE,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,CAAC;IACnC,eAAe,EAAE,MAAM,CAAC;CACzB;AASD;;;;;;;GAOG;AACH,wBAAgB,oCAAoC,CAClD,KAAK,EAAE,WAAW,EAClB,MAAM,EAAE,YAAY,GACnB,kBAAkB,CA8DpB;AAED;;;;;;;GAOG;AACH,wBAAgB,iBAAiB,CAC/B,KAAK,EAAE,WAAW,EAClB,MAAM,EAAE,YAAY,GACnB;IACD,WAAW,EAAE,MAAM,CAAC;IACpB,aAAa,EAAE,MAAM,CAAC;IACtB,WAAW,EAAE,MAAM,CAAC;IACpB,SAAS,EAAE,OAAO,CAAC;IACnB,UAAU,EAAE,OAAO,CAAC;IACpB,cAAc,EAAE,MAAM,CAAC;CACxB,CAwBA"}
|
|
@@ -0,0 +1,110 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Predictive Consolidation Module
|
|
3
|
+
*
|
|
4
|
+
* Phase 4 Organic Brain Feature
|
|
5
|
+
*
|
|
6
|
+
* Determines when to run consolidation early based on memory pressure
|
|
7
|
+
* and activity patterns, rather than waiting for the 4-hour cycle.
|
|
8
|
+
*/
|
|
9
|
+
import { getDatabase } from '../database/init.js';
|
|
10
|
+
/**
|
|
11
|
+
* Memory limits (from DEFAULT_CONFIG)
|
|
12
|
+
*/
|
|
13
|
+
const MAX_SHORT_TERM = 100;
|
|
14
|
+
const MAX_LONG_TERM = 1000;
|
|
15
|
+
const MAX_TOTAL = MAX_SHORT_TERM + MAX_LONG_TERM;
|
|
16
|
+
/**
|
|
17
|
+
* Determine if consolidation should run early
|
|
18
|
+
* More intelligent than just waiting for the 4-hour cycle
|
|
19
|
+
*
|
|
20
|
+
* @param stats - Current memory statistics
|
|
21
|
+
* @param config - Worker configuration
|
|
22
|
+
* @returns Decision object with shouldRun, reason, and urgency
|
|
23
|
+
*/
|
|
24
|
+
export function shouldTriggerPredictiveConsolidation(stats, config) {
|
|
25
|
+
const stmFullness = stats.shortTerm / MAX_SHORT_TERM;
|
|
26
|
+
const totalFullness = stats.total / MAX_TOTAL;
|
|
27
|
+
// Critical: Over 85% STM capacity - consolidate immediately
|
|
28
|
+
if (stmFullness > config.stmCriticalThreshold) {
|
|
29
|
+
return {
|
|
30
|
+
shouldRun: true,
|
|
31
|
+
reason: `STM at ${(stmFullness * 100).toFixed(0)}% (critical threshold)`,
|
|
32
|
+
urgency: 'critical',
|
|
33
|
+
};
|
|
34
|
+
}
|
|
35
|
+
// High: Over 80% total capacity
|
|
36
|
+
if (totalFullness > config.totalMemoryWarningThreshold) {
|
|
37
|
+
return {
|
|
38
|
+
shouldRun: true,
|
|
39
|
+
reason: `Total memory at ${(totalFullness * 100).toFixed(0)}% capacity`,
|
|
40
|
+
urgency: 'high',
|
|
41
|
+
};
|
|
42
|
+
}
|
|
43
|
+
// Medium: Over 70% STM + high recent activity
|
|
44
|
+
if (stmFullness > config.stmWarningThreshold) {
|
|
45
|
+
const db = getDatabase();
|
|
46
|
+
// Check recent activity (memories created in last 30 minutes)
|
|
47
|
+
const recentActivity = db.prepare(`
|
|
48
|
+
SELECT COUNT(*) as count FROM memories
|
|
49
|
+
WHERE created_at > datetime('now', '-30 minutes')
|
|
50
|
+
`).get();
|
|
51
|
+
if (recentActivity.count >= config.highActivityThreshold) {
|
|
52
|
+
return {
|
|
53
|
+
shouldRun: true,
|
|
54
|
+
reason: `STM at ${(stmFullness * 100).toFixed(0)}% with high activity (${recentActivity.count} recent memories)`,
|
|
55
|
+
urgency: 'medium',
|
|
56
|
+
};
|
|
57
|
+
}
|
|
58
|
+
// Check if many memories are below deletion threshold
|
|
59
|
+
// These are candidates for cleanup
|
|
60
|
+
const lowScoreCount = db.prepare(`
|
|
61
|
+
SELECT COUNT(*) as count FROM memories
|
|
62
|
+
WHERE type = 'short_term' AND decayed_score < 0.25
|
|
63
|
+
`).get();
|
|
64
|
+
if (lowScoreCount.count > 15) {
|
|
65
|
+
return {
|
|
66
|
+
shouldRun: true,
|
|
67
|
+
reason: `${lowScoreCount.count} STM memories below salience threshold`,
|
|
68
|
+
urgency: 'medium',
|
|
69
|
+
};
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
// No consolidation needed
|
|
73
|
+
return {
|
|
74
|
+
shouldRun: false,
|
|
75
|
+
reason: 'No consolidation triggers met',
|
|
76
|
+
urgency: 'low',
|
|
77
|
+
};
|
|
78
|
+
}
|
|
79
|
+
/**
|
|
80
|
+
* Get detailed memory pressure information
|
|
81
|
+
* Useful for dashboard visualization and debugging
|
|
82
|
+
*
|
|
83
|
+
* @param stats - Current memory statistics
|
|
84
|
+
* @param config - Worker configuration
|
|
85
|
+
* @returns Object with detailed pressure metrics
|
|
86
|
+
*/
|
|
87
|
+
export function getMemoryPressure(stats, config) {
|
|
88
|
+
const stmFullness = stats.shortTerm / MAX_SHORT_TERM;
|
|
89
|
+
const ltmFullness = stats.longTerm / MAX_LONG_TERM;
|
|
90
|
+
const totalFullness = stats.total / MAX_TOTAL;
|
|
91
|
+
const isCritical = stmFullness > config.stmCriticalThreshold;
|
|
92
|
+
const isWarning = stmFullness > config.stmWarningThreshold ||
|
|
93
|
+
totalFullness > config.totalMemoryWarningThreshold;
|
|
94
|
+
let recommendation = 'Memory levels healthy';
|
|
95
|
+
if (isCritical) {
|
|
96
|
+
recommendation = 'Immediate consolidation recommended';
|
|
97
|
+
}
|
|
98
|
+
else if (isWarning) {
|
|
99
|
+
recommendation = 'Consider running consolidation soon';
|
|
100
|
+
}
|
|
101
|
+
return {
|
|
102
|
+
stmFullness,
|
|
103
|
+
totalFullness,
|
|
104
|
+
ltmFullness,
|
|
105
|
+
isWarning,
|
|
106
|
+
isCritical,
|
|
107
|
+
recommendation,
|
|
108
|
+
};
|
|
109
|
+
}
|
|
110
|
+
//# sourceMappingURL=predictive-consolidation.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"predict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