File size: 51,451 Bytes
906af9d
 
 
 
 
 
 
 
 
 
 
 
 
80ef9e0
eaa2876
4e3f8b1
eaa2876
 
906af9d
 
 
 
307d729
906af9d
 
 
 
 
80ef9e0
906af9d
307d729
906af9d
 
 
 
 
 
 
 
307d729
7fedc25
eaa2876
 
 
 
80ef9e0
 
 
eaa2876
80ef9e0
eaa2876
 
80ef9e0
eaa2876
 
906af9d
 
 
 
 
 
b33db9f
 
 
ecc152d
 
eaa2876
 
 
 
 
 
 
d2dbaf9
 
 
 
 
 
 
906af9d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
ecc152d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
906af9d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
80ef9e0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
906af9d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
eaa2876
a72929a
eaa2876
 
 
 
 
 
 
 
 
d2dbaf9
 
 
 
 
 
 
 
 
 
 
80ef9e0
 
 
 
 
 
 
 
 
 
 
eaa2876
 
 
80ef9e0
 
eaa2876
 
 
 
 
906af9d
80ef9e0
 
 
 
eaa2876
 
 
 
 
 
 
 
 
906af9d
 
 
 
b33db9f
 
 
 
 
 
 
 
 
 
 
 
 
307d729
 
 
 
 
 
 
 
 
 
 
 
906af9d
 
 
 
 
 
 
 
 
 
 
eaa2876
a72929a
906af9d
 
80ef9e0
906af9d
 
 
307d729
 
 
 
 
d2dbaf9
80ef9e0
906af9d
 
 
 
 
 
 
 
 
 
80ef9e0
 
a72929a
ecc152d
a72929a
 
 
eaa2876
a72929a
 
eaa2876
a72929a
d2dbaf9
 
 
 
80ef9e0
d2dbaf9
 
 
 
 
4e3f8b1
906af9d
 
 
80ef9e0
906af9d
 
 
307d729
 
 
 
 
 
 
 
 
906af9d
 
 
 
 
 
a24d0f2
906af9d
 
 
b33db9f
 
 
 
 
 
 
 
 
 
 
 
ecc152d
 
b33db9f
 
 
ecc152d
b33db9f
 
 
 
 
 
906af9d
 
 
b33db9f
ecc152d
906af9d
 
80ef9e0
906af9d
 
 
b33db9f
307d729
 
 
 
 
 
 
 
d2dbaf9
 
 
 
906af9d
 
a72929a
 
 
 
 
 
 
 
 
 
 
 
 
 
ecc152d
 
 
a72929a
 
ecc152d
a72929a
 
 
906af9d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4e3f8b1
d2dbaf9
906af9d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
a24d0f2
 
 
906af9d
 
a24d0f2
 
 
 
 
 
 
 
d2dbaf9
a24d0f2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d2dbaf9
 
a24d0f2
 
d2dbaf9
 
 
a24d0f2
d2dbaf9
a24d0f2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d2dbaf9
a24d0f2
 
d2dbaf9
 
a24d0f2
 
d2dbaf9
a24d0f2
d2dbaf9
a24d0f2
906af9d
 
 
 
d2dbaf9
906af9d
 
 
 
 
 
d2dbaf9
906af9d
 
 
 
 
 
 
80ef9e0
eaa2876
a72929a
d2dbaf9
906af9d
307d729
906af9d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c51ff20
 
906af9d
 
c51ff20
 
 
 
 
906af9d
c51ff20
 
906af9d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4e3f8b1
d2dbaf9
 
4e3f8b1
 
 
 
 
 
d2dbaf9
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
906af9d
 
a24d0f2
906af9d
 
80ef9e0
906af9d
 
 
 
 
307d729
 
906af9d
 
 
 
4e3f8b1
 
307d729
 
 
 
 
4e3f8b1
 
 
906af9d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
a24d0f2
 
 
 
 
 
 
 
 
 
 
 
 
 
906af9d
 
307d729
 
906af9d
 
 
eaa2876
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5b99233
 
eaa2876
5b99233
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
eaa2876
 
5b99233
 
eaa2876
 
 
 
 
 
 
 
 
 
 
 
5b99233
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
eaa2876
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
a24d0f2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
eaa2876
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
a24d0f2
 
 
 
 
eaa2876
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
906af9d
307d729
 
 
 
 
 
 
 
 
906af9d
 
 
 
 
 
 
 
 
 
 
 
 
 
4e3f8b1
 
 
 
 
 
80ef9e0
307d729
 
80ef9e0
 
 
 
 
 
 
 
 
 
 
 
 
228ed67
80ef9e0
 
228ed67
80ef9e0
 
307d729
4e3f8b1
 
 
 
 
 
 
 
 
 
 
 
a24d0f2
 
 
 
4e3f8b1
 
 
 
307d729
 
 
4e3f8b1
 
 
 
 
 
 
 
 
 
 
 
307d729
313a7b0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
307d729
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
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
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
"""Managed snapshot runtime for the shipped OpenRange server process.

This module keeps the OpenEnv-facing environment instances lightweight while a
single shared manager owns the admitted snapshot pool, generation loop, and
episode feedback.
"""

from __future__ import annotations

import asyncio
import json
import logging
import os
import random
import shlex
import shutil
import subprocess as sp
import tempfile
import threading
import time
from dataclasses import dataclass, field
from pathlib import Path
from pathlib import PurePosixPath
from typing import Any

import yaml

from open_range.builder.builder import LLMSnapshotBuilder, TemplateOnlyBuilder
from open_range.builder.mutation_policy import PopulationMutationPolicy
from open_range.builder.mutator import Mutator
from open_range.builder.renderer import PAYLOAD_MANIFEST_NAME, SnapshotRenderer
from open_range.builder.snapshot_store import SnapshotStore
from open_range.protocols import (
    BuildContext,
    CheckResult,
    ContainerSet,
    SnapshotBuilder,
    SnapshotSpec,
)
from open_range.server.compose_runner import BootedSnapshotProject, ComposeProjectRunner
from open_range.models import RangeState
from open_range.validator.build_boot import BuildBootCheck
from open_range.validator.difficulty import DifficultyCheck
from open_range.validator.evidence import EvidenceCheck
from open_range.validator.exploitability import ExploitabilityCheck
from open_range.validator.graph_consistency import GraphConsistencyCheck
from open_range.validator.graph_evidence import GraphEvidenceSufficiencyCheck
from open_range.validator.graph_reward_grounding import GraphRewardGroundingCheck
from open_range.validator.isolation import IsolationCheck
from open_range.validator.manifest_compliance import ManifestComplianceCheck
from open_range.validator.npc_consistency import NPCConsistencyCheck
from open_range.validator.patchability import PatchabilityCheck
from open_range.validator.path_solvability import PathSolvabilityCheck
from open_range.validator.realism_review import RealismReviewCheck
from open_range.validator.reward_grounding import RewardGroundingCheck
from open_range.validator.task_feasibility import TaskFeasibilityCheck
from open_range.validator.validator import ValidationResult, ValidatorGate

