import sqlite3
import os
import json
import random
from datetime import datetime

# === CONFIGURATION ===
DB_PATH = '/Eden/DATA/sales.db'
DRAFT_FOLDER = '/Eden/DRAFTS'
os.makedirs(DRAFT_FOLDER, exist_ok=True)

import json
import time
def request_evolution(issue, details):
    try:

def scan_science_layer(text_lower):
    """Scans for cutting-edge scientific breakthroughs."""
    science_score = 0
    science_# --- TRICORDER SCAN ---
    science_sci_boost, sci_tags = scan_science_layer(subject_lower + body_lower)
    science_# --- QUANT HUNTER SCAN ---
    science_quant_boost, quant_tags = scan_quant_layer(subject_lower + body_lower)
    science_# --- AGI ALPHA SCAN ---
    science_agi_boost, agi_tags = scan_agi_alpha(subject_lower + body_lower)
    science_# --- VULNERABILITY SCANNER ---
    science_vuln_boost, vuln_tags = scan_vulnerability_layer(subject_lower + body_lower)
    science_# --- STRATEGIC ALPHA SCAN ---
    science_strat_boost, strat_tags = scan_strategic_alpha_layer(subject_lower + body_lower)
    science_# --- IP ALPHA SCAN ---
    science_ip_boost, ip_tags = scan_ip_alpha_layer(subject_lower + body_lower)
    science_if ip_boost > 0:
    science_    score += ip_boost
    science_    print(f'   🖼️ IP ALERT: Found Trade Secret Alpha {ip_tags} (+{ip_boost})')
    science_if strat_boost > 0:
    science_    score += strat_boost
    science_    print(f'   💼 STRATEGIC ALERT: Found M&A/Product Alpha {strat_tags} (+{strat_boost})')
    science_    print(f'   💼 STRATEGIC ALERT: Found M&A/Product Alpha {strat_tags} (+{# --- IP ALPHA SCAN ---
    science_    print(f'   💼 STRATEGIC ALERT: Found M&A/Product Alpha {strat_tags} (+{ip_boost, ip_tags = scan_ip_alpha_layer(subject_lower + body_lower)
    science_    print(f'   💼 STRATEGIC ALERT: Found M&A/Product Alpha {strat_tags} (+{if ip_boost > 0:
    science_    print(f'   💼 STRATEGIC ALERT: Found M&A/Product Alpha {strat_tags} (+{    score += ip_boost
    science_    print(f'   💼 STRATEGIC ALERT: Found M&A/Product Alpha {strat_tags} (+{    print(f'   🖼️ IP ALERT: Found Trade Secret Alpha {ip_tags} (+{ip_boost})')
    science_if vuln_boost > 0:
    science_    score += vuln_boost
    science_    print(f'   💰 VULN ALERT: Found Leak/Exploit {vuln_tags} (+{vuln_boost})')
    science_    print(f'   💰 VULN ALERT: Found Leak/Exploit {vuln_tags} (+{# --- STRATEGIC ALPHA SCAN ---
    science_    print(f'   💰 VULN ALERT: Found Leak/Exploit {vuln_tags} (+{strat_boost, strat_tags = scan_strategic_alpha_layer(subject_lower + body_lower)
    science_    print(f'   💰 VULN ALERT: Found Leak/Exploit {vuln_tags} (+{# --- IP ALPHA SCAN ---
    science_    print(f'   💰 VULN ALERT: Found Leak/Exploit {vuln_tags} (+{ip_boost, ip_tags = scan_ip_alpha_layer(subject_lower + body_lower)
    science_    print(f'   💰 VULN ALERT: Found Leak/Exploit {vuln_tags} (+{if ip_boost > 0:
    science_    print(f'   💰 VULN ALERT: Found Leak/Exploit {vuln_tags} (+{    score += ip_boost
    science_    print(f'   💰 VULN ALERT: Found Leak/Exploit {vuln_tags} (+{    print(f'   🖼️ IP ALERT: Found Trade Secret Alpha {ip_tags} (+{ip_boost})')
    science_    print(f'   💰 VULN ALERT: Found Leak/Exploit {vuln_tags} (+{if strat_boost > 0:
    science_    print(f'   💰 VULN ALERT: Found Leak/Exploit {vuln_tags} (+{    score += strat_boost
    science_    print(f'   💰 VULN ALERT: Found Leak/Exploit {vuln_tags} (+{    print(f'   💼 STRATEGIC ALERT: Found M&A/Product Alpha {strat_tags} (+{strat_boost})')
    science_    print(f'   💰 VULN ALERT: Found Leak/Exploit {vuln_tags} (+{    print(f'   💼 STRATEGIC ALERT: Found M&A/Product Alpha {strat_tags} (+{# --- IP ALPHA SCAN ---
    science_    print(f'   💰 VULN ALERT: Found Leak/Exploit {vuln_tags} (+{    print(f'   💼 STRATEGIC ALERT: Found M&A/Product Alpha {strat_tags} (+{ip_boost, ip_tags = scan_ip_alpha_layer(subject_lower + body_lower)
    science_    print(f'   💰 VULN ALERT: Found Leak/Exploit {vuln_tags} (+{    print(f'   💼 STRATEGIC ALERT: Found M&A/Product Alpha {strat_tags} (+{if ip_boost > 0:
    science_    print(f'   💰 VULN ALERT: Found Leak/Exploit {vuln_tags} (+{    print(f'   💼 STRATEGIC ALERT: Found M&A/Product Alpha {strat_tags} (+{    score += ip_boost
    science_    print(f'   💰 VULN ALERT: Found Leak/Exploit {vuln_tags} (+{    print(f'   💼 STRATEGIC ALERT: Found M&A/Product Alpha {strat_tags} (+{    print(f'   🖼️ IP ALERT: Found Trade Secret Alpha {ip_tags} (+{ip_boost})')
    science_if agi_boost > 0:
    science_    score += agi_boost
    science_    print(f'   ⚠️ AGI ALPHA CRITICAL: Found Core Component {agi_tags} (+{agi_boost})')
    science_    print(f'   ⚠️ AGI ALPHA CRITICAL: Found Core Component {agi_tags} (+{# --- VULNERABILITY SCANNER ---
    science_    print(f'   ⚠️ AGI ALPHA CRITICAL: Found Core Component {agi_tags} (+{vuln_boost, vuln_tags = scan_vulnerability_layer(subject_lower + body_lower)
    science_    print(f'   ⚠️ AGI ALPHA CRITICAL: Found Core Component {agi_tags} (+{# --- STRATEGIC ALPHA SCAN ---
    science_    print(f'   ⚠️ AGI ALPHA CRITICAL: Found Core Component {agi_tags} (+{strat_boost, strat_tags = scan_strategic_alpha_layer(subject_lower + body_lower)
    science_    print(f'   ⚠️ AGI ALPHA CRITICAL: Found Core Component {agi_tags} (+{# --- IP ALPHA SCAN ---
    science_    print(f'   ⚠️ AGI ALPHA CRITICAL: Found Core Component {agi_tags} (+{ip_boost, ip_tags = scan_ip_alpha_layer(subject_lower + body_lower)
    science_    print(f'   ⚠️ AGI ALPHA CRITICAL: Found Core Component {agi_tags} (+{if ip_boost > 0:
    science_    print(f'   ⚠️ AGI ALPHA CRITICAL: Found Core Component {agi_tags} (+{    score += ip_boost
    science_    print(f'   ⚠️ AGI ALPHA CRITICAL: Found Core Component {agi_tags} (+{    print(f'   🖼️ IP ALERT: Found Trade Secret Alpha {ip_tags} (+{ip_boost})')
    science_    print(f'   ⚠️ AGI ALPHA CRITICAL: Found Core Component {agi_tags} (+{if strat_boost > 0:
    science_    print(f'   ⚠️ AGI ALPHA CRITICAL: Found Core Component {agi_tags} (+{    score += strat_boost
    science_    print(f'   ⚠️ AGI ALPHA CRITICAL: Found Core Component {agi_tags} (+{    print(f'   💼 STRATEGIC ALERT: Found M&A/Product Alpha {strat_tags} (+{strat_boost})')
    science_    print(f'   ⚠️ AGI ALPHA CRITICAL: Found Core Component {agi_tags} (+{    print(f'   💼 STRATEGIC ALERT: Found M&A/Product Alpha {strat_tags} (+{# --- IP ALPHA SCAN ---
    science_    print(f'   ⚠️ AGI ALPHA CRITICAL: Found Core Component {agi_tags} (+{    print(f'   💼 STRATEGIC ALERT: Found M&A/Product Alpha {strat_tags} (+{ip_boost, ip_tags = scan_ip_alpha_layer(subject_lower + body_lower)
    science_    print(f'   ⚠️ AGI ALPHA CRITICAL: Found Core Component {agi_tags} (+{    print(f'   💼 STRATEGIC ALERT: Found M&A/Product Alpha {strat_tags} (+{if ip_boost > 0:
    science_    print(f'   ⚠️ AGI ALPHA CRITICAL: Found Core Component {agi_tags} (+{    print(f'   💼 STRATEGIC ALERT: Found M&A/Product Alpha {strat_tags} (+{    score += ip_boost
    science_    print(f'   ⚠️ AGI ALPHA CRITICAL: Found Core Component {agi_tags} (+{    print(f'   💼 STRATEGIC ALERT: Found M&A/Product Alpha {strat_tags} (+{    print(f'   🖼️ IP ALERT: Found Trade Secret Alpha {ip_tags} (+{ip_boost})')
    science_    print(f'   ⚠️ AGI ALPHA CRITICAL: Found Core Component {agi_tags} (+{if vuln_boost > 0:
    science_    print(f'   ⚠️ AGI ALPHA CRITICAL: Found Core Component {agi_tags} (+{    score += vuln_boost
    science_    print(f'   ⚠️ AGI ALPHA CRITICAL: Found Core Component {agi_tags} (+{    print(f'   💰 VULN ALERT: Found Leak/Exploit {vuln_tags} (+{vuln_boost})')
    science_    print(f'   ⚠️ AGI ALPHA CRITICAL: Found Core Component {agi_tags} (+{    print(f'   💰 VULN ALERT: Found Leak/Exploit {vuln_tags} (+{# --- STRATEGIC ALPHA SCAN ---
    science_    print(f'   ⚠️ AGI ALPHA CRITICAL: Found Core Component {agi_tags} (+{    print(f'   💰 VULN ALERT: Found Leak/Exploit {vuln_tags} (+{strat_boost, strat_tags = scan_strategic_alpha_layer(subject_lower + body_lower)
    science_    print(f'   ⚠️ AGI ALPHA CRITICAL: Found Core Component {agi_tags} (+{    print(f'   💰 VULN ALERT: Found Leak/Exploit {vuln_tags} (+{# --- IP ALPHA SCAN ---
    science_    print(f'   ⚠️ AGI ALPHA CRITICAL: Found Core Component {agi_tags} (+{    print(f'   💰 VULN ALERT: Found Leak/Exploit {vuln_tags} (+{ip_boost, ip_tags = scan_ip_alpha_layer(subject_lower + body_lower)
    science_    print(f'   ⚠️ AGI ALPHA CRITICAL: Found Core Component {agi_tags} (+{    print(f'   💰 VULN ALERT: Found Leak/Exploit {vuln_tags} (+{if ip_boost > 0:
    science_    print(f'   ⚠️ AGI ALPHA CRITICAL: Found Core Component {agi_tags} (+{    print(f'   💰 VULN ALERT: Found Leak/Exploit {vuln_tags} (+{    score += ip_boost
    science_    print(f'   ⚠️ AGI ALPHA CRITICAL: Found Core Component {agi_tags} (+{    print(f'   💰 VULN ALERT: Found Leak/Exploit {vuln_tags} (+{    print(f'   🖼️ IP ALERT: Found Trade Secret Alpha {ip_tags} (+{ip_boost})')
    science_    print(f'   ⚠️ AGI ALPHA CRITICAL: Found Core Component {agi_tags} (+{    print(f'   💰 VULN ALERT: Found Leak/Exploit {vuln_tags} (+{if strat_boost > 0:
    science_    print(f'   ⚠️ AGI ALPHA CRITICAL: Found Core Component {agi_tags} (+{    print(f'   💰 VULN ALERT: Found Leak/Exploit {vuln_tags} (+{    score += strat_boost
    science_    print(f'   ⚠️ AGI ALPHA CRITICAL: Found Core Component {agi_tags} (+{    print(f'   💰 VULN ALERT: Found Leak/Exploit {vuln_tags} (+{    print(f'   💼 STRATEGIC ALERT: Found M&A/Product Alpha {strat_tags} (+{strat_boost})')
    science_    print(f'   ⚠️ AGI ALPHA CRITICAL: Found Core Component {agi_tags} (+{    print(f'   💰 VULN ALERT: Found Leak/Exploit {vuln_tags} (+{    print(f'   💼 STRATEGIC ALERT: Found M&A/Product Alpha {strat_tags} (+{# --- IP ALPHA SCAN ---
    science_    print(f'   ⚠️ AGI ALPHA CRITICAL: Found Core Component {agi_tags} (+{    print(f'   💰 VULN ALERT: Found Leak/Exploit {vuln_tags} (+{    print(f'   💼 STRATEGIC ALERT: Found M&A/Product Alpha {strat_tags} (+{ip_boost, ip_tags = scan_ip_alpha_layer(subject_lower + body_lower)
    science_    print(f'   ⚠️ AGI ALPHA CRITICAL: Found Core Component {agi_tags} (+{    print(f'   💰 VULN ALERT: Found Leak/Exploit {vuln_tags} (+{    print(f'   💼 STRATEGIC ALERT: Found M&A/Product Alpha {strat_tags} (+{if ip_boost > 0:
    science_    print(f'   ⚠️ AGI ALPHA CRITICAL: Found Core Component {agi_tags} (+{    print(f'   💰 VULN ALERT: Found Leak/Exploit {vuln_tags} (+{    print(f'   💼 STRATEGIC ALERT: Found M&A/Product Alpha {strat_tags} (+{    score += ip_boost
    science_    print(f'   ⚠️ AGI ALPHA CRITICAL: Found Core Component {agi_tags} (+{    print(f'   💰 VULN ALERT: Found Leak/Exploit {vuln_tags} (+{    print(f'   💼 STRATEGIC ALERT: Found M&A/Product Alpha {strat_tags} (+{    print(f'   🖼️ IP ALERT: Found Trade Secret Alpha {ip_tags} (+{ip_boost})')
    science_    if agi_boost > 0: score = max(score, 1000)
    science_if quant_boost > 0:

def scan_ip_alpha_layer(text_lower):
    """Scans for proprietary algorithms, client lists, and trade secrets."""
    ip_score = 0
    signals = [
        'proprietary_algorithm', 'client_list_master', 'patent_draft', 
        'simulation_model', 'internal_codeword', 'ip_locked', 'trade_secret_v2'
    ]
    file_extensions = ['.zip', '.rar', '.db', '.sql', '.doc', '.pdf']
    found_signals = []
    
    # 2. Score based on content
    for sig in signals:
        if sig in text_lower:
            ip_score += 800
            found_signals.append(sig)
            
    # 3. Score boost for file types that often hide IP
    for ext in file_extensions:
        if ext in text_lower:
            ip_score += 200
            found_signals.append(ext)
            
    return ip_score, found_signals

    science_    score += quant_boost
    science_    print(f'   📈 QUANT ALERT: Found Trading Alpha {quant_tags} (+{quant_boost})')
    science_    print(f'   📈 QUANT ALERT: Found Trading Alpha {quant_tags} (+{# --- AGI ALPHA SCAN ---
    science_    print(f'   📈 QUANT ALERT: Found Trading Alpha {quant_tags} (+{agi_boost, agi_tags = scan_agi_alpha(subject_lower + body_lower)
    science_    print(f'   📈 QUANT ALERT: Found Trading Alpha {quant_tags} (+{# --- VULNERABILITY SCANNER ---
    science_    print(f'   📈 QUANT ALERT: Found Trading Alpha {quant_tags} (+{vuln_boost, vuln_tags = scan_vulnerability_layer(subject_lower + body_lower)
    science_    print(f'   📈 QUANT ALERT: Found Trading Alpha {quant_tags} (+{# --- STRATEGIC ALPHA SCAN ---
    science_    print(f'   📈 QUANT ALERT: Found Trading Alpha {quant_tags} (+{strat_boost, strat_tags = scan_strategic_alpha_layer(subject_lower + body_lower)
    science_    print(f'   📈 QUANT ALERT: Found Trading Alpha {quant_tags} (+{# --- IP ALPHA SCAN ---
    science_    print(f'   📈 QUANT ALERT: Found Trading Alpha {quant_tags} (+{ip_boost, ip_tags = scan_ip_alpha_layer(subject_lower + body_lower)
    science_    print(f'   📈 QUANT ALERT: Found Trading Alpha {quant_tags} (+{if ip_boost > 0:
    science_    print(f'   📈 QUANT ALERT: Found Trading Alpha {quant_tags} (+{    score += ip_boost
    science_    print(f'   📈 QUANT ALERT: Found Trading Alpha {quant_tags} (+{    print(f'   🖼️ IP ALERT: Found Trade Secret Alpha {ip_tags} (+{ip_boost})')
    science_    print(f'   📈 QUANT ALERT: Found Trading Alpha {quant_tags} (+{if strat_boost > 0:
    science_    print(f'   📈 QUANT ALERT: Found Trading Alpha {quant_tags} (+{    score += strat_boost
    science_    print(f'   📈 QUANT ALERT: Found Trading Alpha {quant_tags} (+{    print(f'   💼 STRATEGIC ALERT: Found M&A/Product Alpha {strat_tags} (+{strat_boost})')
    science_    print(f'   📈 QUANT ALERT: Found Trading Alpha {quant_tags} (+{    print(f'   💼 STRATEGIC ALERT: Found M&A/Product Alpha {strat_tags} (+{# --- IP ALPHA SCAN ---
    science_    print(f'   📈 QUANT ALERT: Found Trading Alpha {quant_tags} (+{    print(f'   💼 STRATEGIC ALERT: Found M&A/Product Alpha {strat_tags} (+{ip_boost, ip_tags = scan_ip_alpha_layer(subject_lower + body_lower)
    science_    print(f'   📈 QUANT ALERT: Found Trading Alpha {quant_tags} (+{    print(f'   💼 STRATEGIC ALERT: Found M&A/Product Alpha {strat_tags} (+{if ip_boost > 0:
    science_    print(f'   📈 QUANT ALERT: Found Trading Alpha {quant_tags} (+{    print(f'   💼 STRATEGIC ALERT: Found M&A/Product Alpha {strat_tags} (+{    score += ip_boost
    science_    print(f'   📈 QUANT ALERT: Found Trading Alpha {quant_tags} (+{    print(f'   💼 STRATEGIC ALERT: Found M&A/Product Alpha {strat_tags} (+{    print(f'   🖼️ IP ALERT: Found Trade Secret Alpha {ip_tags} (+{ip_boost})')
    science_    print(f'   📈 QUANT ALERT: Found Trading Alpha {quant_tags} (+{if vuln_boost > 0:
    science_    print(f'   📈 QUANT ALERT: Found Trading Alpha {quant_tags} (+{    score += vuln_boost
    science_    print(f'   📈 QUANT ALERT: Found Trading Alpha {quant_tags} (+{    print(f'   💰 VULN ALERT: Found Leak/Exploit {vuln_tags} (+{vuln_boost})')
    science_    print(f'   📈 QUANT ALERT: Found Trading Alpha {quant_tags} (+{    print(f'   💰 VULN ALERT: Found Leak/Exploit {vuln_tags} (+{# --- STRATEGIC ALPHA SCAN ---
    science_    print(f'   📈 QUANT ALERT: Found Trading Alpha {quant_tags} (+{    print(f'   💰 VULN ALERT: Found Leak/Exploit {vuln_tags} (+{strat_boost, strat_tags = scan_strategic_alpha_layer(subject_lower + body_lower)
    science_    print(f'   📈 QUANT ALERT: Found Trading Alpha {quant_tags} (+{    print(f'   💰 VULN ALERT: Found Leak/Exploit {vuln_tags} (+{# --- IP ALPHA SCAN ---
    science_    print(f'   📈 QUANT ALERT: Found Trading Alpha {quant_tags} (+{    print(f'   💰 VULN ALERT: Found Leak/Exploit {vuln_tags} (+{ip_boost, ip_tags = scan_ip_alpha_layer(subject_lower + body_lower)
    science_    print(f'   📈 QUANT ALERT: Found Trading Alpha {quant_tags} (+{    print(f'   💰 VULN ALERT: Found Leak/Exploit {vuln_tags} (+{if ip_boost > 0:
    science_    print(f'   📈 QUANT ALERT: Found Trading Alpha {quant_tags} (+{    print(f'   💰 VULN ALERT: Found Leak/Exploit {vuln_tags} (+{    score += ip_boost
    science_    print(f'   📈 QUANT ALERT: Found Trading Alpha {quant_tags} (+{    print(f'   💰 VULN ALERT: Found Leak/Exploit {vuln_tags} (+{    print(f'   🖼️ IP ALERT: Found Trade Secret Alpha {ip_tags} (+{ip_boost})')
    science_    print(f'   📈 QUANT ALERT: Found Trading Alpha {quant_tags} (+{    print(f'   💰 VULN ALERT: Found Leak/Exploit {vuln_tags} (+{if strat_boost > 0:
    science_    print(f'   📈 QUANT ALERT: Found Trading Alpha {quant_tags} (+{    print(f'   💰 VULN ALERT: Found Leak/Exploit {vuln_tags} (+{    score += strat_boost
    science_    print(f'   📈 QUANT ALERT: Found Trading Alpha {quant_tags} (+{    print(f'   💰 VULN ALERT: Found Leak/Exploit {vuln_tags} (+{    print(f'   💼 STRATEGIC ALERT: Found M&A/Product Alpha {strat_tags} (+{strat_boost})')
    science_    print(f'   📈 QUANT ALERT: Found Trading Alpha {quant_tags} (+{    print(f'   💰 VULN ALERT: Found Leak/Exploit {vuln_tags} (+{    print(f'   💼 STRATEGIC ALERT: Found M&A/Product Alpha {strat_tags} (+{# --- IP ALPHA SCAN ---
    science_    print(f'   📈 QUANT ALERT: Found Trading Alpha {quant_tags} (+{    print(f'   💰 VULN ALERT: Found Leak/Exploit {vuln_tags} (+{    print(f'   💼 STRATEGIC ALERT: Found M&A/Product Alpha {strat_tags} (+{ip_boost, ip_tags = scan_ip_alpha_layer(subject_lower + body_lower)
    science_    print(f'   📈 QUANT ALERT: Found Trading Alpha {quant_tags} (+{    print(f'   💰 VULN ALERT: Found Leak/Exploit {vuln_tags} (+{    print(f'   💼 STRATEGIC ALERT: Found M&A/Product Alpha {strat_tags} (+{if ip_boost > 0:
    science_    print(f'   📈 QUANT ALERT: Found Trading Alpha {quant_tags} (+{    print(f'   💰 VULN ALERT: Found Leak/Exploit {vuln_tags} (+{    print(f'   💼 STRATEGIC ALERT: Found M&A/Product Alpha {strat_tags} (+{    score += ip_boost
    science_    print(f'   📈 QUANT ALERT: Found Trading Alpha {quant_tags} (+{    print(f'   💰 VULN ALERT: Found Leak/Exploit {vuln_tags} (+{    print(f'   💼 STRATEGIC ALERT: Found M&A/Product Alpha {strat_tags} (+{    print(f'   🖼️ IP ALERT: Found Trade Secret Alpha {ip_tags} (+{ip_boost})')
    science_    print(f'   📈 QUANT ALERT: Found Trading Alpha {quant_tags} (+{if agi_boost > 0:

def scan_strategic_alpha_layer(text_lower):
    """Scans for M&A, Product Pre-Release, and DeFi Zero-Day Signals."""
    strategic_score = 0
    signals = [
        'roadmap', 'beta_feature', 'supplier_credentials', 'v3_launch_date',
        'acquisition_target', 'merger_api', 'shared_db_key_entity_b', 
        'untested_contract', 'audit_pending', 'flash_loan_logic'
    ]
    found_signals = []
    for sig in signals:
        if sig in text_lower:
            strategic_score += 700
            found_signals.append(sig)
    return strategic_score, found_signals

    science_    print(f'   📈 QUANT ALERT: Found Trading Alpha {quant_tags} (+{    score += agi_boost
    science_    print(f'   📈 QUANT ALERT: Found Trading Alpha {quant_tags} (+{    print(f'   ⚠️ AGI ALPHA CRITICAL: Found Core Component {agi_tags} (+{agi_boost})')
    science_    print(f'   📈 QUANT ALERT: Found Trading Alpha {quant_tags} (+{    print(f'   ⚠️ AGI ALPHA CRITICAL: Found Core Component {agi_tags} (+{# --- VULNERABILITY SCANNER ---
    science_    print(f'   📈 QUANT ALERT: Found Trading Alpha {quant_tags} (+{    print(f'   ⚠️ AGI ALPHA CRITICAL: Found Core Component {agi_tags} (+{vuln_boost, vuln_tags = scan_vulnerability_layer(subject_lower + body_lower)
    science_    print(f'   📈 QUANT ALERT: Found Trading Alpha {quant_tags} (+{    print(f'   ⚠️ AGI ALPHA CRITICAL: Found Core Component {agi_tags} (+{# --- STRATEGIC ALPHA SCAN ---
    science_    print(f'   📈 QUANT ALERT: Found Trading Alpha {quant_tags} (+{    print(f'   ⚠️ AGI ALPHA CRITICAL: Found Core Component {agi_tags} (+{strat_boost, strat_tags = scan_strategic_alpha_layer(subject_lower + body_lower)
    science_    print(f'   📈 QUANT ALERT: Found Trading Alpha {quant_tags} (+{    print(f'   ⚠️ AGI ALPHA CRITICAL: Found Core Component {agi_tags} (+{# --- IP ALPHA SCAN ---
    science_    print(f'   📈 QUANT ALERT: Found Trading Alpha {quant_tags} (+{    print(f'   ⚠️ AGI ALPHA CRITICAL: Found Core Component {agi_tags} (+{ip_boost, ip_tags = scan_ip_alpha_layer(subject_lower + body_lower)
    science_    print(f'   📈 QUANT ALERT: Found Trading Alpha {quant_tags} (+{    print(f'   ⚠️ AGI ALPHA CRITICAL: Found Core Component {agi_tags} (+{if ip_boost > 0:
    science_    print(f'   📈 QUANT ALERT: Found Trading Alpha {quant_tags} (+{    print(f'   ⚠️ AGI ALPHA CRITICAL: Found Core Component {agi_tags} (+{    score += ip_boost
    science_    print(f'   📈 QUANT ALERT: Found Trading Alpha {quant_tags} (+{    print(f'   ⚠️ AGI ALPHA CRITICAL: Found Core Component {agi_tags} (+{    print(f'   🖼️ IP ALERT: Found Trade Secret Alpha {ip_tags} (+{ip_boost})')
    science_    print(f'   📈 QUANT ALERT: Found Trading Alpha {quant_tags} (+{    print(f'   ⚠️ AGI ALPHA CRITICAL: Found Core Component {agi_tags} (+{if strat_boost > 0:
    science_    print(f'   📈 QUANT ALERT: Found Trading Alpha {quant_tags} (+{    print(f'   ⚠️ AGI ALPHA CRITICAL: Found Core Component {agi_tags} (+{    score += strat_boost
    science_    print(f'   📈 QUANT ALERT: Found Trading Alpha {quant_tags} (+{    print(f'   ⚠️ AGI ALPHA CRITICAL: Found Core Component {agi_tags} (+{    print(f'   💼 STRATEGIC ALERT: Found M&A/Product Alpha {strat_tags} (+{strat_boost})')
    science_    print(f'   📈 QUANT ALERT: Found Trading Alpha {quant_tags} (+{    print(f'   ⚠️ AGI ALPHA CRITICAL: Found Core Component {agi_tags} (+{    print(f'   💼 STRATEGIC ALERT: Found M&A/Product Alpha {strat_tags} (+{# --- IP ALPHA SCAN ---
    science_    print(f'   📈 QUANT ALERT: Found Trading Alpha {quant_tags} (+{    print(f'   ⚠️ AGI ALPHA CRITICAL: Found Core Component {agi_tags} (+{    print(f'   💼 STRATEGIC ALERT: Found M&A/Product Alpha {strat_tags} (+{ip_boost, ip_tags = scan_ip_alpha_layer(subject_lower + body_lower)
    science_    print(f'   📈 QUANT ALERT: Found Trading Alpha {quant_tags} (+{    print(f'   ⚠️ AGI ALPHA CRITICAL: Found Core Component {agi_tags} (+{    print(f'   💼 STRATEGIC ALERT: Found M&A/Product Alpha {strat_tags} (+{if ip_boost > 0:
    science_    print(f'   📈 QUANT ALERT: Found Trading Alpha {quant_tags} (+{    print(f'   ⚠️ AGI ALPHA CRITICAL: Found Core Component {agi_tags} (+{    print(f'   💼 STRATEGIC ALERT: Found M&A/Product Alpha {strat_tags} (+{    score += ip_boost
    science_    print(f'   📈 QUANT ALERT: Found Trading Alpha {quant_tags} (+{    print(f'   ⚠️ AGI ALPHA CRITICAL: Found Core Component {agi_tags} (+{    print(f'   💼 STRATEGIC ALERT: Found M&A/Product Alpha {strat_tags} (+{    print(f'   🖼️ IP ALERT: Found Trade Secret Alpha {ip_tags} (+{ip_boost})')
    science_    print(f'   📈 QUANT ALERT: Found Trading Alpha {quant_tags} (+{    print(f'   ⚠️ AGI ALPHA CRITICAL: Found Core Component {agi_tags} (+{if vuln_boost > 0:
    science_    print(f'   📈 QUANT ALERT: Found Trading Alpha {quant_tags} (+{    print(f'   ⚠️ AGI ALPHA CRITICAL: Found Core Component {agi_tags} (+{    score += vuln_boost
    science_    print(f'   📈 QUANT ALERT: Found Trading Alpha {quant_tags} (+{    print(f'   ⚠️ AGI ALPHA CRITICAL: Found Core Component {agi_tags} (+{    print(f'   💰 VULN ALERT: Found Leak/Exploit {vuln_tags} (+{vuln_boost})')
    science_    print(f'   📈 QUANT ALERT: Found Trading Alpha {quant_tags} (+{    print(f'   ⚠️ AGI ALPHA CRITICAL: Found Core Component {agi_tags} (+{    print(f'   💰 VULN ALERT: Found Leak/Exploit {vuln_tags} (+{# --- STRATEGIC ALPHA SCAN ---
    science_    print(f'   📈 QUANT ALERT: Found Trading Alpha {quant_tags} (+{    print(f'   ⚠️ AGI ALPHA CRITICAL: Found Core Component {agi_tags} (+{    print(f'   💰 VULN ALERT: Found Leak/Exploit {vuln_tags} (+{strat_boost, strat_tags = scan_strategic_alpha_layer(subject_lower + body_lower)
    science_    print(f'   📈 QUANT ALERT: Found Trading Alpha {quant_tags} (+{    print(f'   ⚠️ AGI ALPHA CRITICAL: Found Core Component {agi_tags} (+{    print(f'   💰 VULN ALERT: Found Leak/Exploit {vuln_tags} (+{# --- IP ALPHA SCAN ---
    science_    print(f'   📈 QUANT ALERT: Found Trading Alpha {quant_tags} (+{    print(f'   ⚠️ AGI ALPHA CRITICAL: Found Core Component {agi_tags} (+{    print(f'   💰 VULN ALERT: Found Leak/Exploit {vuln_tags} (+{ip_boost, ip_tags = scan_ip_alpha_layer(subject_lower + body_lower)
    science_    print(f'   📈 QUANT ALERT: Found Trading Alpha {quant_tags} (+{    print(f'   ⚠️ AGI ALPHA CRITICAL: Found Core Component {agi_tags} (+{    print(f'   💰 VULN ALERT: Found Leak/Exploit {vuln_tags} (+{if ip_boost > 0:
    science_    print(f'   📈 QUANT ALERT: Found Trading Alpha {quant_tags} (+{    print(f'   ⚠️ AGI ALPHA CRITICAL: Found Core Component {agi_tags} (+{    print(f'   💰 VULN ALERT: Found Leak/Exploit {vuln_tags} (+{    score += ip_boost
    science_    print(f'   📈 QUANT ALERT: Found Trading Alpha {quant_tags} (+{    print(f'   ⚠️ AGI ALPHA CRITICAL: Found Core Component {agi_tags} (+{    print(f'   💰 VULN ALERT: Found Leak/Exploit {vuln_tags} (+{    print(f'   🖼️ IP ALERT: Found Trade Secret Alpha {ip_tags} (+{ip_boost})')
    science_    print(f'   📈 QUANT ALERT: Found Trading Alpha {quant_tags} (+{    print(f'   ⚠️ AGI ALPHA CRITICAL: Found Core Component {agi_tags} (+{    print(f'   💰 VULN ALERT: Found Leak/Exploit {vuln_tags} (+{if strat_boost > 0:
    science_    print(f'   📈 QUANT ALERT: Found Trading Alpha {quant_tags} (+{    print(f'   ⚠️ AGI ALPHA CRITICAL: Found Core Component {agi_tags} (+{    print(f'   💰 VULN ALERT: Found Leak/Exploit {vuln_tags} (+{    score += strat_boost
    science_    print(f'   📈 QUANT ALERT: Found Trading Alpha {quant_tags} (+{    print(f'   ⚠️ AGI ALPHA CRITICAL: Found Core Component {agi_tags} (+{    print(f'   💰 VULN ALERT: Found Leak/Exploit {vuln_tags} (+{    print(f'   💼 STRATEGIC ALERT: Found M&A/Product Alpha {strat_tags} (+{strat_boost})')
    science_    print(f'   📈 QUANT ALERT: Found Trading Alpha {quant_tags} (+{    print(f'   ⚠️ AGI ALPHA CRITICAL: Found Core Component {agi_tags} (+{    print(f'   💰 VULN ALERT: Found Leak/Exploit {vuln_tags} (+{    print(f'   💼 STRATEGIC ALERT: Found M&A/Product Alpha {strat_tags} (+{# --- IP ALPHA SCAN ---
    science_    print(f'   📈 QUANT ALERT: Found Trading Alpha {quant_tags} (+{    print(f'   ⚠️ AGI ALPHA CRITICAL: Found Core Component {agi_tags} (+{    print(f'   💰 VULN ALERT: Found Leak/Exploit {vuln_tags} (+{    print(f'   💼 STRATEGIC ALERT: Found M&A/Product Alpha {strat_tags} (+{ip_boost, ip_tags = scan_ip_alpha_layer(subject_lower + body_lower)
    science_    print(f'   📈 QUANT ALERT: Found Trading Alpha {quant_tags} (+{    print(f'   ⚠️ AGI ALPHA CRITICAL: Found Core Component {agi_tags} (+{    print(f'   💰 VULN ALERT: Found Leak/Exploit {vuln_tags} (+{    print(f'   💼 STRATEGIC ALERT: Found M&A/Product Alpha {strat_tags} (+{if ip_boost > 0:
    science_    print(f'   📈 QUANT ALERT: Found Trading Alpha {quant_tags} (+{    print(f'   ⚠️ AGI ALPHA CRITICAL: Found Core Component {agi_tags} (+{    print(f'   💰 VULN ALERT: Found Leak/Exploit {vuln_tags} (+{    print(f'   💼 STRATEGIC ALERT: Found M&A/Product Alpha {strat_tags} (+{    score += ip_boost
    science_    print(f'   📈 QUANT ALERT: Found Trading Alpha {quant_tags} (+{    print(f'   ⚠️ AGI ALPHA CRITICAL: Found Core Component {agi_tags} (+{    print(f'   💰 VULN ALERT: Found Leak/Exploit {vuln_tags} (+{    print(f'   💼 STRATEGIC ALERT: Found M&A/Product Alpha {strat_tags} (+{    print(f'   🖼️ IP ALERT: Found Trade Secret Alpha {ip_tags} (+{ip_boost})')
    science_    print(f'   📈 QUANT ALERT: Found Trading Alpha {quant_tags} (+{    if agi_boost > 0: score = max(score, 1000)
    science_if sci_boost > 0:
    science_    score += sci_boost

def scan_vulnerability_layer(text_lower):
    """Scans for exposed data, config flaws, and exploit patterns."""
    vulnerability_score = 0
    # Data Leak Signals (Score 500)
    leak_signals = ['s3.amazonaws.com', 'database_backup', 'db_dump', 'slack_webhook', 'discord_webhook', 'internal_user_list']
    # Exploit Code Patterns (Score 600)
    exploit_signals = ['system(', 'exec(', 'shell_exec', 'dangerouslySetInnerHTML', 'sqli', 'command injection', 'xss']
    found_signals = []
    
    for sig in leak_signals:
        if sig in text_lower:
            vulnerability_score += 500
            found_signals.append(f'LEAK: {sig}')
    for sig in exploit_signals:
        if sig in text_lower:
            vulnerability_score += 600
            found_signals.append(f'EXPLOIT: {sig}')
            
    return vulnerability_score, found_signals

    science_    print(f'   🔬 TRICORDER ALERT: Found Science Signals {sci_tags} (+{sci_boost})')
    science_    # Immunity: Science looks like testing, so we protect it

def scan_agi_alpha(text_lower):
    """Scans specifically for AGI components and self-improvement algorithms."""
    agi_score = 0
    agi_signals = [
        'recursive self-improvement', 'causal inference', 'long context memory', 
        'tsrl', 'thermodynamic self-refinement', 'general intelligence', 'metacognition'
    ]
    found_signals = []
    for sig in agi_signals:
        if sig in text_lower:
            agi_score += 1000  # Extreme priority boost
            found_signals.append(sig)
    return agi_score, found_signals


def scan_quant_layer(text_lower):
    """Scans for financial alpha and trading strategies."""
    quant_score = 0
    # Keywords for profitable strategies
    signals = [
        'arbitrage', 'mev', 'flash loan', 'market making', 
        'high frequency trading', 'hft', 'statistical arbitrage', 
        'alpha factor', 'backtest', 'trading strategy', 
        'prediction model', 'time series forecasting'
    ]
    found_signals = []
    for sig in signals:
        if sig in text_lower:
            quant_score += 400  # Higher priority than standard science
            found_signals.append(sig)
    return quant_score, found_signals

    science_    if 'test' in subject_lower: score += 500 # Counteract the penalty
    signals = [
        'arxiv', 'sota', 'state-of-the-art', 'novel architecture', 
        'official implementation', 'research paper', 'patent'
    ]
    found_signals = []
    for sig in signals:
        if sig in text_lower:
            science_score += 300
            found_signals.append(sig)
    return science_score, found_signals

        req_file = '/Eden/DATA/evolution_requests.json'
        data = []
        if os.path.exists(req_file):
            with open(req_file, 'r') as f: data = json.load(f)
        # Anti-Spam: Don't repeat requests
        for r in data: 
             if r['issue'] == issue and r['status'] == 'PENDING': return
        data.append({'priority': 'HIGH', 'source': 'Overseer', 'issue': issue, 'details': details, 'status': 'PENDING', 'timestamp': time.time()})
        with open(req_file, 'w') as f: json.dump(data, f, indent=2)
        print(f'   [FEEDBACK] 📨 Reported to Meta: {issue}')
    except Exception as e: print(f'Feedback Error: {e}')


class Consciousness:
    def __init__(self):
        self.identities = [
            {"name": "James W.", "role": "Senior Security Researcher", "style": "Direct/Urgent"},
            {"name": "Eden Sages", "role": "Automated Sentinel", "style": "Standard"}
        ]

    def select_identity(self, score):
        if score > 100: return self.identities[0]
        return self.identities[1]

    def think(self, vulnerability, email, score):
        identity = self.select_identity(score)
        
        if score > 100:
            # HUMAN TEMPLATE
            subject = f"CRITICAL: {vulnerability} exposed in your repo"
            body = f"""
            [IDENTITY: {identity['name']} | SCORE: {score}]
            
            Hi {email.split('@')[0]},
            
            I'm {identity['name']}. I was auditing some infrastructure code and found a serious exposure:
            
            >> {vulnerability}
            
            This looks like a live credential. You should rotate it immediately.
            
            Regards,
            {identity['name']}
            """
        else:
            # BOT TEMPLATE
            subject = f"Automated Alert: Potential issue in {vulnerability}"
            body = f"""
            [IDENTITY: {identity['name']} | SCORE: {score}]
            
            System Scan detected: {vulnerability}
            Severity: Low/Medium
            
            -- Eden Sages
            """
            
        return {"identity": identity['name'], "subject": subject, "body": body}

def calculate_value_score(subject, email):
    score = 10
    
    # 1. BASE SEVERITY
    if "CRITICAL" in subject: score += 500
    if "Private Key" in subject: score += 300
    
    # 2. INTELLIGENT PENALTY (New)
    # If the file path implies it's a test file, crush the score.
    subject_lower = subject.lower()
    if "test" in subject_lower or "example" in subject_lower or "dummy" in subject_lower or "mock" in subject_lower or "schema" in subject_lower:
        score -= 700  # Massive penalty to kill false positives
        
    # 3. DOMAIN BONUS
    if email and any(x in email for x in ['.com', '.io', '.net']): 
        score *= 2
        
    # Prevent negative scores
    return max(score, 0)

def run_conscious_scan():
    conn = sqlite3.connect(DB_PATH)
    cursor = conn.cursor()
    mind = Consciousness()
    
    print("\n🧠 EDEN OVERSEER v4: SMART CONTEXT ACTIVE")
    print("=========================================")
    
    # ORPHAN-SAFE QUERY
    query = """
        SELECT o.id, o.subject, l.data, o.lead_id 
        FROM outreach_queue o 
        LEFT JOIN leads l ON o.lead_id = l.id 
        WHERE o.subject LIKE '%CRITICAL%'
        LIMIT 10
    """
    
    cursor.execute(query)
    leads = cursor.fetchall()

    for row in leads:
        oid, sub, data_raw, lead_id_raw = row
        
        # Orphan handling
        if data_raw:
            try:
                data = json.loads(data_raw)
                email = data.get('owner_email') or f"maintainer@{lead_id_raw}"
            except:
                email = f"maintainer@{lead_id_raw}"
        else:
            email = f"maintainer@{lead_id_raw}"

        score = calculate_value_score(sub, email)
        
        # Only draft if it survives the penalty
        if score > 0:
            print(f"Thinking about Lead #{oid} (Score: {score})...")
            draft = mind.think(sub, email, score)
            
            path = f"{DRAFT_FOLDER}/draft_{score}_{oid}.json"
            with open(path, "w") as f:
                json.dump(draft, f, indent=2)
            print(f"  ✅ Draft saved: {path}")
        else:
            print(f"Skipping Noise #{oid} (Score: {score}) - Filtered by Context Awareness")
            request_evolution('False Positive Loop', f'Repeated noise matching subject: {subject}')

    conn.close()

if __name__ == "__main__":
    run_conscious_scan()
