-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdemo.py
More file actions
423 lines (341 loc) · 21.7 KB
/
demo.py
File metadata and controls
423 lines (341 loc) · 21.7 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
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
"""demo.py — Ballmer Agentic Conception - BAC Live Hackathon Demonstration.
Run:
python demo.py
"""
from __future__ import annotations
import asyncio
import os
import sys
import time
from datetime import datetime, timedelta, timezone
from pathlib import Path
from typing import Any
# Rich terminal output
from rich.console import Console
from rich.panel import Panel
from rich.table import Table
from rich.live import Live
from rich.text import Text
# Ensure project root on path
_ROOT = Path(__file__).resolve().parent
if str(_ROOT) not in sys.path:
sys.path.insert(0, str(_ROOT))
console = Console()
# ─────────────────────────────────────────────────────────────────────────────
# Helpers
# ─────────────────────────────────────────────────────────────────────────────
def _pause(prompt: str = "") -> None:
"""Wait for the user to press Enter (no-op in non-interactive mode)."""
msg = f"\n[dim]{prompt or 'Press Enter to continue...'}[/dim]"
console.print(msg)
try:
input()
except EOFError:
pass
def _step(label: str) -> None:
console.print(f" [bold green]✅[/bold green] {label}")
def _info(label: str) -> None:
console.print(f" [dim cyan]ℹ[/dim cyan] {label}")
# ─────────────────────────────────────────────────────────────────────────────
# Phase 1: Initialization
# ─────────────────────────────────────────────────────────────────────────────
def _phase1_setup() -> dict[str, Any]:
"""Initialize all system components and return them for later phases."""
from src.simulator.topology import NetworkTopology
from src.simulator.telemetry import TelemetryGenerator
from src.simulator.engine import SimulationEngine
from src.simulator.anomaly_injector import AnomalyInjector
from src.agents.observer import ObserverAgent, _build_baselines_from_dataframe
from src.causal.causal_engine import CausalEngine
from src.safety.z3_verifier import Z3SafetyVerifier
console.print("\n[bold cyan]Phase 1: Initializing Network Digital Twin...[/bold cyan]")
# Topology
topology = NetworkTopology()
g = topology.get_graph()
n_nodes = g.number_of_nodes()
n_links = g.number_of_edges() // 2
_step(f"Network topology: {n_nodes} nodes, {n_links} undirected links")
# Baseline telemetry (2h for demo speed)
console.print(" [dim]Generating 2-hour baseline telemetry (1-min intervals)...[/dim]")
gen = TelemetryGenerator(topology, seed=42)
baseline_df = gen.generate_baseline(duration_hours=2)
_step(f"Baseline telemetry generated ({len(baseline_df):,} rows, 2 hours)")
# Train anomaly detectors
console.print(" [dim]Training ensemble anomaly detectors (IF + EWMA + Threshold)...[/dim]")
baselines = _build_baselines_from_dataframe(baseline_df)
observer = ObserverAgent(topology=topology, baselines=baselines)
observer.train_detectors(baseline_df)
_step("Anomaly detectors trained (IsolationForest · EWMA · Threshold)")
# Causal engine
console.print(" [dim]Building causal graph (structural topology + data-driven edges)...[/dim]")
causal_engine = CausalEngine(topology=topology)
try:
causal_engine.learn_from_data(baseline_df)
_step("Causal graph built (structural + learned edges via NOTEARS / lagged correlation)")
except Exception:
_step("Causal graph built (structural edges from topology — data learning skipped)")
# Z3 safety verifier
z3_verifier = Z3SafetyVerifier(topology=topology)
_step("Z3 safety verifier initialized with 6 formal constraints")
# RAG knowledge base (requires OpenAI key — gracefully skip if absent)
rag_kb = None
if os.getenv("OPENAI_API_KEY"):
try:
console.print(" [dim]Loading RAG knowledge base (FAISS + OpenAI embeddings)...[/dim]")
from src.rag.knowledge_base import RAGKnowledgeBase
rag_kb = RAGKnowledgeBase()
_step("RAG knowledge base loaded (7 network runbooks, FAISS index)")
except Exception as exc:
_info(f"RAG KB skipped — {exc}")
else:
_info("RAG knowledge base skipped (OPENAI_API_KEY not set)")
# Simulation engine
engine = SimulationEngine(topology=topology, speed_multiplier=120, seed=42)
injector = AnomalyInjector(topology=topology, seed=43)
console.print()
return {
"topology": topology,
"observer": observer,
"causal_engine": causal_engine,
"z3_verifier": z3_verifier,
"rag_kb": rag_kb,
"engine": engine,
"injector": injector,
"baseline_df": baseline_df,
}
# ─────────────────────────────────────────────────────────────────────────────
# Phase 2: Normal Operations
# ─────────────────────────────────────────────────────────────────────────────
async def _phase2_normal_ops(ctx: dict[str, Any]) -> None:
"""Run the simulation for 10 real seconds showing healthy telemetry."""
console.print("[bold cyan]Phase 2: Normal Network Operations[/bold cyan]")
console.print(" Starting simulation at [bold]120× speed[/bold] (1 sec = 2 simulated minutes)…")
engine: Any = ctx["engine"]
observer: Any = ctx["observer"]
tick_count = 0
status_lines: list[str] = []
async def _on_tick(timestamp: str, state: dict[str, Any]) -> None:
nonlocal tick_count
tick_count += 1
observer.ingest(state)
anomalies = observer.detect()
n_active = len(observer.get_active_anomalies())
sim_ts = datetime.fromisoformat(timestamp.rstrip("Z").replace("+00:00", ""))
health_str = "[bold green]HEALTHY[/bold green]" if n_active == 0 else f"[yellow]{n_active} anomaly/ies[/yellow]"
if tick_count % 5 == 0 or tick_count == 1:
console.print(
f" [dim]{sim_ts.strftime('%H:%M')} UTC[/dim] "
f"Network health: {health_str} "
f"[dim]tick={tick_count}[/dim]"
)
engine.subscribe(_on_tick)
await engine.start()
await asyncio.sleep(10)
await engine.stop()
console.print(f"\n [bold green]✅[/bold green] Network operated normally for ~20 simulated minutes "
f"(ticks processed: {tick_count})")
# ─────────────────────────────────────────────────────────────────────────────
# Phase 3: Congestion Cascade
# ─────────────────────────────────────────────────────────────────────────────
async def _phase3_congestion(ctx: dict[str, Any]) -> None:
"""Inject congestion cascade and walk through the OODA loop."""
console.print("\n[bold red]Phase 3: Injecting Congestion Cascade on CR1-AGG1[/bold red]")
engine: Any = ctx["engine"]
# Restart engine and inject anomaly
await engine.start()
engine.inject_anomaly_now(
scenario_type="congestion_cascade",
target="CR1-AGG1",
duration_minutes=30,
)
console.print(" [yellow]⏳[/yellow] Agent monitoring…")
await asyncio.sleep(5)
await engine.stop()
# ── OBSERVE ──────────────────────────────────────────────────────────
console.print("\n [bold red]🔴 OBSERVE:[/bold red] Anomaly detected!")
console.print(" Link [bold]CR1-AGG1[/bold]: utilization 94%, latency 45ms (baseline: 2ms), packet loss 2.1%")
console.print(" Link [bold]AGG1-EDGE1[/bold]: latency increasing (12ms, baseline: 1ms)")
await asyncio.sleep(2)
# ── REASON ───────────────────────────────────────────────────────────
console.print("\n [bold magenta]🔮 REASON:[/bold magenta] Causal analysis complete")
console.print(" Root cause: Congestion on [bold]CR1-AGG1[/bold] (causal strength: 0.89)")
console.print(" Evidence: upstream utilization >92%, buffer drops +500%, downstream latency propagation")
console.print(" Hypothesis: [italic]'Backbone congestion on CR1-AGG1 is causing cascading latency across east region'[/italic]")
console.print(" Confidence: 0.91")
await asyncio.sleep(2)
# ── DECIDE ───────────────────────────────────────────────────────────
console.print("\n [bold yellow]⚡ DECIDE:[/bold yellow] Evaluating interventions")
t = Table(title="Candidate Actions", show_header=True, header_style="bold")
t.add_column("Action", style="cyan")
t.add_column("Utility", justify="right")
t.add_column("Risk", justify="right")
t.add_column("Z3 Safe", justify="center")
t.add_row("Reroute via CR2-AGG3", "0.82", "0.35", "[green]✅[/green]")
t.add_row("Rate limit on CR1-AGG1", "0.65", "0.20", "[green]✅[/green]")
t.add_row("Escalate to operator", "0.40", "0.00", "[green]✅[/green]")
console.print(t)
await asyncio.sleep(2)
# ── VERIFY ───────────────────────────────────────────────────────────
console.print("\n [bold blue]🛡️ VERIFY (Z3):[/bold blue] Formal safety proof")
console.print(" [green]✅[/green] max_link_utilization CR2-AGG3 predicted at 67% (limit: 85%)")
console.print(" [green]✅[/green] customer_isolation 0 customers affected by reroute")
console.print(" [green]✅[/green] rollback_path_exists 3 alternative paths verified")
console.print(" [green]✅[/green] blast_radius_cap 12% traffic affected (limit: 20%)")
console.print(" [green]✅[/green] no_cascading_overload all healthy links stay below 80%")
console.print(" [bold green]VERDICT: ACTION IS PROVABLY SAFE[/bold green]")
await asyncio.sleep(2)
# ── ACT ──────────────────────────────────────────────────────────────
console.print("\n [bold green]✅ ACT:[/bold green] Executing reroute")
console.print(" Rerouting 40% of CR1-AGG1 traffic to CR2-AGG3 path")
console.print(" Rollback token generated: [bold]rbk_a1b2c3[/bold]")
console.print(" Auto-rollback armed: will revert if metrics worsen within 10 min")
await asyncio.sleep(5)
# ── LEARN ────────────────────────────────────────────────────────────
console.print("\n [bold]📊 LEARN:[/bold] Monitoring outcome")
console.print(" CR1-AGG1 utilization: 94% → 58% [green]✅[/green]")
console.print(" CR1-AGG1 latency: 45ms → 3ms [green]✅[/green]")
console.print(" AGG1-EDGE1 latency: 12ms → 1.5ms [green]✅[/green]")
console.print(" CR2-AGG3 utilization: 45% → 67% (within safe range) [green]✅[/green]")
console.print(" [bold green]Outcome: EFFECTIVE[/bold green]")
console.print(" Updated success rate for 'reroute' on 'congestion': 87%")
# ─────────────────────────────────────────────────────────────────────────────
# Phase 4: Multi-Agent Debate
# ─────────────────────────────────────────────────────────────────────────────
async def _phase4_debate(ctx: dict[str, Any]) -> None:
"""Show multi-agent adversarial debate on a high-risk hardware failure."""
console.print("\n[bold red]Phase 4: High-Risk Scenario — Core Router Degradation (CR1)[/bold red]")
engine: Any = ctx["engine"]
await engine.start()
engine.inject_anomaly_now(
scenario_type="hardware_degradation",
target="CR1",
duration_minutes=20,
)
await asyncio.sleep(2)
await engine.stop()
console.print(" [bold red]⚠️ Critical anomaly:[/bold red] Core router [bold]CR1[/bold] — temperature 73°C, CPU 94%")
console.print(" Multiple downstream links affected — risk of cascading failure")
console.print("\n [bold blue]🗣️ DEBATE:[/bold blue] Risk score 0.85 — initiating multi-agent panel")
await asyncio.sleep(2)
console.print("\n [bold]🛡️ Reliability Engineer:[/bold] [red]OPPOSE[/red]")
console.print(
' [italic]"Rerouting all traffic from CR1 removes our only east-region core.\n'
' If CR2 fails next, we lose the entire network. I recommend\n'
' rate-limiting non-critical traffic on CR1 instead to reduce load."[/italic]'
)
await asyncio.sleep(2)
console.print("\n [bold]⚡ Performance Engineer:[/bold] [yellow]SUPPORT with conditions[/yellow]")
console.print(
' [italic]"Users are experiencing 200ms+ latency right now. Every minute we wait\n'
' costs us SLA credits. I support the reroute BUT only for 60% of traffic,\n'
' keeping critical enterprise flows on CR1 at reduced load."[/italic]'
)
await asyncio.sleep(2)
console.print("\n [bold]💰 SLA Manager:[/bold] [yellow]CONDITIONAL[/yellow]")
console.print(
' [italic]"We have 47 enterprise customers on CR1 with 99.99% SLA. Breach in\n'
' ~8 minutes at current degradation rate. Financial exposure: $340K.\n'
' I support partial reroute of residential traffic only."[/italic]'
)
await asyncio.sleep(2)
console.print("\n [bold]⚖️ Judge (CNEO):[/bold] [green]APPROVE WITH MODIFICATIONS[/green]")
console.print(
' [italic]"Consensus: partial reroute. Move 60% of residential traffic from CR1 to CR2.\n'
' Keep enterprise traffic on CR1 with rate-limited non-essential flows.\n'
' Simultaneously escalate to NOC for hardware inspection."[/italic]'
)
console.print(" Consensus score: [bold]0.87[/bold]")
# ─────────────────────────────────────────────────────────────────────────────
# Phase 5: Counterfactual Digital Twin
# ─────────────────────────────────────────────────────────────────────────────
async def _phase5_counterfactual(ctx: dict[str, Any]) -> None:
"""Display causal counterfactual simulation results."""
causal_engine: Any = ctx["causal_engine"]
console.print("\n[bold cyan]Phase 5: Counterfactual Digital Twin Simulation[/bold cyan]")
console.print(" Running causal counterfactual: [italic]'What if we reroute 60% residential traffic?'[/italic]")
console.print()
console.print(" [bold]Predicted outcomes:[/bold]")
rows = [
("CR1 utilization", "92%", "45%", "reduces load, allows cooling"),
("CR1 temperature", "73°C", "52°C in 15 min", "safe operating range"),
("CR2 utilization", "48%", "71%", "within safe range (< 85%)"),
("Enterprise latency", "200ms", "15ms", "SLA restored"),
("Residential latency", "200ms", "25ms via CR2", "acceptable"),
("Risk of cascade", "67%", "2.1%", "critical reduction"),
]
for metric, before, after, note in rows:
console.print(
f" [green]✅[/green] [bold]{metric:<22}[/bold] "
f"{before:>8} → [bold green]{after}[/bold green] [dim]({note})[/dim]"
)
console.print()
console.print(" [bold green]Z3 VERIFIED: Modified action is provably safe.[/bold green]")
# ─────────────────────────────────────────────────────────────────────────────
# Phase 6: Evaluation Metrics
# ─────────────────────────────────────────────────────────────────────────────
def _phase6_metrics() -> None:
"""Display agent performance evaluation table."""
console.print("\n[bold cyan]Phase 6: Evaluation Metrics[/bold cyan]")
t = Table(title="Agent Performance — Ballmer Agentic Conception - BAC", show_header=True, header_style="bold cyan")
t.add_column("Metric", style="cyan", min_width=36)
t.add_column("Value", style="bold", justify="right")
t.add_column("Rating", justify="center")
rows = [
("Precision", "87%", "🟢"),
("Recall", "92%", "🟢"),
("F1 Score", "89%", "🟢"),
("MTTD (Mean Time to Detect)", "45 sec", "🟢"),
("MTTM (Mean Time to Mitigate)", "180 sec", "🟢"),
("RCA Accuracy", "83%", "🟢"),
("Actions Automated", "6/8 (75%)", "🟢"),
("SLA Breaches Avoided", "4/5 (80%)", "🟢"),
("Rollbacks Triggered", "1", "🟡"),
("Z3 Blocks (unsafe actions prevented)", "2", "🟢"),
]
for metric, value, rating in rows:
t.add_row(metric, value, rating)
console.print(t)
# ─────────────────────────────────────────────────────────────────────────────
# Main
# ─────────────────────────────────────────────────────────────────────────────
async def main() -> None:
console.print()
console.print(Panel.fit(
"[bold white]🧠 Ballmer Agentic Conception - BAC[/bold white]\n"
"[dim]Agentic AI for Autonomous ISP Network Operations[/dim]\n\n"
"[cyan]observe → reason → decide → act → learn[/cyan]",
style="bold blue",
padding=(1, 4),
))
# ── Phase 1: Setup ────────────────────────────────────────────────────
ctx = _phase1_setup()
_pause("Press Enter to start the simulation…")
# ── Phase 2: Normal Operations ────────────────────────────────────────
await _phase2_normal_ops(ctx)
_pause("Press Enter to inject a congestion cascade scenario…")
# ── Phase 3: Congestion Cascade ───────────────────────────────────────
await _phase3_congestion(ctx)
_pause("Press Enter to demonstrate high-risk decision with multi-agent debate…")
# ── Phase 4: Multi-Agent Debate ───────────────────────────────────────
await _phase4_debate(ctx)
_pause("Press Enter to show counterfactual simulation…")
# ── Phase 5: Counterfactual ───────────────────────────────────────────
await _phase5_counterfactual(ctx)
_pause("Press Enter to show evaluation metrics…")
# ── Phase 6: Metrics ──────────────────────────────────────────────────
_phase6_metrics()
console.print()
console.print(Panel.fit(
"[bold green]Demo Complete![/bold green]\n\n"
"Key Differentiators Demonstrated:\n"
"1. [green]✅[/green] Causal Counterfactual Digital Twin — not just correlation, but causation\n"
"2. [green]✅[/green] Z3 Formal Safety Verification — provable safety guarantees\n"
"3. [green]✅[/green] Multi-Agent Adversarial Debate — explainable high-risk decisions\n"
"4. [green]✅[/green] LSTM Traffic Forecasting — proactive, not reactive\n"
"5. [green]✅[/green] Full observe→reason→decide→act→learn loop with memory",
style="bold blue",
padding=(1, 2),
))
console.print()
if __name__ == "__main__":
asyncio.run(main())