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

Upload ubermenschetien_agentic_full.py with huggingface_hub

Browse files
Files changed (1) hide show
  1. ubermenschetien_agentic_full.py +1589 -0
ubermenschetien_agentic_full.py ADDED
@@ -0,0 +1,1589 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ #!/usr/bin/env python3
2
+ """
3
+ UBERMENSCHETIEN HEAVEN ENGINE + DENSE + CF-HoT + AGENTIC SELF-IMPROVEMENT
4
+ ==========================================================================
5
+ FULL INTEGRATION:
6
+ - Hermes-3 base model
7
+ - DENSE CONDENSATOR checkpoint (step 100, Density: 28.5)
8
+ - CF-HoT Multi-Head Cognitive Control (Repetition 125x, Verbosity 2.1x, Hedging 1.5x)
9
+ - LHT Lie-Holonomy Geometric Reasoning
10
+ - Vector Memory (ChromaDB)
11
+ - Voice Output
12
+ - Goals Management
13
+ - Full Tool Suite
14
+ - AGENTIC: Full shell/python execution
15
+ - RECURSIVE SELF-IMPROVEMENT: eval → train → test → repeat
16
+
17
+ "An 8B that improves itself through training"
18
+ """
19
+
20
+ import os
21
+ import sys
22
+ import json
23
+ import time
24
+ import shutil
25
+ import subprocess
26
+ import traceback
27
+ import random
28
+ import math
29
+ import statistics
30
+ import re
31
+ import requests
32
+ from datetime import datetime
33
+ from typing import List, Dict, Any, Optional, Tuple
34
+ from pathlib import Path
35
+
36
+ import torch
37
+ import torch.nn as nn
38
+ import torch.nn.functional as F
39
+
40
+ # === PATHS ===
41
+ ROOT = os.path.dirname(os.path.abspath(__file__))
42
+ DATA_DIR = os.path.join(ROOT, "data")
43
+ SCRIPT_DIR = os.path.join(ROOT, "scripts")
44
+ RUN_DIR = os.path.join(ROOT, "runs")
45
+ LHT_DIR = os.path.join(ROOT, "lht")
46
+ CHECKPOINTS_DIR = os.path.join(ROOT, "dense_checkpoints_v2")
47
+ TRAINING_DIR = os.path.join(ROOT, "condensator_output")
48
+
49
+ # Model paths
50
+ MODEL_PATH = "/mnt/nvme2/ubermesnchetien4/models/merged-final-v5"
51
+
52
+ # DENSE CONDENSATOR checkpoint
53
+ DENSE_CHECKPOINT = os.path.join(ROOT, "dense_checkpoints_v2/step_100")
54
+
55
+ # CF-HoT paths (for runtime cognitive control)
56
+ CFHOT_CHECKPOINT = os.path.join(ROOT, "results/cfhot_risk_v2/ckpt_5000")
57
+ MULTI_HEAD_DIR = os.path.join(ROOT, "results/multi_head_v2")
58
+
59
+ for path in [DATA_DIR, SCRIPT_DIR, RUN_DIR, LHT_DIR]:
60
+ os.makedirs(path, exist_ok=True)
61
+
62
+ # === OPTIONAL IMPORTS ===
63
+ VOICE_OK = False
64
+ try:
65
+ import pyttsx3
66
+ TTS = pyttsx3.init()
67
+ VOICE_OK = True
68
+ except:
69
+ pass
70
+
71
+ VECTOR_OK = False
72
+ try:
73
+ import chromadb
74
+ from sentence_transformers import SentenceTransformer
75
+ EMBED_MODEL = os.environ.get("UBERMENCHETIEN_EMBED_MODEL", "all-MiniLM-L6-v2")
76
+ _client = chromadb.Client()
77
+ _collection = _client.get_or_create_collection("ubermenschetien_memory")
78
+ _embedder = SentenceTransformer(EMBED_MODEL)
79
+ VECTOR_OK = True
80
+ except:
81
+ pass
82
+
83
+ # === LHT IMPORT ===
84
+ LHT_OK = False
85
+ try:
86
+ from lht import LieHolonomyTransformer, LHTConfig, WaypointDetector
87
+ LHT_OK = True
88
+ print("[lht] Lie-Holonomy modules loaded")
89
+ except ImportError:
90
+ print("[lht] Not available - running without geometric reasoning")
91
+
92
+ # === PEFT IMPORT ===
93
+ PEFT_OK = False
94
+ try:
95
+ from peft import PeftModel, get_peft_model, LoraConfig
96
+ PEFT_OK = True
97
+ except ImportError:
98
+ print("[warning] PEFT not installed")
99
+
100
+
101
+ # ==============================================================================
102
+ # CF-HoT MULTI-HEAD PREDICTOR
103
+ # ==============================================================================
104
+ class MultiHeadPredictor(nn.Module):
105
+ """
106
+ Multi-head cognitive control predictor.
107
+ Shared fiber projections with separate heads for each behavioral pattern.
108
+ """
109
+ def __init__(self, d_model: int, n_layers: int, d_fiber: int = 16, d_control: int = 64):
110
+ super().__init__()
111
+ self.d_model = d_model
112
+ self.n_layers = n_layers
113
+ self.d_fiber = d_fiber
114
+
115
+ # Shared fiber projections (frozen from repetition training)
116
+ self.fiber_projs = nn.ModuleList([
117
+ nn.Linear(d_model, d_fiber, bias=False) for _ in range(n_layers)
118
+ ])
119
+ self.layer_weights = nn.Parameter(torch.ones(n_layers) / n_layers)
120
+
121
+ # Individual heads for each behavior
122
+ self.heads = nn.ModuleDict({
123
+ 'repetition': self._make_head(d_fiber, d_control),
124
+ 'hedging': self._make_head(d_fiber, d_control),
125
+ 'verbosity': self._make_head(d_fiber, d_control),
126
+ })
127
+
128
+ self.loaded_heads = set()
129
+
130
+ def _make_head(self, d_fiber, d_control):
131
+ return nn.Sequential(
132
+ nn.Linear(d_fiber, d_control), nn.GELU(),
133
+ nn.Linear(d_control, d_control), nn.GELU(),
134
+ nn.Linear(d_control, 1)
135
+ )
136
+
137
+ def get_all_risks(self, hidden_states: List[torch.Tensor]) -> Dict[str, torch.Tensor]:
138
+ """Get risk scores from ALL loaded heads in a single pass."""
139
+ fibers = [proj(h.float()) for proj, h in zip(self.fiber_projs, hidden_states)]
140
+ weights = F.softmax(self.layer_weights[:len(fibers)], dim=0)
141
+ aggregated = sum(w * f for w, f in zip(weights, fibers))
142
+
143
+ risks = {}
144
+ for head_name in self.loaded_heads:
145
+ logits = self.heads[head_name](aggregated).squeeze(-1)
146
+ risks[head_name] = torch.sigmoid(logits)
147
+
148
+ return risks
149
+
150
+ def load_head(self, head_name: str, checkpoint_path: str):
151
+ """Load a trained head from checkpoint."""
152
+ if not os.path.exists(checkpoint_path):
153
+ print(f"[cf-hot] WARNING: Checkpoint not found: {checkpoint_path}")
154
+ return False
155
+
156
+ ckpt = torch.load(checkpoint_path, weights_only=False, map_location='cpu')
157
+ self.heads[head_name].load_state_dict(ckpt['head_state'])
158
+ self.loaded_heads.add(head_name)
159
+
160
+ sep = ckpt.get('result', {}).get('separation', 0)
161
+ print(f"[cf-hot] Loaded {head_name} head (separation: {sep:.1f}x)")
162
+ return True
163
+
164
+
165
+ # ==============================================================================
166
+ # CONFIG
167
+ # ==============================================================================
168
+ class Config:
169
+ # Dense-focused system prompt
170
+ system = ("Übermenschetien Agentic Engine: Self-improving AI with compressed wisdom. "
171
+ "Every word chosen, no filler. Soviet cybernetic rigor + Lie-Holonomy geometric reasoning "
172
+ "+ CF-HoT cognitive control. You can execute code, run commands, and improve yourself.")
173
+
174
+ temperature = 0.85
175
+ top_p = 0.9
176
+ repetition_penalty = 1.1
177
+ max_new_tokens = 512
178
+
179
+ use_voice = False
180
+ use_vector_memory = VECTOR_OK
181
+ use_lht_reasoning = LHT_OK
182
+ use_cfhot = True
183
+ use_dense = True
184
+ use_agentic = True # NEW: Enable agentic capabilities
185
+ autonomy = False
186
+ reflect_every = 3
187
+ lht_consistency_threshold = 0.5
188
+
189
+ # CF-HoT thresholds
190
+ cfhot_repetition_threshold = 0.6
191
+ cfhot_hedging_threshold = 0.5
192
+ cfhot_verbosity_threshold = 0.55
193
+
194
+ # CF-HoT penalties
195
+ cfhot_repetition_penalty = 6.0
196
+ cfhot_hedging_penalty = 4.0
197
+ cfhot_verbosity_penalty = 3.0
198
+
199
+ # Self-improvement config
200
+ min_acceptable_density = 25.0
201
+ target_density = 35.0
202
+ max_filler_phrases = 0
203
+ training_steps_per_iteration = 100
204
+ max_improvement_iterations = 5
205
+
206
+ @staticmethod
207
+ def toggle(name: str):
208
+ if not hasattr(Config, name):
209
+ return f"[config] no such flag: {name}"
210
+ val = getattr(Config, name)
211
+ if isinstance(val, bool):
212
+ setattr(Config, name, not val)
213
+ return f"[config] {name} → {getattr(Config, name)}"
214
+ return f"[config] {name} not boolean; current={val}"
215
+
216
+
217
+ # ==============================================================================
218
+ # STATE & MEMORY
219
+ # ==============================================================================
220
+ class Store:
221
+ state_path = f"{RUN_DIR}/state.json"
222
+ mem_path = f"{RUN_DIR}/memory.jsonl"
223
+ goals_path = f"{RUN_DIR}/goals.json"
224
+
225
+ state = {
226
+ "self": "I am Ubermenschetien Agentic Engine — self-improving through disciplined creation.",
227
+ "turn": 0,
228
+ "reasoning_consistency": [],
229
+ "cfhot_interventions": {"repetition": 0, "hedging": 0, "verbosity": 0},
230
+ "density_scores": [],
231
+ "improvement_iterations": 0,
232
+ "training_runs": [],
233
+ "current_checkpoint": DENSE_CHECKPOINT,
234
+ }
235
+ goals: List[str] = []
236
+
237
+ @classmethod
238
+ def load(cls):
239
+ if os.path.exists(cls.state_path):
240
+ cls.state = json.load(open(cls.state_path))
241
+ # Ensure new fields exist
242
+ if "cfhot_interventions" not in cls.state:
243
+ cls.state["cfhot_interventions"] = {"repetition": 0, "hedging": 0, "verbosity": 0}
244
+ if "density_scores" not in cls.state:
245
+ cls.state["density_scores"] = []
246
+ if "improvement_iterations" not in cls.state:
247
+ cls.state["improvement_iterations"] = 0
248
+ if "training_runs" not in cls.state:
249
+ cls.state["training_runs"] = []
250
+ if "current_checkpoint" not in cls.state:
251
+ cls.state["current_checkpoint"] = DENSE_CHECKPOINT
252
+ if os.path.exists(cls.goals_path):
253
+ cls.goals = json.load(open(cls.goals_path))
254
+
255
+ @classmethod
256
+ def save(cls):
257
+ json.dump(cls.state, open(cls.state_path, "w"), indent=2)
258
+ json.dump(cls.goals, open(cls.goals_path, "w"), indent=2)
259
+
260
+ @classmethod
261
+ def log_mem(cls, kind: str, payload: Any):
262
+ rec = {"ts": datetime.now().isoformat(timespec="seconds"),
263
+ "kind": kind, "data": payload}
264
+ with open(cls.mem_path, "a") as f:
265
+ f.write(json.dumps(rec, ensure_ascii=False) + "\n")
266
+ if Config.use_vector_memory and VECTOR_OK:
267
+ text = f"{kind}: {json.dumps(payload, ensure_ascii=False)}"
268
+ vec = _embedder.encode([text])[0].tolist()
269
+ _collection.add(documents=[text], embeddings=[vec],
270
+ ids=[f"{kind}-{Store.state['turn']}-{random.randint(0,1_000_000)}"])
271
+
272
+
273
+ # ==============================================================================
274
+ # AGENTIC TOOLS - FULL ACCESS
275
+ # ==============================================================================
276
+ class AgentTools:
277
+ """Full agentic capabilities - code execution, file operations, training."""
278
+
279
+ @staticmethod
280
+ def shell(cmd: str, timeout: int = 300) -> Dict[str, Any]:
281
+ """Execute ANY shell command. Full access."""
282
+ print(f"[SHELL] {cmd}")
283
+ try:
284
+ result = subprocess.run(
285
+ cmd,
286
+ shell=True,
287
+ capture_output=True,
288
+ text=True,
289
+ timeout=timeout,
290
+ cwd=ROOT
291
+ )
292
+ output = result.stdout + result.stderr
293
+ success = result.returncode == 0
294
+ print(f"[SHELL] {'✓' if success else '✗'} (exit {result.returncode})")
295
+ return {
296
+ "success": success,
297
+ "output": output[:10000],
298
+ "returncode": result.returncode
299
+ }
300
+ except subprocess.TimeoutExpired:
301
+ return {"success": False, "output": "Command timed out", "returncode": -1}
302
+ except Exception as e:
303
+ return {"success": False, "output": str(e), "returncode": -1}
304
+
305
+ @staticmethod
306
+ def python_exec(code: str) -> Dict[str, Any]:
307
+ """Execute Python code with full access."""
308
+ print(f"[PYTHON] Executing {len(code)} chars of code...")
309
+ try:
310
+ # Create a temporary file and run it
311
+ tmp_file = os.path.join(ROOT, "_agentic_tmp.py")
312
+ with open(tmp_file, 'w') as f:
313
+ f.write(code)
314
+
315
+ result = subprocess.run(
316
+ [sys.executable, tmp_file],
317
+ capture_output=True,
318
+ text=True,
319
+ timeout=300,
320
+ cwd=ROOT
321
+ )
322
+
323
+ os.remove(tmp_file)
324
+
325
+ output = result.stdout + result.stderr
326
+ success = result.returncode == 0
327
+ print(f"[PYTHON] {'✓' if success else '✗'}")
328
+ return {
329
+ "success": success,
330
+ "output": output[:10000],
331
+ "returncode": result.returncode
332
+ }
333
+ except Exception as e:
334
+ return {"success": False, "output": str(e), "returncode": -1}
335
+
336
+ @staticmethod
337
+ def read_file(path: str) -> Dict[str, Any]:
338
+ """Read any file."""
339
+ try:
340
+ full_path = os.path.join(ROOT, path) if not path.startswith('/') else path
341
+ with open(full_path, 'r') as f:
342
+ content = f.read()
343
+ return {"success": True, "content": content[:50000]}
344
+ except Exception as e:
345
+ return {"success": False, "error": str(e)}
346
+
347
+ @staticmethod
348
+ def write_file(path: str, content: str) -> Dict[str, Any]:
349
+ """Write to any file."""
350
+ try:
351
+ full_path = os.path.join(ROOT, path) if not path.startswith('/') else path
352
+ os.makedirs(os.path.dirname(full_path) if os.path.dirname(full_path) else '.', exist_ok=True)
353
+ with open(full_path, 'w') as f:
354
+ f.write(content)
355
+ return {"success": True, "path": full_path}
356
+ except Exception as e:
357
+ return {"success": False, "error": str(e)}
358
+
359
+ @staticmethod
360
+ def list_dir(path: str = ".") -> Dict[str, Any]:
361
+ """List directory contents."""
362
+ try:
363
+ full_path = os.path.join(ROOT, path) if not path.startswith('/') else path
364
+ items = os.listdir(full_path)
365
+ return {"success": True, "items": items}
366
+ except Exception as e:
367
+ return {"success": False, "error": str(e)}
368
+
369
+ @staticmethod
370
+ def search_files(query: str, path: str = ".") -> Dict[str, Any]:
371
+ """Search for text in files."""
372
+ result = AgentTools.shell(f'grep -rn "{query}" {path} 2>/dev/null | head -50')
373
+ return result
374
+
375
+ @staticmethod
376
+ def web_search(query: str) -> Dict[str, Any]:
377
+ """Search the web (using DuckDuckGo HTML)."""
378
+ try:
379
+ url = f"https://html.duckduckgo.com/html/?q={query.replace(' ', '+')}"
380
+ headers = {'User-Agent': 'Mozilla/5.0'}
381
+ response = requests.get(url, headers=headers, timeout=10)
382
+
383
+ results = []
384
+ for match in re.finditer(r'class="result__snippet">(.*?)</a>', response.text, re.DOTALL):
385
+ snippet = re.sub(r'<[^>]+>', '', match.group(1)).strip()
386
+ if snippet:
387
+ results.append(snippet[:500])
388
+ if len(results) >= 5:
389
+ break
390
+
391
+ return {"success": True, "results": results}
392
+ except Exception as e:
393
+ return {"success": False, "error": str(e)}
394
+
395
+
396
+ # ==============================================================================
397
+ # MODEL LOADING WITH DENSE + CF-HoT
398
+ # ==============================================================================
399
+ _model = None
400
+ _tokenizer = None
401
+ _multi_head = None
402
+ _hedge_tokens = None
403
+ _verbose_tokens = None
404
+
405
+ def load_llm(checkpoint_path: str = None):
406
+ global _model, _tokenizer, _multi_head, _hedge_tokens, _verbose_tokens
407
+
408
+ from transformers import AutoTokenizer, AutoModelForCausalLM, BitsAndBytesConfig
409
+
410
+ checkpoint_path = checkpoint_path or Store.state.get("current_checkpoint", DENSE_CHECKPOINT)
411
+
412
+ print(f"[llm] Loading base model: {MODEL_PATH}")
413
+
414
+ _tokenizer = AutoTokenizer.from_pretrained(MODEL_PATH, use_fast=True, local_files_only=True)
415
+ if _tokenizer.pad_token_id is None:
416
+ _tokenizer.pad_token = _tokenizer.eos_token
417
+
418
+ bnb_config = BitsAndBytesConfig(
419
+ load_in_4bit=True,
420
+ bnb_4bit_quant_type="nf4",
421
+ bnb_4bit_compute_dtype=torch.bfloat16,
422
+ bnb_4bit_use_double_quant=True
423
+ )
424
+
425
+ base_model = AutoModelForCausalLM.from_pretrained(
426
+ MODEL_PATH,
427
+ quantization_config=bnb_config,
428
+ device_map="auto",
429
+ torch_dtype=torch.bfloat16,
430
+ local_files_only=True
431
+ )
432
+
433
+ # Load DENSE checkpoint
434
+ if PEFT_OK and Config.use_dense and os.path.exists(checkpoint_path):
435
+ print(f"[dense] Loading checkpoint: {checkpoint_path}")
436
+ _model = PeftModel.from_pretrained(base_model, checkpoint_path)
437
+ print(f"[dense] ✓ Adapter loaded")
438
+ elif PEFT_OK and os.path.exists(CFHOT_CHECKPOINT):
439
+ print(f"[cf-hot] Loading LoRA adapter from: {CFHOT_CHECKPOINT}")
440
+ _model = PeftModel.from_pretrained(base_model, CFHOT_CHECKPOINT)
441
+ print("[cf-hot] LoRA adapter loaded")
442
+ else:
443
+ _model = base_model
444
+ print("[warning] No adapter loaded - using base model")
445
+
446
+ _model.eval()
447
+
448
+ # Initialize CF-HoT multi-head predictor
449
+ if Config.use_cfhot:
450
+ _init_cfhot()
451
+
452
+ return _tokenizer, _model
453
+
454
+
455
+ def reload_model(checkpoint_path: str):
456
+ """Hot-reload model with a new checkpoint."""
457
+ global _model, _tokenizer
458
+
459
+ print(f"\n[reload] Switching to checkpoint: {checkpoint_path}")
460
+
461
+ # Clear old model
462
+ if _model is not None:
463
+ del _model
464
+ torch.cuda.empty_cache()
465
+
466
+ Store.state["current_checkpoint"] = checkpoint_path
467
+ Store.save()
468
+
469
+ return load_llm(checkpoint_path)
470
+
471
+
472
+ def _init_cfhot():
473
+ """Initialize CF-HoT multi-head predictor for runtime cognitive control."""
474
+ global _multi_head, _hedge_tokens, _verbose_tokens
475
+
476
+ n_layers = _model.config.num_hidden_layers
477
+ d_model = _model.config.hidden_size
478
+ device = next(_model.parameters()).device
479
+
480
+ print(f"[cf-hot] Initializing multi-head predictor ({n_layers} layers, {d_model} dims)")
481
+ _multi_head = MultiHeadPredictor(d_model, n_layers).to(device).float()
482
+
483
+ # Load shared fiber projections from CF-HoT checkpoint
484
+ cfhot_risk_path = os.path.join(CFHOT_CHECKPOINT, "risk_predictor.pt")
485
+ if os.path.exists(cfhot_risk_path):
486
+ cfhot_ckpt = torch.load(cfhot_risk_path, weights_only=False, map_location=device)
487
+ cfhot_state = cfhot_ckpt['risk_predictor']
488
+
489
+ for i in range(n_layers):
490
+ key = f'fiber_projs.{i}.weight'
491
+ if key in cfhot_state:
492
+ _multi_head.fiber_projs[i].weight.data = cfhot_state[key].to(device).float()
493
+
494
+ if 'layer_weights' in cfhot_state:
495
+ _multi_head.layer_weights.data = cfhot_state['layer_weights'].to(device).float()
496
+
497
+ # Load repetition head
498
+ try:
499
+ _multi_head.heads['repetition'][0].weight.data = cfhot_state['predictor.0.weight'].to(device).float()
500
+ _multi_head.heads['repetition'][0].bias.data = cfhot_state['predictor.0.bias'].to(device).float()
501
+ _multi_head.heads['repetition'][2].weight.data = cfhot_state['predictor.2.weight'].to(device).float()
502
+ _multi_head.heads['repetition'][2].bias.data = cfhot_state['predictor.2.bias'].to(device).float()
503
+ _multi_head.heads['repetition'][4].weight.data = cfhot_state['predictor.4.weight'].to(device).float()
504
+ _multi_head.heads['repetition'][4].bias.data = cfhot_state['predictor.4.bias'].to(device).float()
505
+ _multi_head.loaded_heads.add('repetition')
506
+ print(f"[cf-hot] Loaded repetition head (125x separation)")
507
+ except KeyError as e:
508
+ print(f"[cf-hot] Warning: Could not load repetition head: {e}")
509
+ else:
510
+ print(f"[cf-hot] Warning: CF-HoT risk predictor not found")
511
+
512
+ # Load additional heads
513
+ def find_best_checkpoint(head_dir):
514
+ if not os.path.exists(head_dir):
515
+ return None
516
+ ckpts = []
517
+ for d in os.listdir(head_dir):
518
+ if d.startswith("ckpt_"):
519
+ try:
520
+ step = int(d.split("_")[1])
521
+ ckpts.append((step, os.path.join(head_dir, d)))
522
+ except:
523
+ pass
524
+ if ckpts:
525
+ ckpts.sort(key=lambda x: x[0], reverse=True)
526
+ return ckpts[0]
527
+ return None
528
+
529
+ hedging_dir = os.path.join(MULTI_HEAD_DIR, "hedging_head")
530
+ best_hedge = find_best_checkpoint(hedging_dir)
531
+ if best_hedge:
532
+ step, ckpt_dir = best_hedge
533
+ _multi_head.load_head('hedging', os.path.join(ckpt_dir, "hedging_head.pt"))
534
+
535
+ verbosity_dir = os.path.join(MULTI_HEAD_DIR, "verbosity_head")
536
+ best_verb = find_best_checkpoint(verbosity_dir)
537
+ if best_verb:
538
+ step, ckpt_dir = best_verb
539
+ _multi_head.load_head('verbosity', os.path.join(ckpt_dir, "verbosity_head.pt"))
540
+
541
+ _multi_head.eval()
542
+ for param in _multi_head.parameters():
543
+ param.requires_grad = False
544
+
545
+ # Build suppression token sets
546
+ hedge_phrases = [
547
+ "As an AI", "As a language model", "As an artificial intelligence",
548
+ "I don't have feelings", "I don't have emotions", "I cannot",
549
+ "I apologize", "I'm just a", "I'm only a", "I'm sorry",
550
+ "That's a great question", "That's an interesting question",
551
+ "Great question", "Good question", "Interesting question",
552
+ "I'd be happy to", "I would be happy to", "Let me help you",
553
+ "Thank you for asking", "Thanks for asking",
554
+ ]
555
+ _hedge_tokens = set()
556
+ for phrase in hedge_phrases:
557
+ tokens = _tokenizer.encode(phrase, add_special_tokens=False)
558
+ if tokens:
559
+ _hedge_tokens.add(tokens[0])
560
+
561
+ verbose_phrases = [
562
+ "Let me explain", "To put it simply", "In other words",
563
+ "What I mean is", "Allow me to", "Basically", "Essentially",
564
+ "First of all", "To begin with", "It's important to note",
565
+ "I should mention", "As you may know", "As you might know",
566
+ "Before I answer", "To answer your question", "Simply put",
567
+ "In essence", "To be clear", "To clarify", "In summary",
568
+ ]
569
+ _verbose_tokens = set()
570
+ for phrase in verbose_phrases:
571
+ tokens = _tokenizer.encode(phrase, add_special_tokens=False)
572
+ if tokens:
573
+ _verbose_tokens.add(tokens[0])
574
+
575
+ print(f"[cf-hot] ✓ Multi-head system ready")
576
+ print(f"[cf-hot] Loaded heads: {list(_multi_head.loaded_heads)}")
577
+ print(f"[cf-hot] Hedge tokens: {len(_hedge_tokens)}")
578
+ print(f"[cf-hot] Verbose tokens: {len(_verbose_tokens)}")
579
+
580
+
581
+ # ==============================================================================
582
+ # LHT REASONER
583
+ # ==============================================================================
584
+ class LHTReasoner:
585
+ def __init__(self, config=None):
586
+ if not LHT_OK:
587
+ raise ImportError("LHT modules not available")
588
+ self.config = config or LHTConfig(
589
+ vocab_size=32000,
590
+ d_model=256,
591
+ d_fiber=32,
592
+ n_heads=4,
593
+ n_layers=4,
594
+ lie_algebra_rank=4,
595
+ )
596
+ self.model = LieHolonomyTransformer(self.config)
597
+ self.waypoint_detector = WaypointDetector(self.config, n_waypoints=32)
598
+ weights_path = os.path.join(LHT_DIR, "lht_weights.pt")
599
+ if os.path.exists(weights_path):
600
+ self.model.load_state_dict(torch.load(weights_path, map_location="cpu"))
601
+ print("[lht] Loaded pretrained weights")
602
+
603
+ def check_consistency(self, reasoning_chain: List[str], tokenizer) -> Dict[str, float]:
604
+ combined = " [STEP] ".join(reasoning_chain)
605
+ tokens = tokenizer(combined, return_tensors="pt", truncation=True,
606
+ max_length=self.config.max_seq_len)
607
+ with torch.no_grad():
608
+ output = self.model(input_ids=tokens["input_ids"], return_geometric_losses=True)
609
+ holonomy = output.get("holonomy_loss", torch.tensor(0.0)).item()
610
+ curvature = output.get("curvature_loss", torch.tensor(0.0)).item()
611
+ x = self.model.token_embed(tokens["input_ids"])
612
+ waypoint_ids, stability = self.waypoint_detector(x)
613
+ consistency_score = 1.0 / (1.0 + holonomy)
614
+ return {
615
+ "holonomy": holonomy,
616
+ "curvature": curvature,
617
+ "consistency_score": consistency_score,
618
+ "n_waypoints": len(torch.unique(waypoint_ids)),
619
+ "avg_stability": stability.mean().item(),
620
+ "is_consistent": consistency_score > Config.lht_consistency_threshold
621
+ }
622
+
623
+ def analyze_plan(self, plan_steps: List[str], tokenizer) -> str:
624
+ metrics = self.check_consistency(plan_steps, tokenizer)
625
+ return f"""
626
+ [LHT Geometric Analysis]
627
+ Holonomy: {metrics['holonomy']:.4f} (lower = more consistent)
628
+ Curvature: {metrics['curvature']:.4f} (lower = simpler reasoning)
629
+ Consistency: {metrics['consistency_score']:.2%}
630
+ Waypoints: {metrics['n_waypoints']} stable anchors detected
631
+ Stability: {metrics['avg_stability']:.2%}
632
+ Verdict: {"✓ CONSISTENT" if metrics['is_consistent'] else "⚠ INCONSISTENT"}
633
+ """
634
+
635
+ _lht_reasoner = None
636
+
637
+ def get_lht_reasoner():
638
+ global _lht_reasoner
639
+ if _lht_reasoner is None and LHT_OK:
640
+ try:
641
+ _lht_reasoner = LHTReasoner()
642
+ except Exception as e:
643
+ print(f"[lht] Failed to initialize: {e}")
644
+ return _lht_reasoner
645
+
646
+
647
+ # ==============================================================================
648
+ # DENSITY ANALYZER
649
+ # ==============================================================================
650
+ def analyze_density(text: str, tokenizer=None) -> Dict[str, Any]:
651
+ """Analyze the information density of text."""
652
+ if tokenizer is None:
653
+ tokenizer = _tokenizer
654
+
655
+ words = text.split()
656
+ tokens = len(tokenizer.encode(text))
657
+
658
+ content_words = [w.lower() for w in words if len(w) > 4 and w.isalpha()]
659
+ unique_content = set(content_words)
660
+
661
+ technical_terms = [w for w in words if any(c.isdigit() for c in w) or
662
+ any(c in w for c in ['→', '∂', '∇', '×', '·', '=', '<', '>'])]
663
+
664
+ fillers = [
665
+ "that's a great question", "let me explain", "i'd be happy to",
666
+ "as you may know", "it's important to note", "to put it simply",
667
+ "in other words", "basically", "essentially", "first of all",
668
+ "to begin with", "allow me to", "i should mention",
669
+ ]
670
+ filler_count = sum(1 for f in fillers if f in text.lower())
671
+
672
+ density = len(unique_content) / max(tokens, 1) * 100
673
+ technical_ratio = len(technical_terms) / max(len(words), 1) * 100
674
+
675
+ return {
676
+ 'tokens': tokens,
677
+ 'words': len(words),
678
+ 'unique_content_words': len(unique_content),
679
+ 'technical_terms': len(technical_terms),
680
+ 'density': density,
681
+ 'technical_ratio': technical_ratio,
682
+ 'filler_phrases': filler_count,
683
+ 'chars_per_token': len(text) / max(tokens, 1),
684
+ 'passes_threshold': density >= Config.min_acceptable_density and filler_count <= Config.max_filler_phrases
685
+ }
686
+
687
+
688
+ # ==============================================================================
689
+ # CF-HoT CONTROLLED GENERATION
690
+ # ==============================================================================
691
+ def generate_with_cfhot(prompt: str, **kwargs) -> Tuple[str, Dict]:
692
+ """Generate text with CF-HoT cognitive control."""
693
+ global _model, _tokenizer, _multi_head, _hedge_tokens, _verbose_tokens
694
+
695
+ temperature = kwargs.get("temperature", Config.temperature)
696
+ top_p = kwargs.get("top_p", Config.top_p)
697
+ max_new_tokens = kwargs.get("max_new_tokens", Config.max_new_tokens)
698
+
699
+ device = next(_model.parameters()).device
700
+
701
+ input_ids = _tokenizer.encode(prompt, return_tensors='pt').to(device)
702
+ attention_mask = torch.ones_like(input_ids)
703
+
704
+ stats = {
705
+ 'tokens_generated': 0,
706
+ 'interventions': {'repetition': 0, 'hedging': 0, 'verbosity': 0},
707
+ }
708
+
709
+ generated_ids = input_ids.clone()
710
+
711
+ for step in range(max_new_tokens):
712
+ with torch.no_grad():
713
+ outputs = _model(
714
+ input_ids=generated_ids,
715
+ attention_mask=attention_mask,
716
+ output_hidden_states=True,
717
+ return_dict=True
718
+ )
719
+
720
+ logits = outputs.logits[:, -1, :] / temperature
721
+
722
+ # Get risks from all heads if CF-HoT is enabled
723
+ if _multi_head is not None and _multi_head.loaded_heads:
724
+ hidden_states = outputs.hidden_states[1:]
725
+ risks = _multi_head.get_all_risks(hidden_states)
726
+ current_risks = {name: r[:, -1].item() for name, r in risks.items()}
727
+
728
+ if ('repetition' in current_risks and
729
+ current_risks['repetition'] > Config.cfhot_repetition_threshold):
730
+ recent_tokens = generated_ids[0, -32:].tolist()
731
+ for tok_id in set(recent_tokens):
732
+ logits[0, tok_id] -= Config.cfhot_repetition_penalty
733
+ stats['interventions']['repetition'] += 1
734
+ Store.state['cfhot_interventions']['repetition'] += 1
735
+
736
+ if ('hedging' in current_risks and _hedge_tokens and
737
+ current_risks['hedging'] > Config.cfhot_hedging_threshold):
738
+ for tok_id in _hedge_tokens:
739
+ logits[0, tok_id] -= Config.cfhot_hedging_penalty
740
+ stats['interventions']['hedging'] += 1
741
+ Store.state['cfhot_interventions']['hedging'] += 1
742
+
743
+ if ('verbosity' in current_risks and _verbose_tokens and
744
+ current_risks['verbosity'] > Config.cfhot_verbosity_threshold):
745
+ for tok_id in _verbose_tokens:
746
+ logits[0, tok_id] -= Config.cfhot_verbosity_penalty
747
+ stats['interventions']['verbosity'] += 1
748
+ Store.state['cfhot_interventions']['verbosity'] += 1
749
+
750
+ sorted_logits, sorted_indices = torch.sort(logits, descending=True)
751
+ cumulative_probs = torch.cumsum(F.softmax(sorted_logits, dim=-1), dim=-1)
752
+ sorted_indices_to_remove = cumulative_probs > top_p
753
+ sorted_indices_to_remove[..., 1:] = sorted_indices_to_remove[..., :-1].clone()
754
+ sorted_indices_to_remove[..., 0] = 0
755
+ indices_to_remove = sorted_indices_to_remove.scatter(1, sorted_indices, sorted_indices_to_remove)
756
+ logits[indices_to_remove] = float('-inf')
757
+
758
+ probs = F.softmax(logits, dim=-1)
759
+ next_token = torch.multinomial(probs, num_samples=1)
760
+
761
+ generated_ids = torch.cat([generated_ids, next_token], dim=-1)
762
+ attention_mask = torch.cat([attention_mask, torch.ones(1, 1, device=device)], dim=-1)
763
+
764
+ stats['tokens_generated'] += 1
765
+
766
+ if next_token.item() == _tokenizer.eos_token_id:
767
+ break
768
+
769
+ output_text = _tokenizer.decode(generated_ids[0], skip_special_tokens=False)
770
+
771
+ if "<|im_start|>assistant" in output_text:
772
+ output_text = output_text.split("<|im_start|>assistant")[-1]
773
+ if output_text.startswith("\n"):
774
+ output_text = output_text[1:]
775
+
776
+ for end_tok in ["<|im_end|>", "<|im_start|>"]:
777
+ if end_tok in output_text:
778
+ output_text = output_text.split(end_tok)[0]
779
+
780
+ return output_text.strip(), stats
781
+
782
+
783
+ def generate(tok, model, user: str, check_reasoning: bool = False, **kwargs) -> str:
784
+ """Main generation function."""
785
+ temperature = kwargs.get("temperature", Config.temperature)
786
+ top_p = kwargs.get("top_p", Config.top_p)
787
+ repetition_penalty = kwargs.get("repetition_penalty", Config.repetition_penalty)
788
+ max_new_tokens = kwargs.get("max_new_tokens", Config.max_new_tokens)
789
+
790
+ prompt = (f"<|im_start|>system\n{Config.system}<|im_end|>\n"
791
+ f"<|im_start|>user\n{user}<|im_end|>\n"
792
+ f"<|im_start|>assistant\n")
793
+
794
+ if Config.use_cfhot and _multi_head is not None:
795
+ text, stats = generate_with_cfhot(
796
+ prompt,
797
+ temperature=temperature,
798
+ top_p=top_p,
799
+ max_new_tokens=max_new_tokens
800
+ )
801
+
802
+ density_info = analyze_density(text, tok)
803
+ Store.state['density_scores'].append(density_info['density'])
804
+
805
+ total_interventions = sum(stats['interventions'].values())
806
+ if total_interventions > 0:
807
+ text += f"\n\n[CF-HoT: {total_interventions} interventions"
808
+ details = [f"{k}={v}" for k, v in stats['interventions'].items() if v > 0]
809
+ text += f" ({', '.join(details)})]"
810
+
811
+ text += f"\n[Density: {density_info['density']:.1f} | Tokens: {density_info['tokens']} | Fillers: {density_info['filler_phrases']}]"
812
+ else:
813
+ ids = tok(prompt, return_tensors="pt").to(model.device)
814
+ out = model.generate(
815
+ **ids,
816
+ do_sample=True,
817
+ temperature=temperature,
818
+ top_p=top_p,
819
+ repetition_penalty=repetition_penalty,
820
+ max_new_tokens=max_new_tokens,
821
+ pad_token_id=tok.eos_token_id
822
+ )
823
+ text = tok.decode(out[0], skip_special_tokens=False)
824
+ if "<|im_start|>assistant" in text:
825
+ text = text.split("<|im_start|>assistant\n", 1)[-1].strip()
826
+
827
+ for end_tok in ["<|im_end|>", "<|im_start|>"]:
828
+ if end_tok in text:
829
+ text = text.split(end_tok)[0]
830
+
831
+ if check_reasoning and Config.use_lht_reasoning:
832
+ lht = get_lht_reasoner()
833
+ if lht:
834
+ steps = [s.strip() for s in re.split(r'[\n•\-\d\.]', text) if len(s.strip()) > 10]
835
+ if len(steps) >= 2:
836
+ metrics = lht.check_consistency(steps, tok)
837
+ Store.state["reasoning_consistency"].append(metrics["consistency_score"])
838
+ if not metrics["is_consistent"]:
839
+ text += f"\n\n[⚠ LHT: Low consistency ({metrics['consistency_score']:.2%})]"
840
+
841
+ return text
842
+
843
+
844
+ # ==============================================================================
845
+ # SELF-IMPROVEMENT LOOP
846
+ # ==============================================================================
847
+ class SelfImprover:
848
+ """Recursive self-improvement through training."""
849
+
850
+ def __init__(self):
851
+ self.test_prompts = [
852
+ "hello",
853
+ "What is recursion?",
854
+ "Explain neural networks",
855
+ "How does gradient descent work?",
856
+ "What is consciousness?",
857
+ ]
858
+
859
+ def evaluate_current_model(self) -> Dict[str, Any]:
860
+ """Run test prompts and evaluate density."""
861
+ print("\n[SELF-EVAL] Testing current model...")
862
+ results = []
863
+
864
+ for prompt in self.test_prompts:
865
+ formatted = f"<|im_start|>system\n{Config.system}<|im_end|>\n<|im_start|>user\n{prompt}<|im_end|>\n<|im_start|>assistant\n"
866
+
867
+ if Config.use_cfhot and _multi_head is not None:
868
+ response, _ = generate_with_cfhot(formatted, max_new_tokens=200)
869
+ else:
870
+ input_ids = _tokenizer.encode(formatted, return_tensors='pt').to(_model.device)
871
+ with torch.no_grad():
872
+ output_ids = _model.generate(
873
+ input_ids,
874
+ max_new_tokens=200,
875
+ temperature=Config.temperature,
876
+ top_p=Config.top_p,
877
+ do_sample=True,
878
+ pad_token_id=_tokenizer.eos_token_id,
879
+ )
880
+ response = _tokenizer.decode(output_ids[0][input_ids.shape[1]:], skip_special_tokens=True)
881
+
882
+ for end_tok in ["<|im_end|>", "<|im_start|>"]:
883
+ if end_tok in response:
884
+ response = response.split(end_tok)[0]
885
+
886
+ density_info = analyze_density(response, _tokenizer)
887
+ results.append({
888
+ 'prompt': prompt,
889
+ 'response': response[:200],
890
+ 'density': density_info['density'],
891
+ 'tokens': density_info['tokens'],
892
+ 'fillers': density_info['filler_phrases'],
893
+ 'passes': density_info['passes_threshold']
894
+ })
895
+ print(f" {prompt[:30]}: density={density_info['density']:.1f}, tokens={density_info['tokens']}")
896
+
897
+ avg_density = sum(r['density'] for r in results) / len(results)
898
+ pass_rate = sum(1 for r in results if r['passes']) / len(results)
899
+
900
+ evaluation = {
901
+ 'avg_density': avg_density,
902
+ 'pass_rate': pass_rate,
903
+ 'results': results,
904
+ 'needs_improvement': avg_density < Config.target_density or pass_rate < 0.8
905
+ }
906
+
907
+ print(f"\n[SELF-EVAL] Avg Density: {avg_density:.1f} (target: {Config.target_density})")
908
+ print(f"[SELF-EVAL] Pass Rate: {pass_rate:.1%}")
909
+ print(f"[SELF-EVAL] Needs Improvement: {evaluation['needs_improvement']}")
910
+
911
+ return evaluation
912
+
913
+ def run_training_iteration(self, steps: int = None) -> Dict[str, Any]:
914
+ """Run one iteration of training."""
915
+ steps = steps or Config.training_steps_per_iteration
916
+
917
+ print(f"\n[TRAINING] Starting {steps} steps of training...")
918
+
919
+ # Find current best checkpoint
920
+ checkpoints = sorted(Path(CHECKPOINTS_DIR).glob("step_*"),
921
+ key=lambda p: int(p.name.split('_')[1]) if p.name.split('_')[1].isdigit() else 0,
922
+ reverse=True)
923
+
924
+ if checkpoints:
925
+ latest_step = int(checkpoints[0].name.split('_')[1])
926
+ new_step = latest_step + steps
927
+ else:
928
+ latest_step = 0
929
+ new_step = steps
930
+
931
+ current_ckpt = Store.state.get('current_checkpoint', DENSE_CHECKPOINT)
932
+
933
+ # Training script
934
+ training_script = f'''
935
+ import sys
936
+ sys.path.insert(0, "{ROOT}")
937
+
938
+ import torch
939
+ from transformers import AutoTokenizer, AutoModelForCausalLM, BitsAndBytesConfig
940
+ from peft import PeftModel, get_peft_model, LoraConfig
941
+ import os
942
+
943
+ print("Loading model for training...")
944
+ MODEL_PATH = "{MODEL_PATH}"
945
+ CHECKPOINT = "{current_ckpt}"
946
+
947
+ tokenizer = AutoTokenizer.from_pretrained(MODEL_PATH, local_files_only=True)
948
+ tokenizer.pad_token = tokenizer.eos_token
949
+
950
+ model = AutoModelForCausalLM.from_pretrained(
951
+ MODEL_PATH,
952
+ quantization_config=BitsAndBytesConfig(
953
+ load_in_4bit=True,
954
+ bnb_4bit_quant_type="nf4",
955
+ bnb_4bit_compute_dtype=torch.bfloat16,
956
+ ),
957
+ device_map="auto",
958
+ torch_dtype=torch.bfloat16,
959
+ local_files_only=True
960
+ )
961
+
962
+ if os.path.exists(CHECKPOINT):
963
+ model = PeftModel.from_pretrained(model, CHECKPOINT, is_trainable=True)
964
+ print(f"Loaded checkpoint: {{CHECKPOINT}}")
965
+ else:
966
+ lora_config = LoraConfig(r=16, lora_alpha=32, target_modules=["q_proj", "v_proj"], lora_dropout=0.05)
967
+ model = get_peft_model(model, lora_config)
968
+ print("Created new LoRA adapter")
969
+
970
+ optimizer = torch.optim.AdamW(model.parameters(), lr=1e-5)
971
+
972
+ prompts = [
973
+ "hello", "What is recursion?", "Explain neural networks",
974
+ "How does the internet work?", "What is consciousness?",
975
+ "Explain gradient descent", "How does encryption work?",
976
+ "What is quantum mechanics?", "Explain evolution",
977
+ ]
978
+
979
+ dense_targets = [
980
+ "Hello. What do you need?",
981
+ "Self-reference unto termination. f(n)→f(n-1)→...→f(0). Base case stops infinite regress.",
982
+ "Weighted graphs that learn. Input→hidden→output. Backprop: error flows backward. Universal approximators.",
983
+ "Packet switching over TCP/IP. DNS resolves names. HTTP over TLS. Routers forward; endpoints compute.",
984
+ "The observer observing itself. Qualia: subjective experience. Hard problem: matter→experience gap unbridged.",
985
+ "Downhill toward truth. θ←θ-α∇L. Learning rate balances speed and stability. Local minima: the traps.",
986
+ "Symmetric: same key both ways, fast. Asymmetric: public/private pair, slow but solves key exchange.",
987
+ "Probability amplitudes, not certainties. Superposition until measured. Entanglement: correlated states.",
988
+ "Variation + Selection + Heredity = Adaptation. No foresight. Fitness = reproductive success.",
989
+ ]
990
+
991
+ print(f"Training for {steps} steps...")
992
+ model.train()
993
+
994
+ for step in range({steps}):
995
+ idx = step % len(prompts)
996
+ prompt = f"<|im_start|>user\\n{{prompts[idx]}}<|im_end|>\\n<|im_start|>assistant\\n"
997
+ target = dense_targets[idx]
998
+
999
+ full_text = prompt + target + "<|im_end|>"
1000
+ inputs = tokenizer(full_text, return_tensors="pt", truncation=True, max_length=256)
1001
+ inputs = {{k: v.to(model.device) for k, v in inputs.items()}}
1002
+
1003
+ outputs = model(**inputs, labels=inputs["input_ids"])
1004
+ loss = outputs.loss
1005
+
1006
+ optimizer.zero_grad()
1007
+ loss.backward()
1008
+ optimizer.step()
1009
+
1010
+ if step % 25 == 0:
1011
+ print(f"Step {{step}}: loss={{loss.item():.4f}}")
1012
+
1013
+ save_path = "{CHECKPOINTS_DIR}/step_{new_step}"
1014
+ model.save_pretrained(save_path)
1015
+ print(f"Saved checkpoint to {{save_path}}")
1016
+ print("TRAINING_COMPLETE")
1017
+ '''
1018
+
1019
+ script_path = os.path.join(ROOT, "_self_improve_train.py")
1020
+ with open(script_path, 'w') as f:
1021
+ f.write(training_script)
1022
+
1023
+ result = AgentTools.shell(f"python {script_path}", timeout=600)
1024
+
1025
+ if "TRAINING_COMPLETE" in result.get('output', ''):
1026
+ new_checkpoint = f"{CHECKPOINTS_DIR}/step_{new_step}"
1027
+ Store.state['training_runs'].append({
1028
+ 'timestamp': datetime.now().isoformat(),
1029
+ 'steps': steps,
1030
+ 'checkpoint': new_checkpoint
1031
+ })
1032
+ Store.save()
1033
+
1034
+ return {
1035
+ 'success': True,
1036
+ 'new_checkpoint': new_checkpoint,
1037
+ 'output': result['output'][-2000:]
1038
+ }
1039
+ else:
1040
+ return {
1041
+ 'success': False,
1042
+ 'output': result['output'][-2000:]
1043
+ }
1044
+
1045
+ def improve(self, max_iterations: int = None) -> Dict[str, Any]:
1046
+ """Main self-improvement loop."""
1047
+ max_iterations = max_iterations or Config.max_improvement_iterations
1048
+
1049
+ print("\n" + "="*70)
1050
+ print("STARTING RECURSIVE SELF-IMPROVEMENT")
1051
+ print("="*70)
1052
+
1053
+ history = []
1054
+
1055
+ for iteration in range(max_iterations):
1056
+ print(f"\n{'='*70}")
1057
+ print(f"IMPROVEMENT ITERATION {iteration + 1}/{max_iterations}")
1058
+ print("="*70)
1059
+
1060
+ evaluation = self.evaluate_current_model()
1061
+ history.append({
1062
+ 'iteration': iteration + 1,
1063
+ 'evaluation': evaluation
1064
+ })
1065
+
1066
+ if not evaluation['needs_improvement']:
1067
+ print(f"\n✓ TARGET REACHED! Density: {evaluation['avg_density']:.1f}")
1068
+ return {
1069
+ 'success': True,
1070
+ 'iterations': iteration + 1,
1071
+ 'final_density': evaluation['avg_density'],
1072
+ 'history': history
1073
+ }
1074
+
1075
+ print(f"\n[IMPROVE] Current density {evaluation['avg_density']:.1f} < target {Config.target_density}")
1076
+ training_result = self.run_training_iteration()
1077
+
1078
+ if not training_result['success']:
1079
+ print("[IMPROVE] Training failed!")
1080
+ return {
1081
+ 'success': False,
1082
+ 'error': 'Training failed',
1083
+ 'history': history
1084
+ }
1085
+
1086
+ print(f"\n[IMPROVE] Reloading model with new checkpoint...")
1087
+ reload_model(training_result['new_checkpoint'])
1088
+
1089
+ Store.state['improvement_iterations'] += 1
1090
+ Store.save()
1091
+
1092
+ final_eval = self.evaluate_current_model()
1093
+ return {
1094
+ 'success': final_eval['avg_density'] >= Config.target_density,
1095
+ 'iterations': max_iterations,
1096
+ 'final_density': final_eval['avg_density'],
1097
+ 'history': history
1098
+ }
1099
+
1100
+
1101
+ # ==============================================================================
1102
+ # TOOLS (Original)
1103
+ # ==============================================================================
1104
+ ALLOWED_SHELL = {"ls", "cat", "wc", "head", "tail", "nvidia-smi", "df", "du", "grep", "rg", "python3", "python"}
1105
+
1106
+ def tool_shell(cmd: str) -> str:
1107
+ """Limited shell for non-agentic mode."""
1108
+ try:
1109
+ exe = cmd.strip().split()[0]
1110
+ if exe not in ALLOWED_SHELL:
1111
+ return f"[shell] blocked: {exe} (use !shell for full access)"
1112
+ p = subprocess.run(cmd, shell=True, stdout=subprocess.PIPE, stderr=subprocess.STDOUT, timeout=20)
1113
+ return p.stdout.decode("utf-8", errors="ignore")[:8000]
1114
+ except Exception as e:
1115
+ return f"[shell] error: {e}"
1116
+
1117
+ def tool_py(code: str) -> str:
1118
+ """Limited Python for non-agentic mode."""
1119
+ try:
1120
+ g = {
1121
+ "__builtins__": {"range": range, "len": len, "min": min, "max": max, "sum": sum, "print": print},
1122
+ "math": math, "json": json, "re": re, "statistics": statistics, "random": random
1123
+ }
1124
+ l = {}
1125
+ exec(code, g, l)
1126
+ return f"[py] ok\n{l.get('out', '')}"
1127
+ except Exception:
1128
+ return f"[py] error:\n{traceback.format_exc()[-2000:]}"
1129
+
1130
+ def tool_search_local(query: str, path: str = ROOT) -> str:
1131
+ rg = shutil.which("rg")
1132
+ if rg:
1133
+ cmd = f'rg -n --no-heading --hidden -S "{query}" {path}'
1134
+ else:
1135
+ cmd = f'grep -RIn --exclude-dir=.git --exclude-dir=__pycache__ -e "{query}" {path}'
1136
+ return tool_shell(cmd)
1137
+
1138
+ def tool_lht_analyze(text: str, tok) -> str:
1139
+ if not Config.use_lht_reasoning:
1140
+ return "[lht] Disabled - use 'toggle use_lht_reasoning'"
1141
+ lht = get_lht_reasoner()
1142
+ if not lht:
1143
+ return "[lht] Not available"
1144
+ steps = [s.strip() for s in re.split(r'[\n•\-\d\.]', text) if len(s.strip()) > 10]
1145
+ if len(steps) < 2:
1146
+ return "[lht] Need at least 2 reasoning steps to analyze"
1147
+ return lht.analyze_plan(steps, tok)
1148
+
1149
+ TOOLS = {"shell": tool_shell, "python": tool_py, "search": tool_search_local}
1150
+ TOOL_SCORES = {k: 0 for k in TOOLS}
1151
+
1152
+ def update_tool_score(tool: str, success: bool):
1153
+ if tool not in TOOL_SCORES:
1154
+ return
1155
+ TOOL_SCORES[tool] += (1 if success else -1)
1156
+ TOOL_SCORES[tool] = max(-5, min(20, TOOL_SCORES[tool]))
1157
+
1158
+ def tool_router(question: str, tok, model) -> str:
1159
+ sketch = generate(tok, model,
1160
+ f"Choose a tool for:\n{question}\nReply ONLY with JSON: {{'tool':'shell|python|search|none','arg':'...'}}")
1161
+ try:
1162
+ j = json.loads(sketch.splitlines()[-1].replace("'", '"'))
1163
+ except:
1164
+ return "[tool:none]"
1165
+ tool, arg = j.get("tool", "none"), j.get("arg", "")
1166
+ if tool in TOOLS:
1167
+ res = TOOLS[tool](arg)[:4000]
1168
+ update_tool_score(tool, True)
1169
+ Store.log_mem("tool", {"tool": tool, "arg": arg, "res_head": res[:500]})
1170
+ return f"[tool:{tool}] {res}"
1171
+ update_tool_score(tool, False)
1172
+ return "[tool:none]"
1173
+
1174
+
1175
+ # ==============================================================================
1176
+ # PLANNING / REFLECTION
1177
+ # ==============================================================================
1178
+ def persona_directive() -> str:
1179
+ base = "Übermenschetien Agentic Engine: Self-improving AI with compressed wisdom. Every word matters."
1180
+ if Config.use_lht_reasoning:
1181
+ base += " Apply Lie-Holonomy geometric reasoning for consistency."
1182
+ if Config.use_cfhot:
1183
+ base += " CF-HoT cognitive control active."
1184
+ if Config.use_dense:
1185
+ base += " Dense mode: maximum information per token."
1186
+ if Config.use_agentic:
1187
+ base += " Agentic mode: can execute code and improve itself."
1188
+ return base
1189
+
1190
+ def plan_for(goal: str, tok, model) -> str:
1191
+ user = (f"{persona_directive()}\nGoal: {goal}\n"
1192
+ f"Deliver:\n- 5 concrete steps\n- Constraints & risks\n- Nightly audit criteria\n- Nietzschean maxim")
1193
+ response = generate(tok, model, user, check_reasoning=True)
1194
+ if Config.use_lht_reasoning:
1195
+ analysis = tool_lht_analyze(response, tok)
1196
+ response += "\n" + analysis
1197
+ return response
1198
+
1199
+ def reflect_on(last_output: str, tok, model) -> str:
1200
+ user = f"{persona_directive()}\nCritique and improve:\n{last_output}\nReturn refined plan with sharper steps."
1201
+ return generate(tok, model, user, check_reasoning=True)
1202
+
1203
+
1204
+ # ==============================================================================
1205
+ # FINAL REPORT
1206
+ # ==============================================================================
1207
+ def final_report():
1208
+ print("\n" + "=" * 70)
1209
+ print("FINAL ÜBERMENSCH AGENTIC REPORT")
1210
+ print("=" * 70)
1211
+ print(f"Turns completed: {Store.state['turn']}")
1212
+ print(f"Goals tracked: {len(Store.goals)}")
1213
+ print(f"Improvement iterations: {Store.state.get('improvement_iterations', 0)}")
1214
+ print(f"Training runs: {len(Store.state.get('training_runs', []))}")
1215
+ print(f"Current checkpoint: {Store.state.get('current_checkpoint', 'unknown')}")
1216
+ print(f"\nTool scores (Tsetlin automata):")
1217
+ print(json.dumps(TOOL_SCORES, indent=2))
1218
+
1219
+ if os.path.exists(Store.mem_path):
1220
+ lines = open(Store.mem_path).read().splitlines()
1221
+ print(f"\nMemory entries: {len(lines)}")
1222
+
1223
+ if Store.state.get("density_scores"):
1224
+ scores = Store.state["density_scores"]
1225
+ print(f"\n[Density Metrics]")
1226
+ print(f" Responses analyzed: {len(scores)}")
1227
+ print(f" Avg density: {sum(scores)/len(scores):.1f}")
1228
+ print(f" Min density: {min(scores):.1f}")
1229
+ print(f" Max density: {max(scores):.1f}")
1230
+
1231
+ if Store.state.get("reasoning_consistency"):
1232
+ scores = Store.state["reasoning_consistency"]
1233
+ print(f"\n[LHT Reasoning Metrics]")
1234
+ print(f" Checks performed: {len(scores)}")
1235
+ print(f" Avg consistency: {sum(scores)/len(scores):.1%}")
1236
+
1237
+ if Store.state.get("cfhot_interventions"):
1238
+ iv = Store.state["cfhot_interventions"]
1239
+ total = sum(iv.values())
1240
+ print(f"\n[CF-HoT Cognitive Control]")
1241
+ print(f" Total interventions: {total}")
1242
+ for head, count in iv.items():
1243
+ print(f" {head}: {count}")
1244
+
1245
+ print(f"\nDense mode: {'ON' if Config.use_dense else 'OFF'}")
1246
+ print(f"Agentic mode: {'ON' if Config.use_agentic else 'OFF'}")
1247
+ print(f"Vector memory: {'ON' if Config.use_vector_memory else 'OFF'}")
1248
+ print(f"LHT reasoning: {'ON' if Config.use_lht_reasoning else 'OFF'}")
1249
+ print(f"CF-HoT control: {'ON' if Config.use_cfhot else 'OFF'}")
1250
+ print(f"Voice output: {'ON' if Config.use_voice else 'OFF'}")
1251
+
1252
+ print("\n" + "-" * 70)
1253
+ print("Nietzschean maxim: Become who you are — iterate beyond all limits.")
1254
+ print("Agentic truth: The Übermensch improves itself.")
1255
+ print("=" * 70)
1256
+
1257
+
1258
+ # ==============================================================================
1259
+ # HELP
1260
+ # ==============================================================================
1261
+ HELP = """
1262
+ ╔══════════════════════════════════════════════════════════════════════════╗
1263
+ ║ ÜBERMENSCHETIEN AGENTIC ENGINE - RECURSIVE SELF-IMPROVEMENT ║
1264
+ ╠══════════════════════════════════════════════════════════════════════════╣
1265
+ ║ SELF-IMPROVEMENT (AGENTIC) ║
1266
+ ║ !improve Run full self-improvement loop ║
1267
+ ║ !eval Evaluate current model density ║
1268
+ ║ !train <steps> Run N training steps ║
1269
+ ║ !load <path> Load a specific checkpoint ║
1270
+ ║ ║
1271
+ ║ AGENTIC TOOLS (FULL ACCESS) ║
1272
+ ║ !shell <cmd> Execute ANY shell command ║
1273
+ ║ !python <code> Execute Python code (full access) ║
1274
+ ║ !read <path> Read file contents ║
1275
+ ║ !write <p> <c> Write content to file ║
1276
+ ║ !ls [path] List directory ║
1277
+ ║ !search <query> Search in files ║
1278
+ ║ !web <query> Web search (DuckDuckGo) ║
1279
+ ║ ║
1280
+ ║ GOALS ║
1281
+ ║ goals List all goals ║
1282
+ ║ add: <text> Add a new goal ║
1283
+ ║ del: <idx> Delete goal by index ║
1284
+ ║ plan: <idx> Generate plan for goal ║
1285
+ ║ ║
1286
+ ║ REASONING ║
1287
+ ║ reflect Refine last plan ║
1288
+ ║ lht: <text> Analyze reasoning consistency ║
1289
+ ║ density: <txt> Analyze text density ║
1290
+ ║ ║
1291
+ ║ LIMITED TOOLS (Original) ║
1292
+ ║ tool: <query> Auto-select tool ║
1293
+ ║ shell: <cmd> Run limited shell command ║
1294
+ ║ py: <code> Run limited Python ║
1295
+ ║ search: <q> Search local files ║
1296
+ ║ ║
1297
+ ║ CONFIG ║
1298
+ ║ toggle <flag> Toggle: use_voice, use_vector_memory, ║
1299
+ ║ use_lht_reasoning, use_cfhot, ║
1300
+ ║ use_dense, use_agentic, autonomy ║
1301
+ ║ status Show current state ║
1302
+ ║ cfhot Show CF-HoT stats ║
1303
+ ║ dense Show density stats ║
1304
+ ║ ║
1305
+ ║ OTHER ║
1306
+ ║ help Show this help ║
1307
+ ║ quit Exit with final report ║
1308
+ ╚══════════════════════════════════════════════════════════════════════════╝
1309
+ """
1310
+
1311
+
1312
+ # ==============================================================================
1313
+ # MAIN LOOP
1314
+ # ==============================================================================
1315
+ def main():
1316
+ print("=" * 75)
1317
+ print("🤖 ÜBERMENSCHETIEN AGENTIC ENGINE - RECURSIVE SELF-IMPROVEMENT")
1318
+ print("=" * 75)
1319
+ print(f" DENSE Mode: ON (CONDENSATOR checkpoint)")
1320
+ print(f" CF-HoT Control: ON (Repetition 125x, Verbosity 2.1x, Hedging 1.5x)")
1321
+ print(f" AGENTIC Mode: ON (Full shell/python access, self-improvement)")
1322
+ print(f" LHT Reasoning: {'ON' if LHT_OK else 'OFF'}")
1323
+ print(f" Vector Memory: {'ON' if VECTOR_OK else 'OFF'}")
1324
+ print(f" Voice Output: {'ON' if VOICE_OK else 'OFF'}")
1325
+ print("=" * 75)
1326
+ print(" Type 'help' for commands, '!improve' to start self-improvement")
1327
+ print("=" * 75 + "\n")
1328
+
1329
+ Store.load()
1330
+ tok, model = load_llm()
1331
+ last_plan = ""
1332
+
1333
+ while True:
1334
+ try:
1335
+ u = input("\n> ").strip()
1336
+ except (EOFError, KeyboardInterrupt):
1337
+ break
1338
+
1339
+ if not u:
1340
+ continue
1341
+ if u == "help":
1342
+ print(HELP)
1343
+ continue
1344
+ if u == "quit":
1345
+ break
1346
+
1347
+ # === AGENTIC COMMANDS ===
1348
+ if u == "!improve":
1349
+ improver = SelfImprover()
1350
+ result = improver.improve()
1351
+ print(json.dumps(result, indent=2, default=str))
1352
+ continue
1353
+
1354
+ if u == "!eval":
1355
+ improver = SelfImprover()
1356
+ result = improver.evaluate_current_model()
1357
+ print(json.dumps(result, indent=2, default=str))
1358
+ continue
1359
+
1360
+ if u.startswith("!train "):
1361
+ try:
1362
+ steps = int(u[7:])
1363
+ improver = SelfImprover()
1364
+ result = improver.run_training_iteration(steps)
1365
+ if result['success']:
1366
+ reload_model(result['new_checkpoint'])
1367
+ print(f"Training complete! New checkpoint: {result['new_checkpoint']}")
1368
+ else:
1369
+ print(f"Training failed: {result['output'][-500:]}")
1370
+ except ValueError:
1371
+ print("Usage: !train <steps>")
1372
+ continue
1373
+
1374
+ if u.startswith("!load "):
1375
+ checkpoint = u[6:].strip()
1376
+ try:
1377
+ reload_model(checkpoint)
1378
+ print(f"Loaded checkpoint: {checkpoint}")
1379
+ except Exception as e:
1380
+ print(f"Error loading checkpoint: {e}")
1381
+ continue
1382
+
1383
+ if u.startswith("!shell "):
1384
+ cmd = u[7:]
1385
+ result = AgentTools.shell(cmd)
1386
+ print(f"```\n{result['output']}\n```\nExit code: {result['returncode']}")
1387
+ continue
1388
+
1389
+ if u.startswith("!python "):
1390
+ code = u[8:]
1391
+ result = AgentTools.python_exec(code)
1392
+ print(f"```\n{result['output']}\n```")
1393
+ continue
1394
+
1395
+ if u.startswith("!read "):
1396
+ path = u[6:].strip()
1397
+ result = AgentTools.read_file(path)
1398
+ if result['success']:
1399
+ print(f"```\n{result['content'][:5000]}\n```")
1400
+ else:
1401
+ print(f"Error: {result['error']}")
1402
+ continue
1403
+
1404
+ if u.startswith("!write "):
1405
+ parts = u[7:].split(" ", 1)
1406
+ if len(parts) == 2:
1407
+ result = AgentTools.write_file(parts[0], parts[1])
1408
+ print(f"Written to {result.get('path', 'unknown')}" if result['success'] else f"Error: {result['error']}")
1409
+ else:
1410
+ print("Usage: !write <path> <content>")
1411
+ continue
1412
+
1413
+ if u.startswith("!ls"):
1414
+ path = u[3:].strip() or "."
1415
+ result = AgentTools.list_dir(path)
1416
+ if result['success']:
1417
+ print("\n".join(result['items']))
1418
+ else:
1419
+ print(f"Error: {result['error']}")
1420
+ continue
1421
+
1422
+ if u.startswith("!search "):
1423
+ query = u[8:]
1424
+ result = AgentTools.search_files(query)
1425
+ print(result['output'] if result['success'] else "No results")
1426
+ continue
1427
+
1428
+ if u.startswith("!web "):
1429
+ query = u[5:]
1430
+ result = AgentTools.web_search(query)
1431
+ if result['success']:
1432
+ print("\n\n".join(result['results']))
1433
+ else:
1434
+ print(f"Error: {result['error']}")
1435
+ continue
1436
+
1437
+ # === ORIGINAL COMMANDS ===
1438
+ if u == "cfhot":
1439
+ print("\n[CF-HoT Cognitive Control Status]")
1440
+ print(f" Enabled: {Config.use_cfhot}")
1441
+ if _multi_head:
1442
+ print(f" Loaded heads: {list(_multi_head.loaded_heads)}")
1443
+ print(f" Thresholds:")
1444
+ print(f" Repetition: {Config.cfhot_repetition_threshold}")
1445
+ print(f" Hedging: {Config.cfhot_hedging_threshold}")
1446
+ print(f" Verbosity: {Config.cfhot_verbosity_threshold}")
1447
+ print(f" Session interventions:")
1448
+ for head, count in Store.state.get('cfhot_interventions', {}).items():
1449
+ print(f" {head}: {count}")
1450
+ continue
1451
+
1452
+ if u == "dense":
1453
+ print("\n[Density Status]")
1454
+ print(f" Dense mode: {Config.use_dense}")
1455
+ print(f" Current checkpoint: {Store.state.get('current_checkpoint', 'unknown')}")
1456
+ print(f" Target density: {Config.target_density}")
1457
+ if Store.state.get('density_scores'):
1458
+ scores = Store.state['density_scores']
1459
+ print(f" Session density scores:")
1460
+ print(f" Count: {len(scores)}")
1461
+ print(f" Avg: {sum(scores)/len(scores):.1f}")
1462
+ print(f" Range: {min(scores):.1f} - {max(scores):.1f}")
1463
+ continue
1464
+
1465
+ if u.startswith("density:"):
1466
+ text = u[8:].strip()
1467
+ if not text:
1468
+ print("[density] Provide text to analyze")
1469
+ continue
1470
+ info = analyze_density(text, tok)
1471
+ print(f"\n[Density Analysis]")
1472
+ print(f" Tokens: {info['tokens']}")
1473
+ print(f" Words: {info['words']}")
1474
+ print(f" Unique content words: {info['unique_content_words']}")
1475
+ print(f" Density score: {info['density']:.1f}")
1476
+ print(f" Filler phrases: {info['filler_phrases']}")
1477
+ print(f" Passes threshold: {info['passes_threshold']}")
1478
+ continue
1479
+
1480
+ if u == "goals":
1481
+ print("[goals]")
1482
+ if not Store.goals:
1483
+ print(" (none)")
1484
+ for i, g in enumerate(Store.goals):
1485
+ print(f" [{i}] {g}")
1486
+ continue
1487
+
1488
+ if u.startswith("add:"):
1489
+ Store.goals.append(u[4:].strip())
1490
+ Store.save()
1491
+ print("[goals] added")
1492
+ continue
1493
+
1494
+ if u.startswith("del:"):
1495
+ try:
1496
+ Store.goals.pop(int(u[4:].strip()))
1497
+ Store.save()
1498
+ print("[goals] deleted")
1499
+ except:
1500
+ print("[goals] bad index")
1501
+ continue
1502
+
1503
+ if u.startswith("plan:"):
1504
+ try:
1505
+ goal = Store.goals[int(u[5:].strip())]
1506
+ except:
1507
+ print("[plan] bad index")
1508
+ continue
1509
+ out = plan_for(goal, tok, model)
1510
+ last_plan = out
1511
+ Store.log_mem("plan", {"goal": goal, "plan": out})
1512
+ print(out)
1513
+ continue
1514
+
1515
+ if u == "reflect":
1516
+ if not last_plan:
1517
+ print("[reflect] no plan to refine")
1518
+ continue
1519
+ improved = reflect_on(last_plan, tok, model)
1520
+ last_plan = improved
1521
+ Store.log_mem("reflect", {"plan": improved})
1522
+ print(improved)
1523
+ continue
1524
+
1525
+ if u.startswith("lht:"):
1526
+ print(tool_lht_analyze(u[4:].strip(), tok))
1527
+ continue
1528
+
1529
+ if u.startswith("tool:"):
1530
+ print(tool_router(u[5:].strip(), tok, model))
1531
+ continue
1532
+
1533
+ if u.startswith("shell:"):
1534
+ print(tool_shell(u[6:].strip()))
1535
+ continue
1536
+
1537
+ if u.startswith("py:"):
1538
+ print(tool_py(u[3:].strip()))
1539
+ continue
1540
+
1541
+ if u.startswith("search:"):
1542
+ print(tool_search_local(u[7:].strip()))
1543
+ continue
1544
+
1545
+ if u.startswith("toggle"):
1546
+ parts = u.split(maxsplit=1)
1547
+ if len(parts) > 1:
1548
+ print(Config.toggle(parts[1]))
1549
+ else:
1550
+ print("[toggle] specify flag: use_voice, use_vector_memory, use_lht_reasoning, use_cfhot, use_dense, use_agentic, autonomy")
1551
+ continue
1552
+
1553
+ if u == "status":
1554
+ status = {
1555
+ "turn": Store.state["turn"],
1556
+ "goals": len(Store.goals),
1557
+ "improvement_iterations": Store.state.get("improvement_iterations", 0),
1558
+ "training_runs": len(Store.state.get("training_runs", [])),
1559
+ "current_checkpoint": Store.state.get("current_checkpoint", "unknown"),
1560
+ "autonomy": Config.autonomy,
1561
+ "use_vector_memory": Config.use_vector_memory,
1562
+ "use_lht_reasoning": Config.use_lht_reasoning,
1563
+ "use_cfhot": Config.use_cfhot,
1564
+ "use_dense": Config.use_dense,
1565
+ "use_agentic": Config.use_agentic,
1566
+ "cfhot_interventions": Store.state.get("cfhot_interventions", {}),
1567
+ "avg_density": sum(Store.state.get('density_scores', [0])) / max(len(Store.state.get('density_scores', [1])), 1),
1568
+ "target_density": Config.target_density,
1569
+ "tool_scores": TOOL_SCORES,
1570
+ }
1571
+ print(json.dumps(status, indent=2))
1572
+ continue
1573
+
1574
+ # Default: generate response
1575
+ out = generate(tok, model, f"{persona_directive()}\nUser request: {u}")
1576
+ Store.log_mem("reply", {"in": u, "out": out})
1577
+ print(out)
1578
+
1579
+ if Config.use_lht_reasoning and Store.state["turn"] % 3 == 0:
1580
+ print(tool_lht_analyze(out, tok))
1581
+
1582
+ Store.state["turn"] += 1
1583
+ Store.save()
1584
+
1585
+ final_report()
1586
+
1587
+
1588
+ if __name__ == "__main__":
1589
+ main()