File size: 66,840 Bytes
7e43568
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
ffd18e2
7e43568
 
ffd18e2
7e43568
ffd18e2
 
7e43568
ffd18e2
7e43568
 
 
 
ffd18e2
7e43568
ffd18e2
 
 
 
 
 
 
 
7e43568
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
bea6321
7e43568
 
 
 
 
bea6321
 
 
 
 
7e43568
 
 
bea6321
 
 
 
 
 
 
 
7e43568
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
b596ed6
7e43568
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
b596ed6
7e43568
 
 
 
 
b596ed6
7e43568
 
 
 
b596ed6
 
 
 
 
 
7e43568
 
 
 
b596ed6
 
 
 
 
7e43568
 
 
 
 
 
 
 
b596ed6
 
 
7e43568
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
b596ed6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7e43568
 
 
 
 
 
 
 
b596ed6
 
 
7e43568
 
 
 
 
 
 
 
 
 
 
 
 
4ff38f2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7e43568
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4ff38f2
 
7e43568
 
 
 
 
 
 
 
 
 
b596ed6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7e43568
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
b596ed6
7e43568
b596ed6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7e43568
 
 
b596ed6
 
 
 
 
 
 
 
7e43568
b596ed6
7e43568
 
 
 
 
 
 
b596ed6
 
 
 
 
7e43568
 
 
b596ed6
 
 
 
7e43568
 
b596ed6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7e43568
b596ed6
7e43568
 
 
 
 
b596ed6
7e43568
 
 
b596ed6
 
 
 
 
 
7e43568
 
 
b596ed6
 
 
 
 
 
7e43568
 
 
b596ed6
7e43568
b596ed6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7e43568
b596ed6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7e43568
 
 
b596ed6
 
 
 
 
 
 
7e43568
b596ed6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7e43568
b596ed6
 
 
 
7e43568
b596ed6
 
 
 
 
 
 
 
 
 
 
 
7e43568
b596ed6
 
 
 
 
 
 
 
 
 
 
 
 
7e43568
 
 
 
 
 
 
 
 
 
 
 
b596ed6
7e43568
 
 
 
 
bea6321
 
 
 
 
 
 
 
 
 
 
ffd18e2
bea6321
 
ffd18e2
bea6321
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
ffd18e2
bea6321
 
 
 
 
 
 
ffd18e2
3f8ff82
 
 
 
 
 
ffd18e2
 
 
 
 
 
 
 
 
 
 
 
 
3f8ff82
 
 
 
 
 
ffd18e2
 
 
3f8ff82
 
ffd18e2
3f8ff82
 
 
 
 
 
 
 
 
 
 
 
ffd18e2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
bea6321
 
 
 
 
 
 
 
 
 
ffd18e2
 
bea6321
 
ffd18e2
bea6321
ffd18e2
bea6321
 
 
 
ffd18e2
 
 
 
bea6321
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
b596ed6
bea6321
 
 
ffd18e2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3f8ff82
 
 
 
 
 
 
 
 
 
ffd18e2
 
3f8ff82
 
 
ffd18e2
 
 
 
 
 
3f8ff82
 
 
 
 
 
ffd18e2
 
3f8ff82
ffd18e2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
bea6321
 
7e43568
 
 
 
 
 
b596ed6
bea6321
ffd18e2
b596ed6
 
7e43568
 
 
 
bea6321
 
 
 
7e43568
 
 
 
 
 
b596ed6
 
 
 
7e43568
 
 
 
4ff38f2
7e43568
 
4ff38f2
b596ed6
7e43568
bea6321
 
 
 
 
 
 
 
 
ffd18e2
 
 
7e43568
 
 
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
#!/usr/bin/env python3
"""Soci Agent NN β€” Self-Improvement Pipeline

Collects training data from the live simulation, retrains the ONNX model,
and pushes the improved version back to HuggingFace Hub.

Three modes:
    python nn_selfimprove.py collect   β€” Watch live sim, collect training samples
    python nn_selfimprove.py train     β€” Retrain NN on collected data
    python nn_selfimprove.py push      β€” Push improved model to HF Hub
    python nn_selfimprove.py all       β€” Do all three in sequence

Requires: pip install torch onnx onnxruntime httpx huggingface_hub numpy
"""

from __future__ import annotations

import argparse
import asyncio
import json
import logging
import math
import os
import random
import sys
import time
from collections import Counter
from dataclasses import dataclass
from pathlib import Path
from typing import Optional

import httpx
import numpy as np

logging.basicConfig(
    level=logging.INFO,
    format="%(asctime)s %(levelname)s %(name)s: %(message)s",
)
logger = logging.getLogger("nn_selfimprove")

# ── Paths ────────────────────────────────────────────────────────────────

SCRIPT_DIR = Path(__file__).parent
PROJECT_DIR = SCRIPT_DIR.parent
DATA_DIR = PROJECT_DIR / "data" / "nn_training"
SAMPLES_FILE = DATA_DIR / "collected_samples.jsonl"
MODEL_DIR = PROJECT_DIR / "models"
BEST_PT = MODEL_DIR / "soci_agent_best.pt"
ONNX_PATH = MODEL_DIR / "soci_agent.onnx"

# ── Domain constants (must match nn_client.py and notebook) ──────────────

ACTION_TYPES = ["move", "work", "eat", "sleep", "talk", "exercise", "shop", "relax", "wander"]
ACTION_TO_IDX = {a: i for i, a in enumerate(ACTION_TYPES)}

LOCATIONS = [
    "house_elena", "house_marcus", "house_helen", "house_diana", "house_kai",
    "house_priya", "house_james", "house_rosa", "house_yuki", "house_frank",
    "apartment_block_1", "apartment_block_2", "apartment_block_3",
    "apt_northeast", "apt_northwest", "apt_southeast", "apt_southwest",
    "cafe", "grocery", "bar", "restaurant", "bakery", "cinema", "diner", "pharmacy",
    "office", "office_tower", "factory", "school", "hospital",
    "park", "gym", "library", "church", "town_square", "sports_field",
    "street_north", "street_south", "street_east", "street_west",
]
LOC_TO_IDX = {loc: i for i, loc in enumerate(LOCATIONS)}
NUM_LOCATIONS = len(LOCATIONS)

NEED_NAMES = ["hunger", "energy", "social", "purpose", "comfort", "fun"]
ACTION_DURATIONS = {"move": 1, "work": 4, "eat": 2, "sleep": 8, "talk": 2, "exercise": 3, "shop": 2, "relax": 2, "wander": 1}

FEATURE_DIM = 47
NUM_ACTIONS = len(ACTION_TYPES)

# ── Feature encoding (same as nn_client.py) ──────────────────────────────

def _time_period(hour: int) -> int:
    if hour < 6: return 0
    if hour < 9: return 1
    if hour < 12: return 2
    if hour < 14: return 3
    if hour < 18: return 4
    if hour < 22: return 5
    return 6


def encode_features(
    personality: dict, age: float, hour: int, minute: int, day: int,
    needs: dict, mood: float, current_loc: str,
    home_loc: str = "", work_loc: str = "", num_people: int = 0,
) -> list[float]:
    """Encode agent state into 47-dim feature vector."""
    f: list[float] = []
    f.append(personality.get("openness", 5) / 10.0)
    f.append(personality.get("conscientiousness", 5) / 10.0)
    f.append(personality.get("extraversion", 5) / 10.0)
    f.append(personality.get("agreeableness", 5) / 10.0)
    f.append(personality.get("neuroticism", 5) / 10.0)
    f.append(age / 100.0)
    f.append(math.sin(2 * math.pi * hour / 24))
    f.append(math.cos(2 * math.pi * hour / 24))
    f.append(math.sin(2 * math.pi * minute / 60))
    f.append(math.cos(2 * math.pi * minute / 60))
    dow = (day - 1) % 7
    f.append(dow / 7.0)
    f.append(1.0 if dow >= 5 else 0.0)
    for n in NEED_NAMES:
        f.append(needs.get(n, 0.5))
    f.append(max(-1.0, min(1.0, mood)))
    vals = [needs.get(n, 0.5) for n in NEED_NAMES]
    urgent_idx = int(np.argmin(vals))
    f.append(urgent_idx / 5.0)
    f.append(1.0 if any(v < 0.15 for v in vals) else 0.0)
    zone = 0 if current_loc.startswith(("house_", "apartment_", "apt_")) else (
        1 if current_loc in ("cafe", "grocery", "bar", "restaurant", "bakery", "cinema", "diner", "pharmacy") else (
        2 if current_loc in ("office", "office_tower", "factory", "school", "hospital") else 3))
    f.append(zone / 3.0)
    f.append(1.0 if current_loc == home_loc else 0.0)
    f.append(1.0 if current_loc == work_loc else 0.0)
    f.append(min(num_people / 10.0, 1.0))
    loc_oh = [0.0] * 6
    if zone == 0: loc_oh[0] = 1.0
    elif zone == 1: loc_oh[1] = 1.0
    elif zone == 2: loc_oh[2] = 1.0
    elif current_loc.startswith("street_"): loc_oh[4] = 1.0
    else: loc_oh[3] = 1.0
    if current_loc == home_loc: loc_oh[5] = 1.0
    f.extend(loc_oh)
    tp = [0.0] * 7
    tp[_time_period(hour)] = 1.0
    f.extend(tp)
    f.extend([0.0] * 9)  # last action
    return f


