blyte 0.30.0

The blyte coding-agent harness: headless runs with a sealed verification gate, and a REPL.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
//! `blyte dashboard` local HTTP server and job collector (plan 2026-09-30).

use std::collections::HashSet;
use std::io::{BufRead, Write};
use std::path::{Path, PathBuf};
use std::time::UNIX_EPOCH;

#[derive(Debug, Clone)]
pub struct Sources {
    pub roots: Vec<PathBuf>,
    pub runs_dirs: Vec<PathBuf>,
    pub hours: u64,
}

#[derive(serde::Deserialize)]
struct DashboardConfig {
    roots: Option<Vec<String>>,
    runs_dirs: Option<Vec<String>>,
}

fn expand_home(path_str: &str, home: &Path) -> PathBuf {
    if let Some(rest) = path_str.strip_prefix("~/") {
        home.join(rest)
    } else if path_str == "~" {
        home.to_path_buf()
    } else {
        PathBuf::from(path_str)
    }
}

impl Sources {
    pub fn from_args(args: &crate::cli::DashboardArgs) -> Self {
        Self::from_args_with_where(args).0
    }

    pub fn from_args_with_where(args: &crate::cli::DashboardArgs) -> (Self, String) {
        if !args.roots.is_empty() || !args.runs_dirs.is_empty() {
            return (
                Self {
                    roots: args.roots.clone(),
                    runs_dirs: args.runs_dirs.clone(),
                    hours: args.hours,
                },
                "--root/--runs-dir".to_string(),
            );
        }

        let mut roots = Vec::new();
        let mut runs_dirs = Vec::new();
        let mut dashboard_contributed = false;

        if let Some(home) = std::env::var_os("HOME").map(PathBuf::from) {
            let config_path = home.join(".config/blyte/dashboard.toml");
            if let Ok(content) = std::fs::read_to_string(&config_path) {
                if let Ok(cfg) = toml::from_str::<DashboardConfig>(&content) {
                    if let Some(r) = cfg.roots {
                        for p in r {
                            let expanded = expand_home(&p, &home);
                            if !roots.contains(&expanded) {
                                roots.push(expanded);
                                dashboard_contributed = true;
                            }
                        }
                    }
                    if let Some(d) = cfg.runs_dirs {
                        for p in d {
                            let expanded = expand_home(&p, &home);
                            if !runs_dirs.contains(&expanded) {
                                runs_dirs.push(expanded);
                                dashboard_contributed = true;
                            }
                        }
                    }
                }
            }
        }

        let mut dispatch_contributed = false;
        for reg in crate::registry::registered() {
            let already_listed = roots
                .iter()
                .any(|r| r == &reg || std::fs::canonicalize(r).map(|c| c == reg).unwrap_or(false));
            if !already_listed {
                roots.push(reg);
                dispatch_contributed = true;
            }
        }

        let mut parts = Vec::new();
        if dashboard_contributed {
            parts.push("dashboard.toml");
        }
        if dispatch_contributed {
            parts.push("dispatch history");
        }
        let where_from = parts.join(" and ");

        (
            Self {
                roots,
                runs_dirs,
                hours: args.hours,
            },
            where_from,
        )
    }
}

#[derive(serde::Serialize, serde::Deserialize, Debug, Clone, PartialEq, Eq)]
pub struct CommitRecord {
    pub sha: String,
    pub subject: String,
}

#[derive(serde::Serialize, Debug, Clone)]
pub struct Job {
    pub key: String,
    pub id: String,
    pub repo: String,
    pub status: String,
    pub gate_running: bool,
    pub title: String,
    pub task: String,
    pub plan: Option<String>,
    pub now: String,
    pub started_at: Option<u64>,
    pub finished_at: Option<u64>,
    pub elapsed_s: Option<u64>,
    pub cost_usd: Option<f64>,
    pub turns: u64,
    pub gate_attempts: u64,
    pub gate_failures: Vec<String>,
    pub model: Option<String>,
    pub last_tool: Option<String>,
    pub worktree: Option<String>,
    pub worktree_exists: Option<bool>,
    pub log: String,
    pub commits: Vec<CommitRecord>,
    pub changed_files: Vec<String>,
    pub tags: Vec<String>,
    pub verdict: Option<String>,
    pub root: Option<PathBuf>,
    pub plan_file: Option<String>,
    pub plan_task: Option<String>,
    pub task_file: Option<String>,
    #[serde(skip)]
    pub research: bool,
    #[serde(skip)]
    pub start_plan: Option<String>,
    #[serde(skip)]
    pub file_tags: Vec<String>,
    #[serde(skip)]
    pub verdict_notes: Option<String>,
    #[serde(skip)]
    pub verdict_ts: Option<u64>,
    #[serde(skip)]
    pub log_mtime: Option<u64>,
}

#[derive(serde::Serialize, Debug, Clone)]
pub struct Summary {
    pub running: usize,
    pub last_24h: usize,
    pub repos: usize,
    pub finished_24h: usize,
    pub passed_24h: usize,
    pub cost_24h_usd: f64,
}

#[derive(serde::Serialize, Debug, Clone)]
pub struct DashboardResponse {
    pub generated_at: u64,
    pub summary: Summary,
    pub jobs: Vec<Job>,
}

fn strip_ansi(s: &str) -> String {
    let mut out = String::with_capacity(s.len());
    let mut chars = s.chars().peekable();
    while let Some(c) = chars.next() {
        if c == '\x1b' && chars.peek() == Some(&'[') {
            chars.next();
            while let Some(&next_c) = chars.peek() {
                chars.next();
                if ('\x40'..='\x7e').contains(&next_c) {
                    break;
                }
                if next_c == '\n' || next_c == '\x1b' {
                    out.push(next_c);
                    break;
                }
            }
            continue;
        }
        out.push(c);
    }
    out
}

