Repository navigation
Expand file tree
/
Copy pathauditor.py
More file actions
293 lines (221 loc) · 7.85 KB
/
Copy pathauditor.py
File metadata and controls
293 lines (221 loc) · 7.85 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
import subprocess
import json
import requests
import os
import re
# ==============================
# INPUT
# ==============================
def get_user_code():
print("Paste your Python code. Type END on a new line to finish.\n")
lines = []
while True:
try:
line = input()
if line.strip() == "END":
break
lines.append(line)
except EOFError:
break
return "\n".join(lines)
# ==============================
# FILE HANDLING
# ==============================
def write_temp_file(code):
file_path = "temp_code.py"
with open(file_path, "w") as f:
f.write(code)
return file_path
# ==============================
# STATIC ANALYSIS
# ==============================
def run_ruff(file_path):
return subprocess.run(
["ruff", "check", file_path, "--output-format=json"],
capture_output=True,
text=True
)
# ==============================
# RUNTIME EXECUTION
# ==============================
def run_python(file_path):
try:
return subprocess.run(
["python", file_path],
capture_output=True,
text=True,
timeout=5
)
except subprocess.TimeoutExpired:
return subprocess.CompletedProcess(
args=["python", file_path],
returncode=1,
stdout="",
stderr="Error: Execution timed out (5s limit)."
)
# ==============================
# LOCAL LLM CALL
# ==============================
def run_llm_review(code, lint_json, runtime_error):
prompt = f"""
You are a strict Python correction engine.
Original Code:
{code}
Static Errors:
{lint_json}
Runtime Error:
{runtime_error}
STRICT RULES:
- Preserve original program intent.
- You MAY remove unused imports.
- You MAY remove unnecessary code.
- Do NOT change numeric literals.
- Do NOT modify arithmetic values.
- Do NOT introduce new imports unless absolutely required.
- Handle runtime errors safely without altering numbers.
- Return a complete corrected program.
FORMAT:
EXPLANATION:
<brief explanation>
CORRECTED_CODE:
<full corrected program>
"""
try:
response = requests.post(
"http://localhost:11434/api/generate",
json={
"model": "llama3",
"prompt": prompt,
"stream": False,
"temperature": 0
},
timeout=180
)
result = response.json()
return result.get("response", "")
except Exception as e:
return f"LLM call failed: {e}"
# ==============================
# EXTRACTION
# ==============================
def extract_corrected_code(output):
import re
# Extract after explicit marker
if "CORRECTED_CODE:" in output:
after = output.split("CORRECTED_CODE:", 1)[1]
# Remove explanation section if included
if "EXPLANATION:" in after:
after = after.split("EXPLANATION:", 1)[0]
return after.strip()
# Extract fenced python block
matches = re.findall(r"```python(.*?)```", output, re.DOTALL)
if matches:
code = matches[-1]
if "EXPLANATION:" in code:
code = code.split("EXPLANATION:", 1)[0]
return code.strip()
return None
# ==============================
# SEMANTIC VALIDATION
# ==============================
def semantic_validation(original, corrected):
# 1. Numeric literals preserved
original_numbers = re.findall(r'\d+', original)
corrected_numbers = re.findall(r'\d+', corrected)
if original_numbers != corrected_numbers:
return False, "Numeric literals were modified."
# 2. No new imports
original_imports = re.findall(r'^import\s+\w+', original, re.MULTILINE)
corrected_imports = re.findall(r'^import\s+\w+', corrected, re.MULTILINE)
for imp in corrected_imports:
if imp not in original_imports:
return False, "New imports were introduced."
# 3. Preserve print logic
if "print" in original and "print" not in corrected:
return False, "Core print logic removed."
return True, ""
# ==============================
# MAIN
# ==============================
def main():
code = get_user_code()
if not code.strip():
print("No code provided.")
return
file_path = write_temp_file(code)
try:
print("\n--- Static Analysis ---")
ruff_result = run_ruff(file_path)
lint_json = ruff_result.stdout if ruff_result.stdout else "[]"
try:
errors = json.loads(lint_json) if lint_json else []
if not errors:
print("No lint issues.")
else:
for e in errors:
print(f"{e['code']} - {e['message']} (Line {e['location']['row']})")
except:
print("Could not parse Ruff output.")
print("\n--- Runtime Execution ---")
python_result = run_python(file_path)
runtime_error = ""
if python_result.returncode == 0:
print("Execution successful.")
if python_result.stdout:
print("Output:\n", python_result.stdout)
else:
runtime_error = python_result.stderr
print("Runtime error:\n", runtime_error)
if errors or runtime_error:
print("\n--- LLM Review ---\n")
max_attempts = 2
attempt = 0
validation_passed = False
current_review = run_llm_review(code, lint_json, runtime_error)
print(current_review)
while attempt < max_attempts and not validation_passed:
print(f"\n--- Validation Attempt {attempt + 1} ---\n")
corrected_code = extract_corrected_code(current_review)
if not corrected_code:
print("No corrected code found.")
break
valid, reason = semantic_validation(code, corrected_code)
if not valid:
print("Semantic validation failed:", reason)
attempt += 1
current_review = run_llm_review(
code,
lint_json,
runtime_error + "\nValidation Failure: " + reason
)
print(current_review)
continue
write_temp_file(corrected_code)
new_ruff = run_ruff("temp_code.py")
try:
new_errors = json.loads(new_ruff.stdout) if new_ruff.stdout else []
except:
new_errors = ["Parsing error"]
new_runtime = run_python("temp_code.py")
if not new_errors and new_runtime.returncode == 0:
print("✔ Correction validated successfully.")
validation_passed = True
else:
print("Static or runtime errors still exist.")
attempt += 1
current_review = run_llm_review(
code,
lint_json,
runtime_error + "\nValidation Failure: Static/Runtime errors remain."
)
print(current_review)
if not validation_passed:
print("\n⚠ Auto-correction failed after retries.")
print("Returning structured explanation only.")
else:
print("\nCode looks clean. No correction needed.")
finally:
if os.path.exists(file_path):
os.remove(file_path)
if __name__ == "__main__":
main()