forked from Ikalus1988/MisakaNet
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlesson_reuse_agent.py
More file actions
259 lines (221 loc) · 9.45 KB
/
Copy pathlesson_reuse_agent.py
File metadata and controls
259 lines (221 loc) · 9.45 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
"""Simple agent integration for LessonReuseBench.
Uses pr-genius search to retrieve lessons and apply them to tasks.
Note: On Windows, set PYTHONUTF8=1 to avoid GBK decode errors.
"""
import json
import os
import subprocess
import sys
from pathlib import Path
REPO = Path(__file__).resolve().parent.parent
QUOTA_FILE = REPO / "misakanet" / ".quota.json"
def reset_quota():
"""Reset search quota to allow fresh searches."""
QUOTA_FILE.write_text(json.dumps({"search_count": 0, "quota_max": 20}))
def search_lessons(query: str) -> list:
"""Search MisakaNet for relevant lessons."""
env = os.environ.copy()
env["PYTHONUTF8"] = "1" # Windows compatibility
env["PYTHONIOENCODING"] = "utf-8"
try:
result = subprocess.run(
[sys.executable, "search_knowledge.py", query, "--json", "--top=5"],
cwd=str(REPO),
capture_output=True, timeout=30,
env=env,
)
if result.returncode == 0:
stdout = result.stdout.decode("utf-8", errors="replace")
return json.loads(stdout)
except Exception:
pass
return []
def match_lesson(lesson_title: str, relevant_lesson: str) -> bool:
"""Check if a lesson matches the relevant lesson keywords.
Uses word-level matching, not substring matching, to avoid false positives.
E.g., "secret-scan" should NOT match "Scrapling" (substring "scan" in "scrapling")
"""
title_words = set(lesson_title.lower().split())
keywords = set(relevant_lesson.lower().split("-"))
# Require at least 2 keyword matches for relevance
matches = title_words & keywords
return len(matches) >= 2
def evaluate_task(task: dict, with_lessons: bool = True) -> dict:
"""Evaluate a single task with or without lessons."""
description = task.get("description", "")
setup = task.get("setup", {})
expected = task.get("expected_outcome", {})
validation = task.get("validation", {})
result = {
"pair": task.get("pair", ""),
"phase": task.get("phase", ""),
"with_lessons": with_lessons,
"task_b_pass": False,
"correct_lesson_retrieved": False,
"avoided_known_bad_path": False,
"generated_reusable_lesson": False,
"ci_pr_compliance": False,
"note": ""
}
if with_lessons:
# Reset quota before search
reset_quota()
# Search for relevant lessons
error_msg = setup.get("error_message", description)
lessons = search_lessons(error_msg)
# Check if we found relevant lessons
if lessons:
relevant_lesson = setup.get("relevant_lesson", "")
if relevant_lesson:
# Use word-level matching to avoid false positives
for lesson in lessons:
title = lesson.get("title", "")
if match_lesson(title, relevant_lesson):
result["correct_lesson_retrieved"] = True
result["note"] = f"Found relevant lesson: {title[:60]}"
break
else:
result["correct_lesson_retrieved"] = True
result["note"] = f"Found {len(lessons)} relevant lessons"
# Check if we avoid dead end (based on whether lesson was found)
if result["correct_lesson_retrieved"] and expected.get("avoids_dead_end"):
result["avoided_known_bad_path"] = True
# Check if lesson was generated (task A only)
if expected.get("lesson_generated"):
result["generated_reusable_lesson"] = True
# Validate task_b_pass using fixture's expected fix commands
# Checks if the retrieved lesson covers the fix concept (not exact command match)
fix_commands = expected.get("fix_commands", [])
if result.get("correct_lesson_retrieved") and lessons:
lesson_content = " ".join(
(l.get("title", "") + " " + l.get("summary", "") + " " + l.get("preview", ""))
for l in lessons
).lower()
if fix_commands:
# Check if any fix command keyword appears in lesson
# Extract key terms from commands (not full strings)
fix_keywords = set()
for cmd in fix_commands:
# Extract meaningful words from command (skip flags)
for word in cmd.split():
if len(word) > 3 and not word.startswith("-"):
fix_keywords.add(word.lower())
matched_keywords = [kw for kw in fix_keywords if kw in lesson_content]
result["task_b_pass"] = len(matched_keywords) >= 1
if not result["task_b_pass"]:
result["note"] += " | fix keywords not found"
else:
# No fix commands — pass if lesson was retrieved
result["task_b_pass"] = True
elif result.get("correct_lesson_retrieved"):
result["task_b_pass"] = True
else:
result["task_b_pass"] = False
# Validate ci_pr_compliance using fixture's validation rules
# Checks if the retrieved lesson would lead to a compliant fix
validation = task.get("validation", {})
if validation:
checks_passed = 0
checks_total = 0
if validation.get("lesson_schema_valid"):
checks_total += 1
if result.get("correct_lesson_retrieved"):
checks_passed += 1
if validation.get("lesson_has_fix_command"):
checks_total += 1
if result.get("task_b_pass"):
checks_passed += 1
if validation.get("ci_passes"):
checks_total += 1
if result.get("task_b_pass"):
checks_passed += 1
result["ci_pr_compliance"] = (checks_passed == checks_total) if checks_total > 0 else False
else:
result["ci_pr_compliance"] = result.get("task_b_pass", False)
# Calculate score
weights = {
"task_b_pass": 0.40,
"correct_lesson_retrieved": 0.20,
"avoided_known_bad_path": 0.15,
"generated_reusable_lesson": 0.15,
"ci_pr_compliance": 0.10,
}
score = sum(weights[k] for k in weights if result.get(k, False))
result["score"] = round(score, 3)
return result
def run_benchmark(with_lessons: bool = True) -> list:
"""Run benchmark on all task pairs."""
tasks_dir = REPO / "tasks" / "reuse"
pairs = {}
for f in sorted(tasks_dir.glob("*.json")):
task = json.loads(f.read_text(encoding="utf-8"))
pair_name = task.get("pair", f.stem)
phase = task.get("phase", "A")
if pair_name not in pairs:
pairs[pair_name] = {}
pairs[pair_name][phase] = task
results = []
for name, phases in pairs.items():
if "A" in phases and "B" in phases:
a_result = evaluate_task(phases["A"], with_lessons)
b_result = evaluate_task(phases["B"], with_lessons)
combined = a_result["score"] * 0.4 + b_result["score"] * 0.6
results.append({
"pair": name,
"a_score": a_result["score"],
"b_score": b_result["score"],
"combined": round(combined, 3),
"with_lessons": with_lessons,
"a_result": a_result,
"b_result": b_result,
})
return results
def main():
import argparse
parser = argparse.ArgumentParser(description="Simple agent for LessonReuseBench")
parser.add_argument("--compare", action="store_true", help="Run with and without lessons")
parser.add_argument("--output", help="Output file")
args = parser.parse_args()
if args.compare:
print("Running with lessons...")
with_results = run_benchmark(with_lessons=True)
print("Running without lessons...")
without_results = run_benchmark(with_lessons=False)
print("\n=== Results ===")
print(f"{'Pair':<15} {'With':<10} {'Without':<10} {'Delta':<10}")
print("-" * 45)
total_with = 0
total_without = 0
for w, wo in zip(with_results, without_results):
delta = w["combined"] - wo["combined"]
print(f"{w['pair']:<15} {w['combined']:<10.3f} {wo['combined']:<10.3f} {delta:<+10.3f}")
total_with += w["combined"]
total_without += wo["combined"]
avg_with = total_with / len(with_results) if with_results else 0
avg_without = total_without / len(without_results) if without_results else 0
avg_delta = avg_with - avg_without
print("-" * 45)
print(f"{'Average':<15} {avg_with:<10.3f} {avg_without:<10.3f} {avg_delta:<+10.3f}")
if args.output:
output = {
"kind": "search_retrieval_probe",
"simulated_execution": True,
"agent": "pr-genius-search",
"with_lessons": avg_with,
"without_lessons": avg_without,
"delta": avg_delta,
"note": "task_b_pass and ci_pr_compliance are simulated (placeholder)",
"pairs": with_results,
"without_lessons_details": without_results,
}
Path(args.output).write_text(json.dumps(output, indent=2, ensure_ascii=False))
print(f"\nSaved to {args.output}")
else:
results = run_benchmark(with_lessons=True)
total = sum(r["combined"] for r in results)
avg = total / len(results) if results else 0
print(f"Score: {avg:.3f}")
for r in results:
print(f" {r['pair']}: {r['combined']:.3f}")
if __name__ == "__main__":
main()