File size: 69,902 Bytes
b555842
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
# gui/pages/execute.py  — v6  (Phase 3)
# ─────────────────────────────────────────────────────────────────────────────
# FIXES vs v5:
#   1. Tax calc: reads from orchestration_tax_summary DB table — no stdout parsing
#   2. Live view: rendered ONLY when running/done, never bleeds into other pages
#      get_conn() is non-blocking with 2s timeout so startup never hangs
#   3. Timeline: static snapshot, no more full-box rerender flicker
#      Uses a stable key so Plotly doesn't recreate on every rerun
#   4. Run History: always at bottom, newest first, expandable beauty cards
#   5. Tab 2 just shows start button — live panel is ABOVE tabs
#   6. _parse_summary kept as fallback only; primary source is DB
# ─────────────────────────────────────────────────────────────────────────────

import math, subprocess, time as _time, re as _re, threading, os, signal
from pathlib import Path
from datetime import datetime

import streamlit as st
import pandas as pd
import plotly.graph_objects as go
from gui.components.session_tree import render_session_tree

from gui.config     import PROJECT_ROOT, LIVE_API, WF_COLORS, PL, \
                           INSIGHTS_RULES, DASHBOARD_CFG, STATUS_COLORS, STATUS_ICONS
from gui.connection import get_conn, api_post, api_get
from gui.db         import q, q1
from gui.helpers    import fl, _human_energy, _human_water, _human_carbon

try:
    import requests as _req
    _REQUESTS_OK = True
except ImportError:
    _REQUESTS_OK = False

try:
    import yaml as _yaml
    _YAML_OK = True
except ImportError:
    _YAML_OK = False


# ══════════════════════════════════════════════════════════════════════════════
# THREAD-SAFE SHARED STORE
# ══════════════════════════════════════════════════════════════════════════════
import threading as _threading
_STORE_LOCK = _threading.Lock()
_STORE: dict = {
    "running":     False,
    "done":        False,
    "phase":       "idle",
    "progress":    0.0,
    "log":         [],
    "metrics":     {},
    "result_rows": [],
    "group_id":    "",
    "run_record":  None,
    "sessions":    [],
    "queue":       [],
    "saved":       [],
    "stop":        False,
    "current_cmd": "",
    "timeline_snap": None,   # cached Plotly fig — only rebuilt when group_id changes
    "timeline_gid":  "",     # which group_id the snap belongs to
}

def _store_get(key, default=None):
    with _STORE_LOCK:
        return _STORE.get(key, default)

def _store_set(key, value):
    with _STORE_LOCK:
        _STORE[key] = value

def _store_append(key, value):
    with _STORE_LOCK:
        _STORE.setdefault(key, []).append(value)

def _store_log(line):
    with _STORE_LOCK:
        _STORE["log"].append(line)
        if len(_STORE["log"]) > 200:
            _STORE["log"] = _STORE["log"][-200:]


# ── Config shortcuts ──────────────────────────────────────────────────────────
_STUCK_MINS    = DASHBOARD_CFG.get("stuck_run", {}).get("threshold_minutes", 30)
_KILL_ON_RESET = DASHBOARD_CFG.get("stuck_run", {}).get("kill_process", True)
_QUEUE_FILE    = PROJECT_ROOT / DASHBOARD_CFG.get("queue", {}).get(
                     "persist_file", "config/queue_state.yaml")
_MAX_LOG       = DASHBOARD_CFG.get("live", {}).get("max_log_lines", 200)
_AUTO_SWITCH   = DASHBOARD_CFG.get("live", {}).get("auto_switch_to_analysis", True)


# ══════════════════════════════════════════════════════════════════════════════
# SESSION STATE
# ══════════════════════════════════════════════════════════════════════════════

def _init_state():
    with _STORE_LOCK:
        snap = dict(_STORE)
    for _k in ("running","done","phase","progress","log","metrics",
               "result_rows","group_id","run_record","sessions"):
        st.session_state[f"ex_{_k}"] = snap.get(_k)
    for _k in ("queue","saved"):
        if f"ex_{_k}" not in st.session_state:
            st.session_state[f"ex_{_k}"] = snap.get(_k, [])
        with _STORE_LOCK:
            _STORE[_k] = list(st.session_state[f"ex_{_k}"])
    if "ex_thread" not in st.session_state:
        st.session_state["ex_thread"] = None


# ══════════════════════════════════════════════════════════════════════════════
# QUEUE PERSISTENCE
# ══════════════════════════════════════════════════════════════════════════════

def _save_queue():
    if not _YAML_OK:
        return
    try:
        _QUEUE_FILE.parent.mkdir(parents=True, exist_ok=True)
        with open(_QUEUE_FILE, "w") as f:
            _yaml.dump({"queue": _store_get("queue", [])}, f)
    except Exception:
        pass

def _load_queue():
    if not _YAML_OK or not _QUEUE_FILE.exists():
        return
    try:
        data = _yaml.safe_load(_QUEUE_FILE.read_text()) or {}
        loaded = data.get("queue", [])
        with _STORE_LOCK:
            if loaded and not _STORE.get("queue"):
                _STORE["queue"] = loaded
    except Exception:
        pass


# ══════════════════════════════════════════════════════════════════════════════
# TASK LOADER
# ══════════════════════════════════════════════════════════════════════════════

def _load_tasks() -> tuple:
    if _YAML_OK:
        yaml_path = PROJECT_ROOT / "config" / "tasks.yaml"
        if yaml_path.exists():
            try:
                raw   = _yaml.safe_load(yaml_path.read_text()) or {}
                tasks = raw.get("tasks", [])
                ids   = [t["id"] for t in tasks if "id" in t]
                cats  = {t["id"]: t.get("category", "") for t in tasks}
                names = {t["id"]: t.get("name", t["id"]) for t in tasks}
                if ids:
                    return ids, cats, names
            except Exception as e:
                st.warning(f"⚠️ Could not parse config/tasks.yaml: {e}")

    try:
        df = q("SELECT DISTINCT task_name FROM experiments "
               "WHERE task_name IS NOT NULL ORDER BY task_name")
        ids = df.task_name.tolist() if not df.empty else []
    except Exception:
        ids = []

    if not ids:
        st.error("❌ No tasks found in config/tasks.yaml and DB is empty.")
        ids = []

    return ids, {i: "" for i in ids}, {i: i for i in ids}


# ══════════════════════════════════════════════════════════════════════════════
# STUCK RUN DETECTOR
# ══════════════════════════════════════════════════════════════════════════════

def _show_stuck_runs():
    try:
        stuck = q(f"""
            SELECT exp_id, task_name, provider, group_id,
                   started_at, runs_completed, runs_total
            FROM experiments
            WHERE status = 'running'
              AND started_at IS NOT NULL
              AND (julianday('now') - julianday(started_at)) * 1440 > {_STUCK_MINS}
            ORDER BY exp_id
        """)
    except Exception:
        return
    if stuck.empty:
        return
    for _, row in stuck.iterrows():
        try:
            started = datetime.fromisoformat(str(row.started_at))
            mins    = int((_time.time() - started.timestamp()) / 60)
            elapsed = f"{mins} min ago"
        except Exception:
            elapsed = str(row.started_at)
        st.markdown(
            f"<div style='background:#1a0508;border:1px solid #ef4444;"
            f"border-left:4px solid #ef4444;border-radius:5px;"
            f"padding:10px 14px;margin-bottom:8px;'>"
            f"<div style='font-size:11px;font-weight:700;color:#ef4444;margin-bottom:4px;'>"
            f"🚨 Stuck Experiment Detected</div>"
            f"<div style='font-size:10px;color:#c8d8e8;'>"
            f"exp_id={row.exp_id} · {row.task_name} · {row.provider} · "
            f"started {elapsed} · {row.runs_completed}/{row.runs_total} runs</div>"
            f"</div>",
            unsafe_allow_html=True,
        )
        if st.button(f"⚡ Force Reset exp_{row.exp_id}", key=f"reset_{row.exp_id}"):
            _force_reset_experiment(int(row.exp_id))
            st.success(f"exp_{row.exp_id} marked as error. Refresh to confirm.")
            st.rerun()

