File size: 69,987 Bytes
e2d9760
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
994a063
 
e2d9760
 
994a063
e2d9760
 
 
 
 
 
994a063
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
e2d9760
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
994a063
 
e2d9760
 
 
 
994a063
e2d9760
994a063
 
 
 
 
 
e2d9760
 
 
 
 
994a063
 
 
 
e2d9760
994a063
 
 
 
 
 
e2d9760
994a063
 
 
 
e2d9760
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8a4157c
4ba73c1
 
 
 
 
 
 
8a4157c
 
 
 
 
8ce75de
 
 
 
994a063
8ce75de
 
 
 
 
 
 
 
e2d9760
994a063
 
 
 
 
 
 
 
8a4157c
 
 
 
 
 
 
 
 
 
 
 
994a063
 
 
 
 
8a4157c
4ba73c1
 
 
 
 
8ce75de
 
4ba73c1
994a063
 
 
 
 
 
 
8ce75de
 
 
4ba73c1
 
8ce75de
4ba73c1
8ce75de
 
 
 
 
e2d9760
 
8ce75de
e2d9760
 
 
8ce75de
8a4157c
e2d9760
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
"""
Rhodawk AI β€” Universal Language Runtime Abstraction
=====================================================
Replaces all Python-specific hardcoding in app.py with a pluggable runtime
system. Each LanguageRuntime implementation handles:

  1. detect()           β€” fingerprint a cloned repo to identify its language
  2. setup_env()        β€” install deps, return an EnvConfig (replaces setup_target_venv)
  3. discover_tests()   β€” find test files matching language conventions
  4. run_tests()        β€” execute one test file, return (output, exit_code)
  5. run_sast()         β€” language-specific static analysis on a diff
  6. run_supply_chain() β€” language-specific dep audit on a diff
  7. get_mcp_domains()  β€” docs domains the MCP fetch tool is allowed to hit
  8. get_fix_prompt_instructions() β€” language-aware instructions appended to Aider prompts

RuntimeFactory.for_repo(repo_dir) auto-detects and returns the right runtime.
Fall-through: if detection is ambiguous, PythonRuntime is used as default.

Supported languages
-------------------
  Python     pytest / uv / pip-audit / bandit
  JavaScript jest|mocha|vitest / npm / npm-audit / eslint-security
  TypeScript same as JS + tsc type-check step
  Java       Maven|Gradle / JUnit|TestNG / OWASP dep-check / semgrep-java
  Go         go test / govulncheck / gosec
  Rust       cargo test / cargo-audit / clippy
  Ruby       RSpec|Minitest / bundler / bundle-audit / brakeman

Adding a new language
---------------------
  1. Subclass LanguageRuntime.
  2. Implement all abstract methods.
  3. Append instance to RuntimeFactory._REGISTRY (order matters β€” first match wins).
"""

from __future__ import annotations

import glob
import json
import os
import re
import signal
import shutil
import subprocess
import tempfile
import threading
import time
from abc import ABC, abstractmethod
from dataclasses import dataclass, field
from pathlib import Path
from typing import Optional


_runtime_process_groups: set[int] = set()
_runtime_process_lock = threading.Lock()


def kill_runtime_processes() -> int:
    with _runtime_process_lock:
        groups = list(_runtime_process_groups)
        _runtime_process_groups.clear()
    killed = 0
    for pgid in groups:
        try:
            os.killpg(pgid, signal.SIGTERM)
            killed += 1
        except ProcessLookupError:
            continue
    time.sleep(0.2)
    for pgid in groups:
        try:
            os.killpg(pgid, signal.SIGKILL)
        except ProcessLookupError:
            continue
    return killed


# ──────────────────────────────────────────────────────────────
# SHARED DATA STRUCTURES
# (mirror / extend SastReport + SupplyChainReport from existing modules
#  so app.py doesn't need to import from two places)
# ──────────────────────────────────────────────────────────────

@dataclass
class EnvConfig:
    """Opaque handle returned by setup_env(); passed back into run_tests()."""
    language: str
    test_runner_cmd: list[str]        # base command e.g. ["npx", "jest"]
    env_dir: str                      # venv / node_modules parent / JAVA_HOME etc.
    extra_env: dict[str, str] = field(default_factory=dict)
    metadata: dict = field(default_factory=dict)   # language-specific extras


@dataclass
class RuntimeSastFinding:
    severity: str        # CRITICAL | HIGH | MEDIUM | LOW
    category: str
    line_number: int
    line_content: str
    description: str


@dataclass
class RuntimeSastReport:
    passed: bool
    language: str
    findings: list[RuntimeSastFinding] = field(default_factory=list)
    tool_output: str = ""
    blocked_reason: Optional[str] = None

    def summary(self) -> str:
        if self.passed:
            return f"[{self.language}] SAST PASSED β€” {len(self.findings)} informational finding(s)."
        return f"[{self.language}] SAST BLOCKED β€” {self.blocked_reason}"


@dataclass
class RuntimeSupplyChainReport:
    passed: bool
    language: str
    new_packages: list[str] = field(default_factory=list)
    cve_findings: list[dict] = field(default_factory=list)
    typosquat_findings: list[str] = field(default_factory=list)
    metadata_findings: list[str] = field(default_factory=list)
    blocked_reason: Optional[str] = None

    def summary(self) -> str:
        if self.passed:
            return f"[{self.language}] Supply chain OK. Checked {len(self.new_packages)} package(s)."
        return f"[{self.language}] Supply chain BLOCKED: {self.blocked_reason}"


# ──────────────────────────────────────────────────────────────
# BASE CLASS
# ──────────────────────────────────────────────────────────────

class LanguageRuntime(ABC):
    """Abstract base β€” one concrete subclass per supported language."""

    language: str = "unknown"

    # ── Detection ────────────────────────────────────────────

    @classmethod
    @abstractmethod
    def detect(cls, repo_dir: str) -> bool:
        """
        Return True if this runtime should handle the given repo.
        Called in priority order by RuntimeFactory; first True wins.
        """

    # ── Environment Setup ────────────────────────────────────

    @abstractmethod
    def setup_env(self, repo_dir: str, persistent_dir: str = "/data") -> EnvConfig:
        """
        Install dependencies; return an EnvConfig.
        Must be idempotent β€” called every audit run.
        """

    # ── Test Discovery ───────────────────────────────────────

    @abstractmethod
    def discover_tests(self, repo_dir: str) -> list[str]:
        """
        Return a list of test file paths RELATIVE to repo_dir.
        Paths must be passable directly to run_tests().
        """

    # ── Test Execution ───────────────────────────────────────

    @abstractmethod
    def run_tests(
        self,
        test_path: str,        # relative path from discover_tests()
        repo_dir: str,
        env_config: EnvConfig,
        timeout: int = 120,
    ) -> tuple[str, int]:
        """
        Run a single test file. Return (combined_output, exit_code).
        exit_code 0 means all tests in file passed.
        """

    # ── Security Gates ───────────────────────────────────────

    @abstractmethod
    def run_sast(
        self,
        diff_text: str,
        changed_files: list[str],
        repo_dir: str,
    ) -> RuntimeSastReport:
        """Run static analysis on the AI-generated diff."""

    @abstractmethod
    def run_supply_chain(
        self,
        diff_text: str,
        repo_dir: str,
    ) -> RuntimeSupplyChainReport:
        """Audit dependencies added/changed by the AI diff."""

    # ── Prompt / MCP Helpers ─────────────────────────────────

    @abstractmethod
    def get_mcp_domains(self) -> list[str]:
        """Return allowed documentation domains for the MCP fetch tool."""

    @abstractmethod
    def get_fix_prompt_instructions(
        self,
        test_path: str,
        branch_name: str,
        src_hint: str = "",
    ) -> str:
        """
        Language-specific instruction block appended to Aider prompts.
        Replaces the Python-hardcoded 'fix requirements.txt or src_file' section.
        """

    # ── Shared Subprocess Helper ─────────────────────────────

    @staticmethod
    def _run(
        cmd: list[str],
        cwd: str,
        timeout: int = 300,
        extra_env: dict | None = None,
        raise_on_error: bool = False,
    ) -> tuple[str, int]:
        """Thin safe subprocess wrapper (shell=False enforced)."""
        if isinstance(cmd, str):
            raise TypeError("SECURITY: Use list commands, not strings.")
        env = os.environ.copy()
        # Strip secrets from child process environment
        for secret in [
            "OPENROUTER_API_KEY", "GITHUB_TOKEN", "GITHUB_PERSONAL_ACCESS_TOKEN",
            "TELEGRAM_BOT_TOKEN", "SLACK_WEBHOOK_URL", "RHODAWK_WEBHOOK_SECRET",
        ]:
            env.pop(secret, None)
        if extra_env:
            env.update(extra_env)
        proc = None
        pgid = None
        try:
            proc = subprocess.Popen(
                cmd, shell=False, cwd=cwd,
                stdout=subprocess.PIPE, stderr=subprocess.PIPE,
                text=True, env=env, start_new_session=True,
            )
            try:
                pgid = os.getpgid(proc.pid)
                with _runtime_process_lock:
                    _runtime_process_groups.add(pgid)
            except ProcessLookupError:
                pgid = None
            stdout, stderr = proc.communicate(timeout=timeout)
            output = (stdout or "") + "\n" + (stderr or "")
            if raise_on_error and proc.returncode != 0:
                raise subprocess.CalledProcessError(proc.returncode, cmd)
            return output, proc.returncode
        except FileNotFoundError as e:
            if raise_on_error:
                raise
            return str(e), 127
        except subprocess.TimeoutExpired:
            if proc:
                try:
                    os.killpg(os.getpgid(proc.pid), signal.SIGKILL)
                except ProcessLookupError:
                    pass
                proc.communicate()
            return f"[TIMEOUT after {timeout}s]", 1
        finally:
            if pgid is not None:
                with _runtime_process_lock:
                    _runtime_process_groups.discard(pgid)


