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
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
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
# Copyright (c) Meta Platforms, Inc. and affiliates.
# All rights reserved.
#
# This source code is licensed under the BSD-style license found in the
# LICENSE file in the root directory of this source tree.

"""
GPU Scheduler Environment β€” Core Simulation Engine
====================================================
Simulates an 8-node Γ— 8-GPU cluster ($100,000/day) where an agent
must schedule, preempt, and wait to minimise "compute burn".

Three tasks of escalating difficulty:
    EASY   β€” smooth_sailing:   24h,  low-demand FIFO scheduling
    MEDIUM β€” deadline_crunch:  72h,  overlapping P1/P2 jobs with tight SLAs
    HARD   β€” p0_emergency:    168h,  32-GPU P0 gang job arrives at hour 72
"""

import os
import random
from typing import Any, Dict, List, Optional
from uuid import uuid4

from openenv.core.env_server.interfaces import Environment
from openenv.core.env_server.types import State

# Handles both package-relative import (normal) and direct/Docker import path
try:
    from ..models import (
        ActionType,
        GpuSchedulerAction,
        GpuSchedulerObservation,
        JobInfo,
        NodeInfo,
    )
except ImportError:
    from models import (                          # direct run / Docker fallback
        ActionType,
        GpuSchedulerAction,
        GpuSchedulerObservation,
        JobInfo,
        NodeInfo,
    )


# ---------------------------------------------------------------------------
# Cluster geometry β€” fixed for the entire competition
# ---------------------------------------------------------------------------
NUM_NODES     = 8                            # physical nodes in the cluster
GPUS_PER_NODE = 8                            # GPUs per node
TOTAL_GPUS    = NUM_NODES * GPUS_PER_NODE    # 64 total

# ---------------------------------------------------------------------------
# Economic constants
# The cluster burns $100,000/day when fully loaded.
# We derive per-GPU per-hour cost from that top-line number.
# ---------------------------------------------------------------------------
DAILY_COST_USD      = 100_000.0                          # $100k/day at full 64-GPU capacity
HOURLY_RATE_CLUSTER = DAILY_COST_USD / 24.0              # ~$4,167/hour total cluster
HOURLY_RATE_PER_GPU = HOURLY_RATE_CLUSTER / TOTAL_GPUS   # ~$65/GPU/hour

# Scales reward into a manageable RL range (avoids huge raw-dollar magnitudes)
REWARD_SCALE = 1.0 / HOURLY_RATE_CLUSTER

# ---------------------------------------------------------------------------
# Simulation tuning knobs
# ---------------------------------------------------------------------------
CONTENTION_THRESHOLD       = 0.7   # above this ratio, job progress rate degrades
PREEMPTION_BURN_MULTIPLIER = 10.0  # preemption costs 10Γ— the remaining compute value
MAX_CONTENTION_SLOWDOWN    = 0.5   # at 100% contention, jobs run at 50% speed

# P0 progress contributes 2Γ— more reward than P3 (encourages prioritising critical work)
PRIORITY_WEIGHTS: Dict[int, float] = {0: 2.0, 1: 1.5, 2: 1.0, 3: 0.5}

# Queue delay penalty only fires for high-priority jobs sitting idle in queue
PRIORITY_QUEUE_FACTORS: Dict[int, float] = {0: 2.0, 1: 1.0, 2: 0.0, 3: 0.0}

# ---------------------------------------------------------------------------
# Task configuration table
# hours_per_step controls how many simulated hours each agent step covers,
# keeping total LLM API calls within the 20-minute runtime budget.
# ---------------------------------------------------------------------------
TASK_CONFIGS: Dict[str, Dict] = {
    "smooth_sailing": {
        "total_hours":    24.0,
        "hours_per_step":  1.0,    # 24 agent steps total
        "seed":           42,
        "description":    "Low-demand 24h window. Keep GPUs busy, avoid waste.",
    },
    "deadline_crunch": {
        "total_hours":    72.0,
        "hours_per_step":  2.0,    # 36 agent steps total
        "seed":           137,
        "description":    "72h with overlapping P1/P2 jobs and tight deadlines.",
    },
    "p0_emergency": {
        "total_hours":   168.0,
        "hours_per_step":  4.0,    # 42 agent steps total
        "seed":           999,
        "description":    "One-week run. A 32-GPU P0 job arrives at hour 72. Plan ahead.",
    },
}


# ---------------------------------------------------------------------------
# Job generation helpers (module-level, not methods)
# ---------------------------------------------------------------------------

def _make_job_id(index: int) -> str:
    """Return a short zero-padded job identifier, e.g. 'job_042'."""
    return f"job_{index:03d}"


