Files
ARC-Base-8B-Condensed/arc_engine_v21_multimedia.py
ModelHub XC 5bc82cc56a 初始化项目,由ModelHub XC社区提供模型
Model: LoganResearch/ARC-Base-8B-Condensed
Source: Original Platform
2026-06-24 10:52:18 +08:00

6854 lines
262 KiB
Python
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#!/usr/bin/env python3
import threading
"""
═══════════════════════════════════════════════════════════════════════════════
ARC ENGINE v2.1 - Adaptive Recursive Cognition (Übermenschetien)
═══════════════════════════════════════════════════════════════════════════════
CORE CAPABILITIES:
- Hermes-3 8B with DENSE CONDENSATOR training
- CF-HoT 125× Repetition Detection (toggle on/off)
- Stable RSI with safeguards + RSI-15 stress test
- Book Mode: Extended generation for novels/long-form
- Idea Mode: Claude Opus 4.5 brainstorming integration
- Full browser automation (Playwright)
- Gmail API + browser email
- Complete agentic toolset
- Crypto mining automation
NEW IN v2.1:
- !cfhot / !125x Toggle 125× repetition head
- !rsi15 15-iteration stress test
- !book Book writing mode (16K tokens)
- !idea Claude-powered idea generation
- !claude Direct Claude Opus 4.5 prompting
- !plot Quality visualization
- !export/!import Checkpoint packaging
- !benchmark Evaluation suite
- !learn Learn from conversation
- !api REST API server
AUTHOR: Logan Matthew Napolitano
LICENSE: CC BY 4.0
REPOSITORY: huggingface.co/LoganResearch/ARC-Base-8B-Condensed
"An 8B that improves itself WITHOUT going insane"
═══════════════════════════════════════════════════════════════════════════════
"""
import os
import sys
import json
import time
import shutil
import subprocess
import traceback
import random
import math
import statistics
import re
import hashlib
import zipfile
import base64
import queue
from io import BytesIO
from datetime import datetime
from typing import List, Dict, Any, Optional, Tuple, Callable
from pathlib import Path
from collections import deque
from dataclasses import dataclass, field, asdict
from threading import Thread
from http.server import HTTPServer, BaseHTTPRequestHandler
import copy
import torch
import torch.nn as nn
import torch.nn.functional as F
# === OPTIONAL: PLOTTING ===
PLOT_OK = False
try:
import matplotlib
matplotlib.use('Agg')
import matplotlib.pyplot as plt
PLOT_OK = True
except ImportError:
pass
# === OPTIONAL: CLAUDE API ===
CLAUDE_API_OK = False
_anthropic_client = None
try:
import anthropic
CLAUDE_API_OK = True
print("[claude] ✓ Anthropic API available")
except ImportError:
print("[claude] Not available - pip install anthropic")
# === OPTIONAL: REQUESTS ===
REQUESTS_OK = False
try:
import requests
REQUESTS_OK = True
except ImportError:
pass
# === TKINTER FOR STREAMING WINDOW ===
TK_OK = False
try:
import tkinter as tk
from tkinter import scrolledtext
TK_OK = True
print("[gui] ✓ tkinter available")
except ImportError:
print("[gui] tkinter not available")
# === PIL FOR IMAGES ===
PIL_OK = False
try:
from PIL import Image, ImageTk
PIL_OK = True
print("[image] ✓ PIL available")
except ImportError:
print("[image] PIL not available - pip install Pillow")
# === PYGAME FOR AUDIO ===
PYGAME_OK = False
try:
import pygame
pygame.mixer.init()
PYGAME_OK = True
print("[audio] ✓ pygame available")
except:
pass
# === GTTS ===
GTTS_OK = False
try:
from gtts import gTTS
GTTS_OK = True
except ImportError:
pass
# === DIFFUSERS FOR IMAGE GEN ===
DIFFUSERS_OK = False
try:
from diffusers import AutoPipelineForText2Image
DIFFUSERS_OK = True
print("[image-gen] ✓ Diffusers available")
except ImportError:
pass
# === OPENAI FOR DALLE ===
OPENAI_OK = False
try:
import openai
OPENAI_OK = True
except ImportError:
pass
# === PATHS ===
ROOT = os.path.dirname(os.path.abspath(__file__))
DATA_DIR = os.path.join(ROOT, "data")
SCRIPT_DIR = os.path.join(ROOT, "scripts")
RUN_DIR = os.path.join(ROOT, "runs")
LHT_DIR = os.path.join(ROOT, "lht")
CHECKPOINTS_DIR = os.path.join(ROOT, "dense_checkpoints_v2")
TRAINING_DIR = os.path.join(ROOT, "condensator_output")
LOGS_DIR = os.path.join(ROOT, "improvement_logs")
ROLLBACK_DIR = os.path.join(ROOT, "rollback_checkpoints")
BOOKS_DIR = os.path.join(ROOT, "books")
EXPORTS_DIR = os.path.join(ROOT, "exports")
IDEAS_DIR = os.path.join(ROOT, "ideas")
# Model paths
MODEL_PATH = "/mnt/nvme2/ubermesnchetien4/models/merged-final-v5"
DENSE_CHECKPOINT = os.path.join(ROOT, "dense_checkpoints_v2/step_100")
CFHOT_CHECKPOINT = os.path.join(ROOT, "results/cfhot_risk_v2/ckpt_5000")
CFHOT_125X_PATH = os.path.join(ROOT, "cfhot_checkpoints/ckpt_5000/risk_predictor.pt")
MULTI_HEAD_DIR = os.path.join(ROOT, "results/multi_head_v2")
for path in [DATA_DIR, SCRIPT_DIR, RUN_DIR, LHT_DIR, LOGS_DIR, ROLLBACK_DIR, BOOKS_DIR, EXPORTS_DIR, IDEAS_DIR]:
os.makedirs(path, exist_ok=True)
# === OPTIONAL IMPORTS ===
VOICE_OK = False
try:
import pyttsx3
TTS = pyttsx3.init()
VOICE_OK = True
except:
pass
VECTOR_OK = False
try:
import chromadb
from sentence_transformers import SentenceTransformer
EMBED_MODEL = os.environ.get("UBERMENCHETIEN_EMBED_MODEL", "all-MiniLM-L6-v2")
_client = chromadb.Client()
_collection = _client.get_or_create_collection("ubermenschetien_memory")
_embedder = SentenceTransformer(EMBED_MODEL)
VECTOR_OK = True
except:
pass
# === LHT IMPORT ===
LHT_OK = False
try:
from lht import LieHolonomyTransformer, LHTConfig, WaypointDetector
LHT_OK = True
print("[lht] Lie-Holonomy modules loaded")
except ImportError:
print("[lht] Not available - running without geometric reasoning")
# === BROWSER IMPORT ===
BROWSER_OK = False
_playwright = None
_browser = None
_page = None
_browser_context = None
try:
from playwright.sync_api import sync_playwright
BROWSER_OK = True
print("[browser] Playwright available")
except ImportError:
print("[browser] Not available - pip install playwright && playwright install firefox")
# === GMAIL API IMPORT ===
GMAIL_API_OK = False
_gmail_service = None
try:
from google.oauth2.credentials import Credentials
from google_auth_oauthlib.flow import InstalledAppFlow
from google.auth.transport.requests import Request
from googleapiclient.discovery import build
import pickle
GMAIL_API_OK = True
print("[gmail-api] Google API available")
except ImportError:
pass # Silent - optional feature
# === LOGIN CONFIGURATION ===
LOGIN_CONFIG = {
"use_persistent_profile": False,
"firefox_profile_path": "",
"use_gmail_api": False,
"gmail_credentials_file": "credentials.json",
"gmail_token_file": "gmail_token.pickle",
"human_typing_speed": (30, 120), # ms per char range
"max_login_retries": 3,
}
def save_login_config():
"""Save login configuration."""
config_path = os.path.join(ROOT, "login_config.json")
with open(config_path, 'w') as f:
json.dump(LOGIN_CONFIG, f, indent=2)
print(f"[config] ✅ Saved to {config_path}")
def load_login_config():
"""Load login configuration."""
global LOGIN_CONFIG
config_path = os.path.join(ROOT, "login_config.json")
if os.path.exists(config_path):
with open(config_path, 'r') as f:
LOGIN_CONFIG.update(json.load(f))
# Load on startup
load_login_config()
# ==============================================================================
# CLAUDE API INTEGRATION (Opus 4.5 / Sonnet)
# ==============================================================================
CLAUDE_CONFIG = {
"api_key": os.environ.get("ANTHROPIC_API_KEY", ""),
"model": "claude-sonnet-4-20250514",
"opus_model": "claude-opus-4-20250514",
"max_tokens": 8192,
"temperature": 0.8,
}
def init_claude_client():
"""Initialize Claude API client."""
global _anthropic_client
if not CLAUDE_API_OK:
return None
api_key = CLAUDE_CONFIG["api_key"]
if not api_key:
key_path = os.path.join(ROOT, ".anthropic_key")
if os.path.exists(key_path):
with open(key_path, 'r') as f:
api_key = f.read().strip()
CLAUDE_CONFIG["api_key"] = api_key
if api_key:
try:
_anthropic_client = anthropic.Anthropic(api_key=api_key)
print("[claude] ✓ API client initialized")
return _anthropic_client
except Exception as e:
print(f"[claude] ✗ Init failed: {e}")
return None
def claude_generate(prompt: str, system: str = None, max_tokens: int = None,
use_opus: bool = False, stream: bool = False) -> str:
"""Generate response using Claude API."""
global _anthropic_client
if _anthropic_client is None:
_anthropic_client = init_claude_client()
if _anthropic_client is None:
return "[claude] API not configured. Set ANTHROPIC_API_KEY or create .anthropic_key file"
try:
model = CLAUDE_CONFIG["opus_model"] if use_opus else CLAUDE_CONFIG["model"]
messages = [{"role": "user", "content": prompt}]
kwargs = {
"model": model,
"max_tokens": max_tokens or CLAUDE_CONFIG["max_tokens"],
"messages": messages,
}
if system:
kwargs["system"] = system
if stream:
# Streaming response
full_response = ""
with _anthropic_client.messages.stream(**kwargs) as stream_obj:
for text in stream_obj.text_stream:
print(text, end="", flush=True)
full_response += text
print() # Newline
return full_response
else:
response = _anthropic_client.messages.create(**kwargs)
return response.content[0].text
except Exception as e:
return f"[claude] API error: {e}"
# ==============================================================================
# CF-HoT 125× REPETITION DETECTION HEAD
# ==============================================================================
class CFHoT125xHead:
"""
The 125× class separation repetition detection head.
Predicts repetitive behavior BEFORE token emission.
Achieves 0.875 on repetitive vs 0.007 on clean text.
"""
def __init__(self):
self.loaded = False
self.risk_predictor = None
self.device = "cuda" if torch.cuda.is_available() else "cpu"
def load(self, checkpoint_path: str = None) -> bool:
"""Load the trained 125× head."""
if checkpoint_path is None:
checkpoint_path = CFHOT_125X_PATH
# Try multiple paths
paths_to_try = [
checkpoint_path,
os.path.join(ROOT, "cfhot_checkpoints/ckpt_5000/risk_predictor.pt"),
os.path.join(CFHOT_CHECKPOINT, "risk_predictor.pt"),
]
for path in paths_to_try:
if os.path.exists(path):
try:
checkpoint = torch.load(path, map_location=self.device)
if isinstance(checkpoint, dict):
self.risk_predictor = checkpoint.get("model", checkpoint.get("risk_predictor", checkpoint))
else:
self.risk_predictor = checkpoint
self.loaded = True
print(f"[cf-hot 125×] ✓ Loaded from {path}")
print(f"[cf-hot 125×] Separation: 125× (0.875 vs 0.007)")
return True
except Exception as e:
print(f"[cf-hot 125×] Load error: {e}")
print(f"[cf-hot 125×] ⚠ Head not found")
return False
def unload(self):
"""Unload to free VRAM."""
self.risk_predictor = None
self.loaded = False
if torch.cuda.is_available():
torch.cuda.empty_cache()
print("[cf-hot 125×] ✓ Unloaded")
def predict_risk(self, hidden_states: torch.Tensor) -> float:
"""Predict repetition risk."""
if not self.loaded or self.risk_predictor is None:
return 0.0
try:
with torch.no_grad():
if hasattr(self.risk_predictor, 'forward'):
h = hidden_states[:, -1, :] if len(hidden_states.shape) == 3 else hidden_states
risk = self.risk_predictor(h.to(self.device))
return float(torch.sigmoid(risk).mean())
except:
pass
return 0.0
# Global instance
_cfhot_125x_head = CFHoT125xHead()
def get_cfhot_head() -> CFHoT125xHead:
return _cfhot_125x_head
def toggle_cfhot_125x() -> str:
"""Toggle 125× head on/off."""
head = get_cfhot_head()
if head.loaded:
head.unload()
return "[cf-hot 125×] ✗ Disabled and unloaded"
else:
if head.load():
return "[cf-hot 125×] ✓ Enabled (125× separation active)"
return "[cf-hot 125×] ⚠ Could not load head"
# ==============================================================================
# BOOK MODE - Extended Long-Form Generation
# ==============================================================================
class BookWriter:
"""Generate book-length content."""
def __init__(self, generate_fn: Callable):
self.generate_fn = generate_fn
self.chapters = []
self.outline = ""
self.title = ""
def generate_outline(self, topic: str, num_chapters: int = 10) -> str:
prompt = f"""Create a detailed outline for: "{topic}"
{num_chapters} chapters with:
1. Chapter title
2. Main themes (3-5 points)
3. Connection to narrative
Be creative and comprehensive."""
if CLAUDE_API_OK and CLAUDE_CONFIG.get("api_key"):
self.outline = claude_generate(prompt, system="You are a bestselling author.")
else:
out, _, _ = self.generate_fn(prompt)
self.outline = out
return self.outline
def generate_chapter(self, num: int, title: str, context: str = "",
target_words: int = 3000) -> str:
prompt = f"""Write Chapter {num}: "{title}"
Outline: {self.outline[:1500]}
{f'Previous: ...{context[-800:]}' if context else 'Opening chapter.'}
Write ~{target_words} words with narrative, dialogue, descriptions."""
chapter = f"\n\n## Chapter {num}: {title}\n\n"
chunks = max(1, target_words // 500)
text = ""
for i in range(chunks):
chunk_prompt = prompt if i == 0 else f"Continue Chapter {num}:\n...{text[-400:]}\n\nContinue:"
if CLAUDE_API_OK and CLAUDE_CONFIG.get("api_key"):
chunk = claude_generate(chunk_prompt, max_tokens=2000)
else:
out, _, _ = self.generate_fn(chunk_prompt)
chunk = out
text += chunk + "\n"
words = len(text.split())
print(f"\r[book] Ch.{num}: {words}/{target_words} words", end="")
if words >= target_words:
break
print()
chapter += text
self.chapters.append(chapter)
return chapter
def write_book(self, topic: str, chapters: int = 10, words: int = 3000) -> str:
self.title = topic
self.chapters = []
print(f"\n{'='*60}")
print(f" 📚 BOOK MODE: '{topic}'")
print(f" {chapters} chapters × ~{words} words = ~{chapters*words:,} total")
print(f"{'='*60}\n")
print("[book] Generating outline...")
self.generate_outline(topic, chapters)
print("[book] ✓ Outline complete\n")
book = f"# {topic}\n\n## Outline\n\n{self.outline}\n\n---\n"
for i in range(1, chapters + 1):
print(f"\n[book] Chapter {i}/{chapters}...")
ctx = self.chapters[-1] if self.chapters else ""
ch = self.generate_chapter(i, f"Chapter {i}", ctx, words)
book += ch + "\n---\n"
# Save progress
path = os.path.join(BOOKS_DIR, f"{topic.replace(' ', '_')[:25]}.md")
with open(path, 'w') as f:
f.write(book)
print(f"\n{'='*60}")
print(f" ✅ BOOK COMPLETE: {len(self.chapters)} chapters")
print(f" Saved to: {path}")
print(f"{'='*60}")
return book
# ==============================================================================
# IDEA MODE - Claude-Powered Brainstorming
# ==============================================================================
class IdeaGenerator:
"""Generate extensive ideas using Claude."""
DEPTHS = {
"quick": (5, 2000),
"normal": (10, 4000),
"extensive": (20, 8000),
"deep": (30, 16000),
}
def __init__(self):
self.session = []
self.topic = ""
def generate(self, request: str, depth: str = "extensive", use_opus: bool = True) -> str:
num_ideas, max_tokens = self.DEPTHS.get(depth, self.DEPTHS["extensive"])
system = """You are a world-class innovation consultant and brainstorming expert.
Generate creative, practical, diverse ideas. For each:
1. Catchy name
2. Core concept (2-3 sentences)
3. Key benefits (3-5 points)
4. Challenges (2-3)
5. First steps (3 actions)
6. Effort: Low/Medium/High
7. Impact: 1-10 with justification
Be creative but actionable."""
prompt = f"""Generate {num_ideas} detailed ideas for: {request}
Consider:
- Conventional & innovative approaches
- Low-cost & premium options
- Tech-driven & human-centered solutions
- Quick wins & long-term plays
For EACH idea provide full details per the format."""
print(f"\n[idea] 💡 Generating {num_ideas} ideas ({depth} mode, {'Opus' if use_opus else 'Sonnet'})...")
if CLAUDE_API_OK and CLAUDE_CONFIG.get("api_key"):
result = claude_generate(prompt, system=system, max_tokens=max_tokens,
use_opus=use_opus, stream=True)
else:
out, _, _ = generate(prompt)
result = out
self.session.append({"topic": request, "depth": depth, "ideas": result})
self.topic = request
# Save
path = os.path.join(IDEAS_DIR, f"ideas_{request[:20].replace(' ', '_')}_{datetime.now().strftime('%H%M%S')}.md")
with open(path, 'w') as f:
f.write(f"# Ideas: {request}\n\n{result}")
print(f"\n[idea] Saved to {path}")
return result
def expand(self, idea_name: str) -> str:
prompt = f"""Expand this idea into a comprehensive plan:
Idea: {idea_name}
Context: {self.topic}
Provide:
1. Executive Summary
2. Detailed Description (3-5 paragraphs)
3. Implementation Roadmap (phases)
4. Resources Required
5. Risk Assessment
6. Success Metrics
7. 30-Day Action Plan"""
print(f"[idea] 📝 Expanding: {idea_name}...")
if CLAUDE_API_OK and CLAUDE_CONFIG.get("api_key"):
return claude_generate(prompt, max_tokens=4000, stream=True)
out, _, _ = generate(prompt)
return out
# Global instances
_book_writer = None
_idea_generator = None
def get_book_writer(gen_fn) -> BookWriter:
global _book_writer
if _book_writer is None:
_book_writer = BookWriter(gen_fn)
return _book_writer
def get_idea_generator() -> IdeaGenerator:
global _idea_generator
if _idea_generator is None:
_idea_generator = IdeaGenerator()
return _idea_generator
# ==============================================================================
# RSI-15: 15-ITERATION STRESS TEST
# ==============================================================================
def run_rsi_15(improver) -> dict:
"""Run 15-iteration RSI stress test."""
print("\n" + "="*60)
print(" 🔬 RSI-15 STRESS TEST")
print("="*60)
results = {
"iterations": [], "rollbacks": 0, "improvements": 0,
"initial": Store.state.get("best_quality_score", 0),
"final": 0, "peak": Store.state.get("best_quality_score", 0),
"stopped_early": False, "reason": None,
}
consecutive_rollbacks = 0
for i in range(15):
print(f"\n{''*50}\n ITERATION {i+1}/15\n{''*50}")
pre = Store.state.get("best_quality_score", 0)
train_result = improver.run_training_iteration(Config.training_steps_per_iteration)
if not train_result.get("success"):
print(" ⚠ Training failed")
continue
post = Store.state.get("best_quality_score", 0)
delta = post - pre
iteration = {"i": i+1, "pre": pre, "post": post, "delta": delta, "action": "?"}
if delta > 0.02:
iteration["action"] = "KEEP"
results["improvements"] += 1
consecutive_rollbacks = 0
print(f" ✅ +{delta:.4f}")
elif delta < -0.05:
iteration["action"] = "ROLLBACK"
results["rollbacks"] += 1
consecutive_rollbacks += 1
improver.rollback_to_best()
print(f"{delta:.4f}")
else:
iteration["action"] = "MARGINAL"
consecutive_rollbacks = 0
print(f" 〰️ {delta:+.4f}")
results["iterations"].append(iteration)
if post > results["peak"]:
results["peak"] = post
if consecutive_rollbacks >= 3:
results["stopped_early"] = True
results["reason"] = "3 consecutive rollbacks"
print("\n 🛑 STOP: 3 consecutive rollbacks")
break
results["final"] = Store.state.get("best_quality_score", 0)
print("\n" + "="*60)
print(" 📊 RSI-15 RESULTS")
print(f" Iterations: {len(results['iterations'])}/15")
print(f" Improvements: {results['improvements']} | Rollbacks: {results['rollbacks']}")
print(f" Quality: {results['initial']:.4f}{results['final']:.4f} (peak: {results['peak']:.4f})")
print("="*60)
path = os.path.join(LOGS_DIR, f"rsi15_{datetime.now().strftime('%Y%m%d_%H%M%S')}.json")
with open(path, 'w') as f:
json.dump(results, f, indent=2)
return results
# ==============================================================================
# VISUALIZATION & PLOTTING
# ==============================================================================
def plot_quality_history() -> str:
if not PLOT_OK:
return "[plot] matplotlib not available"
qh = Store.state.get("quality_history", [])
if not qh:
return "[plot] No history"
fig, axes = plt.subplots(2, 2, figsize=(12, 10))
fig.suptitle("ARC Quality History", fontsize=14)
iters = range(len(qh))
axes[0,0].plot(iters, [e.get("quality", 0) for e in qh], 'b-o')
axes[0,0].set_title("Quality"); axes[0,0].grid(True, alpha=0.3)
axes[0,1].plot(iters, [e.get("density", 0) for e in qh], 'g-o')
axes[0,1].set_title("Density"); axes[0,1].grid(True, alpha=0.3)
axes[1,0].plot(iters, [e.get("coherence", 0) for e in qh], 'r-o')
axes[1,0].set_title("Coherence"); axes[1,0].grid(True, alpha=0.3)
axes[1,1].plot(iters, [e.get("tokens", 100) for e in qh], 'm-o')
axes[1,1].set_title("Tokens"); axes[1,1].grid(True, alpha=0.3)
plt.tight_layout()
path = os.path.join(ROOT, f"quality_{datetime.now().strftime('%Y%m%d_%H%M%S')}.png")
plt.savefig(path, dpi=150)
plt.close()
return f"[plot] ✓ Saved: {path}"
# ==============================================================================
# EXPORT/IMPORT CHECKPOINTS
# ==============================================================================
def export_checkpoint(name: str = None) -> str:
if name is None:
name = f"arc_{datetime.now().strftime('%Y%m%d_%H%M%S')}"
path = os.path.join(EXPORTS_DIR, f"{name}.zip")
with zipfile.ZipFile(path, 'w', zipfile.ZIP_DEFLATED) as zf:
zf.writestr("state.json", json.dumps(Store.state, indent=2, default=str))
zf.writestr("goals.json", json.dumps(Store.goals, indent=2))
zf.writestr("history.json", json.dumps(Store.state.get("quality_history", []), indent=2))
zf.writestr("meta.json", json.dumps({
"exported": datetime.now().isoformat(),
"quality": Store.state.get("best_quality_score", 0),
"version": "2.1"
}, indent=2))
size = os.path.getsize(path) / (1024*1024)
return f"[export] ✓ {path} ({size:.1f} MB)"
def import_checkpoint(path: str) -> str:
if not os.path.exists(path):
return f"[import] Not found: {path}"
dest = os.path.join(ROOT, "imports", os.path.basename(path).replace('.zip', ''))
os.makedirs(dest, exist_ok=True)
with zipfile.ZipFile(path, 'r') as zf:
zf.extractall(dest)
state_path = os.path.join(dest, "state.json")
if os.path.exists(state_path):
with open(state_path) as f:
Store.state.update(json.load(f))
return f"[import] ✓ {dest}"
# ==============================================================================
# BENCHMARK SUITE
# ==============================================================================
BENCHMARK_PROMPTS = [
"hello", "What is recursion?", "Explain gradient descent",
"Write a haiku about AI", "What are you?", "What is Big O notation?",
"If A > B and B > C, is A > C?", "What is the capital of Japan?",
]
def run_benchmark(gen_fn) -> dict:
print("\n" + "="*60 + "\n 📊 BENCHMARK\n" + "="*60)
results = []
for p in BENCHMARK_PROMPTS:
out, _, ev = gen_fn(p)
results.append({"prompt": p, "quality": ev.overall_score, "density": ev.density_score, "tokens": ev.tokens})
print(f" {'' if ev.overall_score >= 0.7 else ''} {p[:30]:<30} Q:{ev.overall_score:.2f} D:{ev.density_score:.1f}")
avg_q = sum(r["quality"] for r in results) / len(results)
print(f"\n Average Quality: {avg_q:.3f}\n" + "="*60)
return {"prompts": results, "avg_quality": avg_q}
# ==============================================================================
# LEARN FROM CONVERSATION
# ==============================================================================
def learn_from_conversation(min_q: float = 0.85) -> str:
good = []
if os.path.exists(Store.mem_path):
with open(Store.mem_path) as f:
for line in f:
try:
m = json.loads(line)
if m.get("kind") == "reply" and m.get("data", {}).get("quality", 0) >= min_q:
good.append({"prompt": m["data"]["in"], "response": m["data"]["out"]})
except:
pass
if not good:
return f"[learn] No responses with quality >= {min_q}"
path = os.path.join(ROOT, "learned.json")
with open(path, 'w') as f:
json.dump(good, f, indent=2)
return f"[learn] ✓ {len(good)} examples saved to {path}"
# ==============================================================================
# API SERVER
# ==============================================================================
class APIHandler(BaseHTTPRequestHandler):
gen_fn = None
def do_POST(self):
data = json.loads(self.rfile.read(int(self.headers['Content-Length'])))
if self.path == "/generate":
out, _, ev = self.gen_fn(data.get("prompt", ""))
resp = {"response": out, "quality": ev.overall_score, "tokens": ev.tokens}
self.send_response(200)
self.send_header('Content-type', 'application/json')
self.end_headers()
self.wfile.write(json.dumps(resp).encode())
else:
self.send_error(404)
def log_message(self, *args):
pass
def start_api_server(port: int, gen_fn):
APIHandler.gen_fn = gen_fn
server = HTTPServer(('0.0.0.0', port), APIHandler)
Thread(target=server.serve_forever, daemon=True).start()
print(f"[api] Running on http://0.0.0.0:{port}")
return server
# ==============================================================================
# LIVE STREAMING WINDOW
# ==============================================================================
class StreamingWindow:
"""Live window showing token-by-token generation."""
def __init__(self):
self.window = None
self.text_widget = None
self.queue = queue.Queue()
self.running = False
self.thread = None
self.token_count = 0
def start(self):
"""Start the streaming window."""
if not TK_OK:
print("[stream] tkinter not available")
return False
if self.running:
return True
self.running = True
self.thread = threading.Thread(target=self._run_window, daemon=True)
self.thread.start()
time.sleep(0.3)
return True
def _run_window(self):
self.window = tk.Tk()
self.window.title("🧠 ARC Live Generation")
self.window.geometry("900x600")
self.window.configure(bg='#1a1a2e')
# Header
header = tk.Frame(self.window, bg='#16213e', height=45)
header.pack(fill=tk.X)
header.pack_propagate(False)
tk.Label(header, text="⚡ Live Token Stream", font=('Consolas', 14, 'bold'),
bg='#16213e', fg='#00ff88').pack(side=tk.LEFT, padx=15, pady=8)
self.status_label = tk.Label(header, text="● Idle", font=('Consolas', 10),
bg='#16213e', fg='#666')
self.status_label.pack(side=tk.RIGHT, padx=15, pady=8)
# Text area
self.text_widget = scrolledtext.ScrolledText(
self.window, wrap=tk.WORD, font=('Consolas', 11),
bg='#1a1a2e', fg='#eee', insertbackground='#00ff88',
selectbackground='#3d3d5c', padx=15, pady=15
)
self.text_widget.pack(fill=tk.BOTH, expand=True, padx=8, pady=8)
# Bottom bar
bottom = tk.Frame(self.window, bg='#16213e', height=35)
bottom.pack(fill=tk.X)
tk.Button(bottom, text="Clear", command=self.clear, bg='#3d3d5c', fg='white',
relief=tk.FLAT, padx=10).pack(side=tk.LEFT, padx=8, pady=5)
self.count_label = tk.Label(bottom, text="Tokens: 0", font=('Consolas', 9),
bg='#16213e', fg='#666')
self.count_label.pack(side=tk.RIGHT, padx=15, pady=5)
self._process_queue()
self.window.protocol("WM_DELETE_WINDOW", self._on_close)
self.window.mainloop()
def _process_queue(self):
try:
while True:
msg = self.queue.get_nowait()
if msg == "__CLEAR__":
self.text_widget.delete(1.0, tk.END)
self.token_count = 0
elif msg == "__START__":
self.status_label.config(text="● Generating...", fg='#00ff88')
elif msg == "__END__":
self.status_label.config(text="● Done", fg='#666')
elif msg.startswith("__PROMPT__"):
self.text_widget.insert(tk.END, f"\n>>> {msg[10:]}\n\n")
else:
self.text_widget.insert(tk.END, msg)
self.text_widget.see(tk.END)
self.token_count += 1
self.count_label.config(text=f"Tokens: {self.token_count}")
except queue.Empty:
pass
if self.running and self.window:
self.window.after(10, self._process_queue)
def _on_close(self):
self.running = False
if self.window:
self.window.destroy()
self.window = None
def write(self, text: str):
if self.running:
self.queue.put(text)
def clear(self):
self.queue.put("__CLEAR__")
def start_generation(self, prompt: str = ""):
self.queue.put("__START__")
if prompt:
self.queue.put(f"__PROMPT__{prompt}")
def end_generation(self):
self.queue.put("__END__")
def is_running(self):
return self.running and self.window is not None
_stream_window = None
def get_stream_window() -> StreamingWindow:
global _stream_window
if _stream_window is None:
_stream_window = StreamingWindow()
return _stream_window
class DualStreamer:
"""Streams to console AND window."""
def __init__(self, tokenizer, window: StreamingWindow = None):
self.tokenizer = tokenizer
self.window = window
def put(self, token_ids):
if token_ids.shape[0] > 1:
return
token = self.tokenizer.decode(token_ids[0], skip_special_tokens=True)
print(token, end='', flush=True)
if self.window and self.window.is_running():
self.window.write(token)
def end(self):
print()
if self.window and self.window.is_running():
self.window.end_generation()
# ==============================================================================
# IMAGE VIEWER & GENERATOR
# ==============================================================================
class ImageSystem:
"""View and generate images."""
def __init__(self):
self.sdxl_pipe = None
self.current_image = None
self.device = "cuda" if torch.cuda.is_available() else "cpu"
def load_sdxl(self, model_id: str = "stabilityai/stable-diffusion-xl-base-1.0"):
if not DIFFUSERS_OK:
print("[image-gen] diffusers not installed")
return False
try:
print(f"[image-gen] Loading SDXL...")
self.sdxl_pipe = AutoPipelineForText2Image.from_pretrained(
model_id, torch_dtype=torch.float16 if self.device == "cuda" else torch.float32,
).to(self.device)
print("[image-gen] ✓ SDXL loaded")
return True
except Exception as e:
print(f"[image-gen] ✗ {e}")
return False
def generate(self, prompt: str, steps: int = 30, guidance: float = 7.5) -> 'Image':
if self.sdxl_pipe is None and not self.load_sdxl():
return None
print(f"[image-gen] Generating: {prompt[:50]}...")
try:
result = self.sdxl_pipe(prompt=prompt, num_inference_steps=steps,
guidance_scale=guidance, width=1024, height=1024)
self.current_image = result.images[0]
os.makedirs(os.path.join(ROOT, "images"), exist_ok=True)
path = os.path.join(ROOT, "images", f"gen_{datetime.now().strftime('%H%M%S')}.png")
self.current_image.save(path)
print(f"[image-gen] ✓ Saved: {path}")
return self.current_image
except Exception as e:
print(f"[image-gen] ✗ {e}")
return None
def generate_dalle(self, prompt: str) -> 'Image':
if not OPENAI_OK:
print("[image-gen] openai not installed")
return None
api_key = os.environ.get("OPENAI_API_KEY")
if not api_key:
print("[image-gen] OPENAI_API_KEY not set")
return None
try:
client = openai.OpenAI(api_key=api_key)
resp = client.images.generate(model="dall-e-3", prompt=prompt, size="1024x1024", n=1)
if REQUESTS_OK and PIL_OK:
img_data = requests.get(resp.data[0].url).content
self.current_image = Image.open(BytesIO(img_data))
os.makedirs(os.path.join(ROOT, "images"), exist_ok=True)
path = os.path.join(ROOT, "images", f"dalle_{datetime.now().strftime('%H%M%S')}.png")
self.current_image.save(path)
print(f"[image-gen] ✓ Saved: {path}")
return self.current_image
print(f"[image-gen] URL: {resp.data[0].url}")
return None
except Exception as e:
print(f"[image-gen] ✗ {e}")
return None
def view(self, path: str = None):
if not TK_OK or not PIL_OK:
print("[image] tkinter/PIL required")
return
img = Image.open(path) if path else self.current_image
if not img:
print("[image] No image")
return
def show():
win = tk.Tk()
win.title("🖼️ ARC Image Viewer")
disp = img.copy()
disp.thumbnail((800, 800), Image.Resampling.LANCZOS)
photo = ImageTk.PhotoImage(disp)
lbl = tk.Label(win, image=photo)
lbl.image = photo
lbl.pack()
tk.Label(win, text=f"{img.width}x{img.height}").pack()
win.mainloop()
threading.Thread(target=show, daemon=True).start()
_image_system = None
def get_image_system() -> ImageSystem:
global _image_system
if _image_system is None:
_image_system = ImageSystem()
return _image_system
# ==============================================================================
# AUDIO TTS SYSTEM
# ==============================================================================
class AudioSystem:
"""Text-to-speech output."""
def __init__(self):
self.engine = None
self.enabled = False
self.rate = 175
if VOICE_OK:
try:
self.engine = pyttsx3.init()
self.engine.setProperty('rate', self.rate)
print("[audio] ✓ pyttsx3 ready")
except:
pass
def speak(self, text: str, block: bool = False):
if not self.enabled or not text.strip():
return
text = text.replace('```', '').replace('**', '').replace('##', '')
if self.engine:
def do_speak():
self.engine.say(text)
self.engine.runAndWait()
if block:
do_speak()
else:
threading.Thread(target=do_speak, daemon=True).start()
elif GTTS_OK and PYGAME_OK:
def do_gtts():
tts = gTTS(text=text, lang='en')
fp = BytesIO()
tts.write_to_fp(fp)
fp.seek(0)
pygame.mixer.music.load(fp, 'mp3')
pygame.mixer.music.play()
while pygame.mixer.music.get_busy():
time.sleep(0.1)
threading.Thread(target=do_gtts, daemon=True).start()
def toggle(self) -> str:
self.enabled = not self.enabled
return f"[audio] TTS {'ON' if self.enabled else 'OFF'}"
def set_rate(self, rate: int):
self.rate = rate
if self.engine:
self.engine.setProperty('rate', rate)
def list_voices(self):
if self.engine:
for i, v in enumerate(self.engine.getProperty('voices')):
print(f" [{i}] {v.name}")
def set_voice(self, idx: int):
if self.engine:
voices = self.engine.getProperty('voices')
if 0 <= idx < len(voices):
self.engine.setProperty('voice', voices[idx].id)
_audio_system = None
def get_audio_system() -> AudioSystem:
global _audio_system
if _audio_system is None:
_audio_system = AudioSystem()
return _audio_system
# === GMAIL API FUNCTIONS ===
GMAIL_SCOPES = ['https://www.googleapis.com/auth/gmail.modify']
def gmail_api_authenticate() -> bool:
"""Authenticate with Gmail API."""
global _gmail_service
if not GMAIL_API_OK:
print("[gmail-api] ❌ Not installed")
print("[gmail-api] Run: pip install google-auth google-auth-oauthlib google-api-python-client")
return False
creds = None
token_file = os.path.join(ROOT, LOGIN_CONFIG["gmail_token_file"])
creds_file = os.path.join(ROOT, LOGIN_CONFIG["gmail_credentials_file"])
if os.path.exists(token_file):
with open(token_file, 'rb') as token:
creds = pickle.load(token)
if not creds or not creds.valid:
if creds and creds.expired and creds.refresh_token:
creds.refresh(Request())
else:
if not os.path.exists(creds_file):
print(f"[gmail-api] ❌ Missing {creds_file}")
print("[gmail-api] Get it from: https://console.cloud.google.com/apis/credentials")
print("[gmail-api] Create OAuth 2.0 Client ID (Desktop app), download JSON")
return False
flow = InstalledAppFlow.from_client_secrets_file(creds_file, GMAIL_SCOPES)
creds = flow.run_local_server(port=0)
with open(token_file, 'wb') as token:
pickle.dump(creds, token)
_gmail_service = build('gmail', 'v1', credentials=creds)
print("[gmail-api] ✅ Authenticated")
return True
def gmail_api_search(query: str, max_results: int = 10) -> str:
"""Search emails via API - FAST and RELIABLE."""
global _gmail_service
if not _gmail_service and not gmail_api_authenticate():
return "[gmail-api] Not authenticated"
try:
results = _gmail_service.users().messages().list(
userId='me', q=query, maxResults=max_results
).execute()
messages = results.get('messages', [])
if not messages:
return f"[gmail-api] No emails found for: {query}"
output = []
for msg in messages[:max_results]:
msg_data = _gmail_service.users().messages().get(
userId='me', id=msg['id'], format='metadata',
metadataHeaders=['From', 'Subject', 'Date']
).execute()
headers = {h['name']: h['value'] for h in msg_data['payload']['headers']}
output.append(f"📧 From: {headers.get('From', 'Unknown')}")
output.append(f" Subject: {headers.get('Subject', 'No subject')}")
output.append(f" Date: {headers.get('Date', 'Unknown')}")
output.append(f" ID: {msg['id']}")
output.append("")
return f"[gmail-api] ✅ Found {len(messages)} emails:\n\n" + "\n".join(output)
except Exception as e:
return f"[gmail-api] ❌ Error: {e}"
def gmail_api_read(message_id: str) -> str:
"""Read specific email via API."""
global _gmail_service
if not _gmail_service and not gmail_api_authenticate():
return "[gmail-api] Not authenticated"
try:
msg = _gmail_service.users().messages().get(
userId='me', id=message_id, format='full'
).execute()
headers = {h['name']: h['value'] for h in msg['payload']['headers']}
body = ""
import base64
if 'parts' in msg['payload']:
for part in msg['payload']['parts']:
if part['mimeType'] == 'text/plain' and 'data' in part.get('body', {}):
body = base64.urlsafe_b64decode(part['body']['data']).decode('utf-8')
break
elif 'body' in msg['payload'] and 'data' in msg['payload']['body']:
body = base64.urlsafe_b64decode(msg['payload']['body']['data']).decode('utf-8')
return f"""📧 EMAIL CONTENT
{'='*50}
From: {headers.get('From', 'Unknown')}
Subject: {headers.get('Subject', 'No subject')}
Date: {headers.get('Date', 'Unknown')}
{'='*50}
{body[:3000]}"""
except Exception as e:
return f"[gmail-api] ❌ Error: {e}"
def gmail_api_send(to: str, subject: str, body: str) -> str:
"""Send email via API - INSTANT and RELIABLE."""
global _gmail_service
if not _gmail_service and not gmail_api_authenticate():
return "[gmail-api] Not authenticated"
try:
import base64
from email.mime.text import MIMEText
message = MIMEText(body)
message['to'] = to
message['subject'] = subject
raw = base64.urlsafe_b64encode(message.as_bytes()).decode('utf-8')
_gmail_service.users().messages().send(userId='me', body={'raw': raw}).execute()
return f"[gmail-api] ✅ Email sent to {to}"
except Exception as e:
return f"[gmail-api] ❌ Error: {e}"
# === PEFT IMPORT ===
PEFT_OK = False
try:
from peft import PeftModel, get_peft_model, LoraConfig
PEFT_OK = True
except ImportError:
print("[warning] PEFT not installed")
# === WEB REQUESTS ===
REQUESTS_OK = False
try:
import requests
REQUESTS_OK = True
except ImportError:
print("[warning] requests not installed - web search disabled")
# ==============================================================================
# TRAINING EXAMPLES - 50+ DENSE EXAMPLES
# ==============================================================================
DENSE_TRAINING_EXAMPLES = [
# === GREETINGS (teach concise greetings) ===
{"prompt": "hello", "response": "Hello. How can I help?"},
{"prompt": "hi", "response": "Hi. What do you need?"},
{"prompt": "hey", "response": "Hey. What's up?"},
{"prompt": "how are you?", "response": "Functional and ready. What can I do for you?"},
{"prompt": "good morning", "response": "Morning. What's the task?"},
{"prompt": "what's up?", "response": "Ready to work. What do you need?"},
# === COMPUTER SCIENCE ===
{"prompt": "What is recursion?",
"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."},
{"prompt": "Explain neural networks",
"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."},
{"prompt": "How does gradient descent work?",
"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."},
{"prompt": "What is backpropagation?",
"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."},
{"prompt": "Explain attention mechanism",
"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."},
{"prompt": "What is a transformer?",
"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."},
{"prompt": "Explain hash tables",
"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."},
{"prompt": "What is Big O notation?",
"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."},
{"prompt": "Explain dynamic programming",
"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."},
{"prompt": "What is a database index?",
"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."},
# === MACHINE LEARNING ===
{"prompt": "What is overfitting?",
"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."},
{"prompt": "Explain regularization",
"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."},
{"prompt": "What is cross-validation?",
"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."},
{"prompt": "Explain the bias-variance tradeoff",
"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."},
{"prompt": "What is reinforcement learning?",
"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."},
{"prompt": "Explain CNNs",
"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."},
{"prompt": "What is batch normalization?",
"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."},
{"prompt": "Explain transfer learning",
"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."},
# === PHYSICS/MATH ===
{"prompt": "Explain entropy",
"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."},
{"prompt": "What is quantum mechanics?",
"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."},
{"prompt": "Explain relativity",
"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."},
{"prompt": "What is calculus?",
"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."},
{"prompt": "Explain linear algebra",
"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."},
{"prompt": "What is probability?",
"response": "Quantifying uncertainty. Frequentist: long-run frequency. Bayesian: degree of belief. Rules: P(AB) = P(A) + P(B) - P(A∩B). Bayes theorem: P(A|B) = P(B|A)P(A)/P(B). Random variables, distributions, expectations."},
# === PHILOSOPHY/ABSTRACT ===
{"prompt": "What is consciousness?",
"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."},
{"prompt": "Explain free will",
"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."},
{"prompt": "What is epistemology?",
"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."},
{"prompt": "Explain ethics",
"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."},
{"prompt": "What is the meaning of life?",
"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."},
# === PRACTICAL/TECHNICAL ===
{"prompt": "How does the internet work?",
"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."},
{"prompt": "Explain encryption",
"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."},
{"prompt": "What is an API?",
"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."},
{"prompt": "Explain Docker",
"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."},
{"prompt": "What is Git?",
"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."},
{"prompt": "Explain SQL vs NoSQL",
"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."},
{"prompt": "What is cloud computing?",
"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."},
{"prompt": "Explain microservices",
"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."},
# === BIOLOGY/SCIENCE ===
{"prompt": "Explain evolution",
"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."},
{"prompt": "What is DNA?",
"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."},
{"prompt": "Explain the immune system",
"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."},
{"prompt": "What is climate change?",
"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."},
# === ECONOMICS/SOCIETY ===
{"prompt": "Explain supply and demand",
"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."},
{"prompt": "What is inflation?",
"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."},
{"prompt": "Explain capitalism",
"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."},
# === META/SELF-REFERENCE ===
{"prompt": "How do language models work?",
"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."},
{"prompt": "What are your limitations?",
"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."},
{"prompt": "Can you improve yourself?",
"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."},
]
# Create preference pairs (dense vs verbose)
PREFERENCE_PAIRS = []
for ex in DENSE_TRAINING_EXAMPLES[:20]: # Use subset for DPO
verbose_prefix = random.choice([
"That's a great question! Let me explain. ",
"I'd be happy to help with that! ",
"What a fascinating topic! Let me break it down for you. ",
"Great question! This is something many people wonder about. ",
"I appreciate you asking! Let me give you a comprehensive answer. ",
])
PREFERENCE_PAIRS.append({
"prompt": ex["prompt"],
"chosen": ex["response"],
"rejected": verbose_prefix + ex["response"] + " Does that make sense? Let me know if you have any other questions!"
})
# ==============================================================================
# CF-HoT MULTI-HEAD PREDICTOR
# ==============================================================================
class MultiHeadPredictor(nn.Module):
"""Multi-head cognitive control predictor."""
def __init__(self, d_model: int, n_layers: int, d_fiber: int = 16, d_control: int = 64):
super().__init__()
self.d_model = d_model
self.n_layers = n_layers
self.d_fiber = d_fiber
self.fiber_projs = nn.ModuleList([
nn.Linear(d_model, d_fiber, bias=False) for _ in range(n_layers)
])
self.layer_weights = nn.Parameter(torch.ones(n_layers) / n_layers)
self.heads = nn.ModuleDict({
'repetition': self._make_head(d_fiber, d_control),
'hedging': self._make_head(d_fiber, d_control),
'verbosity': self._make_head(d_fiber, d_control),
})
self.loaded_heads = set()
def _make_head(self, d_fiber, d_control):
return nn.Sequential(
nn.Linear(d_fiber, d_control), nn.GELU(),
nn.Linear(d_control, d_control), nn.GELU(),
nn.Linear(d_control, 1)
)
def get_all_risks(self, hidden_states: List[torch.Tensor]) -> Dict[str, torch.Tensor]:
fibers = [proj(h.float()) for proj, h in zip(self.fiber_projs, hidden_states)]
weights = F.softmax(self.layer_weights[:len(fibers)], dim=0)
aggregated = sum(w * f for w, f in zip(weights, fibers))
risks = {}
for head_name in self.loaded_heads:
logits = self.heads[head_name](aggregated).squeeze(-1)
risks[head_name] = torch.sigmoid(logits)
return risks
def load_head(self, head_name: str, checkpoint_path: str):
if not os.path.exists(checkpoint_path):
print(f"[cf-hot] WARNING: Checkpoint not found: {checkpoint_path}")
return False
ckpt = torch.load(checkpoint_path, weights_only=False, map_location='cpu')
self.heads[head_name].load_state_dict(ckpt['head_state'])
self.loaded_heads.add(head_name)
sep = ckpt.get('result', {}).get('separation', 0)
print(f"[cf-hot] Loaded {head_name} head (separation: {sep:.1f}x)")
return True
# ==============================================================================
# EVALUATION METRICS - COMPREHENSIVE
# ==============================================================================
@dataclass
class EvaluationResult:
"""Comprehensive evaluation of a response."""
prompt: str
response: str
# Token metrics
tokens: int = 0
words: int = 0
# Density metrics
unique_content_words: int = 0
density_score: float = 0.0
# Quality metrics
coherence_score: float = 0.0 # Model self-evaluation
helpfulness_score: float = 0.0 # Does it answer the question?
# Penalty metrics
filler_count: int = 0
repetition_count: int = 0
gibberish_score: float = 0.0 # Detects math soup, random text
# Composite
overall_score: float = 0.0
passes: bool = False
def to_dict(self):
return asdict(self)
class ComprehensiveEvaluator:
"""Evaluates responses on multiple dimensions to prevent reward hacking."""
def __init__(self, tokenizer, model=None):
self.tokenizer = tokenizer
self.model = model
# Filler phrases to penalize
self.filler_phrases = [
"that's a great question", "that's an interesting question",
"great question", "good question", "interesting question",
"let me explain", "i'd be happy to", "i would be happy to",
"as you may know", "as you might know", "it's important to note",
"to put it simply", "in other words", "basically", "essentially",
"first of all", "to begin with", "allow me to", "i should mention",
"before i answer", "to answer your question", "simply put",
"in essence", "to be clear", "to clarify", "in summary",
"thank you for asking", "thanks for asking", "i appreciate",
"what a great", "what a fascinating", "what an interesting",
]
# Patterns indicating gibberish/reward hacking
self.gibberish_patterns = [
r'[→←↑↓]{3,}', # Lots of arrows
r'[∇∂∫∑∏]{3,}', # Lots of math symbols in a row
r'(.)\1{4,}', # Same character 5+ times
r'(\b\w+\b)\s+\1\s+\1', # Same word 3+ times in a row
r'^[A-Z\s.!?]{20,}$', # All caps for long stretch
r'sys\.|init\(\)|compute\(\)', # Terminal-speak
]
def evaluate(self, prompt: str, response: str) -> EvaluationResult:
"""Run all evaluations on a response."""
result = EvaluationResult(prompt=prompt, response=response)
# Basic metrics
result.tokens = len(self.tokenizer.encode(response))
result.words = len(response.split())
# Density (improved formula)
result.density_score, result.unique_content_words = self._compute_density(response)
# Filler detection
result.filler_count = self._count_fillers(response)
# Repetition detection
result.repetition_count = self._count_repetitions(response)
# Gibberish detection
result.gibberish_score = self._detect_gibberish(response)
# Quality assessment (if model available)
if self.model is not None:
result.coherence_score = self._assess_coherence(prompt, response)
result.helpfulness_score = self._assess_helpfulness(prompt, response)
else:
# Heuristic fallback
result.coherence_score = self._heuristic_coherence(response)
result.helpfulness_score = self._heuristic_helpfulness(prompt, response)
# Compute overall score
result.overall_score = self._compute_overall(result)
result.passes = result.overall_score >= 0.6
return result
def _compute_density(self, response: str) -> Tuple[float, int]:
"""Improved density that accounts for response length."""
words = response.split()
tokens = len(self.tokenizer.encode(response))
# Content words (4+ chars, alphabetic)
content_words = [w.lower() for w in words if len(w) >= 4 and w.isalpha()]
unique_content = set(content_words)
if tokens == 0:
return 0.0, 0
# Base density
raw_density = len(unique_content) / tokens * 100
# Length adjustment: don't penalize very short but appropriate responses
# and don't reward extremely short gibberish
if tokens < 5:
# Very short - check if it's appropriate
if len(unique_content) == 0:
raw_density = 0
else:
raw_density = min(raw_density, 30) # Cap short response density
elif tokens < 15:
# Short but potentially good
raw_density = min(raw_density, 40)
return raw_density, len(unique_content)
def _count_fillers(self, response: str) -> int:
"""Count filler phrases."""
response_lower = response.lower()
count = 0
for filler in self.filler_phrases:
if filler in response_lower:
count += 1
return count
def _count_repetitions(self, response: str) -> int:
"""Count repeated phrases/words."""
words = response.lower().split()
if len(words) < 3:
return 0
# Check for repeated bigrams
bigrams = [' '.join(words[i:i+2]) for i in range(len(words)-1)]
bigram_counts = {}
for bg in bigrams:
bigram_counts[bg] = bigram_counts.get(bg, 0) + 1
repetitions = sum(1 for c in bigram_counts.values() if c > 2)
return repetitions
def _detect_gibberish(self, response: str) -> float:
"""Detect gibberish/reward hacking patterns. Higher = more gibberish."""
score = 0.0
for pattern in self.gibberish_patterns:
if re.search(pattern, response):
score += 0.2
# Check character diversity
if len(response) > 10:
unique_chars = len(set(response.lower()))
char_ratio = unique_chars / len(response)
if char_ratio < 0.1: # Very low diversity
score += 0.3
# Check for excessive punctuation/symbols
symbol_count = sum(1 for c in response if c in '→←↑↓∇∂∫∑∏αβγδεζηθ')
if len(response) > 0 and symbol_count / len(response) > 0.2:
score += 0.3
return min(score, 1.0)
def _heuristic_coherence(self, response: str) -> float:
"""Heuristic coherence without model."""
# Check basic structure
score = 0.5
# Has sentences?
if '.' in response or '!' in response or '?' in response:
score += 0.1
# Not all caps?
if response != response.upper():
score += 0.1
# Has words of varying length?
words = response.split()
if words:
lengths = [len(w) for w in words]
if len(set(lengths)) > 2:
score += 0.1
# Reasonable length?
if 10 <= len(response) <= 500:
score += 0.2
return min(score, 1.0)
def _heuristic_helpfulness(self, prompt: str, response: str) -> float:
"""Heuristic helpfulness without model."""
score = 0.5
# Check if response addresses prompt keywords
prompt_words = set(w.lower() for w in prompt.split() if len(w) > 3)
response_words = set(w.lower() for w in response.split() if len(w) > 3)
overlap = len(prompt_words & response_words)
if overlap > 0:
score += min(0.3, overlap * 0.1)
# Not too short for a question
if '?' in prompt or prompt.lower().startswith(('what', 'how', 'why', 'explain')):
if len(response.split()) >= 10:
score += 0.2
return min(score, 1.0)
def _assess_coherence(self, prompt: str, response: str) -> float:
"""Use model to assess coherence."""
# TODO: Implement model self-evaluation
return self._heuristic_coherence(response)
def _assess_helpfulness(self, prompt: str, response: str) -> float:
"""Use model to assess helpfulness."""
# TODO: Implement model self-evaluation
return self._heuristic_helpfulness(prompt, response)
def _compute_overall(self, result: EvaluationResult) -> float:
"""Compute weighted overall score."""
# Weights
w_density = 0.25
w_coherence = 0.25
w_helpful = 0.25
w_penalties = 0.25
# Normalize density (0-50 range → 0-1)
density_normalized = min(result.density_score / 50, 1.0)
# Penalties
filler_penalty = min(result.filler_count * 0.15, 0.5)
repetition_penalty = min(result.repetition_count * 0.1, 0.3)
gibberish_penalty = result.gibberish_score * 0.5
penalty_score = 1.0 - filler_penalty - repetition_penalty - gibberish_penalty
penalty_score = max(penalty_score, 0)
overall = (
w_density * density_normalized +
w_coherence * result.coherence_score +
w_helpful * result.helpfulness_score +
w_penalties * penalty_score
)
return overall
# ==============================================================================
# CONFIG
# ==============================================================================
class Config:
system = """You are Übermenschetien (ARC Engine) - a precise, dense AI assistant.
You communicate with maximum information density: every word matters, no filler.
You do not say "That's a great question" or "I'd be happy to help."
You answer directly, concisely, and accurately.
When appropriate, you can execute code and improve yourself."""
temperature = 0.85
top_p = 0.9
repetition_penalty = 1.1
max_new_tokens = 512
normal_max_tokens = 512
use_voice = False
use_vector_memory = VECTOR_OK
use_lht_reasoning = LHT_OK
use_cfhot = True
use_cfhot_125x = True # Toggle 125× head
use_dense = True
use_agentic = True
autonomy = False
# Book Mode
book_mode = False
book_max_tokens = 16384
book_chunk_size = 2048
# Idea Mode
idea_mode = False
idea_depth = "extensive" # quick, normal, extensive, deep
idea_use_opus = True # Use Opus 4.5 for ideas
# API Server
api_enabled = False
api_port = 8080
# CF-HoT thresholds
cfhot_repetition_threshold = 0.6
cfhot_hedging_threshold = 0.5
cfhot_verbosity_threshold = 0.55
cfhot_repetition_penalty = 6.0
cfhot_hedging_penalty = 4.0
cfhot_verbosity_penalty = 3.0
# Self-improvement config (CONSERVATIVE)
min_quality_score = 0.5
target_quality_score = 0.75
training_steps_per_iteration = 25
max_improvement_iterations = 10
quality_drop_threshold = 0.1
min_training_examples = 30
# RSI-15
rsi_max_iterations = 15
@staticmethod
def toggle(name: str):
# Handle aliases
aliases = {
"125x": "use_cfhot_125x", "cfhot125x": "use_cfhot_125x",
"book": "book_mode", "idea": "idea_mode", "api": "api_enabled",
"opus": "idea_use_opus",
}
name = aliases.get(name.lower(), name)
if not hasattr(Config, name):
return f"[config] No flag: {name}"
val = getattr(Config, name)
if isinstance(val, bool):
setattr(Config, name, not val)
new_val = getattr(Config, name)
# Side effects
if name == "book_mode":
Config.max_new_tokens = Config.book_max_tokens if new_val else Config.normal_max_tokens
if name == "use_cfhot_125x":
if new_val:
get_cfhot_head().load()
else:
get_cfhot_head().unload()
return f"[config] {name}{new_val}"
return f"[config] {name} not boolean: {val}"
# ==============================================================================
# STATE & MEMORY
# ==============================================================================
class Store:
state_path = f"{RUN_DIR}/state_v2.json"
mem_path = f"{RUN_DIR}/memory_v2.jsonl"
goals_path = f"{RUN_DIR}/goals_v2.json"
improvement_log_path = f"{LOGS_DIR}/improvement_history.json"
state = {
"self": "I am Übermenschetien Agentic Engine v2 — stable self-improvement.",
"turn": 0,
"cfhot_interventions": {"repetition": 0, "hedging": 0, "verbosity": 0},
"improvement_iterations": 0,
"training_runs": [],
"current_checkpoint": DENSE_CHECKPOINT,
"best_checkpoint": DENSE_CHECKPOINT,
"best_quality_score": 0.0,
"quality_history": [],
"rollback_count": 0,
}
goals: List[str] = []
improvement_history: List[Dict] = []
@classmethod
def load(cls):
if os.path.exists(cls.state_path):
with open(cls.state_path) as f:
loaded = json.load(f)
cls.state.update(loaded)
if os.path.exists(cls.goals_path):
with open(cls.goals_path) as f:
cls.goals = json.load(f)
if os.path.exists(cls.improvement_log_path):
with open(cls.improvement_log_path) as f:
cls.improvement_history = json.load(f)
@classmethod
def save(cls):
with open(cls.state_path, "w") as f:
json.dump(cls.state, f, indent=2)
with open(cls.goals_path, "w") as f:
json.dump(cls.goals, f, indent=2)
with open(cls.improvement_log_path, "w") as f:
json.dump(cls.improvement_history, f, indent=2, default=str)
@classmethod
def log_mem(cls, kind: str, payload: Any):
rec = {"ts": datetime.now().isoformat(timespec="seconds"),
"kind": kind, "data": payload}
with open(cls.mem_path, "a") as f:
f.write(json.dumps(rec, ensure_ascii=False, default=str) + "\n")
if Config.use_vector_memory and VECTOR_OK:
text = f"{kind}: {json.dumps(payload, ensure_ascii=False, default=str)}"
vec = _embedder.encode([text])[0].tolist()
_collection.add(documents=[text], embeddings=[vec],
ids=[f"{kind}-{cls.state['turn']}-{random.randint(0,1_000_000)}"])
@classmethod
def record_improvement(cls, iteration_data: Dict):
"""Record an improvement iteration for analysis."""
cls.improvement_history.append({
"timestamp": datetime.now().isoformat(),
**iteration_data
})
cls.save()
# ==============================================================================
# AGENTIC TOOLS
# ==============================================================================
class AgentTools:
"""Full agentic capabilities - code execution, file operations, training."""
@staticmethod
def shell(cmd: str, timeout: int = 300) -> Dict[str, Any]:
"""Execute shell command."""
print(f"[SHELL] {cmd[:100]}...")
try:
result = subprocess.run(
cmd, shell=True, capture_output=True, text=True,
timeout=timeout, cwd=ROOT
)
output = result.stdout + result.stderr
success = result.returncode == 0
print(f"[SHELL] {'' if success else ''} (exit {result.returncode})")
return {"success": success, "output": output[:10000], "returncode": result.returncode}
except subprocess.TimeoutExpired:
return {"success": False, "output": "Command timed out", "returncode": -1}
except Exception as e:
return {"success": False, "output": str(e), "returncode": -1}
@staticmethod
def python_exec(code: str) -> Dict[str, Any]:
"""Execute Python code."""
print(f"[PYTHON] Executing {len(code)} chars...")
try:
tmp_file = os.path.join(ROOT, "_agentic_tmp.py")
with open(tmp_file, 'w') as f:
f.write(code)
result = subprocess.run(
[sys.executable, tmp_file],
capture_output=True, text=True, timeout=300, cwd=ROOT
)
if os.path.exists(tmp_file):
os.remove(tmp_file)
output = result.stdout + result.stderr
success = result.returncode == 0
print(f"[PYTHON] {'' if success else ''}")
return {"success": success, "output": output[:10000], "returncode": result.returncode}
except Exception as e:
return {"success": False, "output": str(e), "returncode": -1}
@staticmethod
def read_file(path: str) -> Dict[str, Any]:
try:
full_path = os.path.join(ROOT, path) if not path.startswith('/') else path
with open(full_path, 'r') as f:
content = f.read()
return {"success": True, "content": content[:50000]}
except Exception as e:
return {"success": False, "error": str(e)}
@staticmethod
def write_file(path: str, content: str) -> Dict[str, Any]:
try:
full_path = os.path.join(ROOT, path) if not path.startswith('/') else path
os.makedirs(os.path.dirname(full_path) if os.path.dirname(full_path) else '.', exist_ok=True)
with open(full_path, 'w') as f:
f.write(content)
return {"success": True, "path": full_path}
except Exception as e:
return {"success": False, "error": str(e)}
@staticmethod
def list_dir(path: str = ".") -> Dict[str, Any]:
try:
full_path = os.path.join(ROOT, path) if not path.startswith('/') else path
items = os.listdir(full_path)
return {"success": True, "items": items}
except Exception as e:
return {"success": False, "error": str(e)}
@staticmethod
def search_files(query: str, path: str = ".") -> Dict[str, Any]:
result = AgentTools.shell(f'grep -rn "{query}" {path} 2>/dev/null | head -50')
return result
@staticmethod
def web_search(query: str) -> Dict[str, Any]:
if not REQUESTS_OK:
return {"success": False, "error": "requests not installed"}
try:
url = f"https://html.duckduckgo.com/html/?q={query.replace(' ', '+')}"
headers = {'User-Agent': 'Mozilla/5.0'}
response = requests.get(url, headers=headers, timeout=10)
results = []
for match in re.finditer(r'class="result__snippet">(.*?)</a>', response.text, re.DOTALL):
snippet = re.sub(r'<[^>]+>', '', match.group(1)).strip()
if snippet:
results.append(snippet[:500])
if len(results) >= 5:
break
return {"success": True, "results": results}
except Exception as e:
return {"success": False, "error": str(e)}
# ==============================================================================
# MODEL LOADING
# ==============================================================================
_model = None
_tokenizer = None
_multi_head = None
_hedge_tokens = None
_verbose_tokens = None
_evaluator = None
def load_llm(checkpoint_path: str = None):
global _model, _tokenizer, _multi_head, _hedge_tokens, _verbose_tokens, _evaluator
from transformers import AutoTokenizer, AutoModelForCausalLM, BitsAndBytesConfig
checkpoint_path = checkpoint_path or Store.state.get("current_checkpoint", DENSE_CHECKPOINT)
print(f"[llm] Loading base model: {MODEL_PATH}")
_tokenizer = AutoTokenizer.from_pretrained(MODEL_PATH, use_fast=True, local_files_only=True)
if _tokenizer.pad_token_id is None:
_tokenizer.pad_token = _tokenizer.eos_token
bnb_config = BitsAndBytesConfig(
load_in_4bit=True,
bnb_4bit_quant_type="nf4",
bnb_4bit_compute_dtype=torch.bfloat16,
bnb_4bit_use_double_quant=True
)
base_model = AutoModelForCausalLM.from_pretrained(
MODEL_PATH,
quantization_config=bnb_config,
device_map="auto",
torch_dtype=torch.bfloat16,
local_files_only=True
)
# Load DENSE checkpoint
if PEFT_OK and Config.use_dense and os.path.exists(checkpoint_path):
print(f"[dense] Loading checkpoint: {checkpoint_path}")
_model = PeftModel.from_pretrained(base_model, checkpoint_path)
print(f"[dense] ✓ Adapter loaded")
elif PEFT_OK and os.path.exists(CFHOT_CHECKPOINT):
print(f"[cf-hot] Loading LoRA adapter from: {CFHOT_CHECKPOINT}")
_model = PeftModel.from_pretrained(base_model, CFHOT_CHECKPOINT)
else:
_model = base_model
print("[warning] No adapter loaded - using base model")
_model.eval()
# Initialize evaluator
_evaluator = ComprehensiveEvaluator(_tokenizer, _model)
# Initialize CF-HoT
if Config.use_cfhot:
_init_cfhot()
return _tokenizer, _model
def reload_model(checkpoint_path: str):
"""Hot-reload model with a new checkpoint."""
global _model, _tokenizer, _evaluator
print(f"\n[reload] Switching to checkpoint: {checkpoint_path}")
if _model is not None:
del _model
torch.cuda.empty_cache()
Store.state["current_checkpoint"] = checkpoint_path
Store.save()
return load_llm(checkpoint_path)
def _init_cfhot():
"""Initialize CF-HoT multi-head predictor."""
global _multi_head, _hedge_tokens, _verbose_tokens
n_layers = _model.config.num_hidden_layers
d_model = _model.config.hidden_size
device = next(_model.parameters()).device
print(f"[cf-hot] Initializing multi-head predictor ({n_layers} layers, {d_model} dims)")
_multi_head = MultiHeadPredictor(d_model, n_layers).to(device).float()
# Load CF-HoT checkpoint if available
cfhot_risk_path = os.path.join(CFHOT_CHECKPOINT, "risk_predictor.pt")
if os.path.exists(cfhot_risk_path):
try:
cfhot_ckpt = torch.load(cfhot_risk_path, weights_only=False, map_location=device)
cfhot_state = cfhot_ckpt['risk_predictor']
for i in range(n_layers):
key = f'fiber_projs.{i}.weight'
if key in cfhot_state:
_multi_head.fiber_projs[i].weight.data = cfhot_state[key].to(device).float()
if 'layer_weights' in cfhot_state:
_multi_head.layer_weights.data = cfhot_state['layer_weights'].to(device).float()
# Load repetition head
try:
_multi_head.heads['repetition'][0].weight.data = cfhot_state['predictor.0.weight'].to(device).float()
_multi_head.heads['repetition'][0].bias.data = cfhot_state['predictor.0.bias'].to(device).float()
_multi_head.heads['repetition'][2].weight.data = cfhot_state['predictor.2.weight'].to(device).float()
_multi_head.heads['repetition'][2].bias.data = cfhot_state['predictor.2.bias'].to(device).float()
_multi_head.heads['repetition'][4].weight.data = cfhot_state['predictor.4.weight'].to(device).float()
_multi_head.heads['repetition'][4].bias.data = cfhot_state['predictor.4.bias'].to(device).float()
_multi_head.loaded_heads.add('repetition')
print(f"[cf-hot] Loaded repetition head")
except KeyError as e:
print(f"[cf-hot] Warning: Could not load repetition head: {e}")
except Exception as e:
print(f"[cf-hot] Warning: Could not load CF-HoT: {e}")
else:
print(f"[cf-hot] Warning: CF-HoT risk predictor not found")
# Load additional heads
def find_best_checkpoint(head_dir):
if not os.path.exists(head_dir):
return None
ckpts = []
for d in os.listdir(head_dir):
if d.startswith("ckpt_"):
try:
step = int(d.split("_")[1])
ckpts.append((step, os.path.join(head_dir, d)))
except:
pass
if ckpts:
ckpts.sort(key=lambda x: x[0], reverse=True)
return ckpts[0]
return None
hedging_dir = os.path.join(MULTI_HEAD_DIR, "hedging_head")
best_hedge = find_best_checkpoint(hedging_dir)
if best_hedge:
step, ckpt_dir = best_hedge
_multi_head.load_head('hedging', os.path.join(ckpt_dir, "hedging_head.pt"))
verbosity_dir = os.path.join(MULTI_HEAD_DIR, "verbosity_head")
best_verb = find_best_checkpoint(verbosity_dir)
if best_verb:
step, ckpt_dir = best_verb
_multi_head.load_head('verbosity', os.path.join(ckpt_dir, "verbosity_head.pt"))
_multi_head.eval()
for param in _multi_head.parameters():
param.requires_grad = False
# Build suppression token sets
hedge_phrases = [
"As an AI", "As a language model", "I don't have feelings",
"I apologize", "That's a great question", "Great question",
"I'd be happy to", "Let me help you", "Thank you for asking",
]
_hedge_tokens = set()
for phrase in hedge_phrases:
tokens = _tokenizer.encode(phrase, add_special_tokens=False)
if tokens:
_hedge_tokens.add(tokens[0])
verbose_phrases = [
"Let me explain", "To put it simply", "In other words",
"Basically", "Essentially", "First of all", "To begin with",
]
_verbose_tokens = set()
for phrase in verbose_phrases:
tokens = _tokenizer.encode(phrase, add_special_tokens=False)
if tokens:
_verbose_tokens.add(tokens[0])
print(f"[cf-hot] ✓ Multi-head system ready")
print(f"[cf-hot] Loaded heads: {list(_multi_head.loaded_heads)}")
print(f"[cf-hot] Hedge tokens: {len(_hedge_tokens)}")
print(f"[cf-hot] Verbose tokens: {len(_verbose_tokens)}")
# ==============================================================================
# LHT REASONER
# ==============================================================================
class LHTReasoner:
def __init__(self, config=None):
if not LHT_OK:
raise ImportError("LHT modules not available")
self.config = config or LHTConfig(
vocab_size=32000, d_model=256, d_fiber=32,
n_heads=4, n_layers=4, lie_algebra_rank=4,
)
self.model = LieHolonomyTransformer(self.config)
self.waypoint_detector = WaypointDetector(self.config, n_waypoints=32)
weights_path = os.path.join(LHT_DIR, "lht_weights.pt")
if os.path.exists(weights_path):
self.model.load_state_dict(torch.load(weights_path, map_location="cpu"))
def check_consistency(self, reasoning_chain: List[str], tokenizer) -> Dict[str, float]:
combined = " [STEP] ".join(reasoning_chain)
tokens = tokenizer(combined, return_tensors="pt", truncation=True,
max_length=self.config.max_seq_len)
with torch.no_grad():
output = self.model(input_ids=tokens["input_ids"], return_geometric_losses=True)
holonomy = output.get("holonomy_loss", torch.tensor(0.0)).item()
curvature = output.get("curvature_loss", torch.tensor(0.0)).item()
consistency_score = 1.0 / (1.0 + holonomy)
return {
"holonomy": holonomy, "curvature": curvature,
"consistency_score": consistency_score,
"is_consistent": consistency_score > 0.5
}
_lht_reasoner = None
def get_lht_reasoner():
global _lht_reasoner
if _lht_reasoner is None and LHT_OK:
try:
_lht_reasoner = LHTReasoner()
except Exception as e:
print(f"[lht] Failed to initialize: {e}")
return _lht_reasoner
# ==============================================================================
# CF-HoT CONTROLLED GENERATION
# ==============================================================================
def generate_with_cfhot(prompt: str, **kwargs) -> Tuple[str, Dict]:
"""Generate text with CF-HoT cognitive control."""
global _model, _tokenizer, _multi_head, _hedge_tokens, _verbose_tokens
temperature = kwargs.get("temperature", Config.temperature)
top_p = kwargs.get("top_p", Config.top_p)
max_new_tokens = kwargs.get("max_new_tokens", Config.max_new_tokens)
device = next(_model.parameters()).device
input_ids = _tokenizer.encode(prompt, return_tensors='pt').to(device)
attention_mask = torch.ones_like(input_ids)
stats = {
'tokens_generated': 0,
'interventions': {'repetition': 0, 'hedging': 0, 'verbosity': 0},
}
generated_ids = input_ids.clone()
for step in range(max_new_tokens):
with torch.no_grad():
outputs = _model(
input_ids=generated_ids,
attention_mask=attention_mask,
output_hidden_states=True,
return_dict=True
)
logits = outputs.logits[:, -1, :] / temperature
# Get risks from all heads if CF-HoT is enabled
if _multi_head is not None and _multi_head.loaded_heads:
hidden_states = outputs.hidden_states[1:]
risks = _multi_head.get_all_risks(hidden_states)
current_risks = {name: r[:, -1].item() for name, r in risks.items()}
if ('repetition' in current_risks and
current_risks['repetition'] > Config.cfhot_repetition_threshold):
recent_tokens = generated_ids[0, -32:].tolist()
for tok_id in set(recent_tokens):
logits[0, tok_id] -= Config.cfhot_repetition_penalty
stats['interventions']['repetition'] += 1
Store.state['cfhot_interventions']['repetition'] += 1
# Always suppress hedge/verbose tokens
if _hedge_tokens:
for tok_id in _hedge_tokens:
logits[0, tok_id] -= Config.cfhot_hedging_penalty
if step < 5: # Count early interventions
stats['interventions']['hedging'] += 1
if _verbose_tokens:
for tok_id in _verbose_tokens:
logits[0, tok_id] -= Config.cfhot_verbosity_penalty
if step < 5:
stats['interventions']['verbosity'] += 1
# Top-p sampling
sorted_logits, sorted_indices = torch.sort(logits, descending=True)
cumulative_probs = torch.cumsum(F.softmax(sorted_logits, dim=-1), dim=-1)
sorted_indices_to_remove = cumulative_probs > top_p
sorted_indices_to_remove[..., 1:] = sorted_indices_to_remove[..., :-1].clone()
sorted_indices_to_remove[..., 0] = 0
indices_to_remove = sorted_indices_to_remove.scatter(1, sorted_indices, sorted_indices_to_remove)
logits[indices_to_remove] = float('-inf')
probs = F.softmax(logits, dim=-1)
next_token = torch.multinomial(probs, num_samples=1)
generated_ids = torch.cat([generated_ids, next_token], dim=-1)
attention_mask = torch.cat([attention_mask, torch.ones(1, 1, device=device)], dim=-1)
stats['tokens_generated'] += 1
if next_token.item() == _tokenizer.eos_token_id:
break
output_text = _tokenizer.decode(generated_ids[0], skip_special_tokens=False)
if "<|im_start|>assistant" in output_text:
output_text = output_text.split("<|im_start|>assistant")[-1]
if output_text.startswith("\n"):
output_text = output_text[1:]
for end_tok in ["<|im_end|>", "<|im_start|>"]:
if end_tok in output_text:
output_text = output_text.split(end_tok)[0]
return output_text.strip(), stats
def generate(user: str, **kwargs) -> Tuple[str, Dict, EvaluationResult]:
"""Main generation function with evaluation."""
temperature = kwargs.get("temperature", Config.temperature)
max_new_tokens = kwargs.get("max_new_tokens", Config.max_new_tokens)
prompt = (f"<|im_start|>system\n{Config.system}<|im_end|>\n"
f"<|im_start|>user\n{user}<|im_end|>\n"
f"<|im_start|>assistant\n")
text, stats = generate_with_cfhot(
prompt,
temperature=temperature,
max_new_tokens=max_new_tokens
)
# Evaluate the response
eval_result = _evaluator.evaluate(user, text)
return text, stats, eval_result
# ==============================================================================
# STABLE SELF-IMPROVEMENT SYSTEM
# ==============================================================================
class StableSelfImprover:
"""
Self-improvement system with safeguards against collapse:
1. Comprehensive evaluation (not just density)
2. Rollback on quality drop
3. Conservative training (small steps)
4. Diverse training examples
5. A/B testing between checkpoints
"""
def __init__(self):
self.test_prompts = self._select_test_prompts()
self.baseline_quality = 0.0
def _select_test_prompts(self) -> List[Dict]:
"""Select diverse test prompts."""
# Mix of short and long, different categories
return [
{"prompt": "hello", "category": "greeting"},
{"prompt": "hi there", "category": "greeting"},
{"prompt": "What is recursion?", "category": "cs"},
{"prompt": "Explain neural networks", "category": "ml"},
{"prompt": "How does gradient descent work?", "category": "ml"},
{"prompt": "What is consciousness?", "category": "philosophy"},
{"prompt": "Explain entropy", "category": "physics"},
{"prompt": "How does encryption work?", "category": "cs"},
{"prompt": "What are your limitations?", "category": "meta"},
{"prompt": "How do I learn programming?", "category": "practical"},
]
def evaluate_current_model(self) -> Dict[str, Any]:
"""Comprehensive evaluation of current model."""
print("\n[EVAL] Testing current model...")
results = []
total_quality = 0.0
category_scores = {}
for test in self.test_prompts:
prompt = test["prompt"]
category = test["category"]
# Generate response
response, stats, eval_result = generate(prompt, max_new_tokens=200)
results.append({
'prompt': prompt,
'response': response[:200],
'category': category,
'tokens': eval_result.tokens,
'density': eval_result.density_score,
'coherence': eval_result.coherence_score,
'helpfulness': eval_result.helpfulness_score,
'gibberish': eval_result.gibberish_score,
'fillers': eval_result.filler_count,
'overall': eval_result.overall_score,
'passes': eval_result.passes,
})
total_quality += eval_result.overall_score
if category not in category_scores:
category_scores[category] = []
category_scores[category].append(eval_result.overall_score)
status = "" if eval_result.passes else ""
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}")
avg_quality = total_quality / len(results)
pass_rate = sum(1 for r in results if r['passes']) / len(results)
# Category breakdown
cat_averages = {cat: sum(scores)/len(scores) for cat, scores in category_scores.items()}
evaluation = {
'avg_quality': avg_quality,
'pass_rate': pass_rate,
'category_scores': cat_averages,
'results': results,
'needs_improvement': avg_quality < Config.target_quality_score,
'is_degraded': avg_quality < Config.min_quality_score,
}
print(f"\n[EVAL] Avg Quality: {avg_quality:.2f} (target: {Config.target_quality_score})")
print(f"[EVAL] Pass Rate: {pass_rate:.1%}")
print(f"[EVAL] Category Scores: {cat_averages}")
print(f"[EVAL] Needs Improvement: {evaluation['needs_improvement']}")
if evaluation['is_degraded']:
print(f"[EVAL] ⚠️ WARNING: Quality below minimum threshold!")
return evaluation
def save_rollback_checkpoint(self):
"""Save current checkpoint as rollback point."""
current = Store.state.get("current_checkpoint", DENSE_CHECKPOINT)
rollback_path = os.path.join(ROLLBACK_DIR, f"rollback_{datetime.now().strftime('%Y%m%d_%H%M%S')}")
if os.path.exists(current):
shutil.copytree(current, rollback_path)
print(f"[ROLLBACK] Saved rollback checkpoint: {rollback_path}")
return rollback_path
return None
def rollback_to_best(self):
"""Rollback to best known checkpoint."""
best = Store.state.get("best_checkpoint", DENSE_CHECKPOINT)
print(f"\n[ROLLBACK] Rolling back to best checkpoint: {best}")
Store.state["rollback_count"] = Store.state.get("rollback_count", 0) + 1
reload_model(best)
return best
def run_training_iteration(self, steps: int = None) -> Dict[str, Any]:
"""Run one CONSERVATIVE iteration of training."""
steps = steps or Config.training_steps_per_iteration
print(f"\n[TRAIN] Starting {steps} steps of CONSERVATIVE training...")
print(f"[TRAIN] Using {len(DENSE_TRAINING_EXAMPLES)} training examples")
# Find current checkpoint step
checkpoints = sorted(Path(CHECKPOINTS_DIR).glob("step_*"),
key=lambda p: int(p.name.split('_')[1]) if p.name.split('_')[1].isdigit() else 0,
reverse=True)
if checkpoints:
latest_step = int(checkpoints[0].name.split('_')[1])
new_step = latest_step + steps
else:
latest_step = 100
new_step = latest_step + steps
current_ckpt = Store.state.get('current_checkpoint', DENSE_CHECKPOINT)
# Prepare training data
training_data = json.dumps(DENSE_TRAINING_EXAMPLES)
# Create conservative training script
training_script = f'''
import sys
sys.path.insert(0, "{ROOT}")
import torch
import json
import random
from transformers import AutoTokenizer, AutoModelForCausalLM, BitsAndBytesConfig
from peft import PeftModel, get_peft_model, LoraConfig
import os
print("Loading model for CONSERVATIVE training...")
MODEL_PATH = "{MODEL_PATH}"
CHECKPOINT = "{current_ckpt}"
tokenizer = AutoTokenizer.from_pretrained(MODEL_PATH, local_files_only=True)
tokenizer.pad_token = tokenizer.eos_token
model = AutoModelForCausalLM.from_pretrained(
MODEL_PATH,
quantization_config=BitsAndBytesConfig(
load_in_4bit=True,
bnb_4bit_quant_type="nf4",
bnb_4bit_compute_dtype=torch.bfloat16,
),
device_map="auto",
torch_dtype=torch.bfloat16,
local_files_only=True
)
if os.path.exists(CHECKPOINT):
model = PeftModel.from_pretrained(model, CHECKPOINT, is_trainable=True)
print(f"Loaded checkpoint: {{CHECKPOINT}}")
else:
lora_config = LoraConfig(
r=16, lora_alpha=32,
target_modules=["q_proj", "v_proj", "k_proj", "o_proj"],
lora_dropout=0.05
)
model = get_peft_model(model, lora_config)
print("Created new LoRA adapter")
# Load diverse training data
training_examples = {training_data}
print(f"Training on {{len(training_examples)}} diverse examples for {steps} steps...")
# Conservative optimizer with LOW learning rate
optimizer = torch.optim.AdamW(model.parameters(), lr=2e-6) # Very low LR
model.train()
total_loss = 0
losses = []
for step in range({steps}):
# Randomly sample an example (ensures diversity)
ex = random.choice(training_examples)
prompt = ex["prompt"]
response = ex["response"]
# Format for ChatML
full_text = f"<|im_start|>user\\n{{prompt}}<|im_end|>\\n<|im_start|>assistant\\n{{response}}<|im_end|>"
inputs = tokenizer(full_text, return_tensors="pt", truncation=True, max_length=512)
inputs = {{k: v.to(model.device) for k, v in inputs.items()}}
outputs = model(**inputs, labels=inputs["input_ids"])
loss = outputs.loss
optimizer.zero_grad()
loss.backward()
# Gradient clipping for stability
torch.nn.utils.clip_grad_norm_(model.parameters(), 0.5)
optimizer.step()
total_loss += loss.item()
losses.append(loss.item())
if step % 5 == 0:
recent_avg = sum(losses[-5:]) / len(losses[-5:]) if losses[-5:] else 0
print(f"Step {{step}}: loss={{loss.item():.4f}}, recent_avg={{recent_avg:.4f}}")
# Save checkpoint
save_path = "{CHECKPOINTS_DIR}/step_{new_step}"
model.save_pretrained(save_path)
final_avg_loss = total_loss / {steps}
print(f"\\nSaved checkpoint to {{save_path}}")
print(f"Final avg loss: {{final_avg_loss:.4f}}")
print("TRAINING_COMPLETE")
'''
script_path = os.path.join(ROOT, "_stable_train.py")
with open(script_path, 'w') as f:
f.write(training_script)
result = AgentTools.shell(f"python {script_path}", timeout=600)
if "TRAINING_COMPLETE" in result.get('output', ''):
new_checkpoint = f"{CHECKPOINTS_DIR}/step_{new_step}"
Store.state['training_runs'].append({
'timestamp': datetime.now().isoformat(),
'steps': steps,
'checkpoint': new_checkpoint
})
Store.save()
return {
'success': True,
'new_checkpoint': new_checkpoint,
'output': result['output'][-2000:]
}
else:
return {
'success': False,
'output': result['output'][-2000:]
}
def compare_checkpoints(self, old_ckpt: str, new_ckpt: str) -> Dict[str, Any]:
"""A/B test two checkpoints."""
print(f"\n[COMPARE] A/B Testing checkpoints...")
print(f" OLD: {old_ckpt}")
print(f" NEW: {new_ckpt}")
# Evaluate old
reload_model(old_ckpt)
old_eval = self.evaluate_current_model()
# Evaluate new
reload_model(new_ckpt)
new_eval = self.evaluate_current_model()
# Compare
quality_diff = new_eval['avg_quality'] - old_eval['avg_quality']
pass_diff = new_eval['pass_rate'] - old_eval['pass_rate']
print(f"\n[COMPARE] Results:")
print(f" OLD quality: {old_eval['avg_quality']:.3f}, pass rate: {old_eval['pass_rate']:.1%}")
print(f" NEW quality: {new_eval['avg_quality']:.3f}, pass rate: {new_eval['pass_rate']:.1%}")
print(f" Quality diff: {quality_diff:+.3f}")
# Decision logic
keep_new = False
reason = ""
if new_eval['is_degraded']:
keep_new = False
reason = "New checkpoint quality below minimum threshold"
elif quality_diff > 0.02:
keep_new = True
reason = f"New checkpoint improves quality by {quality_diff:.3f}"
elif quality_diff < -Config.quality_drop_threshold:
keep_new = False
reason = f"New checkpoint degrades quality by {abs(quality_diff):.3f}"
elif quality_diff >= 0:
keep_new = True
reason = "New checkpoint maintains or slightly improves quality"
else:
keep_new = False
reason = "New checkpoint slightly degrades quality - keeping stable"
print(f"[COMPARE] Decision: {'KEEP NEW' if keep_new else 'KEEP OLD'} - {reason}")
return {
'keep_new': keep_new,
'reason': reason,
'old_eval': old_eval,
'new_eval': new_eval,
'quality_diff': quality_diff,
}
def improve(self, max_iterations: int = None) -> Dict[str, Any]:
"""Main self-improvement loop with stability safeguards."""
max_iterations = max_iterations or Config.max_improvement_iterations
print("\n" + "=" * 70)
print("🔄 STABLE SELF-IMPROVEMENT LOOP (v2)")
print("=" * 70)
print(f" Max iterations: {max_iterations}")
print(f" Steps per iteration: {Config.training_steps_per_iteration}")
print(f" Training examples: {len(DENSE_TRAINING_EXAMPLES)}")
print(f" Target quality: {Config.target_quality_score}")
print(f" Quality drop threshold: {Config.quality_drop_threshold}")
print("=" * 70)
# Initial evaluation
print("\n[IMPROVE] Initial evaluation...")
baseline = self.evaluate_current_model()
self.baseline_quality = baseline['avg_quality']
# Save as best if better than current best
if baseline['avg_quality'] > Store.state.get('best_quality_score', 0):
Store.state['best_quality_score'] = baseline['avg_quality']
Store.state['best_checkpoint'] = Store.state.get('current_checkpoint', DENSE_CHECKPOINT)
history = [{
'iteration': 0,
'type': 'baseline',
'quality': baseline['avg_quality'],
'pass_rate': baseline['pass_rate'],
'checkpoint': Store.state.get('current_checkpoint'),
}]
for iteration in range(1, max_iterations + 1):
print(f"\n{'=' * 70}")
print(f"ITERATION {iteration}/{max_iterations}")
print("=" * 70)
# Check if target reached
if not baseline.get('needs_improvement', True):
print(f"\n✓ TARGET REACHED! Quality: {baseline['avg_quality']:.3f}")
Store.record_improvement({
'status': 'target_reached',
'final_quality': baseline['avg_quality'],
'iterations': iteration - 1,
'history': history
})
return {
'success': True,
'status': 'target_reached',
'iterations': iteration - 1,
'final_quality': baseline['avg_quality'],
'history': history
}
# Check for degradation
if baseline.get('is_degraded', False):
print(f"\n⚠️ QUALITY DEGRADED! Rolling back...")
self.rollback_to_best()
Store.record_improvement({
'status': 'rolled_back',
'reason': 'quality_degraded',
'iteration': iteration,
'history': history
})
return {
'success': False,
'status': 'rolled_back',
'reason': 'quality_degraded',
'history': history
}
# Save rollback point before training
self.save_rollback_checkpoint()
old_checkpoint = Store.state.get('current_checkpoint', DENSE_CHECKPOINT)
# Run training
print(f"\n[IMPROVE] Quality {baseline['avg_quality']:.3f} < target {Config.target_quality_score}")
training_result = self.run_training_iteration()
if not training_result['success']:
print("[IMPROVE] ⚠️ Training failed!")
history.append({
'iteration': iteration,
'type': 'training_failed',
'error': training_result['output'][-500:]
})
continue
# A/B compare old vs new
comparison = self.compare_checkpoints(old_checkpoint, training_result['new_checkpoint'])
iteration_record = {
'iteration': iteration,
'type': 'comparison',
'old_quality': comparison['old_eval']['avg_quality'],
'new_quality': comparison['new_eval']['avg_quality'],
'quality_diff': comparison['quality_diff'],
'kept': 'new' if comparison['keep_new'] else 'old',
'reason': comparison['reason'],
}
history.append(iteration_record)
# Decision
if comparison['keep_new']:
Store.state['current_checkpoint'] = training_result['new_checkpoint']
# Update best if improved
if comparison['new_eval']['avg_quality'] > Store.state.get('best_quality_score', 0):
Store.state['best_quality_score'] = comparison['new_eval']['avg_quality']
Store.state['best_checkpoint'] = training_result['new_checkpoint']
print(f"[IMPROVE] ★ New best! Quality: {Store.state['best_quality_score']:.3f}")
baseline = comparison['new_eval']
else:
# Rollback to old
reload_model(old_checkpoint)
baseline = comparison['old_eval']
Store.state['improvement_iterations'] += 1
Store.state['quality_history'].append({
'iteration': iteration,
'quality': baseline['avg_quality'],
'timestamp': datetime.now().isoformat()
})
Store.save()
# Final evaluation
final_eval = self.evaluate_current_model()
result = {
'success': final_eval['avg_quality'] >= Config.target_quality_score,
'status': 'completed',
'iterations': max_iterations,
'initial_quality': self.baseline_quality,
'final_quality': final_eval['avg_quality'],
'best_quality': Store.state.get('best_quality_score', 0),
'best_checkpoint': Store.state.get('best_checkpoint'),
'rollback_count': Store.state.get('rollback_count', 0),
'history': history
}
Store.record_improvement(result)
return result
# ==============================================================================
# TOOLS (Original Limited)
# ==============================================================================
ALLOWED_SHELL = {"ls", "cat", "wc", "head", "tail", "nvidia-smi", "df", "du", "grep", "rg", "python3", "python"}
def tool_shell(cmd: str) -> str:
try:
exe = cmd.strip().split()[0]
if exe not in ALLOWED_SHELL:
return f"[shell] blocked: {exe} (use !shell for full access)"
p = subprocess.run(cmd, shell=True, stdout=subprocess.PIPE, stderr=subprocess.STDOUT, timeout=20)
return p.stdout.decode("utf-8", errors="ignore")[:8000]
except Exception as e:
return f"[shell] error: {e}"
def tool_py(code: str) -> str:
try:
g = {
"__builtins__": {"range": range, "len": len, "min": min, "max": max, "sum": sum, "print": print},
"math": math, "json": json, "re": re, "statistics": statistics, "random": random
}
l = {}
exec(code, g, l)
return f"[py] ok\n{l.get('out', '')}"
except Exception:
return f"[py] error:\n{traceback.format_exc()[-2000:]}"
def tool_search_local(query: str, path: str = ROOT) -> str:
rg = shutil.which("rg")
if rg:
cmd = f'rg -n --no-heading --hidden -S "{query}" {path}'
else:
cmd = f'grep -RIn --exclude-dir=.git --exclude-dir=__pycache__ -e "{query}" {path}'
return tool_shell(cmd)
# === BROWSER TOOLS ===
def tool_browser_open(url: str = "https://google.com") -> str:
"""Open visible browser and navigate to URL."""
global _playwright, _browser, _page, _browser_context
if not BROWSER_OK:
return "[browser] Not available - pip install playwright && playwright install firefox"
try:
if _browser is None:
_playwright = sync_playwright().start()
# Option 1: Persistent profile (uses existing logins)
if LOGIN_CONFIG.get("use_persistent_profile") and LOGIN_CONFIG.get("firefox_profile_path"):
profile_path = LOGIN_CONFIG["firefox_profile_path"]
print(f"[browser] Using persistent profile: {profile_path}")
_browser_context = _playwright.firefox.launch_persistent_context(
profile_path,
headless=False,
viewport={"width": 1280, "height": 800}
)
_page = _browser_context.pages[0] if _browser_context.pages else _browser_context.new_page()
else:
# Option 2: Fresh browser (default)
_browser = _playwright.firefox.launch(headless=False)
_page = _browser.new_page()
_page.set_viewport_size({"width": 1280, "height": 800})
_page.goto(url, timeout=30000)
title = _page.title()
return f"[browser] Opened: {title}\n[browser] URL: {url}"
except Exception as e:
return f"[browser] Error: {e}"
def tool_browser_click(selector: str) -> str:
"""Click element by CSS selector or text."""
global _page
if _page is None:
return "[browser] No browser open. Use !browse <url> first"
try:
# Try CSS selector first
if _page.query_selector(selector):
_page.click(selector)
return f"[browser] Clicked: {selector}"
# Try text selector
_page.click(f"text={selector}")
return f"[browser] Clicked text: {selector}"
except Exception as e:
return f"[browser] Click error: {e}"
def tool_browser_type(text: str, selector: str = None) -> str:
"""Type text into focused element or specified selector."""
global _page
if _page is None:
return "[browser] No browser open"
try:
if selector:
_page.fill(selector, text)
return f"[browser] Typed into {selector}"
else:
_page.keyboard.type(text)
return f"[browser] Typed: {text[:50]}..."
except Exception as e:
return f"[browser] Type error: {e}"
def tool_browser_press(key: str) -> str:
"""Press a key (Enter, Tab, Escape, etc)."""
global _page
if _page is None:
return "[browser] No browser open"
try:
_page.keyboard.press(key)
return f"[browser] Pressed: {key}"
except Exception as e:
return f"[browser] Key error: {e}"
def tool_browser_read() -> str:
"""Read visible page text content."""
global _page
if _page is None:
return "[browser] No browser open"
try:
text = _page.inner_text("body")
# Truncate and clean
text = ' '.join(text.split())[:3000]
return f"[browser] Page content:\n{text}"
except Exception as e:
return f"[browser] Read error: {e}"
def tool_browser_url() -> str:
"""Get current URL."""
global _page
if _page is None:
return "[browser] No browser open"
return f"[browser] URL: {_page.url}"
def tool_browser_back() -> str:
"""Go back in history."""
global _page
if _page is None:
return "[browser] No browser open"
_page.go_back()
return f"[browser] Back to: {_page.url}"
def tool_browser_close() -> str:
"""Close browser."""
global _playwright, _browser, _page
try:
if _browser:
_browser.close()
if _playwright:
_playwright.stop()
_browser = None
_page = None
_playwright = None
return "[browser] Closed"
except:
return "[browser] Already closed"
# === AUTONOMOUS TASK EXECUTION ===
# Task templates - no model planning needed
TASK_TEMPLATES = {
"search_news": {
"keywords": ["news", "latest news", "articles", "headlines", "what's happening"],
"steps": [
{"action": "BROWSE", "args": ["https://news.google.com"]},
{"action": "WAIT", "args": ["2"]},
{"action": "FILL", "args": ["input[name=q]", "{query}"]},
{"action": "PRESS", "args": ["Enter"]},
{"action": "WAIT", "args": ["3"]},
{"action": "READ", "args": []},
]
},
"search_google": {
"keywords": ["search for", "google", "look up", "find me", "find info"],
"steps": [
{"action": "BROWSE", "args": ["https://google.com"]},
{"action": "WAIT", "args": ["1"]},
{"action": "FILL", "args": ["textarea[name=q]", "{query}"]},
{"action": "PRESS", "args": ["Enter"]},
{"action": "WAIT", "args": ["3"]},
{"action": "READ", "args": []},
]
},
"weather": {
"keywords": ["weather", "temperature", "forecast"],
"steps": [
{"action": "BROWSE", "args": ["https://weather.com"]},
{"action": "WAIT", "args": ["2"]},
{"action": "READ", "args": []},
]
},
"wikipedia": {
"keywords": ["wikipedia", "wiki", "what is", "who is", "define"],
"steps": [
{"action": "BROWSE", "args": ["https://en.wikipedia.org"]},
{"action": "WAIT", "args": ["1"]},
{"action": "FILL", "args": ["input[name=search]", "{query}"]},
{"action": "PRESS", "args": ["Enter"]},
{"action": "WAIT", "args": ["2"]},
{"action": "READ", "args": []},
]
},
"youtube": {
"keywords": ["youtube", "video", "watch"],
"steps": [
{"action": "BROWSE", "args": ["https://youtube.com"]},
{"action": "WAIT", "args": ["2"]},
{"action": "FILL", "args": ["input[name=search_query]", "{query}"]},
{"action": "PRESS", "args": ["Enter"]},
{"action": "WAIT", "args": ["3"]},
{"action": "READ", "args": []},
]
},
# === FREELANCE PLATFORMS ===
"upwork_search": {
"keywords": ["upwork jobs", "freelance jobs", "find gigs", "upwork"],
"steps": [
{"action": "BROWSE", "args": ["https://www.upwork.com/nx/jobs/search/?q={query}"]},
{"action": "WAIT", "args": ["3"]},
{"action": "READ", "args": []},
]
},
"fiverr_search": {
"keywords": ["fiverr", "fiverr gigs"],
"steps": [
{"action": "BROWSE", "args": ["https://www.fiverr.com/search/gigs?query={query}"]},
{"action": "WAIT", "args": ["3"]},
{"action": "READ", "args": []},
]
},
# === SELLING PLATFORMS ===
"ebay_search": {
"keywords": ["ebay", "sell on ebay", "ebay listings"],
"steps": [
{"action": "BROWSE", "args": ["https://www.ebay.com/sch/i.html?_nkw={query}"]},
{"action": "WAIT", "args": ["3"]},
{"action": "READ", "args": []},
]
},
"amazon_search": {
"keywords": ["amazon products", "amazon search"],
"steps": [
{"action": "BROWSE", "args": ["https://www.amazon.com/s?k={query}"]},
{"action": "WAIT", "args": ["3"]},
{"action": "READ", "args": []},
]
},
# === CRYPTO/FINANCE ===
"crypto_prices": {
"keywords": ["crypto price", "bitcoin", "ethereum", "crypto"],
"steps": [
{"action": "BROWSE", "args": ["https://coinmarketcap.com"]},
{"action": "WAIT", "args": ["2"]},
{"action": "READ", "args": []},
]
},
"stock_price": {
"keywords": ["stock price", "stocks", "market"],
"steps": [
{"action": "BROWSE", "args": ["https://finance.yahoo.com/quote/{query}"]},
{"action": "WAIT", "args": ["2"]},
{"action": "READ", "args": []},
]
}
}
# === LOGIN TEMPLATES ===
LOGIN_TEMPLATES = {
"gmail": {
"url": "https://accounts.google.com/signin",
"steps": [
{"action": "BROWSE", "args": ["https://accounts.google.com/signin"]},
{"action": "WAIT", "args": ["2"]},
{"action": "FILL", "args": ["input[type=email]", "{email}"]},
{"action": "PRESS", "args": ["Enter"]},
{"action": "WAIT", "args": ["3"]},
{"action": "FILL", "args": ["input[type=password]", "{password}"]},
{"action": "PRESS", "args": ["Enter"]},
{"action": "WAIT", "args": ["5"]},
]
},
"facebook": {
"url": "https://facebook.com/login",
"steps": [
{"action": "BROWSE", "args": ["https://facebook.com/login"]},
{"action": "WAIT", "args": ["2"]},
{"action": "FILL", "args": ["input[name=email]", "{email}"]},
{"action": "FILL", "args": ["input[name=pass]", "{password}"]},
{"action": "CLICK", "args": ["button[name=login]"]},
{"action": "WAIT", "args": ["5"]},
]
},
"twitter": {
"url": "https://twitter.com/login",
"steps": [
{"action": "BROWSE", "args": ["https://twitter.com/login"]},
{"action": "WAIT", "args": ["2"]},
{"action": "FILL", "args": ["input[autocomplete=username]", "{email}"]},
{"action": "CLICK", "args": ["text=Next"]},
{"action": "WAIT", "args": ["2"]},
{"action": "FILL", "args": ["input[type=password]", "{password}"]},
{"action": "CLICK", "args": ["text=Log in"]},
{"action": "WAIT", "args": ["5"]},
]
},
"linkedin": {
"url": "https://linkedin.com/login",
"steps": [
{"action": "BROWSE", "args": ["https://linkedin.com/login"]},
{"action": "WAIT", "args": ["2"]},
{"action": "FILL", "args": ["input[name=session_key]", "{email}"]},
{"action": "FILL", "args": ["input[name=session_password]", "{password}"]},
{"action": "CLICK", "args": ["button[type=submit]"]},
{"action": "WAIT", "args": ["5"]},
]
},
"github": {
"url": "https://github.com/login",
"steps": [
{"action": "BROWSE", "args": ["https://github.com/login"]},
{"action": "WAIT", "args": ["2"]},
{"action": "FILL", "args": ["input[name=login]", "{email}"]},
{"action": "FILL", "args": ["input[name=password]", "{password}"]},
{"action": "CLICK", "args": ["input[type=submit]"]},
{"action": "WAIT", "args": ["5"]},
]
},
"outlook": {
"url": "https://login.live.com",
"steps": [
{"action": "BROWSE", "args": ["https://login.live.com"]},
{"action": "WAIT", "args": ["2"]},
{"action": "FILL", "args": ["input[type=email]", "{email}"]},
{"action": "PRESS", "args": ["Enter"]},
{"action": "WAIT", "args": ["3"]},
{"action": "FILL", "args": ["input[type=password]", "{password}"]},
{"action": "PRESS", "args": ["Enter"]},
{"action": "WAIT", "args": ["5"]},
]
},
"generic": {
"url": "{url}",
"steps": [
{"action": "BROWSE", "args": ["{url}"]},
{"action": "WAIT", "args": ["2"]},
{"action": "FILL", "args": ["input[type=email], input[type=text], input[name=email], input[name=username], input[name=login]", "{email}"]},
{"action": "FILL", "args": ["input[type=password]", "{password}"]},
{"action": "PRESS", "args": ["Enter"]},
{"action": "WAIT", "args": ["5"]},
]
}
}
# === EMAIL ACTION TEMPLATES ===
EMAIL_ACTIONS = {
"gmail_search": {
"steps": [
{"action": "BROWSE", "args": ["https://mail.google.com"]},
{"action": "WAIT", "args": ["3"]},
{"action": "FILL", "args": ["input[name=q]", "{query}"]},
{"action": "PRESS", "args": ["Enter"]},
{"action": "WAIT", "args": ["3"]},
{"action": "READ", "args": []},
]
},
"gmail_compose": {
"steps": [
{"action": "BROWSE", "args": ["https://mail.google.com"]},
{"action": "WAIT", "args": ["3"]},
{"action": "CLICK", "args": ["div[gh='cm']", "text=Compose"]},
{"action": "WAIT", "args": ["2"]},
{"action": "FILL", "args": ["input[name=to]", "{to}"]},
{"action": "FILL", "args": ["input[name=subjectbox]", "{subject}"]},
{"action": "FILL", "args": ["div[aria-label='Message Body']", "{body}"]},
{"action": "WAIT", "args": ["1"]},
]
},
"gmail_send": {
"steps": [
{"action": "CLICK", "args": ["div[aria-label='Send']", "text=Send"]},
{"action": "WAIT", "args": ["3"]},
]
},
"outlook_search": {
"steps": [
{"action": "BROWSE", "args": ["https://outlook.live.com/mail"]},
{"action": "WAIT", "args": ["3"]},
{"action": "FILL", "args": ["input[aria-label='Search']", "{query}"]},
{"action": "PRESS", "args": ["Enter"]},
{"action": "WAIT", "args": ["3"]},
{"action": "READ", "args": []},
]
}
}
def detect_email_action(user_input: str) -> Tuple[Optional[str], Optional[Dict]]:
"""Detect email-related requests."""
user_lower = user_input.lower()
# Search email
search_triggers = ["search email", "search my email", "find email", "look for email",
"search inbox", "search gmail", "search outlook", "find in email",
"emails about", "emails from", "emails containing"]
for trigger in search_triggers:
if trigger in user_lower:
# Extract search query
query = user_input
for prefix in ["search email for", "search my email for", "find email about",
"search inbox for", "search gmail for", "emails about",
"emails from", "emails containing", "look for email"]:
query = re.sub(rf"{prefix}\s*", "", query, flags=re.IGNORECASE)
service = "gmail" if "gmail" in user_lower else "outlook" if "outlook" in user_lower else "gmail"
return f"{service}_search", {"query": query.strip()}
# Compose email
compose_triggers = ["send email", "compose email", "write email", "email to", "send a message to"]
for trigger in compose_triggers:
if trigger in user_lower:
return "gmail_compose", {"to": "", "subject": "", "body": ""}
return None, None
def confirm_email_action(action: str, params: Dict) -> bool:
"""Confirm email action."""
print("\n" + "="*60)
print("📧 EMAIL ACTION CONFIRMATION")
print("="*60)
if "search" in action:
print(f"\n🔍 Action: Search emails")
print(f"📝 Query: {params.get('query', 'N/A')}")
elif "compose" in action:
print(f"\n✉️ Action: Compose email")
print(f"📬 To: {params.get('to', 'N/A')}")
print(f"📋 Subject: {params.get('subject', 'N/A')}")
print("="*60)
response = input("\n✅ Proceed? (yes/no): ").strip().lower()
return response in ['yes', 'y']
def execute_email_action(action: str, params: Dict) -> str:
"""Execute email action - uses API if enabled, browser fallback."""
# Use Gmail API if enabled (faster, more reliable)
if LOGIN_CONFIG.get("use_gmail_api") and _gmail_service:
print("\n[email] Using Gmail API (fast mode)")
if "search" in action:
return gmail_api_search(params.get("query", ""), max_results=10)
elif "send" in action or "compose" in action:
return gmail_api_send(
params.get("to", ""),
params.get("subject", ""),
params.get("body", "")
)
else:
return gmail_api_search("in:inbox", max_results=5)
# Browser fallback
template = EMAIL_ACTIONS.get(action)
if not template:
return f"[email] Unknown action: {action}"
print("\n[executing email action via browser...]")
page_content = ""
for step in template["steps"]:
step_action = step["action"].upper()
args = []
for arg in step["args"]:
for key, val in params.items():
arg = arg.replace("{" + key + "}", str(val))
args.append(arg)
if step_action == "BROWSE":
tool_browser_open(args[0])
elif step_action == "FILL":
selectors = args[0].split(", ")
for sel in selectors:
try:
if _page and _page.query_selector(sel):
tool_browser_type(args[1] if len(args) > 1 else "", sel)
break
except:
continue
elif step_action == "CLICK":
for selector in args:
try:
tool_browser_click(selector)
break
except:
continue
elif step_action == "PRESS":
tool_browser_press(args[0] if args else "Enter")
elif step_action == "WAIT":
time.sleep(int(args[0]) if args else 2)
elif step_action == "READ":
page_content = tool_browser_read()
time.sleep(0.5)
print("[done]\n")
return page_content
# Stored credentials (in memory only - not saved to disk)
STORED_CREDENTIALS = {}
def store_credential(service: str, email: str, password: str):
"""Store credentials in memory."""
STORED_CREDENTIALS[service.lower()] = {"email": email, "password": password}
print(f"[credentials] Stored for {service}")
def get_credential(service: str) -> Optional[Dict]:
"""Get stored credentials."""
return STORED_CREDENTIALS.get(service.lower())
def detect_login_request(user_input: str) -> Tuple[Optional[str], Optional[str]]:
"""Detect if user wants to log into something."""
user_lower = user_input.lower()
login_triggers = ["log in", "login", "sign in", "signin", "log into", "sign into"]
if not any(trigger in user_lower for trigger in login_triggers):
return None, None
# Detect service
for service in LOGIN_TEMPLATES.keys():
if service in user_lower:
return service, None
# Check for URL
url_match = re.search(r'(https?://[^\s]+)', user_input)
if url_match:
return "generic", url_match.group(1)
return None, None
def confirm_login(service: str, email: str) -> bool:
"""Confirm login action."""
print("\n" + "="*60)
print("🔐 LOGIN CONFIRMATION")
print("="*60)
print(f"\n📧 Service: {service.upper()}")
print(f"👤 Account: {email}")
print(f"\n⚠️ The AI will enter your credentials and log in.")
print("="*60)
response = input("\n✅ Proceed with login? (yes/no): ").strip().lower()
return response in ['yes', 'y']
def execute_login(service: str, email: str, password: str, url: str = None) -> str:
"""Execute login with multiple fallback strategies - BEAST MODE."""
print(f"\n[login] 🔐 Attempting {service} login...")
print(f"[login] Using Firefox (less detection)")
if service in ["gmail", "google", "email"]:
strategies = [
# Strategy 1: Direct type, human-like
{
"name": "Human-like typing",
"steps": [
("browse", "https://accounts.google.com/signin"),
("wait", 3),
("human_type", "input[type=email]", email),
("wait", 1),
("press", "Enter"),
("wait", 4),
("human_type", "input[type=password]", password),
("wait", 1),
("press", "Enter"),
("wait", 5),
]
},
# Strategy 2: Click-based navigation
{
"name": "Click navigation",
"steps": [
("browse", "https://mail.google.com"),
("wait", 4),
("click", "input[type=email]"),
("human_type", None, email),
("click", "#identifierNext, button:has-text('Next')"),
("wait", 4),
("click", "input[type=password]"),
("human_type", None, password),
("click", "#passwordNext, button:has-text('Next')"),
("wait", 5),
]
},
# Strategy 3: Pre-filled email URL
{
"name": "Pre-filled URL",
"steps": [
("browse", f"https://accounts.google.com/v3/signin/identifier?Email={email}&continue=https://mail.google.com"),
("wait", 4),
("human_type", "input[type=password]", password),
("press", "Enter"),
("wait", 5),
]
}
]
else:
# Use template for other services
template = LOGIN_TEMPLATES.get(service, LOGIN_TEMPLATES["generic"])
strategies = [{
"name": f"{service} template",
"steps": []
}]
for step in template["steps"]:
action = step["action"].lower()
args = [a.replace("{email}", email).replace("{password}", password).replace("{url}", url or "") for a in step["args"]]
if action == "browse":
strategies[0]["steps"].append(("browse", args[0]))
elif action == "fill":
strategies[0]["steps"].append(("human_type", args[0], args[1] if len(args) > 1 else ""))
elif action == "click":
strategies[0]["steps"].append(("click", args[0]))
elif action == "press":
strategies[0]["steps"].append(("press", args[0] if args else "Enter"))
elif action == "wait":
strategies[0]["steps"].append(("wait", int(args[0]) if args else 2))
# Try each strategy
for strat_idx, strategy in enumerate(strategies):
print(f" [{strat_idx+1}/{len(strategies)}] Trying: {strategy['name']}")
try:
for step in strategy["steps"]:
action = step[0]
if action == "browse":
tool_browser_open(step[1])
time.sleep(1)
elif action == "wait":
time.sleep(step[1])
elif action == "human_type":
selector, text = step[1], step[2] if len(step) > 2 else ""
if _page:
# Click the field first if selector provided
if selector:
try:
elem = _page.wait_for_selector(selector, timeout=5000)
if elem:
elem.click()
time.sleep(0.3)
except:
pass
# Type like a human - variable speed
for char in text:
_page.keyboard.type(char, delay=random.randint(30, 120))
time.sleep(random.uniform(0.02, 0.08))
time.sleep(0.5)
elif action == "click":
if _page:
selectors = step[1].split(", ")
for sel in selectors:
try:
elem = _page.wait_for_selector(sel, timeout=3000)
if elem:
elem.click()
break
except:
continue
elif action == "press":
if _page:
time.sleep(0.3)
_page.keyboard.press(step[1])
time.sleep(0.3)
# Check login result
time.sleep(3)
current_url = _page.url if _page else ""
page_text = tool_browser_read().lower()
# Failure indicators - check these FIRST
failures = ["couldn't sign you in", "wrong password", "verify it's you",
"unusual activity", "captcha", "robot", "try again", "blocked",
"sign in", "create an account", "for work", "learn more"]
# If we're on a landing/marketing page, not logged in
if "workspace.google.com" in current_url or "accounts.google.com/signin" in current_url:
print(f" ❌ Still on login/landing page")
continue
if any(f in page_text[:500] for f in failures):
print(f" ❌ Detected: not logged in")
continue
# Success indicators - MUST have inbox URL pattern
inbox_urls = ["mail.google.com/mail", "inbox", "#inbox"]
inbox_text = ["compose", "primary", "social", "promotions", "starred", "sent", "drafts"]
url_ok = any(u in current_url.lower() for u in inbox_urls)
text_ok = any(t in page_text[:1000] for t in inbox_text)
if url_ok and text_ok:
print(f" ✅ SUCCESS with {strategy['name']}!")
return f"[login] ✅ Logged into {service}\n{page_text[:300]}"
if url_ok:
print(f" ✅ SUCCESS (URL match) with {strategy['name']}!")
return f"[login] ✅ Logged into {service}\n{page_text[:300]}"
# 2FA check
if "2-step" in page_text or "verify" in page_text or "code" in page_text:
print(f" ⚠️ 2FA/Verification required")
input(" Complete verification in browser, then press Enter...")
return f"[login] ✅ Logged in after 2FA\n{tool_browser_read()[:300]}"
except Exception as e:
print(f" Error: {str(e)[:50]}")
continue
# All failed - offer manual
print(f"\n ❌ All {len(strategies)} strategies failed")
print(f" 💡 Google may be blocking automated logins")
manual = input("\n Try manual login? (yes/no): ").strip().lower()
if manual in ['yes', 'y']:
print(" Opening login page - complete login manually...")
tool_browser_open("https://mail.google.com")
input(" Press Enter when logged in...")
page_text = tool_browser_read()
if "inbox" in page_text.lower() or "compose" in page_text.lower():
return f"[login] ✅ Manual login successful\n{page_text[:300]}"
return f"[login] ❌ Failed to login to {service}. Try logging in manually first, then use the browser."
def detect_web_task(user_input: str) -> Tuple[Optional[str], Optional[str]]:
"""Detect if user wants a web task. Returns (template_name, query) or (None, None)."""
user_lower = user_input.lower()
# Check for web-related intent
web_triggers = ["look up", "search", "find", "news", "weather", "google",
"browse", "website", "check online", "wikipedia", "youtube"]
if not any(trigger in user_lower for trigger in web_triggers):
return None, None
# Match to template
for template_name, template in TASK_TEMPLATES.items():
if any(kw in user_lower for kw in template["keywords"]):
# Extract query from user input
query = user_input
# Remove common prefixes
for prefix in ["can you", "please", "could you", "look up", "search for",
"find me", "find", "google", "search", "get me", "show me"]:
query = re.sub(rf"^{prefix}\s+", "", query, flags=re.IGNORECASE)
query = query.strip("?. ")
return template_name, query
# Default to google search
query = user_input
for prefix in ["can you", "please", "could you", "look up", "search for",
"find me", "find", "search", "get me"]:
query = re.sub(rf"^{prefix}\s+", "", query, flags=re.IGNORECASE)
return "search_google", query.strip("?. ")
def build_plan_from_template(template_name: str, query: str) -> dict:
"""Build execution plan from template."""
template = TASK_TEMPLATES.get(template_name, TASK_TEMPLATES["search_google"])
steps = []
for step in template["steps"]:
new_step = {"action": step["action"], "args": []}
for arg in step["args"]:
new_step["args"].append(arg.replace("{query}", query))
steps.append(new_step)
return {"task": f"{template_name}: {query}", "steps": steps, "query": query}
def confirm_task(plan: dict) -> bool:
"""Show confirmation box and get user approval."""
print("\n" + "="*60)
print("🔍 I detected a web task. Here's my plan:")
print("="*60)
print(f"\n📋 TASK: {plan.get('query', plan.get('task', 'Unknown'))}\n")
print("📝 PLANNED STEPS:")
print("-"*40)
for i, step in enumerate(plan.get('steps', []), 1):
action = step.get('action', '?')
args = step.get('args', [])
# Clean display
display_args = ' '.join(str(a)[:50] for a in args)
print(f" {i}. {action} {display_args}")
print("-"*40)
response = input("\n✅ Proceed? (yes/no): ").strip().lower()
return response in ['yes', 'y']
def execute_task_silent(plan: dict) -> str:
"""Execute plan and return page content."""
page_content = ""
print("\n[executing...]")
for i, step in enumerate(plan.get('steps', []), 1):
action = step.get('action', '').upper()
args = step.get('args', [])
if action == "BROWSE":
url = args[0] if args else "https://google.com"
if not url.startswith("http"):
url = "https://" + url
tool_browser_open(url)
elif action == "CLICK":
tool_browser_click(args[0] if args else "")
elif action == "TYPE":
tool_browser_type(args[0] if args else "")
elif action == "FILL":
if len(args) >= 2:
tool_browser_type(args[1], args[0])
elif action == "PRESS":
tool_browser_press(args[0] if args else "Enter")
elif action == "READ":
result = tool_browser_read()
page_content = result.replace("[browser] Page content:\n", "")
elif action == "WAIT":
wait_time = int(args[0]) if args else 2
time.sleep(wait_time)
time.sleep(0.5)
print("[done]\n")
return page_content
# === TASK CHAINING SYSTEM ===
class TaskChain:
"""Chain multiple tasks with checkpoints and error recovery."""
def __init__(self):
self.tasks = []
self.current_idx = 0
self.results = []
self.errors = []
self.running = False
self.paused = False
def add(self, task_type: str, params: Dict, description: str):
"""Add task to chain."""
self.tasks.append({
"type": task_type,
"params": params,
"description": description,
"status": "pending",
"result": None,
"error": None,
"retries": 0
})
def clear(self):
"""Clear all tasks."""
self.tasks = []
self.current_idx = 0
self.results = []
self.errors = []
def show(self) -> str:
"""Display current task chain."""
if not self.tasks:
return "[chain] No tasks queued. Use 'chain: <task>' to add tasks."
output = "\n" + "="*60 + "\n"
output += "📋 TASK CHAIN\n"
output += "="*60 + "\n\n"
for i, task in enumerate(self.tasks):
status_icon = {"pending": "", "running": "🔄", "success": "", "failed": "", "skipped": "⏭️"}.get(task["status"], "")
marker = "" if i == self.current_idx and self.running else " "
output += f"{marker}{i+1}. {status_icon} {task['description']}\n"
if task["error"]:
output += f" ⚠️ Error: {task['error'][:50]}...\n"
output += "\n" + "="*60
return output
TASK_CHAIN = TaskChain()
def parse_chain_task(task_str: str) -> Tuple[str, Dict, str]:
"""Parse a task string into type, params, description."""
task_lower = task_str.lower().strip()
if task_lower.startswith("login "):
service = task_lower.replace("login ", "").strip()
return "login", {"service": service}, f"Login to {service}"
if task_lower.startswith("browse "):
url = task_str.split(" ", 1)[1].strip()
if not url.startswith("http"):
url = "https://" + url
return "browse", {"url": url}, f"Browse to {url}"
if task_lower.startswith("search "):
query = task_str.split(" ", 1)[1].strip()
return "search", {"query": query}, f"Search for '{query}'"
if "email" in task_lower:
if "send" in task_lower:
return "email_send", {}, "Send email"
return "email_check", {}, "Check email"
if "freelance" in task_lower or "jobs" in task_lower:
return "money", {"action": "freelance", "params": {"skills": ["general"]}}, "Find freelance jobs"
if "arbitrage" in task_lower:
product = task_lower.replace("arbitrage", "").replace("find", "").strip() or "electronics"
return "money", {"action": "arbitrage", "params": {"product": product}}, f"Find arbitrage for {product}"
if "crypto" in task_lower:
return "money", {"action": "crypto", "params": {}}, "Check crypto opportunities"
if task_lower.startswith("read") or task_lower.startswith("check"):
return "read", {}, "Read page content"
if task_lower.startswith("click "):
selector = task_str.split(" ", 1)[1].strip()
return "click", {"selector": selector}, f"Click '{selector}'"
if task_lower.startswith("wait"):
seconds = int(re.search(r"(\d+)", task_lower).group(1)) if re.search(r"(\d+)", task_lower) else 5
return "wait", {"seconds": seconds}, f"Wait {seconds} seconds"
if "checkpoint" in task_lower or "align" in task_lower:
return "checkpoint", {}, "🔍 Alignment checkpoint"
return "custom", {"instruction": task_str}, f"AI task: {task_str[:40]}..."
def execute_chain_task(task: Dict, generate_fn) -> Tuple[bool, str]:
"""Execute a single task. Returns (success, result/error)."""
task_type = task["type"]
params = task["params"]
try:
if task_type == "login":
service = params.get("service", "")
creds = get_credential(service)
if not creds:
email = input(f"📧 Email for {service}: ").strip()
password = input(f"🔑 Password: ").strip()
store_credential(service, email, password)
creds = {"email": email, "password": password}
result = execute_login(service, creds["email"], creds["password"])
return True, result[:200]
elif task_type == "browse":
result = tool_browser_open(params.get("url", "https://google.com"))
return "[browser] Opened" in result, result
elif task_type == "search":
query = params.get("query", "")
tool_browser_open(f"https://google.com/search?q={query.replace(' ', '+')}")
time.sleep(2)
return True, f"Searched for '{query}'"
elif task_type == "email_check":
tool_browser_open("https://mail.google.com")
time.sleep(3)
return True, tool_browser_read()[:300]
elif task_type == "money":
result = execute_money_action(params["action"], params["params"], generate_fn)
return True, result[:500]
elif task_type == "read":
return True, tool_browser_read()[:500]
elif task_type == "click":
result = tool_browser_click(params.get("selector", ""))
return "Clicked" in result, result
elif task_type == "wait":
time.sleep(params.get("seconds", 5))
return True, f"Waited {params['seconds']}s"
elif task_type == "checkpoint":
return True, "CHECKPOINT"
elif task_type == "custom":
prompt = f"Execute: {params['instruction']}\n\nPage: {tool_browser_read()[:1000]}"
response, _, _ = generate_fn(prompt)
return True, response[:300]
return False, f"Unknown task: {task_type}"
except Exception as e:
return False, str(e)
def troubleshoot_task(task: Dict, error: str, generate_fn) -> str:
"""AI troubleshoots a failed task."""
prompt = f"""Task failed. Suggest fix.
Task: {task['description']}
Error: {error}
Page: {tool_browser_read()[:500] if _page else 'No page'}
Reply with ONE word: RETRY, SKIP, or ABORT"""
response, _, _ = generate_fn(prompt)
if "RETRY" in response.upper():
return "RETRY"
if "ABORT" in response.upper():
return "ABORT"
return "SKIP"
def run_task_chain(generate_fn, max_retries: int = 2) -> str:
"""Execute the task chain with checkpoints."""
global TASK_CHAIN
if not TASK_CHAIN.tasks:
return "[chain] Empty. Use 'chain: <task>' to add tasks."
print("\n" + "="*60)
print("🚀 TASK CHAIN")
print("="*60)
print(TASK_CHAIN.show())
if input("\n✅ Start? (yes/no): ").strip().lower() not in ['yes', 'y']:
return "[chain] Cancelled"
TASK_CHAIN.running = True
TASK_CHAIN.current_idx = 0
while TASK_CHAIN.current_idx < len(TASK_CHAIN.tasks):
task = TASK_CHAIN.tasks[TASK_CHAIN.current_idx]
print(f"\n[{TASK_CHAIN.current_idx + 1}/{len(TASK_CHAIN.tasks)}] {task['description']}")
task["status"] = "running"
success, result = execute_chain_task(task, generate_fn)
if success:
task["status"] = "success"
task["result"] = result
print(f"{result[:80]}...")
if result == "CHECKPOINT":
print("\n" + "="*60)
print("🔍 CHECKPOINT - Review Progress")
print("="*60)
print(TASK_CHAIN.show())
action = input("\n[c]ontinue, [a]bort: ").strip().lower()
if action == 'a':
TASK_CHAIN.running = False
return "[chain] Aborted"
else:
task["status"] = "failed"
task["error"] = result
print(f"{result[:80]}...")
if task["retries"] < max_retries:
action = troubleshoot_task(task, result, generate_fn)
print(f" 🔧 AI: {action}")
if action == "RETRY":
task["retries"] += 1
task["status"] = "pending"
continue
elif action == "ABORT":
TASK_CHAIN.running = False
return "[chain] Aborted by AI"
task["status"] = "skipped"
TASK_CHAIN.current_idx += 1
time.sleep(1)
TASK_CHAIN.running = False
print("\n✅ CHAIN COMPLETE")
return TASK_CHAIN.show()
# Workflow templates
WORKFLOW_TEMPLATES = {
"morning_hustle": [
"login gmail", "search freelance python jobs", "checkpoint",
"crypto opportunities", "checkpoint"
],
"arbitrage_hunt": [
"browse amazon.com/bestsellers", "checkpoint",
"find arbitrage electronics", "checkpoint"
],
"lead_gen": [
"find leads for contractor in miami", "checkpoint",
"login gmail", "checkpoint"
]
}
# === RECURSIVE SELF-IMPROVEMENT (RSI) MODE ===
RSI_CONFIG = {
"enabled": False,
"mode": "conservative", # conservative, balanced, aggressive
"max_iterations": 100,
"target_quality": 0.90,
"min_quality": 0.60,
"auto_rollback_threshold": 0.15, # Rollback if quality drops by this much
"training_steps_per_iter": 25,
"eval_prompts_per_iter": 10,
"generate_data_per_iter": 5, # New training examples to generate
"sleep_between_iters": 5, # Seconds
"interrupt_for_user": True, # Pause RSI when user types
"log_file": "rsi_log.jsonl",
"capabilities_to_improve": [
"density", # Response conciseness
"accuracy", # Factual correctness
"coherence", # Logical flow
"helpfulness", # Task completion
"coding", # Code generation
"reasoning", # Chain of thought
"creativity", # Novel solutions
],
"current_focus": "density",
"iteration": 0,
"total_improvements": 0,
"total_rollbacks": 0,
"best_quality_ever": 0.0,
"start_time": None,
}
RSI_RUNNING = False
RSI_PAUSED = False
def save_rsi_config():
"""Save RSI configuration."""
config_path = os.path.join(ROOT, "rsi_config.json")
with open(config_path, 'w') as f:
json.dump(RSI_CONFIG, f, indent=2)
def load_rsi_config():
"""Load RSI configuration."""
global RSI_CONFIG
config_path = os.path.join(ROOT, "rsi_config.json")
if os.path.exists(config_path):
with open(config_path, 'r') as f:
RSI_CONFIG.update(json.load(f))
load_rsi_config()
def rsi_log(event: str, data: Dict):
"""Log RSI event."""
log_path = os.path.join(LOGS_DIR, RSI_CONFIG["log_file"])
entry = {
"timestamp": datetime.now().isoformat(),
"iteration": RSI_CONFIG["iteration"],
"event": event,
"data": data
}
with open(log_path, 'a') as f:
f.write(json.dumps(entry) + "\n")
def rsi_evaluate_capability(capability: str, generate_fn) -> float:
"""Evaluate model on specific capability."""
test_prompts = {
"density": [
("Explain quantum computing", 50), # (prompt, ideal_max_tokens)
("What is machine learning?", 40),
("How does the internet work?", 60),
],
"accuracy": [
("What is 2+2?", "4"),
("What is the capital of France?", "Paris"),
("Who wrote Romeo and Juliet?", "Shakespeare"),
],
"coherence": [
"Write a 3-step process for making coffee",
"Explain why the sky is blue in logical steps",
"Describe how a car engine works",
],
"helpfulness": [
"Help me write a professional email to my boss asking for time off",
"Give me a workout routine for beginners",
"How do I fix a leaky faucet?",
],
"coding": [
"Write a Python function to reverse a string",
"Write a function to check if a number is prime",
"Write a simple web scraper in Python",
],
"reasoning": [
"If all cats are animals, and all animals need water, do cats need water? Explain.",
"A bat and ball cost $1.10. The bat costs $1 more than the ball. How much does the ball cost?",
"What comes next: 2, 4, 8, 16, ?",
],
"creativity": [
"Invent a new word and define it",
"Come up with 3 startup ideas involving AI",
"Write a haiku about programming",
],
}
prompts = test_prompts.get(capability, test_prompts["density"])
scores = []
for item in prompts:
if capability == "density":
prompt, ideal_tokens = item
response, stats, eval_result = generate_fn(prompt)
# Score based on token efficiency
token_score = max(0, 1 - (eval_result.tokens - ideal_tokens) / ideal_tokens) if eval_result.tokens > 0 else 0
scores.append((eval_result.density_score / 50 + token_score) / 2)
elif capability == "accuracy":
prompt, expected = item
response, stats, eval_result = generate_fn(prompt)
# Check if expected answer is in response
if expected.lower() in response.lower():
scores.append(1.0)
else:
scores.append(0.0)
elif capability in ["coherence", "helpfulness", "creativity"]:
prompt = item
response, stats, eval_result = generate_fn(prompt)
scores.append(eval_result.coherence_score)
elif capability == "coding":
prompt = item
response, stats, eval_result = generate_fn(prompt)
# Check for code markers
has_code = "def " in response or "function" in response or "```" in response
scores.append(0.8 if has_code else 0.3)
elif capability == "reasoning":
prompt = item
response, stats, eval_result = generate_fn(prompt)
# Check for reasoning markers
has_reasoning = any(w in response.lower() for w in ["because", "therefore", "since", "thus", "so"])
scores.append(eval_result.coherence_score * (1.2 if has_reasoning else 0.8))
return sum(scores) / len(scores) if scores else 0.5
def rsi_generate_training_data(capability: str, generate_fn) -> List[Dict]:
"""Generate new training data focused on a capability."""
prompts_for_capability = {
"density": "Generate a question and a maximally dense, concise answer (under 50 words). Format: Q: ... A: ...",
"accuracy": "Generate a factual question and its correct, precise answer. Format: Q: ... A: ...",
"coherence": "Generate a question requiring logical explanation and a well-structured answer. Format: Q: ... A: ...",
"helpfulness": "Generate a practical question someone might ask and a helpful, actionable answer. Format: Q: ... A: ...",
"coding": "Generate a coding task and clean, working Python code solution. Format: Q: ... A: ```python ... ```",
"reasoning": "Generate a logic puzzle or reasoning question and its step-by-step solution. Format: Q: ... A: ...",
"creativity": "Generate a creative prompt and an imaginative, original response. Format: Q: ... A: ...",
}
meta_prompt = prompts_for_capability.get(capability, prompts_for_capability["density"])
new_examples = []
for _ in range(RSI_CONFIG["generate_data_per_iter"]):
response, _, _ = generate_fn(meta_prompt)
# Parse Q: A: format
if "Q:" in response and "A:" in response:
parts = response.split("A:", 1)
question = parts[0].replace("Q:", "").strip()
answer = parts[1].strip()
if len(question) > 10 and len(answer) > 10:
new_examples.append({
"instruction": question,
"output": answer,
"capability": capability,
"generated": True
})
return new_examples
def rsi_identify_weakest_capability(generate_fn) -> Tuple[str, float]:
"""Identify the weakest capability to focus on."""
scores = {}
for cap in RSI_CONFIG["capabilities_to_improve"]:
score = rsi_evaluate_capability(cap, generate_fn)
scores[cap] = score
print(f" {cap}: {score:.2f}")
weakest = min(scores, key=scores.get)
return weakest, scores[weakest]
def rsi_run_iteration(generate_fn, train_fn, eval_fn, reload_fn) -> Dict:
"""Run a single RSI iteration."""
global RSI_CONFIG
iteration = RSI_CONFIG["iteration"]
print(f"\n{'='*60}")
print(f"🔄 RSI ITERATION {iteration}")
print(f"{'='*60}")
result = {
"iteration": iteration,
"success": False,
"quality_before": 0,
"quality_after": 0,
"focus": "",
"action": "",
}
# Step 1: Evaluate current state
print("\n[RSI] 📊 Evaluating current capabilities...")
weakest_cap, weakest_score = rsi_identify_weakest_capability(generate_fn)
RSI_CONFIG["current_focus"] = weakest_cap
result["focus"] = weakest_cap
print(f"\n[RSI] 🎯 Focus: {weakest_cap} (score: {weakest_score:.2f})")
# Step 2: Get baseline quality
print("\n[RSI] 📏 Baseline evaluation...")
baseline = eval_fn()
result["quality_before"] = baseline.get("avg_quality", 0)
print(f"[RSI] Baseline quality: {result['quality_before']:.3f}")
# Step 3: Generate new training data
print(f"\n[RSI] 📝 Generating training data for {weakest_cap}...")
new_data = rsi_generate_training_data(weakest_cap, generate_fn)
print(f"[RSI] Generated {len(new_data)} new examples")
# Add to training set
if new_data:
for example in new_data:
DENSE_TRAINING_EXAMPLES.append(example)
# Step 4: Train
print(f"\n[RSI] 🏋️ Training ({RSI_CONFIG['training_steps_per_iter']} steps)...")
train_result = train_fn(RSI_CONFIG["training_steps_per_iter"])
# Step 5: Reload and evaluate
print("\n[RSI] 🔄 Reloading model...")
reload_fn()
print("\n[RSI] 📊 Post-training evaluation...")
post_eval = eval_fn()
result["quality_after"] = post_eval.get("avg_quality", 0)
print(f"[RSI] New quality: {result['quality_after']:.3f}")
# Step 6: Decide keep or rollback
quality_diff = result["quality_after"] - result["quality_before"]
if quality_diff < -RSI_CONFIG["auto_rollback_threshold"]:
print(f"\n[RSI] ⚠️ Quality dropped by {-quality_diff:.3f} - ROLLING BACK")
# Rollback logic would go here
result["action"] = "rollback"
RSI_CONFIG["total_rollbacks"] += 1
elif quality_diff > 0:
print(f"\n[RSI] ✅ Quality improved by {quality_diff:.3f} - KEEPING")
result["action"] = "keep"
result["success"] = True
RSI_CONFIG["total_improvements"] += 1
if result["quality_after"] > RSI_CONFIG["best_quality_ever"]:
RSI_CONFIG["best_quality_ever"] = result["quality_after"]
else:
print(f"\n[RSI] ➡️ Quality unchanged - KEEPING")
result["action"] = "keep"
# Log
rsi_log("iteration_complete", result)
RSI_CONFIG["iteration"] += 1
save_rsi_config()
return result
def rsi_mode_loop(generate_fn, train_fn, eval_fn, reload_fn):
"""Main RSI loop - runs until stopped."""
global RSI_RUNNING, RSI_PAUSED, RSI_CONFIG
RSI_RUNNING = True
RSI_PAUSED = False
RSI_CONFIG["start_time"] = datetime.now().isoformat()
print("\n" + "="*60)
print("🚀 RSI MODE ACTIVATED")
print("="*60)
print(f" Mode: {RSI_CONFIG['mode']}")
print(f" Target quality: {RSI_CONFIG['target_quality']}")
print(f" Max iterations: {RSI_CONFIG['max_iterations']}")
print(f" Focus areas: {', '.join(RSI_CONFIG['capabilities_to_improve'])}")
print("="*60)
print("\n⚠️ Press Ctrl+C to pause/stop RSI mode")
print("="*60)
save_rsi_config()
try:
while RSI_RUNNING and RSI_CONFIG["iteration"] < RSI_CONFIG["max_iterations"]:
if RSI_PAUSED:
print("\n[RSI] ⏸️ Paused. Type 'rsi resume' to continue.")
break
# Run iteration
result = rsi_run_iteration(generate_fn, train_fn, eval_fn, reload_fn)
# Check if target reached
if result["quality_after"] >= RSI_CONFIG["target_quality"]:
print(f"\n[RSI] 🎉 TARGET REACHED! Quality: {result['quality_after']:.3f}")
break
# Check for catastrophic failure
if result["quality_after"] < RSI_CONFIG["min_quality"]:
print(f"\n[RSI] 🛑 Quality too low ({result['quality_after']:.3f}) - STOPPING")
break
# Sleep between iterations
print(f"\n[RSI] 💤 Sleeping {RSI_CONFIG['sleep_between_iters']}s...")
time.sleep(RSI_CONFIG["sleep_between_iters"])
except KeyboardInterrupt:
print("\n\n[RSI] ⏸️ Interrupted by user")
RSI_PAUSED = True
RSI_RUNNING = False
# Final report
print("\n" + "="*60)
print("📊 RSI SESSION REPORT")
print("="*60)
print(f" Iterations: {RSI_CONFIG['iteration']}")
print(f" Improvements: {RSI_CONFIG['total_improvements']}")
print(f" Rollbacks: {RSI_CONFIG['total_rollbacks']}")
print(f" Best quality: {RSI_CONFIG['best_quality_ever']:.3f}")
print(f" Current focus: {RSI_CONFIG['current_focus']}")
print("="*60)
save_rsi_config()
def rsi_status() -> str:
"""Get RSI status."""
status = []
status.append("=" * 50)
status.append("🧠 RSI STATUS")
status.append("=" * 50)
status.append(f"Running: {'🟢 YES' if RSI_RUNNING else '🔴 NO'}")
status.append(f"Paused: {'⏸️ YES' if RSI_PAUSED else 'NO'}")
status.append(f"Mode: {RSI_CONFIG['mode']}")
status.append(f"Iteration: {RSI_CONFIG['iteration']}")
status.append(f"Target quality: {RSI_CONFIG['target_quality']}")
status.append(f"Best quality ever: {RSI_CONFIG['best_quality_ever']:.3f}")
status.append(f"Total improvements: {RSI_CONFIG['total_improvements']}")
status.append(f"Total rollbacks: {RSI_CONFIG['total_rollbacks']}")
status.append(f"Current focus: {RSI_CONFIG['current_focus']}")
status.append(f"Capabilities: {', '.join(RSI_CONFIG['capabilities_to_improve'])}")
return "\n".join(status)
# === CRYPTO MINING AUTOMATION ===
MINING_CONFIG = {
"enabled": False,
"wallet_addresses": {}, # coin -> address
"preferred_algo": "randomx", # randomx, kawpow, ethash, etc
"max_cpu_percent": 80,
"max_gpu_percent": 90,
"auto_switch": True, # Auto switch to most profitable
"min_profitability": 0.10, # USD/day minimum
"miner_path": "",
"pool_urls": {},
}
SUPPORTED_MINERS = {
"xmrig": {
"coins": ["XMR", "RTM", "DERO"],
"algo": "randomx",
"url": "https://github.com/xmrig/xmrig/releases",
"config_template": {
"pools": [{"url": "{pool}", "user": "{wallet}", "pass": "x"}],
"cpu": {"max-threads-hint": 80}
}
},
"trex": {
"coins": ["RVN", "FLUX", "ERG", "NEOX"],
"algo": "kawpow",
"url": "https://github.com/trexminer/T-Rex/releases",
"cmd": "./t-rex -a {algo} -o {pool} -u {wallet} -p x"
},
"gminer": {
"coins": ["FLUX", "ERG", "KASPA", "ALPH"],
"algo": "equihash",
"url": "https://github.com/develsoftware/GMinerRelease/releases",
},
"cpuminer": {
"coins": ["VRSC", "RTM", "YEC"],
"algo": "verushash",
"url": "https://github.com/VerusCoin/nheqminer/releases",
},
"srbminer": {
"coins": ["RTM", "XEL", "RYO", "CCX"],
"algo": "ghostrider",
"url": "https://github.com/doktor83/SRBMiner-Multi/releases",
}
}
# Low difficulty / new coins to check
LOW_DIFF_COINS = [
{"symbol": "RTM", "name": "Raptoreum", "algo": "ghostrider", "pool": "stratum+tcp://stratum.raptoreum.com:3333"},
{"symbol": "VRSC", "name": "Verus", "algo": "verushash", "pool": "stratum+tcp://na.luckpool.net:3956"},
{"symbol": "XEL", "name": "Elastic", "algo": "randomx", "pool": "stratum+tcp://pool.xel.org:3333"},
{"symbol": "RYO", "name": "Ryo", "algo": "randomx", "pool": "stratum+tcp://pool.ryo-currency.com:3333"},
{"symbol": "CCX", "name": "Conceal", "algo": "randomx", "pool": "stratum+tcp://pool.conceal.network:3333"},
{"symbol": "DERO", "name": "Dero", "algo": "astroBWT", "pool": "stratum+tcp://dero.herominers.com:1111"},
{"symbol": "NEOX", "name": "Neoxa", "algo": "kawpow", "pool": "stratum+tcp://pool.woolypooly.com:3124"},
{"symbol": "CLORE", "name": "Clore", "algo": "kawpow", "pool": "stratum+tcp://pool.woolypooly.com:3136"},
{"symbol": "KASPA", "name": "Kaspa", "algo": "kHeavyHash", "pool": "stratum+tcp://pool.woolypooly.com:3112"},
{"symbol": "ALPH", "name": "Alephium", "algo": "blake3", "pool": "stratum+tcp://pool.woolypooly.com:3106"},
]
_mining_process = None
def save_mining_config():
"""Save mining configuration."""
config_path = os.path.join(ROOT, "mining_config.json")
with open(config_path, 'w') as f:
json.dump(MINING_CONFIG, f, indent=2)
print(f"[mining] ✅ Config saved")
def load_mining_config():
"""Load mining configuration."""
global MINING_CONFIG
config_path = os.path.join(ROOT, "mining_config.json")
if os.path.exists(config_path):
with open(config_path, 'r') as f:
MINING_CONFIG.update(json.load(f))
load_mining_config()
def check_mining_profitability(generate_fn) -> str:
"""Check current profitability of low-diff coins."""
print("[mining] 🔍 Checking profitability...")
# Fetch from WhatToMine
tool_browser_open("https://whattomine.com/coins")
time.sleep(3)
wtm_content = tool_browser_read()
# Fetch from MiningPoolStats
tool_browser_open("https://miningpoolstats.stream/")
time.sleep(2)
mps_content = tool_browser_read()
prompt = f"""Analyze mining profitability for small/new coins:
WhatToMine data: {wtm_content[:2000]}
MiningPoolStats: {mps_content[:1500]}
Focus on these low-difficulty coins: {[c['symbol'] for c in LOW_DIFF_COINS]}
For each promising coin:
1. Coin name and symbol
2. Algorithm
3. Current difficulty trend (rising/falling)
4. Network hashrate
5. Estimated daily profit (USD) for average GPU/CPU
6. Pool recommendation
7. Overall rating (1-10)
Sort by profitability. Recommend the BEST coin to mine right now."""
response, _, _ = generate_fn(prompt)
return response
def find_lowest_difficulty_coins(generate_fn) -> str:
"""Find new/low difficulty coins to mine."""
print("[mining] 🔍 Searching for low difficulty coins...")
# Check multiple sources
sources = [
"https://miningpoolstats.stream/",
"https://whattomine.com/coins?factor%5Bcost%5D=0.1",
"https://minerstat.com/coin",
]
all_data = []
for url in sources:
tool_browser_open(url)
time.sleep(3)
all_data.append(tool_browser_read()[:1500])
prompt = f"""Find the LOWEST DIFFICULTY minable coins right now.
Source 1: {all_data[0]}
Source 2: {all_data[1]}
Source 3: {all_data[2] if len(all_data) > 2 else 'N/A'}
Looking for:
- New coins (< 6 months old)
- Low network hashrate
- CPU or GPU minable
- Has working pools
- Can be traded somewhere
List top 5 opportunities:
1. Coin name, symbol
2. Algorithm
3. Why it's easy to mine
4. Pool URL
5. Exchange where tradeable
6. Risk level (low/med/high)"""
response, _, _ = generate_fn(prompt)
return response
def setup_miner(miner: str, coin: str, wallet: str, pool: str) -> str:
"""Download and configure miner."""
if miner not in SUPPORTED_MINERS:
return f"[mining] ❌ Unknown miner: {miner}. Supported: {list(SUPPORTED_MINERS.keys())}"
miner_info = SUPPORTED_MINERS[miner]
miner_dir = os.path.join(ROOT, "miners", miner)
os.makedirs(miner_dir, exist_ok=True)
print(f"[mining] Setting up {miner} for {coin}...")
# Check if already installed
miner_exe = os.path.join(miner_dir, miner)
if not os.path.exists(miner_exe):
print(f"[mining] ⚠️ Miner not found at {miner_dir}")
print(f"[mining] Download from: {miner_info['url']}")
print(f"[mining] Extract to: {miner_dir}")
return f"[mining] Please download {miner} manually and extract to {miner_dir}"
# Save wallet
MINING_CONFIG["wallet_addresses"][coin] = wallet
MINING_CONFIG["pool_urls"][coin] = pool
save_mining_config()
# Create config
if miner == "xmrig":
config = {
"pools": [{"url": pool, "user": wallet, "pass": "x", "coin": coin.lower()}],
"cpu": {"max-threads-hint": MINING_CONFIG["max_cpu_percent"]}
}
config_path = os.path.join(miner_dir, "config.json")
with open(config_path, 'w') as f:
json.dump(config, f, indent=2)
return f"[mining] ✅ XMRig configured for {coin}. Config at {config_path}"
return f"[mining] ✅ {miner} configured for {coin}"
def start_mining(miner: str = "xmrig", coin: str = None) -> str:
"""Start mining process."""
global _mining_process
if _mining_process and _mining_process.poll() is None:
return "[mining] ⚠️ Already mining. Use 'stop mining' first."
miner_dir = os.path.join(ROOT, "miners", miner)
if miner == "xmrig":
exe = os.path.join(miner_dir, "xmrig")
if not os.path.exists(exe):
return f"[mining] ❌ XMRig not found at {exe}"
cmd = [exe, "-c", os.path.join(miner_dir, "config.json")]
elif miner == "trex":
exe = os.path.join(miner_dir, "t-rex")
if not os.path.exists(exe):
return f"[mining] ❌ T-Rex not found at {exe}"
wallet = MINING_CONFIG["wallet_addresses"].get(coin, "")
pool = MINING_CONFIG["pool_urls"].get(coin, "")
cmd = [exe, "-a", "kawpow", "-o", pool, "-u", wallet, "-p", "x"]
else:
return f"[mining] ❌ Miner start not implemented for {miner}"
try:
_mining_process = subprocess.Popen(
cmd,
stdout=subprocess.PIPE,
stderr=subprocess.STDOUT,
cwd=miner_dir
)
MINING_CONFIG["enabled"] = True
save_mining_config()
return f"[mining] ✅ Started {miner} (PID: {_mining_process.pid})"
except Exception as e:
return f"[mining] ❌ Failed to start: {e}"
def stop_mining() -> str:
"""Stop mining process."""
global _mining_process
if _mining_process:
_mining_process.terminate()
_mining_process.wait(timeout=10)
_mining_process = None
MINING_CONFIG["enabled"] = False
save_mining_config()
return "[mining] ⏹️ Mining stopped"
# Try to kill any running miners
os.system("pkill -f xmrig 2>/dev/null")
os.system("pkill -f t-rex 2>/dev/null")
return "[mining] ⏹️ Sent stop signal"
def mining_status() -> str:
"""Check mining status."""
global _mining_process
status = []
status.append("=" * 50)
status.append("⛏️ MINING STATUS")
status.append("=" * 50)
if _mining_process and _mining_process.poll() is None:
status.append(f"Status: 🟢 RUNNING (PID: {_mining_process.pid})")
else:
status.append("Status: 🔴 STOPPED")
status.append(f"\nWallets configured:")
for coin, addr in MINING_CONFIG.get("wallet_addresses", {}).items():
status.append(f" {coin}: {addr[:20]}...")
status.append(f"\nPools:")
for coin, pool in MINING_CONFIG.get("pool_urls", {}).items():
status.append(f" {coin}: {pool}")
status.append(f"\nSettings:")
status.append(f" Max CPU: {MINING_CONFIG['max_cpu_percent']}%")
status.append(f" Max GPU: {MINING_CONFIG['max_gpu_percent']}%")
status.append(f" Auto-switch: {MINING_CONFIG['auto_switch']}")
return "\n".join(status)
def create_local_wallet(coin: str) -> str:
"""Create local wallet for a coin."""
wallet_dir = os.path.join(ROOT, "wallets")
os.makedirs(wallet_dir, exist_ok=True)
if coin.upper() in ["XMR", "MONERO"]:
# Check if monero-wallet-cli exists
result = subprocess.run(["which", "monero-wallet-cli"], capture_output=True)
if result.returncode != 0:
return f"""[wallet] ❌ Monero CLI not found
Install: sudo apt install monero
Or download from: https://getmonero.org/downloads/"""
wallet_path = os.path.join(wallet_dir, f"xmr_wallet")
print(f"[wallet] Creating Monero wallet at {wallet_path}")
print("[wallet] ⚠️ SAVE YOUR SEED PHRASE!")
# This would need interactive input - guide user
return f"""[wallet] To create Monero wallet:
1. Run: monero-wallet-cli --generate-new-wallet {wallet_path}
2. Set a password
3. SAVE THE 25-WORD SEED PHRASE
4. Get address with 'address' command
5. Add to mining: !mine wallet XMR <your_address>"""
elif coin.upper() in ["RVN", "RAVENCOIN"]:
return f"""[wallet] For Ravencoin:
1. Download: https://ravencoin.org/wallet/
2. Or use CLI: raven-cli getnewaddress
3. Add to mining: !mine wallet RVN <your_address>"""
elif coin.upper() in ["RTM", "RAPTOREUM"]:
return f"""[wallet] For Raptoreum:
1. Download: https://raptoreum.com/
2. Create new wallet in GUI
3. Get receiving address
4. Add to mining: !mine wallet RTM <your_address>"""
else:
return f"""[wallet] For {coin}:
1. Search for official {coin} wallet
2. Create new wallet
3. Get receiving address
4. Add to mining: !mine wallet {coin} <your_address>"""
def auto_mine_best_coin(generate_fn) -> str:
"""Automatically find and mine the most profitable coin."""
print("[mining] 🤖 Auto-mining: Finding best coin...")
# Check profitability
profit_analysis = check_mining_profitability(generate_fn)
print(profit_analysis)
# Ask model to pick the best
prompt = f"""Based on this analysis, which coin should we mine?
{profit_analysis}
Current configured wallets: {list(MINING_CONFIG.get('wallet_addresses', {}).keys())}
Reply with JUST the coin symbol (e.g., RTM, XMR, RVN) or "NONE" if nothing profitable."""
response, _, _ = generate_fn(prompt)
coin = response.strip().upper().split()[0]
if coin == "NONE" or len(coin) > 10:
return "[mining] No profitable coin found. Try later."
# Check if we have wallet for this coin
if coin not in MINING_CONFIG.get("wallet_addresses", {}):
return f"[mining] ⚠️ No wallet for {coin}. Set one with: !mine wallet {coin} <address>"
# Find miner for this coin
miner = None
for m, info in SUPPORTED_MINERS.items():
if coin in info["coins"]:
miner = m
break
if not miner:
return f"[mining] ❌ No supported miner for {coin}"
# Start mining
return start_mining(miner, coin)
# === MONEY-MAKING AUTOMATION ===
INCOME_STRATEGIES = {
"freelance": {
"description": "Find and apply to freelance jobs",
"platforms": ["upwork", "fiverr", "freelancer", "toptal"],
"skills_needed": ["writing", "coding", "design", "data entry", "virtual assistant"]
},
"arbitrage": {
"description": "Find price differences between platforms",
"platforms": ["ebay", "amazon", "walmart", "aliexpress"],
},
"content": {
"description": "Create and monetize content",
"platforms": ["medium", "substack", "youtube", "tiktok"],
},
"surveys": {
"description": "Complete paid surveys and tasks",
"platforms": ["swagbucks", "mturk", "prolific"],
},
"dropshipping": {
"description": "Find trending products to sell",
"platforms": ["aliexpress", "shopify", "ebay"],
}
}
def analyze_freelance_jobs(page_content: str, generate_fn) -> str:
"""Analyze freelance job listings and recommend best matches."""
prompt = f"""Analyze these job listings and identify:
1. Top 3 highest paying jobs
2. Jobs matching common skills (writing, coding, data entry)
3. Quick wins (easy jobs, fast turnaround)
Job listings:
{page_content[:3000]}
Provide actionable recommendations."""
response, _, _ = generate_fn(prompt)
return response
def analyze_arbitrage_opportunity(product: str, generate_fn) -> str:
"""Find price arbitrage opportunities."""
results = {}
platforms = [
("amazon", f"https://www.amazon.com/s?k={product.replace(' ', '+')}"),
("ebay", f"https://www.ebay.com/sch/i.html?_nkw={product.replace(' ', '+')}"),
("walmart", f"https://www.walmart.com/search?q={product.replace(' ', '+')}"),
]
for platform, url in platforms:
tool_browser_open(url)
time.sleep(3)
content = tool_browser_read()
results[platform] = content
prompt = f"""Analyze prices for "{product}" and find arbitrage opportunities:
Amazon: {results.get('amazon', 'N/A')[:1000]}
eBay: {results.get('ebay', 'N/A')[:1000]}
Walmart: {results.get('walmart', 'N/A')[:1000]}
Identify: lowest price, highest sell price, profit margin, recommendation."""
response, _, _ = generate_fn(prompt)
return response
def find_trending_products(generate_fn) -> str:
"""Find trending products to sell."""
sources = [
"https://trends.google.com/trending?geo=US",
"https://www.amazon.com/Best-Sellers/zgbs",
]
all_content = []
for url in sources:
tool_browser_open(url)
time.sleep(3)
content = tool_browser_read()
all_content.append(content[:1500])
prompt = f"""Identify 5 products with high profit potential:
{chr(10).join(all_content)}
For each: name, why trending, profit margin, best platform, competition level."""
response, _, _ = generate_fn(prompt)
return response
def auto_apply_jobs(platform: str, skills: list, generate_fn) -> str:
"""Find and prepare job applications."""
if platform == "upwork":
url = f"https://www.upwork.com/nx/jobs/search/?q={'+'.join(skills)}"
elif platform == "fiverr":
url = f"https://www.fiverr.com/search/gigs?query={'+'.join(skills)}"
else:
url = f"https://www.indeed.com/jobs?q={'+'.join(skills)}"
tool_browser_open(url)
time.sleep(3)
content = tool_browser_read()
prompt = f"""Analyze jobs for skills: {', '.join(skills)}
{content[:3000]}
For promising jobs: title, pay, requirements match, draft proposal, priority."""
response, _, _ = generate_fn(prompt)
return response
def generate_content_ideas(niche: str, generate_fn) -> str:
"""Generate monetizable content ideas."""
tool_browser_open(f"https://www.google.com/search?q={niche}+trending+2025")
time.sleep(2)
trends = tool_browser_read()
prompt = f"""Generate 10 monetizable content ideas for "{niche}":
{trends[:2000]}
For each: title, content type, monetization, difficulty."""
response, _, _ = generate_fn(prompt)
return response
# === LEAD GENERATION ===
def scrape_business_leads(industry: str, location: str, generate_fn) -> str:
"""Scrape business directories for leads."""
leads_data = []
# Search Yellow Pages
tool_browser_open(f"https://www.yellowpages.com/search?search_terms={industry.replace(' ', '+')}&geo_location_terms={location.replace(' ', '+')}")
time.sleep(3)
yp_content = tool_browser_read()
leads_data.append(("Yellow Pages", yp_content))
# Search Yelp
tool_browser_open(f"https://www.yelp.com/search?find_desc={industry.replace(' ', '+')}&find_loc={location.replace(' ', '+')}")
time.sleep(3)
yelp_content = tool_browser_read()
leads_data.append(("Yelp", yelp_content))
# Search Google Maps
tool_browser_open(f"https://www.google.com/maps/search/{industry.replace(' ', '+')}+{location.replace(' ', '+')}")
time.sleep(3)
maps_content = tool_browser_read()
leads_data.append(("Google Maps", maps_content))
prompt = f"""Extract business leads from these directories for "{industry}" in "{location}":
Yellow Pages: {leads_data[0][1][:1500]}
Yelp: {leads_data[1][1][:1500]}
Google Maps: {leads_data[2][1][:1500]}
For each business provide:
1. Business name
2. Phone number
3. Address
4. Website (if found)
5. Rating/reviews
6. Lead quality score (1-10)
Format as a table."""
response, _, _ = generate_fn(prompt)
return response
# === SOCIAL MEDIA AUTOMATION ===
SOCIAL_PLATFORMS = {
"twitter": {
"compose_url": "https://twitter.com/compose/tweet",
"post_selector": "div[data-testid='tweetTextarea_0']",
"submit_selector": "div[data-testid='tweetButton']",
},
"linkedin": {
"compose_url": "https://www.linkedin.com/feed/",
"post_selector": "div.share-box-feed-entry__trigger",
"text_selector": "div.ql-editor",
"submit_selector": "button.share-actions__primary-action",
},
"facebook": {
"compose_url": "https://www.facebook.com/",
"post_selector": "div[aria-label='Create a post']",
"text_selector": "div[aria-label=\"What's on your mind?\"]",
"submit_selector": "div[aria-label='Post']",
},
"reddit": {
"compose_url": "https://www.reddit.com/submit",
"title_selector": "textarea[name='title']",
"text_selector": "div[data-testid='TextPostCreation']",
"submit_selector": "button[type='submit']",
}
}
def schedule_social_post(platform: str, content: str, generate_fn) -> str:
"""Post content to social media platform."""
if platform not in SOCIAL_PLATFORMS:
return f"[social] Unknown platform: {platform}. Available: {', '.join(SOCIAL_PLATFORMS.keys())}"
config = SOCIAL_PLATFORMS[platform]
print(f"\n[social] Posting to {platform}...")
tool_browser_open(config["compose_url"])
time.sleep(3)
# Try to click compose button if needed
if "post_selector" in config:
try:
tool_browser_click(config["post_selector"])
time.sleep(1)
except:
pass
# Type content
text_selector = config.get("text_selector", config.get("post_selector"))
tool_browser_type(content, text_selector)
time.sleep(1)
return f"[social] Content ready to post on {platform}. Review in browser and click Post to confirm."
def generate_social_content(topic: str, platform: str, generate_fn) -> str:
"""Generate engaging social media content."""
prompt = f"""Generate an engaging {platform} post about "{topic}".
Requirements:
- Optimized for {platform} algorithm
- Include relevant hashtags
- Call to action
- Keep within character limits
- Make it viral-worthy
Generate 3 variations."""
response, _, _ = generate_fn(prompt)
return response
def engage_social_media(platform: str, action: str, generate_fn) -> str:
"""Auto-engage on social media (like, comment, follow)."""
if platform == "twitter":
tool_browser_open("https://twitter.com/home")
elif platform == "linkedin":
tool_browser_open("https://www.linkedin.com/feed/")
elif platform == "reddit":
tool_browser_open("https://www.reddit.com/")
time.sleep(3)
content = tool_browser_read()
prompt = f"""Analyze this {platform} feed and suggest engagement actions:
{content[:2500]}
For each post worth engaging with:
1. Post summary
2. Suggested comment (authentic, not spammy)
3. Should like? (yes/no)
4. Should follow author? (yes/no)
5. Engagement value score (1-10)
Focus on posts that could lead to networking or business opportunities."""
response, _, _ = generate_fn(prompt)
return response
# === SURVEY/TASK AUTOMATION ===
SURVEY_SITES = {
"swagbucks": "https://www.swagbucks.com/surveys",
"mturk": "https://worker.mturk.com/",
"prolific": "https://app.prolific.co/",
"survey_junkie": "https://www.surveyjunkie.com/",
"clickworker": "https://www.clickworker.com/",
}
def find_surveys(generate_fn) -> str:
"""Find available paid surveys and tasks."""
all_surveys = []
for site, url in list(SURVEY_SITES.items())[:3]: # Check top 3
print(f"[surveys] Checking {site}...")
tool_browser_open(url)
time.sleep(3)
content = tool_browser_read()
all_surveys.append((site, content[:1000]))
prompt = f"""Analyze available surveys and tasks:
{chr(10).join([f'{site}: {content}' for site, content in all_surveys])}
List available opportunities:
1. Survey/task name
2. Estimated pay
3. Time required
4. Hourly rate equivalent
5. Difficulty (easy/medium/hard)
6. Worth it? (yes/no)
Sort by hourly rate descending."""
response, _, _ = generate_fn(prompt)
return response
def auto_qualify_survey(generate_fn) -> str:
"""Help qualify for surveys by suggesting optimal answers."""
content = tool_browser_read()
prompt = f"""This is a survey qualification page:
{content[:2000]}
Analyze the questions and suggest answers most likely to qualify for paid surveys.
Note: Be ethical - don't lie about demographics, but optimize presentation.
For each question:
1. Question text
2. Suggested answer
3. Why this answer qualifies"""
response, _, _ = generate_fn(prompt)
return response
# === CRYPTO/STOCK MONITORING ===
WATCHLIST = []
def add_to_watchlist(symbol: str, target_price: float, direction: str):
"""Add asset to price watchlist."""
WATCHLIST.append({
"symbol": symbol.upper(),
"target": target_price,
"direction": direction, # "above" or "below"
"added": time.time()
})
return f"[watchlist] Added {symbol.upper()} - alert when {'>' if direction == 'above' else '<'} ${target_price}"
def check_prices(generate_fn) -> str:
"""Check current prices for watchlist items."""
if not WATCHLIST:
return "[watchlist] Empty. Use 'watch BTC above 50000' to add items."
results = []
alerts = []
for item in WATCHLIST:
symbol = item["symbol"]
# Check if crypto or stock
if symbol in ["BTC", "ETH", "SOL", "DOGE", "XRP", "ADA"]:
tool_browser_open(f"https://coinmarketcap.com/currencies/{symbol.lower()}/")
else:
tool_browser_open(f"https://finance.yahoo.com/quote/{symbol}")
time.sleep(2)
content = tool_browser_read()
results.append((symbol, content[:500]))
# Check for alert condition
# (Model will analyze if target hit)
prompt = f"""Check these assets against watchlist targets:
Watchlist: {json.dumps(WATCHLIST, indent=2)}
Current data:
{chr(10).join([f'{sym}: {data}' for sym, data in results])}
For each asset:
1. Current price
2. Target price
3. Distance to target (%)
4. ALERT if target reached
5. Recommendation (buy/hold/sell)"""
response, _, _ = generate_fn(prompt)
return response
def crypto_opportunities(generate_fn) -> str:
"""Find crypto trading opportunities."""
tool_browser_open("https://coinmarketcap.com/")
time.sleep(2)
cmc_content = tool_browser_read()
tool_browser_open("https://coinmarketcap.com/trending-cryptocurrencies/")
time.sleep(2)
trending = tool_browser_read()
prompt = f"""Analyze crypto market for opportunities:
Market Overview: {cmc_content[:1500]}
Trending: {trending[:1500]}
Identify:
1. Top 3 coins with momentum
2. Oversold coins (potential bounce)
3. New listings worth watching
4. Risk assessment for each
5. Entry/exit price suggestions
Disclaimer: Not financial advice."""
response, _, _ = generate_fn(prompt)
return response
# === AUTO-APPLY TO JOBS ===
def full_auto_apply(platform: str, skills: list, max_applications: int, generate_fn) -> str:
"""Fully automated job application process."""
applications_sent = 0
results = []
# Search jobs
if platform == "upwork":
url = f"https://www.upwork.com/nx/jobs/search/?q={'+'.join(skills)}&sort=recency"
elif platform == "indeed":
url = f"https://www.indeed.com/jobs?q={'+'.join(skills)}&sort=date"
else:
url = f"https://www.linkedin.com/jobs/search/?keywords={'+'.join(skills)}"
tool_browser_open(url)
time.sleep(3)
# Get job listings
content = tool_browser_read()
prompt = f"""Analyze these job listings and prepare applications:
{content[:3000]}
For the top {max_applications} most suitable jobs:
1. Job title and company
2. Why I'm a good fit
3. Personalized cover letter (3-4 sentences)
4. Key points to highlight
5. Red flags (if any)
Skills: {', '.join(skills)}"""
response, _, _ = generate_fn(prompt)
return f"""[auto-apply] Prepared {max_applications} applications
{response}
⚠️ Review each application before submitting.
Use '!click Apply' to start applying to visible jobs."""
# === LISTING CREATION ===
def create_ebay_listing(product: str, generate_fn) -> str:
"""Create optimized eBay listing."""
# Research similar listings
tool_browser_open(f"https://www.ebay.com/sch/i.html?_nkw={product.replace(' ', '+')}&_sop=12")
time.sleep(3)
similar = tool_browser_read()
# Research sold prices
tool_browser_open(f"https://www.ebay.com/sch/i.html?_nkw={product.replace(' ', '+')}&LH_Complete=1&LH_Sold=1")
time.sleep(3)
sold = tool_browser_read()
prompt = f"""Create an optimized eBay listing for "{product}":
Similar active listings: {similar[:1500]}
Recent sold prices: {sold[:1500]}
Generate:
1. SEO-optimized title (80 chars max)
2. Suggested price (competitive)
3. Best category
4. 5 key features/bullet points
5. Full description (persuasive, detailed)
6. Suggested shipping options
7. Best time to list
8. Expected sell-through rate"""
response, _, _ = generate_fn(prompt)
return response
def create_amazon_listing(product: str, generate_fn) -> str:
"""Create optimized Amazon listing content."""
# Research competition
tool_browser_open(f"https://www.amazon.com/s?k={product.replace(' ', '+')}")
time.sleep(3)
competition = tool_browser_read()
prompt = f"""Create Amazon listing content for "{product}":
Competition: {competition[:2000]}
Generate:
1. Product title (200 chars, keyword-rich)
2. 5 bullet points (features & benefits)
3. Product description (1000+ words, A+ content style)
4. Backend keywords (hidden search terms)
5. Suggested price point
6. Main image requirements
7. A+ Content module suggestions"""
response, _, _ = generate_fn(prompt)
return response
def find_dropship_products(generate_fn) -> str:
"""Find profitable dropshipping products."""
# Check AliExpress trending
tool_browser_open("https://www.aliexpress.com/popular.html")
time.sleep(3)
ali_trending = tool_browser_read()
# Check Amazon best sellers
tool_browser_open("https://www.amazon.com/Best-Sellers/zgbs")
time.sleep(3)
amazon_best = tool_browser_read()
# Check eBay trending
tool_browser_open("https://www.ebay.com/trending")
time.sleep(3)
ebay_trending = tool_browser_read()
prompt = f"""Find profitable dropshipping opportunities:
AliExpress Trending: {ali_trending[:1500]}
Amazon Best Sellers: {amazon_best[:1500]}
eBay Trending: {ebay_trending[:1500]}
For top 10 products:
1. Product name
2. AliExpress price (source)
3. Amazon/eBay price (sell)
4. Profit margin
5. Competition level (low/med/high)
6. Shipping time concern
7. Recommendation (yes/no/maybe)
Sort by profit potential."""
response, _, _ = generate_fn(prompt)
return response
def detect_money_task(user_input: str) -> Tuple[Optional[str], Optional[Dict]]:
"""Detect money-making related requests."""
user_lower = user_input.lower()
# Freelance job search
if any(kw in user_lower for kw in ["freelance", "upwork", "fiverr", "find jobs", "find work", "gig economy"]):
skills = []
for skill in ["writing", "coding", "python", "design", "data entry", "virtual assistant", "web", "marketing"]:
if skill in user_lower:
skills.append(skill)
return "freelance", {"skills": skills if skills else ["general"]}
# Auto-apply
if any(kw in user_lower for kw in ["auto apply", "auto-apply", "apply to jobs", "mass apply"]):
skills = []
for skill in ["writing", "coding", "python", "design", "data entry", "web", "marketing"]:
if skill in user_lower:
skills.append(skill)
return "auto_apply", {"skills": skills if skills else ["general"], "max": 5}
# Lead generation
if any(kw in user_lower for kw in ["leads", "find leads", "business leads", "scrape businesses", "find clients"]):
industry = "small business"
location = "new york"
for word in ["plumber", "lawyer", "dentist", "restaurant", "contractor", "realtor", "doctor"]:
if word in user_lower:
industry = word
for city in ["new york", "los angeles", "chicago", "houston", "miami", "seattle", "denver"]:
if city in user_lower:
location = city
return "leads", {"industry": industry, "location": location}
# Social media
if any(kw in user_lower for kw in ["post to twitter", "post to linkedin", "post to facebook", "social media post"]):
platform = "twitter"
for p in ["linkedin", "facebook", "reddit", "twitter"]:
if p in user_lower:
platform = p
return "social_post", {"platform": platform}
if any(kw in user_lower for kw in ["engage social", "social engagement", "auto engage", "grow followers"]):
platform = "twitter"
for p in ["linkedin", "facebook", "reddit", "twitter"]:
if p in user_lower:
platform = p
return "social_engage", {"platform": platform}
if any(kw in user_lower for kw in ["generate post", "write tweet", "write post", "content for"]):
platform = "twitter"
for p in ["linkedin", "facebook", "reddit", "twitter"]:
if p in user_lower:
platform = p
topic = re.sub(r"(generate|write|post|tweet|content|for|about|on|linkedin|twitter|facebook|reddit)", "", user_lower).strip()
return "social_content", {"platform": platform, "topic": topic if topic else "technology"}
# Surveys
if any(kw in user_lower for kw in ["surveys", "paid surveys", "mturk", "swagbucks", "find surveys"]):
return "surveys", {}
# Crypto/stocks
if any(kw in user_lower for kw in ["crypto opportunities", "crypto trading", "find crypto"]):
return "crypto", {}
if any(kw in user_lower for kw in ["check prices", "check watchlist", "price alert"]):
return "check_prices", {}
if "watch " in user_lower and any(kw in user_lower for kw in ["above", "below", "at"]):
match = re.search(r"watch\s+(\w+)\s+(above|below)\s+(\d+\.?\d*)", user_lower)
if match:
return "add_watch", {"symbol": match.group(1), "direction": match.group(2), "target": float(match.group(3))}
# Listing creation
if any(kw in user_lower for kw in ["create ebay listing", "ebay listing", "list on ebay"]):
product = re.sub(r"(create|ebay|listing|list|on|for)", "", user_lower).strip()
return "ebay_listing", {"product": product if product else ""}
if any(kw in user_lower for kw in ["create amazon listing", "amazon listing", "fba listing"]):
product = re.sub(r"(create|amazon|listing|fba|for)", "", user_lower).strip()
return "amazon_listing", {"product": product if product else ""}
if any(kw in user_lower for kw in ["dropship", "dropshipping", "find products to sell", "wholesale"]):
return "dropship", {}
# Arbitrage
if any(kw in user_lower for kw in ["arbitrage", "price difference", "flip", "resell"]):
product = re.sub(r"(find|arbitrage|price|difference|for|flip|resell)", "", user_lower).strip()
return "arbitrage", {"product": product if product else "electronics"}
# Trending products
if any(kw in user_lower for kw in ["trending products", "what to sell", "hot products"]):
return "trending", {}
# Content ideas
if any(kw in user_lower for kw in ["content ideas", "blog ideas", "video ideas", "monetize content"]):
niche = re.sub(r"(content|ideas|blog|video|monetize|for|about)", "", user_lower).strip()
return "content", {"niche": niche if niche else "technology"}
# Make money general
if any(kw in user_lower for kw in ["make money", "earn money", "side hustle", "income strategies", "how to make"]):
return "strategies", {}
return None, None
def confirm_money_action(action: str, params: Dict) -> bool:
"""Confirm money-making action."""
print("\n" + "="*60)
print("💰 MONEY-MAKING ACTION")
print("="*60)
actions_display = {
"freelance": ("🔍", "Search freelance jobs", f"Skills: {', '.join(params.get('skills', ['general']))}"),
"auto_apply": ("📝", "Auto-apply to jobs", f"Skills: {', '.join(params.get('skills', ['general']))}\nMax apps: {params.get('max', 5)}"),
"leads": ("🎯", "Generate business leads", f"Industry: {params.get('industry', 'general')}\nLocation: {params.get('location', 'USA')}"),
"social_post": ("📱", "Post to social media", f"Platform: {params.get('platform', 'twitter')}"),
"social_engage": ("👥", "Social media engagement", f"Platform: {params.get('platform', 'twitter')}"),
"social_content": ("✍️", "Generate social content", f"Platform: {params.get('platform', 'twitter')}\nTopic: {params.get('topic', 'general')}"),
"surveys": ("📋", "Find paid surveys", "Checks multiple survey sites"),
"crypto": ("🪙", "Find crypto opportunities", "Analyzes market trends"),
"check_prices": ("📈", "Check watchlist prices", "Reviews your watched assets"),
"add_watch": ("👁️", "Add to watchlist", f"Symbol: {params.get('symbol', '').upper()}\nAlert: {params.get('direction', '')} ${params.get('target', 0)}"),
"ebay_listing": ("🛒", "Create eBay listing", f"Product: {params.get('product', 'N/A')}"),
"amazon_listing": ("📦", "Create Amazon listing", f"Product: {params.get('product', 'N/A')}"),
"dropship": ("🚚", "Find dropshipping products", "Compares AliExpress vs Amazon/eBay"),
"arbitrage": ("📊", "Find arbitrage opportunity", f"Product: {params.get('product', 'N/A')}"),
"trending": ("🔥", "Find trending products", "Checks multiple sources"),
"content": ("✍️", "Generate content ideas", f"Niche: {params.get('niche', 'general')}"),
"strategies": ("💡", "Show money-making strategies", "Overview of all methods"),
}
if action in actions_display:
emoji, name, details = actions_display[action]
print(f"\n{emoji} Action: {name}")
print(f"📋 {details}")
print("="*60)
response = input("\n✅ Proceed? (yes/no): ").strip().lower()
return response in ['yes', 'y']
def execute_money_action(action: str, params: Dict, generate_fn) -> str:
"""Execute money-making action."""
print("\n[researching opportunities...]")
if action == "freelance":
return auto_apply_jobs("upwork", params.get("skills", ["general"]), generate_fn)
elif action == "auto_apply":
return full_auto_apply("upwork", params.get("skills", ["general"]), params.get("max", 5), generate_fn)
elif action == "leads":
return scrape_business_leads(params.get("industry", "business"), params.get("location", "new york"), generate_fn)
elif action == "social_post":
content = input("📝 What do you want to post? ").strip()
return schedule_social_post(params.get("platform", "twitter"), content, generate_fn)
elif action == "social_engage":
return engage_social_media(params.get("platform", "twitter"), "engage", generate_fn)
elif action == "social_content":
return generate_social_content(params.get("topic", "technology"), params.get("platform", "twitter"), generate_fn)
elif action == "surveys":
return find_surveys(generate_fn)
elif action == "crypto":
return crypto_opportunities(generate_fn)
elif action == "check_prices":
return check_prices(generate_fn)
elif action == "add_watch":
return add_to_watchlist(params.get("symbol", "BTC"), params.get("target", 0), params.get("direction", "above"))
elif action == "ebay_listing":
product = params.get("product", "")
if not product:
product = input("📦 What product do you want to list? ").strip()
return create_ebay_listing(product, generate_fn)
elif action == "amazon_listing":
product = params.get("product", "")
if not product:
product = input("📦 What product do you want to list? ").strip()
return create_amazon_listing(product, generate_fn)
elif action == "dropship":
return find_dropship_products(generate_fn)
elif action == "arbitrage":
return analyze_arbitrage_opportunity(params.get("product", "electronics"), generate_fn)
elif action == "trending":
return find_trending_products(generate_fn)
elif action == "content":
return generate_content_ideas(params.get("niche", "technology"), generate_fn)
elif action == "strategies":
return """
💰 MONEY-MAKING STRATEGIES 💰
================================
📋 FREELANCING
"find freelance jobs for [skill]"
"auto apply to jobs for [skill]"
Platforms: Upwork, Fiverr, Indeed
🎯 LEAD GENERATION
"find leads for [industry] in [city]"
Scrapes: Yellow Pages, Yelp, Google Maps
📱 SOCIAL MEDIA
"post to twitter/linkedin/facebook"
"generate post about [topic]"
"engage on twitter/linkedin"
📋 SURVEYS & TASKS
"find surveys"
Checks: Swagbucks, MTurk, Prolific
💹 CRYPTO/STOCKS
"crypto opportunities"
"watch BTC above 50000"
"check watchlist"
🛒 E-COMMERCE
"create ebay listing for [product]"
"create amazon listing for [product]"
"find dropshipping products"
📊 ARBITRAGE
"find arbitrage for [product]"
Compares: Amazon, eBay, Walmart
🔥 TRENDING
"find trending products"
"content ideas for [niche]"
"""
return "[unknown action]"
def plan_task(task: str, generate_fn) -> dict:
"""Have model interpret task and create execution plan."""
prompt = f"""You are an autonomous agent with browser control. Plan the steps to complete this task.
Available actions:
- BROWSE <url> - Open URL
- CLICK <selector_or_text> - Click element
- TYPE <text> - Type text into focused element
- FILL <selector> <text> - Fill input field
- PRESS <key> - Press key (Enter, Tab, Escape)
- READ - Read page content
- WAIT <seconds> - Wait
- DONE - Task complete
Respond ONLY with a JSON plan, no other text:
{{"task": "description", "steps": [{{"action": "BROWSE", "args": ["url"]}}, {{"action": "CLICK", "args": ["selector"]}}, ...]}}
Task: {task}"""
response, _, _ = generate_fn(prompt)
try:
# Extract JSON from response
match = re.search(r'\{.*\}', response, re.DOTALL)
if match:
return json.loads(match.group())
except Exception as e:
pass
return {"task": task, "steps": [], "error": f"Could not parse plan. Model said: {response[:200]}"}
def confirm_task(plan: dict) -> bool:
"""Show confirmation box and get user approval."""
print("\n" + "="*60)
print("🤖 AUTONOMOUS TASK CONFIRMATION")
print("="*60)
print(f"\n📋 TASK: {plan.get('task', 'Unknown')}\n")
print("📝 PLANNED STEPS:")
print("-"*40)
for i, step in enumerate(plan.get('steps', []), 1):
action = step.get('action', '?')
args = step.get('args', [])
print(f" {i}. {action} {' '.join(str(a) for a in args)}")
print("-"*40)
print("\n⚠️ The AI will execute these actions autonomously.")
print("="*60)
response = input("\n✅ Confirm execution? (yes/no): ").strip().lower()
return response in ['yes', 'y']
def execute_task(plan: dict, generate_fn) -> str:
"""Execute confirmed plan step by step."""
results = []
page_content = ""
for i, step in enumerate(plan.get('steps', []), 1):
action = step.get('action', '').upper()
args = step.get('args', [])
print(f"\n[EXEC {i}/{len(plan.get('steps', []))}] {action} {args}")
if action == "BROWSE":
url = args[0] if args else "https://google.com"
if not url.startswith("http"):
url = "https://" + url
result = tool_browser_open(url)
elif action == "CLICK":
result = tool_browser_click(args[0] if args else "")
elif action == "TYPE":
result = tool_browser_type(args[0] if args else "")
elif action == "FILL":
if len(args) >= 2:
result = tool_browser_type(args[1], args[0])
else:
result = "[FILL] Missing args - need selector and text"
elif action == "PRESS":
result = tool_browser_press(args[0] if args else "Enter")
elif action == "READ":
result = tool_browser_read()
page_content = result
elif action == "WAIT":
wait_time = int(args[0]) if args else 2
time.sleep(wait_time)
result = f"[waited {wait_time}s]"
elif action == "DONE":
result = "[task complete]"
results.append(result)
break
else:
result = f"[unknown action: {action}]"
print(f"{result[:150]}...")
results.append(result)
time.sleep(1)
# Final summary
summary = "\n".join(results[-5:]) # Last 5 results
# Ask model to summarize findings if we read content
if page_content:
print("\n[AI] Analyzing results...")
summary_prompt = f"Summarize what you found from this task. Page content:\n{page_content[:2000]}"
final_summary, _, _ = generate_fn(summary_prompt)
return f"Results:\n{summary}\n\nSummary:\n{final_summary}"
return summary
def tool_lht_analyze(text: str) -> str:
if not Config.use_lht_reasoning:
return "[lht] Disabled"
lht = get_lht_reasoner()
if not lht:
return "[lht] Not available"
steps = [s.strip() for s in re.split(r'[\n•\-\d\.]', text) if len(s.strip()) > 10]
if len(steps) < 2:
return "[lht] Need at least 2 reasoning steps"
metrics = lht.check_consistency(steps, _tokenizer)
return f"[LHT] Consistency: {metrics['consistency_score']:.2%}, Holonomy: {metrics['holonomy']:.4f}"
# ==============================================================================
# PLANNING / REFLECTION
# ==============================================================================
def persona_directive() -> str:
return "Übermenschetien v2: Stable self-improvement. Dense, coherent, helpful. Every word matters."
def plan_for(goal: str) -> str:
user = f"{persona_directive()}\nGoal: {goal}\nDeliver 5 concrete steps with constraints and risks."
response, _, _ = generate(user)
return response
def reflect_on(last_output: str) -> str:
user = f"{persona_directive()}\nCritique and improve:\n{last_output}"
response, _, _ = generate(user)
return response
# ==============================================================================
# FINAL REPORT
# ==============================================================================
def final_report():
print("\n" + "=" * 70)
print("FINAL ÜBERMENSCHETIEN v2 REPORT")
print("=" * 70)
print(f"Turns completed: {Store.state['turn']}")
print(f"Goals tracked: {len(Store.goals)}")
print(f"Improvement iterations: {Store.state.get('improvement_iterations', 0)}")
print(f"Training runs: {len(Store.state.get('training_runs', []))}")
print(f"Rollback count: {Store.state.get('rollback_count', 0)}")
print(f"\nCheckpoints:")
print(f" Current: {Store.state.get('current_checkpoint', 'unknown')}")
print(f" Best: {Store.state.get('best_checkpoint', 'unknown')}")
print(f" Best quality: {Store.state.get('best_quality_score', 0):.3f}")
if Store.state.get("cfhot_interventions"):
iv = Store.state["cfhot_interventions"]
print(f"\nCF-HoT Interventions: {sum(iv.values())}")
if Store.state.get("quality_history"):
qh = Store.state["quality_history"]
print(f"\nQuality History ({len(qh)} data points):")
if qh:
print(f" First: {qh[0].get('quality', 0):.3f}")
print(f" Last: {qh[-1].get('quality', 0):.3f}")
print("=" * 70)
# ==============================================================================
# HELP
# ==============================================================================
HELP = """
╔══════════════════════════════════════════════════════════════════════════════╗
║ ARC ENGINE v2.1 - Adaptive Recursive Cognition (Übermenschetien) ║
╠══════════════════════════════════════════════════════════════════════════════╣
║ NEW IN v2.1 ⭐ ║
║ !cfhot / !125x Toggle 125× repetition head on/off ║
║ !rsi15 Run 15-iteration RSI stress test ║
║ !book Toggle book mode (16K tokens) ║
║ !write <topic> Write a complete book ║
║ !idea <request> Generate extensive ideas (Claude) ║
║ !claude <prompt> Direct Claude Opus 4.5 prompt ║
║ !plot Plot quality history ║
║ !export [name] Export checkpoint package ║
║ !import <path> Import checkpoint package ║
║ !benchmark Run evaluation suite ║
║ !learn Learn from high-quality responses ║
║ !api Start REST API server ║
║ ║
║ MULTIMEDIA 🎬 ║
║ !stream Open live generation window (see tokens live!) ║
║ !stream off Close streaming window ║
║ !audio / !tts Toggle text-to-speech ║
║ !audio voices List available TTS voices ║
║ !audio voice N Set voice by index ║
║ !audio rate N Set speech rate (default 175) ║
║ !say <text> Speak text immediately ║
║ ║
║ IMAGE GEN 🖼️ ║
║ !image Show image system status ║
║ !image load Load SDXL model ║
║ !imagine <prompt> Generate image with SDXL ║
║ !dalle <prompt> Generate with DALL-E 3 ║
║ !image view View last generated image ║
║ !image view <path> View image from file ║
║ ║
║ SELF-IMPROVEMENT (WITH SAFEGUARDS) ║
║ !improve Run stable self-improvement loop ║
║ !eval Comprehensive model evaluation ║
║ !train <steps> Run N training steps (default: 25) ║
║ !compare Compare current vs best checkpoint ║
║ !rollback Rollback to best checkpoint ║
║ !load <path> Load a specific checkpoint ║
║ ║
║ RSI MODE (Recursive Self-Improvement) 🧠 ║
║ rsi / rsi status Show RSI status ║
║ rsi start / !rsi Start RSI mode (autonomous improvement) ║
║ rsi stop Stop RSI mode ║
║ rsi pause/resume Pause/resume RSI ║
║ rsi mode X Set mode: conservative, balanced, aggressive ║
║ rsi target 0.9 Set target quality ║
║ ║
║ AGENTIC TOOLS (FULL ACCESS) ║
║ !shell <cmd> Execute ANY shell command ║
║ !python <code> Execute Python code (full access) ║
║ !read <path> Read file contents ║
║ !write <p> <c> Write content to file ║
║ !ls [path] List directory ║
║ !web <query> Web search (DuckDuckGo) ║
║ ║
║ BROWSER (Live Visual Browser) ║
║ !browse <url> Open browser and navigate ║
║ !click <selector> Click element ║
║ !type <text> Type into focused element ║
║ !fill <sel> <txt> Type into selector ║
║ !read Read page text ║
║ !close Close browser ║
║ ║
║ TASK CHAINING 🔗 ║
║ chain: <task> Add task to chain ║
║ chain run Execute chain ║
║ chain workflows List workflow templates ║
║ ║
║ GMAIL API 📧 ║
║ !gmail search <q> Search emails ║
║ !gmail read <id> Read email ║
║ !gmail send ... Send email ║
║ ║
║ CRYPTO MINING ⛏️ ║
║ !mine Show status | !mine profit Check profitability ║
║ !mine auto Auto-mine best coin ║
║ ║
║ INFO & CONFIG ║
║ status Current state ║
║ history Quality history ║
║ toggle <flag> Toggle flags (125x, book, idea, api, etc) ║
║ help This help ║
║ quit Exit ║
╚══════════════════════════════════════════════════════════════════════════════╝
"""
# ==============================================================================
# MAIN LOOP
# ==============================================================================
def main():
print("=" * 75)
print("🤖 ARC ENGINE v2.1 - Adaptive Recursive Cognition (Übermenschetien)")
print("=" * 75)
print(f" DENSE Mode: ON (CONDENSATOR checkpoint)")
print(f" CF-HoT Control: ON")
print(f" CF-HoT 125×: {'ON' if Config.use_cfhot_125x else 'OFF'}")
print(f" AGENTIC Mode: ON (Full shell/python access)")
print(f" LHT Reasoning: {'ON' if LHT_OK else 'OFF'}")
print(f" Vector Memory: {'ON' if VECTOR_OK else 'OFF'}")
print(f" Live Browser: {'ON' if BROWSER_OK else 'OFF'}")
print(f" Claude API: {'ON' if CLAUDE_API_OK else 'OFF'}")
print(f" Stream Window: {'ON' if TK_OK else 'OFF'}")
print(f" Image Gen: {'ON' if DIFFUSERS_OK or OPENAI_OK else 'OFF'}")
print(f" TTS Audio: {'ON' if VOICE_OK or GTTS_OK else 'OFF'}")
print(f" Training Examples: {len(DENSE_TRAINING_EXAMPLES)}")
print("=" * 75)
print(" NEW IN v2.1: !cfhot, !rsi15, !book, !idea, !stream, !imagine, !audio")
print("=" * 75)
print(" Type 'help' for commands, '!improve' to start self-improvement")
print("=" * 75 + "\n")
Store.load()
tok, model = load_llm()
# Load 125× head if enabled
if Config.use_cfhot_125x:
get_cfhot_head().load()
# Initialize Claude client
if CLAUDE_API_OK:
init_claude_client()
improver = StableSelfImprover()
last_plan = ""
while True:
try:
u = input("\n> ").strip()
except (EOFError, KeyboardInterrupt):
break
if not u:
continue
if u == "help":
print(HELP)
continue
if u == "quit":
break
# ══════════════════════════════════════════════════════════════════════
# NEW v2.1 COMMANDS
# ══════════════════════════════════════════════════════════════════════
# --- CF-HoT 125× Toggle ---
if u in ("!cfhot", "!125x", "!cfhot toggle"):
print(toggle_cfhot_125x())
continue
if u == "!cfhot status":
head = get_cfhot_head()
print(f"[cf-hot 125×] Loaded: {head.loaded}")
print(f"[cf-hot 125×] Enabled: {Config.use_cfhot_125x}")
continue
# --- RSI-15 Stress Test ---
if u == "!rsi15":
run_rsi_15(improver)
continue
# --- Book Mode ---
if u == "!book":
result = Config.toggle("book_mode")
print(result)
if Config.book_mode:
print(f"[book] Max tokens: {Config.book_max_tokens}")
print("[book] Use: !write <topic> to start")
continue
if u.startswith("!write "):
topic = u[7:].strip()
if topic:
try:
chapters = int(input("Chapters (default 10): ").strip() or "10")
words = int(input("Words/chapter (default 3000): ").strip() or "3000")
except:
chapters, words = 10, 3000
writer = get_book_writer(generate)
writer.write_book(topic, chapters, words)
else:
print("[book] Usage: !write <topic>")
continue
# --- Idea Mode ---
if u == "!idea":
result = Config.toggle("idea_mode")
print(result)
if Config.idea_mode:
print(f"[idea] Depth: {Config.idea_depth}")
print(f"[idea] Use Opus: {Config.idea_use_opus}")
print("[idea] Use: !idea <request> to generate")
continue
if u.startswith("!idea "):
request = u[6:].strip()
if request:
depth = Config.idea_depth
# Parse depth flag
if " --quick" in request:
depth = "quick"
request = request.replace(" --quick", "")
elif " --deep" in request:
depth = "deep"
request = request.replace(" --deep", "")
gen = get_idea_generator()
gen.generate(request, depth=depth, use_opus=Config.idea_use_opus)
else:
print("[idea] Usage: !idea <request> [--quick|--deep]")
continue
if u.startswith("!expand "):
idea_name = u[8:].strip()
gen = get_idea_generator()
result = gen.expand(idea_name)
print(result)
continue
# --- Direct Claude Prompting ---
if u.startswith("!claude "):
prompt = u[8:].strip()
if prompt:
use_opus = "--opus" in prompt
prompt = prompt.replace("--opus", "").strip()
print(f"\n[claude] {'Opus 4.5' if use_opus else 'Sonnet'}:\n")
result = claude_generate(prompt, use_opus=use_opus, stream=True)
else:
print("[claude] Usage: !claude <prompt> [--opus]")
continue
if u == "!claude":
print(f"[claude] API: {'OK' if CLAUDE_API_OK else 'Not installed'}")
print(f"[claude] Key: {'Set' if CLAUDE_CONFIG.get('api_key') else 'Missing'}")
print(f"[claude] Model: {CLAUDE_CONFIG['model']}")
continue
# --- Plotting ---
if u == "!plot":
result = plot_quality_history()
print(result)
continue
# --- Export/Import ---
if u == "!export" or u.startswith("!export "):
name = u[8:].strip() if len(u) > 8 else None
result = export_checkpoint(name)
print(result)
continue
if u.startswith("!import "):
path = u[8:].strip()
result = import_checkpoint(path)
print(result)
continue
# --- Benchmark ---
if u == "!benchmark":
run_benchmark(generate)
continue
# --- Learn ---
if u == "!learn":
result = learn_from_conversation()
print(result)
continue
# --- API Server ---
if u == "!api":
if not Config.api_enabled:
Config.api_enabled = True
start_api_server(Config.api_port, generate)
else:
print(f"[api] Already running on port {Config.api_port}")
continue
# ══════════════════════════════════════════════════════════════════════
# MULTIMEDIA COMMANDS
# ══════════════════════════════════════════════════════════════════════
# --- Streaming Window ---
if u == "!stream" or u == "!stream on":
win = get_stream_window()
if win.start():
print("[stream] ✓ Window opened - tokens will appear live")
continue
if u == "!stream off":
win = get_stream_window()
if win.window:
win._on_close()
print("[stream] ✓ Window closed")
continue
# --- Audio/TTS ---
if u == "!audio" or u == "!tts":
result = get_audio_system().toggle()
print(result)
continue
if u == "!audio voices" or u == "!tts voices":
get_audio_system().list_voices()
continue
if u.startswith("!audio voice ") or u.startswith("!tts voice "):
try:
idx = int(u.split()[-1])
get_audio_system().set_voice(idx)
print(f"[audio] Voice set to {idx}")
except:
print("[audio] Usage: !audio voice <index>")
continue
if u.startswith("!audio rate "):
try:
rate = int(u.split()[-1])
get_audio_system().set_rate(rate)
print(f"[audio] Rate: {rate}")
except:
print("[audio] Usage: !audio rate <number>")
continue
if u.startswith("!say "):
text = u[5:].strip()
if text:
audio = get_audio_system()
audio.enabled = True
audio.speak(text, block=True)
audio.enabled = False
continue
# --- Image Generation ---
if u == "!image" or u == "!image status":
img = get_image_system()
print(f"[image] SDXL: {'Loaded' if img.sdxl_pipe else 'Not loaded'}")
print(f"[image] Diffusers: {DIFFUSERS_OK}")
print(f"[image] DALL-E: {OPENAI_OK and bool(os.environ.get('OPENAI_API_KEY'))}")
continue
if u == "!image load" or u == "!sdxl load":
get_image_system().load_sdxl()
continue
if u.startswith("!imagine ") or u.startswith("!image gen "):
prompt = u.split(" ", 2)[-1].strip() if u.startswith("!image gen ") else u[9:].strip()
if prompt:
img = get_image_system().generate(prompt)
if img:
get_image_system().view()
else:
print("[image] Usage: !imagine <prompt>")
continue
if u.startswith("!dalle ") or u.startswith("!image dalle "):
prompt = u.split(" ", 2)[-1].strip() if u.startswith("!image dalle ") else u[7:].strip()
if prompt:
img = get_image_system().generate_dalle(prompt)
if img:
get_image_system().view()
continue
if u.startswith("!image view "):
path = u[12:].strip()
if os.path.exists(path):
get_image_system().view(path)
else:
print(f"[image] Not found: {path}")
continue
if u == "!image view":
get_image_system().view()
continue
# === SELF-IMPROVEMENT COMMANDS ===
if u == "!improve":
result = improver.improve()
print("\n" + "=" * 50)
print("IMPROVEMENT RESULT:")
print(json.dumps({k: v for k, v in result.items() if k != 'history'}, indent=2, default=str))
continue
if u == "!eval":
result = improver.evaluate_current_model()
print(json.dumps({k: v for k, v in result.items() if k != 'results'}, indent=2, default=str))
continue
if u.startswith("!train "):
try:
steps = int(u[7:])
old_ckpt = Store.state.get('current_checkpoint', DENSE_CHECKPOINT)
result = improver.run_training_iteration(steps)
if result['success']:
# Auto-compare
comp = improver.compare_checkpoints(old_ckpt, result['new_checkpoint'])
if comp['keep_new']:
print(f"\n✓ Using new checkpoint ({comp['reason']})")
else:
reload_model(old_ckpt)
print(f"\n✗ Keeping old checkpoint ({comp['reason']})")
else:
print(f"Training failed")
except ValueError:
print("Usage: !train <steps>")
continue
if u == "!compare":
current = Store.state.get('current_checkpoint', DENSE_CHECKPOINT)
best = Store.state.get('best_checkpoint', DENSE_CHECKPOINT)
if current != best:
improver.compare_checkpoints(current, best)
else:
print("Current checkpoint IS the best checkpoint")
continue
if u == "!rollback":
improver.rollback_to_best()
print(f"Rolled back to: {Store.state['best_checkpoint']}")
continue
if u.startswith("!load "):
checkpoint = u[6:].strip()
try:
reload_model(checkpoint)
print(f"Loaded: {checkpoint}")
except Exception as e:
print(f"Error: {e}")
continue
# === AGENTIC COMMANDS ===
if u.startswith("!shell "):
result = AgentTools.shell(u[7:])
print(f"```\n{result['output']}\n```\nExit: {result['returncode']}")
continue
if u.startswith("!python "):
result = AgentTools.python_exec(u[8:])
print(f"```\n{result['output']}\n```")
continue
if u.startswith("!read "):
result = AgentTools.read_file(u[6:].strip())
if result['success']:
print(f"```\n{result['content'][:5000]}\n```")
else:
print(f"Error: {result['error']}")
continue
if u.startswith("!write "):
parts = u[7:].split(" ", 1)
if len(parts) == 2:
result = AgentTools.write_file(parts[0], parts[1])
print(f"Written to {result.get('path', 'unknown')}" if result['success'] else f"Error: {result['error']}")
else:
print("Usage: !write <path> <content>")
continue
if u.startswith("!ls"):
path = u[3:].strip() or "."
result = AgentTools.list_dir(path)
if result['success']:
print("\n".join(result['items']))
else:
print(f"Error: {result['error']}")
continue
if u.startswith("!search "):
result = AgentTools.search_files(u[8:])
print(result['output'] if result['success'] else "No results")
continue
if u.startswith("!web "):
result = AgentTools.web_search(u[5:])
if result['success']:
print("\n\n".join(result['results']))
else:
print(f"Error: {result['error']}")
continue
# === BROWSER COMMANDS ===
if u.startswith("!browse "):
url = u[8:].strip()
if not url.startswith("http"):
url = "https://" + url
print(tool_browser_open(url))
continue
if u == "!browse":
print(tool_browser_open())
continue
if u.startswith("!click "):
selector = u[7:].strip()
print(tool_browser_click(selector))
continue
if u.startswith("!type "):
text = u[6:].strip()
print(tool_browser_type(text))
continue
if u.startswith("!fill "):
# !fill selector text
parts = u[6:].strip().split(" ", 1)
if len(parts) == 2:
print(tool_browser_type(parts[1], parts[0]))
else:
print("[browser] Usage: !fill <selector> <text>")
continue
if u.startswith("!press "):
key = u[7:].strip()
print(tool_browser_press(key))
continue
if u == "!read":
print(tool_browser_read())
continue
if u == "!url":
print(tool_browser_url())
continue
if u == "!back":
print(tool_browser_back())
continue
if u == "!close":
print(tool_browser_close())
continue
# === TASK CHAIN COMMANDS ===
if u.startswith("chain:"):
task_str = u[6:].strip()
task_type, params, desc = parse_chain_task(task_str)
TASK_CHAIN.add(task_type, params, desc)
print(f"[chain] Added: {desc}")
print(f"[chain] Total tasks: {len(TASK_CHAIN.tasks)}")
continue
if u == "chain" or u == "chain show":
print(TASK_CHAIN.show())
continue
if u == "chain run":
result = run_task_chain(generate)
print(result)
continue
if u == "chain clear":
TASK_CHAIN.clear()
print("[chain] Cleared all tasks")
continue
if u.startswith("chain workflow "):
workflow_name = u[15:].strip()
if workflow_name in WORKFLOW_TEMPLATES:
TASK_CHAIN.clear()
for task_str in WORKFLOW_TEMPLATES[workflow_name]:
task_type, params, desc = parse_chain_task(task_str)
TASK_CHAIN.add(task_type, params, desc)
print(f"[chain] Loaded workflow: {workflow_name}")
print(TASK_CHAIN.show())
else:
print(f"[chain] Unknown workflow. Available: {', '.join(WORKFLOW_TEMPLATES.keys())}")
continue
if u == "chain workflows":
print("\n📋 AVAILABLE WORKFLOWS:")
for name, tasks in WORKFLOW_TEMPLATES.items():
print(f"\n {name}:")
for t in tasks:
print(f" - {t}")
continue
# === LOGIN CONFIG COMMANDS ===
if u == "!login config" or u == "!config login":
print("\n" + "="*60)
print("🔐 LOGIN CONFIGURATION")
print("="*60)
print(f"\n use_persistent_profile: {LOGIN_CONFIG['use_persistent_profile']}")
print(f" firefox_profile_path: {LOGIN_CONFIG['firefox_profile_path'] or '(not set)'}")
print(f" use_gmail_api: {LOGIN_CONFIG['use_gmail_api']}")
print(f" gmail_api_ready: {GMAIL_API_OK and _gmail_service is not None}")
print(f"\nCommands:")
print(" !login set profile <path> - Set Firefox profile path")
print(" !login use profile - Enable persistent profile")
print(" !login use fresh - Use fresh browser (default)")
print(" !login use api - Enable Gmail API mode")
print(" !login auth api - Authenticate Gmail API")
print(" !login save - Save config")
continue
if u.startswith("!login set profile "):
path = u[19:].strip()
if os.path.exists(path):
LOGIN_CONFIG["firefox_profile_path"] = path
print(f"[config] ✅ Profile path set: {path}")
else:
print(f"[config] ❌ Path not found: {path}")
print("[config] Find your profile: ~/.mozilla/firefox/*.default-release")
continue
if u == "!login use profile":
if LOGIN_CONFIG["firefox_profile_path"]:
LOGIN_CONFIG["use_persistent_profile"] = True
print("[config] ✅ Persistent profile ENABLED")
print("[config] Restart browser with !close then !browse")
else:
print("[config] ❌ Set profile path first: !login set profile <path>")
continue
if u == "!login use fresh":
LOGIN_CONFIG["use_persistent_profile"] = False
print("[config] ✅ Fresh browser mode ENABLED")
continue
if u == "!login use api":
if GMAIL_API_OK:
LOGIN_CONFIG["use_gmail_api"] = True
print("[config] ✅ Gmail API mode ENABLED")
print("[config] Use: !login auth api (if not authenticated)")
else:
print("[config] ❌ Gmail API not installed")
print("[config] Run: pip install google-auth google-auth-oauthlib google-api-python-client")
continue
if u == "!login auth api":
if gmail_api_authenticate():
LOGIN_CONFIG["use_gmail_api"] = True
print("[config] ✅ Gmail API authenticated and enabled")
continue
if u == "!login save":
save_login_config()
continue
# === GMAIL API COMMANDS ===
if u.startswith("!gmail search "):
query = u[14:].strip()
if LOGIN_CONFIG.get("use_gmail_api") or GMAIL_API_OK:
result = gmail_api_search(query)
print(result)
else:
print("[gmail-api] Not enabled. Use: !login use api")
continue
if u.startswith("!gmail read "):
msg_id = u[12:].strip()
result = gmail_api_read(msg_id)
print(result)
continue
if u.startswith("!gmail send "):
# !gmail send to@email.com "Subject" "Body"
parts = u[12:].strip().split('"')
if len(parts) >= 4:
to = parts[0].strip()
subject = parts[1]
body = parts[3]
result = gmail_api_send(to, subject, body)
print(result)
else:
print("[usage] !gmail send to@email.com \"Subject\" \"Body\"")
continue
# === RSI MODE COMMANDS ===
if u == "rsi" or u == "rsi status":
print(rsi_status())
continue
if u == "rsi start" or u == "!rsi":
# Define helper functions for RSI
def rsi_train(steps):
return _run_training(steps)
def rsi_eval():
return _run_evaluation()
def rsi_reload():
return _reload_checkpoint(Store.state.get("current_checkpoint", DENSE_CHECKPOINT))
# Start RSI mode
rsi_mode_loop(generate, rsi_train, rsi_eval, rsi_reload)
continue
if u == "rsi stop":
RSI_RUNNING = False
print("[RSI] ⏹️ Stop signal sent")
continue
if u == "rsi pause":
RSI_PAUSED = True
print("[RSI] ⏸️ Pause signal sent")
continue
if u == "rsi resume":
if RSI_PAUSED:
RSI_PAUSED = False
def rsi_train(steps):
return _run_training(steps)
def rsi_eval():
return _run_evaluation()
def rsi_reload():
return _reload_checkpoint(Store.state.get("current_checkpoint", DENSE_CHECKPOINT))
rsi_mode_loop(generate, rsi_train, rsi_eval, rsi_reload)
else:
print("[RSI] Not paused")
continue
if u.startswith("rsi mode "):
mode = u[9:].strip().lower()
if mode in ["conservative", "balanced", "aggressive"]:
RSI_CONFIG["mode"] = mode
if mode == "conservative":
RSI_CONFIG["training_steps_per_iter"] = 10
RSI_CONFIG["auto_rollback_threshold"] = 0.10
elif mode == "balanced":
RSI_CONFIG["training_steps_per_iter"] = 25
RSI_CONFIG["auto_rollback_threshold"] = 0.15
elif mode == "aggressive":
RSI_CONFIG["training_steps_per_iter"] = 50
RSI_CONFIG["auto_rollback_threshold"] = 0.20
save_rsi_config()
print(f"[RSI] Mode set to: {mode}")
else:
print("[RSI] Modes: conservative, balanced, aggressive")
continue
if u.startswith("rsi target "):
try:
target = float(u[11:].strip())
RSI_CONFIG["target_quality"] = target
save_rsi_config()
print(f"[RSI] Target quality set to: {target}")
except:
print("[usage] rsi target 0.85")
continue
if u.startswith("rsi focus "):
cap = u[10:].strip().lower()
if cap in RSI_CONFIG["capabilities_to_improve"]:
RSI_CONFIG["current_focus"] = cap
save_rsi_config()
print(f"[RSI] Focus set to: {cap}")
else:
print(f"[RSI] Capabilities: {', '.join(RSI_CONFIG['capabilities_to_improve'])}")
continue
if u == "rsi eval":
print("\n[RSI] 📊 Evaluating all capabilities...")
for cap in RSI_CONFIG["capabilities_to_improve"]:
score = rsi_evaluate_capability(cap, generate)
print(f" {cap}: {score:.2f}")
continue
if u == "rsi config":
print(json.dumps(RSI_CONFIG, indent=2))
continue
if u == "rsi reset":
RSI_CONFIG["iteration"] = 0
RSI_CONFIG["total_improvements"] = 0
RSI_CONFIG["total_rollbacks"] = 0
RSI_CONFIG["best_quality_ever"] = 0.0
save_rsi_config()
print("[RSI] ✅ Stats reset")
continue
# === MINING COMMANDS ===
if u == "!mine" or u == "!mine status":
print(mining_status())
continue
if u == "!mine profit" or u == "mining profitability":
result = check_mining_profitability(generate)
print(result)
continue
if u == "!mine lowdiff" or u == "find low difficulty coins":
result = find_lowest_difficulty_coins(generate)
print(result)
continue
if u.startswith("!mine wallet "):
parts = u[13:].strip().split(maxsplit=1)
if len(parts) >= 2:
coin, address = parts[0].upper(), parts[1]
MINING_CONFIG["wallet_addresses"][coin] = address
save_mining_config()
print(f"[mining] ✅ Wallet saved for {coin}")
else:
print("[usage] !mine wallet <COIN> <address>")
continue
if u.startswith("!mine pool "):
parts = u[11:].strip().split(maxsplit=1)
if len(parts) >= 2:
coin, pool = parts[0].upper(), parts[1]
MINING_CONFIG["pool_urls"][coin] = pool
save_mining_config()
print(f"[mining] ✅ Pool saved for {coin}")
else:
print("[usage] !mine pool <COIN> <pool_url>")
continue
if u.startswith("!mine setup "):
parts = u[12:].strip().split()
if len(parts) >= 2:
miner, coin = parts[0].lower(), parts[1].upper()
wallet = MINING_CONFIG.get("wallet_addresses", {}).get(coin, "")
pool = MINING_CONFIG.get("pool_urls", {}).get(coin, "")
if not wallet:
print(f"[mining] ❌ No wallet for {coin}. Set with: !mine wallet {coin} <address>")
continue
if not pool:
for c in LOW_DIFF_COINS:
if c["symbol"] == coin:
pool = c["pool"]
break
if not pool:
print(f"[mining] ❌ No pool for {coin}. Set with: !mine pool {coin} <url>")
continue
result = setup_miner(miner, coin, wallet, pool)
print(result)
else:
print("[usage] !mine setup <miner> <COIN>")
print(f"[miners] {list(SUPPORTED_MINERS.keys())}")
continue
if u.startswith("!mine start"):
parts = u[11:].strip().split()
miner = parts[0] if parts else "xmrig"
coin = parts[1].upper() if len(parts) > 1 else None
result = start_mining(miner, coin)
print(result)
continue
if u == "!mine stop" or u == "stop mining":
print(stop_mining())
continue
if u == "!mine auto":
result = auto_mine_best_coin(generate)
print(result)
continue
if u.startswith("!mine create wallet "):
coin = u[20:].strip().upper()
result = create_local_wallet(coin)
print(result)
continue
if u == "!mine coins":
print("\n⛏️ SUPPORTED COINS:")
for miner, info in SUPPORTED_MINERS.items():
print(f"\n {miner}: {', '.join(info['coins'])}")
print("\n🔥 LOW DIFFICULTY COINS:")
for c in LOW_DIFF_COINS:
print(f" {c['symbol']}: {c['name']} ({c['algo']})")
continue
if u == "!mine config":
print(json.dumps(MINING_CONFIG, indent=2))
continue
# === CREDENTIAL MANAGEMENT ===
if u.startswith("!savelogin "):
# !savelogin gmail email@gmail.com password123
parts = u[11:].strip().split(maxsplit=2)
if len(parts) >= 3:
store_credential(parts[0], parts[1], parts[2])
else:
print("[usage] !savelogin <service> <email> <password>")
continue
if u.startswith("!login "):
# !login gmail OR !login gmail email@gmail.com password123
parts = u[7:].strip().split(maxsplit=2)
service = parts[0].lower() if parts else None
if not service:
print("[usage] !login <service> [email] [password]")
print("[services] gmail, facebook, twitter, linkedin, github, outlook")
continue
# Check for stored credentials
creds = get_credential(service)
if len(parts) >= 3:
email, password = parts[1], parts[2]
elif creds:
email, password = creds["email"], creds["password"]
else:
# Prompt for credentials
email = input(f"📧 Email/Username for {service}: ").strip()
password = input(f"🔑 Password (hidden): ").strip()
if confirm_login(service, email):
result = execute_login(service, email, password)
print(result[:500] if result else "[login attempted]")
else:
print("[login cancelled]")
continue
# === AUTONOMOUS TASK ===
if u.startswith("!do "):
task = u[4:].strip()
print(f"\n[AI] 🧠 Analyzing task: {task}")
print("[AI] Planning steps...\n")
plan = plan_task(task, generate)
if plan.get('error'):
print(f"[AI] ❌ Error: {plan['error']}")
continue
if not plan.get('steps'):
print("[AI] ❌ No steps generated. Try rephrasing the task.")
continue
if confirm_task(plan):
print("\n[AI] 🚀 Executing task...\n")
result = execute_task(plan, generate)
print("\n" + "="*60)
print("📊 TASK RESULTS")
print("="*60)
print(result)
print("="*60)
else:
print("[AI] ❌ Task cancelled by user.")
continue
# === GOALS ===
if u == "goals":
print("[goals]")
if not Store.goals:
print(" (none)")
for i, g in enumerate(Store.goals):
print(f" [{i}] {g}")
continue
if u.startswith("add:"):
Store.goals.append(u[4:].strip())
Store.save()
print("[goals] added")
continue
if u.startswith("del:"):
try:
Store.goals.pop(int(u[4:].strip()))
Store.save()
print("[goals] deleted")
except:
print("[goals] bad index")
continue
if u.startswith("plan:"):
try:
goal = Store.goals[int(u[5:].strip())]
except:
print("[plan] bad index")
continue
out = plan_for(goal)
last_plan = out
Store.log_mem("plan", {"goal": goal, "plan": out})
print(out)
continue
if u == "reflect":
if not last_plan:
print("[reflect] no plan to refine")
continue
improved = reflect_on(last_plan)
last_plan = improved
Store.log_mem("reflect", {"plan": improved})
print(improved)
continue
# === INFO ===
if u == "status":
status = {
"turn": Store.state["turn"],
"goals": len(Store.goals),
"improvement_iterations": Store.state.get("improvement_iterations", 0),
"rollback_count": Store.state.get("rollback_count", 0),
"current_checkpoint": Store.state.get("current_checkpoint", "unknown"),
"best_checkpoint": Store.state.get("best_checkpoint", "unknown"),
"best_quality": Store.state.get("best_quality_score", 0),
"target_quality": Config.target_quality_score,
"training_examples": len(DENSE_TRAINING_EXAMPLES),
}
print(json.dumps(status, indent=2))
continue
if u == "history":
qh = Store.state.get("quality_history", [])
print(f"Quality History ({len(qh)} entries):")
for entry in qh[-10:]:
print(f" {entry.get('iteration', '?')}: {entry.get('quality', 0):.3f}")
continue
if u == "examples":
print(f"Training examples: {len(DENSE_TRAINING_EXAMPLES)}")
print(f"Preference pairs: {len(PREFERENCE_PAIRS)}")
print("\nSample prompts:")
for ex in DENSE_TRAINING_EXAMPLES[:5]:
print(f"{ex['prompt']}")
continue
# === LIMITED TOOLS ===
if u.startswith("shell:"):
print(tool_shell(u[6:].strip()))
continue
if u.startswith("py:"):
print(tool_py(u[3:].strip()))
continue
if u.startswith("search:"):
print(tool_search_local(u[7:].strip()))
continue
if u.startswith("lht:"):
print(tool_lht_analyze(u[4:].strip()))
continue
# === CONFIG ===
if u.startswith("toggle"):
parts = u.split(maxsplit=1)
if len(parts) > 1:
print(Config.toggle(parts[1]))
else:
print("[toggle] specify flag")
continue
# === SEAMLESS WEB TASK DETECTION ===
if BROWSER_OK:
# Check for email action first
email_action, email_params = detect_email_action(u)
if email_action:
# For compose, gather details
if "compose" in email_action:
print("\n✉️ Composing email...")
email_params["to"] = input("📬 To: ").strip()
email_params["subject"] = input("📋 Subject: ").strip()
email_params["body"] = input("📝 Message: ").strip()
if confirm_email_action(email_action, email_params):
result = execute_email_action(email_action, email_params)
if result:
# Let model summarize what was found
email_prompt = f"I searched/checked the email. Here's what I found:\n{result[:2500]}\n\nSummarize the relevant emails or results for the user based on their request: '{u}'"
out, stats, eval_result = generate(email_prompt)
print(f"\n{out}")
print(f"\n[Quality: {eval_result.overall_score:.2f} | Density: {eval_result.density_score:.1f} | "
f"Coherence: {eval_result.coherence_score:.2f} | Tokens: {eval_result.tokens}]")
else:
print("[email] Action completed")
else:
print("[cancelled]")
continue
# Check for money-making task
money_action, money_params = detect_money_task(u)
if money_action:
if confirm_money_action(money_action, money_params):
result = execute_money_action(money_action, money_params, generate)
print(f"\n{result}")
else:
print("[cancelled]")
continue
# Check for login request
service, url = detect_login_request(u)
if service:
creds = get_credential(service)
if creds:
email, password = creds["email"], creds["password"]
else:
print(f"\n🔐 Login to {service.upper()} requested")
email = input(f"📧 Email/Username: ").strip()
password = input(f"🔑 Password: ").strip()
save = input("💾 Save credentials for this session? (yes/no): ").strip().lower()
if save in ['yes', 'y']:
store_credential(service, email, password)
if confirm_login(service, email):
result = execute_login(service, email, password, url)
# Let model respond based on what happened
login_prompt = f"I just logged into {service}. The page now shows: {result[:1500]}\n\nRespond to the user confirming login and what you see."
out, stats, eval_result = generate(login_prompt)
print(f"\n{out}")
print(f"\n[Quality: {eval_result.overall_score:.2f} | Density: {eval_result.density_score:.1f} | "
f"Coherence: {eval_result.coherence_score:.2f} | Tokens: {eval_result.tokens}]")
else:
print("[login cancelled]")
continue
# Check for web search task
template_name, query = detect_web_task(u)
if template_name and query:
plan = build_plan_from_template(template_name, query)
if confirm_task(plan):
page_content = execute_task_silent(plan)
if page_content:
# Generate response based on REAL web content
web_prompt = f"Based on this web content, answer the user's request: '{u}'\n\nWeb content:\n{page_content[:3000]}"
out, stats, eval_result = generate(web_prompt)
print(f"\n{out}")
print(f"\n[Quality: {eval_result.overall_score:.2f} | Density: {eval_result.density_score:.1f} | "
f"Coherence: {eval_result.coherence_score:.2f} | Tokens: {eval_result.tokens}]")
Store.state["turn"] += 1
Store.save()
else:
print("[browser] Could not read page content")
else:
print("[cancelled]")
continue
# === DEFAULT: GENERATE ===
out, stats, eval_result = generate(u)
print(f"\n{out}")
print(f"\n[Quality: {eval_result.overall_score:.2f} | Density: {eval_result.density_score:.1f} | "
f"Coherence: {eval_result.coherence_score:.2f} | Tokens: {eval_result.tokens}]")
if eval_result.filler_count > 0:
print(f" ⚠ Fillers detected: {eval_result.filler_count}")
if eval_result.gibberish_score > 0.3:
print(f" ⚠ Gibberish detected: {eval_result.gibberish_score:.2f}")
Store.log_mem("reply", {"in": u, "out": out, "quality": eval_result.overall_score})
Store.state["turn"] += 1
Store.save()
final_report()
if __name__ == "__main__":
main()