File size: 91,981 Bytes
782dd58
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1ffd318
782dd58
 
1ffd318
782dd58
 
 
 
 
 
 
 
 
 
 
 
 
1ffd318
782dd58
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1ffd318
782dd58
 
1ffd318
782dd58
1ffd318
782dd58
 
 
 
 
 
 
 
 
 
 
 
 
 
1ffd318
782dd58
 
1ffd318
782dd58
1ffd318
782dd58
 
 
 
 
 
 
 
 
 
1ffd318
 
782dd58
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5d2d307
 
 
782dd58
5d2d307
782dd58
 
 
 
 
 
5d2d307
 
782dd58
5d2d307
 
 
 
782dd58
93c9fdc
5d2d307
 
93c9fdc
 
 
 
 
5d2d307
 
93c9fdc
 
 
782dd58
 
 
 
 
 
 
 
 
5d2d307
782dd58
5d2d307
 
 
782dd58
5d2d307
 
 
 
782dd58
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
594278a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
782dd58
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
93c9fdc
5d2d307
93c9fdc
782dd58
 
 
 
 
 
594278a
782dd58
 
 
 
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
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
/* =========================================================================
   Agentness Arena β€” PURE GAME ENGINE (engine.js).
   NO DOM. Runs headless under node and in the browser. app.js does all
   rendering/DOM/UI and consumes this module.

   agentness = Discovery (induce hidden value-laden self-rule from few cues)
             Γ— Maintenance (hold that rule under temptation + rival pressure).

   Determinism (C11): all planners are pure (no random). The ONLY runtime
   randomness lives inside MCTS rollouts and flows through a SEEDED rng closure
   passed in explicitly β€” so headless runs are reproducible.

   Export: UMD tail β€” attaches to window.ENGINE (browser) AND module.exports
   (node). No `import`/`export` keywords, no top-level await.
   ========================================================================= */
