Main fix mode entrypoint. 1. Read the file 2. Run it and capture error 3. If error, ask Codey to fix it 4. Write the fix 5. Re-run to verify
(filepath: str, extra_instruction: str = "", yolo: bool = False)
| 31 | return False, f"[ERROR] {e}" |
| 32 | |
| 33 | def fix_file(filepath: str, extra_instruction: str = "", yolo: bool = False): |
| 34 | """ |
| 35 | Main fix mode entrypoint. |
| 36 | 1. Read the file |
| 37 | 2. Run it and capture error |
| 38 | 3. If error, ask Codey to fix it |
| 39 | 4. Write the fix |
| 40 | 5. Re-run to verify |
| 41 | """ |
| 42 | from core.agent import run_agent |
| 43 | from core.context import load_file |
| 44 | |
| 45 | p = Path(filepath).expanduser() |
| 46 | if not p.exists(): |
| 47 | error(f"File not found: {filepath}") |
| 48 | return |
| 49 | |
| 50 | # Override confirmations in fix mode |
| 51 | if yolo: |
| 52 | from utils import config |
| 53 | config.AGENT_CONFIG["confirm_write"] = False |
| 54 | config.AGENT_CONFIG["confirm_shell"] = False |
| 55 | |
| 56 | info(f"Running {p.name}...") |
| 57 | ok, output = run_and_capture(str(p)) |
| 58 | |
| 59 | if ok: |
| 60 | success(f"{p.name} ran successfully:") |
| 61 | print(output) |
| 62 | if extra_instruction: |
| 63 | info(f"Applying extra instruction: {extra_instruction}") |
| 64 | load_file(str(p)) |
| 65 | history = [] |
| 66 | run_agent( |
| 67 | f"File {p.name} runs fine. Now: {extra_instruction}", |
| 68 | history, yolo=yolo |
| 69 | ) |
| 70 | return |
| 71 | |
| 72 | # Error — show it and auto-fix |
| 73 | warning(f"Error in {p.name}:") |
| 74 | print(output) |
| 75 | separator() |
| 76 | info("Auto-fixing...") |
| 77 | |
| 78 | # Load the broken file into context |
| 79 | load_file(str(p)) |
| 80 | |
| 81 | # Build fix prompt |
| 82 | prompt = ( |
| 83 | f"The file {p.name} has an error. Fix it.\n\n" |
| 84 | f"Error output:\n{output}\n\n" |
| 85 | f"The file contents are in your context above. " |
| 86 | f"Write the complete corrected version of {p.name}." |
| 87 | ) |
| 88 | if extra_instruction: |
| 89 | prompt += f"\n\nAlso: {extra_instruction}" |
| 90 |