fn extract_title(task: &str) -> String {
    for line in task.lines() {
        let trimmed = line.trim();
        if let Some(pos) = trimmed.find("Task: \"") {
            let rest = &trimmed[pos + "Task: \"".len()..];
            if let Some(end) = rest.find('"') {
                return rest[..end].chars().take(120).collect();
            }
        }
    }
    for line in task.lines() {
        let trimmed = line.trim();
        if trimmed.starts_with('#') {
            let text = trimmed.trim_start_matches('#').trim();
            if !text.is_empty() {
                return text.chars().take(120).collect();
            }
        }
    }
    for line in task.lines() {
        let trimmed = line.trim_start_matches(['#', ' ']).trim_end();
        if !trimmed.is_empty() {
            return trimmed.chars().take(120).collect();
        }
    }
    String::new()
}

fn job_title(task: &str, id: &str) -> String {
    let title = extract_title(task);
    if id.is_empty() {
        return title;
    }
    let p_colon = format!("Task {id}:");
    let p_space = format!("Task {id} ");
    let rest = if let Some(after) = title.strip_prefix(&p_colon) {
        after.trim()
    } else if let Some(after) = title.strip_prefix(&p_space) {
        after.trim()
    } else {
        return title;
    };
    if rest.is_empty() {
        title
    } else {
        rest.chars().take(120).collect()
    }
}

fn extract_plan(task: &str) -> Option<String> {
    for line in task.lines() {
        let trimmed = line.trim();
        if let Some(rest) = trimmed.strip_prefix("Plan: ") {
            let plan = rest.trim();
            if !plan.is_empty() {
                return Some(plan.to_string());
            }
        }
    }
    None
}

fn extract_prompt_task_title(prompt: &str) -> Option<String> {
    for line in prompt.lines() {
        let trimmed = line.trim();
        if let Some(pos) = trimmed.find("Task: \"") {
            let rest = &trimmed[pos + "Task: \"".len()..];
            if let Some(end) = rest.find('"') {
                return Some(rest[..end].to_string());
            }
        }
    }
    None
}

fn extract_first_task_id(text: &str) -> Option<String> {
    for line in text.lines() {
        let plain = plain_line(line);
        let mut idx = 0;
        while let Some(pos) = plain[idx..].find("Task") {
            let start = idx + pos;
            let after = &plain[start + 4..];
            if after.starts_with(' ') || after.starts_with('\t') {
                let trimmed = after.trim_start();
                let id: String = trimmed
                    .chars()
                    .take_while(|c| c.is_alphanumeric() || *c == '_' || *c == '-')
                    .collect();
                if !id.is_empty() {
                    return Some(format!("Task {id}"));
                }
            }
            idx = start + 4;
        }
    }
    None
}

fn parse_heading(line: &str) -> Option<(usize, &str)> {
    let trimmed = line.trim_start();
    let hashes = trimmed.chars().take_while(|&c| c == '#').count();
    if hashes > 0 {
        let after = &trimmed[hashes..];
        if after.is_empty() || after.starts_with(' ') || after.starts_with('\t') {
            return Some((hashes, after.trim()));
        }
    }
    None
}

fn extract_task_from_plan(plan_content: &str, title: &str) -> Option<String> {
    let mut target_heading = None;
    let mut offset = 0;

    for line in plan_content.split_inclusive('\n') {
        let line_len = line.len();
        let trimmed = line.trim_end_matches(['\r', '\n']);
        if let Some((level, text)) = parse_heading(trimmed) {
            if text == title {
                target_heading = Some((offset, level));
                break;
            }
        }
        offset += line_len;
    }

    let (start_offset, target_level) = target_heading?;

    // A `#` line inside a fenced code block (a shell comment, say) is not a heading.
    let mut end_offset = plan_content.len();
    let mut curr_offset = 0;
    let mut in_fence = false;
    for line in plan_content.split_inclusive('\n') {
        let line_len = line.len();
        if line.trim_start().starts_with("```") {
            in_fence = !in_fence;
        } else if curr_offset > start_offset && !in_fence {
            let trimmed = line.trim_end_matches(['\r', '\n']);
            if let Some((level, _)) = parse_heading(trimmed) {
                if level <= target_level {
                    end_offset = curr_offset;
                    break;
                }
            }
        }
        curr_offset += line_len;
    }

    Some(plan_content[start_offset..end_offset].to_string())
}

fn extract_now_from_final_message(msg: &str) -> String {
    let pick = msg
        .lines()
        .map(str::trim)
        .find(|l| !l.is_empty() && !l.starts_with('#'))
        .or_else(|| msg.lines().map(str::trim).find(|l| !l.is_empty()))
        .unwrap_or("");
    plain_line(pick.trim_start_matches(['#', ' ']))
        .chars()
        .take(200)
        .collect()
}

/// One line of markdown as plain text: no list marker, no `**`, `__` or backticks.
fn plain_line(line: &str) -> String {
    let mut l = line.trim();
    if let Some(rest) = l.strip_prefix("- ").or_else(|| l.strip_prefix("* ")) {
        l = rest;
    } else if let Some((n, rest)) = l.split_once(". ") {
        if !n.is_empty() && n.chars().all(|c| c.is_ascii_digit()) {
            l = rest;
        }
    }
    l.replace("**", "")
        .replace("__", "")
        .replace('`', "")
        .trim()
        .to_string()
}

struct LogInfo {
    turns: u64,
    last_tool: Option<String>,
    gate_attempts: u64,
    gate_running: bool,
    mtime: Option<u64>,
}

