-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathautodev.py
More file actions
528 lines (447 loc) · 17.2 KB
/
Copy pathautodev.py
File metadata and controls
528 lines (447 loc) · 17.2 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
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
#!/usr/bin/env python3
"""
AutoDev - Autonomous Development Loop using OODA Cycle.
Main entry point for the autonomous development system.
Integrates Observe, Orient, Decide, Act phases with progress monitoring.
"""
import json
import argparse
import time
import signal
import sys
from pathlib import Path
from typing import Optional, Dict, Any, List
from dataclasses import dataclass, asdict
from datetime import datetime
# Add project root to path
sys.path.insert(0, str(Path(__file__).parent))
from ooda.observe import Observer
from ooda.orient import Orienter
from ooda.decide import Decider, PatchResult
from ooda.act import Actor, ActResult
from monitor.progress import ProgressMonitor
@dataclass
class LoopResult:
"""Result of the OODA loop execution."""
success: bool
total_iterations: int
successful_patches: int
failed_patches: int
final_state: str # "tests_passing", "max_iterations", "error", "timeout"
error_message: Optional[str]
duration_seconds: float
statistics: Dict[str, Any]
class SafetyLimits:
"""Enforce safety limits on the OODA loop."""
def __init__(self, config_path: Path):
config = json.loads(config_path.read_text())
self.max_iterations = config["iteration"]["max_iterations"]
self.timeout_minutes = config["iteration"]["timeout_minutes"]
self.require_human_approval = config["safety"]["require_human_approval"]
self.auto_rollback = config["safety"]["auto_rollback"]
def check_iteration_limit(self, current: int) -> bool:
"""Check if we've exceeded iteration limit."""
return current >= self.max_iterations
def check_timeout(self, start_time: float) -> bool:
"""Check if we've exceeded timeout."""
elapsed = (time.time() - start_time) / 60
return elapsed >= self.timeout_minutes
class OODALoop:
"""
Main OODA Loop orchestrator.
Coordinates Observe, Orient, Decide, Act phases with:
- Progress monitoring
- Safety limits
- Detailed logging
- Human-in-the-loop intervention points
"""
def __init__(
self,
project_root: Optional[Path] = None,
target_dir: str = "mal",
verbose: bool = False
):
self.project_root = project_root or Path(__file__).parent
self.target_dir = target_dir
self.verbose = verbose
# Load configuration
config_path = self.project_root / "config"
self.limits = SafetyLimits(config_path / "limits.json")
# Initialize OODA phases
self.observer = Observer(self.project_root)
self.orient = Orienter(self.project_root)
self.decider = Decider(self.project_root)
self.actor = Actor(self.project_root, target_dir)
self.monitor = ProgressMonitor(self.project_root)
# State tracking
self.start_time = None
self.current_iteration = 0
self.should_stop = False
# Set up signal handlers for graceful shutdown
signal.signal(signal.SIGINT, self._handle_interrupt)
def _handle_interrupt(self, signum, frame):
"""Handle Ctrl+C gracefully."""
print("\n\nInterrupt received. Finishing current iteration...")
self.should_stop = True
def _log(self, message: str, level: str = "INFO"):
"""Log message with timestamp if verbose."""
if self.verbose or level in ("WARN", "ERROR"):
timestamp = datetime.now().strftime("%H:%M:%S")
print(f"[{timestamp}] {level}: {message}")
def _print_header(self, title: str):
"""Print a section header."""
width = 50
print(f"\n{'=' * width}")
print(f"{title:^{width}}")
print(f"{'=' * width}")
def run(
self,
target_file: Optional[str] = None,
max_iterations: Optional[int] = None
) -> LoopResult:
"""
Run the OODA loop until tests pass or limits reached.
Args:
target_file: Specific file to fix (if None, auto-detect)
max_iterations: Override default max iterations
Returns:
LoopResult with complete execution details
"""
self.start_time = time.time()
max_iter = max_iterations or self.limits.max_iterations
self._print_header("AutoDev - OODA Loop Starting")
print(f"Project: {self.project_root}")
print(f"Target directory: {self.target_dir}")
print(f"Max iterations: {max_iter}")
print(f"Timeout: {self.limits.timeout_minutes} minutes")
successful_patches = 0
failed_patches = 0
for iteration in range(1, max_iter + 1):
if self.should_stop:
break
if self.limits.check_iteration_limit(iteration - 1):
self._log("Maximum iterations reached", "WARN")
break
if self.limits.check_timeout(self.start_time):
self._log("Timeout reached", "WARN")
break
self.current_iteration = iteration
self._print_header(f"Iteration {iteration}/{max_iter}")
# === OBSERVE ===
print("\n[OBSERVE] Checking current state...")
observe_data = self._observe_phase(target_file)
if observe_data["tests_passing"]:
return self._create_result(
success=True,
total_iterations=iteration,
successful_patches=successful_patches,
failed_patches=failed_patches,
final_state="tests_passing"
)
# === ORIENT ===
print("\n[ORIENT] Querying context memory...")
orient_data = self._orient_phase(observe_data, target_file)
# Display context info
similar_count = len(orient_data.get("similar_errors", []))
if similar_count > 0:
print(f" Found {similar_count} similar past errors")
lessons_count = len(orient_data.get("lessons_learned", []))
if lessons_count > 0:
print(f" {lessons_count} lessons learned available")
# === DECIDE ===
print("\n[DECIDE] Generating patch...")
decide_data = self._decide_phase(observe_data, orient_data)
if not decide_data["patch_result"]:
return self._create_result(
success=False,
total_iterations=iteration,
successful_patches=successful_patches,
failed_patches=failed_patches,
final_state="error",
error_message="Failed to generate patch"
)
patch_result = decide_data["patch_result"]
print(f" Model: {patch_result.model_used}")
print(f" Attempts: {patch_result.attempts}")
print(f" Patch size: {len(patch_result.patch)} bytes")
# Human approval check
if self.limits.require_human_approval:
if not self._request_approval(patch_result.patch):
print(" Patch rejected by user")
failed_patches += 1
continue
# === ACT ===
print("\n[ACT] Applying patch and verifying...")
act_result = self._act_phase(
observe_data, orient_data, decide_data, iteration
)
if act_result.success:
successful_patches += 1
print(f" [OK] Patch applied and verified!")
else:
failed_patches += 1
print(f" [FAIL] Patch failed: {act_result.errors[0] if act_result.errors else 'Unknown'}")
# Record result for learning
self.orient.record_result(
error=observe_data.get("error_output", ""),
fix=patch_result.patch,
file_path=observe_data.get("target_file", ""),
success=act_result.success,
iteration=iteration
)
# Create iteration record
self.orient.create_iteration_record(
iteration=iteration,
observe=observe_data,
orient=orient_data,
decide=decide_data,
act=asdict(act_result),
outcome="success" if act_result.success else "failed"
)
# Loop ended without success
return self._create_result(
success=False,
total_iterations=max_iter,
successful_patches=successful_patches,
failed_patches=failed_patches,
final_state="max_iterations"
)
def _observe_phase(self, target_file: Optional[str]) -> Dict[str, Any]:
"""Execute OBSERVE phase with Docker-based Mal testing."""
# Run actual Mal tests via Docker
success, output = self.observer.run_tests()
# Parse the test output
parsed = self.observer.parse_mal_test_output(output)
# Determine target file
if target_file:
detected_file = target_file
else:
# Auto-detect from current step
current_step = self.observer.get_current_step()
detected_file = f"step{current_step}.py" if current_step else "step3.py"
# Get current code
current_code = ""
try:
if detected_file:
current_code = self.observer.read_file(detected_file)
except Exception as e:
self._log(f"Could not read file {detected_file}: {e}", "WARN")
return {
"tests_passing": success,
"error_output": output,
"target_file": detected_file,
"current_code": current_code,
"missing_functions": parsed.get("missing_functions", []),
"passed": parsed.get("passed", 0),
"failed": parsed.get("failed", 0),
"timestamp": datetime.now().isoformat()
}
def _orient_phase(
self,
observe_data: Dict[str, Any],
target_file: Optional[str]
) -> Dict[str, Any]:
"""Execute ORIENT phase."""
file_path = target_file or observe_data.get("target_file", "")
error = observe_data.get("error_output", "")
context = self.orient.query_context(
error_message=error,
file_path=file_path,
iteration=self.current_iteration
)
return context
def _decide_phase(
self,
observe_data: Dict[str, Any],
orient_data: Dict[str, Any]
) -> Dict[str, Any]:
"""Execute DECIDE phase."""
file_path = observe_data.get("target_file", "")
current_code = observe_data.get("current_code", "")
error = observe_data.get("error_output", "")
missing = observe_data.get("missing_functions", [])
current_step = self.observer.get_current_step()
if not file_path:
return {"patch_result": None, "error": "No target file"}
patch_result = self.decider.generate_patch(
file_path=file_path,
current_code=current_code,
error_message=error,
context=orient_data,
test_name="",
missing_functions=missing,
current_step=current_step
)
return {
"patch_result": patch_result,
"error": None if patch_result else "Generation failed"
}
def _act_phase(
self,
observe_data: Dict[str, Any],
orient_data: Dict[str, Any],
decide_data: Dict[str, Any],
iteration: int
) -> ActResult:
"""Execute ACT phase."""
file_path = observe_data.get("target_file", "")
patch_result = decide_data.get("patch_result")
if not patch_result:
return ActResult(
success=False,
patch_applied=False,
verification_passed=False,
rollback_performed=False,
output="",
errors=["No patch to apply"],
file_modified=file_path,
iteration=iteration
)
return self.actor.act(
file_path=file_path,
diff=patch_result.patch,
iteration=iteration,
observe_data=observe_data,
orient_data=orient_data,
decide_data={"model": patch_result.model_used, "attempts": patch_result.attempts}
)
def _detect_file(self, error_output: str) -> Optional[str]:
"""Detect target file from error output."""
for line in error_output.split('\n'):
if '.py' in line:
# Extract file path
parts = line.split()
for part in parts:
if part.endswith('.py'):
# Clean up the path
path = part.strip(':').strip('"').strip("'")
return path
return None
def _request_approval(self, patch: str) -> bool:
"""Request human approval for patch."""
print("\n --- Patch Preview ---")
lines = patch.split('\n')
for line in lines[:20]: # Show first 20 lines
print(f" {line}")
if len(lines) > 20:
print(f" ... ({len(lines) - 20} more lines)")
print(" --- End Preview ---\n")
while True:
response = input(" Apply this patch? [y/n] ").lower().strip()
if response in ('y', 'yes'):
return True
elif response in ('n', 'no'):
return False
print(" Please enter 'y' or 'n'")
def _create_result(
self,
success: bool,
total_iterations: int,
successful_patches: int,
failed_patches: int,
final_state: str,
error_message: Optional[str] = None
) -> LoopResult:
"""Create LoopResult with statistics."""
duration = time.time() - self.start_time
# Gather statistics from all phases
decide_stats = self.decider.get_statistics()
act_stats = self.actor.get_statistics()
orient_stats = self.orient.get_statistics()
statistics = {
"decide": decide_stats,
"act": act_stats,
"orient": orient_stats,
"duration_seconds": duration,
"success_rate": successful_patches / max(1, (successful_patches + failed_patches))
}
return LoopResult(
success=success,
total_iterations=total_iterations,
successful_patches=successful_patches,
failed_patches=failed_patches,
final_state=final_state,
error_message=error_message,
duration_seconds=duration,
statistics=statistics
)
def main():
"""CLI entry point for AutoDev."""
parser = argparse.ArgumentParser(
description="AutoDev - Autonomous Development Loop",
formatter_class=argparse.RawDescriptionHelpFormatter,
epilog="""
Examples:
%(prog)s # Run with defaults
%(prog)s -f test.py # Fix specific file
%(prog)s --max-iterations 5 # Limit iterations
%(prog)s --verbose # Verbose output
%(prog)s --watch # Watch progress
"""
)
parser.add_argument(
"--file", "-f",
help="Specific file to fix"
)
parser.add_argument(
"--max-iterations", "-n",
type=int,
help="Maximum iterations (overrides config)"
)
parser.add_argument(
"--target-dir", "-d",
default="mal",
help="Target directory containing code (default: mal)"
)
parser.add_argument(
"--verbose", "-v",
action="store_true",
help="Verbose output"
)
parser.add_argument(
"--watch", "-w",
action="store_true",
help="Watch mode - show progress dashboard"
)
parser.add_argument(
"--approve", "-a",
action="store_true",
help="Require human approval for each patch"
)
args = parser.parse_args()
# Watch mode
if args.watch:
monitor = ProgressMonitor(Path(__file__).parent)
try:
monitor.watch()
except KeyboardInterrupt:
print("\nStopped watching.")
return
# Run OODA loop
loop = OODALoop(verbose=args.verbose, target_dir=args.target_dir)
# Override config if requested
if args.approve:
loop.limits.require_human_approval = True
result = loop.run(target_file=args.file, max_iterations=args.max_iterations)
# Print final report
print("\n" + "=" * 50)
print("AutoDev Execution Complete".center(50))
print("=" * 50)
print(f"\nFinal State: {result.final_state}")
print(f"Iterations: {result.total_iterations}")
print(f"Successful Patches: {result.successful_patches}")
print(f"Failed Patches: {result.failed_patches}")
print(f"Duration: {result.duration_seconds:.1f} seconds")
if result.statistics.get("success_rate"):
print(f"Success Rate: {result.statistics['success_rate']:.1%}")
print()
# Show progress report
loop.monitor.print_report()
if result.success:
print("[SUCCESS] All tests passing!")
exit(0)
else:
print(f"[FAILED] {result.error_message or 'Loop did not complete successfully'}")
exit(1)
if __name__ == "__main__":
main()