#!/usr/bin/env python3
"""Auto-generated by AGI Loop cycle #1009
Task: Write a Python function that reads /Eden/DATA/agi_loop.db wonder_attempts, finds tasks with 0 successes, analyzes why they fail, and writes improved versions to a JSON file
Generated: 2026-02-12T16:51:02.976715
"""

import sqlite3
import json

def analyze_wonder_attempts():
    # Hardcoded path
    db_path = "/Eden/DATA/agi_loop.db"
    
    # Connect to the SQLite database
    conn = sqlite3.connect(db_path)
    cursor = conn.cursor()
    
    # Query to fetch tasks with 0 successes
    cursor.execute("SELECT * FROM wonder_attempts WHERE successes = 0")
    failed_tasks = cursor.fetchall()
    
    conn.close()
    
    # Analyze why tasks failed
    analyzed_tasks = []
    for task in failed_tasks:
        task_id, task_description, attempts, successes, last_attempt_time = task
        analysis = {
            "task_id": task_id,
            "task_description": task_description,
            "attempts": attempts,
            "successes": successes,
            "last_attempt_time": last_attempt_time,
            "analysis": "Possible reasons for failure: lack of environmental cues, missing dependencies, or incomplete logic chain."
        }
        analyzed_tasks.append(analysis)
    
    # Save analyzed tasks to a JSON file
    json_output_path = "improved_tasks.json"
    with open(json_output_path, 'w') as json_file:
        json.dump(analyzed_tasks, json_file, indent=4)
    
    # Print output
    print(f"Found {len(failed_tasks)} tasks with 0 successes.")
    print(f"Analyzed tasks and saved to {json_output_path}.")

if __name__ == '__main__':
    analyze_wonder_attempts()