# ──────────────────────────────────────────────────────────────
# PYTHON RUNTIME  (refactored from existing app.py behaviour)
# ──────────────────────────────────────────────────────────────

class PythonRuntime(LanguageRuntime):
    language = "python"

    @classmethod
    def detect(cls, repo_dir: str) -> bool:
        markers = [
            "requirements.txt", "setup.py", "setup.cfg",
            "pyproject.toml", "Pipfile", "poetry.lock",
        ]
        return any(os.path.exists(os.path.join(repo_dir, m)) for m in markers)

    def setup_env(self, repo_dir: str, persistent_dir: str = "/data") -> EnvConfig:
        import sys
        # FIX-011: Use a per-repo venv name derived from repo_dir so that switching
        # between target repos never reuses a stale virtualenv with different deps.
        # The old shared "target_venv" caused "[Errno 2] No such file or directory:
        # '/data/target_venv/bin/pip'" when the venv belonged to a different repo.
        import hashlib as _hashlib
        _repo_hash = _hashlib.md5(repo_dir.encode()).hexdigest()[:8]
        venv_dir = os.path.join(persistent_dir, f"target_venv_{_repo_hash}")

        # FIX: guarantee the parent directory exists before uv/venv tries to write into it.
        # In HuggingFace Spaces /data is a mounted volume that may not be pre-created.
        os.makedirs(persistent_dir, exist_ok=True)

        # FIX: redirect uv cache to /tmp which is always writable in HuggingFace Spaces.
        # Without this, uv fails with "Permission denied" on /home/rhodawk/.cache/uv
        # and silently skips package installation, causing all tests to fail on import.
        uv_env = {"UV_CACHE_DIR": "/tmp/uv-cache"}
        uv_bin = shutil.which("uv")

        pytest_bin_check = os.path.join(venv_dir, "bin", "pytest")
        # FIX: if venv exists but pytest is missing, the previous install was broken
        # (e.g. uv pip install silently failed due to cache permission error).
        # Force a clean rebuild so packages are properly installed this run.
        if os.path.exists(venv_dir) and not os.path.exists(pytest_bin_check):
            shutil.rmtree(venv_dir, ignore_errors=True)

        if not os.path.exists(venv_dir):
            if uv_bin:
                out, code = self._run(
                    [uv_bin, "venv", "--python", sys.executable, venv_dir],
                    cwd="/tmp",
                    extra_env=uv_env,
                )
            else:
                out, code = ("uv executable not found", 127)
            if code != 0:
                ui_msg = (
                    f"uv venv failed (exit {code}) β€” falling back to "
                    f"python -m venv. uv output: {out.strip()[:200]}"
                )
                import warnings
                warnings.warn(ui_msg)
                self._run(
                    [sys.executable, "-m", "venv", venv_dir],
                    cwd="/tmp",
                    raise_on_error=True,
                )
                self._run(
                    [os.path.join(venv_dir, "bin", "python"), "-m", "ensurepip", "--upgrade"],
                    cwd="/tmp",
                    raise_on_error=False,
                )

        # FIX-010: Resolve the venv's python interpreter to use as the pip fallback.
        # Using `python -m pip` via the venv's python binary avoids the "No such file or
        # directory: '/data/target_venv/bin/pip'" error seen when a fresh venv was created
        # via `python -m venv` (which includes pip) but the bare pip script is absent.
        venv_python = os.path.join(venv_dir, "bin", "python")

        def _install_deps(args: list[str]) -> bool:
            """Try uv pip install first; fall back to the venv python -m pip on failure."""
            if uv_bin:
                out, code = self._run(
                    [uv_bin, "pip", "install", "--python", venv_dir, "--quiet"] + args,
                    cwd=repo_dir, timeout=600, extra_env=uv_env,
                )
            else:
                out, code = ("uv executable not found", 127)
            if code != 0:
                import warnings
                warnings.warn(f"uv pip install failed (exit {code}) β€” falling back to pip. {out.strip()[:200]}")
                # FIX-010: use venv_python -m pip instead of a bare pip_bin path β€”
                # the `pip` script may be absent in freshly-created stdlib venvs.
                _, pip_code = self._run(
                    [venv_python, "-m", "pip", "install", "--quiet"] + args,
                    cwd=repo_dir, timeout=600,
                )
                return pip_code == 0
            return True

        req_path = os.path.join(repo_dir, "requirements.txt")
        if os.path.exists(req_path):
            _install_deps(["-r", req_path])
        else:
            pyproject = os.path.join(repo_dir, "pyproject.toml")
            if os.path.exists(pyproject):
                _install_deps(["-e", ".[dev,test]"])

        pytest_bin = os.path.join(venv_dir, "bin", "pytest")
        return EnvConfig(
            language="python",
            test_runner_cmd=[pytest_bin],
            env_dir=venv_dir,
        )

    def discover_tests(self, repo_dir: str) -> list[str]:
        patterns = [
            f"{repo_dir}/**/test_*.py",
            f"{repo_dir}/**/*_test.py",
        ]
        found = set()
        for pat in patterns:
            for path in glob.glob(pat, recursive=True):
                found.add(os.path.relpath(path, repo_dir))
        # Exclude .tox / .venv / site-packages
        return sorted(
            p for p in found
            if not any(x in p for x in [".tox", "site-packages", ".venv", "node_modules"])
        )

    def run_tests(self, test_path: str, repo_dir: str, env_config: EnvConfig, timeout: int = 120) -> tuple[str, int]:
        pytest_bin = env_config.test_runner_cmd[0]
        return self._run(
            [pytest_bin, test_path, "-v", "--tb=short"],
            cwd=repo_dir, timeout=timeout,
        )

    def run_sast(self, diff_text: str, changed_files: list[str], repo_dir: str) -> RuntimeSastReport:
        # Delegate to existing sast_gate module to avoid duplication
        try:
            from sast_gate import run_sast_gate
            report = run_sast_gate(diff_text, changed_files, repo_dir)
            findings = [
                RuntimeSastFinding(f.severity, f.category, f.line_number, f.line_content, f.description)
                for f in report.findings
            ]
            return RuntimeSastReport(
                passed=report.passed,
                language="python",
                findings=findings,
                tool_output=report.bandit_output + "\n" + report.semgrep_output,
                blocked_reason=report.blocked_reason,
            )
        except ImportError:
            return RuntimeSastReport(passed=True, language="python", tool_output="[sast_gate not available]")

    def run_supply_chain(self, diff_text: str, repo_dir: str) -> RuntimeSupplyChainReport:
        try:
            from supply_chain import run_supply_chain_gate
            report = run_supply_chain_gate(diff_text, repo_dir)
            return RuntimeSupplyChainReport(
                passed=report.passed,
                language="python",
                new_packages=report.new_packages,
                cve_findings=report.cve_findings,
                typosquat_findings=report.typosquat_findings,
                metadata_findings=report.metadata_findings,
                blocked_reason=report.blocked_reason,
            )
        except ImportError:
            return RuntimeSupplyChainReport(passed=True, language="python")

    def get_mcp_domains(self) -> list[str]:
        return [
            "docs.python.org", "pypi.org", "docs.github.com",
            "packaging.python.org", "peps.python.org",
        ]

    def get_fix_prompt_instructions(self, test_path: str, branch_name: str, src_hint: str = "") -> str:
        src_target = src_hint or "the relevant source file"
        return (
            f"1. Fix '{src_target}' or 'requirements.txt' to make the test pass. "
            f"Do NOT modify test files.\n"
            f"2. Use the 'fetch-docs' MCP tool to look up documentation on "
            f"docs.python.org or pypi.org if needed.\n"
            f"3. Work on branch '{branch_name}'. Commit the minimal fix.\n"
            f"4. The fix must be minimal and must not introduce regressions."
        )


