gemma_glm / gemma_glm.py
DigitalEuan's picture
Upload 5 files
9fdc101 verified
Raw
History Blame Contribute Delete
90 kB
#!/usr/bin/env python3
"""
Gemma-GLM β€” Full Geometric Language Machine
=============================================
The complete LLM-GLM hybrid system in a single file.
Combines probabilistic LLM fluency with deterministic geometric verification.
Zero trainable parameters. The intelligence is in the mathematics.
Components:
Β§1 UBP Substrate (Golay [24,12,8], Leech Ξ›β‚‚β‚„, exact constants)
Β§2 Vector Engine (24-bit substrate, SVD vocab, NRCI)
Β§3 CRG (115+ concept knowledge graph, self-growing)
Β§4 Math Engine (exact rational arithmetic, physics formulas)
Β§5 Script Engine (Python sandbox, AST analysis, validation)
Β§6 Vision Pipeline (MOG patches, visual NRCI, dual resonance)
Β§7 Resonance & Veto (multi-signal scoring, CRG-first constraints)
Β§8 LogitsProcessor (drop-in for HuggingFace models)
Β§9 Pipeline (System 1/2 loop)
Β§10 Speculative Decoding (GLM draft β†’ LLM verify)
Β§11 KV Pruning (CRG-based attention cache)
Β§12 ValueGeometry (self-assembling integer geometry)
Β§13 Agent (interactive REPL)
Author: E.R.A. Craig (DigitalEuan) + LLM-GLM integration
Repository: https://github.com/DigitalEuan/UBP_Repo
License: Complete terms in LICENSE.txt
Usage:
python3 gemma_glm.py # Interactive REPL
python3 gemma_glm.py --test # Full self-test
python3 gemma_glm.py --api http://localhost:8080 # With LLM
python3 gemma_glm.py --profile photon # ValueGeometry profile
python3 gemma_glm.py --math "169/0.8176" # Exact math
python3 gemma_glm.py --code "print(42)" # Sandbox execution
Requirements: Python β‰₯ 3.10, stdlib only (no pip installs for core)
Optional: torch, transformers (for HuggingFace model integration)
"""
from __future__ import annotations
__version__ = "1.0.0"
__author__ = "E.R.A. Craig (DigitalEuan)"
import sys, os, re, json, math, time, hashlib, io, signal, ast, random, csv
from fractions import Fraction as F
from collections import Counter, defaultdict, OrderedDict
from dataclasses import dataclass, field
from typing import List, Dict, Tuple, Optional, Any, Set
from contextlib import contextmanager
# ════════════════════════════════════════════════════════════════════════════════
# Β§1 UBP SUBSTRATE β€” Exact Constants & Engines
# ════════════════════════════════════════════════════════════════════════════════
# Ο€ via 58-term continued fraction (OEIS A001203), good to ~80 decimal digits
_PI_CF = [3,7,15,1,292,1,1,1,2,1,3,1,14,2,1,1,2,2,2,2,1,84,2,1,1,15,3,13,1,4,
2,6,6,99,1,2,2,6,3,5,1,1,6,8,1,7,1,6,1,99,7,4,1,3,3,1,4,1]
def _compute_pi(terms=50):
c = _PI_CF[:min(terms, len(_PI_CF))]
if not c: return F(3)
x = F(c[-1])
for coeff in c[-2::-1]:
x = F(coeff) + F(1) / x
return x
# Exact constants (all Fraction)
_PI = _compute_pi(50)
_PHI = F(1618033988749895, 10**15)
_E = F(2718281828459045, 10**15)
_MONAD = _PI * _PHI * _E
_WOBBLE = _MONAD - int(_MONAD)
_L = _WOBBLE / 13
_Y_INV = _PI + F(2) / _PI
_Y = F(1) / _Y_INV
_SIGMA = F(29, 24)
_L_S = _L * _SIGMA
_U_E = F(24**3)
# Float convenience
PI, Y, Y_INV = float(_PI), float(_Y), float(_Y_INV)
PHI_f, E_f = float(_PHI), float(_E)
WOBBLE, L_f, L_S = float(_WOBBLE), float(_L), float(_L_S)
U_E = 13824
LY = L_f * Y
SHEAR_1 = 1 + 3 * LY
SHEAR_2 = 1 + 3 * LY + 12 * LY**2
# ════════════════════════════════════════════════════════════════════════════════
# Β§1.1 GOLAY [24,12,8] ENGINE β€” Full syndrome table
# ════════════════════════════════════════════════════════════════════════════════
class GolayCodeEngine:
"""Extended binary Golay [24, 12, 8] code with full syndrome decoding."""
B = [
[0,1,1,1,1,1,1,1,1,1,1,1],[1,1,1,0,1,1,1,0,0,0,1,0],
[1,1,0,1,1,1,0,0,0,1,0,1],[1,0,1,1,1,0,0,0,1,0,1,1],
[1,1,1,1,0,0,0,1,0,1,1,0],[1,1,1,0,0,0,1,0,1,1,0,1],
[1,1,0,0,0,1,0,1,1,0,1,1],[1,0,0,0,1,0,1,1,0,1,1,1],
[1,0,0,1,0,1,1,0,1,1,1,0],[1,0,1,0,1,1,0,1,1,1,0,0],
[1,1,0,1,1,0,1,1,1,0,0,0],[1,0,1,1,0,1,1,1,0,0,0,1],
]
def __init__(self):
self.G = [[1 if i==j else 0 for j in range(12)] + self.B[i] for i in range(12)]
self.H = [[self.B[j][i] for j in range(12)] + [1 if i==j else 0 for j in range(12)]
for i in range(12)]
self._H_cols = [tuple(self.H[j][k] for j in range(12)) for k in range(24)]
self._syndrome_table = None
self._codewords = None
self._octads = None
def encode(self, msg12):
if len(msg12) != 12: raise ValueError("need 12 bits")
cw = list(msg12)
for j in range(12):
p = 0
for i in range(12): p ^= msg12[i] & self.B[j][i]
cw.append(p)
return cw
def syndrome(self, v24):
s = [0]*12
for k, bit in enumerate(v24):
if bit:
col = self._H_cols[k]
for j in range(12): s[j] ^= col[j]
return s
def syndrome_weight(self, v24):
return sum(self.syndrome(v24))
def snap_to_codeword(self, v24):
if len(v24) != 24: raise ValueError("need 24 bits")
s = self.syndrome(v24)
sw = sum(s)
if sw == 0:
return list(v24), {"syndrome_weight":0,"corrected":False,"anchor_distance":0,"correctable":True}
self._ensure_syndrome_table()
st = tuple(s)
if st in self._syndrome_table:
e = self._syndrome_table[st]
corrected = [v24[i] ^ e[i] for i in range(24)]
return corrected, {"syndrome_weight":sw,"corrected":True,
"anchor_distance":sum(e),"correctable":True}
return list(v24), {"syndrome_weight":sw,"corrected":False,"anchor_distance":-1,"correctable":False}
def _ensure_syndrome_table(self):
if self._syndrome_table is not None: return
cols = self._H_cols
table = {tuple([0]*12): [0]*24}
# Weight-1 errors
for i in range(24):
e = [0]*24; e[i] = 1
table[cols[i]] = e
# Weight-2 errors
for i in range(24):
for j in range(i+1, 24):
s = tuple(a^b for a,b in zip(cols[i], cols[j]))
e = [0]*24; e[i]=1; e[j]=1
table[s] = e
# Weight-3 errors
for i in range(24):
for j in range(i+1, 24):
sij = tuple(a^b for a,b in zip(cols[i], cols[j]))
for k in range(j+1, 24):
s = tuple(a^b for a,b in zip(sij, cols[k]))
e = [0]*24; e[i]=1; e[j]=1; e[k]=1
table[s] = e
self._syndrome_table = table
def get_all_codewords(self):
if self._codewords is None:
self._codewords = []
for i in range(4096):
msg = [(i>>k)&1 for k in range(12)]
self._codewords.append(self.encode(msg))
return self._codewords
def get_octads(self):
if self._octads is None:
self._octads = [c for c in self.get_all_codewords() if sum(c)==8]
return self._octads
def decode(self, v24):
cw, meta = self.snap_to_codeword(v24)
return cw[:12], meta["correctable"], meta["anchor_distance"]
GOLAY = GolayCodeEngine()
# ════════════════════════════════════════════════════════════════════════════════
# Β§1.2 LEECH LATTICE Ξ›β‚‚β‚„ β€” Exact NRCI
# ════════════════════════════════════════════════════════════════════════════════
class LeechLatticeEngine:
"""Leech lattice Ξ›β‚‚β‚„ engine. Exact Fraction arithmetic."""
DIM = 24
SCALE = 8
KISSING = 196560
def __init__(self, golay):
self.golay = golay
self.Y = _Y
self.Y_INV = _Y_INV
def calculate_symmetry_tax(self, point):
hw = sum(1 for x in point if x)
ns = sum(x*x for x in point)
return F(hw) * self.Y + F(ns, 8)
def calculate_nrci(self, point):
tax = self.calculate_symmetry_tax(point)
return F(10) / (F(10) + tax)
def ontological_health(self, point):
return {
"Reality": F(sum(abs(c) for c in point[0:6]), 12),
"Info": F(sum(abs(c) for c in point[6:12]), 12),
"Activation": F(sum(abs(c) for c in point[12:18]), 12),
"Potential": F(sum(abs(c) for c in point[18:24]), 12),
}
def rank_by_stability(self, points):
return sorted([(p, self.calculate_symmetry_tax(p)) for p in points], key=lambda x: x[1])
LEECH = LeechLatticeEngine(GOLAY)
# ════════════════════════════════════════════════════════════════════════════════
# Β§2 VECTOR ENGINE β€” 24-bit substrate operations
# ════════════════════════════════════════════════════════════════════════════════
def word_to_hash24(word):
h = hashlib.sha256(word.lower().strip().encode()).digest()
return [(h[i//8]>>(7-i%8))&1 for i in range(24)]
def snap(vec):
return GOLAY.snap_to_codeword(list(vec))
def hamming(a, b):
return sum(x^y for x,y in zip(a,b))
def vector_to_hex(vec):
return sum((1<<(23-i)) for i in range(24) if vec[i])
def classify_vec(vec):
hw = sum(vec)
n = float(LEECH.calculate_nrci(vec))
if hw==0: lat="Identity"
elif hw==8: lat="Octad"
elif hw==12: lat="Dodecad"
elif hw==16: lat="Hexadecad"
elif hw==24: lat="Full"
else: lat=f"HW-{hw}"
return {"hw":hw,"nrci":n,"lattice":lat,"hex":f"0x{vector_to_hex(vec):06X}",
"in_band":n>=0.70}
def mog_quadrants(vec):
return [sum(vec[i:i+6]) for i in range(0,24,6)]
def mog_dominant_layer(vec):
layers = ["Reality","Information","Activation","Potential"]
q = mog_quadrants(vec)
idx = q.index(max(q))
return layers[idx], q[idx]
# ════════════════════════════════════════════════════════════════════════════════
# Β§2.1 SVD VOCABULARY BUILDER
# ════════════════════════════════════════════════════════════════════════════════
class SVDVocabulary:
"""Builds distributional 24-bit vectors from corpus using PPMI + SVD."""
def __init__(self):
self.word_vectors = {}
self.word_snapped = {}
self.word_meta = {}
def build_from_definitions(self, definitions, context_size=100, window=8):
try:
import numpy as np
except ImportError:
return self._build_hash_fallback(definitions)
tokens = []
for defn in definitions.values():
tokens.extend(re.findall(r"[a-z]+", defn.lower()))
tokens = [t for t in tokens if len(t)>=3]
target_words = sorted(definitions.keys())
vocab_idx = {w:i for i,w in enumerate(target_words)}
freq = Counter(tokens)
context_words = [w for w,_ in freq.most_common(context_size+len(target_words))
if w not in vocab_idx][:context_size]
ctx_idx = {w:i for i,w in enumerate(context_words)}
cooc = np.zeros((len(target_words), len(context_words)))
all_tokens = re.findall(r"[a-z]+", " ".join(definitions.values()).lower())
for i, tok in enumerate(all_tokens):
if tok not in vocab_idx: continue
wi = vocab_idx[tok]
for j in range(max(0,i-window), min(len(all_tokens),i+window+1)):
if j==i: continue
ctx = all_tokens[j]
if ctx in ctx_idx: cooc[wi, ctx_idx[ctx]] += 1
total = cooc.sum()
if total==0: return self._build_hash_fallback(definitions)
row_sums = cooc.sum(axis=1, keepdims=True)
col_sums = cooc.sum(axis=0, keepdims=True)
row_sums[row_sums==0] = 1
col_sums[col_sums==0] = 1
ppmi = np.log2((cooc*total)/(row_sums*col_sums)+1e-10)
ppmi[ppmi<0] = 0
U, S, Vt = np.linalg.svd(ppmi, full_matrices=False)
svd_vecs = U[:, :24] * S[:24]
medians = np.median(svd_vecs, axis=0)
bit_vecs = (svd_vecs > medians).astype(int)
for i, word in enumerate(target_words):
raw = [int(b) for b in bit_vecs[i]]
self.word_vectors[word] = raw
snapped, meta = GOLAY.snap_to_codeword(raw)
self.word_snapped[word] = snapped
self.word_meta[word] = {"raw_hw":sum(raw),"snapped_hw":sum(snapped),
"nrci":float(LEECH.calculate_nrci(snapped)),
"lattice":classify_vec(snapped)["lattice"],
"method":"svd"}
return len(self.word_snapped)
def _build_hash_fallback(self, definitions):
for word in definitions:
raw = word_to_hash24(word)
snapped, _ = snap(raw)
self.word_vectors[word] = raw
self.word_snapped[word] = snapped
self.word_meta[word] = {"raw_hw":sum(raw),"snapped_hw":sum(snapped),
"nrci":float(LEECH.calculate_nrci(snapped)),
"lattice":classify_vec(snapped)["lattice"],
"method":"hash"}
return len(self.word_snapped)
def get_vector(self, word):
w = word.lower().strip()
if w in self.word_snapped: return self.word_snapped[w]
raw = word_to_hash24(w)
snapped, _ = snap(raw)
return snapped
def get_meta(self, word):
w = word.lower().strip()
if w in self.word_meta: return self.word_meta[w]
vec = self.get_vector(w)
return {"nrci":float(LEECH.calculate_nrci(vec)),"lattice":classify_vec(vec)["lattice"],"method":"hash"}
def save(self, path):
with open(path,"w") as f:
json.dump({"word_snapped":self.word_snapped,"word_meta":self.word_meta},f)
def load(self, path):
if not os.path.exists(path): return False
with open(path) as f: data=json.load(f)
self.word_snapped = {k:list(v) for k,v in data["word_snapped"].items()}
self.word_meta = data.get("word_meta",{})
return True
# ════════════════════════════════════════════════════════════════════════════════
# Β§2.2 IDEA ZONE β€” Running context centroid
# ════════════════════════════════════════════════════════════════════════════════
class IdeaZone:
"""Maintains a running EMA centroid for the current topic."""
def __init__(self, alpha=0.3):
self.alpha = alpha
self.centroid = [0.0]*24
self.words = []
self._snapped = None
def update(self, word, vocab):
vec = vocab.get_vector(word)
self.centroid = [self.alpha*v + (1-self.alpha)*c for v,c in zip(vec, self.centroid)]
self.words.append(word.lower())
self._snapped = None
def get_centroid(self):
if self._snapped is None:
bits = [1 if c>0.5 else 0 for c in self.centroid]
self._snapped, _ = snap(bits)
return self._snapped
def get_centroid_nrci(self):
return float(LEECH.calculate_nrci(self.get_centroid()))
def reset(self):
self.centroid = [0.0]*24
self.words = []
self._snapped = None
# ════════════════════════════════════════════════════════════════════════════════
# Β§3 CRG β€” Concept Relation Graph (115+ concepts, self-growing)
# ════════════════════════════════════════════════════════════════════════════════
STATIC_CRG = {
# Physics particles
"photon":{"is_a":["boson","particle"],"related":["energy","light","wave","radiation","quantum","frequency"]},
"electron":{"is_a":["fermion","lepton","particle"],"related":["charge","mass","spin","orbital","energy","atom"]},
"quark":{"is_a":["fermion","particle"],"related":["proton","neutron","strong","color","gluon","hadron"]},
"neutron":{"is_a":["baryon","particle"],"related":["quark","nucleus","mass","proton","decay"]},
"proton":{"is_a":["baryon","particle"],"related":["quark","charge","nucleus","neutron","hydrogen"]},
"boson":{"is_a":["particle"],"related":["force","spin","carrier","photon","gluon","higgs","w_boson","z_boson"]},
"fermion":{"is_a":["particle"],"related":["spin","exclusion","electron","quark","matter"]},
"lepton":{"is_a":["fermion","particle"],"related":["electron","muon","tau","neutrino"]},
"muon":{"is_a":["lepton","fermion"],"related":["electron","mass","decay","anomaly"]},
"gluon":{"is_a":["boson"],"related":["strong","quark","color","confinement","fusion"]},
"higgs":{"is_a":["boson","scalar"],"related":["mass","field","symmetry","mechanism","vacuum"]},
"neutrino":{"is_a":["lepton"],"related":["weak","oscillation","mass","detection"]},
"tau":{"is_a":["lepton"],"related":["electron","muon","mass","decay"]},
# Physics concepts
"energy":{"is_a":["quantity","conserved"],"related":["mass","work","photon","frequency","wave","light","kinetic","potential"]},
"force":{"is_a":["interaction"],"related":["boson","carrier","acceleration","gravity","electromagnetic","strong","weak"]},
"mass":{"is_a":["property"],"related":["energy","higgs","gravity","particle","electron","inertia"]},
"charge":{"is_a":["property"],"related":["electromagnetic","electron","force","color","conservation"]},
"wave":{"is_a":["phenomenon"],"related":["photon","light","frequency","interference","energy","wavelength"]},
"field":{"is_a":["concept"],"related":["higgs","electromagnetic","energy","space","quantum","vacuum"]},
"spin":{"is_a":["property"],"related":["angular","fermion","boson","magnetic","electron","statistics"]},
"light":{"is_a":["electromagnetic","radiation","wave"],"related":["photon","wave","speed","energy","spectrum"]},
"space":{"is_a":["dimension"],"related":["time","spacetime","curvature","field","vacuum","expansion"]},
"time":{"is_a":["dimension"],"related":["space","spacetime","dilation","arrow","entropy"]},
"gravity":{"is_a":["force","interaction"],"related":["mass","spacetime","curvature","einstein","wave","black_hole"]},
"entropy":{"is_a":["quantity"],"related":["disorder","temperature","information","arrow","time","thermodynamics"]},
"symmetry":{"is_a":["concept"],"related":["group","gauge","breaking","invariance","higgs","conservation"]},
"quantum":{"is_a":["concept"],"related":["photon","wave","field","mechanics","coherence","entanglement","superposition"]},
"relativity":{"is_a":["theory"],"related":["einstein","spacetime","gravity","mass","energy","speed"]},
"spacetime":{"is_a":["concept"],"related":["space","time","gravity","curvature","einstein","relativity"]},
"thermodynamics":{"is_a":["theory"],"related":["energy","entropy","temperature","heat","work","laws"]},
# Mathematics
"lattice":{"is_a":["structure"],"related":["periodic","gauge","golay","leech","symmetry","crystal"]},
"tensor":{"is_a":["mathematical"],"related":["spacetime","curvature","metric","vector","index"]},
"vector":{"is_a":["mathematical"],"related":["direction","magnitude","space","tensor","basis"]},
"matrix":{"is_a":["mathematical"],"related":["linear","operator","quantum","tensor","determinant"]},
"group":{"is_a":["mathematical","structure"],"related":["symmetry","gauge","algebra","representation"]},
"topology":{"is_a":["mathematical"],"related":["invariant","phase","defect","lattice","betti"]},
"algebra":{"is_a":["mathematical"],"related":["group","ring","field","equation","structure"]},
"calculus":{"is_a":["mathematical"],"related":["derivative","integral","limit","continuous","analysis"]},
"geometry":{"is_a":["mathematical"],"related":["space","distance","angle","shape","curvature"]},
"number":{"is_a":["mathematical"],"related":["prime","integer","rational","real","complex","quantity"]},
"prime":{"is_a":["number","mathematical"],"related":["factor","divisible","fundamental","distribution"]},
"equation":{"is_a":["mathematical"],"related":["solve","variable","expression","balance","function"]},
"function":{"is_a":["mathematical"],"related":["mapping","domain","range","continuous","derivative"]},
"probability":{"is_a":["mathematical","quantity"],"related":["random","distribution","expected","event","sample"]},
"statistics":{"is_a":["mathematical"],"related":["data","mean","variance","distribution","sample"]},
"infinity":{"is_a":["mathematical","concept"],"related":["limit","series","uncountable","continuous"]},
"pi":{"is_a":["constant","mathematical"],"related":["circle","ratio","circumference","irrational"]},
"zero":{"is_a":["number","mathematical"],"related":["identity","addition","nothing","origin"]},
# Script/writing
"character":{"is_a":["entity"],"related":["motivation","arc","dialogue","conflict","development"]},
"dialogue":{"is_a":["element"],"related":["character","subtext","voice","conflict","revelation"]},
"plot":{"is_a":["structure"],"related":["conflict","resolution","arc","tension","story"]},
"conflict":{"is_a":["element"],"related":["protagonist","antagonist","stakes","tension","resolution"]},
"protagonist":{"is_a":["character"],"related":["arc","goal","conflict","transformation","agency"]},
"antagonist":{"is_a":["character"],"related":["opposition","conflict","stakes","protagonist"]},
"arc":{"is_a":["structure"],"related":["character","transformation","beginning","middle","end"]},
"tension":{"is_a":["element"],"related":["conflict","stakes","pacing","suspense","drama"]},
"theme":{"is_a":["element"],"related":["meaning","story","character","symbol","message"]},
"beat":{"is_a":["unit"],"related":["scene","action","reaction","turning_point","rhythm"]},
"climax":{"is_a":["beat","structure"],"related":["conflict","resolution","tension","peak","confrontation"]},
"resolution":{"is_a":["beat","structure"],"related":["climax","aftermath","new_normal","closure","denouement"]},
"scene":{"is_a":["unit"],"related":["setting","action","dialogue","character","beat"]},
# General knowledge
"atom":{"is_a":["structure"],"related":["electron","proton","neutron","nucleus","element","molecule"]},
"molecule":{"is_a":["structure"],"related":["atom","bond","chemical","compound","reaction"]},
"cell":{"is_a":["structure","biology"],"related":["life","membrane","dna","division","organism"]},
"dna":{"is_a":["molecule","biology"],"related":["gene","code","life","heredity","protein"]},
"evolution":{"is_a":["process","biology"],"related":["natural_selection","adaptation","species","change"]},
"planet":{"is_a":["body","astronomy"],"related":["orbit","star","gravity","solar_system","earth"]},
"star":{"is_a":["body","astronomy"],"related":["fusion","light","gravity","nuclear","sun"]},
"galaxy":{"is_a":["structure","astronomy"],"related":["star","gravity","dark_matter","universe"]},
"universe":{"is_a":["concept","astronomy"],"related":["cosmos","big_bang","expansion","matter","energy"]},
"brain":{"is_a":["organ","biology"],"related":["neuron","thought","consciousness","mind","nervous"]},
"consciousness":{"is_a":["concept","philosophy"],"related":["mind","awareness","brain","experience","qualia"]},
"information":{"is_a":["concept"],"related":["data","entropy","bits","processing","communication"]},
"language":{"is_a":["system"],"related":["communication","grammar","meaning","symbol","expression"]},
"computer":{"is_a":["machine","technology"],"related":["program","data","algorithm","processing","information"]},
"algorithm":{"is_a":["procedure","computer"],"related":["step","computation","efficiency","logic","data"]},
"network":{"is_a":["structure"],"related":["node","connection","graph","communication","distributed"]},
"system":{"is_a":["concept"],"related":["component","interaction","emergence","feedback","organization"]},
"pattern":{"is_a":["concept"],"related":["regularity","repetition","structure","recognition","order"]},
"structure":{"is_a":["concept"],"related":["organization","form","component","relationship","design"]},
"change":{"is_a":["process"],"related":["time","transformation","difference","motion","growth"]},
"balance":{"is_a":["concept"],"related":["equilibrium","stability","harmony","force","tension"]},
"emergence":{"is_a":["concept"],"related":["system","complexity","property","whole","interaction"]},
"complexity":{"is_a":["concept"],"related":["system","emergence","nonlinear","chaos","organization"]},
"nature":{"is_a":["concept"],"related":["environment","biology","physics","world","organic"]},
"culture":{"is_a":["system"],"related":["society","art","tradition","values","expression"]},
"society":{"is_a":["system"],"related":["people","institution","culture","law","interaction"]},
"art":{"is_a":["activity","culture"],"related":["beauty","expression","creativity","form","meaning"]},
"music":{"is_a":["art"],"related":["rhythm","melody","harmony","sound","emotion"]},
"story":{"is_a":["structure","narrative"],"related":["character","plot","conflict","theme","meaning"]},
"science":{"is_a":["discipline"],"related":["method","experiment","theory","evidence","knowledge"]},
"technology":{"is_a":["tool"],"related":["innvention","computer","engineering","progress","system"]},
"life":{"is_a":["phenomenon","biology"],"related":["organism","growth","reproduction","metabolism","consciousness"]},
"death":{"is_a":["process","biology"],"related":["life","end","mortality","decay","entropy"]},
"love":{"is_a":["emotion"],"related":["affection","attachment","care","bond","relationship"]},
"fear":{"is_a":["emotion"],"related":["danger","threat","anxiety","survival","response"]},
"truth":{"is_a":["concept","philosophy"],"related":["fact","reality","evidence","knowledge","certainty"]},
"beauty":{"is_a":["concept","aesthetic"],"related":["harmony","proportion","form","pleasure","art"]},
"justice":{"is_a":["concept","ethics"],"related":["fairness","law","rights","equality","morality"]},
"freedom":{"is_a":["concept","politics"],"related":["liberty","choice","autonomy","rights","constraint"]},
"power":{"is_a":["concept"],"related":["authority","force","influence","control","energy"]},
"mind":{"is_a":["concept","philosophy"],"related":["thought","consciousness","brain","reason","perception"]},
"knowledge":{"is_a":["concept"],"related":["information","learning","wisdom","understanding","education"]},
"wisdom":{"is_a":["concept"],"related":["knowledge","judgment","insight","experience","discernment"]},
"thought":{"is_a":["concept"],"related":["idea","reasoning","analysis","reflection","cognition"]},
"radiation":{"is_a":["phenomenon"],"related":["photon","light","wave","energy","electromagnetic"]},
"frequency":{"is_a":["property"],"related":["wave","photon","energy","resonance","oscillation"]},
"particle":{"is_a":["entity"],"related":["boson","fermion","photon","electron","quark","quantum"]},
"electromagnetic":{"is_a":["force","interaction"],"related":["photon","light","charge","field","radiation"]},
"nucleus":{"is_a":["structure"],"related":["proton","neutron","atom","strong","binding"]},
"decay":{"is_a":["process"],"related":["muon","neutron","particle","radiation","unstable"]},
"mechanism":{"is_a":["concept"],"related":["higgs","symmetry","breaking","process","cause"]},
"dark_matter":{"is_a":["concept","astronomy"],"related":["galaxy","gravity","universe","halo","rotation"]},
"dark_energy":{"is_a":["concept","astronomy"],"related":["expansion","universe","cosmological","acceleration"]},
}
class DynamicCRG:
"""Self-growing Concept Relation Graph.
Starts with static taxonomy, infers unknown words from context."""
MORPHOLOGY = {"tion":"process","tron":"particle","ism":"concept","ics":"discipline",
"ity":"property","ness":"property","ment":"process","ology":"discipline",
"ance":"property","ence":"property","ure":"process","er":"entity","or":"entity"}
def __init__(self, path=None):
self.nodes = {}
self.adj = defaultdict(set)
self.co_occurrence = defaultdict(int)
self.path = path
self.stats = {"static":0,"dynamic":0,"speculative":0,"confirmed":0}
for w, e in STATIC_CRG.items():
self.nodes[w] = {"is_a":e.get("is_a",[]),"related":e.get("related",[]),
"spec":False,"hits":0,"conf":1.0,"source":"static"}
self._build_adj(w)
self.stats["static"] = len(STATIC_CRG)
if path and os.path.exists(path):
try:
with open(path) as f: d=json.load(f)
for w,n in d.get("nodes",{}).items():
if w not in self.nodes: self.nodes[w]=n
except: pass
def _build_adj(self, w):
node = self.nodes.get(w,{})
for cat in node.get("is_a",[]): self.adj[w].add(cat); self.adj[cat].add(w)
for rel in node.get("related",[]): self.adj[w].add(rel); self.adj[rel].add(w)
def is_known(self, w): return w.lower().strip() in self.nodes
def is_speculative(self, w):
n=self.nodes.get(w.lower().strip()); return n.get("spec",True) if n else True
def get(self, w): return self.nodes.get(w, {"is_a":[],"related":[],"spec":True,"hits":0,"conf":0.1})
def __contains__(self, w): return w in self.nodes
def __len__(self): return len(self.nodes)
def distance(self, a, b, max_d=6):
if a==b: return 0
vis={a}; fr=[(a,0)]
while fr:
nf=[]
for nd,c in fr:
node=self.nodes.get(nd,{})
for nb in node.get("is_a",[])+node.get("related",[]):
if nb==b: return c+(1 if nb in node.get("is_a",[]) else 2)
if nb not in vis and c+2<=max_d: vis.add(nb); nf.append((nb,c+2))
fr=nf
return max_d+1
def min_dist_zone(self, w, zone):
return min(self.distance(z,w) for z in zone) if zone else 7
def encounter(self, word, context="", neighbors=None):
w = word.lower().strip()
if w in self.nodes:
self.nodes[w]["hits"] += 1
self.nodes[w]["last_seen"] = time.time()
return self.nodes[w]
cats, rels = [], []
for nb in (neighbors or []):
if nb in self.nodes:
cats.extend(self.nodes[nb]["is_a"])
rels.append(nb)
for suf, cat in self.MORPHOLOGY.items():
if w.endswith(suf) and len(w) > len(suf)+2:
cats.append(cat)
cats = list(set(cats))[:3]
rels = list(set(rels))[:5]
self.nodes[w] = {"is_a":cats or ["unknown"],"related":rels,"spec":True,
"hits":1,"conf":0.5,"source":"inferred","first_seen":time.time()}
for cat in cats: self.adj[w].add(cat); self.adj[cat].add(w)
for rel in rels: self.adj[w].add(rel); self.adj[rel].add(w)
self.stats["dynamic"] += 1
return self.nodes[w]
def confirm(self, w):
n = self.nodes.get(w)
if n and n.get("spec") and n.get("hits",0)>=3:
n["spec"] = False
n["conf"] = min(1.0, n["conf"]+0.2)
n["source"] = "confirmed"
def speculate(self, word, context="", neighbors=None):
w = word.lower().strip()
node = self.encounter(w, context, neighbors)
return {"word":w,"speculative":node.get("spec",True),"confidence":node.get("conf",0.1),
"inferred_categories":node.get("is_a",[]),"inferred_related":node.get("related",[])[:5],
"source":node.get("source","unknown"),"flag":"⚑ SPECULATIVE" if node.get("spec") else "CONFIRMED"}
def save(self):
if self.path:
with open(self.path,"w") as f:
json.dump({"nodes":self.nodes,"stats":self.stats},f)
# ════════════════════════════════════════════════════════════════════════════════
# Β§4 MATH ENGINE β€” Exact rational arithmetic
# ════════════════════════════════════════════════════════════════════════════════
@dataclass
class MathResult:
operation: str
input_repr: str
result: Any
exact_str: str
approx: float
is_exact: bool
trace: List[str]
fingerprint: Dict[str, Any]
elapsed_us: int = 0
class MathEngine:
"""Exact arithmetic via fractions.Fraction. Zero floating-point drift."""
def add(self, a, b): return self._op("add", f"{a}+{b}", F(a)+F(b))
def sub(self, a, b): return self._op("sub", f"{a}-{b}", F(a)-F(b))
def mul(self, a, b): return self._op("mul", f"{a}*{b}", F(a)*F(b))
def div(self, a, b):
if b==0: return MathResult("div",f"{a}/{b}",None,"undefined",float("nan"),False,["div by 0"],{})
return self._op("div", f"{a}/{b}", F(a)/F(b))
def pow(self, a, b): return self._op("pow", f"{a}^{b}", F(a)**int(b))
def sqrt_exact(self, n):
s = int(math.isqrt(int(n)))
if s*s == int(n): return self._op("sqrt", f"√{n}", F(s))
return MathResult("sqrt",f"√{n}",float(n)**0.5,f"√{n}β‰ˆ{float(n)**0.5:.10f}",
float(n)**0.5,False,[f"{n} not perfect square"],{})
def muon_ratio(self):
r = F(169)/_WOBBLE
err = abs(float(r)-206.7683)/206.7683*100
m = self._op("muon_ratio","169/w",r)
m.fingerprint = {"target_error_pct":err,"verdict":"PREDICTIVE" if err<0.1 else "SURPRISING" if err<1 else "PROVISIONAL"}
return m
def alpha_s(self):
r = 24*_Y**4
err = abs(float(r)-0.1181)/0.1181*100
m = self._op("alpha_s","24*Y^4",r)
m.fingerprint = {"target_error_pct":err,"verdict":"PREDICTIVE" if err<0.1 else "SURPRISING" if err<1 else "PROVISIONAL"}
return m
def hubble(self):
r = F(1,3)*_WOBBLE*_Y**3*_U_E
err = abs(float(r)-70)/70*100
m = self._op("H0","(1/3)*w*Y^3*U_e",r)
m.fingerprint = {"target_error_pct":err,"verdict":"PREDICTIVE" if err<0.1 else "SURPRISING" if err<1 else "PROVISIONAL"}
return m
def constant(self, name):
return {"Y":float(_Y),"PI":float(_PI),"PHI":float(_PHI),"E":float(_E),
"WOBBLE":float(_WOBBLE),"L":float(_L),"U_E":float(_U_E),
"L_S":float(_L_S),"MONAD":float(_MONAD)}.get(name)
def _op(self, op, inp, result):
t0 = time.perf_counter_ns()
exact = str(result)
approx = float(result)
vec = [int(b) for b in word_to_hash24(exact[:20])]
snapped, _ = snap(vec)
fp = classify_vec(snapped)
elapsed = int((time.perf_counter_ns()-t0)/1000)
return MathResult(op,inp,result,exact,approx,True,
[f"{op}: {inp} = {exact}"],fp,elapsed)
MATH = MathEngine()
# ════════════════════════════════════════════════════════════════════════════════
# Β§5 SCRIPT ENGINE β€” Python code sandbox
# ════════════════════════════════════════════════════════════════════════════════
SAFE_MODS = {'math','json','re','hashlib','random','itertools','collections','functools',
'fractions','decimal','string','textwrap','typing','dataclasses','copy','operator',
'bisect','heapq','array','datetime','time','calendar','statistics','numbers','cmath'}
BLOCKED_MODS = {'os','sys','subprocess','shutil','socket','http','urllib','requests','ftplib',
'smtplib','ctypes','importlib','multiprocessing','threading','signal','pickle',
'webbrowser','cgi','wsgiref'}
def run_code(code, timeout=5.0):
"""Run Python code in sandbox. Returns ExecutionResult."""
for mod in BLOCKED_MODS:
if re.search(rf'(?:import|from)\s+{mod}\b', code):
return ExecutionResult(False,"","",f"blocked: {mod}",0)
if re.search(r'\b(?:exec|eval)\s*\(', code):
return ExecutionResult(False,"","",'blocked: exec/eval',0)
t0 = time.perf_counter()
old_out, old_err = sys.stdout, sys.stderr
cap_out, cap_err = io.StringIO(), io.StringIO()
def safe_import(name, *a, **k):
base = name.split('.')[0]
if base in SAFE_MODS: return __import__(name, *a, **k)
raise ImportError(f"'{name}' blocked in sandbox")
globs = {
"__builtins__":{
"__import__":safe_import,"print":print,"len":len,"range":range,"int":int,
"float":float,"str":str,"list":list,"dict":dict,"set":set,"tuple":tuple,"bool":bool,
"abs":abs,"min":min,"max":max,"sum":sum,"round":round,"sorted":sorted,
"enumerate":enumerate,"zip":zip,"map":map,"filter":filter,"any":any,"all":all,
"isinstance":isinstance,"type":type,"dir":dir,"vars":vars,"getattr":getattr,
"hasattr":hasattr,"hash":hash,"repr":repr,"format":format,"chr":chr,"ord":ord,
"hex":hex,"oct":oct,"id":id,"property":property,"object":object,"super":super,
"frozenset":frozenset,"slice":slice,"True":True,"False":False,"None":None,
"Exception":Exception,"ValueError":ValueError,"TypeError":TypeError,"KeyError":KeyError,
"IndexError":IndexError,"ZeroDivisionError":ZeroDivisionError,"NameError":NameError,
"AttributeError":AttributeError,"RuntimeError":RuntimeError,"ImportError":ImportError,
"OverflowError":OverflowError,"StopIteration":StopIteration,"AssertionError":AssertionError,
"TimeoutError":TimeoutError,"OSError":OSError,"FileNotFoundError":FileNotFoundError,
"ArithmeticError":ArithmeticError,"LookupError":LookupError,"MemoryError":MemoryError,
},
"__name__":"__main__","__doc__":None,"__file__":"<sandbox>",
"math":math,"json":json,"re":re,"hashlib":hashlib,"random":random,
"itertools":__import__('itertools'),"collections":__import__('collections'),
"functools":__import__('functools'),"fractions":__import__('fractions'),
"Fraction":F,"Counter":Counter,"defaultdict":defaultdict,
"typing":__import__('typing'),
}
def alarm(s,f):
raise TimeoutError(f"exceeded {timeout}s")
try:
sys.stdout = cap_out; sys.stderr = cap_err
compiled = compile(code, "<sandbox>", "exec")
if hasattr(signal, 'SIGALRM'):
old_h = signal.signal(signal.SIGALRM, alarm)
signal.setitimer(signal.ITIMER_REAL, timeout)
try: exec(compiled, globs)
finally:
if hasattr(signal, 'SIGALRM'):
signal.setitimer(signal.ITIMER_REAL, 0)
signal.signal(signal.SIGALRM, old_h)
return ExecutionResult(True, cap_out.getvalue(), cap_err.getvalue(), "",
(time.perf_counter()-t0)*1000)
except TimeoutError:
return ExecutionResult(False, cap_out.getvalue(), cap_err.getvalue(),
f"TimeoutError: exceeded {timeout}s", (time.perf_counter()-t0)*1000)
except Exception as e:
return ExecutionResult(False, cap_out.getvalue(), cap_err.getvalue(),
f"{type(e).__name__}: {e}", (time.perf_counter()-t0)*1000)
finally:
sys.stdout = old_out; sys.stderr = old_err
@dataclass
class ExecutionResult:
success: bool
stdout: str
stderr: str
exception: str
time_ms: float
def analyze_code(code):
"""AST analysis of Python code."""
result = {"valid":True,"functions":[],"classes":[],"imports":[],"complexity":1,
"has_docstrings":False,"has_type_hints":False,"has_tests":False,"has_main":False,
"n_lines":len(code.split('\n')),"nrci":0.5,"verdict":"UNKNOWN"}
try: tree = ast.parse(code)
except SyntaxError as e: return {"valid":False,"error":f"line {e.lineno}: {e.msg}"}
for node in ast.walk(tree):
if isinstance(node, (ast.FunctionDef, ast.AsyncFunctionDef)):
result["functions"].append(node.name)
if node.name.startswith("test_"): result["has_tests"]=True
if node.returns: result["has_type_hints"]=True
if node.body and isinstance(node.body[0],ast.Expr) and isinstance(node.body[0].value,ast.Constant):
result["has_docstrings"]=True
elif isinstance(node, ast.ClassDef): result["classes"].append(node.name)
elif isinstance(node, (ast.Import, ast.ImportFrom)):
for a in (node.names if isinstance(node,ast.Import) else [node.module or ""]):
result["imports"].append(a if isinstance(a,str) else a.name)
elif isinstance(node, (ast.If, ast.IfExp)): result["complexity"]+=1
elif isinstance(node, (ast.For, ast.While)): result["complexity"]+=1
elif isinstance(node, ast.Try): result["complexity"]+=1
result["has_main"] = "if __name__" in code
n = 0.5
if result["has_docstrings"]: n+=0.1
if result["has_type_hints"]: n+=0.05
if result["has_tests"]: n+=0.1
if result["has_main"]: n+=0.05
if "try" in code: n+=0.1
if result["complexity"]>15: n-=0.1
result["nrci"] = max(0, min(1, n))
result["verdict"] = "EXCELLENT" if n>=0.75 else "GOOD" if n>=0.60 else "FAIR" if n>=0.45 else "NEEDS_WORK"
return result
def validate_code(code):
"""Full code validation."""
a = analyze_code(code)
try: compile(code, '<validate>', 'exec'); compiles=True; compile_error=""
except SyntaxError as e: compiles=False; compile_error=f"line {e.lineno}: {e.msg}"
safety = None
for mod in BLOCKED_MODS:
if re.search(rf'(?:import|from)\s+{mod}\b', code): safety=f"blocked: {mod}"; break
return {"valid_syntax":a["valid"],"compiles":compiles,"compile_error":compile_error,
"safe":safety is None,"safety_issue":safety,"analysis":a,
"nrci_score":a["nrci"],"verdict":a["verdict"]}
# ════════════════════════════════════════════════════════════════════════════════
# Β§6 VISION PIPELINE β€” MOG patches, visual NRCI, dual resonance
# ════════════════════════════════════════════════════════════════════════════════
def patches_to_mog(patches):
"""Convert image patches to Golay-snapped MOG states."""
return [GOLAY.snap_to_codeword(list(p))[0] for p in patches]
def visual_nrci(patches):
"""Compute NRCI statistics across image patches."""
nrcis = [float(LEECH.calculate_nrci(p)) for p in patches]
mean = sum(nrcis)/len(nrcis) if nrcis else 0
std = (sum((x-mean)**2 for x in nrcis)/len(nrcis))**0.5 if nrcis else 0
verdict = "HIGH_COHERENCE" if mean>0.74 else "MODERATE" if mean>0.68 else "LOW_COHERENCE"
plateau_072 = sum(1 for n in nrcis if abs(n-0.7196)<0.05)
plateau_050 = sum(1 for n in nrcis if abs(n-0.50)<0.05)
return {"nrci_mean":round(mean,4),"nrci_min":round(min(nrcis),4) if nrcis else 0,
"nrci_max":round(max(nrcis),4) if nrcis else 0,"nrci_std":round(std,4),
"verdict":verdict,"n_patches":len(patches),
"plateau_072":plateau_072,"plateau_050":plateau_050}
def image_centroid(patches):
"""Compute EMA centroid of image patches."""
c = [0.0]*24
for p in patches:
for i in range(24): c[i] = 0.3*p[i] + 0.7*c[i]
bits = [1 if x>0.5 else 0 for x in c]
return GOLAY.snap_to_codeword(bits)[0]
def text_visual_resonance(text_vec, vis_vec):
"""Compute dual-modality resonance between text and visual vectors."""
d = hamming(text_vec, vis_vec)
and_v = [a&b for a,b in zip(text_vec, vis_vec)]
and_hw = sum(and_v)
snapped_and, _ = snap(and_v)
snapped_nrci = float(LEECH.calculate_nrci(snapped_and))
resonance = "HIGH" if d<=2 else "MODERATE" if d<=6 else "LOW" if d<=10 else "NONE"
return {"hamming_distance":d,"resonance":resonance,"and_hw":and_hw,
"mass_defect":2*and_hw,"snapped_and_nrci":snapped_nrci}
# ════════════════════════════════════════════════════════════════════════════════
# Β§7 RESONANCE & VETO β€” Multi-signal scoring, CRG-first constraints
# ════════════════════════════════════════════════════════════════════════════════
def geometric_resonance(candidate_vec, zone_centroid, prev_vec=None,
weights=(0.4, 0.4, 0.2)):
"""Geometric resonance score: Hamming proximity + NRCI + transition smoothness."""
w_prox, w_nrci, w_smooth = weights
d = hamming(candidate_vec, zone_centroid)
proximity = 1.0 - d/24.0
nrci_val = float(LEECH.calculate_nrci(candidate_vec))
if prev_vec is not None:
smoothness = 1.0 - hamming(candidate_vec, prev_vec)/24.0
else:
smoothness = 0.5
return w_prox*proximity + w_nrci*nrci_val + w_smooth*smoothness
class HardVeto:
"""CRG-first constraint masking. Semantic grounding beats random vectors."""
def __init__(self, vocab, max_hamming=14, min_nrci=0.58,
crg=None, max_crg_dist=6, allowed_lattices=None):
self.vocab = vocab
self.max_hamming = max_hamming
self.min_nrci = min_nrci
self.crg = crg
self.max_crg_dist = max_crg_dist
self.allowed_lattices = allowed_lattices
self._zone_words = []
def set_zone_words(self, words):
self._zone_words = words
def check(self, word, zone):
"""Check if word passes all constraints. Returns (pass, reason)."""
vec = self.vocab.get_vector(word)
info = classify_vec(vec)
# 1. NRCI threshold
if info["nrci"] < self.min_nrci:
return False, f"NRCI {info['nrci']:.4f}<{self.min_nrci}"
# 2. CRG distance (check first β€” semantic beats random)
crg_d = None
if self.crg and self._zone_words:
crg_d = self.crg.min_dist_zone(word, self._zone_words)
if crg_d > self.max_crg_dist and word in self.crg:
return False, f"CRG dist {crg_d}>{self.max_crg_dist}"
# Unknown words (not in CRG) get penalized but not hard-vetoed
# unless they also fail Hamming
# 3. Hamming (skip if strong CRG connection)
centroid = zone.get_centroid()
d = hamming(vec, centroid)
if d > self.max_hamming and (crg_d is None or crg_d > 4):
return False, f"d_H {d}>{self.max_hamming}"
# 4. Lattice restriction
if self.allowed_lattices and info["lattice"] not in self.allowed_lattices:
return False, f"lattice '{info['lattice']}' not allowed"
# 5. Unknown word penalty (not in CRG + not in vocab)
if self.crg and word not in self.crg and not hasattr(self.vocab, 'word_snapped'):
pass # allow if vocab has it
elif self.crg and word not in self.crg:
# Not in CRG and not known β€” soft check via NRCI only
if info["nrci"] < 0.65:
return False, f"unknown+low NRCI {info['nrci']:.4f}"
return True, "pass"
def filter_candidates(self, candidates, zone):
passed, vetoed = [], []
for w in candidates:
ok, reason = self.check(w, zone)
if ok: passed.append(w)
else: vetoed.append((w, reason))
return passed, vetoed
class ResonanceScorer:
"""Multi-signal resonance: proximity + NRCI + coherence + grid match."""
def __init__(self, vocab, zone, crg=None, bias_strength=1.0):
self.vocab = vocab
self.zone = zone
self.crg = crg
self.bias_strength = bias_strength
self.prev_vec = None
def score(self, word):
vec = self.vocab.get_vector(word)
centroid = self.zone.get_centroid()
proximity = 1.0 - hamming(vec, centroid)/24.0
nrci_val = float(LEECH.calculate_nrci(vec))
# Grid match (from ValueGeometry)
p = token_profile(word)
centroid_grid = self._zone_grid()
grid_match = 1.0 if p["grid"]==centroid_grid else 0.5
# CRG bonus
crg_bonus = 0
if self.crg and self.zone.words:
d = self.crg.min_dist_zone(word, self.zone.words[-5:])
crg_bonus = max(0, (6-d)/6) * 0.1
resonance = 0.30*proximity + 0.30*nrci_val + 0.20*grid_match + 0.20*crg_bonus
return {"resonance":round(resonance,4),"proximity":round(proximity,4),
"nrci":round(nrci_val,4),"grid_match":round(grid_match,4),
"crg_bonus":round(crg_bonus,4)}
def bias_logits(self, candidates, base_logits):
centroid = self.zone.get_centroid()
biased = {}
for w in candidates:
vec = self.vocab.get_vector(w)
r = geometric_resonance(vec, centroid, self.prev_vec)
biased[w] = base_logits.get(w,0) + self.bias_strength * r * 5.0
return biased
def update_context(self, word):
self.prev_vec = self.vocab.get_vector(word)
self.zone.update(word, self.vocab)
def _zone_grid(self):
grids = {}
for w in self.zone.words[-5:]:
g = token_profile(w)["grid"]
grids[g] = grids.get(g,0)+1
return max(grids, key=grids.get) if grids else "other"
# ════════════════════════════════════════════════════════════════════════════════
# Β§8 LOGITSPROCESSOR β€” Drop-in for HuggingFace models
# ════════════════════════════════════════════════════════════════════════════════
try:
import torch
from transformers import LogitsProcessor as _HFLogitsProcessor
_HF_AVAILABLE = True
except (ImportError, OSError):
class _HFLogitsProcessor:
def __call__(self, input_ids, scores): return scores
_HF_AVAILABLE = False
class GLMLogitsProcessor(_HFLogitsProcessor):
"""Drop-in HuggingFace LogitsProcessor with GLM verification.
Usage:
processor = GLMLogitsProcessor(tokenizer=tokenizer, crg=crg)
outputs = model.generate(input_ids, logits_processor=[processor])
"""
def __init__(self, tokenizer=None, vocab=None, crg=None, bias_strength=1.0,
max_crg_dist=6, min_nrci=0.55, context_window=20):
super().__init__()
self.tokenizer = tokenizer
self.vocab = vocab or SVDVocabulary()
self.crg = crg or DynamicCRG()
self.bias_strength = bias_strength
self.max_crg_dist = max_crg_dist
self.min_nrci = min_nrci
self.context_window = context_window
self.zone = IdeaZone()
self._token_cache = {}
self._vector_cache = {}
self._crg_cache = {}
self.stats = {"total":0,"vetoed":0,"biased":0}
def __call__(self, input_ids, scores):
self._update_zone(input_ids[0])
centroid = self.zone.get_centroid()
zone_words = self.zone.words[-5:]
for token_id in range(scores.shape[1]):
word = self._token_to_word(token_id)
if not word or len(word)<2: continue
# CRG veto
if zone_words:
min_d = min(self._crg_dist(z, word) for z in zone_words)
word_in_crg = word in self.crg
if min_d > self.max_crg_dist and word_in_crg:
scores[:, token_id] = float('-inf')
self.stats["vetoed"] += 1
continue
elif min_d > self.max_crg_dist and not word_in_crg:
scores[:, token_id] -= 2.0
# NRCI filter
vec = self._get_vector(word)
n = float(LEECH.calculate_nrci(vec))
if n < self.min_nrci:
scores[:, token_id] += (n - self.min_nrci) * 10
# Resonance bias
if self.bias_strength > 0:
r = geometric_resonance(vec, centroid)
scores[:, token_id] += self.bias_strength * (r - 0.5) * 2.0
self.stats["total"] += 1
return scores
def _update_zone(self, token_ids):
if self.tokenizer is None: return
recent = token_ids[-self.context_window:].tolist()
text = self.tokenizer.decode(recent, skip_special_tokens=True)
for w in re.findall(r'[a-z]{3,}', text.lower()):
self.zone.update(w, self.vocab)
def _token_to_word(self, token_id):
if token_id in self._token_cache: return self._token_cache[token_id]
if self.tokenizer is None: self._token_cache[token_id]=""; return ""
try:
w = self.tokenizer.decode([token_id], skip_special_tokens=True).strip().lower()
w = re.sub(r'^[^\w]+|[^\w]+$','',w); w = re.sub(r'^##','',w)
self._token_cache[token_id] = w; return w
except: self._token_cache[token_id]=""; return ""
def _get_vector(self, word):
if word in self._vector_cache: return self._vector_cache[word]
vec = self.vocab.get_vector(word)
self._vector_cache[word] = vec; return vec
def _crg_dist(self, a, b):
key = (min(a,b), max(a,b))
if key in self._crg_cache: return self._crg_cache[key]
d = self.crg.distance(a, b)
self._crg_cache[key] = d; return d
def get_stats(self):
total = self.stats["total"] or 1
return {"total_evaluated":self.stats["total"],"vetoed":self.stats["vetoed"],
"veto_rate":f"{self.stats['vetoed']/total:.1%}",
"biased":self.stats["biased"],"zone_nrci":self.zone.get_centroid_nrci(),
"zone_words":self.zone.words[-10:]}
def reset(self):
self.zone = IdeaZone()
self._token_cache.clear(); self._vector_cache.clear(); self._crg_cache.clear()
self.stats = {"total":0,"vetoed":0,"biased":0}
def create_glm_processor(tokenizer=None, bias_strength=1.0, **kwargs):
"""Create a GLM LogitsProcessor with sensible defaults."""
return GLMLogitsProcessor(tokenizer=tokenizer, bias_strength=bias_strength, **kwargs)
# ════════════════════════════════════════════════════════════════════════════════
# Β§9 PIPELINE β€” System 1/2 loop
# ════════════════════════════════════════════════════════════════════════════════
class LLMPipeline:
"""Full System 1/2 hybrid pipeline.
System 1 (LLM) proposes β†’ System 2 (GLM) verifies β†’ output."""
def __init__(self, vocab, crg=None, bias_strength=1.0,
max_hamming=14, min_nrci=0.58, max_crg_dist=6):
self.vocab = vocab
self.zone = IdeaZone()
self.crg = crg or DynamicCRG()
self.scorer = ResonanceScorer(vocab, self.zone, crg, bias_strength)
self.veto = HardVeto(vocab, max_hamming, min_nrci, crg, max_crg_dist)
self.math = MATH
self.history = []
def process(self, input_text, candidates, base_logits=None,
apply_veto=True, apply_bias=True):
"""Run one pipeline step."""
result = PipelineResult()
result.input_text = input_text
result.raw_candidates = list(candidates)
for w in input_text.lower().split():
if len(w)>=3 and w.isalpha(): self.zone.update(w, self.vocab)
self.veto.set_zone_words(self.zone.words[-5:])
if base_logits is None: base_logits = {c:0.0 for c in candidates}
# Math check
result.math_results = self._check_math(input_text)
# Veto
working = list(candidates)
if apply_veto:
working, result.vetoed = self.veto.filter_candidates(working, self.zone)
if not working: working = list(candidates); result.vetoed = []
result.passed = list(working)
# Bias
if apply_bias and working:
result.biased_logits = self.scorer.bias_logits(working, base_logits)
result.selected = max(result.biased_logits, key=result.biased_logits.get)
else:
result.biased_logits = {w:base_logits.get(w,0) for w in working}
result.selected = max(result.biased_logits, key=result.biased_logits.get) if working else None
if result.selected: self.scorer.update_context(result.selected)
result.zone_nrci = self.zone.get_centroid_nrci()
result.zone_lattice = classify_vec(self.zone.get_centroid())["lattice"]
result.stats = {"total":len(candidates),"vetoed":len(result.vetoed),
"passed":len(result.passed),"zone_nrci":result.zone_nrci,
"veto_rate":len(result.vetoed)/len(candidates) if candidates else 0}
self.history.append(result)
return result
def _check_math(self, text):
results = []
for a, op, b in re.findall(r'(\d+[\.\d]*)\s*([+\-Γ—*/])\s*(\d+[\.\d]*)', text)[:3]:
try:
a_v, b_v = F(a) if '.' in a else int(a), F(b) if '.' in b else int(b)
if op=='+': results.append(self.math.add(a_v,b_v))
elif op=='-': results.append(self.math.sub(a_v,b_v))
elif op in ('Γ—','*'): results.append(self.math.mul(a_v,b_v))
elif op=='/': results.append(self.math.div(a_v,b_v))
except: pass
return results
def reset(self):
self.zone = IdeaZone()
self.scorer = ResonanceScorer(self.vocab, self.zone, self.crg)
self.history = []
@dataclass
class PipelineResult:
input_text: str = ""
raw_candidates: List[str] = field(default_factory=list)
vetoed: List[Tuple[str,str]] = field(default_factory=list)
passed: List[str] = field(default_factory=list)
biased_logits: Dict[str,float] = field(default_factory=dict)
selected: Optional[str] = None
math_results: List[Any] = field(default_factory=list)
zone_nrci: float = 0.0
zone_lattice: str = ""
stats: Dict[str,Any] = field(default_factory=dict)
# ════════════════════════════════════════════════════════════════════════════════
# Β§10 SPECULATIVE DECODING β€” GLM draft β†’ LLM verify
# ════════════════════════════════════════════════════════════════════════════════
class GLMDrafter:
"""Drafts token sequences using deterministic geometric transitions."""
def __init__(self, vocab, crg, zone):
self.vocab = vocab
self.crg = crg
self.zone = zone
def draft(self, n_tokens=5):
draft = []
current_zone = list(self.zone.words[-5:])
centroid = self.zone.get_centroid()
for i in range(n_tokens):
candidates = set()
for zw in current_zone:
node = self.crg.get(zw)
for rel in node.get("related",[])+node.get("is_a",[]):
candidates.add(rel)
candidates = [c for c in candidates if c in self.crg.nodes]
if not candidates: break
scored = []
for w in candidates:
vec = self.vocab.get_vector(w)
r = geometric_resonance(vec, centroid)
if w in [dw for dw,_ in draft]: r *= 0.3
scored.append((w, r))
scored.sort(key=lambda x: -x[1])
if scored:
draft.append(scored[0])
current_zone.append(scored[0][0])
current_zone = current_zone[-5:]
return draft
def draft_and_verify(self, n_tokens=5, verify_fn=None):
draft = self.draft(n_tokens)
accepted, rejected = [], []
for word, conf in draft:
if verify_fn:
ok, reason = verify_fn(word)
if ok: accepted.append((word, conf, reason))
else: rejected.append((word, conf, reason)); break
else:
accepted.append((word, conf, "no verification"))
return {"draft":draft,"accepted":accepted,"rejected":rejected,
"acceptance_rate":len(accepted)/len(draft) if draft else 0,
"draft_confidence":sum(c for _,c in draft)/len(draft) if draft else 0}
# ════════════════════════════════════════════════════════════════════════════════
# Β§11 KV PRUNING β€” CRG-based attention cache
# ════════════════════════════════════════════════════════════════════════════════
class CRGKVPruner:
"""Prunes KV cache based on CRG topology relevance."""
def __init__(self, vocab, crg, max_cache_size=100):
self.vocab = vocab
self.crg = crg
self.max_cache_size = max_cache_size
self.active_zone = []
def update_zone(self, zone_words):
self.active_zone = zone_words
def score_token(self, word):
if not self.active_zone: return 0.5
w = word.lower().strip()
if self.crg.is_known(w):
d = self.crg.min_dist_zone(w, self.active_zone)
return max(0, 1.0 - d/6.0)
else:
vec = self.vocab.get_vector(w)
zone_vecs = [self.vocab.get_vector(z) for z in self.active_zone]
if zone_vecs:
avg_d = sum(hamming(vec, zv) for zv in zone_vecs)/len(zone_vecs)
return max(0, (1.0 - avg_d/24.0) * 0.5)
return 0.3
# ════════════════════════════════════════════════════════════════════════════════
# Β§12 VALUEGEOMETRY β€” Self-assembling integer geometry
# ════════════════════════════════════════════════════════════════════════════════
def to_gray(n, bits=24):
n = abs(int(n)) & ((1<<bits)-1)
g = n ^ (n>>1)
return [(g>>i)&1 for i in range(bits-1,-1,-1)]
def prime_factors(n):
if n<2: return []
f, d = [], 2
while d*d<=n:
e=0
while n%d==0: n//=d; e+=1
if e: f.append((d,e))
d+=1
if n>1: f.append((n,1))
return f
def is_prime(n):
if n<2: return False
if n<4: return True
if n%2==0 or n%3==0: return False
i=5
while i*i<=n:
if n%i==0 or n%(i+2)==0: return False
i+=6
return True
def omega(n): return len(prime_factors(n))
def token_to_int(token):
return int.from_bytes(hashlib.sha256(token.lower().strip().encode()).digest()[:4], 'big')
def token_profile(token):
"""ValueGeometry profile: self-assembling geometry from prime factorization."""
n = token_to_int(token)
pf = prime_factors(n)
w = len(pf)
lpf = pf[-1][0] if pf else 1
primes = [p for p,_ in pf]
if w<=1: imb = 0.0
else:
masses = [math.log(p) for p in primes]
mean = sum(masses)/len(masses)
std = math.sqrt(sum((m-mean)**2 for m in masses)/len(masses))
imb = std/mean if mean>0 else 0
grid = "square" if lpf%4==1 or lpf==2 else "hexagonal" if lpf%3==1 or lpf==3 else "other"
wobble = "Smooth" if imb<0.001 else "Light" if imb<0.15 else "Moderate" if imb<0.30 else "Heavy"
gray = to_gray(n)
snapped, _ = GOLAY.snap_to_codeword(gray)
nrci_val = float(LEECH.calculate_nrci(snapped))
hw = sum(snapped)
if hw==0: lat="Identity"
elif hw==8: lat="Octad"
elif hw==12: lat="Dodecad"
elif hw==16: lat="Hexadecad"
else: lat=f"HW-{hw}"
band = "IN-BAND" if nrci_val>=0.70 else "ANOMALY" if nrci_val>=0.60 else "SUBLIMINAL"
return {"token":token,"n":n,"factors":pf,"omega":w,"grid":grid,"imbalance":round(imb,4),
"wobble":wobble,"is_prime":is_prime(n),"nrci":round(nrci_val,4),
"lattice":lat,"band":band,"gray_hw":sum(gray),"snap_hw":hw}
def gray_golay_pipeline(n):
"""Full Gray→Golay→NRCI pipeline."""
gray = to_gray(n)
snapped, meta = GOLAY.snap_to_codeword(gray)
nrci_val = float(LEECH.calculate_nrci(snapped))
hw = sum(snapped)
if hw==0: lat="Identity"
elif hw==8: lat="Octad"
elif hw==12: lat="Dodecad"
elif hw==16: lat="Hexadecad"
else: lat=f"HW-{hw}"
return {"n":n,"gray_hw":sum(gray),"snap_hw":hw,"nrci":round(nrci_val,6),
"lattice":lat,"band":"IN-BAND" if nrci_val>=0.70 else "ANOMALY" if nrci_val>=0.60 else "SUBLIMINAL",
"syndrome_weight":meta["syndrome_weight"],"corrected":meta["corrected"]}
# ════════════════════════════════════════════════════════════════════════════════
# Β§13 AGENT β€” Interactive REPL
# ════════════════════════════════════════════════════════════════════════════════
class GLMAgent:
"""Interactive Gemma-GLM Agent with full system integration."""
BANNER = """
╔════════════════════════════════════════════════════════════════════╗
β•‘ Gemma-GLM v1.0 β€” Geometric Language Machine β•‘
β•‘ The deterministic brain for any LLM. β•‘
β•‘ β•‘
β•‘ Type /help for commands, or just talk. β•‘
β•šβ•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•"""
def __init__(self, api_url=None, crg_path=None):
self.crg = DynamicCRG(crg_path or os.path.expanduser("~/.gemma_glm_crg.json"))
self.vocab = SVDVocabulary()
self.zone = IdeaZone()
self.math = MATH
self.api_url = api_url
self.history = []
def run(self):
print(self.BANNER)
while True:
try:
user = input("\n> ").strip()
except (EOFError, KeyboardInterrupt):
print("\nGoodbye."); break
if not user: continue
if user.startswith("/"): self._command(user)
else: self._respond(user)
def _command(self, cmd):
parts = cmd.split()
c = parts[0].lower()
if c == "/help":
print("""
Commands:
/muon Compute muon/electron mass ratio
/alpha_s Compute strong coupling constant
/hubble Compute Hubble constant
/profile <word> ValueGeometry profile
/pipeline <text> ... Run System 1/2 pipeline
/code <python> Run code in sandbox
/validate <file> Validate a Python file
/draft [n] Draft n tokens (speculative decoding)
/zone Show current context zone
/crg <word> CRG info for word
/crgstats CRG statistics
/vision Demo visual NRCI
/veto <word> ... Test veto on words
/resonance <word> Resonance score
/math <expr> Exact math (e.g., /math 1/3 + 1/6)
/quit Save and exit
""")
elif c == "/muon":
r = self.math.muon_ratio()
t = r.fingerprint.get("target_error_pct",0)
v = r.fingerprint.get("verdict","?")
print(f" m_ΞΌ/m_e = {r.approx:.4f} (target: 206.7683, err: {t:.4f}% [{v}])")
elif c == "/alpha_s":
r = self.math.alpha_s()
t = r.fingerprint.get("target_error_pct",0)
print(f" Ξ±_s = {r.approx:.6f} (target: 0.1181, err: {t:.4f}%)")
elif c == "/hubble":
r = self.math.hubble()
t = r.fingerprint.get("target_error_pct",0)
print(f" Hβ‚€ = {r.approx:.4f} km/s/Mpc (target: 70.0, err: {t:.4f}%)")
elif c == "/profile" and len(parts)>1:
p = token_profile(parts[1])
print(f" Token: {p['token']}")
print(f" Integer: {p['n']}")
print(f" Factors: {p['factors']}")
print(f" Ο‰ (dim): {p['omega']}")
print(f" Grid: {p['grid']}")
print(f" Imbalance:{p['imbalance']}")
print(f" Wobble: {p['wobble']}")
print(f" NRCI: {p['nrci']:.4f}")
print(f" Lattice: {p['lattice']}")
print(f" Band: {p['band']}")
print(f" Prime: {p['is_prime']}")
elif c == "/pipeline":
text = " ".join(parts[1:])
candidates = ["electron","boson","radiation","frequency","field",
"mass","energy","quantum","kitchen","umbrella"]
pipe = LLMPipeline(self.vocab, self.crg)
r = pipe.process(text, candidates)
print(f" Zone: {pipe.zone.words[-5:]}")
print(f" Vetoed: {[w for w,_ in r.vetoed]}")
print(f" Passed: {r.passed}")
print(f" Selected: '{r.selected}'")
print(f" Zone NRCI: {r.zone_nrci:.4f} ({r.zone_lattice})")
elif c == "/code":
code = " ".join(parts[1:])
r = run_code(code, timeout=5.0)
if r.success: print(f" Output: {r.stdout.strip()}")
else: print(f" Error: {r.exception}")
print(f" Time: {r.time_ms:.2f}ms")
elif c == "/validate" and len(parts)>1:
path = parts[1]
if os.path.exists(path):
with open(path) as f: code=f.read()
v = validate_code(code)
print(f" Valid: {v['valid_syntax']}")
print(f" Compiles: {v['compiles']}")
print(f" Safe: {v['safe']}")
print(f" NRCI: {v['nrci_score']:.2f}")
print(f" Verdict: {v['verdict']}")
else: print(f" File not found: {path}")
elif c == "/draft":
n = int(parts[1]) if len(parts)>1 else 5
drafter = GLMDrafter(self.vocab, self.crg, self.zone)
draft = drafter.draft(n)
print(f" Zone: {self.zone.words[-5:]}")
for w, s in draft:
spec = "⚑" if self.crg.is_speculative(w) else " "
print(f" {spec} {w:20s} resonance={s:.4f}")
elif c == "/zone":
print(f" Words: {self.zone.words[-10:]}")
zc = self.zone.get_centroid() if self.zone.words else [0]*24
print(f" Centroid HW={sum(zc)}, NRCI={float(LEECH.calculate_nrci(zc)):.4f}")
elif c == "/crg" and len(parts)>1:
w = parts[1].lower()
n = self.crg.get(w)
print(f" {w}:")
print(f" is_a: {n['is_a']}")
print(f" related: {n['related'][:8]}")
print(f" speculative: {n.get('spec',False)}")
print(f" hits: {n.get('hits',0)}")
elif c == "/crgstats":
total = len(self.crg)
static = sum(1 for n in self.crg.nodes.values() if not n.get("spec"))
spec = sum(1 for n in self.crg.nodes.values() if n.get("spec"))
print(f" Total: {total}")
print(f" Static: {static}")
print(f" Speculative: {spec}")
print(f" Edges: {sum(len(v) for v in self.crg.adj.values())}")
elif c == "/vision":
random.seed(42)
crisp = [[1 if (i+j)%3==0 else 0 for j in range(24)] for i in range(24)]
noisy = [[random.randint(0,1) for _ in range(24)] for _ in range(24)]
cs = visual_nrci(patches_to_mog(crisp))
ns = visual_nrci(patches_to_mog(noisy))
print(f" Crisp: NRCI={cs['nrci_mean']:.4f} ({cs['verdict']})")
print(f" Noisy: NRCI={ns['nrci_mean']:.4f} ({ns['verdict']})")
print(f" Gap: {cs['nrci_mean']-ns['nrci_mean']:.4f}")
elif c == "/veto" and len(parts)>1:
words = parts[1:]
veto = HardVeto(self.vocab, crg=self.crg)
veto.set_zone_words(self.zone.words[-5:] or ["photon","energy","light"])
for w in words:
ok, reason = veto.check(w, self.zone)
print(f" {'PASS' if ok else 'VETO':4s} {w:16s} {reason}")
elif c == "/resonance" and len(parts)>1:
w = parts[1]
if not self.zone.words:
for zw in ["photon","energy","light"]: self.zone.update(zw, self.vocab)
scorer = ResonanceScorer(self.vocab, self.zone, self.crg)
s = scorer.score(w)
print(f" {w}: resonance={s['resonance']:.4f} (prox={s['proximity']:.4f}, "
f"nrci={s['nrci']:.4f}, grid={s['grid_match']:.4f})")
elif c == "/math" and len(parts)>1:
expr = " ".join(parts[1:])
try:
r = eval(expr, {"__builtins__":{},"Fraction":F,"math":math})
print(f" = {r}")
if isinstance(r, F): print(f" β‰ˆ {float(r):.10f}")
except Exception as e: print(f" Error: {e}")
elif c == "/quit":
self.crg.save(); print("Goodbye."); sys.exit(0)
else:
print(f" Unknown command: {c}. Type /help.")
def _respond(self, user):
words = re.findall(r'[a-z]{3,}', user.lower())
for w in words:
self.zone.update(w, self.vocab)
if not self.crg.is_known(w): self.crg.encounter(w, user, self.zone.words[-5:])
self.zone.words = self.zone.words[-20:]
# Math detection
m = re.search(r'(\d+[\d.]*)\s*([+\-Γ—*/^])\s*(\d+[\d.]*)', user)
if m:
a, op, b = m.groups()
av, bv = F(a), F(b)
if op=='+': r=av+bv
elif op=='-': r=av-bv
elif op in ('Γ—','*'): r=av*bv
elif op=='/': r=av/bv if bv!=0 else "undefined"
elif op=='^': r=av**int(bv)
else: r="?"
print(f" {a} {op} {b} = {r}")
if isinstance(r,F) and r.denominator!=1: print(f" β‰ˆ {float(r):.10f}")
return
# Physics detection
if any(kw in user.lower() for kw in ["muon","mass ratio"]):
r = self.math.muon_ratio()
print(f" The muon/electron mass ratio is {r.approx:.4f}")
print(f" (UBP formula: 169/w, error: {r.fingerprint.get('target_error_pct',0):.4f}%)")
return
if any(kw in user.lower() for kw in ["alpha","coupling","strong force"]):
r = self.math.alpha_s()
print(f" The strong coupling Ξ±_s = {r.approx:.6f}")
print(f" (UBP formula: 24·Y⁴, error: {r.fingerprint.get('target_error_pct',0):.4f}%)")
return
if any(kw in user.lower() for kw in ["hubble","expansion","universe"]):
r = self.math.hubble()
print(f" The Hubble constant Hβ‚€ = {r.approx:.2f} km/s/Mpc")
print(f" (UBP formula: β…“Β·wΒ·YΒ³Β·U_e, error: {r.fingerprint.get('target_error_pct',0):.4f}%)")
return
if any(kw in user.lower() for kw in ["write code","function","script","python"]):
print(" I can help with code. Use /code to run Python, or paste code for analysis.")
return
# LLM or draft
if self.api_url:
try:
import urllib.request
data = json.dumps({"prompt":user,"max_tokens":200}).encode()
req = urllib.request.Request(self.api_url, data=data,
headers={"Content-Type":"application/json"})
resp = urllib.request.urlopen(req, timeout=10)
result = json.loads(resp.read())
text = result.get("text",result.get("choices",[{}])[0].get("text",""))
print(f" {text[:500]}")
except Exception as e:
print(f" LLM error: {e}")
self._draft_response()
else:
self._draft_response()
def _draft_response(self):
if not self.zone.words:
print(" I'm listening. Ask about physics, math, or code.")
return
drafter = GLMDrafter(self.vocab, self.crg, self.zone)
draft = drafter.draft(5)
if draft:
print(f" Related concepts: {', '.join(w for w,_ in draft)}")
print(f" (Use /draft for more, or connect an LLM with --api)")
# ════════════════════════════════════════════════════════════════════════════════
# SELF-TEST
# ════════════════════════════════════════════════════════════════════════════════
def self_test():
"""Full system self-test."""
print("═"*60)
print("GEMMA-GLM SELF-TEST")
print("═"*60)
passed = 0; total = 0
# 1. Golay engine
total += 1
# Encode a real codeword from a message
msg = [1,0,1,1,0,1,0,0,1,0,1,1]
cw = GOLAY.encode(msg)
s2, m2 = GOLAY.snap_to_codeword(cw)
if s2==cw and sum(cw)==12: passed+=1; print(f" βœ“ Golay [24,12,8] engine (encode+snap, HW={sum(cw)})")
else: print(f" βœ— Golay engine: snap HW={sum(s2)}, meta={m2}")
# 2. Leech NRCI (use a real octad)
total += 1
octads = GOLAY.get_octads()
oct = octads[0] # first octad
n = float(LEECH.calculate_nrci(oct))
hw = sum(oct)
if hw==8 and 0.75 < n < 0.77: passed+=1; print(f" βœ“ Leech NRCI = {n:.4f} (octad HW={hw})")
else: print(f" βœ— Leech NRCI = {n:.4f} (expected ~0.7623, got HW={hw})")
# 3. Constants
total += 1
if abs(float(_PI) - 3.14159) < 0.001:
passed+=1; print(f" βœ“ Ο€ = {float(_PI):.10f}")
else: print(f" βœ— Ο€ = {float(_PI)}")
# 4. Math engine
total += 1
r = MATH.muon_ratio()
if abs(r.approx - 206.77) < 0.1:
passed+=1; print(f" βœ“ muon/e = {r.approx:.4f} (err={r.fingerprint.get('target_error_pct',0):.4f}%)")
else: print(f" βœ— muon/e = {r.approx:.4f}")
# 5. Fractions
total += 1
r = MATH.add(F(1,3), F(1,6))
if r.result == F(1,2): passed+=1; print(f" βœ“ 1/3 + 1/6 = {r.result}")
else: print(f" βœ— 1/3 + 1/6 = {r.result}")
# 6. CRG
total += 1
crg = DynamicCRG()
d1 = crg.distance("photon","boson")
d2 = crg.distance("photon","kitchen")
if d1 < d2: passed+=1; print(f" βœ“ CRG: photonβ†’boson={d1}, photonβ†’kitchen={d2}")
else: print(f" βœ— CRG distances")
# 7. Dynamic growth
total += 1
crg.encounter("nanoparticle","tiny particle",["particle"])
if "nanoparticle" in crg: passed+=1; print(" βœ“ Dynamic CRG growth")
else: print(" βœ— Dynamic growth")
# 8. Code sandbox
total += 1
r = run_code("from fractions import Fraction\nprint(Fraction(1,3))")
if r.success and "1/3" in r.stdout: passed+=1; print(" βœ“ Code sandbox")
else: print(f" βœ— Code sandbox: {r.exception}")
# 9. Code analysis
total += 1
a = analyze_code("def f():\n pass")
if a["valid"]: passed+=1; print(" βœ“ Code analysis")
else: print(" βœ— Code analysis")
# 10. Safety
total += 1
r = run_code("import os")
if not r.success: passed+=1; print(" βœ“ Safety blocking")
else: print(" βœ— Safety blocking")
# 11. Vision
total += 1
random.seed(42)
patches = [[random.randint(0,1) for _ in range(24)] for _ in range(24)]
vs = visual_nrci(patches_to_mog(patches))
if vs["nrci_mean"]>0: passed+=1; print(f" βœ“ Vision NRCI = {vs['nrci_mean']:.4f} ({vs['verdict']})")
else: print(" βœ— Vision NRCI")
# 12. Token profile
total += 1
p = token_profile("photon")
if p["nrci"]>0: passed+=1; print(f" βœ“ ValueGeometry: photon grid={p['grid']}, Ο‰={p['omega']}")
else: print(" βœ— ValueGeometry")
# 13. Gray→Golay→NRCI
total += 1
vg = gray_golay_pipeline(137)
if vg["band"]=="IN-BAND": passed+=1; print(f" βœ“ Pipeline: 137β†’{vg['lattice']} ({vg['band']})")
else: print(f" βœ— Pipeline: {vg}")
# 14. Veto
total += 1
vocab = SVDVocabulary()
zone = IdeaZone()
for w in ["photon","energy","light","quantum","wave"]: zone.update(w, vocab)
veto = HardVeto(vocab, crg=crg)
veto.set_zone_words(zone.words[-5:])
ok1, r1 = veto.check("boson", zone)
ok2, r2 = veto.check("protagonist", zone)
if ok1 and not ok2: passed+=1; print(" βœ“ Veto: boson=PASS, protagonist=VETO")
else: print(f" βœ— Veto: boson={ok1}({r1}), protagonist={ok2}({r2})")
# 15. Resonance
total += 1
r = geometric_resonance(vocab.get_vector("boson"), zone.get_centroid())
if r > 0.5: passed+=1; print(f" βœ“ Resonance: boson={r:.4f}")
else: print(f" βœ— Resonance: {r:.4f}")
# 16. Pipeline
total += 1
pipe = LLMPipeline(vocab, crg)
r = pipe.process("explain photon",["frequency","kitchen","boson","umbrella"])
if r.selected in ["frequency","boson"]: passed+=1; print(f" βœ“ Pipeline: selected='{r.selected}'")
else: print(f" βœ— Pipeline: selected='{r.selected}'")
# 17. Speculative draft
total += 1
drafter = GLMDrafter(vocab, crg, zone)
draft = drafter.draft(3)
if len(draft)>=2: passed+=1; print(f" βœ“ Speculative: {[w for w,_ in draft]}")
else: print(f" βœ— Speculative: {draft}")
# 18. KV pruner
total += 1
pruner = CRGKVPruner(vocab, crg)
pruner.update_zone(["photon","energy"])
if pruner.score_token("photon") > pruner.score_token("kitchen"):
passed+=1; print(" βœ“ KV pruner: photon > kitchen")
else: print(" βœ— KV pruner")
print(f"\n Result: {passed}/{total} passed")
if passed == total:
print(" ══════════════════════════════════════")
print(" βœ“ ALL SYSTEMS OPERATIONAL")
print(" ══════════════════════════════════════")
return passed == total
# ════════════════════════════════════════════════════════════════════════════════
# MAIN
# ════════════════════════════════════════════════════════════════════════════════
if __name__ == "__main__":
import argparse
parser = argparse.ArgumentParser(description="Gemma-GLM β€” Geometric Language Machine")
parser.add_argument("--test", action="store_true", help="Run self-test")
parser.add_argument("--api", type=str, help="LLM API URL")
parser.add_argument("--profile", type=str, help="ValueGeometry profile for word")
parser.add_argument("--math", type=str, help="Exact math expression")
parser.add_argument("--code", type=str, help="Run code in sandbox")
parser.add_argument("--pipeline", type=str, help="Run pipeline with text")
parser.add_argument("--draft", type=int, help="Draft n tokens")
parser.add_argument("--crg-path", type=str, default=None, help="CRG persistence path")
args = parser.parse_args()
if args.test:
ok = self_test()
sys.exit(0 if ok else 1)
elif args.profile:
p = token_profile(args.profile)
for k,v in p.items(): print(f" {k}: {v}")
elif args.math:
try:
r = eval(args.math, {"__builtins__":{},"Fraction":F,"math":math})
print(f" = {r}")
if isinstance(r,F): print(f" β‰ˆ {float(r):.10f}")
except Exception as e: print(f" Error: {e}")
elif args.code:
r = run_code(args.code)
if r.success: print(r.stdout)
else: print(f"Error: {r.exception}")
elif args.pipeline:
vocab = SVDVocabulary()
crg = DynamicCRG()
pipe = LLMPipeline(vocab, crg)
candidates = ["electron","boson","radiation","frequency","field",
"mass","energy","quantum","kitchen","umbrella"]
r = pipe.process(args.pipeline, candidates)
print(f" Selected: {r.selected}")
print(f" Vetoed: {[w for w,_ in r.vetoed]}")
print(f" Zone NRCI: {r.zone_nrci:.4f}")
elif args.draft:
vocab = SVDVocabulary()
crg = DynamicCRG()
zone = IdeaZone()
for w in ["photon","energy","light"]: zone.update(w, vocab)
drafter = GLMDrafter(vocab, crg, zone)
draft = drafter.draft(args.draft)
for w,s in draft: print(f" {w:20s} {s:.4f}")
else:
agent = GLMAgent(api_url=args.api, crg_path=args.crg_path)
agent.run()