def _force_reset_experiment(exp_id: int):
    import sqlite3
    try:
        conn = sqlite3.connect(str(PROJECT_ROOT / "data" / "experiments.db"))
        conn.execute(
            "UPDATE experiments SET status='error', error_message='Force reset by UI' "
            "WHERE exp_id = ?", (exp_id,))
        conn.commit()
        conn.close()
    except Exception as e:
        st.error(f"DB reset failed: {e}")
        return
    if _KILL_ON_RESET:
        try:
            import psutil
            for proc in psutil.process_iter(["pid", "cmdline"]):
                cmdline = " ".join(proc.info.get("cmdline") or [])
                if "run_experiment" in cmdline or "test_harness" in cmdline:
                    os.kill(proc.info["pid"], signal.SIGTERM)
        except Exception:
            pass


# ══════════════════════════════════════════════════════════════════════════════
# SVG GAUGES
# ══════════════════════════════════════════════════════════════════════════════

def _gauge_svg(value, vmin, vmax, label, unit, color, warn=None, danger=None):
    pct   = max(0.0, min(1.0, (value - vmin) / max(vmax - vmin, 1e-9)))
    angle = -140 + pct * 280
    r     = 52; cx, cy = 60, 62
    ex    = cx + r * math.sin(math.radians(angle))
    ey    = cy - r * math.cos(math.radians(angle))
    large = 1 if pct > 0.5 else 0
    bx    = cx + r * math.sin(math.radians(-140))
    by    = cy - r * math.cos(math.radians(-140))
    ex0   = cx - r * math.sin(math.radians(-140))
    ey0   = cy - r * math.cos(math.radians(-140))
    nclr  = ("#ef4444" if danger and value >= danger
             else "#f59e0b" if warn and value >= warn else color)
    return (f"<div style='text-align:center;padding:2px 0;'>"
            f"<svg width='120' height='92' viewBox='0 0 120 92'>"
            f"<path d='M {bx:.1f} {by:.1f} A {r} {r} 0 1 1 {ex0:.1f} {ey0:.1f}'"
            f" fill='none' stroke='#1e2d45' stroke-width='8' stroke-linecap='round'/>"
            f"<path d='M {bx:.1f} {by:.1f} A {r} {r} 0 {large} 1 {ex:.1f} {ey:.1f}'"
            f" fill='none' stroke='{nclr}' stroke-width='8' stroke-linecap='round'/>"
            f"<circle cx='{cx}' cy='{cy}' r='4' fill='{nclr}'/>"
            f"<text x='{cx}' y='{cy+5}' text-anchor='middle' font-size='14'"
            f" font-weight='700' fill='#e8f0f8' font-family='monospace'>{value:.1f}</text>"
            f"<text x='{cx}' y='{cy+19}' text-anchor='middle' font-size='7' fill='#7090b0'>{unit}</text>"
            f"<text x='{cx}' y='85' text-anchor='middle' font-size='8'"
            f" font-weight='600' fill='{nclr}'>{label}</text>"
            f"<text x='6'   y='74' text-anchor='middle' font-size='6' fill='#3d5570'>{vmin}</text>"
            f"<text x='114' y='74' text-anchor='middle' font-size='6' fill='#3d5570'>{vmax}</text>"
            f"</svg></div>")

def _bar_gauge(value, vmax, label, unit, color):
    pct = max(0.0, min(100.0, value / max(vmax, 1e-9) * 100))
    return (f"<div style='margin:4px 0 8px;'>"
            f"<div style='display:flex;justify-content:space-between;"
            f"font-size:9px;color:#7090b0;margin-bottom:3px;'>"
            f"<span style='font-weight:600;color:#e8f0f8'>{label}</span>"
            f"<span style='font-family:monospace;color:{color}'>{value:.0f} {unit}</span>"
            f"</div><div style='background:#1e2d45;border-radius:3px;height:7px;overflow:hidden;'>"
            f"<div style='background:{color};width:{pct:.1f}%;height:100%;"
            f"border-radius:3px;transition:width 0.4s;'></div></div></div>")


# ══════════════════════════════════════════════════════════════════════════════
# FIX #3: STABLE GANTT — built once per group_id, cached in _STORE
# No more full-box rerender every 1s. Only rebuilds when gid changes.
# ══════════════════════════════════════════════════════════════════════════════

def _gantt_chart_stable(group_id: str):
    """Render timeline only when group_id changes — prevents blinking."""
    if not group_id:
        return

    # Return cached fig if same group
    cached_gid = _store_get("timeline_gid", "")
    cached_fig = _store_get("timeline_snap", None)
    if cached_gid == group_id and cached_fig is not None:
        st.plotly_chart(cached_fig, use_container_width=True,
                        key=f"gantt_{group_id}")
        return

    # Build fresh
    try:
        exps = q(f"""
            SELECT exp_id, task_name, provider, status, started_at, completed_at
            FROM experiments WHERE group_id = '{group_id}' ORDER BY exp_id
        """)
    except Exception:
        return
    if exps is None or exps.empty:
        return

    bars = []
    now  = datetime.utcnow()
    first_start = None

    for _, row in exps.iterrows():
        try:
            s = datetime.fromisoformat(str(row.started_at)) if row.started_at else None
            e = datetime.fromisoformat(str(row.completed_at)) if row.completed_at else None
            if s is None:
                continue
            if first_start is None:
                first_start = s
            end   = e if e else now
            dur_s = max((end - s).total_seconds(), 0.5)
            offset= (s - first_start).total_seconds()
            st_db = str(row.get("status", "")).lower()
            clr   = {"completed": "#3b82f6", "running": "#22c55e",
                     "failed": "#ef4444", "error": "#ef4444"}.get(st_db, "#4b5563")
            bars.append({
                "label":  f"exp_{row.exp_id} · {row.provider[:3]} · {str(row.task_name)[:14]}",
                "start":  offset,
                "dur":    dur_s,
                "color":  clr,
                "status": st_db,
            })
        except Exception:
            continue

    if not bars:
        return

    fig = go.Figure()
    for bar in reversed(bars):
        fig.add_trace(go.Bar(
            name=bar["label"],
            x=[bar["dur"]], y=[bar["label"]],
            base=bar["start"],
            orientation="h",
            marker_color=bar["color"],
            marker_opacity=0.85,
            showlegend=False,
            hovertemplate=(f"{bar['label']}<br>{bar['dur']:.1f}s"
                           f"<br>{bar['status']}<extra></extra>"),
        ))

    _pl_g = {k: v for k, v in PL.items() if k != 'margin'}
    fig.update_layout(
        **_pl_g,
        height=max(80 + len(bars) * 26, 120),
        barmode="overlay",
        xaxis_title="Elapsed (s)",
        margin=dict(l=10, r=10, t=20, b=24),
        title=dict(text="⏱ Timeline", font=dict(size=9), x=0),
    )

    _store_set("timeline_snap", fig)
    _store_set("timeline_gid",  group_id)
    st.plotly_chart(fig, use_container_width=True, key=f"gantt_{group_id}")


# ══════════════════════════════════════════════════════════════════════════════
# BACKGROUND RUN THREAD
# ══════════════════════════════════════════════════════════════════════════════