fn parse_log(path: &Path) -> LogInfo {
    let mtime = std::fs::metadata(path)
        .ok()
        .and_then(|m| m.modified().ok())
        .and_then(|t| t.duration_since(UNIX_EPOCH).ok())
        .map(|d| d.as_secs());

    let raw = std::fs::read_to_string(path).unwrap_or_default();
    let text = strip_ansi(&raw);

    let mut max_turn = 0u64;
    let mut last_tool = None;
    let mut gate_attempts = 0u64;
    let mut last_event_line: Option<String> = None;

    for line in text.lines() {
        let trimmed = line.trim();
        if let Some(pos) = trimmed.find("[Turn ") {
            let rest = &trimmed[pos + "[Turn ".len()..];
            if let Some(end) = rest.find(']') {
                if let Ok(n) = rest[..end].trim().parse::<u64>() {
                    if n > max_turn {
                        max_turn = n;
                    }
                }
            }
        }

        if let Some(pos) = trimmed.find("● ") {
            let tool = trimmed[pos + "● ".len()..].trim();
            last_tool = Some(tool.chars().take(200).collect());
        }

        if trimmed.starts_with("✓ Gate passed") || trimmed.starts_with("✗ Gate failed") {
            gate_attempts += 1;
        }

        if trimmed.contains("[Turn ")
            || trimmed.contains("● ")
            || trimmed.starts_with("⏳ Gate running")
            || trimmed.starts_with("✓ Gate passed")
            || trimmed.starts_with("✗ Gate failed")
        {
            last_event_line = Some(trimmed.to_string());
        }
    }

    let gate_running = last_event_line
        .map(|l| l.starts_with("⏳ Gate running"))
        .unwrap_or(false);

    LogInfo {
        turns: max_turn,
        last_tool,
        gate_attempts,
        gate_running,
        mtime,
    }
}

fn file_mtime(path: &Path) -> Option<u64> {
    std::fs::metadata(path)
        .ok()
        .and_then(|m| m.modified().ok())
        .and_then(|t| t.duration_since(UNIX_EPOCH).ok())
        .map(|d| d.as_secs())
}

pub fn parse_etime(s: &str) -> Option<u64> {
    let s = s.trim();
    if s.is_empty() {
        return None;
    }

    let (days, time_str) = match s.split_once('-') {
        Some((d, rest)) => {
            let days = d.parse::<u64>().ok()?;
            (days, rest)
        }
        None => (0, s),
    };

    let parts: Vec<&str> = time_str.split(':').collect();
    match parts.len() {
        2 => {
            let m = parts[0].parse::<u64>().ok()?;
            let sec = parts[1].parse::<u64>().ok()?;
            Some(days * 86400 + m * 60 + sec)
        }
        3 => {
            let h = parts[0].parse::<u64>().ok()?;
            let m = parts[1].parse::<u64>().ok()?;
            let sec = parts[2].parse::<u64>().ok()?;
            Some(days * 86400 + h * 3600 + m * 60 + sec)
        }
        _ => None,
    }
}

fn get_process_etime(pid: i64) -> Option<u64> {
    // macOS: ask the kernel for the start time. `ps` is setuid there, so blyte's own sandbox
    // cannot run it, and this also saves a process per PID.
    #[cfg(target_os = "macos")]
    {
        let mut info: libc::proc_bsdinfo = unsafe { std::mem::zeroed() };
        let size = std::mem::size_of::<libc::proc_bsdinfo>() as libc::c_int;
        let ret = unsafe {
            libc::proc_pidinfo(
                pid as libc::c_int,
                libc::PROC_PIDTBSDINFO,
                0,
                &mut info as *mut _ as *mut libc::c_void,
                size,
            )
        };
        if ret == size && info.pbi_start_tvsec > 0 {
            let now = std::time::SystemTime::now()
                .duration_since(UNIX_EPOCH)
                .map(|d| d.as_secs())
                .unwrap_or(0);
            return Some(now.saturating_sub(info.pbi_start_tvsec));
        }
    }

    if let Ok(output) = std::process::Command::new("ps")
        .args(["-o", "etime=", "-p", &pid.to_string()])
        .output()
    {
        if output.status.success() {
            let s = String::from_utf8_lossy(&output.stdout);
            if let Some(sec) = parse_etime(&s) {
                return Some(sec);
            }
        }
    }

    None
}

#[derive(serde::Deserialize, Debug)]
struct AgySummary {
    status: Option<String>,
    error: Option<String>,
    duration_seconds: Option<f64>,
    num_turns: Option<u64>,
    response: Option<String>,
}

struct RawJob {
    id: String,
    repo: String,
    root: Option<PathBuf>,
    task: String,
    task_file: Option<String>,
    pid: Option<i64>,
    started_at: Option<u64>,
    research: bool,
    start_plan: Option<String>,
    start_plan_task: Option<String>,
    file_tags: Vec<String>,
    log_path: PathBuf,
    report_path: PathBuf,
    tags_path: PathBuf,
    verdict_path: PathBuf,
    worktree: Option<String>,
    commits: Vec<CommitRecord>,
    changed_files: Vec<String>,
    summary_final_msg: Option<String>,
    is_runs_dir: bool,
    agy_summary_path: Option<PathBuf>,
}

pub fn repo_name(checkout: &Path) -> String {
    let from_git = std::process::Command::new("git")
        .args(["rev-parse", "--path-format=absolute", "--git-common-dir"])
        .current_dir(checkout)
        .output()
        .ok()
        .filter(|out| out.status.success())
        .and_then(|out| {
            let s = String::from_utf8_lossy(&out.stdout);
            let common = PathBuf::from(s.trim());
            if common.file_name().and_then(|n| n.to_str()) == Some(".git") {
                common
                    .parent()
                    .and_then(|p| p.file_name())
                    .and_then(|n| n.to_str())
                    .map(|n| n.to_string())
            } else {
                None
            }
        });

    let raw = from_git.unwrap_or_else(|| {
        checkout
            .file_name()
            .and_then(|n| n.to_str())
            .map(|n| n.to_string())
            .or_else(|| {
                checkout
                    .canonicalize()
                    .ok()
                    .as_deref()
                    .and_then(Path::file_name)
                    .and_then(|n| n.to_str())
                    .map(|n| n.to_string())
            })
            .unwrap_or_else(|| "unknown".to_string())
    });

    if let Some((prefix, _)) = raw.split_once("--") {
        if !prefix.is_empty() {
            return prefix.to_string();
        }
    }
    raw
}

