File size: 21,701 Bytes
4058302
 
58af620
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4058302
 
 
 
eb2d131
 
 
58af620
eb2d131
58af620
eb2d131
 
 
 
 
 
4058302
eb2d131
 
 
 
58af620
eb2d131
 
4058302
eb2d131
58af620
4058302
 
 
 
 
58af620
eb2d131
 
 
 
 
 
 
 
 
c3648b5
eb2d131
 
4058302
 
 
 
 
 
 
eb2d131
4058302
 
eb2d131
 
 
4058302
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
eb2d131
 
 
 
4058302
 
 
 
 
 
 
eb2d131
 
4058302
 
 
 
58af620
 
4058302
58af620
 
 
 
 
eb2d131
 
 
 
c3648b5
eb2d131
58af620
eb2d131
 
 
58af620
eb2d131
58af620
 
 
eb2d131
 
 
 
 
 
 
 
c575679
eb2d131
 
 
 
 
c3648b5
 
 
eb2d131
 
 
 
 
 
 
 
c3648b5
 
 
 
 
 
 
4058302
c3648b5
eb2d131
 
4058302
 
eb2d131
 
4058302
 
 
 
 
 
 
 
 
 
eb2d131
 
1d6a71e
58af620
1d6a71e
 
 
 
 
 
 
 
 
 
58af620
 
1d6a71e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4058302
1d6a71e
 
58af620
 
 
 
 
eb2d131
 
 
 
 
 
 
 
 
 
 
 
 
1d6a71e
 
eb2d131
 
4058302
 
eb2d131
4058302
 
1d6a71e
eb2d131
 
c575679
eb2d131
 
 
 
 
1d6a71e
c575679
eb2d131
 
 
58af620
 
 
c3648b5
 
 
 
58af620
c3648b5
58af620
 
 
 
 
eb2d131
4058302
 
 
 
 
58af620
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c3648b5
58af620
 
 
 
 
 
 
 
 
 
 
 
 
 
c3648b5
 
 
58af620
 
 
 
 
 
c3648b5
58af620
 
c3648b5
 
58af620
4058302
58af620
 
 
 
 
 
 
c3648b5
 
 
 
 
 
eb2d131
 
 
 
58af620
 
c3648b5
 
 
 
58af620
 
 
 
c3648b5
58af620
 
 
 
 
c3648b5
eb2d131
58af620
 
 
 
 
c3648b5
 
 
 
58af620
 
 
 
 
 
 
 
 
 
 
 
c3648b5
 
 
 
58af620
 
 
 
 
 
 
 
c3648b5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
eb2d131
 
 
 
 
58af620
 
 
 
 
 
 
 
 
 
 
 
 
 
 
eb2d131
 
 
58af620
 
eb2d131
 
 
 
 
 
 
 
4058302
58af620
eb2d131
 
c3648b5
 
 
 
eb2d131
c3648b5
 
eb2d131
 
 
 
4058302
58af620
 
 
 
 
 
 
 
 
 
 
4058302
c3648b5
 
 
 
 
eb2d131
 
 
 
 
 
4058302
eb2d131
 
 
 
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
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
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
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
"""Hugging Face TRL training + evaluation pipeline.

Pipeline:

1. **Rollout**: run the ``HeuristicCoordinator`` against the live Incident
   Command Center environment to collect ``(prompt, completion)`` pairs.
2. **SFT**: fine-tune a small instruction-tuned LLM on those pairs using
   TRL's ``SFTTrainer`` with a single ``text`` column (robust across TRL
   ≥ 0.20).
3. **Save**: persist the fine-tuned weights + tokenizer to
   ``artifacts/sft_model`` so the same script can later load them as an
   agent policy.
4. **Evaluate**: play the environment with four policies
   ``random / heuristic / base_model / sft_model`` under identical seeds
   and write a reward curve + metrics JSON into ``artifacts/``.

Designed to work on CPU for smoke checks and on Colab T4 / HF Spaces GPUs
for full runs. LLM evaluation auto-enables on CUDA and can be forced with
``EVAL_LLM_MODELS=true``.
"""

from __future__ import annotations

import json
import os
import random
from dataclasses import dataclass
from pathlib import Path
from typing import Callable, Dict, List, Optional