# ──────────────────────────────────────────────────────────────
# JAVASCRIPT / NODE.JS RUNTIME
# ──────────────────────────────────────────────────────────────

class NodeRuntime(LanguageRuntime):
    language = "javascript"

    # Known JS test runners in detection priority order
    _RUNNER_CONFIGS = [
        # (package_key_in_scripts, runner_name, base_cmd)
        ("jest",    "jest",    ["npx", "jest", "--no-coverage", "--forceExit"]),
        ("vitest",  "vitest",  ["npx", "vitest", "run"]),
        ("mocha",   "mocha",   ["npx", "mocha", "--exit"]),
        ("jasmine", "jasmine", ["npx", "jasmine"]),
        ("ava",     "ava",     ["npx", "ava"]),
        ("tap",     "tap",     ["npx", "tap"]),
    ]

    @classmethod
    def detect(cls, repo_dir: str) -> bool:
        pkg = os.path.join(repo_dir, "package.json")
        if not os.path.exists(pkg):
            return False
        try:
            data = json.loads(Path(pkg).read_text())
            # Exclude TypeScript repos β€” TypeScriptRuntime handles them
            return not cls._is_typescript(repo_dir, data)
        except Exception:
            return False

    @classmethod
    def _is_typescript(cls, repo_dir: str, pkg_data: dict) -> bool:
        if os.path.exists(os.path.join(repo_dir, "tsconfig.json")):
            return True
        deps = {**pkg_data.get("dependencies", {}), **pkg_data.get("devDependencies", {})}
        return "typescript" in deps

    def _detect_runner(self, repo_dir: str) -> tuple[str, list[str]]:
        pkg_path = os.path.join(repo_dir, "package.json")
        try:
            data = json.loads(Path(pkg_path).read_text())
        except Exception:
            return "jest", ["npx", "jest", "--no-coverage", "--forceExit"]

        all_deps = {
            **data.get("dependencies", {}),
            **data.get("devDependencies", {}),
        }
        scripts = data.get("scripts", {})
        test_script = scripts.get("test", "")

        for key, name, cmd in self._RUNNER_CONFIGS:
            if key in all_deps or key in test_script:
                return name, cmd

        return "jest", ["npx", "jest", "--no-coverage", "--forceExit"]

    def setup_env(self, repo_dir: str, persistent_dir: str = "/data") -> EnvConfig:
        pkg_lock = os.path.join(repo_dir, "package-lock.json")
        yarn_lock = os.path.join(repo_dir, "yarn.lock")
        pnpm_lock = os.path.join(repo_dir, "pnpm-lock.yaml")

        if os.path.exists(pnpm_lock):
            self._run(["pnpm", "install", "--frozen-lockfile", "--prefer-offline"],
                      cwd=repo_dir, timeout=600)
        elif os.path.exists(yarn_lock):
            self._run(["yarn", "install", "--frozen-lockfile"],
                      cwd=repo_dir, timeout=600)
        else:
            self._run(["npm", "ci", "--prefer-offline"],
                      cwd=repo_dir, timeout=600)

        runner_name, runner_cmd = self._detect_runner(repo_dir)
        return EnvConfig(
            language=self.language,
            test_runner_cmd=runner_cmd,
            env_dir=os.path.join(repo_dir, "node_modules"),
            metadata={"runner": runner_name},
        )

    def discover_tests(self, repo_dir: str) -> list[str]:
        patterns = [
            f"{repo_dir}/**/*.test.js",
            f"{repo_dir}/**/*.spec.js",
            f"{repo_dir}/**/__tests__/**/*.js",
        ]
        found = set()
        for pat in patterns:
            for path in glob.glob(pat, recursive=True):
                found.add(os.path.relpath(path, repo_dir))
        return sorted(
            p for p in found
            if "node_modules" not in p and ".min." not in p
        )

    def run_tests(self, test_path: str, repo_dir: str, env_config: EnvConfig, timeout: int = 120) -> tuple[str, int]:
        runner = env_config.metadata.get("runner", "jest")
        cmd = env_config.test_runner_cmd[:]

        if runner == "jest":
            # Jest wants the file path as the last arg, using --testPathPattern for safety
            cmd += ["--testPathPattern", re.escape(test_path)]
        elif runner == "vitest":
            cmd += [test_path]
        elif runner == "mocha":
            cmd += [test_path]
        else:
            cmd += [test_path]

        return self._run(cmd, cwd=repo_dir, timeout=timeout)

    def run_sast(self, diff_text: str, changed_files: list[str], repo_dir: str) -> RuntimeSastReport:
        findings = []
        # 1. Universal secret patterns (from existing sast_gate logic)
        findings.extend(self._scan_secrets(diff_text))
        # 2. JS-specific dangerous patterns
        findings.extend(self._scan_js_patterns(diff_text))
        # 3. Semgrep with JS/TS ruleset
        tool_output = self._run_semgrep(changed_files, repo_dir)

        critical = [f for f in findings if f.severity == "CRITICAL"]
        high = [f for f in findings if f.severity == "HIGH"]
        blocked_reason = None
        if critical:
            blocked_reason = f"CRITICAL: {critical[0].description}"
        elif len(high) >= 3:
            blocked_reason = f"{len(high)} HIGH severity findings exceed threshold"

        return RuntimeSastReport(
            passed=blocked_reason is None,
            language=self.language,
            findings=findings,
            tool_output=tool_output,
            blocked_reason=blocked_reason,
        )

    def _scan_secrets(self, diff_text: str) -> list[RuntimeSastFinding]:
        """Reuse the universal secret patterns from sast_gate."""
        try:
            from sast_gate import _scan_diff_for_secrets, _SECRET_PATTERNS, _DANGEROUS_PATTERNS
            raw = _scan_diff_for_secrets(diff_text)
            return [
                RuntimeSastFinding(f.severity, f.category, f.line_number, f.line_content, f.description)
                for f in raw
            ]
        except ImportError:
            return []

    _JS_DANGEROUS = [
        (re.compile(r'\beval\s*\('), "CRITICAL", "eval() call β€” arbitrary code execution"),
        (re.compile(r'\bnew\s+Function\s*\('), "CRITICAL", "new Function() β€” dynamic code eval"),
        (re.compile(r'\bchild_process\.exec\s*\('), "HIGH", "child_process.exec β€” shell injection risk"),
        (re.compile(r'\brequire\s*\(\s*[`\'"]\.\.'), "HIGH", "Path-traversal require()"),
        (re.compile(r'innerHTML\s*=\s*'), "HIGH", "innerHTML assignment β€” XSS risk"),
        (re.compile(r'document\.write\s*\('), "HIGH", "document.write() β€” XSS risk"),
        (re.compile(r'__proto__'), "HIGH", "Prototype pollution vector"),
        (re.compile(r'\.prototype\['), "HIGH", "Prototype pollution vector"),
        (re.compile(r'dangerouslySetInnerHTML'), "HIGH", "dangerouslySetInnerHTML β€” XSS risk"),
    ]

    def _scan_js_patterns(self, diff_text: str) -> list[RuntimeSastFinding]:
        findings = []
        for i, line in enumerate(diff_text.splitlines(), 1):
            if not line.startswith("+"):
                continue
            for pattern, severity, description in self._JS_DANGEROUS:
                if pattern.search(line):
                    findings.append(RuntimeSastFinding(
                        severity=severity, category="js-security",
                        line_number=i, line_content=line[1:].strip(),
                        description=description,
                    ))
        return findings

    def _run_semgrep(self, changed_files: list[str], repo_dir: str) -> str:
        js_files = [f for f in changed_files if f.endswith((".js", ".jsx", ".mjs", ".cjs"))]
        if not js_files:
            return ""
        outputs = []
        for rel in js_files:
            abs_path = os.path.join(repo_dir, rel)
            if os.path.exists(abs_path):
                out, _ = self._run(
                    ["semgrep", "--config", "p/javascript", "--quiet", abs_path],
                    cwd=repo_dir, timeout=90,
                )
                outputs.append(out)
        return "\n".join(outputs)

    def run_supply_chain(self, diff_text: str, repo_dir: str) -> RuntimeSupplyChainReport:
        new_packages = self._extract_new_npm_packages(diff_text)
        typosquats = [self._check_npm_typosquat(p) for p in new_packages]
        typosquats = [t for t in typosquats if t]

        if typosquats:
            return RuntimeSupplyChainReport(
                passed=False, language=self.language,
                new_packages=new_packages, typosquat_findings=typosquats,
                blocked_reason=f"Possible typosquatting: {typosquats[0]}",
            )

        cve_findings = self._run_npm_audit(repo_dir)
        critical_cves = [c for c in cve_findings if c.get("severity") == "critical"]
        if critical_cves:
            return RuntimeSupplyChainReport(
                passed=False, language=self.language,
                new_packages=new_packages, cve_findings=cve_findings,
                blocked_reason=f"Critical CVE in npm dep: {critical_cves[0].get('name')}",
            )

        return RuntimeSupplyChainReport(
            passed=True, language=self.language,
            new_packages=new_packages, cve_findings=cve_findings,
        )

    _NPM_TOP_PACKAGES = {
        "express", "react", "vue", "angular", "lodash", "axios", "moment",
        "webpack", "babel", "typescript", "eslint", "jest", "mocha", "chai",
        "next", "nuxt", "gatsby", "fastify", "koa", "hapi", "sequelize",
        "mongoose", "typeorm", "prisma", "socket.io", "passport", "jsonwebtoken",
        "bcrypt", "dotenv", "cors", "helmet", "nodemailer", "multer", "sharp",
        "redis", "ioredis", "pg", "mysql2", "mongodb", "rxjs", "graphql",
        "apollo-server", "nestjs", "redux", "zustand", "vite", "rollup",
    }

    def _extract_new_npm_packages(self, diff_text: str) -> list[str]:
        added = []
        in_pkg = False
        for line in diff_text.splitlines():
            if "package.json" in line:
                in_pkg = True
            if in_pkg and line.startswith("+") and not line.startswith("+++"):
                match = re.search(r'"([@\w][\w\-./]*)"\s*:', line[1:])
                if match:
                    pkg = match.group(1).lstrip("@").split("/")[-1].lower()
                    if pkg not in ("version", "name", "description", "main", "scripts"):
                        added.append(match.group(1))
        return list(dict.fromkeys(added))  # dedup preserving order

    def _check_npm_typosquat(self, package_name: str) -> Optional[str]:
        pkg = package_name.lstrip("@").split("/")[-1].lower().replace("-", "").replace("_", "")
        for known in self._NPM_TOP_PACKAGES:
            known_clean = known.lower().replace("-", "").replace("_", "")
            if pkg == known_clean:
                continue
            if abs(len(pkg) - len(known_clean)) <= 2:
                dist = self._levenshtein(pkg, known_clean)
                if 0 < dist <= 2:
                    return f"'{package_name}' is {dist} edit(s) from '{known}' β€” possible typosquat"
        return None

    @staticmethod
    def _levenshtein(s1: str, s2: str) -> int:
        if len(s1) < len(s2):
            return NodeRuntime._levenshtein(s2, s1)
        if not s2:
            return len(s1)
        prev = list(range(len(s2) + 1))
        for c1 in s1:
            curr = [prev[0] + 1]
            for j, c2 in enumerate(s2):
                curr.append(min(prev[j + 1] + 1, curr[j] + 1, prev[j] + (c1 != c2)))
            prev = curr
        return prev[-1]

    def _run_npm_audit(self, repo_dir: str) -> list[dict]:
        out, code = self._run(
            ["npm", "audit", "--json", "--audit-level=critical"],
            cwd=repo_dir, timeout=120,
        )
        try:
            data = json.loads(out.strip())
            vulns = data.get("vulnerabilities", {})
            results = []
            for name, info in vulns.items():
                results.append({
                    "name": name,
                    "severity": info.get("severity", "unknown"),
                    "via": [v if isinstance(v, str) else v.get("title", "") for v in info.get("via", [])],
                })
            return results
        except Exception:
            return []

    def get_mcp_domains(self) -> list[str]:
        return [
            "nodejs.org", "npmjs.com", "developer.mozilla.org",
            "jestjs.io", "mochajs.org", "vitest.dev",
            "expressjs.com", "docs.github.com",
        ]

    def get_fix_prompt_instructions(self, test_path: str, branch_name: str, src_hint: str = "") -> str:
        src_target = src_hint or "the relevant source file"
        return (
            f"1. Fix '{src_target}' or 'package.json' to make the test pass. "
            f"Do NOT modify test files.\n"
            f"2. Use the 'fetch-docs' MCP tool to look up documentation on "
            f"nodejs.org or npmjs.com if needed.\n"
            f"3. Work on branch '{branch_name}'. Commit the minimal fix.\n"
            f"4. Run 'npm run build' if a build step exists before committing.\n"
            f"5. The fix must be minimal and must not introduce regressions."
        )