# ════════════════════════════════════════════════════════════════════════
# STEP 1: COLLECT β€” Watch live sim and record training samples
# ════════════════════════════════════════════════════════════════════════

async def collect(
    base_url: str = "https://raymelius-soci2.hf.space",
    duration_minutes: int = 60,
    poll_interval: float = 3.0,
):
    """Poll the live simulation and collect (state, action) training pairs.

    Each tick, for each agent we observe:
    - Input: agent persona + needs + mood + location + time
    - Label: the action they actually chose (whether from NN, Gemini, or routine)

    This is teacher-free learning β€” whatever the simulation does IS the label.
    When Gemini makes a decision (10% of the time), it's a high-quality sample.
    """
    DATA_DIR.mkdir(parents=True, exist_ok=True)

    logger.info(f"Collecting from {base_url} for {duration_minutes} min...")
    logger.info(f"Saving to {SAMPLES_FILE}")

    # Fetch agent personas (static data)
    async with httpx.AsyncClient(base_url=base_url, timeout=30.0) as client:
        # /api/agents returns a dict keyed by agent ID
        agents_resp = await client.get("/api/agents")
        agents_resp.raise_for_status()
        agents_dict = agents_resp.json()  # {aid: {name, age, location, ...}}

        # Build persona cache β€” detail endpoint has needs/relationships but
        # not raw personality scores, so we use summary age + defaults
        persona_cache: dict[str, dict] = {}
        for aid, agent_summary in agents_dict.items():
            try:
                detail_resp = await client.get(f"/api/agents/{aid}")
                if detail_resp.status_code == 200:
                    detail = detail_resp.json()
                    pers = detail.get("personality", {})
                    persona_cache[aid] = {
                        "openness": pers.get("openness", 5),
                        "conscientiousness": pers.get("conscientiousness", 5),
                        "extraversion": pers.get("extraversion", 5),
                        "agreeableness": pers.get("agreeableness", 5),
                        "neuroticism": pers.get("neuroticism", 5),
                        "age": detail.get("age", 30),
                        "home": detail.get("home_location", ""),
                        "work": detail.get("work_location", ""),
                    }
            except Exception:
                pass

        logger.info(f"Cached {len(persona_cache)} agent personas")

        # Poll loop
        samples_collected = 0
        last_tick = -1
        start_time = time.monotonic()
        end_time = start_time + duration_minutes * 60

        with open(SAMPLES_FILE, "a") as f:
            while time.monotonic() < end_time:
                try:
                    # Get current city state
                    city_resp = await client.get("/api/city")
                    if city_resp.status_code != 200:
                        await asyncio.sleep(poll_interval)
                        continue
                    city = city_resp.json()

                    clock = city.get("clock", {})
                    tick = clock.get("total_ticks", 0)

                    # Skip if same tick
                    if tick == last_tick:
                        await asyncio.sleep(poll_interval)
                        continue
                    last_tick = tick

                    hour = clock.get("hour", 12)
                    minute = clock.get("minute", 0)
                    day = clock.get("day", 1)

                    # Count agents per location
                    loc_counts: dict[str, int] = {}
                    for aid, adata in city.get("agents", {}).items():
                        loc = adata.get("location", "")
                        loc_counts[loc] = loc_counts.get(loc, 0) + 1

                    # Collect a sample for each agent
                    for aid, adata in city.get("agents", {}).items():
                        action_str = adata.get("action", "idle")
                        state = adata.get("state", "idle")
                        location = adata.get("location", "")
                        mood = adata.get("mood", 0.0)
                        needs = adata.get("needs", {})

                        # Map state to action type
                        state_to_action = {
                            "idle": "wander", "moving": "move", "working": "work",
                            "eating": "eat", "sleeping": "sleep",
                            "socializing": "talk", "in_conversation": "talk",
                            "exercising": "exercise", "shopping": "shop",
                            "relaxing": "relax",
                        }
                        action_type = state_to_action.get(state, "wander")

                        if action_type not in ACTION_TO_IDX:
                            continue

                        persona = persona_cache.get(aid, {
                            "openness": 5, "conscientiousness": 5, "extraversion": 5,
                            "agreeableness": 5, "neuroticism": 5, "age": 30,
                            "home": "", "work": "",
                        })

                        features = encode_features(
                            personality=persona,
                            age=persona.get("age", 30),
                            hour=hour, minute=minute, day=day,
                            needs=needs, mood=mood,
                            current_loc=location,
                            home_loc=persona.get("home", ""),
                            work_loc=persona.get("work", ""),
                            num_people=loc_counts.get(location, 0),
                        )

                        sample = {
                            "features": features,
                            "action_idx": ACTION_TO_IDX[action_type],
                            "target_loc_idx": LOC_TO_IDX.get(location, 0),
                            "duration": ACTION_DURATIONS.get(action_type, 2),
                            "tick": tick,
                            "agent_id": aid,
                            "source": city.get("llm_provider", "unknown"),
                        }

                        f.write(json.dumps(sample) + "\n")
                        samples_collected += 1

                    elapsed = (time.monotonic() - start_time) / 60
                    logger.info(
                        f"Tick {tick} | Day {day} {hour:02d}:{minute:02d} | "
                        f"{samples_collected:,} samples | {elapsed:.1f} min"
                    )

                except httpx.HTTPError as e:
                    logger.warning(f"HTTP error: {e}")
                except Exception as e:
                    logger.error(f"Collection error: {e}", exc_info=True)

                await asyncio.sleep(poll_interval)

    logger.info(f"Collection done: {samples_collected:,} samples saved to {SAMPLES_FILE}")
    return samples_collected


# ════════════════════════════════════════════════════════════════════════
# STEP 2: TRAIN β€” Retrain the NN on collected + synthetic data
# ════════════════════════════════════════════════════════════════════════