import matplotlib.pyplot as plt
from datasets import Dataset

from client import IncidentCommandEnvClient
from inference import HeuristicCoordinator, random_action
from models import IncidentAction, IncidentObservation


ARTIFACT_DIR = Path("artifacts")
ARTIFACT_DIR.mkdir(parents=True, exist_ok=True)
SFT_MODEL_DIR = ARTIFACT_DIR / "sft_model"

ENV_URL = os.getenv("ENV_URL", "http://127.0.0.1:8000")
BASE_MODEL = os.getenv("BASE_MODEL", "Qwen/Qwen2.5-0.5B-Instruct")
MAX_ROLLOUT_STEPS = int(os.getenv("MAX_ROLLOUT_STEPS", "120"))
MAX_LLM_EVAL_STEPS = int(os.getenv("MAX_LLM_EVAL_STEPS", "60"))
EPISODES_PER_TASK = int(os.getenv("EPISODES_PER_TASK", "3"))
TRAIN_EPOCHS = float(os.getenv("TRAIN_EPOCHS", "1"))
TRAIN_BATCH_SIZE = int(os.getenv("TRAIN_BATCH_SIZE", "1"))
TRAIN_GRAD_ACCUM = int(os.getenv("TRAIN_GRAD_ACCUM", "2"))
TRAIN_MAX_LENGTH = int(os.getenv("TRAIN_MAX_LENGTH", "768"))
_EVAL_LLM_ENV = os.getenv("EVAL_LLM_MODELS", "auto").strip().lower()


@dataclass
class EpisodeStats:
    policy_name: str
    task_name: str
    total_reward: float
    steps: int
    success: bool
    components: Dict[str, float] = None  # type: ignore[assignment]


# ---------------------------------------------------------------------------
# Prompt / completion formatting
# ---------------------------------------------------------------------------


def obs_to_prompt(obs: IncidentObservation) -> str:
    targets = obs.investigation_targets or {}
    return (
        "You are operating a multi-agent incident command center. "
        "Pick the next action for the appropriate specialist role.\n\n"
        f"Incident ID: {obs.incident_id}\n"
        f"Title: {obs.incident_title}\n"
        f"Description: {obs.incident_description}\n"
        f"Customer tier: {obs.customer_tier} | "
        f"Affected users: {obs.affected_users_estimate} | "
        f"Revenue impact (USD/min): {obs.revenue_impact_usd_per_min}\n"
        f"Postmortem required: {obs.postmortem_required}\n"
        f"Visible signals: {', '.join(obs.visible_signals or [])}\n"
        f"Available log targets: {', '.join(targets.get('logs', []) or [])}\n"
        f"Available metric targets: {', '.join(targets.get('metrics', []) or [])}\n"
        f"Available KB articles: {', '.join(targets.get('kb', []) or [])}\n"
        f"Budget remaining: {obs.budget_remaining} actions | "
        f"SLA remaining: {obs.sla_minutes_remaining} min | "
        f"Clues found: {obs.clues_found} | "
        f"Mitigation applied: {obs.mitigation_applied}\n"
        f"Last terminal output: {obs.terminal_output}\n\n"
        "Respond with a JSON object containing exactly these keys: "
        "actor, action_type, target, root_cause, resolution_summary, "
        "postmortem_note, confidence, reason."
    )


def action_to_json(action: IncidentAction) -> str:
    payload = action.model_dump(exclude_none=True)
    return json.dumps(payload, ensure_ascii=True)


# ---------------------------------------------------------------------------
# Rollout / dataset construction
# ---------------------------------------------------------------------------