# ──────────────────────────────────────────────────────────────
# TYPESCRIPT RUNTIME  (extends NodeRuntime)
# ──────────────────────────────────────────────────────────────

class TypeScriptRuntime(NodeRuntime):
    language = "typescript"

    @classmethod
    def detect(cls, repo_dir: str) -> bool:
        if not os.path.exists(os.path.join(repo_dir, "package.json")):
            return False
        if os.path.exists(os.path.join(repo_dir, "tsconfig.json")):
            return True
        try:
            data = json.loads(Path(os.path.join(repo_dir, "package.json")).read_text())
            deps = {**data.get("dependencies", {}), **data.get("devDependencies", {})}
            return "typescript" in deps
        except Exception:
            return False

    def discover_tests(self, repo_dir: str) -> list[str]:
        patterns = [
            f"{repo_dir}/**/*.test.ts",
            f"{repo_dir}/**/*.spec.ts",
            f"{repo_dir}/**/*.test.tsx",
            f"{repo_dir}/**/*.spec.tsx",
            f"{repo_dir}/**/__tests__/**/*.ts",
            f"{repo_dir}/**/__tests__/**/*.tsx",
        ]
        found = set()
        for pat in patterns:
            for path in glob.glob(pat, recursive=True):
                found.add(os.path.relpath(path, repo_dir))
        return sorted(p for p in found if "node_modules" not in p)

    def setup_env(self, repo_dir: str, persistent_dir: str = "/data") -> EnvConfig:
        env = super().setup_env(repo_dir, persistent_dir)
        env.language = "typescript"
        return env

    def run_sast(self, diff_text: str, changed_files: list[str], repo_dir: str) -> RuntimeSastReport:
        report = super().run_sast(diff_text, changed_files, repo_dir)
        report.language = "typescript"
        # Additional TypeScript-specific: run tsc --noEmit to catch type errors
        ts_errors = self._run_tsc(repo_dir)
        if ts_errors:
            report.tool_output += f"\n\n[tsc --noEmit]\n{ts_errors}"
        return report

    def _run_tsc(self, repo_dir: str) -> str:
        tsc_path = os.path.join(repo_dir, "node_modules", ".bin", "tsc")
        if not os.path.exists(tsc_path):
            return ""
        out, code = self._run(
            [tsc_path, "--noEmit", "--skipLibCheck"],
            cwd=repo_dir, timeout=120,
        )
        return out if code != 0 else ""

    def _run_semgrep(self, changed_files: list[str], repo_dir: str) -> str:
        ts_files = [f for f in changed_files if f.endswith((".ts", ".tsx"))]
        if not ts_files:
            return ""
        outputs = []
        for rel in ts_files:
            abs_path = os.path.join(repo_dir, rel)
            if os.path.exists(abs_path):
                out, _ = self._run(
                    ["semgrep", "--config", "p/typescript", "--quiet", abs_path],
                    cwd=repo_dir, timeout=90,
                )
                outputs.append(out)
        return "\n".join(outputs)

    def get_mcp_domains(self) -> list[str]:
        return super().get_mcp_domains() + [
            "www.typescriptlang.org", "react.dev", "nextjs.org",
        ]

    def get_fix_prompt_instructions(self, test_path: str, branch_name: str, src_hint: str = "") -> str:
        src_target = src_hint or "the relevant TypeScript source file"
        return (
            f"1. Fix '{src_target}' or 'package.json' to make the test pass. "
            f"Do NOT modify test files.\n"
            f"2. Ensure the fix passes 'tsc --noEmit' with no new type errors.\n"
            f"3. Use the 'fetch-docs' MCP tool for typescriptlang.org or npmjs.com if needed.\n"
            f"4. Work on branch '{branch_name}'. Commit the minimal fix.\n"
            f"5. The fix must not introduce regressions or break existing type contracts."
        )


