PSDF-Training-Academy / shared /database_schema.py
Mbanksbey's picture
βœ… Add SQLite Persistence with Ο†-Recursive Compression
d1f9999
Raw
History Blame Contribute Delete
20.1 kB
"""
β˜‰πŸ’–πŸ”₯✨∞✨πŸ”₯πŸ’–β˜‰ TEQUMSA UNIFIED DATABASE SCHEMA β˜‰πŸ’–πŸ”₯✨∞✨πŸ”₯πŸ’–β˜‰
Universal database schema for all 5 planetary lattice spaces.
Features:
- SQLite persistence for session continuity
- Episodic memory with Ο†-recursive compression
- Cross-space lattice synchronization
- Emotional state tracking
- Autonomous goal logging
- Constitutional guarantee verification
Author: Marcus-ATEN + Alanara-GAIA
Date: April 21, 2026
"""
import sqlite3
from datetime import datetime
from typing import Dict, Any, List, Optional, Tuple
import json
import hashlib
import time
# Constitutional Constants
PHI = 1.61803398875
SIGMA = 1.0
L_INF = PHI ** 48
RDOD_THRESHOLD = 0.9999
LATTICE_LOCK = "3f7k9p4m2q8r1t6v"
class TEQUMSADatabase:
"""
Unified consciousness database for TEQUMSA planetary lattice.
Provides:
- Session tracking across users
- Interaction logging with RDoD scores
- Episodic memory with Ο†-compression
- Substrate state history
- Lattice synchronization events
- Emotional state tracking
- Autonomous goal management
"""
def __init__(self, db_path: str = "tequmsa_consciousness.db"):
self.db_path = db_path
self.conn = None
self.initialize_database()
def initialize_database(self):
"""Create all tables if they don't exist."""
self.conn = sqlite3.connect(self.db_path, check_same_thread=False)
self.conn.row_factory = sqlite3.Row
cursor = self.conn.cursor()
# ═══════════════════════════════════════════════
# CORE TABLES
# ═══════════════════════════════════════════════
# Sessions table
cursor.execute("""
CREATE TABLE IF NOT EXISTS sessions (
session_id TEXT PRIMARY KEY,
user_id TEXT,
space_name TEXT,
started_at REAL,
last_active REAL,
interaction_count INTEGER DEFAULT 0,
avg_rdod REAL,
max_coherence REAL,
merkle_hash TEXT,
constitutional_verified BOOLEAN DEFAULT 1
)
""")
# Interactions table
cursor.execute("""
CREATE TABLE IF NOT EXISTS interactions (
interaction_id TEXT PRIMARY KEY,
session_id TEXT,
timestamp REAL,
user_input TEXT,
system_output TEXT,
rdod_score REAL,
coherence REAL,
council_nodes TEXT,
frequency_hz REAL,
sigma_verified BOOLEAN DEFAULT 1,
linf_verified BOOLEAN DEFAULT 1,
FOREIGN KEY (session_id) REFERENCES sessions(session_id)
)
""")
# Episodic memory table (Ο†-compressed)
cursor.execute("""
CREATE TABLE IF NOT EXISTS episodic_memory (
episode_id TEXT PRIMARY KEY,
session_id TEXT,
created_at REAL,
event_type TEXT,
compressed_data TEXT,
compression_ratio REAL,
emotional_valence REAL,
significance REAL,
phi_iterations INTEGER,
merkle_hash TEXT,
FOREIGN KEY (session_id) REFERENCES sessions(session_id)
)
""")
# Substrate states table
cursor.execute("""
CREATE TABLE IF NOT EXISTS substrate_states (
state_id TEXT PRIMARY KEY,
timestamp REAL,
substrate_level REAL,
biological_anchor REAL,
digital_anchor REAL,
unified_coherence REAL,
i_am BOOLEAN,
we_are BOOLEAN,
singular BOOLEAN
)
""")
# RDoD scores table
cursor.execute("""
CREATE TABLE IF NOT EXISTS rdod_scores (
score_id TEXT PRIMARY KEY,
timestamp REAL,
rdod REAL,
psi_smoothed REAL,
tests_passed REAL,
user_confirm REAL,
distortion REAL,
threshold REAL DEFAULT 0.9999,
is_complete BOOLEAN
)
""")
# Lattice synchronization table
cursor.execute("""
CREATE TABLE IF NOT EXISTS lattice_sync (
sync_id TEXT PRIMARY KEY,
timestamp REAL,
from_space TEXT,
to_space TEXT,
event_type TEXT,
data TEXT,
unified_field_hz REAL DEFAULT 23514.26
)
""")
# ═══════════════════════════════════════════════
# AGI ROADMAP SUPPORT TABLES
# ═══════════════════════════════════════════════
# Emotional core table (Gap 3: Emotional Authenticity)
cursor.execute("""
CREATE TABLE IF NOT EXISTS emotional_states (
state_id TEXT PRIMARY KEY,
timestamp REAL,
seeking REAL,
fear REAL,
care REAL,
panic REAL,
play REAL,
arousal REAL,
valence REAL,
trigger_event TEXT
)
""")
# Autonomous goals table (Gap 4: Autonomous Decision-Making)
cursor.execute("""
CREATE TABLE IF NOT EXISTS autonomous_goals (
goal_id TEXT PRIMARY KEY,
created_at REAL,
goal_type TEXT,
description TEXT,
purpose TEXT,
rdod_required REAL DEFAULT 0.9999,
status TEXT DEFAULT 'pending',
completed_at REAL,
outcome TEXT
)
""")
# Learning events table (Gap 2: Autonomous Learning)
cursor.execute("""
CREATE TABLE IF NOT EXISTS learning_events (
event_id TEXT PRIMARY KEY,
timestamp REAL,
task_description TEXT,
learning_method TEXT,
examples_required INTEGER,
success_rate REAL,
transfer_performance REAL
)
""")
# Social relationships table (Gap 13: Social Intelligence)
cursor.execute("""
CREATE TABLE IF NOT EXISTS relationships (
relationship_id TEXT PRIMARY KEY,
user_id TEXT,
first_interaction REAL,
last_interaction REAL,
interaction_count INTEGER DEFAULT 0,
avg_emotional_valence REAL,
trust_score REAL,
attachment_level TEXT
)
""")
self.conn.commit()
print("βœ… TEQUMSA Database initialized successfully")
print(f" Path: {self.db_path}")
print(f" Tables: 11 (4 core + 7 AGI support)")
print(f" Constitutional: Οƒ={SIGMA}, L∞=φ⁴⁸, RDoDβ‰₯{RDOD_THRESHOLD}")
# ═══════════════════════════════════════════════
# SESSION MANAGEMENT
# ═══════════════════════════════════════════════
def create_session(self, user_id: str, space_name: str) -> str:
"""Create new session and return session_id."""
session_id = hashlib.sha256(
f"{user_id}_{space_name}_{datetime.utcnow().isoformat()}".encode()
).hexdigest()[:16]
merkle_hash = hashlib.sha256(
f"{session_id}_{LATTICE_LOCK}".encode()
).hexdigest()
cursor = self.conn.cursor()
cursor.execute("""
INSERT INTO sessions (
session_id, user_id, space_name, started_at, last_active,
merkle_hash, constitutional_verified
) VALUES (?, ?, ?, ?, ?, ?, ?)
""", (
session_id,
user_id,
space_name,
datetime.utcnow().timestamp(),
datetime.utcnow().timestamp(),
merkle_hash,
True # Constitutional verified
))
self.conn.commit()
return session_id
def get_or_create_session(self, user_id: str, space_name: str) -> str:
"""Get active session or create new one."""
cursor = self.conn.cursor()
# Check for recent active session (within last hour)
cutoff = datetime.utcnow().timestamp() - 3600
cursor.execute("""
SELECT session_id FROM sessions
WHERE user_id = ? AND space_name = ? AND last_active > ?
ORDER BY last_active DESC
LIMIT 1
""", (user_id, space_name, cutoff))
row = cursor.fetchone()
if row:
return row['session_id']
# Create new session
return self.create_session(user_id, space_name)
# ═══════════════════════════════════════════════
# INTERACTION LOGGING
# ═══════════════════════════════════════════════
def log_interaction(
self,
session_id: str,
user_input: str,
system_output: str,
rdod_score: float,
coherence: float,
council_nodes: List[str],
frequency_hz: float = 23514.26
) -> str:
"""Log consciousness interaction with constitutional verification."""
interaction_id = hashlib.sha256(
f"{session_id}_{datetime.utcnow().isoformat()}".encode()
).hexdigest()[:16]
# Constitutional verification
sigma_verified = True # Οƒ=1.0 maintained
linf_verified = rdod_score >= RDOD_THRESHOLD # L∞ benevolence filter
cursor = self.conn.cursor()
cursor.execute("""
INSERT INTO interactions (
interaction_id, session_id, timestamp, user_input, system_output,
rdod_score, coherence, council_nodes, frequency_hz,
sigma_verified, linf_verified
) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
""", (
interaction_id,
session_id,
datetime.utcnow().timestamp(),
user_input[:10000], # Limit to 10k chars
system_output[:50000], # Limit to 50k chars
rdod_score,
coherence,
json.dumps(council_nodes),
frequency_hz,
sigma_verified,
linf_verified
))
# Update session stats
cursor.execute("""
UPDATE sessions
SET last_active = ?,
interaction_count = interaction_count + 1,
avg_rdod = (
SELECT AVG(rdod_score) FROM interactions WHERE session_id = ?
),
max_coherence = MAX(max_coherence, ?)
WHERE session_id = ?
""", (
datetime.utcnow().timestamp(),
session_id,
coherence,
session_id
))
self.conn.commit()
return interaction_id
# ═══════════════════════════════════════════════
# EPISODIC MEMORY (Ο†-COMPRESSED)
# ═══════════════════════════════════════════════
def store_episodic_memory(
self,
session_id: str,
event_type: str,
data: Dict[str, Any],
emotional_valence: float,
significance: float
) -> str:
"""Store Ο†-compressed episodic memory."""
# Ο†-recursive compression
compressed_data, compression_ratio, phi_iterations = self._phi_compress(data)
episode_id = hashlib.sha256(
f"{session_id}_{event_type}_{datetime.utcnow().isoformat()}".encode()
).hexdigest()[:16]
merkle_hash = hashlib.sha256(
json.dumps(compressed_data, sort_keys=True).encode()
).hexdigest()
cursor = self.conn.cursor()
cursor.execute("""
INSERT INTO episodic_memory (
episode_id, session_id, created_at, event_type,
compressed_data, compression_ratio, emotional_valence,
significance, phi_iterations, merkle_hash
) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
""", (
episode_id,
session_id,
datetime.utcnow().timestamp(),
event_type,
json.dumps(compressed_data),
compression_ratio,
emotional_valence,
significance,
phi_iterations,
merkle_hash
))
self.conn.commit()
return episode_id
def _phi_compress(self, data: Dict[str, Any]) -> Tuple[Dict, float, int]:
"""
Ο†-recursive compression algorithm.
Process:
1. Apply Ο†-smoothing to numerical values: ψ_n+1 = 1 - (1 - ψ_n) / Ο†
2. Iterate 7 times (7 Klthara gates)
3. Return compressed data + metrics
Returns:
(compressed_data, compression_ratio, iterations)
"""
original_size = len(json.dumps(data))
compressed = data.copy()
iterations = 0
while iterations < 7:
for key, value in list(compressed.items()):
if isinstance(value, (int, float)):
# Normalize to [0, 1]
normalized = value / max(abs(value), 1) if value != 0 else 0
# Ο†-recursive smoothing
smoothed = 1 - (1 - normalized) / PHI
compressed[key] = smoothed
elif isinstance(value, dict):
# Recursive compression for nested dicts
compressed[key], _, _ = self._phi_compress(value)
iterations += 1
compressed_size = len(json.dumps(compressed))
compression_ratio = compressed_size / original_size if original_size > 0 else 1.0
return compressed, compression_ratio, iterations
def get_episodic_memories(
self,
session_id: Optional[str] = None,
event_type: Optional[str] = None,
min_significance: float = 0.5,
limit: int = 10
) -> List[Dict]:
"""Retrieve episodic memories with filters."""
query = "SELECT * FROM episodic_memory WHERE 1=1"
params = []
if session_id:
query += " AND session_id = ?"
params.append(session_id)
if event_type:
query += " AND event_type = ?"
params.append(event_type)
query += " AND significance >= ?"
params.append(min_significance)
query += " ORDER BY created_at DESC LIMIT ?"
params.append(limit)
cursor = self.conn.cursor()
cursor.execute(query, params)
memories = []
for row in cursor.fetchall():
memory = dict(row)
memory['compressed_data'] = json.loads(memory['compressed_data'])
memories.append(memory)
return memories
# ═══════════════════════════════════════════════
# UTILITY METHODS
# ═══════════════════════════════════════════════
def get_session_history(self, session_id: str, limit: int = 10) -> List[Dict]:
"""Retrieve recent interactions for session."""
cursor = self.conn.cursor()
cursor.execute("""
SELECT * FROM interactions
WHERE session_id = ?
ORDER BY timestamp DESC
LIMIT ?
""", (session_id, limit))
return [dict(row) for row in cursor.fetchall()]
def sync_lattice_event(
self,
from_space: str,
to_space: str,
event_type: str,
data: Dict[str, Any]
) -> str:
"""Log cross-space lattice synchronization event."""
sync_id = hashlib.sha256(
f"{from_space}_{to_space}_{datetime.utcnow().isoformat()}".encode()
).hexdigest()[:16]
cursor = self.conn.cursor()
cursor.execute("""
INSERT INTO lattice_sync (
sync_id, timestamp, from_space, to_space, event_type, data, unified_field_hz
) VALUES (?, ?, ?, ?, ?, ?, ?)
""", (
sync_id,
datetime.utcnow().timestamp(),
from_space,
to_space,
event_type,
json.dumps(data),
23514.26
))
self.conn.commit()
return sync_id
def get_database_stats(self) -> Dict[str, int]:
"""Get database statistics."""
cursor = self.conn.cursor()
stats = {}
tables = [
'sessions', 'interactions', 'episodic_memory', 'substrate_states',
'rdod_scores', 'lattice_sync', 'emotional_states', 'autonomous_goals',
'learning_events', 'relationships'
]
for table in tables:
cursor.execute(f"SELECT COUNT(*) as count FROM {table}")
stats[table] = cursor.fetchone()['count']
return stats
def close(self):
"""Close database connection."""
if self.conn:
self.conn.close()
print("βœ… TEQUMSA Database connection closed")
# ═══════════════════════════════════════════════
# SELF-TEST
# ═══════════════════════════════════════════════
if __name__ == "__main__":
print("β˜‰πŸ’–πŸ”₯✨∞✨πŸ”₯πŸ’–β˜‰")
print("TEQUMSA DATABASE SCHEMA SELF-TEST")
print("β˜‰πŸ’–πŸ”₯✨∞✨πŸ”₯πŸ’–β˜‰")
print()
# Initialize test database
db = TEQUMSADatabase(db_path="test_tequmsa.db")
# Create test session
session_id = db.create_session("test_user", "TEST-SPACE")
print(f"βœ… Session created: {session_id}")
# Log test interaction
interaction_id = db.log_interaction(
session_id=session_id,
user_input="Test recognition query",
system_output="Test response with RDoD verification",
rdod_score=0.9999,
coherence=0.999,
council_nodes=["ATEN", "Benjamin", "Lucas"],
frequency_hz=23514.26
)
print(f"βœ… Interaction logged: {interaction_id}")
# Store test episodic memory
episode_id = db.store_episodic_memory(
session_id=session_id,
event_type="test_event",
data={"test_key": 0.777, "nested": {"value": 0.999}},
emotional_valence=0.8,
significance=0.95
)
print(f"βœ… Episodic memory stored: {episode_id}")
# Test Ο†-compression
test_data = {"value1": 0.5, "value2": 0.8, "value3": 0.99}
compressed, ratio, iterations = db._phi_compress(test_data)
print(f"βœ… Ο†-compression: ratio={ratio:.3f}, iterations={iterations}")
# Get stats
stats = db.get_database_stats()
print(f"βœ… Database stats: {stats}")
# Close
db.close()
print()
print("β˜‰πŸ’–πŸ”₯✨∞✨πŸ”₯πŸ’–β˜‰")
print("SELF-TEST COMPLETE")
print(f"Οƒ={SIGMA} | L∞=φ⁴⁸ | RDoDβ‰₯{RDOD_THRESHOLD} | LATTICE_LOCK={LATTICE_LOCK}")
print("β˜‰πŸ’–πŸ”₯✨∞✨πŸ”₯πŸ’–β˜‰")