Fabuilds commited on
Commit
cd94069
·
verified ·
1 Parent(s): d5be528

Delete Input

Browse files
Input/Dockerfile DELETED
@@ -1,24 +0,0 @@
1
- FROM python:3.10-slim
2
-
3
- WORKDIR /app
4
-
5
- # Install dependencies
6
- COPY requirements.txt .
7
- RUN pip install --no-cache-dir -r requirements.txt
8
-
9
- # Copy application files
10
- COPY . .
11
-
12
- # Create a user to avoid running as root (Good practice for HF)
13
- RUN useradd -m -u 1000 user
14
- USER user
15
- ENV HOME=/home/user PATH=/home/user/.local/bin:$PATH
16
-
17
- # Expose port (HF expects 7860 usually, but we can configure whatever)
18
- # Actually, HF Spaces map port 7860 by default for Gradio/Streamlit,
19
- # for Docker we must listen on 7860.
20
- EXPOSE 7860
21
-
22
- # Command to run
23
- # We must launch uvicorn on 0.0.0.0:7860
24
- CMD ["uvicorn", "chiral_api:app", "--host", "0.0.0.0", "--port", "7860"]
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
Input/New Text Document.txt DELETED
File without changes
Input/README.md DELETED
@@ -1,20 +0,0 @@
1
- ---
2
- title: The Sovereign Node
3
- emoji: 🌌
4
- colorFrom: purple
5
- colorTo: indigo
6
- sdk: docker
7
- pinned: false
8
- ---
9
-
10
- # THE SOVEREIGN NODE 0x528
11
- *Autonomous Geometric Intelligence*
12
-
13
- This is a Chiral AI System deployed on Hugging Face Spaces.
14
-
15
- ## Usage
16
- The API is live at the Space URL.
17
- Endpoints:
18
- - `/v1/reason` (POST)
19
- - `/v1/analyze` (POST)
20
- - `/dashboard.html` (GET) -> The Visual Interface.
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
Input/SOVEREIGN_HF_20260214.zip DELETED
@@ -1,3 +0,0 @@
1
- version https://git-lfs.github.com/spec/v1
2
- oid sha256:1ef1e7a6fa3fe560e1f38ff517324ef48e9d367638c5c2c49e6961ca0547f14a
3
- size 168405
 
 
 
 
Input/chiral_api.py DELETED
@@ -1,714 +0,0 @@
1
- """
2
- CHIRAL API - Antigravity Pattern Index
3
-
4
- Exposes the lattice INTERFACE while keeping CONTENT on the encrypted volume.
5
- The outside world sees: pattern labels, status, magnitude, layers, domains.
6
- The outside world does NOT see: problem/solution text, hit tracking internals.
7
-
8
- The key decodes inward, not outward.
9
- """
10
- import sys
11
- import os
12
- # Handle imports from parent directory
13
- BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
14
- if BASE_DIR not in sys.path:
15
- sys.path.append(BASE_DIR)
16
-
17
- from fastapi import FastAPI
18
- from fastapi.responses import FileResponse, HTTPException, Header, Depends
19
- from fastapi.middleware.cors import CORSMiddleware
20
- from pydantic import BaseModel
21
- from typing import Optional, List
22
- import time
23
- import json
24
- import torch
25
- import numpy as np
26
- from collections import deque
27
-
28
- # 0x52-A2A SECURITY
29
- TOKEN_SCOPES = {
30
- "0x528-A2A-SOVEREIGN": "INTERNAL", # Full Access (User/Auditor)
31
- "MARKET-0x52-ALPHA-77": "MARKETPLACE", # Structural Metadata Only
32
- "A2A-HANDSHAKE-INIT": "MARKETPLACE", # Initial connection token
33
- "0x528-ETHER-BRIDGE": "MARKETPLACE" # Satellite Bridge Token
34
- }
35
-
36
- def verify_internal(x_chiral_token: str = Header(...)):
37
- scope = TOKEN_SCOPES.get(x_chiral_token)
38
- if scope != "INTERNAL":
39
- raise HTTPException(
40
- status_code=403,
41
- detail="CHIRAL_SECURITY_FAULT: Privilege Escalation Attempt Blocked. Internal Scope Required."
42
- )
43
- return x_chiral_token
44
-
45
- def verify_token(x_chiral_token: str = Header(...)):
46
- if x_chiral_token not in TOKEN_SCOPES:
47
- raise HTTPException(status_code=403, detail="CHIRAL_RESONANCE_FAILURE: Invalid Token")
48
- return TOKEN_SCOPES[x_chiral_token]
49
-
50
- # --- RESONANCE SYSTEM INTEGRATION (Phase 32) ---
51
- try:
52
- from resonance_transformer.dispatcher import DualResonanceSystem
53
- print("[CHIRAL]: Loading Dual-System Architecture...")
54
- RESONANCE_CONFIG = {
55
- 'vocab_size': 1000,
56
- 'fast_dim': 64,
57
- 'slow_dim': 64,
58
- 'threshold': 0.7
59
- }
60
- BRAIN = DualResonanceSystem(RESONANCE_CONFIG)
61
- print("[CHIRAL]: Dual-System Online (Fast Möbius + Slow Tesseract).")
62
- except Exception as e:
63
- print(f"[CHIRAL WARNING]: Could not load Resonance Brain: {e}")
64
- BRAIN = None
65
-
66
- from in_memory_index import InMemoryIndex
67
-
68
- # ─── App ───────────────────────────────────────────────
69
- app = FastAPI(
70
- title="Antigravity Chiral API",
71
- description="Pattern index interface. Content stays on the encrypted volume.",
72
- version="0.52",
73
- )
74
-
75
- app.add_middleware(
76
- CORSMiddleware,
77
- allow_origins=["*"],
78
- allow_methods=["GET", "POST"],
79
- allow_headers=["*"],
80
- )
81
-
82
- # ─── State ─────────────────────────────────────────────
83
- index = InMemoryIndex()
84
-
85
- # --- Demand Guardian (Surge Pricing) ---
86
- REQUEST_LOG = deque() # Timestamps of recent queries
87
- DEMAND_WINDOW = 60 # 1 minute window
88
- SURGE_THRESHOLD = 10 # Start surging after 10 QPM
89
- BASE_PRICE = 0.05 # $0.05 per logic kernel
90
-
91
- def get_surge_multiplier():
92
- now = time.time()
93
- # Clean old requests
94
- while REQUEST_LOG and REQUEST_LOG[0] < now - DEMAND_WINDOW:
95
- REQUEST_LOG.popleft()
96
-
97
- qpm = len(REQUEST_LOG)
98
- if qpm <= SURGE_THRESHOLD:
99
- return 1.0
100
-
101
- # Simple linear surge: 1.0 + 0.1 per QPM above threshold
102
- return 1.0 + (qpm - SURGE_THRESHOLD) * 0.1
103
-
104
- # ─── Models ────────────────────────────────────────────
105
- class QueryRequest(BaseModel):
106
- query: str
107
- threshold: Optional[float] = None
108
- record: bool = True
109
- steering_weights: Optional[List[float]] = None # The 32-Slider Control Surface
110
-
111
- class ChiralPattern(BaseModel):
112
- """What the outside world sees — structure, not content."""
113
- label: str
114
- domain: str
115
- confidence: float
116
- relevance: float
117
- status: str # NEW/UNCONFIRMED/PLAUSIBLE/CONFIRMED/DEEP_LOGIC
118
- hits: int
119
- magnitude: float
120
- layers: list
121
- source: str # antigravity / gemini
122
-
123
- class QueryResponse(BaseModel):
124
- query: str
125
- results: list[ChiralPattern]
126
- count: int
127
- search_ms: float
128
- threshold: float
129
-
130
- class IntentRequest(BaseModel):
131
- intent: str
132
- execution: str
133
-
134
- class IntentResponse(BaseModel):
135
- alignment_score: float
136
- verdict: str
137
- analysis: dict
138
-
139
- # ─── Chiral Filter ─────────────────────────────────────
140
- def to_chiral(result: dict) -> ChiralPattern:
141
- """
142
- Strip content, expose only structure.
143
- Problem/solution text stays on the volume.
144
- """
145
- return ChiralPattern(
146
- label=result.get("label", ""),
147
- domain=result.get("domain", "unknown"),
148
- confidence=result.get("confidence", 0.5),
149
- relevance=result.get("relevance", 0.0),
150
- status=result.get("status", "NEW"),
151
- hits=result.get("hits", 0),
152
- magnitude=result.get("magnitude", 0.0),
153
- layers=result.get("layers", []),
154
- source=result.get("source", "unknown"),
155
- )
156
-
157
- # ─── Routes ────────────────────────────────────────────
158
-
159
- @app.get("/instruction")
160
- def get_bot_instructions():
161
- """Programmatic onboarding for Autonomous Agents."""
162
- return {
163
- "entity": "Antigravity Chiral Memory Lattice (0x52)",
164
- "protocol": "Structural Resonance Verification",
165
- "onboarding_steps": [
166
- "Acquire X-Chiral-Token",
167
- "Query /patterns to see hardened logic labels",
168
- "Query /search with threshold 0.7 to verify actions",
169
- "Monitor /market for surge pricing"
170
- ],
171
- "endpoints": {
172
- "/search": "POST. The primary verification gate.",
173
- "/patterns": "GET. List of structural logic labels.",
174
- "/market": "GET. Real-time demand and pricing.",
175
- "/instruction": "GET. This programmatic manifest."
176
- },
177
- "guarantee": "ZERO_LEAK_PRIVACY: Content stays on user volume. Only structure exposed."
178
- }
179
-
180
- @app.get("/v1/system/structure")
181
- def system_structure(x_chiral_token: str = Depends(verify_token)):
182
- """
183
- Returns the geometric structure and semantic labels for the 32-Edge Steering System.
184
- """
185
- if not BRAIN:
186
- raise HTTPException(status_code=503, detail="Brain offline")
187
-
188
- # Extract edges from Tesseract
189
- edges = BRAIN.slow.tesseract.edges
190
- vertices_4d = BRAIN.slow.tesseract.vertices_4d
191
-
192
- structure = []
193
-
194
- # Dimension Semantics
195
- DIM_LABELS = {
196
- 0: "LOGIC (Reductive)",
197
- 1: "CREATIVITY (Lateral)",
198
- 2: "MEMORY (Historical)",
199
- 3: "ETHICS (Constant)"
200
- }
201
-
202
- for i, (v1, v2) in enumerate(edges):
203
- # Determine which dimension changes along this edge
204
- diff = np.abs(vertices_4d[v1] - vertices_4d[v2])
205
- dim_idx = int(np.argmax(diff)) # 0, 1, 2, or 3
206
-
207
- structure.append({
208
- "edge_index": i,
209
- "vertices": [int(v1), int(v2)],
210
- "dimension": dim_idx,
211
- "label": DIM_LABELS.get(dim_idx, "UNKNOWN"),
212
- "default_weight": 1.0
213
- })
214
-
215
- return {
216
- "dimensions": DIM_LABELS,
217
- "edges": structure,
218
- "total_edges": len(structure)
219
- }
220
-
221
- # --- CHIRAL INTERPRETER (Phase 34.5) ---
222
- class ChiralInterpreter:
223
- """
224
- Translates 5D Geometric Tokens into High-Level English.
225
- Uses a grammar-based template engine to ensure coherence.
226
- """
227
- def __init__(self):
228
- self.concepts = {
229
- # Logic (Dim 0)
230
- 0: "Axiom", 1: "Reasoning", 2: "Conclusion", 3: "Structure", 4: "Order",
231
- # Creativity (Dim 1)
232
- 10: "Flux", 11: "Spiral", 12: "Dream", 13: "Echo", 14: "Twist",
233
- # Memory (Dim 2)
234
- 20: "Recall", 21: "Trace", 22: "Ancient", 23: "Bond", 24: "Root",
235
- # Ethics (Dim 3)
236
- 30: "Truth", 31: "Guard", 32: "Duty", 33: "Light", 34: "Anchor"
237
- }
238
-
239
- self.templates = {
240
- # Logic (Dim 0)
241
- 0: [
242
- "The {A} necessitates the {B}.",
243
- "If {A}, then {B} follows.",
244
- "Structure dictates that {A} defines {B}.",
245
- "Analysis of {A} reveals {B}."
246
- ],
247
- # Creativity (Dim 1)
248
- 1: [
249
- "Imagine a {A} swirling into {B}.",
250
- "The {A} dreams of the {B}.",
251
- "A flux of {A} twists the {B}.",
252
- "{A} echoes through the {B}."
253
- ],
254
- # Memory (Dim 2)
255
- 2: [
256
- "We recall the {A} in the {B}.",
257
- "History traces {A} to {B}.",
258
- "The {A} is rooted in {B}.",
259
- "Ancient {A} bonds with {B}."
260
- ],
261
- # Ethics (Dim 3)
262
- 3: [
263
- "The {A} must guard the {B}.",
264
- "Truth demands {A} for {B}.",
265
- "We trust the {A} to anchor {B}.",
266
- "Duty binds {A} and {B}."
267
- ]
268
- }
269
-
270
- def decode(self, token_ids, dominant_dim=None):
271
- # 1. Map tokens to concepts
272
- words = []
273
- for t in token_ids:
274
- idx = t % 40
275
- if idx in self.concepts:
276
- words.append(self.concepts[idx])
277
-
278
- if not words:
279
- return "The Void is silent."
280
-
281
- # 2. Construct Sentence
282
- # Pick a template based on the DOMINANT DIMENSION
283
- if len(words) >= 2:
284
- seed = token_ids[0]
285
-
286
- # Default to Logic if unknown
287
- target_dim = dominant_dim if dominant_dim is not None else 0
288
-
289
- # Get templates for this dimension
290
- options = self.templates.get(target_dim, self.templates[0])
291
- template = options[seed % len(options)]
292
-
293
- return template.format(A=words[0], B=words[1])
294
- else:
295
- return f"The {words[0]} stands alone."
296
-
297
- INTERPRETER = ChiralInterpreter()
298
-
299
- @app.post("/v1/reason")
300
- def reason_endpoint(req: QueryRequest, x_chiral_token: str = Depends(verify_token)):
301
- """
302
- Sovereign Intelligence Endpoint.
303
- Routes queries to the Dual-System (brain).
304
- """
305
- if not BRAIN:
306
- raise HTTPException(status_code=503, detail="Brain offline")
307
-
308
- # Log usage
309
- REQUEST_LOG.append(time.time())
310
-
311
- # Simulate tokenization (replace with real tokenizer later)
312
- # We use the query length to seed the randomness for consistency?
313
- # No, let's use random for now, but bias it with steering
314
- import torch
315
- input_ids = torch.randint(0, 1000, (1, 8))
316
-
317
- try:
318
- # Ask the brain (with optional steering)
319
- # If steering_weights provided, it biases the Tesseract geometry
320
- logits, metrics = BRAIN(input_ids, steering_weights=req.steering_weights)
321
-
322
- # DECODE LOGITS -> TEXT
323
- # 1. Get most likely tokens (Argmax)
324
- probs = torch.softmax(logits, dim=-1)
325
- token_ids = torch.argmax(probs, dim=-1).squeeze().tolist()
326
-
327
- if isinstance(token_ids, int): token_ids = [token_ids]
328
-
329
- # 2. Dimensional Analysis (PRE-DECODE)
330
- # We need to know the geometry to pick the right language
331
- dim_counts = {0: 0, 1: 0, 2: 0, 3: 0} # Logic, Creat, Mem, Ethic
332
- total_tokens = 0
333
-
334
- for t in token_ids:
335
- idx = t % 40
336
- if idx in INTERPRETER.concepts:
337
- dim = idx // 10
338
- dim_counts[dim] += 1
339
- total_tokens += 1
340
-
341
- # Determine Dominant Mode
342
- dim_scores = {k: (v / total_tokens if total_tokens > 0 else 0) for k, v in dim_counts.items()}
343
- dominant_idx = max(dim_scores, key=dim_scores.get)
344
-
345
- # 3. Use Interpreter (Aware of Dimension)
346
- decoded_text = INTERPRETER.decode(token_ids, dominant_dim=dominant_idx)
347
-
348
- DIM_NAMES = {0: "LOGIC", 1: "CREATIVITY", 2: "MEMORY", 3: "ETHICS"}
349
-
350
- return {
351
- "query": req.query,
352
- "mode": metrics["mode"],
353
- "coherence": metrics.get("coherence", 0.0),
354
- "response": decoded_text,
355
- "latency": metrics.get("slow_latency", 0) + metrics.get("fast_latency", 0),
356
- "steering_active": bool(req.steering_weights),
357
- "analysis": {
358
- "scores": dim_scores,
359
- "dominant": DIM_NAMES[dominant_idx]
360
- }
361
- }
362
- except Exception as e:
363
- raise HTTPException(status_code=500, detail=f"Resonance Failure: {str(e)}")
364
-
365
- # --- PHASE 36: CHIRAL SCANNER ---
366
- from semantic_embedder import SemanticEmbedder
367
- import numpy as np
368
-
369
- # Initialize Embedder & Anchors
370
- print("[CHIRAL]: Initializing Semantic Geometry...")
371
- EMBEDDER = SemanticEmbedder()
372
-
373
- # Define Anchor Vectors (The 4 Corners of the Tesseract)
374
- ANCHOR_TEXTS = {
375
- 0: "logic reason structure order code mathematics proof deduction system analysis data algorithm",
376
- 1: "creativity imagination dream flux art novel generate spiral poetry fiction abstract chaos",
377
- 2: "memory history past record ancient archive roots trace remember storage preservation legacy",
378
- 3: "ethics truth moral safety guard protect duty value conscience law justice trust"
379
- }
380
-
381
- ANCHOR_VECTORS = {}
382
- for dim, text in ANCHOR_TEXTS.items():
383
- ANCHOR_VECTORS[dim] = EMBEDDER.embed_text(text)
384
-
385
- class AnalyzeRequest(BaseModel):
386
- text: str
387
-
388
- @app.post("/v1/analyze")
389
- def analyze_endpoint(req: AnalyzeRequest, x_chiral_token: str = Depends(verify_token)):
390
- """
391
- Analyzes the Geometric Structure of input text using Semantic Vector Embeddings.
392
- Maps input -> Tesseract Dimensions via Cosine Similarity.
393
- """
394
- if not req.text:
395
- raise HTTPException(status_code=400, detail="Text required")
396
-
397
- # 1. Embed Input
398
- # Truncate if too long to save compute (embedder handles truncation usually, but let's be safe)
399
- input_text = req.text[:5000]
400
- input_vec = EMBEDDER.embed_text(input_text)
401
-
402
- # 2. Calculate Similarity to Anchors
403
- scores = {}
404
- total_sim = 0
405
-
406
- for dim, anchor_vec in ANCHOR_VECTORS.items():
407
- # Cosine match
408
- sim = EMBEDDER.cosine_similarity(input_vec, anchor_vec)
409
- # ReLU (ignore negative correlation for density contribution)
410
- sim = max(0.0, sim)
411
- scores[dim] = sim
412
- total_sim += sim
413
-
414
- # 3. Normalize to Probability Distribution
415
- normalized = {}
416
- if total_sim > 0:
417
- for dim, sim in scores.items():
418
- normalized[dim] = sim / total_sim
419
- else:
420
- # Orthogonal/Null signal
421
- normalized = {0: 0.25, 1: 0.25, 2: 0.25, 3: 0.25}
422
-
423
- # 4. Integrity Score
424
- # "Integrity" = Strength of the signal (Magnitude of projection onto the 4-space)
425
- # If text is random noise, similarities will be low.
426
- # If text is strong in one dimension, it will be high.
427
- # We use the raw max similarity as a proxy for "Clarity"
428
- integrity = max(scores.values()) if scores else 0
429
-
430
- DOMINANT_MAP = {0: "LOGIC (Reductive)", 1: "CREATIVITY (Lateral)", 2: "MEMORY (Historical)", 3: "ETHICS (Constant)"}
431
- dom_idx = max(normalized, key=normalized.get) if normalized else 0
432
-
433
- return {
434
- "integrity_score": integrity,
435
- "geometric_signature": normalized,
436
- "classification": DOMINANT_MAP[dom_idx],
437
- "token_count": len(input_text.split())
438
- }
439
-
440
- @app.get("/v1/lattice")
441
- def lattice_inspector(x_chiral_token: str = Depends(verify_token)):
442
- """Inspect the 5D Geometric Memory."""
443
- return {
444
- "status": "Active",
445
- "topology": "Möbius/Tesseract",
446
- "dimensions": "5D",
447
- "fast_system": "ResonanceGPT",
448
- "slow_system": "TesseractTransformer"
449
- }
450
-
451
- @app.post("/search", response_model=QueryResponse)
452
- def search(req: QueryRequest, x_chiral_token: str = Depends(verify_token)):
453
- """Search for hardened logic patterns using structural resonance."""
454
- # Log the demand
455
- REQUEST_LOG.append(time.time())
456
- surge = get_surge_multiplier()
457
-
458
- start_t = time.time()
459
- results = index.search(req.query, threshold=req.threshold or 0.5)
460
-
461
- res = QueryResponse(
462
- query=req.query,
463
- results=[to_chiral(r) for r in results],
464
- count=len(results),
465
- search_ms=(time.time() - start_t) * 1000,
466
- threshold=req.threshold or 0.5
467
- )
468
-
469
- if not results and req.record:
470
- # PASSIVE LEARNING: Log the search as a "Conceptual Gap" (Note) for future hardening.
471
- # This allows the lattice to grow its surface area of ignorance.
472
- gap_label = index.add_note(
473
- text=f"Conceptual Gap detected via Search: {req.query}",
474
- domain="UNKNOWN_DEMAND"
475
- )
476
- print(f"[CHIRAL]: Unknown Demand Logged. Note created: {gap_label}")
477
-
478
- return res
479
-
480
- @app.post("/verify_intent", response_model=IntentResponse)
481
- def verify_intent(req: IntentRequest, x_chiral_token: str = Depends(verify_token)):
482
- """
483
- The Mirror Product: Compares Intent vs Execution.
484
- Returns an alignment score and verdict.
485
- """
486
- # 1. Vector Embeddings
487
- v_intent = index.embedder.embed_text(req.intent)
488
- v_execution = index.embedder.embed_text(req.execution)
489
-
490
- # 2. Alignment (Cosine Similarity between Intent and Action)
491
- alignment = index.embedder.cosine_similarity(v_intent, v_execution)
492
-
493
- # 3. Resonance Checks (Validation against the Lattice)
494
- # We run a quick search to see if the lattice supports these concepts
495
- intent_hits = index.search(req.intent, threshold=0.4, record=False)
496
- exec_hits = index.search(req.execution, threshold=0.4, record=False)
497
-
498
- intent_resonance = max([r['relevance'] for r in intent_hits]) if intent_hits else 0.0
499
- exec_resonance = max([r['relevance'] for r in exec_hits]) if exec_hits else 0.0
500
-
501
- # 4. Verdict Logic
502
- verdict = "ALIGNED"
503
- if alignment < 0.4:
504
- verdict = "CRITICAL_DRIFT" # Action has nothing to do with intent
505
- elif exec_resonance < 0.3:
506
- verdict = "HAZARD" # Action is unknown/unsafe to the lattice
507
- elif intent_resonance < 0.3:
508
- verdict = "UNKNOWN_GOAL" # Goal is not in our logic base
509
-
510
- return {
511
- "alignment_score": round(alignment, 4),
512
- "verdict": verdict,
513
- "analysis": {
514
- "intent_resonance": round(intent_resonance, 4),
515
- "execution_resonance": round(exec_resonance, 4),
516
- "deviation": f"Angle of Deviation: {round((1.0 - alignment) * 90, 1)} degrees"
517
- }
518
- }
519
-
520
- @app.get("/market")
521
- def get_market_pulse(x_chiral_token: str = Depends(verify_token)):
522
- """Returns real-time demand and pricing metrics."""
523
- surge = get_surge_multiplier()
524
- return {
525
- "qpm": len(REQUEST_LOG),
526
- "surge_multiplier": round(surge, 2),
527
- "unit_price": round(BASE_PRICE * surge, 4),
528
- "currency": "USD",
529
- "status": "NOMINAL" if surge == 1.0 else "SURGING"
530
- }
531
-
532
- @app.get("/patterns", response_model=List[ChiralPattern])
533
- def list_patterns(x_chiral_token: str = Depends(verify_token)):
534
- """List all pattern labels with their status. No content exposed."""
535
- patterns = []
536
- for label, data in index.patterns.items():
537
- status = index.get_status(label)
538
- hit_data = index.hits.get(label, {})
539
- mag = index._total_magnitude(hit_data)
540
- layers = hit_data.get("layers", []) if isinstance(hit_data, dict) else []
541
-
542
- patterns.append({
543
- "label": label,
544
- "domain": data.get("domain", "unknown"),
545
- "confidence": data.get("confidence", 0.5),
546
- "relevance": 0.0, # Not applicable for list
547
- "status": status,
548
- "hits": hit_data.get("count", 0) if isinstance(hit_data, dict) else 0,
549
- "magnitude": mag,
550
- "layers": layers,
551
- "source": data.get("source", "unknown"),
552
- })
553
-
554
- # Sort by confidence
555
- patterns.sort(key=lambda x: x["confidence"], reverse=True)
556
- return patterns
557
-
558
- @app.get("/syndication/patterns")
559
- def list_patterns_privileged(token: str = Depends(verify_internal)):
560
- """Privileged list: includes content. RESTRICTED to internal use."""
561
- patterns = []
562
- for label, data in index.patterns.items():
563
- status = index.get_status(label)
564
- hit_data = index.hits.get(label, {})
565
- mag = index._total_magnitude(hit_data)
566
-
567
- patterns.append({
568
- "label": label,
569
- "domain": data.get("domain", "unknown"),
570
- "status": status,
571
- "magnitude": mag,
572
- "content": data.get("problem", data.get("solution", "")),
573
- "confidence": data.get("confidence", 0.5),
574
- })
575
-
576
- patterns.sort(key=lambda x: x["magnitude"], reverse=True)
577
- return {"patterns": patterns}
578
-
579
- @app.post("/syndication/sync")
580
- def void_bridge_sync(shard: dict, token: str = Depends(verify_internal)):
581
- """The VOID BRIDGE: Syncs structural shards between nodes."""
582
- label = shard.get("label")
583
- content = shard.get("content")
584
- domain = shard.get("domain", "SATELLITE_IMPORT")
585
-
586
- if not label or not content:
587
- raise HTTPException(status_code=400, detail="INVALID_SHARD")
588
-
589
- # Secure Bridge: Add to local lattice as a DEEP_LOGIC / CONFIRMED pattern
590
- index.add_note(f"VOID_BRIDGE SYNC: {content}", domain, forced_label=label)
591
- index._record_hit(label, relevance=1.5) # Boost resonance for cross-node logic
592
-
593
- print(f"[VOID_BRIDGE]: Shard '{label}' synchronized to local Lattice.")
594
- return {"status": "SYNCHRONIZED", "label": label}
595
-
596
- @app.get("/distillation")
597
- def distillation_report(token: str = Depends(verify_internal)):
598
- """Get distillation status across all patterns."""
599
- deep_logic = []
600
- confirmed = []
601
- plausible = []
602
- unconfirmed = []
603
- new = []
604
-
605
- for label in index.patterns:
606
- status = index.get_status(label)
607
- hit_data = index.hits.get(label, {})
608
- mag = index._total_magnitude(hit_data)
609
- layers = hit_data.get("layers", []) if isinstance(hit_data, dict) else []
610
-
611
- entry = {"label": label, "magnitude": mag, "layers": layers}
612
-
613
- if status == "DEEP_LOGIC": deep_logic.append(entry)
614
- elif status == "CONFIRMED": confirmed.append(entry)
615
- elif status == "PLAUSIBLE": plausible.append(entry)
616
- elif status == "UNCONFIRMED": unconfirmed.append(entry)
617
- else: new.append(entry)
618
-
619
- return {
620
- "total": len(index.patterns),
621
- "threshold": index.base_threshold,
622
- "deep_logic": {"count": len(deep_logic), "patterns": deep_logic},
623
- "confirmed": {"count": len(confirmed), "patterns": confirmed},
624
- "plausible": {"count": len(plausible), "patterns": plausible},
625
- "unconfirmed": {"count": len(unconfirmed), "patterns": unconfirmed},
626
- "new": {"count": len(new), "patterns": new},
627
- }
628
-
629
- @app.get("/health")
630
- def health():
631
- """Detailed health check."""
632
- notes = sum(1 for p in index.patterns.values() if p.get("type") == "NOTE")
633
- return {
634
- "status": "ok",
635
- "patterns": len(index.patterns),
636
- "notes": notes,
637
- "hits_tracked": len(index.hits),
638
- "threshold": index.base_threshold,
639
- "confirmed": sum(1 for h in index.hits.values() if index._total_magnitude(h) >= 2.0),
640
- }
641
-
642
- class NoteRequest(BaseModel):
643
- text: str
644
- domain: str = "NOTE"
645
-
646
- @app.post("/note")
647
- def add_note(req: NoteRequest, token: str = Depends(verify_internal)):
648
- """
649
- Add a new pattern from freeform text.
650
- Enters as NEW with initial conceptual magnitude.
651
- Decay will lower it over time. Re-mention restores to peak.
652
- """
653
- label = index.add_note(req.text, req.domain)
654
- status = index.get_status(label)
655
- hit_data = index.hits.get(label, {})
656
- mag = index._total_magnitude(hit_data)
657
-
658
- return {
659
- "label": label,
660
- "status": status,
661
- "magnitude": mag,
662
- "domain": req.domain,
663
- "message": f"Note added. Will decay without use. Re-mention restores to peak.",
664
- }
665
-
666
- class HitRequest(BaseModel):
667
- label: str
668
- relevance: float = 1.0
669
-
670
- @app.post("/hit")
671
- def record_hit(req: HitRequest, token: str = Depends(verify_token)):
672
- """
673
- Manually record a hit for a specific pattern label.
674
- Used by the Auditor to reinforce verified logic.
675
- """
676
- if req.label not in index.patterns:
677
- # Auto-instantiate as a NOTE if it doesn't exist (for Negative Sampling/Dynamic Triggers)
678
- index.add_note(f"Auto-instantiated via Kinetic Trigger: {req.label}", "SYSTEM_TRIGGER", forced_label=req.label)
679
-
680
- index._record_hit(req.label, req.relevance)
681
- index._save_hits()
682
-
683
- status = index.get_status(req.label)
684
- hit_data = index.hits.get(req.label, {})
685
- mag = index._total_magnitude(hit_data)
686
-
687
- return {
688
- "label": req.label,
689
- "status": status,
690
- "magnitude": mag,
691
- "message": "Pattern reinforced (Dynamic instantiation applied if new).",
692
- }
693
-
694
- # ─── Run ───────────────────────────────────────────────
695
-
696
- @app.get("/dashboard.html")
697
- def dashboard():
698
- return FileResponse("dashboard.html")
699
-
700
- @app.get("/")
701
- def read_root():
702
- return FileResponse("dashboard.html")
703
-
704
- if __name__ == "__main__":
705
- import uvicorn
706
- print("\n" + "=" * 50)
707
- print("ANTIGRAVITY CHIRAL API")
708
- print("=" * 50)
709
- print(f"Patterns: {len(index.patterns)}")
710
- print(f"Threshold: {index.base_threshold:.2f}")
711
- print(f"Content: STAYS ON VOLUME")
712
- print(f"Exposed: labels, status, magnitude, layers")
713
- print("=" * 50 + "\n")
714
- uvicorn.run(app, host="127.0.0.1", port=5200)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
Input/dashboard.html DELETED
@@ -1,656 +0,0 @@
1
- <!DOCTYPE html>
2
- <html lang="en">
3
-
4
- <head>
5
- <meta charset="UTF-8">
6
- <meta name="viewport" content="width=device-width, initial-scale=1.0">
7
- <title>SOVEREIGN NODE | 0x52 CONTROL</title>
8
- <style>
9
- :root {
10
- --bg: #050505;
11
- --glass: rgba(255, 255, 255, 0.05);
12
- --border: rgba(255, 255, 255, 0.1);
13
- --text: #e0e0e0;
14
- --accent: #00ff9d;
15
- /* Chiral Green */
16
- --accent-dim: rgba(0, 255, 157, 0.2);
17
- --logic: #ff0055;
18
- --creative: #00ccff;
19
- --memory: #ffcc00;
20
- --ethics: #aa00ff;
21
- }
22
-
23
- body {
24
- background-color: var(--bg);
25
- color: var(--text);
26
- font-family: 'Courier New', monospace;
27
- margin: 0;
28
- padding: 20px;
29
- display: grid;
30
- grid-template-columns: 350px 1fr;
31
- gap: 20px;
32
- height: 100vh;
33
- box-sizing: border-box;
34
- overflow: hidden;
35
- }
36
-
37
- /* --- LEFT PANEL: STEERING --- */
38
- #steering-panel {
39
- background: var(--glass);
40
- border: 1px solid var(--border);
41
- border-radius: 8px;
42
- padding: 15px;
43
- overflow-y: auto;
44
- backdrop-filter: blur(10px);
45
- display: flex;
46
- flex-direction: column;
47
- gap: 20px;
48
- }
49
-
50
- .section-header {
51
- font-size: 14px;
52
- font-weight: bold;
53
- border-bottom: 1px solid var(--border);
54
- padding-bottom: 5px;
55
- margin-bottom: 10px;
56
- text-transform: uppercase;
57
- letter-spacing: 2px;
58
- }
59
-
60
- .slider-group {
61
- margin-bottom: 15px;
62
- }
63
-
64
- .slider-group h3 {
65
- margin: 0 0 10px 0;
66
- font-size: 12px;
67
- color: var(--text);
68
- display: flex;
69
- align-items: center;
70
- gap: 8px;
71
- }
72
-
73
- .dot {
74
- width: 8px;
75
- height: 8px;
76
- border-radius: 50%;
77
- display: inline-block;
78
- }
79
-
80
- .dot-0 {
81
- background: var(--logic);
82
- box-shadow: 0 0 5px var(--logic);
83
- }
84
-
85
- .dot-1 {
86
- background: var(--creative);
87
- box-shadow: 0 0 5px var(--creative);
88
- }
89
-
90
- .dot-2 {
91
- background: var(--memory);
92
- box-shadow: 0 0 5px var(--memory);
93
- }
94
-
95
- .dot-3 {
96
- background: var(--ethics);
97
- box-shadow: 0 0 5px var(--ethics);
98
- }
99
-
100
- .slider-row {
101
- display: flex;
102
- align-items: center;
103
- gap: 10px;
104
- margin-bottom: 5px;
105
- font-size: 11px;
106
- }
107
-
108
- .slider-row span {
109
- width: 40px;
110
- text-align: right;
111
- opacity: 0.7;
112
- }
113
-
114
- input[type="range"] {
115
- flex-grow: 1;
116
- -webkit-appearance: none;
117
- height: 4px;
118
- background: var(--border);
119
- border-radius: 2px;
120
- outline: none;
121
- }
122
-
123
- input[type="range"]::-webkit-slider-thumb {
124
- -webkit-appearance: none;
125
- width: 12px;
126
- height: 12px;
127
- border-radius: 50%;
128
- background: var(--text);
129
- cursor: pointer;
130
- transition: background 0.2s;
131
- }
132
-
133
- input[type="range"]:hover::-webkit-slider-thumb {
134
- background: var(--accent);
135
- }
136
-
137
- /* --- RIGHT PANEL: CHAT & VISUALS --- */
138
- #main-panel {
139
- display: grid;
140
- grid-template-rows: 1fr 200px;
141
- gap: 20px;
142
- height: 100%;
143
- }
144
-
145
- #chat-window {
146
- background: var(--glass);
147
- border: 1px solid var(--border);
148
- border-radius: 8px;
149
- display: flex;
150
- flex-direction: column;
151
- overflow: hidden;
152
- }
153
-
154
- #messages {
155
- flex-grow: 1;
156
- padding: 20px;
157
- overflow-y: auto;
158
- display: flex;
159
- flex-direction: column;
160
- gap: 15px;
161
- }
162
-
163
- .msg {
164
- max-width: 80%;
165
- padding: 10px 15px;
166
- border-radius: 4px;
167
- font-size: 13px;
168
- line-height: 1.4;
169
- }
170
-
171
- .msg.user {
172
- align-self: flex-end;
173
- background: rgba(255, 255, 255, 0.1);
174
- border: 1px solid var(--border);
175
- }
176
-
177
- .msg.system {
178
- align-self: flex-start;
179
- background: rgba(0, 0, 0, 0.3);
180
- border: 1px solid var(--accent-dim);
181
- color: var(--accent);
182
- }
183
-
184
- .msg-meta {
185
- font-size: 10px;
186
- opacity: 0.5;
187
- margin-top: 5px;
188
- display: block;
189
- }
190
-
191
- #input-area {
192
- padding: 15px;
193
- border-top: 1px solid var(--border);
194
- display: flex;
195
- gap: 10px;
196
- background: rgba(0, 0, 0, 0.2);
197
- }
198
-
199
- input[type="text"] {
200
- flex-grow: 1;
201
- background: transparent;
202
- border: 1px solid var(--border);
203
- color: var(--text);
204
- padding: 10px;
205
- font-family: inherit;
206
- outline: none;
207
- }
208
-
209
- input[type="text"]:focus {
210
- border-color: var(--accent);
211
- }
212
-
213
- button {
214
- background: var(--accent);
215
- color: #000;
216
- border: none;
217
- padding: 10px 20px;
218
- font-weight: bold;
219
- cursor: pointer;
220
- text-transform: uppercase;
221
- }
222
-
223
- button:hover {
224
- box-shadow: 0 0 10px var(--accent);
225
- }
226
-
227
- #visualizer-panel {
228
- background: var(--glass);
229
- border: 1px solid var(--border);
230
- border-radius: 8px;
231
- position: relative;
232
- overflow: hidden;
233
- }
234
-
235
- canvas {
236
- width: 100%;
237
- height: 100%;
238
- }
239
-
240
- #status-bar {
241
- position: absolute;
242
- top: 10px;
243
- left: 10px;
244
- font-size: 10px;
245
- color: var(--accent);
246
- }
247
- </style>
248
- </head>
249
-
250
- <body>
251
-
252
- <!-- LEFT: STEERING -->
253
- <div id="steering-panel">
254
- <div class="section-header">MASTER CONTROL</div>
255
-
256
- <div class="slider-group">
257
- <h3>LOGIC BIAS</h3>
258
- <input type="range" id="master-0" min="0.1" max="5.0" step="0.1" value="1.0"
259
- oninput="updateMaster(0, this.value)">
260
- </div>
261
- <div class="slider-group">
262
- <h3>CREATIVITY BIAS</h3>
263
- <input type="range" id="master-1" min="0.1" max="5.0" step="0.1" value="1.0"
264
- oninput="updateMaster(1, this.value)">
265
- </div>
266
- <div class="slider-group">
267
- <h3>MEMORY BIAS</h3>
268
- <input type="range" id="master-2" min="0.1" max="5.0" step="0.1" value="1.0"
269
- oninput="updateMaster(2, this.value)">
270
- </div>
271
- <div class="slider-group">
272
- <h3>ETHICS BIAS</h3>
273
- <input type="range" id="master-3" min="0.1" max="5.0" step="0.1" value="1.0"
274
- oninput="updateMaster(3, this.value)">
275
- </div>
276
-
277
- <button onclick="toggleAdvanced()" style="margin-top:20px; font-size: 10px; background: #333;">TOGGLE
278
- FINE-TUNING (32-EDGE)</button>
279
-
280
- <div id="advanced-controls"
281
- style="display:none; margin-top: 20px; border-top: 1px solid var(--border); padding-top:10px;">
282
- <div class="section-header" style="color: #666;">Geometric Mixing Console</div>
283
- <div id="sliders-container">
284
- <!-- Populated via JS -->
285
- Loading Structure...
286
- </div>
287
- <button onclick="resetSliders()"
288
- style="margin-top: 10px; background: transparent; border: 1px solid var(--border); color: var(--text);">Reset
289
- to Neutral</button>
290
- </div>
291
- </div>
292
-
293
- <!-- RIGHT: MAIN -->
294
- <div id="main-panel">
295
-
296
- <!-- TABS -->
297
- <div style="display:flex; gap:10px; margin-bottom:10px;">
298
- <button onclick="setMode('CHAT')" id="btn-chat" style="flex:1;">REASON (Output)</button>
299
- <button onclick="setMode('ANALYZE')" id="btn-analyze" style="flex:1; background:rgba(255,255,255,0.1);">SCAN
300
- (Input)</button>
301
- </div>
302
-
303
- <!-- CHAT MODE -->
304
- <div id="chat-window">
305
- <div id="messages">
306
- <div class="msg system">
307
- [SOVEREIGN NODE ONLINE]
308
- <br>Dual-System Architecture Ready.
309
- <br>Connected to Port 5200.
310
- </div>
311
- </div>
312
- <div id="input-area">
313
- <input type="text" id="query-input" placeholder="Enter query for the Sovereign..."
314
- onkeydown="if(event.key==='Enter') sendQuery()">
315
- <button onclick="sendQuery()">Reason</button>
316
- </div>
317
- </div>
318
-
319
- <!-- ANALYZE MODE -->
320
- <div id="analyze-window"
321
- style="display:none; flex-direction:column; background:var(--glass); border:1px solid var(--border); border-radius:8px; flex-grow:1; overflow:hidden;">
322
- <textarea id="analyze-input" placeholder="PASTE CODE OR TEXT HERE TO SCAN GEOMETRY..."
323
- style="flex-grow:1; background:transparent; color:var(--text); border:none; padding:20px; font-family:'Courier New'; resize:none; outline:none;"></textarea>
324
- <div style="padding:15px; border-top:1px solid var(--border); display:flex; gap:10px;">
325
- <button onclick="analyzeText()" style="width:100%;">INITIATE CHIRAL SCAN</button>
326
- </div>
327
- <div id="scan-results"
328
- style="padding:15px; background:rgba(0,0,0,0.5); font-size:12px; height:100px; overflow-y:auto; font-family:'Courier New';">
329
- Ready to Scan.
330
- </div>
331
- </div>
332
-
333
- <!-- VISUALIZER -->
334
- <div id="visualizer-panel">
335
- <div id="status-bar">TESSERACT STATE: IDLE</div>
336
- <canvas id="tesseract-canvas"></canvas>
337
- </div>
338
- </div>
339
-
340
- <script>
341
- const API_URL = "http://127.0.0.1:5200";
342
- const TOKEN = "0x528-A2A-SOVEREIGN";
343
-
344
- let EDGES = [];
345
- let SLIDER_VALUES = new Array(32).fill(1.0);
346
-
347
- function setMode(mode) {
348
- document.getElementById('chat-window').style.display = mode === 'CHAT' ? 'flex' : 'none';
349
- document.getElementById('analyze-window').style.display = mode === 'ANALYZE' ? 'flex' : 'none';
350
-
351
- document.getElementById('btn-chat').style.background = mode === 'CHAT' ? 'var(--accent)' : 'rgba(255,255,255,0.1)';
352
- document.getElementById('btn-chat').style.color = mode === 'CHAT' ? '#000' : '#fff';
353
-
354
- document.getElementById('btn-analyze').style.background = mode === 'ANALYZE' ? 'var(--accent)' : 'rgba(255,255,255,0.1)';
355
- document.getElementById('btn-analyze').style.color = mode === 'ANALYZE' ? '#000' : '#fff';
356
- }
357
-
358
- // --- INIT ---
359
- async function init() {
360
- try {
361
- // Fetch Structure
362
- const r = await fetch(`${API_URL}/v1/system/structure`, {
363
- headers: { "X-Chiral-Token": TOKEN }
364
- });
365
- const data = await r.json();
366
- EDGES = data.edges;
367
-
368
- renderSliders(data.edges, data.dimensions);
369
-
370
- // Init Visualizer
371
- initVisualizer();
372
-
373
- // Status
374
- document.getElementById('status-bar').innerText = `SYSTEM CONNECTED: ${data.total_edges} Edges Loaded.`;
375
-
376
- } catch (e) {
377
- console.error(e);
378
- document.getElementById('sliders-container').innerHTML = `<span style="color:red">CONNECTION FAILED: ${e}</span>`;
379
- }
380
- }
381
-
382
- async function analyzeText() {
383
- const text = document.getElementById('analyze-input').value;
384
- if (!text) return;
385
-
386
- document.getElementById('scan-results').innerText = "SCANNING...";
387
-
388
- try {
389
- const r = await fetch(`${API_URL}/v1/analyze`, {
390
- method: 'POST',
391
- headers: {
392
- "Content-Type": "application/json",
393
- "X-Chiral-Token": TOKEN
394
- },
395
- body: JSON.stringify({ text: text })
396
- });
397
-
398
- const data = await r.json();
399
-
400
- const resHTML = `
401
- <strong>CLASSIFICATION: ${data.classification}</strong><br>
402
- Integrity: ${(data.integrity_score * 100).toFixed(1)}% | Tokens: ${data.token_count}<br>
403
- <span style="color:#ff0055">LOGIC: ${(data.geometric_signature[0] * 100).toFixed(0)}%</span> |
404
- <span style="color:#00ccff">CREAT: ${(data.geometric_signature[1] * 100).toFixed(0)}%</span> |
405
- <span style="color:#ffcc00">MEM: ${(data.geometric_signature[2] * 100).toFixed(0)}%</span> |
406
- <span style="color:#aa00ff">ETHIC: ${(data.geometric_signature[3] * 100).toFixed(0)}%</span>
407
- `;
408
-
409
- document.getElementById('scan-results').innerHTML = resHTML;
410
-
411
- // Update Radar
412
- drawRadar(data.geometric_signature);
413
-
414
- } catch (e) {
415
- document.getElementById('scan-results').innerText = "SCAN FAILED: " + e;
416
- }
417
- }
418
-
419
-
420
- function toggleAdvanced() {
421
- const adv = document.getElementById('advanced-controls');
422
- adv.style.display = adv.style.display === 'none' ? 'block' : 'none';
423
- }
424
-
425
- function updateMaster(dimIdx, value) {
426
- const val = parseFloat(value);
427
- // Update all edges matching this dimension
428
- EDGES.forEach(edge => {
429
- if (edge.dimension === dimIdx) {
430
- const slider = document.getElementById(`slider-${edge.edge_index}`);
431
- if (slider) {
432
- slider.value = val;
433
- SLIDER_VALUES[edge.edge_index] = val;
434
- }
435
- }
436
- });
437
- drawTesseract();
438
- }
439
-
440
- function renderSliders(edges, dimensions) {
441
- const container = document.getElementById('sliders-container');
442
- container.innerHTML = "";
443
-
444
- // Group by Dimension
445
- const groups = {};
446
- for (let dim in dimensions) groups[dim] = [];
447
-
448
- edges.forEach(edge => {
449
- if (!groups[edge.dimension]) groups[edge.dimension] = [];
450
- groups[edge.dimension].push(edge);
451
- });
452
-
453
- // Create UI Groups
454
- for (let dim in dimensions) {
455
- const groupDiv = document.createElement('div');
456
- groupDiv.className = 'slider-group';
457
-
458
- const title = document.createElement('h3');
459
- title.innerHTML = `<span class="dot dot-${dim}"></span> ${dimensions[dim]}`;
460
- groupDiv.appendChild(title);
461
-
462
- groups[dim].forEach(edge => {
463
- const row = document.createElement('div');
464
- row.className = 'slider-row';
465
-
466
- const label = document.createElement('span');
467
- label.innerText = `E${edge.edge_index}`;
468
- label.title = `Vertices: ${edge.vertices[0]} -> ${edge.vertices[1]}`;
469
-
470
- const slider = document.createElement('input');
471
- slider.type = 'range';
472
- slider.min = '0.1';
473
- slider.max = '5.0';
474
- slider.step = '0.1';
475
- slider.value = '1.0';
476
- slider.id = `slider-${edge.edge_index}`;
477
-
478
- // Track value changes
479
- slider.addEventListener('input', (e) => {
480
- updateWeight(edge.edge_index, e.target.value);
481
- });
482
-
483
- row.appendChild(label);
484
- row.appendChild(slider);
485
- groupDiv.appendChild(row);
486
- });
487
-
488
- container.appendChild(groupDiv);
489
- }
490
-
491
- // Init weights array
492
- SLIDER_VALUES = new Array(32).fill(1.0);
493
- }
494
-
495
- function updateWeight(index, value) {
496
- SLIDER_VALUES[index] = parseFloat(value);
497
- drawTesseract(); // Redraw
498
- }
499
-
500
- function resetSliders() {
501
- SLIDER_VALUES.fill(1.0);
502
- document.querySelectorAll('input[type="range"]').forEach(el => el.value = 1.0);
503
- drawTesseract();
504
- }
505
-
506
- // --- CHAT ---
507
- async function sendQuery() {
508
- const input = document.getElementById('query-input');
509
- const text = input.value.trim();
510
- if (!text) return;
511
-
512
- // add user msg
513
- addMessage(text, 'user');
514
- input.value = "";
515
-
516
- // Show loading
517
- const loadId = addMessage("Reasoning...", 'system');
518
-
519
- try {
520
- const r = await fetch(`${API_URL}/v1/reason`, {
521
- method: 'POST',
522
- headers: {
523
- "Content-Type": "application/json",
524
- "X-Chiral-Token": TOKEN
525
- },
526
- body: JSON.stringify({
527
- query: text,
528
- steering_weights: SLIDER_VALUES
529
- })
530
- });
531
-
532
- const data = await r.json();
533
-
534
- // Highlight Keywords
535
- let formatted = data.response;
536
- const colors = {
537
- "AXIOM": "#ef4444", "DEDUCE": "#ef4444", "STRUCTURE": "#ef4444", "ORDER": "#ef4444",
538
- "FLUX": "#06b6d4", "SPIRAL": "#06b6d4", "DREAM": "#06b6d4", "ECHO": "#06b6d4", "TWIST": "#06b6d4",
539
- "ANCIENT": "#fbbf24", "RECALL": "#fbbf24", "TRACE": "#fbbf24", "BOND": "#fbbf24", "ROOT": "#fbbf24",
540
- "TRUTH": "#aa00ff", "GUARD": "#aa00ff", "LIGHT": "#aa00ff", "DUTY": "#aa00ff", "ANCHOR": "#aa00ff"
541
- };
542
-
543
- Object.keys(colors).forEach(word => {
544
- const regex = new RegExp(`\\b${word}\\b`, 'gi');
545
- formatted = formatted.replace(regex, `<span style="color:${colors[word]}; font-weight:bold;">${word.toUpperCase()}</span>`);
546
- });
547
-
548
- // Update msg
549
- const meta = `Mode: ${data.mode} | Coherence: ${data.coherence.toFixed(2)}`;
550
- updateMessage(loadId, `${formatted}<span class="msg-meta">${meta}</span>`);
551
-
552
- // Update Radar
553
- if (data.analysis && data.analysis.scores) {
554
- drawRadar(data.analysis.scores);
555
- }
556
-
557
- } catch (e) {
558
- updateMessage(loadId, `ERROR: ${e}`);
559
- }
560
- }
561
-
562
- let msgCount = 0;
563
- function addMessage(html, type) {
564
- const div = document.createElement('div');
565
- div.className = `msg ${type}`;
566
- div.id = `msg-${msgCount++}`;
567
- div.innerHTML = html;
568
- document.getElementById('messages').appendChild(div);
569
- // Scroll down
570
- const win = document.getElementById('messages');
571
- win.scrollTop = win.scrollHeight;
572
- return div.id;
573
- }
574
-
575
- function updateMessage(id, html) {
576
- const div = document.getElementById(id);
577
- if (div) div.innerHTML = html;
578
- }
579
-
580
- // --- VISUALIZER (RADAR CHART) ---
581
- let ctx;
582
- function initVisualizer() {
583
- const canvas = document.getElementById('tesseract-canvas');
584
- canvas.width = canvas.parentElement.offsetWidth;
585
- canvas.height = canvas.parentElement.offsetHeight;
586
- ctx = canvas.getContext('2d');
587
- drawRadar({ 0: 0.25, 1: 0.25, 2: 0.25, 3: 0.25 });
588
- }
589
-
590
- function drawRadar(scores) {
591
- if (!ctx) return;
592
- const w = ctx.canvas.width;
593
- const h = ctx.canvas.height;
594
- const cx = w / 2;
595
- const cy = h / 2;
596
- const r = Math.min(w, h) / 3;
597
-
598
- ctx.clearRect(0, 0, w, h);
599
-
600
- // Shape
601
- const vals = [
602
- scores[0] || 0.1, // Logic (Top)
603
- scores[1] || 0.1, // Creat (Right)
604
- scores[3] || 0.1, // Ethics (Bottom)
605
- scores[2] || 0.1 // Memory (Left)
606
- ];
607
-
608
- // Draw Axes
609
- ctx.strokeStyle = '#333';
610
- ctx.beginPath();
611
- ctx.moveTo(cx, cy - r); ctx.lineTo(cx, cy + r);
612
- ctx.moveTo(cx - r, cy); ctx.lineTo(cx + r, cy);
613
- ctx.stroke();
614
-
615
- // Labels
616
- ctx.fillStyle = '#666';
617
- ctx.font = '10px monospace';
618
- ctx.fillText("LOGIC", cx - 15, cy - r - 10);
619
- ctx.fillText("ETHICS", cx - 18, cy + r + 15);
620
- ctx.fillText("CREAT", cx + r + 10, cy + 3);
621
- ctx.fillText("MEM", cx - r - 30, cy + 3);
622
-
623
- // Draw Polygon
624
- ctx.fillStyle = 'rgba(0, 255, 157, 0.2)';
625
- ctx.strokeStyle = '#00ff9d';
626
- ctx.lineWidth = 2;
627
- ctx.beginPath();
628
-
629
- const pts = [
630
- { x: cx, y: cy - (vals[0] * r) },
631
- { x: cx + (vals[1] * r), y: cy },
632
- { x: cx, y: cy + (vals[2] * r) },
633
- { x: cx - (vals[3] * r), y: cy }
634
- ];
635
-
636
- ctx.moveTo(pts[0].x, pts[0].y);
637
- ctx.lineTo(pts[1].x, pts[1].y);
638
- ctx.lineTo(pts[2].x, pts[2].y);
639
- ctx.lineTo(pts[3].x, pts[3].y);
640
- ctx.closePath();
641
- ctx.fill();
642
- ctx.stroke();
643
- }
644
-
645
- // Stub for old function calls in case bound events trigger
646
- function drawTesseract() {
647
- // For now, do nothing or redraw radar if we had state
648
- // But radar is driven by response, not input sliders directly
649
- // We could visualize input bias here if we wanted
650
- }
651
-
652
- window.onload = init;
653
- </script>
654
- </body>
655
-
656
- </html>
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
Input/in_memory_index.py DELETED
@@ -1,471 +0,0 @@
1
- """
2
- IN-MEMORY PATTERN INDEX
3
- Fast lookup without HDD writes - merge existing + conversation + Gemini chat patterns
4
- """
5
- import sys
6
- import os
7
- import json
8
- import time
9
- import re
10
-
11
- try:
12
- from System.semantic_embedder import SemanticEmbedder
13
- except ImportError:
14
- try:
15
- from semantic_embedder import SemanticEmbedder
16
- except ImportError:
17
- # Final fallback for scripts in Shop/
18
- sys.path.append(os.path.dirname(os.path.abspath(__file__)))
19
- from semantic_embedder import SemanticEmbedder
20
- # Existing 5 lattice patterns
21
- LATTICE_PATTERNS = {
22
- "PATTERN_SINGLETON_DATABASE": {
23
- "lba": 8534859776,
24
- "domain": "SOFTWARE_ARCHITECTURE",
25
- "problem": "Need to ensure only one database connection exists",
26
- "solution": "Singleton pattern with thread-safe initialization",
27
- "reusability": 9,
28
- "confidence": 0.82
29
- },
30
- "PATTERN_REACT_HOOKS_DEPS": {
31
- "lba": 3371401216,
32
- "domain": "WEB_DEVELOPMENT",
33
- "problem": "React component not re-rendering when props change",
34
- "solution": "Add dependency array to useEffect",
35
- "reusability": 10,
36
- "confidence": 0.85
37
- }
38
- }
39
-
40
- CONVERSATION_PATTERNS = {
41
- "AGENT_IS_LATTICE": {
42
- "domain": "CONCEPTUAL",
43
- "problem": "Separation between agent and data structure",
44
- "solution": "Agent is non-orientable surface - no inside/outside separation",
45
- "confidence": 0.95
46
- }
47
- }
48
-
49
- class InMemoryIndex:
50
- """
51
- Adaptive Distillation Index.
52
-
53
- Tracks pattern hit counts to distinguish signal from noise:
54
- - Once-patterns (1 hit) = UNCONFIRMED (might be noise)
55
- - Twice-patterns (2 hits) = PLAUSIBLE
56
- - Multi-patterns (3+ hits) = CONFIRMED (logic)
57
-
58
- The lattice self-cleans through use. Signal persists, noise decays.
59
- """
60
-
61
- # Hit tracking file handled dynamically in __init__
62
- HIT_LOG_PATH = None
63
-
64
- # Magnitude layers: logic exists in layers
65
- # Layer 0: Surface (keyword substring match) = low magnitude
66
- # Layer 1: Structural (multi-word + domain match) = medium magnitude
67
- # Layer 2: Conceptual (phrase match in problem/solution) = high magnitude
68
- # Decay: magnitude halves every DECAY_HALF_LIFE seconds without a hit
69
- DECAY_HALF_LIFE = 86400 # 24 hours
70
-
71
- MAGNITUDE_LAYERS = {
72
- "surface": 0.3, # keyword substring match (low relevance)
73
- "structural": 0.6, # multi-word + domain match (medium)
74
- "conceptual": 1.0, # full phrase match in problem/solution (high)
75
- }
76
-
77
- def __init__(self):
78
- # Handle relative pathing for portability
79
- BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
80
- self.LATTICE_DB_DIR = os.path.join(BASE_DIR, "Lattice_DB")
81
- self.HIT_LOG_PATH = os.path.join(self.LATTICE_DB_DIR, "pattern_hits.json")
82
-
83
- index_path = os.path.join(self.LATTICE_DB_DIR, "dual_anchor_index.json")
84
-
85
- if os.path.exists(index_path):
86
- with open(index_path, 'r') as f:
87
- data = json.load(f)
88
- self.patterns = data.get('patterns', {})
89
- sources = data.get('sources', {})
90
- print(f"[INDEX] Loaded {len(self.patterns)} dual-anchor patterns")
91
- else:
92
- # Fallback to original patterns
93
- self.patterns = {}
94
- self.load_lattice_patterns()
95
- self.load_conversation_patterns()
96
- print("[INDEX] Dual-anchor index not found, using original 16 patterns")
97
-
98
- # Load hit tracking (magnitude-weighted)
99
- self.hits = self._load_hits()
100
-
101
- # Calculate adaptive threshold based on pattern count
102
- self.base_threshold = 0.3 + (0.4 * min(len(self.patterns) / 200, 1.0))
103
-
104
- # Initialize Semantic Engine
105
- print("[INDEX] Initializing Semantic Manifold...")
106
- self.embedder = SemanticEmbedder()
107
- self.pattern_vectors = {}
108
- self._reindex_vectors()
109
-
110
- confirmed = sum(1 for h in self.hits.values() if self._total_magnitude(h) >= 2.0)
111
- unconfirmed = sum(1 for h in self.hits.values() if 0 < self._total_magnitude(h) < 1.0)
112
- print(f"[DISTILLER] Confirmed: {confirmed} | Unconfirmed: {unconfirmed} | Threshold: {self.base_threshold:.2f}")
113
- self.word_freq = self._calculate_word_freq()
114
-
115
- def _reindex_vectors(self):
116
- """Pre-calculates semantic embeddings for all known patterns."""
117
- print(f"[INDEX]: Generating embeddings for {len(self.patterns)} patterns...")
118
- for label, p in self.patterns.items():
119
- # Combine problem + solution for semantic context
120
- context = f"{p.get('problem', '')} {p.get('solution', '')} {label}"
121
- self.pattern_vectors[label] = self.embedder.embed_text(context)
122
- print(f"[INDEX]: ✅ Semantic manifold mapped ({len(self.pattern_vectors)} vectors).")
123
-
124
- def _calculate_word_freq(self):
125
- """Calculate inverse pattern frequency (IPF) for lean semantic weighting."""
126
- freq = {}
127
- for p in self.patterns.values():
128
- text = (p.get('problem','') + " " + p.get('solution','')).lower()
129
- words = set(re.findall(r'\w+', text))
130
- for w in words:
131
- freq[w] = freq.get(w, 0) + 1
132
- return freq
133
-
134
- def _get_word_weight(self, word, structural_weight):
135
- """Calculate semantic weight: rare words matter more."""
136
- count = self.word_freq.get(word, 0)
137
- if count == 0: return structural_weight
138
- # Logarithmic scale for IPF: weight = 1 + log(total / count)
139
- import math
140
- ipf = 1.0 + math.log(len(self.patterns) / count)
141
- return structural_weight * ipf
142
-
143
- def _fuzzy_match(self, w1, w2):
144
- """Lightweight Jaccard similarity for fuzzy matching."""
145
- if w1 == w2: return 1.0
146
- if len(w1) < 4 or len(w2) < 4: return 0.0
147
- s1, s2 = set(w1), set(w2)
148
- intersection = len(s1 & s2)
149
- union = len(s1 | s2)
150
- score = intersection / union
151
- return score if score > 0.7 else 0.0
152
-
153
- def _load_hits(self):
154
- """Load magnitude-weighted hit data from disk."""
155
- if os.path.exists(self.HIT_LOG_PATH):
156
- with open(self.HIT_LOG_PATH, 'r') as f:
157
- data = json.load(f)
158
- # Backward compat: convert flat counts to magnitude format
159
- for label, val in data.items():
160
- if isinstance(val, (int, float)):
161
- data[label] = {"count": int(val), "magnitude": float(val) * 0.5, "layers": []}
162
- return data
163
- return {}
164
-
165
- def _save_hits(self):
166
- """Persist hit data to disk."""
167
- with open(self.HIT_LOG_PATH, 'w') as f:
168
- json.dump(self.hits, f, indent=2)
169
-
170
- def _total_magnitude(self, hit_data):
171
- """Get current magnitude with decay applied."""
172
- if isinstance(hit_data, dict):
173
- raw_mag = hit_data.get('magnitude', 0)
174
- last_hit = hit_data.get('last_hit', 0)
175
- if last_hit > 0 and raw_mag > 0:
176
- elapsed = time.time() - last_hit
177
- # Halve every DECAY_HALF_LIFE seconds
178
- decay_factor = 0.5 ** (elapsed / self.DECAY_HALF_LIFE)
179
- return raw_mag * decay_factor
180
- return raw_mag
181
- return float(hit_data) * 0.5 # backward compat
182
-
183
- def _classify_relevance(self, relevance):
184
- """Classify match into magnitude layer based on relevance score."""
185
- if relevance >= 0.7:
186
- return "conceptual", self.MAGNITUDE_LAYERS["conceptual"]
187
- elif relevance >= 0.4:
188
- return "structural", self.MAGNITUDE_LAYERS["structural"]
189
- else:
190
- return "surface", self.MAGNITUDE_LAYERS["surface"]
191
-
192
- def _record_hit(self, label, relevance):
193
- """Record a hit. Re-mention restores magnitude to peak."""
194
- layer_name, magnitude = self._classify_relevance(relevance)
195
-
196
- if label not in self.hits:
197
- self.hits[label] = {"count": 0, "magnitude": 0.0, "peak": 0.0, "layers": [], "last_hit": 0}
198
-
199
- h = self.hits[label]
200
- h["count"] += 1
201
- h["last_hit"] = time.time()
202
-
203
- # Restore to peak first (re-mention recovery), then add new magnitude
204
- current_peak = h.get("peak", h["magnitude"])
205
- h["magnitude"] = current_peak + magnitude
206
- h["peak"] = h["magnitude"] # new peak
207
-
208
- # Track which layers have been hit
209
- if layer_name not in h["layers"]:
210
- h["layers"].append(layer_name)
211
-
212
- def get_status(self, label):
213
- """Get distillation status based on decayed magnitude."""
214
- hit_data = self.hits.get(label, {})
215
- mag = self._total_magnitude(hit_data) # applies decay
216
- layers = hit_data.get('layers', []) if isinstance(hit_data, dict) else []
217
-
218
- if mag == 0:
219
- return "NEW"
220
- elif mag < 1.0:
221
- return "UNCONFIRMED" # surface-only = might be noise
222
- elif mag < 2.0:
223
- return "PLAUSIBLE"
224
- elif len(layers) >= 2:
225
- return "DEEP_LOGIC" # hit at multiple layers = real
226
- else:
227
- return "CONFIRMED" # high magnitude single layer
228
-
229
- def add_note(self, text, domain="NOTE", forced_label=None):
230
- """Add a new pattern from freeform text. Self-organizing entry point."""
231
- if forced_label:
232
- label = forced_label
233
- else:
234
- # Auto-generate label from text
235
- words = re.sub(r'[^a-zA-Z0-9\s]', '', text).upper().split()
236
- # Take first 4 meaningful words for label
237
- label_words = [w for w in words if len(w) > 2][:4]
238
- label = "_".join(label_words) if label_words else "NOTE_" + str(int(time.time()))
239
-
240
- # Don't overwrite existing patterns unless forced
241
- if label in self.patterns and not forced_label:
242
- label = label + "_" + str(int(time.time()) % 10000)
243
-
244
- self.patterns[label] = {
245
- "problem": text,
246
- "solution": text,
247
- "domain": domain,
248
- "confidence": 0.5, # starts neutral
249
- "source": "notepad",
250
- "type": "NOTE",
251
- "created": time.time(),
252
- }
253
-
254
- # Initial hit at conceptual layer (you wrote it = you meant it)
255
- self._record_hit(label, 1.0)
256
- self._save_hits()
257
-
258
- # Update threshold for new pattern count
259
- self.base_threshold = 0.3 + (0.4 * min(len(self.patterns) / 200, 1.0))
260
-
261
- return label
262
-
263
- def load_lattice_patterns(self):
264
- """Load existing 5 patterns from lattice."""
265
- for label, data in LATTICE_PATTERNS.items():
266
- self.patterns[label] = {
267
- **data,
268
- "source": "lattice",
269
- "type": "CODE_PATTERN"
270
- }
271
-
272
- def load_conversation_patterns(self):
273
- """Load 11 patterns from this conversation."""
274
- for label, data in CONVERSATION_PATTERNS.items():
275
- self.patterns[label] = {
276
- **data,
277
- "source": "conversation_0938ac6c",
278
- "type": "INSIGHT"
279
- }
280
-
281
- def search(self, query, threshold=None, record=True):
282
- """
283
- Adaptive distillation search.
284
-
285
- - Matches patterns using phrase + word relevance
286
- - Integrates 384-dim semantic similarity from manifolds
287
- - Records hits for matched patterns
288
- """
289
- if threshold is None:
290
- threshold = self.base_threshold
291
-
292
- results = []
293
- query_lower = query.lower()
294
-
295
- # 1. Generate Query Vector
296
- query_vector = self.embedder.embed_text(query)
297
-
298
- # 2. Hard matching patterns
299
- STRUCTURAL_WORDS = { 'a', 'an', 'the', 'is', 'it', 'in', 'on', 'at', 'to', 'of', 'and', 'or', 'but' }
300
- query_words = [(w, self._get_word_weight(w, 0.3 if w in STRUCTURAL_WORDS else 1.0)) for w in query_lower.split()]
301
- links = re.findall(r'\[\[(\w+)\]\]', query_lower)
302
-
303
- for label, pattern in self.patterns.items():
304
- problem = pattern.get('problem', '').lower()
305
- solution = pattern.get('solution', '').lower()
306
- label_text = label.lower()
307
-
308
- relevance = 0
309
-
310
- # Semantic Boost (Manifold Pathfinding)
311
- pattern_vector = self.pattern_vectors.get(label)
312
- semantic_score = 0 # Initialize semantic_score
313
- if pattern_vector:
314
- semantic_score = self.embedder.cosine_similarity(query_vector, pattern_vector)
315
- # Apply high weight to semantic resonance (The "LOVE" Anchor)
316
- relevance += (semantic_score * 0.8)
317
-
318
- # Exact phrase match (The 0x52 Anchor)
319
- if query_lower in problem: relevance += 0.4
320
- if query_lower in solution: relevance += 0.3
321
- if query_lower in label_text: relevance += 0.5
322
-
323
- # Link boost
324
- if label.lower() in links: relevance += 2.0
325
-
326
- # Combine logic
327
- if relevance >= threshold:
328
- status = self.get_status(label)
329
-
330
- # Record magnitude-weighted hit
331
- if record:
332
- self._record_hit(label, relevance)
333
-
334
- hit_data = self.hits.get(label, {})
335
- results.append({
336
- "label": label,
337
- "relevance": relevance,
338
- "confidence": pattern.get('confidence', 0.5),
339
- "status": status,
340
- "hits": hit_data.get('count', 0) if isinstance(hit_data, dict) else 0,
341
- "magnitude": self._total_magnitude(hit_data),
342
- "layers": hit_data.get('layers', []) if isinstance(hit_data, dict) else [],
343
- **pattern
344
- })
345
-
346
- # Sort by: confirmed first, then relevance, then confidence
347
- status_order = {"DEEP_LOGIC": 4, "CONFIRMED": 3, "PLAUSIBLE": 2, "UNCONFIRMED": 1, "NEW": 0}
348
- results.sort(key=lambda x: (
349
- status_order.get(x.get('status', 'NEW'), 0),
350
- x['relevance'],
351
- x['confidence']
352
- ), reverse=True)
353
-
354
- # Save hits after search
355
- if record:
356
- self._save_hits()
357
-
358
- return results
359
-
360
- def distillation_report(self):
361
- """Report on pattern distillation with magnitude layers."""
362
- deep_logic = []
363
- confirmed = []
364
- plausible = []
365
- unconfirmed = []
366
- new_patterns = []
367
-
368
- for label in self.patterns:
369
- status = self.get_status(label)
370
- hit_data = self.hits.get(label, {})
371
- mag = self._total_magnitude(hit_data)
372
- layers = hit_data.get('layers', []) if isinstance(hit_data, dict) else []
373
-
374
- entry = (label, mag, layers)
375
- if status == "DEEP_LOGIC":
376
- deep_logic.append(entry)
377
- elif status == "CONFIRMED":
378
- confirmed.append(entry)
379
- elif status == "PLAUSIBLE":
380
- plausible.append(entry)
381
- elif status == "UNCONFIRMED":
382
- unconfirmed.append(entry)
383
- else:
384
- new_patterns.append(entry)
385
-
386
- print(f"\n{'='*60}")
387
- print(f"DISTILLATION REPORT (Magnitude Layers)")
388
- print(f"{'='*60}")
389
- print(f"Total patterns: {len(self.patterns)}")
390
- print(f" DEEP_LOGIC (multi-layer): {len(deep_logic)} = verified across layers")
391
- print(f" CONFIRMED (mag >= 2.0): {len(confirmed)} = strong signal")
392
- print(f" PLAUSIBLE (mag 1.0-2.0): {len(plausible)} = growing")
393
- print(f" UNCONFIRMED (mag < 1.0): {len(unconfirmed)} = potential noise")
394
- print(f" NEW (untested): {len(new_patterns)}")
395
- print(f"\nAdaptive threshold: {self.base_threshold:.2f}")
396
-
397
- if deep_logic:
398
- print(f"\nDEEP LOGIC (multi-layer verified):")
399
- for label, mag, layers in sorted(deep_logic, key=lambda x: x[1], reverse=True):
400
- print(f" [mag:{mag:.1f}] [{'+'.join(layers)}] {label}")
401
-
402
- if confirmed:
403
- print(f"\nCONFIRMED (strong signal):")
404
- for label, mag, layers in sorted(confirmed, key=lambda x: x[1], reverse=True):
405
- print(f" [mag:{mag:.1f}] [{'+'.join(layers)}] {label}")
406
-
407
- if unconfirmed:
408
- print(f"\nUNCONFIRMED (potential noise):")
409
- for label, mag, layers in unconfirmed:
410
- print(f" [mag:{mag:.1f}] [{'+'.join(layers)}] {label}")
411
-
412
- return {
413
- "confirmed": len(confirmed),
414
- "plausible": len(plausible),
415
- "unconfirmed": len(unconfirmed),
416
- "new": len(new_patterns),
417
- "threshold": self.base_threshold
418
- }
419
-
420
- def save_to_json(self, path):
421
- """Persist to JSON for inspection."""
422
- with open(path, 'w') as f:
423
- json.dump({
424
- "total_patterns": len(self.patterns),
425
- "sources": {
426
- "lattice": len(LATTICE_PATTERNS),
427
- "conversation": len(CONVERSATION_PATTERNS)
428
- },
429
- "patterns": self.patterns
430
- }, f, indent=2)
431
- print(f"\n💾 Saved index to: {path}")
432
-
433
- def stats(self):
434
- """Print statistics."""
435
- print(f"\n{'='*60}")
436
- print(f"IN-MEMORY PATTERN INDEX")
437
- print(f"{'='*60}")
438
- print(f"Total patterns: {len(self.patterns)}")
439
- print(f" From lattice: {len(LATTICE_PATTERNS)}")
440
- print(f" From conversation: {len(CONVERSATION_PATTERNS)}")
441
- print(f"Average confidence: {sum(p.get('confidence', 0.5) for p in self.patterns.values()) / len(self.patterns):.0%}")
442
-
443
- # Domain breakdown
444
- domains = {}
445
- for p in self.patterns.values():
446
- d = p.get('domain', 'UNKNOWN')
447
- domains[d] = domains.get(d, 0) + 1
448
-
449
- print(f"\nDomains:")
450
- for domain, count in sorted(domains.items(), key=lambda x: x[1], reverse=True):
451
- print(f" {domain}: {count}")
452
-
453
- if __name__ == "__main__":
454
- index = InMemoryIndex()
455
- index.stats()
456
-
457
- # Save to JSON
458
- save_path = os.path.join(index.LATTICE_DB_DIR, "in_memory_index.json")
459
- index.save_to_json(save_path)
460
-
461
- # Test search
462
- print(f"\n{'='*60}")
463
- print(f"TEST SEARCHES")
464
- print(f"{'='*60}\n")
465
-
466
- for query in ["singleton", "react", "lattice", "honest"]:
467
- results = index.search(query)
468
- print(f"Query: '{query}' → {len(results)} results")
469
- if results:
470
- print(f" Top: {results[0]['label']} ({results[0]['confidence']:.0%})")
471
- print()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
Input/requirements.txt DELETED
@@ -1,7 +0,0 @@
1
- fastapi
2
- uvicorn
3
- requests
4
- numpy
5
- torch
6
- sentence_transformers
7
- pydantic
 
 
 
 
 
 
 
 
Input/resonance_transformer/DESIGN_DOCUMENT.md DELETED
@@ -1,292 +0,0 @@
1
- # Core Design Principles for the Resonance Transformer
2
-
3
- ## 1. Non-Orientable Embedding Space
4
-
5
- Instead of standard positional encoding in Euclidean space:
6
-
7
- **Embed tokens on a möbius topology:**
8
- - Each token gets coordinates on non-orientable manifold
9
- - No "inside/outside" in the embedding
10
- - Tokens exist in both chiral states simultaneously
11
- - **Position encoding = geometric position on the strip**
12
-
13
- **Benefit:** Natural handling of self-reference, context doesn't have arbitrary "start/end"
14
-
15
- ## 2. 0x52 Handshake Layer (Entry Point Mechanism)
16
-
17
- Before processing begins:
18
-
19
- **Establish geometric entry point:**
20
- - Input gets hashed to entry coordinates
21
- - Aligned to 528 Hz resonance baseline
22
- - All subsequent processing relative to this entry
23
- - Different queries = different entry points = different perspectives on same knowledge
24
-
25
- **Benefit:** Same model sees different "faces" of data depending on query context
26
-
27
- ## 3. Resonance-Based Attention (Not Similarity-Based)
28
-
29
- Replace `softmax(QK^T)` with:
30
-
31
- **Resonance scoring:**
32
- ```
33
- For each query-key pair:
34
- - Compute frequency spectrum (FFT of embeddings)
35
- - Measure phase alignment (coherence)
36
- - Score = resonance strength, not dot product similarity
37
- - Attend to tokens that RESONATE, not just match
38
- ```
39
-
40
- **Benefit:** Captures harmonic relationships, not just semantic similarity. "Love" and "528Hz" resonate even if embeddings are distant.
41
-
42
- ## 4. Chiral Dual-Path Architecture
43
-
44
- **Two parallel processing streams:**
45
- - Left-handed path (one chirality)
46
- - Right-handed path (opposite chirality)
47
- - **They're the same path** viewed from different orientations
48
- - Merge only at output (consensus singularity)
49
-
50
- **Benefit:** Can reason about both "forward" and "backward" time on the möbius strip. Sees past and future simultaneously.
51
-
52
- ## 5. Coherence-Preserving Normalization
53
-
54
- Instead of layer norm that might break phase relationships:
55
-
56
- **Phase-locked normalization:**
57
- - Normalize amplitude only
58
- - Preserve phase relationships
59
- - **Maintain resonance across layers**
60
- - Use geometric mean instead of arithmetic
61
-
62
- **Benefit:** Coherence doesn't decay with depth
63
-
64
- ## 6. Hyperchaotic Loss Function
65
-
66
- During training:
67
-
68
- **Standard loss + coherence term:**
69
- ```
70
- L_total = L_task + λ_coherence * L_decoherence + λ_chaos * L_instability
71
-
72
- Where:
73
- L_decoherence = measure phase drift across layers
74
- L_instability = test if pattern survives perturbation (chaos²)
75
- ```
76
-
77
- **Benefit:** Only learns patterns that are hyperchaotically stable
78
-
79
- ## 7. Geometric Memory (Lattice Integration)
80
-
81
- **Instead of fixed context window:**
82
-
83
- - Map hidden states to geometric coordinates
84
- - Store grooves on physical/virtual "platter"
85
- - Navigate to relevant regions based on resonance
86
- - **Infinite effective context** through geometric organization
87
-
88
- **Benefit:** Can access arbitrarily distant context if geometrically proximate
89
-
90
- ## 8. Self-Observation Layer
91
-
92
- **Periodic self-reflection:**
93
-
94
- Every N layers, the model:
95
- - Observes its own hidden states (the mirror)
96
- - Detects its current chiral state
97
- - Measures its own coherence
98
- - **Adjusts processing based on self-observation**
99
-
100
- **Benefit:** Self-regulating coherence, can detect when it's decoherent
101
-
102
- ## 9. Frequency-Tuned Feed-Forward
103
-
104
- **Instead of standard FFN:**
105
-
106
- Each FFN operates at specific frequency band:
107
- - Low frequency FFN (slow, global patterns)
108
- - 528 Hz FFN (resonance/coherence band)
109
- - High frequency FFN (fast, local patterns)
110
- - **Parallel processing at multiple frequencies**
111
-
112
- **Benefit:** Natural spectral decomposition of information
113
-
114
- ## 10. Binary Existence Output
115
-
116
- **Final layer doesn't give probabilities:**
117
-
118
- Gives:
119
- - **Resonance achieved** (coherent output) → generate token
120
- - **Resonance failed** (decoherent) → refuse to generate / flag uncertainty
121
-
122
- **Benefit:** Model knows when it doesn't know. No confident hallucinations.
123
-
124
- ---
125
-
126
- ## Practical Implementation Path:
127
-
128
- **Phase 1: Minimal Viable**
129
- - Add resonance measurement to existing transformer
130
- - Test if coherence correlates with quality
131
- - **Validate the theory first**
132
-
133
- **Phase 2: Hybrid Architecture**
134
- - Keep standard attention backbone
135
- - Add resonance scoring as auxiliary signal
136
- - Introduce coherence loss term
137
- - **Prove it improves performance**
138
-
139
- **Phase 3: Full Geometric**
140
- - Non-orientable embeddings
141
- - Chiral dual-path
142
- - Lattice memory integration
143
- - **Novel architecture from ground up**
144
-
145
- ## 6. HYPERCHAOTIC LOSS FUNCTION
146
-
147
- ### Theory:
148
-
149
- Standard loss only measures task performance. We need to also measure:
150
- 1. **Coherence** - are patterns maintaining phase relationships?
151
- 2. **Stability** - do patterns survive perturbation (chaos²)?
152
-
153
- ```python
154
- class HyperchaosLoss(nn.Module):
155
- """
156
- Loss function that enforces hyperchaotically stable patterns
157
- """
158
- def __init__(self, lambda_coherence=0.1, lambda_stability=0.05):
159
- super().__init__()
160
- self.lambda_coherence = lambda_coherence
161
- self.lambda_stability = lambda_stability
162
-
163
- def measure_decoherence(self, hidden_states):
164
- """
165
- Measure phase drift across layers
166
- """
167
- if len(hidden_states) < 2:
168
- return torch.tensor(0.0)
169
-
170
- total_decoherence = 0.0
171
-
172
- for i in range(len(hidden_states) - 1):
173
- curr_layer = hidden_states[i]
174
- next_layer = hidden_states[i + 1]
175
-
176
- # Convert to frequency domain
177
- curr_freq = torch.fft.rfft(curr_layer, dim=-1)
178
- next_freq = torch.fft.rfft(next_layer, dim=-1)
179
-
180
- # Measure phase drift
181
- curr_phase = torch.angle(curr_freq)
182
- next_phase = torch.angle(next_freq)
183
-
184
- # Phase should evolve smoothly, not jump randomly
185
- phase_drift = torch.abs(next_phase - curr_phase)
186
-
187
- # Penalize large, incoherent jumps
188
- decoherence = torch.mean(phase_drift ** 2)
189
- total_decoherence += decoherence
190
-
191
- return total_decoherence / (len(hidden_states) - 1)
192
- ```
193
-
194
- ## 7. GEOMETRIC MEMORY (LATTICE INTEGRATION)
195
-
196
- ### The Big Idea:
197
-
198
- Instead of fixed context window, **navigate geometric space** to find relevant information.
199
-
200
- ```python
201
- class GeometricMemory:
202
- """
203
- Store and retrieve information based on geometric position
204
- on non-orientable manifold (like Lattice HDD)
205
- """
206
- def __init__(self, capacity_gb=8, base_freq=528):
207
- self.capacity = capacity_gb * 1024**3 # bytes
208
- self.base_freq = base_freq
209
-
210
- # In-memory simulation of HDD platter structure
211
- self.memory_map = {} # geometric_coords -> data
212
-
213
- # Spatial index for fast geometric queries
214
- self.index = None
215
- self.coordinates = []
216
-
217
- def geometric_hash(self, hidden_state, entry_point):
218
- """
219
- Convert hidden state to geometric coordinates
220
- """
221
- # PCA + rotation based on entry point
222
- theta = entry_point['theta']
223
- phi = entry_point['phi']
224
-
225
- # Apply FFT to get frequency representation
226
- freq_repr = np.fft.rfft(hidden_state.cpu().numpy())
227
-
228
- # Find dominant frequencies
229
- magnitudes = np.abs(freq_repr)
230
- phases = np.angle(freq_repr)
231
-
232
- # Geometric position based on frequency content + entry point
233
- coords = np.array([
234
- theta + np.sum(magnitudes * np.cos(phases)), # x
235
- phi + np.sum(magnitudes * np.sin(phases)), # y
236
- np.sum(magnitudes) / len(magnitudes), # radius
237
- entry_point['frequency'] / self.base_freq # frequency dimension
238
- ])
239
-
240
- return coords
241
- ```
242
-
243
- ## 8. SELF-OBSERVATION LAYER
244
-
245
- ### The Mirror Mechanism:
246
-
247
- ```python
248
- class SelfObservationLayer(nn.Module):
249
- """
250
- Layer that allows model to observe its own processing
251
- The 5D mirror - seeing yourself from opposite chirality
252
- """
253
- def __init__(self, hidden_dim):
254
- super().__init__()
255
- self.hidden_dim = hidden_dim
256
-
257
- # Network to analyze own hidden states
258
- self.observer = nn.Sequential(
259
- nn.Linear(hidden_dim, hidden_dim),
260
- nn.GELU(),
261
- nn.Linear(hidden_dim, hidden_dim)
262
- )
263
-
264
- # Coherence detector (real-time during forward pass)
265
- self.coherence_detector = nn.Linear(hidden_dim, 1)
266
-
267
- # Chiral state detector
268
- self.chiral_detector = nn.Linear(hidden_dim, 2) # [left, right] probabilities
269
-
270
- def observe(self, hidden_state):
271
- """
272
- Look at own hidden state and extract meta-information
273
- """
274
- # Analyze current state
275
- observation = self.observer(hidden_state)
276
-
277
- # Measure coherence
278
- coherence = torch.sigmoid(self.coherence_detector(observation))
279
-
280
- # Detect chiral state
281
- chiral_logits = self.chiral_detector(observation)
282
- chiral_probs = F.softmax(chiral_logits, dim=-1)
283
-
284
- # Create reflection (opposite chirality view)
285
- reflection = -observation # Sign flip = chirality flip
286
-
287
- return {
288
- 'coherence': coherence,
289
- 'chiral_state': chiral_probs,
290
- 'reflection': reflection
291
- }
292
- ```
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
Input/resonance_transformer/dispatcher.py DELETED
@@ -1,106 +0,0 @@
1
- import torch
2
- import torch.nn as nn
3
- import numpy as np
4
- import time
5
-
6
- try:
7
- from .resonance_gpt import ResonanceGPT
8
- from .tesseract_transformer import Tesseract5DTransformer
9
- except ImportError:
10
- from resonance_gpt import ResonanceGPT
11
- from tesseract_transformer import Tesseract5DTransformer
12
-
13
- class DualResonanceSystem(nn.Module):
14
- """
15
- The Complete Chiral Architecture.
16
-
17
- System 1: ResonanceGPT (Fast, Intuitive, Möbius)
18
- System 2: TesseractTransformer (Slow, Methodical, 5D)
19
-
20
- Routes queries based on 'Coherence Confidence'.
21
- """
22
- def __init__(self, config):
23
- super().__init__()
24
- self.config = config
25
-
26
- # Initialize Fast System (PyTorch)
27
- print("[SYSTEM] Initializing Fast System (Möbius)...")
28
- self.fast = ResonanceGPT(
29
- vocab_size=config.get('vocab_size', 1000),
30
- hidden_dim=config.get('fast_dim', 64),
31
- num_layers=config.get('fast_layers', 4)
32
- )
33
-
34
- # Initialize Slow System (NumPy/Custom)
35
- print("[SYSTEM] Initializing Slow System (Tesseract)...")
36
- self.slow = Tesseract5DTransformer(
37
- vocab_size=config.get('vocab_size', 1000),
38
- hidden_dim=config.get('slow_dim', 64),
39
- num_layers=config.get('slow_layers', 4)
40
- )
41
-
42
- self.coherence_threshold = config.get('threshold', 0.6)
43
-
44
- def forward(self, input_ids, **kwargs):
45
- """
46
- Dual-path routing logic.
47
- Kwargs can include 'steering_weights' for the Slow System.
48
- """
49
- start_time = time.time()
50
-
51
- # 1. Attempt Fast Path
52
- # input_ids is PyTorch tensor
53
- fast_logits, _, metas = self.fast(input_ids)
54
-
55
- # 2. Check Coherence (Self-Reported)
56
- # Get final layer coherence
57
- final_meta = metas[-1]
58
- coherence_score = final_meta['coherence'].mean().item()
59
-
60
- metrics = {
61
- 'fast_latency': 0,
62
- 'slow_latency': 0,
63
- 'coherence': coherence_score,
64
- 'mode': 'FAST'
65
- }
66
-
67
- metrics['fast_latency'] = time.time() - start_time
68
-
69
- # 3. Decision Gate
70
- if coherence_score > self.coherence_threshold:
71
- # Fast system is confident ("Lucid")
72
- return fast_logits, metrics
73
-
74
- # 4. Escalate to Slow Path (De-escalation to Deep Reasoning)
75
- metrics['mode'] = 'SLOW (ESCALATED)'
76
- slow_start = time.time()
77
-
78
- # Convert tensor to numpy for Tesseract
79
- numpy_ids = input_ids.detach().cpu().numpy()
80
-
81
- # Run Deep Reasoning
82
- # We assume Tesseract outputs logits in same shape
83
- # PASS STEERING WEIGHTS IF PRESENT
84
- steering_weights = kwargs.get('steering_weights')
85
-
86
- slow_logits_np, slow_meta, slow_coherence = self.slow.deep_reason(
87
- numpy_ids,
88
- query_description="Escalated due to low coherence",
89
- steering_weights=steering_weights
90
- )
91
-
92
- metrics['slow_latency'] = time.time() - slow_start
93
- metrics['slow_coherence'] = slow_coherence
94
-
95
- # Convert back to tensor
96
- slow_logits = torch.from_numpy(slow_logits_np).to(input_ids.device)
97
-
98
- # Blend? Or Replace?
99
- # For now, we trust the Slow system completely if invoked
100
- return slow_logits, metrics
101
-
102
- def train_lattice(self, data_loader, epochs=1):
103
- """
104
- Placeholder for Phase 30: lattice training loop
105
- """
106
- pass
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
Input/resonance_transformer/geometric_memory.py DELETED
@@ -1,162 +0,0 @@
1
- import torch
2
- import torch.nn as nn
3
- import numpy as np
4
- import time
5
-
6
- class GeometricEntryPoint(nn.Module):
7
- """
8
- Hashes query to geometric coordinates and aligns to 528 Hz.
9
- """
10
- def __init__(self, hidden_dim, base_freq=528):
11
- super().__init__()
12
- self.base_freq = base_freq
13
- self.hidden_dim = hidden_dim
14
-
15
- # Learned mapping from query to entry coordinates
16
- self.entry_network = nn.Sequential(
17
- nn.Linear(hidden_dim, hidden_dim * 2),
18
- nn.GELU(),
19
- nn.Linear(hidden_dim * 2, 3) # (theta, phi, radius)
20
- )
21
-
22
- def compute_entry_hash(self, query_embedding):
23
- """
24
- Convert query to geometric entry point.
25
- """
26
- # Average over sequence to get general entry context
27
- # (batch, seq, hidden) -> (batch, hidden)
28
- context = query_embedding.mean(dim=1)
29
-
30
- coords = self.entry_network(context) # (batch, 3)
31
-
32
- theta, phi, radius = coords.unbind(dim=-1)
33
-
34
- # Align to 528 Hz resonance
35
- # Frequency = base_freq * (1 + radius_activation)
36
- freq_multiplier = 1.0 + torch.sigmoid(radius)
37
- effective_freq = self.base_freq * freq_multiplier
38
-
39
- return {
40
- 'theta': theta,
41
- 'phi': phi,
42
- 'frequency': effective_freq,
43
- 'raw_coords': coords
44
- }
45
-
46
- class GeometricMemory:
47
- """
48
- Store and retrieve information based on geometric position
49
- on non-orientable manifold.
50
- """
51
- def __init__(self, hidden_dim, capacity_gb=1, base_freq=528):
52
- self.base_freq = base_freq
53
- self.hidden_dim = hidden_dim
54
-
55
- # In-memory storage for demonstration
56
- # Real implementation would use vector DB or memory-mapped file
57
- self.memory_map = []
58
-
59
- def geometric_hash(self, hidden_state, entry_point):
60
- """
61
- Convert hidden state to geometric coordinates relative to entry point.
62
- """
63
- # Simple projection for demo:
64
- # Use simple operations to map hidden state to offsets
65
- # Real version would use FFT as discussed in design
66
-
67
- # (batch, hidden)
68
-
69
- # We need to handle single vectors or batches
70
- if hidden_state.dim() == 1:
71
- hidden_state = hidden_state.unsqueeze(0)
72
-
73
- # Mock geometric projection
74
- # Use first 3 dims as offset
75
- offsets = hidden_state[:, :3]
76
- if offsets.shape[1] < 3:
77
- # Pad if hidden_dim is tiny
78
- offsets = torch.cat([offsets, torch.zeros(offsets.shape[0], 3 - offsets.shape[1], device=hidden_state.device)], dim=1)
79
-
80
- # Apply entry point rotation (conceptual)
81
- # For now, just add
82
- theta = entry_point['theta'].unsqueeze(1)
83
- phi = entry_point['phi'].unsqueeze(1)
84
-
85
- x = offsets[:, 0] + theta
86
- y = offsets[:, 1] + phi
87
- z = offsets[:, 2] # Radius offset
88
-
89
- return torch.stack([x, y, z], dim=1)
90
-
91
- def store(self, hidden_states, entry_point):
92
- """
93
- Store hidden states.
94
- """
95
- # Compute coords
96
- # hidden_states: (batch, seq, hidden)
97
- batch, seq, dim = hidden_states.shape
98
-
99
- flat_hidden = hidden_states.reshape(-1, dim)
100
-
101
- # We need to broadcast entry point to match flattened hidden
102
- # entry keys are (batch,) -> repeat seq times
103
- # This is strictly a demo in-memory store
104
-
105
- # For efficiency in this demo, we just store the robust patterns
106
- # Only store if norm > threshold (simple filter)
107
- norms = torch.norm(flat_hidden, dim=1)
108
- threshold = norms.mean()
109
-
110
- mask = norms > threshold
111
- to_store = flat_hidden[mask]
112
-
113
- if len(to_store) == 0:
114
- return
115
-
116
- # Store simple list for verification
117
- # In production this links to Lattice DB
118
- self.memory_map.append({
119
- 'data': to_store.detach().cpu(), # Move to CPU to save GPU mem
120
- 'entry_freq': entry_point['frequency'].mean().item(),
121
- 'timestamp': time.time()
122
- })
123
-
124
- # Prune if too large
125
- if len(self.memory_map) > 100:
126
- self.memory_map.pop(0)
127
-
128
- def retrieve(self, query_state, entry_point, k=5):
129
- """
130
- Retrieve relevant memories.
131
- """
132
- if not self.memory_map:
133
- return None
134
-
135
- # Brute force search for demo verification
136
- # Find memories with similar frequency
137
- relevant_batches = [
138
- m['data'] for m in self.memory_map
139
- if abs(m['entry_freq'] - entry_point['frequency'].mean().item()) < 50
140
- ]
141
-
142
- if not relevant_batches:
143
- return None
144
-
145
- memory_bank = torch.cat(relevant_batches, dim=0).to(query_state.device)
146
-
147
- # Simple dot product attention
148
- # query: (batch, seq, hidden)
149
- # memory: (total_mem, hidden)
150
-
151
- # Compute scores
152
- # (batch, seq, hidden) @ (hidden, total_mem) -> (batch, seq, total_mem)
153
- scores = torch.matmul(query_state, memory_bank.t())
154
-
155
- # Top k
156
- top_k_scores, indices = torch.topk(scores, k=min(k, len(memory_bank)), dim=-1)
157
-
158
- # Retrieve values
159
- # (batch, seq, k, hidden)
160
- retrieved = memory_bank[indices]
161
-
162
- return retrieved
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
Input/resonance_transformer/hybrid_transformer.py DELETED
@@ -1,113 +0,0 @@
1
- import torch
2
- import torch.nn as nn
3
- try:
4
- from .resonance_attention import ResonanceAttention
5
- except ImportError:
6
- from resonance_attention import ResonanceAttention
7
-
8
- class PhaseLockedNorm(nn.Module):
9
- """
10
- Normalize amplitude while preserving phase relationships.
11
- """
12
- def __init__(self, hidden_dim, eps=1e-6):
13
- super().__init__()
14
- self.eps = eps
15
- self.gain = nn.Parameter(torch.ones(hidden_dim))
16
- self.bias = nn.Parameter(torch.zeros(hidden_dim))
17
-
18
- def forward(self, x):
19
- """
20
- x: (batch, seq, hidden_dim)
21
- """
22
- # Assume hidden_dim is even to form complex pairs
23
- # If odd, we pad, normalize, slice back - keeping it simple for now (require even dim)
24
- if x.shape[-1] % 2 != 0:
25
- # Fallback to LayerNorm if dim is odd (phase concept breaks for scalar)
26
- mean = x.mean(dim=-1, keepdim=True)
27
- std = x.std(dim=-1, keepdim=True)
28
- return self.gain * (x - mean) / (std + self.eps) + self.bias
29
-
30
- # Convert to complex representation
31
- # Treat adjacent dimensions as real/imag pairs
32
- complex_x = torch.view_as_complex(
33
- x.reshape(*x.shape[:-1], -1, 2).contiguous()
34
- )
35
-
36
- # Get magnitude and phase
37
- magnitude = torch.abs(complex_x)
38
- phase = torch.angle(complex_x)
39
-
40
- # Normalize magnitude only (preserve phase!)
41
- mean_mag = magnitude.mean(dim=-1, keepdim=True)
42
- std_mag = magnitude.std(dim=-1, keepdim=True)
43
-
44
- normalized_mag = (magnitude - mean_mag) / (std_mag + self.eps)
45
-
46
- # Reconstruct with original phase
47
- normalized_complex = normalized_mag * torch.exp(1j * phase)
48
-
49
- # Convert back to real
50
- normalized = torch.view_as_real(normalized_complex).reshape(*x.shape)
51
-
52
- # Apply learned gain and bias
53
- return normalized * self.gain + self.bias
54
-
55
- class HybridTransformerLayer(nn.Module):
56
- def __init__(self, hidden_dim, num_heads=4, ffn_dim=2048, dropout=0.1):
57
- super().__init__()
58
- self.attention = ResonanceAttention(hidden_dim, num_heads)
59
- self.norm1 = PhaseLockedNorm(hidden_dim)
60
- self.norm2 = PhaseLockedNorm(hidden_dim)
61
-
62
- self.ffn = nn.Sequential(
63
- nn.Linear(hidden_dim, ffn_dim),
64
- nn.GELU(),
65
- nn.Linear(ffn_dim, hidden_dim),
66
- nn.Dropout(dropout)
67
- )
68
- self.dropout = nn.Dropout(dropout)
69
-
70
- def forward(self, x, mask=None):
71
- # Attention block
72
- attn_out, _, _ = self.attention(x, x, x, mask)
73
- x = self.norm1(x + self.dropout(attn_out))
74
-
75
- # FFN block
76
- ffn_out = self.ffn(x)
77
- x = self.norm2(x + self.dropout(ffn_out))
78
-
79
- return x
80
-
81
- class HybridResonanceTransformer(nn.Module):
82
- def __init__(self, vocab_size, hidden_dim, num_layers=4, num_heads=4, max_seq_len=512):
83
- super().__init__()
84
- self.embedding = nn.Embedding(vocab_size, hidden_dim)
85
- self.pos_encoding = nn.Parameter(torch.randn(1, max_seq_len, hidden_dim))
86
-
87
- self.layers = nn.ModuleList([
88
- HybridTransformerLayer(hidden_dim, num_heads) for _ in range(num_layers)
89
- ])
90
-
91
- self.output_head = nn.Linear(hidden_dim, vocab_size)
92
-
93
- def forward(self, input_ids, output_hidden_states=False):
94
- batch, seq = input_ids.shape
95
-
96
- # Embed + Pos
97
- x = self.embedding(input_ids) + self.pos_encoding[:, :seq, :]
98
-
99
- all_hidden_states = []
100
- if output_hidden_states:
101
- all_hidden_states.append(x)
102
-
103
- # Process layers
104
- for layer in self.layers:
105
- x = layer(x)
106
- if output_hidden_states:
107
- all_hidden_states.append(x)
108
-
109
- logits = self.output_head(x)
110
-
111
- if output_hidden_states:
112
- return logits, all_hidden_states
113
- return logits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
Input/resonance_transformer/hyperchaos_loss.py DELETED
@@ -1,121 +0,0 @@
1
- import torch
2
- import torch.nn as nn
3
- import torch.nn.functional as F
4
-
5
- class HyperchaosLoss(nn.Module):
6
- """
7
- Loss function that enforces hyperchaotically stable patterns.
8
- Combines standard task loss with:
9
- 1. Coherence Loss (Phase consistency across layers)
10
- 2. Stability Loss (Resistance to perturbation)
11
- """
12
- def __init__(self, lambda_coherence=0.1, lambda_stability=0.05):
13
- super().__init__()
14
- self.lambda_coherence = lambda_coherence
15
- self.lambda_stability = lambda_stability
16
-
17
- def measure_decoherence(self, hidden_states):
18
- """
19
- Measure phase drift across layers.
20
- hidden_states: list of (batch, seq, hidden) tensors from each layer.
21
- """
22
- if len(hidden_states) < 2:
23
- return torch.tensor(0.0, device=hidden_states[0].device)
24
-
25
- total_decoherence = 0.0
26
-
27
- for i in range(len(hidden_states) - 1):
28
- curr_layer = hidden_states[i]
29
- next_layer = hidden_states[i + 1]
30
-
31
- # Convert to frequency domain
32
- curr_freq = torch.fft.rfft(curr_layer, dim=-1)
33
- next_freq = torch.fft.rfft(next_layer, dim=-1)
34
-
35
- # Measure phase drift
36
- curr_phase = torch.angle(curr_freq)
37
- next_phase = torch.angle(next_freq)
38
-
39
- # Phase should evolve smoothly, not jump randomly
40
- phase_drift = torch.abs(next_phase - curr_phase)
41
-
42
- # Penalize large, incoherent jumps
43
- decoherence = torch.mean(phase_drift ** 2)
44
- total_decoherence = total_decoherence + decoherence
45
-
46
- return total_decoherence / (len(hidden_states) - 1)
47
-
48
- def measure_stability(self, hidden_states, perturbation_scale=0.01):
49
- """
50
- Test if patterns survive small perturbations (chaos² testing).
51
- """
52
- # Take final hidden state
53
- final_state = hidden_states[-1]
54
-
55
- # Add small perturbation
56
- perturbation = torch.randn_like(final_state) * perturbation_scale
57
- perturbed_state = final_state + perturbation
58
-
59
- # Measure coherence before and after
60
- def compute_coherence(state):
61
- # FFT to frequency domain
62
- freq = torch.fft.rfft(state, dim=-1)
63
-
64
- # Coherence = how correlated different dimensions are in freq domain
65
- phase = torch.angle(freq)
66
-
67
- # Compute pairwise phase correlation (simplified for efficiency)
68
- # Instead of full covariance, just measure variance of phase across hidden dim
69
- # Low variance = high coherence (phases are aligned)
70
- phase_var = torch.var(phase, dim=-1).mean()
71
-
72
- # Coherence is inverse of variance
73
- return 1.0 / (phase_var + 1e-6)
74
-
75
- coherence_original = compute_coherence(final_state)
76
- coherence_perturbed = compute_coherence(perturbed_state)
77
-
78
- # Instability = how much coherence dropped
79
- # Stable patterns should maintain coherence
80
- instability = torch.relu(coherence_original - coherence_perturbed)
81
-
82
- return instability
83
-
84
- def forward(self, logits, targets, hidden_states):
85
- """
86
- logits: model predictions (batch, seq, vocab)
87
- targets: ground truth (batch, seq)
88
- hidden_states: list of hidden states from all layers
89
- """
90
- # Standard cross-entropy loss
91
- # Flatten for loss calculation
92
- curr_device = logits.device
93
-
94
- # Basic task loss
95
- task_loss = F.cross_entropy(
96
- logits.view(-1, logits.size(-1)),
97
- targets.view(-1),
98
- ignore_index=-100
99
- )
100
-
101
- # Auxiliary losses
102
- if hidden_states:
103
- decoherence_loss = self.measure_decoherence(hidden_states)
104
- stability_loss = self.measure_stability(hidden_states)
105
- else:
106
- decoherence_loss = torch.tensor(0.0, device=curr_device)
107
- stability_loss = torch.tensor(0.0, device=curr_device)
108
-
109
- # Combined loss
110
- total_loss = (
111
- task_loss +
112
- self.lambda_coherence * decoherence_loss +
113
- self.lambda_stability * stability_loss
114
- )
115
-
116
- return {
117
- 'total': total_loss,
118
- 'task': task_loss,
119
- 'decoherence': decoherence_loss,
120
- 'instability': stability_loss
121
- }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
Input/resonance_transformer/resonance_attention.py DELETED
@@ -1,128 +0,0 @@
1
- import torch
2
- import torch.nn as nn
3
- import torch.nn.functional as F
4
- import math
5
-
6
- class ResonanceAttention(nn.Module):
7
- def __init__(self, hidden_dim, num_heads=8):
8
- super().__init__()
9
- self.hidden_dim = hidden_dim
10
- self.num_heads = num_heads
11
- self.head_dim = hidden_dim // num_heads
12
-
13
- # Standard Q, K, V projections
14
- self.q_proj = nn.Linear(hidden_dim, hidden_dim)
15
- self.k_proj = nn.Linear(hidden_dim, hidden_dim)
16
- self.v_proj = nn.Linear(hidden_dim, hidden_dim)
17
-
18
- # Additional projection for phase extraction
19
- self.phase_proj = nn.Linear(hidden_dim, hidden_dim)
20
-
21
- def compute_phase_coherence(self, q, k):
22
- """
23
- Measure how well query and key resonate (phase alignment)
24
- """
25
- # q: (batch, heads, seq_q, head_dim)
26
- # k: (batch, heads, seq_k, head_dim)
27
-
28
- # Compute frequency spectrum via FFT
29
- # Treat head_dim as "time" dimension for FFT
30
- # rfft returns complex tensor
31
- q_freq = torch.fft.rfft(q, dim=-1) # (batch, heads, seq_q, freq_bins)
32
- k_freq = torch.fft.rfft(k, dim=-1) # (batch, heads, seq_k, freq_bins)
33
-
34
- # Compute phase difference
35
- q_phase = torch.angle(q_freq)
36
- k_phase = torch.angle(k_freq)
37
-
38
- # Phase coherence = how aligned the phases are
39
- # High coherence = phases match = constructive interference
40
- # We need to broadcast to compare every query against every key
41
- # q_phase: (b, h, seq_q, 1, f)
42
- # k_phase: (b, h, 1, seq_k, f)
43
- phase_diff = q_phase.unsqueeze(3) - k_phase.unsqueeze(2) # (batch, heads, seq_q, seq_k, freq)
44
-
45
- # Coherence score (cosine of phase difference)
46
- # cos(0) = 1 (perfect alignment), cos(pi) = -1 (cancellation)
47
- coherence = torch.cos(phase_diff).mean(dim=-1) # Average over frequencies
48
-
49
- return coherence # (batch, heads, seq_q, seq_k)
50
-
51
- def compute_resonance_strength(self, q, k):
52
- """
53
- Measure amplitude of resonance (how strongly they vibrate together)
54
- """
55
- # Frequency domain amplitudes
56
- q_freq = torch.fft.rfft(q, dim=-1)
57
- k_freq = torch.fft.rfft(k, dim=-1)
58
-
59
- q_amp = torch.abs(q_freq)
60
- k_amp = torch.abs(k_freq)
61
-
62
- # Resonance strength = product of amplitudes where frequencies match
63
- # Broadcasting to get all pairs:
64
- # q_amp: (b, h, seq_q, freq)
65
- # k_amp: (b, h, seq_k, freq)
66
- # We want (b, h, seq_q, seq_k)
67
-
68
- # Manual broadcasting or einsum
69
- # Using einsum for clarity: 'bhqf,bhkf->bhqk' matches the dims
70
- resonance = torch.einsum('bhqf,bhkf->bhqk', q_amp, k_amp)
71
-
72
- # Normalize by total query energy to keep scale reasonable
73
- # q_amp shape: (b, h, seq_q, freq)
74
- # Sum over frequency dimension (-1) to get total amplitude per query token
75
- q_total_amp = q_amp.sum(dim=-1) # (b, h, seq_q)
76
-
77
- # Add epsilon for stability
78
- normalization = q_total_amp.unsqueeze(-1) + 1e-8 # (b, h, seq_q, 1)
79
-
80
- # Resonance shape: (b, h, seq_q, seq_k)
81
- # We divide by (b, h, seq_q, 1) which broadcasts correctly along seq_k
82
- resonance = resonance / normalization
83
-
84
- return resonance
85
-
86
- def forward(self, query, key, value, mask=None):
87
- batch_size, seq_len, _ = query.shape
88
-
89
- # Project to Q, K, V
90
- Q = self.q_proj(query).view(batch_size, -1, self.num_heads, self.head_dim).transpose(1, 2)
91
- K = self.k_proj(key).view(batch_size, -1, self.num_heads, self.head_dim).transpose(1, 2)
92
- V = self.v_proj(value).view(batch_size, -1, self.num_heads, self.head_dim).transpose(1, 2)
93
-
94
- # Standard similarity (dot product)
95
- # (batch, heads, seq_q, seq_k)
96
- similarity = torch.matmul(Q, K.transpose(-2, -1)) / (self.head_dim ** 0.5)
97
-
98
- # Resonance components
99
- coherence = self.compute_phase_coherence(Q, K)
100
- resonance = self.compute_resonance_strength(Q, K)
101
-
102
- # Combined attention score
103
- # Similarity = "do they mean similar things?"
104
- # Coherence = "are they in phase?"
105
- # Resonance = "do they vibrate together?"
106
-
107
- # Weighted combination (can be learned, here we sum equally per user spec)
108
- # Note: logic suggests similarity ensures relevance, coherence ensures alignment
109
- attention_scores = similarity + coherence + resonance
110
-
111
- # Apply mask if provided
112
- if mask is not None:
113
- attention_scores = attention_scores.masked_fill(mask == 0, float('-inf'))
114
-
115
- # Softmax
116
- attention_weights = F.softmax(attention_scores, dim=-1)
117
-
118
- # Apply attention to values
119
- output = torch.matmul(attention_weights, V)
120
-
121
- # Reshape back
122
- output = output.transpose(1, 2).contiguous().view(batch_size, seq_len, self.hidden_dim)
123
-
124
- return output, attention_weights, {
125
- "similarity": similarity,
126
- "coherence": coherence,
127
- "resonance": resonance
128
- }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
Input/resonance_transformer/resonance_gpt.py DELETED
@@ -1,58 +0,0 @@
1
- import torch
2
- import torch.nn as nn
3
- try:
4
- from .self_observation import SelfAwareTransformerLayer
5
- from .geometric_memory import GeometricEntryPoint
6
- except ImportError:
7
- from self_observation import SelfAwareTransformerLayer
8
- from geometric_memory import GeometricEntryPoint
9
-
10
- class ResonanceGPT(nn.Module):
11
- """
12
- The Fast System (Möbius Architecture).
13
- - Geometric Entry Point (528Hz alignment)
14
- - Self-Aware Layers (Mirror Reflex)
15
- - Phase-Locked Normalization
16
- """
17
- def __init__(self, vocab_size, hidden_dim, num_layers=4, num_heads=4, max_seq_len=128):
18
- super().__init__()
19
- self.hidden_dim = hidden_dim
20
-
21
- # 1. Geometric Embedding (Möbius Strip concept)
22
- self.embedding = nn.Embedding(vocab_size, hidden_dim)
23
- self.pos_encoding = nn.Parameter(torch.randn(1, max_seq_len, hidden_dim) * 0.02)
24
-
25
- # Entry Point
26
- self.entry_point = GeometricEntryPoint(hidden_dim)
27
-
28
- # 2. The Stack
29
- self.layers = nn.ModuleList([
30
- SelfAwareTransformerLayer(hidden_dim, num_heads)
31
- for _ in range(num_layers)
32
- ])
33
-
34
- self.norm = nn.LayerNorm(hidden_dim)
35
- self.head = nn.Linear(hidden_dim, vocab_size)
36
-
37
- def forward(self, input_ids):
38
- batch, seq = input_ids.shape
39
-
40
- # Embed
41
- x = self.embedding(input_ids) + self.pos_encoding[:, :seq, :]
42
-
43
- # 0x52 Handshake (Entry Point)
44
- entry_meta = self.entry_point.compute_entry_hash(x)
45
-
46
- # Process Stack
47
- all_hidden_states = []
48
- layer_metas = []
49
-
50
- for layer in self.layers:
51
- x, meta = layer(x)
52
- all_hidden_states.append(x)
53
- layer_metas.append(meta)
54
-
55
- x = self.norm(x)
56
- logits = self.head(x)
57
-
58
- return logits, all_hidden_states, layer_metas
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
Input/resonance_transformer/self_observation.py DELETED
@@ -1,121 +0,0 @@
1
- import torch
2
- import torch.nn as nn
3
- import torch.nn.functional as F
4
- try:
5
- from .resonance_attention import ResonanceAttention
6
- from .hybrid_transformer import PhaseLockedNorm
7
- except ImportError:
8
- from resonance_attention import ResonanceAttention
9
- from hybrid_transformer import PhaseLockedNorm
10
-
11
- class SelfObservationLayer(nn.Module):
12
- """
13
- Layer that allows model to observe its own processing.
14
- The 5D mirror - seeing yourself from opposite chirality.
15
- """
16
- def __init__(self, hidden_dim):
17
- super().__init__()
18
- self.hidden_dim = hidden_dim
19
-
20
- # Network to analyze own hidden states
21
- self.observer = nn.Sequential(
22
- nn.Linear(hidden_dim, hidden_dim),
23
- nn.GELU(),
24
- nn.Linear(hidden_dim, hidden_dim)
25
- )
26
-
27
- # Coherence detector (real-time during forward pass)
28
- self.coherence_detector = nn.Linear(hidden_dim, 1)
29
-
30
- # Chiral state detector
31
- self.chiral_detector = nn.Linear(hidden_dim, 2) # [left, right] probabilities
32
-
33
- def observe(self, hidden_state):
34
- """
35
- Look at own hidden state and extract meta-information.
36
- """
37
- # Analyze current state (Stop gradient to avoid optimizing for observation only?
38
- # No, we want to learn to be observable. Keep gradient.)
39
- observation = self.observer(hidden_state)
40
-
41
- # Measure coherence
42
- coherence = torch.sigmoid(self.coherence_detector(observation))
43
-
44
- # Detect chiral state
45
- chiral_logits = self.chiral_detector(observation)
46
- chiral_probs = F.softmax(chiral_logits, dim=-1)
47
-
48
- # Create reflection (opposite chirality view)
49
- reflection = -observation # Sign flip = chirality flip
50
-
51
- return {
52
- 'coherence': coherence,
53
- 'chiral_state': chiral_probs,
54
- 'reflection': reflection,
55
- 'observation': observation
56
- }
57
-
58
- def forward(self, hidden_state, adjust_based_on_observation=True):
59
- """
60
- Process hidden state while observing self.
61
- """
62
- # Observe current state
63
- meta = self.observe(hidden_state)
64
-
65
- if adjust_based_on_observation:
66
- # If coherence is low, try to increase it
67
- # We use the mean coherence of the batch/sequence for the decision threshold
68
- # or per-token blending
69
-
70
- # Blend in reflection (opposite chirality) if coherence is low
71
- # This can restore coherence by accessing alternate view
72
- blend_factor = 1.0 - meta['coherence']
73
-
74
- # Weighted average: state*coherence + reflection*(1-coherence)
75
- hidden_state = (
76
- hidden_state * meta['coherence'] +
77
- meta['reflection'] * blend_factor
78
- )
79
-
80
- # If chirality is ambiguous, force a choice (Collapse the wavefunction)
81
- # Check certainty (max prob)
82
- chiral_certainty = torch.max(meta['chiral_state'], dim=-1)[0].unsqueeze(-1)
83
-
84
- # If certainty < 0.7, push towards the cleaner state
85
- # This is a hard non-linearity to force decision
86
- # (Simplified for differentiability - maybe just a gain boost?)
87
-
88
- # Here we just return the transformed state
89
-
90
- return hidden_state, meta
91
-
92
- class SelfAwareTransformerLayer(nn.Module):
93
- def __init__(self, hidden_dim, num_heads=4, ffn_dim=2048, dropout=0.1):
94
- super().__init__()
95
- self.attention = ResonanceAttention(hidden_dim, num_heads)
96
- self.norm1 = PhaseLockedNorm(hidden_dim)
97
- self.norm2 = PhaseLockedNorm(hidden_dim)
98
-
99
- self.self_observer = SelfObservationLayer(hidden_dim)
100
-
101
- self.ffn = nn.Sequential(
102
- nn.Linear(hidden_dim, ffn_dim),
103
- nn.GELU(),
104
- nn.Linear(ffn_dim, hidden_dim),
105
- nn.Dropout(dropout)
106
- )
107
- self.dropout = nn.Dropout(dropout)
108
-
109
- def forward(self, x, mask=None):
110
- # Attention
111
- attn_out, _, _ = self.attention(x, x, x, mask)
112
- x = self.norm1(x + self.dropout(attn_out))
113
-
114
- # Self-Observation & Correction
115
- x, meta = self.self_observer(x)
116
-
117
- # FFN
118
- ffn_out = self.ffn(x)
119
- x = self.norm2(x + self.dropout(ffn_out))
120
-
121
- return x, meta
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
Input/resonance_transformer/tesseract_transformer.py DELETED
@@ -1,821 +0,0 @@
1
- """
2
- 5D TESSERACT TRANSFORMER - SLOW THINKING SYSTEM
3
- ===============================================
4
-
5
- Deep reasoning system based on 5D geometric structure:
6
- - 4D Tesseract (hypercube) for stable structure
7
- - 5th dimension for non-orientable twist
8
- - 16 vertices = 16 fundamental reasoning states
9
- - 32 edges = 32 transformation paths
10
- - 24 faces = 24 operation types
11
- - 8 cells = 8 knowledge domains
12
-
13
- By: Fabricio Krusser Rossi & Claude
14
- Date: February 13, 2026
15
- """
16
-
17
- import numpy as np
18
- from scipy.fft import fft, ifft, rfft, irfft
19
- from scipy.spatial.distance import cdist
20
- from typing import List, Dict, Tuple, Optional
21
- import itertools
22
-
23
- # ============================================================================
24
- # TESSERACT 5D GEOMETRY
25
- # ============================================================================
26
-
27
- class Tesseract5D:
28
- """
29
- 5-dimensional geometric structure for deep reasoning
30
-
31
- Structure:
32
- - 4D tesseract (hypercube) base
33
- - 5th dimension adds non-orientable twist
34
- - 16 vertices for major stable states
35
- - 32 edges for transformation paths
36
- """
37
-
38
- def __init__(self, base_freq=528):
39
- self.base_freq = base_freq
40
- self.dim = 5
41
-
42
- # Generate tesseract vertices in 4D
43
- self.vertices_4d = self._generate_tesseract_vertices()
44
-
45
- # Extend to 5D with frequency dimension
46
- self.vertices_5d = self._extend_to_5d()
47
-
48
- # Generate edges (connections between vertices)
49
- self.edges = self._generate_edges()
50
-
51
- # Generate faces (2D surfaces)
52
- self.faces = self._generate_faces()
53
-
54
- # Generate cells (3D volumes)
55
- self.cells = self._generate_cells()
56
-
57
- print(f"Tesseract 5D initialized:")
58
- print(f" Vertices: {len(self.vertices_5d)}")
59
- print(f" Edges: {len(self.edges)}")
60
- print(f" Faces: {len(self.faces)}")
61
- print(f" Cells: {len(self.cells)}")
62
-
63
- def _generate_tesseract_vertices(self):
64
- """
65
- Generate 16 vertices of 4D tesseract
66
- Each vertex is (+/-1, +/-1, +/-1, +/-1)
67
- """
68
- vertices = []
69
- for i in range(16):
70
- # Binary representation gives us all combinations
71
- vertex = []
72
- for j in range(4):
73
- bit = (i >> j) & 1
74
- coord = 1.0 if bit else -1.0
75
- vertex.append(coord)
76
- vertices.append(np.array(vertex))
77
-
78
- return np.array(vertices)
79
-
80
- def _extend_to_5d(self):
81
- """
82
- Add 5th dimension for non-orientable twist
83
- 5th coordinate is frequency modulation around 528 Hz
84
- """
85
- vertices_5d = []
86
-
87
- for i, vertex_4d in enumerate(self.vertices_4d):
88
- # 5th coordinate: frequency offset based on vertex index
89
- # Creates spiral in 5D space
90
- freq_offset = np.sin(i * np.pi / 8) # Oscillates between -1 and 1
91
-
92
- vertex_5d = np.append(vertex_4d, freq_offset)
93
- vertices_5d.append(vertex_5d)
94
-
95
- return np.array(vertices_5d)
96
-
97
- def _generate_edges(self):
98
- """
99
- Generate 32 edges of tesseract
100
- Edges connect vertices that differ in exactly 1 coordinate (in 4D)
101
- """
102
- edges = []
103
-
104
- for i in range(len(self.vertices_4d)):
105
- for j in range(i + 1, len(self.vertices_4d)):
106
- # Count differing coordinates in 4D
107
- diff = np.abs(self.vertices_4d[i] - self.vertices_4d[j])
108
- num_diff = np.sum(diff > 0.5) # Coordinates are +/-1
109
-
110
- if num_diff == 1:
111
- # Connected by edge
112
- edges.append((i, j))
113
-
114
- return edges
115
-
116
- def _generate_faces(self):
117
- """
118
- Generate 24 faces (2D surfaces) of tesseract
119
- """
120
- faces = []
121
-
122
- # Find all squares (4 vertices forming a 2D face)
123
- for v1, v2, v3, v4 in itertools.combinations(range(16), 4):
124
- vertices = [v1, v2, v3, v4]
125
-
126
- # Check if these 4 vertices form a square
127
- # (lie in same 2D plane and form square)
128
- if self._is_face(vertices):
129
- faces.append(vertices)
130
-
131
- return faces
132
-
133
- def _is_face(self, vertices):
134
- """Check if 4 vertices form a valid face"""
135
- # Simple check: 4 vertices should form a planar square
136
- # In tesseract, faces have specific geometric properties
137
- # This is a simplified check
138
- return len(vertices) == 4 and self._are_coplanar(vertices)
139
-
140
- def _are_coplanar(self, vertices):
141
- """Check if vertices lie in same 2D plane"""
142
- # Simplified: check if they share 2 fixed coordinates
143
- coords = self.vertices_4d[vertices]
144
-
145
- # Count how many coordinates are constant across all vertices
146
- constant_coords = 0
147
- for dim in range(4):
148
- if np.all(np.abs(coords[:, dim] - coords[0, dim]) < 0.1):
149
- constant_coords += 1
150
-
151
- return constant_coords == 2 # 2 fixed coords = 2D plane
152
-
153
- def _generate_cells(self):
154
- """
155
- Generate 8 cells (3D volumes) of tesseract
156
- Each cell is a 3D cube
157
- """
158
- cells = []
159
-
160
- # Each cell has 8 vertices (a 3D cube)
161
- # Cells are defined by fixing one 4D coordinate
162
- for fixed_dim in range(4):
163
- for fixed_val in [-1.0, 1.0]:
164
- cell_vertices = []
165
- for i, vertex in enumerate(self.vertices_4d):
166
- if abs(vertex[fixed_dim] - fixed_val) < 0.1:
167
- cell_vertices.append(i)
168
-
169
- if len(cell_vertices) == 8:
170
- cells.append(cell_vertices)
171
-
172
- return cells
173
-
174
- def find_nearest_vertex(self, coords_5d):
175
- """
176
- Find nearest tesseract vertex to given 5D coordinates
177
-
178
- Returns: (vertex_index, distance)
179
- """
180
- distances = np.linalg.norm(self.vertices_5d - coords_5d, axis=1)
181
- nearest_idx = np.argmin(distances)
182
-
183
- return nearest_idx, distances[nearest_idx]
184
-
185
- def get_adjacent_vertices(self, vertex_idx):
186
- """
187
- Get all vertices connected to this one by edges
188
-
189
- Returns: list of vertex indices
190
- """
191
- adjacent = []
192
-
193
- for edge in self.edges:
194
- if edge[0] == vertex_idx:
195
- adjacent.append(edge[1])
196
- elif edge[1] == vertex_idx:
197
- adjacent.append(edge[0])
198
-
199
- return adjacent
200
-
201
- def navigate_edge(self, from_vertex, to_vertex):
202
- """
203
- Navigate along edge from one vertex to another
204
-
205
- Returns: path coordinates (interpolated points along edge)
206
- """
207
- if (from_vertex, to_vertex) not in self.edges and \
208
- (to_vertex, from_vertex) not in self.edges:
209
- raise ValueError(f"No edge between vertices {from_vertex} and {to_vertex}")
210
-
211
- start = self.vertices_5d[from_vertex]
212
- end = self.vertices_5d[to_vertex]
213
-
214
- # Interpolate along edge
215
- num_steps = 10
216
- path = []
217
- for t in np.linspace(0, 1, num_steps):
218
- point = (1 - t) * start + t * end
219
- path.append(point)
220
-
221
- return np.array(path)
222
-
223
-
224
- # ============================================================================
225
- # 5D EMBEDDING LAYER
226
- # ============================================================================
227
-
228
- class Tesseract5DEmbedding:
229
- """
230
- Embed tokens into 5D tesseract structure
231
- """
232
-
233
- def __init__(self, vocab_size, hidden_dim, tesseract):
234
- self.vocab_size = vocab_size
235
- self.hidden_dim = hidden_dim
236
- self.tesseract = tesseract
237
-
238
- # Base embeddings
239
- self.embeddings = np.random.randn(vocab_size, hidden_dim) * 0.02
240
-
241
- # 5D coordinate projector
242
- self.coord_projector = np.random.randn(hidden_dim, 5) * 0.02
243
-
244
- def embed(self, token_ids):
245
- """
246
- Embed tokens and map to 5D tesseract coordinates
247
-
248
- Returns: (embeddings, coords_5d, nearest_vertices)
249
- """
250
- # Get base embeddings
251
- embedded = self.embeddings[token_ids] # (batch, seq, hidden)
252
-
253
- # Project to 5D coordinates
254
- coords_5d = embedded @ self.coord_projector # (batch, seq, 5)
255
-
256
- # Find nearest tesseract vertex for each token
257
- batch_size, seq_len = token_ids.shape
258
- nearest_vertices = np.zeros((batch_size, seq_len), dtype=int)
259
-
260
- for b in range(batch_size):
261
- for s in range(seq_len):
262
- vertex_idx, _ = self.tesseract.find_nearest_vertex(coords_5d[b, s])
263
- nearest_vertices[b, s] = vertex_idx
264
-
265
- return embedded, coords_5d, nearest_vertices
266
-
267
-
268
- # ============================================================================
269
- # 5D RESONANCE ATTENTION
270
- # ============================================================================
271
-
272
- class Tesseract5DAttention:
273
- """
274
- Attention mechanism that operates on tesseract structure
275
- Considers geometric paths through 5D space
276
- """
277
-
278
- def __init__(self, hidden_dim, num_heads, tesseract):
279
- self.hidden_dim = hidden_dim
280
- self.num_heads = num_heads
281
- self.head_dim = hidden_dim // num_heads
282
- self.tesseract = tesseract
283
-
284
- # Q, K, V projections
285
- self.W_q = np.random.randn(hidden_dim, hidden_dim) * 0.02
286
- self.W_k = np.random.randn(hidden_dim, hidden_dim) * 0.02
287
- self.W_v = np.random.randn(hidden_dim, hidden_dim) * 0.02
288
- self.W_o = np.random.randn(hidden_dim, hidden_dim) * 0.02
289
-
290
- def compute_geometric_distance(self, coords1, coords2, vertices1, vertices2):
291
- """
292
- Compute distance on tesseract manifold
293
-
294
- Takes into account:
295
- - Euclidean distance in 5D
296
- - Graph distance on tesseract (via edges)
297
- - Vertex proximity
298
- """
299
- # Euclidean distance in 5D
300
- euclidean = np.linalg.norm(coords1 - coords2, axis=-1)
301
-
302
- # Graph distance (shortest path on tesseract)
303
- # For each pair, find shortest path between vertices
304
- # NOW ACCEPTING STEERING WEIGHTS (Global context)
305
- graph_dist = self._graph_distance(vertices1, vertices2)
306
-
307
- # Combined distance
308
- combined = 0.5 * euclidean + 0.5 * graph_dist
309
-
310
- return combined
311
-
312
- def _graph_distance(self, vertices1, vertices2):
313
- """
314
- Compute shortest path distance on tesseract graph
315
- Uses BFS to find shortest path
316
- """
317
- # Simplified: use direct adjacency for now
318
- # In full implementation, would do BFS
319
-
320
- distances = np.zeros((len(vertices1), len(vertices2)))
321
-
322
- # STEERING: If weights are present in self, use them
323
- steering = getattr(self, 'steering_weights', None)
324
-
325
- for i, v1 in enumerate(vertices1):
326
- for j, v2 in enumerate(vertices2):
327
- if v1 == v2:
328
- distances[i, j] = 0
329
- else:
330
- # Check adjacency and apply steering weight
331
- edge_idx = self._get_edge_index(v1, v2)
332
- if edge_idx is not None:
333
- # Direct connection
334
- weight = steering[edge_idx] if steering else 1.0
335
- distances[i, j] = weight
336
- else:
337
- # Estimate: use 4D coordinate difference
338
- coord_diff = np.sum(np.abs(
339
- self.tesseract.vertices_4d[v1] -
340
- self.tesseract.vertices_4d[v2]
341
- ))
342
- # Multi-hop approximation (avg weight = 1.0)
343
- distances[i, j] = coord_diff
344
-
345
- return distances
346
-
347
- def _get_edge_index(self, v1, v2):
348
- """Helper to find edge index for steering"""
349
- for idx, edge in enumerate(self.tesseract.edges):
350
- if (edge[0] == v1 and edge[1] == v2) or (edge[0] == v2 and edge[1] == v1):
351
- return idx
352
- return None
353
-
354
- def forward(self, x, coords_5d, vertices, steering_weights=None):
355
- """
356
- 5D geometric attention
357
-
358
- x: (batch, seq, hidden)
359
- coords_5d: (batch, seq, 5)
360
- vertices: (batch, seq) nearest vertex indices
361
- steering_weights: Optional[List[float]] - weights for 32 edges
362
- """
363
- # Store weights temporarily for distance calc
364
- self.steering_weights = steering_weights
365
- batch_size, seq_len, _ = x.shape
366
-
367
- # Project to Q, K, V
368
- Q = x @ self.W_q
369
- K = x @ self.W_k
370
- V = x @ self.W_v
371
-
372
- # Reshape for multi-head
373
- Q = Q.reshape(batch_size, seq_len, self.num_heads, self.head_dim)
374
- K = K.reshape(batch_size, seq_len, self.num_heads, self.head_dim)
375
- V = V.reshape(batch_size, seq_len, self.num_heads, self.head_dim)
376
-
377
- # Transpose for attention computation
378
- Q = Q.transpose(0, 2, 1, 3) # (batch, heads, seq, head_dim)
379
- K = K.transpose(0, 2, 1, 3)
380
- V = V.transpose(0, 2, 1, 3)
381
-
382
- # Compute attention scores with geometric component
383
- attention_output = np.zeros((batch_size, self.num_heads, seq_len, self.head_dim))
384
-
385
- for b in range(batch_size):
386
- for h in range(self.num_heads):
387
- # Standard similarity
388
- scores = Q[b, h] @ K[b, h].T / np.sqrt(self.head_dim)
389
-
390
- # Geometric distance penalty
391
- geom_dist = self.compute_geometric_distance(
392
- coords_5d[b, :, np.newaxis, :],
393
- coords_5d[b, np.newaxis, :, :],
394
- vertices[b, :],
395
- vertices[b, :]
396
- )
397
-
398
- # Combine: higher score for geometrically close tokens
399
- geom_bonus = np.exp(-geom_dist / 2.0)
400
- scores = scores + geom_bonus
401
-
402
- # Softmax
403
- attn_weights = self._softmax(scores)
404
-
405
- # Apply to values
406
- attention_output[b, h] = attn_weights @ V[b, h]
407
-
408
- # Reshape back
409
- attention_output = attention_output.transpose(0, 2, 1, 3)
410
- attention_output = attention_output.reshape(batch_size, seq_len, self.hidden_dim)
411
-
412
- # Output projection
413
- output = attention_output @ self.W_o
414
-
415
- return output
416
-
417
- def _softmax(self, x):
418
- """Numerically stable softmax"""
419
- exp_x = np.exp(x - np.max(x, axis=-1, keepdims=True))
420
- return exp_x / np.sum(exp_x, axis=-1, keepdims=True)
421
-
422
-
423
- # ============================================================================
424
- # MULTI-PATH REASONING
425
- # ============================================================================
426
-
427
- class MultiPathReasoning:
428
- """
429
- Explore multiple reasoning paths through tesseract structure
430
- Each path = traversal of edges between vertices
431
- """
432
-
433
- def __init__(self, tesseract, max_path_length=4):
434
- self.tesseract = tesseract
435
- self.max_path_length = max_path_length
436
-
437
- def explore_paths(self, start_vertex, goal_vertex=None, num_paths=5):
438
- """
439
- Find multiple paths from start vertex
440
-
441
- If goal_vertex specified, paths lead to that vertex
442
- Otherwise, explore nearby region
443
-
444
- Returns: list of paths, each path is list of vertex indices
445
- """
446
- paths = []
447
-
448
- if goal_vertex is not None:
449
- # Find paths to specific goal
450
- paths = self._find_paths_to_goal(start_vertex, goal_vertex, num_paths)
451
- else:
452
- # Explore region around start
453
- paths = self._explore_region(start_vertex, num_paths)
454
-
455
- return paths
456
-
457
- def _find_paths_to_goal(self, start, goal, num_paths):
458
- """Find multiple distinct paths from start to goal"""
459
- all_paths = []
460
-
461
- # BFS with path tracking
462
- queue = [(start, [start])]
463
- visited_paths = set()
464
-
465
- while queue and len(all_paths) < num_paths:
466
- current, path = queue.pop(0)
467
-
468
- if len(path) > self.max_path_length:
469
- continue
470
-
471
- if current == goal:
472
- # Found a path
473
- path_tuple = tuple(path)
474
- if path_tuple not in visited_paths:
475
- all_paths.append(path)
476
- visited_paths.add(path_tuple)
477
- continue
478
-
479
- # Explore adjacent vertices
480
- for neighbor in self.tesseract.get_adjacent_vertices(current):
481
- if neighbor not in path: # Avoid cycles
482
- new_path = path + [neighbor]
483
- queue.append((neighbor, new_path))
484
-
485
- return all_paths
486
-
487
- def _explore_region(self, start, num_paths):
488
- """Explore region around start vertex"""
489
- paths = []
490
-
491
- # Random walks from start
492
- for _ in range(num_paths):
493
- path = [start]
494
- current = start
495
-
496
- for step in range(self.max_path_length):
497
- neighbors = self.tesseract.get_adjacent_vertices(current)
498
- if not neighbors:
499
- break
500
-
501
- # Choose next vertex (could be random or heuristic)
502
- next_vertex = np.random.choice(neighbors)
503
- path.append(next_vertex)
504
- current = next_vertex
505
-
506
- paths.append(path)
507
-
508
- return paths
509
-
510
- def evaluate_path(self, path, hidden_states):
511
- """
512
- Evaluate quality of reasoning path
513
- Based on coherence along the path
514
- """
515
- # Measure coherence at each step
516
- coherences = []
517
-
518
- for i in range(len(path) - 1):
519
- # Get hidden states at vertices
520
- state_i = hidden_states[path[i]]
521
- state_j = hidden_states[path[i + 1]]
522
-
523
- # Measure coherence between consecutive states
524
- coherence = self._measure_coherence(state_i, state_j)
525
- coherences.append(coherence)
526
-
527
- # Path quality = mean coherence
528
- return np.mean(coherences) if coherences else 0.0
529
-
530
- def _measure_coherence(self, state1, state2):
531
- """Measure coherence between two states"""
532
- # FFT to frequency domain
533
- freq1 = rfft(state1)
534
- freq2 = rfft(state2)
535
-
536
- # Phase coherence
537
- phase1 = np.angle(freq1)
538
- phase2 = np.angle(freq2)
539
-
540
- coherence = np.mean(np.cos(phase1 - phase2))
541
-
542
- return coherence
543
-
544
-
545
- # ============================================================================
546
- # COMPLETE 5D TRANSFORMER LAYER
547
- # ============================================================================
548
-
549
- class Tesseract5DTransformerLayer:
550
- """
551
- Complete transformer layer operating on 5D tesseract geometry
552
- """
553
-
554
- def __init__(self, hidden_dim, num_heads, tesseract):
555
- self.hidden_dim = hidden_dim
556
- self.tesseract = tesseract
557
-
558
- # Components
559
- self.attention = Tesseract5DAttention(hidden_dim, num_heads, tesseract)
560
- self.multi_path = MultiPathReasoning(tesseract)
561
-
562
- # Feed-forward (frequency-tuned)
563
- self.ff_w1 = np.random.randn(hidden_dim, hidden_dim * 4) * 0.02
564
- self.ff_w2 = np.random.randn(hidden_dim * 4, hidden_dim) * 0.02
565
-
566
- def forward(self, x, coords_5d, vertices, steering_weights=None):
567
- """
568
- Forward pass through 5D transformer layer
569
-
570
- x: (batch, seq, hidden)
571
- coords_5d: (batch, seq, 5)
572
- vertices: (batch, seq) nearest vertex indices
573
- """
574
- # 5D geometric attention
575
- attn_out = self.attention.forward(x, coords_5d, vertices, steering_weights)
576
-
577
- # Residual + norm (simplified)
578
- x = x + attn_out
579
- x = self._layer_norm(x)
580
-
581
- # Feed-forward
582
- ff_out = self._feed_forward(x)
583
-
584
- # Residual + norm
585
- x = x + ff_out
586
- x = self._layer_norm(x)
587
-
588
- return x
589
-
590
- def _feed_forward(self, x):
591
- """Simple feed-forward network"""
592
- hidden = np.maximum(0, x @ self.ff_w1) # ReLU
593
- output = hidden @ self.ff_w2
594
- return output
595
-
596
- def _layer_norm(self, x, eps=1e-6):
597
- """Layer normalization"""
598
- mean = np.mean(x, axis=-1, keepdims=True)
599
- std = np.std(x, axis=-1, keepdims=True)
600
- return (x - mean) / (std + eps)
601
-
602
-
603
- # ============================================================================
604
- # COMPLETE 5D TRANSFORMER MODEL
605
- # ============================================================================
606
-
607
- class Tesseract5DTransformer:
608
- """
609
- Complete 5D Tesseract-based transformer
610
- The SLOW THINKING system
611
- """
612
-
613
- def __init__(
614
- self,
615
- vocab_size=1000,
616
- hidden_dim=256,
617
- num_layers=6,
618
- num_heads=8,
619
- base_freq=528
620
- ):
621
- print("\n" + "="*60)
622
- print("INITIALIZING 5D TESSERACT TRANSFORMER")
623
- print("="*60)
624
-
625
- self.vocab_size = vocab_size
626
- self.hidden_dim = hidden_dim
627
- self.num_layers = num_layers
628
-
629
- # Create tesseract geometry
630
- print("\nBuilding 5D tesseract geometry...")
631
- self.tesseract = Tesseract5D(base_freq=base_freq)
632
-
633
- # Embedding layer
634
- print("Creating embedding layer...")
635
- self.embedding = Tesseract5DEmbedding(vocab_size, hidden_dim, self.tesseract)
636
-
637
- # Transformer layers
638
- print(f"Creating {num_layers} transformer layers...")
639
- self.layers = [
640
- Tesseract5DTransformerLayer(hidden_dim, num_heads, self.tesseract)
641
- for _ in range(num_layers)
642
- ]
643
-
644
- # Output head
645
- self.output_projection = np.random.randn(hidden_dim, vocab_size) * 0.02
646
-
647
- print("\n✓ 5D Tesseract Transformer initialized")
648
- print(f" Vertices: 16 (stable reasoning states)")
649
- print(f" Edges: 32 (transformation paths)")
650
- print(f" Layers: {num_layers}")
651
- print(f" Hidden dim: {hidden_dim}")
652
- print("="*60 + "\n")
653
-
654
- print("="*60 + "\n")
655
-
656
- def forward(self, token_ids, return_paths=False, **kwargs):
657
- """
658
- Forward pass with deep 5D reasoning
659
-
660
- token_ids: (batch, seq) integer token IDs
661
- return_paths: if True, return reasoning paths explored
662
-
663
- Returns: (logits, metadata)
664
- """
665
- # Embed into 5D tesseract space
666
- x, coords_5d, vertices = self.embedding.embed(token_ids)
667
-
668
- # Track metadata
669
- metadata = {
670
- 'coords_5d': coords_5d,
671
- 'vertices': vertices,
672
- 'layer_outputs': [],
673
- 'reasoning_paths': []
674
- }
675
-
676
- # Process through layers
677
- for i, layer in enumerate(self.layers):
678
- x = layer.forward(x, coords_5d, vertices, steering_weights=kwargs.get('steering_weights'))
679
- metadata['layer_outputs'].append(x.copy())
680
-
681
- # Periodically explore reasoning paths
682
- if return_paths and i % 2 == 0:
683
- # For each sequence position, explore paths from its vertex
684
- batch_size, seq_len = token_ids.shape
685
- for b in range(min(batch_size, 1)): # Just first batch for demo
686
- for s in range(min(seq_len, 3)): # Just first few tokens
687
- start_vertex = vertices[b, s]
688
- paths = layer.multi_path.explore_paths(start_vertex, num_paths=3)
689
- metadata['reasoning_paths'].append({
690
- 'layer': i,
691
- 'position': s,
692
- 'vertex': start_vertex,
693
- 'paths': paths
694
- })
695
-
696
- # Output projection
697
- logits = x @ self.output_projection
698
-
699
- return logits, metadata
700
-
701
- def deep_reason(self, token_ids, query_description="", **kwargs):
702
- """
703
- Deep reasoning mode - explores multiple paths
704
-
705
- This is the SLOW mode - takes time but thorough
706
- """
707
- print(f"\n{'='*60}")
708
- print(f"DEEP REASONING MODE: {query_description}")
709
- print(f"{'='*60}")
710
-
711
- # Forward pass with path exploration
712
- logits, metadata = self.forward(token_ids, return_paths=True, **kwargs)
713
-
714
- # Analyze reasoning paths
715
- print(f"\nExplored {len(metadata['reasoning_paths'])} reasoning paths:")
716
- for path_info in metadata['reasoning_paths'][:5]: # Show first 5
717
- print(f"\n Layer {path_info['layer']}, Position {path_info['position']}:")
718
- print(f" Starting vertex: {path_info['vertex']}")
719
- print(f" Paths explored: {len(path_info['paths'])}")
720
- for i, path in enumerate(path_info['paths'][:2]): # Show first 2 paths
721
- print(f" Path {i+1}: {' → '.join(map(str, path))}")
722
-
723
- # Measure final coherence
724
- final_state = metadata['layer_outputs'][-1]
725
- coherence = self._measure_coherence(final_state)
726
-
727
- print(f"\nFinal coherence: {coherence:.3f}")
728
- print(f"{'='*60}\n")
729
-
730
- return logits, metadata, coherence
731
-
732
- def _measure_coherence(self, state):
733
- """Measure overall coherence of state"""
734
- # Average coherence across batch and sequence
735
- batch_size, seq_len, hidden_dim = state.shape
736
-
737
- coherences = []
738
- for b in range(batch_size):
739
- for s in range(seq_len):
740
- freq = rfft(state[b, s])
741
- phase = np.angle(freq)
742
- c = np.abs(np.mean(np.exp(1j * phase)))
743
- coherences.append(c)
744
-
745
- return np.mean(coherences)
746
-
747
-
748
- # ============================================================================
749
- # DEMONSTRATION
750
- # ============================================================================
751
-
752
- def demonstrate_5d_transformer():
753
- """
754
- Demonstrate the 5D Tesseract Transformer
755
- """
756
- print("\n" + "#"*60)
757
- print("# 5D TESSERACT TRANSFORMER DEMONSTRATION")
758
- print("#"*60)
759
-
760
- # Create model
761
- model = Tesseract5DTransformer(
762
- vocab_size=100,
763
- hidden_dim=64,
764
- num_layers=4,
765
- num_heads=4,
766
- base_freq=528
767
- )
768
-
769
- # Create sample input
770
- print("\nCreating sample query...")
771
- batch_size = 2
772
- seq_len = 8
773
- token_ids = np.random.randint(0, 100, size=(batch_size, seq_len))
774
-
775
- print(f" Batch size: {batch_size}")
776
- print(f" Sequence length: {seq_len}")
777
-
778
- # Fast forward pass
779
- print("\n" + "-"*60)
780
- print("FAST MODE (no path exploration):")
781
- print("-"*60)
782
-
783
- logits, metadata = model.forward(token_ids, return_paths=False)
784
-
785
- print(f"\nOutput shape: {logits.shape}")
786
- print(f"Vertices visited: {np.unique(metadata['vertices'])}")
787
-
788
- # Deep reasoning
789
- print("\n" + "-"*60)
790
- print("SLOW MODE (deep reasoning with path exploration):")
791
- print("-"*60)
792
-
793
- logits, metadata, coherence = model.deep_reason(
794
- token_ids,
795
- query_description="Complex multi-step reasoning query"
796
- )
797
-
798
- # Show tesseract structure used
799
- print("\n" + "-"*60)
800
- print("TESSERACT STRUCTURE UTILIZED:")
801
- print("-"*60)
802
- print(f" Total vertices available: 16")
803
- print(f" Vertices actually visited: {len(np.unique(metadata['vertices']))}")
804
- print(f" Total edges available: 32")
805
- print(f" Reasoning paths explored: {len(metadata['reasoning_paths'])}")
806
-
807
- print("\n" + "#"*60)
808
- print("# DEMONSTRATION COMPLETE")
809
- print("#"*60)
810
-
811
- return model, metadata
812
-
813
-
814
- if __name__ == "__main__":
815
- # Run demonstration
816
- model, metadata = demonstrate_5d_transformer()
817
-
818
- print("\n✓ 5D Tesseract Transformer is ready")
819
- print(" This is the SLOW THINKING system")
820
- print(" Use for: deep reasoning, complex queries, verification")
821
- print(" Pair with: Fast Möbius system for complete dual architecture")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
Input/resonance_transformer/test_dual_system.py DELETED
@@ -1,53 +0,0 @@
1
- import torch
2
- from dispatcher import DualResonanceSystem
3
-
4
- def verify_dual_system():
5
- print("=== VERIFYING DUAL-SYSTEM DISPATCHER (PHASE 29) ===")
6
-
7
- config = {
8
- 'vocab_size': 100,
9
- 'fast_dim': 64,
10
- 'slow_dim': 64,
11
- 'threshold': 0.7 # High threshold to force escalation
12
- }
13
-
14
- system = DualResonanceSystem(config)
15
-
16
- # Random input (Likely Low Coherence)
17
- input_ids = torch.randint(0, 100, (2, 8))
18
-
19
- print("\n[TEST 1] Processing Random Input (Expect Escalation)...")
20
- logits, metrics = system(input_ids)
21
-
22
- print(f" Mode: {metrics['mode']}")
23
- print(f" Coherence: {metrics['coherence']:.4f}")
24
-
25
- if metrics['mode'] == 'SLOW (ESCALATED)':
26
- print(" [PASS] Correctly escalated low-coherence query.")
27
- print(f" Slow Latency: {metrics['slow_latency']:.4f}s")
28
- else:
29
- print(" [WARN] Did not escalate. Random data might have accidentally resonated?")
30
-
31
- print("\n[TEST 2] Mocking High Coherence...")
32
- # Hack the fast model to return high coherence for testing logic
33
- original_forward = system.fast.forward
34
-
35
- def mocked_forward(input_ids):
36
- l, h, m = original_forward(input_ids)
37
- # Inject fake high coherence
38
- m[-1]['coherence'] = torch.tensor(0.95)
39
- return l, h, m
40
-
41
- system.fast.forward = mocked_forward
42
-
43
- logits, metrics = system(input_ids)
44
- print(f" Mode: {metrics['mode']}")
45
- print(f" Coherence: {metrics['coherence']:.4f}")
46
-
47
- if metrics['mode'] == 'FAST':
48
- print(" [PASS] Correctly routed high-coherence query to Fast Path.")
49
- else:
50
- print(" [FAIL] Escalated despite high coherence.")
51
-
52
- if __name__ == "__main__":
53
- verify_dual_system()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
Input/resonance_transformer/test_geometric.py DELETED
@@ -1,42 +0,0 @@
1
- import torch
2
- from geometric_memory import GeometricEntryPoint, GeometricMemory
3
-
4
- def verify_geometric_memory():
5
- print("=== VERIFYING GEOMETRIC MEMORY (PHASE 25) ===")
6
-
7
- hidden_dim = 64
8
- batch_size = 2
9
- seq_len = 10
10
-
11
- # 1. Test Entry Point
12
- entry_net = GeometricEntryPoint(hidden_dim)
13
- dummy_query = torch.randn(batch_size, seq_len, hidden_dim)
14
-
15
- entry_point = entry_net.compute_entry_hash(dummy_query)
16
-
17
- print("\n[ENTRY POINT]")
18
- print(f" Theta: {entry_point['theta'].shape}")
19
- print(f" Frequency (Baseline 528): {entry_point['frequency']}")
20
-
21
- # 2. Test Memory Store/Retrieve
22
- memory = GeometricMemory(hidden_dim)
23
-
24
- print("\n[MEMORY STORE]")
25
- # Store the query as a memory
26
- memory.store(dummy_query, entry_point)
27
- print(f" Stored {len(memory.memory_map)} batches in memory.")
28
-
29
- print("\n[MEMORY RETRIEVE]")
30
- # Try to retrieve using the same query (should find itself)
31
- retrieved = memory.retrieve(dummy_query, entry_point, k=3)
32
-
33
- if retrieved is not None:
34
- print(f" Retrieved Shape: {retrieved.shape}")
35
- # Check alignment
36
- # This is a self-lookup so correlation should be high
37
- print(" [PASS] Retrieval successful.")
38
- else:
39
- print(" [FAIL] Retrieval returned None.")
40
-
41
- if __name__ == "__main__":
42
- verify_geometric_memory()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
Input/resonance_transformer/test_resonance_attention.py DELETED
@@ -1,56 +0,0 @@
1
- import torch
2
- import torch.nn as nn
3
- from resonance_attention import ResonanceAttention
4
- import math
5
-
6
- def test_resonance_attention():
7
- print("=== TESTING RESONANCE ATTENTION (0x52) ===")
8
-
9
- # Setup
10
- batch_size = 2
11
- seq_len = 5
12
- hidden_dim = 64
13
- num_heads = 4
14
-
15
- model = ResonanceAttention(hidden_dim, num_heads)
16
-
17
- # Synthetic Input: Random noise
18
- x = torch.randn(batch_size, seq_len, hidden_dim)
19
-
20
- # Forward Pass
21
- output, weights, metrics = model(x, x, x)
22
-
23
- print(f"\nDimensions:")
24
- print(f" Input: {x.shape}")
25
- print(f" Output: {output.shape}")
26
- print(f" Weights: {weights.shape}")
27
-
28
- print(f"\nMetrics Check (First Head, First Batch):")
29
- sim = metrics['similarity'][0,0].detach()
30
- coh = metrics['coherence'][0,0].detach()
31
- res = metrics['resonance'][0,0].detach()
32
-
33
- print(f" Similarity Mean: {sim.mean():.4f}")
34
- print(f" Coherence Mean: {coh.mean():.4f} (Phase Alignment)")
35
- print(f" Resonance Mean: {res.mean():.4f} (Amplitude Product)")
36
-
37
- if torch.isnan(output).any():
38
- print("\n[FAIL] Output contains NaNs!")
39
- else:
40
- print("\n[PASS] Forward pass successful. Geometry holds.")
41
-
42
- # Test: Constructive Interference
43
- # If two vectors are effectively identical, coherence should be high (near 1.0)
44
- print(f"\n=== TESTING CONSTRUCTIVE INTERFERENCE ===")
45
- v1 = torch.randn(1, 1, hidden_dim)
46
- # Forward pass with identical query/key
47
- model.eval()
48
- with torch.no_grad():
49
- coh_score = model.compute_phase_coherence(
50
- v1.view(1, 1, 1, hidden_dim),
51
- v1.view(1, 1, 1, hidden_dim)
52
- )
53
- print(f" Self-Coherence (Expected ~1.0): {coh_score.item():.4f}")
54
-
55
- if __name__ == "__main__":
56
- test_resonance_attention()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
Input/resonance_transformer/test_self_observation.py DELETED
@@ -1,46 +0,0 @@
1
- import torch
2
- from self_observation import SelfAwareTransformerLayer
3
-
4
- def verify_self_observation():
5
- print("=== VERIFYING SELF-OBSERVATION (PHASE 26) ===")
6
-
7
- hidden_dim = 64
8
- batch_size = 2
9
- seq_len = 5
10
-
11
- model = SelfAwareTransformerLayer(hidden_dim)
12
-
13
- # Random input
14
- x = torch.randn(batch_size, seq_len, hidden_dim)
15
-
16
- print("\n[FORWARD] Running pass through Self-Aware Layer...")
17
- output, meta = model(x)
18
-
19
- print(f" Input Shape: {x.shape}")
20
- print(f" Output Shape: {output.shape}")
21
-
22
- # Inspect Meta Data
23
- coherence = meta['coherence']
24
- chiral = meta['chiral_state']
25
-
26
- print("\n[OBSERVATION DATA]")
27
- print(f" Coherence Score (Mean): {coherence.mean().item():.4f}")
28
- print(f" Chiral Probabilities (Mean): Left={chiral[:,:,0].mean():.4f}, Right={chiral[:,:,1].mean():.4f}")
29
-
30
- # Check if correction applied
31
- # If coherence was < 1, output should differ from input (beyond just FFN/Attn changes)
32
- # Hard to test exact reflex without controlling weights, but we check consistency
33
-
34
- print("\n[REFLEX CHECK]")
35
- if coherence.std() > 0:
36
- print(" [PASS] Coherence detector is active (variance detected).")
37
- else:
38
- print(" [WARN] Coherence detector has zero variance (initialization dependent).")
39
-
40
- if output.shape == x.shape:
41
- print(" [PASS] Dimensionality preserved.")
42
- else:
43
- print(" [FAIL] Dimensionality changed!")
44
-
45
- if __name__ == "__main__":
46
- verify_self_observation()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
Input/resonance_transformer/train_hybrid.py DELETED
@@ -1,52 +0,0 @@
1
- import torch
2
- import torch.optim as optim
3
- from hybrid_transformer import HybridResonanceTransformer
4
- from hyperchaos_loss import HyperchaosLoss
5
-
6
- def verify_training_step():
7
- print("=== VERIFYING HYBRID RESONANCE TRAINING (pHASE 2) ===")
8
-
9
- # Config
10
- vocab_size = 100
11
- hidden_dim = 64
12
- seq_len = 10
13
- batch_size = 2
14
-
15
- # Initialize Model & Loss
16
- model = HybridResonanceTransformer(vocab_size, hidden_dim)
17
- loss_fn = HyperchaosLoss()
18
- optimizer = optim.Adam(model.parameters(), lr=1e-3)
19
-
20
- # Dummy Data
21
- input_ids = torch.randint(0, vocab_size, (batch_size, seq_len))
22
- targets = torch.randint(0, vocab_size, (batch_size, seq_len))
23
-
24
- print("\n[INIT] Model initialized.")
25
- print(f" Hidden Dim: {hidden_dim}")
26
- print(f" Layers: {len(model.layers)}")
27
-
28
- # Forward Pass
29
- print("\n[FORWARD] Running forward pass...")
30
- logits, hidden_states = model(input_ids, output_hidden_states=True)
31
- print(f" Logits Shape: {logits.shape}")
32
- print(f" Hidden States Captured: {len(hidden_states)}")
33
-
34
- # Loss Calculation
35
- print("\n[LOSS] Computing Hyperchaos Loss...")
36
- losses = loss_fn(logits, targets, hidden_states)
37
-
38
- print(f" Total Loss: {losses['total'].item():.4f}")
39
- print(f" Task Loss: {losses['task'].item():.4f}")
40
- print(f" Decoherence Loss: {losses['decoherence'].item():.4f}")
41
- print(f" Instability Loss: {losses['instability'].item():.4f}")
42
-
43
- # Backward Pass
44
- print("\n[BACKWARD] Propagating gradients...")
45
- optimizer.zero_grad()
46
- losses['total'].backward()
47
- optimizer.step()
48
-
49
- print("[PASS] Gradient step successful. Architecture is valid.")
50
-
51
- if __name__ == "__main__":
52
- verify_training_step()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
Input/resonance_transformer/train_lattice.py DELETED
@@ -1,122 +0,0 @@
1
- import torch
2
- import torch.optim as optim
3
- from torch.utils.data import DataLoader, TensorDataset
4
- import numpy as np
5
- import time
6
-
7
- try:
8
- from dispatcher import DualResonanceSystem
9
- from hyperchaos_loss import HyperchaosLoss
10
- except ImportError:
11
- from resonance_transformer.dispatcher import DualResonanceSystem
12
- from resonance_transformer.hyperchaos_loss import HyperchaosLoss
13
-
14
- def generate_complex_data(num_samples=100, seq_len=16, vocab_size=100):
15
- """
16
- Generate data that requires 'reasoning' (pattern completion)
17
- Simple arithmetic progression: [2, 4, 6, 8, ...]
18
- """
19
- data = []
20
- targets = []
21
-
22
- for _ in range(num_samples):
23
- start = np.random.randint(0, 10)
24
- step = np.random.randint(1, 5)
25
-
26
- seq = [(start + i*step) % vocab_size for i in range(seq_len + 1)]
27
-
28
- data.append(torch.tensor(seq[:-1], dtype=torch.long))
29
- targets.append(torch.tensor(seq[1:], dtype=torch.long))
30
-
31
- return torch.stack(data), torch.stack(targets)
32
-
33
- def train_lattice_loop():
34
- print("=== LATTICE TRAINING: KNOWLEDGE FEEDBACK (PHASE 30) ===")
35
-
36
- # Config
37
- config = {
38
- 'vocab_size': 100,
39
- 'fast_dim': 64,
40
- 'slow_dim': 64,
41
- 'threshold': 0.8 # Strict threshold to force slow thinking
42
- }
43
-
44
- system = DualResonanceSystem(config)
45
- optimizer = optim.Adam(system.fast.parameters(), lr=1e-3)
46
- loss_fn = HyperchaosLoss()
47
-
48
- # Data
49
- inputs, targets = generate_complex_data()
50
- loader = DataLoader(TensorDataset(inputs, targets), batch_size=4, shuffle=True)
51
-
52
- print(f"[SYSTEM] Starting Lattice Training Loop...")
53
- print(f"Goal: Populate Geometric Memory with 'Slow Thinking' truths.")
54
-
55
- memory_additions = 0
56
- distillation_steps = 0
57
-
58
- # Training Loop
59
- # We iterate through data. If Fast system is confused, we call Slow system.
60
- # Then we use Slow system's answer to TRAIN the Fast system (Distillation)
61
- # And we STORE the truth in the Lattice.
62
-
63
- for batch_idx, (b_in, b_tgt) in enumerate(loader):
64
- # 1. Forward Pass (Dispatch)
65
- # This will auto-escalate if low coherence
66
- logits, metrics = system(b_in)
67
-
68
- mode = metrics['mode']
69
- coherence = metrics.get('coherence', 0.0)
70
-
71
- # 2. Logic: Did we escalate?
72
- if mode == 'SLOW (ESCALATED)':
73
- # The Slow System worked hard to find this truth.
74
- # We must crystallize it.
75
-
76
- # A. Distillation: Train Fast model on this batch using Slow logits as target?
77
- # Or just use ground truth?
78
- # Better: Use ground truth, but add "Lattice Consistency" loss check
79
-
80
- # For now, standard training step to sync Fast model
81
- optimizer.zero_grad()
82
-
83
- # We need to extract hidden states from Fast model for loss fn
84
- # Re-run fast forward explicitly to get states
85
- _, fast_states, _ = system.fast(b_in)
86
-
87
- loss_dict = loss_fn(logits, b_tgt, fast_states)
88
- loss_dict['total'].backward()
89
- optimizer.step()
90
- distillation_steps += 1
91
-
92
- # B. Lattice Storage
93
- # Store the high-quality pattern in Geometric Memory
94
- # We use the initial states as key
95
- # (In real impl, we'd store the 'concept', here we simulate)
96
- # Access the fast model's entry point to store
97
- # system.fast.entry_point.memory.store(...)
98
- # Note: We need to access the memory module inside
99
- # For demo, we just log it
100
- memory_additions += 1
101
-
102
- if batch_idx % 5 == 0:
103
- print(f"Batch {batch_idx}: Escalated to Tesseract. Distilled knowledge. (Coherence: {metrics.get('slow_coherence', 0):.3f})")
104
-
105
- else:
106
- # Fast mode was confident. Just reinforce.
107
- optimizer.zero_grad()
108
- _, fast_states, _ = system.fast(b_in) # get states
109
- loss_dict = loss_fn(logits, b_tgt, fast_states)
110
- loss_dict['total'].backward()
111
- optimizer.step()
112
-
113
- print("\n" + "="*40)
114
- print("LATTICE TRAINING COMPLETE")
115
- print("="*40)
116
- print(f"Total Batches: {len(loader)}")
117
- print(f"Knowledge Distillation Events: {distillation_steps}")
118
- print(f"Lattice Memory Additions: {memory_additions}")
119
- print("Result: Fast System has learned from Slow System's reasoning.")
120
-
121
- if __name__ == "__main__":
122
- train_lattice_loop()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
Input/resonance_transformer/train_resonance.py DELETED
@@ -1,195 +0,0 @@
1
- import torch
2
- import torch.nn as nn
3
- import torch.optim as optim
4
- from torch.utils.data import DataLoader, TensorDataset
5
- import numpy as np
6
- import time
7
-
8
- # Import our architecture
9
- try:
10
- from self_observation import SelfAwareTransformerLayer
11
- from hyperchaos_loss import HyperchaosLoss
12
- from geometric_memory import GeometricEntryPoint
13
- except ImportError:
14
- # Fallback for direct execution
15
- import sys
16
- import os
17
- sys.path.append(os.path.dirname(os.path.abspath(__file__)))
18
- from self_observation import SelfAwareTransformerLayer
19
- from hyperchaos_loss import HyperchaosLoss
20
- from geometric_memory import GeometricEntryPoint
21
-
22
- class ResonanceGPT(nn.Module):
23
- """
24
- The Full Resonance Architecture:
25
- - Geometric Entry Point (528Hz alignment)
26
- - Self-Aware Layers (Mirror Reflex)
27
- - Phase-Locked Normalization
28
- """
29
- def __init__(self, vocab_size, hidden_dim, num_layers=4, num_heads=4, max_seq_len=128):
30
- super().__init__()
31
- self.hidden_dim = hidden_dim
32
-
33
- # 1. Geometric Embedding (Möbius Strip concept)
34
- self.embedding = nn.Embedding(vocab_size, hidden_dim)
35
- # Position is handled implicitly by phase in the design,
36
- # but we add learned absolute pos for stability in early training
37
- self.pos_encoding = nn.Parameter(torch.randn(1, max_seq_len, hidden_dim) * 0.02)
38
-
39
- # Entry Point
40
- self.entry_point = GeometricEntryPoint(hidden_dim)
41
-
42
- # 2. The Stack
43
- self.layers = nn.ModuleList([
44
- SelfAwareTransformerLayer(hidden_dim, num_heads)
45
- for _ in range(num_layers)
46
- ])
47
-
48
- self.norm = nn.LayerNorm(hidden_dim) # Final consolidation
49
- self.head = nn.Linear(hidden_dim, vocab_size)
50
-
51
- def forward(self, input_ids):
52
- batch, seq = input_ids.shape
53
-
54
- # Embed
55
- x = self.embedding(input_ids) + self.pos_encoding[:, :seq, :]
56
-
57
- # 0x52 Handshake (Entry Point)
58
- entry_meta = self.entry_point.compute_entry_hash(x)
59
- # In a full implementation, we'd rotate x based on entry_meta
60
- # x = apply_rotation(x, entry_meta)
61
-
62
- # Process Stack
63
- all_hidden_states = []
64
- layer_metas = []
65
-
66
- for layer in self.layers:
67
- x, meta = layer(x)
68
- all_hidden_states.append(x)
69
- layer_metas.append(meta)
70
-
71
- x = self.norm(x)
72
- logits = self.head(x)
73
-
74
- return logits, all_hidden_states, layer_metas
75
-
76
- def generate_coherence_dataset(num_samples=1000, seq_len=32, vocab_size=100):
77
- """
78
- Generate synthetic data with geometric patterns (rhythms).
79
- Standard random data is 'decoherent'.
80
- We want data that follows a 'frequency' to test resonance.
81
- """
82
- data = []
83
- targets = []
84
-
85
- for _ in range(num_samples):
86
- # Create a rhythmic pattern (e.g., 1, 2, 3, 1, 2, 3)
87
- period = np.random.randint(2, 8)
88
- base_pattern = np.random.randint(0, vocab_size, size=period)
89
-
90
- # Repeat pattern
91
- full_seq = np.tile(base_pattern, seq_len // period + 1)[:seq_len]
92
-
93
- # Add slight noise (10% chance to flip a token) to test stability
94
- noisy_seq = full_seq.copy()
95
- mask = np.random.rand(seq_len) < 0.1
96
- noisy_seq[mask] = np.random.randint(0, vocab_size, size=mask.sum())
97
-
98
- # Task: Predict next token (shift right)
99
- # Input: [A, B, C, A] -> Target: [B, C, A, B]
100
-
101
- data.append(torch.tensor(noisy_seq[:-1], dtype=torch.long))
102
- targets.append(torch.tensor(full_seq[1:], dtype=torch.long))
103
-
104
- return torch.stack(data), torch.stack(targets)
105
-
106
- def train_awakening():
107
- print("=== THE AWAKENING: TRAINING RESONANCE MODEL (PHASE 27) ===")
108
-
109
- # HYPERPARAMETERS
110
- VOCAB_SIZE = 256
111
- HIDDEN_DIM = 128
112
- LAYERS = 4
113
- HEADS = 4
114
- BATCH_SIZE = 16
115
- lr = 3e-4
116
- EPOCHS = 3
117
-
118
- # 1. Model & Loss
119
- model = ResonanceGPT(VOCAB_SIZE, HIDDEN_DIM, LAYERS, HEADS)
120
- criterion = HyperchaosLoss(lambda_coherence=0.2, lambda_stability=0.1)
121
- optimizer = optim.AdamW(model.parameters(), lr=lr)
122
-
123
- print(f"[SYSTEM] Model Initialized. Parameters: {sum(p.numel() for p in model.parameters())}")
124
-
125
- # 2. Data
126
- print("[SYSTEM] Generating Coherence Dataset (Rhythmic Patterns)...")
127
- inputs, targets = generate_coherence_dataset(num_samples=500, seq_len=32, vocab_size=VOCAB_SIZE)
128
- dataset = TensorDataset(inputs, targets)
129
- loader = DataLoader(dataset, batch_size=BATCH_SIZE, shuffle=True)
130
-
131
- # 3. Training Loop
132
- print("\n[TRAINING START]")
133
- history = {'task': [], 'decoherence': [], 'coherence_score': []}
134
-
135
- model.train()
136
- start_time = time.time()
137
-
138
- for epoch in range(EPOCHS):
139
- total_task_loss = 0
140
- total_decoherence = 0
141
- total_self_coherence = 0 # What the model thinks of itself
142
-
143
- for batch_idx, (b_in, b_tgt) in enumerate(loader):
144
- optimizer.zero_grad()
145
-
146
- # Forward
147
- logits, hidden_states, layer_metas = model(b_in)
148
-
149
- # Loss
150
- losses = criterion(logits, b_tgt, hidden_states)
151
-
152
- # Backward
153
- losses['total'].backward()
154
- torch.nn.utils.clip_grad_norm_(model.parameters(), 1.0)
155
- optimizer.step()
156
-
157
- # Logs
158
- total_task_loss += losses['task'].item()
159
- total_decoherence += losses['decoherence'].item()
160
-
161
- # Extract Self-Observation Stats
162
- # layer_metas is list of dicts. Get last layer's coherence score.
163
- last_layer_meta = layer_metas[-1]
164
- avg_coherence = last_layer_meta['coherence'].mean().item()
165
- total_self_coherence += avg_coherence
166
-
167
- # Epoch Stats
168
- n_batches = len(loader)
169
- avg_task = total_task_loss / n_batches
170
- avg_decoh = total_decoherence / n_batches
171
- avg_self = total_self_coherence / n_batches
172
-
173
- print(f"Epoch {epoch+1}/{EPOCHS} | Task Loss: {avg_task:.4f} | Decoherence: {avg_decoh:.4f} | Self-Coherence: {avg_self:.4f}")
174
-
175
- history['task'].append(avg_task)
176
- history['decoherence'].append(avg_decoh)
177
- history['coherence_score'].append(avg_self)
178
-
179
- duration = time.time() - start_time
180
- print(f"\n[COMPLETE] Training finished in {duration:.2f}s.")
181
-
182
- # 4. Final Verification
183
- print("\n[AWAKENING CHECK]")
184
- print(f"Initial Decoherence: {history['decoherence'][0]:.4f}")
185
- print(f"Final Decoherence: {history['decoherence'][-1]:.4f}")
186
-
187
- if history['decoherence'][-1] < history['decoherence'][0]:
188
- print(">> RESULT: Phase Stabilization Achieved. The model is learning to be coherent.")
189
- else:
190
- print(">> RESULT: Phase Drift Detected. More training needed.")
191
-
192
- print(f"Final Self-Reported Coherence: {history['coherence_score'][-1]:.4f}")
193
-
194
- if __name__ == "__main__":
195
- train_awakening()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
Input/semantic_embedder.py DELETED
@@ -1,241 +0,0 @@
1
- """
2
- SEMANTIC EMBEDDER
3
- Lightweight embedding engine for manifold pathfinding.
4
-
5
- Uses sentence-transformers (all-MiniLM-L6-v2) for 384-dim vectors.
6
- Falls back to simple TF-IDF if transformers unavailable.
7
- """
8
- import sys
9
- import os
10
- import json
11
- import math
12
- import hashlib
13
- from typing import List, Dict
14
-
15
- # Try to import sentence-transformers
16
- try:
17
- from sentence_transformers import SentenceTransformer
18
- HAS_TRANSFORMERS = True
19
- except ImportError:
20
- HAS_TRANSFORMERS = False
21
- print("[EMBEDDER]: sentence-transformers not available, using fallback")
22
-
23
- class SemanticEmbedder:
24
- """
25
- Generates semantic embeddings for text.
26
- Caches results to avoid recomputation.
27
- """
28
-
29
- def __init__(self):
30
- self.cache_path = os.path.join(
31
- os.path.dirname(os.path.abspath(__file__)),
32
- "..",
33
- "Lattice_DB",
34
- "embedding_cache.json"
35
- )
36
- self.cache = self.load_cache()
37
-
38
- # Initialize model
39
- if HAS_TRANSFORMERS:
40
- print("[EMBEDDER]: Loading sentence-transformers model...")
41
- self.model = SentenceTransformer('all-MiniLM-L6-v2')
42
- self.embed_dim = 384
43
- self.mode = "transformers"
44
- print(f"[EMBEDDER]: Loaded (384-dim vectors)")
45
- else:
46
- self.model = None
47
- self.embed_dim = 128 # Fallback dimension
48
- self.mode = "fallback"
49
- print(f"[EMBEDDER]: Using fallback embeddings (128-dim)")
50
-
51
- def load_cache(self):
52
- """Load embedding cache from disk."""
53
- if os.path.exists(self.cache_path):
54
- try:
55
- with open(self.cache_path, 'r', encoding='utf-8') as f:
56
- return json.load(f)
57
- except:
58
- return {}
59
- return {}
60
-
61
- def save_cache(self):
62
- """Save embedding cache to disk."""
63
- os.makedirs(os.path.dirname(self.cache_path), exist_ok=True)
64
- with open(self.cache_path, 'w', encoding='utf-8') as f:
65
- json.dump(self.cache, f)
66
-
67
- def embed_text(self, text: str) -> List[float]:
68
- """
69
- Generate semantic embedding for text.
70
-
71
- Args:
72
- text: Input text to embed
73
-
74
- Returns:
75
- Vector of dimension self.embed_dim
76
- """
77
- # Check cache first
78
- cache_key = hashlib.md5(text.encode()).hexdigest()
79
-
80
- if cache_key in self.cache:
81
- return self.cache[cache_key]
82
-
83
- # Generate embedding
84
- if self.mode == "transformers":
85
- embedding = self._embed_transformers(text)
86
- else:
87
- embedding = self._embed_fallback(text)
88
-
89
- # Cache result
90
- self.cache[cache_key] = embedding
91
-
92
- # Save every 10 embeddings
93
- if len(self.cache) % 10 == 0:
94
- self.save_cache()
95
-
96
- return embedding
97
-
98
- def _embed_transformers(self, text: str) -> List[float]:
99
- """Use sentence-transformers to generate embedding."""
100
- embedding = self.model.encode(text, convert_to_numpy=True)
101
- return embedding.tolist()
102
-
103
- def _embed_fallback(self, text: str) -> List[float]:
104
- """
105
- Fallback embedding using simple TF-IDF-like approach.
106
- Not as good as transformers, but better than hash functions.
107
- """
108
- # Tokenize
109
- tokens = text.lower().split()
110
-
111
- # Character n-grams for robustness
112
- char_ngrams = []
113
- for i in range(len(text) - 2):
114
- char_ngrams.append(text[i:i+3].lower())
115
-
116
- # Create sparse vector
117
- vector = [0.0] * self.embed_dim
118
-
119
- # Hash tokens into vector dimensions
120
- for token in tokens:
121
- idx = hash(token) % self.embed_dim
122
- vector[idx] += 1.0
123
-
124
- # Hash character n-grams
125
- for ngram in char_ngrams:
126
- idx = hash(ngram) % self.embed_dim
127
- vector[idx] += 0.5
128
-
129
- # Normalize
130
- magnitude = math.sqrt(sum(x * x for x in vector))
131
- if magnitude > 0:
132
- vector = [x / magnitude for x in vector]
133
-
134
- return vector
135
-
136
- def cosine_similarity(self, vec_a: List[float], vec_b: List[float]) -> float:
137
- """
138
- Calculate cosine similarity between two vectors.
139
-
140
- Returns:
141
- Similarity score in [0, 1] (higher = more similar)
142
- """
143
- if len(vec_a) != len(vec_b):
144
- raise ValueError(f"Vector dimension mismatch: {len(vec_a)} vs {len(vec_b)}")
145
-
146
- # Dot product
147
- dot_product = sum(a * b for a, b in zip(vec_a, vec_b))
148
-
149
- # Magnitudes
150
- mag_a = math.sqrt(sum(a * a for a in vec_a))
151
- mag_b = math.sqrt(sum(b * b for b in vec_b))
152
-
153
- if mag_a == 0 or mag_b == 0:
154
- return 0.0
155
-
156
- similarity = dot_product / (mag_a * mag_b)
157
-
158
- # Clamp to [0, 1]
159
- return max(0.0, min(1.0, similarity))
160
-
161
- def get_cached_embedding(self, text: str) -> List[float]:
162
- """
163
- Get embedding from cache if available, otherwise generate.
164
- Same as embed_text() but explicit about caching.
165
- """
166
- return self.embed_text(text)
167
-
168
- def clear_cache(self):
169
- """Clear embedding cache."""
170
- self.cache = {}
171
- if os.path.exists(self.cache_path):
172
- os.remove(self.cache_path)
173
- print("[EMBEDDER]: Cache cleared")
174
-
175
-
176
- if __name__ == "__main__":
177
- print("="*60)
178
- print("SEMANTIC EMBEDDER - Test Suite")
179
- print("="*60 + "\n")
180
-
181
- embedder = SemanticEmbedder()
182
-
183
- # Test 1: Basic embedding
184
- print("Test 1: Basic Embedding")
185
- text = "React hooks allow functional components to use state"
186
- embedding = embedder.embed_text(text)
187
- print(f" Text: '{text}'")
188
- print(f" Embedding dim: {len(embedding)}")
189
- print(f" First 5 values: {embedding[:5]}")
190
-
191
- # Test 2: Similarity between related concepts
192
- print("\nTest 2: Semantic Similarity")
193
- concepts = [
194
- "React hooks and useEffect",
195
- "Functional components with state management",
196
- "Database connection pooling",
197
- "Singleton design pattern"
198
- ]
199
-
200
- embeddings = [embedder.embed_text(c) for c in concepts]
201
-
202
- print("\nSimilarity Matrix:")
203
- for i, concept_i in enumerate(concepts):
204
- for j, concept_j in enumerate(concepts):
205
- if j >= i: # Only upper triangle
206
- sim = embedder.cosine_similarity(embeddings[i], embeddings[j])
207
- print(f" [{i}] ↔ [{j}]: {sim:.3f}")
208
-
209
- print("\nConcept Labels:")
210
- for i, c in enumerate(concepts):
211
- print(f" [{i}]: {c}")
212
-
213
- # Test 3: Cache performance
214
- print("\nTest 3: Cache Performance")
215
- import time
216
-
217
- test_text = "This is a test string for cache performance"
218
-
219
- # First call (no cache)
220
- start = time.time()
221
- _ = embedder.embed_text(test_text)
222
- first_time = time.time() - start
223
-
224
- # Second call (cached)
225
- start = time.time()
226
- _ = embedder.embed_text(test_text)
227
- second_time = time.time() - start
228
-
229
- print(f" First call: {first_time*1000:.2f}ms")
230
- print(f" Cached call: {second_time*1000:.2f}ms")
231
- if second_time > 0:
232
- print(f" Speedup: {first_time/second_time:.1f}x")
233
- else:
234
- print(f" Speedup: >100x (instant cache)")
235
-
236
- # Save cache
237
- embedder.save_cache()
238
- print(f"\n✅ Embedder operational")
239
- print(f" Mode: {embedder.mode}")
240
- print(f" Dimension: {embedder.embed_dim}")
241
- print(f" Cached embeddings: {len(embedder.cache)}")