# ──────────────────────────────────────────────────────────────
# JAVA RUNTIME
# ──────────────────────────────────────────────────────────────

class JavaRuntime(LanguageRuntime):
    language = "java"

    @classmethod
    def detect(cls, repo_dir: str) -> bool:
        markers = ["pom.xml", "build.gradle", "build.gradle.kts", "gradlew"]
        return any(os.path.exists(os.path.join(repo_dir, m)) for m in markers)

    def _has_maven(self, repo_dir: str) -> bool:
        return os.path.exists(os.path.join(repo_dir, "pom.xml"))

    def setup_env(self, repo_dir: str, persistent_dir: str = "/data") -> EnvConfig:
        if self._has_maven(repo_dir):
            self._run(
                ["mvn", "dependency:resolve", "-q", "-B"],
                cwd=repo_dir, timeout=600,
            )
            runner_cmd = ["mvn", "test", "-B"]
        else:
            # Gradle
            gradlew = os.path.join(repo_dir, "gradlew")
            gradle_exec = gradlew if os.path.exists(gradlew) else "gradle"
            self._run([gradle_exec, "dependencies", "-q"], cwd=repo_dir, timeout=600)
            runner_cmd = [gradle_exec, "test"]

        return EnvConfig(
            language="java",
            test_runner_cmd=runner_cmd,
            env_dir=repo_dir,
            metadata={"build_system": "maven" if self._has_maven(repo_dir) else "gradle"},
        )

    def discover_tests(self, repo_dir: str) -> list[str]:
        patterns = [
            f"{repo_dir}/**/src/test/**/*Test.java",
            f"{repo_dir}/**/src/test/**/*Tests.java",
            f"{repo_dir}/**/src/test/**/*Spec.java",
            f"{repo_dir}/**/src/test/**/*IT.java",
        ]
        found = set()
        for pat in patterns:
            for path in glob.glob(pat, recursive=True):
                found.add(os.path.relpath(path, repo_dir))
        return sorted(found)

    def run_tests(self, test_path: str, repo_dir: str, env_config: EnvConfig, timeout: int = 300) -> tuple[str, int]:
        build = env_config.metadata.get("build_system", "maven")
        # Extract class name from path: src/test/java/com/example/FooTest.java β†’ com.example.FooTest
        class_name = (
            test_path
            .replace("src/test/java/", "")
            .replace("/", ".")
            .replace(".java", "")
        )
        if build == "maven":
            cmd = ["mvn", "test", "-B", f"-Dtest={class_name}", "-Dsurefire.failIfNoSpecifiedTests=false"]
        else:
            gradlew = os.path.join(repo_dir, "gradlew")
            gradle_exec = gradlew if os.path.exists(gradlew) else "gradle"
            cmd = [gradle_exec, "test", f"--tests", class_name]
        return self._run(cmd, cwd=repo_dir, timeout=timeout)

    def run_sast(self, diff_text: str, changed_files: list[str], repo_dir: str) -> RuntimeSastReport:
        findings = []
        findings.extend(self._scan_secrets(diff_text))
        findings.extend(self._scan_java_patterns(diff_text))
        semgrep_out = self._run_semgrep_java(changed_files, repo_dir)

        critical = [f for f in findings if f.severity == "CRITICAL"]
        high = [f for f in findings if f.severity == "HIGH"]
        blocked_reason = None
        if critical:
            blocked_reason = f"CRITICAL: {critical[0].description}"
        elif len(high) >= 3:
            blocked_reason = f"{len(high)} HIGH severity findings"

        return RuntimeSastReport(
            passed=blocked_reason is None,
            language="java",
            findings=findings,
            tool_output=semgrep_out,
            blocked_reason=blocked_reason,
        )

    def _scan_secrets(self, diff_text: str) -> list[RuntimeSastFinding]:
        try:
            from sast_gate import _scan_diff_for_secrets
            return [
                RuntimeSastFinding(f.severity, f.category, f.line_number, f.line_content, f.description)
                for f in _scan_diff_for_secrets(diff_text)
            ]
        except ImportError:
            return []

    _JAVA_DANGEROUS = [
        (re.compile(r'Runtime\.getRuntime\(\)\.exec\s*\('), "CRITICAL", "Runtime.exec() β€” OS injection risk"),
        (re.compile(r'ProcessBuilder\s*\('), "HIGH", "ProcessBuilder β€” verify no untrusted input"),
        (re.compile(r'ObjectInputStream'), "CRITICAL", "Java deserialization β€” RCE risk"),
        (re.compile(r'ScriptEngine.*eval\s*\('), "CRITICAL", "ScriptEngine.eval() β€” code injection"),
        (re.compile(r'\.createQuery\(.*\+'), "HIGH", "JPQL/HQL string concat β€” injection risk"),
        (re.compile(r'Statement.*execute.*\+'), "HIGH", "SQL concatenation β€” injection risk"),
        (re.compile(r'@SuppressWarnings\("unchecked"\)'), "LOW", "Unchecked cast suppressed"),
        (re.compile(r'System\.exit\s*\('), "MEDIUM", "System.exit() in library code"),
    ]

    def _scan_java_patterns(self, diff_text: str) -> list[RuntimeSastFinding]:
        findings = []
        for i, line in enumerate(diff_text.splitlines(), 1):
            if not line.startswith("+"):
                continue
            for pattern, severity, description in self._JAVA_DANGEROUS:
                if pattern.search(line):
                    findings.append(RuntimeSastFinding(
                        severity=severity, category="java-security",
                        line_number=i, line_content=line[1:].strip(),
                        description=description,
                    ))
        return findings

    def _run_semgrep_java(self, changed_files: list[str], repo_dir: str) -> str:
        java_files = [f for f in changed_files if f.endswith(".java")]
        if not java_files:
            return ""
        outputs = []
        for rel in java_files[:10]:  # cap to avoid timeout on large repos
            abs_path = os.path.join(repo_dir, rel)
            if os.path.exists(abs_path):
                out, _ = self._run(
                    ["semgrep", "--config", "p/java", "--quiet", abs_path],
                    cwd=repo_dir, timeout=90,
                )
                outputs.append(out)
        return "\n".join(outputs)

    def run_supply_chain(self, diff_text: str, repo_dir: str) -> RuntimeSupplyChainReport:
        new_packages = self._extract_new_maven_deps(diff_text)
        cve_out, code = self._run_owasp_check(repo_dir)
        cve_findings = self._parse_owasp_output(cve_out)
        critical_cves = [c for c in cve_findings if c.get("cvssScore", 0) >= 9.0]
        if critical_cves:
            return RuntimeSupplyChainReport(
                passed=False, language="java",
                new_packages=new_packages, cve_findings=cve_findings,
                blocked_reason=f"Critical CVE in Maven dep: {critical_cves[0].get('name')}",
            )
        return RuntimeSupplyChainReport(
            passed=True, language="java",
            new_packages=new_packages, cve_findings=cve_findings,
        )

    def _extract_new_maven_deps(self, diff_text: str) -> list[str]:
        added = []
        in_pom = False
        for line in diff_text.splitlines():
            if "pom.xml" in line or "build.gradle" in line:
                in_pom = True
            if in_pom and line.startswith("+") and not line.startswith("+++"):
                # Maven: <artifactId>foo</artifactId>
                m = re.search(r"<artifactId>\s*([\w\-\.]+)\s*</artifactId>", line)
                if m:
                    added.append(m.group(1))
                # Gradle: implementation 'group:name:version'
                m2 = re.search(r"['\"][\w\.\-]+:([\w\.\-]+):", line)
                if m2:
                    added.append(m2.group(1))
        return list(dict.fromkeys(added))

    def _run_owasp_check(self, repo_dir: str) -> tuple[str, int]:
        if self._has_maven(repo_dir):
            return self._run(
                ["mvn", "org.owasp:dependency-check-maven:check", "-B",
                 "-DfailBuildOnCVSS=9", "-Dformat=JSON"],
                cwd=repo_dir, timeout=300,
            )
        return "", 0

    def _parse_owasp_output(self, raw: str) -> list[dict]:
        try:
            data = json.loads(raw)
            deps = data.get("dependencies", [])
            results = []
            for dep in deps:
                for vuln in dep.get("vulnerabilities", []):
                    results.append({
                        "name": dep.get("fileName", ""),
                        "cve": vuln.get("name", ""),
                        "cvssScore": vuln.get("cvssv3", {}).get("baseScore", 0),
                    })
            return results
        except Exception:
            return []

    def get_mcp_domains(self) -> list[str]:
        return [
            "docs.oracle.com", "mvnrepository.com", "maven.apache.org",
            "docs.spring.io", "docs.github.com", "javadoc.io",
        ]

    def get_fix_prompt_instructions(self, test_path: str, branch_name: str, src_hint: str = "") -> str:
        src_target = src_hint or "the relevant Java source file in src/main/java"
        return (
            f"1. Fix '{src_target}' or 'pom.xml'/'build.gradle' to make the test pass. "
            f"Do NOT modify test files under src/test.\n"
            f"2. Use the 'fetch-docs' MCP tool for docs.spring.io or javadoc.io if needed.\n"
            f"3. Ensure the fix compiles cleanly β€” no raw type warnings, no checked exception swallowing.\n"
            f"4. Work on branch '{branch_name}'. Commit the minimal fix.\n"
            f"5. The fix must not break other tests or change public API signatures."
        )