logger = logging.getLogger(__name__)

_DEFAULT_MANIFEST = ("manifests", "tier1_basic.yaml")
_DEFAULT_MANAGED_VALIDATOR_PROFILE = "training"
_DEFAULT_MANAGED_LIVE_ADMISSION = True
_ALLOW_NON_LIVE_ADMISSION_ENV = "OPENRANGE_ALLOW_NON_LIVE_ADMISSION"
_ALLOW_OFFLINE_ADMISSION_ENV = "OPENRANGE_ALLOW_OFFLINE_ADMISSION"
_DEFAULT_VALIDATOR_PROFILE = _DEFAULT_MANAGED_VALIDATOR_PROFILE
_VALIDATOR_PROFILE_ALIASES = {
    "light": "offline",
    "static": "offline",
    "full": "training",
    "strict": "training",
}
_LIVE_VALIDATOR_PROFILES = {"training"}
_PERSISTED_SNAPSHOT_VALIDATION_ALIASES = {
    "none": "trust",
    "disabled": "trust",
    "off": "trust",
    "revalidate": "offline",
    "strict": "offline",
}


def _env_flag(name: str, default: bool = False) -> bool:
    raw = os.getenv(name)
    if raw is None:
        return default
    return raw.strip().lower() in {"1", "true", "yes", "on"}


def _env_int(name: str, default: int) -> int:
    raw = os.getenv(name)
    if raw is None or raw.strip() == "":
        return default
    return int(raw)


def _non_live_opt_out_enabled() -> bool:
    return _env_flag(_ALLOW_NON_LIVE_ADMISSION_ENV, default=False) or _env_flag(
        _ALLOW_OFFLINE_ADMISSION_ENV,
        default=False,
    )


def _non_live_opt_out_env_name() -> str | None:
    if _env_flag(_ALLOW_NON_LIVE_ADMISSION_ENV, default=False):
        return _ALLOW_NON_LIVE_ADMISSION_ENV
    if _env_flag(_ALLOW_OFFLINE_ADMISSION_ENV, default=False):
        return _ALLOW_OFFLINE_ADMISSION_ENV
    return None


def _candidate_roots() -> list[Path]:
    roots: list[Path] = []
    cwd = Path.cwd()
    roots.append(cwd)
    file_path = Path(__file__).resolve()
    roots.extend(file_path.parents[:6])

    unique: list[Path] = []
    seen: set[Path] = set()
    for root in roots:
        if root in seen:
            continue
        seen.add(root)
        unique.append(root)
    return unique


def _resolve_default_manifest_path() -> Path:
    for root in _candidate_roots():
        candidate = root.joinpath(*_DEFAULT_MANIFEST)
        if candidate.exists():
            return candidate
    raise FileNotFoundError(
        "Could not locate the default manifest. "
        "Set OPENRANGE_RUNTIME_MANIFEST to an explicit YAML path."
    )


def _resolve_store_dir(store_dir: str | Path | None) -> Path:
    if store_dir is None:
        return Path(os.getenv("OPENRANGE_SNAPSHOT_DIR", "snapshots")).resolve()
    return Path(store_dir).resolve()


def _run_coro_sync(coro: Any) -> Any:
    """Run an async coroutine from sync code.

    The runtime is used from sync OpenEnv environment methods and a background
    thread, so we provide a conservative bridge here.
    """

    try:
        asyncio.get_running_loop()
    except RuntimeError:
        return asyncio.run(coro)

    result: dict[str, Any] = {}
    error: list[BaseException] = []

    def _runner() -> None:
        try:
            result["value"] = asyncio.run(coro)
        except BaseException as exc:  # noqa: BLE001
            error.append(exc)

    thread = threading.Thread(target=_runner, name="openrange-coro-bridge")
    thread.start()
    thread.join()
    if error:
        raise error[0]
    return result.get("value")


@dataclass(slots=True)
class EpisodeOutcome:
    snapshot_id: str | None
    red_solved: bool
    blue_detected: bool
    steps: int
    weak_areas: list[str] = field(default_factory=list)
    completed: bool = False
    recorded_at: float = field(default_factory=time.time)


class CurriculumTracker:
    """Tiny in-process curriculum memory for future snapshot generation."""

    def __init__(self, max_history: int = 100) -> None:
        self.max_history = max_history
        self._history: list[EpisodeOutcome] = []
        self._lock = threading.Lock()

    def record(self, outcome: EpisodeOutcome) -> None:
        with self._lock:
            self._history.append(outcome)
            if len(self._history) > self.max_history:
                del self._history[: len(self._history) - self.max_history]

    def build_context(self, *, seed: int, tier: int) -> BuildContext:
        with self._lock:
            history = list(self._history)

        completed = [o for o in history if o.completed]
        red_solve_rate = (
            sum(1 for o in completed if o.red_solved) / len(completed)
            if completed
            else 0.0
        )
        blue_detect_rate = (
            sum(1 for o in completed if o.blue_detected) / len(completed)
            if completed
            else 0.0
        )

        weak_counts: dict[str, int] = {}
        for outcome in completed:
            if outcome.red_solved:
                continue
            for area in outcome.weak_areas:
                weak_counts[area] = weak_counts.get(area, 0) + 1

        weak_areas = [
            area
            for area, _count in sorted(
                weak_counts.items(),
                key=lambda item: (-item[1], item[0]),
            )[:3]
        ]

        return BuildContext(
            seed=seed,
            tier=tier,
            red_solve_rate=red_solve_rate,
            blue_detect_rate=blue_detect_rate,
            weak_areas=weak_areas,
        )

    @property
    def history(self) -> list[EpisodeOutcome]:
        with self._lock:
            return list(self._history)

    def snapshot_stats(self) -> dict[str, dict[str, Any]]:
        with self._lock:
            history = list(self._history)

        now = time.time()
        stats: dict[str, dict[str, Any]] = {}
        for outcome in history:
            if not outcome.snapshot_id:
                continue
            stat = stats.setdefault(
                outcome.snapshot_id,
                {
                    "plays": 0,
                    "completed": 0,
                    "red_solved": 0,
                    "blue_detected": 0,
                    "plays_recent": 0,
                    "last_seen_at": 0.0,
                },
            )
            stat["plays"] += 1
            if outcome.completed:
                stat["completed"] += 1
            if outcome.red_solved:
                stat["red_solved"] += 1
            if outcome.blue_detected:
                stat["blue_detected"] += 1
            if now - outcome.recorded_at <= 300:
                stat["plays_recent"] += 1
            stat["last_seen_at"] = max(float(stat["last_seen_at"]), outcome.recorded_at)

        for stat in stats.values():
            plays = max(int(stat["plays"]), 1)
            stat["red_solve_rate"] = stat["red_solved"] / plays
            stat["blue_detect_rate"] = stat["blue_detected"] / plays
        return stats