pub fn collect_jobs(src: &Sources, now: u64) -> Vec<Job> {
    let mut raw_jobs = Vec::new();
    let mut seen_keys = HashSet::new();

    // 1. Dispatch layout
    let mut wt_dirs = Vec::new();
    for root in &src.roots {
        let r_wt = root.join(".wt");
        if r_wt.is_dir() && !wt_dirs.contains(&r_wt) {
            wt_dirs.push(r_wt);
        }
        if let Ok(entries) = std::fs::read_dir(root) {
            for entry in entries.flatten() {
                let path = entry.path();
                if path.is_dir() {
                    let c_wt = path.join(".wt");
                    if c_wt.is_dir() && !wt_dirs.contains(&c_wt) {
                        wt_dirs.push(c_wt);
                    }
                }
            }
        }
    }

    for wt_dir in &wt_dirs {
        let repo = wt_dir
            .parent()
            .map(repo_name)
            .unwrap_or_else(|| "unknown".to_string());

        if let Ok(entries) = std::fs::read_dir(wt_dir) {
            for entry in entries.flatten() {
                let fname = entry.file_name().to_string_lossy().to_string();
                if let Some(id) = fname.strip_suffix(".start.json") {
                    let id = id.to_string();
                    let key = format!("{repo}/{id}");
                    if !seen_keys.insert(key) {
                        continue;
                    }

                    let start_path = wt_dir.join(format!("{id}.start.json"));
                    let start_val = std::fs::read_to_string(&start_path)
                        .ok()
                        .and_then(|s| serde_json::from_str::<serde_json::Value>(&s).ok());
                    let pid = start_val.as_ref().and_then(|v| v["pid"].as_i64());
                    let started_at = start_val.as_ref().and_then(|v| v["started_at"].as_u64());
                    let research = start_val
                        .as_ref()
                        .and_then(|v| v.get("research"))
                        .and_then(|v| v.as_bool())
                        .unwrap_or(false);
                    let start_plan = start_val
                        .as_ref()
                        .and_then(|v| v["plan"].as_str())
                        .map(|s| s.to_string())
                        .filter(|s| !s.is_empty());
                    let start_plan_task = start_val
                        .as_ref()
                        .and_then(|v| v["plan_task"].as_str())
                        .map(|s| s.to_string())
                        .filter(|s| !s.is_empty());
                    let task_file = start_val
                        .as_ref()
                        .and_then(|v| v["task_file"].as_str())
                        .map(|s| s.to_string());
                    let task = task_file
                        .as_deref()
                        .and_then(|p| std::fs::read_to_string(p).ok())
                        .unwrap_or_default();
                    let mut file_tags = Vec::new();
                    if let Some(tags_arr) = start_val.as_ref().and_then(|v| v["tags"].as_array()) {
                        for t in tags_arr {
                            if let Some(s) = t.as_str() {
                                file_tags.push(s.to_string());
                            }
                        }
                    }
                    if let Some(s) = start_val
                        .as_ref()
                        .and_then(|v| v["session"].as_str())
                        .filter(|s| !s.is_empty())
                    {
                        let prefix: String = s.chars().take(8).collect();
                        let tag = format!("session:{prefix}");
                        if !file_tags.contains(&tag) {
                            file_tags.push(tag);
                        }
                    }

                    let summary_path = wt_dir.join(format!("{id}.json"));
                    let mut commits = Vec::new();
                    let mut changed_files = Vec::new();
                    let mut summary_final_msg = None;
                    let mut worktree = None;
                    if summary_path.is_file() {
                        if let Ok(content) = std::fs::read_to_string(&summary_path) {
                            if let Ok(val) = serde_json::from_str::<serde_json::Value>(&content) {
                                if let Some(arr) = val["commits"].as_array() {
                                    for c in arr {
                                        if let (Some(sha), Some(subject)) =
                                            (c["sha"].as_str(), c["subject"].as_str())
                                        {
                                            commits.push(CommitRecord {
                                                sha: sha.to_string(),
                                                subject: subject.to_string(),
                                            });
                                        }
                                    }
                                }
                                if let Some(arr) = val["changed_files"].as_array() {
                                    for f in arr {
                                        if let Some(s) = f.as_str() {
                                            changed_files.push(s.to_string());
                                        }
                                    }
                                }
                                summary_final_msg =
                                    val["final_message"].as_str().map(|s| s.to_string());
                                worktree = val["worktree"].as_str().map(|s| s.to_string());
                            }
                        }
                    }

                    raw_jobs.push(RawJob {
                        id: id.clone(),
                        repo: repo.clone(),
                        root: wt_dir.parent().map(|p| p.to_path_buf()),
                        task,
                        task_file,
                        pid,
                        started_at,
                        research,
                        start_plan,
                        start_plan_task,
                        file_tags,
                        log_path: wt_dir.join(format!("{id}.log")),
                        report_path: wt_dir.join(format!("{id}.report.json")),
                        tags_path: wt_dir.join(format!("{id}.tags.json")),
                        verdict_path: wt_dir.join(format!("{id}.verdict.json")),
                        worktree,
                        commits,
                        changed_files,
                        summary_final_msg,
                        is_runs_dir: false,
                        agy_summary_path: None,
                    });
                }
            }
        }
    }

    // 2. Runs-dir layout
    for runs_dir in &src.runs_dirs {
        if let Ok(repo_entries) = std::fs::read_dir(runs_dir) {
            for repo_entry in repo_entries.flatten() {
                let repo_path = repo_entry.path();
                if repo_path.is_dir() {
                    let repo = repo_path
                        .file_name()
                        .and_then(|n| n.to_str())
                        .unwrap_or("unknown")
                        .to_string();

                    if let Ok(entries) = std::fs::read_dir(&repo_path) {
                        for entry in entries.flatten() {
                            let fname = entry.file_name().to_string_lossy().to_string();
                            if let Some(id) = fname.strip_suffix(".log") {
                                let id = id.to_string();
                                let key = format!("{repo}/{id}");
                                if !seen_keys.insert(key) {
                                    continue;
                                }

                                let pid_path = repo_path.join(format!("{id}.pid"));
                                let (pid, started_at) = if pid_path.is_file() {
                                    let pid_text =
                                        std::fs::read_to_string(&pid_path).unwrap_or_default();
                                    let pid = pid_text.trim().parse::<i64>().ok();
                                    let mtime = file_mtime(&pid_path);
                                    (pid, mtime)
                                } else {
                                    (None, None)
                                };

                                let prompt_path = repo_path.join(format!("{id}.prompt"));
                                let task = if prompt_path.is_file() {
                                    std::fs::read_to_string(&prompt_path).unwrap_or_default()
                                } else {
                                    String::new()
                                };

                                let worktree_path = repo_path.join(format!("{id}.worktree"));
                                let worktree = if worktree_path.is_file() {
                                    std::fs::read_to_string(&worktree_path)
                                        .ok()
                                        .map(|s| s.trim().to_string())
                                        .filter(|s| !s.is_empty())
                                } else {
                                    None
                                };

                                let root = worktree.as_ref().and_then(|wt_str| {
                                    let p = Path::new(wt_str.trim_end_matches('/'));
                                    if p.exists() {
                                        if let Ok(out) = std::process::Command::new("git")
                                            .args(["rev-parse", "--git-common-dir"])
                                            .current_dir(p)
                                            .output()
                                        {
                                            if out.status.success() {
                                                let s = String::from_utf8_lossy(&out.stdout)
                                                    .trim()
                                                    .to_string();
                                                let common = PathBuf::from(s);
                                                let abs = if common.is_relative() {
                                                    p.join(common)
                                                } else {
                                                    common
                                                };
                                                if abs.file_name().and_then(|n| n.to_str())
                                                    == Some(".git")
                                                {
                                                    if let Some(parent) = abs.parent() {
                                                        return Some(parent.to_path_buf());
                                                    }
                                                }
                                            }
                                        }
                                    }
                                    if p.parent()
                                        .and_then(|par| par.file_name())
                                        .and_then(|n| n.to_str())
                                        == Some(".wt")
                                    {
                                        return p
                                            .parent()
                                            .and_then(|par| par.parent())
                                            .map(Path::to_path_buf);
                                    }
                                    if let (Some(par), Some(grandpar)) =
                                        (p.parent(), p.parent().and_then(|par| par.parent()))
                                    {
                                        if par.file_name().and_then(|n| n.to_str())
                                            == Some("worktrees")
                                            && grandpar.file_name().and_then(|n| n.to_str())
                                                == Some(".claude")
                                        {
                                            return grandpar.parent().map(Path::to_path_buf);
                                        }
                                    }
                                    if let Some(file_name) = p.file_name().and_then(|n| n.to_str())
                                    {
                                        if let Some((base, _)) = file_name.split_once("--") {
                                            if !base.is_empty() {
                                                return p.parent().map(|par| par.join(base));
                                            }
                                        }
                                    }
                                    Some(p.to_path_buf())
                                });

                                let agy_summary_path = {
                                    let p = repo_path.join(format!("{id}.json"));
                                    if p.is_file() {
                                        Some(p)
                                    } else {
                                        None
                                    }
                                };

                                raw_jobs.push(RawJob {
                                    id: id.clone(),
                                    repo: repo.clone(),
                                    root,
                                    task,
                                    task_file: None,
                                    pid,
                                    started_at,
                                    research: false,
                                    start_plan: None,
                                    start_plan_task: None,
                                    file_tags: Vec::new(),
                                    log_path: repo_path.join(format!("{id}.log")),
                                    report_path: repo_path.join(format!("{id}.report.json")),
                                    tags_path: repo_path.join(format!("{id}.tags.json")),
                                    verdict_path: repo_path.join(format!("{id}.verdict.json")),
                                    worktree,
                                    commits: Vec::new(),
                                    changed_files: Vec::new(),
                                    summary_final_msg: None,
                                    is_runs_dir: true,
                                    agy_summary_path,
                                });
                            }
                        }
                    }
                }
            }
        }
    }

    let mut jobs = Vec::new();
    let mut plan_cache: std::collections::HashMap<PathBuf, Option<String>> =
        std::collections::HashMap::new();

    for raw in raw_jobs {
        let title = job_title(&raw.task, &raw.id);
        let plan = extract_plan(&raw.task);
        let prompt_task_title = extract_prompt_task_title(&raw.task);
        let plan_file = raw.start_plan.clone().or_else(|| plan.clone());
        let plan_task = raw.start_plan_task.clone().or_else(|| {
            prompt_task_title
                .clone()
                .or_else(|| extract_first_task_id(&raw.task))
        });

        let task_content = if let (Some(ref p_str), Some(ref t_title)) = (&plan, &prompt_task_title)
        {
            let p_path = if let Some(home) = std::env::var_os("HOME").map(PathBuf::from) {
                expand_home(p_str, &home)
            } else {
                PathBuf::from(p_str)
            };
            let cached_plan = plan_cache
                .entry(p_path.clone())
                .or_insert_with(|| std::fs::read_to_string(&p_path).ok());
            if let Some(plan_text) = cached_plan {
                extract_task_from_plan(plan_text, t_title).unwrap_or_else(|| raw.task.clone())
            } else {
                raw.task.clone()
            }
        } else {
            raw.task.clone()
        };

        let mut file_tags = raw.file_tags.clone();
        if raw.tags_path.is_file() {
            if let Ok(content) = std::fs::read_to_string(&raw.tags_path) {
                if let Ok(arr) = serde_json::from_str::<Vec<String>>(&content) {
                    for t in arr {
                        if !file_tags.contains(&t) {
                            file_tags.push(t);
                        }
                    }
                }
            }
        }

        let mut tags = Vec::new();
        tags.push(format!("repo: {}", raw.repo));
        if let Some(ref p) = plan {
            if let Some(stem) = Path::new(p).file_stem().and_then(|s| s.to_str()) {
                tags.push(format!("plan: {stem}"));
            }
        }
        for t in &file_tags {
            if !tags.contains(t) {
                tags.push(t.clone());
            }
        }

        let (verdict, verdict_notes, verdict_ts) = if raw.verdict_path.is_file() {
            let val = std::fs::read_to_string(&raw.verdict_path)
                .ok()
                .and_then(|s| serde_json::from_str::<serde_json::Value>(&s).ok());
            let v = val
                .as_ref()
                .and_then(|obj| obj["verdict"].as_str().map(|s| s.to_string()));
            let notes = val
                .as_ref()
                .and_then(|obj| obj["notes"].as_str().map(|s| s.to_string()));
            let ts = val
                .as_ref()
                .and_then(|obj| obj["ts"].as_u64())
                .or_else(|| file_mtime(&raw.verdict_path));
            (v, notes, ts)
        } else {
            (None, None, None)
        };

        let log_info = parse_log(&raw.log_path);

        let report_json = if raw.report_path.is_file() {
            std::fs::read_to_string(&raw.report_path)
                .ok()
                .and_then(|s| serde_json::from_str::<serde_json::Value>(&s).ok())
        } else {
            None
        };

        let report_mtime = if raw.report_path.is_file() {
            file_mtime(&raw.report_path)
        } else {
            None
        };

        let agy_summary = if report_json.is_none() {
            raw.agy_summary_path.as_ref().and_then(|p| {
                std::fs::read_to_string(p)
                    .ok()
                    .and_then(|s| serde_json::from_str::<AgySummary>(&s).ok())
            })
        } else {
            None
        };

        let (
            status,
            gate_running,
            cost_usd,
            turns,
            gate_attempts,
            gate_failures,
            model,
            finished_at,
            elapsed_s,
            now_str,
        ) = if let Some(ref report) = report_json {
            let outcome = report["outcome"].as_str().unwrap_or("unknown").to_string();
            let cost = report["cost_usd"].as_f64();
            let turns = report["turns"].as_u64().unwrap_or(log_info.turns);
            let gate_attempts = report["gate_attempts"]
                .as_u64()
                .unwrap_or(log_info.gate_attempts);
            let model = report["model_id"].as_str().map(|s| s.to_string());

            let gate_failures = report["gate_failures"]
                .as_array()
                .map(|arr| {
                    let skip = arr.len().saturating_sub(2);
                    arr.iter()
                        .skip(skip)
                        .map(|v| {
                            let s = v.as_str().unwrap_or("");
                            let count = s.chars().count();
                            if count > 1500 {
                                s.chars().skip(count - 1500).collect()
                            } else {
                                s.to_string()
                            }
                        })
                        .collect()
                })
                .unwrap_or_default();

            let finished_at = report_mtime;
            let elapsed_s = if let Some(dur) = report["duration_seconds"].as_f64() {
                Some(dur.round() as u64)
            } else if let (Some(fin), Some(st)) = (finished_at, raw.started_at) {
                if fin >= st {
                    Some(fin - st)
                } else {
                    None
                }
            } else {
                None
            };

            let report_final_msg = report["final_message"].as_str();
            let now_from_msg = raw
                .summary_final_msg
                .as_deref()
                .or(report_final_msg)
                .map(extract_now_from_final_message)
                .filter(|s| !s.is_empty());
            let now_str = if let Some(s) = now_from_msg {
                s
            } else if let Some(ref tool) = log_info.last_tool {
                tool.clone()
            } else {
                String::new()
            };

            (
                outcome,
                false,
                cost,
                turns,
                gate_attempts,
                gate_failures,
                model,
                finished_at,
                elapsed_s,
                now_str,
            )
        } else if let Some(ref agy) = agy_summary {
            let status = if agy.status.as_deref() == Some("ERROR") {
                "error".to_string()
            } else {
                "finished".to_string()
            };
            let cost = None;
            let turns = agy.num_turns.unwrap_or(log_info.turns);
            let gate_attempts = log_info.gate_attempts;
            let gate_failures = Vec::new();
            let model = None;
            let finished_at = raw.agy_summary_path.as_ref().and_then(|p| file_mtime(p));
            let elapsed_s = agy.duration_seconds.map(|d| d.round() as u64);
            let now_str = if status == "error" {
                let err = agy.error.as_deref().unwrap_or("");
                let s = extract_now_from_final_message(err);
                if !s.is_empty() {
                    s
                } else {
                    err.lines().next().unwrap_or("").chars().take(200).collect()
                }
            } else {
                let resp = agy.response.as_deref().unwrap_or("");
                let s = extract_now_from_final_message(resp);
                if !s.is_empty() {
                    s
                } else {
                    resp.lines()
                        .next()
                        .unwrap_or("")
                        .chars()
                        .take(200)
                        .collect()
                }
            };

            (
                status,
                false,
                cost,
                turns,
                gate_attempts,
                gate_failures,
                model,
                finished_at,
                elapsed_s,
                now_str,
            )
        } else {
            // A live PID is this job's only if that process started with the job, not after it.
            // Only runs-dir jobs are checked: their PID files outlive the job for days, and the
            // dispatch layout's test fixture fakes a start time before its own process.
            let started_with_job = |p: i64| match (get_process_etime(p), raw.started_at) {
                (Some(elapsed), Some(started_at)) => now.saturating_sub(elapsed) <= started_at + 60,
                _ => true,
            };
            let is_alive = raw.pid.is_some_and(|p| {
                p > 0
                    && unsafe { libc::kill(p as libc::pid_t, 0) == 0 }
                    && (!raw.is_runs_dir || started_with_job(p))
            });
            let status = if is_alive {
                "running".to_string()
            } else {
                "stopped".to_string()
            };
            let gate_running = if status == "running" {
                log_info.gate_running
            } else {
                false
            };
            let cost = None;
            let turns = log_info.turns;
            let gate_attempts = log_info.gate_attempts;
            let gate_failures = Vec::new();
            let model = None;
            let finished_at = None;
            let elapsed_s = if status == "running" {
                raw.started_at.map(|s| now.saturating_sub(s))
            } else {
                None
            };
            let now_str = log_info.last_tool.clone().unwrap_or_default();

            (
                status,
                gate_running,
                cost,
                turns,
                gate_attempts,
                gate_failures,
                model,
                finished_at,
                elapsed_s,
                now_str,
            )
        };

        // Rule 7: Kept: every running job, and any other job whose finished_at (or, with no
        // report, the log's modified time) is within hours of now.
        let keep = if status == "running" {
            true
        } else {
            let effective_finished = finished_at.or(log_info.mtime);
            if let Some(fin) = effective_finished {
                fin >= now.saturating_sub(src.hours * 3600)
            } else {
                false
            }
        };
        if !keep {
            continue;
        }

        let task_truncated: String = task_content.chars().take(2000).collect();
        let worktree_exists = raw.worktree.as_deref().map(|wt| Path::new(wt).is_dir());

        jobs.push(Job {
            key: format!("{}/{}", raw.repo, raw.id),
            id: raw.id,
            repo: raw.repo,
            status,
            gate_running,
            title,
            task: task_truncated,
            plan,
            now: now_str,
            started_at: raw.started_at,
            finished_at,
            elapsed_s,
            cost_usd,
            turns,
            gate_attempts,
            gate_failures,
            model,
            last_tool: log_info.last_tool,
            worktree: raw.worktree,
            worktree_exists,
            log: raw.log_path.display().to_string(),
            commits: raw.commits,
            changed_files: raw.changed_files,
            tags,
            verdict,
            root: raw.root,
            plan_file,
            plan_task,
            task_file: raw.task_file,
            start_plan: raw.start_plan,
            file_tags,
            verdict_notes,
            verdict_ts,
            log_mtime: log_info.mtime,
            research: raw.research,
        });
    }

    // Sort jobs: running jobs first, newest started_at first; then the rest, newest finished_at first.
    jobs.sort_by(|a, b| {
        let a_running = a.status == "running";
        let b_running = b.status == "running";
        match (a_running, b_running) {
            (true, false) => std::cmp::Ordering::Less,
            (false, true) => std::cmp::Ordering::Greater,
            (true, true) => b
                .started_at
                .unwrap_or(0)
                .cmp(&a.started_at.unwrap_or(0))
                .then_with(|| a.key.cmp(&b.key)),
            (false, false) => {
                let a_fin = a.finished_at.or(a.log_mtime).unwrap_or(0);
                let b_fin = b.finished_at.or(b.log_mtime).unwrap_or(0);
                b_fin.cmp(&a_fin).then_with(|| a.key.cmp(&b.key))
            }
        }
    });

    jobs
}