# ──────────────────────────────────────────────────────────────
# GO RUNTIME
# ──────────────────────────────────────────────────────────────

class GoRuntime(LanguageRuntime):
    language = "go"

    @classmethod
    def detect(cls, repo_dir: str) -> bool:
        return os.path.exists(os.path.join(repo_dir, "go.mod"))

    def setup_env(self, repo_dir: str, persistent_dir: str = "/data") -> EnvConfig:
        self._run(["go", "mod", "download"], cwd=repo_dir, timeout=300)
        return EnvConfig(
            language="go",
            test_runner_cmd=["go", "test"],
            env_dir=repo_dir,
        )

    def discover_tests(self, repo_dir: str) -> list[str]:
        found = set()
        for path in glob.glob(f"{repo_dir}/**/*_test.go", recursive=True):
            found.add(os.path.relpath(path, repo_dir))
        return sorted(p for p in found if "vendor" not in p)

    def run_tests(self, test_path: str, repo_dir: str, env_config: EnvConfig, timeout: int = 120) -> tuple[str, int]:
        # Go tests run per-package; derive the package from the file path
        pkg_dir = os.path.dirname(os.path.join(repo_dir, test_path))
        pkg_rel = "./" + os.path.relpath(pkg_dir, repo_dir)
        return self._run(
            ["go", "test", "-v", "-count=1", "-run", ".", pkg_rel],
            cwd=repo_dir, timeout=timeout,
        )

    def run_sast(self, diff_text: str, changed_files: list[str], repo_dir: str) -> RuntimeSastReport:
        findings = []
        findings.extend(self._scan_secrets(diff_text))
        findings.extend(self._scan_go_patterns(diff_text))
        gosec_out = self._run_gosec(changed_files, repo_dir)

        critical = [f for f in findings if f.severity == "CRITICAL"]
        blocked_reason = f"CRITICAL: {critical[0].description}" if critical else None
        return RuntimeSastReport(
            passed=blocked_reason is None,
            language="go",
            findings=findings,
            tool_output=gosec_out,
            blocked_reason=blocked_reason,
        )

    def _scan_secrets(self, diff_text: str) -> list[RuntimeSastFinding]:
        try:
            from sast_gate import _scan_diff_for_secrets
            return [
                RuntimeSastFinding(f.severity, f.category, f.line_number, f.line_content, f.description)
                for f in _scan_diff_for_secrets(diff_text)
            ]
        except ImportError:
            return []

    _GO_DANGEROUS = [
        (re.compile(r'exec\.Command\s*\('), "HIGH", "exec.Command β€” verify no untrusted input"),
        (re.compile(r'exec\.CommandContext\s*\('), "HIGH", "exec.CommandContext β€” verify no untrusted input"),
        (re.compile(r'sql\.Open.*\+'), "HIGH", "SQL string concat β€” injection risk"),
        (re.compile(r'reflect\.'), "MEDIUM", "reflect usage β€” verify type safety"),
        (re.compile(r'unsafe\.'), "HIGH", "unsafe package β€” memory safety risk"),
        (re.compile(r'encoding/gob'), "HIGH", "gob deserialization β€” potential RCE if untrusted input"),
        (re.compile(r'os\.Setenv\s*\('), "MEDIUM", "os.Setenv in handler β€” env pollution risk"),
    ]

    def _scan_go_patterns(self, diff_text: str) -> list[RuntimeSastFinding]:
        findings = []
        for i, line in enumerate(diff_text.splitlines(), 1):
            if not line.startswith("+"):
                continue
            for pattern, severity, description in self._GO_DANGEROUS:
                if pattern.search(line):
                    findings.append(RuntimeSastFinding(
                        severity=severity, category="go-security",
                        line_number=i, line_content=line[1:].strip(),
                        description=description,
                    ))
        return findings

    def _run_gosec(self, changed_files: list[str], repo_dir: str) -> str:
        go_files = [f for f in changed_files if f.endswith(".go") and not f.endswith("_test.go")]
        if not go_files:
            return ""
        out, _ = self._run(
            ["gosec", "-fmt", "text", "-quiet", "./..."],
            cwd=repo_dir, timeout=120,
        )
        return out

    def run_supply_chain(self, diff_text: str, repo_dir: str) -> RuntimeSupplyChainReport:
        new_mods = self._extract_new_go_modules(diff_text)
        vuln_out, code = self._run(
            ["govulncheck", "./..."],
            cwd=repo_dir, timeout=180,
        )
        cve_findings = self._parse_govulncheck(vuln_out)
        critical_cves = [c for c in cve_findings if "CRITICAL" in c.get("severity", "")]
        if critical_cves:
            return RuntimeSupplyChainReport(
                passed=False, language="go",
                new_packages=new_mods, cve_findings=cve_findings,
                blocked_reason=f"Critical vulnerability: {critical_cves[0].get('id')}",
            )
        return RuntimeSupplyChainReport(
            passed=True, language="go",
            new_packages=new_mods, cve_findings=cve_findings,
        )

    def _extract_new_go_modules(self, diff_text: str) -> list[str]:
        added = []
        for line in diff_text.splitlines():
            if "go.mod" in line or "go.sum" in line:
                continue
            if line.startswith("+") and not line.startswith("+++"):
                m = re.search(r'require\s+([\w\./\-]+)\s+v', line)
                if m:
                    added.append(m.group(1))
        return list(dict.fromkeys(added))

    def _parse_govulncheck(self, raw: str) -> list[dict]:
        findings = []
        for line in raw.splitlines():
            m = re.search(r"(GO-\d+-\d+|CVE-\d+-\d+)", line)
            if m:
                findings.append({"id": m.group(1), "severity": "HIGH", "context": line.strip()})
        return findings

    def get_mcp_domains(self) -> list[str]:
        return [
            "pkg.go.dev", "go.dev", "docs.github.com",
            "golang.org", "blog.golang.org",
        ]

    def get_fix_prompt_instructions(self, test_path: str, branch_name: str, src_hint: str = "") -> str:
        pkg_dir = os.path.dirname(test_path) or "."
        src_target = src_hint or f"the Go source files in '{pkg_dir}'"
        return (
            f"1. Fix '{src_target}' or 'go.mod' to make the test pass. "
            f"Do NOT modify *_test.go files.\n"
            f"2. Run 'go vet ./...' mentally β€” ensure no vet errors are introduced.\n"
            f"3. Use the 'fetch-docs' MCP tool for pkg.go.dev if needed.\n"
            f"4. Work on branch '{branch_name}'. Commit the minimal fix.\n"
            f"5. The fix must not break other packages. Run only the affected package test."
        )


