Spaces:
Sleeping
Sleeping
File size: 2,044 Bytes
b89e6d6 | 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 | """Phase 6: agentic generate -> execute -> repair loop.
Model-agnostic: takes a `generate_fn(intent, feedback) -> code` callable, so it
works with CodeAssistant or any other generator (and is unit-testable with a mock).
"""
from __future__ import annotations
import sys
from dataclasses import dataclass, field
from pathlib import Path
from typing import Callable
sys.path.append(str(Path(__file__).resolve().parents[2]))
from src.eval.sandbox import run_code # noqa: E402
@dataclass
class RepairTrace:
final_code: str
success: bool
iterations: int
history: list = field(default_factory=list) # list of (code, error)
def repair_loop(
intent: str,
generate_fn: Callable[[str, str | None], str],
check_program_fn: Callable[[str], str],
max_iters: int = 3,
timeout: float = 8.0,
) -> RepairTrace:
"""Iteratively generate and self-correct.
generate_fn(intent, feedback) -> candidate code
feedback is None on the first call, else the previous error string.
check_program_fn(code) -> a runnable program string (code + a smoke
test or the harness test) used to decide pass/fail.
"""
feedback = None
history = []
for i in range(1, max_iters + 1):
code = generate_fn(intent, feedback)
result = run_code(check_program_fn(code), timeout=timeout)
history.append((code, result.error))
if result.ok:
return RepairTrace(code, True, i, history)
feedback = result.error # feed the traceback back in
return RepairTrace(history[-1][0], False, max_iters, history)
def make_repair_generator(assistant):
"""Adapt a CodeAssistant into a generate_fn for the repair loop."""
def generate_fn(intent: str, feedback: str | None) -> str:
if feedback:
intent = (f"{intent}\n\n# Your previous attempt failed with this error:\n"
f"# {feedback}\n# Fix it and return the corrected function.")
return assistant.generate(intent, mode="rag")
return generate_fn
|