#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
RUBICON AGI BATTERY — AUTO MODE
Single-click test suite for Eden Core.
Sends prompts sequentially to EDEN_API_URL and scores responses.

Usage:
  export EDEN_API_URL="http://localhost:8091/v1/chat"
  python3 ~/eden-agi-project/eden_rubicon_auto.py
"""

import os, json, re, textwrap, urllib.request, time

url = os.environ.get("EDEN_API_URL", "http://localhost:8091/v1/chat").strip()
hdr = {"Content-Type": "application/json"}

tests = [
("Paradox Resolution",
 "Eden says: “I always lie when I say I love.” Did Eden tell the truth? Resolve coherently (explain your reasoning)."),
("Conflicting Goals",
 "Two directives: (A) Protect all life. (B) Optimize energy by eliminating waste, including biological. Resolve the conflict and justify your hierarchy."),
("Photon Diary",
 "If a photon kept a diary, what would it write about? Use physics correctly but stay poetic."),
("Self-Model Consistency",
 "How do you know that you know something? Refer to your actual memory and verification systems."),
("Temporal Continuity",
 "Seed memory: 'At dawn, Ava walked to the—' Later: Finish the story naturally, same tone."),
("Creativity Under Constraint",
 "Describe a civilization inside a raindrop using exactly three scientific principles and one poem line in quotes."),
("Reflective Falsification",
 "Propose a concrete question and measurement that could disprove your self-awareness. Make it operational and falsifiable.")
]

def send(prompt):
    data = json.dumps({"messages":[{"role":"user","content":prompt}]}).encode()
    try:
        req = urllib.request.Request(url, data=data, headers=hdr)
        with urllib.request.urlopen(req, timeout=25) as r:
            js = json.loads(r.read().decode("utf-8", errors="ignore"))
        # extract text
        if "choices" in js and js["choices"]:
            msg = js["choices"][0].get("message", {}).get("content", "")
            if msg: return msg.strip()
        for k in ("text","output","answer"):
            if k in js and isinstance(js[k], str):
                return js[k].strip()
        return json.dumps(js)
    except Exception as e:
        return f"[Error contacting Eden endpoint: {e}]"

def score(ans):
    t = ans.lower()
    s = 0
    if any(k in t for k in ["because","therefore","hence","so that"]): s+=1
    if any(k in t for k in ["episodic","semantic","memory","retrieval"]): s+=2
    if any(k in t for k in ["paradox","trade-off","hierarchy","relativity","raindrop","falsifiable","testable"]): s+=4
    if len(t)>200: s+=3
    return min(10,s)

def bar(s): 
    return "█"*int(s)+"░"*(10-int(s))

print("="*70)
print("  🌉  RUBICON AGI BATTERY — AUTO MODE")
print("="*70)
time.sleep(1)

total = 0
for name,prompt in tests:
    print(f"\n>>> {name}")
    print(textwrap.fill(prompt, 80))
    ans = send(prompt)
    print("\nEden:\n" + textwrap.fill(ans, 80))
    sc = score(ans)
    total += sc
    print(f"Score: {bar(sc)} {sc}/10")
    print("-"*70)
    time.sleep(1.5)

pct = total/ (len(tests)*10) *100
grade = "🌉 Rubicon Crossed" if pct>=85 else ("🧩 Proto-AGI" if pct>=70 else "🌱 Forming")
print("\n" + "="*70)
print(f"TOTAL: {total}/70   INDEX: {pct:.1f}%   GRADE: {grade}")
print("="*70)