def train(epochs: int = 20, batch_size: int = 512, lr: float = 3e-4):
    """Retrain the SociAgentTransformer on collected data.

    Loads collected samples from the live sim, mixes with synthetic data
    for robustness, and fine-tunes the existing model weights.
    """
    import torch
    import torch.nn as nn
    import torch.nn.functional as F
    from torch.utils.data import Dataset, DataLoader

    DEVICE = torch.device("cuda" if torch.cuda.is_available() else "cpu")
    logger.info(f"Training on {DEVICE}")

    # ── Load collected data ──────────────────────────────────────────
    collected = []
    source_counts: dict[str, int] = {}
    if SAMPLES_FILE.exists():
        with open(SAMPLES_FILE) as f:
            for line in f:
                line = line.strip()
                if line:
                    sample = json.loads(line)
                    collected.append(sample)
                    src = sample.get("source", "unknown")
                    source_counts[src] = source_counts.get(src, 0) + 1
        logger.info(f"Loaded {len(collected):,} collected samples β€” sources: {source_counts}")
    else:
        logger.warning(f"No collected samples at {SAMPLES_FILE}")

    # Oversample LLM-sourced data (Gemini/Claude/Groq) β€” these are higher quality
    # than NN or routine-generated samples, so we duplicate them 3x
    llm_sources = {"gemini", "claude", "groq"}
    llm_samples = [s for s in collected if s.get("source", "") in llm_sources]
    if llm_samples:
        logger.info(f"Oversampling {len(llm_samples):,} LLM-sourced samples (3x weight)")
        collected.extend(llm_samples * 2)  # 2 extra copies = 3x total weight

    if len(collected) < 100:
        logger.warning("Too few collected samples β€” generating synthetic data to supplement")
        collected.extend(_generate_synthetic(50_000 - len(collected)))

    # ── Dataset ──────────────────────────────────────────────────────
    random.shuffle(collected)
    split = int(len(collected) * 0.9)
    train_data = collected[:split]
    val_data = collected[split:]

    class ActionDataset(Dataset):
        def __init__(self, data):
            self.features = torch.tensor([d["features"] for d in data], dtype=torch.float32)
            self.actions = torch.tensor([d["action_idx"] for d in data], dtype=torch.long)
            self.locations = torch.tensor([d["target_loc_idx"] for d in data], dtype=torch.long)
            self.durations = torch.tensor([d["duration"] for d in data], dtype=torch.float32)

        def __len__(self):
            return len(self.actions)

        def __getitem__(self, idx):
            return {
                "features": self.features[idx],
                "action": self.actions[idx],
                "location": self.locations[idx],
                "duration": self.durations[idx],
            }

    train_ds = ActionDataset(train_data)
    val_ds = ActionDataset(val_data)
    train_loader = DataLoader(train_ds, batch_size=batch_size, shuffle=True)
    val_loader = DataLoader(val_ds, batch_size=1024, shuffle=False)
    logger.info(f"Train: {len(train_ds):,}, Val: {len(val_ds):,}")

    # ── Model (same architecture as notebook) ────────────────────────
    # Import model class inline to avoid dependency on notebook
    model = _build_model().to(DEVICE)

    # Load existing weights if available
    if BEST_PT.exists():
        model.load_state_dict(torch.load(BEST_PT, map_location=DEVICE, weights_only=True))
        logger.info(f"Loaded existing weights from {BEST_PT}")
    else:
        logger.info("Training from scratch (no existing weights)")

    # ── Training loop ────────────────────────────────────────────────
    # Class weights
    action_counts = torch.zeros(NUM_ACTIONS)
    for d in train_data:
        action_counts[d["action_idx"]] += 1
    action_weights = (1.0 / (action_counts + 1.0))
    action_weights = action_weights / action_weights.sum() * NUM_ACTIONS
    action_weights = action_weights.to(DEVICE)

    action_loss_fn = nn.CrossEntropyLoss(weight=action_weights)
    location_loss_fn = nn.CrossEntropyLoss()
    duration_loss_fn = nn.MSELoss()

    optimizer = torch.optim.AdamW(model.parameters(), lr=lr, weight_decay=1e-4)
    scheduler = torch.optim.lr_scheduler.CosineAnnealingLR(optimizer, T_max=epochs, eta_min=1e-6)

    best_acc = 0.0
    MODEL_DIR.mkdir(parents=True, exist_ok=True)
    history = {"train_loss": [], "val_loss": [], "val_action_acc": []}

    for epoch in range(epochs):
        model.train()
        total_loss = 0.0
        n = 0
        for batch in train_loader:
            feat = batch["features"].to(DEVICE)
            out = model(feat)
            loss = (
                1.0 * action_loss_fn(out["action_logits"], batch["action"].to(DEVICE))
                + 0.5 * location_loss_fn(out["location_logits"], batch["location"].to(DEVICE))
                + 0.2 * duration_loss_fn(out["duration"], batch["duration"].to(DEVICE))
            )
            optimizer.zero_grad()
            loss.backward()
            torch.nn.utils.clip_grad_norm_(model.parameters(), 1.0)
            optimizer.step()
            total_loss += loss.item()
            n += 1
        scheduler.step()
        avg_train_loss = total_loss / n

        # Validate
        model.eval()
        correct = 0
        total = 0
        val_loss = 0.0
        with torch.no_grad():
            for batch in val_loader:
                feat = batch["features"].to(DEVICE)
                out = model(feat)
                loss = (
                    1.0 * action_loss_fn(out["action_logits"], batch["action"].to(DEVICE))
                    + 0.5 * location_loss_fn(out["location_logits"], batch["location"].to(DEVICE))
                    + 0.2 * duration_loss_fn(out["duration"], batch["duration"].to(DEVICE))
                )
                val_loss += loss.item()
                pred = out["action_logits"].argmax(dim=-1)
                correct += (pred == batch["action"].to(DEVICE)).sum().item()
                total += feat.shape[0]
        acc = correct / total if total > 0 else 0
        avg_val_loss = val_loss / len(val_loader)

        history["train_loss"].append(avg_train_loss)
        history["val_loss"].append(avg_val_loss)
        history["val_action_acc"].append(acc)

        if acc > best_acc:
            best_acc = acc
            torch.save(model.state_dict(), str(BEST_PT))

        if (epoch + 1) % 5 == 0 or epoch == 0:
            logger.info(
                f"Epoch {epoch+1}/{epochs} | "
                f"Train: {avg_train_loss:.4f} | "
                f"Val: {avg_val_loss:.4f} | "
                f"Acc: {acc:.1%} | "
                f"Best: {best_acc:.1%}"
            )

    logger.info(f"Training done. Best accuracy: {best_acc:.1%}")

    # ── Export to ONNX ───────────────────────────────────────────────
    model.load_state_dict(torch.load(str(BEST_PT), map_location="cpu", weights_only=True))
    model.cpu().eval()

    dummy = torch.randn(1, FEATURE_DIM)
    torch.onnx.export(
        model, dummy, str(ONNX_PATH),
        input_names=["features"],
        output_names=["action_logits", "location_logits", "duration"],
        dynamic_axes={"features": {0: "batch"}},
        opset_version=17,
        dynamo=False,
    )
    onnx_size = ONNX_PATH.stat().st_size / 1024
    logger.info(f"ONNX exported: {ONNX_PATH} ({onnx_size:.0f} KB)")

    # ── Save training stats ───────────────────────────────────────────
    stats = {
        "best_val_action_acc": best_acc,
        "epochs": epochs,
        "train_samples": len(train_ds),
        "val_samples": len(val_ds),
        "collected_samples": sum(source_counts.values()),
        "source_counts": source_counts,
        "model_size_kb": onnx_size,
        "timestamp": time.strftime("%Y-%m-%dT%H:%M:%S"),
        "history": history,
    }
    stats_path = MODEL_DIR / "training_stats.json"
    stats_path.write_text(json.dumps(stats, indent=2))
    logger.info(f"Stats saved to {stats_path}")

    # ── Plot training graphs ──────────────────────────────────────────
    plot_training_graphs(stats_path)

    return best_acc


# ════════════════════════════════════════════════════════════════════════
# STEP 3: PUSH β€” Upload improved model to HuggingFace Hub
# ════════════════════════════════════════════════════════════════════════

