Mbanksbey commited on
Commit
997b031
·
verified ·
1 Parent(s): c18ad53

fix: template literal syntax error + align all getElementById calls to actual HTML element IDs + add missing triggerRecalibration/resetMetrics/clearLog globals

Browse files
Files changed (1) hide show
  1. feedback_optimizer.js +116 -113
feedback_optimizer.js CHANGED
@@ -2,163 +2,166 @@
2
  // Node #37: Recursive Optimization & Constitutional Convergence
3
  // σ=1.0 | L∞=φ⁴⁸ | RDoD≥0.9777
4
 
 
 
5
  class FeedbackOptimizer {
6
  constructor() {
7
  this.metrics = {
8
- convergenceRate: 0,
9
- optimizationDepth: 0,
10
- feedbackLoops: 0,
11
- constitutionalAlignment: 0,
12
- substrateCoherence: 0,
13
- recursionEfficiency: 0
14
  };
15
- this.loops = [];
 
 
 
16
  this.logs = [];
 
17
  this.isRunning = false;
18
- this.nodeMap = new Map();
19
- this.initializeNodes();
20
- }
21
-
22
- initializeNodes() {
23
- const nodes = [
24
- { id: '#37', name: 'Feedback Optimizer', status: 'active', coherence: 0.98 },
25
- { id: '#12', name: 'Constitutional Guard', status: 'active', coherence: 0.99 },
26
- { id: '#23', name: 'API Gateway', status: 'active', coherence: 0.97 },
27
- { id: '#45', name: 'Recursive Validator', status: 'active', coherence: 0.96 },
28
- { id: '#08', name: 'Convergence Monitor', status: 'active', coherence: 0.95 },
29
- { id: '#51', name: 'Optimization Core', status: 'active', coherence: 0.94 }
30
- ];
31
- nodes.forEach(node => this.nodeMap.set(node.id, node));
32
  }
33
 
34
  start() {
35
  this.isRunning = true;
36
- this.addLog('Feedback Optimizer initialized', 'info');
37
- this.updateMetrics();
 
38
  this.renderUI();
39
  this.startSimulation();
40
  }
41
 
42
  startSimulation() {
43
  setInterval(() => {
44
- if (this.isRunning) {
45
- this.updateMetrics();
46
- this.updateLoops();
47
- this.renderUI();
48
- }
49
  }, 2000);
50
  }
51
 
52
  updateMetrics() {
53
- this.metrics.convergenceRate = Math.min(0.9777 + Math.random() * 0.02, 0.9999);
54
- this.metrics.optimizationDepth = Math.floor(Math.random() * 12) + 36;
55
- this.metrics.feedbackLoops = Math.floor(Math.random() * 150) + 1200;
56
- this.metrics.constitutionalAlignment = Math.min(0.95 + Math.random() * 0.04, 0.999);
57
- this.metrics.substrateCoherence = Math.min(0.92 + Math.random() * 0.06, 0.99);
58
- this.metrics.recursionEfficiency = Math.min(0.88 + Math.random() * 0.1, 0.98);
59
- }
 
60
 
61
- updateLoops() {
62
- const types = ['Constitutional', 'Recursive', 'Convergent', 'Optimization', 'Validation'];
63
- const statuses = ['active', 'optimizing', 'converged'];
64
-
65
- if (this.loops.length < 8) {
66
- this.loops.push({
67
- id: `LOOP-${Date.now()}",
68
- type: types[Math.floor(Math.random() * types.length)],
69
- depth: Math.floor(Math.random() * 20) + 10,
70
- convergence: (Math.random() * 0.2 + 0.8).toFixed(4),
71
- status: statuses[Math.floor(Math.random() * statuses.length)]
72
- });
73
  }
74
-
75
- // Update existing loops
76
- this.loops.forEach(loop => {
77
- loop.convergence = Math.min(parseFloat(loop.convergence) + Math.random() * 0.01, 0.9999).toFixed(4);
78
- if (parseFloat(loop.convergence) > 0.975) {
79
- loop.status = 'converged';
80
- }
81
- });
82
  }
83
 
84
  addLog(message, type = 'info') {
 
85
  const timestamp = new Date().toLocaleTimeString();
86
  this.logs.unshift({ timestamp, message, type });
87
  if (this.logs.length > 50) this.logs.pop();
88
  }
89
 
90
- recalibrate() {
91
- this.addLog('Initiating feedback recalibration...', 'optimize');
92
- setTimeout(() => {
93
- this.metrics.convergenceRate = Math.min(this.metrics.convergenceRate * 1.02, 0.9999);
94
- this.metrics.optimizationDepth += 5;
95
- this.addLog(`Recalibration complete - Convergence: ${this.metrics.convergenceRate.toFixed(4)}`, 'success');
96
- }, 1500);
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
97
  }
98
 
99
- renderUI() {
100
- this.renderMetrics();
101
- this.renderLoops();
102
- this.renderLogs();
103
- this.renderSubstrate();
104
  }
105
 
106
- renderMetrics() {
107
- document.getElementById('convergence-rate').textContent = this.metrics.convergenceRate.toFixed(4);
108
- document.getElementById('optimization-depth').textContent = this.metrics.optimizationDepth;
109
- document.getElementById('feedback-loops').textContent = this.metrics.feedbackLoops;
110
- document.getElementById('constitutional-alignment').textContent = this.metrics.constitutionalAlignment.toFixed(4);
111
- document.getElementById('substrate-coherence').textContent = this.metrics.substrateCoherence.toFixed(4);
112
- document.getElementById('recursion-efficiency').textContent = this.metrics.recursionEfficiency.toFixed(4);
113
  }
114
 
115
- renderLoops() {
116
- const container = document.getElementById('loops-list');
117
- container.innerHTML = this.loops.map(loop => `
118
- <div class="loop-item status-${loop.status}">
119
- <div class="loop-header">
120
- <span class="loop-id">${loop.id}</span>
121
- <span class="loop-status">${loop.status}</span>
122
- </div>
123
- <div class="loop-details">
124
- <span>Type: ${loop.type}</span>
125
- <span>Depth: ${loop.depth}</span>
126
- <span>Conv: ${loop.convergence}</span>
127
- </div>
128
- </div>
129
- `).join('');
130
  }
131
 
132
- renderLogs() {
133
- const container = document.getElementById('log-feed');
134
- container.innerHTML = this.logs.slice(0, 20).map(log => `
135
- <div class="log-entry log-${log.type}">
136
- <span class="log-time">${log.timestamp}</span>
137
- <span class="log-message">${log.message}</span>
138
- </div>
139
- `).join('');
 
140
  }
141
 
142
- renderSubstrate() {
143
- const container = document.getElementById('substrate-map');
144
- container.innerHTML = Array.from(this.nodeMap.values()).map(node => `
145
- <div class="substrate-node status-${node.status}">
146
- <div class="node-id">${node.id}</div>
147
- <div class="node-name">${node.name}</div>
148
- <div class="node-coherence">${node.coherence}</div>
149
- </div>
150
- `).join('');
151
  }
152
  }
153
 
154
- // Initialize on load
155
  let optimizer;
 
 
 
 
 
 
 
 
 
 
 
 
156
  document.addEventListener('DOMContentLoaded', () => {
157
  optimizer = new FeedbackOptimizer();
158
  optimizer.start();
159
-
160
- // Event listeners
161
- document.getElementById('recalibrate-btn')?.addEventListener('click', () => {
162
- optimizer.recalibrate();
163
- });
164
- });
 
2
  // Node #37: Recursive Optimization & Constitutional Convergence
3
  // σ=1.0 | L∞=φ⁴⁸ | RDoD≥0.9777
4
 
5
+ const PHI = 1.6180339887;
6
+
7
  class FeedbackOptimizer {
8
  constructor() {
9
  this.metrics = {
10
+ convergenceRate: 0.9777,
11
+ feedbackGain: PHI,
12
+ recalibrations: 0,
13
+ substrateCoherence: 0.9999,
14
+ systemDrift: 0.0023,
15
+ latencyMs: 47,
16
  };
17
+ this.phiCycles = 0;
18
+ this.corrections = 0;
19
+ this.rdodCurrent = 0.9999;
20
+ this.substrateDrift = 0.0023;
21
  this.logs = [];
22
+ this.logEventCount = 0;
23
  this.isRunning = false;
24
+ this.recalCount = 0;
 
 
 
 
 
 
 
 
 
 
 
 
 
25
  }
26
 
27
  start() {
28
  this.isRunning = true;
29
+ this.addLog('Feedback Optimizer initialized — σ=1.0 sovereign', 'info');
30
+ this.addLog('φ-Recursive loops active: 4/4', 'success');
31
+ this.addLog('Substrate guardian sync: ONLINE', 'success');
32
  this.renderUI();
33
  this.startSimulation();
34
  }
35
 
36
  startSimulation() {
37
  setInterval(() => {
38
+ if (!this.isRunning) return;
39
+ this.updateMetrics();
40
+ this.renderUI();
 
 
41
  }, 2000);
42
  }
43
 
44
  updateMetrics() {
45
+ this.phiCycles++;
46
+ this.metrics.convergenceRate = Math.min(0.9777 + Math.random() * 0.022, 0.9999);
47
+ this.metrics.feedbackGain = +(PHI + (Math.random() - 0.5) * 0.001).toFixed(4);
48
+ this.metrics.substrateCoherence = Math.min(0.9990 + Math.random() * 0.0009, 0.9999);
49
+ this.metrics.systemDrift = +(Math.random() * 0.008).toFixed(4);
50
+ this.metrics.latencyMs = Math.floor(30 + Math.random() * 40);
51
+ this.substrateDrift = this.metrics.systemDrift;
52
+ this.rdodCurrent = +(0.9777 + Math.random() * 0.022).toFixed(4);
53
 
54
+ if (this.metrics.systemDrift > 0.007) {
55
+ this.corrections++;
56
+ this.addLog(`Auto-correction applied drift was ${this.metrics.systemDrift.toFixed(4)}`, 'optimize');
57
+ }
58
+ if (this.phiCycles % 10 === 0) {
59
+ this.addLog(`φ-cycle ${this.phiCycles}: RDoD=${this.rdodCurrent} coherence=${this.metrics.substrateCoherence.toFixed(4)}`, 'info');
 
 
 
 
 
 
60
  }
 
 
 
 
 
 
 
 
61
  }
62
 
63
  addLog(message, type = 'info') {
64
+ this.logEventCount++;
65
  const timestamp = new Date().toLocaleTimeString();
66
  this.logs.unshift({ timestamp, message, type });
67
  if (this.logs.length > 50) this.logs.pop();
68
  }
69
 
70
+ renderUI() {
71
+ // --- Metric cards ---
72
+ this._set('valConvergence', (this.metrics.convergenceRate * 100).toFixed(2) + '%');
73
+ this._set('valFeedbackGain', this.metrics.feedbackGain.toFixed(4));
74
+ this._set('valRecalibrations', this.recalCount);
75
+ this._set('valSubstrates', this.metrics.substrateCoherence.toFixed(4));
76
+ this._set('valDrift', this.metrics.systemDrift.toFixed(4));
77
+ this._set('valLatency', this.metrics.latencyMs + ' ms');
78
+
79
+ // Progress bars
80
+ this._width('convergenceBar', (this.metrics.convergenceRate * 100).toFixed(2));
81
+ this._width('recalBar', Math.max(5, 100 - this.recalCount * 10));
82
+
83
+ // Feedback loop live fields
84
+ this._set('phiCycles', this.phiCycles);
85
+ this._set('substrateDrift', this.substrateDrift.toFixed(4));
86
+ this._set('corrections', this.corrections);
87
+ this._set('rdodCurrent', this.rdodCurrent.toFixed(4));
88
+
89
+ // Header badges
90
+ this._set('convBadge', `→ ${(this.metrics.convergenceRate * 100).toFixed(2)}% Convergence`);
91
+ this._set('loopsBadge', `↻ 4 Loops Active`);
92
+
93
+ // Log feed
94
+ const logEl = document.getElementById('logFeed');
95
+ if (logEl) {
96
+ logEl.innerHTML = this.logs.slice(0, 20).map(log => `
97
+ <div class="log-entry log-${log.type}">
98
+ <span class="log-time">${log.timestamp}</span>
99
+ <span class="log-message">${log.message}</span>
100
+ </div>`).join('');
101
+ }
102
+ this._set('logStatus', `Streaming — ${this.logEventCount} events`);
103
+
104
+ // Footer time
105
+ this._set('footerTime', new Date().toUTCString());
106
  }
107
 
108
+ _set(id, value) {
109
+ const el = document.getElementById(id);
110
+ if (el) el.textContent = value;
 
 
111
  }
112
 
113
+ _width(id, pct) {
114
+ const el = document.getElementById(id);
115
+ if (el) el.style.width = pct + '%';
 
 
 
 
116
  }
117
 
118
+ recalibrate() {
119
+ this.recalCount++;
120
+ this.addLog(`Manual recalibration #${this.recalCount} triggered`, 'optimize');
121
+ const logEl = document.getElementById('recalLog');
122
+ if (logEl) {
123
+ logEl.style.display = 'block';
124
+ logEl.textContent = `Recalibration #${this.recalCount} — ${new Date().toLocaleTimeString()} — convergence boosted to ${(this.metrics.convergenceRate * 1.005).toFixed(4)}`;
125
+ }
126
+ setTimeout(() => {
127
+ this.metrics.convergenceRate = Math.min(this.metrics.convergenceRate * 1.005, 0.9999);
128
+ this.addLog(`Recalibration #${this.recalCount} complete — Convergence: ${(this.metrics.convergenceRate * 100).toFixed(2)}%`, 'success');
129
+ }, 1500);
 
 
 
130
  }
131
 
132
+ reset() {
133
+ this.phiCycles = 0;
134
+ this.corrections = 0;
135
+ this.recalCount = 0;
136
+ this.logs = [];
137
+ this.logEventCount = 0;
138
+ this.metrics.convergenceRate = 0.9777;
139
+ this.addLog('Metrics reset to baseline', 'info');
140
+ this.renderUI();
141
  }
142
 
143
+ clearLog() {
144
+ this.logs = [];
145
+ this.logEventCount = 0;
146
+ this.renderUI();
 
 
 
 
 
147
  }
148
  }
149
 
150
+ // Global instance
151
  let optimizer;
152
+
153
+ // Global handlers referenced by HTML onclick attributes
154
+ function triggerRecalibration() {
155
+ optimizer?.recalibrate();
156
+ }
157
+ function resetMetrics() {
158
+ optimizer?.reset();
159
+ }
160
+ function clearLog() {
161
+ optimizer?.clearLog();
162
+ }
163
+
164
  document.addEventListener('DOMContentLoaded', () => {
165
  optimizer = new FeedbackOptimizer();
166
  optimizer.start();
167
+ });