pub fn collect(src: &Sources, now: u64) -> serde_json::Value {
    let jobs = collect_jobs(src, now);
    let running = jobs.iter().filter(|j| j.status == "running").count();
    let last_24h = jobs
        .iter()
        .filter(|j| {
            j.started_at
                .is_some_and(|s| s >= now.saturating_sub(24 * 3600))
        })
        .count();
    let repos = jobs.iter().map(|j| &j.repo).collect::<HashSet<_>>().len();

    let finished_jobs: Vec<_> = jobs
        .iter()
        .filter(|j| {
            j.status != "running"
                && j.finished_at
                    .or(j.log_mtime)
                    .is_some_and(|f| f >= now.saturating_sub(24 * 3600))
        })
        .collect();

    let finished_24h = finished_jobs.len();
    let passed_24h = finished_jobs
        .iter()
        .filter(|j| j.status == "verified")
        .count();
    let sum_cost: f64 = finished_jobs.iter().filter_map(|j| j.cost_usd).sum();
    let cost_24h_usd = (sum_cost * 100.0).round() / 100.0;

    let response = DashboardResponse {
        generated_at: now,
        summary: Summary {
            running,
            last_24h,
            repos,
            finished_24h,
            passed_24h,
            cost_24h_usd,
        },
        jobs,
    };

    serde_json::to_value(&response).unwrap()
}

