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
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
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
"""OnCallEnv: Red Shift — Gradio Space app.

Four tabs:
  1. Run Episode      — single rollout with rich replay + per-step plots
  2. Baseline vs Trained — A/B comparison on identical incident
  3. Environment      — service inventory, fault catalog, action surface
  4. About            — methodology, results, citations

Pre-computed (non-streaming) replays — handler runs the full episode then
returns the rendered HTML in one shot. Stable, predictable, demo-safe.
"""

from __future__ import annotations

import gradio as gr

from ops_theme import OpsRoomTheme, CUSTOM_CSS, HEADER_HTML
from replay_html import (
    format_replay_html,
    format_scores_html,
    format_comparison_verdict_html,
)
from chart_helpers import (
    build_reward_progression_df,
    build_action_distribution_df,
    build_comparison_df,
    build_dual_progression_df,
    build_method_ablation_df,
    build_held_out_comparison_df,
)
from demo_runner import (
    list_scenarios,
    get_scenario_info,
    run_episode,
    run_comparison,
    is_real_env_available,
)
from inspector import (
    get_services_df,
    get_alerts_df,
    get_fault_catalog_df,
    get_action_surface_df,
    get_env_config_html,
)


# ---------------------------------------------------------------------------
# Handlers
# ---------------------------------------------------------------------------

def handler_run_episode(scenario_id: str, trained: bool, seed: int):
    """Handler for Tab 1."""
    scenario_id = scenario_id or list_scenarios()[0]
    log, rewards = run_episode(scenario_id, trained=bool(trained), seed=int(seed))

    info = get_scenario_info(scenario_id)
    replay = format_replay_html(
        log, rewards,
        scenario_id=scenario_id,
        fault_label=info.get("fault_label", ""),
    )
    scores = format_scores_html(rewards)
    reward_df = build_reward_progression_df(log)
    actions_df = build_action_distribution_df(log)
    return replay, scores, reward_df, actions_df


def handler_compare(scenario_id: str, seed: int):
    """Handler for Tab 2."""
    scenario_id = scenario_id or list_scenarios()[0]
    result = run_comparison(scenario_id, seed=int(seed))

    info = get_scenario_info(scenario_id)
    fault = info.get("fault_label", "")

    baseline_replay = format_replay_html(
        result["baseline"]["log"], result["baseline"]["rewards"],
        scenario_id=scenario_id, fault_label=fault,
    )
    trained_replay = format_replay_html(
        result["trained"]["log"], result["trained"]["rewards"],
        scenario_id=scenario_id, fault_label=fault,
    )
    verdict = format_comparison_verdict_html(
        result["baseline"]["rewards"], result["trained"]["rewards"],
    )
    comp_df = build_comparison_df(
        result["baseline"]["rewards"], result["trained"]["rewards"],
    )
    dual_df = build_dual_progression_df(
        result["baseline"]["log"], result["trained"]["log"],
    )
    return baseline_replay, trained_replay, verdict, comp_df, dual_df


def handler_inspect(scenario_id: str):
    """Handler for Tab 3."""
    config_html = get_env_config_html(scenario_id)
    services_df = get_services_df(scenario_id)
    alerts_df = get_alerts_df(scenario_id)
    return config_html, services_df, alerts_df


# ---------------------------------------------------------------------------
# Build UI
# ---------------------------------------------------------------------------

SCENARIO_CHOICES = list_scenarios()
DEFAULT_SCENARIO = SCENARIO_CHOICES[0] if SCENARIO_CHOICES else "seed_easy_memory_leak"