def push(repo_id: str = "RayMelius/soci-agent-nn", accuracy: float = None,
         base_url: str = "https://raymelius-soci2.hf.space"):
    """Push the retrained ONNX model to HuggingFace Hub, then trigger live reload."""
    from huggingface_hub import HfApi, login

    token = os.environ.get("HF_TOKEN", "")
    if not token:
        logger.error("HF_TOKEN not set. Export it: export HF_TOKEN=hf_...")
        sys.exit(1)

    if not ONNX_PATH.exists():
        logger.error(f"No ONNX model at {ONNX_PATH}. Run 'train' first.")
        sys.exit(1)

    login(token=token)
    api = HfApi()

    # Compare against previous accuracy if available
    try:
        from huggingface_hub import hf_hub_download
        prev_stats_path = hf_hub_download(repo_id=repo_id, filename="training_stats.json", token=token)
        prev_stats = json.loads(open(prev_stats_path).read())
        prev_acc = prev_stats.get("best_accuracy")
        if prev_acc is not None and accuracy is not None:
            delta = accuracy - prev_acc
            symbol = "+" if delta >= 0 else ""
            logger.info(f"Previous accuracy: {prev_acc:.1%} β†’ New: {accuracy:.1%} ({symbol}{delta:.1%})")
        elif prev_acc is not None:
            logger.info(f"Previous accuracy: {prev_acc:.1%} (no new accuracy to compare)")
    except Exception:
        logger.info("No previous training_stats.json found β€” first push")
    api.create_repo(repo_id, exist_ok=True)

    # Upload ONNX
    api.upload_file(
        path_or_fileobj=str(ONNX_PATH),
        path_in_repo="soci_agent.onnx",
        repo_id=repo_id,
        commit_message="Self-improve: retrained on live sim data",
    )
    logger.info(f"ONNX model pushed to https://huggingface.co/{repo_id}")

    # Upload PyTorch weights too
    if BEST_PT.exists():
        api.upload_file(
            path_or_fileobj=str(BEST_PT),
            path_in_repo="soci_agent_best.pt",
            repo_id=repo_id,
            commit_message="Self-improve: retrained weights",
        )
        logger.info("PyTorch weights pushed")

    # Upload training stats
    stats = {
        "samples_file": str(SAMPLES_FILE),
        "num_samples": sum(1 for _ in open(SAMPLES_FILE)) if SAMPLES_FILE.exists() else 0,
        "model_size_kb": ONNX_PATH.stat().st_size / 1024,
        "timestamp": time.strftime("%Y-%m-%dT%H:%M:%S"),
    }
    if accuracy is not None:
        stats["best_accuracy"] = round(accuracy, 4)
    stats_path = MODEL_DIR / "training_stats.json"
    stats_path.write_text(json.dumps(stats, indent=2))
    api.upload_file(
        path_or_fileobj=str(stats_path),
        path_in_repo="training_stats.json",
        repo_id=repo_id,
    )

    logger.info("Push complete!")

    # Trigger hot-reload on the live simulation if reachable
    try:
        resp = httpx.post(f"{base_url}/api/nn/reload", timeout=30.0)
        if resp.status_code == 200:
            logger.info(f"Live sim NN reloaded: {resp.json().get('message', 'ok')}")
        else:
            logger.warning(f"Could not reload live sim NN: HTTP {resp.status_code}")
    except Exception as e:
        logger.warning(f"Could not reach live sim for reload: {e}")


# ════════════════════════════════════════════════════════════════════════
# Training Graphs
# ════════════════════════════════════════════════════════════════════════

def plot_training_graphs(stats_path: Path | str | None = None):
    """Plot training loss and accuracy curves from saved training stats.

    Saves the plot to models/training_graphs.png and displays it.
    """
    import matplotlib
    matplotlib.use("Agg")
    import matplotlib.pyplot as plt

    stats_path = Path(stats_path) if stats_path else MODEL_DIR / "training_stats.json"
    if not stats_path.exists():
        logger.error(f"No training stats found at {stats_path}")
        return

    stats = json.loads(stats_path.read_text())
    history = stats.get("history", {})

    train_loss = history.get("train_loss", [])
    val_loss = history.get("val_loss", [])
    val_action_acc = history.get("val_action_acc", [])

    if not train_loss:
        logger.error("No training history found in stats file")
        return

    epochs_range = list(range(1, len(train_loss) + 1))

    fig, axes = plt.subplots(1, 2, figsize=(14, 5))
    fig.suptitle(
        f"Soci Self-Improve Training β€” {stats.get('timestamp', '?')}  |  "
        f"Best Acc: {stats.get('best_val_action_acc', stats.get('best_accuracy', 0)):.1%}",
        fontsize=13, fontweight="bold",
    )

    # Loss curves
    ax = axes[0]
    ax.plot(epochs_range, train_loss, label="Train Loss", color="#2196F3", linewidth=2)
    if val_loss:
        ax.plot(epochs_range, val_loss, label="Val Loss", color="#F44336", linewidth=2)
    ax.set_xlabel("Epoch")
    ax.set_ylabel("Loss")
    ax.set_title("Training & Validation Loss")
    ax.legend()
    ax.grid(True, alpha=0.3)
    ax.set_xlim(1, len(train_loss))

    # Action accuracy
    ax = axes[1]
    if val_action_acc:
        ax.plot(epochs_range, [a * 100 for a in val_action_acc], label="Action Accuracy",
                color="#4CAF50", linewidth=2)
        best_epoch = int(np.argmax(val_action_acc)) + 1
        best_acc = max(val_action_acc) * 100
        ax.axhline(y=best_acc, color="#4CAF50", linestyle="--", alpha=0.4)
        ax.annotate(f"Best: {best_acc:.1f}% (epoch {best_epoch})",
                    xy=(best_epoch, best_acc), fontsize=9,
                    xytext=(best_epoch + 1, best_acc - 3),
                    arrowprops=dict(arrowstyle="->", color="#4CAF50"),
                    color="#4CAF50")
    ax.set_xlabel("Epoch")
    ax.set_ylabel("Accuracy (%)")
    ax.set_title("Action Prediction Accuracy")
    ax.legend()
    ax.grid(True, alpha=0.3)
    ax.set_xlim(1, len(train_loss))

    # Footer
    footer = (
        f"Train: {stats.get('train_samples', '?'):,} samples  |  "
        f"Val: {stats.get('val_samples', '?'):,} samples  |  "
        f"Collected: {stats.get('collected_samples', 0):,}  |  "
        f"Model: {stats.get('model_size_kb', 0):.0f} KB"
    )
    fig.text(0.5, 0.01, footer, ha="center", fontsize=9, color="gray")

    plt.tight_layout(rect=[0, 0.03, 1, 0.95])

    graph_path = MODEL_DIR / "training_graphs.png"
    fig.savefig(str(graph_path), dpi=150, bbox_inches="tight")
    logger.info(f"Training graphs saved to {graph_path}")

    try:
        import warnings
        with warnings.catch_warnings():
            warnings.simplefilter("ignore")
            matplotlib.use("TkAgg")
            plt.show(block=False)
            plt.pause(0.5)
    except Exception:
        pass

    plt.close(fig)


# ════════════════════════════════════════════════════════════════════════
# Model architecture (inline to avoid import dependency)
# ════════════════════════════════════════════════════════════════════════

