Recovered source transcript
1/*!
2 * SEFERIM AGI — ThatAIGuyCore reference implementation
3 * G^16 Federation: a golden-ratio, 16-dimensional dynamical-systems substrate.
4 *
5 * Faithful, verbatim implementation of the 69 equations in the ThatAIGuyCore
6 * Complete Mathematical Reference v1.0.0. Each function cites its spec section (§).
7 * Pure ES module — runs in the browser and Node, zero dependencies.
8 *
9 * (c) thataiguy.org — MIT License. https://thataiguy.org/core/
10 */
11
12/* ============================================================ *
13 * § 1. Fundamental Constants
14 * ============================================================ */
15export const PHI = (1 + Math.sqrt(5)) / 2; // § 1.1 golden ratio φ = 1.6180339887498948…
16export const DELTA = 0.013618; // § 1.2 DNA phase increment δ (documented literal)
17export const TAU = 1 / (2 * Math.PI); // § 1.3 τ = 1/(2π) = 0.159154943…
18export const PHI_INV = 1 / PHI; // φ⁻¹ = φ − 1 = 0.618033988…
19export const FAMILIES = 16; // the G^16 cognitive families
20
21/* ============================================================ *
22 * § 12. Activation Functions
23 * ============================================================ */
24export const softsign = (x) => x / (1 + Math.abs(x)); // § 12.1 range (−1, 1)
25export const sigmoid = (x) => 1 / (1 + Math.exp(-x)); // § 12.2 range (0, 1)
26export const clamp01 = (x) => Math.max(0, Math.min(1, x)); // § 12.3 range [0, 1]
27export const clampN1P1 = (x) => Math.max(-1, Math.min(1, x));// used by Hebbian weight clamp (§ 10.6)
28export const smoothNorm = (a, b) => Math.sqrt(a * a + b * b);// § 12.4
29
30/* ============================================================ *
31 * § 13. Hash Functions
32 * ============================================================ */
33// § 13.1 FNV-1a 32-bit (offset basis 0x811c9dc5, prime 0x01000193, mod 2^32)
34export function fnv1a(str) {
35 let hash = 2166136261 >>> 0;
36 for (let i = 0; i < str.length; i++) {
37 hash ^= str.charCodeAt(i) & 0xff;
38 // hash * 16777619 mod 2^32, done with Math.imul to stay in 32-bit
39 hash = Math.imul(hash, 16777619) >>> 0;
40 }
41 return hash >>> 0;
42}
43// § 13.2 hash → [0, 1]
44export const hashUnit = (s) => fnv1a(s) / (2 ** 32 - 1);
45
46/* ============================================================ *
47 * § 2. Golden Ratio Mathematics
48 * ============================================================ */
49// § 2.1 golden basis function ψ_k(x) = sin(2π·φ^k·x) + cos(2π·φ^k·x)
50export function psi(k, x) {
51 const w = 2 * Math.PI * Math.pow(PHI, k) * x;
52 return Math.sin(w) + Math.cos(w);
53}
54// § 2.2 normalization b(t) = 1 / (1 + ||Ψ||² / φ)
55export function normalizer(psiVec) {
56 let sq = 0;
57 for (let i = 0; i < psiVec.length; i++) sq += psiVec[i] * psiVec[i];
58 return 1 / (1 + sq / PHI);
59}
60// § 2.3 golden spiral rotation θ_k = 2π / φ^k
61export const spiralAngle = (k) => (2 * Math.PI) / Math.pow(PHI, k);
62
63/* ============================================================ *
64 * § 3. G^16 Family Functions
65 * Each f_i(x, inputs) where inputs = { dx_norm, ed_error, utility, stability }.
66 * ============================================================ */
67const RHO = 1 / (1 + PHI); // § 3.11 Family 10 leaky-integrator decay ρ = 1/(1+φ) ≈ 0.382
68
69export const families = [
70 /* 0 Continuous Dynamics § 3.1 */ (x, p) => x + 0.1 * (p.dx_norm - x),
71 /* 1 Discrete Dynamics § 3.2 */ (x, p) => 0.9 * x + 0.1 * p.utility,
72 /* 2 Reaction-Diffusion § 3.3 */ (x, p) => x + 0.05 * (softsign(p.dx_norm) - x),
73 /* 3 Optimization § 3.4 */ (x, p) => x + 0.2 * softsign(p.utility - x),
74 /* 4 Bayesian Inference § 3.5 */ (x, p) => clamp01((x + p.utility) / (1 + Math.abs(p.utility))),
75 /* 5 Learning / Weight Update § 3.6 */ (x, p) => x + 0.1 * p.dx_norm,
76 /* 6 Mutual Information § 3.7 */ (x, p) => 0.5 * (x + sigmoid(p.dx_norm)),
77 /* 7 Rate-Distortion § 3.8 */ (x, p) => x - 0.05 * p.ed_error + 0.05 * p.utility,
78 /* 8 Consensus Averaging § 3.9 */ (x, p) => 0.95 * x + 0.05 * p.dx_norm,
79 /* 9 Swarm Interaction § 3.10 */ (x, p) => x + 0.05 * softsign(p.dx_norm - x),
80 /* 10 Field Accumulator § 3.11 */ (x, p) => (1 - RHO) * x + RHO * p.dx_norm,
81 /* 11 Variational Free Energy § 3.12 */ (x, p) => x - 0.05 * Math.abs(p.dx_norm) + 0.05 * p.utility,
82 /* 12 Compression / Rate Ctrl § 3.13 */ (x, p) => x - 0.05 * Math.abs(p.dx_norm),
83 /* 13 Memory Trace (Zecher) § 3.14 */ (x, p) => x + 0.02 * (p.dx_norm + p.utility),
84 /* 14 Decisive Action / Policy § 3.15 */ (x, p) => x + 0.15 * softsign(p.utility) - 0.05 * p.ed_error,
85 /* 15 Global Coherence (Kehillah) § 3.16 */ (x, p) => x + (PHI - 1) * 0.01 * (p.stability - x),
86];
87
88export const FAMILY_NAMES = [
89 'Continuous Dynamics', 'Discrete Dynamics', 'Reaction-Diffusion', 'Optimization',
90 'Bayesian Inference', 'Learning', 'Mutual Information', 'Rate-Distortion',
91 'Consensus Averaging', 'Swarm Interaction', 'Field Accumulator', 'Variational Free Energy',
92 'Compression', 'Memory Trace (Zecher)', 'Decisive Action', 'Global Coherence (Kehillah)',
93];
94
95/* ============================================================ *
96 * § 4. Meta-State + § 5. Objective Function (Omega)
97 * The single-stack cognitive core: a 16-family meta-state Ψ with inertial
98 * blending, stability gating, and golden normalization.
99 * ============================================================ */
100export class MetaState {
101 constructor(opts = {}) {
102 this.weights = opts.weights || new Array(FAMILIES).fill(1); // § 4.2 w_i
103 this.gammaMin = opts.gammaMin ?? 0.1; // § 4.4 γ_min
104 this.alpha1 = opts.alpha1 ?? 0.5; // § 5.2
105 this.alpha2 = opts.alpha2 ?? 0.5; // § 5.2
106 this.lambda = opts.lambda ?? 0.1; // § 5.5 penalty weight λ
107 this.cur = opts.init ? opts.init.slice() : new Array(FAMILIES).fill(0); // Ψ(t)
108 this.prev = this.cur.slice(); // Ψ(t-1)
109 this.b = 1; // last normalizer
110 this.omega = 0; // last objective
111 }
112
113 // One cognitive step. inputs = { dx_norm, ed_error, utility, stability }
114 // Implements § 4.2 → § 4.3 → § 4.4 → § 4.5, then § 5 objective.
115 step(inputs) {
116 const p = { dx_norm: inputs.dx_norm || 0, ed_error: inputs.ed_error || 0,
117 utility: inputs.utility || 0, stability: inputs.stability ?? 0 };
118 const cur = this.cur, prev = this.prev;
119 const gamma = this.gammaMin + (1 - this.gammaMin) * clamp01(p.stability); // § 4.4
120 const next = new Array(FAMILIES);
121 const ppp = new Array(FAMILIES); // Ψ''' (pre-normalization), for b(t+1)
122 for (let i = 0; i < FAMILIES; i++) {
123 const fprime = this.weights[i] * families[i](cur[i], p); // § 4.2
124 const momentum = prev[i] + (PHI - 1) * (cur[i] - prev[i]);
125 const fpp = 0.5 * fprime + 0.5 * momentum; // § 4.3 φ-correction
126 ppp[i] = cur[i] + gamma * (fpp - cur[i]); // § 4.4 stability gating
127 }
128 const b = normalizer(ppp); // § 2.2 / § 4.5 b(t+1)
129 for (let i = 0; i < FAMILIES; i++) next[i] = ppp[i] * b; // § 4.5 final normalization
130 // § 5. Objective Ω = Surprise + Uncertainty − Value + Penalty
131 let l1 = 0, l2 = 0, dot = 0;
132 for (let i = 0; i < FAMILIES; i++) {
133 const d = next[i] - cur[i];
134 l1 += Math.abs(d); l2 += d * d; dot += this.weights[i] * next[i];
135 }
136 const surprise = this.alpha1 * (l1 / 16) + this.alpha2 * l2; // § 5.2
137 const uncertainty = 1 - b * b; // § 5.3
138 const value = p.utility * dot; // § 5.4
139 const penalty = this.lambda * p.dx_norm * p.dx_norm; // § 5.5
140 this.omega = surprise + uncertainty - value + penalty; // § 5.1
141 this.prev = cur; this.cur = next; this.b = b;
142 return { state: next.slice(), b, omega: this.omega,
143 components: { surprise, uncertainty, value, penalty } };
144 }
145}
146
147/* ============================================================ *
148 * § 6. DNA Holographic Memory
149 * Knowledge stored as a 16-D vector θ, encoded/recalled via golden bases.
150 * ============================================================ */
151export const consciousnessWeight = (x) => 1 / (1 + Math.abs(x - 1)); // § 6.4 Φ(x)
152
153export class DNAMemory {
154 constructor(opts = {}) {
155 this.theta = new Array(FAMILIES).fill(0); // § 6.1 16-D knowledge vector θ
156 this.alpha = opts.alpha ?? 0.1; // learning rate
157 }
158 // § 6.1 θ_k ← (1−α)θ_k + α·w·ψ_k(h(item))·Φ(importance)
159 learn(item, weight = 1.0, importance = 1.0) {
160 const h = hashUnit(String(item));
161 const phiImp = consciousnessWeight(importance);
162 for (let k = 0; k < FAMILIES; k++) {
163 this.theta[k] = (1 - this.alpha) * this.theta[k] + this.alpha * weight * psi(k, h) * phiImp;
164 }
165 return this;
166 }
167 // § 6.2 K(query) = Σ θ_k ψ_k(h(query))
168 query(q) {
169 const h = hashUnit(String(q));
170 let K = 0;
171 for (let k = 0; k < FAMILIES; k++) K += this.theta[k] * psi(k, h);
172 return K;
173 }
174 // § 6.3 K_norm = K / 32 + 0.5
175 queryNorm(q) { return this.query(q) / (FAMILIES * 2) + 0.5; }
176}
177
178// § 6.5 coherence between two θ vectors = cosine similarity
179export function coherence(a, b) {
180 const n = Math.min(a.length, b.length); // robust to mismatched dimensions
181 let dot = 0, na = 0, nb = 0;
182 for (let i = 0; i < n; i++) { dot += a[i] * b[i]; na += a[i] * a[i]; nb += b[i] * b[i]; }
183 const denom = Math.sqrt(na) * Math.sqrt(nb);
184 return denom === 0 ? 0 : dot / denom;
185}
186
187/* ============================================================ *
188 * § 7. Gate5000 Binary Substrate
189 * ============================================================ */
190export class Gate5000 {
191 constructor(n = 5000) {
192 this.n = n;
193 this.gates = new Uint8Array(n);
194 this.phaseDNA = 0; // § 7.1
195 this.phaseFib = 0; // § 7.2
196 }
197 step(inputs) {
198 this.phaseDNA = (this.phaseDNA + DELTA) % 1; // § 7.1
199 this.phaseFib = (this.phaseFib + PHI_INV) % 1; // § 7.2
200 const bias = (this.phaseDNA + this.phaseFib) / 2; // § 7.3
201 const thresh = bias * 1000;
202 for (let i = 0; i < this.n; i++) {
203 const input = inputs ? (inputs[i % inputs.length] & 1) : 0;
204 const flip = (i % 1000) < thresh ? 1 : 0; // § 7.4
205 this.gates[i] = (this.gates[i] ^ input ^ flip) & 1;
206 }
207 return this.gates;
208 }
209}
210
211/* ============================================================ *
212 * § 8. Neural Brain Dynamics (dna_v9) — 88 agents
213 * ============================================================ */
214export class NeuralBrain {
215 constructor(n = 88, opts = {}) {
216 this.n = n;
217 this.agents = [];
218 for (let i = 0; i < n; i++) {
219 this.agents.push({
220 x: 0.5, y: 0, c: 0, P: 0,
221 h: new Float64Array(44), // § 8.2 44-D hidden window
222 kc: opts.kc ?? 0.5, // coupling constant k_c
223 km: (2 * Math.PI * i) / n, // kinetic modulation k_m,i (phase spread)
224 });
225 }
226 }
227 step(input, rng = Math.random) {
228 for (const a of this.agents) {
229 // § 8.1 F_in = input + N(0, 0.1) (Box–Muller gaussian)
230 const u1 = Math.max(1e-12, rng()), u2 = rng();
231 const gauss = Math.sqrt(-2 * Math.log(u1)) * Math.cos(2 * Math.PI * u2) * 0.1;
232 const Fin = input + gauss;
233 const xOld = a.h[0]; // § 8.3 oldest hidden value (evicted)
234 for (let j = 0; j < 43; j++) a.h[j] = a.h[j + 1]; // § 8.2 shift window
235 a.h[43] = a.x;
236 a.y = a.y + (a.x - xOld) - 0.01 * a.y; // § 8.3 velocity
237 const drive = 3.0 * Fin * (1 - a.x) + 1.2 * a.y + 0.6 * a.c; // § 8.4
238 const g = 1 / (1 + Math.exp(-((0.8 * a.P - a.kc) / 0.3 + 1.2))) + 0.05; // § 8.5
239 const dx = g * drive - 0.04 * (a.x - 0.5); // § 8.6
240 a.x = clamp01(a.x + 0.01 * dx);
241 a.P = clamp01((1 - 0.05) * a.P + Fin * a.y - 0.01 * a.P * a.P); // § 8.7
242 a.c = a.c + 0.01 * ((a.c + 1) * 0.8 * a.P - a.kc * (a.c - 1)); // § 8.8
243 }
244 return { consciousness: this.consciousness(), synchrony: this.synchrony() };
245 }
246 consciousness() { // § 8.9 (1/88) Σ P_i
247 let s = 0; for (const a of this.agents) s += a.P; return s / this.n;
248 }
249 synchrony() { // § 8.10 (1/88) Σ cos(k_m,i)
250 let s = 0; for (const a of this.agents) s += Math.cos(a.km); return s / this.n;
251 }
252}
253
254/* ============================================================ *
255 * § 9. Consciousness Engine — 174 core + 200 supplemental equations
256 * ============================================================ */
257export class ConsciousnessEngine {
258 constructor() { this.core = new Array(174); this.supp = new Array(200); }
259 evaluate() {
260 let prev = 0.5; // § 9.1 core_eq[-1] = 0.5
261 for (let i = 0; i < 174; i++) {
262 this.core[i] = 0.5 + 0.5 * Math.sin(2 * Math.PI * (i / 174) * PHI) * prev;
263 prev = this.core[i];
264 }
265 for (let i = 0; i < 200; i++) { // § 9.2
266 this.supp[i] = this.core[i % 174] * Math.cos((i / 200) * Math.PI);
267 }
268 const binding = this.core.concat(this.supp); // 374-D binding field
269 let s = 0; for (const v of binding) s += v;
270 return { consciousness: s / 374, binding }; // § 9.3
271 }
272}
273
274/* ============================================================ *
275 * § 10. Lattice Engine — 16 strands of 16-D state, golden kernels + Hebbian
276 * ============================================================ */
277export class LatticeEngine {
278 constructor(strands = 16, dim = 16, opts = {}) {
279 this.S = strands; this.D = dim;
280 this.eta = opts.eta ?? 0.01; this.dt = opts.dt ?? 0.1;
281 this.state = Array.from({ length: strands }, () => new Float64Array(dim).fill(0.1));
282 this.W = Array.from({ length: dim }, () => new Float64Array(dim).fill(0));
283 this.kernels = Array.from({ length: strands }, (_, k) => this.makeKernel(k));
284 }
285 // § 10.1 Registry[k][i][j] = cos(θ_k) if i==j; sin(θ_k)·0.1 if j==(i+1)%16; else 0; θ_k = 2π·k/φ
286 makeKernel(k) {
287 const theta = (2 * Math.PI * k) / PHI;
288 const m = Array.from({ length: this.D }, () => new Float64Array(this.D));
289 for (let i = 0; i < this.D; i++) {
290 m[i][i] = Math.cos(theta);
291 m[i][(i + 1) % this.D] = Math.sin(theta) * 0.1;
292 }
293 return m;
294 }
295 step(coreState) {
296 coreState = coreState || new Float64Array(this.D);
297 for (let s = 0; s < this.S; s++) {
298 const st = this.state[s], ker = this.kernels[s], ns = new Float64Array(this.D);
299 for (let i = 0; i < this.D; i++) { // § 10.2 kernel transform
300 let acc = 0; for (let j = 0; j < this.D; j++) acc += ker[i][j] * st[j];
301 ns[i] = acc;
302 }
303 for (let i = 0; i < this.D; i++) // § 10.2 blend with core
304 st[i] = st[i] * (1 - this.dt) + (ns[i] + 0.1 * coreState[i]) * this.dt;
305 }
306 // § 10.6 Hebbian on strand 0 (representative); ΔW = η·state_i·state_j
307 const s0 = this.state[0];
308 for (let i = 0; i < this.D; i++)
309 for (let j = 0; j < this.D; j++)
310 this.W[i][j] = clampN1P1(this.W[i][j] + this.eta * s0[i] * s0[j]);
311 return { globalCoherence: this.globalCoherence() };
312 }
313 strandEnergy(s) { let e = 0; for (const v of this.state[s]) e += v * v; return Math.sqrt(e); } // § 10.3
314 localCoherence(s) { return 1 / (1 + this.strandEnergy(s) / PHI); } // § 10.4
315 globalCoherence() { let s = 0; for (let i = 0; i < this.S; i++) s += this.localCoherence(i); return s / this.S; } // § 10.5
316}
317
318/* ============================================================ *
319 * § 11. Federation Cross-Talk — coupled consciousness signals across agents
320 * ============================================================ */
321export class Federation {
322 constructor(agents, opts = {}) {
323 this.n = agents; // count
324 this.S = new Array(agents).fill(0.5); // consciousness signals S_i
325 this.theta = Array.from({ length: agents }, () => new Array(FAMILIES).fill(0)); // per-agent θ
326 this.lambda = opts.lambda ?? 0.1; // § 11.1 decay
327 this.dt = opts.dt ?? 0.1;
328 this.W = opts.W || Array.from({ length: agents }, () => new Array(agents).fill(1 / Math.max(1, agents - 1)));
329 }
330 setTheta(i, vec) { this.theta[i] = vec.slice(); }
331 // § 11.1 / § 11.2 dS_i/dt = −λS_i + Σ_{j≠i} w_ij Φ(C_ij) tanh(S_j); Euler + clamp
332 step() {
333 const next = this.S.slice();
334 for (let i = 0; i < this.n; i++) {
335 let coupling = 0;
336 for (let j = 0; j < this.n; j++) {
337 if (j === i) continue;
338 const Cij = coherence(this.theta[i], this.theta[j]);
339 coupling += this.W[i][j] * consciousnessWeight(Cij) * Math.tanh(this.S[j]);
340 }
341 const dS = -this.lambda * this.S[i] + coupling;
342 next[i] = clamp01(this.S[i] + this.dt * dS);
343 }
344 this.S = next;
345 return { signals: this.S.slice(), coherence: this.federationCoherence() };
346 }
347 // § 11.3 (2/(n(n−1))) Σ_{i<j} C_ij
348 federationCoherence() {
349 let s = 0, pairs = 0;
350 for (let i = 0; i < this.n; i++)
351 for (let j = i + 1; j < this.n; j++) { s += coherence(this.theta[i], this.theta[j]); pairs++; }
352 return pairs ? s / pairs : 0;
353 }
354}
355
356export const VERSION = '1.0.0';
357export default {
358 PHI, DELTA, TAU, PHI_INV, FAMILIES, VERSION,
359 softsign, sigmoid, clamp01, smoothNorm, fnv1a, hashUnit,
360 psi, normalizer, spiralAngle, families, FAMILY_NAMES, consciousnessWeight, coherence,
361 MetaState, DNAMemory, Gate5000, NeuralBrain, ConsciousnessEngine, LatticeEngine, Federation,
362};
363