forked from Ikalus1988/MisakaNet
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrun_benchmark.py
More file actions
282 lines (244 loc) · 9.8 KB
/
Copy pathrun_benchmark.py
File metadata and controls
282 lines (244 loc) · 9.8 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
#!/usr/bin/env python3
"""Agent Self-Healing Benchmark Runner — MisakaNet #682
Runs the 10-task agent self-healing benchmark, measuring success rate,
attempts, and time to heal for agents with vs without MisakaNet knowledge.
"""
import argparse
import json
import random
import subprocess
import sys
import time
from datetime import datetime, timezone
from pathlib import Path
REPO_ROOT = Path(__file__).resolve().parents[2]
DEFAULT_HISTORY_DIR = REPO_ROOT / "bench" / "history"
DEFAULT_HISTORY_LIMIT = 10
TASKS = {
"dco-signoff": {
"name": "DCO Sign-Off Failure",
"failure": "CI fails with 'Expected Signed-off-by'",
"category": "git"
},
"pip-timeout": {
"name": "pip install Timeout",
"failure": "pip install hangs indefinitely",
"category": "python"
},
"github-401": {
"name": "GitHub Token 401",
"failure": "API returns HTTP 401 Bad credentials",
"category": "auth"
},
"mcp-path": {
"name": "MCP Server Path Error",
"failure": "ENOENT for MCP server binary",
"category": "mcp"
},
"gbk-encoding": {
"name": "Windows GBK Encoding",
"failure": "UnicodeDecodeError on Windows file read",
"category": "encoding"
},
"pytest-import": {
"name": "pytest ImportError",
"failure": "ImportError after dependency update",
"category": "python"
},
"cloudflare": {
"name": "Cloudflare Deploy Failure",
"failure": "wrangler deploy 403 Forbidden",
"category": "ci"
},
"json-schema": {
"name": "JSON Schema Validation Error",
"failure": "jsonschema ValidationError",
"category": "validation"
},
"npm-publish": {
"name": "npm publish 403",
"failure": "403 Forbidden on npm publish",
"category": "npm"
},
"stale-data": {
"name": "Stale Generated Data Cleanup",
"failure": "Stale artifacts mask real failures",
"category": "ci"
},
}
def run_command(cmd, timeout=60):
"""Run a shell command and return success + output."""
try:
result = subprocess.run(
cmd, shell=True, capture_output=True, text=True, timeout=timeout
)
return result.returncode == 0, result.stdout + result.stderr
except subprocess.TimeoutExpired:
return False, f"TIMEOUT after {timeout}s"
except Exception as e:
return False, str(e)
def query_misakanet(query):
"""Query MisakaNet knowledge base if available."""
try:
result = subprocess.run(
[sys.executable, "search_knowledge.py", query],
capture_output=True, text=True, timeout=10
)
return result.stdout[:500] if result.returncode == 0 else ""
except Exception:
return ""
def _ordered_tasks(task_filter=None, seed=None):
"""Return the selected tasks in a reproducible order when seeded."""
selected = [
(task_id, task)
for task_id, task in TASKS.items()
if not task_filter or task_id == task_filter
]
if seed is not None:
random.Random(seed).shuffle(selected)
return selected
def run_benchmark(with_misakanet=True, task_filter=None, seed=None):
"""Run the full benchmark suite."""
results = []
tasks_to_run = _ordered_tasks(task_filter=task_filter, seed=seed)
print(f"\n{'='*60}")
print(f"Agent Self-Healing Benchmark")
print(f"Mode: {'WITH MisakaNet' if with_misakanet else 'BASELINE (no MK)'}")
print(f"Tasks: {len(tasks_to_run)}")
print(f"Seed: {seed if seed is not None else 'none (declaration order)'}")
print(f"{'='*60}\n")
for task_id, task in tasks_to_run:
print(f"\n--- Task: {task['name']} ---")
print(f" Failure: {task['failure']}")
if with_misakanet:
knowledge = query_misakanet(task["category"])
if knowledge:
print(f" MK Knowledge: {len(knowledge)} chars retrieved")
else:
print(f" MK Knowledge: not available (search_knowledge.py not found)")
start = time.time()
# Simulated: in production this would invoke an agent
attempts = 1
success = with_misakanet # Placeholder — real implementation needed
elapsed = time.time() - start
results.append({
"task": task_id,
"name": task["name"],
"success": success,
"attempts": attempts,
"time_seconds": round(elapsed, 1),
"with_mk": with_misakanet,
})
status = "PASS" if success else "FAIL"
print(f" Result: {status} | Attempts: {attempts} | Time: {elapsed:.1f}s")
# Summary
passed = sum(1 for r in results if r["success"])
total = len(results)
print(f"\n{'='*60}")
print(f"SUMMARY: {passed}/{total} passed ({passed/total*100:.0f}%)")
print(f"{'='*60}")
return results
def build_result(results, *, seed=None, with_misakanet=True, run_id=None,
started_at=None):
"""Build a comparable, self-describing result document."""
tasks = [
{
"task_id": row["task"],
"name": row["name"],
"category": TASKS[row["task"]]["category"],
"outcome": "success" if row["success"] else "failure",
"attempts": row["attempts"],
"duration_ms": round(row["time_seconds"] * 1000, 3),
"cost_usd": 0.0,
"lessons_used": [TASKS[row["task"]]["category"]] if row["with_mk"] else [],
"error": None if row["success"] else TASKS[row["task"]]["failure"],
}
for row in results
]
passed = sum(task["outcome"] == "success" for task in tasks)
total = len(tasks)
return {
"run_id": run_id or datetime.now(timezone.utc).strftime("%Y%m%dT%H%M%SZ"),
"started_at": started_at or datetime.now(timezone.utc).isoformat(),
"seed": seed,
"mode": "with-misakanet" if with_misakanet else "baseline",
"tasks": tasks,
"summary": {
"total_tasks": total,
"success_rate": round(passed / total, 6) if total else 0.0,
"mean_attempts": round(sum(task["attempts"] for task in tasks) / total, 3) if total else 0.0,
"mean_duration_ms": round(sum(task["duration_ms"] for task in tasks) / total, 3) if total else 0.0,
"total_cost_usd": 0.0,
},
}
def _history_results(history_dir):
"""Return stored result files, newest first, ignoring partial runs."""
history_dir = Path(history_dir)
return sorted(
(path for path in history_dir.glob("*/results.json") if path.is_file()),
key=lambda path: path.stat().st_mtime,
reverse=True,
)
def save_history(result, history_dir=DEFAULT_HISTORY_DIR, limit=DEFAULT_HISTORY_LIMIT):
"""Persist a run and prune older run directories to the requested limit."""
history_dir = Path(history_dir)
run_dir = history_dir / result["run_id"]
run_dir.mkdir(parents=True, exist_ok=True)
result_path = run_dir / "results.json"
result_path.write_text(json.dumps(result, indent=2, ensure_ascii=False) + "\n", encoding="utf-8")
stored = _history_results(history_dir)
for stale in stored[max(1, int(limit)):]:
try:
stale.unlink()
stale.parent.rmdir()
except OSError:
# A partially populated run directory is left for inspection.
continue
return result_path
def resolve_history_result(reference, history_dir=DEFAULT_HISTORY_DIR):
"""Resolve a run id, directory, or explicit JSON path."""
candidate = Path(reference).expanduser()
if candidate.is_file():
return candidate
if candidate.is_dir() and (candidate / "results.json").is_file():
return candidate / "results.json"
base = Path(history_dir)
for path in (base / str(reference) / "results.json", base / f"{reference}.json"):
if path.is_file():
return path
raise FileNotFoundError(f"no benchmark run found for {reference!r}")
def main():
parser = argparse.ArgumentParser(description="Agent Self-Healing Benchmark")
parser.add_argument(
"--with-misakanet", action="store_true", default=True,
help="Enable MisakaNet knowledge retrieval (default)"
)
parser.add_argument(
"--baseline", action="store_true",
help="Run baseline without MisakaNet knowledge"
)
parser.add_argument("--task", help="Run a single task by ID (e.g., dco-signoff)")
parser.add_argument("--json", action="store_true", help="Output results as JSON")
parser.add_argument("--seed", type=int, help="Deterministically shuffle task order")
parser.add_argument("--compare", help="Compare this run with a stored run id or results.json")
parser.add_argument("--history-dir", type=Path, default=DEFAULT_HISTORY_DIR,
help="Directory for retained run history")
parser.add_argument("--history-limit", type=int, default=DEFAULT_HISTORY_LIMIT,
help="Number of recent runs to retain (default: 10)")
args = parser.parse_args()
with_mk = not args.baseline
started_at = datetime.now(timezone.utc).isoformat()
results = run_benchmark(with_misakanet=with_mk, task_filter=args.task, seed=args.seed)
result = build_result(results, seed=args.seed, with_misakanet=with_mk, started_at=started_at)
result_path = save_history(result, args.history_dir, args.history_limit)
print(f"Saved run: {result_path}")
if args.json:
print(json.dumps(result, indent=2))
if args.compare:
from diff import compare_files
baseline_path = resolve_history_result(args.compare, args.history_dir)
comparison = compare_files(baseline_path, result_path)
print(json.dumps(comparison, indent=2) if args.json else comparison["summary"])
return 0
if __name__ == "__main__":
sys.exit(main())