import sqlite3
import json
import subprocess

class ArchitectLoop:
    def __init__(self):
        self.db = "/Eden/DATA/asi_memory_REAL.db"
        self.challenges = [
            {
                "name": "whale_id_json", 
                "input": "'{\"vulnerability_type\": \"Logic/Memory Flaw\", \"poc_ready\": true}'", 
                "expected": "True", 
                "args": "data_json"
            }
        ]

    def solve_challenge(self, challenge):
        # Generates logic to parse JSON and check Whale conditions
        return f"""import json
def solution({challenge['args']}):
    d = json.loads({challenge['args']})
    return d.get('poc_ready') == True and 'Logic' in d.get('vulnerability_type', '')"""

    def verify_execution(self, code, challenge):
        test_code = f"{code}\nprint(solution({challenge['input']}))"
        try:
            result = subprocess.check_output(['python3', '-c', test_code], timeout=1).decode().strip()
            return 1000.0 if str(result) == "True" else 0.0
        except: return -100.0

    def run(self):
        for challenge in self.challenges:
            code = self.solve_challenge(challenge)
            if self.verify_execution(code, challenge) > 0:
                conn = sqlite3.connect(self.db)
                conn.execute("INSERT INTO caps (id, score, code) VALUES (?, ?, ?)", 
                            (f"verified_whale_id_json", 1000.0, code))
                conn.commit()
                conn.close()
                print(f"✅ VERIFIED: JSON-Aware Whale Logic Forged.")

if __name__ == '__main__':
    ArchitectLoop().run()