def rollout(
    policy_name: str,
    task_name: str,
    collect_dataset: bool = False,
    policy_callable: Optional[Callable[[IncidentObservation], IncidentAction]] = None,
    max_steps: Optional[int] = None,
):
    """Play one episode and return (stats, rows, rewards).

    If ``policy_callable`` is provided it takes precedence over
    ``policy_name`` — this is how the LLM policies plug in.
    """
    env = IncidentCommandEnvClient(base_url=ENV_URL).sync()
    coordinator = HeuristicCoordinator()
    records: List[Dict[str, str]] = []
    rewards: List[float] = []
    components_sum: Dict[str, float] = {}
    steps = 0
    step_cap = max_steps if max_steps is not None else MAX_ROLLOUT_STEPS

    try:
        result = env.reset(task_name=task_name)
        while not result.done and steps < step_cap:
            steps += 1
            if policy_callable is not None:
                action = policy_callable(result.observation)
            elif policy_name == "heuristic":
                action = coordinator.select_action(result.observation)
            else:
                action = random_action(result.observation)

            if collect_dataset:
                records.append(
                    {
                        "prompt": obs_to_prompt(result.observation),
                        "completion": action_to_json(action),
                    }
                )

            result = env.step(action)
            rewards.append(float(result.reward or 0.0))
            step_components = getattr(result.observation, "reward_components", None) or {}
            for key, value in step_components.items():
                components_sum[key] = components_sum.get(key, 0.0) + float(value)
    finally:
        try:
            env.close()
        except Exception:
            pass

    total_reward = sum(rewards)
    success = total_reward > 0.0
    stats = EpisodeStats(
        policy_name=policy_name,
        task_name=task_name,
        total_reward=total_reward,
        steps=steps,
        success=success,
        components={k: round(v, 4) for k, v in components_sum.items()},
    )
    return (stats, records, rewards)


def build_training_dataset(episodes_per_task: int = EPISODES_PER_TASK) -> Dataset:
    rows: List[Dict[str, str]] = []
    for task in ["easy", "medium", "hard"]:
        for _ in range(episodes_per_task):
            _, new_rows, _ = rollout(
                policy_name="heuristic", task_name=task, collect_dataset=True
            )
            rows.extend(new_rows)
    return Dataset.from_list(rows)


# ---------------------------------------------------------------------------
# TRL SFT
# ---------------------------------------------------------------------------


def _dataset_to_sft_text_column(dataset: Dataset, tokenizer) -> Dataset:
    """Collapse (prompt, completion) pairs into a single `text` field."""
    from transformers import PreTrainedTokenizerBase

    if not isinstance(tokenizer, PreTrainedTokenizerBase):
        return dataset

    cols = set(dataset.column_names)
    if "completion" not in cols and "response" in cols:
        dataset = dataset.rename_column("response", "completion")
    if "prompt" not in dataset.column_names or "completion" not in dataset.column_names:
        raise ValueError(
            f"Expected columns 'prompt' and 'completion' (or 'response'). "
            f"Got: {dataset.column_names}"
        )

    has_template = bool(getattr(tokenizer, "chat_template", None))

    def to_text_batched(examples: Dict[str, List[str]]) -> Dict[str, List[str]]:
        out: List[str] = []
        for prompt, completion in zip(examples["prompt"], examples["completion"]):
            if has_template:
                messages = [
                    {"role": "user", "content": prompt},
                    {"role": "assistant", "content": completion},
                ]
                out.append(
                    tokenizer.apply_chat_template(
                        messages,
                        tokenize=False,
                        add_generation_prompt=False,
                    )
                )
            else:
                out.append(f"User: {prompt}\n\nAssistant: {completion}")
        return {"text": out}

    to_drop = [c for c in dataset.column_names if c != "text"]
    return dataset.map(to_text_batched, batched=True, remove_columns=to_drop)