pub fn route(path: &str, src: &Sources) -> (u16, &'static str, Vec<u8>) {
    let clean_path = path.split('?').next().unwrap_or(path);
    match clean_path {
        "/" | "/index.html" => {
            let html = include_str!("../assets/dashboard/index.html");
            (200, "text/html; charset=utf-8", html.as_bytes().to_vec())
        }
        "/favicon.svg" => {
            let svg = include_bytes!("../assets/dashboard/favicon.svg");
            (200, "image/svg+xml", svg.to_vec())
        }
        "/fonts/archivo.woff2" => {
            let font = include_bytes!("../assets/dashboard/fonts/archivo.woff2");
            (200, "font/woff2", font.to_vec())
        }
        "/fonts/plex-mono-400.woff2" => {
            let font = include_bytes!("../assets/dashboard/fonts/plex-mono-400.woff2");
            (200, "font/woff2", font.to_vec())
        }
        "/fonts/plex-mono-500.woff2" => {
            let font = include_bytes!("../assets/dashboard/fonts/plex-mono-500.woff2");
            (200, "font/woff2", font.to_vec())
        }
        "/api/jobs" => {
            let now = std::time::SystemTime::now()
                .duration_since(UNIX_EPOCH)
                .map(|d| d.as_secs())
                .unwrap_or(0);
            let val = collect(src, now);
            let body = serde_json::to_vec(&val).unwrap_or_default();
            (200, "application/json", body)
        }
        "/api/plans" => {
            let now = std::time::SystemTime::now()
                .duration_since(UNIX_EPOCH)
                .map(|d| d.as_secs())
                .unwrap_or(0);
            let query = path.split_once('?').map(|(_, q)| q).unwrap_or("");
            let running_only = query
                .split('&')
                .any(|p| p == "running=1" || p == "running=true");
            let val = crate::plans::collect_plans_filtered(src, now, running_only);
            let body = serde_json::to_vec(&val).unwrap_or_default();
            (200, "application/json", body)
        }
        _ => (404, "text/plain; charset=utf-8", b"Not Found\n".to_vec()),
    }
}