def _build_model():
    """Build SociAgentTransformer β€” same architecture as the training notebook."""
    import torch
    import torch.nn as nn
    import torch.nn.functional as F

    class FeatureTokenizer(nn.Module):
        GROUPS = [
            ("personality", 0, 6), ("time", 6, 12), ("needs", 12, 21),
            ("location", 21, 31), ("time_period", 31, 38), ("last_action", 38, 47),
        ]

        def __init__(self, d_model):
            super().__init__()
            self.projections = nn.ModuleList()
            for name, start, end in self.GROUPS:
                self.projections.append(nn.Sequential(
                    nn.Linear(end - start, d_model), nn.LayerNorm(d_model), nn.GELU(),
                ))
            self.pos_embed = nn.Parameter(torch.randn(1, len(self.GROUPS), d_model) * 0.02)

        def forward(self, features):
            tokens = []
            for i, (_, start, end) in enumerate(self.GROUPS):
                tokens.append(self.projections[i](features[:, start:end]))
            tokens = torch.stack(tokens, dim=1)
            return tokens + self.pos_embed

    class MoEFeedForward(nn.Module):
        def __init__(self, d_model, d_ff, num_experts=4, top_k=2):
            super().__init__()
            self.num_experts = num_experts
            self.top_k = top_k
            self.gate = nn.Linear(d_model, num_experts, bias=False)
            self.experts = nn.ModuleList([
                nn.Sequential(nn.Linear(d_model, d_ff), nn.GELU(), nn.Linear(d_ff, d_model))
                for _ in range(num_experts)
            ])

        def forward(self, x):
            B, S, D = x.shape
            gate_probs = F.softmax(self.gate(x), dim=-1)
            top_k_probs, top_k_idx = gate_probs.topk(self.top_k, dim=-1)
            top_k_probs = top_k_probs / top_k_probs.sum(dim=-1, keepdim=True)
            output = torch.zeros_like(x)
            for k in range(self.top_k):
                eidx = top_k_idx[:, :, k]
                w = top_k_probs[:, :, k].unsqueeze(-1)
                for e in range(self.num_experts):
                    mask = (eidx == e).unsqueeze(-1)
                    if mask.any():
                        output = output + mask.float() * w * self.experts[e](x)
            return output

    class TransformerBlock(nn.Module):
        def __init__(self, d_model, nhead, d_ff, num_experts=4, dropout=0.1):
            super().__init__()
            self.attn = nn.MultiheadAttention(d_model, nhead, dropout=dropout, batch_first=True)
            self.norm1 = nn.LayerNorm(d_model)
            self.moe_ff = MoEFeedForward(d_model, d_ff, num_experts)
            self.norm2 = nn.LayerNorm(d_model)
            self.dropout = nn.Dropout(dropout)

        def forward(self, x):
            attn_out, _ = self.attn(x, x, x)
            x = self.norm1(x + self.dropout(attn_out))
            ff_out = self.moe_ff(x)
            return self.norm2(x + self.dropout(ff_out))

    class SociAgentTransformer(nn.Module):
        def __init__(self, d_model=128, nhead=8, num_layers=4, d_ff=256, num_experts=4, dropout=0.1):
            super().__init__()
            self.tokenizer = FeatureTokenizer(d_model)
            self.layers = nn.ModuleList([
                TransformerBlock(d_model, nhead, d_ff, num_experts, dropout)
                for _ in range(num_layers)
            ])
            self.cls_query = nn.Parameter(torch.randn(1, 1, d_model) * 0.02)
            self.cls_attn = nn.MultiheadAttention(d_model, nhead, dropout=dropout, batch_first=True)
            self.cls_norm = nn.LayerNorm(d_model)
            self.action_head = nn.Sequential(
                nn.Linear(d_model, d_model), nn.GELU(), nn.Dropout(dropout),
                nn.Linear(d_model, NUM_ACTIONS),
            )
            self.location_head = nn.Sequential(
                nn.Linear(d_model + NUM_ACTIONS, d_model), nn.GELU(), nn.Dropout(dropout),
                nn.Linear(d_model, NUM_LOCATIONS),
            )
            self.duration_head = nn.Sequential(
                nn.Linear(d_model + NUM_ACTIONS, d_model // 2), nn.GELU(),
                nn.Linear(d_model // 2, 1),
            )

        def forward(self, features):
            tokens = self.tokenizer(features)
            for layer in self.layers:
                tokens = layer(tokens)
            B = features.shape[0]
            cls = self.cls_query.expand(B, -1, -1)
            cls_out, _ = self.cls_attn(cls, tokens, tokens)
            h = self.cls_norm(cls_out.squeeze(1))
            action_logits = self.action_head(h)
            action_probs = F.softmax(action_logits.detach(), dim=-1)
            h_a = torch.cat([h, action_probs], dim=-1)
            location_logits = self.location_head(h_a)
            duration = torch.sigmoid(self.duration_head(h_a)) * 7.0 + 1.0
            return {
                "action_logits": action_logits,
                "location_logits": location_logits,
                "duration": duration.squeeze(-1),
            }

    return SociAgentTransformer()


# ════════════════════════════════════════════════════════════════════════
# Synthetic data fallback (when not enough collected samples)
# ════════════════════════════════════════════════════════════════════════

# Inline personas for synthetic generation β€” must match personas.yaml
_PERSONAS = [
    # House 1 β€” Elena & Lila (roommates)
    {"id": "elena",  "O": 8, "C": 7, "E": 4, "A": 6, "N": 5, "age": 34, "home": "house_elena", "work": "office",
     "tags": ["freelance", "introvert", "tech"], "hangouts": ["cafe", "library"]},
    {"id": "lila",   "O":10, "C": 3, "E": 6, "A": 7, "N": 7, "age": 33, "home": "house_elena", "work": "library",
     "tags": ["creative", "emotional", "crush_elena"], "hangouts": ["park", "cafe", "library"]},
    # House 2 β€” Marcus & Zoe (siblings)
    {"id": "marcus", "O": 5, "C": 8, "E": 9, "A": 7, "N": 3, "age": 28, "home": "house_marcus", "work": "gym",
     "tags": ["athletic", "extrovert", "community"], "hangouts": ["park", "sports_field", "cafe"]},
    {"id": "zoe",    "O": 8, "C": 4, "E": 8, "A": 6, "N": 7, "age": 19, "home": "house_marcus", "work": "library",
     "tags": ["student", "social_media", "young"], "hangouts": ["cafe", "cinema", "park", "town_square"]},
    # House 3 β€” Helen & Alice (close friends)
    {"id": "helen",  "O": 6, "C": 8, "E": 6, "A": 8, "N": 4, "age": 67, "home": "house_helen", "work": "library",
     "tags": ["retired", "bookworm", "widow"], "hangouts": ["library", "park", "bakery", "church"]},
    {"id": "alice",  "O": 5, "C": 8, "E": 6, "A": 8, "N": 3, "age": 58, "home": "house_helen", "work": "bakery",
     "tags": ["retired", "baker", "nurturing"], "hangouts": ["bakery", "grocery", "church"]},
    # House 4 β€” Diana & Marco (mother & son)
    {"id": "diana",  "O": 4, "C": 9, "E": 5, "A": 6, "N": 7, "age": 41, "home": "house_diana", "work": "grocery",
     "tags": ["business_owner", "single_mother", "protective"], "hangouts": ["grocery"]},
    {"id": "marco",  "O": 7, "C": 4, "E": 6, "A": 5, "N": 6, "age": 16, "home": "house_diana", "work": "school",
     "tags": ["student", "teen", "gamer"], "hangouts": ["park", "cinema", "cafe", "sports_field"]},
    # House 5 β€” Kai (lives alone)
    {"id": "kai",    "O": 9, "C": 3, "E": 7, "A": 5, "N": 6, "age": 22, "home": "house_kai", "work": "cafe",
     "tags": ["musician", "creative", "dropout"], "hangouts": ["bar", "park", "town_square"]},
    # House 6 β€” Priya & Nina (flatmates)
    {"id": "priya",  "O": 7, "C": 9, "E": 5, "A": 8, "N": 6, "age": 38, "home": "house_priya", "work": "hospital",
     "tags": ["overworked", "caring", "guilt"], "hangouts": ["hospital", "pharmacy"]},
    {"id": "nina",   "O": 5, "C": 8, "E": 9, "A": 4, "N": 5, "age": 29, "home": "house_priya", "work": "office",
     "tags": ["ambitious", "networker", "suspicious"], "hangouts": ["cafe", "restaurant", "office_tower"]},
    # House 7 β€” James & Theo (housemates)
    {"id": "james",  "O": 5, "C": 6, "E": 8, "A": 7, "N": 4, "age": 55, "home": "house_james", "work": "bar",
     "tags": ["social_hub", "divorced", "storyteller"], "hangouts": ["bar"]},
    {"id": "theo",   "O": 3, "C": 7, "E": 4, "A": 5, "N": 5, "age": 45, "home": "house_james", "work": "factory",
     "tags": ["blue_collar", "stoic", "handy"], "hangouts": ["bar", "diner"]},
    # House 8 β€” Rosa & Omar
    {"id": "rosa",   "O": 6, "C": 9, "E": 7, "A": 8, "N": 5, "age": 62, "home": "house_rosa", "work": "restaurant",
     "tags": ["nurturing", "italian", "community_mother"], "hangouts": ["restaurant", "grocery"]},
    {"id": "omar",   "O": 6, "C": 6, "E": 7, "A": 7, "N": 4, "age": 50, "home": "house_rosa", "work": "restaurant",
     "tags": ["immigrant", "philosophical", "hardworking"], "hangouts": ["restaurant", "cafe", "park"]},
    # House 9 β€” Yuki & Devon (flatmates)
    {"id": "yuki",   "O": 8, "C": 6, "E": 5, "A": 9, "N": 3, "age": 26, "home": "house_yuki", "work": "gym",
     "tags": ["mindful", "calm", "empathetic"], "hangouts": ["park", "gym", "library"]},
    {"id": "devon",  "O": 9, "C": 5, "E": 6, "A": 4, "N": 6, "age": 30, "home": "house_yuki", "work": "office",
     "tags": ["investigative", "paranoid", "curious"], "hangouts": ["cafe", "bar", "library", "town_square"]},
    # House 10 β€” Frank, George & Sam
    {"id": "frank",  "O": 3, "C": 7, "E": 5, "A": 4, "N": 5, "age": 72, "home": "house_frank", "work": "bar",
     "tags": ["retired", "cantankerous", "creature_of_habit"], "hangouts": ["bar", "diner"]},
    {"id": "george", "O": 4, "C": 7, "E": 3, "A": 6, "N": 4, "age": 47, "home": "house_frank", "work": "factory",
     "tags": ["night_shift", "widower", "observant"], "hangouts": ["park"]},
    {"id": "sam",    "O": 7, "C": 8, "E": 3, "A": 7, "N": 4, "age": 40, "home": "house_frank", "work": "library",
     "tags": ["quiet", "bookish", "inclusive"], "hangouts": ["library", "park", "cafe"]},
]


def _persona_hangout(p: dict, fallbacks: list[str]) -> str:
    """Pick a location the persona naturally gravitates toward."""
    hangouts = p.get("hangouts", [])
    if hangouts and random.random() < 0.6:
        return random.choice(hangouts)
    return random.choice(fallbacks)


def _generate_synthetic(n: int) -> list[dict]:
    """Generate persona-aware synthetic training samples."""
    data = []
    for _ in range(n):
        p = random.choice(_PERSONAS)
        persona = {
            "openness": p["O"], "conscientiousness": p["C"], "extraversion": p["E"],
            "agreeableness": p["A"], "neuroticism": p["N"],
        }
        tags = p.get("tags", [])
        is_night_shift = "night_shift" in tags
        is_retired = "retired" in tags
        is_student = "student" in tags

        hour = random.randint(0, 23)
        minute = random.choice([0, 15, 30, 45])
        day = random.randint(1, 30)
        is_weekend = ((day - 1) % 7) >= 5
        period = _time_period(hour)

        # Persona-aware needs generation
        needs = {}
        for nm in NEED_NAMES:
            if random.random() < 0.15:
                needs[nm] = round(random.uniform(0.0, 0.2), 2)
            else:
                needs[nm] = round(random.uniform(0.2, 1.0), 2)

        if "overworked" in tags:
            needs["energy"] = round(min(needs["energy"], random.uniform(0.1, 0.5)), 2)
            needs["social"] = round(min(needs["social"], random.uniform(0.1, 0.5)), 2)
        if "athletic" in tags:
            needs["energy"] = round(max(needs["energy"], random.uniform(0.5, 0.9)), 2)
        if "emotional" in tags:
            swing = random.choice(NEED_NAMES)
            needs[swing] = round(random.uniform(0.0, 0.3), 2)
        if "creature_of_habit" in tags:
            for nm in NEED_NAMES:
                needs[nm] = round(needs[nm] * 0.7 + 0.2, 2)
        if is_night_shift and 6 <= hour <= 18:
            needs["energy"] = round(min(needs["energy"], random.uniform(0.05, 0.35)), 2)
        if "mindful" in tags:
            for nm in NEED_NAMES:
                needs[nm] = round(max(needs[nm], 0.2), 2)
        if is_student:
            needs["social"] = round(max(needs["social"], random.uniform(0.3, 0.7)), 2)

        # Persona-aware mood
        avg_need = sum(needs.values()) / len(needs)
        mood = round(max(-1.0, min(1.0,
            (avg_need - 0.5) * 2 + random.uniform(-0.5, 0.5) * (p["N"] / 10.0)
        )), 2)

        # Persona-aware starting location
        if is_night_shift:
            if period in (0, 6):
                loc = p["work"]
            elif period in (2, 3):
                loc = p["home"]
            else:
                loc = random.choice([p["home"], "park"] if random.random() < 0.7 else [p["home"]])
        elif period == 0:
            loc = p["home"]
        elif period in (2, 4) and not is_weekend:
            if is_retired:
                loc = random.choice([p["home"]] + p.get("hangouts", ["park"]))
            else:
                loc = random.choice([p["work"], p["work"], _persona_hangout(p, ["cafe"])])
        elif period == 5:
            loc = random.choice([p["home"], _persona_hangout(p, ["bar", "cafe"])])
        else:
            loc = random.choice([p["home"], p["work"]])

        # --- Determine action ---
        urgent = [(nm, needs[nm]) for nm in NEED_NAMES if needs[nm] < 0.15]
        urgent.sort(key=lambda x: x[1])
        action = None
        target = loc

        # Priority 1: Critical needs
        if urgent:
            need_name = urgent[0][0]
            if need_name == "hunger":
                eat_locs = ["cafe", "restaurant", "bakery", "diner", p["home"]]
                if "community_mother" in tags:
                    eat_locs = ["restaurant", p["home"]]
                elif "baker" in tags:
                    eat_locs = ["bakery", p["home"]]
                action, target = "eat", random.choice(eat_locs)
            elif need_name == "energy":
                action, target = "sleep", p["home"]
            elif need_name == "social":
                social_locs = ["cafe", "bar", "park", "town_square"]
                if "social_hub" in tags:
                    social_locs = ["bar", "bar", "restaurant"]
                elif "networker" in tags:
                    social_locs = ["cafe", "restaurant", "office"]
                action, target = "talk", random.choice(social_locs)
            elif need_name == "purpose":
                action, target = "work", p["work"]
            elif need_name == "comfort":
                action, target = "relax", random.choice([p["home"], "park", "library"])
            elif need_name == "fun":
                fun_locs = ["park", "cinema", "bar", "sports_field"]
                if is_student:
                    fun_locs = ["cinema", "park", "cafe", "town_square"]
                action, target = random.choice(["relax", "exercise", "wander"]), random.choice(fun_locs)

        # Priority 2: Night shift inverted schedule (George)
        if action is None and is_night_shift:
            if period in (0, 6):
                action, target = "work", p["work"]
            elif period == 1:
                action, target = "move", p["home"]
            elif period in (2, 3):
                if needs["energy"] < 0.6:
                    action, target = "sleep", p["home"]
                else:
                    action, target = "relax", random.choice([p["home"], "park"])
            elif period in (4, 5):
                if needs["hunger"] < 0.5:
                    action, target = "eat", random.choice(["diner", "restaurant", p["home"]])
                else:
                    action, target = "move", p["work"]

        # Priority 3: Persona-specific patterns
        if action is None:
            pid = p.get("id", "")
            if pid == "frank" and period in (5, 6) and random.random() < 0.7:
                action, target = "relax", "bar"
            elif pid == "lila" and random.random() < 0.15:
                action = random.choice(["wander", "talk", "relax"])
                target = random.choice(["house_elena", "cafe", "library"])
            elif pid == "rosa" and period in (1, 2) and random.random() < 0.4:
                action, target = "shop", "grocery"
            elif pid == "omar" and period in (2, 3, 4) and not is_weekend and random.random() < 0.5:
                action, target = "wander", random.choice(["street_north", "street_south", "street_east", "street_west"])
            elif pid == "diana" and not is_weekend and period in (2, 3, 4) and random.random() < 0.7:
                action, target = "work", "grocery"
            elif pid == "marcus" and period == 1 and random.random() < 0.6:
                action, target = "exercise", random.choice(["gym", "park", "sports_field"])
            elif pid == "yuki" and period == 1 and random.random() < 0.5:
                action, target = "exercise", random.choice(["park", "gym"])
            elif pid == "devon" and period in (2, 4) and random.random() < 0.3:
                action = random.choice(["wander", "talk"])
                target = random.choice(["cafe", "bar", "town_square", "library"])

        # Priority 4: General time-of-day patterns
        if action is None:
            if period == 0:
                action, target = "sleep", p["home"]
            elif period == 1:
                if needs["hunger"] < 0.5:
                    action, target = "eat", random.choice(["cafe", "bakery", p["home"]])
                elif p["E"] >= 6 and random.random() < 0.3:
                    action, target = "exercise", random.choice(["gym", "park", "sports_field"])
                else:
                    action, target = "move", p["work"]
            elif period in (2, 4):
                if is_weekend:
                    r = random.random()
                    if is_retired:
                        if r < 0.35:
                            action, target = "relax", _persona_hangout(p, ["park", "library", p["home"]])
                        elif r < 0.55:
                            action, target = "talk", _persona_hangout(p, ["cafe", "park", "church"])
                        elif r < 0.7:
                            action, target = "shop", random.choice(["grocery", "pharmacy", "bakery"])
                        else:
                            action, target = "wander", random.choice(["park", "town_square"])
                    elif is_student:
                        if r < 0.3:
                            action, target = "talk", random.choice(["cafe", "park", "cinema", "town_square"])
                        elif r < 0.5:
                            action, target = "relax", random.choice(["cinema", "park", p["home"]])
                        elif r < 0.7:
                            action, target = "exercise", random.choice(["gym", "park", "sports_field"])
                        else:
                            action, target = "wander", random.choice(["town_square", "street_north"])
                    else:
                        if r < 0.25:
                            action, target = "relax", _persona_hangout(p, ["park", "cafe", p["home"]])
                        elif r < 0.45 and p["E"] >= 6:
                            action, target = "talk", _persona_hangout(p, ["cafe", "park", "town_square"])
                        elif r < 0.6:
                            action, target = "shop", random.choice(["grocery", "pharmacy"])
                        elif r < 0.8:
                            action, target = "exercise", random.choice(["gym", "park"])
                        else:
                            action, target = "wander", random.choice(["park", "town_square"])
                else:
                    work_prob = 0.5 + p["C"] * 0.05
                    if "business_owner" in tags or "overworked" in tags:
                        work_prob += 0.15
                    if is_retired:
                        work_prob = 0.15
                    if random.random() < work_prob:
                        action, target = "work", p["work"]
                    else:
                        action = random.choice(["wander", "relax", "talk"])
                        target = _persona_hangout(p, ["cafe", "park", "town_square"])
            elif period == 3:
                if needs["hunger"] < 0.6:
                    action, target = "eat", random.choice(["cafe", "restaurant", "bakery", "diner"])
                else:
                    action, target = "relax", random.choice(["park", "cafe"])
            elif period == 5:
                social_bias = p["E"] / 10.0
                r = random.random()
                if r < social_bias * 0.5:
                    action, target = "talk", random.choice(["bar", "restaurant", "park", "cafe"])
                elif r < 0.4:
                    action, target = "eat", random.choice(["restaurant", "bar", "diner", p["home"]])
                elif r < 0.55:
                    action, target = "exercise", random.choice(["gym", "park"])
                elif r < 0.7:
                    action, target = "relax", _persona_hangout(p, ["cinema", "bar", p["home"]])
                else:
                    action, target = "relax", p["home"]
            elif period == 6:
                if needs["energy"] < 0.4:
                    action, target = "sleep", p["home"]
                else:
                    action, target = "relax", p["home"]

        # Move override
        if target != loc and action != "move" and random.random() < 0.3:
            action = "move"

        # Duration adjustments
        dur = ACTION_DURATIONS.get(action, 2)
        if is_retired and dur > 3 and action not in ("sleep", "work"):
            dur = min(dur, 3)

        features = encode_features(
            personality=persona, age=p["age"],
            hour=hour, minute=minute, day=day,
            needs=needs, mood=mood, current_loc=loc,
            home_loc=p["home"], work_loc=p["work"],
        )

        data.append({
            "features": features,
            "action_idx": ACTION_TO_IDX.get(action, 0),
            "target_loc_idx": LOC_TO_IDX.get(target, 0),
            "duration": min(max(dur, 1), 8),
        })

    return data


# ════════════════════════════════════════════════════════════════════════
# STEP 4: SCHEDULED β€” Nightly Gemini collection + retrain cycle
# ════════════════════════════════════════════════════════════════════════

async def scheduled(
    base_url: str = "https://raymelius-soci2.hf.space",
    collect_minutes: int = 120,
    epochs: int = 25,
    repo_id: str = "RayMelius/soci-agent-nn",
    gemini_prob: float = 0.50,
):
    """Daily training cycle: switch to Gemini at quota reset, collect, retrain, push.

    Flow:
      1. Wait until Gemini quota resets (10:00 AM Athens / Europe/Athens)
      2. Switch live sim to Gemini provider, raise probability
      3. Collect high-quality (state, action) samples from Gemini decisions
      4. Switch back to NN when done (or when quota exhausted)
      5. Train on collected Gemini samples (weighted 3x vs NN/routine samples)
      6. Push improved model to HF Hub
      7. Repeat next night

    Usage:
        python nn_selfimprove.py scheduled --collect-minutes 120 --gemini-prob 0.50
    """
    import datetime

    async def _api_call(client: httpx.AsyncClient, method: str, path: str, **kwargs):
        """Make API call with retries."""
        for attempt in range(3):
            try:
                resp = await getattr(client, method)(path, timeout=30.0, **kwargs)
                return resp
            except httpx.HTTPError as e:
                logger.warning(f"API {method.upper()} {path} attempt {attempt+1} failed: {e}")
                if attempt < 2:
                    await asyncio.sleep(5)
        return None

    async def switch_provider(client: httpx.AsyncClient, provider: str, prob: float):
        """Switch the live sim's LLM provider and probability."""
        resp = await _api_call(client, "post", "/api/llm/provider",
                               json={"provider": provider})
        if resp and resp.status_code == 200:
            logger.info(f"Switched provider to: {provider}")
        else:
            logger.error(f"Failed to switch to {provider}: {resp.status_code if resp else 'no response'}")
            return False

        resp = await _api_call(client, "post", f"/api/controls/llm_probability?value={prob}")
        if resp and resp.status_code == 200:
            logger.info(f"Set probability to: {prob:.0%}")
        else:
            logger.warning(f"Failed to set probability: {resp.status_code if resp else 'no response'}")

        return True

    async def calculate_probability(client: httpx.AsyncClient, target_minutes: int) -> float:
        """Query remaining Gemini quota and return a reasonable probability.

        The real bottleneck is RPM (requests per minute), not probability.
        With 50 agents, even low probability saturates the RPM rate limiter.
        Gemini: 4 RPM β†’ max 240 calls/hour β†’ 1500 RPD lasts ~6.25h.
        Probability mainly controls LLM-vs-routine quality, not quota duration.
        """
        resp = await _api_call(client, "get", "/api/llm/quota")
        if not resp or resp.status_code != 200:
            logger.warning("Could not fetch quota β€” using default probability")
            return gemini_prob

        quota = resp.json()
        remaining = quota.get("remaining", 1500)

        if remaining <= 0:
            logger.warning("No Gemini quota remaining!")
            return 0.0

        # Get per-provider RPM info
        providers = quota.get("providers", {})
        gemini_info = providers.get("gemini", {})
        rpm = gemini_info.get("rpm", 4)
        max_calls_per_hour = rpm * 60
        hours_available = remaining / max_calls_per_hour
        target_hours = target_minutes / 60.0

        logger.info(
            f"Quota: {remaining} remaining, RPM={rpm} β†’ "
            f"max {max_calls_per_hour} calls/h β†’ ~{hours_available:.1f}h available"
        )

        if hours_available >= target_hours:
            prob = gemini_prob
            logger.info(f"Quota sufficient for {target_minutes}min target β†’ using {prob:.0%}")
        else:
            # Quota won't last β€” reduce probability (marginal help with many agents)
            prob = max(0.02, 0.10 * (hours_available / target_hours))
            logger.warning(
                f"Quota only lasts ~{hours_available:.1f}h but target is {target_hours:.1f}h "
                f"β†’ reducing probability to {prob:.1%}"
            )

        return round(prob, 4)

    async def wait_until_reset():
        """Wait until next Gemini quota reset (10:00 AM Athens / Europe/Athens)."""
        try:
            from zoneinfo import ZoneInfo
        except ImportError:
            from backports.zoneinfo import ZoneInfo

        athens = ZoneInfo("Europe/Athens")
        now = datetime.datetime.now(athens)
        reset_today = now.replace(hour=10, minute=0, second=5, microsecond=0)

        # If we've already passed 10:00 AM today, target tomorrow
        if now >= reset_today:
            reset_target = reset_today + datetime.timedelta(days=1)
        else:
            reset_target = reset_today

        wait_secs = (reset_target - now).total_seconds()
        logger.info(f"Waiting {wait_secs/3600:.1f}h until Gemini reset ({reset_target.strftime('%Y-%m-%d %H:%M %Z')})")
        await asyncio.sleep(wait_secs)

    # ── Main loop ─────────────────────────────────────────────────────
    cycle = 0
    while True:
        cycle += 1
        logger.info(f"{'='*60}")
        logger.info(f"TRAINING CYCLE {cycle}")
        logger.info(f"{'='*60}")

        # 1. Wait for Gemini quota reset (10:00 AM Athens)
        await wait_until_reset()

        async with httpx.AsyncClient(base_url=base_url) as client:
            # 2. Switch to Gemini first
            logger.info("Switching live sim to Gemini...")
            ok = await switch_provider(client, "gemini", 0.01)  # start low
            if not ok:
                logger.error("Could not switch to Gemini β€” skipping this cycle")
                continue

            # 3. Calculate probability to spread quota over collection period
            calc_prob = await calculate_probability(client, collect_minutes)
            await switch_provider(client, "gemini", calc_prob)
            logger.info(f"Collecting for {collect_minutes} min with Gemini at {calc_prob:.1%} probability...")

        # collect() creates its own client
        n_samples = await collect(
            base_url=base_url,
            duration_minutes=collect_minutes,
            poll_interval=3.0,
        )
        logger.info(f"Collected {n_samples:,} samples this cycle")

        # 4. Switch back to NN + restore default probability
        async with httpx.AsyncClient(base_url=base_url) as client:
            await switch_provider(client, "nn", 1.0)

        # 5. Count Gemini-sourced samples
        gemini_samples = 0
        if SAMPLES_FILE.exists():
            with open(SAMPLES_FILE) as f:
                for line in f:
                    if '"source": "gemini"' in line or '"source":"gemini"' in line:
                        gemini_samples += 1
        logger.info(f"Total Gemini-sourced samples in file: {gemini_samples:,}")

        if gemini_samples < 50:
            logger.warning("Too few Gemini samples β€” skipping training this cycle")
            continue

        # 6. Train (Gemini samples get 3x weight in the training loop)
        logger.info("Starting retraining...")
        best_acc = train(epochs=epochs)
        logger.info(f"Training done β€” best accuracy: {best_acc:.1%}")

        # 7. Push improved model
        if os.environ.get("HF_TOKEN"):
            logger.info("Pushing improved model to HF Hub...")
            push(repo_id=repo_id, accuracy=best_acc, base_url=base_url)
        else:
            logger.warning("HF_TOKEN not set β€” skipping push")

        logger.info(f"Cycle {cycle} complete! Next cycle at 10:00 AM Athens.")


# ════════════════════════════════════════════════════════════════════════
# STEP 5: BUDGET β€” Check quota and auto-set probability
# ════════════════════════════════════════════════════════════════════════

async def budget(
    base_url: str = "https://raymelius-soci2.hf.space",
    target_minutes: int = 60,
    apply: bool = True,
):
    """Check Gemini quota, calculate and optionally apply the right probability.

    Usage:
        python nn_selfimprove.py budget --minutes 60   # spread quota over 1 hour
        python nn_selfimprove.py budget --minutes 120  # spread over 2 hours
    """
    async with httpx.AsyncClient(base_url=base_url, timeout=30.0) as client:
        resp = await client.get("/api/llm/quota")
        if resp.status_code != 200:
            logger.error(f"Could not fetch quota: {resp.status_code}")
            return

        quota = resp.json()
        provider = quota.get("provider", "?")
        num_agents = quota.get("num_agents", 0)

        # Get Gemini-specific quota from providers dict
        providers = quota.get("providers", {})
        gemini_info = providers.get("gemini", {})
        remaining = gemini_info.get("remaining", quota.get("remaining", 0))
        daily_limit = gemini_info.get("daily_limit", quota.get("daily_limit", 1500))
        daily_requests = gemini_info.get("daily_requests", quota.get("daily_requests", 0))
        rpm = gemini_info.get("rpm", 4)
        max_calls_per_hour = rpm * 60
        hours_available = remaining / max_calls_per_hour if max_calls_per_hour > 0 else 0

        logger.info(f"Provider: {provider}")
        logger.info(f"Daily quota: {daily_requests}/{daily_limit} used, {remaining} remaining")
        logger.info(f"Rate limit: {rpm} RPM β†’ max {max_calls_per_hour} calls/hour")
        logger.info(f"Estimated runtime at max RPM: ~{hours_available:.1f}h")
        logger.info(f"Sim: {num_agents} agents")

        if remaining <= 0:
            logger.warning("No quota remaining! Wait for reset (10:00 AM Athens).")
            return

        target_hours = target_minutes / 60.0
        # Probability controls LLM-vs-routine quality, RPM is the real bottleneck
        if hours_available >= target_hours:
            prob = 0.20  # moderate: good mix of LLM and routine
        else:
            prob = max(0.02, 0.10 * (hours_available / target_hours))

        logger.info(
            f"Target: {target_minutes} min β†’ probability {prob:.2%} "
            f"(RPM-limited to ~{max_calls_per_hour} calls/h, {remaining} remaining)"
        )

        if apply:
            # Switch to Gemini if not already
            if provider != "gemini":
                resp = await client.post("/api/llm/provider", json={"provider": "gemini"})
                if resp.status_code == 200:
                    logger.info("Switched to Gemini")
                else:
                    logger.warning(f"Could not switch to Gemini: {resp.status_code}")

            resp = await client.post(f"/api/controls/llm_probability?value={prob}")
            if resp.status_code == 200:
                logger.info(f"Applied probability: {prob:.2%}")
            else:
                logger.warning(f"Could not set probability: {resp.status_code}")

            logger.info(f"Done! Gemini will run at {prob:.2%} for ~{target_minutes} min. "
                        f"Start collecting: python nn_selfimprove.py collect --minutes {target_minutes}")


# ════════════════════════════════════════════════════════════════════════
# CLI
# ════════════════════════════════════════════════════════════════════════

def main():
    parser = argparse.ArgumentParser(description="Soci Agent NN β€” Self-Improvement Pipeline")
    parser.add_argument("mode", choices=["collect", "train", "push", "all", "scheduled", "budget", "graph"],
                        help="collect=watch live sim, train=retrain NN, push=upload to HF, "
                             "all=full pipeline, scheduled=daily Gemini cycle, "
                             "budget=check quota & set probability, "
                             "graph=display training graphs from last run")
    parser.add_argument("--url", default="https://raymelius-soci2.hf.space",
                        help="Live simulation URL (default: HF Space)")
    parser.add_argument("--minutes", type=int, default=60,
                        help="Collection duration in minutes (default: 60)")
    parser.add_argument("--collect-minutes", type=int, default=120,
                        help="Scheduled mode: collection duration in minutes (default: 120)")
    parser.add_argument("--gemini-prob", type=float, default=0.50,
                        help="Scheduled mode: LLM probability during Gemini collection (default: 0.50)")
    parser.add_argument("--epochs", type=int, default=20,
                        help="Training epochs (default: 20)")
    parser.add_argument("--repo", default="RayMelius/soci-agent-nn",
                        help="HF Hub repo ID")
    args = parser.parse_args()

    if args.mode == "graph":
        plot_training_graphs()
        return

    if args.mode in ("collect", "all"):
        asyncio.run(collect(base_url=args.url, duration_minutes=args.minutes))

    if args.mode in ("train", "all"):
        best_acc = train(epochs=args.epochs)

    if args.mode in ("push", "all"):
        acc = best_acc if args.mode == "all" else None
        push(repo_id=args.repo, accuracy=acc, base_url=args.url)

    if args.mode == "scheduled":
        asyncio.run(scheduled(
            base_url=args.url,
            collect_minutes=args.collect_minutes,
            epochs=args.epochs,
            repo_id=args.repo,
            gemini_prob=args.gemini_prob,
        ))

    if args.mode == "budget":
        asyncio.run(budget(base_url=args.url, target_minutes=args.minutes, apply=True))


if __name__ == "__main__":
    main()