# ──────────────────────────────────────────────────────────────
# RUST RUNTIME
# ──────────────────────────────────────────────────────────────

class RustRuntime(LanguageRuntime):
    language = "rust"

    @classmethod
    def detect(cls, repo_dir: str) -> bool:
        return os.path.exists(os.path.join(repo_dir, "Cargo.toml"))

    def setup_env(self, repo_dir: str, persistent_dir: str = "/data") -> EnvConfig:
        self._run(["cargo", "fetch"], cwd=repo_dir, timeout=300)
        return EnvConfig(
            language="rust",
            test_runner_cmd=["cargo", "test"],
            env_dir=repo_dir,
        )

    def discover_tests(self, repo_dir: str) -> list[str]:
        # Rust tests live inside source files and in tests/ directory
        found = set()
        # Integration tests
        for path in glob.glob(f"{repo_dir}/tests/**/*.rs", recursive=True):
            found.add(os.path.relpath(path, repo_dir))
        # Source files with #[cfg(test)] β€” treat each .rs file as a test candidate
        for path in glob.glob(f"{repo_dir}/src/**/*.rs", recursive=True):
            content = Path(path).read_text(errors="ignore")
            if "#[cfg(test)]" in content or "#[test]" in content:
                found.add(os.path.relpath(path, repo_dir))
        return sorted(found)

    def run_tests(self, test_path: str, repo_dir: str, env_config: EnvConfig, timeout: int = 180) -> tuple[str, int]:
        # Derive test module name from path
        if test_path.startswith("tests/"):
            test_name = Path(test_path).stem
            return self._run(
                ["cargo", "test", "--test", test_name, "--", "--nocapture"],
                cwd=repo_dir, timeout=timeout,
            )
        else:
            # Unit tests β€” run the whole crate's unit tests
            return self._run(
                ["cargo", "test", "--lib", "--", "--nocapture"],
                cwd=repo_dir, timeout=timeout,
            )

    def run_sast(self, diff_text: str, changed_files: list[str], repo_dir: str) -> RuntimeSastReport:
        findings = []
        findings.extend(self._scan_secrets(diff_text))
        findings.extend(self._scan_rust_patterns(diff_text))
        clippy_out = self._run_clippy(repo_dir)

        critical = [f for f in findings if f.severity == "CRITICAL"]
        blocked_reason = f"CRITICAL: {critical[0].description}" if critical else None
        return RuntimeSastReport(
            passed=blocked_reason is None, language="rust",
            findings=findings, tool_output=clippy_out,
            blocked_reason=blocked_reason,
        )

    def _scan_secrets(self, diff_text: str) -> list[RuntimeSastFinding]:
        try:
            from sast_gate import _scan_diff_for_secrets
            return [
                RuntimeSastFinding(f.severity, f.category, f.line_number, f.line_content, f.description)
                for f in _scan_diff_for_secrets(diff_text)
            ]
        except ImportError:
            return []

    _RUST_DANGEROUS = [
        (re.compile(r'\bunsafe\s*\{'), "HIGH", "unsafe block β€” verify memory safety invariants"),
        (re.compile(r'std::mem::transmute'), "CRITICAL", "mem::transmute β€” type system bypass"),
        (re.compile(r'from_raw_parts'), "HIGH", "slice::from_raw_parts β€” verify pointer validity"),
        (re.compile(r'Box::from_raw'), "HIGH", "Box::from_raw β€” double-free risk"),
        (re.compile(r'std::process::Command'), "MEDIUM", "process::Command β€” verify no untrusted input"),
        (re.compile(r'panic!\s*\(.*unwrap'), "LOW", "unwrap in panic context"),
    ]

    def _scan_rust_patterns(self, diff_text: str) -> list[RuntimeSastFinding]:
        findings = []
        for i, line in enumerate(diff_text.splitlines(), 1):
            if not line.startswith("+"):
                continue
            for pattern, severity, description in self._RUST_DANGEROUS:
                if pattern.search(line):
                    findings.append(RuntimeSastFinding(
                        severity=severity, category="rust-security",
                        line_number=i, line_content=line[1:].strip(),
                        description=description,
                    ))
        return findings

    def _run_clippy(self, repo_dir: str) -> str:
        out, _ = self._run(
            ["cargo", "clippy", "--all-targets", "--", "-D", "warnings"],
            cwd=repo_dir, timeout=300,
        )
        return out

    def run_supply_chain(self, diff_text: str, repo_dir: str) -> RuntimeSupplyChainReport:
        new_crates = self._extract_new_crates(diff_text)
        audit_out, code = self._run(["cargo", "audit", "--json"], cwd=repo_dir, timeout=120)
        cve_findings = self._parse_cargo_audit(audit_out)
        critical_cves = [c for c in cve_findings if c.get("cvss", 0) >= 9.0]
        if critical_cves:
            return RuntimeSupplyChainReport(
                passed=False, language="rust",
                new_packages=new_crates, cve_findings=cve_findings,
                blocked_reason=f"Critical CVE in crate: {critical_cves[0].get('package')}",
            )
        return RuntimeSupplyChainReport(
            passed=True, language="rust",
            new_packages=new_crates, cve_findings=cve_findings,
        )

    def _extract_new_crates(self, diff_text: str) -> list[str]:
        added = []
        in_cargo = False
        for line in diff_text.splitlines():
            if "Cargo.toml" in line:
                in_cargo = True
            if in_cargo and line.startswith("+") and not line.startswith("+++"):
                m = re.search(r'^([\w\-]+)\s*=', line[1:].strip())
                if m:
                    added.append(m.group(1))
        return list(dict.fromkeys(added))

    def _parse_cargo_audit(self, raw: str) -> list[dict]:
        try:
            data = json.loads(raw)
            vulns = data.get("vulnerabilities", {}).get("list", [])
            return [
                {
                    "package": v.get("advisory", {}).get("package", ""),
                    "id": v.get("advisory", {}).get("id", ""),
                    "cvss": v.get("advisory", {}).get("cvss", 0) or 0,
                }
                for v in vulns
            ]
        except Exception:
            return []

    def get_mcp_domains(self) -> list[str]:
        return ["doc.rust-lang.org", "crates.io", "docs.rs", "docs.github.com"]

    def get_fix_prompt_instructions(self, test_path: str, branch_name: str, src_hint: str = "") -> str:
        src_target = src_hint or "src/lib.rs or the relevant module"
        return (
            f"1. Fix '{src_target}' or 'Cargo.toml' to make the test pass. "
            f"Do NOT modify test modules (files or #[cfg(test)] blocks).\n"
            f"2. Ensure the fix passes 'cargo clippy -- -D warnings' with no new lints.\n"
            f"3. Use the 'fetch-docs' MCP tool for docs.rs or doc.rust-lang.org if needed.\n"
            f"4. Work on branch '{branch_name}'. Commit the minimal fix.\n"
            f"5. Unsafe code must include a SAFETY comment explaining the invariant."
        )


# ──────────────────────────────────────────────────────────────
# RUBY RUNTIME
# ──────────────────────────────────────────────────────────────

