-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patharc_watchdog.py
More file actions
291 lines (228 loc) · 10 KB
/
arc_watchdog.py
File metadata and controls
291 lines (228 loc) · 10 KB
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
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
#!/usr/bin/env python3
"""
ARC AGI Watchdog System
Ensures continuous operation and self-improvement
"""
import time
import subprocess
import json
import psutil
import threading
from pathlib import Path
from datetime import datetime, timedelta
import sys
import os
class ARCWatchdog:
"""Watchdog for continuous self-improvement"""
def __init__(self):
self.running = True
self.solver_process = None
self.log_dir = Path("logs/watchdog")
self.log_dir.mkdir(parents=True, exist_ok=True)
self.status_file = self.log_dir / "status.json"
self.restart_count = 0
self.last_improvement = datetime.now()
self.improvement_interval = timedelta(minutes=30)
def log(self, message, level="INFO"):
"""Log watchdog events"""
timestamp = datetime.now().isoformat()
log_entry = f"[{timestamp}] [{level}] {message}"
print(log_entry)
# Write to log file
log_file = self.log_dir / f"watchdog_{datetime.now().strftime('%Y%m%d')}.log"
with open(log_file, 'a') as f:
f.write(log_entry + "\n")
def update_status(self, status_data):
"""Update status file"""
status_data['timestamp'] = datetime.now().isoformat()
status_data['restart_count'] = self.restart_count
with open(self.status_file, 'w') as f:
json.dump(status_data, f, indent=2)
def check_solver_health(self):
"""Check if solver is running and healthy"""
if self.solver_process:
# Check if process is alive
if self.solver_process.poll() is not None:
self.log("Solver process died", "ERROR")
return False
# Check CPU usage (shouldn't be stuck)
try:
process = psutil.Process(self.solver_process.pid)
cpu_percent = process.cpu_percent(interval=1)
if cpu_percent < 1:
self.log(f"Solver appears stuck (CPU: {cpu_percent}%)", "WARNING")
# Don't kill yet, might be waiting for Claude Code
except psutil.NoSuchProcess:
self.log("Solver process not found", "ERROR")
return False
else:
self.log("No solver process", "WARNING")
return False
return True
def start_solver(self):
"""Start the solver process"""
self.log("Starting solver process")
try:
# Start the integrated app with solver
self.solver_process = subprocess.Popen(
[sys.executable, "arc_integrated_app.py"],
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
text=True
)
self.log(f"Solver started with PID: {self.solver_process.pid}")
self.update_status({
'solver_pid': self.solver_process.pid,
'status': 'running',
'start_time': datetime.now().isoformat()
})
return True
except Exception as e:
self.log(f"Failed to start solver: {e}", "ERROR")
return False
def trigger_improvement(self):
"""Trigger self-improvement cycle"""
self.log("Triggering self-improvement cycle")
# Read current performance
try:
results_file = Path("enhanced_solver_results.json")
if results_file.exists():
with open(results_file, 'r') as f:
results = json.load(f)
solve_rate = results.get('solved_count', 0) / max(1, results.get('total_puzzles', 1))
if solve_rate < 1.0:
# Generate improvement prompt
prompt = f"""
Current solve rate: {solve_rate:.1%}
Puzzles: {results.get('solved_count')}/{results.get('total_puzzles')}
Analyze failures and generate:
1. New heuristic for unsolved patterns
2. Optimization for slow-solving puzzles
Save improvements to patterns/improvement_{datetime.now().strftime('%Y%m%d_%H%M%S')}.py
"""
# Call Claude Code
self.call_claude_code(prompt)
else:
self.log("Perfect solve rate achieved!", "SUCCESS")
except Exception as e:
self.log(f"Error reading performance: {e}", "ERROR")
self.last_improvement = datetime.now()
def call_claude_code(self, prompt):
"""Call Claude Code for improvements"""
self.log("Calling Claude Code for improvements")
cmd = [
"claude",
"--allowedTools", "Bash,Read,WebSearch,Fetch",
"--permission-mode", "acceptEdits",
"--message", prompt
]
try:
# Log the conversation
conversation_log = Path("logs/claude_conversations") / f"improvement_{datetime.now().strftime('%Y%m%d_%H%M%S')}.json"
conversation_log.parent.mkdir(parents=True, exist_ok=True)
# In production, this would actually call Claude Code
# For now, log the attempt
with open(conversation_log, 'w') as f:
json.dump({
'timestamp': datetime.now().isoformat(),
'prompt': prompt,
'status': 'simulated'
}, f, indent=2)
self.log(f"Claude Code conversation logged to: {conversation_log}")
except Exception as e:
self.log(f"Claude Code call failed: {e}", "ERROR")
def monitor_loop(self):
"""Main monitoring loop"""
self.log("Watchdog monitoring started")
while self.running:
try:
# Check solver health
if not self.check_solver_health():
self.log("Solver unhealthy, restarting", "WARNING")
self.restart_count += 1
if self.solver_process:
self.solver_process.terminate()
time.sleep(2)
self.start_solver()
# Check if improvement needed
time_since_improvement = datetime.now() - self.last_improvement
if time_since_improvement > self.improvement_interval:
self.trigger_improvement()
# Update status
self.update_status({
'status': 'monitoring',
'solver_healthy': self.check_solver_health(),
'last_improvement': self.last_improvement.isoformat()
})
# Sleep before next check
time.sleep(10)
except KeyboardInterrupt:
self.log("Received shutdown signal")
self.running = False
except Exception as e:
self.log(f"Monitor error: {e}", "ERROR")
time.sleep(5)
def commit_improvements(self):
"""Periodically commit improvements to git"""
self.log("Committing improvements to git")
try:
# Add all changes
subprocess.run(["git", "add", "-A"], check=False)
# Get status for commit message
status = "Continuous improvement checkpoint"
if self.status_file.exists():
with open(self.status_file, 'r') as f:
status_data = json.load(f)
if 'solver_healthy' in status_data:
status = f"Watchdog: Solver {'healthy' if status_data['solver_healthy'] else 'restarted'}"
# Commit
subprocess.run([
"git", "commit", "-m",
f"{status}\nRestart count: {self.restart_count}\nTimestamp: {datetime.now().isoformat()}"
], check=False)
# Push
subprocess.run(["git", "push"], check=False)
self.log("Changes committed and pushed")
except Exception as e:
self.log(f"Git commit failed: {e}", "WARNING")
def run(self):
"""Main watchdog execution"""
print("="*70)
print("ARC AGI Watchdog System")
print("Ensuring Continuous Self-Improvement")
print("="*70)
# Start solver
if not self.start_solver():
self.log("Failed to start solver, exiting", "ERROR")
return
# Start commit thread
def commit_loop():
while self.running:
time.sleep(600) # Commit every 10 minutes
self.commit_improvements()
commit_thread = threading.Thread(target=commit_loop, daemon=True)
commit_thread.start()
# Run monitoring loop
try:
self.monitor_loop()
finally:
self.log("Watchdog shutting down")
if self.solver_process:
self.solver_process.terminate()
# Final commit
self.commit_improvements()
self.log("Watchdog stopped")
def main():
"""Entry point"""
watchdog = ARCWatchdog()
# Check for daemon mode
if "--daemon" in sys.argv:
# Run as background daemon
import daemon
with daemon.DaemonContext():
watchdog.run()
else:
# Run in foreground
watchdog.run()
if __name__ == "__main__":
main()