def _thread_run_local(_first_exp: dict, sid: str):
    import sqlite3 as _sl3

    def _db1(sql):
        try:
            con = _sl3.connect(str(PROJECT_ROOT / "data" / "experiments.db"), timeout=3)
            row = con.execute(sql).fetchone()
            con.close()
            return row[0] if row else None
        except Exception:
            return None

    def _refresh_gid():
        try:
            con = _sl3.connect(str(PROJECT_ROOT / "data" / "experiments.db"), timeout=2)
            row = con.execute(
                "SELECT group_id FROM experiments ORDER BY exp_id DESC LIMIT 1"
            ).fetchone()
            con.close()
            gid = row[0] if row else ""
            if gid:
                _store_set("group_id", gid)
                # Invalidate timeline cache so it rebuilds for new group
                if gid != _store_get("timeline_gid", ""):
                    _store_set("timeline_snap", None)
        except Exception:
            pass

    def _poll_telemetry(rid):
        if not _REQUESTS_OK or not rid:
            return
        m = _store_get("metrics", {}).copy()
        try:
            er = _req.get(f"http://127.0.0.1:8765/api/runs/{rid}/samples/energy", timeout=2).json()
            pw = er.get("power", []) if isinstance(er, dict) else []
            if pw:
                m["pkg_w"]  = float(pw[-1].get("pkg_w", 0))
                m["core_w"] = float(pw[-1].get("core_w", 0))
        except Exception:
            pass
        try:
            cr = _req.get(f"http://127.0.0.1:8765/api/runs/{rid}/samples/cpu", timeout=2).json()
            if isinstance(cr, list) and cr:
                m["temp_c"] = float(cr[-1].get("package_temp", 0))
                m["util"]   = float(cr[-1].get("cpu_util_percent", 0))
                m["ipc"]    = float(cr[-1].get("ipc", 0))
        except Exception:
            pass
        try:
            ir = _req.get(f"http://127.0.0.1:8765/api/runs/{rid}/samples/interrupts", timeout=2).json()
            if isinstance(ir, list) and ir:
                m["irq"] = float(ir[-1].get("interrupts_per_sec", 0))
        except Exception:
            pass
        _store_set("metrics", m)

    def _run_one(exp, exp_sid):
        _store_set("run_record",    {"sid": exp_sid, "name": exp.get("name","?"), "exp": exp})
        _store_set("log",           [])
        _store_set("progress",      0.0)
        _store_set("phase",         "starting")
        _store_set("metrics",       {})
        _store_set("result_rows",   [])
        _store_set("group_id",      "")
        _store_set("stop",          False)
        _store_set("current_cmd",   " ".join(str(x) for x in exp.get("cmd", [])))

        cmd = exp.get("cmd", [])
        if not cmd:
            _store_log(f"[ERROR] No cmd found in experiment config: {exp}")
            return -1

        last_rid = _db1("SELECT COALESCE(MAX(run_id),0) FROM runs") or 0
        line_ctr = 0
        rc       = -1

        try:
            proc = subprocess.Popen(
                cmd, stdout=subprocess.PIPE, stderr=subprocess.STDOUT,
                text=True, cwd=str(PROJECT_ROOT), bufsize=1)

            for raw in iter(proc.stdout.readline, ""):
                line = raw.rstrip()
                if not line:
                    continue
                _store_log(line)

                if _store_get("stop", False):
                    proc.terminate()
                    _store_log("[STOPPED by user]")
                    proc.wait()
                    return -2

                lo = line.lower()
                if   "planning"  in lo: _store_set("phase", "planning")
                elif "execution" in lo: _store_set("phase", "execution")
                elif "synth"     in lo: _store_set("phase", "synthesis")
                elif "rep " in lo or "pair" in lo: _store_set("phase", "running")
                if any(k in lo for k in ["complete", "saved", "✅"]):
                    _store_set("phase", "complete")

                for pat in ["rep ", "pair ", "progress:"]:
                    if pat in lo and "/" in lo:
                        try:
                            seg = lo.split(pat)[-1].split("/")
                            d = int(seg[0].strip().split()[-1])
                            t = int(seg[1].split()[0])
                            _store_set("progress", min(d / t, 1.0))
                        except Exception:
                            pass
                        break

                _refresh_gid()
                line_ctr += 1
                if line_ctr % 5 == 0:
                    nr = _db1("SELECT COALESCE(MAX(run_id),0) FROM runs") or last_rid
                    if nr > last_rid:
                        last_rid = nr
                    _poll_telemetry(last_rid)

            proc.wait()
            rc = proc.returncode
        except Exception as ex:
            _store_log(f"[ERROR] {ex}")
            rc = -1

        _store_set("phase",    "complete" if rc == 0 else "error")
        _store_set("progress", 1.0)
        _refresh_gid()

        # FIX #1: Read tax from DB — no more stdout parsing
        gid = _store_get("group_id", "")
        rows = _load_tax_from_db(gid) if gid else []
        _store_set("result_rows", rows)

        _store_append("sessions", {
            "sid":          exp_sid,
            "name":         exp.get("name", "?"),
            "status":       "complete" if rc == 0 else "error",
            "log":          _store_get("log", []).copy(),
            "summary_rows": rows,
            "ts":           _time.strftime("%H:%M:%S"),
            "rc":           rc,
            "group_id":     gid,
        })
        return rc

    _store_set("running", True)
    _store_set("done",    False)

    _run_one(_first_exp, sid)

    # Safety cap: never drain more than 50 queued experiments per thread start
    _max_drain = 50
    _drained   = 0
    while _drained < _max_drain:
        if _store_get("stop", False):
            break
        with _STORE_LOCK:
            q_list = _STORE.get("queue", [])
            if not q_list:
                break
            nxt = q_list.pop(0)
        _drained += 1
        _run_one(nxt, f"ses_{int(_time.time()*1000)}")

    _store_set("running", False)
    _store_set("done",    True)


# ══════════════════════════════════════════════════════════════════════════════
# FIX #1: TAX FROM DB — agentic_energy / linear_energy, always > 1
# ══════════════════════════════════════════════════════════════════════════════

def _load_tax_from_db(group_id: str) -> list:
    """
    Load orchestration tax directly from orchestration_tax_summary.
    tax_multiplier = agentic_dynamic_uj / linear_dynamic_uj  → always >= 1
    Falls back to computing from runs table if summary table is empty.
    """
    if not group_id:
        return []
    try:
        df = q(f"""
            SELECT
                el.provider,
                el.task_name                          AS task,
                rl.total_energy_uj / 1e6              AS linear_j,
                ra.total_energy_uj / 1e6              AS agentic_j,
                -- FIX: agentic / linear, always >= 1
                CASE WHEN rl.total_energy_uj > 0
                     THEN CAST(ra.total_energy_uj AS REAL) / rl.total_energy_uj
                     ELSE 1.0 END                     AS tax_x,
                '' AS ci
            FROM orchestration_tax_summary ots
            JOIN runs rl ON ots.linear_run_id  = rl.run_id
            JOIN runs ra ON ots.agentic_run_id = ra.run_id
            JOIN experiments el ON rl.exp_id = el.exp_id
            WHERE el.group_id = '{group_id}'
            ORDER BY ots.comparison_id
        """)
        if df is not None and not df.empty:
            return df.to_dict("records")
    except Exception:
        pass

    # Fallback: compute from runs directly
    try:
        df2 = q(f"""
            SELECT
                e.provider,
                e.task_name                                      AS task,
                AVG(CASE WHEN r.workflow_type='linear'
                         THEN r.total_energy_uj/1e6 END)         AS linear_j,
                AVG(CASE WHEN r.workflow_type='agentic'
                         THEN r.total_energy_uj/1e6 END)         AS agentic_j
            FROM runs r
            JOIN experiments e ON r.exp_id = e.exp_id
            WHERE e.group_id = '{group_id}'
            GROUP BY e.provider, e.task_name
            HAVING linear_j IS NOT NULL AND agentic_j IS NOT NULL
        """)
        if df2 is not None and not df2.empty:
            df2["tax_x"] = df2["agentic_j"] / df2["linear_j"].clip(lower=1e-9)
            df2["ci"] = ""
            return df2.to_dict("records")
    except Exception:
        pass
    return []


# ══════════════════════════════════════════════════════════════════════════════
# LIVE VIEW WIDGET
# ══════════════════════════════════════════════════════════════════════════════

