-
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathSCEEngine.ts
More file actions
242 lines (207 loc) · 8.44 KB
/
SCEEngine.ts
File metadata and controls
242 lines (207 loc) · 8.44 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
import {
EngineConfig,
EnginePhase,
KnowledgeGraph,
Node,
Synapse,
ActivatedNode,
SecurityRuleResult,
PruningLog
} from '../../../types';
import { PhaseManager } from './PhaseManager';
import { AdjacencyIndex } from '../graph/AdjacencyIndex';
import { GraphTraversal } from '../graph/GraphTraversal';
import { RelationshipManager } from '../graph/RelationshipManager';
import { runSpreadingActivation } from '../activation/SpreadingActivation';
import { applyEnergyDynamics } from '../activation/EnergyDynamics';
import { updateHebbianWeights } from '../learning/HebbianLearning';
import { CoActivationTracker } from '../learning/CoActivationTracker';
import { calculateMetrics } from '../metrics/Telemetry';
import { pruneWeakEdges } from '../pruning/EdgePruning';
import { pruneWithMMR } from '../pruning/MMRPruning';
import { detectContradictions } from '../safety/ContradictionDetector';
import { enforceOrthogonality } from '../safety/OrthogonalityEnforcer';
import { HyperedgeManager } from '../hyperedges/HyperedgeManager';
export class SCEEngine {
public graph: KnowledgeGraph;
public config: EngineConfig;
private phaseManager: PhaseManager;
private adjacencyIndex: AdjacencyIndex;
private traversal: GraphTraversal;
private relationshipManager: RelationshipManager;
private coActivationTracker: CoActivationTracker;
private hyperedgeManager: HyperedgeManager;
private sessionMetrics = {
connectionsThisSession: 0,
lastReset: Date.now()
};
private readonly MAX_CONNECTIONS_PER_SESSION = 15;
constructor(initialGraph?: KnowledgeGraph, config?: Partial<EngineConfig>) {
this.config = {
gamma: 0.9,
theta: 0.1,
heatBias: 0.4,
mmrLambda: 0.7,
maxActivationDepth: 3,
enableHebbian: true,
enableMemoryExpansion: true,
enablePruning: true,
enableSpreadingActivation: true,
enableHyperedges: true,
pruneConsolidatedEdges: true,
safeMode: true,
enableFirewall: true,
hybridRules: true,
extractionModel: 'llama3-70b-8192', // Default fallback
apiKeys: {},
baseUrls: {},
models: {},
...config
} as EngineConfig; // Cast to satisfy strict type checking if Partial misses some nested required props
this.graph = initialGraph || { nodes: {}, synapses: [], hyperedges: [] };
// Ensure nodes have physics properties
Object.values(this.graph.nodes).forEach(node => {
if (node.activation === undefined) node.activation = 0;
if (node.salience === undefined) node.salience = 0;
});
// Initialize Subsystems
this.phaseManager = new PhaseManager();
this.adjacencyIndex = new AdjacencyIndex(this.graph);
this.traversal = new GraphTraversal(this.adjacencyIndex);
this.relationshipManager = new RelationshipManager(this.graph, this.traversal);
this.coActivationTracker = new CoActivationTracker();
this.hyperedgeManager = new HyperedgeManager(this.graph, this.config);
// Build initial index
this.adjacencyIndex.get();
}
/**
* Set Cognitive Phase (Explore / Inference / Consolidate)
*/
setPhase(phase: EnginePhase) {
this.phaseManager.set(phase);
}
getPhase(): EnginePhase {
return this.phaseManager.get();
}
get phase() { return this.phaseManager.get(); }
/**
* Add Node to Graph
*/
addNode(node: Node) {
this.graph.nodes[node.id] = { ...node, activation: 0, salience: 0.5 };
this.adjacencyIndex.invalidate();
}
/**
* Add Synapse to Graph
*/
addEdge(synapse: Synapse) {
this.graph.synapses.push(synapse);
this.adjacencyIndex.invalidate();
}
/**
* Add Explicit Relationships with gating (Neurogenesis)
*/
addExplicitRelationships(relations: { source: string; target: string; type: string; confidence: number; context?: string }[]): { source: string, target: string, status: string }[] {
// Session Reset Check
const ONE_HOUR = 60 * 60 * 1000;
if (Date.now() - this.sessionMetrics.lastReset > ONE_HOUR) {
this.sessionMetrics = { connectionsThisSession: 0, lastReset: Date.now() };
}
if (this.sessionMetrics.connectionsThisSession >= this.MAX_CONNECTIONS_PER_SESSION) {
console.warn('[SCE] Session Connection Quota Exceeded.');
return [];
}
const results = this.relationshipManager.addExplicitRelationships(this.phase, relations);
this.sessionMetrics.connectionsThisSession += results.filter(r => r.status === 'created').length;
if (results.length > 0) this.adjacencyIndex.invalidate();
return results;
}
/**
* Spreading Activation
*/
spreadingActivation(seeds: string[]): ActivatedNode[] {
return runSpreadingActivation(this.graph, this.config, this.adjacencyIndex.get(), seeds);
}
/**
* Main Query Entry Point
*/
query(seedNodeIds: string[]): ActivatedNode[] {
// Find goal nodes to add as persistent sources
const goalNodes = Object.values(this.graph.nodes)
.filter(n => n.type === 'goal' && !n.isArchived)
.map(n => n.id);
const seeds = Array.from(new Set([...seedNodeIds, ...goalNodes]));
return this.spreadingActivation(seeds);
}
/**
* Pruning (MMR)
*/
pruneWithMMR(activated: ActivatedNode[], queryText: string, maxResults = 8): { selected: ActivatedNode[]; log: PruningLog[] } {
return pruneWithMMR(this.graph, this.config, activated, queryText, maxResults);
}
/**
* Hebbian Learning
*/
updateHebbianWeights(activatedList: ActivatedNode[]): { source: string, target: string, delta: number }[] {
return updateHebbianWeights(this.graph, this.config, this.phase, activatedList);
}
/**
* Energy Dynamics
*/
applyEnergyDynamics(alpha?: number) {
applyEnergyDynamics(this.graph, this.phase, this.config, alpha);
}
/**
* Post-Query Lifecycle Updates
*/
afterQuery(activatedNodes?: ActivatedNode[]) {
if (activatedNodes) {
// 1. Hebbian (internal logic moved to updateHebbianWeights call in pipeline usually,
// or we call it here if pipeline doesn't?
// The legacy code called trackCoActivations here.
this.coActivationTracker.track(activatedNodes);
}
// Note: The periodic consolidation logic was here in legacy.
// We'll leave it to external caller or implement periodic check if needed.
// For now, assuming pipeline drives lifecycle.
}
/**
* Run Periodic Logic (Safety, Pruning, Consolidation)
*/
consolidateGraphStructure() {
// 1. Co-Activation & Clique Consolidation
const stats = this.hyperedgeManager.consolidate(
this.adjacencyIndex.get(),
this.coActivationTracker.getMap()
);
// 2. Pruning
const pruned = pruneWeakEdges(this.graph);
// 3. Orthogonality
enforceOrthogonality(this.graph, this.phase);
return { ...stats, pruned, edgesRemoved: stats.edgesRemoved + pruned };
}
createIntelligentClusters(newNodeIds: string[], contextNodeId: string, config?: EngineConfig): { hyperedgesCreated: number; clusters: any[] } {
return this.hyperedgeManager.createIntelligentClusters(newNodeIds, contextNodeId, config);
}
detectCrossClusterPatterns(contextNodeId: string): any[] {
return this.hyperedgeManager.detectCrossClusterPatterns(contextNodeId);
}
/**
* Detect logical contradictions
*/
detectContradictions(activated: ActivatedNode[]): SecurityRuleResult[] {
// detectContradictions requires adjacency map (target, weight, type).
// AdjacencyIndex gives that.
// We need to verify if AdjacencyIndex preserves TYPE. Yes it does.
return detectContradictions(activated, this.graph, this.adjacencyIndex.get());
}
/**
* Get system metrics/telemetry
*/
calculateMetrics(lastLatency: number = 0, weightChanges: { delta: number }[] = [], activationDepths: number[] = []) {
return calculateMetrics(this.graph, lastLatency, weightChanges, activationDepths);
}
getStats(lastLatency: number = 0) {
return this.calculateMetrics(lastLatency);
}
}