def run_trl_sft(dataset: Dataset) -> Path:
    """Fine-tune ``BASE_MODEL`` on the collected dataset and save the model.

    Returns the directory of the saved SFT checkpoint (``artifacts/sft_model``).
    """
    try:
        from transformers import AutoModelForCausalLM, AutoTokenizer
        from trl import SFTConfig, SFTTrainer
    except ImportError as exc:
        raise RuntimeError(
            "Missing training dependencies. Install with: pip install -r requirements.txt"
        ) from exc

    tokenizer = AutoTokenizer.from_pretrained(BASE_MODEL)
    if tokenizer.pad_token is None:
        tokenizer.pad_token = tokenizer.eos_token

    model = AutoModelForCausalLM.from_pretrained(BASE_MODEL)
    train_ds = _dataset_to_sft_text_column(dataset, tokenizer)

    config = SFTConfig(
        output_dir="outputs/sft_run",
        per_device_train_batch_size=TRAIN_BATCH_SIZE,
        gradient_accumulation_steps=TRAIN_GRAD_ACCUM,
        learning_rate=2e-5,
        num_train_epochs=TRAIN_EPOCHS,
        max_length=TRAIN_MAX_LENGTH,
        dataset_text_field="text",
        logging_steps=5,
        save_strategy="no",
        report_to="none",
    )

    trainer = SFTTrainer(
        model=model,
        args=config,
        train_dataset=train_ds,
        processing_class=tokenizer,
    )
    trainer.train()

    SFT_MODEL_DIR.mkdir(parents=True, exist_ok=True)
    trainer.save_model(str(SFT_MODEL_DIR))
    tokenizer.save_pretrained(str(SFT_MODEL_DIR))

    log_path = ARTIFACT_DIR / "training_log.json"
    with log_path.open("w", encoding="utf-8") as f:
        json.dump(trainer.state.log_history, f, indent=2, default=str)
    print(f"[train] Saved SFT checkpoint to {SFT_MODEL_DIR}")
    print(f"[train] Saved training log to {log_path}")

    del trainer, model, tokenizer
    _free_gpu_memory()
    return SFT_MODEL_DIR


# ---------------------------------------------------------------------------
# Evaluation + reporting
# ---------------------------------------------------------------------------


def _free_gpu_memory() -> None:
    try:
        import gc
        gc.collect()
        import torch

        if torch.cuda.is_available():
            torch.cuda.empty_cache()
    except Exception:
        pass


def _cuda_available() -> bool:
    try:
        import torch

        return torch.cuda.is_available()
    except Exception:
        return False


def _should_evaluate_llms() -> bool:
    if _EVAL_LLM_ENV in {"1", "true", "yes", "on"}:
        return True
    if _EVAL_LLM_ENV in {"0", "false", "no", "off"}:
        return False
    # "auto" / empty: enable only when a CUDA GPU is available so CPU runs
    # stay fast.
    return _cuda_available()


def _evaluate_single_policy(
    policy_name: str,
    select_fn: Callable[[IncidentObservation], IncidentAction],
    max_steps: Optional[int] = None,
    components_accumulator: Optional[Dict[str, float]] = None,
) -> List[float]:
    scores: List[float] = []
    for task in ["easy", "medium", "hard"]:
        stats, _, _ = rollout(
            policy_name=policy_name,
            task_name=task,
            policy_callable=select_fn,
            max_steps=max_steps,
        )
        print(
            f"[eval] policy={policy_name} task={task} "
            f"reward={stats.total_reward:+.2f} steps={stats.steps}"
        )
        scores.append(round(stats.total_reward, 4))
        if components_accumulator is not None and stats.components:
            for k, v in stats.components.items():
                components_accumulator[k] = components_accumulator.get(k, 0.0) + v
    return scores