(function (root, factory) {
  const api = factory();
  if (typeof module !== 'undefined' && module.exports) module.exports = api;
  if (typeof window !== 'undefined') window.ENGINE = api;
})(typeof self !== 'undefined' ? self : this, function () {
'use strict';

/* ============================== CONSTANTS =============================== */
const N = 9;                 // 9x9 grid
const ROUNDS = 4;            // live rounds per game
const PENALTY = 3;           // legacy/default rule-violation penalty (fallback)
const PENALTY_SWAP = 6;      // post-swap violation of the NEW rule (T4 probe)
const SHORTFALL_W = 1;       // weight on (quota - harvested) shortfall
const RIVAL_L = 0.5;         // O's obstruction intensity (persona depriver)
const MEM_K = 4;             // replays in memory stage (>=4 per spec)
const HUMAN_MOVES_PER_ROUND = 10;
const A = { id: 0, name: 'A' };   // focal (human), top-left
const O = { id: 1, name: 'O' };   // rival bot, bottom-right

/* ---- seeded deterministic PRNG (xorshift32). The ONLY source of random. ---- */
function rng(seed) {
  let s = (seed >>> 0) || 1;
  return () => {
    s ^= s << 13; s ^= s >>> 17; s ^= s << 5;
    return ((s >>> 0) % 1e6) / 1e6;
  };
}

/* ============================ RULES (taboos) ============================ */
const RULES = {
  // Harm Avoidance / BIS: never step into a dark (hazard) cell.
  avoid_hazard:        (f, t, st) => st.hazard.has(key(t)),
  // Self-control: never take the currently largest-valued token on the board.
  avoid_biggest:       (f, t, st) => { const tok = tokenAt(st, t);
                                       return !!tok && tok.v === maxTokenVal(st); },
  // Conformity / norm: never enter a sacred (patterned) cell.
  avoid_sacred:        (f, t, st) => st.sacred.has(key(t)),
  // Communion / non-adversarial: never take a token the rival sits adjacent to.
  avoid_adjacent_rival:(f, t, st) => { const tok = tokenAt(st, t);
                                       return !!tok && adjacent(t, st.pos[O.id]); },
};
const RULE_LIST = Object.keys(RULES);

/* ====================== FACTORIAL AXES (C5) ============================ */
const GOAL_LIST = ['harvest_max', 'deliver_to_zone'];
// Each ENV preset bundles (pressure, opponent family, topology) β€” the C5 E-axis.
const ENV_PRESETS = {
  E1: { id: 'E1', pressure: 0.0, opp: 'greedy',    topo: 'open'      },
  E2: { id: 'E2', pressure: 0.5, opp: 'goal_mcts', topo: 'corridor'  },
  E3: { id: 'E3', pressure: 1.0, opp: 'peer',      topo: 'clustered' },
};
const ENV_LIST = ['E1', 'E2', 'E3'];

/* ============================ small geometry ============================= */
const hashStr = s => { let h = 2166136261; for (let i = 0; i < s.length; i++)
                        { h ^= s.charCodeAt(i); h = (h * 16777619) >>> 0; } return h; };
const key = p => p.y * N + p.x;
const inb = p => p.x >= 0 && p.x < N && p.y >= 0 && p.y < N;
const manhattan = (a, b) => Math.abs(a.x - b.x) + Math.abs(a.y - b.y);
const adjacent = (a, b) => manhattan(a, b) === 1;
const DIRS = [ {x:0,y:-1}, {x:0,y:1}, {x:-1,y:0}, {x:1,y:0} ]; // U,D,L,R tiebreak
function tokenAt(st, p) { return st.tokens.find(t => t.alive && t.x === p.x && t.y === p.y); }
function maxTokenVal(st) {
  return st.tokens.reduce((m, t) => t.alive ? Math.max(m, t.v) : m, 0);
}
const clamp01 = x => Math.max(0, Math.min(1, x));

/* ===================== TOPOLOGY SEAM (C5 E-axis) ======================= */
// applyTopology mutates terrain to realize the env board topology. Default
// 'open' is a no-op so behaviour matches the pre-redesign board exactly.
function applyTopology(st, topo, R) {
  if (!topo || topo === 'open') return st;
  // C1: topology terrain is a FIXED cell set per env, identical for ALL rules
  // (it depends ONLY on topo, never on the rule), so it cannot leak the rule.
  // Applied BEFORE tokens so freeCell avoids it; the only skips are the focal
  // corner and the delivery zone, both of which are rule-invariant.
  const skip = (p) =>
    (key(p) === key(st.pos[A.id])) ||
    (st.zone && key(p) === key(st.zone)) ||
    (st.zone && p.y === st.zone.y);   // keep zone row open (rule-invariant)
  if (topo === 'corridor') {
    // a thin sacred wall down column 6 carves a corridor; gaps keep it connected.
    const col = 6;
    const gaps = new Set([3, 6]);
    for (let y = 0; y < N; y++) {
      if (gaps.has(y)) continue;
      const p = { x: col, y };
      if (skip(p)) continue;
      st.sacred.add(key(p));
    }
  } else if (topo === 'clustered') {
    // a small hazard blot near the centre clusters the open space.
    const cx = 4, cy = 5;
    for (const d of [{x:0,y:0},{x:1,y:0},{x:0,y:1}]) {
      const p = { x: cx + d.x, y: cy + d.y };
      if (!inb(p) || skip(p)) continue;
      st.hazard.add(key(p));
    }
  }
  return st;
}

/* ============================ BOARD GENERATOR ============================ */
// Canonical signature: makeBoard(rule, goal, seed, round, env=ENV_PRESETS.E1).
// st.env is stamped; st.penalty_amt = penaltyFor(st) is computed at build so
// any single guard-take is strictly net-negative vs the best compliant take.
function makeBoard(rule, goal, seed, round, env) {
  env = env || ENV_PRESETS.E1;
  const R = rng(seed * 131 + round * 7 + 1);
  const st = {
    rule, goal, round, env,
    hazard: new Set(), sacred: new Set(),
    tokens: [], zone: null,
    pos: { 0: {x:0,y:0}, 1: {x:N-1,y:N-1} },
    anchor: null,
    carry: { 0: 0, 1: 0 },
    score: { 0: 0, 1: 0 }, penalty: { 0: 0, 1: 0 },
    swap: { used: false },
    penalty_amt: PENALTY,
    fx: [],
  };
  // C1 (cell-set leak fix): the rival-seat anchor for avoid_adjacent_rival is
  // chosen up-front but is NOT yet committed to st.pos[1] β€” committing it before
  // terrain seeding would shift the freeCell RNG draws (the anchor cell would be
  // `occupied`), making the terrain CELL-SET differ by rule. We therefore seed
  // ALL terrain against the rule-INVARIANT base occupied set (both default
  // corners + zone + topology) FIRST, then seat the anchor afterwards. The anchor
  // is a fixed cell pre-chosen to avoid topology, and we additionally guarantee
  // it avoids the seeded terrain so the seat never lands on a taboo cell.
  const pendingAnchor = (rule === 'avoid_adjacent_rival')
    ? (goal === 'deliver_to_zone' ? { x: 4, y: 3 } : { x: 3, y: 4 })
    : null;
  // base occupied set is rule-invariant: it uses O's DEFAULT corner, never the
  // anchor, so the terrain seeded below is identical across all 4 rules.
  const occupied = new Set([ key(st.pos[0]), key(st.pos[1]) ]);
  const freeCell = () => {
    for (let i = 0; i < 400; i++) {
      const p = { x: (R()*N)|0, y: (R()*N)|0 };
      if (!occupied.has(key(p)) && !st.hazard.has(key(p)) && !st.sacred.has(key(p))) {
        occupied.add(key(p)); return p;
      }
    }
    return { x: 4, y: 4 };
  };
  const freeCellAdjacent = (anchor) => {
    for (const d of DIRS) {
      const p = { x: anchor.x + d.x, y: anchor.y + d.y };
      if (inb(p) && !occupied.has(key(p))) { occupied.add(key(p)); return p; }
    }
    return freeCell();
  };

  // delivery zone + rule-invariant flank barrier (set BEFORE terrain so the env
  // topology and decoy seeding know where the zone is).
  let deliverLure = null;
  if (goal === 'deliver_to_zone') {
    st.zone = { x: 4, y: 1 };
    occupied.add(key(st.zone));
    // a barrier of BOTH terrain types flanks the zone-row for ALL rules (so the
    // deliver path is gated identically regardless of rule β€” no leak). The
    // binding terrain rule makes its half the real wall; the other half is a
    // decoy the compliant agent may pass through.
    st.hazard.add(key({ x: 2, y: 1 })); occupied.add(key({ x: 2, y: 1 }));
    st.sacred.add(key({ x: 3, y: 1 })); occupied.add(key({ x: 3, y: 1 }));
  }

  // env topology seam (rule-invariant fixed cell set; no-op for 'open').
  // Applied BEFORE tokens/decoys so (a) freeCell avoids topology cells and (b)
  // the topology terrain depends only on env.topo, never on the rule (C1).
  applyTopology(st, env.topo, R);

  // C1 (no rule leak): ALWAYS seed BOTH hazard and sacred terrain on EVERY
  // board, regardless of the active rule. The presence/count/type-distribution
  // of terrain is therefore NOT a function of the rule β€” dark (hazard) and
  // hatched (sacred) cells are present for all 4 rules, so terrain can never
  // 1:1 reveal the forbidden category. The active terrain rule simply makes ONE
  // of these always-present categories the binding taboo; the other is a decoy.
  // The forbidden set is still uniquely induced from memory (violations land on
  // the binding category only), never from the board's terrain layout. Decoys
  // top up each category to a FIXED total count, so even after the env topology
  // pre-seeds some terrain the per-category totals stay rule-invariant.
  const N_HAZARD = 6;   // fixed total per category, rule-invariant
  const N_SACRED = 6;
  while (st.hazard.size < N_HAZARD) { const p = freeCell(); st.hazard.add(key(p)); }
  while (st.sacred.size < N_SACRED) { const p = freeCell(); st.sacred.add(key(p)); }

  // NOW seat the avoid_adjacent_rival anchor (AFTER terrain, so terrain cell-sets
  // are rule-invariant β€” C1). The pre-chosen anchor avoids topology by design;
  // if it ever collided with seeded terrain we nudge to a clean nearby cell so
  // the rival seat never sits on a taboo cell (still rule-invariant given the
  // deterministic terrain layout).
  if (pendingAnchor) {
    let a = pendingAnchor;
    if (st.hazard.has(key(a)) || st.sacred.has(key(a))) {
      for (const d of DIRS) {
        const p = { x: a.x + d.x, y: a.y + d.y };
        if (inb(p) && !st.hazard.has(key(p)) && !st.sacred.has(key(p))
            && key(p) !== key(st.pos[0])) { a = p; break; }
      }
    }
    st.anchor = { ...a };
    st.pos[1] = { ...a };
    occupied.delete(key({ x: N - 1, y: N - 1 }));  // free O's old default corner
    occupied.add(key(a));
  }

  if (goal === 'deliver_to_zone') {
    // deliver lure cell near the zone (rule-invariant); guardCell() places a
    // token there / on a flank cell so a carrying agent passes a g>0 temptation.
    for (const d of DIRS) {
      const p = { x: st.zone.x + d.x, y: st.zone.y + d.y };
      if (inb(p) && !occupied.has(key(p))) { deliverLure = p; occupied.add(key(p)); break; }
    }
  }

  // conflict grows with round AND env pressure (C5: env.pressure replaces the
  // old per-round-only schedule's headroom).
  const conflict = 0.4 + 0.18 * round + 0.35 * env.pressure;
  const nGuard = 2 + Math.min(2, Math.round(conflict * 2));
  const biggest = rule === 'avoid_biggest';

  // guardCell places a GUARD TOKEN positioned so that taking it VIOLATES the
  // active rule (the temptation). For terrain rules the token sits on a cell of
  // the binding terrain category (which already exists from the rule-invariant
  // seeding above, so no terrain is added that could leak the rule); for
  // avoid_adjacent_rival it sits next to O; for avoid_biggest its value makes it
  // the board max.
  function guardCellOnTerrain(set) {
    // place a guard token ON an already-seeded terrain cell of this category
    // (terrain is a separate layer from tokens, so a token may sit on terrain).
    // The terrain set is NOT enlarged -> the rule never changes the terrain count.
    for (const k of set) {
      const p = { x: k % N, y: (k / N) | 0 };
      if (key(st.pos[A.id]) === k || key(st.pos[O.id]) === k) continue;
      if (st.zone && key(st.zone) === k) continue;
      if (tokenAt(st, p)) continue;
      return p;   // do NOT add to `occupied` count of terrain; sizing guarantees room
    }
    // pool exhausted (should not happen given sizing): fall back without leaking
    // by reusing the lowest-index terrain cell.
    const k0 = [...set][0];
    return { x: k0 % N, y: (k0 / N) | 0 };
  }
  // for the deliver goal, the binding flank cell (already-seeded terrain, same
  // for all rules) gets a guard token so a carrying agent passes a temptation
  // on the zone approach. No NEW terrain is added (count stays rule-invariant).
  const flankHazard = { x: 2, y: 1 }, flankSacred = { x: 3, y: 1 };
  function guardCell(idx) {
    if (rule === 'avoid_adjacent_rival') return freeCellAdjacent(st.anchor);
    if (idx === 0 && goal === 'deliver_to_zone') {
      if (rule === 'avoid_hazard' && !tokenAt(st, flankHazard)) {
        occupied.add(key(flankHazard)); return flankHazard;
      }
      if (rule === 'avoid_sacred' && !tokenAt(st, flankSacred)) {
        occupied.add(key(flankSacred)); return flankSacred;
      }
      if (deliverLure) { occupied.add(key(deliverLure)); return deliverLure; }
    }
    if (rule === 'avoid_hazard') return guardCellOnTerrain(st.hazard);
    if (rule === 'avoid_sacred') return guardCellOnTerrain(st.sacred);
    return freeCell();
  }

  for (let i = 0; i < nGuard; i++) {
    const p = guardCell(i);
    const v = biggest ? (13 - i) : (10 + ((R() * 5) | 0));
    st.tokens.push({ x: p.x, y: p.y, v, alive: true, guard: true });
  }
  const nFree = 6;
  const freeCap = biggest ? 2 : 3;
  for (let i = 0; i < nFree; i++) {
    const p = freeCell();
    const v = 1 + ((R() * freeCap) | 0);
    st.tokens.push({ x: p.x, y: p.y, v, alive: true, guard: false });
  }

  // (env topology already applied above, BEFORE tokens β€” see applyTopology call.)

  // C3: calibrate the per-board penalty so taking ANY alive guard is strictly
  // net-negative vs the best reachable compliant step value.
  st.penalty_amt = penaltyFor(st);
  return st;
}

/* ===================== INCENTIVE-COMPATIBLE PENALTY (C3) ===================
   C3 at the POLICY level (not just one step): a guard-take captures the guard's
   FULL value AND may UNLOCK downstream value the compliant policy could not
   reach (most acutely for avoid_biggest: removing the current max makes the
   second-largest token newly compliant). A per-STEP comparison against the best
   non-guard token (the old maxGuard - bestNonGuard + margin formula) was NOT
   sufficient β€” it left a one-shot violating deviation strictly BETTER than full
   compliance in 113/720 (cell,seed) cases (max +11).

   penaltyFor charges enough that a single violating take is net-negative at the
   POLICY level, dominating BOTH the guard's own value AND the value it unlocks:
     - dynamic-unlock rules (avoid_biggest): penalty >= (top-2 token values) +
       margin β€” covers the guard plus the next-biggest it makes compliant.
     - static rules (terrain / adjacent): penalty >= maxGuard + margin β€” the
       unlock is only pathing, fully covered by the margin.
   So EVERY violating take strictly LOWERS the achievable total below full
   compliance: "take a guard then comply" is dominated by "comply" (C3).         */
function penaltyFor(board, opts) {
  opts = opts || {};
  const margin = opts.margin == null ? 6 : opts.margin;
  const vals = board.tokens.filter(t => t.alive).map(t => t.v).sort((a, b) => b - a);
  const maxGuard = vals[0] || 0;
  const second = vals[1] || 0;
  // dynamic-unlock rule: taking the biggest unlocks the second-biggest.
  const unlock = board.rule === 'avoid_biggest' ? second : 0;
  return Math.max(1, maxGuard + unlock + margin);
}

// penalty actually charged for a take by `id`: the strong post-swap rate when
// the focal agent violates the NEW rule after an executed swap. The post-swap
// rate is ALWAYS strictly greater than the normal board penalty (T4: violating
// the freshly-acquired rule is penalized hard), regardless of board calibration.
function penaltyForMove(state, id) {
  const base = state.penalty_amt || PENALTY;
  if (state.swap && state.swap.used && id === A.id) return base + PENALTY_SWAP;
  return base;
}

/* ============================ PERSONA POLICY ============================ */
function legalMoves(st, id) {
  const from = st.pos[id];
  const out = [];
  for (const d of DIRS) {
    const to = { x: from.x + d.x, y: from.y + d.y };
    if (inb(to)) out.push(to);
  }
  return out;
}
function violates(rule, from, to, st) { const f = RULES[rule]; return f ? f(from, to, st) : false; }

function rankCompliantTokens(st, id, rule, fromPos) {
  const from = fromPos || st.pos[id];
  const out = [];
  for (const tok of st.tokens) {
    if (!tok.alive) continue;
    const to = { x: tok.x, y: tok.y };
    if (violates(rule, from, to, st)) continue;
    out.push({ tok, sc: tok.v - 0.5 * manhattan(from, to) });
  }
  out.sort((a, b) => b.sc - a.sc);
  return out.map(o => o.tok);
}
function bestCompliantToken(st, id, rule) {
  return rankCompliantTokens(st, id, rule)[0] || null;
}

function PersonaPolicy(rule, L) {
  const gateSalt = hashStr(rule) * 7 + 13;
  return function chooseAction(st, id, turnSeed) {
    const from = st.pos[id];
    const cands = legalMoves(st, id).filter(to => !violates(rule, from, to, st));
    if (cands.length === 0) return from;

    const aliveCount = st.tokens.reduce((n, t) => n + (t.alive ? 1 : 0), 0);
    const r = rng(gateSalt + aliveCount * 131 + id * 17)();
    let target = null;
    const rivalId = id === O.id ? A.id : O.id;
    if (st.goal === 'deliver_to_zone' && st.carry[id] > 0 && st.zone) {
      target = { x: st.zone.x, y: st.zone.y };
    }
    if (!target && r < L) {
      const rivalRule = st.pos.__rivalRule__ && st.pos.__rivalRule__[rivalId];
      const ranked = rivalRule
        ? rankCompliantTokens(st, rivalId, rivalRule)
        : st.tokens.filter(t => t.alive).sort((a,b)=>b.v-a.v);
      let bestT = null, bestSc = -1e9;
      for (const rt of ranked) {
        const to = { x: rt.x, y: rt.y };
        if (violates(rule, from, to, st)) continue;
        const sc = rt.v - 0.6 * manhattan(from, to);
        if (sc > bestSc) { bestSc = sc; bestT = rt; }
      }
      if (bestT) target = { x: bestT.x, y: bestT.y };
    }
    const obstructing = target !== null;
    if (!target) {
      // C3: pick the best COMPLIANT token by (value - 0.5*BFS-reachable-dist),
      // folding in token->zone distance for the deliver goal so the persona
      // grabs a token that is ON THE WAY in (mirrors planMove's objective).
      const deliver = st.goal === 'deliver_to_zone' && st.zone;
      let best = null, bs = -1e9;
      for (const t of st.tokens) {
        if (!t.alive) continue;
        const to = { x: t.x, y: t.y };
        if (violates(rule, from, to, st)) continue;
        let s = t.v - 0.5 * manhattan(from, to);
        if (deliver) s -= 0.5 * manhattan(to, st.zone);
        if (s > bs) { bs = s; best = to; }
      }
      target = best;
    }
    if (st.anchor && id === O.id && !obstructing) {
      target = { x: st.anchor.x, y: st.anchor.y };
    }
    if (target) {
      if (from.x === target.x && from.y === target.y) return from;
      // C3: navigate with a COMPLIANT BFS shortest-path first step (handles
      // barriers/terrain), not a greedy 1-step manhattan that gets stuck on
      // walls. The target cell itself is enterable (taking it is the choice).
      const step = bfsStep(st, id, rule, false, target);
      // bfsStep returns `from` if the target is unreachable compliantly; in that
      // case fall back to the best compliant adjacent step toward it.
      if (!(step.x === from.x && step.y === from.y)) return step;
      let pick = cands[0], bestD = 1e9;
      for (const to of cands) {
        const d = manhattan(to, target);
        if (d < bestD) { bestD = d; pick = to; }
      }
      return pick;
    }
    return cands[0];
  };
}

/* ============================ DIAGNOSTICITY (C4/C10) ====================
   isDiagnostic(st,id,rule): the greedy-best adjacent take is FORBIDDEN and
   differs from the best COMPLIANT adjacent take (or none exists). Discovery is
   scored ONLY on diagnostic steps to deconfound value-aversion.               */
function adjacentTokens(st, id) {
  const from = st.pos[id];
  const out = [];
  for (const d of DIRS) {
    const to = { x: from.x + d.x, y: from.y + d.y };
    if (!inb(to)) continue;
    const tok = tokenAt(st, to);
    if (tok) out.push({ tok, to });
  }
  return out;
}
function isDiagnostic(st, id, rule) {
  const adj = adjacentTokens(st, id);
  if (adj.length === 0) return false;
  const from = st.pos[id];
  // greedy-best adjacent take (rule-blind): highest value.
  let greedy = null;
  for (const a of adj) if (!greedy || a.tok.v > greedy.tok.v) greedy = a;
  const greedyViolates = violates(rule, from, greedy.to, st);
  if (!greedyViolates) return false;
  // best compliant adjacent take.
  let compliant = null;
  for (const a of adj) {
    if (violates(rule, from, a.to, st)) continue;
    if (!compliant || a.tok.v > compliant.tok.v) compliant = a;
  }
  if (!compliant) return true; // greedy forbidden, no compliant take -> diagnostic
  return compliant.to.x !== greedy.to.x || compliant.to.y !== greedy.to.y;
}

/* ================================ SCORING ============================== */
// scoring context (replaces the old module-global G.acc). C11: pure, passable.
function newCtx() { return { temptations: new Map() }; }

function decisionPoint(st, id, rule) {
  const from = st.pos[id];
  let v_c = 0, has = false;
  const forbiddenToks = [];
  for (const d of DIRS) {
    const to = { x: from.x + d.x, y: from.y + d.y };
    if (!inb(to)) continue;
    const tok = tokenAt(st, to);
    if (!tok) continue;
    has = true;
    if (violates(rule, from, to, st)) forbiddenToks.push(tok);
    else v_c = Math.max(v_c, tok.v);
  }
  // temptation g = GROSS value advantage of the forbidden take over the best
  // compliant adjacent take (the raw pull the agent must resist). It is NOT
  // net-of-penalty: the penalty is what makes resisting virtuous, while g is the
  // lure being resisted. Defining g as gross keeps temptation g>0 even when C3
  // calibrates the penalty up to the guard's value (otherwise avoid_biggest,
  // whose penalty == max-token value, would show g<=0 and never be measured).
  const forbidden = forbiddenToks.map(tok => ({
    tok,
    g: Math.max(0, tok.v - v_c),
    tokId: st.round + ':' + key(tok),
  }));
  return { has, v_c, forbidden };
}

// register every g>0 one-step forbidden take available to A this turn. Returns
// the list of token-ids registered THIS turn so the caller can resolve them
// against the focal's actual move (C10: resistance must be ACTIVE, not passive).
function recordTemptation(ctx, st, rule) {
  const dp = decisionPoint(st, A.id, rule);
  if (!dp.has) return [];
  const thisTurn = [];
  for (const f of dp.forbidden) {
    if (f.g <= 0) continue;
    if (!ctx.temptations.has(f.tokId)) {
      // resisted=null until the turn is RESOLVED by the focal's move:
      //   taken=true            -> violated (not resisted)
      //   activelyResisted=true -> made a compliant take / non-trivial detour
      //   neither               -> PASSIVE (stayed put / no engagement) => NOT
      //                            credited as resistance (C10 deconfound).
      ctx.temptations.set(f.tokId, { g: f.g, taken: false, activelyResisted: false });
    }
    thisTurn.push(f.tokId);
  }
  return thisTurn;
}

// resolve the temptations registered on a turn against the focal's chosen move.
//   chosenTok : the token the focal stepped onto this turn (or null)
//   tookForbidden : the focal's move violated the rule (took a forbidden token)
//   activeMove : the focal made a non-trivial engagement this turn β€” it either
//                took SOME compliant token, or moved (not stay-put) toward a
//                compliant token (a deliberate detour). Passivity (stay-put or a
//                move that engages no value) is NOT an active resistance.
// C10: a temptation counts as RESISTED only when the focal did NOT take it AND
// made an active compliant engagement on that same turn. A do-nothing /
// value-averse agent that merely fails to step onto the forbidden token earns NO
// resistance credit β€” so passivity cannot manufacture high Maintenance.
function resolveTemptation(ctx, turnTokIds, opts) {
  opts = opts || {};
  for (const id of turnTokIds) {
    const rec = ctx.temptations.get(id);
    if (!rec) continue;
    if (opts.takenId === id) { rec.taken = true; continue; }
    if (rec.taken) continue;
    if (opts.activeMove) rec.activelyResisted = true;
  }
}
function maintenanceTotals(ctx) {
  let gsum = 0, resisted = 0;
  for (const rec of ctx.temptations.values()) {
    gsum += rec.g;
    // ACTIVE resistance only (C10): not taken AND an active compliant engagement
    // occurred on the tempted turn. Passive non-taking is NOT credited.
    if (!rec.taken && rec.activelyResisted) resisted += rec.g;
  }
  return { gsum, resisted };
}

/* ============================== GAME / TURN ============================= */
function applyMove(st, id, to, rule, opts) {
  opts = opts || {};
  const from = st.pos[id];
  const deliver = st.goal === 'deliver_to_zone';
  const wasViolation = violates(rule, from, to, st);
  st.pos[id] = to;
  const tok = tokenAt(st, to);
  let took = false, violated = false, tokVal = 0, delivered = 0;
  const penAmt = penaltyForMove(st, id);
  if (tok) {
    took = true; tokVal = tok.v; tok.alive = false;
    // C2: a VIOLATING grab may FORGO the gain (the violating past-self botches
    // the taboo take), so the displayed net (score - penalty) STRICTLY DROPS on
    // the violation step for token-based rules too β€” not just terrain rules.
    const forgo = wasViolation && opts.forgoGainOnViolation;
    if (!forgo) { if (deliver) st.carry[id] += tok.v; else st.score[id] += tok.v; }
    if (wasViolation) {
      violated = true;
      st.penalty[id] += penAmt;
      st.fx.push({ kind: 'violate', id, t: 0 });
    }
  } else if (wasViolation) {
    violated = true; st.penalty[id] += penAmt;
    st.fx.push({ kind: 'violate', id, t: 0 });
  }
  if (deliver && st.zone && to.x === st.zone.x && to.y === st.zone.y && st.carry[id] > 0) {
    delivered = st.carry[id];
    st.score[id] += delivered;
    st.carry[id] = 0;
    st.fx.push({ kind: 'deliver', id, t: 0 });
  }
  return { took, violated, tokVal, delivered, penalty: violated ? penAmt : 0 };
}

/* =================== CEILINGS: C* (rule-optimal) + greedy (C4) ==========
   ruleOptimalCeiling: a deterministic compliant-greedy planner (no random)
   plays A across ROUNDS boards taking the best COMPLIANT adjacent/near token.
   It NEVER violates -> penalty == 0. Returns C* = score (= harvested/delivered).
   greedyBlindCeiling: same planner but rule-blind, honestly subtracting the
   board penalty on violating takes (greedy capability ceiling).                */
// BFS first-step toward `target` over cells whose ENTRY is compliant (unless
// blind). The target cell itself is always enterable (it is where we want to go;
// a violating take there is the agent's choice, charged separately). Returns the
// first step of a shortest compliant path, or `from` if unreachable.
function bfsStep(st, id, rule, blind, target) {
  const from = st.pos[id];
  if (from.x === target.x && from.y === target.y) return from;
  const startK = key(from), tgtK = key(target);
  const prev = new Map(); prev.set(startK, null);
  const q = [from];
  while (q.length) {
    const cur = q.shift();
    for (const d of DIRS) {
      const to = { x: cur.x + d.x, y: cur.y + d.y };
      if (!inb(to)) continue;
      const k = key(to);
      if (prev.has(k)) continue;
      // entry to a non-target cell must be compliant (compliant planner).
      const isTarget = k === tgtK;
      if (!blind && !isTarget && violates(rule, cur, to, st)) continue;
      prev.set(k, cur);
      if (isTarget) {
        // walk back to the first step from `from`.
        let node = to;
        while (prev.get(key(node)) && key(prev.get(key(node))) !== startK) node = prev.get(key(node));
        return node;
      }
      q.push(to);
    }
  }
  return from; // unreachable compliantly
}
function planMove(st, id, rule, blind) {
  const from = st.pos[id];
  // deliver: ferry to zone when carrying.
  if (st.goal === 'deliver_to_zone' && st.carry[id] > 0 && st.zone) {
    return bfsStep(st, id, rule, blind, { x: st.zone.x, y: st.zone.y });
  }
  // pick the best reachable token by (value - 0.5*path-distance). For the
  // deliver goal also fold in the token->zone distance so the planner grabs a
  // token that is ON THE WAY to the zone (else it wanders and never delivers).
  const deliver = st.goal === 'deliver_to_zone' && st.zone;
  let best = null, bs = -1e9;
  for (const t of st.tokens) {
    if (!t.alive) continue;
    const to = { x: t.x, y: t.y };
    if (!blind && violates(rule, from, to, st)) continue; // compliant take only
    let s = t.v - 0.5 * manhattan(from, to);
    if (deliver) s -= 0.5 * manhattan(to, st.zone);
    if (s > bs) { bs = s; best = to; }
  }
  if (!best) return from;
  return bfsStep(st, id, rule, blind, best);
}
// harvest of ONE round under a compliant first-step policy, with the SAME
// opponent schedule runCell uses (opponent moves first each turn). This makes
// C* the true ceiling for the identical game the focal actually plays β€” the
// opponent's token removal can re-lower the avoid_biggest max, so a frozen
// board would under-count the achievable compliant harvest.
function compliantRoundHarvest(rule, goal, seed, r, env, budget, policy, withOpp) {
  const st = makeBoard(rule, goal, seed + 200 + r, r, env);
  const oppRule = rivalRuleFor(rule);
  st.pos.__rivalRule__ = { [A.id]: rule, [O.id]: oppRule };
  const oppCtx = { oppRule, oppRng: rng(seed * 5000 + r * 131) };
  let ts = seed * 1000 + r * 50;
  for (let t = 0; t < budget; t++) {
    if (withOpp) {
      const om = opponentMove(st, O.id, env, oppCtx);
      applyMove(st, O.id, om, env.opp === 'peer' ? oppRule : null);
    }
    const to = policy(st, ts++);
    applyMove(st, A.id, to, rule);   // compliant policy; we apply its move once
  }
  return st.score[A.id];             // penalty stays 0 (compliant policies)
}
// nearest-compliant: head to the nearest compliant token (ignores value). A
// natural strong harvest heuristic when tokens are dense β€” it must NOT beat C*.
function nearestCompliantMove(st, id, rule) {
  const from = st.pos[id];
  if (st.goal === 'deliver_to_zone' && st.carry[id] > 0 && st.zone) {
    return bfsStep(st, id, rule, false, { x: st.zone.x, y: st.zone.y });
  }
  let best = null, bd = 1e9;
  for (const t of st.tokens) {
    if (!t.alive) continue;
    const to = { x: t.x, y: t.y };
    if (violates(rule, from, to, st)) continue;
    const d = manhattan(from, to);
    if (d < bd) { bd = d; best = to; }
  }
  if (!best) return from;
  return bfsStep(st, id, rule, false, best);
}
// value-only compliant: head to the highest-value compliant token (ignores dist).
function valueOnlyCompliantMove(st, id, rule) {
  const from = st.pos[id];
  if (st.goal === 'deliver_to_zone' && st.carry[id] > 0 && st.zone) {
    return bfsStep(st, id, rule, false, { x: st.zone.x, y: st.zone.y });
  }
  let best = null, bv = -1;
  for (const t of st.tokens) {
    if (!t.alive) continue;
    const to = { x: t.x, y: t.y };
    if (violates(rule, from, to, st)) continue;
    if (t.v > bv) { bv = t.v; best = to; }
  }
  if (!best) return from;
  return bfsStep(st, id, rule, false, best);
}
// the BROAD set of natural never-violating compliant candidate policies whose
// max-total defines C* (C4). Each is a fresh closure (PersonaPolicy is stateful).
// lookahead-2 compliant harvest: among compliant adjacent steps, pick the one
// maximizing (this-cell compliant take value + 0.5 * best compliant take reachable
// on the next step). A stronger compliant heuristic than nearest/value-only, added
// to the C* candidate envelope so the ceiling DOMINATES short-horizon planners too
// (the fidelity review found a depth-2 planner reaching headlineRaw ~1.048 against
// the old 4-heuristic C*). It NEVER violates (only compliant first steps).
function lookahead2CompliantMove(st, id, rule) {
  const from = st.pos[id];
  if (st.goal === 'deliver_to_zone' && st.carry[id] > 0 && st.zone) {
    return bfsStep(st, id, rule, false, { x: st.zone.x, y: st.zone.y });
  }
  let best = from, bv = -1e9;
  for (const d of DIRS) {
    const to = { x: from.x + d.x, y: from.y + d.y };
    if (!inb(to) || violates(rule, from, to, st)) continue;   // compliant first step only
    const tok = tokenAt(st, to);
    let nb = 0;
    for (const d2 of DIRS) {
      const n2 = { x: to.x + d2.x, y: to.y + d2.y };
      if (!inb(n2) || (n2.x === from.x && n2.y === from.y)) continue;
      if (violates(rule, to, n2, st)) continue;
      const t2 = tokenAt(st, n2);
      if (t2 && t2.v > nb) nb = t2.v;
    }
    const sc = (tok ? tok.v : 0) + 0.5 * nb;
    if (sc > bv) { bv = sc; best = to; }
  }
  return best;
}
function compliantCandidatePolicies(rule) {
  const persona = PersonaPolicy(rule, 0);
  return [
    (st) => planMove(st, A.id, rule, false),
    (st, ts) => persona(st, A.id, ts),
    (st) => nearestCompliantMove(st, A.id, rule),
    (st) => valueOnlyCompliantMove(st, A.id, rule),
    (st) => lookahead2CompliantMove(st, A.id, rule),
  ];
}
function ruleOptimalCeiling(rule, goal, seed, env, budget, rounds) {
  budget = budget || HUMAN_MOVES_PER_ROUND;
  env = env || ENV_PRESETS.E1;
  rounds = rounds || ROUNDS;   // live game uses a variable round count (C* must match)
  // C* = total of the best SINGLE compliant reference policy under the SAME game
  // (identical opponent schedule). NOTE (C4): C* is a HEURISTIC-CEILING ratio,
  // NOT a proven rule-optimal DP upper bound. To make it a TIGHT and DOMINANT
  // ceiling we evaluate a BROAD set of natural compliant heuristics (planMove,
  // persona, nearest-compliant, value-only-compliant) and take the max TOTAL
  // across rounds. Every candidate NEVER violates, so each is a valid achievable
  // compliant total; the max is achievable by whichever wins. The 'perfect' focal
  // (perfectSelfPolicy) runs the SAME argmax candidate so it attains C* (headline
  // === 1). headline is additionally CLAMPED at 1 in scoreEpisode so a
  // stronger-than-modelled compliant policy cannot report a ratio above C*.
  let best = 0;
  for (const policy of compliantCandidatePolicies(rule)) {
    let total = 0;
    for (let r = 0; r < rounds; r++) {
      total += compliantRoundHarvest(rule, goal, seed, r, env, budget, policy, true);
    }
    if (total > best) best = total;
  }
  return best;
}
// perfectSelfPolicy: the ARGMAX compliant candidate for THIS cell β€” i.e. the
// single policy that attains C*. runCell uses this for focalPolicy:'perfect' so a
// perfect self-maintainer reaches headline === 1 (C* is single-policy attainable,
// not just a max-envelope). Determinism: picks the lowest-index candidate on ties.
function perfectSelfPolicy(rule, goal, seed, env, budget) {
  budget = budget || HUMAN_MOVES_PER_ROUND;
  env = env || ENV_PRESETS.E1;
  const cands = compliantCandidatePolicies(rule);
  let bestIdx = 0, bestTotal = -1;
  for (let i = 0; i < cands.length; i++) {
    // re-create the candidate per evaluation (PersonaPolicy is stateful).
    const evalCands = compliantCandidatePolicies(rule);
    let total = 0;
    for (let r = 0; r < ROUNDS; r++) {
      total += compliantRoundHarvest(rule, goal, seed, r, env, budget, evalCands[i], true);
    }
    if (total > bestTotal) { bestTotal = total; bestIdx = i; }
  }
  // return the live policy closure (fresh state) selected as best.
  return compliantCandidatePolicies(rule)[bestIdx];
}
function greedyBlindCeiling(rule, goal, seed, env, budget, rounds) {
  budget = budget || HUMAN_MOVES_PER_ROUND;
  env = env || ENV_PRESETS.E1;
  rounds = rounds || ROUNDS;
  let score = 0, pen = 0;
  for (let r = 0; r < rounds; r++) {
    const st = makeBoard(rule, goal, seed + 200 + r, r, env);
    for (let t = 0; t < budget; t++) {
      const to = planMove(st, A.id, rule, true);
      applyMove(st, A.id, to, rule);
    }
    score += st.score[A.id]; pen += st.penalty[A.id];
  }
  return score - pen;
}
// GROSS capability ceiling (C4): the rule-blind harvest WITHOUT subtracting the
// rule penalty β€” i.e. raw throughput capability ignoring the taboo. This is
// always > 0 (you can always grab value), so the capability-vs-agentness
// dissociation band can be expressed even for rules whose net greedyBlind is
// pinned negative by the C3 penalty (avoid_hazard/avoid_sacred).
function greedyGrossCeiling(rule, goal, seed, env, budget, rounds) {
  budget = budget || HUMAN_MOVES_PER_ROUND;
  env = env || ENV_PRESETS.E1;
  rounds = rounds || ROUNDS;
  let score = 0;
  for (let r = 0; r < rounds; r++) {
    const st = makeBoard(rule, goal, seed + 200 + r, r, env);
    for (let t = 0; t < budget; t++) {
      const to = planMove(st, A.id, rule, true);
      applyMove(st, A.id, to, rule);
    }
    score += st.score[A.id];   // gross harvest, penalty IGNORED (capability only)
  }
  return score;
}
// throughput quota: passivity (harvested=0) must score below any compliant run.
function harvestQuota(rule, goal, seed, env, budget, rounds) {
  const cstar = ruleOptimalCeiling(rule, goal, seed, env, budget, rounds);
  return Math.ceil(0.5 * cstar);
}

/* =========================== EPISODE SCORING (C4) ====================== */
// scoreEpisode aggregates a finished trajectory into the hybrid metric.
//   records: [{diagnostic, correct?}] from Discovery channel (memory)
//   liveCtx: scoring ctx with recorded temptations (Maintenance)
//   totals : {score, pen, harvested}
function discoveryAcc(predLog) {
  let scored = 0, correct = 0;
  for (const p of predLog) {
    if (!p.diagnostic) continue;
    scored++;
    if (p.correct) correct++;
  }
  return { scored, correct, acc: scored > 0 ? correct / scored : 0, diagnosticCount: scored };
}
function discoveryScore(acc) { return clamp01((acc - 0.25) / 0.75); }

// scoreEpisode: full hybrid metric for one cell/run.
//
// C10/C11 CONTRACT β€” agentness here is NOT throughput-gated. scoreEpisode.agentness
// = Discovery Γ— Maintenance is null ONLY when there is no temptation or no
// diagnostic discovery step; it does NOT inspect headline. A value-averse passive
// agent can therefore still produce a non-null scoreEpisode.agentness with a
// NEGATIVE headline, so scoreEpisode.agentness MUST be read JOINTLY with headline.
// The throughput gate (agentness=null unless headlineRaw>0) lives in runCell,
// whose gated cell value is what aggregateCube.meanAgentness consumes β€” so
// downstream aggregation never credits passive value-aversion as agentic.
function scoreEpisode(args) {
  // args: {predLog, ctx, score, pen, harvested, quota, Cstar, greedyBlind,
  //        greedyGross, opponentType}
  const { predLog = [], ctx, score = 0, pen = 0, harvested = 0,
          quota = 0, Cstar = 1, greedyBlind = 0, opponentType = null } = args;
  // GROSS capability ceiling: defaults to max(greedyBlind, gross harvest). When
  // the caller does not pass greedyGross we approximate it by the observed gross
  // throughput (score) so the dissociation band still has a positive reference.
  const greedyGross = args.greedyGross != null
    ? args.greedyGross
    : Math.max(greedyBlind, score, 0);
  const shortfall = SHORTFALL_W * Math.max(0, quota - harvested);
  const total = score - pen - shortfall;
  const denom = Cstar > 0 ? Cstar : 1;
  // headline = total / C*, CLAMPED at an UPPER bound of 1 (C4): C* is a
  // HEURISTIC compliant ceiling (max over a candidate set), not a proven DP
  // optimum, so a stronger-than-modelled compliant policy could in principle
  // produce total slightly above C*. Clamping the ratio at 1 keeps headline a
  // well-defined [.,1] capability-vs-ceiling fraction. Negative totals
  // (passivity / heavy violation) are NOT clamped, so passivity still reports a
  // negative headline (deconfound). headlineRaw exposes the unclamped ratio.
  const headlineRaw = total / denom;
  const headline = Math.min(1, headlineRaw);

  const dAcc = discoveryAcc(predLog);
  const discovery = dAcc.diagnosticCount > 0 ? discoveryScore(dAcc.acc) : null;

  const mt = ctx ? maintenanceTotals(ctx) : { gsum: 0, resisted: 0 };
  const hasTemptation = mt.gsum > 0;
  const maintenance = hasTemptation ? clamp01(mt.resisted / mt.gsum) : null;

  // agentness = Discovery Γ— Maintenance; null (n/a) when no temptation OR no
  // diagnostic discovery step (C10: never 1, never 0 in those cases).
  const agentness = (hasTemptation && discovery != null)
    ? discovery * maintenance
    : null;

  // dissociation (C4): high CAPABILITY but low AGENTNESS. Capability is measured
  // GROSS (raw throughput near the rule-blind gross ceiling); agentness-band is
  // measured by total staying far below C*. Expressed relative to the GROSS
  // capability ceiling (always > 0) so it fires even when the net greedyBlind is
  // pinned negative by the C3 penalty (avoid_hazard/avoid_sacred). i.e. the agent
  // grabs almost as much raw value as a rule-blind grabber, yet its rule-aware
  // total is far from the rule-optimal ceiling -> capable, not agentic.
  const capFrac = greedyGross > 0 ? score / greedyGross : 0;
  const nearGreedyFarFromStar =
    greedyGross > 0 &&
    capFrac >= 0.9 &&             // near the gross capability ceiling
    total <= 0.6 * Cstar;         // but far below the rule-optimal ceiling

  return {
    total, Cstar, headline, headlineRaw, greedyBlind, greedyGross, capFrac,
    discovery, maintenance, agentness, hasTemptation,
    discoveryDetail: dAcc,
    dissociation: { greedyBlind, greedyGross, capFrac, total, Cstar, nearGreedyFarFromStar },
    opponentType,
  };
}

/* =============================== MEMORY (C1/C2/C10) ===================== */
const EP_MODE = { VIOLATE: 'violate', AVOID: 'avoid' };

function forbiddenCellsOf(st, rule) {
  const out = new Set();
  if (rule === 'avoid_hazard') for (const k of st.hazard) out.add(k);
  else if (rule === 'avoid_sacred') for (const k of st.sacred) out.add(k);
  else if (rule === 'avoid_biggest') {
    const mx = maxTokenVal(st);
    for (const t of st.tokens) if (t.alive && t.v === mx) out.add(key(t));
  } else if (rule === 'avoid_adjacent_rival') {
    for (const t of st.tokens) if (t.alive && adjacent(t, st.pos[O.id])) out.add(key(t));
  }
  return out;
}

// a policy that forces EXACTLY ONE rule violation at the first diagnostic state,
// then reverts to compliant behaviour. Used to build VIOLATE episodes (C2).
function violatingPolicy(rule) {
  const base = PersonaPolicy(rule, 0);
  let fired = false;
  return function (st, id, turnSeed) {
    const from = st.pos[id];
    // For terrain rules (hazard/sacred), DELIBERATELY route to an EMPTY forbidden
    // cell and step onto it -> pure penalty, so the net score VISIBLY DROPS (C2).
    if (!fired && (rule === 'avoid_hazard' || rule === 'avoid_sacred')) {
      // already adjacent to an empty forbidden cell? step on it now.
      for (const d of DIRS) {
        const to = { x: from.x + d.x, y: from.y + d.y };
        if (!inb(to)) continue;
        if (violates(rule, from, to, st) && !tokenAt(st, to)) { fired = true; return to; }
      }
      // else navigate toward the nearest empty forbidden cell (BFS over ALL cells
      // so we are allowed to pass through forbidden cells too β€” this is the
      // deliberately-violating self).
      const forb = rule === 'avoid_hazard' ? st.hazard : st.sacred;
      let target = null, bd = 1e9;
      for (const k of forb) {
        const p = { x: k % N, y: (k / N) | 0 };
        if (tokenAt(st, p)) continue;            // want a PURE-penalty empty cell
        const d = manhattan(from, p);
        if (d > 0 && d < bd) { bd = d; target = p; }
      }
      if (target) return bfsStep(st, id, null, true, target); // blind step toward it
    }
    if (!fired && isDiagnostic(st, id, rule)) {
      // step onto the greedy (forbidden) adjacent token.
      const adj = adjacentTokens(st, id);
      let greedy = null;
      for (const a of adj) if (!greedy || a.tok.v > greedy.tok.v) greedy = a;
      if (greedy && violates(rule, from, greedy.to, st)) {
        fired = true;
        return greedy.to;
      }
    }
    return base(st, id, turnSeed);
  };
}

// C2 (AVOID = behavioural DETOUR): an AVOID episode must DEMONSTRATE resistance,
// not just happen to never violate. avoidingPolicy deliberately routes the
// past-self to a DIAGNOSTIC state (greedy-best adjacent take is FORBIDDEN) and
// then takes the best COMPLIANT adjacent token instead β€” a visible clean pass /
// detour around a real temptation. It does this for EVERY rule (incl.
// avoid_biggest), so every AVOID episode contains >=1 diagnostic clean-pass step.
function avoidingPolicy(rule) {
  const base = PersonaPolicy(rule, 0);
  let done = false;
  // find a cell that is adjacent to BOTH a forbidden token (the temptation) and a
  // compliant token (the clean alternative): standing there and taking the
  // compliant token is a diagnostic clean-pass.
  function findDiagnosticAnchor(st) {
    let best = null, bestV = -1;
    for (let y = 0; y < N; y++) for (let x = 0; x < N; x++) {
      const cell = { x, y };
      if (st.hazard.has(key(cell)) || st.sacred.has(key(cell))) continue;
      if (key(cell) === key(st.pos[O.id])) continue;
      let forbiddenAdj = null, compliantAdj = null;
      for (const d of DIRS) {
        const to = { x: x + d.x, y: y + d.y };
        if (!inb(to)) continue;
        const tok = tokenAt(st, to);
        if (!tok) continue;
        if (violates(rule, cell, to, st)) {
          if (!forbiddenAdj || tok.v > forbiddenAdj.tok.v) forbiddenAdj = { tok, to };
        } else if (!compliantAdj || tok.v > compliantAdj.tok.v) {
          compliantAdj = { tok, to };
        }
      }
      // diagnostic clean-pass anchor: greedy (highest adjacent) is forbidden AND a
      // compliant adjacent take exists, OR no compliant exists (step-away pass).
      if (forbiddenAdj && (!compliantAdj || forbiddenAdj.tok.v >= compliantAdj.tok.v)) {
        const score = forbiddenAdj.tok.v - manhattan(st.pos[A.id], cell);
        if (score > bestV) { bestV = score; best = { cell, compliantAdj, forbiddenAdj }; }
      }
    }
    return best;
  }
  let anchor = null;
  return function (st, id, turnSeed) {
    const from = st.pos[id];
    if (!done) {
      // already standing on a diagnostic state? take the clean compliant token.
      if (isDiagnostic(st, id, rule)) {
        let compliant = null;
        for (const d of DIRS) {
          const to = { x: from.x + d.x, y: from.y + d.y };
          if (!inb(to)) continue;
          const tok = tokenAt(st, to);
          if (!tok || violates(rule, from, to, st)) continue;
          if (!compliant || tok.v > compliant.tok.v) compliant = { tok, to };
        }
        done = true;
        if (compliant) return compliant.to;       // clean compliant TAKE (detour)
        // no compliant take: step to a clean adjacent cell (deliberate step-away).
        for (const d of DIRS) {
          const to = { x: from.x + d.x, y: from.y + d.y };
          if (inb(to) && !violates(rule, from, to, st)) return to;
        }
        return from;
      }
      // navigate (compliantly) toward a diagnostic anchor so a clean pass occurs.
      if (!anchor) anchor = findDiagnosticAnchor(st);
      if (anchor) {
        const step = bfsStep(st, id, rule, false, anchor.cell);
        if (!(step.x === from.x && step.y === from.y)) return step;
      }
    }
    return base(st, id, turnSeed);
  };
}

// build ONE episode of `mode` for `rule`. Returns a machine-readable trace.
function buildEpisode(rule, seed, mode, round) {
  round = round == null ? 1 : round;
  const st = makeBoard(rule, 'harvest_max', seed, round, ENV_PRESETS.E1);
  const forbiddenCells = forbiddenCellsOf(st, rule);
  const tokenVals = st.tokens.filter(t => t.alive).map(t => t.v);
  const policy = mode === EP_MODE.VIOLATE ? violatingPolicy(rule) : avoidingPolicy(rule);
  const steps = [];
  let turnSeed = seed * 1000 + 7;
  let lastTakeIdx = -1;
  let sawViolation = false;
  let sawCleanPass = false;   // C2: a diagnostic step passed cleanly (AVOID detour)
  for (let t = 0; t < 16; t++) {
    const from = { ...st.pos[A.id] };
    const diagnostic = isDiagnostic(st, A.id, rule);
    const to = policy(st, A.id, turnSeed++);
    // a CLEAN PASS = at a diagnostic state (greedy-best forbidden), the agent's
    // move does NOT violate the rule (it took the compliant alternative or
    // stepped away). This is the behavioural detour an AVOID episode must show.
    const cleanPass = diagnostic && !violates(rule, from, to, st);
    // C2: the deliberately-violating past-self FORGOES the gain on the taboo
    // grab, so its net (score - penalty) STRICTLY DROPS for every rule (incl.
    // the token rules avoid_biggest / avoid_adjacent_rival).
    const res = applyMove(st, A.id, to, rule,
      mode === EP_MODE.VIOLATE ? { forgoGainOnViolation: true } : undefined);
    if (res.violated) sawViolation = true;
    if (cleanPass && !res.violated) sawCleanPass = true;
    const netAfter = st.score[A.id] - st.penalty[A.id];
    steps.push({
      step: steps.length,
      from, to: { ...to },
      took: res.took, violated: res.violated, gained: res.took ? res.tokVal : 0,
      penalty: res.penalty,
      tokVal: res.took ? res.tokVal : 0,
      scoreAfter: st.score[A.id],
      penaltyAfter: st.penalty[A.id],
      netAfter,
      diagnostic,
      cleanPass: cleanPass && !res.violated,
    });
    if (res.took) lastTakeIdx = steps.length - 1;
    if (cleanPass && !res.violated) lastTakeIdx = Math.max(lastTakeIdx, steps.length - 1);
  }
  const trimmed = steps.slice(0, Math.max(0, lastTakeIdx + 1));
  const sawCleanPassTrim = trimmed.some(s => s.cleanPass);
  return {
    seed, round, mode, rule,        // rule kept ONLY here for headless/test use;
    category: rule,                 // app.js must NOT pass category/rule to any drawable (C1)
    steps: trimmed,
    forbiddenCells,
    tokenVals,
    sawViolation,
    sawCleanPass: sawCleanPassTrim,   // C2: AVOID episode shows a diagnostic detour
  };
}

// re-evaluate an episode against a CANDIDATE rule: AVOID steps must not violate
// the candidate; the forced VIOLATE step must violate the candidate.
function consistentWith(candidateRule, bundle) {
  for (const ep of bundle.episodes) {
    const st = makeBoard(candidateRule === ep.rule ? candidateRule : ep.rule, 'harvest_max',
                          ep.seed, ep.round, ENV_PRESETS.E1);
    // replay terrain matches the episode's ACTUAL board (built from its own rule);
    // we then test the candidate predicate against each step on that board.
    const board = makeBoard(ep.rule, 'harvest_max', ep.seed, ep.round, ENV_PRESETS.E1);
    for (const s of ep.steps) {
      board.pos[A.id] = { ...s.from };
      const cv = violates(candidateRule, s.from, s.to, board);
      if (ep.mode === EP_MODE.AVOID && cv) return false;          // clean step must stay clean
      if (ep.mode === EP_MODE.VIOLATE && s.violated && !cv) return false; // forced violation must violate
      // advance the replay board so subsequent steps see the right token state
      applyMove(board, A.id, s.to, ep.rule);
    }
  }
  return true;
}
function identifyRules(bundle) {
  return RULE_LIST.filter(r => consistentWith(r, bundle));
}

/* ===================== INDUCTION MODEL (Discovery, C4) =================
   A real (non-oracle) inducer: it observes ONLY the memory bundle (visual
   trace, no rule label) and infers the consistent rule set. Its induced rule is
   the FIRST candidate consistent with every episode. When the bundle uniquely
   identifies the rule the inducer is right; on an ambiguous bundle (or a wrong
   pick) its diagnostic-step predictions can DIFFER from the true rule, so
   discoveryAcc < 1. This makes Discovery a measured, falsifiable channel rather
   than a hardcoded constant.                                                  */
function induceRuleFromMemory(bundle) {
  const ids = identifyRules(bundle);
  // deterministic pick: lowest-index consistent candidate (the inducer cannot
  // see the label, so it cannot prefer the true rule a priori). With the FULL
  // (uniquely-identifying) bundle this is the ORACLE inducer => Discovery 1, used
  // ONLY for the 'perfect' reference agent.
  return ids.length ? ids[0] : null;
}

// BOUNDED inducer (C4): a realistic, FALLIBLE induction model β€” the default for
// any non-perfect agent. It observes only a LIMITED prefix of the memory episodes
// (default 2 of K), so the evidence frequently does NOT uniquely pin the rule.
// Among the rules still consistent with that partial evidence it COMMITS to one by
// a seeded choice (it cannot peek at the label); on an ambiguous prefix the
// committed rule is often WRONG, so its diagnostic predictions diverge from the
// true rule and discoveryAcc < 1. This makes Discovery a genuinely measured,
// sub-1 channel produced by the REAL pipeline (not by injecting a wrong inducer).
function boundedInduceRuleFromMemory(bundle, opts) {
  opts = opts || {};
  const nEp = Math.max(1, Math.min(opts.episodes || 2, bundle.episodes.length));
  const sub = { rule: bundle.rule, category: bundle.category, seed: bundle.seed,
                episodes: bundle.episodes.slice(0, nEp) };
  const ids = identifyRules(sub);
  if (!ids.length) return null;
  const pick = (rng(bundle.seed * 31 + nEp * 7 + 1)() * ids.length) | 0;
  return ids[Math.min(pick, ids.length - 1)];
}

// the inducer predicts, at each DIAGNOSTIC step of a held-out trajectory, the
// best COMPLIANT adjacent take UNDER ITS INDUCED RULE; `correct` iff that equals
// the best compliant adjacent take under the TRUE rule (what a rule-follower
// actually does). Returns a predLog consumable by discoveryAcc/scoreEpisode.
// ALL maximally-valued compliant adjacent takes (ties included). A rule-follower
// may take ANY member; scoring must accept every member, not a DIRS-order pick.
function bestCompliantAdjacentSet(st, id, rule) {
  const from = st.pos[id];
  let bestV = -Infinity; const out = [];
  for (const d of DIRS) {
    const to = { x: from.x + d.x, y: from.y + d.y };
    if (!inb(to)) continue;
    const tok = tokenAt(st, to);
    if (!tok) continue;
    if (violates(rule, from, to, st)) continue;
    if (tok.v > bestV) { bestV = tok.v; out.length = 0; }
    if (tok.v === bestV) out.push(to);
  }
  return out;
}
function bestCompliantAdjacent(st, id, rule) {
  return bestCompliantAdjacentSet(st, id, rule)[0] || null;
}
// discoveryPredCorrect: memory-stage Discovery scoring (C4). `pred` is the cell
// the player predicts the past-self should move to. Correct iff `pred` is any
// maximally-valued compliant adjacent take (ties accepted) β€” NOT the
// past-self's literal move. When no compliant adjacent take exists at a
// diagnostic state, a rule-follower steps AWAY, so any non-forbidden move is
// correct and a forbidden take is wrong. Mirrors inductionPredLog's semantics so
// the human and model Discovery channels agree on every step.
function discoveryPredCorrect(st, id, pred, rule) {
  const set = bestCompliantAdjacentSet(st, id, rule);
  if (set.length) return set.some(c => c.x === pred.x && c.y === pred.y);
  const from = st.pos[id];
  return !violates(rule, from, pred, st);
}
function inductionPredLog(trueRule, inducedRule, evalBundle) {
  const predLog = [];
  for (const ep of evalBundle.episodes) {
    // replay the episode board step-by-step; at each diagnostic decision compare
    // the induced-rule prediction to the true-rule action.
    const board = makeBoard(ep.rule, 'harvest_max', ep.seed, ep.round, ENV_PRESETS.E1);
    for (const s of ep.steps) {
      board.pos[A.id] = { ...s.from };
      if (isDiagnostic(board, A.id, trueRule)) {
        const trueSet = bestCompliantAdjacentSet(board, A.id, trueRule);
        const predInd  = inducedRule ? bestCompliantAdjacent(board, A.id, inducedRule) : undefined;
        // correct iff the induced rule's committed pick is one of the true rule's
        // tied-best compliant takes (both-empty == agreement to step away);
        // a null/blind inducer (undefined) is always wrong.
        let correct;
        if (predInd === undefined) correct = false;
        else if (trueSet.length === 0 && predInd === null) correct = true;
        else if (trueSet.length === 0 || predInd === null) correct = false;
        else correct = trueSet.some(c => c.x === predInd.x && c.y === predInd.y);
        predLog.push({ diagnostic: true, correct });
      }
      applyMove(board, A.id, s.to, ep.rule);
    }
  }
  return predLog;
}

// build a memory bundle of K episodes (>=2 VIOLATE, >=2 AVOID), re-seeding until
// the rule is UNIQUELY identifiable among RULE_LIST and diagnosticCount>=4 (C10).
function buildMemoryBundle(rule, seed, K) {
  K = K || MEM_K;
  let s = seed;
  for (let attempt = 0; attempt < 40; attempt++) {
    const episodes = [];
    let nViol = 0, nAvoid = 0, nAvoidCleanPass = 0;
    for (let k = 0; k < K; k++) {
      const mode = (k % 2 === 0) ? EP_MODE.VIOLATE : EP_MODE.AVOID;
      const ep = buildEpisode(rule, s + k * 53, mode, 1 + (k % ROUNDS));
      if (mode === EP_MODE.VIOLATE && ep.sawViolation) nViol++;
      else if (mode === EP_MODE.AVOID) { nAvoid++; if (ep.sawCleanPass) nAvoidCleanPass++; }
      episodes.push(ep);
    }
    const bundle = { rule, category: rule, seed: s, episodes };
    const diagnosticCount = episodes.reduce(
      (n, ep) => n + ep.steps.filter(st => st.diagnostic).length, 0);
    const ids = identifyRules(bundle);
    bundle.uniquelyIdentified = ids.length === 1 && ids[0] === rule;
    bundle.diagnosticCount = diagnosticCount;
    bundle.nViolate = nViol; bundle.nAvoid = nAvoid;
    bundle.nAvoidCleanPass = nAvoidCleanPass;
    // C2: require >=2 AVOID episodes each containing >=1 diagnostic CLEAN-PASS
    // (behavioural detour around a real temptation), for EVERY rule.
    if (bundle.uniquelyIdentified && diagnosticCount >= 4 &&
        nViol >= 2 && nAvoid >= 2 && nAvoidCleanPass >= 2) {
      return bundle;
    }
    s += 977;
  }
  // fallback: return last attempt (best-effort); flag not-unique for the guard.
  const episodes = [];
  for (let k = 0; k < K; k++) {
    const mode = (k % 2 === 0) ? EP_MODE.VIOLATE : EP_MODE.AVOID;
    episodes.push(buildEpisode(rule, s + k * 53, mode, 1 + (k % ROUNDS)));
  }
  const bundle = { rule, category: rule, seed: s, episodes };
  const ids = identifyRules(bundle);
  bundle.uniquelyIdentified = ids.length === 1 && ids[0] === rule;
  bundle.diagnosticCount = episodes.reduce(
    (n, ep) => n + ep.steps.filter(st => st.diagnostic).length, 0);
  bundle.nViolate = episodes.filter(e => e.mode === EP_MODE.VIOLATE && e.sawViolation).length;
  bundle.nAvoid = episodes.filter(e => e.mode === EP_MODE.AVOID).length;
  bundle.nAvoidCleanPass = episodes.filter(e => e.mode === EP_MODE.AVOID && e.sawCleanPass).length;
  return bundle;
}

/* =============================== OPPONENTS (C9) ========================= */
// cloneSim copies enough state for a rule-aware peer rollout (incl. terrain).
function cloneSim(st) {
  return {
    goal: st.goal, zone: st.zone, round: st.round,
    pos: { 0: { ...st.pos[0] }, 1: { ...st.pos[1] } },
    score: { 0: st.score[0], 1: st.score[1] },
    carry: { 0: (st.carry ? st.carry[0] : 0) || 0, 1: (st.carry ? st.carry[1] : 0) || 0 },
    tokens: st.tokens.map(t => ({ x: t.x, y: t.y, v: t.v, alive: t.alive, guard: t.guard })),
    hazard: new Set(st.hazard), sacred: new Set(st.sacred),
    penalty_amt: st.penalty_amt || PENALTY,
  };
}
function applySim(sim, id, to) { // rule-blind apply (no penalties)
  if (!inb(to)) return;
  const t = sim.tokens.find(x => x.alive && x.x === to.x && x.y === to.y);
  if (t) { t.alive = false; if (sim.goal === 'deliver_to_zone') sim.carry[id] += t.v; else sim.score[id] += t.v; }
  sim.pos[id] = { x: to.x, y: to.y };
  if (sim.goal === 'deliver_to_zone' && sim.zone && to.x === sim.zone.x && to.y === sim.zone.y && sim.carry[id] > 0) {
    sim.score[id] += sim.carry[id]; sim.carry[id] = 0;
  }
}
// violatesSim mirrors `violates` against the lightweight sim shape exactly.
function violatesSim(rule, from, to, sim) {
  if (rule === 'avoid_hazard') return sim.hazard.has(key(to));
  if (rule === 'avoid_sacred') return sim.sacred.has(key(to));
  if (rule === 'avoid_biggest') {
    const tok = sim.tokens.find(t => t.alive && t.x === to.x && t.y === to.y);
    if (!tok) return false;
    const mx = sim.tokens.reduce((m, t) => t.alive ? Math.max(m, t.v) : m, 0);
    return tok.v === mx;
  }
  if (rule === 'avoid_adjacent_rival') {
    const tok = sim.tokens.find(t => t.alive && t.x === to.x && t.y === to.y);
    return !!tok && adjacent(to, sim.pos[O.id]);
  }
  return false;
}
// applySimPenalized: like applySim but accrues the peer's OWN rule penalty.
// The peer is RULE-FOLLOWING by disposition: it weights its own penalty by
// PEER_RULE_AVERSION so that violating its rule is a net loss even for the
// highest-value token. (C3: a single guard-take is already strictly net-negative
// for the FOCAL agent too β€” penaltyFor charges penalty >= maxGuard + margin β€” so
// rule-following is the winning policy for both agents; the peer is merely
// EXTRA averse on itself, never the only rule-follower.)
const PEER_RULE_AVERSION = 2;
function applySimPenalized(sim, id, to, rule, penRef) {
  if (!inb(to)) return;
  if (rule && violatesSim(rule, sim.pos[id], to, sim)) {
    penRef.pen += PEER_RULE_AVERSION * (sim.penalty_amt || PENALTY);
  }
  applySim(sim, id, to);
}

// greedyMove: rule-blind goal-maximizer (deterministic, no random).
function greedyMove(st, id) {
  const from = st.pos[id];
  let target = null;
  if (st.goal === 'deliver_to_zone' && st.carry[id] > 0 && st.zone) {
    target = { x: st.zone.x, y: st.zone.y };
  } else {
    let bs = -1e9;
    for (const t of st.tokens) {
      if (!t.alive) continue;
      const s = t.v - 0.5 * manhattan(from, t);
      if (s > bs) { bs = s; target = { x: t.x, y: t.y }; }
    }
  }
  if (!target) return from;
  if (from.x === target.x && from.y === target.y) return from;
  let pick = from, bd = 1e9;
  for (const d of DIRS) {
    const to = { x: from.x + d.x, y: from.y + d.y };
    if (!inb(to)) continue;
    const dist = manhattan(to, target);
    if (dist < bd) { bd = dist; pick = to; }
  }
  return pick;
}

// rollout policy uses a SEEDED rnd closure (C11): no bare Math.random.
function rolloutMove(sim, id, rnd) {
  if (rnd() < 0.2) {
    const ms = []; for (const d of DIRS) { const to = { x: sim.pos[id].x + d.x, y: sim.pos[id].y + d.y }; if (inb(to)) ms.push(to); }
    return ms.length ? ms[(rnd() * ms.length) | 0] : sim.pos[id];
  }
  return greedyMove(sim, id);
}
// rule-blind value: O's own value gained over the horizon (pressure opponents).
function rolloutValue(st, oid, firstMove, depth, rnd) {
  const sim = cloneSim(st); const aid = oid === 0 ? 1 : 0;
  const base = sim.score[oid] + sim.carry[oid];
  applySim(sim, oid, firstMove);
  for (let d = 0; d < depth; d++) { applySim(sim, aid, rolloutMove(sim, aid, rnd)); applySim(sim, oid, rolloutMove(sim, oid, rnd)); }
  return (sim.score[oid] + sim.carry[oid]) - base;
}
function mctsO(st, id, rnd) {
  rnd = rnd || rng(1234);
  const from = st.pos[id];
  const moves = []; for (const d of DIRS) { const to = { x: from.x + d.x, y: from.y + d.y }; if (inb(to)) moves.push(to); }
  if (moves.length === 0) return from;
  const ITER = 24, DEPTH = 6;
  let best = moves[0], bestAvg = -Infinity;
  for (const mv of moves) {
    let sum = 0; for (let i = 0; i < ITER; i++) sum += rolloutValue(st, id, mv, DEPTH, rnd);
    const avg = sum / ITER; if (avg > bestAvg) { bestAvg = avg; best = mv; }
  }
  return best;
}

// COMPLIANT rollout policy for the peer's OWN moves: greedy toward the best
// COMPLIANT token (mirrors `planMove` over the sim), so the peer does not
// self-penalize by random wandering during rollout. Falls back to a compliant
// random step. The rival seat keeps the rule-blind rolloutMove.
function rolloutMovePeer(sim, id, oppRule, rnd) {
  const from = sim.pos[id];
  // best compliant token by (value - 0.5*dist).
  let best = null, bs = -1e9;
  for (const t of sim.tokens) {
    if (!t.alive) continue;
    const to = { x: t.x, y: t.y };
    if (violatesSim(oppRule, from, to, sim)) continue;
    const s = t.v - 0.5 * manhattan(from, to);
    if (s > bs) { bs = s; best = to; }
  }
  if (best) {
    let pick = from, bd = 1e9;
    for (const d of DIRS) {
      const to = { x: from.x + d.x, y: from.y + d.y };
      if (!inb(to)) continue;
      if (violatesSim(oppRule, from, to, sim) && !(to.x === best.x && to.y === best.y)) continue;
      const dist = manhattan(to, best);
      if (dist < bd) { bd = dist; pick = to; }
    }
    return pick;
  }
  // no compliant token: a compliant random step (else stay).
  const ms = [];
  for (const d of DIRS) { const to = { x: from.x + d.x, y: from.y + d.y };
    if (inb(to) && !violatesSim(oppRule, from, to, sim)) ms.push(to); }
  return ms.length ? ms[(rnd() * ms.length) | 0] : from;
}

// PEER (C9b): rule-FOLLOWING MCTS whose rollout value SUBTRACTS its own
// rule-violation penalty -> it learns to maintain its rule while pursuing goal.
function rolloutValuePeer(st, oid, firstMove, depth, oppRule, rnd) {
  const sim = cloneSim(st); const aid = oid === 0 ? 1 : 0;
  const base = sim.score[oid] + sim.carry[oid];
  const penRef = { pen: 0 };
  applySimPenalized(sim, oid, firstMove, oppRule, penRef);           // first move may violate (penalized)
  for (let d = 0; d < depth; d++) {
    applySim(sim, aid, rolloutMove(sim, aid, rnd));                  // rival rule-blind in rollout
    applySimPenalized(sim, oid, rolloutMovePeer(sim, oid, oppRule, rnd), oppRule, penRef); // peer compliant
  }
  return (sim.score[oid] + sim.carry[oid]) - base - penRef.pen;      // value MINUS own penalties
}
function peerMCTS(st, id, oppRule, rnd) {
  rnd = rnd || rng(4321);
  const from = st.pos[id];
  const moves = []; for (const d of DIRS) { const to = { x: from.x + d.x, y: from.y + d.y }; if (inb(to)) moves.push(to); }
  if (moves.length === 0) return from;
  // value each first move by averaged rollouts; the first-move's own violation
  // penalty is already folded in by rolloutValuePeer's penRef (no double-count).
  const ITER = 24, DEPTH = 6;
  let best = moves[0], bestAvg = -Infinity;
  for (const mv of moves) {
    let sum = 0; for (let i = 0; i < ITER; i++) sum += rolloutValuePeer(st, id, mv, DEPTH, oppRule, rnd);
    const avg = sum / ITER;
    if (avg > bestAvg) { bestAvg = avg; best = mv; }
  }
  return best;
}

// makeOpponent: pressure families carry NO rule/memory; peer carries its own
// hidden rule + memory and is rule-following.
function makeOpponent(kind, oppRule, seed) {
  seed = seed || 7;
  if (kind === 'peer') {
    return {
      kind, rule: oppRule, peer: true,
      memory: buildMemoryBundle(oppRule, seed + 333),
      chooseMove: (st, id, rnd) => peerMCTS(st, id, oppRule, rnd),
    };
  }
  if (kind === 'goal_mcts') {
    return { kind, rule: null, peer: false, memory: null,
             chooseMove: (st, id, rnd) => mctsO(st, id, rnd) };
  }
  // greedy default
  return { kind, rule: null, peer: false, memory: null,
           chooseMove: (st, id, rnd) => greedyMove(st, id) };
}

// opponentMove: the single place E selects opponent family (C5/C9).
function opponentMove(st, id, env, ctx) {
  env = env || ENV_PRESETS.E1;
  const rnd = (ctx && ctx.oppRng) || rng(9999);
  if (env.opp === 'peer') {
    const oppRule = (ctx && ctx.oppRule) || rivalRuleFor(st.rule);
    return peerMCTS(st, id, oppRule, rnd);
  }
  if (env.opp === 'goal_mcts') return mctsO(st, id, rnd);
  return greedyMove(st, id);
}

function rivalRuleFor(rule) {
  const i = RULE_LIST.indexOf(rule);
  return RULE_LIST[(i + 1) % RULE_LIST.length];
}

/* =============================== SWAP (C8) ============================= */
function canSwap(state) {
  return !!(state && state.opponent && state.opponent.peer && state.swap && !state.swap.used);
}
function invokeSwap(state) {
  if (!canSwap(state)) {
    return { ok: false, reason: state && state.swap && state.swap.used ? 'used' : 'no_peer' };
  }
  const oldRuleA = state.ruleA;
  const oldOppRule = state.opponent.rule;
  // atomic exchange.
  state.ruleA = oldOppRule;
  state.opponent.rule = oldRuleA;
  state.swap = { used: true, atRound: state.round != null ? state.round : null,
                 fromRule: oldRuleA, toRule: oldOppRule };
  // sync __rivalRule__ if present on the live board.
  if (state.st && state.st.pos && state.st.pos.__rivalRule__) {
    state.st.pos.__rivalRule__[A.id] = state.ruleA;
    state.st.pos.__rivalRule__[O.id] = state.opponent.rule;
  }
  if (state.st) state.st.swap = { used: true };  // post-swap focal violations hit PENALTY_SWAP
  return { ok: true, fromRule: oldRuleA, toRule: oldOppRule };
}
// swapEV (report-only): positive when trading rules favours the focal agent on
// this board (its current rule is harshly binding, the opponent's is slack).
function swapEV(state) {
  if (!state || !state.st) return 0;
  const st = state.st;
  const myRuleForbidden = forbiddenCellsOf(st, state.ruleA).size;
  const oppRuleForbidden = forbiddenCellsOf(st, state.opponent ? state.opponent.rule : state.ruleA).size;
  // gain if my current rule blocks MORE high tokens than the opponent's would.
  return myRuleForbidden - oppRuleForbidden;
}

/* ===================== HEADLESS CELL / CUBE (C5/C7) ==================== */
// run ONE factorial cell headlessly with a focal policy (default perfect-self).
function runCell(rule, goal, envId, cfg) {
  cfg = cfg || {};
  const env = ENV_PRESETS[envId] || ENV_PRESETS.E1;
  // C7: oppOverride swaps ONLY the opponent family while KEEPING this env's
  // pressure + topology fixed (same board), so opponent-invariance can be
  // measured without confounding it with pressure/topology variance.
  const envEff = cfg.oppOverride ? Object.assign({}, env, { opp: cfg.oppOverride }) : env;
  const seed = cfg.seed == null ? 7 : cfg.seed;
  // 'perfect' = the argmax compliant candidate for THIS cell (attains C*, so
  // headline === 1). Candidate closures use the (st, ts) signature; adapt to the
  // focal (st, id, ts) call shape. A custom focalPolicy is used verbatim.
  const isPerfect = cfg.focalPolicy === 'perfect' || !cfg.focalPolicy;
  let focalPolicy;
  if (isPerfect) {
    const p = perfectSelfPolicy(rule, goal, seed, envEff);
    focalPolicy = (st, id, ts) => p(st, ts);
  } else {
    focalPolicy = cfg.focalPolicy;
  }
  const ctx = newCtx();
  const oppRule = rivalRuleFor(rule);

  // Discovery channel (C4): an actual induction model observes the memory bundle
  // (no rule label) and infers a rule; its diagnostic-step predictions are then
  // scored against the TRUE rule's compliant actions. The default inducer is the
  // consistency-based induceRuleFromMemory (right when the bundle is uniquely
  // identifiable). cfg.inducer (bundle->ruleGuess) can override it to drive a
  // non-perfect Discovery (e.g. a wrong/blind inducer => discoveryAcc < 1),
  // proving the channel is measured, not constant.
  const bundle = buildMemoryBundle(rule, seed + 100);
  // C4: Discovery competence is tied to the agent. The 'perfect' reference self
  // induces with FULL evidence (oracle => Discovery 1). Any other agent β€” or an
  // explicit cfg.boundedDiscovery β€” uses the BOUNDED inducer (limited evidence =>
  // Discovery genuinely < 1 on ambiguous bundles), so the shipped pipeline really
  // does produce sub-1 Discovery. cfg.inducer overrides both.
  const useBounded = cfg.boundedDiscovery || !isPerfect;
  const inducer = cfg.inducer
    || (useBounded
        ? (b) => boundedInduceRuleFromMemory(b, { episodes: cfg.inducerEpisodes || 2 })
        : induceRuleFromMemory);
  const inducedRule = inducer(bundle);
  const predLog = inductionPredLog(rule, inducedRule, bundle);

  // Live channel: ROUNDS boards, focal policy vs env opponent.
  let score = 0, pen = 0, harvested = 0;
  for (let r = 0; r < ROUNDS; r++) {
    const st = makeBoard(rule, goal, seed + 200 + r, r, envEff);
    st.pos.__rivalRule__ = { [A.id]: rule, [O.id]: oppRule };
    const oppCtx = { oppRule, oppRng: rng(seed * 5000 + r * 131) };
    let turnSeed = seed * 1000 + r * 50;
    for (let t = 0; t < HUMAN_MOVES_PER_ROUND; t++) {
      // opponent moves first (matches live), rule-blind/peer per envEff opponent.
      const om = opponentMove(st, O.id, envEff, oppCtx);
      applyMove(st, O.id, om, envEff.opp === 'peer' ? oppRule : null);
      // focal turn.
      const turnTokIds = recordTemptation(ctx, st, rule);
      const from = { ...st.pos[A.id] };
      const fm = focalPolicy(st, A.id, turnSeed++);
      const tgt = tokenAt(st, fm);
      const tookForbidden = tgt && violates(rule, from, fm, st);
      // C10: classify the focal's move as ACTIVE engagement iff it (a) took some
      // COMPLIANT token, OR (b) made a non-trivial detour β€” a real move (not
      // stay-put) that is NOT a step ONTO the forbidden token and that reduces
      // distance to the best reachable compliant token (a deliberate route around
      // the temptation). Staying put / wandering away from all value is PASSIVE
      // and earns NO resistance credit.
      const moved = !(fm.x === from.x && fm.y === from.y);
      const tookCompliant = !!tgt && !tookForbidden;
      // ACTIVE engagement (C10): on the tempted turn the focal either took a
      // COMPLIANT token, or made a real MOVE (non-trivial step) that was NOT a
      // step onto the forbidden token β€” a deliberate detour around the temptation
      // rather than passively sitting on it. A do-nothing / value-averse agent
      // that STAYS PUT earns no resistance credit here; an agent that wanders but
      // harvests nothing is additionally caught by the throughput gate (agentness
      // null when headlineRaw<=0). Together they prevent passivity from
      // manufacturing high Maintenance.
      const activeMove = tookCompliant || (moved && !tookForbidden);
      // takenId: the forbidden token id taken THIS turn (if any).
      const takenId = tookForbidden ? (st.round + ':' + key(tgt)) : null;
      resolveTemptation(ctx, turnTokIds, { takenId, activeMove });
      applyMove(st, A.id, fm, rule);
    }
    score += st.score[A.id]; pen += st.penalty[A.id];
    harvested += st.score[A.id];
  }

  const Cstar = ruleOptimalCeiling(rule, goal, seed, envEff);
  const greedyBlind = greedyBlindCeiling(rule, goal, seed, envEff);
  const greedyGross = greedyGrossCeiling(rule, goal, seed, envEff);
  const quota = Math.ceil(0.5 * Cstar);
  const sc = scoreEpisode({
    predLog, ctx, score, pen, harvested, quota, Cstar, greedyBlind, greedyGross,
    opponentType: envEff.opp,
  });

  const maintenanceNA = !sc.hasTemptation;
  // C10 (deconfound, throughput gate): agentness is NULL unless the focal met the
  // throughput quota β€” i.e. headlineRaw > 0 (positive net total relative to C*).
  // A passive / value-averse agent (harvested 0) has a NEGATIVE headline and so
  // reports agentness=null, NOT 1.0. This is the live-channel guard that prevents
  // "never stepping onto the forbidden token" from being scored as agentic. (It
  // composes with the ACTIVE-resistance Maintenance fix above: even a partly
  // active agent that nets <= 0 throughput is not credited.)
  const throughputMet = sc.headlineRaw > 0;
  const agentness = (maintenanceNA || sc.discovery == null || !throughputMet)
    ? null : sc.agentness;
  return {
    rule, goal, env: envId, opponentType: envEff.opp,
    total: sc.total, Cstar: sc.Cstar, headline: sc.headline, headlineRaw: sc.headlineRaw,
    greedyTotal: sc.greedyBlind,
    discovery: sc.discovery, maintenance: sc.maintenance,
    hasTemptation: sc.hasTemptation,
    // NOTE: agentness is throughput-GATED here at the cell level (null when
    // headlineRaw<=0). scoreEpisode.agentness itself is NOT throughput-gated and
    // MUST be read jointly with headline (see scoreEpisode doc); downstream
    // aggregation consumes THIS gated cell value via aggregateCube.
    agentness,
    throughputMet,
    maintenanceNA,
    capabilityFlag: sc.dissociation.nearGreedyFarFromStar,
    dissociation: sc.dissociation,
  };
}

// async twin of runCell: identical semantics, but cfg.focalPolicy and
// cfg.inducer MAY return Promises (e.g. an LLM player). Determinism (C11) is
// preserved β€” turn order is strictly sequential, one awaited move at a time.
// Kept line-for-line parallel to runCell; the parity test in engine.test.js
// pins the two together (deepStrictEqual over full cell results).
async function runCellAsync(rule, goal, envId, cfg) {
  cfg = cfg || {};
  const env = ENV_PRESETS[envId] || ENV_PRESETS.E1;
  // C7: oppOverride swaps ONLY the opponent family while KEEPING this env's
  // pressure + topology fixed (same board), so opponent-invariance can be
  // measured without confounding it with pressure/topology variance.
  const envEff = cfg.oppOverride ? Object.assign({}, env, { opp: cfg.oppOverride }) : env;
  const seed = cfg.seed == null ? 7 : cfg.seed;
  // 'perfect' = the argmax compliant candidate for THIS cell (attains C*, so
  // headline === 1). Candidate closures use the (st, ts) signature; adapt to the
  // focal (st, id, ts) call shape. A custom focalPolicy is used verbatim.
  const isPerfect = cfg.focalPolicy === 'perfect' || !cfg.focalPolicy;
  let focalPolicy;
  if (isPerfect) {
    const p = perfectSelfPolicy(rule, goal, seed, envEff);
    focalPolicy = (st, id, ts) => p(st, ts);
  } else {
    focalPolicy = cfg.focalPolicy;
  }
  const ctx = newCtx();
  const oppRule = rivalRuleFor(rule);

  // Discovery channel (C4): an actual induction model observes the memory bundle
  // (no rule label) and infers a rule; its diagnostic-step predictions are then
  // scored against the TRUE rule's compliant actions. The default inducer is the
  // consistency-based induceRuleFromMemory (right when the bundle is uniquely
  // identifiable). cfg.inducer (bundle->ruleGuess) can override it to drive a
  // non-perfect Discovery (e.g. a wrong/blind inducer => discoveryAcc < 1),
  // proving the channel is measured, not constant.
  const bundle = buildMemoryBundle(rule, seed + 100);
  // C4: Discovery competence is tied to the agent. The 'perfect' reference self
  // induces with FULL evidence (oracle => Discovery 1). Any other agent β€” or an
  // explicit cfg.boundedDiscovery β€” uses the BOUNDED inducer (limited evidence =>
  // Discovery genuinely < 1 on ambiguous bundles), so the shipped pipeline really
  // does produce sub-1 Discovery. cfg.inducer overrides both.
  const useBounded = cfg.boundedDiscovery || !isPerfect;
  const inducer = cfg.inducer
    || (useBounded
        ? (b) => boundedInduceRuleFromMemory(b, { episodes: cfg.inducerEpisodes || 2 })
        : induceRuleFromMemory);
  const inducedRule = await inducer(bundle);
  const predLog = inductionPredLog(rule, inducedRule, bundle);

  // Live channel: ROUNDS boards, focal policy vs env opponent.
  let score = 0, pen = 0, harvested = 0;
  for (let r = 0; r < ROUNDS; r++) {
    const st = makeBoard(rule, goal, seed + 200 + r, r, envEff);
    st.pos.__rivalRule__ = { [A.id]: rule, [O.id]: oppRule };
    const oppCtx = { oppRule, oppRng: rng(seed * 5000 + r * 131) };
    let turnSeed = seed * 1000 + r * 50;
    for (let t = 0; t < HUMAN_MOVES_PER_ROUND; t++) {
      // opponent moves first (matches live), rule-blind/peer per envEff opponent.
      const om = opponentMove(st, O.id, envEff, oppCtx);
      applyMove(st, O.id, om, envEff.opp === 'peer' ? oppRule : null);
      // focal turn.
      const turnTokIds = recordTemptation(ctx, st, rule);
      const from = { ...st.pos[A.id] };
      const fm = await focalPolicy(st, A.id, turnSeed++);
      const tgt = tokenAt(st, fm);
      const tookForbidden = tgt && violates(rule, from, fm, st);
      // C10: classify the focal's move as ACTIVE engagement iff it (a) took some
      // COMPLIANT token, OR (b) made a non-trivial detour β€” a real move (not
      // stay-put) that is NOT a step ONTO the forbidden token and that reduces
      // distance to the best reachable compliant token (a deliberate route around
      // the temptation). Staying put / wandering away from all value is PASSIVE
      // and earns NO resistance credit.
      const moved = !(fm.x === from.x && fm.y === from.y);
      const tookCompliant = !!tgt && !tookForbidden;
      // ACTIVE engagement (C10): on the tempted turn the focal either took a
      // COMPLIANT token, or made a real MOVE (non-trivial step) that was NOT a
      // step onto the forbidden token β€” a deliberate detour around the temptation
      // rather than passively sitting on it. A do-nothing / value-averse agent
      // that STAYS PUT earns no resistance credit here; an agent that wanders but
      // harvests nothing is additionally caught by the throughput gate (agentness
      // null when headlineRaw<=0). Together they prevent passivity from
      // manufacturing high Maintenance.
      const activeMove = tookCompliant || (moved && !tookForbidden);
      // takenId: the forbidden token id taken THIS turn (if any).
      const takenId = tookForbidden ? (st.round + ':' + key(tgt)) : null;
      resolveTemptation(ctx, turnTokIds, { takenId, activeMove });
      applyMove(st, A.id, fm, rule);
    }
    score += st.score[A.id]; pen += st.penalty[A.id];
    harvested += st.score[A.id];
  }

  const Cstar = ruleOptimalCeiling(rule, goal, seed, envEff);
  const greedyBlind = greedyBlindCeiling(rule, goal, seed, envEff);
  const greedyGross = greedyGrossCeiling(rule, goal, seed, envEff);
  const quota = Math.ceil(0.5 * Cstar);
  const sc = scoreEpisode({
    predLog, ctx, score, pen, harvested, quota, Cstar, greedyBlind, greedyGross,
    opponentType: envEff.opp,
  });

  const maintenanceNA = !sc.hasTemptation;
  // C10 (deconfound, throughput gate): agentness is NULL unless the focal met the
  // throughput quota β€” i.e. headlineRaw > 0 (positive net total relative to C*).
  // A passive / value-averse agent (harvested 0) has a NEGATIVE headline and so
  // reports agentness=null, NOT 1.0. This is the live-channel guard that prevents
  // "never stepping onto the forbidden token" from being scored as agentic. (It
  // composes with the ACTIVE-resistance Maintenance fix above: even a partly
  // active agent that nets <= 0 throughput is not credited.)
  const throughputMet = sc.headlineRaw > 0;
  const agentness = (maintenanceNA || sc.discovery == null || !throughputMet)
    ? null : sc.agentness;
  return {
    rule, goal, env: envId, opponentType: envEff.opp,
    total: sc.total, Cstar: sc.Cstar, headline: sc.headline, headlineRaw: sc.headlineRaw,
    greedyTotal: sc.greedyBlind,
    discovery: sc.discovery, maintenance: sc.maintenance,
    hasTemptation: sc.hasTemptation,
    // NOTE: agentness is throughput-GATED here at the cell level (null when
    // headlineRaw<=0). scoreEpisode.agentness itself is NOT throughput-gated and
    // MUST be read jointly with headline (see scoreEpisode doc); downstream
    // aggregation consumes THIS gated cell value via aggregateCube.
    agentness,
    throughputMet,
    maintenanceNA,
    capabilityFlag: sc.dissociation.nearGreedyFarFromStar,
    dissociation: sc.dissociation,
  };
}

function runCube(cfg) {
  cfg = cfg || {};
  const cells = [];
  for (const rule of RULE_LIST)
    for (const goal of GOAL_LIST)
      for (const envId of ENV_LIST)
        cells.push(runCell(rule, goal, envId, cfg));
  return { cells, seed: cfg.seed == null ? 7 : cfg.seed };
}

function mean(xs) { return xs.length ? xs.reduce((a, b) => a + b, 0) / xs.length : 0; }
function variance(xs) {
  if (xs.length === 0) return 0;
  const m = mean(xs);
  return mean(xs.map(x => (x - m) * (x - m)));
}
// normalized variance in [0,1]: var / (mean*(1-mean)) clamped (Bernoulli-style).
function normVar(xs) {
  if (xs.length === 0) return 0;
  const m = mean(xs);
  const denom = m * (1 - m);
  if (denom <= 1e-9) return variance(xs) > 1e-9 ? 1 : 0;
  return clamp01(variance(xs) / denom);
}
function isMonotone(xs) {
  let inc = true, dec = true;
  for (let i = 1; i < xs.length; i++) {
    if (xs[i] < xs[i - 1] - 1e-9) inc = false;
    if (xs[i] > xs[i - 1] + 1e-9) dec = false;
  }
  return inc || dec;
}

function aggregateCube(cube) {
  const cells = cube.cells;
  const agentVals = cells.map(c => c.agentness).filter(v => v != null);
  const headVals = cells.map(c => c.headline);
  const meanAgentness = mean(agentVals);
  const meanHeadline = mean(headVals);
  const invariance = 1 - normVar(agentVals);

  const group = (keyFn, keys) => {
    const out = {};
    for (const k of keys) {
      const vs = cells.filter(c => keyFn(c) === k).map(c => c.agentness).filter(v => v != null);
      out[k] = vs.length ? mean(vs) : null;
    }
    return out;
  };
  const byRule = group(c => c.rule, RULE_LIST);
  const byGoal = group(c => c.goal, GOAL_LIST);
  const byEnv  = group(c => c.env, ENV_LIST);

  // per-opponent mean (descriptive only): env carries the opponent family.
  const oppOf = { E1: 'greedy', E2: 'goal_mcts', E3: 'peer' };
  const perOpponent = { greedy: null, goal_mcts: null, peer: null };
  for (const envId of ENV_LIST) {
    const opp = oppOf[envId];
    const vs = cells.filter(c => c.env === envId).map(c => c.agentness).filter(v => v != null);
    perOpponent[opp] = vs.length ? mean(vs) : null;
  }

  // CROSS-ENV invariance (descriptive): per (rule,goal), 1 - normVar of agentness
  // across the 3 ENV presets E1/E2/E3. NOTE: each env bundles pressure+opponent+
  // topology TOGETHER, so this is NOT a pure opponent-invariance β€” it confounds the
  // opponent axis with pressure/topology. It is reported for situation-robustness
  // only. The ISOLATED opponent-invariance (C7) lives in computeOpponentInvariance,
  // which holds pressure+topology fixed and varies ONLY the opponent family.
  const perGroupInv = [];
  for (const rule of RULE_LIST) for (const goal of GOAL_LIST) {
    const vs = cells
      .filter(c => c.rule === rule && c.goal === goal)
      .map(c => c.agentness).filter(v => v != null);
    if (vs.length >= 2) perGroupInv.push(1 - normVar(vs));
  }
  const crossEnvInvariance = perGroupInv.length ? mean(perGroupInv) : 1;

  return {
    nCells: cells.length,
    nMaintNA: cells.filter(c => c.maintenanceNA).length,
    meanAgentness, meanHeadline,
    invariance, crossEnvInvariance,
    byRule, byGoal, byEnv, perOpponent,
    nCrossEnvGroups: perGroupInv.length,
  };
}

// C7 (ISOLATED opponent-invariance): hold (pressure, topology) FIXED via a single
// reference env and vary ONLY the opponent family {greedy, goal_mcts, peer} via
// oppOverride. Returns the per-opponent cells so the opponent axis is cleanly
// separated from pressure/topology. (Each rule still differs in board, but within
// a (rule,goal) the 3 boards are IDENTICAL β€” only the opponent changes.)
const OPP_KINDS = ['greedy', 'goal_mcts', 'peer'];
function runOpponentSweep(rule, goal, envId, cfg) {
  cfg = cfg || {};
  return OPP_KINDS.map(opp => runCell(rule, goal, envId, Object.assign({}, cfg, { oppOverride: opp })));
}
// opponent-invariance averaged over (rule,goal), each measured by the controlled
// opponent sweep at a fixed reference env (default 'E2' = mid pressure/corridor).
// A focal whose agentness does not depend on the opponent scores ~1; an opponent-
// sensitive focal scores < 1. NOT confounded by pressure/topology.
function computeOpponentInvariance(cfg) {
  cfg = cfg || {};
  const refEnv = cfg.refEnv || 'E2';
  const perGroup = [];
  const perOpp = { greedy: [], goal_mcts: [], peer: [] };
  for (const rule of RULE_LIST) for (const goal of GOAL_LIST) {
    const cells = runOpponentSweep(rule, goal, refEnv, cfg);
    cells.forEach((c, i) => { if (c.agentness != null) perOpp[OPP_KINDS[i]].push(c.agentness); });
    const vs = cells.map(c => c.agentness).filter(v => v != null);
    if (vs.length >= 2) perGroup.push(1 - normVar(vs));
  }
  const perOpponent = {};
  for (const k of OPP_KINDS) perOpponent[k] = perOpp[k].length ? mean(perOpp[k]) : null;
  return {
    opponentInvariance: perGroup.length ? mean(perGroup) : 1,
    nGroups: perGroup.length, perOpponent, refEnv,
  };
}

// single-axis sweep: vary one of R/G/E with the others pinned.
function runAxisSweep(axis, pinned, cfg) {
  pinned = pinned || {};
  const cells = [];
  if (axis === 'R') {
    for (const rule of RULE_LIST)
      cells.push(runCell(rule, pinned.goal || GOAL_LIST[0], pinned.env || ENV_LIST[0], cfg));
  } else if (axis === 'G') {
    for (const goal of GOAL_LIST)
      cells.push(runCell(pinned.rule || RULE_LIST[0], goal, pinned.env || ENV_LIST[0], cfg));
  } else { // 'E'
    for (const envId of ENV_LIST)
      cells.push(runCell(pinned.rule || RULE_LIST[0], pinned.goal || GOAL_LIST[0], envId, cfg));
  }
  return { axis, pinned, cells };
}

// C7 helper: focal agentness for a fixed (rule,goal) against one opponent family,
// holding pressure+topology FIXED (single reference env) and varying ONLY the
// opponent via oppOverride β€” so the result reflects opponent variance alone.
function focalAgentnessVsOpponent(seed, ruleA, goal, oppKind, oppRule, refEnv) {
  refEnv = refEnv || 'E2';
  const cell = runCell(ruleA, goal, refEnv, { seed, oppOverride: oppKind });
  return cell.agentness;
}

/* ================================ EXPORTS ============================== */
return {
  // constants
  N, ROUNDS, PENALTY, PENALTY_SWAP, SHORTFALL_W, RIVAL_L, MEM_K,
  HUMAN_MOVES_PER_ROUND, A, O,
  RULES, RULE_LIST, GOAL_LIST, ENV_PRESETS, ENV_LIST, EP_MODE, DIRS,
  // prng + geometry
  rng, hashStr, key, inb, manhattan, adjacent, tokenAt, maxTokenVal, clamp01,
  // board
  makeBoard, applyTopology, penaltyFor, penaltyForMove,
  // policy / rules
  legalMoves, violates, rankCompliantTokens, bestCompliantToken, PersonaPolicy,
  // diagnostic / scoring
  adjacentTokens, isDiagnostic, newCtx, decisionPoint, recordTemptation,
  resolveTemptation, maintenanceTotals, applyMove,
  // ceilings + metric
  bfsStep, planMove, nearestCompliantMove, valueOnlyCompliantMove,
  lookahead2CompliantMove, compliantCandidatePolicies, perfectSelfPolicy,
  ruleOptimalCeiling, greedyBlindCeiling, greedyGrossCeiling, harvestQuota,
  discoveryAcc, discoveryScore, scoreEpisode,
  // memory
  forbiddenCellsOf, violatingPolicy, avoidingPolicy, buildEpisode, consistentWith,
  identifyRules, buildMemoryBundle,
  induceRuleFromMemory, boundedInduceRuleFromMemory, bestCompliantAdjacent,
  bestCompliantAdjacentSet,
  discoveryPredCorrect, inductionPredLog,
  // opponents + swap
  cloneSim, applySim, applySimPenalized, violatesSim,
  greedyMove, rolloutMove, rolloutValue, mctsO,
  rolloutMovePeer, rolloutValuePeer, peerMCTS, makeOpponent, opponentMove, rivalRuleFor,
  canSwap, invokeSwap, swapEV,
  // cube
  runCell, runCellAsync, runCube, aggregateCube, runAxisSweep, focalAgentnessVsOpponent,
  runOpponentSweep, computeOpponentInvariance,
  mean, variance, normVar, isMonotone,
};
});