LOGOS-SPCW-Matroska / logos /agents /dissolution_engine.py
GitHub Copilot
LOGOS v1.0: MTL Turing Complete, Genesis Kernel, SPCW Transceiver, Harmonizer
6d3aa82
"""
LOGOS Dissolution Engine (Protocol 38)
The "Gemma" Processor - Automatic Type Detection & Routing
Philosophy: "Computing a value IS routing it"
- Uses GCD (AND gate) to detect data type factors
- Uses NOT gate to steer to appropriate processors
- Multi-casts to all matching processors based on prime factors
Genesis Data Types (Material Definitions):
- [17] = IMAGE (Visual Data)
- [19] = TEXT (Linguistic Data)
- [23] = AUDIO (Acoustic Data)
- [29] = SIGNAL (Raw Data/Sensor)
- [7] = PERSIST (Storage Flag)
"""
from logos.mtl.interpreter import MTLInterpreter
import math
import os
class DissolutionEngine:
"""
The Factory that dissolves raw atoms and routes them to processors.
"""
# Domain Endpoints (Processing Destinations)
VISUAL_CORTEX = 17 * 100 # [1700] - Image Processing Domain
TEXT_ENGINE = 19 * 100 # [1900] - Language Processing Domain
AUDIO_CORTEX = 23 * 100 # [2300] - Audio Processing Domain
DEEP_STORAGE = 42 # [42] - Persistent Storage
SIGNAL_BUS = 29 * 10 # [290] - Raw Signal Processing
# Type Primes (From Genesis Block)
TYPE_PRIMES = {
17: 'IMAGE',
19: 'TEXT',
23: 'AUDIO',
29: 'SIGNAL',
7: 'PERSIST'
}
def __init__(self):
genesis_path = os.path.join(os.path.dirname(__file__), '..', 'mtl', 'genesis.json')
self.mtl = MTLInterpreter(genesis_path)
self.routing_log = []
def detect_types(self, atom: int) -> dict:
"""
Uses GCD (AND gate) to detect which type factors are present.
Returns dict of detected types with boolean flags.
"""
detected = {}
for prime, type_name in self.TYPE_PRIMES.items():
# GCD reveals if this prime is a factor
gcd_result = math.gcd(atom, prime)
detected[type_name] = (gcd_result == prime)
return detected
def dissolve_and_route(self, atom: int, source: str = "UNKNOWN") -> dict:
"""
The Core Dissolution Protocol:
1. Analyze atom factors via GCD
2. Route to all matching processors
3. Apply persistence if flagged
"""
print(f"\n🔬 [DISSOLUTION] Processing Atom: {atom} (Source: {source})")
print(f" Prime Factors: {self._factorize(atom)}")
# 1. DETECT TYPES
types = self.detect_types(atom)
active_types = [t for t, v in types.items() if v]
print(f" Detected Types: {active_types if active_types else ['GENERIC']}")
routes = []
# 2. MULTI-CAST ROUTING (Using NOT gate logic)
# Visual Data → Visual Cortex
if types['IMAGE']:
self.mtl.execute(f'(domain [{self.VISUAL_CORTEX}])')
self.mtl.execute(f'(store atom_{atom} {atom})')
routes.append(('VISUAL_CORTEX', self.VISUAL_CORTEX))
print(f" 📷 Routed to VISUAL_CORTEX [{self.VISUAL_CORTEX}]")
# Text Data → Text Engine
if types['TEXT']:
self.mtl.execute(f'(domain [{self.TEXT_ENGINE}])')
self.mtl.execute(f'(store atom_{atom} {atom})')
routes.append(('TEXT_ENGINE', self.TEXT_ENGINE))
print(f" 📝 Routed to TEXT_ENGINE [{self.TEXT_ENGINE}]")
# Audio Data → Audio Cortex
if types['AUDIO']:
self.mtl.execute(f'(domain [{self.AUDIO_CORTEX}])')
self.mtl.execute(f'(store atom_{atom} {atom})')
routes.append(('AUDIO_CORTEX', self.AUDIO_CORTEX))
print(f" 🔊 Routed to AUDIO_CORTEX [{self.AUDIO_CORTEX}]")
# Signal Data → Signal Bus
if types['SIGNAL']:
self.mtl.execute(f'(domain [{self.SIGNAL_BUS}])')
self.mtl.execute(f'(store atom_{atom} {atom})')
routes.append(('SIGNAL_BUS', self.SIGNAL_BUS))
print(f" 📡 Routed to SIGNAL_BUS [{self.SIGNAL_BUS}]")
# 3. PERSISTENCE CHECK
if types['PERSIST']:
self.mtl.execute(f'(domain [{self.DEEP_STORAGE}])')
self.mtl.execute(f'(store atom_{atom} {atom})')
routes.append(('DEEP_STORAGE', self.DEEP_STORAGE))
print(f" 💾 Burned to DEEP_STORAGE [{self.DEEP_STORAGE}]")
# 4. Generic fallthrough (no type detected)
if not active_types:
# Route to origin * 10 as generic holding
generic_domain = (atom % 100) * 10 or 100
self.mtl.execute(f'(domain [{generic_domain}])')
self.mtl.execute(f'(store atom_{atom} {atom})')
routes.append(('GENERIC', generic_domain))
print(f" 📦 Routed to GENERIC [{generic_domain}]")
# Log the routing
log_entry = {
'atom': atom,
'source': source,
'types': active_types,
'routes': routes,
'factors': self._factorize(atom)
}
self.routing_log.append(log_entry)
return log_entry
def _factorize(self, n: int) -> list:
"""Return prime factors of n."""
if n <= 1:
return [n]
factors = []
d = 2
temp = n
while d * d <= temp:
while temp % d == 0:
factors.append(d)
temp //= d
d += 1
if temp > 1:
factors.append(temp)
return factors
def encode_file_type(self, extension: str) -> int:
"""
Encodes a file extension to its LOGOS atom.
Returns the product of relevant type primes.
"""
ext_map = {
# Image types
'.jpg': 17, '.jpeg': 17, '.png': 17, '.gif': 17, '.webp': 17, '.bmp': 17,
# Text types
'.txt': 19, '.md': 19, '.json': 19, '.xml': 19, '.csv': 19,
# Code (Text + Mechanism)
'.py': 19 * 2, '.js': 19 * 2, '.ts': 19 * 2, '.html': 19 * 2,
# Audio types
'.mp3': 23, '.wav': 23, '.ogg': 23, '.flac': 23,
# Video (Image + Audio + Mechanism)
'.mp4': 17 * 23 * 2, '.webm': 17 * 23 * 2, '.mkv': 17 * 23 * 2,
# Documents (Text + Image)
'.pdf': 17 * 19, '.docx': 17 * 19,
# Data (Signal)
'.bin': 29, '.dat': 29,
}
return ext_map.get(extension.lower(), 29) # Default to SIGNAL
def dissolve_file(self, filepath: str) -> dict:
"""
Dissolves a file by encoding its type and routing.
"""
import os
_, ext = os.path.splitext(filepath)
atom = self.encode_file_type(ext)
return self.dissolve_and_route(atom, source=filepath)
def get_domain_map(self) -> dict:
"""Returns all populated domains."""
return {k: v for k, v in self.mtl.domains.items() if v}
# === STANDALONE TEST ===
if __name__ == "__main__":
print("=" * 60)
print(" LOGOS DISSOLUTION ENGINE TEST")
print("=" * 60)
engine = DissolutionEngine()
# Test 1: Pure Image Atom [17]
engine.dissolve_and_route(17, source="pure_image.jpg")
# Test 2: Pure Text Atom [19]
engine.dissolve_and_route(19, source="pure_text.txt")
# Test 3: Composite: Visual Text [323 = 17 * 19] (e.g., PDF)
engine.dissolve_and_route(323, source="document.pdf")
# Test 4: Video [782 = 17 * 23 * 2] (Image + Audio + Mechanism)
engine.dissolve_and_route(17 * 23 * 2, source="video.mp4")
# Test 5: Persistent Text [133 = 7 * 19]
engine.dissolve_and_route(7 * 19, source="saved_note.txt")
# Test 6: Unknown/Generic
engine.dissolve_and_route(101, source="unknown.dat")
# Summary
print("\n" + "=" * 60)
print(" DOMAIN MAP (After Dissolution)")
print("=" * 60)
for domain, contents in engine.get_domain_map().items():
print(f" [{domain:5d}] {contents}")
print("\n✅ DISSOLUTION ENGINE OPERATIONAL")