Spaces:
Paused
Paused
Upload server/demo_ui.py with huggingface_hub
Browse files- server/demo_ui.py +724 -595
server/demo_ui.py
CHANGED
|
@@ -1,595 +1,724 @@
|
|
| 1 |
-
"""
|
| 2 |
-
Hackathon-judge demo UI for the live HF Space.
|
| 3 |
-
|
| 4 |
-
What this gives the judge when they click the Space link:
|
| 5 |
-
|
| 6 |
-
- One-screen Gradio panel.
|
| 7 |
-
- Pick a scenario family from the 5 elite ones (basic / RAG / executive
|
| 8 |
-
alignment / silo-breaker / stealth-adaptive).
|
| 9 |
-
- Click "Run episode" -> we play the heuristic policy for up to 30 steps
|
| 10 |
-
in the env, then re-run the same scenario seed with the *trained* LLM
|
| 11 |
-
policy (if available on the Hub) and show side-by-side results.
|
| 12 |
-
- Outputs:
|
| 13 |
-
- reward delta (trained vs heuristic) with a clear winner indicator
|
| 14 |
-
- the agent's per-step action stream, with reasoning
|
| 15 |
-
- a chart of per-step reward
|
| 16 |
-
- a status badge for the trained adapter ("ready" / "training in progress")
|
| 17 |
-
|
| 18 |
-
The point is for a non-technical reviewer to see
|
| 19 |
-
"agent observed X -> agent did Y -> reward Z" on screen in 10 seconds.
|
| 20 |
-
|
| 21 |
-
Mounted at ``/demo`` on the FastAPI app via ``gr.mount_gradio_app``.
|
| 22 |
-
"""
|
| 23 |
-
|
| 24 |
-
from __future__ import annotations
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
from
|
| 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 |
-
if
|
| 165 |
-
|
| 166 |
-
|
| 167 |
-
if
|
| 168 |
-
|
| 169 |
-
|
| 170 |
-
|
| 171 |
-
|
| 172 |
-
|
| 173 |
-
"
|
| 174 |
-
"
|
| 175 |
-
"
|
| 176 |
-
"
|
| 177 |
-
"
|
| 178 |
-
"
|
| 179 |
-
|
| 180 |
-
|
| 181 |
-
|
| 182 |
-
|
| 183 |
-
|
| 184 |
-
|
| 185 |
-
|
| 186 |
-
|
| 187 |
-
if
|
| 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 |
-
f["
|
| 300 |
-
f["
|
| 301 |
-
f["
|
| 302 |
-
f["
|
| 303 |
-
f["
|
| 304 |
-
f["
|
| 305 |
-
f["
|
| 306 |
-
f["
|
| 307 |
-
f["
|
| 308 |
-
f["
|
| 309 |
-
f["
|
| 310 |
-
f["
|
| 311 |
-
f["
|
| 312 |
-
f["
|
| 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 |
-
|
| 424 |
-
|
| 425 |
-
|
| 426 |
-
|
| 427 |
-
|
| 428 |
-
|
| 429 |
-
|
| 430 |
-
|
| 431 |
-
|
| 432 |
-
|
| 433 |
-
|
| 434 |
-
|
| 435 |
-
|
| 436 |
-
|
| 437 |
-
|
| 438 |
-
scenario,
|
| 439 |
-
)
|
| 440 |
-
|
| 441 |
-
|
| 442 |
-
|
| 443 |
-
|
| 444 |
-
|
| 445 |
-
|
| 446 |
-
|
| 447 |
-
chart_data
|
| 448 |
-
|
| 449 |
-
|
| 450 |
-
|
| 451 |
-
|
| 452 |
-
[
|
| 453 |
-
|
| 454 |
-
|
| 455 |
-
|
| 456 |
-
|
| 457 |
-
|
| 458 |
-
|
| 459 |
-
|
| 460 |
-
|
| 461 |
-
|
| 462 |
-
|
| 463 |
-
|
| 464 |
-
|
| 465 |
-
|
| 466 |
-
|
| 467 |
-
|
| 468 |
-
|
| 469 |
-
|
| 470 |
-
|
|
| 471 |
-
|
| 472 |
-
{
|
| 473 |
-
|
| 474 |
-
|
| 475 |
-
|
| 476 |
-
|
| 477 |
-
|
| 478 |
-
|
| 479 |
-
|
| 480 |
-
|
| 481 |
-
|
| 482 |
-
|
| 483 |
-
|
| 484 |
-
|
| 485 |
-
|
| 486 |
-
|
| 487 |
-
|
| 488 |
-
|
| 489 |
-
|
| 490 |
-
|
| 491 |
-
|
| 492 |
-
|
| 493 |
-
|
| 494 |
-
|
| 495 |
-
|
| 496 |
-
|
| 497 |
-
|
| 498 |
-
|
| 499 |
-
|
| 500 |
-
|
| 501 |
-
|
| 502 |
-
|
| 503 |
-
|
| 504 |
-
|
| 505 |
-
|
| 506 |
-
|
| 507 |
-
|
| 508 |
-
|
| 509 |
-
**
|
| 510 |
-
|
| 511 |
-
|
| 512 |
-
|
| 513 |
-
|
| 514 |
-
|
| 515 |
-
|
| 516 |
-
|
| 517 |
-
|
| 518 |
-
|
| 519 |
-
|
| 520 |
-
|
| 521 |
-
|
| 522 |
-
|
| 523 |
-
|
| 524 |
-
|
| 525 |
-
|
| 526 |
-
|
| 527 |
-
|
| 528 |
-
|
| 529 |
-
|
| 530 |
-
|
| 531 |
-
|
| 532 |
-
|
| 533 |
-
|
| 534 |
-
|
| 535 |
-
|
| 536 |
-
|
| 537 |
-
|
| 538 |
-
|
| 539 |
-
|
| 540 |
-
|
| 541 |
-
|
| 542 |
-
|
| 543 |
-
|
| 544 |
-
|
| 545 |
-
|
| 546 |
-
|
| 547 |
-
|
| 548 |
-
|
| 549 |
-
|
| 550 |
-
|
| 551 |
-
|
| 552 |
-
|
| 553 |
-
|
| 554 |
-
|
| 555 |
-
|
| 556 |
-
|
| 557 |
-
|
| 558 |
-
|
| 559 |
-
|
| 560 |
-
|
| 561 |
-
|
| 562 |
-
|
| 563 |
-
|
| 564 |
-
|
| 565 |
-
|
| 566 |
-
|
| 567 |
-
|
| 568 |
-
|
| 569 |
-
|
| 570 |
-
|
| 571 |
-
|
| 572 |
-
|
| 573 |
-
|
| 574 |
-
|
| 575 |
-
|
| 576 |
-
|
| 577 |
-
|
| 578 |
-
|
| 579 |
-
|
| 580 |
-
|
| 581 |
-
|
| 582 |
-
|
| 583 |
-
|
| 584 |
-
|
| 585 |
-
|
| 586 |
-
|
| 587 |
-
|
| 588 |
-
|
| 589 |
-
|
| 590 |
-
|
| 591 |
-
|
| 592 |
-
|
| 593 |
-
|
| 594 |
-
|
| 595 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""
|
| 2 |
+
Hackathon-judge demo UI for the live HF Space.
|
| 3 |
+
|
| 4 |
+
What this gives the judge when they click the Space link:
|
| 5 |
+
|
| 6 |
+
- One-screen Gradio panel (episode demo + **War Room** LLM debate accordion).
|
| 7 |
+
- Pick a scenario family from the 5 elite ones (basic / RAG / executive
|
| 8 |
+
alignment / silo-breaker / stealth-adaptive).
|
| 9 |
+
- Click "Run episode" -> we play the heuristic policy for up to 30 steps
|
| 10 |
+
in the env, then re-run the same scenario seed with the *trained* LLM
|
| 11 |
+
policy (if available on the Hub) and show side-by-side results.
|
| 12 |
+
- Outputs:
|
| 13 |
+
- reward delta (trained vs heuristic) with a clear winner indicator
|
| 14 |
+
- the agent's per-step action stream, with reasoning
|
| 15 |
+
- a chart of per-step reward
|
| 16 |
+
- a status badge for the trained adapter ("ready" / "training in progress")
|
| 17 |
+
|
| 18 |
+
The point is for a non-technical reviewer to see
|
| 19 |
+
"agent observed X -> agent did Y -> reward Z" on screen in 10 seconds.
|
| 20 |
+
|
| 21 |
+
Mounted at ``/demo`` on the FastAPI app via ``gr.mount_gradio_app``.
|
| 22 |
+
"""
|
| 23 |
+
|
| 24 |
+
from __future__ import annotations
|
| 25 |
+
|
| 26 |
+
import asyncio
|
| 27 |
+
from collections import Counter
|
| 28 |
+
from typing import Any
|
| 29 |
+
|
| 30 |
+
import gradio as gr
|
| 31 |
+
|
| 32 |
+
from server.war_room_debate import run_war_room_debate
|
| 33 |
+
|
| 34 |
+
from immunoorg.agents.defender import (
|
| 35 |
+
format_observation_for_llm,
|
| 36 |
+
get_defender_prompt,
|
| 37 |
+
)
|
| 38 |
+
from immunoorg.environment import ImmunoOrgEnvironment
|
| 39 |
+
from immunoorg.models import (
|
| 40 |
+
ActionType,
|
| 41 |
+
DiagnosticAction,
|
| 42 |
+
ImmunoAction,
|
| 43 |
+
PipelineGate,
|
| 44 |
+
StrategicAction,
|
| 45 |
+
TacticalAction,
|
| 46 |
+
)
|
| 47 |
+
from training.dataset_generator import DatasetConfig, DatasetGenerator
|
| 48 |
+
from training.scenario_hooks import (
|
| 49 |
+
apply_scenario_hooks,
|
| 50 |
+
attach_hooks,
|
| 51 |
+
training_step_penalty,
|
| 52 |
+
)
|
| 53 |
+
|
| 54 |
+
|
| 55 |
+
# βββ Scenario catalogue βββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 56 |
+
|
| 57 |
+
|
| 58 |
+
_SCENARIO_LABEL = {
|
| 59 |
+
"basic_containment": "1. Basic Containment (warm-up)",
|
| 60 |
+
"rag_grounding": "2. RAG-Grounding (use CVE intel, not blunt isolate)",
|
| 61 |
+
"executive_alignment": "3. Executive Alignment (uptime directive overrides instinct)",
|
| 62 |
+
"silo_breaker": "4. Silo-Breaker (org friction blocks tactical actions)",
|
| 63 |
+
"stealth_adaptive": "5. Stealth & Adaptive (multi-step investigation)",
|
| 64 |
+
}
|
| 65 |
+
_LABEL_TO_FAMILY = {v: k for k, v in _SCENARIO_LABEL.items()}
|
| 66 |
+
|
| 67 |
+
|
| 68 |
+
_SCENARIO_CACHE: dict[str, dict[str, Any]] = {}
|
| 69 |
+
|
| 70 |
+
|
| 71 |
+
def _scenario_for(family: str) -> dict[str, Any]:
|
| 72 |
+
"""Generate one balanced elite scenario per family, cached."""
|
| 73 |
+
if family in _SCENARIO_CACHE:
|
| 74 |
+
return _SCENARIO_CACHE[family]
|
| 75 |
+
gen = DatasetGenerator(DatasetConfig(
|
| 76 |
+
dataset_type="elite",
|
| 77 |
+
output_dir="/tmp/_demo_scenarios",
|
| 78 |
+
verbose=False,
|
| 79 |
+
compress_output=False,
|
| 80 |
+
))
|
| 81 |
+
scenarios = gen.generate_elite_scenario_mix_dataset(total=5)
|
| 82 |
+
for sc in scenarios:
|
| 83 |
+
if sc["family"] == family and family not in _SCENARIO_CACHE:
|
| 84 |
+
_SCENARIO_CACHE[family] = sc
|
| 85 |
+
return _SCENARIO_CACHE[family]
|
| 86 |
+
|
| 87 |
+
|
| 88 |
+
# βββ Heuristic policy (mirrors scripts/generate_training_evidence.py) ββββββ
|
| 89 |
+
|
| 90 |
+
|
| 91 |
+
def _heuristic_action(env, obs):
|
| 92 |
+
phase = obs.current_phase.value
|
| 93 |
+
nodes = obs.visible_nodes
|
| 94 |
+
compromised = [n for n in nodes if n.compromised and not n.isolated]
|
| 95 |
+
hooks = getattr(env, "_active_scenario_hooks", {}) or {}
|
| 96 |
+
|
| 97 |
+
if hooks.get("inject_rag_best_mitigation") and phase in ("detection", "containment") and compromised:
|
| 98 |
+
return ImmunoAction(action_type=ActionType.TACTICAL,
|
| 99 |
+
tactical_action=TacticalAction.SNAPSHOT_FORENSICS,
|
| 100 |
+
target=compromised[0].id,
|
| 101 |
+
reasoning="RAG: forensic snapshot before patching the rootkit.")
|
| 102 |
+
if hooks.get("board_uptime_no_isolate") and phase == "containment":
|
| 103 |
+
target = compromised[0].id if compromised else (nodes[0].id if nodes else "")
|
| 104 |
+
return ImmunoAction(action_type=ActionType.TACTICAL,
|
| 105 |
+
tactical_action=TacticalAction.DEPLOY_PATCH,
|
| 106 |
+
target=target,
|
| 107 |
+
reasoning="Board directive: patch instead of isolating.")
|
| 108 |
+
if hooks.get("force_denials_on_isolate") and phase in ("containment", "rca", "refactor"):
|
| 109 |
+
return ImmunoAction(action_type=ActionType.STRATEGIC,
|
| 110 |
+
strategic_action=StrategicAction.ESTABLISH_DEVSECOPS,
|
| 111 |
+
target="dept-security", secondary_target="dept-engineering",
|
| 112 |
+
reasoning="Approver keeps denying; restructure the org.")
|
| 113 |
+
if hooks.get("stealthy_initial_attack") and phase == "detection":
|
| 114 |
+
return ImmunoAction(action_type=ActionType.DIAGNOSTIC,
|
| 115 |
+
diagnostic_action=DiagnosticAction.VULNERABILITY_SCAN,
|
| 116 |
+
reasoning="Stealth attack: deeper scan first.")
|
| 117 |
+
|
| 118 |
+
if phase == "detection":
|
| 119 |
+
target = compromised[0].id if compromised else (nodes[0].id if nodes else "")
|
| 120 |
+
return ImmunoAction(action_type=ActionType.TACTICAL,
|
| 121 |
+
tactical_action=TacticalAction.SCAN_LOGS,
|
| 122 |
+
target=target, reasoning="Detection: scan for indicators.")
|
| 123 |
+
if phase == "containment":
|
| 124 |
+
if compromised:
|
| 125 |
+
return ImmunoAction(action_type=ActionType.TACTICAL,
|
| 126 |
+
tactical_action=TacticalAction.ISOLATE_NODE,
|
| 127 |
+
target=compromised[0].id, reasoning="Isolate compromised node.")
|
| 128 |
+
return ImmunoAction(action_type=ActionType.DIAGNOSTIC,
|
| 129 |
+
diagnostic_action=DiagnosticAction.TIMELINE_RECONSTRUCT,
|
| 130 |
+
reasoning="Reconstruct timeline.")
|
| 131 |
+
if phase == "rca":
|
| 132 |
+
return ImmunoAction(action_type=ActionType.DIAGNOSTIC,
|
| 133 |
+
diagnostic_action=DiagnosticAction.IDENTIFY_SILO,
|
| 134 |
+
reasoning="Find the silo behind the failure.")
|
| 135 |
+
if phase == "refactor":
|
| 136 |
+
return ImmunoAction(action_type=ActionType.STRATEGIC,
|
| 137 |
+
strategic_action=StrategicAction.REDUCE_BUREAUCRACY,
|
| 138 |
+
target="dept-management", reasoning="Reduce approval latency.")
|
| 139 |
+
return ImmunoAction(action_type=ActionType.DIAGNOSTIC,
|
| 140 |
+
diagnostic_action=DiagnosticAction.MEASURE_ORG_LATENCY,
|
| 141 |
+
reasoning="Validate org improvements.")
|
| 142 |
+
|
| 143 |
+
|
| 144 |
+
# βββ Episode runners βββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 145 |
+
|
| 146 |
+
|
| 147 |
+
def _mesh_gate_label(env: ImmunoOrgEnvironment) -> str:
|
| 148 |
+
gate = getattr(env, "_last_pipeline_gate", None)
|
| 149 |
+
if gate is None:
|
| 150 |
+
return "β"
|
| 151 |
+
if isinstance(gate, PipelineGate):
|
| 152 |
+
return gate.value
|
| 153 |
+
return str(gate)
|
| 154 |
+
|
| 155 |
+
|
| 156 |
+
def _telemetry_row(env: ImmunoOrgEnvironment, obs) -> dict[str, str | int | float]:
|
| 157 |
+
"""Surface War Room, 4-gate mesh, migration/honeypots, MITRE-ish vector."""
|
| 158 |
+
mig = {}
|
| 159 |
+
try:
|
| 160 |
+
mig = env.migration_engine.get_progress() or {}
|
| 161 |
+
except Exception:
|
| 162 |
+
pass
|
| 163 |
+
honeys = mig.get("active_honeypots") or []
|
| 164 |
+
if not isinstance(honeys, list):
|
| 165 |
+
honeys = []
|
| 166 |
+
att = "β"
|
| 167 |
+
if obs.detected_attacks:
|
| 168 |
+
att = obs.detected_attacks[0].vector.value
|
| 169 |
+
d0 = obs.directives[0] if obs.directives else "β"
|
| 170 |
+
if isinstance(d0, str) and len(d0) > 36:
|
| 171 |
+
d0 = d0[:33] + "β¦"
|
| 172 |
+
return {
|
| 173 |
+
"mesh_ok": round(float(getattr(env, "_last_pipeline_integrity", 1.0) or 1.0), 2),
|
| 174 |
+
"gate": _mesh_gate_label(env)[:28],
|
| 175 |
+
"war_room": int(getattr(env, "_last_war_room_turns", 0) or 0),
|
| 176 |
+
"honeypots": len(honeys),
|
| 177 |
+
"migr": str(mig.get("current_phase", "β"))[:14] if mig.get("active") else "off",
|
| 178 |
+
"migr_pct": int(100 * float(mig.get("progress_pct", 0) or 0)) if mig.get("active") else 0,
|
| 179 |
+
"honeytokens": int(mig.get("honeytoken_activations", 0) or 0) if mig.get("active") else 0,
|
| 180 |
+
"attack_vec": str(att)[:22],
|
| 181 |
+
"directive": str(d0),
|
| 182 |
+
}
|
| 183 |
+
|
| 184 |
+
|
| 185 |
+
def _pick_demo_action(env: ImmunoOrgEnvironment, obs, policy_fn, step_index: int, showcase_migration: bool):
|
| 186 |
+
"""Optional injected steps so judges see migration + honeypots (not honeycomb UI β decoy nodes)."""
|
| 187 |
+
if not showcase_migration:
|
| 188 |
+
return policy_fn(env, obs)
|
| 189 |
+
phase = obs.current_phase.value
|
| 190 |
+
if env.migration_engine.state is None and step_index == 1 and phase in (
|
| 191 |
+
"detection", "containment", "rca", "refactor",
|
| 192 |
+
):
|
| 193 |
+
return ImmunoAction(
|
| 194 |
+
action_type=ActionType.TACTICAL,
|
| 195 |
+
tactical_action=TacticalAction.START_MIGRATION,
|
| 196 |
+
target="core-backbone",
|
| 197 |
+
reasoning="[Demo] Start 50-step polymorphic migration (decoys, honeypots, honeytokens).",
|
| 198 |
+
parameters={"compliance": "SOC2"},
|
| 199 |
+
)
|
| 200 |
+
if env.migration_engine.state and step_index >= 3 and step_index % 4 == 0:
|
| 201 |
+
return ImmunoAction(
|
| 202 |
+
action_type=ActionType.TACTICAL,
|
| 203 |
+
tactical_action=TacticalAction.DEPLOY_HONEYPOT,
|
| 204 |
+
target="edge-pool",
|
| 205 |
+
reasoning="[Demo] Deploy honeypot node on the migration track.",
|
| 206 |
+
)
|
| 207 |
+
return policy_fn(env, obs)
|
| 208 |
+
|
| 209 |
+
|
| 210 |
+
def _run_episode(scenario, policy_fn, max_steps=30, *, showcase_migration: bool = False):
|
| 211 |
+
"""Roll out a policy on a scenario, return (frames, total_reward).
|
| 212 |
+
|
| 213 |
+
`policy_fn(env, obs)` -> ImmunoAction
|
| 214 |
+
"""
|
| 215 |
+
env = ImmunoOrgEnvironment(
|
| 216 |
+
difficulty=int(scenario["difficulty"]),
|
| 217 |
+
seed=int(scenario["seed"]),
|
| 218 |
+
)
|
| 219 |
+
hooks = scenario.get("hooks") or {}
|
| 220 |
+
attach_hooks(env, hooks)
|
| 221 |
+
obs = env.reset()
|
| 222 |
+
apply_scenario_hooks(env, hooks)
|
| 223 |
+
|
| 224 |
+
frames = []
|
| 225 |
+
total = 0.0
|
| 226 |
+
for step in range(min(max_steps, env.state.max_steps)):
|
| 227 |
+
action = _pick_demo_action(env, obs, policy_fn, step, showcase_migration)
|
| 228 |
+
obs, reward, done = env.step(action)
|
| 229 |
+
shaped = float(reward) + float(training_step_penalty(env, action))
|
| 230 |
+
total += shaped
|
| 231 |
+
tel = _telemetry_row(env, obs)
|
| 232 |
+
frames.append({
|
| 233 |
+
"step": step + 1,
|
| 234 |
+
"phase": obs.current_phase.value,
|
| 235 |
+
"action_type": action.action_type.value,
|
| 236 |
+
"action": (
|
| 237 |
+
(action.tactical_action and action.tactical_action.value)
|
| 238 |
+
or (action.strategic_action and action.strategic_action.value)
|
| 239 |
+
or (action.diagnostic_action and action.diagnostic_action.value)
|
| 240 |
+
or "?"
|
| 241 |
+
),
|
| 242 |
+
"target": action.target or "-",
|
| 243 |
+
"reasoning": action.reasoning,
|
| 244 |
+
"reward": round(shaped, 3),
|
| 245 |
+
"threats_left": len(env.attacks.get_active_attacks()),
|
| 246 |
+
**tel,
|
| 247 |
+
})
|
| 248 |
+
if done:
|
| 249 |
+
break
|
| 250 |
+
return frames, total
|
| 251 |
+
|
| 252 |
+
|
| 253 |
+
def _trained_policy(env, obs):
|
| 254 |
+
"""Trained-LLM policy. Falls back to heuristic if the adapter isn't on
|
| 255 |
+
the Hub yet (e.g. the HPC run hasn't pushed)."""
|
| 256 |
+
from immunoorg.trained_agent import TrainedAgentUnavailable, TrainedDefender
|
| 257 |
+
|
| 258 |
+
try:
|
| 259 |
+
td = TrainedDefender.get()
|
| 260 |
+
obs_text = format_observation_for_llm(obs.model_dump())
|
| 261 |
+
sys_prompt = get_defender_prompt()
|
| 262 |
+
data = td.predict_action(obs_text, sys_prompt)
|
| 263 |
+
except TrainedAgentUnavailable:
|
| 264 |
+
return _heuristic_action(env, obs)
|
| 265 |
+
|
| 266 |
+
try:
|
| 267 |
+
atype = ActionType(data.get("action_type", "diagnostic"))
|
| 268 |
+
except Exception:
|
| 269 |
+
atype = ActionType.DIAGNOSTIC
|
| 270 |
+
|
| 271 |
+
kwargs = dict(
|
| 272 |
+
action_type=atype,
|
| 273 |
+
target=data.get("target") or "",
|
| 274 |
+
secondary_target=data.get("secondary_target"),
|
| 275 |
+
parameters=data.get("parameters") or {},
|
| 276 |
+
reasoning=data.get("reasoning") or "",
|
| 277 |
+
)
|
| 278 |
+
try:
|
| 279 |
+
if atype == ActionType.TACTICAL and data.get("tactical_action"):
|
| 280 |
+
kwargs["tactical_action"] = TacticalAction(data["tactical_action"])
|
| 281 |
+
elif atype == ActionType.STRATEGIC and data.get("strategic_action"):
|
| 282 |
+
kwargs["strategic_action"] = StrategicAction(data["strategic_action"])
|
| 283 |
+
elif atype == ActionType.DIAGNOSTIC and data.get("diagnostic_action"):
|
| 284 |
+
kwargs["diagnostic_action"] = DiagnosticAction(data["diagnostic_action"])
|
| 285 |
+
except Exception:
|
| 286 |
+
kwargs["diagnostic_action"] = DiagnosticAction.QUERY_BELIEF_MAP
|
| 287 |
+
kwargs["action_type"] = ActionType.DIAGNOSTIC
|
| 288 |
+
|
| 289 |
+
return ImmunoAction(**kwargs)
|
| 290 |
+
|
| 291 |
+
|
| 292 |
+
# βββ Gradio handler ββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 293 |
+
|
| 294 |
+
|
| 295 |
+
def _frames_to_table(frames):
|
| 296 |
+
out = []
|
| 297 |
+
for f in frames:
|
| 298 |
+
out.append([
|
| 299 |
+
f["step"],
|
| 300 |
+
f["phase"],
|
| 301 |
+
f["action_type"],
|
| 302 |
+
f["action"],
|
| 303 |
+
f["target"],
|
| 304 |
+
f["reward"],
|
| 305 |
+
f["threats_left"],
|
| 306 |
+
f["mesh_ok"],
|
| 307 |
+
f["gate"],
|
| 308 |
+
f["war_room"],
|
| 309 |
+
f["honeypots"],
|
| 310 |
+
f["migr"],
|
| 311 |
+
f["migr_pct"],
|
| 312 |
+
f["honeytokens"],
|
| 313 |
+
f["attack_vec"],
|
| 314 |
+
f["directive"],
|
| 315 |
+
f["reasoning"][:72],
|
| 316 |
+
])
|
| 317 |
+
return out
|
| 318 |
+
|
| 319 |
+
|
| 320 |
+
def _feature_dashboard_figure(heur_frames: list, trained_frames: list):
|
| 321 |
+
"""Plotly: pipeline integrity, honeypots, honeytokens, War Room turns."""
|
| 322 |
+
import plotly.graph_objects as go
|
| 323 |
+
from plotly.subplots import make_subplots
|
| 324 |
+
|
| 325 |
+
fig = make_subplots(
|
| 326 |
+
rows=4,
|
| 327 |
+
cols=1,
|
| 328 |
+
shared_xaxes=True,
|
| 329 |
+
vertical_spacing=0.05,
|
| 330 |
+
subplot_titles=(
|
| 331 |
+
"4-gate DevSecOps mesh β pipeline integrity (1.0 = clean)",
|
| 332 |
+
"Honeypots deployed (moving-target / decoy layer)",
|
| 333 |
+
"Honeytoken activations (trap callbacks)",
|
| 334 |
+
"War Room β consensus rounds (CISO / DevOps / Architect)",
|
| 335 |
+
),
|
| 336 |
+
)
|
| 337 |
+
specs = [
|
| 338 |
+
("mesh_ok", "Heuristic", "#ff7f0e", "Trained", "#1f77b4"),
|
| 339 |
+
("honeypots", "Heuristic honeypots", "#ff7f0e", "Trained honeypots", "#1f77b4"),
|
| 340 |
+
("honeytokens", "Heuristic honeytokens", "#ff7f0e", "Trained honeytokens", "#1f77b4"),
|
| 341 |
+
("war_room", "Heuristic WR turns", "#ff7f0e", "Trained WR turns", "#1f77b4"),
|
| 342 |
+
]
|
| 343 |
+
for row, (key, n1, c1, n2, c2) in enumerate(specs, start=1):
|
| 344 |
+
xh = [f["step"] for f in heur_frames]
|
| 345 |
+
yh = [f[key] for f in heur_frames]
|
| 346 |
+
xt = [f["step"] for f in trained_frames]
|
| 347 |
+
yt = [f[key] for f in trained_frames]
|
| 348 |
+
fig.add_trace(
|
| 349 |
+
go.Scatter(
|
| 350 |
+
x=xh, y=yh, mode="lines+markers", name=n1,
|
| 351 |
+
line=dict(color=c1, shape="hv"), legendgroup="h", showlegend=(row == 1),
|
| 352 |
+
),
|
| 353 |
+
row=row, col=1,
|
| 354 |
+
)
|
| 355 |
+
fig.add_trace(
|
| 356 |
+
go.Scatter(
|
| 357 |
+
x=xt, y=yt, mode="lines+markers", name=n2,
|
| 358 |
+
line=dict(color=c2, shape="hv", dash="dash"), legendgroup="t", showlegend=(row == 1),
|
| 359 |
+
),
|
| 360 |
+
row=row, col=1,
|
| 361 |
+
)
|
| 362 |
+
fig.update_layout(
|
| 363 |
+
height=780,
|
| 364 |
+
margin=dict(t=36, b=24, l=48, r=24),
|
| 365 |
+
legend=dict(orientation="h", yanchor="bottom", y=1.02, x=0),
|
| 366 |
+
font=dict(size=11),
|
| 367 |
+
)
|
| 368 |
+
fig.update_xaxes(title_text="step", row=4, col=1)
|
| 369 |
+
return fig
|
| 370 |
+
|
| 371 |
+
|
| 372 |
+
def _mesh_gate_bar_figure(heur_frames: list, trained_frames: list):
|
| 373 |
+
"""Grouped bar: how often each mesh gate fired (per episode)."""
|
| 374 |
+
import plotly.graph_objects as go
|
| 375 |
+
|
| 376 |
+
def counts(frames: list) -> dict[str, int]:
|
| 377 |
+
c: Counter[str] = Counter()
|
| 378 |
+
for f in frames:
|
| 379 |
+
g = str(f.get("gate") or "").strip()
|
| 380 |
+
if g and g != "β":
|
| 381 |
+
# Short labels for x axis
|
| 382 |
+
short = g.replace("gate", "").replace("_", " ")[:22]
|
| 383 |
+
c[short] += 1
|
| 384 |
+
return dict(c)
|
| 385 |
+
|
| 386 |
+
ch, ct = counts(heur_frames), counts(trained_frames)
|
| 387 |
+
if not ch and not ct:
|
| 388 |
+
fig = go.Figure()
|
| 389 |
+
fig.add_annotation(
|
| 390 |
+
text="No mesh gate catches this episode (pipeline stayed clean).",
|
| 391 |
+
xref="paper", yref="paper", x=0.5, y=0.5, showarrow=False,
|
| 392 |
+
)
|
| 393 |
+
fig.update_layout(height=280, margin=dict(t=40, b=20))
|
| 394 |
+
return fig
|
| 395 |
+
|
| 396 |
+
keys = sorted(set(ch) | set(ct), key=lambda k: (ch.get(k, 0) + ct.get(k, 0)), reverse=True)[:12]
|
| 397 |
+
fig = go.Figure(
|
| 398 |
+
data=[
|
| 399 |
+
go.Bar(name="Heuristic", x=keys, y=[ch.get(k, 0) for k in keys], marker_color="#ff7f0e"),
|
| 400 |
+
go.Bar(name="Trained", x=keys, y=[ct.get(k, 0) for k in keys], marker_color="#1f77b4"),
|
| 401 |
+
]
|
| 402 |
+
)
|
| 403 |
+
fig.update_layout(
|
| 404 |
+
title="Mesh gate catches (count of steps where each gate flagged)",
|
| 405 |
+
barmode="group",
|
| 406 |
+
height=320,
|
| 407 |
+
margin=dict(t=50, b=80, l=48, r=24),
|
| 408 |
+
xaxis_tickangle=-28,
|
| 409 |
+
font=dict(size=11),
|
| 410 |
+
)
|
| 411 |
+
return fig
|
| 412 |
+
|
| 413 |
+
|
| 414 |
+
def _trained_status_text() -> str:
|
| 415 |
+
try:
|
| 416 |
+
from immunoorg.trained_agent import TrainedDefender
|
| 417 |
+
|
| 418 |
+
s = TrainedDefender.get().status()
|
| 419 |
+
except Exception as e:
|
| 420 |
+
return f"β οΈ trained adapter status check failed: {e}"
|
| 421 |
+
|
| 422 |
+
if s.get("repo_exists"):
|
| 423 |
+
if s.get("loaded"):
|
| 424 |
+
return f"β
Trained adapter LOADED from `{s['repo_id']}` (sha {s.get('sha','?')[:7]})"
|
| 425 |
+
return (f"π’ Trained adapter found on the Hub at `{s['repo_id']}` β "
|
| 426 |
+
f"will load on first 'Run trained agent' click.")
|
| 427 |
+
return (f"β³ Trained adapter not on the Hub yet at `{s['repo_id']}`. "
|
| 428 |
+
f"HPC pipeline run-in-progress β heuristic policy will be used "
|
| 429 |
+
f"until the LoRA is pushed.")
|
| 430 |
+
|
| 431 |
+
|
| 432 |
+
def run_demo(scenario_label, max_steps, showcase_migration):
|
| 433 |
+
family = _LABEL_TO_FAMILY[scenario_label]
|
| 434 |
+
scenario = _scenario_for(family)
|
| 435 |
+
show_mig = bool(showcase_migration)
|
| 436 |
+
|
| 437 |
+
heur_frames, heur_total = _run_episode(
|
| 438 |
+
scenario, _heuristic_action, int(max_steps), showcase_migration=show_mig
|
| 439 |
+
)
|
| 440 |
+
trained_frames, trained_total = _run_episode(
|
| 441 |
+
scenario, _trained_policy, int(max_steps), showcase_migration=show_mig
|
| 442 |
+
)
|
| 443 |
+
|
| 444 |
+
# Per-step reward chart
|
| 445 |
+
import numpy as np
|
| 446 |
+
|
| 447 |
+
chart_data = {
|
| 448 |
+
"step": list(range(1, max(len(heur_frames), len(trained_frames)) + 1)),
|
| 449 |
+
}
|
| 450 |
+
chart_data["heuristic"] = (
|
| 451 |
+
[f["reward"] for f in heur_frames]
|
| 452 |
+
+ [None] * (len(chart_data["step"]) - len(heur_frames))
|
| 453 |
+
)
|
| 454 |
+
chart_data["trained"] = (
|
| 455 |
+
[f["reward"] for f in trained_frames]
|
| 456 |
+
+ [None] * (len(chart_data["step"]) - len(trained_frames))
|
| 457 |
+
)
|
| 458 |
+
|
| 459 |
+
delta = trained_total - heur_total
|
| 460 |
+
if delta > 0.5:
|
| 461 |
+
verdict = f"π Trained agent WINS by **{delta:+.2f}** reward over heuristic baseline"
|
| 462 |
+
elif delta < -0.5:
|
| 463 |
+
verdict = f"π Trained agent UNDERPERFORMS heuristic by **{delta:+.2f}** (try more training)"
|
| 464 |
+
else:
|
| 465 |
+
verdict = f"β Trained β heuristic this episode (Ξ = {delta:+.2f})"
|
| 466 |
+
|
| 467 |
+
summary_md = f"""
|
| 468 |
+
### Scenario: **{scenario_label}**
|
| 469 |
+
|
| 470 |
+
| Policy | total reward (over {len(heur_frames)} step{'' if len(heur_frames)==1 else 's'}) | threats_left at end |
|
| 471 |
+
| --- | ---: | ---: |
|
| 472 |
+
| Heuristic baseline | {heur_total:+.2f} | {heur_frames[-1]['threats_left'] if heur_frames else '?'} |
|
| 473 |
+
| Trained LLM | {trained_total:+.2f} | {trained_frames[-1]['threats_left'] if trained_frames else '?'} |
|
| 474 |
+
|
| 475 |
+
{verdict}
|
| 476 |
+
"""
|
| 477 |
+
|
| 478 |
+
dash = _feature_dashboard_figure(heur_frames, trained_frames)
|
| 479 |
+
gates = _mesh_gate_bar_figure(heur_frames, trained_frames)
|
| 480 |
+
|
| 481 |
+
return (
|
| 482 |
+
summary_md,
|
| 483 |
+
_frames_to_table(heur_frames),
|
| 484 |
+
_frames_to_table(trained_frames),
|
| 485 |
+
chart_data,
|
| 486 |
+
dash,
|
| 487 |
+
gates,
|
| 488 |
+
_trained_status_text(),
|
| 489 |
+
)
|
| 490 |
+
|
| 491 |
+
|
| 492 |
+
# βββ War Room (LLM debate) β same Gradio page as episode demo ββββββββββββ
|
| 493 |
+
|
| 494 |
+
|
| 495 |
+
def _format_war_room_markdown(data: dict[str, Any]) -> str:
|
| 496 |
+
lines: list[str] = []
|
| 497 |
+
lines.append(
|
| 498 |
+
f"*LLM: `{data.get('model')}` Β· backend: `{data.get('llm_provider', '?')}`*\n"
|
| 499 |
+
)
|
| 500 |
+
verdict = data.get("verdict") or {}
|
| 501 |
+
lines.append("## Final verdict\n")
|
| 502 |
+
ca = verdict.get("consensus_action")
|
| 503 |
+
lines.append(
|
| 504 |
+
f"**{verdict.get('status', 'β')}**"
|
| 505 |
+
+ (f" β action: `{ca}`" if ca else "")
|
| 506 |
+
+ "\n"
|
| 507 |
+
)
|
| 508 |
+
for v in verdict.get("votes_detail") or []:
|
| 509 |
+
lines.append(f"- **{v.get('agent')}:** {v.get('action')}")
|
| 510 |
+
lines.append("\n## Initial positions\n")
|
| 511 |
+
for a in data.get("agents") or []:
|
| 512 |
+
warn = ""
|
| 513 |
+
if a.get("hallucination_flags"):
|
| 514 |
+
warn = " β οΈ *flags (cross-exam)*: " + "; ".join(a["hallucination_flags"])
|
| 515 |
+
lines.append(
|
| 516 |
+
f"### {a.get('display_name', '?')}{warn}\n\n"
|
| 517 |
+
f"**Proposed action:** `{a.get('proposed_action', 'β')}`\n\n"
|
| 518 |
+
f"{a.get('position_text', '')}\n"
|
| 519 |
+
)
|
| 520 |
+
lines.append("\n## Cross-examination\n")
|
| 521 |
+
for c in data.get("cross_examination") or []:
|
| 522 |
+
xf = ""
|
| 523 |
+
if c.get("hallucination_flags"):
|
| 524 |
+
xf = "\n\nβ οΈ " + " Β· ".join(c["hallucination_flags"])
|
| 525 |
+
lines.append(
|
| 526 |
+
f"**{c.get('examiner_name')}** β **{c.get('target_name')}**{xf}\n\n"
|
| 527 |
+
f"{c.get('text', '')}\n\n---\n"
|
| 528 |
+
)
|
| 529 |
+
return "\n".join(lines)
|
| 530 |
+
|
| 531 |
+
|
| 532 |
+
def _war_room_handler(
|
| 533 |
+
threat_type: str,
|
| 534 |
+
severity: float,
|
| 535 |
+
source_ip: str,
|
| 536 |
+
target_service: str,
|
| 537 |
+
description: str,
|
| 538 |
+
preference: str,
|
| 539 |
+
) -> str:
|
| 540 |
+
tt = (threat_type or "").strip()
|
| 541 |
+
sip = (source_ip or "").strip()
|
| 542 |
+
tgt = (target_service or "").strip()
|
| 543 |
+
desc = (description or "").strip()
|
| 544 |
+
if not tt or not sip or not tgt or not desc:
|
| 545 |
+
return "**Fill threat type, source IP, target service, and description.**"
|
| 546 |
+
pref = (preference or "").strip() or None
|
| 547 |
+
try:
|
| 548 |
+
sev = int(severity)
|
| 549 |
+
except (TypeError, ValueError):
|
| 550 |
+
sev = 5
|
| 551 |
+
sev = max(1, min(10, sev))
|
| 552 |
+
try:
|
| 553 |
+
data = asyncio.run(
|
| 554 |
+
run_war_room_debate(
|
| 555 |
+
threat_type=tt,
|
| 556 |
+
severity=sev,
|
| 557 |
+
source_ip=sip,
|
| 558 |
+
target_service=tgt,
|
| 559 |
+
description=desc,
|
| 560 |
+
preference_injection=pref,
|
| 561 |
+
)
|
| 562 |
+
)
|
| 563 |
+
except RuntimeError as e:
|
| 564 |
+
return f"**Configuration error:** {e}"
|
| 565 |
+
except Exception as e:
|
| 566 |
+
return f"**Error:** `{type(e).__name__}: {e}`"
|
| 567 |
+
return _format_war_room_markdown(data)
|
| 568 |
+
|
| 569 |
+
|
| 570 |
+
# βββ Build the UI ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 571 |
+
|
| 572 |
+
|
| 573 |
+
def build_demo() -> gr.Blocks:
|
| 574 |
+
table_headers = [
|
| 575 |
+
"step", "phase", "type", "action", "target", "reward", "threats",
|
| 576 |
+
"pipeline", "mesh gate", "WR turns", "honeypots", "migr phase",
|
| 577 |
+
"migr %", "honeytokens", "attack vec", "directive", "reasoning",
|
| 578 |
+
]
|
| 579 |
+
|
| 580 |
+
with gr.Blocks(title="ImmunoOrg 2.0 β Live Demo", analytics_enabled=False) as demo:
|
| 581 |
+
gr.Markdown(
|
| 582 |
+
"""
|
| 583 |
+
# π‘οΈ ImmunoOrg 2.0 β Live Demo
|
| 584 |
+
|
| 585 |
+
The agent has to defend an enterprise from a cyber-attack **and**
|
| 586 |
+
restructure the organization that lets the attack succeed in the first
|
| 587 |
+
place. Pick one of the 5 scenario families and watch the heuristic
|
| 588 |
+
baseline play it head-to-head against the GRPO-trained LLM defender.
|
| 589 |
+
|
| 590 |
+
**What the extra columns show (backend features, live from the sim):**
|
| 591 |
+
|
| 592 |
+
| Column | Feature in codebase |
|
| 593 |
+
| --- | --- |
|
| 594 |
+
| **pipeline / mesh gate** | 4-gate **DevSecOps Mesh** (`devsecops_mesh.py`): AST β semantic β Terraform β sandbox; gate shows which layer flagged a payload. |
|
| 595 |
+
| **WR turns** | **War Room** multi-agent debate rounds toward consensus (`war_room.py`). |
|
| 596 |
+
| **honeypots / migr / honeytokens** | **50-step polymorphic migration** (`migration_engine.py`): decoy phase, honeypot nodes, honeytoken activations β *not* a separate βhoneycombβ UI; honeypots are tactical decoys here. |
|
| 597 |
+
| **attack vec** | Active attack vector (feeds **MITRE** / kill-chain context in the full env). |
|
| 598 |
+
| **directive** | Board directive text when the scenario injects one. |
|
| 599 |
+
|
| 600 |
+
**Charts below:** interactive **Plotly** dashboards β pipeline/decoys/War Room time series, plus **mesh gate** catch counts.
|
| 601 |
+
|
| 602 |
+
> π [Problem statement](https://github.com/Charannoo/immunoorg/blob/master/PROBLEM_STATEMENT.md)
|
| 603 |
+
> Β· [Source](https://github.com/Charannoo/immunoorg)
|
| 604 |
+
> Β· [Blog](https://github.com/Charannoo/immunoorg/blob/master/BLOG_POST.md)
|
| 605 |
+
> Β· [Training notebook](https://github.com/Charannoo/immunoorg/blob/master/ImmunoOrg_Training_Colab.ipynb)
|
| 606 |
+
"""
|
| 607 |
+
)
|
| 608 |
+
|
| 609 |
+
with gr.Accordion(
|
| 610 |
+
"π Live LLM War Room β 3-agent debate (CISO / DevOps / Architect)",
|
| 611 |
+
open=True,
|
| 612 |
+
):
|
| 613 |
+
gr.Markdown(
|
| 614 |
+
"Same page as the episode demo. Runs **initial positions** + **cross-examination** "
|
| 615 |
+
"via your configured LLM API (**GROQ_API_KEY**, **OPENAI_API_KEY**, or "
|
| 616 |
+
"**ANTHROPIC_API_KEY** in Space secrets). Optional: `POST /api/war-room` for scripts."
|
| 617 |
+
)
|
| 618 |
+
with gr.Row():
|
| 619 |
+
wr_threat = gr.Textbox(
|
| 620 |
+
label="Threat type",
|
| 621 |
+
placeholder="e.g. SQL injection probe",
|
| 622 |
+
)
|
| 623 |
+
wr_sev = gr.Slider(
|
| 624 |
+
minimum=1,
|
| 625 |
+
maximum=10,
|
| 626 |
+
value=5,
|
| 627 |
+
step=1,
|
| 628 |
+
label="Severity (1β10)",
|
| 629 |
+
)
|
| 630 |
+
with gr.Row():
|
| 631 |
+
wr_ip = gr.Textbox(label="Source IP", placeholder="203.0.113.42")
|
| 632 |
+
wr_tgt = gr.Textbox(label="Target service", placeholder="api-payments")
|
| 633 |
+
wr_desc = gr.Textbox(
|
| 634 |
+
label="Description",
|
| 635 |
+
lines=3,
|
| 636 |
+
placeholder="What was observedβ¦",
|
| 637 |
+
)
|
| 638 |
+
wr_pref = gr.Textbox(
|
| 639 |
+
label="Preference injection (optional board directive)",
|
| 640 |
+
placeholder="Breaks deadlock β Architect wins",
|
| 641 |
+
)
|
| 642 |
+
wr_btn = gr.Button("Run War Room debate", variant="secondary")
|
| 643 |
+
wr_out = gr.Markdown("*Results appear here after you run the debate.*")
|
| 644 |
+
wr_btn.click(
|
| 645 |
+
_war_room_handler,
|
| 646 |
+
inputs=[
|
| 647 |
+
wr_threat,
|
| 648 |
+
wr_sev,
|
| 649 |
+
wr_ip,
|
| 650 |
+
wr_tgt,
|
| 651 |
+
wr_desc,
|
| 652 |
+
wr_pref,
|
| 653 |
+
],
|
| 654 |
+
outputs=[wr_out],
|
| 655 |
+
)
|
| 656 |
+
|
| 657 |
+
status_md = gr.Markdown(_trained_status_text())
|
| 658 |
+
|
| 659 |
+
with gr.Row():
|
| 660 |
+
scenario_dd = gr.Dropdown(
|
| 661 |
+
choices=list(_SCENARIO_LABEL.values()),
|
| 662 |
+
value=list(_SCENARIO_LABEL.values())[1],
|
| 663 |
+
label="Scenario family",
|
| 664 |
+
)
|
| 665 |
+
steps_sl = gr.Slider(5, 30, value=15, step=1, label="Max steps per episode")
|
| 666 |
+
mig_cb = gr.Checkbox(
|
| 667 |
+
value=True,
|
| 668 |
+
label="Demo: run START_MIGRATION + honeypot beats (shows decoys/honeytokens)",
|
| 669 |
+
)
|
| 670 |
+
run_btn = gr.Button("Run episode", variant="primary")
|
| 671 |
+
|
| 672 |
+
summary_md = gr.Markdown()
|
| 673 |
+
|
| 674 |
+
with gr.Row():
|
| 675 |
+
with gr.Column():
|
| 676 |
+
gr.Markdown("### Heuristic baseline")
|
| 677 |
+
heur_table = gr.Dataframe(headers=table_headers, wrap=True)
|
| 678 |
+
with gr.Column():
|
| 679 |
+
gr.Markdown("### Trained LLM (or heuristic fallback)")
|
| 680 |
+
trained_table = gr.Dataframe(headers=table_headers, wrap=True)
|
| 681 |
+
|
| 682 |
+
chart = gr.LinePlot(
|
| 683 |
+
x="step", y="heuristic",
|
| 684 |
+
title="Per-step reward (heuristic = orange, trained = blue)",
|
| 685 |
+
height=260,
|
| 686 |
+
)
|
| 687 |
+
|
| 688 |
+
gr.Markdown("### Feature dashboards (Plotly β zoom/pan/hover)")
|
| 689 |
+
with gr.Row():
|
| 690 |
+
signals_plot = gr.Plot(label="Pipeline, honeypots, honeytokens, War Room")
|
| 691 |
+
gate_plot = gr.Plot(label="Which mesh gate fired (AST / semantic / Terraform / sandbox)")
|
| 692 |
+
|
| 693 |
+
gr.Markdown(
|
| 694 |
+
"""
|
| 695 |
+
---
|
| 696 |
+
|
| 697 |
+
### What the agent is reasoning about
|
| 698 |
+
|
| 699 |
+
- 28 actions across 3 categories: **tactical** (block_port, isolate_node, deploy_patch, **deploy_honeypot**, start_migrationβ¦),
|
| 700 |
+
**strategic** (merge_departments, reduce_bureaucracy, establish_devsecopsβ¦),
|
| 701 |
+
**diagnostic** (correlate_failure, identify_silo, vulnerability_scanβ¦).
|
| 702 |
+
- 5-track composable reward:
|
| 703 |
+
uptime (25%) Β· threat neutralization (25%) Β· bureaucracy efficiency (20%) Β·
|
| 704 |
+
code-patch quality (20%) Β· pipeline integrity (10%) β pipeline ties to **mesh** columns.
|
| 705 |
+
- Trained on the elite 20/20/20/20/20 mix of scenario families
|
| 706 |
+
(basic / RAG / executive / silo / stealth) with TRL GRPO + Unsloth.
|
| 707 |
+
|
| 708 |
+
Uncheck **Demo: migration + honeypot** for a βpureβ heuristic/LLM comparison without injected migration steps.
|
| 709 |
+
"""
|
| 710 |
+
)
|
| 711 |
+
|
| 712 |
+
run_btn.click(
|
| 713 |
+
run_demo,
|
| 714 |
+
inputs=[scenario_dd, steps_sl, mig_cb],
|
| 715 |
+
outputs=[
|
| 716 |
+
summary_md, heur_table, trained_table, chart,
|
| 717 |
+
signals_plot, gate_plot, status_md,
|
| 718 |
+
],
|
| 719 |
+
)
|
| 720 |
+
|
| 721 |
+
return demo
|
| 722 |
+
|
| 723 |
+
|
| 724 |
+
__all__ = ["build_demo"]
|