def _render_live_view():
    _running = _store_get('running', False)
    _done    = _store_get('done',    False)
    if not (_running or _done):
        return
    st.session_state.ex_running = _running
    st.session_state.ex_done    = _done

    rec   = _store_get('run_record') or {}
    phase = _store_get('phase', 'idle')
    prog  = _store_get('progress', 0.0)
    log   = _store_get('log', [])
    m     = _store_get('metrics', {})
    gid   = _store_get('group_id', '')

    pc = {"starting": "#7090b0", "planning": "#f59e0b",
          "execution": "#3b82f6", "synthesis": "#a78bfa",
          "running": "#22c55e", "complete": "#22c55e",
          "error": "#ef4444", "idle": "#3d5570"}.get(phase, "#7090b0")

    hdr_col, stop_col = st.columns([5, 1])
    cur_cmd = _store_get('current_cmd', '')
    hdr_col.markdown(
        f"<div style='background:#080d18;border:1px solid {pc}44;"
        f"border-left:4px solid {pc};border-radius:5px;"
        f"padding:8px 14px;margin:2px 0;display:flex;align-items:center;gap:14px;'>"
        f"<span style='font-size:10px;padding:3px 10px;background:{pc}22;"
        f"border:1px solid {pc};border-radius:4px;color:{pc};font-weight:700;'>"
        f"● {phase.upper()}</span>"
        f"<span style='font-size:10px;color:#e8f0f8;margin-left:6px;font-weight:600;'>"
        f"{rec.get('name','')}</span>"
        f"<span style='font-size:9px;color:#3d5570;margin-left:8px;'>{int(prog*100)}%</span>"
        f"{'<span style=\"font-size:9px;color:#22c55e;margin-left:8px;\">⚡ RUNNING</span>' if _running else ''}"
        f"</div>",
        unsafe_allow_html=True)

    if _running:
        if stop_col.button('⏹ Stop', type='secondary', use_container_width=True,
                           key='stop_run_btn'):
            _store_set('stop', True)
            st.warning('Stop signal sent.')

    if cur_cmd:
        st.markdown(
            f"<div style='font-family:monospace;font-size:9px;color:#3d5570;"
            f"background:#05080f;border:1px solid #1e2d45;border-radius:4px;"
            f"padding:4px 10px;margin-bottom:4px;overflow-x:auto;white-space:nowrap;'>"
            f"$ {cur_cmd[:200]}</div>",
            unsafe_allow_html=True)
    st.progress(prog)

    left_col, right_col = st.columns([1, 1])

    with left_col:
        st.markdown(
            "<div style='font-size:10px;font-weight:700;color:#7090b0;"
            "text-transform:uppercase;letter-spacing:.08em;margin-bottom:4px;'>"
            "🌳 Session Tree</div>", unsafe_allow_html=True)
        render_session_tree(group_id=gid, expanded=True,
                            live_log=_store_get('log', []), key_suffix="live")
        st.markdown(
            "<div style='font-size:10px;font-weight:700;color:#7090b0;"
            "text-transform:uppercase;letter-spacing:.08em;margin:6px 0 2px;'>"
            "⏱ Timeline</div>", unsafe_allow_html=True)
        # FIX #3: stable chart — no blink
        _gantt_chart_stable(gid)

    with right_col:
        st.markdown(
            "<div style='font-size:10px;font-weight:700;color:#7090b0;"
            "text-transform:uppercase;letter-spacing:.08em;margin-bottom:6px;'>"
            "⚡ Live Telemetry</div>", unsafe_allow_html=True)
        st.markdown(
            f"<div style='display:flex;justify-content:space-around;'>"
            f"{_gauge_svg(m.get('pkg_w',0),0,80,'Pkg Power','W','#3b82f6',warn=50,danger=70)}"
            f"{_gauge_svg(m.get('core_w',0),0,60,'Core Power','W','#22c55e',warn=40,danger=55)}"
            f"{_gauge_svg(m.get('temp_c',0),30,105,'Pkg Temp','°C','#f59e0b',warn=80,danger=95)}"
            f"</div>", unsafe_allow_html=True)
        st.markdown(
            _bar_gauge(m.get('util', 0),   100,   "CPU Util",  "%",        "#38bdf8") +
            _bar_gauge(min(m.get('irq', 0), 50000), 50000, "IRQ Rate", "/s", "#f59e0b") +
            _bar_gauge(m.get('ipc', 0),    3.0,   "IPC",       "inst/cyc", "#a78bfa"),
            unsafe_allow_html=True)

        if log:
            log_html = "".join(
                f"<div style='color:"
                f"{'#ef4444' if any(k in l.lower() for k in ['error','fail','traceback']) else '#22c55e' if any(k in l.lower() for k in ['complete','saved','✅','pair']) else '#f59e0b' if 'planning' in l.lower() else '#b8c8d8'};"
                f"font-family:monospace;font-size:9px;line-height:1.5;'>"
                f"{l.replace('<','&lt;').replace('>','&gt;')}</div>"
                for l in log[-40:])
            st.markdown(
                "<div style='background:#050810;border:1px solid #1e2d45;"
                "border-radius:4px;padding:8px;height:220px;overflow-y:auto;'>"
                f"{log_html}</div>", unsafe_allow_html=True)

    if _running:
        st.rerun()

    if st.session_state.ex_done and st.session_state.ex_result_rows:
        st.divider()
        st.markdown("### 📊 Results")
        _analytics_card({
            "sid":          rec.get("sid", ""),
            "summary_rows": st.session_state.ex_result_rows,
            "log":          log,
        })
        st.session_state.ex_done = False

    if not _running and st.session_state.ex_queue:
        st.info(f"⏳ {len(st.session_state.ex_queue)} more queued — click ▶ Start again.")


# ══════════════════════════════════════════════════════════════════════════════
# ANALYTICS CARD  (uses DB-sourced tax rows — always agentic/linear >= 1)
# ══════════════════════════════════════════════════════════════════════════════