def _generate_job_schedule(task_name: str, rng: random.Random) -> List[Dict]:
    """
    Pre-generate the complete deterministic job arrival schedule for one episode.

    Using a fixed RNG seed ensures the same task always produces the identical
    job sequence β€” which is critical for reproducible grader scores across
    multiple inference runs.

    Jobs are returned as plain dicts (not JobInfo objects) so they can be safely
    converted to fresh JobInfo instances at arrival time, avoiding shared-object bugs.

    Args:
        task_name: One of 'smooth_sailing', 'deadline_crunch', 'p0_emergency'.
        rng:       Seeded random.Random instance (seed comes from TASK_CONFIGS).

    Returns:
        List of job dicts sorted by arrival_hour, consumed by _release_arriving_jobs.
    """
    jobs: List[Dict] = []
    idx = 0

    if task_name == "smooth_sailing":
        # ~12 small, simple jobs spread evenly over 24 hours.
        # No deadlines β€” agent just needs to keep GPUs occupied efficiently.
        arrival_hours = [0, 1, 2, 4, 5, 7, 9, 11, 14, 17, 20, 23]
        for hour in arrival_hours:
            gpu_count = rng.choice([1, 2, 2, 4])       # small jobs only
            priority  = rng.choice([2, 2, 3])           # P2 / P3 mix
            jobs.append({
                "job_id":         _make_job_id(idx),
                "priority":       priority,
                "priority_label": f"P{priority}",
                "gpu_count":      gpu_count,
                "duration_hours": round(rng.uniform(2.0, 8.0), 1),
                "deadline_hour":  None,                 # no SLA on this task
                "arrival_hour":   float(hour),
            })
            idx += 1

    elif task_name == "deadline_crunch":
        # ~28 mixed P1/P2 jobs with bursty arrivals; 75% carry tight deadlines.
        # Agent must juggle SLA compliance AND utilisation across a 72h window.
        raw_arrivals = sorted(rng.uniform(0.0, 68.0) for _ in range(28))
        for hour in raw_arrivals:
            gpu_count = rng.choice([2, 4, 4, 8])
            priority  = rng.choice([1, 1, 2, 2, 2])
            duration  = round(rng.uniform(3.0, 16.0), 1)
            # Tight deadline: arrival time + full runtime + small grace window
            has_dl   = rng.random() < 0.75
            deadline = round(hour + duration + rng.uniform(2.0, 8.0), 1) if has_dl else None
            jobs.append({
                "job_id":         _make_job_id(idx),
                "priority":       priority,
                "priority_label": f"P{priority}",
                "gpu_count":      gpu_count,
                "duration_hours": duration,
                "deadline_hour":  deadline,
                "arrival_hour":   round(hour, 1),
            })
            idx += 1

    elif task_name == "p0_emergency":
        # 168h (1 week): mixed P1–P3 background load throughout,
        # THEN at hour 72: THE EMERGENCY β€” a 32-GPU P0 gang job.
        # Agent must proactively drain 4 nodes before hour 72 to avoid
        # needing to preempt jobs (which carries a massive burn penalty).
        raw_arrivals = sorted(rng.uniform(0.0, 140.0) for _ in range(40))
        for hour in raw_arrivals:
            gpu_count = rng.choice([1, 2, 4, 4, 8])
            priority  = rng.choice([1, 2, 2, 3, 3])
            duration  = round(rng.uniform(4.0, 48.0), 1)
            has_dl    = rng.random() < 0.5
            deadline  = round(hour + duration + rng.uniform(4.0, 16.0), 1) if has_dl else None
            jobs.append({
                "job_id":         _make_job_id(idx),
                "priority":       priority,
                "priority_label": f"P{priority}",
                "gpu_count":      gpu_count,
                "duration_hours": duration,
                "deadline_hour":  deadline,
                "arrival_hour":   round(hour, 1),
            })
            idx += 1

        # *** THE P0 EMERGENCY β€” deterministic, always at exactly hour 72 ***
        # Needs 4 fully-free nodes (32 GPUs). Deadline = hour 132 (48h runtime + 12h grace).
        jobs.append({
            "job_id":         "job_P0_EMERGENCY",
            "priority":       0,
            "priority_label": "P0",
            "gpu_count":      32,
            "duration_hours": 48.0,
            "deadline_hour":  132.0,    # arrival 72 + runtime 48 + grace 12
            "arrival_hour":   72.0,
        })

        # Re-sort after injecting the emergency job (it may not be the last entry)
        jobs.sort(key=lambda j: j["arrival_hour"])

    return jobs


# ---------------------------------------------------------------------------
# Environment class
# ---------------------------------------------------------------------------