class RubyRuntime(LanguageRuntime):
    language = "ruby"

    @classmethod
    def detect(cls, repo_dir: str) -> bool:
        return (
            os.path.exists(os.path.join(repo_dir, "Gemfile")) or
            os.path.exists(os.path.join(repo_dir, "Gemfile.lock"))
        )

    def _detect_runner(self, repo_dir: str) -> str:
        spec_dir = os.path.join(repo_dir, "spec")
        test_dir = os.path.join(repo_dir, "test")
        if os.path.isdir(spec_dir):
            return "rspec"
        if os.path.isdir(test_dir):
            return "minitest"
        return "rspec"

    def setup_env(self, repo_dir: str, persistent_dir: str = "/data") -> EnvConfig:
        self._run(["bundle", "install", "--jobs", "4", "--retry", "3"],
                  cwd=repo_dir, timeout=600)
        runner = self._detect_runner(repo_dir)
        if runner == "rspec":
            cmd = ["bundle", "exec", "rspec"]
        else:
            cmd = ["bundle", "exec", "ruby", "-Ilib", "-Itest"]
        return EnvConfig(
            language="ruby",
            test_runner_cmd=cmd,
            env_dir=repo_dir,
            metadata={"runner": runner},
        )

    def discover_tests(self, repo_dir: str) -> list[str]:
        patterns = [
            f"{repo_dir}/spec/**/*_spec.rb",
            f"{repo_dir}/test/**/*_test.rb",
            f"{repo_dir}/test/**/test_*.rb",
        ]
        found = set()
        for pat in patterns:
            for path in glob.glob(pat, recursive=True):
                found.add(os.path.relpath(path, repo_dir))
        return sorted(found)

    def run_tests(self, test_path: str, repo_dir: str, env_config: EnvConfig, timeout: int = 120) -> tuple[str, int]:
        runner = env_config.metadata.get("runner", "rspec")
        if runner == "rspec":
            cmd = ["bundle", "exec", "rspec", test_path, "--format", "documentation"]
        else:
            cmd = ["bundle", "exec", "ruby", "-Ilib", "-Itest", test_path]
        return self._run(cmd, cwd=repo_dir, timeout=timeout)

    def run_sast(self, diff_text: str, changed_files: list[str], repo_dir: str) -> RuntimeSastReport:
        findings = []
        findings.extend(self._scan_secrets(diff_text))
        findings.extend(self._scan_ruby_patterns(diff_text))
        brakeman_out = self._run_brakeman(repo_dir)

        critical = [f for f in findings if f.severity == "CRITICAL"]
        blocked_reason = f"CRITICAL: {critical[0].description}" if critical else None
        return RuntimeSastReport(
            passed=blocked_reason is None, language="ruby",
            findings=findings, tool_output=brakeman_out,
            blocked_reason=blocked_reason,
        )

    def _scan_secrets(self, diff_text: str) -> list[RuntimeSastFinding]:
        try:
            from sast_gate import _scan_diff_for_secrets
            return [
                RuntimeSastFinding(f.severity, f.category, f.line_number, f.line_content, f.description)
                for f in _scan_diff_for_secrets(diff_text)
            ]
        except ImportError:
            return []

    _RUBY_DANGEROUS = [
        (re.compile(r'\beval\s*[(\s]'), "CRITICAL", "eval() β€” arbitrary code execution"),
        (re.compile(r'\bsend\s*\(.*params'), "HIGH", "Dynamic dispatch with user params β€” injection risk"),
        (re.compile(r'`.*#\{'), "HIGH", "Shell interpolation in backtick β€” OS injection"),
        (re.compile(r'system\s*\(.*#\{'), "HIGH", "system() with interpolation β€” OS injection"),
        (re.compile(r'Marshal\.load'), "CRITICAL", "Marshal.load β€” deserialization RCE risk"),
        (re.compile(r'\.html_safe'), "HIGH", "html_safe β€” potential XSS"),
        (re.compile(r'raw\s+params'), "HIGH", "raw params output β€” XSS risk"),
        (re.compile(r'find_by_sql\s*\(.*\+'), "HIGH", "SQL concat in find_by_sql β€” injection"),
    ]

    def _scan_ruby_patterns(self, diff_text: str) -> list[RuntimeSastFinding]:
        findings = []
        for i, line in enumerate(diff_text.splitlines(), 1):
            if not line.startswith("+"):
                continue
            for pattern, severity, description in self._RUBY_DANGEROUS:
                if pattern.search(line):
                    findings.append(RuntimeSastFinding(
                        severity=severity, category="ruby-security",
                        line_number=i, line_content=line[1:].strip(),
                        description=description,
                    ))
        return findings

    def _run_brakeman(self, repo_dir: str) -> str:
        gemfile = os.path.join(repo_dir, "Gemfile")
        if not os.path.exists(gemfile):
            return ""
        out, _ = self._run(
            ["bundle", "exec", "brakeman", "-q", "--no-pager", "-f", "text"],
            cwd=repo_dir, timeout=120,
        )
        return out

    def run_supply_chain(self, diff_text: str, repo_dir: str) -> RuntimeSupplyChainReport:
        new_gems = self._extract_new_gems(diff_text)
        audit_out, _ = self._run(
            ["bundle", "exec", "bundler-audit", "check", "--update"],
            cwd=repo_dir, timeout=120,
        )
        cve_findings = self._parse_bundle_audit(audit_out)
        critical_cves = [c for c in cve_findings if "CRITICAL" in c.get("criticality", "")]
        if critical_cves:
            return RuntimeSupplyChainReport(
                passed=False, language="ruby",
                new_packages=new_gems, cve_findings=cve_findings,
                blocked_reason=f"Critical gem vulnerability: {critical_cves[0].get('gem')}",
            )
        return RuntimeSupplyChainReport(
            passed=True, language="ruby",
            new_packages=new_gems, cve_findings=cve_findings,
        )

    def _extract_new_gems(self, diff_text: str) -> list[str]:
        added = []
        for line in diff_text.splitlines():
            if "Gemfile" in line and not line.startswith("+"):
                continue
            if line.startswith("+") and not line.startswith("+++"):
                m = re.search(r"gem\s+['\"]([^'\"]+)['\"]", line)
                if m:
                    added.append(m.group(1))
        return list(dict.fromkeys(added))

    def _parse_bundle_audit(self, raw: str) -> list[dict]:
        findings = []
        for line in raw.splitlines():
            m = re.search(r"Name:\s+(\S+)", line)
            if m:
                findings.append({"gem": m.group(1), "criticality": "HIGH"})
        return findings

    def get_mcp_domains(self) -> list[str]:
        return [
            "ruby-doc.org", "rubygems.org", "api.rubyonrails.org",
            "guides.rubyonrails.org", "docs.github.com",
        ]

    def get_fix_prompt_instructions(self, test_path: str, branch_name: str, src_hint: str = "") -> str:
        src_target = src_hint or "the relevant Ruby source file under app/ or lib/"
        return (
            f"1. Fix '{src_target}' or 'Gemfile' to make the test pass. "
            f"Do NOT modify spec/ or test/ files.\n"
            f"2. Use the 'fetch-docs' MCP tool for api.rubyonrails.org or rubygems.org if needed.\n"
            f"3. Work on branch '{branch_name}'. Commit the minimal fix.\n"
            f"4. Ensure ActiveRecord queries use parameterized forms β€” no string interpolation in SQL.\n"
            f"5. The fix must not break other tests or change model validations."
        )


# ──────────────────────────────────────────────────────────────
# RUNTIME FACTORY
# ──────────────────────────────────────────────────────────────

class RuntimeFactory:
    """
    Detects and returns the correct LanguageRuntime for a cloned repo.
    Detection order matters β€” TypeScript before Node (TS is a superset),
    Python last as fallback since many repos have a setup.py alongside other stacks.
    """

    _REGISTRY: list[type[LanguageRuntime]] = [
        TypeScriptRuntime,   # before Node β€” TS repos also have package.json
        NodeRuntime,
        JavaRuntime,
        GoRuntime,
        RustRuntime,
        RubyRuntime,
        PythonRuntime,       # last: many repos include Python tooling as secondary
    ]

    @classmethod
    def for_repo(cls, repo_dir: str) -> LanguageRuntime:
        """
        Auto-detect and instantiate the correct runtime.
        Falls back to PythonRuntime if nothing matches.
        """
        for runtime_cls in cls._REGISTRY:
            if runtime_cls.detect(repo_dir):
                return runtime_cls()
        return PythonRuntime()  # safe fallback

    @classmethod
    def language_of(cls, repo_dir: str) -> str:
        """Lightweight language label without instantiation."""
        return cls.for_repo(repo_dir).language

    @classmethod
    def supported_languages(cls) -> list[str]:
        return [r.language for r in cls._REGISTRY]