Repository navigation
Expand file tree
/
Copy pathbaseline.py
More file actions
368 lines (312 loc) · 14.4 KB
/
Copy pathbaseline.py
File metadata and controls
368 lines (312 loc) · 14.4 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
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
"""
baseline.py — Advanced deterministic + LLM baseline for NetworkDiagnosticsEnv.
NO API KEY REQUIRED for the rule-based agent.
All 6 tasks are covered with an expert playbook.
Usage:
# Rule-based (no API key):
python baseline.py
# LLM agent:
OPENAI_API_KEY=sk-... python baseline.py
# Against HF Space:
ENV_URL=https://your-space.hf.space python baseline.py
"""
import json
import os
import sys
try:
import httpx
def _post(url, body):
with httpx.Client(timeout=30) as c:
r = c.post(url, json=body)
r.raise_for_status()
return r.json()
def _get(url):
with httpx.Client(timeout=30) as c:
r = c.get(url)
r.raise_for_status()
return r.json()
except ImportError:
import urllib.request
def _post(url, body):
data = json.dumps(body).encode()
req = urllib.request.Request(url, data=data,
headers={"Content-Type": "application/json"}, method="POST")
with urllib.request.urlopen(req, timeout=30) as resp:
return json.loads(resp.read())
def _get(url):
with urllib.request.urlopen(url, timeout=30) as resp:
return json.loads(resp.read())
ENV_URL = os.environ.get("ENV_URL", "http://localhost:7860").rstrip("/")
OPENAI_API_KEY = os.environ.get("OPENAI_API_KEY", "")
# ── Expert playbook for rule-based agent ──────────────────────────────────────
# Each entry is a list of action dicts sent in sequence.
# The expert always uses the minimal correct tool path.
EXPERT_PLAYBOOK = {
# ── Easy ──────────────────────────────────────────────────────────────────
"dns_failure": {
"steps": [
{"action_type": "ListToolsAction"},
{"action_type": "CallToolAction", "tool_name": "nslookup",
"tool_params": {"domain": "google.com"}},
{"action_type": "CallToolAction", "tool_name": "check_logs",
"tool_params": {"host": "dns-server", "lines": 20}},
{"action_type": "ResolveAction",
"root_cause": "dns_misconfiguration",
"fix_applied": "fixed_named_conf_and_restarted_named"},
],
},
"dhcp_starvation": {
"steps": [
{"action_type": "ListToolsAction"},
{"action_type": "CallToolAction", "tool_name": "check_dhcp",
"tool_params": {"host": "dhcp-server"}},
{"action_type": "CallToolAction", "tool_name": "arp_scan",
"tool_params": {"subnet": "10.0.0.0/24"}},
{"action_type": "ResolveAction",
"root_cause": "dhcp_pool_exhausted",
"fix_applied": "blocked_rogue_mac_flushed_stale_leases"},
],
},
# ── Medium ────────────────────────────────────────────────────────────────
"firewall_block": {
"steps": [
{"action_type": "ListToolsAction"},
{"action_type": "CallToolAction", "tool_name": "ping",
"tool_params": {"target": "internet-router"}},
{"action_type": "CallToolAction", "tool_name": "traceroute",
"tool_params": {"target": "internet-router"}},
{"action_type": "CallToolAction", "tool_name": "check_iptables",
"tool_params": {"host": "internet-router", "chain": "FORWARD"}},
{"action_type": "ResolveAction",
"root_cause": "firewall_rule_drop",
"fix_applied": "iptables_delete_drop_rule"},
],
},
"ntp_drift": {
"steps": [
{"action_type": "ListToolsAction"},
{"action_type": "CallToolAction", "tool_name": "curl",
"tool_params": {"url": "https://internal-svc/health"}},
{"action_type": "CallToolAction", "tool_name": "check_ntp",
"tool_params": {"host": "host-a"}},
{"action_type": "CallToolAction", "tool_name": "check_service",
"tool_params": {"host": "ntp-server", "service": "ntpd"}},
{"action_type": "ResolveAction",
"root_cause": "ntp_clock_skew",
"fix_applied": "restarted_ntpd_and_force_synced_clocks"},
],
},
# ── Hard ──────────────────────────────────────────────────────────────────
"cascading_failure": {
"steps": [
{"action_type": "ListToolsAction"},
{"action_type": "CallToolAction", "tool_name": "curl",
"tool_params": {"url": "http://web-svc/health"}},
{"action_type": "CallToolAction", "tool_name": "check_logs",
"tool_params": {"host": "app-server", "lines": 30}},
{"action_type": "CallToolAction", "tool_name": "traceroute",
"tool_params": {"target": "db-server"}},
{"action_type": "CallToolAction", "tool_name": "check_bgp",
"tool_params": {"host": "core-router"}},
{"action_type": "CallToolAction", "tool_name": "check_logs",
"tool_params": {"host": "core-router", "lines": 30}},
{"action_type": "ResolveAction",
"root_cause": "bgp_peer_reset",
"fix_applied": "restart_bgp_session_on_core_router"},
],
},
"split_brain": {
"steps": [
{"action_type": "ListToolsAction"},
{"action_type": "CallToolAction", "tool_name": "check_cluster",
"tool_params": {"host": "cluster-node-1"}},
{"action_type": "CallToolAction", "tool_name": "check_logs",
"tool_params": {"host": "cluster-node-2", "lines": 30}},
{"action_type": "CallToolAction", "tool_name": "check_logs",
"tool_params": {"host": "cluster-node-1", "lines": 30}},
{"action_type": "CallToolAction", "tool_name": "check_cluster",
"tool_params": {"host": "cluster-node-2"}},
{"action_type": "ResolveAction",
"root_cause": "split_brain_misconfigured_heartbeat",
"fix_applied": "fenced_stale_leader_increased_heartbeat_timeout"},
],
},
}
# ── Rule-based agent ──────────────────────────────────────────────────────────
def run_rule_based_agent(task: dict) -> dict:
scenario_id = task["task_id"]
difficulty = task["difficulty"]
print(f"\n{'='*65}")
print(f" Task : {task['name']} [{difficulty.upper()}]")
print(f" Agent : rule-based expert (no API key required)")
print(f" Scenario : {scenario_id}")
print(f"{'='*65}")
obs = _post(f"{ENV_URL}/reset", {
"scenario_id": scenario_id,
"difficulty": difficulty,
"os_profile": "linux",
})
print(f" [RESET] {obs['observation']['stdout'][:100]}")
playbook = EXPERT_PLAYBOOK.get(scenario_id, {})
steps_ = playbook.get("steps", [])
tool_cost = 0.0
tool_names = []
steps_taken = 0
final_rca = ""
for action in steps_:
result = _post(f"{ENV_URL}/step", action)
obs_ = result["observation"]
reward = result.get("reward", 0.0)
out = (obs_.get("stdout") or obs_.get("stderr") or "")[:100]
meta = obs_.get("info", {}).get("tool_metadata", {})
if reward < 0:
tool_cost += reward
atype = action["action_type"]
if atype == "CallToolAction":
tool_names.append(action.get("tool_name", "?"))
steps_taken += 1
meta_str = f" metadata={json.dumps(meta)}" if meta else ""
print(f" [Step {steps_taken:2d}] {atype:25s} | {out}{meta_str}")
if atype == "ResolveAction":
final_rca = action.get("root_cause", "")
break
grade = _post(f"{ENV_URL}/grader", {
"scenario_id": scenario_id,
"root_cause_submitted": final_rca,
"steps_taken": steps_taken,
"tool_cost_sum": tool_cost,
"tool_names": tool_names,
})
print(f"\n Score : {grade['score']:.4f} | Passed: {grade['passed']}")
print(f" Expected: {grade['expected_root_cause']}")
print(f" Got : {final_rca}")
if "breakdown" in grade:
for dim, val in grade["breakdown"].items():
print(f" {dim:18s}: {val:.4f}")
return grade
# ── LLM agent ─────────────────────────────────────────────────────────────────
def run_llm_agent(task: dict) -> dict:
try:
from openai import OpenAI
except ImportError:
print(" [WARN] openai package not installed — falling back to rule-based agent")
return run_rule_based_agent(task)
client = OpenAI(api_key=OPENAI_API_KEY)
model = os.environ.get("OPENAI_MODEL", "gpt-4o-mini")
scenario_id = task["task_id"]
difficulty = task["difficulty"]
max_steps = task["max_steps"]
print(f"\n{'='*65}")
print(f" Task : {task['name']} [{difficulty.upper()}]")
print(f" Agent : LLM ({model})")
print(f"{'='*65}")
system = (
"You are a network SRE agent. At each turn reply ONLY with a single JSON action:\n"
'{"action_type":"ListToolsAction"}\n'
'{"action_type":"CallToolAction","tool_name":"<n>","tool_params":{...}}\n'
'{"action_type":"ResolveAction","root_cause":"<rca>","fix_applied":"<fix>"}\n'
"Known root-cause strings: dns_misconfiguration | dhcp_pool_exhausted | "
"firewall_rule_drop | ntp_clock_skew | bgp_peer_reset | split_brain_misconfigured_heartbeat"
)
obs = _post(f"{ENV_URL}/reset", {
"scenario_id": scenario_id, "difficulty": difficulty, "os_profile": "linux"
})
print(f" [RESET] {obs['observation']['stdout'][:100]}")
messages = [
{"role": "system", "content": system},
{"role": "user", "content": (
f"Scenario: {task.get('description', task['name'])}\n"
f"Initial obs: {obs['observation']['stdout']}\n"
"Start with ListToolsAction."
)},
]
tool_cost = 0.0
tool_names = []
steps_taken = 0
final_rca = ""
while steps_taken < max_steps:
resp = client.chat.completions.create(
model=model, messages=messages, temperature=0.0, max_tokens=300
)
raw = resp.choices[0].message.content.strip()
messages.append({"role": "assistant", "content": raw})
try:
action = json.loads(raw)
except json.JSONDecodeError:
messages.append({"role": "user", "content": "Reply ONLY with a JSON action object."})
continue
result = _post(f"{ENV_URL}/step", action)
obs_ = result["observation"]
reward = result.get("reward", 0.0)
out = (obs_.get("stdout") or obs_.get("stderr") or "")[:120]
meta = obs_.get("info", {}).get("tool_metadata", {})
if reward < 0:
tool_cost += reward
atype = action.get("action_type", "?")
if atype == "CallToolAction":
tool_names.append(action.get("tool_name", "?"))
steps_taken += 1
print(f" [Step {steps_taken:2d}] {atype:25s} | {out}")
feedback = out
if meta:
feedback += f"\n[metadata] {json.dumps(meta)}"
messages.append({"role": "user", "content": f"Observation: {feedback}"})
if atype == "ResolveAction":
final_rca = action.get("root_cause", "")
break
grade = _post(f"{ENV_URL}/grader", {
"scenario_id": scenario_id,
"root_cause_submitted": final_rca,
"steps_taken": steps_taken,
"tool_cost_sum": tool_cost,
"tool_names": tool_names,
})
print(f"\n Score: {grade['score']:.4f} | Passed: {grade['passed']}")
return grade
# ── Main ──────────────────────────────────────────────────────────────────────
def main():
agent_label = f"LLM ({os.environ.get('OPENAI_MODEL','gpt-4o-mini')})" if OPENAI_API_KEY else "Rule-based (no API key)"
print(f"\nNetworkDiagnosticsEnv v2.0 — Baseline Script")
print(f" Server : {ENV_URL}")
print(f" Agent : {agent_label}")
try:
health = _get(f"{ENV_URL}/health")
print(f" Health : {health}")
except Exception as e:
print(f"\nERROR: Cannot reach {ENV_URL} ({e})")
print(" Start the server: uvicorn server.app:app --port 7860")
sys.exit(1)
tasks_resp = _get(f"{ENV_URL}/tasks")
tasks = tasks_resp["tasks"]
print(f" Tasks : {[t['task_id'] for t in tasks]} ({len(tasks)} total)\n")
run_task = run_llm_agent if OPENAI_API_KEY else run_rule_based_agent
all_results = []
for task in tasks:
try:
result = run_task(task)
all_results.append(result)
except Exception as e:
print(f" [ERROR] Task {task['task_id']} failed: {e}")
all_results.append({"score": 0.0, "passed": False})
scores = [r["score"] for r in all_results]
avg = round(sum(scores) / len(scores), 4) if scores else 0.0
n_pass = sum(1 for r in all_results if r["passed"])
print(f"\n{'='*65}")
print(f"BASELINE RESULTS")
print(f" Tasks run : {len(all_results)}")
print(f" Tasks passed : {n_pass} / {len(all_results)}")
print(f" Scores : {[round(s, 4) for s in scores]}")
print(f" Average score: {avg}")
print(f"{'='*65}\n")
print("Verifying against /baseline endpoint (deterministic):")
try:
bl = _get(f"{ENV_URL}/baseline")
print(f" /baseline avg : {bl['average_score']}")
print(f" /baseline tasks: {bl['tasks_total']} passed={bl['tasks_passed']}")
for t in bl["tasks"]:
print(f" {t['task_id']:35s} [{t['difficulty']:6s}] "
f"score={t['score']:.4f} passed={t['passed']}")
except Exception as e:
print(f" /baseline check failed: {e}")
if __name__ == "__main__":
main()