Spaces:
Running
Running
File size: 9,019 Bytes
a72140d | 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 | import os
import json
import argparse
import re
import sys
from openai import OpenAI
from utils import read_python_files, content_to_json, extract_planning
def parse_and_apply_changes(responses, debug_dir, save_num=1):
"""Apply SEARCH / REPLACE edits produced by the LLM to files in debug_dir."""
for response in responses:
# Split into blocks per file
file_blocks = re.split(r"Filename:\s*([^\n]+)", response)
# Example: ['', 'file1.py', '...file1 content...', 'file2.py', '...file2 content...', ...]
if len(file_blocks) < 3:
print(f"β No filename patterns found in response:\n{response[:200]}...\n")
continue
# Process blocks per file (odd indices: filename, even indices: diff content)
for i in range(1, len(file_blocks), 2):
filename = file_blocks[i].strip()
file_content_block = file_blocks[i + 1]
filepath = os.path.join(debug_dir, filename)
# SEARCH/REPLACE pattern
search_replace_pattern = (
r"<<<<<<< SEARCH\n(.*?)\n=======\n(.*?)\n>>>>>>> REPLACE"
)
matches = re.findall(search_replace_pattern, file_content_block, re.DOTALL)
if not matches:
print(f"β No SEARCH/REPLACE patterns found for file: {filename}\n")
continue
# Check file existence
if not os.path.exists(filepath):
print(f"β File does not exist: {filepath}\n")
continue
# Read file
try:
with open(filepath, "r", encoding="utf-8") as f:
file_content = f.read()
except Exception as e:
print(f"β Error reading file {filepath}: {e}\n")
continue
modified = False
# Apply SEARCH/REPLACE
for idx, (search_text, replace_text) in enumerate(matches, 1):
search_text = search_text.strip()
replace_text = replace_text.strip()
if search_text in file_content:
file_content = file_content.replace(search_text, replace_text)
modified = True
print(f"β
{filename}: Modification {idx} applied")
else:
print(
f"β {filename}: Search text for modification {idx} not found:\n"
f"{search_text[:200]}...\n"
)
# If modified, create backup and save
if modified:
backup_path = f"{filepath}.{save_num:03d}.bak"
try:
os.rename(filepath, backup_path)
with open(filepath, "w", encoding="utf-8") as f:
f.write(file_content)
print(f"πΎ {filename}: File saved. Backup: {backup_path}\n")
except Exception as e:
print(f"β Error saving file {filepath}: {e}\n")
else:
print(f"βΉοΈ {filename}: No modifications applied\n")
def parse_args() -> argparse.Namespace:
parser = argparse.ArgumentParser(
description="Debug a generated repository given an error log and planning artifacts."
)
parser.add_argument(
"--error_file_name",
type=str,
required=True,
help="Path to a text file containing the execution error message.",
)
# Either provide output_dir directly, or let the script construct it from the dataset style
parser.add_argument(
"--output_dir",
type=str,
required=True,
help=(
"Root output directory that contains planning_trajectories.json and the debug directory."
),
)
parser.add_argument(
"--paper_name",
type=str,
required=True,
help="Paper name for output_dir.",
)
parser.add_argument(
"--model",
type=str,
default="o4-mini",
help="OpenAI chat model used for debugging.",
)
parser.add_argument(
"--save_num",
type=int,
default=1,
required=True,
help="Backup index appended as .<save_num>.bak when saving modified files.",
)
return parser.parse_args()
args = parse_args()
client = OpenAI(api_key = os.environ["OPENAI_API_KEY"])
if not os.path.exists(args.error_file_name):
raise FileNotFoundError(f"Error file not found: {args.error_file_name}")
with open(args.error_file_name, "r", encoding="utf-8") as f:
execution_error_msg = f.read()
# --------------------------------------------------
# Resolve output_dir and debug_dir
# --------------------------------------------------
output_dir = os.path.abspath(args.output_dir)
debug_dir = os.path.abspath(args.output_repo_dir)
# --------------------------------------------------
# Load planning trajectories and task list
# --------------------------------------------------
planning_traj_path = os.path.join(
output_dir, f"planning_trajectories.json"
)
if not os.path.exists(planning_traj_path):
print(f"β Planning trajectories not found: {planning_traj_path}", file=sys.stderr)
sys.exit(1)
context_lst = extract_planning(planning_traj_path)
# context_lst indices: 0 overview, 1 detailed, 2 PRD (per your original comment)
task_list = content_to_json(context_lst[2])
todo_file_lst = task_list.get("Task list", [])
# --------------------------------------------------
# Load repo files and configuration files
# --------------------------------------------------
python_dict = read_python_files(debug_dir)
codes = ""
for todo_file in todo_file_lst:
if todo_file.endswith(".yaml"):
continue
if todo_file not in python_dict:
print(f"β οΈ {todo_file} not found in python_dict. Skipping.")
continue
codes += f"```python\n## File name: {todo_file}\n{python_dict[todo_file]}\n```\n\n"
config_path = os.path.join(debug_dir, "config.yaml")
if os.path.exists(config_path):
with open(config_path, "r", encoding="utf-8") as f:
config_yaml = f.read()
codes += f"```yaml\n## File name: config.yaml\n{config_yaml}\n```\n\n"
reproduce_path = os.path.join(debug_dir, "reproduce.sh")
if os.path.exists(reproduce_path):
with open(reproduce_path, "r", encoding="utf-8") as f:
reproduce_sh = f.read()
codes += f"```bash\n## File name: reproduce.sh\n{reproduce_sh}\n```\n\n"
# --------------------------------------------------
# Build debugging prompt
# --------------------------------------------------
msg = [
{
"role": "system",
"content": """You are a highly capable code assistant specializing in debugging real-world code repositories. You will be provided with:
(1) a code repository (in part or in full), and
(2) one or more execution error messages generated during the execution of the repository.
Your objective is to debug the code so that it executes successfully.
This may involve identifying the root causes of the errors, modifying faulty logic or syntax, handling missing dependencies, or making other appropriate corrections.
Guidelines:
- Provide the exact lines or file changes needed to resolve the issue.
- When necessary, suggest best practices or improvements to prevent similar issues.
- Show only the modified lines using a unified diff format:
<<<<<<< SEARCH
original line
=======
corrected line
>>>>>>> REPLACE
- If multiple fixes are needed, provide them sequentially with clear separation.
- If external dependencies or environment setups are required (for example, packages, versions, file paths), specify them explicitly.
Constraints:
- Do not make speculative edits without justification.
- Do not assume access to an internet connection for installation or retrieval unless explicitly stated.
- Prioritize minimal and effective fixes that preserve the original intent of the code.
- Maintain the coding style and structure used in the original repository unless refactoring is necessary for correctness.
""",
},
{
"role": "user",
"content": f"""
### Code Repository
{codes}
--
### Execution Error Messages
{execution_error_msg}
--
## Instruction
Now, you need to debug the above code so that it runs without errors. Identify the cause of the execution error and modify the code appropriately. Your output must follow the exact format as shown in the example below.
--
## Format Example
Filename: train.py
<<<<<<< SEARCH
result = model.predict(input_data)
=======
result = model(input_data)
>>>>>>> REPLACE
--
## Answer
""",
},
]
response = client.chat.completions.create(
model=args.model,
messages=msg,
reasoning_effort="high",
)
answer = response.choices[0].message.content
# print("===== RAW MODEL ANSWER =====")
# print(answer)
# Use the direct API response as input to the patch applier
responses = [answer]
parse_and_apply_changes(responses, debug_dir, save_num=args.save_num)
|