-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathagent_autofix.py
More file actions
294 lines (241 loc) · 10 KB
/
agent_autofix.py
File metadata and controls
294 lines (241 loc) · 10 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
#!/usr/bin/env python3
"""
Intelligent Agent that fixes SimplicityHL compilation errors automatically
Uses an LLM to understand errors and generate fixes
"""
import asyncio
import sys
import json
from pathlib import Path
from mcp import ClientSession, StdioServerParameters
from mcp.client.stdio import stdio_client
# Configure your LLM here - examples for different providers
LLM_PROVIDER = "anthropic" # Options: "anthropic", "openai", "local"
# For Anthropic Claude
ANTHROPIC_API_KEY = None # Set your API key or use environment variable
async def fix_code_with_llm(source_code: str, error_message: str, attempt: int) -> str:
"""
Uses an LLM to fix the code based on the error message.
This is where you integrate your preferred LLM:
- Anthropic Claude
- OpenAI GPT
- Local model via Ollama
- Any other LLM API
"""
print(f"\n[Agent] Analyzing error (attempt {attempt})...")
print(f"Error: {error_message[:200]}...")
if LLM_PROVIDER == "anthropic":
print("[Warning] Anthropic API not configured. Using rule-based fixes.")
return apply_rule_based_fixes(source_code, error_message)
elif LLM_PROVIDER == "openai":
print("[Warning] OpenAI API not configured. Using rule-based fixes.")
return apply_rule_based_fixes(source_code, error_message)
else:
return apply_rule_based_fixes(source_code, error_message)
def apply_rule_based_fixes(source_code: str, error_message: str) -> str:
"""
Apply rule-based fixes based on common SimplicityHL syntax errors.
"""
import re
fixed_code = source_code
# Rule 1: Add 'fn main() -> ()' wrapper if missing
if "expected program" in error_message:
print("[Fix] Adding 'fn main() -> ()' wrapper")
fixed_code = f"fn main() -> () {{\n{fixed_code}\n ()\n}}"
if "expected EOI or item" in error_message:
print("[Fix] Removing comments (SimplicityHL may not support // comments)")
# Remove single-line comments
lines = fixed_code.split('\n')
lines = [line for line in lines if not line.strip().startswith('//')]
fixed_code = '\n'.join(lines)
# Rule 2: Remove fn main() wrapper - SimplicityHL is top-level
if "fn main()" in fixed_code or "fn main (" in fixed_code:
print("[Fix] Removing fn main() wrapper (not needed in SimplicityHL)")
fixed_code = re.sub(
r'fn\s+main\s*\(\s*\)\s*\{(.*)\}',
r'\1',
fixed_code,
flags=re.DOTALL
)
# Clean up indentation
lines = fixed_code.split('\n')
lines = [line[4:] if line.startswith(' ') else line for line in lines]
fixed_code = '\n'.join(lines)
# Rule 3: Remove incorrect tuple patterns
if "let (" in fixed_code and ",)" in fixed_code:
print("[Fix] Removing incorrect tuple patterns")
fixed_code = re.sub(r'let\s+\((\w+),\)\s*=', r'let \1 =', fixed_code)
return fixed_code.strip() + '\n'
async def compile_and_fix(
session: ClientSession,
source_code: str,
witness_data: str,
max_attempts: int = 5,
use_llm: bool = False
) -> tuple[bool, str, list]:
"""
Attempts to compile code and automatically fix errors.
"""
current_code = source_code
attempts_history = []
for attempt in range(1, max_attempts + 1):
print(f"\n{'='*60}")
print(f"[Attempt {attempt}/{max_attempts}]")
print(f"{'='*60}")
# Try to compile
result = await session.call_tool(
"compile_simplicity",
arguments={
"source_code": current_code,
"witness_data": witness_data
}
)
content = result.content[0].text if result.content else ""
# Remove unicode for Windows compatibility
content_safe = content.encode('ascii', 'ignore').decode('ascii')
# Store attempt
attempts_history.append({
"attempt": attempt,
"code": current_code,
"success": "Compilation successful!" in content_safe,
"result": content_safe[:500]
})
# Check if successful
if "Compilation successful!" in content_safe:
print("\n[SUCCESS] Code compiled successfully!")
return True, current_code, attempts_history
# Extract error message
error_msg = content_safe
if "message" in content_safe:
try:
json_start = content_safe.find('{')
json_data = json.loads(content_safe[json_start:])
if isinstance(json_data, dict) and "result_json" in json_data:
error_msg = json_data["result_json"].get("message", error_msg)
except:
pass
print(f"\n[FAILED] Compilation failed")
# Remove unicode chars from error message for Windows compatibility
error_msg_safe = error_msg.encode('ascii', 'ignore').decode('ascii')
print(f"Error: {error_msg_safe[:200]}...")
# Don't try to fix on last attempt
if attempt >= max_attempts:
print(f"\n[Info] Max attempts ({max_attempts}) reached.")
break
# Try to fix the code
print(f"\n[Fixing] Attempting to fix code...")
if use_llm:
current_code = await fix_code_with_llm(current_code, error_msg, attempt)
else:
current_code = apply_rule_based_fixes(current_code, error_msg)
print(f"\n[Preview] Fixed code:")
preview = current_code[:300] + "..." if len(current_code) > 300 else current_code
# Remove unicode for Windows
preview_safe = preview.encode('ascii', 'ignore').decode('ascii')
print(preview_safe)
return False, current_code, attempts_history
async def test_file_with_agent(
session: ClientSession,
name: str,
source_file: str,
witness_file: str,
max_attempts: int = 5,
use_llm: bool = False
):
"""Test a single file with automatic fixing"""
print(f"\n{'='*70}")
print(f"[Testing] {name}")
print(f"{'='*70}")
# Read files
source_code = Path(source_file).read_text()
witness_data = Path(witness_file).read_text() if Path(witness_file).exists() else ""
print(f"Original code length: {len(source_code)} chars")
# Try to compile and fix
success, final_code, history = await compile_and_fix(
session,
source_code,
witness_data,
max_attempts=max_attempts,
use_llm=use_llm
)
# Save results
if success:
output_file = Path(source_file).parent / f"{Path(source_file).stem}_fixed.simf"
output_file.write_text(final_code)
print(f"\n[Saved] Fixed code saved to: {output_file}")
return success, history
async def main():
"""Run the agent"""
print("="*70)
print("SimplicityHL Auto-Fix Agent")
print("="*70)
# Parse arguments
use_docker = "--docker" in sys.argv
use_llm = "--llm" in sys.argv
max_attempts = 5
for arg in sys.argv:
if arg.startswith("--max-attempts="):
max_attempts = int(arg.split("=")[1])
print(f"\nConfiguration:")
print(f" Mode: {'Docker' if use_docker else 'Local'}")
print(f" LLM: {'Enabled' if use_llm else 'Rule-based only'}")
print(f" Max attempts: {max_attempts}")
# Setup server connection
if use_docker:
server_params = StdioServerParameters(
command="docker",
args=["exec", "-i", "mcp-simplicity-server", "python", "server.py"]
)
else:
server_params = StdioServerParameters(
command="python",
args=["server.py"]
)
async with stdio_client(server_params) as (read, write):
async with ClientSession(read, write) as session:
await session.initialize()
print("\n[Connected] MCP server ready")
# Test files
test_files = [
("Arithmetic", "examples/arithmetic.simf", "examples/arithmetic.wit"),
("Scoping", "examples/scoping.simf", "examples/scoping.wit"),
("Witness Equality", "examples/witness_equality.simf", "examples/witness_equality.wit"),
("Witness Computation", "examples/witness_computation.simf", "examples/witness_computation.wit"),
]
results = {}
all_history = {}
for name, source, witness in test_files:
success, history = await test_file_with_agent(
session,
name,
source,
witness,
max_attempts=max_attempts,
use_llm=use_llm
)
results[name] = success
all_history[name] = history
# Final summary
print("\n" + "="*70)
print("[SUMMARY] Final Results")
print("="*70)
for name, success in results.items():
status = "[OK] FIXED" if success else "[FAIL] NOT FIXED"
attempts = len(all_history[name])
print(f"{status}: {name} ({attempts} attempts)")
total = len(results)
fixed = sum(results.values())
print(f"\n[Results] {fixed}/{total} files fixed")
if fixed == total:
print("[Success] All files successfully fixed!")
return 0
else:
print(f"[Warning] {total - fixed} file(s) could not be fixed")
print("\nNext steps:")
print(" 1. Review the error messages above")
print(" 2. Check the SimplicityHL documentation for correct syntax")
print(" 3. Enable LLM mode with --llm flag for smarter fixes")
return 1
if __name__ == "__main__":
exit_code = asyncio.run(main())
sys.exit(exit_code)