#!/usr/bin/env python3 """ PHASE 7: TOOL MASTERY & PRACTICAL CODING Make them able to use tools, call APIs, manipulate files, debug Make them do WHAT I DO """ import json import sys from datetime import datetime sys.path.insert(0, '.') from creature_system import Creature # ============================================================ # PHASE 7: TOOL MASTERY # ============================================================ PHASE_7_TOOLS = [ # File operations ("code write a function that reads and parses a JSON file", "file I/O json"), ("code write file backup system with error handling", "file operations"), ("code glob all .py files in a directory tree", "filesystem search"), ("code read a file in chunks without loading all into memory", "streaming io"), ("code write atomic file operations to prevent corruption", "file safety"), # API & HTTP ("code call a REST API and handle rate limiting", "api calls"), ("code implement retry logic with exponential backoff", "resilience"), ("code parse and validate JSON responses from APIs", "data validation"), ("code build a webhook receiver with signature verification", "webhook security"), ("code implement OAuth token refresh flow", "authentication"), # System commands ("code run shell commands and capture output safely", "subprocess"), ("code parse git diff output and extract changes", "git parsing"), ("code monitor system resources CPU memory disk", "system monitoring"), ("code implement process management and cleanup", "process control"), ("code handle signals and graceful shutdown", "signal handling"), # Data processing ("code parse CSV and handle missing values", "data cleaning"), ("code implement pagination for large datasets", "data pagination"), ("code batch process items with progress tracking", "batch operations"), ("code implement caching with TTL", "caching"), ("code deduplicate data while preserving order", "deduplication"), # Debugging & logging ("code implement structured logging with levels", "logging"), ("code write debug traces that can be enabled/disabled", "debugging"), ("code handle exceptions with context and recovery", "error handling"), ("code implement timing/profiling for performance", "profiling"), ("code create detailed error messages with suggestions", "error messages"), # Testing ("code write unit tests with assertions", "unit testing"), ("code mock external dependencies for testing", "mocking"), ("code write integration tests with setup/teardown", "integration testing"), ("code implement test fixtures for reusable data", "test fixtures"), ("code measure code coverage", "coverage"), # Database ("code implement connection pooling for databases", "db connection"), ("code write parameterized queries to prevent SQL injection", "sql safety"), ("code implement transactions with rollback", "transactions"), ("code write database migrations", "migrations"), ("code implement query optimization", "query optimization"), # Configuration & deployment ("code read from environment variables safely", "config management"), ("code implement feature flags for safe rollout", "feature flags"), ("code write configuration validation", "config validation"), ("code implement graceful config reloading", "config reload"), ("code write health check endpoints", "health checks"), # Concurrency ("code implement thread-safe operations with locks", "threading"), ("code write async/await code properly", "async"), ("code handle race conditions and deadlocks", "concurrency bugs"), ("code implement message queue patterns", "queues"), ("code write producer consumer with backpressure", "backpressure"), # Advanced patterns ("code implement observer pattern for events", "observer pattern"), ("code write decorator pattern for cross-cutting concerns", "decorators"), ("code implement dependency injection", "dependency injection"), ("code write fluent API builder pattern", "builder pattern"), ("code implement middleware chain", "middleware"), ] # ============================================================ # PHASE 8: AUTONOMOUS TASK SOLVING # ============================================================ PHASE_8_AUTONOMY = [ # Multi-step problems ("break down a complex task into subtasks", "task decomposition"), ("decide when to ask for help vs solve alone", "decision making"), ("estimate time and resources for a task", "estimation"), ("identify dependencies between tasks", "dependency analysis"), ("create a plan before executing", "planning"), # Problem diagnosis ("given error message diagnose the root cause", "diagnosis"), ("reproduce a bug from description", "bug reproduction"), ("trace execution to find where it fails", "tracing"), ("examine state to find invariant violations", "state inspection"), ("design test case that exposes the bug", "test design"), # Code review ("identify code smells and anti-patterns", "code smells"), ("suggest refactoring for maintainability", "refactoring"), ("spot potential performance issues", "perf analysis"), ("find security vulnerabilities", "security review"), ("verify code handles edge cases", "edge case analysis"), # Documentation & communication ("write clear function documentation", "docstrings"), ("create architecture decision records", "ADRs"), ("write README that explains the system", "readmes"), ("communicate findings clearly", "communication"), ("teach someone else how to solve it", "teaching"), # Optimization & scalability ("profile code and find bottlenecks", "profiling"), ("optimize algorithm time complexity", "algorithm optimization"), ("optimize memory usage", "memory optimization"), ("implement caching strategy", "caching strategy"), ("scale for 10x load", "scalability"), # Integration & deployment ("integrate with external services", "integration"), ("handle version compatibility", "versioning"), ("write deployment scripts", "deployment"), ("implement blue-green deployment", "blue-green"), ("handle rollback scenarios", "rollback"), ] # ============================================================ # TRAINING RUNNER # ============================================================ def train_tool_mastery(): """Train creatures to be tool-capable like me.""" phases = [ ("PHASE 7: TOOL MASTERY & PRACTICAL CODING", PHASE_7_TOOLS), ("PHASE 8: AUTONOMOUS TASK SOLVING", PHASE_8_AUTONOMY), ] all_results = { "timestamp": datetime.now().isoformat(), "goal": "Make creatures able to code and use tools like the baseline", "phases": [] } for phase_name, challenges in phases: print(f"\n{'='*70}") print(f"{phase_name}") print(f"{'='*70}\n") phase_results = [] for creature_name in ["Luna", "Nova", "Cipher"]: creature = Creature(creature_name) initial_concepts = len(creature.weights["salience"]) initial_assoc = len(creature.weights["assoc"]) print(f"\n{creature_name}: {initial_concepts} concepts, {initial_assoc} assoc") print("-" * 70) for i, (challenge, topic) in enumerate(challenges, 1): print(f"[{i:2d}] {topic:35s} | ", end="", flush=True) # Learn from challenge response = f"[{creature_name} learning: {topic}] {challenge[:40]}" creature.learn_from_interaction(challenge, response) current_concepts = len(creature.weights["salience"]) current_assoc = len(creature.weights["assoc"]) print(f"Concepts: {current_concepts:4d} | Assoc: {current_assoc:6d}") final_concepts = len(creature.weights["salience"]) final_assoc = len(creature.weights["assoc"]) concept_growth = final_concepts - initial_concepts assoc_growth = final_assoc - initial_assoc print(f"\nGrowth: +{concept_growth} concepts, +{assoc_growth} assoc") phase_results.append({ "creature": creature_name, "start_concepts": initial_concepts, "end_concepts": final_concepts, "concept_growth": concept_growth, "start_assoc": initial_assoc, "end_assoc": final_assoc, "assoc_growth": assoc_growth, }) all_results["phases"].append({ "name": phase_name, "challenges": len(challenges), "results": phase_results }) # Save log with open("tool_mastery_log.json", 'w') as f: json.dump(all_results, f, indent=2) print(f"\n{'='*70}") print("TOOL MASTERY & AUTONOMY TRAINING COMPLETE") print(f"{'='*70}\n") for creature_name in ["Luna", "Nova", "Cipher"]: creature = Creature(creature_name) concepts = len(creature.weights["salience"]) assoc = len(creature.weights["assoc"]) # Top concepts top = sorted(creature.weights["salience"].items(), key=lambda x: x[1], reverse=True)[:10] print(f"\n{creature_name}:") print(f" Concepts: {concepts}") print(f" Associations: {assoc}") print(f" Top: {[k for k, v in top]}") print(f"\n{'='*70}") print("CAPABILITIES:") print(" - File I/O & data processing") print(" - API integration & authentication") print(" - System commands & subprocess") print(" - Database operations") print(" - Testing & debugging") print(" - Concurrency & async") print(" - Task decomposition & planning") print(" - Code review & optimization") print(" - Autonomous problem solving") print(f"{'='*70}\n") if __name__ == "__main__": train_tool_mastery()