def _analytics_card(session: dict):
    rows  = session.get("summary_rows", [])
    lines = session.get("log", [])
    sid   = session.get("sid", "x")
    gid   = session.get("group_id", "")

    # If no rows in memory, try loading from DB using group_id
    if not rows and gid:
        rows = _load_tax_from_db(gid)

    if rows:
        _thresholds = INSIGHTS_RULES.get("tax_thresholds", {})
        def _tax_color(tx):
            if tx >= _thresholds.get("extreme", {}).get("min", 15):   return "#ef4444"
            if tx >= _thresholds.get("high",    {}).get("max", 15):   return "#f59e0b"
            if tx >= _thresholds.get("moderate",{}).get("max",  5):   return "#38bdf8"
            return "#22c55e"

        rh = ""
        for r in rows:
            lin_j = float(r.get("linear_j", 0) or 0)
            age_j = float(r.get("agentic_j", 0) or 0)
            tax_x = float(r.get("tax_x", 0) or 0)
            # Ensure tax is always agentic/linear
            if lin_j > 0 and age_j > 0:
                tax_x = age_j / lin_j
            tc  = _tax_color(tax_x)
            mx  = max(lin_j, age_j, 0.001)
            lw  = lin_j / mx * 100
            aw  = age_j / mx * 100
            hi   = _human_energy(age_j)
            hi_s = hi[0][1] if hi else ""
            rh += (
                f"<tr style='border-bottom:1px solid #111827;'>"
                f"<td style='padding:9px 8px;font-size:10px;color:#7090b0;'>"
                f"{r.get('provider','')}</td>"
                f"<td style='padding:9px 8px;font-size:10px;color:#e8f0f8;min-width:140px;'>"
                f"{r.get('task','')}</td>"
                f"<td style='padding:9px 8px;'>"
                f"<div style='font-size:11px;color:#22c55e;font-family:monospace;'>"
                f"{lin_j:.4f} J</div>"
                f"<div style='background:#1e2d45;border-radius:2px;height:5px;width:110px;margin-top:3px;'>"
                f"<div style='background:#22c55e;width:{lw:.0f}%;height:100%;border-radius:2px;'></div></div>"
                f"</td><td style='padding:9px 8px;'>"
                f"<div style='font-size:11px;color:#ef4444;font-family:monospace;'>"
                f"{age_j:.4f} J</div>"
                f"<div style='background:#1e2d45;border-radius:2px;height:5px;width:110px;margin-top:3px;'>"
                f"<div style='background:#ef4444;width:{aw:.0f}%;height:100%;border-radius:2px;'></div></div>"
                f"</td><td style='padding:9px 8px;text-align:center;'>"
                f"<span style='font-size:14px;font-weight:700;color:{tc};font-family:monospace;'>"
                f"{tax_x:.2f}×</span>"
                f"</td><td style='padding:9px 8px;font-size:9px;color:#7090b0;'>"
                f"{hi_s}</td>"
                f"</tr>"
            )

        st.markdown(
            "<div style='background:#07090f;border:1px solid #1e2d45;border-radius:8px;"
            "overflow:hidden;margin:10px 0;'>"
            "<div style='background:#0a0e1a;padding:8px 14px;border-bottom:1px solid #1e2d45;"
            "font-size:10px;font-weight:700;color:#4fc3f7;letter-spacing:.08em;"
            "text-transform:uppercase;'>⚡ Agentic vs Linear — Energy Overhead</div>"
            "<table style='width:100%;border-collapse:collapse;'>"
            "<thead><tr style='background:#0a0e1a;border-bottom:2px solid #1e2d45;'>"
            "<th style='padding:7px 8px;font-size:9px;color:#3d5570;text-align:left;text-transform:uppercase;'>Provider</th>"
            "<th style='padding:7px 8px;font-size:9px;color:#3d5570;text-align:left;text-transform:uppercase;'>Task</th>"
            "<th style='padding:7px 8px;font-size:9px;color:#22c55e;text-align:left;text-transform:uppercase;'>Linear</th>"
            "<th style='padding:7px 8px;font-size:9px;color:#ef4444;text-align:left;text-transform:uppercase;'>Agentic</th>"
            "<th style='padding:7px 8px;font-size:9px;color:#f59e0b;text-align:center;text-transform:uppercase;'>Tax (A/L)</th>"
            "<th style='padding:7px 8px;font-size:9px;color:#3d5570;text-align:left;text-transform:uppercase;'>Insight</th>"
            f"</tr></thead><tbody>{rh}</tbody></table></div>",
            unsafe_allow_html=True,
        )

        if len(rows) > 1:
            tax_vals = [age_j / max(lin_j, 1e-9)
                        for r in rows
                        for lin_j, age_j in [(float(r.get("linear_j",0) or 0),
                                              float(r.get("agentic_j",0) or 0))]]
            best_r  = min(rows, key=lambda r: float(r.get("agentic_j",0) or 0) /
                          max(float(r.get("linear_j",0) or 1), 1e-9))
            worst_r = max(rows, key=lambda r: float(r.get("agentic_j",0) or 0) /
                          max(float(r.get("linear_j",0) or 1), 1e-9))
            avg_t = sum(tax_vals) / len(tax_vals)
            best_t  = float(best_r.get("agentic_j",0)) / max(float(best_r.get("linear_j",1)), 1e-9)
            worst_t = float(worst_r.get("agentic_j",0)) / max(float(worst_r.get("linear_j",1)), 1e-9)
            c1, c2, c3 = st.columns(3)
            c1.success(f"**✅ Lowest overhead**\n\n{best_r.get('provider','')} · "
                       f"{str(best_r.get('task',''))[:24]}\n\n**{best_t:.2f}×**")
            c2.error(f"**⚠ Highest overhead**\n\n{worst_r.get('provider','')} · "
                     f"{str(worst_r.get('task',''))[:24]}\n\n**{worst_t:.2f}×**")
            c3.info(f"**📈 Average**\n\n{len(rows)} comparisons · **{avg_t:.2f}×** mean tax")

        df  = pd.DataFrame(rows)
        df["label"] = df["provider"].astype(str) + " · " + df["task"].astype(str).str[:22]
        fig = go.Figure()
        fig.add_trace(go.Bar(name="Linear",  x=df["label"], y=df["linear_j"],
                             marker_color="#22c55e", text=df["linear_j"].round(3),
                             textposition="outside", textfont=dict(size=8)))
        fig.add_trace(go.Bar(name="Agentic", x=df["label"], y=df["agentic_j"],
                             marker_color="#ef4444", text=df["agentic_j"].round(3),
                             textposition="outside", textfont=dict(size=8)))
        _pl2 = {k: v for k, v in PL.items() if k != 'margin'}
        fig.update_layout(**_pl2, barmode="group", height=260,
                          title="Linear vs Agentic energy",
                          xaxis_tickangle=20, margin=dict(t=40, b=10))
        import time as _t
        st.plotly_chart(fig, use_container_width=True,
                        key=f'ac_{sid}_{_t.time_ns()}')

        csv = df[["provider", "task", "linear_j", "agentic_j"]].copy()
        csv["tax_x"] = csv["agentic_j"] / csv["linear_j"].clip(lower=1e-9)
        st.download_button("📥 Export CSV", csv.to_csv(index=False),
                           file_name=f"alems_{sid}.csv",
                           mime="text/csv", key=f"csv_{sid}_{__import__('time').time_ns()}")
    else:
        st.info("No tax data found yet. Results appear after the experiment completes.")

    with st.expander("📋 Raw log", expanded=False):
        log_html = "".join(
            f"<div style='color:{'#ef4444' if any(k in l.lower() for k in ['error','fail']) else '#22c55e' if any(k in l.lower() for k in ['complete','saved','✅']) else '#b8c8d8'};"
            f"font-family:monospace;font-size:10px;line-height:1.5;'>"
            f"{l.replace('<','&lt;').replace('>','&gt;')}</div>"
            for l in lines)
        st.markdown(
            "<div style='background:#050810;border:1px solid #1e2d45;border-radius:4px;"
            f"padding:10px;max-height:300px;overflow-y:auto;'>{log_html}</div>",
            unsafe_allow_html=True,
        )


# ══════════════════════════════════════════════════════════════════════════════
# REMOTE EXECUTION
# ══════════════════════════════════════════════════════════════════════════════

