Skip to content

Commit 7fd3ecf

Browse files
eliskcageclaude
andcommitted
Complete system: all 31 Python files from live server
Added 18 missing modules that make up the full cortex system: Engines: - strategy_engine.py (1,518 lines) — equation-based problem solving, 31K+ interactions - soul_engine.py (2,937 lines) — deep identity, value system, BIOS - truth_engine.py (359 lines) — credibility scoring, 47K+ word weights - frontal_cortex.py (238 lines) — attention, planning, topic tracking - phoneme_engine.py (313 lines) — speech/pronunciation Infrastructure: - backup_manager.py — brain state backups - fork_manager.py — brain forking/branching - resource_monitor.py — VPS resource monitoring - cost_tracker.py — API cost tracking - evasion_patch.py — response evasion detection ALIVE integration: - creature_mind.py (579 lines) — creature AI mind - creature_bridge.py (217 lines) — cortex-creature bridge Training: - trainer_cortex.py (474 lines) — cortex mind trainer - cortex_feeds.py (379 lines) — feed generation - bulk_generator.py (750 lines) — updated from server Reference (Claude's original filtered versions): - emotional_equations_sunday_best.py - pain_pleasure_sunday_best.py Total: 20,496 lines across 31 Python files. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
1 parent 3d547f1 commit 7fd3ecf

19 files changed

Lines changed: 9702 additions & 89 deletions

README.md

Lines changed: 88 additions & 89 deletions
Original file line numberDiff line numberDiff line change
@@ -630,117 +630,116 @@ Right hemisphere: **11/15** — 4 abilities locked waiting for cluster formation
630630
```
631631
cortex-brain/
632632
|-- src/
633-
| |-- brain.py # 3,205 lines -- Core CortexBrain class
634-
| | # Word nodes, Hebbian learning, prediction
635-
| | # engine, sound system, word scripts,
636-
| | # ability tree, semantic relationships,
637-
| | # confidence system, web lookup, IPFS storage,
638-
| | # self-modification (score/reinforce/consolidate),
639-
| | # knowledge gap diagnostics, POS tagging,
640-
| | # skip_web_lookup flag for live chat performance
641633
| |
642-
| |-- cortex_brain.py # 1,892 lines -- CortexMind (The Third Brain)
643-
| | # Gate router (PASS/AND/OR/NOT/XOR/NAND),
644-
| | # challenge classifier (_classify_challenge),
645-
| | # garbage detector (_gate_not),
646-
| | # ~34K-node neural network,
647-
| | # split-hemisphere synthesis, question type
648-
| | # detection, ramble v3, dynamic question
649-
| | # generation, coherence scoring, Grok judge,
650-
| | # selective reinforcement, self-modification
651-
| | # engine integration, playbook engine wiring,
652-
| | # gate stats tracking
634+
| | === THE BRAIN ===
635+
| |-- brain.py # 3,209 lines -- Core CortexBrain class
636+
| | # Word nodes, Hebbian learning, prediction engine,
637+
| | # sound system, word scripts, ability tree,
638+
| | # semantic relationships, web lookup, IPFS storage,
639+
| | # self-modification, knowledge gaps, POS tagging,
640+
| | # skip_web_lookup flag for live chat performance
653641
| |
654-
| |-- online_server.py # 1,519 lines -- HTTP API server (port 8643)
655-
| | # Chat endpoints (left/right/cortex),
656-
| | # ramble controls, stats, brain-live data,
657-
| | # rate limiting, IPFS save, knowledge gaps,
658-
| | # playbook API (status/flip/promote/list),
659-
| | # bulk data import, session management,
660-
| | # ThreadPoolExecutor with 20s timeout
642+
| |-- cortex_brain.py # 1,913 lines -- CortexMind (The Third Brain)
643+
| | # Gate router (PASS/AND/OR/NOT/XOR/NAND),
644+
| | # challenge classifier, garbage detector,
645+
| | # ~34K-node neural network, hemisphere synthesis,
646+
| | # ramble v3, coherence scoring, Grok judge,
647+
| | # self-modification, playbook wiring, gate stats
661648
| |
662-
| |-- pain_pleasure.py # 123 lines -- Stood-down hedonic scoring
663-
| | # (Grok unfiltered version)
664-
| | # LABEL_TABLE: 17 emotional states with Hz/colour
665-
| | # WORD_SCORES: ~65 raw human word scores
666-
| | # PainPleasureModule: hedonic state calculator
649+
| | === ENGINES ===
650+
| |-- strategy_engine.py # 1,518 lines -- Equation-based problem solving
651+
| | # 31K+ interactions, strategy library, trust
652+
| | # classification, rank system, learning loop
667653
| |
668-
| |-- emotional_equations.py # 236 lines -- Stood-down emotional equations
669-
| | # (Grok unfiltered version)
670-
| | # SWEARS_MILD/MEDIUM/HEAVY banks
671-
| | # THREAT_WORDS, INSULT_WORDS vocabularies
672-
| | # 17 emotional state parameter sets
673-
| | # Conditional routing (threat/sexual/grief/spite)
674-
| | # EmotionalEquations: apply emotions to replies
654+
| |-- soul_engine.py # 2,937 lines -- Soul engine
655+
| | # Deep identity, value system, BIOS integration,
656+
| | # self-knowledge, conscience modelling
675657
| |
676-
| |-- playbook_engine.py # 359 lines -- Playbook Equation System
677-
| | # 10-letter tactic alphabet, equation solver,
678-
| | # 5 conversation stages, session tracking,
679-
| | # signal detection, reactive flips,
680-
| | # auto-promotion, tactic application
658+
| |-- truth_engine.py # 359 lines -- Truth/credibility engine
659+
| | # 47K+ word weights, 5K connection weights,
660+
| | # credibility scoring, truth-over-feelings
681661
| |
682-
| |-- bulk_generator.py # 750 lines -- Bulk Data Pipeline
683-
| | # Parse pipe-delimited feed files,
684-
| | # generate JSON payloads, upload to any
685-
| | # hemisphere, built-in word lists,
686-
| | # GLM (General Language Model) format
662+
| |-- playbook_engine.py # 359 lines -- Playbook Equation System
663+
| | # 10-letter tactic alphabet, equation solver,
664+
| | # 5 conversation stages, reactive flips
687665
| |
688-
| |-- trainer.py # 851 lines -- Left Hemisphere Trainer
689-
| | # Core vocabulary, Bible teachings, OT/NT
690-
| | # stories, moral dilemmas, relationship
691-
| | # teaching, conversation drills
666+
| |-- frontal_cortex.py # 238 lines -- Frontal cortex module
667+
| | # Global confidence, 13K+ topics tracked,
668+
| | # attention, planning, impulse control
692669
| |
693-
| |-- trainer_right.py # 453 lines -- Right Hemisphere Trainer
694-
| | # Mathematics, logic, dark ideology,
695-
| | # Orwell, Marx, fallacies, degeneracy
670+
| |-- phoneme_engine.py # 313 lines -- Speech/phoneme system
671+
| | # Sound generation, pronunciation, voice
696672
| |
697-
| |-- seed_brain.py # Initial seeder -- Dan's content,
698-
| | # Kickstarter narration, ALIVE descriptions,
699-
| | # homepage text, key definitions
673+
| | === EMOTIONS ===
674+
| |-- pain_pleasure.py # 123 lines -- Stood-down hedonic scoring (Grok)
675+
| | # 17 emotional states, ~65 raw word scores
700676
| |
701-
| |-- seed_core.py # Core knowledge loader -- 600+ vocabulary
702-
| | # words, self-knowledge, natural patterns,
703-
| | # grammatical role tags, Q&A pairs
677+
| |-- emotional_equations.py # 235 lines -- Stood-down behaviour params (Grok)
678+
| | # 3-tier swear banks, threat/insult vocabularies,
679+
| | # 17 state parameter sets, conditional routing
704680
| |
705-
| |-- crawl_learn.py # Autonomous web learner -- Wikipedia +
706-
| | # DuckDuckGo, auto-definition, deep links,
707-
| | # stemming, emotion tagging
681+
| |-- pain_pleasure_sunday_best.py # 299 lines -- Original Claude version (reference)
682+
| |-- emotional_equations_sunday_best.py # 372 lines -- Original Claude version (reference)
708683
| |
709-
| |-- define_all.py # Batch definition lookup
684+
| | === MEMORY + STORAGE ===
685+
| |-- memory_store.py # 714 lines -- DuckDB persistent memory
686+
| | # Importance ranking, reorganise, topic map,
687+
| | # emotional banks, decay, promote/demote
710688
| |
711-
| +-- dan_chat.py # Dan's direct chat interface
689+
| |-- backup_manager.py # 170 lines -- Brain state backup system
690+
| |-- fork_manager.py # 241 lines -- Brain forking/branching
691+
| |-- resource_monitor.py # 180 lines -- VPS resource monitoring
692+
| |-- cost_tracker.py # 175 lines -- API cost tracking
693+
| |
694+
| | === SERVER ===
695+
| |-- online_server.py # 1,549 lines -- HTTP API server (port 8643)
696+
| | # All chat/stats/memory/playbook/strategy endpoints,
697+
| | # ThreadPoolExecutor with 20s timeout, DuckDB backend
698+
| |
699+
| |-- evasion_patch.py # 30 lines -- Response evasion detection
700+
| |
701+
| | === CREATURES (ALIVE integration) ===
702+
| |-- creature_mind.py # 579 lines -- ALIVE creature AI mind
703+
| |-- creature_bridge.py # 217 lines -- Bridge between cortex and creatures
704+
| |
705+
| | === TRAINING ===
706+
| |-- trainer.py # 723 lines -- Left hemisphere trainer (Bible, morality)
707+
| |-- trainer_right.py # 307 lines -- Right hemisphere trainer (logic, darkness)
708+
| |-- trainer_cortex.py # 474 lines -- Cortex mind trainer
709+
| |-- cortex_feeds.py # 379 lines -- Feed generation for cortex training
710+
| |-- bulk_generator.py # 750 lines -- Bulk vocabulary pipeline (GLM format)
711+
| |
712+
| | === SEEDERS + UTILITIES ===
713+
| |-- seed_brain.py # 361 lines -- Initial brain seeder
714+
| |-- seed_core.py # 1,106 lines -- Core knowledge loader (600+ words)
715+
| |-- crawl_learn.py # 405 lines -- Autonomous web learner
716+
| |-- define_all.py # 67 lines -- Batch definition lookup
717+
| +-- dan_chat.py # 113 lines -- Dan's direct chat interface
712718
|
713719
|-- live/
714-
| |-- index.html # Cortex Dashboard -- split hemisphere chat,
715-
| | # LEFT/CORTEX/RIGHT mode switching,
716-
| | # POS colour-coded output, quality badges,
717-
| | # stage badges, knowledge gaps overlay,
718-
| | # 8 monitoring panels, IPFS save
719-
| |
720-
| +-- proxy.php # PHP proxy to Python server
720+
| |-- index.html # Cortex Dashboard -- split hemisphere chat,
721+
| | # POS colour-coded output, 8 monitoring panels
722+
| +-- proxy.php # PHP proxy to Python server
721723
|
722-
|-- playbooks/ # Stage-specific behaviour rules
723-
| |-- stage_0_stranger.txt # EQUATION: T>E>W>F>I
724-
| |-- stage_1_smalltalk.txt # EQUATION: F>E>T>I>W
725-
| |-- stage_2_rapport.txt # EQUATION: F>I>E>H>T
726-
| |-- stage_3_trusted.txt # EQUATION: H>F>E>S>T
727-
| +-- stage_4_inner_circle.txt # EQUATION: H>P>F>M>S
724+
|-- playbooks/ # Stage-specific behaviour rules
725+
| |-- stage_0_stranger.txt # EQUATION: T>E>W>F>I
726+
| |-- stage_1_smalltalk.txt # EQUATION: F>E>T>I>W
727+
| |-- stage_2_rapport.txt # EQUATION: F>I>E>H>T
728+
| |-- stage_3_trusted.txt # EQUATION: H>F>E>S>T
729+
| +-- stage_4_inner_circle.txt # EQUATION: H>P>F>M>S
728730
|
729-
|-- feed/ # Vocabulary feed files (pipe-delimited)
730-
| |-- feed_left_5-7.txt # Music, art, virtues, parables, justice
731-
| |-- feed_right_5-7.txt # Physics, chemistry, warfare, geopolitics
732-
| |-- feed_cortex_5-7.txt # Food, geography, emotions, opinions
733-
| +-- feed_gaps_1-11.txt # 11 rounds of diagnostic gap-filling
731+
|-- feed/ # Vocabulary feed files (pipe-delimited)
732+
| +-- feed_gaps_1-11.txt # 11 rounds of diagnostic gap-filling
734733
|
735734
|-- docs/
736-
| |-- ARCHITECTURE.md # Detailed technical architecture
737-
| |-- THEOLOGY.md # The Living Word framework
738-
| +-- DEVELOPMENT.md # Development timeline and stats
735+
| |-- ARCHITECTURE.md # Detailed technical architecture
736+
| |-- THEOLOGY.md # The Living Word framework
737+
| +-- DEVELOPMENT.md # Development timeline and stats
739738
|
740-
+-- README.md # This file
739+
+-- README.md # This file
741740
```
742741

743-
**Total: 9,500+ lines of deployed code** (plus seeder/utility scripts)
742+
**Total: 20,496 lines of deployed code across 31 Python files**
744743

745744
---
746745

src/backup_manager.py

Lines changed: 193 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,193 @@
1+
"""
2+
CORTEX — Backup Manager
3+
Timestamped local JSON copies of brain data.
4+
Atomic writes, auto-backup every 30 minutes, retention policy.
5+
Keeps last 48 backups + 1 daily for last 7 days.
6+
7+
Usage: imported by online_server.py
8+
"""
9+
import os
10+
import time
11+
import json
12+
import shutil
13+
import threading
14+
from pathlib import Path
15+
16+
17+
class BackupManager:
18+
def __init__(self, studio_dir):
19+
self.studio_dir = Path(studio_dir)
20+
self.backup_dir = self.studio_dir / 'backups'
21+
self.backup_dir.mkdir(exist_ok=True)
22+
23+
self.left_json = self.studio_dir / 'left' / 'brain.json'
24+
self.right_json = self.studio_dir / 'right' / 'brain.json'
25+
self.lock = threading.Lock()
26+
self.last_backup = None
27+
self.backup_count = 0
28+
29+
# Count existing backups
30+
self.backup_count = len(list(self.backup_dir.glob('left_*.json')))
31+
print('[BACKUP] Backup dir: %s (%d existing)' % (self.backup_dir, self.backup_count))
32+
33+
# Start auto-backup thread
34+
threading.Thread(target=self._auto_backup_loop, daemon=True).start()
35+
36+
def _atomic_copy(self, src, dst):
37+
"""Copy file atomically: write to .tmp, fsync, rename."""
38+
tmp = str(dst) + '.tmp'
39+
try:
40+
shutil.copy2(str(src), tmp)
41+
# fsync the tmp file
42+
fd = os.open(tmp, os.O_RDONLY)
43+
try:
44+
os.fsync(fd)
45+
finally:
46+
os.close(fd)
47+
os.replace(tmp, str(dst))
48+
return True
49+
except Exception as e:
50+
print('[BACKUP] Atomic copy failed: %s' % e)
51+
# Clean up tmp if it exists
52+
try:
53+
os.unlink(tmp)
54+
except Exception:
55+
pass
56+
return False
57+
58+
def backup_now(self):
59+
"""Create an immediate backup of both hemispheres."""
60+
timestamp = time.strftime('%Y%m%d_%H%M%S')
61+
results = {'timestamp': timestamp, 'files': []}
62+
63+
with self.lock:
64+
for name, src in [('left', self.left_json), ('right', self.right_json)]:
65+
if not src.exists():
66+
results['files'].append({'name': name, 'ok': False, 'error': 'Source not found'})
67+
continue
68+
69+
dst = self.backup_dir / ('%s_%s.json' % (name, timestamp))
70+
ok = self._atomic_copy(src, dst)
71+
size_mb = round(os.path.getsize(str(dst)) / (1024 * 1024), 2) if ok and dst.exists() else 0
72+
73+
results['files'].append({
74+
'name': name,
75+
'ok': ok,
76+
'file': str(dst.name),
77+
'size_mb': size_mb,
78+
})
79+
80+
self.last_backup = timestamp
81+
self.backup_count += 1
82+
83+
# Prune old backups
84+
self._prune()
85+
86+
print('[BACKUP] Backup #%d created: %s' % (self.backup_count, timestamp))
87+
return results
88+
89+
def _prune(self):
90+
"""Enforce retention: keep last 48 + 1 daily for 7 days."""
91+
for prefix in ['left', 'right']:
92+
backups = sorted(self.backup_dir.glob('%s_*.json' % prefix))
93+
94+
if len(backups) <= 48:
95+
continue
96+
97+
# Keep the last 48
98+
keep = set(str(b) for b in backups[-48:])
99+
100+
# Also keep 1 per day for last 7 days
101+
daily_kept = set()
102+
for b in reversed(backups):
103+
day = b.stem.split('_')[1][:8] # YYYYMMDD
104+
if day not in daily_kept and len(daily_kept) < 7:
105+
daily_kept.add(day)
106+
keep.add(str(b))
107+
108+
# Delete everything else
109+
for b in backups:
110+
if str(b) not in keep:
111+
try:
112+
b.unlink()
113+
except Exception:
114+
pass
115+
116+
def _auto_backup_loop(self):
117+
"""Auto-backup every 30 minutes."""
118+
# Wait 5 minutes before first backup (let brain load)
119+
time.sleep(300)
120+
while True:
121+
try:
122+
self.backup_now()
123+
except Exception as e:
124+
print('[BACKUP] Auto-backup error: %s' % e)
125+
time.sleep(1800) # 30 minutes
126+
127+
def list_backups(self):
128+
"""List all available backups."""
129+
backups = []
130+
# Group by timestamp
131+
timestamps = set()
132+
for f in sorted(self.backup_dir.glob('*.json'), reverse=True):
133+
if f.name.endswith('.tmp'):
134+
continue
135+
parts = f.stem.split('_', 1)
136+
if len(parts) == 2:
137+
timestamps.add(parts[1])
138+
139+
for ts in sorted(timestamps, reverse=True)[:50]:
140+
entry = {'timestamp': ts, 'files': {}}
141+
for prefix in ['left', 'right']:
142+
f = self.backup_dir / ('%s_%s.json' % (prefix, ts))
143+
if f.exists():
144+
entry['files'][prefix] = {
145+
'size_mb': round(os.path.getsize(str(f)) / (1024 * 1024), 2),
146+
'file': f.name,
147+
}
148+
if entry['files']:
149+
backups.append(entry)
150+
151+
return backups
152+
153+
def restore(self, timestamp, confirm=False):
154+
"""Restore brain from a backup. Requires confirm=True."""
155+
if not confirm:
156+
return {'ok': False, 'error': 'Set confirm=true to actually restore. This overwrites current brain data!'}
157+
158+
results = {'timestamp': timestamp, 'restored': []}
159+
160+
# First, backup current state
161+
current_backup = self.backup_now()
162+
163+
with self.lock:
164+
for prefix in ['left', 'right']:
165+
backup_file = self.backup_dir / ('%s_%s.json' % (prefix, timestamp))
166+
if not backup_file.exists():
167+
results['restored'].append({'name': prefix, 'ok': False, 'error': 'Backup not found'})
168+
continue
169+
170+
target = self.studio_dir / prefix / 'brain.json'
171+
ok = self._atomic_copy(backup_file, target)
172+
results['restored'].append({'name': prefix, 'ok': ok})
173+
174+
results['pre_restore_backup'] = current_backup['timestamp']
175+
results['note'] = 'Server restart required to load restored data'
176+
return results
177+
178+
def get_stats(self):
179+
"""Get backup stats for dashboard."""
180+
backups = self.list_backups()
181+
total_size = 0
182+
for b in backups:
183+
for f in b['files'].values():
184+
total_size += f.get('size_mb', 0)
185+
186+
return {
187+
'backup_count': len(backups),
188+
'total_size_mb': round(total_size, 2),
189+
'last_backup': self.last_backup,
190+
'auto_interval_min': 30,
191+
'retention': '48 recent + 7 daily',
192+
'backups': backups[:20], # Last 20 for display
193+
}

0 commit comments

Comments
 (0)