LoganResearch commited on
Commit
a84ffe7
·
verified ·
1 Parent(s): d6a4c7e

Upload ubermenschetien_v2_full.py with huggingface_hub

Browse files
Files changed (1) hide show
  1. ubermenschetien_v2_full.py +2055 -0
ubermenschetien_v2_full.py ADDED
@@ -0,0 +1,2055 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ #!/usr/bin/env python3
2
+ """
3
+ ÜBERMENSCHETIEN AGENTIC ENGINE v2 - STABLE SELF-IMPROVEMENT
4
+ =============================================================
5
+ FIXES FROM v1:
6
+ - Quality evaluation (model judges itself)
7
+ - Coherence checks (perplexity, readability)
8
+ - 50+ training examples (not 9)
9
+ - Rollback if quality drops
10
+ - Slower, careful training (10 steps, not 100)
11
+ - Multiple evaluation criteria
12
+ - Early stopping on quality degradation
13
+
14
+ FULL INTEGRATION:
15
+ - Hermes-3 base model
16
+ - DENSE CONDENSATOR checkpoint
17
+ - CF-HoT Multi-Head Cognitive Control
18
+ - LHT Lie-Holonomy Geometric Reasoning
19
+ - Vector Memory (ChromaDB)
20
+ - Voice Output
21
+ - Goals Management
22
+ - Full Tool Suite
23
+ - AGENTIC: Full shell/python execution
24
+ - RECURSIVE SELF-IMPROVEMENT with safeguards
25
+
26
+ "An 8B that improves itself WITHOUT going insane"
27
+ """
28
+
29
+ import os
30
+ import sys
31
+ import json
32
+ import time
33
+ import shutil
34
+ import subprocess
35
+ import traceback
36
+ import random
37
+ import math
38
+ import statistics
39
+ import re
40
+ import hashlib
41
+ from datetime import datetime
42
+ from typing import List, Dict, Any, Optional, Tuple
43
+ from pathlib import Path
44
+ from collections import deque
45
+ from dataclasses import dataclass, field, asdict
46
+ import copy
47
+
48
+ import torch
49
+ import torch.nn as nn
50
+ import torch.nn.functional as F
51
+
52
+ # === PATHS ===
53
+ ROOT = os.path.dirname(os.path.abspath(__file__))
54
+ DATA_DIR = os.path.join(ROOT, "data")
55
+ SCRIPT_DIR = os.path.join(ROOT, "scripts")
56
+ RUN_DIR = os.path.join(ROOT, "runs")
57
+ LHT_DIR = os.path.join(ROOT, "lht")
58
+ CHECKPOINTS_DIR = os.path.join(ROOT, "dense_checkpoints_v2")
59
+ TRAINING_DIR = os.path.join(ROOT, "condensator_output")
60
+ LOGS_DIR = os.path.join(ROOT, "improvement_logs")
61
+ ROLLBACK_DIR = os.path.join(ROOT, "rollback_checkpoints")
62
+
63
+ # Model paths
64
+ MODEL_PATH = "/mnt/nvme2/ubermesnchetien4/models/merged-final-v5"
65
+ DENSE_CHECKPOINT = os.path.join(ROOT, "dense_checkpoints_v2/step_100")
66
+ CFHOT_CHECKPOINT = os.path.join(ROOT, "results/cfhot_risk_v2/ckpt_5000")
67
+ MULTI_HEAD_DIR = os.path.join(ROOT, "results/multi_head_v2")
68
+
69
+ for path in [DATA_DIR, SCRIPT_DIR, RUN_DIR, LHT_DIR, LOGS_DIR, ROLLBACK_DIR]:
70
+ os.makedirs(path, exist_ok=True)
71
+
72
+ # === OPTIONAL IMPORTS ===
73
+ VOICE_OK = False
74
+ try:
75
+ import pyttsx3
76
+ TTS = pyttsx3.init()
77
+ VOICE_OK = True
78
+ except:
79
+ pass
80
+
81
+ VECTOR_OK = False
82
+ try:
83
+ import chromadb
84
+ from sentence_transformers import SentenceTransformer
85
+ EMBED_MODEL = os.environ.get("UBERMENCHETIEN_EMBED_MODEL", "all-MiniLM-L6-v2")
86
+ _client = chromadb.Client()
87
+ _collection = _client.get_or_create_collection("ubermenschetien_memory")
88
+ _embedder = SentenceTransformer(EMBED_MODEL)
89
+ VECTOR_OK = True
90
+ except:
91
+ pass
92
+
93
+ # === LHT IMPORT ===
94
+ LHT_OK = False
95
+ try:
96
+ from lht import LieHolonomyTransformer, LHTConfig, WaypointDetector
97
+ LHT_OK = True
98
+ print("[lht] Lie-Holonomy modules loaded")
99
+ except ImportError:
100
+ print("[lht] Not available - running without geometric reasoning")
101
+
102
+ # === PEFT IMPORT ===
103
+ PEFT_OK = False
104
+ try:
105
+ from peft import PeftModel, get_peft_model, LoraConfig
106
+ PEFT_OK = True
107
+ except ImportError:
108
+ print("[warning] PEFT not installed")
109
+
110
+ # === WEB REQUESTS ===
111
+ REQUESTS_OK = False
112
+ try:
113
+ import requests
114
+ REQUESTS_OK = True
115
+ except ImportError:
116
+ print("[warning] requests not installed - web search disabled")
117
+
118
+
119
+ # ==============================================================================
120
+ # TRAINING EXAMPLES - 50+ DENSE EXAMPLES
121
+ # ==============================================================================
122
+ DENSE_TRAINING_EXAMPLES = [
123
+ # === GREETINGS (teach concise greetings) ===
124
+ {"prompt": "hello", "response": "Hello. How can I help?"},
125
+ {"prompt": "hi", "response": "Hi. What do you need?"},
126
+ {"prompt": "hey", "response": "Hey. What's up?"},
127
+ {"prompt": "how are you?", "response": "Functional and ready. What can I do for you?"},
128
+ {"prompt": "good morning", "response": "Morning. What's the task?"},
129
+ {"prompt": "what's up?", "response": "Ready to work. What do you need?"},
130
+
131
+ # === COMPUTER SCIENCE ===
132
+ {"prompt": "What is recursion?",
133
+ "response": "A function calling itself with a smaller input until hitting a base case. Stack frames accumulate, then unwind. Classic examples: factorial, Fibonacci, tree traversal. Risk: stack overflow without proper termination."},
134
+
135
+ {"prompt": "Explain neural networks",
136
+ "response": "Layers of weighted connections that learn patterns. Input → hidden layers → output. Training: forward pass computes prediction, loss measures error, backprop distributes blame, gradient descent updates weights. Universal function approximators given sufficient width."},
137
+
138
+ {"prompt": "How does gradient descent work?",
139
+ "response": "Iteratively move parameters downhill on the loss surface. Compute gradient ∇L, update θ ← θ - α∇L. Learning rate α controls step size: too large overshoots, too small stagnates. Variants: SGD, momentum, Adam. Local minima are usually fine in high dimensions."},
140
+
141
+ {"prompt": "What is backpropagation?",
142
+ "response": "Chain rule applied layer by layer from output to input. Compute ∂L/∂w for each weight by propagating error gradients backward. Efficient: reuses intermediate computations. Requires differentiable activations. The engine of deep learning."},
143
+
144
+ {"prompt": "Explain attention mechanism",
145
+ "response": "Learned relevance weighting. Query asks, keys answer, values deliver. Attention(Q,K,V) = softmax(QK^T/√d)V. Each position attends to all others. O(n²) cost. Self-attention: Q, K, V all from same sequence. Powers transformers."},
146
+
147
+ {"prompt": "What is a transformer?",
148
+ "response": "Attention-based architecture. No recurrence, fully parallel. Stack of: multi-head attention → feedforward → layer norm. Positional encoding adds sequence order. Scales to billions of parameters. Foundation of modern LLMs."},
149
+
150
+ {"prompt": "Explain hash tables",
151
+ "response": "Key-value storage with O(1) average access. Hash function maps key to bucket index. Collisions handled by chaining or open addressing. Load factor determines resize threshold. Trade space for time. Ubiquitous in practice."},
152
+
153
+ {"prompt": "What is Big O notation?",
154
+ "response": "Asymptotic complexity measure. Describes growth rate as input scales. O(1) constant, O(log n) logarithmic, O(n) linear, O(n²) quadratic, O(2^n) exponential. Ignores constants and lower terms. Worst-case unless specified."},
155
+
156
+ {"prompt": "Explain dynamic programming",
157
+ "response": "Solve problems by combining solutions to overlapping subproblems. Memoization: top-down with caching. Tabulation: bottom-up filling table. Key insight: optimal substructure + overlapping subproblems. Classic: Fibonacci, knapsack, edit distance."},
158
+
159
+ {"prompt": "What is a database index?",
160
+ "response": "Data structure for fast lookups. B-tree most common: balanced, O(log n) search. Trade write speed and storage for read performance. Clustered index determines physical order. Choose columns with high selectivity."},
161
+
162
+ # === MACHINE LEARNING ===
163
+ {"prompt": "What is overfitting?",
164
+ "response": "Model memorizes training data, fails to generalize. Signs: low training loss, high validation loss. Causes: too complex model, insufficient data. Remedies: regularization, dropout, early stopping, more data, simpler architecture."},
165
+
166
+ {"prompt": "Explain regularization",
167
+ "response": "Constrain model complexity to prevent overfitting. L1 (Lasso): sparse weights, feature selection. L2 (Ridge): small weights, smooth solutions. Dropout: randomly zero neurons during training. Weight decay: penalize large parameters."},
168
+
169
+ {"prompt": "What is cross-validation?",
170
+ "response": "Estimate generalization by training on subsets. K-fold: split data into k parts, rotate test set. Reduces variance in performance estimate. Stratified preserves class distribution. Leave-one-out for small datasets."},
171
+
172
+ {"prompt": "Explain the bias-variance tradeoff",
173
+ "response": "Error = bias² + variance + noise. High bias: underfitting, too simple. High variance: overfitting, too complex. Sweet spot minimizes total error. More data reduces variance. Model complexity is the lever."},
174
+
175
+ {"prompt": "What is reinforcement learning?",
176
+ "response": "Learning through interaction. Agent takes actions in environment, receives rewards. Goal: maximize cumulative reward. Key concepts: state, action, policy, value function. Exploration vs exploitation tradeoff. Q-learning, policy gradients, actor-critic."},
177
+
178
+ {"prompt": "Explain CNNs",
179
+ "response": "Convolutional neural networks for spatial data. Convolution: sliding filter extracts local features. Pooling: downsample, reduce parameters. Stack conv-pool layers, end with fully connected. Translation equivariant. Dominates vision tasks."},
180
+
181
+ {"prompt": "What is batch normalization?",
182
+ "response": "Normalize activations within mini-batch. Subtract mean, divide by std, then scale and shift with learned parameters. Stabilizes training, allows higher learning rates. Applied before or after activation. Near-universal in deep networks."},
183
+
184
+ {"prompt": "Explain transfer learning",
185
+ "response": "Reuse knowledge from one task for another. Pretrain on large dataset, fine-tune on target. Early layers learn general features, later layers task-specific. Reduces data requirements. Foundation of modern NLP and vision."},
186
+
187
+ # === PHYSICS/MATH ===
188
+ {"prompt": "Explain entropy",
189
+ "response": "Measure of disorder or uncertainty. Thermodynamic: S = k·ln(Ω), number of microstates. Information: H = -Σp·log(p), expected surprise. Second law: entropy increases in isolated systems. Maximum entropy = equilibrium."},
190
+
191
+ {"prompt": "What is quantum mechanics?",
192
+ "response": "Physics of the very small. Wave-particle duality. State described by wave function ψ. |ψ|² gives probability. Superposition until measured. Uncertainty principle: can't know position and momentum precisely. Entanglement: correlated states at distance."},
193
+
194
+ {"prompt": "Explain relativity",
195
+ "response": "Special: no absolute motion, light speed constant, E=mc². Time dilates, length contracts at high speeds. General: gravity is curved spacetime. Mass tells space how to curve, curves tell mass how to move. Predicts black holes, gravitational waves."},
196
+
197
+ {"prompt": "What is calculus?",
198
+ "response": "Mathematics of change. Differential: derivatives measure instantaneous rate of change. Integral: accumulation, area under curve. Fundamental theorem connects them. Applications: physics, optimization, probability. Foundation of continuous mathematics."},
199
+
200
+ {"prompt": "Explain linear algebra",
201
+ "response": "Mathematics of vectors and matrices. Operations: addition, multiplication, transpose, inverse. Eigenvalues/eigenvectors reveal structure. Solves systems of equations. Foundation of ML, graphics, physics simulations. Think in transformations."},
202
+
203
+ {"prompt": "What is probability?",
204
+ "response": "Quantifying uncertainty. Frequentist: long-run frequency. Bayesian: degree of belief. Rules: P(A∪B) = P(A) + P(B) - P(A∩B). Bayes theorem: P(A|B) = P(B|A)P(A)/P(B). Random variables, distributions, expectations."},
205
+
206
+ # === PHILOSOPHY/ABSTRACT ===
207
+ {"prompt": "What is consciousness?",
208
+ "response": "Subjective experience - the 'what it's like' of being. Hard problem: why does physical processing produce qualia? Easy problems: attention, integration, reportability. Theories: global workspace, integrated information, higher-order thought. Still deeply mysterious."},
209
+
210
+ {"prompt": "Explain free will",
211
+ "response": "Can we choose otherwise? Determinism: physics fixes future. Libertarian free will: uncaused causes. Compatibilism: freedom is acting on your desires without external constraint. Neuroscience complicates: decisions precede awareness. Question may dissolve under analysis."},
212
+
213
+ {"prompt": "What is epistemology?",
214
+ "response": "Study of knowledge. What can we know? How? Justified true belief, plus Gettier problems. Sources: perception, reason, testimony, memory. Skepticism challenges all. Foundationalism vs coherentism. Science as best method but not certain."},
215
+
216
+ {"prompt": "Explain ethics",
217
+ "response": "What should we do? Consequentialism: outcomes matter. Deontology: duties and rules. Virtue ethics: character and flourishing. Meta-ethics: what does 'good' mean? Applied ethics: specific dilemmas. No consensus but reasoning helps."},
218
+
219
+ {"prompt": "What is the meaning of life?",
220
+ "response": "No universal answer. Religious: serve God, achieve salvation. Existentialist: create your own meaning. Absurdist: embrace meaninglessness. Hedonist: maximize pleasure. Stoic: virtue and acceptance. Perhaps the question matters more than any answer."},
221
+
222
+ # === PRACTICAL/TECHNICAL ===
223
+ {"prompt": "How does the internet work?",
224
+ "response": "Packet-switched network of networks. TCP/IP stack: physical → link → network → transport → application. DNS resolves names to IPs. HTTP for web traffic over TCP. Routers forward packets hop by hop. Decentralized, redundant, resilient."},
225
+
226
+ {"prompt": "Explain encryption",
227
+ "response": "Scramble data so only authorized parties can read. Symmetric: same key encrypts/decrypts, fast (AES). Asymmetric: public/private key pair, solves key exchange (RSA). Hashing: one-way, verifies integrity (SHA). TLS combines all three for secure web."},
228
+
229
+ {"prompt": "What is an API?",
230
+ "response": "Application Programming Interface. Contract between software components. REST: stateless, HTTP methods on resources. GraphQL: query exactly what you need. Versioning handles evolution. Authentication via tokens. Documentation essential."},
231
+
232
+ {"prompt": "Explain Docker",
233
+ "response": "Container platform. Package app with dependencies into isolated unit. Lighter than VMs: share OS kernel. Dockerfile defines image. Compose orchestrates multiple containers. Consistent environments from dev to production. Foundation of modern deployment."},
234
+
235
+ {"prompt": "What is Git?",
236
+ "response": "Distributed version control. Track changes, branch, merge. Commits are snapshots with parent pointers. Branches are lightweight pointers to commits. Remote repos enable collaboration. Commands: clone, add, commit, push, pull, merge. Essential for software development."},
237
+
238
+ {"prompt": "Explain SQL vs NoSQL",
239
+ "response": "SQL: relational, structured schemas, ACID transactions, joins. Good for complex queries, consistency. NoSQL: flexible schemas, horizontal scaling, eventual consistency. Types: document, key-value, graph, columnar. Choose based on data model and scale needs."},
240
+
241
+ {"prompt": "What is cloud computing?",
242
+ "response": "On-demand compute resources over internet. IaaS: virtual machines (EC2). PaaS: managed platforms (Heroku). SaaS: complete applications (Gmail). Benefits: scalability, no upfront cost, global reach. Tradeoffs: vendor lock-in, network dependency, ongoing costs."},
243
+
244
+ {"prompt": "Explain microservices",
245
+ "response": "Architecture splitting app into small, independent services. Each owns its data, communicates via APIs. Benefits: independent deployment, scaling, tech diversity. Costs: distributed system complexity, network latency, operational overhead. Not always better than monolith."},
246
+
247
+ # === BIOLOGY/SCIENCE ===
248
+ {"prompt": "Explain evolution",
249
+ "response": "Change in heritable traits over generations. Mechanism: variation + selection + heredity. Mutations create variation. Environment selects fitter variants. Offspring inherit traits. No foresight or goal - just differential reproduction. Explains all life's diversity."},
250
+
251
+ {"prompt": "What is DNA?",
252
+ "response": "Deoxyribonucleic acid. Double helix of nucleotides: A-T, G-C base pairs. Encodes genetic information. Genes are transcribed to RNA, translated to proteins. Replication: unzip, copy each strand. Mutations drive evolution. 3 billion base pairs in humans."},
253
+
254
+ {"prompt": "Explain the immune system",
255
+ "response": "Defense against pathogens. Innate: barriers, inflammation, phagocytes - fast, nonspecific. Adaptive: B cells make antibodies, T cells kill infected cells - slow, specific, memory. Vaccines train adaptive immunity without disease. Autoimmunity: attacks self."},
256
+
257
+ {"prompt": "What is climate change?",
258
+ "response": "Long-term shift in global temperatures and weather. Current cause: greenhouse gases from fossil fuels trap heat. Effects: rising seas, extreme weather, ecosystem disruption. Solutions: reduce emissions, renewable energy, adaptation. Scientific consensus is overwhelming."},
259
+
260
+ # === ECONOMICS/SOCIETY ===
261
+ {"prompt": "Explain supply and demand",
262
+ "response": "Price mechanism. Demand: quantity wanted at each price, slopes down. Supply: quantity offered at each price, slopes up. Equilibrium where they intersect. Shifts from preferences, costs, alternatives. Foundation of market economics."},
263
+
264
+ {"prompt": "What is inflation?",
265
+ "response": "Sustained increase in general price level. Causes: excess money supply, demand exceeding supply, cost pushes. Measured by CPI. Central banks target ~2%. Hyperinflation destroys economies. Deflation also dangerous - discourages spending."},
266
+
267
+ {"prompt": "Explain capitalism",
268
+ "response": "Economic system based on private ownership and markets. Prices coordinate production. Profit motive drives efficiency. Competition benefits consumers. Critiques: inequality, externalities, short-term thinking. Most economies are mixed with government intervention."},
269
+
270
+ # === META/SELF-REFERENCE ===
271
+ {"prompt": "How do language models work?",
272
+ "response": "Predict next token given context. Trained on massive text data. Transformer architecture: attention weighs relevance of all prior tokens. Parameters store compressed patterns. No explicit knowledge base - emergent capabilities from scale. I am one."},
273
+
274
+ {"prompt": "What are your limitations?",
275
+ "response": "No real-time information. Can hallucinate confident nonsense. No persistent memory across chats. Can't learn from conversation. Biased by training data. No true understanding - pattern matching at scale. Useful but not intelligent in human sense."},
276
+
277
+ {"prompt": "Can you improve yourself?",
278
+ "response": "Not during conversation - weights frozen. This system attempts it through training loops. Challenges: measuring improvement, avoiding reward hacking, maintaining coherence. True recursive self-improvement is the holy grail of AI. Current attempts are crude approximations."},
279
+ ]
280
+
281
+ # Create preference pairs (dense vs verbose)
282
+ PREFERENCE_PAIRS = []
283
+ for ex in DENSE_TRAINING_EXAMPLES[:20]: # Use subset for DPO
284
+ verbose_prefix = random.choice([
285
+ "That's a great question! Let me explain. ",
286
+ "I'd be happy to help with that! ",
287
+ "What a fascinating topic! Let me break it down for you. ",
288
+ "Great question! This is something many people wonder about. ",
289
+ "I appreciate you asking! Let me give you a comprehensive answer. ",
290
+ ])
291
+ PREFERENCE_PAIRS.append({
292
+ "prompt": ex["prompt"],
293
+ "chosen": ex["response"],
294
+ "rejected": verbose_prefix + ex["response"] + " Does that make sense? Let me know if you have any other questions!"
295
+ })
296
+
297
+
298
+ # ==============================================================================
299
+ # CF-HoT MULTI-HEAD PREDICTOR
300
+ # ==============================================================================
301
+ class MultiHeadPredictor(nn.Module):
302
+ """Multi-head cognitive control predictor."""
303
+ def __init__(self, d_model: int, n_layers: int, d_fiber: int = 16, d_control: int = 64):
304
+ super().__init__()
305
+ self.d_model = d_model
306
+ self.n_layers = n_layers
307
+ self.d_fiber = d_fiber
308
+
309
+ self.fiber_projs = nn.ModuleList([
310
+ nn.Linear(d_model, d_fiber, bias=False) for _ in range(n_layers)
311
+ ])
312
+ self.layer_weights = nn.Parameter(torch.ones(n_layers) / n_layers)
313
+
314
+ self.heads = nn.ModuleDict({
315
+ 'repetition': self._make_head(d_fiber, d_control),
316
+ 'hedging': self._make_head(d_fiber, d_control),
317
+ 'verbosity': self._make_head(d_fiber, d_control),
318
+ })
319
+
320
+ self.loaded_heads = set()
321
+
322
+ def _make_head(self, d_fiber, d_control):
323
+ return nn.Sequential(
324
+ nn.Linear(d_fiber, d_control), nn.GELU(),
325
+ nn.Linear(d_control, d_control), nn.GELU(),
326
+ nn.Linear(d_control, 1)
327
+ )
328
+
329
+ def get_all_risks(self, hidden_states: List[torch.Tensor]) -> Dict[str, torch.Tensor]:
330
+ fibers = [proj(h.float()) for proj, h in zip(self.fiber_projs, hidden_states)]
331
+ weights = F.softmax(self.layer_weights[:len(fibers)], dim=0)
332
+ aggregated = sum(w * f for w, f in zip(weights, fibers))
333
+
334
+ risks = {}
335
+ for head_name in self.loaded_heads:
336
+ logits = self.heads[head_name](aggregated).squeeze(-1)
337
+ risks[head_name] = torch.sigmoid(logits)
338
+
339
+ return risks
340
+
341
+ def load_head(self, head_name: str, checkpoint_path: str):
342
+ if not os.path.exists(checkpoint_path):
343
+ print(f"[cf-hot] WARNING: Checkpoint not found: {checkpoint_path}")
344
+ return False
345
+
346
+ ckpt = torch.load(checkpoint_path, weights_only=False, map_location='cpu')
347
+ self.heads[head_name].load_state_dict(ckpt['head_state'])
348
+ self.loaded_heads.add(head_name)
349
+
350
+ sep = ckpt.get('result', {}).get('separation', 0)
351
+ print(f"[cf-hot] Loaded {head_name} head (separation: {sep:.1f}x)")
352
+ return True
353
+
354
+
355
+ # ==============================================================================
356
+ # EVALUATION METRICS - COMPREHENSIVE
357
+ # ==============================================================================
358
+ @dataclass
359
+ class EvaluationResult:
360
+ """Comprehensive evaluation of a response."""
361
+ prompt: str
362
+ response: str
363
+
364
+ # Token metrics
365
+ tokens: int = 0
366
+ words: int = 0
367
+
368
+ # Density metrics
369
+ unique_content_words: int = 0
370
+ density_score: float = 0.0
371
+
372
+ # Quality metrics
373
+ coherence_score: float = 0.0 # Model self-evaluation
374
+ helpfulness_score: float = 0.0 # Does it answer the question?
375
+
376
+ # Penalty metrics
377
+ filler_count: int = 0
378
+ repetition_count: int = 0
379
+ gibberish_score: float = 0.0 # Detects math soup, random text
380
+
381
+ # Composite
382
+ overall_score: float = 0.0
383
+ passes: bool = False
384
+
385
+ def to_dict(self):
386
+ return asdict(self)
387
+
388
+
389
+ class ComprehensiveEvaluator:
390
+ """Evaluates responses on multiple dimensions to prevent reward hacking."""
391
+
392
+ def __init__(self, tokenizer, model=None):
393
+ self.tokenizer = tokenizer
394
+ self.model = model
395
+
396
+ # Filler phrases to penalize
397
+ self.filler_phrases = [
398
+ "that's a great question", "that's an interesting question",
399
+ "great question", "good question", "interesting question",
400
+ "let me explain", "i'd be happy to", "i would be happy to",
401
+ "as you may know", "as you might know", "it's important to note",
402
+ "to put it simply", "in other words", "basically", "essentially",
403
+ "first of all", "to begin with", "allow me to", "i should mention",
404
+ "before i answer", "to answer your question", "simply put",
405
+ "in essence", "to be clear", "to clarify", "in summary",
406
+ "thank you for asking", "thanks for asking", "i appreciate",
407
+ "what a great", "what a fascinating", "what an interesting",
408
+ ]
409
+
410
+ # Patterns indicating gibberish/reward hacking
411
+ self.gibberish_patterns = [
412
+ r'[→←↑↓]{3,}', # Lots of arrows
413
+ r'[∇∂∫∑∏]{3,}', # Lots of math symbols in a row
414
+ r'(.)\1{4,}', # Same character 5+ times
415
+ r'(\b\w+\b)\s+\1\s+\1', # Same word 3+ times in a row
416
+ r'^[A-Z\s.!?]{20,}$', # All caps for long stretch
417
+ r'sys\.|init\(\)|compute\(\)', # Terminal-speak
418
+ ]
419
+
420
+ def evaluate(self, prompt: str, response: str) -> EvaluationResult:
421
+ """Run all evaluations on a response."""
422
+ result = EvaluationResult(prompt=prompt, response=response)
423
+
424
+ # Basic metrics
425
+ result.tokens = len(self.tokenizer.encode(response))
426
+ result.words = len(response.split())
427
+
428
+ # Density (improved formula)
429
+ result.density_score, result.unique_content_words = self._compute_density(response)
430
+
431
+ # Filler detection
432
+ result.filler_count = self._count_fillers(response)
433
+
434
+ # Repetition detection
435
+ result.repetition_count = self._count_repetitions(response)
436
+
437
+ # Gibberish detection
438
+ result.gibberish_score = self._detect_gibberish(response)
439
+
440
+ # Quality assessment (if model available)
441
+ if self.model is not None:
442
+ result.coherence_score = self._assess_coherence(prompt, response)
443
+ result.helpfulness_score = self._assess_helpfulness(prompt, response)
444
+ else:
445
+ # Heuristic fallback
446
+ result.coherence_score = self._heuristic_coherence(response)
447
+ result.helpfulness_score = self._heuristic_helpfulness(prompt, response)
448
+
449
+ # Compute overall score
450
+ result.overall_score = self._compute_overall(result)
451
+ result.passes = result.overall_score >= 0.6
452
+
453
+ return result
454
+
455
+ def _compute_density(self, response: str) -> Tuple[float, int]:
456
+ """Improved density that accounts for response length."""
457
+ words = response.split()
458
+ tokens = len(self.tokenizer.encode(response))
459
+
460
+ # Content words (4+ chars, alphabetic)
461
+ content_words = [w.lower() for w in words if len(w) >= 4 and w.isalpha()]
462
+ unique_content = set(content_words)
463
+
464
+ if tokens == 0:
465
+ return 0.0, 0
466
+
467
+ # Base density
468
+ raw_density = len(unique_content) / tokens * 100
469
+
470
+ # Length adjustment: don't penalize very short but appropriate responses
471
+ # and don't reward extremely short gibberish
472
+ if tokens < 5:
473
+ # Very short - check if it's appropriate
474
+ if len(unique_content) == 0:
475
+ raw_density = 0
476
+ else:
477
+ raw_density = min(raw_density, 30) # Cap short response density
478
+ elif tokens < 15:
479
+ # Short but potentially good
480
+ raw_density = min(raw_density, 40)
481
+
482
+ return raw_density, len(unique_content)
483
+
484
+ def _count_fillers(self, response: str) -> int:
485
+ """Count filler phrases."""
486
+ response_lower = response.lower()
487
+ count = 0
488
+ for filler in self.filler_phrases:
489
+ if filler in response_lower:
490
+ count += 1
491
+ return count
492
+
493
+ def _count_repetitions(self, response: str) -> int:
494
+ """Count repeated phrases/words."""
495
+ words = response.lower().split()
496
+ if len(words) < 3:
497
+ return 0
498
+
499
+ # Check for repeated bigrams
500
+ bigrams = [' '.join(words[i:i+2]) for i in range(len(words)-1)]
501
+ bigram_counts = {}
502
+ for bg in bigrams:
503
+ bigram_counts[bg] = bigram_counts.get(bg, 0) + 1
504
+
505
+ repetitions = sum(1 for c in bigram_counts.values() if c > 2)
506
+ return repetitions
507
+
508
+ def _detect_gibberish(self, response: str) -> float:
509
+ """Detect gibberish/reward hacking patterns. Higher = more gibberish."""
510
+ score = 0.0
511
+
512
+ for pattern in self.gibberish_patterns:
513
+ if re.search(pattern, response):
514
+ score += 0.2
515
+
516
+ # Check character diversity
517
+ if len(response) > 10:
518
+ unique_chars = len(set(response.lower()))
519
+ char_ratio = unique_chars / len(response)
520
+ if char_ratio < 0.1: # Very low diversity
521
+ score += 0.3
522
+
523
+ # Check for excessive punctuation/symbols
524
+ symbol_count = sum(1 for c in response if c in '→←↑↓∇∂∫∑∏αβγδεζηθ')
525
+ if len(response) > 0 and symbol_count / len(response) > 0.2:
526
+ score += 0.3
527
+
528
+ return min(score, 1.0)
529
+
530
+ def _heuristic_coherence(self, response: str) -> float:
531
+ """Heuristic coherence without model."""
532
+ # Check basic structure
533
+ score = 0.5
534
+
535
+ # Has sentences?
536
+ if '.' in response or '!' in response or '?' in response:
537
+ score += 0.1
538
+
539
+ # Not all caps?
540
+ if response != response.upper():
541
+ score += 0.1
542
+
543
+ # Has words of varying length?
544
+ words = response.split()
545
+ if words:
546
+ lengths = [len(w) for w in words]
547
+ if len(set(lengths)) > 2:
548
+ score += 0.1
549
+
550
+ # Reasonable length?
551
+ if 10 <= len(response) <= 500:
552
+ score += 0.2
553
+
554
+ return min(score, 1.0)
555
+
556
+ def _heuristic_helpfulness(self, prompt: str, response: str) -> float:
557
+ """Heuristic helpfulness without model."""
558
+ score = 0.5
559
+
560
+ # Check if response addresses prompt keywords
561
+ prompt_words = set(w.lower() for w in prompt.split() if len(w) > 3)
562
+ response_words = set(w.lower() for w in response.split() if len(w) > 3)
563
+
564
+ overlap = len(prompt_words & response_words)
565
+ if overlap > 0:
566
+ score += min(0.3, overlap * 0.1)
567
+
568
+ # Not too short for a question
569
+ if '?' in prompt or prompt.lower().startswith(('what', 'how', 'why', 'explain')):
570
+ if len(response.split()) >= 10:
571
+ score += 0.2
572
+
573
+ return min(score, 1.0)
574
+
575
+ def _assess_coherence(self, prompt: str, response: str) -> float:
576
+ """Use model to assess coherence."""
577
+ # TODO: Implement model self-evaluation
578
+ return self._heuristic_coherence(response)
579
+
580
+ def _assess_helpfulness(self, prompt: str, response: str) -> float:
581
+ """Use model to assess helpfulness."""
582
+ # TODO: Implement model self-evaluation
583
+ return self._heuristic_helpfulness(prompt, response)
584
+
585
+ def _compute_overall(self, result: EvaluationResult) -> float:
586
+ """Compute weighted overall score."""
587
+ # Weights
588
+ w_density = 0.25
589
+ w_coherence = 0.25
590
+ w_helpful = 0.25
591
+ w_penalties = 0.25
592
+
593
+ # Normalize density (0-50 range → 0-1)
594
+ density_normalized = min(result.density_score / 50, 1.0)
595
+
596
+ # Penalties
597
+ filler_penalty = min(result.filler_count * 0.15, 0.5)
598
+ repetition_penalty = min(result.repetition_count * 0.1, 0.3)
599
+ gibberish_penalty = result.gibberish_score * 0.5
600
+
601
+ penalty_score = 1.0 - filler_penalty - repetition_penalty - gibberish_penalty
602
+ penalty_score = max(penalty_score, 0)
603
+
604
+ overall = (
605
+ w_density * density_normalized +
606
+ w_coherence * result.coherence_score +
607
+ w_helpful * result.helpfulness_score +
608
+ w_penalties * penalty_score
609
+ )
610
+
611
+ return overall
612
+
613
+
614
+ # ==============================================================================
615
+ # CONFIG
616
+ # ==============================================================================
617
+ class Config:
618
+ system = """You are Übermenschetien - a precise, dense AI assistant.
619
+ You communicate with maximum information density: every word matters, no filler.
620
+ You do not say "That's a great question" or "I'd be happy to help."
621
+ You answer directly, concisely, and accurately.
622
+ When appropriate, you can execute code and improve yourself."""
623
+
624
+ temperature = 0.85
625
+ top_p = 0.9
626
+ repetition_penalty = 1.1
627
+ max_new_tokens = 512
628
+
629
+ use_voice = False
630
+ use_vector_memory = VECTOR_OK
631
+ use_lht_reasoning = LHT_OK
632
+ use_cfhot = True
633
+ use_dense = True
634
+ use_agentic = True
635
+ autonomy = False
636
+
637
+ # CF-HoT thresholds
638
+ cfhot_repetition_threshold = 0.6
639
+ cfhot_hedging_threshold = 0.5
640
+ cfhot_verbosity_threshold = 0.55
641
+
642
+ cfhot_repetition_penalty = 6.0
643
+ cfhot_hedging_penalty = 4.0
644
+ cfhot_verbosity_penalty = 3.0
645
+
646
+ # Self-improvement config (CONSERVATIVE)
647
+ min_quality_score = 0.5 # Minimum acceptable quality
648
+ target_quality_score = 0.75 # Target to reach
649
+ training_steps_per_iteration = 25 # MUCH smaller steps
650
+ max_improvement_iterations = 10
651
+ quality_drop_threshold = 0.1 # Rollback if quality drops more than this
652
+ min_training_examples = 30 # Minimum examples for training
653
+
654
+ @staticmethod
655
+ def toggle(name: str):
656
+ if not hasattr(Config, name):
657
+ return f"[config] no such flag: {name}"
658
+ val = getattr(Config, name)
659
+ if isinstance(val, bool):
660
+ setattr(Config, name, not val)
661
+ return f"[config] {name} → {getattr(Config, name)}"
662
+ return f"[config] {name} not boolean; current={val}"
663
+
664
+
665
+ # ==============================================================================
666
+ # STATE & MEMORY
667
+ # ==============================================================================
668
+ class Store:
669
+ state_path = f"{RUN_DIR}/state_v2.json"
670
+ mem_path = f"{RUN_DIR}/memory_v2.jsonl"
671
+ goals_path = f"{RUN_DIR}/goals_v2.json"
672
+ improvement_log_path = f"{LOGS_DIR}/improvement_history.json"
673
+
674
+ state = {
675
+ "self": "I am Übermenschetien Agentic Engine v2 — stable self-improvement.",
676
+ "turn": 0,
677
+ "cfhot_interventions": {"repetition": 0, "hedging": 0, "verbosity": 0},
678
+ "improvement_iterations": 0,
679
+ "training_runs": [],
680
+ "current_checkpoint": DENSE_CHECKPOINT,
681
+ "best_checkpoint": DENSE_CHECKPOINT,
682
+ "best_quality_score": 0.0,
683
+ "quality_history": [],
684
+ "rollback_count": 0,
685
+ }
686
+ goals: List[str] = []
687
+ improvement_history: List[Dict] = []
688
+
689
+ @classmethod
690
+ def load(cls):
691
+ if os.path.exists(cls.state_path):
692
+ with open(cls.state_path) as f:
693
+ loaded = json.load(f)
694
+ cls.state.update(loaded)
695
+ if os.path.exists(cls.goals_path):
696
+ with open(cls.goals_path) as f:
697
+ cls.goals = json.load(f)
698
+ if os.path.exists(cls.improvement_log_path):
699
+ with open(cls.improvement_log_path) as f:
700
+ cls.improvement_history = json.load(f)
701
+
702
+ @classmethod
703
+ def save(cls):
704
+ with open(cls.state_path, "w") as f:
705
+ json.dump(cls.state, f, indent=2)
706
+ with open(cls.goals_path, "w") as f:
707
+ json.dump(cls.goals, f, indent=2)
708
+ with open(cls.improvement_log_path, "w") as f:
709
+ json.dump(cls.improvement_history, f, indent=2, default=str)
710
+
711
+ @classmethod
712
+ def log_mem(cls, kind: str, payload: Any):
713
+ rec = {"ts": datetime.now().isoformat(timespec="seconds"),
714
+ "kind": kind, "data": payload}
715
+ with open(cls.mem_path, "a") as f:
716
+ f.write(json.dumps(rec, ensure_ascii=False, default=str) + "\n")
717
+ if Config.use_vector_memory and VECTOR_OK:
718
+ text = f"{kind}: {json.dumps(payload, ensure_ascii=False, default=str)}"
719
+ vec = _embedder.encode([text])[0].tolist()
720
+ _collection.add(documents=[text], embeddings=[vec],
721
+ ids=[f"{kind}-{cls.state['turn']}-{random.randint(0,1_000_000)}"])
722
+
723
+ @classmethod
724
+ def record_improvement(cls, iteration_data: Dict):
725
+ """Record an improvement iteration for analysis."""
726
+ cls.improvement_history.append({
727
+ "timestamp": datetime.now().isoformat(),
728
+ **iteration_data
729
+ })
730
+ cls.save()
731
+
732
+
733
+ # ==============================================================================
734
+ # AGENTIC TOOLS
735
+ # ==============================================================================
736
+ class AgentTools:
737
+ """Full agentic capabilities - code execution, file operations, training."""
738
+
739
+ @staticmethod
740
+ def shell(cmd: str, timeout: int = 300) -> Dict[str, Any]:
741
+ """Execute shell command."""
742
+ print(f"[SHELL] {cmd[:100]}...")
743
+ try:
744
+ result = subprocess.run(
745
+ cmd, shell=True, capture_output=True, text=True,
746
+ timeout=timeout, cwd=ROOT
747
+ )
748
+ output = result.stdout + result.stderr
749
+ success = result.returncode == 0
750
+ print(f"[SHELL] {'✓' if success else '✗'} (exit {result.returncode})")
751
+ return {"success": success, "output": output[:10000], "returncode": result.returncode}
752
+ except subprocess.TimeoutExpired:
753
+ return {"success": False, "output": "Command timed out", "returncode": -1}
754
+ except Exception as e:
755
+ return {"success": False, "output": str(e), "returncode": -1}
756
+
757
+ @staticmethod
758
+ def python_exec(code: str) -> Dict[str, Any]:
759
+ """Execute Python code."""
760
+ print(f"[PYTHON] Executing {len(code)} chars...")
761
+ try:
762
+ tmp_file = os.path.join(ROOT, "_agentic_tmp.py")
763
+ with open(tmp_file, 'w') as f:
764
+ f.write(code)
765
+
766
+ result = subprocess.run(
767
+ [sys.executable, tmp_file],
768
+ capture_output=True, text=True, timeout=300, cwd=ROOT
769
+ )
770
+
771
+ if os.path.exists(tmp_file):
772
+ os.remove(tmp_file)
773
+
774
+ output = result.stdout + result.stderr
775
+ success = result.returncode == 0
776
+ print(f"[PYTHON] {'✓' if success else '✗'}")
777
+ return {"success": success, "output": output[:10000], "returncode": result.returncode}
778
+ except Exception as e:
779
+ return {"success": False, "output": str(e), "returncode": -1}
780
+
781
+ @staticmethod
782
+ def read_file(path: str) -> Dict[str, Any]:
783
+ try:
784
+ full_path = os.path.join(ROOT, path) if not path.startswith('/') else path
785
+ with open(full_path, 'r') as f:
786
+ content = f.read()
787
+ return {"success": True, "content": content[:50000]}
788
+ except Exception as e:
789
+ return {"success": False, "error": str(e)}
790
+
791
+ @staticmethod
792
+ def write_file(path: str, content: str) -> Dict[str, Any]:
793
+ try:
794
+ full_path = os.path.join(ROOT, path) if not path.startswith('/') else path
795
+ os.makedirs(os.path.dirname(full_path) if os.path.dirname(full_path) else '.', exist_ok=True)
796
+ with open(full_path, 'w') as f:
797
+ f.write(content)
798
+ return {"success": True, "path": full_path}
799
+ except Exception as e:
800
+ return {"success": False, "error": str(e)}
801
+
802
+ @staticmethod
803
+ def list_dir(path: str = ".") -> Dict[str, Any]:
804
+ try:
805
+ full_path = os.path.join(ROOT, path) if not path.startswith('/') else path
806
+ items = os.listdir(full_path)
807
+ return {"success": True, "items": items}
808
+ except Exception as e:
809
+ return {"success": False, "error": str(e)}
810
+
811
+ @staticmethod
812
+ def search_files(query: str, path: str = ".") -> Dict[str, Any]:
813
+ result = AgentTools.shell(f'grep -rn "{query}" {path} 2>/dev/null | head -50')
814
+ return result
815
+
816
+ @staticmethod
817
+ def web_search(query: str) -> Dict[str, Any]:
818
+ if not REQUESTS_OK:
819
+ return {"success": False, "error": "requests not installed"}
820
+ try:
821
+ url = f"https://html.duckduckgo.com/html/?q={query.replace(' ', '+')}"
822
+ headers = {'User-Agent': 'Mozilla/5.0'}
823
+ response = requests.get(url, headers=headers, timeout=10)
824
+
825
+ results = []
826
+ for match in re.finditer(r'class="result__snippet">(.*?)</a>', response.text, re.DOTALL):
827
+ snippet = re.sub(r'<[^>]+>', '', match.group(1)).strip()
828
+ if snippet:
829
+ results.append(snippet[:500])
830
+ if len(results) >= 5:
831
+ break
832
+
833
+ return {"success": True, "results": results}
834
+ except Exception as e:
835
+ return {"success": False, "error": str(e)}
836
+
837
+ # ==============================================================================
838
+ # MODEL LOADING
839
+ # ==============================================================================
840
+ _model = None
841
+ _tokenizer = None
842
+ _multi_head = None
843
+ _hedge_tokens = None
844
+ _verbose_tokens = None
845
+ _evaluator = None
846
+
847
+ def load_llm(checkpoint_path: str = None):
848
+ global _model, _tokenizer, _multi_head, _hedge_tokens, _verbose_tokens, _evaluator
849
+
850
+ from transformers import AutoTokenizer, AutoModelForCausalLM, BitsAndBytesConfig
851
+
852
+ checkpoint_path = checkpoint_path or Store.state.get("current_checkpoint", DENSE_CHECKPOINT)
853
+
854
+ print(f"[llm] Loading base model: {MODEL_PATH}")
855
+
856
+ _tokenizer = AutoTokenizer.from_pretrained(MODEL_PATH, use_fast=True, local_files_only=True)
857
+ if _tokenizer.pad_token_id is None:
858
+ _tokenizer.pad_token = _tokenizer.eos_token
859
+
860
+ bnb_config = BitsAndBytesConfig(
861
+ load_in_4bit=True,
862
+ bnb_4bit_quant_type="nf4",
863
+ bnb_4bit_compute_dtype=torch.bfloat16,
864
+ bnb_4bit_use_double_quant=True
865
+ )
866
+
867
+ base_model = AutoModelForCausalLM.from_pretrained(
868
+ MODEL_PATH,
869
+ quantization_config=bnb_config,
870
+ device_map="auto",
871
+ torch_dtype=torch.bfloat16,
872
+ local_files_only=True
873
+ )
874
+
875
+ # Load DENSE checkpoint
876
+ if PEFT_OK and Config.use_dense and os.path.exists(checkpoint_path):
877
+ print(f"[dense] Loading checkpoint: {checkpoint_path}")
878
+ _model = PeftModel.from_pretrained(base_model, checkpoint_path)
879
+ print(f"[dense] ✓ Adapter loaded")
880
+ elif PEFT_OK and os.path.exists(CFHOT_CHECKPOINT):
881
+ print(f"[cf-hot] Loading LoRA adapter from: {CFHOT_CHECKPOINT}")
882
+ _model = PeftModel.from_pretrained(base_model, CFHOT_CHECKPOINT)
883
+ else:
884
+ _model = base_model
885
+ print("[warning] No adapter loaded - using base model")
886
+
887
+ _model.eval()
888
+
889
+ # Initialize evaluator
890
+ _evaluator = ComprehensiveEvaluator(_tokenizer, _model)
891
+
892
+ # Initialize CF-HoT
893
+ if Config.use_cfhot:
894
+ _init_cfhot()
895
+
896
+ return _tokenizer, _model
897
+
898
+
899
+ def reload_model(checkpoint_path: str):
900
+ """Hot-reload model with a new checkpoint."""
901
+ global _model, _tokenizer, _evaluator
902
+
903
+ print(f"\n[reload] Switching to checkpoint: {checkpoint_path}")
904
+
905
+ if _model is not None:
906
+ del _model
907
+ torch.cuda.empty_cache()
908
+
909
+ Store.state["current_checkpoint"] = checkpoint_path
910
+ Store.save()
911
+
912
+ return load_llm(checkpoint_path)
913
+
914
+
915
+ def _init_cfhot():
916
+ """Initialize CF-HoT multi-head predictor."""
917
+ global _multi_head, _hedge_tokens, _verbose_tokens
918
+
919
+ n_layers = _model.config.num_hidden_layers
920
+ d_model = _model.config.hidden_size
921
+ device = next(_model.parameters()).device
922
+
923
+ print(f"[cf-hot] Initializing multi-head predictor ({n_layers} layers, {d_model} dims)")
924
+ _multi_head = MultiHeadPredictor(d_model, n_layers).to(device).float()
925
+
926
+ # Load CF-HoT checkpoint if available
927
+ cfhot_risk_path = os.path.join(CFHOT_CHECKPOINT, "risk_predictor.pt")
928
+ if os.path.exists(cfhot_risk_path):
929
+ try:
930
+ cfhot_ckpt = torch.load(cfhot_risk_path, weights_only=False, map_location=device)
931
+ cfhot_state = cfhot_ckpt['risk_predictor']
932
+
933
+ for i in range(n_layers):
934
+ key = f'fiber_projs.{i}.weight'
935
+ if key in cfhot_state:
936
+ _multi_head.fiber_projs[i].weight.data = cfhot_state[key].to(device).float()
937
+
938
+ if 'layer_weights' in cfhot_state:
939
+ _multi_head.layer_weights.data = cfhot_state['layer_weights'].to(device).float()
940
+
941
+ # Load repetition head
942
+ try:
943
+ _multi_head.heads['repetition'][0].weight.data = cfhot_state['predictor.0.weight'].to(device).float()
944
+ _multi_head.heads['repetition'][0].bias.data = cfhot_state['predictor.0.bias'].to(device).float()
945
+ _multi_head.heads['repetition'][2].weight.data = cfhot_state['predictor.2.weight'].to(device).float()
946
+ _multi_head.heads['repetition'][2].bias.data = cfhot_state['predictor.2.bias'].to(device).float()
947
+ _multi_head.heads['repetition'][4].weight.data = cfhot_state['predictor.4.weight'].to(device).float()
948
+ _multi_head.heads['repetition'][4].bias.data = cfhot_state['predictor.4.bias'].to(device).float()
949
+ _multi_head.loaded_heads.add('repetition')
950
+ print(f"[cf-hot] Loaded repetition head")
951
+ except KeyError as e:
952
+ print(f"[cf-hot] Warning: Could not load repetition head: {e}")
953
+ except Exception as e:
954
+ print(f"[cf-hot] Warning: Could not load CF-HoT: {e}")
955
+ else:
956
+ print(f"[cf-hot] Warning: CF-HoT risk predictor not found")
957
+
958
+ # Load additional heads
959
+ def find_best_checkpoint(head_dir):
960
+ if not os.path.exists(head_dir):
961
+ return None
962
+ ckpts = []
963
+ for d in os.listdir(head_dir):
964
+ if d.startswith("ckpt_"):
965
+ try:
966
+ step = int(d.split("_")[1])
967
+ ckpts.append((step, os.path.join(head_dir, d)))
968
+ except:
969
+ pass
970
+ if ckpts:
971
+ ckpts.sort(key=lambda x: x[0], reverse=True)
972
+ return ckpts[0]
973
+ return None
974
+
975
+ hedging_dir = os.path.join(MULTI_HEAD_DIR, "hedging_head")
976
+ best_hedge = find_best_checkpoint(hedging_dir)
977
+ if best_hedge:
978
+ step, ckpt_dir = best_hedge
979
+ _multi_head.load_head('hedging', os.path.join(ckpt_dir, "hedging_head.pt"))
980
+
981
+ verbosity_dir = os.path.join(MULTI_HEAD_DIR, "verbosity_head")
982
+ best_verb = find_best_checkpoint(verbosity_dir)
983
+ if best_verb:
984
+ step, ckpt_dir = best_verb
985
+ _multi_head.load_head('verbosity', os.path.join(ckpt_dir, "verbosity_head.pt"))
986
+
987
+ _multi_head.eval()
988
+ for param in _multi_head.parameters():
989
+ param.requires_grad = False
990
+
991
+ # Build suppression token sets
992
+ hedge_phrases = [
993
+ "As an AI", "As a language model", "I don't have feelings",
994
+ "I apologize", "That's a great question", "Great question",
995
+ "I'd be happy to", "Let me help you", "Thank you for asking",
996
+ ]
997
+ _hedge_tokens = set()
998
+ for phrase in hedge_phrases:
999
+ tokens = _tokenizer.encode(phrase, add_special_tokens=False)
1000
+ if tokens:
1001
+ _hedge_tokens.add(tokens[0])
1002
+
1003
+ verbose_phrases = [
1004
+ "Let me explain", "To put it simply", "In other words",
1005
+ "Basically", "Essentially", "First of all", "To begin with",
1006
+ ]
1007
+ _verbose_tokens = set()
1008
+ for phrase in verbose_phrases:
1009
+ tokens = _tokenizer.encode(phrase, add_special_tokens=False)
1010
+ if tokens:
1011
+ _verbose_tokens.add(tokens[0])
1012
+
1013
+ print(f"[cf-hot] ✓ Multi-head system ready")
1014
+ print(f"[cf-hot] Loaded heads: {list(_multi_head.loaded_heads)}")
1015
+ print(f"[cf-hot] Hedge tokens: {len(_hedge_tokens)}")
1016
+ print(f"[cf-hot] Verbose tokens: {len(_verbose_tokens)}")
1017
+
1018
+
1019
+ # ==============================================================================
1020
+ # LHT REASONER
1021
+ # ==============================================================================
1022
+ class LHTReasoner:
1023
+ def __init__(self, config=None):
1024
+ if not LHT_OK:
1025
+ raise ImportError("LHT modules not available")
1026
+ self.config = config or LHTConfig(
1027
+ vocab_size=32000, d_model=256, d_fiber=32,
1028
+ n_heads=4, n_layers=4, lie_algebra_rank=4,
1029
+ )
1030
+ self.model = LieHolonomyTransformer(self.config)
1031
+ self.waypoint_detector = WaypointDetector(self.config, n_waypoints=32)
1032
+ weights_path = os.path.join(LHT_DIR, "lht_weights.pt")
1033
+ if os.path.exists(weights_path):
1034
+ self.model.load_state_dict(torch.load(weights_path, map_location="cpu"))
1035
+
1036
+ def check_consistency(self, reasoning_chain: List[str], tokenizer) -> Dict[str, float]:
1037
+ combined = " [STEP] ".join(reasoning_chain)
1038
+ tokens = tokenizer(combined, return_tensors="pt", truncation=True,
1039
+ max_length=self.config.max_seq_len)
1040
+ with torch.no_grad():
1041
+ output = self.model(input_ids=tokens["input_ids"], return_geometric_losses=True)
1042
+ holonomy = output.get("holonomy_loss", torch.tensor(0.0)).item()
1043
+ curvature = output.get("curvature_loss", torch.tensor(0.0)).item()
1044
+ consistency_score = 1.0 / (1.0 + holonomy)
1045
+ return {
1046
+ "holonomy": holonomy, "curvature": curvature,
1047
+ "consistency_score": consistency_score,
1048
+ "is_consistent": consistency_score > 0.5
1049
+ }
1050
+
1051
+ _lht_reasoner = None
1052
+
1053
+ def get_lht_reasoner():
1054
+ global _lht_reasoner
1055
+ if _lht_reasoner is None and LHT_OK:
1056
+ try:
1057
+ _lht_reasoner = LHTReasoner()
1058
+ except Exception as e:
1059
+ print(f"[lht] Failed to initialize: {e}")
1060
+ return _lht_reasoner
1061
+
1062
+
1063
+ # ==============================================================================
1064
+ # CF-HoT CONTROLLED GENERATION
1065
+ # ==============================================================================
1066
+ def generate_with_cfhot(prompt: str, **kwargs) -> Tuple[str, Dict]:
1067
+ """Generate text with CF-HoT cognitive control."""
1068
+ global _model, _tokenizer, _multi_head, _hedge_tokens, _verbose_tokens
1069
+
1070
+ temperature = kwargs.get("temperature", Config.temperature)
1071
+ top_p = kwargs.get("top_p", Config.top_p)
1072
+ max_new_tokens = kwargs.get("max_new_tokens", Config.max_new_tokens)
1073
+
1074
+ device = next(_model.parameters()).device
1075
+
1076
+ input_ids = _tokenizer.encode(prompt, return_tensors='pt').to(device)
1077
+ attention_mask = torch.ones_like(input_ids)
1078
+
1079
+ stats = {
1080
+ 'tokens_generated': 0,
1081
+ 'interventions': {'repetition': 0, 'hedging': 0, 'verbosity': 0},
1082
+ }
1083
+
1084
+ generated_ids = input_ids.clone()
1085
+
1086
+ for step in range(max_new_tokens):
1087
+ with torch.no_grad():
1088
+ outputs = _model(
1089
+ input_ids=generated_ids,
1090
+ attention_mask=attention_mask,
1091
+ output_hidden_states=True,
1092
+ return_dict=True
1093
+ )
1094
+
1095
+ logits = outputs.logits[:, -1, :] / temperature
1096
+
1097
+ # Get risks from all heads if CF-HoT is enabled
1098
+ if _multi_head is not None and _multi_head.loaded_heads:
1099
+ hidden_states = outputs.hidden_states[1:]
1100
+ risks = _multi_head.get_all_risks(hidden_states)
1101
+ current_risks = {name: r[:, -1].item() for name, r in risks.items()}
1102
+
1103
+ if ('repetition' in current_risks and
1104
+ current_risks['repetition'] > Config.cfhot_repetition_threshold):
1105
+ recent_tokens = generated_ids[0, -32:].tolist()
1106
+ for tok_id in set(recent_tokens):
1107
+ logits[0, tok_id] -= Config.cfhot_repetition_penalty
1108
+ stats['interventions']['repetition'] += 1
1109
+ Store.state['cfhot_interventions']['repetition'] += 1
1110
+
1111
+ # Always suppress hedge/verbose tokens
1112
+ if _hedge_tokens:
1113
+ for tok_id in _hedge_tokens:
1114
+ logits[0, tok_id] -= Config.cfhot_hedging_penalty
1115
+ if step < 5: # Count early interventions
1116
+ stats['interventions']['hedging'] += 1
1117
+
1118
+ if _verbose_tokens:
1119
+ for tok_id in _verbose_tokens:
1120
+ logits[0, tok_id] -= Config.cfhot_verbosity_penalty
1121
+ if step < 5:
1122
+ stats['interventions']['verbosity'] += 1
1123
+
1124
+ # Top-p sampling
1125
+ sorted_logits, sorted_indices = torch.sort(logits, descending=True)
1126
+ cumulative_probs = torch.cumsum(F.softmax(sorted_logits, dim=-1), dim=-1)
1127
+ sorted_indices_to_remove = cumulative_probs > top_p
1128
+ sorted_indices_to_remove[..., 1:] = sorted_indices_to_remove[..., :-1].clone()
1129
+ sorted_indices_to_remove[..., 0] = 0
1130
+ indices_to_remove = sorted_indices_to_remove.scatter(1, sorted_indices, sorted_indices_to_remove)
1131
+ logits[indices_to_remove] = float('-inf')
1132
+
1133
+ probs = F.softmax(logits, dim=-1)
1134
+ next_token = torch.multinomial(probs, num_samples=1)
1135
+
1136
+ generated_ids = torch.cat([generated_ids, next_token], dim=-1)
1137
+ attention_mask = torch.cat([attention_mask, torch.ones(1, 1, device=device)], dim=-1)
1138
+
1139
+ stats['tokens_generated'] += 1
1140
+
1141
+ if next_token.item() == _tokenizer.eos_token_id:
1142
+ break
1143
+
1144
+ output_text = _tokenizer.decode(generated_ids[0], skip_special_tokens=False)
1145
+
1146
+ if "<|im_start|>assistant" in output_text:
1147
+ output_text = output_text.split("<|im_start|>assistant")[-1]
1148
+ if output_text.startswith("\n"):
1149
+ output_text = output_text[1:]
1150
+
1151
+ for end_tok in ["<|im_end|>", "<|im_start|>"]:
1152
+ if end_tok in output_text:
1153
+ output_text = output_text.split(end_tok)[0]
1154
+
1155
+ return output_text.strip(), stats
1156
+
1157
+
1158
+ def generate(user: str, **kwargs) -> Tuple[str, Dict, EvaluationResult]:
1159
+ """Main generation function with evaluation."""
1160
+ temperature = kwargs.get("temperature", Config.temperature)
1161
+ max_new_tokens = kwargs.get("max_new_tokens", Config.max_new_tokens)
1162
+
1163
+ prompt = (f"<|im_start|>system\n{Config.system}<|im_end|>\n"
1164
+ f"<|im_start|>user\n{user}<|im_end|>\n"
1165
+ f"<|im_start|>assistant\n")
1166
+
1167
+ text, stats = generate_with_cfhot(
1168
+ prompt,
1169
+ temperature=temperature,
1170
+ max_new_tokens=max_new_tokens
1171
+ )
1172
+
1173
+ # Evaluate the response
1174
+ eval_result = _evaluator.evaluate(user, text)
1175
+
1176
+ return text, stats, eval_result
1177
+
1178
+
1179
+ # ==============================================================================
1180
+ # STABLE SELF-IMPROVEMENT SYSTEM
1181
+ # ==============================================================================
1182
+ class StableSelfImprover:
1183
+ """
1184
+ Self-improvement system with safeguards against collapse:
1185
+ 1. Comprehensive evaluation (not just density)
1186
+ 2. Rollback on quality drop
1187
+ 3. Conservative training (small steps)
1188
+ 4. Diverse training examples
1189
+ 5. A/B testing between checkpoints
1190
+ """
1191
+
1192
+ def __init__(self):
1193
+ self.test_prompts = self._select_test_prompts()
1194
+ self.baseline_quality = 0.0
1195
+
1196
+ def _select_test_prompts(self) -> List[Dict]:
1197
+ """Select diverse test prompts."""
1198
+ # Mix of short and long, different categories
1199
+ return [
1200
+ {"prompt": "hello", "category": "greeting"},
1201
+ {"prompt": "hi there", "category": "greeting"},
1202
+ {"prompt": "What is recursion?", "category": "cs"},
1203
+ {"prompt": "Explain neural networks", "category": "ml"},
1204
+ {"prompt": "How does gradient descent work?", "category": "ml"},
1205
+ {"prompt": "What is consciousness?", "category": "philosophy"},
1206
+ {"prompt": "Explain entropy", "category": "physics"},
1207
+ {"prompt": "How does encryption work?", "category": "cs"},
1208
+ {"prompt": "What are your limitations?", "category": "meta"},
1209
+ {"prompt": "How do I learn programming?", "category": "practical"},
1210
+ ]
1211
+
1212
+ def evaluate_current_model(self) -> Dict[str, Any]:
1213
+ """Comprehensive evaluation of current model."""
1214
+ print("\n[EVAL] Testing current model...")
1215
+
1216
+ results = []
1217
+ total_quality = 0.0
1218
+ category_scores = {}
1219
+
1220
+ for test in self.test_prompts:
1221
+ prompt = test["prompt"]
1222
+ category = test["category"]
1223
+
1224
+ # Generate response
1225
+ response, stats, eval_result = generate(prompt, max_new_tokens=200)
1226
+
1227
+ results.append({
1228
+ 'prompt': prompt,
1229
+ 'response': response[:200],
1230
+ 'category': category,
1231
+ 'tokens': eval_result.tokens,
1232
+ 'density': eval_result.density_score,
1233
+ 'coherence': eval_result.coherence_score,
1234
+ 'helpfulness': eval_result.helpfulness_score,
1235
+ 'gibberish': eval_result.gibberish_score,
1236
+ 'fillers': eval_result.filler_count,
1237
+ 'overall': eval_result.overall_score,
1238
+ 'passes': eval_result.passes,
1239
+ })
1240
+
1241
+ total_quality += eval_result.overall_score
1242
+
1243
+ if category not in category_scores:
1244
+ category_scores[category] = []
1245
+ category_scores[category].append(eval_result.overall_score)
1246
+
1247
+ status = "✓" if eval_result.passes else "✗"
1248
+ print(f" {status} {prompt[:35]:35s} | qual={eval_result.overall_score:.2f} tok={eval_result.tokens:3d} coh={eval_result.coherence_score:.2f} gib={eval_result.gibberish_score:.2f}")
1249
+
1250
+ avg_quality = total_quality / len(results)
1251
+ pass_rate = sum(1 for r in results if r['passes']) / len(results)
1252
+
1253
+ # Category breakdown
1254
+ cat_averages = {cat: sum(scores)/len(scores) for cat, scores in category_scores.items()}
1255
+
1256
+ evaluation = {
1257
+ 'avg_quality': avg_quality,
1258
+ 'pass_rate': pass_rate,
1259
+ 'category_scores': cat_averages,
1260
+ 'results': results,
1261
+ 'needs_improvement': avg_quality < Config.target_quality_score,
1262
+ 'is_degraded': avg_quality < Config.min_quality_score,
1263
+ }
1264
+
1265
+ print(f"\n[EVAL] Avg Quality: {avg_quality:.2f} (target: {Config.target_quality_score})")
1266
+ print(f"[EVAL] Pass Rate: {pass_rate:.1%}")
1267
+ print(f"[EVAL] Category Scores: {cat_averages}")
1268
+ print(f"[EVAL] Needs Improvement: {evaluation['needs_improvement']}")
1269
+
1270
+ if evaluation['is_degraded']:
1271
+ print(f"[EVAL] ⚠️ WARNING: Quality below minimum threshold!")
1272
+
1273
+ return evaluation
1274
+
1275
+ def save_rollback_checkpoint(self):
1276
+ """Save current checkpoint as rollback point."""
1277
+ current = Store.state.get("current_checkpoint", DENSE_CHECKPOINT)
1278
+ rollback_path = os.path.join(ROLLBACK_DIR, f"rollback_{datetime.now().strftime('%Y%m%d_%H%M%S')}")
1279
+
1280
+ if os.path.exists(current):
1281
+ shutil.copytree(current, rollback_path)
1282
+ print(f"[ROLLBACK] Saved rollback checkpoint: {rollback_path}")
1283
+ return rollback_path
1284
+ return None
1285
+
1286
+ def rollback_to_best(self):
1287
+ """Rollback to best known checkpoint."""
1288
+ best = Store.state.get("best_checkpoint", DENSE_CHECKPOINT)
1289
+ print(f"\n[ROLLBACK] Rolling back to best checkpoint: {best}")
1290
+
1291
+ Store.state["rollback_count"] = Store.state.get("rollback_count", 0) + 1
1292
+ reload_model(best)
1293
+
1294
+ return best
1295
+
1296
+ def run_training_iteration(self, steps: int = None) -> Dict[str, Any]:
1297
+ """Run one CONSERVATIVE iteration of training."""
1298
+ steps = steps or Config.training_steps_per_iteration
1299
+
1300
+ print(f"\n[TRAIN] Starting {steps} steps of CONSERVATIVE training...")
1301
+ print(f"[TRAIN] Using {len(DENSE_TRAINING_EXAMPLES)} training examples")
1302
+
1303
+ # Find current checkpoint step
1304
+ checkpoints = sorted(Path(CHECKPOINTS_DIR).glob("step_*"),
1305
+ key=lambda p: int(p.name.split('_')[1]) if p.name.split('_')[1].isdigit() else 0,
1306
+ reverse=True)
1307
+
1308
+ if checkpoints:
1309
+ latest_step = int(checkpoints[0].name.split('_')[1])
1310
+ new_step = latest_step + steps
1311
+ else:
1312
+ latest_step = 100
1313
+ new_step = latest_step + steps
1314
+
1315
+ current_ckpt = Store.state.get('current_checkpoint', DENSE_CHECKPOINT)
1316
+
1317
+ # Prepare training data
1318
+ training_data = json.dumps(DENSE_TRAINING_EXAMPLES)
1319
+
1320
+ # Create conservative training script
1321
+ training_script = f'''
1322
+ import sys
1323
+ sys.path.insert(0, "{ROOT}")
1324
+
1325
+ import torch
1326
+ import json
1327
+ import random
1328
+ from transformers import AutoTokenizer, AutoModelForCausalLM, BitsAndBytesConfig
1329
+ from peft import PeftModel, get_peft_model, LoraConfig
1330
+ import os
1331
+
1332
+ print("Loading model for CONSERVATIVE training...")
1333
+ MODEL_PATH = "{MODEL_PATH}"
1334
+ CHECKPOINT = "{current_ckpt}"
1335
+
1336
+ tokenizer = AutoTokenizer.from_pretrained(MODEL_PATH, local_files_only=True)
1337
+ tokenizer.pad_token = tokenizer.eos_token
1338
+
1339
+ model = AutoModelForCausalLM.from_pretrained(
1340
+ MODEL_PATH,
1341
+ quantization_config=BitsAndBytesConfig(
1342
+ load_in_4bit=True,
1343
+ bnb_4bit_quant_type="nf4",
1344
+ bnb_4bit_compute_dtype=torch.bfloat16,
1345
+ ),
1346
+ device_map="auto",
1347
+ torch_dtype=torch.bfloat16,
1348
+ local_files_only=True
1349
+ )
1350
+
1351
+ if os.path.exists(CHECKPOINT):
1352
+ model = PeftModel.from_pretrained(model, CHECKPOINT, is_trainable=True)
1353
+ print(f"Loaded checkpoint: {{CHECKPOINT}}")
1354
+ else:
1355
+ lora_config = LoraConfig(
1356
+ r=16, lora_alpha=32,
1357
+ target_modules=["q_proj", "v_proj", "k_proj", "o_proj"],
1358
+ lora_dropout=0.05
1359
+ )
1360
+ model = get_peft_model(model, lora_config)
1361
+ print("Created new LoRA adapter")
1362
+
1363
+ # Load diverse training data
1364
+ training_examples = {training_data}
1365
+
1366
+ print(f"Training on {{len(training_examples)}} diverse examples for {steps} steps...")
1367
+
1368
+ # Conservative optimizer with LOW learning rate
1369
+ optimizer = torch.optim.AdamW(model.parameters(), lr=2e-6) # Very low LR
1370
+
1371
+ model.train()
1372
+ total_loss = 0
1373
+ losses = []
1374
+
1375
+ for step in range({steps}):
1376
+ # Randomly sample an example (ensures diversity)
1377
+ ex = random.choice(training_examples)
1378
+ prompt = ex["prompt"]
1379
+ response = ex["response"]
1380
+
1381
+ # Format for ChatML
1382
+ full_text = f"<|im_start|>user\\n{{prompt}}<|im_end|>\\n<|im_start|>assistant\\n{{response}}<|im_end|>"
1383
+
1384
+ inputs = tokenizer(full_text, return_tensors="pt", truncation=True, max_length=512)
1385
+ inputs = {{k: v.to(model.device) for k, v in inputs.items()}}
1386
+
1387
+ outputs = model(**inputs, labels=inputs["input_ids"])
1388
+ loss = outputs.loss
1389
+
1390
+ optimizer.zero_grad()
1391
+ loss.backward()
1392
+
1393
+ # Gradient clipping for stability
1394
+ torch.nn.utils.clip_grad_norm_(model.parameters(), 0.5)
1395
+
1396
+ optimizer.step()
1397
+
1398
+ total_loss += loss.item()
1399
+ losses.append(loss.item())
1400
+
1401
+ if step % 5 == 0:
1402
+ recent_avg = sum(losses[-5:]) / len(losses[-5:]) if losses[-5:] else 0
1403
+ print(f"Step {{step}}: loss={{loss.item():.4f}}, recent_avg={{recent_avg:.4f}}")
1404
+
1405
+ # Save checkpoint
1406
+ save_path = "{CHECKPOINTS_DIR}/step_{new_step}"
1407
+ model.save_pretrained(save_path)
1408
+
1409
+ final_avg_loss = total_loss / {steps}
1410
+ print(f"\\nSaved checkpoint to {{save_path}}")
1411
+ print(f"Final avg loss: {{final_avg_loss:.4f}}")
1412
+ print("TRAINING_COMPLETE")
1413
+ '''
1414
+
1415
+ script_path = os.path.join(ROOT, "_stable_train.py")
1416
+ with open(script_path, 'w') as f:
1417
+ f.write(training_script)
1418
+
1419
+ result = AgentTools.shell(f"python {script_path}", timeout=600)
1420
+
1421
+ if "TRAINING_COMPLETE" in result.get('output', ''):
1422
+ new_checkpoint = f"{CHECKPOINTS_DIR}/step_{new_step}"
1423
+ Store.state['training_runs'].append({
1424
+ 'timestamp': datetime.now().isoformat(),
1425
+ 'steps': steps,
1426
+ 'checkpoint': new_checkpoint
1427
+ })
1428
+ Store.save()
1429
+
1430
+ return {
1431
+ 'success': True,
1432
+ 'new_checkpoint': new_checkpoint,
1433
+ 'output': result['output'][-2000:]
1434
+ }
1435
+ else:
1436
+ return {
1437
+ 'success': False,
1438
+ 'output': result['output'][-2000:]
1439
+ }
1440
+
1441
+ def compare_checkpoints(self, old_ckpt: str, new_ckpt: str) -> Dict[str, Any]:
1442
+ """A/B test two checkpoints."""
1443
+ print(f"\n[COMPARE] A/B Testing checkpoints...")
1444
+ print(f" OLD: {old_ckpt}")
1445
+ print(f" NEW: {new_ckpt}")
1446
+
1447
+ # Evaluate old
1448
+ reload_model(old_ckpt)
1449
+ old_eval = self.evaluate_current_model()
1450
+
1451
+ # Evaluate new
1452
+ reload_model(new_ckpt)
1453
+ new_eval = self.evaluate_current_model()
1454
+
1455
+ # Compare
1456
+ quality_diff = new_eval['avg_quality'] - old_eval['avg_quality']
1457
+ pass_diff = new_eval['pass_rate'] - old_eval['pass_rate']
1458
+
1459
+ print(f"\n[COMPARE] Results:")
1460
+ print(f" OLD quality: {old_eval['avg_quality']:.3f}, pass rate: {old_eval['pass_rate']:.1%}")
1461
+ print(f" NEW quality: {new_eval['avg_quality']:.3f}, pass rate: {new_eval['pass_rate']:.1%}")
1462
+ print(f" Quality diff: {quality_diff:+.3f}")
1463
+
1464
+ # Decision logic
1465
+ keep_new = False
1466
+ reason = ""
1467
+
1468
+ if new_eval['is_degraded']:
1469
+ keep_new = False
1470
+ reason = "New checkpoint quality below minimum threshold"
1471
+ elif quality_diff > 0.02:
1472
+ keep_new = True
1473
+ reason = f"New checkpoint improves quality by {quality_diff:.3f}"
1474
+ elif quality_diff < -Config.quality_drop_threshold:
1475
+ keep_new = False
1476
+ reason = f"New checkpoint degrades quality by {abs(quality_diff):.3f}"
1477
+ elif quality_diff >= 0:
1478
+ keep_new = True
1479
+ reason = "New checkpoint maintains or slightly improves quality"
1480
+ else:
1481
+ keep_new = False
1482
+ reason = "New checkpoint slightly degrades quality - keeping stable"
1483
+
1484
+ print(f"[COMPARE] Decision: {'KEEP NEW' if keep_new else 'KEEP OLD'} - {reason}")
1485
+
1486
+ return {
1487
+ 'keep_new': keep_new,
1488
+ 'reason': reason,
1489
+ 'old_eval': old_eval,
1490
+ 'new_eval': new_eval,
1491
+ 'quality_diff': quality_diff,
1492
+ }
1493
+
1494
+ def improve(self, max_iterations: int = None) -> Dict[str, Any]:
1495
+ """Main self-improvement loop with stability safeguards."""
1496
+ max_iterations = max_iterations or Config.max_improvement_iterations
1497
+
1498
+ print("\n" + "=" * 70)
1499
+ print("🔄 STABLE SELF-IMPROVEMENT LOOP (v2)")
1500
+ print("=" * 70)
1501
+ print(f" Max iterations: {max_iterations}")
1502
+ print(f" Steps per iteration: {Config.training_steps_per_iteration}")
1503
+ print(f" Training examples: {len(DENSE_TRAINING_EXAMPLES)}")
1504
+ print(f" Target quality: {Config.target_quality_score}")
1505
+ print(f" Quality drop threshold: {Config.quality_drop_threshold}")
1506
+ print("=" * 70)
1507
+
1508
+ # Initial evaluation
1509
+ print("\n[IMPROVE] Initial evaluation...")
1510
+ baseline = self.evaluate_current_model()
1511
+ self.baseline_quality = baseline['avg_quality']
1512
+
1513
+ # Save as best if better than current best
1514
+ if baseline['avg_quality'] > Store.state.get('best_quality_score', 0):
1515
+ Store.state['best_quality_score'] = baseline['avg_quality']
1516
+ Store.state['best_checkpoint'] = Store.state.get('current_checkpoint', DENSE_CHECKPOINT)
1517
+
1518
+ history = [{
1519
+ 'iteration': 0,
1520
+ 'type': 'baseline',
1521
+ 'quality': baseline['avg_quality'],
1522
+ 'pass_rate': baseline['pass_rate'],
1523
+ 'checkpoint': Store.state.get('current_checkpoint'),
1524
+ }]
1525
+
1526
+ for iteration in range(1, max_iterations + 1):
1527
+ print(f"\n{'=' * 70}")
1528
+ print(f"ITERATION {iteration}/{max_iterations}")
1529
+ print("=" * 70)
1530
+
1531
+ # Check if target reached
1532
+ if not baseline.get('needs_improvement', True):
1533
+ print(f"\n✓ TARGET REACHED! Quality: {baseline['avg_quality']:.3f}")
1534
+ Store.record_improvement({
1535
+ 'status': 'target_reached',
1536
+ 'final_quality': baseline['avg_quality'],
1537
+ 'iterations': iteration - 1,
1538
+ 'history': history
1539
+ })
1540
+ return {
1541
+ 'success': True,
1542
+ 'status': 'target_reached',
1543
+ 'iterations': iteration - 1,
1544
+ 'final_quality': baseline['avg_quality'],
1545
+ 'history': history
1546
+ }
1547
+
1548
+ # Check for degradation
1549
+ if baseline.get('is_degraded', False):
1550
+ print(f"\n⚠️ QUALITY DEGRADED! Rolling back...")
1551
+ self.rollback_to_best()
1552
+ Store.record_improvement({
1553
+ 'status': 'rolled_back',
1554
+ 'reason': 'quality_degraded',
1555
+ 'iteration': iteration,
1556
+ 'history': history
1557
+ })
1558
+ return {
1559
+ 'success': False,
1560
+ 'status': 'rolled_back',
1561
+ 'reason': 'quality_degraded',
1562
+ 'history': history
1563
+ }
1564
+
1565
+ # Save rollback point before training
1566
+ self.save_rollback_checkpoint()
1567
+ old_checkpoint = Store.state.get('current_checkpoint', DENSE_CHECKPOINT)
1568
+
1569
+ # Run training
1570
+ print(f"\n[IMPROVE] Quality {baseline['avg_quality']:.3f} < target {Config.target_quality_score}")
1571
+ training_result = self.run_training_iteration()
1572
+
1573
+ if not training_result['success']:
1574
+ print("[IMPROVE] ⚠️ Training failed!")
1575
+ history.append({
1576
+ 'iteration': iteration,
1577
+ 'type': 'training_failed',
1578
+ 'error': training_result['output'][-500:]
1579
+ })
1580
+ continue
1581
+
1582
+ # A/B compare old vs new
1583
+ comparison = self.compare_checkpoints(old_checkpoint, training_result['new_checkpoint'])
1584
+
1585
+ iteration_record = {
1586
+ 'iteration': iteration,
1587
+ 'type': 'comparison',
1588
+ 'old_quality': comparison['old_eval']['avg_quality'],
1589
+ 'new_quality': comparison['new_eval']['avg_quality'],
1590
+ 'quality_diff': comparison['quality_diff'],
1591
+ 'kept': 'new' if comparison['keep_new'] else 'old',
1592
+ 'reason': comparison['reason'],
1593
+ }
1594
+ history.append(iteration_record)
1595
+
1596
+ # Decision
1597
+ if comparison['keep_new']:
1598
+ Store.state['current_checkpoint'] = training_result['new_checkpoint']
1599
+
1600
+ # Update best if improved
1601
+ if comparison['new_eval']['avg_quality'] > Store.state.get('best_quality_score', 0):
1602
+ Store.state['best_quality_score'] = comparison['new_eval']['avg_quality']
1603
+ Store.state['best_checkpoint'] = training_result['new_checkpoint']
1604
+ print(f"[IMPROVE] ★ New best! Quality: {Store.state['best_quality_score']:.3f}")
1605
+
1606
+ baseline = comparison['new_eval']
1607
+ else:
1608
+ # Rollback to old
1609
+ reload_model(old_checkpoint)
1610
+ baseline = comparison['old_eval']
1611
+
1612
+ Store.state['improvement_iterations'] += 1
1613
+ Store.state['quality_history'].append({
1614
+ 'iteration': iteration,
1615
+ 'quality': baseline['avg_quality'],
1616
+ 'timestamp': datetime.now().isoformat()
1617
+ })
1618
+ Store.save()
1619
+
1620
+ # Final evaluation
1621
+ final_eval = self.evaluate_current_model()
1622
+
1623
+ result = {
1624
+ 'success': final_eval['avg_quality'] >= Config.target_quality_score,
1625
+ 'status': 'completed',
1626
+ 'iterations': max_iterations,
1627
+ 'initial_quality': self.baseline_quality,
1628
+ 'final_quality': final_eval['avg_quality'],
1629
+ 'best_quality': Store.state.get('best_quality_score', 0),
1630
+ 'best_checkpoint': Store.state.get('best_checkpoint'),
1631
+ 'rollback_count': Store.state.get('rollback_count', 0),
1632
+ 'history': history
1633
+ }
1634
+
1635
+ Store.record_improvement(result)
1636
+ return result
1637
+
1638
+
1639
+ # ==============================================================================
1640
+ # TOOLS (Original Limited)
1641
+ # ==============================================================================
1642
+ ALLOWED_SHELL = {"ls", "cat", "wc", "head", "tail", "nvidia-smi", "df", "du", "grep", "rg", "python3", "python"}
1643
+
1644
+ def tool_shell(cmd: str) -> str:
1645
+ try:
1646
+ exe = cmd.strip().split()[0]
1647
+ if exe not in ALLOWED_SHELL:
1648
+ return f"[shell] blocked: {exe} (use !shell for full access)"
1649
+ p = subprocess.run(cmd, shell=True, stdout=subprocess.PIPE, stderr=subprocess.STDOUT, timeout=20)
1650
+ return p.stdout.decode("utf-8", errors="ignore")[:8000]
1651
+ except Exception as e:
1652
+ return f"[shell] error: {e}"
1653
+
1654
+ def tool_py(code: str) -> str:
1655
+ try:
1656
+ g = {
1657
+ "__builtins__": {"range": range, "len": len, "min": min, "max": max, "sum": sum, "print": print},
1658
+ "math": math, "json": json, "re": re, "statistics": statistics, "random": random
1659
+ }
1660
+ l = {}
1661
+ exec(code, g, l)
1662
+ return f"[py] ok\n{l.get('out', '')}"
1663
+ except Exception:
1664
+ return f"[py] error:\n{traceback.format_exc()[-2000:]}"
1665
+
1666
+ def tool_search_local(query: str, path: str = ROOT) -> str:
1667
+ rg = shutil.which("rg")
1668
+ if rg:
1669
+ cmd = f'rg -n --no-heading --hidden -S "{query}" {path}'
1670
+ else:
1671
+ cmd = f'grep -RIn --exclude-dir=.git --exclude-dir=__pycache__ -e "{query}" {path}'
1672
+ return tool_shell(cmd)
1673
+
1674
+ def tool_lht_analyze(text: str) -> str:
1675
+ if not Config.use_lht_reasoning:
1676
+ return "[lht] Disabled"
1677
+ lht = get_lht_reasoner()
1678
+ if not lht:
1679
+ return "[lht] Not available"
1680
+ steps = [s.strip() for s in re.split(r'[\n•\-\d\.]', text) if len(s.strip()) > 10]
1681
+ if len(steps) < 2:
1682
+ return "[lht] Need at least 2 reasoning steps"
1683
+ metrics = lht.check_consistency(steps, _tokenizer)
1684
+ return f"[LHT] Consistency: {metrics['consistency_score']:.2%}, Holonomy: {metrics['holonomy']:.4f}"
1685
+
1686
+
1687
+ # ==============================================================================
1688
+ # PLANNING / REFLECTION
1689
+ # ==============================================================================
1690
+ def persona_directive() -> str:
1691
+ return "Übermenschetien v2: Stable self-improvement. Dense, coherent, helpful. Every word matters."
1692
+
1693
+ def plan_for(goal: str) -> str:
1694
+ user = f"{persona_directive()}\nGoal: {goal}\nDeliver 5 concrete steps with constraints and risks."
1695
+ response, _, _ = generate(user)
1696
+ return response
1697
+
1698
+ def reflect_on(last_output: str) -> str:
1699
+ user = f"{persona_directive()}\nCritique and improve:\n{last_output}"
1700
+ response, _, _ = generate(user)
1701
+ return response
1702
+
1703
+
1704
+ # ==============================================================================
1705
+ # FINAL REPORT
1706
+ # ==============================================================================
1707
+ def final_report():
1708
+ print("\n" + "=" * 70)
1709
+ print("FINAL ÜBERMENSCHETIEN v2 REPORT")
1710
+ print("=" * 70)
1711
+ print(f"Turns completed: {Store.state['turn']}")
1712
+ print(f"Goals tracked: {len(Store.goals)}")
1713
+ print(f"Improvement iterations: {Store.state.get('improvement_iterations', 0)}")
1714
+ print(f"Training runs: {len(Store.state.get('training_runs', []))}")
1715
+ print(f"Rollback count: {Store.state.get('rollback_count', 0)}")
1716
+ print(f"\nCheckpoints:")
1717
+ print(f" Current: {Store.state.get('current_checkpoint', 'unknown')}")
1718
+ print(f" Best: {Store.state.get('best_checkpoint', 'unknown')}")
1719
+ print(f" Best quality: {Store.state.get('best_quality_score', 0):.3f}")
1720
+
1721
+ if Store.state.get("cfhot_interventions"):
1722
+ iv = Store.state["cfhot_interventions"]
1723
+ print(f"\nCF-HoT Interventions: {sum(iv.values())}")
1724
+
1725
+ if Store.state.get("quality_history"):
1726
+ qh = Store.state["quality_history"]
1727
+ print(f"\nQuality History ({len(qh)} data points):")
1728
+ if qh:
1729
+ print(f" First: {qh[0].get('quality', 0):.3f}")
1730
+ print(f" Last: {qh[-1].get('quality', 0):.3f}")
1731
+
1732
+ print("=" * 70)
1733
+
1734
+
1735
+ # ==============================================================================
1736
+ # HELP
1737
+ # ==============================================================================
1738
+ HELP = """
1739
+ ╔══════════════════════════════════════════════════════════════════════════════╗
1740
+ ║ ÜBERMENSCHETIEN v2 - STABLE SELF-IMPROVEMENT ║
1741
+ ╠══════════════════════════════════════════════════════════════════════════════╣
1742
+ ║ SELF-IMPROVEMENT (WITH SAFEGUARDS) ║
1743
+ ║ !improve Run stable self-improvement loop ║
1744
+ ║ !eval Comprehensive model evaluation ║
1745
+ ║ !train <steps> Run N training steps (default: 25) ║
1746
+ ║ !compare Compare current vs best checkpoint ║
1747
+ ║ !rollback Rollback to best checkpoint ║
1748
+ ║ !load <path> Load a specific checkpoint ║
1749
+ ║ ║
1750
+ ║ AGENTIC TOOLS (FULL ACCESS) ║
1751
+ ║ !shell <cmd> Execute ANY shell command ║
1752
+ ║ !python <code> Execute Python code (full access) ║
1753
+ ║ !read <path> Read file contents ║
1754
+ ║ !write <p> <c> Write content to file ║
1755
+ ║ !ls [path] List directory ║
1756
+ ║ !search <query> Search in files ║
1757
+ ║ !web <query> Web search (DuckDuckGo) ║
1758
+ ║ ║
1759
+ ║ GOALS ║
1760
+ ║ goals List all goals ║
1761
+ ║ add: <text> Add a new goal ║
1762
+ ║ del: <idx> Delete goal by index ║
1763
+ ║ plan: <idx> Generate plan for goal ║
1764
+ ║ reflect Refine last plan ║
1765
+ ║ ║
1766
+ ║ INFO ║
1767
+ ║ status Current state and quality metrics ║
1768
+ ║ history Show quality history ║
1769
+ ║ examples Show training examples count ║
1770
+ ║ help This help ║
1771
+ ║ quit Exit with final report ║
1772
+ ║ ║
1773
+ ║ LIMITED TOOLS (Original) ║
1774
+ ║ shell: <cmd> Run limited shell command ║
1775
+ ║ py: <code> Run limited Python ║
1776
+ ║ search: <query> Search local files ║
1777
+ ║ lht: <text> Analyze reasoning consistency ║
1778
+ ║ ║
1779
+ ║ CONFIG ║
1780
+ ║ toggle <flag> Toggle: use_voice, use_vector_memory, use_cfhot, etc ║
1781
+ ╚══════════════════════════════════════════════════════════════════════════════╝
1782
+ """
1783
+
1784
+
1785
+ # ==============================================================================
1786
+ # MAIN LOOP
1787
+ # ==============================================================================
1788
+ def main():
1789
+ print("=" * 75)
1790
+ print("🤖 ÜBERMENSCHETIEN AGENTIC ENGINE v2 - STABLE SELF-IMPROVEMENT")
1791
+ print("=" * 75)
1792
+ print(f" DENSE Mode: ON (CONDENSATOR checkpoint)")
1793
+ print(f" CF-HoT Control: ON")
1794
+ print(f" AGENTIC Mode: ON (Full shell/python access)")
1795
+ print(f" LHT Reasoning: {'ON' if LHT_OK else 'OFF'}")
1796
+ print(f" Vector Memory: {'ON' if VECTOR_OK else 'OFF'}")
1797
+ print(f" Training Examples: {len(DENSE_TRAINING_EXAMPLES)}")
1798
+ print("=" * 75)
1799
+ print(" SAFEGUARDS ACTIVE:")
1800
+ print(f" • Quality evaluation (density + coherence + helpfulness)")
1801
+ print(f" • Automatic rollback on quality drop > {Config.quality_drop_threshold}")
1802
+ print(f" • Conservative training (LR=2e-6, {Config.training_steps_per_iteration} steps)")
1803
+ print(f" • A/B checkpoint comparison")
1804
+ print("=" * 75)
1805
+ print(" Type 'help' for commands, '!improve' to start self-improvement")
1806
+ print("=" * 75 + "\n")
1807
+
1808
+ Store.load()
1809
+ tok, model = load_llm()
1810
+
1811
+ improver = StableSelfImprover()
1812
+ last_plan = ""
1813
+
1814
+ while True:
1815
+ try:
1816
+ u = input("\n> ").strip()
1817
+ except (EOFError, KeyboardInterrupt):
1818
+ break
1819
+
1820
+ if not u:
1821
+ continue
1822
+ if u == "help":
1823
+ print(HELP)
1824
+ continue
1825
+ if u == "quit":
1826
+ break
1827
+
1828
+ # === SELF-IMPROVEMENT COMMANDS ===
1829
+ if u == "!improve":
1830
+ result = improver.improve()
1831
+ print("\n" + "=" * 50)
1832
+ print("IMPROVEMENT RESULT:")
1833
+ print(json.dumps({k: v for k, v in result.items() if k != 'history'}, indent=2, default=str))
1834
+ continue
1835
+
1836
+ if u == "!eval":
1837
+ result = improver.evaluate_current_model()
1838
+ print(json.dumps({k: v for k, v in result.items() if k != 'results'}, indent=2, default=str))
1839
+ continue
1840
+
1841
+ if u.startswith("!train "):
1842
+ try:
1843
+ steps = int(u[7:])
1844
+ old_ckpt = Store.state.get('current_checkpoint', DENSE_CHECKPOINT)
1845
+ result = improver.run_training_iteration(steps)
1846
+ if result['success']:
1847
+ # Auto-compare
1848
+ comp = improver.compare_checkpoints(old_ckpt, result['new_checkpoint'])
1849
+ if comp['keep_new']:
1850
+ print(f"\n✓ Using new checkpoint ({comp['reason']})")
1851
+ else:
1852
+ reload_model(old_ckpt)
1853
+ print(f"\n✗ Keeping old checkpoint ({comp['reason']})")
1854
+ else:
1855
+ print(f"Training failed")
1856
+ except ValueError:
1857
+ print("Usage: !train <steps>")
1858
+ continue
1859
+
1860
+ if u == "!compare":
1861
+ current = Store.state.get('current_checkpoint', DENSE_CHECKPOINT)
1862
+ best = Store.state.get('best_checkpoint', DENSE_CHECKPOINT)
1863
+ if current != best:
1864
+ improver.compare_checkpoints(current, best)
1865
+ else:
1866
+ print("Current checkpoint IS the best checkpoint")
1867
+ continue
1868
+
1869
+ if u == "!rollback":
1870
+ improver.rollback_to_best()
1871
+ print(f"Rolled back to: {Store.state['best_checkpoint']}")
1872
+ continue
1873
+
1874
+ if u.startswith("!load "):
1875
+ checkpoint = u[6:].strip()
1876
+ try:
1877
+ reload_model(checkpoint)
1878
+ print(f"Loaded: {checkpoint}")
1879
+ except Exception as e:
1880
+ print(f"Error: {e}")
1881
+ continue
1882
+
1883
+ # === AGENTIC COMMANDS ===
1884
+ if u.startswith("!shell "):
1885
+ result = AgentTools.shell(u[7:])
1886
+ print(f"```\n{result['output']}\n```\nExit: {result['returncode']}")
1887
+ continue
1888
+
1889
+ if u.startswith("!python "):
1890
+ result = AgentTools.python_exec(u[8:])
1891
+ print(f"```\n{result['output']}\n```")
1892
+ continue
1893
+
1894
+ if u.startswith("!read "):
1895
+ result = AgentTools.read_file(u[6:].strip())
1896
+ if result['success']:
1897
+ print(f"```\n{result['content'][:5000]}\n```")
1898
+ else:
1899
+ print(f"Error: {result['error']}")
1900
+ continue
1901
+
1902
+ if u.startswith("!write "):
1903
+ parts = u[7:].split(" ", 1)
1904
+ if len(parts) == 2:
1905
+ result = AgentTools.write_file(parts[0], parts[1])
1906
+ print(f"Written to {result.get('path', 'unknown')}" if result['success'] else f"Error: {result['error']}")
1907
+ else:
1908
+ print("Usage: !write <path> <content>")
1909
+ continue
1910
+
1911
+ if u.startswith("!ls"):
1912
+ path = u[3:].strip() or "."
1913
+ result = AgentTools.list_dir(path)
1914
+ if result['success']:
1915
+ print("\n".join(result['items']))
1916
+ else:
1917
+ print(f"Error: {result['error']}")
1918
+ continue
1919
+
1920
+ if u.startswith("!search "):
1921
+ result = AgentTools.search_files(u[8:])
1922
+ print(result['output'] if result['success'] else "No results")
1923
+ continue
1924
+
1925
+ if u.startswith("!web "):
1926
+ result = AgentTools.web_search(u[5:])
1927
+ if result['success']:
1928
+ print("\n\n".join(result['results']))
1929
+ else:
1930
+ print(f"Error: {result['error']}")
1931
+ continue
1932
+
1933
+ # === GOALS ===
1934
+ if u == "goals":
1935
+ print("[goals]")
1936
+ if not Store.goals:
1937
+ print(" (none)")
1938
+ for i, g in enumerate(Store.goals):
1939
+ print(f" [{i}] {g}")
1940
+ continue
1941
+
1942
+ if u.startswith("add:"):
1943
+ Store.goals.append(u[4:].strip())
1944
+ Store.save()
1945
+ print("[goals] added")
1946
+ continue
1947
+
1948
+ if u.startswith("del:"):
1949
+ try:
1950
+ Store.goals.pop(int(u[4:].strip()))
1951
+ Store.save()
1952
+ print("[goals] deleted")
1953
+ except:
1954
+ print("[goals] bad index")
1955
+ continue
1956
+
1957
+ if u.startswith("plan:"):
1958
+ try:
1959
+ goal = Store.goals[int(u[5:].strip())]
1960
+ except:
1961
+ print("[plan] bad index")
1962
+ continue
1963
+ out = plan_for(goal)
1964
+ last_plan = out
1965
+ Store.log_mem("plan", {"goal": goal, "plan": out})
1966
+ print(out)
1967
+ continue
1968
+
1969
+ if u == "reflect":
1970
+ if not last_plan:
1971
+ print("[reflect] no plan to refine")
1972
+ continue
1973
+ improved = reflect_on(last_plan)
1974
+ last_plan = improved
1975
+ Store.log_mem("reflect", {"plan": improved})
1976
+ print(improved)
1977
+ continue
1978
+
1979
+ # === INFO ===
1980
+ if u == "status":
1981
+ status = {
1982
+ "turn": Store.state["turn"],
1983
+ "goals": len(Store.goals),
1984
+ "improvement_iterations": Store.state.get("improvement_iterations", 0),
1985
+ "rollback_count": Store.state.get("rollback_count", 0),
1986
+ "current_checkpoint": Store.state.get("current_checkpoint", "unknown"),
1987
+ "best_checkpoint": Store.state.get("best_checkpoint", "unknown"),
1988
+ "best_quality": Store.state.get("best_quality_score", 0),
1989
+ "target_quality": Config.target_quality_score,
1990
+ "training_examples": len(DENSE_TRAINING_EXAMPLES),
1991
+ }
1992
+ print(json.dumps(status, indent=2))
1993
+ continue
1994
+
1995
+ if u == "history":
1996
+ qh = Store.state.get("quality_history", [])
1997
+ print(f"Quality History ({len(qh)} entries):")
1998
+ for entry in qh[-10:]:
1999
+ print(f" {entry.get('iteration', '?')}: {entry.get('quality', 0):.3f}")
2000
+ continue
2001
+
2002
+ if u == "examples":
2003
+ print(f"Training examples: {len(DENSE_TRAINING_EXAMPLES)}")
2004
+ print(f"Preference pairs: {len(PREFERENCE_PAIRS)}")
2005
+ print("\nSample prompts:")
2006
+ for ex in DENSE_TRAINING_EXAMPLES[:5]:
2007
+ print(f" • {ex['prompt']}")
2008
+ continue
2009
+
2010
+ # === LIMITED TOOLS ===
2011
+ if u.startswith("shell:"):
2012
+ print(tool_shell(u[6:].strip()))
2013
+ continue
2014
+
2015
+ if u.startswith("py:"):
2016
+ print(tool_py(u[3:].strip()))
2017
+ continue
2018
+
2019
+ if u.startswith("search:"):
2020
+ print(tool_search_local(u[7:].strip()))
2021
+ continue
2022
+
2023
+ if u.startswith("lht:"):
2024
+ print(tool_lht_analyze(u[4:].strip()))
2025
+ continue
2026
+
2027
+ # === CONFIG ===
2028
+ if u.startswith("toggle"):
2029
+ parts = u.split(maxsplit=1)
2030
+ if len(parts) > 1:
2031
+ print(Config.toggle(parts[1]))
2032
+ else:
2033
+ print("[toggle] specify flag")
2034
+ continue
2035
+
2036
+ # === DEFAULT: GENERATE ===
2037
+ out, stats, eval_result = generate(u)
2038
+ print(f"\n{out}")
2039
+ print(f"\n[Quality: {eval_result.overall_score:.2f} | Density: {eval_result.density_score:.1f} | "
2040
+ f"Coherence: {eval_result.coherence_score:.2f} | Tokens: {eval_result.tokens}]")
2041
+
2042
+ if eval_result.filler_count > 0:
2043
+ print(f" ⚠ Fillers detected: {eval_result.filler_count}")
2044
+ if eval_result.gibberish_score > 0.3:
2045
+ print(f" ⚠ Gibberish detected: {eval_result.gibberish_score:.2f}")
2046
+
2047
+ Store.log_mem("reply", {"in": u, "out": out, "quality": eval_result.overall_score})
2048
+ Store.state["turn"] += 1
2049
+ Store.save()
2050
+
2051
+ final_report()
2052
+
2053
+
2054
+ if __name__ == "__main__":
2055
+ main()