def _run_remote(exp: dict, session_id: str, base_url: str):
    lines = []; summary_rows = []
    if not _REQUESTS_OK:
        st.error("pip install requests"); return -1, lines, summary_rows

    prog_ph = st.progress(0); status_ph = st.empty()
    cols = st.columns([11, 9])
    with cols[0]:
        st.markdown("<div style='font-size:10px;font-weight:600;color:#7090b0;"
                    "text-transform:uppercase;letter-spacing:.08em;margin-bottom:4px;'>"
                    "⬛ Remote terminal</div>", unsafe_allow_html=True)
        out_ph = st.empty()
    with cols[1]:
        st.markdown("<div style='font-size:10px;font-weight:600;color:#7090b0;"
                    "text-transform:uppercase;letter-spacing:.08em;margin-bottom:4px;'>"
                    "⚡ Live telemetry</div>", unsafe_allow_html=True)
        phase_ph = st.empty(); gauge_ph = st.empty(); bar_ph = st.empty()

    _pw = _core_w = _tp = _util = _irq = _ipc = 0.0

    def _draw(phase):
        gauge_ph.markdown(
            f"<div style='display:flex;justify-content:space-around;'>"
            f"{_gauge_svg(_pw,0,80,'Pkg Power','W','#3b82f6',warn=50,danger=70)}"
            f"{_gauge_svg(_core_w,0,60,'Core Power','W','#22c55e',warn=40,danger=55)}"
            f"{_gauge_svg(_tp,30,105,'Pkg Temp','°C','#f59e0b',warn=80,danger=95)}"
            f"</div>", unsafe_allow_html=True)
        bar_ph.markdown(
            _bar_gauge(_util, 100, "CPU Util", "%", "#38bdf8") +
            _bar_gauge(min(_irq, 50000), 50000, "IRQ Rate", "/s", "#f59e0b") +
            _bar_gauge(_ipc, 3.0, "IPC", "inst/cyc", "#a78bfa"),
            unsafe_allow_html=True)
        pc = {"starting": "#7090b0", "running": "#22c55e",
              "complete": "#22c55e", "error": "#ef4444"}.get(phase, "#7090b0")
        phase_ph.markdown(
            f"<div style='font-size:10px;padding:3px 10px;background:{pc}22;"
            f"border:1px solid {pc};border-radius:4px;display:inline-block;color:{pc};'>"
            f"● {phase.upper()}</div>", unsafe_allow_html=True)

    seen = 0
    for _ in range(600):
        _time.sleep(1)
        try:
            r    = _req.get(f"{base_url}/api/run/status/{session_id}", timeout=6)
            data = r.json()
        except Exception as e:
            status_ph.warning(f"Poll error: {e}"); continue

        status = data.get("status", "?")
        log    = data.get("log", [])
        prog   = float(data.get("progress", 0))
        prog_ph.progress(min(prog, 1.0))

        new = log[seen:]; seen = len(log)
        for l in new: lines.append(l)

        if lines:
            html = "".join(
                f"<div style='color:{'#ef4444' if any(k in l.lower() for k in ['error','fail']) else '#22c55e' if any(k in l.lower() for k in ['complete','✅','saved']) else '#b8c8d8'};"
                f"font-family:monospace;font-size:10px;line-height:1.5;'>"
                f"{l.replace('<','&lt;').replace('>','&gt;')}</div>"
                for l in lines[-50:])
            out_ph.markdown(
                "<div style='background:#060a0f;border:1px solid #1e2d45;border-radius:4px;"
                f"padding:8px;max-height:340px;overflow-y:auto;'>{html}</div>",
                unsafe_allow_html=True)

        _draw(status)
        status_ph.markdown(
            f"<div style='font-size:9px;color:#5a7090;'>Session <code>{session_id}</code>"
            f" · <b style='color:#4fc3f7;'>{status}</b></div>", unsafe_allow_html=True)

        if data.get("done") or status in ("complete", "error", "cancelled"):
            if status == "complete":
                prog_ph.progress(1.0)
                st.success("✅ Remote run complete.")
                # Load tax from DB after remote run completes
                gid = data.get("group_id", "")
                summary_rows = _load_tax_from_db(gid) if gid else []
            else:
                st.error(f"Run ended: {status}")
            return (0 if status == "complete" else 1), lines, summary_rows

    st.warning("Polling timed out."); return -1, lines, []


# ══════════════════════════════════════════════════════════════════════════════
# FIX #5 & #6: BEAUTIFUL RUN HISTORY CARD
# ══════════════════════════════════════════════════════════════════════════════

def _history_card(sess: dict, idx: int, expanded: bool):
    """Beautiful history card for Tab 4."""
    status  = sess.get("status", "?")
    is_ok   = status == "complete"
    clr     = "#22c55e" if is_ok else "#ef4444"
    icon    = "✅" if is_ok else "❌"
    name    = sess.get("name", "Run")
    ts      = sess.get("ts", "")
    rows    = sess.get("summary_rows", [])
    gid     = sess.get("group_id", "")
    n_pairs = len(rows)

    # Load from DB if in-memory rows empty
    if not rows and gid:
        rows = _load_tax_from_db(gid)
        n_pairs = len(rows)

    header = f"{icon}  {name}  ·  {ts}  ·  {n_pairs} pair{'s' if n_pairs != 1 else ''}"

    with st.expander(header, expanded=expanded):
        # Card header bar
        st.markdown(
            f"<div style='background:#080c18;border:1px solid {clr}33;"
            f"border-left:4px solid {clr};border-radius:6px;"
            f"padding:8px 14px;margin-bottom:10px;display:flex;"
            f"align-items:center;gap:16px;flex-wrap:wrap;'>"
            f"<span style='font-size:11px;font-weight:700;color:{clr};'>"
            f"{icon} {status.upper()}</span>"
            f"<span style='font-size:11px;color:#c8d8e8;font-weight:600;'>{name}</span>"
            f"<span style='font-size:10px;color:#3d5570;font-family:monospace;'>{ts}</span>"
            f"{'<span style=\"font-size:10px;color:#4b6080;font-family:monospace;\">'+gid+'</span>' if gid else ''}"
            f"</div>",
            unsafe_allow_html=True)

        if rows:
            # Mini tax summary grid
            cols = st.columns(min(len(rows), 3))
            for ci, r in enumerate(rows[:3]):
                lin_j = float(r.get("linear_j", 0) or 0)
                age_j = float(r.get("agentic_j", 0) or 0)
                tax   = age_j / max(lin_j, 1e-9)
                t_clr = "#ef4444" if tax > 10 else "#f59e0b" if tax > 5 else "#22c55e"
                with cols[ci]:
                    st.markdown(
                        f"<div style='background:#07090f;border:1px solid #1e2d45;"
                        f"border-radius:6px;padding:10px 12px;text-align:center;'>"
                        f"<div style='font-size:9px;color:#5a7090;margin-bottom:4px;'>"
                        f"{r.get('provider','')} · {str(r.get('task',''))[:18]}</div>"
                        f"<div style='font-size:22px;font-weight:800;color:{t_clr};"
                        f"font-family:monospace;line-height:1;'>{tax:.2f}×</div>"
                        f"<div style='font-size:8px;color:#3d5570;margin-top:4px;'>"
                        f"Linear {lin_j:.3f}J → Agentic {age_j:.3f}J</div>"
                        f"</div>",
                        unsafe_allow_html=True)

            st.markdown("")
            _analytics_card(dict(sess, summary_rows=rows))
        else:
            st.caption("No tax data available for this run.")


# ══════════════════════════════════════════════════════════════════════════════
# FIX #2: NON-BLOCKING get_conn()
# ══════════════════════════════════════════════════════════════════════════════

def _get_conn_safe() -> dict:
    """get_conn with 2s timeout so startup never hangs."""
    import concurrent.futures
    with concurrent.futures.ThreadPoolExecutor(max_workers=1) as ex:
        fut = ex.submit(get_conn)
        try:
            return fut.result(timeout=2)
        except Exception:
            return {}


# ══════════════════════════════════════════════════════════════════════════════
# MAIN RENDER
# ══════════════════════════════════════════════════════════════════════════════

