Skip to content

Commit 05b37d3

Browse files
trying to thrive
1 parent 0ad3ec2 commit 05b37d3

File tree

8 files changed

+1582
-269
lines changed

8 files changed

+1582
-269
lines changed

README.md

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -372,7 +372,6 @@ def calculate_fibonacci(n: int) -> str:
372372
#### HCMSManager
373373
Hierarchical memory with FAISS vector search.
374374

375-
376375
### Layer 2: Mathematical Grounding
377376
- **Z3 Theorem Prover**: Proves contradictions in factual claims
378377
- **Fact Extraction**: Parses natural language into verifiable assertions

src/hanerma/interface/empathy.py

Lines changed: 65 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
import json
22
import requests
3-
from typing import Dict, Any
3+
import ast
4+
from typing import Dict, Any, Optional
45

56
class EmpathyHandler:
67
"""
78
Emotional Failsafe: Sends tracebacks to local model for JSON mitigation strategies.
9+
Enhanced with autonomous AST patching capabilities.
810
"""
911

1012
def get_mitigation(self, traceback_str: str) -> Dict[str, Any]:
@@ -52,6 +54,68 @@ def get_mitigation(self, traceback_str: str) -> Dict[str, Any]:
5254
"action": "ask_human"
5355
}
5456

57+
async def generate_ast_patch(self, error_str: str, node_data: Dict[str, Any]) -> Optional[ast.AST]:
58+
"""
59+
Generates a new AST node to fix the error dynamically.
60+
Uses AI to analyze the error and create a patched version of the failing AST node.
61+
"""
62+
system_prompt = """
63+
You are an AST Patching AI. Your task is to analyze Python errors and generate corrected AST code.
64+
65+
Given an error message and the original AST node information, output a valid Python code snippet that fixes the error.
66+
The output should be executable Python code that can replace the failing node.
67+
68+
Consider common fixes like:
69+
- Adding try/except blocks
70+
- Providing default values for missing variables
71+
- Correcting function calls
72+
- Adding type checks
73+
74+
Output ONLY the corrected Python code, no explanations.
75+
"""
76+
77+
# Extract node info
78+
original_node = node_data.get('ast_node', {})
79+
node_type = type(original_node).__name__ if original_node else "Unknown"
80+
81+
prompt = f"""
82+
Error: {error_str}
83+
Node Type: {node_type}
84+
Original Node: {ast.dump(original_node) if original_node else "N/A"}
85+
86+
Generate corrected Python code:
87+
"""
88+
89+
full_prompt = f"{system_prompt}\n\n{prompt}"
90+
91+
try:
92+
# Send to local Ollama Qwen model
93+
response = requests.post(
94+
"http://localhost:11434/api/generate",
95+
json={
96+
"model": "qwen",
97+
"prompt": full_prompt,
98+
"stream": False
99+
},
100+
timeout=15
101+
)
102+
response.raise_for_status()
103+
code_snippet = response.json()["response"].strip()
104+
105+
# Parse the generated code into AST
106+
try:
107+
tree = ast.parse(code_snippet)
108+
# Return the first statement as the patch
109+
if tree.body:
110+
return tree.body[0]
111+
except SyntaxError:
112+
return None
113+
114+
except Exception:
115+
return None
116+
117+
return None
118+
55119
def friendly_fail(traceback_str: str) -> Dict[str, Any]:
56120
handler = EmpathyHandler()
57121
return handler.get_mitigation(traceback_str)

0 commit comments

Comments
 (0)