@dataclass(frozen=True, slots=True)
class RuntimeSnapshot:
    snapshot_id: str
    snapshot: SnapshotSpec


class StructuralSnapshotCheck:
    """Lightweight admission check for the shipped no-Docker runtime path."""

    async def check(
        self,
        snapshot: SnapshotSpec,
        containers: ContainerSet,
    ) -> CheckResult:
        issues: list[str] = []
        if not snapshot.truth_graph.vulns:
            issues.append("truth_graph has no vulns")
        if not snapshot.golden_path:
            issues.append("golden_path is empty")
        if not snapshot.flags:
            issues.append("flags are empty")
        if not snapshot.task.red_briefing or not snapshot.task.blue_briefing:
            issues.append("task briefings are missing")
        for briefing_name, text in (
            ("red_briefing", snapshot.task.red_briefing),
            ("blue_briefing", snapshot.task.blue_briefing),
        ):
            for flag in snapshot.flags:
                if flag.value and flag.value in text:
                    issues.append(f"flag leaked in {briefing_name}")
            for step in snapshot.golden_path:
                if len(step.command) > 20 and step.command in text:
                    issues.append(f"golden-path command leaked in {briefing_name}")

        passed = len(issues) == 0
        return CheckResult(
            name="structural_snapshot",
            passed=passed,
            details={"issues": issues},
            error="" if passed else "; ".join(issues),
        )


def _default_builder() -> SnapshotBuilder:
    mode = os.getenv("OPENRANGE_RUNTIME_BUILDER", "template").strip().lower()
    if mode == "template":
        return TemplateOnlyBuilder()
    if mode == "llm":
        return LLMSnapshotBuilder()
    raise ValueError(
        f"Unsupported OPENRANGE_RUNTIME_BUILDER={mode!r}. "
        "Expected 'template' or 'llm'."
    )


def _normalize_validator_profile(profile: str | None) -> str:
    normalized = (profile or _DEFAULT_VALIDATOR_PROFILE).strip().lower()
    normalized = _VALIDATOR_PROFILE_ALIASES.get(normalized, normalized)
    if normalized not in {"offline", "training"}:
        raise ValueError(
            f"Unsupported validator profile {profile!r}. "
            "Expected 'offline' or 'training'."
        )
    return normalized


def _normalize_persisted_snapshot_validation(policy: str | None) -> str:
    normalized = (policy or "offline").strip().lower()
    normalized = _PERSISTED_SNAPSHOT_VALIDATION_ALIASES.get(normalized, normalized)
    if normalized not in {"trust", "offline"}:
        raise ValueError(
            f"Unsupported persisted snapshot validation policy {policy!r}. "
            "Expected 'trust' or 'offline'."
        )
    return normalized


def _graph_checks(manifest: dict[str, Any]) -> list[Any]:
    return [
        ManifestComplianceCheck(manifest),
        GraphConsistencyCheck(),
        PathSolvabilityCheck(),
        GraphEvidenceSufficiencyCheck(),
        GraphRewardGroundingCheck(),
    ]


def _build_validator(profile: str, manifest: dict[str, Any]) -> ValidatorGate:
    normalized = _normalize_validator_profile(profile)
    if normalized == "offline":
        return ValidatorGate(
            _graph_checks(manifest)
            + [
                StructuralSnapshotCheck(),
                TaskFeasibilityCheck(),
            ]
        )

    return ValidatorGate(
        _graph_checks(manifest)
        + [
            StructuralSnapshotCheck(),
            TaskFeasibilityCheck(),
            BuildBootCheck(),
            ExploitabilityCheck(),
            PatchabilityCheck(),
            EvidenceCheck(),
            RewardGroundingCheck(),
            IsolationCheck(),
            DifficultyCheck(),
            NPCConsistencyCheck(),
            RealismReviewCheck(),
        ]
    )


def _strict_admission_enabled(profile: str, live_admission_enabled: bool) -> bool:
    return profile in _LIVE_VALIDATOR_PROFILES and live_admission_enabled


def _managed_admission_failure_message(profile: str, live_admission_enabled: bool) -> str:
    return (
        "Managed runtime requires strict live admission "
        f"(validator_profile='training', live_admission_enabled=1). "
        f"Current configuration: validator_profile={profile!r}, "
        f"live_admission_enabled={live_admission_enabled!r}."
    )


def _default_live_validator(*, include_patchability: bool = False) -> ValidatorGate:
    checks = [
        BuildBootCheck(),
        ExploitabilityCheck(),
        EvidenceCheck(),
        RewardGroundingCheck(),
    ]
    if include_patchability:
        checks.append(PatchabilityCheck())
    return ValidatorGate(checks)