fn handle_connection(mut stream: std::net::TcpStream, src: &Sources) {
    let mut reader = std::io::BufReader::new(&mut stream);
    let mut request_line = String::new();
    if reader.read_line(&mut request_line).is_err() || request_line.is_empty() {
        return;
    }
    drop(reader);

    let parts: Vec<&str> = request_line.split_whitespace().collect();
    if parts.len() < 2 || parts[0] != "GET" {
        let not_found = b"Not Found\n";
        let header = format!(
            "HTTP/1.1 404 Not Found\r\nContent-Type: text/plain; charset=utf-8\r\nContent-Length: {}\r\nConnection: close\r\n\r\n",
            not_found.len()
        );
        let _ = stream.write_all(header.as_bytes());
        let _ = stream.write_all(not_found);
        let _ = stream.flush();
        return;
    }

    let path = parts[1];
    let (code, ctype, body) = route(path, src);
    let clean_path = path.split('?').next().unwrap_or(path);
    let reason = match code {
        200 => "OK",
        404 => "Not Found",
        _ => "OK",
    };
    let extra = if clean_path == "/api/jobs" || clean_path == "/api/plans" {
        "Cache-Control: no-store\r\n"
    } else {
        ""
    };
    let header = format!(
        "HTTP/1.1 {code} {reason}\r\nContent-Type: {ctype}\r\nContent-Length: {}\r\n{extra}Connection: close\r\n\r\n",
        body.len()
    );
    let _ = stream.write_all(header.as_bytes());
    let _ = stream.write_all(&body);
    let _ = stream.flush();
}