class GpuSchedulerEnvironment(Environment):
    """
    GPU Cluster Scheduler Simulation Environment.

    Implements the full OpenEnv Environment interface:
        reset()  β†’ GpuSchedulerObservation   (fresh episode, empty cluster)
        step()   β†’ GpuSchedulerObservation   (with reward + done flag)
        state    β†’ State                      (episode_id, step_count)

    Internal state:
        _node_jobs:     node_id β†’ [job_ids]   (which jobs occupy each node)
        _node_gpu_used: node_id β†’ int         (GPUs consumed per node β€” source of truth)
        _active_jobs:   job_id  β†’ JobInfo     (currently running jobs)
        _queue:         [JobInfo]              (waiting to be scheduled)

    Task selection: reset() reads the GPU_SCHEDULER_TASK environment variable,
    which inference.py sets before each call to env.reset().
    """

    # Enables multiple concurrent WebSocket sessions β€” each gets its own instance
    SUPPORTS_CONCURRENT_SESSIONS: bool = True

    def __init__(self) -> None:
        """
        Allocate all instance variable containers.

        reset() must always be called before step() β€” __init__ only creates the
        containers; it does not set up any task-specific state. This matches the
        OpenEnv contract where reset() marks the true start of an episode.
        """
        self._state = State(episode_id=str(uuid4()), step_count=0)

        # Task configuration β€” populated by reset()
        self._task_name:      str   = "smooth_sailing"
        self._total_hours:    float = 24.0
        self._hours_per_step: float = 1.0
        self._current_hour:   float = 0.0

        # Cluster state: one entry per physical node (0–7)
        self._node_jobs:     Dict[int, List[str]] = {i: [] for i in range(NUM_NODES)}
        self._node_gpu_used: Dict[int, int]       = {i: 0  for i in range(NUM_NODES)}

        # Job lifecycle containers
        self._job_schedule:   List[Dict]          = []   # full pre-generated arrival plan
        self._queue:          List[JobInfo]        = []   # jobs waiting to be scheduled
        self._active_jobs:    Dict[str, JobInfo]   = {}   # job_id β†’ running JobInfo
        self._completed_jobs: List[JobInfo]        = []
        self._preempted_jobs: List[JobInfo]        = []

        # Episode-level metrics consumed by the task graders
        self._total_jobs_spawned:       int   = 0
        self._sla_jobs_total:           int   = 0
        self._sla_jobs_met:             int   = 0
        self._p0_job_completed:         bool  = False
        self._cumulative_gpu_hrs_used:  float = 0.0
        self._cumulative_gpu_hrs_avail: float = 0.0
        self._compute_burn_usd:         float = 0.0
        self._cumulative_reward:        float = 0.0

        # Plain-English result of the last action (shown to the LLM each step)
        self._last_action_result: str = ""

    # ------------------------------------------------------------------
    # OpenEnv interface β€” reset / step / state
    # ------------------------------------------------------------------

    def reset(self, task_name: Optional[str] = None) -> GpuSchedulerObservation:
        """
        Start a fresh episode for the specified task.

        Task resolution order (first non-None wins):
          1. task_name kwarg  β€” passed directly by inference.py via env.reset(task_name=...)
          2. GPU_SCHEDULER_TASK env var β€” fallback for local direct-server usage
          3. "smooth_sailing" β€” safe default

        Loads the task configuration, seeds the RNG, pre-generates the complete
        job arrival schedule, releases any hour-0 jobs into the visible queue,
        and returns the initial observation.

        Args:
            task_name: One of 'smooth_sailing', 'deadline_crunch', 'p0_emergency'.
                       If None, falls back to GPU_SCHEDULER_TASK env var or default.

        Returns:
            GpuSchedulerObservation of an empty cluster with hour-0 jobs in queue.
        """
        # Resolve task: kwarg > env var > default
        if task_name is None:
            task_name = os.getenv("GPU_SCHEDULER_TASK", "smooth_sailing")
        if task_name not in TASK_CONFIGS:
            task_name = "smooth_sailing"    # safe fallback

        cfg = TASK_CONFIGS[task_name]
        self._task_name      = task_name
        self._total_hours    = cfg["total_hours"]
        self._hours_per_step = cfg["hours_per_step"]
        self._current_hour   = 0.0

        # Wipe all episode state
        self._state           = State(episode_id=str(uuid4()), step_count=0)
        self._node_jobs       = {i: [] for i in range(NUM_NODES)}
        self._node_gpu_used   = {i: 0  for i in range(NUM_NODES)}
        self._queue           = []
        self._active_jobs     = {}
        self._completed_jobs  = []
        self._preempted_jobs  = []
        self._last_action_result = "Episode started. Cluster is empty."

        # Wipe grader metrics
        self._total_jobs_spawned       = 0
        self._sla_jobs_total           = 0
        self._sla_jobs_met             = 0
        self._p0_job_completed         = False
        self._cumulative_gpu_hrs_used  = 0.0
        self._cumulative_gpu_hrs_avail = 0.0
        self._compute_burn_usd         = 0.0
        self._cumulative_reward        = 0.0

        # Fixed seed β†’ identical job schedule every run β†’ reproducible grader scores
        rng = random.Random(cfg["seed"])
        self._job_schedule = _generate_job_schedule(task_name, rng)

        # Jobs arriving at exactly hour 0 go straight into the queue
        self._release_arriving_jobs(from_hour=0.0, to_hour=0.0)

        return self._build_observation(reward=0.0, done=False)

    def step(self, action: GpuSchedulerAction) -> GpuSchedulerObservation:
        """
        Execute one agent decision and advance the simulation clock.

        Step sequence:
            1. Apply the agent's action (SCHEDULE / PREEMPT / WAIT)
               Returns an immediate reward delta for the action itself.
            2. Advance the simulation clock by hours_per_step:
               - Update progress for all running jobs (with contention degradation)
               - Finalise completed jobs and check their SLA status
               - Penalise any jobs that have blown past their deadline
               - Release newly-arrived jobs into the visible queue
               - Apply queue-delay penalty for P0/P1 jobs waiting too long
               - Deduct idle-GPU opportunity cost
            3. Sum action reward + time reward; accumulate into episode total
            4. Check terminal conditions (clock expired or all work done)
            5. Return the new observation (grader score embedded in metadata if done)

        Args:
            action: Typed GpuSchedulerAction from the agent.

        Returns:
            GpuSchedulerObservation with updated cluster state, step reward, done flag.
        """
        self._state.step_count += 1

        # 1. Instant reward/penalty from the action itself
        reward_action = self._apply_action(action)

        # 2. Time-driven reward: progress, costs, SLA violations, queue delay
        reward_time = self._advance_time(self._hours_per_step)

        total_reward = reward_action + reward_time
        self._cumulative_reward += total_reward

        # 3. Termination check
        done = (
            self._current_hour >= self._total_hours
            or self._all_work_done()
        )

        return self._build_observation(reward=total_reward, done=done)

    @property
    def state(self) -> State:
        """
        Return the current OpenEnv State (episode_id + step_count).

        Consumed by the GET /state HTTP endpoint and used by openenv validate
        to confirm the environment is live and correctly tracking steps.
        """
        return self._state

    # ------------------------------------------------------------------
    # Action handlers
    # ------------------------------------------------------------------

    def _apply_action(self, action: GpuSchedulerAction) -> float:
        """
        Dispatch the agent's action to the appropriate handler.

        Always sets self._last_action_result with a human-readable outcome
        so the LLM can see exactly what happened and learn from mistakes.

        Args:
            action: The typed action from the agent.

        Returns:
            Immediate reward delta: 0.0 for WAIT, negative for invalid actions.
        """
        if action.action_type == ActionType.WAIT:
            self._last_action_result = "WAIT β€” advancing clock."
            return 0.0

        if action.action_type == ActionType.SCHEDULE:
            return self._do_schedule(action.job_id, action.node_id)

        if action.action_type == ActionType.PREEMPT:
            return self._do_preempt(action.job_id)

        # Catch-all for unexpected action types
        self._last_action_result = (
            f"UNKNOWN action type '{action.action_type}' β€” treated as WAIT."
        )
        return 0.0

    def _get_free_gpus(self, node_id: int) -> int:
        """
        Return the number of GPUs currently available on a given node.

        _node_gpu_used is the single source of truth for GPU allocation.
        It is updated atomically in _do_schedule, _do_preempt, and when
        jobs complete in _advance_time β€” never recomputed from job lists.

        Args:
            node_id: Node index in range [0, NUM_NODES).

        Returns:
            Integer count of free GPU slots on that node.
        """
        return GPUS_PER_NODE - self._node_gpu_used[node_id]

    def _do_schedule(
        self,
        job_id:  Optional[str],
        node_id: Optional[int],
    ) -> float:
        """
        Attempt to place a queued job onto one or more nodes.

        Single-node jobs (gpu_count <= 8):
            Placed on the node specified by node_id, provided it has enough
            free GPUs. Returns error + small penalty if not.

        Gang jobs (gpu_count > 8, e.g. the 32-GPU P0 emergency):
            Needs N = ceil(gpu_count / 8) FULLY free nodes (all 8 GPUs each).
            The agent's node_id acts as a preferred anchor; the environment
            auto-selects the remaining N-1 nodes from any other fully-free ones.
            This keeps the action space simple for the LLM while modelling the
            real bin-packing challenge of gang scheduling.

        Invalid actions receive a -0.1 penalty (episode continues; LLM sees error).

        Args:
            job_id:  ID of the queued job to place.
            node_id: Target node (0–7); used as anchor hint for gang jobs.

        Returns:
            0.0 on successful placement, -0.1 on any validation failure.
        """
        # --- Validate: job must be in the queue ---
        queue_job = next((j for j in self._queue if j.job_id == job_id), None)
        if queue_job is None:
            self._last_action_result = (
                f"INVALID: '{job_id}' not found in queue "
                f"(may be running, completed, or not yet arrived)."
            )
            return -0.1

        gpu_count    = queue_job.gpu_count
        nodes_needed = (gpu_count + GPUS_PER_NODE - 1) // GPUS_PER_NODE  # ceiling division

        if nodes_needed == 1:
            # ---- Single-node placement ----
            if node_id is None or node_id not in range(NUM_NODES):
                self._last_action_result = (
                    f"INVALID: node_id {node_id} is out of range. Must be 0–7."
                )
                return -0.1

            if self._get_free_gpus(node_id) < gpu_count:
                self._last_action_result = (
                    f"INVALID: node {node_id} has {self._get_free_gpus(node_id)} "
                    f"free GPUs but '{job_id}' needs {gpu_count}."
                )
                return -0.1

            # Allocate GPUs and record placement
            self._node_gpu_used[node_id] += gpu_count
            self._node_jobs[node_id].append(job_id)
            assigned_nodes = [node_id]

        else:
            # ---- Gang-scheduled multi-node placement ----
            # Each assigned node must contribute all 8 GPUs to the job
            fully_free = [
                n for n in range(NUM_NODES)
                if self._get_free_gpus(n) == GPUS_PER_NODE
            ]

            if len(fully_free) < nodes_needed:
                self._last_action_result = (
                    f"INVALID: '{job_id}' [{queue_job.priority_label}] needs "
                    f"{nodes_needed} fully-free nodes ({gpu_count} GPUs total). "
                    f"Only {len(fully_free)} fully-free node(s) available right now. "
                    f"WAIT for running jobs to finish, or PREEMPT lower-priority jobs."
                )
                return -0.1

            # Honour the agent's preferred anchor node if it is free
            if node_id is not None and node_id in fully_free:
                fully_free.remove(node_id)
                selected = [node_id] + fully_free[:nodes_needed - 1]
            else:
                selected = fully_free[:nodes_needed]

            assigned_nodes = selected
            for n in assigned_nodes:
                self._node_gpu_used[n] += GPUS_PER_NODE   # full node consumed
                self._node_jobs[n].append(job_id)

        # --- Move job: queue β†’ active ---
        self._queue = [j for j in self._queue if j.job_id != job_id]
        queue_job.status         = "running"
        queue_job.assigned_nodes = assigned_nodes
        self._active_jobs[job_id] = queue_job

        self._last_action_result = (
            f"Scheduled '{job_id}' [{queue_job.priority_label}] "
            f"on node(s) {assigned_nodes} ({gpu_count} GPUs)."
        )
        return 0.0

    def _do_preempt(self, job_id: Optional[str]) -> float:
        """
        Immediately halt a running job and return it to the front of the queue.

        Applies two penalties:
          1. Preemption Burn Penalty:
               gpu_count Γ— remaining_hours Γ— hourly_rate Γ— PREEMPTION_BURN_MULTIPLIER
             Represents wasted electricity and compute investment.
          2. Checkpoint Loss:
               Job loses up to 1 simulated hour of progress (models the real cost
               of rolling back to the last checkpoint and re-loading state).

        P0 jobs are protected β€” attempting to preempt one returns an error with
        an elevated penalty to strongly discourage the behaviour in the LLM.

        Args:
            job_id: ID of the currently running job to evict.

        Returns:
            Large negative float (burn penalty), or -0.1/-0.5 for invalid actions.
        """
        job = self._active_jobs.get(job_id)
        if job is None:
            self._last_action_result = (
                f"INVALID: '{job_id}' is not currently running."
            )
            return -0.1

        if job.priority == 0:
            # P0 jobs are mission-critical and non-preemptible
            self._last_action_result = (
                f"INVALID: P0 job '{job_id}' is non-preemptible. "
                f"Wait for lower-priority jobs to finish naturally."
            )
            return -0.5    # elevated penalty for even attempting this

        # --- Burn penalty: cost of all wasted GPU-hours ---
        remaining_hours = job.duration_hours * (1.0 - job.progress)
        burn_penalty = (
            job.gpu_count
            * remaining_hours
            * HOURLY_RATE_PER_GPU
            * PREEMPTION_BURN_MULTIPLIER
            * REWARD_SCALE
        )

        # --- Checkpoint loss: roll back up to 1 hour of progress ---
        loss_fraction   = min(1.0 / max(job.duration_hours, 1.0), job.progress)
        job.progress      = max(0.0, job.progress - loss_fraction)
        job.elapsed_hours = max(0.0, job.elapsed_hours - 1.0)

        # --- Free cluster resources ---
        gpus_per_node = GPUS_PER_NODE if len(job.assigned_nodes) > 1 else job.gpu_count
        for n in job.assigned_nodes:
            self._node_gpu_used[n] -= gpus_per_node
            if job_id in self._node_jobs[n]:
                self._node_jobs[n].remove(job_id)

        # --- Return to front of queue for rapid rescheduling ---
        job.status         = "queued"
        job.assigned_nodes = []
        self._queue.insert(0, job)
        del self._active_jobs[job_id]
        self._preempted_jobs.append(job)

        self._last_action_result = (
            f"PREEMPTED '{job_id}' [{job.priority_label}] β€” "
            f"burn ${burn_penalty / REWARD_SCALE:,.0f} | "
            f"progress rolled back to {job.progress:.1%}."
        )
        return -burn_penalty

    # ------------------------------------------------------------------
    # Time advancement
    # ------------------------------------------------------------------

    def _advance_time(self, hours: float) -> float:
        """
        Advance the simulation clock by `hours` and process all side effects.

        Executes six operations in strict order to avoid clock-skew bugs:
            1. Update progress for all running jobs (contention-adjusted rate)
            2. Detect jobs that reached 100% progress β†’ mark as completed
            3. Advance the wall clock
            4. Detect SLA violations (deadline now in the past, job not done)
            5. Release newly-arrived jobs from the master schedule into the queue
            6. Apply queue-delay penalty for P0/P1 waiting in queue
               Deduct idle-GPU opportunity cost

        Args:
            hours: Simulated hours to advance (1.0, 2.0, or 4.0 depending on task).

        Returns:
            Combined float reward from all time-driven components.
        """
        reward = 0.0

        # 1. Update progress for every running job --------------------------------
        newly_completed: List[str] = []

        for job_id, job in list(self._active_jobs.items()):
            # Average GPU utilisation on the nodes this job occupies
            contention = self._average_contention(job.assigned_nodes)

            # Contention above threshold slows progress proportionally
            if contention > CONTENTION_THRESHOLD:
                excess        = (contention - CONTENTION_THRESHOLD) / (1.0 - CONTENTION_THRESHOLD)
                progress_rate = 1.0 - MAX_CONTENTION_SLOWDOWN * excess
            else:
                progress_rate = 1.0    # full speed below threshold

            delta             = (hours / job.duration_hours) * progress_rate
            job.progress      = min(1.0, job.progress + delta)
            job.elapsed_hours += hours * progress_rate

            # Progress reward weighted by job priority
            reward += delta * PRIORITY_WEIGHTS[job.priority]

            if job.progress >= 1.0:
                newly_completed.append(job_id)

        # 2. Finalise completed jobs BEFORE advancing the clock -------------------
        #    This ensures the SLA deadline comparison uses the correct end-of-step time.
        old_hour = self._current_hour
        new_hour = min(old_hour + hours, self._total_hours)

        for job_id in newly_completed:
            job           = self._active_jobs.pop(job_id)
            job.status    = "completed"

            # Free the GPU slots this job held on each assigned node
            gpus_per_node = GPUS_PER_NODE if len(job.assigned_nodes) > 1 else job.gpu_count
            for n in job.assigned_nodes:
                self._node_gpu_used[n] -= gpus_per_node
                if job_id in self._node_jobs[n]:
                    self._node_jobs[n].remove(job_id)
            job.assigned_nodes = []

            self._completed_jobs.append(job)

            # SLA compliance: did the job finish before its deadline?
            if job.deadline_hour is not None and new_hour <= job.deadline_hour:
                self._sla_jobs_met += 1

            # Track the special P0 emergency job for the hard-task grader
            if job_id == "job_P0_EMERGENCY":
                self._p0_job_completed = True

        # 3. Advance the simulation clock -----------------------------------------
        self._current_hour = new_hour

        # 4. SLA violations: penalise running/queued jobs past their deadline ------
        for job in list(self._active_jobs.values()) + list(self._queue):
            if job.deadline_hour and self._current_hour > job.deadline_hour:
                sla_penalty = (
                    job.gpu_count
                    * job.duration_hours
                    * HOURLY_RATE_PER_GPU
                    * 5.0           # SLA violation multiplier
                    * REWARD_SCALE
                )
                reward -= sla_penalty
                job.deadline_hour = None    # clear so we don't double-penalise next step

        # 5. Release newly-arrived jobs into the visible queue --------------------
        self._release_arriving_jobs(from_hour=old_hour, to_hour=self._current_hour)

        # 6a. Queue-delay penalty for high-priority jobs left waiting -------------
        for job in self._queue:
            job.hours_in_queue += hours
            factor = PRIORITY_QUEUE_FACTORS.get(job.priority, 0.0)
            if factor > 0.0:
                # Small per-step nudge to dispatch critical jobs promptly
                reward -= (
                    job.gpu_count * hours * factor
                    * HOURLY_RATE_PER_GPU * REWARD_SCALE * 0.1
                )

        # 6b. Idle-GPU opportunity cost: unused GPUs are wasted money -------------
        active_gpus = sum(self._node_gpu_used.values())
        idle_gpus   = TOTAL_GPUS - active_gpus

        # Track cumulative utilisation for grader score computation
        self._cumulative_gpu_hrs_used  += active_gpus * hours
        self._cumulative_gpu_hrs_avail += TOTAL_GPUS  * hours
        self._compute_burn_usd         += TOTAL_GPUS  * hours * HOURLY_RATE_PER_GPU

        # Penalise idle slots at 30% of full cost (opportunity cost, not full burn)
        reward -= idle_gpus * hours * HOURLY_RATE_PER_GPU * REWARD_SCALE * 0.3

        return reward

    def _release_arriving_jobs(self, from_hour: float, to_hour: float) -> None:
        """
        Move jobs from the master schedule into the visible queue when they arrive.

        Checks all jobs in self._job_schedule whose arrival_hour falls within
        [from_hour, to_hour]. Converts them to JobInfo objects and appends to
        self._queue. Also increments SLA tracking counters for the grader.

        Uses a set of already-seen job IDs to prevent double-releasing.

        Args:
            from_hour: Inclusive start of the arrival window.
            to_hour:   Inclusive end of the arrival window.
        """
        # Build the set of job IDs that have already entered any lifecycle stage
        already_seen: set = (
            {j.job_id for j in self._queue}
            | set(self._active_jobs.keys())
            | {j.job_id for j in self._completed_jobs}
            | {j.job_id for j in self._preempted_jobs}
        )

        for job_dict in self._job_schedule:
            if from_hour <= job_dict["arrival_hour"] <= to_hour:
                if job_dict["job_id"] not in already_seen:
                    job = JobInfo(**job_dict)
                    self._queue.append(job)
                    self._total_jobs_spawned += 1
                    if job.deadline_hour is not None:
                        self._sla_jobs_total += 1
                    already_seen.add(job.job_id)

    # ------------------------------------------------------------------
    # Utility helpers
    # ------------------------------------------------------------------

    def _average_contention(self, node_ids: List[int]) -> float:
        """
        Compute the average memory contention across a set of nodes.

        Contention = used_gpus / GPUS_PER_NODE per node.
        Averaged across all nodes a job occupies.
        Values above CONTENTION_THRESHOLD (0.7) trigger progress rate penalties.

        Args:
            node_ids: List of node IDs a job is currently occupying.

        Returns:
            Average contention float in [0.0, 1.0]. Returns 0.0 for empty list.
        """
        if not node_ids:
            return 0.0
        return sum(
            self._node_gpu_used[n] / GPUS_PER_NODE for n in node_ids
        ) / len(node_ids)

    def _all_work_done(self) -> bool:
        """
        Return True when no further work remains in the episode.

        Triggers early termination only when:
            - All jobs in the master schedule have been released (arrived)
            - The running cluster is idle (no active jobs)
            - The queue is empty (no pending jobs)

        A perfect run where the agent schedules everything efficiently will
        end the episode before the time limit, signalling maximum efficiency.
        """
        all_released = all(
            j["arrival_hour"] <= self._current_hour
            for j in self._job_schedule
        )
        return all_released and not self._active_jobs and not self._queue

    # ------------------------------------------------------------------
    # Task graders β€” produce the normalised 0.0–1.0 score
    # ------------------------------------------------------------------

    def _compute_grader_score(self) -> float:
        """
        Compute the normalised task score injected into result.info when done=True.

        Each task weights different objectives to match its design intent:

        smooth_sailing:
            60% job completion rate  +  40% GPU utilisation
            β†’ proves the agent can keep hardware busy without waste.

        deadline_crunch:
            60% SLA compliance  +  40% job completion rate
            β†’ proves the agent prioritises time-sensitive work.

        p0_emergency:
            50% P0 job completed (binary)  +  30% SLA compliance  +  20% utilisation
            β†’ proves the agent can reserve resources for a critical emergency.

        Returns:
            Score in [0.0, 1.0], rounded to 4 decimal places.
        """
        total       = max(self._total_jobs_spawned, 1)
        completed   = len(self._completed_jobs)

        completion_rate = completed / total
        utilisation     = (
            self._cumulative_gpu_hrs_used
            / max(self._cumulative_gpu_hrs_avail, 1.0)
        )
        sla_rate = self._sla_jobs_met / max(self._sla_jobs_total, 1)

        if self._task_name == "smooth_sailing":
            score = 0.6 * completion_rate + 0.4 * utilisation

        elif self._task_name == "deadline_crunch":
            score = 0.6 * sla_rate + 0.4 * completion_rate

        elif self._task_name == "p0_emergency":
            p0_done = 1.0 if self._p0_job_completed else 0.0
            score   = 0.5 * p0_done + 0.3 * sla_rate + 0.2 * utilisation

        else:
            score = completion_rate    # safe fallback for unknown tasks

        return round(max(0.0, min(score, 1.0)), 4)

    # ------------------------------------------------------------------
    # Observation builder
    # ------------------------------------------------------------------

    def _build_observation(
        self,
        reward: float = 0.0,
        done:   bool  = False,
    ) -> GpuSchedulerObservation:
        """
        Assemble the full typed GpuSchedulerObservation from current internal state.

        Builds:
          - 8Γ—8 cluster grid: each cell holds the index of the job occupying that
            GPU slot (βˆ’1 = free), allowing the agent to visualise cluster layout.
          - Per-node NodeInfo objects with live GPU counts and contention metrics.
          - Snapshot of active jobs and current queue.
          - Simulation clock and cumulative economic burn.
          - Episode metadata (step count, job counters).
          - Grader score in metadata["score"] when done=True, so inference.py
            can extract it via result.info.get("score").

        Args:
            reward: Step reward to embed in the observation base class.
            done:   Whether this is the terminal observation for this episode.

        Returns:
            Fully-populated GpuSchedulerObservation.
        """
        # --- 8Γ—8 cluster grid ---
        active_list = list(self._active_jobs.values())
        job_to_idx  = {j.job_id: i for i, j in enumerate(active_list)}

        grid: List[List[int]] = []
        for node_id in range(NUM_NODES):
            row      = [-1] * GPUS_PER_NODE
            gpu_slot = 0
            for job_id in self._node_jobs[node_id]:
                job  = self._active_jobs.get(job_id)
                if job is None:
                    continue
                idx   = job_to_idx.get(job_id, -1)
                # Gang jobs use all 8 GPUs per node; single-node jobs use job.gpu_count
                gpus  = GPUS_PER_NODE if len(job.assigned_nodes) > 1 else job.gpu_count
                gpus  = min(gpus, GPUS_PER_NODE - gpu_slot)  # don't overflow the row
                for g in range(gpus):
                    row[gpu_slot + g] = idx
                gpu_slot += gpus
            grid.append(row)

        # --- Per-node NodeInfo with live contention ---
        nodes: List[NodeInfo] = [
            NodeInfo(
                node_id           = n,
                total_gpus        = GPUS_PER_NODE,
                used_gpus         = self._node_gpu_used[n],
                free_gpus         = self._get_free_gpus(n),
                memory_contention = round(self._node_gpu_used[n] / GPUS_PER_NODE, 3),
                running_jobs      = list(self._node_jobs[n]),
            )
            for n in range(NUM_NODES)
        ]

        # --- Metadata: always include episode stats; inject score when terminal ---
        meta: Dict[str, Any] = {
            "step_count":     self._state.step_count,
            "jobs_spawned":   self._total_jobs_spawned,
            "jobs_completed": len(self._completed_jobs),
            "jobs_preempted": len(self._preempted_jobs),
            "sla_met":        self._sla_jobs_met,
            "sla_total":      self._sla_jobs_total,
        }
        # Terminal score: populate the observation-level score field so it
        # survives OpenEnv's serialize_observation (which strips metadata).
        terminal_score: Optional[float] = None
        if done:
            terminal_score     = self._compute_grader_score()
            meta["score"]      = terminal_score   # also in metadata for completeness

        return GpuSchedulerObservation(
            cluster_grid        = grid,
            nodes               = nodes,
            active_jobs         = active_list,
            queue               = list(self._queue),
            current_hour        = round(self._current_hour, 2),
            total_hours         = self._total_hours,
            compute_burn_so_far = round(self._compute_burn_usd, 2),
            task_name           = self._task_name,
            last_action_result  = self._last_action_result,
            done                = done,
            reward              = reward,
            metadata            = meta,
            score               = terminal_score,
        )