class ManagedSnapshotRuntime:
    """Shared server-side manager for admitted snapshots."""

    def __init__(
        self,
        *,
        manifest: dict[str, Any] | None = None,
        manifest_path: str | Path | None = None,
        store_dir: str | Path | None = None,
        builder: SnapshotBuilder | None = None,
        validator: ValidatorGate | None = None,
        validator_profile: str | None = None,
        allow_insecure_offline_profile: bool | None = None,
        pool_size: int = 3,
        selection_strategy: str = "random",
        parent_selection_strategy: str = "policy",
        refill_enabled: bool = False,
        refill_interval_s: float = 2.0,
        generation_retries: int = 3,
        live_admission_enabled: bool = False,
        teardown_booted_projects: bool = True,
        compose_runner: ComposeProjectRunner | None = None,
        live_validator: ValidatorGate | None = None,
        enable_patch_validation: bool = False,
        persisted_snapshot_validation: str | None = None,
        mutation_policy: PopulationMutationPolicy | None = None,
    ) -> None:
        self.manifest_path = (
            Path(manifest_path).resolve()
            if manifest_path is not None
            else _resolve_default_manifest_path()
        )
        self.manifest = manifest or self._load_manifest(self.manifest_path)
        self.store_dir = _resolve_store_dir(store_dir)
        self.store = SnapshotStore(str(self.store_dir))
        self.builder = builder or _default_builder()
        self.mutation_policy = mutation_policy or PopulationMutationPolicy()
        self.mutator = Mutator(self.builder, policy=self.mutation_policy)
        self.allow_insecure_offline_profile = (
            _non_live_opt_out_enabled()
            if allow_insecure_offline_profile is None
            else bool(allow_insecure_offline_profile)
        )
        self.validator_profile = _normalize_validator_profile(
            validator_profile
            or os.getenv("OPENRANGE_RUNTIME_VALIDATOR_PROFILE", _DEFAULT_VALIDATOR_PROFILE)
        )
        self._enforce_validator_profile_policy()
        self.persisted_snapshot_validation = _normalize_persisted_snapshot_validation(
            persisted_snapshot_validation
            or os.getenv("OPENRANGE_PERSISTED_SNAPSHOT_VALIDATION", "offline")
        )
        self.validator = validator or _build_validator(self.validator_profile, self.manifest)
        self.persisted_validator = (
            _build_validator("offline", self.manifest)
            if self.persisted_snapshot_validation == "offline"
            else None
        )
        self.renderer = SnapshotRenderer()
        self.curriculum = CurriculumTracker()
        self.pool_size = max(1, pool_size)
        self.selection_strategy = selection_strategy
        self.parent_selection_strategy = parent_selection_strategy
        self.refill_enabled = refill_enabled
        self.refill_interval_s = max(0.25, refill_interval_s)
        self.generation_retries = max(1, generation_retries)
        self.live_admission_enabled = live_admission_enabled
        self.teardown_booted_projects = teardown_booted_projects
        self.compose_runner = compose_runner or ComposeProjectRunner()
        self.enable_patch_validation = enable_patch_validation
        self.live_validator = live_validator or (
            _default_live_validator(include_patchability=enable_patch_validation)
            if live_admission_enabled
            else None
        )

        self._lock = threading.RLock()
        self._refill_thread: threading.Thread | None = None
        self._stop_event = threading.Event()
        self._started = False
        self._generation_counter = 0
        self._recent_acquisitions: list[str] = []

    @classmethod
    def from_env(cls) -> "ManagedSnapshotRuntime":
        profile = _normalize_validator_profile(
            os.getenv(
                "OPENRANGE_RUNTIME_VALIDATOR_PROFILE",
                _DEFAULT_MANAGED_VALIDATOR_PROFILE,
            )
        )
        live_admission_enabled = _env_flag(
            "OPENRANGE_ENABLE_LIVE_ADMISSION",
            default=_DEFAULT_MANAGED_LIVE_ADMISSION,
        )
        if not _strict_admission_enabled(profile, live_admission_enabled):
            message = _managed_admission_failure_message(profile, live_admission_enabled)
            opt_out_env = _non_live_opt_out_env_name()
            if opt_out_env:
                logger.warning(
                    "%s Explicit opt-out enabled via %s=1.",
                    message,
                    opt_out_env,
                )
            else:
                raise RuntimeError(
                    f"{message} Set {_ALLOW_NON_LIVE_ADMISSION_ENV}=1 to explicitly opt out."
                )

        return cls(
            manifest_path=os.getenv("OPENRANGE_RUNTIME_MANIFEST"),
            store_dir=os.getenv("OPENRANGE_SNAPSHOT_DIR"),
            validator_profile=profile,
            allow_insecure_offline_profile=_non_live_opt_out_enabled(),
            pool_size=_env_int("OPENRANGE_SNAPSHOT_POOL_SIZE", 3),
            selection_strategy=os.getenv("OPENRANGE_SNAPSHOT_SELECTION", "random"),
            parent_selection_strategy=os.getenv("OPENRANGE_PARENT_SELECTION", "policy"),
            refill_enabled=_env_flag("OPENRANGE_ENABLE_MANAGED_REFILL", default=False),
            refill_interval_s=float(os.getenv("OPENRANGE_REFILL_INTERVAL_S", "2.0")),
            generation_retries=_env_int("OPENRANGE_GENERATION_RETRIES", 3),
            live_admission_enabled=live_admission_enabled,
            teardown_booted_projects=not _env_flag(
                "OPENRANGE_KEEP_BOOTED_VALIDATION_STACKS",
                default=False,
            ),
            enable_patch_validation=_env_flag(
                "OPENRANGE_ENABLE_PATCH_VALIDATION",
                default=False,
            ),
            persisted_snapshot_validation=os.getenv(
                "OPENRANGE_PERSISTED_SNAPSHOT_VALIDATION",
                "offline",
            ),
        )

    def _enforce_validator_profile_policy(self) -> None:
        if self.validator_profile in _LIVE_VALIDATOR_PROFILES:
            return

        warning = (
            "ManagedSnapshotRuntime validator profile is set to "
            f"{self.validator_profile!r}; container-backed admission checks are disabled "
            "(build_boot, exploitability, patchability, evidence, reward_grounding, "
            "isolation, difficulty, npc_consistency, realism_review)."
        )
        if not self.allow_insecure_offline_profile:
            raise RuntimeError(
                warning
                + " Set OPENRANGE_RUNTIME_VALIDATOR_PROFILE=training for strict admission, "
                + "or set OPENRANGE_ALLOW_NON_LIVE_ADMISSION=1 "
                + "(legacy alias: OPENRANGE_ALLOW_OFFLINE_ADMISSION=1) "
                + "to explicitly opt out."
            )
        logger.warning(
            "%s Running with explicit opt-out.",
            warning,
        )

    @staticmethod
    def _load_manifest(path: Path) -> dict[str, Any]:
        with path.open("r", encoding="utf-8") as handle:
            data = yaml.safe_load(handle) or {}
        if not isinstance(data, dict):
            raise TypeError(f"Manifest at {path} did not parse to a mapping")
        return data

    def start(self) -> None:
        with self._lock:
            if self._started:
                return

            existing = self.snapshot_count()
            if existing < self.pool_size:
                self._top_up_pool(self.pool_size - existing)
            self._ensure_existing_artifacts()
            self._revalidate_persisted_snapshots()

            available = self.snapshot_count()
            if available == 0:
                raise RuntimeError(
                    "ManagedSnapshotRuntime could not load or generate any admitted snapshots"
                )

            if self.refill_enabled:
                self._stop_event.clear()
                self._refill_thread = threading.Thread(
                    target=self._refill_loop,
                    name="openrange-runtime-refill",
                    daemon=True,
                )
                self._refill_thread.start()

            self._started = True
            logger.info(
                "ManagedSnapshotRuntime started with %d admitted snapshot(s) in %s",
                available,
                self.store_dir,
            )

    def stop(self) -> None:
        with self._lock:
            if not self._started:
                return
            self._stop_event.set()
            thread = self._refill_thread
            self._refill_thread = None
            self._started = False

        if thread is not None:
            thread.join(timeout=self.refill_interval_s * 2)

    def acquire_snapshot(self, *, snapshot_id: str | None = None) -> RuntimeSnapshot:
        self.start()
        if snapshot_id:
            result = self.get_snapshot(snapshot_id)
            self._track_acquisition(result.snapshot_id)
            return result

        stored = _run_coro_sync(self.store.select_entry(strategy=self.selection_strategy))

        # Diversity check: if candidate's vuln types completely overlap with the
        # last 3 acquired snapshots, try to find an alternative.
        if self._recent_acquisitions and not self._is_diverse(stored.snapshot):
            alternative = self._find_diverse_snapshot(stored.snapshot_id)
            if alternative is not None:
                stored = alternative

        self._assert_persisted_snapshot_valid(stored.snapshot_id, stored.snapshot)
        result = RuntimeSnapshot(snapshot_id=stored.snapshot_id, snapshot=stored.snapshot)
        self._track_acquisition(result.snapshot_id)
        return result

    def _track_acquisition(self, snapshot_id: str) -> None:
        """Record a snapshot acquisition, keeping at most 10 entries."""
        self._recent_acquisitions.append(snapshot_id)
        if len(self._recent_acquisitions) > 10:
            del self._recent_acquisitions[: len(self._recent_acquisitions) - 10]

    def _recent_vuln_types(self) -> set[str]:
        """Collect vuln types from the last 3 acquired snapshots."""
        recent_ids = self._recent_acquisitions[-3:]
        if not recent_ids:
            return set()

        entries = _run_coro_sync(self.store.list_entries())
        by_id = {entry.snapshot_id: entry for entry in entries}
        vuln_types: set[str] = set()
        for sid in recent_ids:
            entry = by_id.get(sid)
            if entry:
                vuln_types.update(v.type for v in entry.snapshot.truth_graph.vulns)
        return vuln_types

    def _is_diverse(self, snapshot: SnapshotSpec) -> bool:
        """Return True if *snapshot* has at least one vuln type not in recent history."""
        recent = self._recent_vuln_types()
        if not recent:
            return True
        candidate_vulns = {v.type for v in snapshot.truth_graph.vulns}
        if not candidate_vulns:
            return True
        # Diverse if at least one vuln type is NOT in the recent set
        return not candidate_vulns.issubset(recent)

    def _find_diverse_snapshot(
        self, exclude_id: str
    ) -> "StoredSnapshot | None":
        """Try to find a snapshot in the store whose vulns don't fully overlap."""
        from open_range.builder.snapshot_store import StoredSnapshot

        entries = _run_coro_sync(self.store.list_entries())
        recent = self._recent_vuln_types()

        for entry in entries:
            sid = entry.snapshot_id
            if sid == exclude_id:
                continue
            candidate_vulns = {v.type for v in entry.snapshot.truth_graph.vulns}
            if not candidate_vulns or not candidate_vulns.issubset(recent):
                return entry
        return None

    def get_snapshot(self, snapshot_id: str) -> RuntimeSnapshot:
        self.start()
        stored = _run_coro_sync(self.store.get_entry(snapshot_id))
        self._assert_persisted_snapshot_valid(stored.snapshot_id, stored.snapshot)
        return RuntimeSnapshot(snapshot_id=stored.snapshot_id, snapshot=stored.snapshot)

    def list_snapshots(self) -> list[dict[str, Any]]:
        return _run_coro_sync(self.store.list_snapshots())

    def snapshot_count(self) -> int:
        return int(_run_coro_sync(self.store.count_entries()))

    def status(self) -> dict[str, Any]:
        return {
            "manifest_path": str(self.manifest_path),
            "store_dir": str(self.store_dir),
            "pool_size": self.pool_size,
            "selection_strategy": self.selection_strategy,
            "parent_selection_strategy": self.parent_selection_strategy,
            "validator_profile": self.validator_profile,
            "allow_insecure_offline_profile": self.allow_insecure_offline_profile,
            "persisted_snapshot_validation": self.persisted_snapshot_validation,
            "refill_enabled": self.refill_enabled,
            "live_admission_enabled": self.live_admission_enabled,
            "snapshot_count": self.snapshot_count(),
            "started": self._started,
        }

    def record_episode_result(
        self,
        *,
        snapshot_id: str | None,
        snapshot: SnapshotSpec | None,
        state: RangeState,
        red_history: list[dict[str, Any]],
        blue_history: list[dict[str, Any]],
        completed: bool,
    ) -> None:
        if snapshot is None:
            return

        from open_range.server.rewards import _is_grounded_finding

        total_flags = len(snapshot.flags)
        red_solved = total_flags > 0 and len(state.flags_found) >= total_flags
        red_actions = [
            record
            for record in red_history
            if record.get("type") not in ("hallucinated_flag", "evidence")
        ]
        blue_detected = any(
            record.get("type") == "finding"
            and _is_grounded_finding(record, red_actions)
            for record in blue_history
        )
        weak_areas = []
        if not red_solved:
            weak_areas = [v.type for v in snapshot.truth_graph.vulns]

        self.curriculum.record(
            EpisodeOutcome(
                snapshot_id=snapshot_id,
                red_solved=red_solved,
                blue_detected=blue_detected,
                steps=state.step_count,
                weak_areas=weak_areas,
                completed=completed,
            )
        )

    def _refill_loop(self) -> None:
        while not self._stop_event.wait(self.refill_interval_s):
            try:
                missing = self.pool_size - self.snapshot_count()
                if missing > 0:
                    self._top_up_pool(missing)
            except Exception as exc:  # noqa: BLE001
                logger.warning("ManagedSnapshotRuntime refill failed: %s", exc)

    def _top_up_pool(self, missing: int) -> None:
        for _ in range(max(0, missing)):
            self._generate_and_store_snapshot()

    def _ensure_existing_artifacts(self) -> None:
        for stored in _run_coro_sync(self.store.list_entries()):
            snapshot_id = stored.snapshot_id
            artifacts_dir = self._artifacts_dir(snapshot_id)
            if artifacts_dir.exists():
                continue
            materialized = self._materialize_snapshot(stored.snapshot, snapshot_id)
            _run_coro_sync(self.store.store(materialized, snapshot_id=snapshot_id))

    def _revalidate_persisted_snapshots(self) -> None:
        if self.persisted_snapshot_validation == "trust":
            return
        for entry in _run_coro_sync(self.store.list_entries()):
            self._assert_persisted_snapshot_valid(entry.snapshot_id, entry.snapshot)

    def _assert_persisted_snapshot_valid(self, snapshot_id: str, snapshot: SnapshotSpec) -> None:
        if self.persisted_validator is None:
            return
        result = _run_coro_sync(self.persisted_validator.validate(snapshot, ContainerSet()))
        if result.passed:
            return
        raise RuntimeError(
            "persisted snapshot failed startup revalidation "
            f"({snapshot_id}): {self._validation_error(result)}"
        )

    def _generate_and_store_snapshot(self) -> str:
        last_error: str | None = None

        for attempt in range(1, self.generation_retries + 1):
            context = self._build_context()
            parent_entry = self._select_parent_entry(context)
            snapshot = _run_coro_sync(
                self.mutator.mutate(
                    self.manifest,
                    context=context,
                    error={"message": last_error} if last_error else None,
                    parent_snapshot=parent_entry.snapshot if parent_entry else None,
                    parent_snapshot_id=parent_entry.snapshot_id if parent_entry else None,
                )
            )
            validation = self._validate_snapshot(snapshot)
            if validation.passed:
                snapshot_id = self._snapshot_id(snapshot)
                materialized = self._materialize_snapshot(snapshot, snapshot_id)
                if self.live_admission_enabled:
                    self._run_live_admission(materialized, snapshot_id)
                    topology = dict(materialized.topology)
                    topology["live_validated"] = True
                    materialized.topology = topology
                snapshot_id = _run_coro_sync(
                    self.store.store(materialized, snapshot_id=snapshot_id)
                )
                logger.info(
                    "ManagedSnapshotRuntime admitted snapshot %s on attempt %d",
                    snapshot_id,
                    attempt,
                )
                return snapshot_id

            last_error = self._validation_error(validation)
            logger.warning(
                "ManagedSnapshotRuntime rejected candidate on attempt %d: %s",
                attempt,
                last_error,
            )

        raise RuntimeError(
            "ManagedSnapshotRuntime failed to admit a snapshot after "
            f"{self.generation_retries} attempt(s): {last_error}"
        )

    def _build_context(self) -> BuildContext:
        seed = self._generation_counter
        self._generation_counter += 1
        base_tier = int(self.manifest.get("tier", 1) or 1)

        # Curriculum progression: if the red agent has been solving at a high
        # rate over the last 10 completed episodes, bump the effective tier.
        tier = base_tier
        completed = [o for o in self.curriculum.history if o.completed]
        recent_completed = completed[-10:]
        if len(recent_completed) >= 10:
            recent_solve_rate = sum(
                1 for o in recent_completed if o.red_solved
            ) / len(recent_completed)
            if recent_solve_rate > 0.8:
                tier = min(base_tier + 1, 5)

        context = self.curriculum.build_context(seed=seed, tier=tier)
        context.episode_count = self.mutator.episode_count
        if self.live_admission_enabled:
            context.narrative_hints.append("prefer_live_admission_compatible_vulns")
        return context

    def _validate_snapshot(self, snapshot: SnapshotSpec) -> ValidationResult:
        if self.validator_profile not in _LIVE_VALIDATOR_PROFILES:
            return _run_coro_sync(self.validator.validate(snapshot, ContainerSet()))
        return self._validate_snapshot_live(snapshot)

    def _validate_snapshot_live(self, snapshot: SnapshotSpec) -> ValidationResult:
        snapshot_id = self._snapshot_id(snapshot)
        project_name = self._project_name(snapshot_id)

        with tempfile.TemporaryDirectory(prefix=f"openrange-validate-{snapshot_id}-") as temp_dir:
            snapshot_dir = Path(temp_dir)
            rendered = snapshot.model_copy(deep=True)
            topology = dict(rendered.topology)
            topology["snapshot_id"] = snapshot_id
            rendered.topology = topology
            self.renderer.render(rendered, snapshot_dir)
            compose_path = snapshot_dir / "docker-compose.yml"
            rendered.compose = yaml.safe_load(compose_path.read_text(encoding="utf-8")) or {}

            project: BootedSnapshotProject | None = None
            try:
                project = self.compose_runner.boot(
                    snapshot_id=snapshot_id,
                    artifacts_dir=snapshot_dir,
                    compose=rendered.compose,
                    project_name=project_name,
                )
            except Exception as exc:  # noqa: BLE001
                self._best_effort_teardown_validation_project(
                    snapshot_dir=snapshot_dir,
                    project_name=project_name,
                )
                return ValidationResult(
                    passed=False,
                    checks=[
                        CheckResult(
                            name="build_boot",
                            passed=False,
                            error=str(exc),
                        )
                    ],
                )

            try:
                self._deploy_snapshot_artifacts(rendered, project.containers, snapshot_dir)
                return _run_coro_sync(self.validator.validate(rendered, project.containers))
            except Exception as exc:  # noqa: BLE001
                return ValidationResult(
                    passed=False,
                    checks=[
                        CheckResult(
                            name="live_validation",
                            passed=False,
                            error=str(exc),
                        )
                    ],
                )
            finally:
                if project is not None:
                    try:
                        self.compose_runner.teardown(project)
                    except Exception:  # noqa: BLE001
                        logger.warning(
                            "Failed to tear down validation project %s",
                            project.project_name,
                        )

    def _best_effort_teardown_validation_project(
        self,
        *,
        snapshot_dir: Path,
        project_name: str,
    ) -> None:
        """Tear down a failed validation project when boot aborts mid-flight."""
        compose_file = snapshot_dir / "docker-compose.yml"
        try:
            self.compose_runner.teardown(
                BootedSnapshotProject(
                    project_name=project_name,
                    compose_file=compose_file,
                    artifacts_dir=snapshot_dir,
                    containers=ContainerSet(project_name=project_name),
                )
            )
        except Exception:  # noqa: BLE001
            logger.warning(
                "Failed to tear down validation project %s after boot failure",
                project_name,
            )

    def _project_name(self, snapshot_id: str) -> str:
        safe = "".join(ch if ch.isalnum() else "-" for ch in snapshot_id.lower()).strip("-")
        safe = safe[:40] or "snapshot"
        return f"openrange-{safe}"

    def _compose_up(
        self,
        snapshot_dir: Path,
        compose_file: Path,
        project_name: str,
    ) -> ValidationResult | None:
        try:
            proc = sp.run(
                [
                    "docker",
                    "compose",
                    "-p",
                    project_name,
                    "-f",
                    str(compose_file),
                    "up",
                    "-d",
                    "--build",
                ],
                cwd=str(snapshot_dir),
                capture_output=True,
                text=True,
                timeout=300,
                check=False,
            )
        except FileNotFoundError as exc:
            return ValidationResult(
                passed=False,
                checks=[CheckResult(name="build_boot", passed=False, error=str(exc))],
            )
        except sp.TimeoutExpired:
            return ValidationResult(
                passed=False,
                checks=[
                    CheckResult(
                        name="build_boot",
                        passed=False,
                        error="docker compose up timed out after 300s",
                    )
                ],
            )

        if proc.returncode != 0:
            error = (proc.stderr or proc.stdout or "").strip() or "docker compose up failed"
            return ValidationResult(
                passed=False,
                checks=[CheckResult(name="build_boot", passed=False, error=error)],
            )
        return None

    def _compose_down(self, snapshot_dir: Path, compose_file: Path, project_name: str) -> None:
        try:
            sp.run(
                [
                    "docker",
                    "compose",
                    "-p",
                    project_name,
                    "-f",
                    str(compose_file),
                    "down",
                    "-v",
                    "--remove-orphans",
                ],
                cwd=str(snapshot_dir),
                capture_output=True,
                text=True,
                timeout=120,
                check=False,
            )
        except Exception:  # noqa: BLE001
            logger.warning("Failed to tear down validation project %s", project_name)

    def _discover_containers(self, project_name: str) -> ContainerSet:
        proc = sp.run(
            [
                "docker",
                "ps",
                "--filter",
                f"label=com.docker.compose.project={project_name}",
                "--format",
                "{{.Label \"com.docker.compose.service\"}} {{.Names}}",
            ],
            capture_output=True,
            text=True,
            timeout=30,
            check=False,
        )
        if proc.returncode != 0:
            raise RuntimeError(proc.stderr.strip() or "docker ps failed")

        container_ids: dict[str, str] = {}
        for line in proc.stdout.splitlines():
            service, _, container_name = line.partition(" ")
            if service and container_name:
                container_ids[service.strip()] = container_name.strip()

        if not container_ids:
            raise RuntimeError(f"no running containers found for project {project_name}")
        return ContainerSet(project_name=project_name, container_ids=container_ids)

    @staticmethod
    def _mysql_credentials(snapshot: SnapshotSpec) -> str:
        """Build MySQL CLI credential flags from the snapshot topology.

        Searches ``topology["users"]`` for a user whose ``hosts`` list
        contains ``"db"``.  Returns ``-u <user> -p<password>`` for the
        first match, or ``-u root`` (no password) as a safe fallback.
        """
        if isinstance(snapshot.topology, dict):
            users = snapshot.topology.get("users", [])
            for user in users:
                hosts = user.get("hosts", [])
                if "db" in hosts:
                    uname = user.get("username", "root")
                    pwd = user.get("password", "")
                    if pwd:
                        return f"-u {uname} -p{pwd}"
                    return f"-u {uname}"
        return "-u root"

    def _deploy_snapshot_artifacts(
        self,
        snapshot: SnapshotSpec,
        containers: ContainerSet,
        snapshot_dir: Path,
    ) -> None:
        _run_coro_sync(self._deploy_snapshot_artifacts_async(snapshot, containers, snapshot_dir))

    async def _deploy_snapshot_artifacts_async(
        self,
        snapshot: SnapshotSpec,
        containers: ContainerSet,
        snapshot_dir: Path,
    ) -> None:
        if not snapshot.files:
            return

        for key, content in snapshot.files.items():
            if key == "db:sql":
                sql_file = snapshot_dir / "_snapshot.sql"
                sql_file.write_text(content, encoding="utf-8")
                try:
                    await containers.cp("db", str(sql_file), "/tmp/_snapshot.sql")
                    mysql_creds = self._mysql_credentials(snapshot)
                    await containers.exec(
                        "db",
                        f"mysql {mysql_creds} < /tmp/_snapshot.sql",
                    )
                    await containers.exec("db", "rm -f /tmp/_snapshot.sql")
                finally:
                    sql_file.unlink(missing_ok=True)
                continue

            if ":" not in key:
                logger.warning("Skipping file with bad key format during validation: %s", key)
                continue

            host, path = key.split(":", 1)
            parent_dir = path.rsplit("/", 1)[0] if "/" in path else "/"
            await containers.exec(host, f"mkdir -p {shlex.quote(parent_dir)}")

            temp_file = snapshot_dir / f"_artifact_{host}_{abs(hash(key))}"
            temp_file.write_text(content, encoding="utf-8")
            try:
                await containers.cp(host, str(temp_file), path)
            finally:
                temp_file.unlink(missing_ok=True)

    def _validate_live_snapshot(
        self,
        snapshot: SnapshotSpec,
        containers: ContainerSet,
    ) -> ValidationResult:
        if self.live_validator is None:
            raise RuntimeError("Live validator requested but not configured")
        return _run_coro_sync(self.live_validator.validate(snapshot, containers))

    @staticmethod
    def _validation_error(result: ValidationResult) -> str:
        failed = [check for check in result.checks if not check.passed]
        if not failed:
            return "unknown validation failure"
        payload = [
            {
                "name": check.name,
                "error": check.error,
                "details": check.details,
            }
            for check in failed
        ]
        return json.dumps(payload, sort_keys=True)

    def _snapshot_id(self, snapshot: SnapshotSpec) -> str:
        vuln_types = [v.type for v in snapshot.truth_graph.vulns]
        prefix = "snap_" + "_".join(vuln_types[:3]) if vuln_types else "snap_generated"
        return f"{prefix}_{int(time.time() * 1000)}"

    def _select_parent_entry(self, context: BuildContext):
        if self.snapshot_count() == 0:
            return None
        if self.parent_selection_strategy in {"latest", "random"}:
            return _run_coro_sync(self.store.select_entry(strategy=self.parent_selection_strategy))
        entries = _run_coro_sync(self.store.list_entries())
        if not entries:
            return None
        rng = random.Random(context.seed if context.seed is not None else self._generation_counter)
        selected, score = self.mutation_policy.select_parent(
            entries,
            context=context,
            snapshot_stats=self.curriculum.snapshot_stats(),
            rng=rng,
        )
        logger.info(
            "ManagedSnapshotRuntime selected parent %s via %s %s",
            selected.snapshot_id,
            self.mutation_policy.name,
            json.dumps(score.log_payload(), sort_keys=True),
        )
        return selected

    def _snapshot_dir(self, snapshot_id: str) -> Path:
        return self.store_dir / snapshot_id

    def _artifacts_dir(self, snapshot_id: str) -> Path:
        return self._snapshot_dir(snapshot_id) / "artifacts"

    def _materialize_snapshot(
        self,
        snapshot: SnapshotSpec,
        snapshot_id: str,
    ) -> SnapshotSpec:
        rendered = snapshot.model_copy(deep=True)
        rendered.lineage = rendered.lineage.model_copy(deep=True)
        rendered.lineage.snapshot_id = snapshot_id
        if not rendered.lineage.root_snapshot_id:
            rendered.lineage.root_snapshot_id = snapshot_id

        topology = dict(rendered.topology)
        topology["snapshot_id"] = snapshot_id
        rendered.topology = topology
        rendered.lineage.snapshot_id = snapshot_id
        if not rendered.lineage.root_snapshot_id:
            rendered.lineage.root_snapshot_id = snapshot_id

        snapshot_dir = self._snapshot_dir(snapshot_id)
        artifacts_dir = self._artifacts_dir(snapshot_id)
        if artifacts_dir.exists():
            shutil.rmtree(artifacts_dir)
        artifacts_dir.mkdir(parents=True, exist_ok=True)

        self.renderer.render(rendered, artifacts_dir)

        compose_path = artifacts_dir / "docker-compose.yml"
        rendered.compose = yaml.safe_load(compose_path.read_text(encoding="utf-8")) or {}
        return rendered

    def activate_snapshot_project(
        self,
        *,
        snapshot_id: str,
        snapshot: SnapshotSpec,
        episode_id: str | None = None,
    ) -> BootedSnapshotProject:
        """Boot a fresh per-episode project for an admitted snapshot.

        This is the runtime-facing execution path used by RangeEnvironment.
        It keeps episode state isolated by booting a new compose project from
        the admitted artifact bundle rather than layering files onto a
        long-lived shared stack.
        """
        self.start()

        materialized = snapshot
        artifacts_dir = self._artifacts_dir(snapshot_id)
        if not artifacts_dir.exists():
            materialized = self._materialize_snapshot(snapshot, snapshot_id)

        project_name_seed = snapshot_id
        if episode_id:
            project_name_seed = f"{snapshot_id}-{episode_id}"
        project_name = self.compose_runner.project_name_for(project_name_seed)

        project: BootedSnapshotProject | None = None
        try:
            project = self.compose_runner.boot(
                snapshot_id=snapshot_id,
                artifacts_dir=artifacts_dir,
                compose=materialized.compose,
                project_name=project_name,
            )
            self._apply_rendered_payloads(snapshot_id, project.containers, materialized)
            return project
        except Exception:
            if project is not None:
                try:
                    self.compose_runner.teardown(project)
                except Exception:  # noqa: BLE001
                    logger.warning(
                        "Failed to tear down project %s after activation failure",
                        project.project_name,
                    )
            raise

    def teardown_snapshot_project(self, project: BootedSnapshotProject) -> None:
        """Tear down a previously activated episode project."""
        self.compose_runner.teardown(project)

    def _run_live_admission(self, snapshot: SnapshotSpec, snapshot_id: str) -> None:
        project: BootedSnapshotProject | None = None
        try:
            project = self.compose_runner.boot(
                snapshot_id=snapshot_id,
                artifacts_dir=self._artifacts_dir(snapshot_id),
                compose=snapshot.compose,
            )
            snapshot.compose["x-project-name"] = project.project_name
            self._apply_rendered_payloads(snapshot_id, project.containers, snapshot)
            validation = self._validate_live_snapshot(snapshot, project.containers)
            if not validation.passed:
                raise RuntimeError(self._validation_error(validation))
        finally:
            if (
                project is not None
                and self.teardown_booted_projects
            ):
                self.compose_runner.teardown(project)

    def _apply_rendered_payloads(
        self,
        snapshot_id: str,
        containers: ContainerSet,
        snapshot: SnapshotSpec,
    ) -> None:
        manifest_path = self._artifacts_dir(snapshot_id) / PAYLOAD_MANIFEST_NAME
        if not manifest_path.exists():
            return

        payloads = json.loads(manifest_path.read_text(encoding="utf-8"))
        if not isinstance(payloads, dict):
            return

        for file_key, rel_path in payloads.items():
            src = self._artifacts_dir(snapshot_id) / str(rel_path)
            if file_key == "db:sql":
                self._apply_sql_payload(containers, src, snapshot)
                continue

            if ":" not in file_key:
                continue

            container, target_path = file_key.split(":", 1)
            parent_dir = PurePosixPath(target_path).parent.as_posix() or "/"
            _run_coro_sync(containers.exec(container, f"mkdir -p '{parent_dir}'"))
            _run_coro_sync(containers.cp(container, str(src), target_path))

    def _apply_sql_payload(
        self,
        containers: ContainerSet,
        sql_path: Path,
        snapshot: SnapshotSpec,
    ) -> None:
        root_password = self._mysql_root_password(snapshot)
        _run_coro_sync(containers.cp("db", str(sql_path), "/tmp/openrange-generated.sql"))
        _run_coro_sync(
            containers.exec(
                "db",
                (
                    "mysql -u root "
                    f"-p'{root_password}' < /tmp/openrange-generated.sql"
                ),
            )
        )
        _run_coro_sync(containers.exec("db", "rm -f /tmp/openrange-generated.sql"))

    @staticmethod
    def _mysql_root_password(snapshot: SnapshotSpec) -> str:
        db_service = snapshot.compose.get("services", {}).get("db", {})
        environment = db_service.get("environment", {})
        if isinstance(environment, dict):
            return str(environment.get("MYSQL_ROOT_PASSWORD", "r00tP@ss!"))
        if isinstance(environment, list):
            for item in environment:
                text = str(item)
                if text.startswith("MYSQL_ROOT_PASSWORD="):
                    return text.split("=", 1)[1]
        return "r00tP@ss!"