pub fn run(args: &crate::cli::DashboardArgs) -> i32 {
    let (sources, where_from) = Sources::from_args_with_where(args);
    let listener = match std::net::TcpListener::bind(format!("127.0.0.1:{}", args.port)) {
        Ok(l) => l,
        Err(e) => {
            eprintln!("error binding 127.0.0.1:{}: {e}", args.port);
            return 1;
        }
    };

    let local_addr = match listener.local_addr() {
        Ok(a) => a,
        Err(e) => {
            eprintln!("error getting local address: {e}");
            return 1;
        }
    };
    let port = local_addr.port();
    let url = format!("http://127.0.0.1:{port}");
    if sources.roots.is_empty() && sources.runs_dirs.is_empty() {
        eprintln!(
            "blyte dashboard: nothing to watch yet: run blyte dispatch in a repository, or list roots in ~/.config/blyte/dashboard.toml"
        );
    } else {
        eprintln!(
            "blyte dashboard: watching {} repositories and {} run directories ({where_from})",
            sources.roots.len(),
            sources.runs_dirs.len()
        );
    }
    println!("blyte dashboard: {url}");
    let _ = std::io::stdout().flush();

    if args.open {
        let cmd = if cfg!(target_os = "macos") {
            "open"
        } else {
            "xdg-open"
        };
        if let Err(e) = std::process::Command::new(cmd).arg(&url).spawn() {
            eprintln!("warning: failed to open browser: {e}");
        }
    }

    let live = args.roots.is_empty() && args.runs_dirs.is_empty();
    let fixed_src = std::sync::Arc::new(sources);
    let args = std::sync::Arc::new(args.clone());
    for stream in listener.incoming() {
        match stream {
            Ok(stream) => {
                let fixed = std::sync::Arc::clone(&fixed_src);
                let args = std::sync::Arc::clone(&args);
                std::thread::spawn(move || {
                    if live {
                        let src = Sources::from_args(&args);
                        handle_connection(stream, &src);
                    } else {
                        handle_connection(stream, &fixed);
                    }
                });
            }
            Err(_) => {
                // Ignore transient errors
            }
        }
    }

    0
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn extract_title_first_markdown_heading() {
        assert_eq!(
            extract_title(
                "From the plan docs/p.md:\n\n### Task G2: an empty gate is skipped\n\nbody"
            ),
            "Task G2: an empty gate is skipped"
        );
    }

    #[test]
    fn extract_title_no_heading_fallback() {
        assert_eq!(extract_title("fix the bug\nmore"), "fix the bug");
    }

    #[test]
    fn extract_title_task_rule_wins() {
        assert_eq!(
            extract_title("# Heading\nTask: \"explicit title\"\nmore"),
            "explicit title"
        );
    }

    #[test]
    fn job_title_strips_task_id_prefix() {
        let text1 = "From the plan docs/p.md:\n\n### Task G2: an empty gate is skipped\n\nbody";
        assert_eq!(job_title(text1, "G2"), "an empty gate is skipped");
        assert_eq!(job_title("Task G3: x", "G2"), "Task G3: x");
        assert_eq!(job_title("Task G2:", "G2"), "Task G2:");
    }

    #[test]
    fn session_tag_from_start_file() {
        let d = tempfile::tempdir().unwrap();
        let wt = d.path().join("repos/repo-a/.wt");
        std::fs::create_dir_all(&wt).unwrap();

        let me = std::process::id();
        std::fs::write(
            wt.join("T1.start.json"),
            format!(r#"{{"id":"T1","pid":{me},"session":"4f83bf80-a445-4090","tags":["x"]}}"#),
        )
        .unwrap();
        std::fs::write(
            wt.join("T2.start.json"),
            format!(r#"{{"id":"T2","pid":{me},"session":"abc"}}"#),
        )
        .unwrap();
        std::fs::write(
            wt.join("T3.start.json"),
            format!(r#"{{"id":"T3","pid":{me},"tags":["x"]}}"#),
        )
        .unwrap();
        std::fs::write(
            wt.join("T4.start.json"),
            format!(r#"{{"id":"T4","pid":{me},"session":"4f83bf80-a445-4090","tags":["session:4f83bf80"]}}"#),
        )
        .unwrap();

        let src = Sources {
            roots: vec![d.path().join("repos")],
            runs_dirs: vec![],
            hours: 24,
        };
        let now = std::time::SystemTime::now()
            .duration_since(std::time::UNIX_EPOCH)
            .unwrap()
            .as_secs();
        let jobs = collect_jobs(&src, now);

        let t1 = jobs.iter().find(|j| j.id == "T1").unwrap();
        assert!(t1.tags.contains(&"x".to_string()));
        assert!(t1.tags.contains(&"session:4f83bf80".to_string()));

        let t2 = jobs.iter().find(|j| j.id == "T2").unwrap();
        assert!(t2.tags.contains(&"session:abc".to_string()));

        let t3 = jobs.iter().find(|j| j.id == "T3").unwrap();
        assert!(!t3.tags.iter().any(|t| t.starts_with("session:")));

        let t4 = jobs.iter().find(|j| j.id == "T4").unwrap();
        assert_eq!(
            t4.tags
                .iter()
                .filter(|t| t.as_str() == "session:4f83bf80")
                .count(),
            1
        );
    }
}