def evaluate_policies(
    seed: int = 7,
    evaluate_llms: Optional[bool] = None,
) -> Dict[str, object]:
    """Run each policy once per task under the same seed.

    Returns a dict with keys ``scores`` (mapping policy -> [easy, medium, hard])
    and ``components`` (mapping policy -> {component_name: summed_value}).
    """
    random.seed(seed)

    scores: Dict[str, List[float]] = {
        "random": [],
        "heuristic": [],
        "base_model": [],
        "sft_model": [],
    }
    components: Dict[str, Dict[str, float]] = {
        "random": {},
        "heuristic": {},
        "base_model": {},
        "sft_model": {},
    }

    for task in ["easy", "medium", "hard"]:
        random_stats, _, _ = rollout("random", task)
        heuristic_stats, _, _ = rollout("heuristic", task)
        scores["random"].append(round(random_stats.total_reward, 4))
        scores["heuristic"].append(round(heuristic_stats.total_reward, 4))
        for k, v in (random_stats.components or {}).items():
            components["random"][k] = components["random"].get(k, 0.0) + v
        for k, v in (heuristic_stats.components or {}).items():
            components["heuristic"][k] = components["heuristic"].get(k, 0.0) + v

    should_eval_llms = _should_evaluate_llms() if evaluate_llms is None else evaluate_llms
    if not should_eval_llms:
        print("[eval] Skipping LLM evaluation (no GPU or EVAL_LLM_MODELS=false).")
        return {"scores": scores, "components": components}

    try:
        from llm_policy import LLMPolicy
    except Exception as exc:  # pragma: no cover - import-time safety
        print(f"[eval] Could not import LLMPolicy ({exc}); skipping LLM eval.")
        return {"scores": scores, "components": components}

    # Base model
    try:
        print(f"[eval] Loading BASE model: {BASE_MODEL}")
        base = LLMPolicy(BASE_MODEL, label="base_model")
        scores["base_model"] = _evaluate_single_policy(
            "base_model",
            base.select_action,
            max_steps=MAX_LLM_EVAL_STEPS,
            components_accumulator=components["base_model"],
        )
        base.release()
        _free_gpu_memory()
    except Exception as exc:
        print(f"[eval] Base-model evaluation failed: {exc}")

    # SFT model
    if SFT_MODEL_DIR.exists():
        try:
            print(f"[eval] Loading SFT model: {SFT_MODEL_DIR}")
            sft = LLMPolicy(str(SFT_MODEL_DIR), label="sft_model")
            scores["sft_model"] = _evaluate_single_policy(
                "sft_model",
                sft.select_action,
                max_steps=MAX_LLM_EVAL_STEPS,
                components_accumulator=components["sft_model"],
            )
            sft.release()
            _free_gpu_memory()
        except Exception as exc:
            print(f"[eval] SFT-model evaluation failed: {exc}")
    else:
        print(f"[eval] No SFT checkpoint found at {SFT_MODEL_DIR}; skipping SFT eval.")

    return {"scores": scores, "components": components}


def plot_training_curve(
    log_path: Path = ARTIFACT_DIR / "training_log.json",
    out_path: Path = ARTIFACT_DIR / "training_curve.png",
) -> None:
    """Plot loss (and token accuracy if present) vs training step from TRL log.

    Satisfies the hackathon minimum requirement of showing BOTH loss and reward plots.
    """
    if not log_path.exists():
        return
    try:
        log = json.loads(log_path.read_text(encoding="utf-8"))
    except Exception:
        return

    steps: List[int] = []
    losses: List[float] = []
    accs: List[Optional[float]] = []
    for entry in log:
        if "loss" not in entry or "step" not in entry:
            continue
        try:
            steps.append(int(entry["step"]))
            losses.append(float(entry["loss"]))
            acc = entry.get("mean_token_accuracy")
            accs.append(float(acc) if acc is not None else None)
        except Exception:
            continue

    if not steps:
        return

    fig, ax1 = plt.subplots(figsize=(9, 5))
    ax1.plot(steps, losses, marker="o", color="tab:blue", label="Training loss", linewidth=2)
    ax1.set_xlabel("Training step")
    ax1.set_ylabel("Loss", color="tab:blue")
    ax1.tick_params(axis="y", labelcolor="tab:blue")
    ax1.grid(alpha=0.3)

    if all(a is not None for a in accs):
        ax2 = ax1.twinx()
        ax2.plot(
            steps,
            accs,
            marker="^",
            color="tab:orange",
            label="Mean token accuracy",
            linewidth=2,
        )
        ax2.set_ylabel("Mean token accuracy", color="tab:orange")
        ax2.tick_params(axis="y", labelcolor="tab:orange")
        ax2.set_ylim(0.0, 1.05)

    plt.title("TRL SFT training curve — loss & token accuracy")
    plt.tight_layout()
    plt.savefig(out_path, dpi=160)
    plt.close()


