-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathcode_validator.py
More file actions
87 lines (67 loc) · 2.9 KB
/
Copy pathcode_validator.py
File metadata and controls
87 lines (67 loc) · 2.9 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
"""Code validation trick with self-healing retry.
Validates model-generated code changes by:
1. Asking the model to describe the proposed change
2. Comparing that description against the original user request
3. Retrying with feedback if the descriptions don't match
"""
from src.trick import Trick, callmodel_sync
class CodeValidatorTrick(Trick):
"""Validate code changes through self-description and comparison."""
def __init__(self, max_attempts: int = 3):
self.max_attempts = max_attempts
def _get_user_request(self, context: list) -> str:
"""Extract the last user message from context."""
for msg in reversed(context):
if msg.get("role") == "user":
return msg.get("content", "")
return ""
def post_hook(self, context: list) -> list:
"""Validate the proposed code change and retry if needed."""
if not context:
return context
last_message = context[-1]
proposed_change = last_message.get("content", "")
if not proposed_change:
return context
user_request = self._get_user_request(context)
attempts = 0
while attempts < self.max_attempts:
try:
desc_context = callmodel_sync(
[{"role": "system", "content": "You are a code reviewer."}],
f"Describe what this code change does:\n\n{proposed_change}",
)
except Exception:
break
model_description = desc_context[-1].get("content", "")
try:
compare_context = callmodel_sync(
[{"role": "system", "content": "You are a validation assistant. Compare the two descriptions."}],
f"User Request:\n{user_request}\n\n"
f"Description of Proposed Code:\n{model_description}\n\n"
"Are these two descriptions effectively the same? "
"Answer Yes or No, and give reasons.",
)
except Exception:
break
validation = compare_context[-1].get("content", "")
verdict = validation.strip().upper()
if verdict.startswith("YES"):
break
attempts += 1
if attempts >= self.max_attempts:
break
context = context[:-1]
try:
context = callmodel_sync(
context,
f"The last proposed change failed the following validation:\n{validation}\n\n"
"You must do a new approach. Generate a different code change.",
)
except Exception:
break
proposed_change = context[-1].get("content", "")
return context
def info(self, capabilities: dict) -> dict:
capabilities["code_validation"] = True
return capabilities