def render(ctx: dict):
    _init_state()
    _load_queue()

    st.title("Execute Run")

    # FIX #2: non-blocking mode banner
    _conn = _get_conn_safe()
    if _conn.get("verified"):
        _hclr = "#22c55e" if _conn.get("harness") else "#f59e0b"
        _hmsg = ("Harness ready — runs execute on lab machine"
                 if _conn.get("harness") else "Server reachable but harness not loaded")
        st.markdown(
            f"<div style='background:#0a2010;border:1px solid #22c55e33;"
            f"border-left:3px solid #22c55e;border-radius:4px;"
            f"padding:8px 14px;margin-bottom:10px;font-size:11px;'>"
            f"🟢 <b style='color:#22c55e'>LIVE MODE</b>  ·  "
            f"<span style='color:{_hclr}'>{_hmsg}</span><br/>"
            f"<span style='color:#3d5570;font-size:9px;'>Tunnel: {_conn.get('url','')}</span></div>",
            unsafe_allow_html=True)
    else:
        st.markdown(
            "<div style='background:#0a0f1a;border:1px solid #1e2d45;"
            "border-left:3px solid #3b82f6;border-radius:4px;"
            "padding:8px 14px;margin-bottom:10px;font-size:11px;'>"
            "⚫ <b style='color:#3b82f6'>LOCAL MODE</b>  ·  "
            "<span style='color:#5a7090'>Runs execute on this machine.</span></div>",
            unsafe_allow_html=True)

    # Queue banner
    # FIX: Only sync session_state.ex_queue → _STORE when NOT running.
    # If running, the thread owns _STORE["queue"]; syncing overwrites pops done by thread
    # and causes infinite re-queuing.
    if 'ex_queue' not in st.session_state:
        st.session_state.ex_queue = _store_get('queue', [])
    elif not _store_get('running', False):
        _store_set('queue', list(st.session_state.ex_queue))
    qlen = len(st.session_state.ex_queue)
    if qlen > 0:
        st.markdown(
            f"<div style='background:#0f1a2e;border:1px solid #3b4fd8;border-radius:4px;"
            f"padding:7px 14px;margin-bottom:10px;font-size:11px;color:#93c5fd;'>"
            f"⏳ <b>{qlen}</b> experiment{'s' if qlen > 1 else ''} queued"
            f"{'  ·  🔴 run in progress' if st.session_state.ex_running else ''}"
            f"</div>",
            unsafe_allow_html=True)

    # Live view is now INSIDE tab 2 only — see TAB 2 block below
    st.session_state.ex_running = _store_get('running', False)
    st.session_state.ex_done    = _store_get('done', False)

    all_tasks, _cat_map, _name_map = _load_tasks()

    tab1, tab2, tab3, tab4 = st.tabs([
        "📋 Create & Queue",
        "⚡ Live Execution",
        "📊 Session Analysis",
        "📈 Run History",
    ])

    # ══════════════════════════════════════════════════════════════════════════
    # TAB 1 — CREATE & QUEUE
    # ══════════════════════════════════════════════════════════════════════════
    with tab1:
        _show_stuck_runs()
        left, right = st.columns([1, 1])

        with left:
            st.markdown("#### 🔬 Build Experiment")
            exp_name = st.text_input("Name", value="My Experiment", key="ex_name")
            exp_mode = st.radio("Mode", ["Single (test_harness)", "Batch (run_experiment)"],
                                horizontal=True, key="ex_mode")

            if not all_tasks:
                st.error("No tasks available. Please check config/tasks.yaml.")
                return

            if "Single" in exp_mode:
                task_labels = [f"{tid}  ({_cat_map.get(tid,'')})" for tid in all_tasks]
                h_task_idx  = st.selectbox("Task", range(len(all_tasks)),
                                            format_func=lambda i: task_labels[i],
                                            key="h_task_idx")
                h_task    = all_tasks[h_task_idx]
                h_prov    = st.selectbox("Provider", ["cloud", "local"], key="h_prov")
                h_reps    = st.number_input("Repetitions", 1, 100, 3, key="h_reps")
                h_country = st.selectbox("Region",
                    ["US","DE","FR","NO","IN","AU","GB","CN","BR"],
                    format_func=lambda x: {"US":"🇺🇸 US","DE":"🇩🇪 DE","FR":"🇫🇷 FR",
                        "NO":"🇳🇴 NO","IN":"🇮🇳 IN","AU":"🇦🇺 AU",
                        "GB":"🇬🇧 GB","CN":"🇨🇳 CN","BR":"🇧🇷 BR"}.get(x, x),
                    key="h_country")
                h_cd      = st.number_input("Cool-down (s)", 0, 120, 5, step=5, key="h_cd")
                h_save_db = st.checkbox("--save-db",   value=True,  key="h_savedb")
                h_opt     = st.checkbox("--optimizer", value=False, key="h_opt")
                h_warmup  = st.checkbox("--no-warmup", value=False, key="h_warmup")
                h_debug   = st.checkbox("--debug",     value=False, key="h_debug")

                cmd = ["python", "-m", "core.execution.tests.test_harness",
                       "--task-id", h_task, "--provider", h_prov,
                       "--repetitions", str(int(h_reps)), "--country", h_country,
                       "--cool-down", str(int(h_cd))]
                if h_save_db: cmd.append("--save-db")
                if h_opt:     cmd.append("--optimizer")
                if h_warmup:  cmd.append("--no-warmup")
                if h_debug:   cmd.append("--debug")

                meta = {"name": exp_name, "mode": "single", "task": h_task,
                        "provider": h_prov, "reps": int(h_reps),
                        "country": h_country, "cmd": cmd}

            else:
                _b_all = st.checkbox("All tasks", value=False, key="b_all")
                if _b_all:
                    _sel = all_tasks; st.caption(f"All {len(all_tasks)} tasks selected")
                else:
                    _sel = st.multiselect(
                        "Tasks", all_tasks,
                        default=all_tasks[:2] if len(all_tasks) >= 2 else all_tasks,
                        format_func=lambda t: f"{t}  ({_cat_map.get(t,'')})",
                        key="b_task_multi")

                b_prov    = st.multiselect("Providers", ["cloud", "local"],
                                           default=["cloud"], key="b_prov")
                b_reps    = st.number_input("Repetitions", 1, 100, 3, key="b_reps")
                b_country = st.selectbox("Region",
                    ["US","DE","FR","NO","IN","AU","GB","CN","BR"],
                    format_func=lambda x: {"US":"🇺🇸 US","DE":"🇩🇪 DE","FR":"🇫🇷 FR",
                        "NO":"🇳🇴 NO","IN":"🇮🇳 IN","AU":"🇦🇺 AU",
                        "GB":"🇬🇧 GB","CN":"🇨🇳 CN","BR":"🇧🇷 BR"}.get(x, x),
                    key="b_country")
                b_cd      = st.number_input("Cool-down (s)", 0, 120, 5, step=5, key="b_cd")
                b_save_db = st.checkbox("--save-db",   value=True,  key="b_savedb")
                b_opt     = st.checkbox("--optimizer", value=False, key="b_opt")
                b_warmup  = st.checkbox("--no-warmup", value=False, key="b_warmup")

                prov_arg  = ",".join(b_prov)  if b_prov else "cloud"
                tasks_arg = ",".join(_sel)    if _sel   else (all_tasks[0] if all_tasks else "")

                cmd = ["python", "-m", "core.execution.tests.run_experiment",
                       "--tasks", tasks_arg, "--providers", prov_arg,
                       "--repetitions", str(int(b_reps)), "--country", b_country,
                       "--cool-down", str(int(b_cd))]
                if b_save_db: cmd.append("--save-db")
                if b_opt:     cmd.append("--optimizer")
                if b_warmup:  cmd.append("--no-warmup")

                meta = {"name": exp_name, "mode": "batch", "tasks": _sel,
                        "providers": b_prov, "reps": int(b_reps),
                        "country": b_country, "cmd": cmd}

            st.code(" \\\n  ".join(cmd), language="bash")

            c1, c2, c3 = st.columns(3)
            if c1.button("💾 Save", use_container_width=True, key="ex_save"):
                st.session_state.ex_saved.append(dict(meta))
                st.success(f"Saved **{exp_name}**")

            if c2.button("▶ Run Now", type="primary", use_container_width=True,
                         key="ex_run_now"):
                if st.session_state.ex_running:
                    st.warning("A run is already in progress. Queue it instead.")
                else:
                    st.session_state.ex_queue.insert(0, dict(meta))
                    _save_queue()
                    st.success("Queued — go to ⚡ Live Execution"); st.rerun()

            if c3.button("➕ Queue", use_container_width=True, key="ex_queue_btn"):
                st.session_state.ex_queue.append(dict(meta))
                _save_queue()
                st.success(f"Queued at position {len(st.session_state.ex_queue)}")

        with right:
            st.markdown("#### 📁 Saved Experiments")
            if not st.session_state.ex_saved:
                st.caption("No saved experiments yet.")
            else:
                for i, exp in enumerate(st.session_state.ex_saved):
                    ea, eb, ec = st.columns([3, 1, 1])
                    ea.markdown(
                        f"<div style='font-size:12px;font-weight:600;color:#e8f0f8;'>"
                        f"{exp['name']}</div>"
                        f"<div style='font-size:10px;color:#7090b0;'>"
                        f"{exp.get('task', ', '.join(exp.get('tasks', [])))[:30]} · "
                        f"{exp.get('provider', '/'.join(exp.get('providers', [])))} · "
                        f"{exp.get('reps', 3)} reps</div>",
                        unsafe_allow_html=True)
                    if eb.button("▶", key=f"sv_run_{i}", use_container_width=True):
                        st.session_state.ex_queue.insert(0, dict(exp))
                        _save_queue(); st.rerun()
                    if ec.button("🗑", key=f"sv_del_{i}", use_container_width=True):
                        st.session_state.ex_saved.pop(i); st.rerun()

                if st.button("▶▶ Run All Saved", type="primary",
                             use_container_width=True, key="run_all"):
                    for e in st.session_state.ex_saved:
                        st.session_state.ex_queue.append(dict(e))
                    _save_queue()
                    st.success(f"Queued {len(st.session_state.ex_saved)} experiments")
                    st.rerun()

            st.divider()
            st.markdown("#### ⏳ Queue")
            if not st.session_state.ex_queue:
                st.caption("Queue is empty.")
            else:
                for i, exp in enumerate(st.session_state.ex_queue):
                    qa, qb = st.columns([4, 1])
                    qa.markdown(
                        f"<div style='font-size:11px;color:#93c5fd;'>"
                        f"#{i+1} — <b>{exp['name']}</b></div>",
                        unsafe_allow_html=True)
                    if qb.button("✕", key=f"q_del_{i}", use_container_width=True):
                        st.session_state.ex_queue.pop(i)
                        _save_queue(); st.rerun()

                if st.button("🗑 Clear queue", use_container_width=True, key="clear_q"):
                    st.session_state.ex_queue.clear()
                    _save_queue(); st.rerun()

    # ══════════════════════════════════════════════════════════════════════════
    # TAB 2 — LIVE EXECUTION (start button only)
    # ══════════════════════════════════════════════════════════════════════════
    with tab2:
        conn = _get_conn_safe()

        if not st.session_state.ex_running and st.session_state.ex_queue:
            next_exp = st.session_state.ex_queue[0]
            rem      = len(st.session_state.ex_queue) - 1

            st.markdown(
                f"<div style='background:#0a1a0a;border:1px solid #22c55e33;"
                f"border-left:3px solid #22c55e;border-radius:4px;"
                f"padding:8px 14px;margin-bottom:10px;font-size:12px;'>"
                f"▶ Ready: <b style='color:#22c55e'>{next_exp['name']}</b>"
                f"{'  ·  '+str(rem)+' more queued' if rem > 0 else ''}"
                f"</div>", unsafe_allow_html=True)

            if st.button(f"▶ Start — {next_exp['name']}", type="primary",
                         use_container_width=True, key="start_next"):

                exp = st.session_state.ex_queue.pop(0)
                _save_queue()
                sid = f"ses_{int(_time.time()*1000)}"

                if conn.get("verified"):
                    payload = {
                        "task_id":      exp.get("task", (exp.get("tasks") or [all_tasks[0] if all_tasks else "gsm8k_basic"])[0]),
                        "provider":     exp.get("provider", (exp.get("providers") or ["cloud"])[0]),
                        "country_code": exp.get("country", "US"),
                        "repetitions":  exp.get("reps", 3),
                        "cool_down":    5,
                        "tasks":        exp.get("tasks", [exp.get("task", all_tasks[0] if all_tasks else "gsm8k_basic")]),
                        "providers":    exp.get("providers", [exp.get("provider", "cloud")]),
                        "token":        conn.get("token", ""),
                    }
                    resp, err = api_post("/api/run/start", payload)
                    if err:
                        st.error(f"Remote start failed: {err}")
                    else:
                        rsid = resp.get("session_id", "")
                        st.success(f"✅ Started — session `{rsid}`")
                        rc, lines, rows = _run_remote(exp, rsid, conn["url"])
                        record = {"sid": sid, "name": exp["name"],
                                  "status": "complete" if rc == 0 else "error",
                                  "log": lines, "summary_rows": rows,
                                  "ts": _time.strftime("%H:%M:%S")}
                        st.session_state.ex_sessions.append(record)
                        if record["status"] == "complete":
                            _analytics_card(record)
                else:
                    st.session_state.ex_run_record = {"sid": sid, "name": exp["name"], "exp": exp}
                    # FIX: sync remaining queue to _STORE ONCE before starting thread
                    # then clear session queue so rerun loop can't re-add items
                    remaining = list(st.session_state.ex_queue)  # already popped first exp above
                    _store_set('queue', remaining)
                    st.session_state.ex_queue.clear()
                    _save_queue()
                    t = threading.Thread(
                        target=_thread_run_local, args=(exp, sid), daemon=True)
                    t.start()
                    st.session_state.ex_thread = t
                    st.rerun()

        elif not st.session_state.ex_running and not st.session_state.ex_queue:
            if not (st.session_state.ex_running or st.session_state.ex_done):
                st.info("Queue is empty. Go to 📋 Create & Queue to add experiments.")

        if st.session_state.ex_running or st.session_state.ex_done:
            _render_live_view()



    # ══════════════════════════════════════════════════════════════════════════
    # TAB 3 — SESSION ANALYSIS
    # ══════════════════════════════════════════════════════════════════════════
    with tab3:
        from gui.pages.session_analysis import render_session_analysis
        st.markdown("### 📊 Session Analysis")

        try:
            recent = q("""
                SELECT
                    e.group_id,
                    COUNT(DISTINCT e.exp_id)                                  AS n_exps,
                    COUNT(DISTINCT r.run_number ||'_'|| CAST(e.exp_id AS TEXT)) AS n_runs,
                    MAX(e.created_at)                                          AS latest
                FROM experiments e
                LEFT JOIN runs r ON r.exp_id = e.exp_id
                GROUP BY e.group_id
                ORDER BY MAX(e.exp_id) DESC
                LIMIT 10
            """)
        except Exception:
            recent = pd.DataFrame()

        gid_options = recent.group_id.tolist() if not recent.empty else []

        if not gid_options:
            st.info("No sessions in DB yet. Run an experiment first.")
        else:
            sel_gid = st.selectbox(
                "Select session",
                gid_options,
                format_func=lambda g: (
                    f"{g}  ({recent[recent.group_id==g].iloc[0].n_exps:.0f} exps, "
                    f"{recent[recent.group_id==g].iloc[0].n_runs or 0:.0f} runs)"
                    if not recent[recent.group_id==g].empty else g
                ),
                key="t3_gid_sel"
            )
            if sel_gid:
                render_session_tree(sel_gid, expanded=False, key_suffix="hist")
                render_session_analysis(sel_gid)

    # ══════════════════════════════════════════════════════════════════════════
    # TAB 4 — RUN HISTORY  (FIX #5: always below, beautiful cards)
    # ══════════════════════════════════════════════════════════════════════════
    with tab4:
        sessions = list(st.session_state.ex_sessions or [])

        if not sessions:
            st.markdown(
                "<div style='text-align:center;padding:60px 0;'>"
                "<div style='font-size:48px;margin-bottom:16px;'>📭</div>"
                "<div style='font-size:16px;color:#4b6080;font-weight:600;'>"
                "No runs yet</div>"
                "<div style='font-size:12px;color:#3d5570;margin-top:6px;'>"
                "Completed runs will appear here as expandable cards</div>"
                "</div>",
                unsafe_allow_html=True)
        else:
            # Newest first
            sessions_rev = list(reversed(sessions))
            n = len(sessions_rev)

            st.markdown(
                f"<div style='display:flex;align-items:center;gap:12px;margin-bottom:16px;'>"
                f"<div style='font-size:11px;font-weight:700;color:#7090b0;"
                f"text-transform:uppercase;letter-spacing:.1em;'>Run History</div>"
                f"<div style='background:#1e2d45;border-radius:10px;padding:2px 10px;"
                f"font-size:10px;color:#4fc3f7;font-weight:600;'>{n} run{'s' if n!=1 else ''}</div>"
                f"</div>",
                unsafe_allow_html=True)

            for i, sess in enumerate(sessions_rev):
                _history_card(sess, idx=i, expanded=(i == 0))