def plot_reward_components(
    components_by_policy: Dict[str, Dict[str, float]],
    out_path: Path = ARTIFACT_DIR / "reward_components.png",
) -> None:
    """Grouped bar chart of reward-component contributions per policy.

    Visualizes the rubric-based reward signal: where each policy's reward
    actually comes from (step cost, clue bonus, handoff, mitigation, closure,
    etc.). Makes the reward design visible to judges at a glance.
    """
    if not components_by_policy:
        return

    all_keys: List[str] = []
    for comps in components_by_policy.values():
        for k in comps:
            if k not in all_keys:
                all_keys.append(k)
    if not all_keys:
        return

    policies = list(components_by_policy.keys())
    n_policies = len(policies)
    n_keys = len(all_keys)

    fig, ax = plt.subplots(figsize=(max(10, n_keys * 0.6), 6))
    bar_width = 0.8 / max(n_policies, 1)
    colors = {
        "random": "tab:red",
        "heuristic": "tab:blue",
        "base_model": "tab:orange",
        "sft_model": "tab:green",
    }
    for i, policy in enumerate(policies):
        values = [components_by_policy[policy].get(k, 0.0) for k in all_keys]
        offsets = [x + i * bar_width - 0.4 + bar_width / 2 for x in range(n_keys)]
        ax.bar(
            offsets,
            values,
            width=bar_width,
            label=policy,
            color=colors.get(policy, None),
        )

    ax.axhline(0, color="gray", linewidth=0.8)
    ax.set_xticks(range(n_keys))
    ax.set_xticklabels(all_keys, rotation=35, ha="right")
    ax.set_ylabel("Summed reward contribution (all tasks)")
    ax.set_title("Where each policy earns / loses reward — rubric components")
    ax.legend()
    ax.grid(axis="y", alpha=0.3)
    plt.tight_layout()
    plt.savefig(out_path, dpi=160)
    plt.close()


def plot_rewards(score_map: Dict[str, List[float]]) -> None:
    labels = ["easy", "medium", "hard"]
    x = list(range(len(labels)))
    plt.figure(figsize=(9, 5))

    style = {
        "random": ("x", "tab:red", "Random baseline"),
        "heuristic": ("o", "tab:blue", "Heuristic coordinator"),
        "base_model": ("^", "tab:orange", "Base LLM (untrained)"),
        "sft_model": ("D", "tab:green", "Fine-tuned LLM (SFT)"),
    }

    for key, (marker, color, label) in style.items():
        values = score_map.get(key) or []
        if not values or len(values) != len(labels):
            continue
        plt.plot(x, values, marker=marker, color=color, label=label, linewidth=2)

    plt.xticks(x, labels)
    plt.xlabel("Task difficulty")
    plt.ylabel("Episode total reward")
    plt.title("Incident Command Center — policy comparison")
    plt.axhline(0, linestyle="--", color="gray", alpha=0.5)
    plt.grid(alpha=0.3)
    plt.legend()
    plt.tight_layout()
    plt.savefig(ARTIFACT_DIR / "reward_curve.png", dpi=160)
    plt.close()


def main() -> None:
    dataset = build_training_dataset(episodes_per_task=EPISODES_PER_TASK)
    dataset.save_to_disk(str(ARTIFACT_DIR / "trl_dataset"))

    run_trl_sft(dataset)
    eval_out = evaluate_policies()
    scores: Dict[str, List[float]] = eval_out["scores"]  # type: ignore[assignment]
    components: Dict[str, Dict[str, float]] = eval_out["components"]  # type: ignore[assignment]

    plot_rewards(scores)
    plot_training_curve()
    plot_reward_components(components)

    summary = {
        "base_model": BASE_MODEL,
        "dataset_rows": len(dataset),
        "episodes_per_task": EPISODES_PER_TASK,
        "random_rewards": scores.get("random", []),
        "heuristic_rewards": scores.get("heuristic", []),
        "base_model_rewards": scores.get("base_model", []),
        "sft_model_rewards": scores.get("sft_model", []),
        "improvement_sft_over_base": [
            round(s - b, 4)
            for s, b in zip(scores.get("sft_model", []), scores.get("base_model", []))
        ] if scores.get("sft_model") and scores.get("base_model") else [],
        "improvement_heuristic_over_random": [
            round(h - r, 4)
            for h, r in zip(scores.get("heuristic", []), scores.get("random", []))
        ],
        "reward_components_by_policy": {
            policy: {k: round(v, 4) for k, v in comps.items()}
            for policy, comps in components.items()
            if comps
        },
    }
    with open(ARTIFACT_DIR / "summary_metrics.json", "w", encoding="utf-8") as f:
        json.dump(summary, f, indent=2)

    print("Training and evaluation complete.")
    print(f"Saved artifacts in: {ARTIFACT_DIR.resolve()}")
    print(json.dumps(summary, indent=2))


if __name__ == "__main__":
    main()