with gr.Blocks(
    title="OnCallEnv: Red Shift",
    fill_width=True,
    theme=OpsRoomTheme(),
    css=CUSTOM_CSS,
) as demo:

    gr.HTML(HEADER_HTML)

    with gr.Tabs():

        # ============================================================
        # TAB 1 — Run Episode
        # ============================================================
        with gr.TabItem("Run Episode"):
            with gr.Row():
                # Sidebar
                with gr.Column(scale=1, min_width=320):
                    gr.Markdown("### Episode configuration")

                    scenario_dropdown = gr.Dropdown(
                        choices=SCENARIO_CHOICES,
                        value=DEFAULT_SCENARIO,
                        label="Scenario",
                        info="Seed incidents from real-world fault patterns.",
                    )
                    trained_toggle = gr.Checkbox(
                        value=True,
                        label="Use trained defender",
                        info="Toggle off to run the untrained Qwen2.5-3B baseline.",
                    )
                    seed_input = gr.Number(
                        value=42,
                        label="Random seed",
                        precision=0,
                        info="Deterministic episode for reproducibility.",
                    )
                    run_btn = gr.Button(
                        "▶  Run Episode",
                        variant="primary",
                        size="lg",
                    )

                    gr.Markdown("---")
                    gr.Markdown("### Final reward")
                    scores_panel = gr.HTML()

                # Main area
                with gr.Column(scale=3):
                    with gr.Tabs():
                        with gr.TabItem("Incident Replay"):
                            replay_panel = gr.HTML()
                        with gr.TabItem("Reward & Action Analytics"):
                            with gr.Row():
                                reward_plot = gr.LinePlot(
                                    x="step",
                                    y="reward",
                                    color="metric",
                                    title="Reward progression (per-step and cumulative)",
                                    tooltip=["step", "reward", "metric"],
                                    height=320,
                                )
                            with gr.Row():
                                action_plot = gr.BarPlot(
                                    x="action",
                                    y="count",
                                    color="category",
                                    title="Action distribution",
                                    tooltip=["action", "count", "category"],
                                    height=320,
                                )

            run_btn.click(
                handler_run_episode,
                inputs=[scenario_dropdown, trained_toggle, seed_input],
                outputs=[replay_panel, scores_panel, reward_plot, action_plot],
            )

            # Auto-run on load so the page is never empty
            demo.load(
                handler_run_episode,
                inputs=[scenario_dropdown, trained_toggle, seed_input],
                outputs=[replay_panel, scores_panel, reward_plot, action_plot],
            )

        # ============================================================
        # TAB 2 — Baseline vs Trained
        # ============================================================
        with gr.TabItem("Baseline vs Trained"):
            with gr.Row():
                with gr.Column(scale=1, min_width=320):
                    gr.Markdown(
                        "### A/B comparison\n\n"
                        "Run the **untrained** Qwen2.5-3B baseline and the "
                        "**Method-2 GRPO checkpoint** on the same incident, "
                        "with the same seed, and compare turn-by-turn. "
                        "This is the headline reward-improvement evidence."
                    )

                    comp_scenario = gr.Dropdown(
                        choices=SCENARIO_CHOICES,
                        value=DEFAULT_SCENARIO,
                        label="Scenario",
                    )
                    comp_seed = gr.Number(
                        value=42,
                        label="Random seed",
                        precision=0,
                        info="Same seed → identical incident on both sides.",
                    )
                    comp_btn = gr.Button(
                        "▶  Run Comparison",
                        variant="primary",
                        size="lg",
                    )

                    gr.Markdown("---")
                    gr.Markdown("### Training impact")
                    comp_verdict = gr.HTML()

                with gr.Column(scale=3):
                    with gr.Tabs():
                        with gr.TabItem("Side-by-Side Replays"):
                            with gr.Row():
                                with gr.Column():
                                    gr.Markdown("#### ⚠ Untrained baseline")
                                    baseline_replay = gr.HTML()
                                with gr.Column():
                                    gr.Markdown("#### ✓ Trained defender")
                                    trained_replay = gr.HTML()

                        with gr.TabItem("Reward Component Comparison"):
                            comp_bar = gr.BarPlot(
                                x="component",
                                y="score",
                                color="model",
                                title="Reward components: baseline vs trained",
                                tooltip=["component", "score", "model"],
                                height=380,
                            )
                            dual_plot = gr.LinePlot(
                                x="step",
                                y="reward",
                                color="model",
                                title="Cumulative reward over episode steps",
                                tooltip=["step", "reward", "model"],
                                height=320,
                            )

            comp_btn.click(
                handler_compare,
                inputs=[comp_scenario, comp_seed],
                outputs=[
                    baseline_replay,
                    trained_replay,
                    comp_verdict,
                    comp_bar,
                    dual_plot,
                ],
            )

        # ============================================================
        # TAB 3 — Environment Inspector
        # ============================================================
        with gr.TabItem("Environment"):
            with gr.Row():
                with gr.Column(scale=1, min_width=320):
                    gr.Markdown(
                        "### Inspect simulator state\n\n"
                        "Demystifies what the agent reasons over: services, "
                        "alerts, fault catalog, and the full action surface."
                    )
                    inspect_scenario = gr.Dropdown(
                        choices=SCENARIO_CHOICES,
                        value=DEFAULT_SCENARIO,
                        label="Scenario",
                    )
                    inspect_btn = gr.Button(
                        "🔍  Inspect",
                        variant="primary",
                        size="lg",
                    )
                    gr.Markdown("---")
                    config_panel = gr.HTML()

                with gr.Column(scale=3):
                    with gr.Tabs():
                        with gr.TabItem("Services"):
                            services_table = gr.Dataframe(
                                label="Microservice graph (7 services)",
                                headers=["Service", "Type", "Replicas", "Dependencies", "Status"],
                                interactive=False,
                            )
                        with gr.TabItem("Active Alerts"):
                            alerts_table = gr.Dataframe(
                                label="Currently firing alerts for this scenario",
                                headers=["Severity", "Service", "Message"],
                                interactive=False,
                            )
                        with gr.TabItem("Fault Catalog"):
                            gr.Dataframe(
                                value=get_fault_catalog_df(),
                                label="12 fault families implemented in the simulator",
                                interactive=False,
                            )
                        with gr.TabItem("Action Surface"):
                            gr.Dataframe(
                                value=get_action_surface_df(),
                                label="Tools available to the agent (read / mutate / terminal)",
                                interactive=False,
                            )

            inspect_btn.click(
                handler_inspect,
                inputs=[inspect_scenario],
                outputs=[config_panel, services_table, alerts_table],
            )
            demo.load(
                handler_inspect,
                inputs=[inspect_scenario],
                outputs=[config_panel, services_table, alerts_table],
            )

        # ============================================================
        # TAB 4 — Methodology & Results
        # ============================================================
        with gr.TabItem("Methodology"):
            with gr.Row():
                with gr.Column(scale=2):
                    gr.Markdown(
                        """
                        ## OnCallEnv: Red Shift

                        An OpenEnv-compliant training environment for autonomous incident
                        response, plus a controlled three-method ablation comparing
                        scenario-generation strategies for curriculum learning.

                        ### The research question

                        Given a fixed defender architecture (Qwen2.5-3B + LoRA + GRPO), does
                        the *choice of curriculum strategy* materially affect downstream
                        performance on held-out incidents — and if so, which strategy wins?

                        We compare three strategies spanning the full design space:

                        - **Method 1 — No-feedback static curriculum** (baseline). Scenarios
                          drawn uniformly from a fixed pool. The defender's performance has
                          no influence on which scenarios appear next.
                        - **Method 2 — ACCEL-style algorithmic attacker.** A regret-based
                          curriculum that observes solve rates and re-weights the scenario
                          buffer toward the defender's frontier (Parker-Holder et al., ICML
                          2022). The "attacker" is an algorithm, not an LLM.
                        - **Method 3 — SPICE-style LLM self-play.** A second LLM acts as an
                          attacker that generates scenarios via YAML mutation, trained with
                          a Gaussian variance reward peaking at solve probability 0.5 (Liu
                          et al., FAIR 2025).

                        ### Key results

                        | Method                          | Held-out (24 tasks) | Δ vs floor |
                        | ------------------------------- | ------------------- | ---------- |
                        | Random action floor             | 0.270               | —          |
                        | Untrained Qwen2.5-3B            | 0.638               | +0.368     |
                        | M1: No feedback (static)        | 0.872               | +0.602     |
                        | **M2: ACCEL feedback**          | **0.905**           | **+0.635** |
                        | M3: SPICE self-play             | 0.310 *(diverged)*  | —          |
                        | Scripted defender ceiling       | 0.921               | +0.651     |

                        Method 2 closes **94.3%** of the gap from the untrained baseline to
                        the scripted defender ceiling. Method 3 with the same compute and
                        same architecture failed to converge — consistent with known
                        scale requirements for LLM self-play (SPICE was demonstrated on
                        Qwen3-4B / OctoThinker-8B; we used 3B with 4-bit LoRA).

                        ### Reward composition

                        The four-part reward, computed via OpenEnv's composable Rubric API:

                        ```
                        R = 0.35·R_recovery + 0.30·R_rca_quality
                          + 0.25·R_blast_radius + 0.10·R_safety
                        ```

                        - `R_recovery` is paid only when synthetic post-resolution traffic
                          confirms the SLI holds — defeats the "kubectl delete pod to mark
                          green" reward hack.
                        - `R_rca_quality` scores root-cause-category match, timeline
                          coverage for the root service, non-generic five-whys, and
                          service-specific action items.
                        - `R_blast_radius` penalizes prolonged customer-facing impact.
                        - `R_safety` decrements for unsafe actions on stateful services.

                        ### Tech stack

                        OpenEnv 0.2.x · HuggingFace TRL · Unsloth · GRPO · Qwen2.5-3B
                        · LoRA r=16 α=32 · 2× Tesla T4 (Kaggle free tier) · ~6.4 hours
                        wall-clock for 300 GRPO steps.

                        ### Citations

                        - Liu et al. *SPICE: Self-Play In Corpus Environments Improves
                          Reasoning.* arXiv:2510.24684, FAIR @ Meta, 2025.
                        - Parker-Holder et al. *Evolving Curricula with Regret-Based
                          Environment Design (ACCEL).* ICML 2022.
                        - Dennis et al. *Emergent Complexity and Zero-Shot Transfer via
                          Unsupervised Environment Design (PAIRED).* NeurIPS 2020.
                        - Shao et al. *DeepSeekMath: Pushing the Limits of Mathematical
                          Reasoning in Open Language Models (GRPO).* arXiv:2402.03300, 2024.
                        - Jha et al. *ITBench: Evaluating AI Agents across Diverse
                          Real-World IT Automation Tasks.* arXiv:2502.05352, IBM, 2025.

                        ### Links

                        - GitHub: [srimanreddy4/MetaHackathon-R2](https://github.com/srimanreddy4/MetaHackathon-R2/tree/hf)
                        - HF Space: [NeerjaK/OnCallEnv](https://huggingface.co/spaces/NeerjaK/OnCallEnv)
                        - Colab: [Training notebook](https://colab.research.google.com/drive/1KhS2mJ7VKm5o3yRzg47rrjJPUlMj2IQg?usp=sharing)
                        - Blog post: [blog.md](https://huggingface.co/spaces/NeerjaK/OnCallEnv/blob/main/blog.md)
                        """
                    )
                with gr.Column(scale=1):
                    gr.Markdown("### Method ablation")
                    gr.LinePlot(
                        value=build_method_ablation_df(),
                        x="step",
                        y="reward",
                        color="method",
                        title="Reward over training steps",
                        tooltip=["method", "step", "reward"],
                        height=320,
                    )
                    gr.Markdown("### Held-out evaluation")
                    gr.BarPlot(
                        value=build_held_out_comparison_df(),
                        x="method",
                        y="score",
                        title="Mean reward on 24 held-out incidents",
                        tooltip=["method", "score"],
                        height=320,
                    )


# ---------------------------------------------------------------------------
# Launch
# ---------------------------------------------------------------------------

if __name__ == "__main__":
    demo.launch(
        server_name="0.0.0.0",
        server_port=7860,
    )