File size: 10,514 Bytes
1e95044
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
#!/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()