beady-eye 0.3.0

A tree of work in flight: bead graphs annotated with the live agents working them
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
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
//! Which pane is working which bead, read from the two directions that can
//! disagree about it.

use std::collections::{BTreeMap, BTreeSet, HashSet};
use std::path::Path;

use serde::Serialize;

use crate::config::{Config, Project};
use crate::model::types::Bead;
use crate::model::types::{Pane, PaneKey, PaneStatus};

#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Serialize)]
#[serde(rename_all = "snake_case")]
pub enum JoinSource {
    /// The bead named the pane. Exact.
    AgentPane,
    /// The pane named the bead. Inferred from free text.
    DisplayAgent,
}

/// A bead, across every tracker `bdi` reads. Prefixes are per-tracker and
/// uncoordinated, so an id on its own does not name a bead.
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Serialize)]
pub struct BeadKey {
    pub project: String,
    pub id: String,
}

#[derive(Debug, Clone, PartialEq, Eq, Serialize)]
pub struct AgentRef {
    pub pane: PaneKey,
    pub pane_status: PaneStatus,
    pub title: Option<String>,
    pub source: JoinSource,
}

/// A disagreement between the two directions of the join. Each is reported
/// rather than resolved: picking a winner would hide exactly the drift `bdi`
/// exists to surface.
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Serialize)]
#[serde(tag = "conflict", rename_all = "kebab-case")]
pub enum Conflict {
    /// The bead's own key and a pane's `display_agent` name different panes.
    BeadAndPaneDisagree {
        bead: BeadKey,
        named_by_bead: PaneKey,
        named_by_pane: PaneKey,
    },
    /// Several panes name one bead. None of them wins it.
    SeveralPanesNameOneBead { bead: BeadKey, panes: Vec<PaneKey> },
    /// Several beads name one pane. None of them gets it.
    ///
    /// `caption` is the pane's own account of what it is working on, carried
    /// here because it is the one thing that tells a live claim from a stale
    /// one and it is nowhere else on the screen: a caption is drawn off the
    /// agent a pane was awarded, and a contested pane is awarded to nobody.
    SeveralBeadsNameOnePane {
        pane: PaneKey,
        caption: Option<String>,
        beads: Vec<BeadKey>,
    },
    /// A pane and the bead naming it, or named by it, sit in different
    /// projects. No join. `pane_project` is absent when the pane's `cwd` is
    /// under no configured project at all.
    PaneInAnotherProject {
        bead: BeadKey,
        pane: PaneKey,
        pane_project: Option<String>,
    },
    /// The bead's own key names a pane id that several sessions each hold a
    /// pane under. A seat writes the id alone, so the key cannot say which,
    /// and the bead gets none of them: picking would draw the wrong seat on
    /// the bead as surely as the right one.
    PaneIdInSeveralSessions {
        bead: BeadKey,
        pane_id: String,
        sessions: Vec<String>,
    },
}

impl Conflict {
    /// The panes this disagreement left holding nothing.
    ///
    /// A candidate rather than a verdict: `BeadAndPaneDisagree` is settled in
    /// the bead's favour, and the panes of `SeveralPanesNameOneBead` may each
    /// hold some other bead by the exact direction. So a caller wanting the
    /// panes that came away empty takes this against the ones the join
    /// awarded — which is what lets the set be generous without a row saying
    /// of a working pane that its claim was refused.
    pub fn refused_panes(&self) -> Vec<PaneKey> {
        match self {
            Conflict::BeadAndPaneDisagree { named_by_pane, .. } => vec![named_by_pane.clone()],
            Conflict::SeveralPanesNameOneBead { panes, .. } => panes.clone(),
            Conflict::SeveralBeadsNameOnePane { pane, .. } => vec![pane.clone()],
            Conflict::PaneInAnotherProject { pane, .. } => vec![pane.clone()],
            Conflict::PaneIdInSeveralSessions {
                pane_id, sessions, ..
            } => sessions
                .iter()
                .map(|session| PaneKey {
                    session: session.clone(),
                    id: pane_id.clone(),
                })
                .collect(),
        }
    }
}

/// One project's assembled rows, as the join reads them.
pub struct ProjectRows<'a> {
    pub project: &'a str,
    pub rows: &'a [Bead],
}

/// What one run holds about panes: every pane a session answered with, and
/// the ids of the panes it knows it is missing.
///
/// The second is not the first's complement, and nothing could enumerate
/// that: a pane id nothing answered for is usually a pane that has gone.
/// It is what a session that has since gone quiet was holding when it last
/// answered, which is the only thing that can place a pane id inside a
/// session this run cannot ask — a seat writes the id alone, and an id
/// names a pane only within its session.
///
/// So a rule that reads a pane's absence can be evaluated over the first and
/// not over the second, and the two are handed over together because a run
/// that has one without the other cannot tell which it is holding.
pub struct Listed<'a> {
    pub panes: &'a [Pane],
    pub out_of_reach: &'a BTreeSet<String>,
}

/// A run every session of which answered, so there is no pane it is missing.
#[cfg(feature = "testing")]
static NOTHING_MISSED: BTreeSet<String> = BTreeSet::new();

/// Behind the feature rather than `cfg(test)` because the tests under
/// `tests/` resolve a join too, and they link the library.
#[cfg(feature = "testing")]
impl<'a> Listed<'a> {
    /// Every pane there is, from a run that asked each session and was
    /// answered by all of them.
    ///
    /// A test's shorthand and no run's: a collection always knows which of
    /// its sessions went quiet, so nothing in production has this to say.
    pub fn all(panes: &'a [Pane]) -> Self {
        Self {
            panes,
            out_of_reach: &NOTHING_MISSED,
        }
    }
}

/// The agents the join could account for, and every disagreement it could not.
#[derive(Debug, Clone, Default, PartialEq, Eq)]
pub struct Joined {
    pub agents: BTreeMap<BeadKey, AgentRef>,
    /// Beads whose own key named a live pane the join would not award them,
    /// and the disagreement that refused it. Only the exact direction is here:
    /// a bead that named nothing has no claim to refuse, and a pane naming an
    /// id another tracker happens to reuse says nothing about that tracker's
    /// bead.
    pub refused: BTreeMap<BeadKey, Conflict>,
    /// Beads whose own key named a pane that is out of this run's reach, so
    /// nothing it holds says whether the seat behind the claim is alive. A
    /// rule reading that pane's absence has no absence to read.
    pub out_of_reach: BTreeSet<BeadKey>,
    pub conflicts: Vec<Conflict>,
}

/// The project a pane sits in: the one holding its directory, or, where none
/// does, the one holding the same place in the main working tree of the
/// repository the directory is in. `None` when no project's does.
///
/// The directory comes first because a config may name a project by its
/// place in a linked worktree. The main working tree is asked second, for a
/// pane in a linked worktree of a project this run never asked where it is
/// worked — one the scope left out — which is held by nothing as it stands.
pub fn project_of<'a>(pane: &Pane, projects: &'a [Project]) -> Option<&'a Project> {
    project_holding(&pane.cwd, projects)
        .or_else(|| project_holding(pane.cwd_in_the_main_working_tree()?, projects))
}

/// The project a path sits in: the one whose deepest working tree contains
/// it. `None` when no project's does.
fn project_holding<'a>(path: &Path, projects: &'a [Project]) -> Option<&'a Project> {
    projects
        .iter()
        .filter_map(|p| Some((p.holds(path)?, p)))
        .max_by_key(|(depth, _)| *depth)
        .map(|(_, project)| project)
}

/// Join live panes onto beads, scoped to each pane's own project.
///
/// Bidirectional, because each direction alone has a hole: a bead's configured
/// key names its pane exactly, and a pane's `display_agent` names its bead by
/// inference. A bead takes its agent from the exact direction when that
/// direction resolved uncontested, otherwise from the inferred direction when
/// exactly one pane names it, otherwise none — and wherever the two directions
/// name different live panes, the disagreement is reported however it went.
///
/// The exact direction names a pane by id alone, which is what a seat has to
/// write, and an id names a pane only within a session. So it is matched
/// across every session: held by one, that is the pane; held by several,
/// the claim is refused and the disagreement reported, because nothing the
/// bead wrote says which.
pub fn resolve(trees: &[ProjectRows<'_>], listed: Listed<'_>, cfg: &Config) -> Joined {
    let Listed {
        panes,
        out_of_reach: panes_out_of_reach,
    } = listed;
    let mut live_under: BTreeMap<&str, Vec<&Pane>> = BTreeMap::new();
    for pane in panes {
        live_under
            .entry(pane.pane_id.as_str())
            .or_default()
            .push(pane);
    }
    let live: BTreeMap<PaneKey, &Pane> = panes.iter().map(|p| (p.key(), p)).collect();

    // Every pane is placed in a project before any bead is looked at, so a
    // colliding id in another tracker never reaches the join at all. Placed
    // against every configured project, read or not: a pane in a project
    // this run left out is in that project, not in a directory nobody
    // configured.
    let pane_project: BTreeMap<PaneKey, Option<&str>> = panes
        .iter()
        .map(|p| {
            (
                p.key(),
                project_of(p, &cfg.projects).map(|q| q.name.as_str()),
            )
        })
        .collect();

    // Which projects hold a bead of each id, so a pane naming one outside its
    // own project is told apart from a pane naming nothing at all.
    let mut projects_holding: BTreeMap<&str, BTreeSet<&str>> = BTreeMap::new();
    for tree in trees {
        for row in tree.rows {
            projects_holding
                .entry(row.id.as_str())
                .or_default()
                .insert(tree.project);
        }
    }

    let mut conflicts: Vec<Conflict> = Vec::new();
    let mut refused: BTreeMap<BeadKey, Conflict> = BTreeMap::new();
    let mut out_of_reach: BTreeSet<BeadKey> = BTreeSet::new();

    // The exact direction: the bead names its pane.
    let mut claims: BTreeMap<PaneKey, BTreeSet<BeadKey>> = BTreeMap::new();
    let mut claimed_pane: BTreeMap<BeadKey, PaneKey> = BTreeMap::new();
    for tree in trees {
        for row in tree.rows {
            let Some(named) = row.metadata.get(&cfg.join.pane_key) else {
                continue;
            };
            let bead = BeadKey {
                project: tree.project.to_string(),
                id: row.id.clone(),
            };
            // An id this run is missing is asked about before the live panes
            // are, because a live pane carrying it need not be the one the
            // bead named: a session mints its ids from `w1` up, so the same
            // id in two sessions is two panes. That is the ambiguity
            // `PaneIdInSeveralSessions` awards to nobody where both panes
            // answered, and here one of them cannot be asked at all — so it
            // is not reported as a disagreement, and the claim resolves to
            // nothing whether or not something live carries its id.
            if panes_out_of_reach.contains(named.as_str()) {
                out_of_reach.insert(bead);
                continue;
            }
            // A named pane that is not live resolves to nothing; that absence
            // is `orphan-claim`'s to report, not a disagreement.
            let Some(holding) = live_under.get(named.as_str()) else {
                continue;
            };
            let pane = match holding.as_slice() {
                [pane] => *pane,
                several => {
                    let ambiguous = Conflict::PaneIdInSeveralSessions {
                        bead: bead.clone(),
                        pane_id: named.clone(),
                        sessions: several.iter().map(|p| p.session.clone()).collect(),
                    };
                    conflicts.push(ambiguous.clone());
                    refused.insert(bead, ambiguous);
                    continue;
                }
            };
            let key = pane.key();
            let holds = pane_project[&key];
            if holds != Some(tree.project) {
                let elsewhere = Conflict::PaneInAnotherProject {
                    bead: bead.clone(),
                    pane: key,
                    pane_project: holds.map(str::to_string),
                };
                conflicts.push(elsewhere.clone());
                refused.insert(bead, elsewhere);
                continue;
            }
            claimed_pane.insert(bead.clone(), key.clone());
            claims.entry(key).or_default().insert(bead);
        }
    }

    let mut agents: BTreeMap<BeadKey, AgentRef> = BTreeMap::new();
    for (key, beads) in claims {
        let pane = live[&key];
        let mut named: Vec<BeadKey> = beads.into_iter().collect();
        if named.len() == 1 {
            agents.insert(named.remove(0), agent_ref(pane, JoinSource::AgentPane));
        } else {
            let shared = Conflict::SeveralBeadsNameOnePane {
                pane: key,
                caption: pane.caption().map(str::to_string),
                beads: named.clone(),
            };
            conflicts.push(shared.clone());
            for bead in named {
                refused.insert(bead, shared.clone());
            }
        }
    }

    // The inferred direction: the pane names its bead.
    let mut named_by: BTreeMap<BeadKey, BTreeSet<PaneKey>> = BTreeMap::new();
    for pane in panes {
        let Some(id) = &pane.display_agent else {
            continue;
        };
        let Some(project) = pane_project[&pane.key()] else {
            continue;
        };
        // A pane in a project this run left out is on that project's work,
        // whose tracker was never read: an id it names says nothing about
        // a read project's bead of the same id.
        if !cfg.reads(project) {
            continue;
        }
        // Most `display_agent` values are free text rather than a bead id.
        let Some(holders) = projects_holding.get(id.as_str()) else {
            continue;
        };
        if holders.contains(project) {
            named_by
                .entry(BeadKey {
                    project: project.to_string(),
                    id: id.clone(),
                })
                .or_default()
                .insert(pane.key());
        } else {
            for holder in holders {
                conflicts.push(Conflict::PaneInAnotherProject {
                    bead: BeadKey {
                        project: holder.to_string(),
                        id: id.clone(),
                    },
                    pane: pane.key(),
                    pane_project: Some(project.to_string()),
                });
            }
        }
    }

    for (bead, from_panes) in named_by {
        let mut inferred: Vec<PaneKey> = from_panes.into_iter().collect();

        if inferred.len() > 1 {
            conflicts.push(Conflict::SeveralPanesNameOneBead {
                bead,
                panes: inferred,
            });
            continue;
        }

        let only = inferred.remove(0);
        // The disagreement stands on what the bead's key named, whether or not
        // that claim survived to be awarded.
        if let Some(claimed) = claimed_pane.get(&bead).filter(|p| **p != only) {
            conflicts.push(Conflict::BeadAndPaneDisagree {
                bead: bead.clone(),
                named_by_bead: claimed.clone(),
                named_by_pane: only.clone(),
            });
        }
        // The exact direction wins where it resolved uncontested; the inferred
        // one only fills the gap it left.
        agents
            .entry(bead)
            .or_insert_with(|| agent_ref(live[&only], JoinSource::DisplayAgent));
    }

    conflicts.sort();
    conflicts.dedup();

    Joined {
        agents,
        refused,
        out_of_reach,
        conflicts,
    }
}

fn agent_ref(pane: &Pane, source: JoinSource) -> AgentRef {
    AgentRef {
        pane: pane.key(),
        pane_status: pane.agent_status.clone(),
        title: pane.caption().map(str::to_string),
        source,
    }
}

/// Live panes that resolved to no bead in any tree.
pub fn unattributed<'a>(panes: &'a [Pane], joined: &Joined) -> Vec<&'a Pane> {
    let taken: HashSet<&PaneKey> = joined.agents.values().map(|a| &a.pane).collect();
    panes.iter().filter(|p| !taken.contains(&p.key())).collect()
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::collect::bd::parse_beads;
    use crate::collect::herdr::parse_agent_list;
    use crate::config::Join;
    use crate::model::tree::Nesting;
    use crate::model::types::testing::{key as pane_key, A_SESSION};
    use crate::model::types::Bead;
    use pretty_assertions::assert_eq;
    use std::path::PathBuf;

    const BEADS: &str = include_str!("../../tests/fixtures/display_agent_bd_list.json");
    const PANES: &str = include_str!("../../tests/fixtures/herdr_agent_list.json");

    /// A tracker and a herdr session captured from the same live moment, so
    /// the pane a bead names is a pane the list reports. The pair the other
    /// fixtures cannot make: nothing hand-written can show that the ids on
    /// both sides agree, because whoever writes it makes them agree.
    const JOINED_BEADS: &str = include_str!("../../tests/fixtures/joined_bd_list.json");
    const JOINED_PANES: &str = include_str!("../../tests/fixtures/joined_herdr_agent_list.json");

    const FIXTURE_PROJECT_PATH: &str = "/tmp/bdi-ground/orbital";

    fn project(name: &str, path: &str) -> Project {
        Project {
            name: name.to_string(),
            path: path.into(),
            environment_command: None,
            credential_command: None,
            poll: true,
            worktrees: Vec::new(),
        }
    }

    fn project_working_in(name: &str, path: &str, worktrees: &[&str]) -> Project {
        Project {
            worktrees: worktrees.iter().map(Into::into).collect(),
            ..project(name, path)
        }
    }

    /// The root of a hand-written tree: the one row that depends on nothing.
    fn root_row(beads: &[Bead]) -> String {
        beads
            .iter()
            .find(|b| b.dependencies.is_empty())
            .expect("a root row")
            .id
            .clone()
    }

    fn rows(json: &str) -> Vec<Bead> {
        let beads = parse_beads(json).expect("the rows parse");
        let root = root_row(&beads);
        Nesting::of(&beads)
            .assemble(&root)
            .expect("the rows assemble")
            .beads
    }

    /// The bodies of `herdr agent list`'s `agents` array, wrapped in its
    /// envelope so the tests exercise the real parser.
    fn panes(agents: &str) -> Vec<Pane> {
        parse_agent_list(
            A_SESSION,
            &format!(r#"{{"id":"cli:agent:list","result":{{"agents":[{agents}]}}}}"#),
        )
        .expect("the panes parse")
    }

    /// The same bodies, listed by `session` rather than by the one a test's
    /// panes are in unless it says otherwise.
    fn panes_in(session: &str, agents: &str) -> Vec<Pane> {
        parse_agent_list(
            session,
            &format!(r#"{{"id":"cli:agent:list","result":{{"agents":[{agents}]}}}}"#),
        )
        .expect("the panes parse")
    }

    fn key(project: &str, id: &str) -> BeadKey {
        BeadKey {
            project: project.to_string(),
            id: id.to_string(),
        }
    }

    /// The three disagreements whose panes are the ones they already carry.
    #[test]
    fn a_disagreement_names_the_panes_it_left_holding_nothing() {
        assert_eq!(
            Conflict::SeveralBeadsNameOnePane {
                pane: pane_key("w:p1"),
                caption: None,
                beads: vec![key("proj", "p-1"), key("proj", "p-2")],
            }
            .refused_panes(),
            vec![pane_key("w:p1")]
        );
        assert_eq!(
            Conflict::PaneInAnotherProject {
                bead: key("proj", "p-1"),
                pane: pane_key("w:p2"),
                pane_project: Some("ferry".to_string()),
            }
            .refused_panes(),
            vec![pane_key("w:p2")]
        );
        assert_eq!(
            Conflict::SeveralPanesNameOneBead {
                bead: key("proj", "p-1"),
                panes: vec![pane_key("w:p3"), pane_key("w:p4")],
            }
            .refused_panes(),
            vec![pane_key("w:p3"), pane_key("w:p4")]
        );
    }

    /// The one disagreement the join settles rather than leaves open: the
    /// bead's own key wins, so the pane it named holds the bead and only the
    /// pane that named the bead back comes away empty.
    #[test]
    fn the_pane_a_bead_named_keeps_its_bead_when_another_pane_names_it_too() {
        assert_eq!(
            Conflict::BeadAndPaneDisagree {
                bead: key("proj", "p-1"),
                named_by_bead: pane_key("w:p1"),
                named_by_pane: pane_key("w:p2"),
            }
            .refused_panes(),
            vec![pane_key("w:p2")]
        );
    }

    /// A pane id alone names a pane in every session that holds one, and the
    /// claim was refused at each of them.
    #[test]
    fn an_id_several_sessions_hold_names_a_refused_pane_in_each_of_them() {
        assert_eq!(
            Conflict::PaneIdInSeveralSessions {
                bead: key("proj", "p-1"),
                pane_id: "w:p1".to_string(),
                sessions: vec![A_SESSION.to_string(), "beacon".to_string()],
            }
            .refused_panes(),
            vec![
                pane_key("w:p1"),
                PaneKey {
                    session: "beacon".to_string(),
                    id: "w:p1".to_string(),
                },
            ]
        );
    }

    /// One bead naming `w:p1`, in a tracker at `/srv/proj`.
    const A_BEAD_NAMING_W_P1: &str = r#"[
      {"id":"p-1","title":"one","status":"in_progress",
       "metadata":{"agent_pane":"w:p1"}}
    ]"#;

    /// A pane id is minted per session, and a bead names one by id alone. Two
    /// sessions each holding that id is a claim nothing the bead wrote can
    /// settle, so nobody is awarded and the disagreement names the sessions.
    #[test]
    fn a_pane_id_held_by_two_sessions_is_refused_and_names_the_sessions() {
        let beads = rows(A_BEAD_NAMING_W_P1);
        let mut live = panes(r#"{"pane_id":"w:p1","cwd":"/srv/proj","agent_status":"idle"}"#);
        live.extend(panes_in(
            "beacon",
            r#"{"pane_id":"w:p1","cwd":"/srv/proj","agent_status":"working"}"#,
        ));
        let cfg = vec![project("proj", "/srv/proj")];

        let joined = resolve(
            &[ProjectRows {
                project: "proj",
                rows: &beads,
            }],
            Listed::all(&live),
            &Config::naming(cfg),
        );

        let ambiguous = Conflict::PaneIdInSeveralSessions {
            bead: key("proj", "p-1"),
            pane_id: "w:p1".to_string(),
            sessions: vec![A_SESSION.to_string(), "beacon".to_string()],
        };
        assert_eq!(joined.agents, BTreeMap::new());
        assert_eq!(joined.conflicts, vec![ambiguous.clone()]);
        assert_eq!(
            joined.refused,
            BTreeMap::from([(key("proj", "p-1"), ambiguous)])
        );
        assert_eq!(
            unattributed(&live, &joined).len(),
            2,
            "both panes are still live, and neither is anybody's"
        );
    }

    /// The refusal is of the bead's own key, and only of that. A pane that
    /// names the bead itself has said which of the two it is, so the
    /// inferred direction fills the gap the refused claim left — as it does
    /// after every other refusal — and the disagreement is still reported,
    /// because the key the seat wrote still names two panes.
    #[test]
    fn a_pane_naming_the_bead_resolves_it_where_its_id_alone_could_not() {
        let beads = rows(A_BEAD_NAMING_W_P1);
        let mut live = panes(r#"{"pane_id":"w:p1","cwd":"/srv/proj","agent_status":"idle"}"#);
        live.extend(panes_in(
            "beacon",
            r#"{"pane_id":"w:p1","cwd":"/srv/proj","agent_status":"working","display_agent":"p-1"}"#,
        ));
        let cfg = vec![project("proj", "/srv/proj")];

        let joined = resolve(
            &[ProjectRows {
                project: "proj",
                rows: &beads,
            }],
            Listed::all(&live),
            &Config::naming(cfg),
        );

        let a = pane_of(&joined, "proj", "p-1");
        assert_eq!(
            a.pane,
            PaneKey {
                session: "beacon".to_string(),
                id: "w:p1".to_string(),
            }
        );
        assert_eq!(a.source, JoinSource::DisplayAgent);
        assert_eq!(
            joined.conflicts.len(),
            1,
            "the key's ambiguity still stands"
        );
        assert!(matches!(
            joined.conflicts[0],
            Conflict::PaneIdInSeveralSessions { .. }
        ));
    }

    fn pane_of<'a>(joined: &'a Joined, project: &str, id: &str) -> &'a AgentRef {
        joined
            .agents
            .get(&key(project, id))
            .unwrap_or_else(|| panic!("{project}/{id} resolved to a pane"))
    }

    fn loose(panes: &[Pane], joined: &Joined) -> Vec<String> {
        let mut ids: Vec<String> = unattributed(panes, joined)
            .into_iter()
            .map(|p| p.pane_id.clone())
            .collect();
        ids.sort();
        ids
    }

    // ---- the two directions -------------------------------------------

    /// The whole point of the tool, against the shapes a real tracker and a
    /// real herdr session actually emit rather than the ones a test writes.
    #[test]
    fn a_captured_bead_takes_the_captured_pane_it_names() {
        let beads = rows(JOINED_BEADS);
        let live = parse_agent_list(A_SESSION, JOINED_PANES).expect("the fixture parses");
        let cfg = vec![project("orbital", FIXTURE_PROJECT_PATH)];

        let joined = resolve(
            &[ProjectRows {
                project: "orbital",
                rows: &beads,
            }],
            Listed::all(&live),
            &Config::naming(cfg),
        );

        let a = pane_of(&joined, "orbital", "orb-9fw.22");
        assert_eq!(a.pane, pane_key("wM:pK"));
        assert_eq!(a.source, JoinSource::AgentPane);
        assert_eq!(joined.conflicts, vec![]);
        assert_eq!(joined.refused, BTreeMap::new());
    }

    #[test]
    fn the_bead_naming_its_pane_resolves_exactly() {
        let beads = rows(
            r#"[
              {"id":"p-1","title":"root","status":"open"},
              {"id":"p-1.1","title":"work","status":"in_progress",
               "dependencies":[{"depends_on_id":"p-1","type":"parent-child"}],
               "metadata":{"agent_pane":"w:p1"}}
            ]"#,
        );
        let live = panes(
            r#"{"pane_id":"w:p1","cwd":"/home/user/proj","agent_status":"working",
                "title":"doing the work"}"#,
        );
        let cfg = vec![project("proj", "/home/user/proj")];

        let joined = resolve(
            &[ProjectRows {
                project: "proj",
                rows: &beads,
            }],
            Listed::all(&live),
            &Config::naming(cfg),
        );

        let a = pane_of(&joined, "proj", "p-1.1");
        assert_eq!(a.pane, pane_key("w:p1"));
        assert_eq!(a.source, JoinSource::AgentPane);
        assert_eq!(a.pane_status, PaneStatus::Working);
        assert_eq!(a.title.as_deref(), Some("doing the work"));
        assert_eq!(joined.conflicts, vec![]);
    }

    /// The line an agent gets is the pane's caption — its label for the state
    /// it is actually in — rather than its title.
    #[test]
    fn an_agent_carries_the_panes_caption() {
        let beads = rows(
            r#"[{"id":"p-1","title":"root","status":"in_progress",
                 "metadata":{"agent_pane":"w:p1"}}]"#,
        );
        let live = panes(
            r#"{"pane_id":"w:p1","cwd":"/home/user/proj","agent_status":"working",
                "title":"the title",
                "state_labels":{"idle":"the idle line","working":"the working line"}}"#,
        );
        let cfg = vec![project("proj", "/home/user/proj")];

        let joined = resolve(
            &[ProjectRows {
                project: "proj",
                rows: &beads,
            }],
            Listed::all(&live),
            &Config::naming(cfg),
        );

        assert_eq!(
            pane_of(&joined, "proj", "p-1").title.as_deref(),
            Some("the working line")
        );
    }

    /// The key is configurable, so a hardcoded `agent_pane` cannot pass.
    #[test]
    fn the_exact_direction_reads_the_configured_key() {
        let beads = rows(
            r#"[
              {"id":"p-1","title":"root","status":"open",
               "metadata":{"herdr_pane":"w:p1","agent_pane":"w:p2"}}
            ]"#,
        );
        let live = panes(
            r#"{"pane_id":"w:p1","cwd":"/home/user/proj","agent_status":"idle"},
               {"pane_id":"w:p2","cwd":"/home/user/proj","agent_status":"idle"}"#,
        );
        let cfg = vec![project("proj", "/home/user/proj")];
        let join = Join {
            pane_key: "herdr_pane".to_string(),
        };

        let joined = resolve(
            &[ProjectRows {
                project: "proj",
                rows: &beads,
            }],
            Listed::all(&live),
            &Config {
                join,
                ..Config::naming(cfg)
            },
        );

        assert_eq!(pane_of(&joined, "proj", "p-1").pane, pane_key("w:p1"));
    }

    #[test]
    fn a_pane_naming_its_bead_resolves_as_inferred() {
        let beads = rows(BEADS);
        let live = parse_agent_list(A_SESSION, PANES).expect("the fixture parses");
        let cfg = vec![project("orbital", FIXTURE_PROJECT_PATH)];

        let joined = resolve(
            &[ProjectRows {
                project: "orbital",
                rows: &beads,
            }],
            Listed::all(&live),
            &Config::naming(cfg),
        );

        // wG:p5 carries display_agent=orb-2kd.3; the one bead in the tracker
        // that names a pane names one this capture does not hold, so the
        // inferred direction is the only one that fires.
        let a = pane_of(&joined, "orbital", "orb-2kd.3");
        assert_eq!(a.pane, pane_key("wG:p5"));
        assert_eq!(a.source, JoinSource::DisplayAgent);
        assert_eq!(joined.agents.len(), 1);
        assert_eq!(joined.conflicts, vec![]);
    }

    /// The pane exists and its bead does not — that absence is `orphan-claim`'s
    /// to report, not a conflict.
    #[test]
    fn a_bead_naming_a_dead_pane_resolves_to_nothing() {
        let beads = rows(
            r#"[{"id":"p-1","title":"root","status":"in_progress",
                 "metadata":{"agent_pane":"w:pGONE"}}]"#,
        );
        let live = panes(r#"{"pane_id":"w:p1","cwd":"/home/user/proj","agent_status":"idle"}"#);
        let cfg = vec![project("proj", "/home/user/proj")];

        let joined = resolve(
            &[ProjectRows {
                project: "proj",
                rows: &beads,
            }],
            Listed::all(&live),
            &Config::naming(cfg),
        );

        assert_eq!(joined.agents, BTreeMap::new());
        assert_eq!(joined.conflicts, vec![]);
    }

    /// The same absence, over a run that is missing the pane rather than
    /// holding no such pane. It resolves to nothing either way — a pane this
    /// run cannot see is not a pane it can award — and the difference is
    /// carried out for the rule that would otherwise read the absence.
    ///
    /// Both beads are asked at once, because a set that took every claim
    /// that resolved to nothing would say the same thing and mean nothing.
    #[test]
    fn a_bead_naming_a_pane_this_run_is_missing_is_told_from_one_naming_a_dead_pane() {
        let beads = rows(
            r#"[{"id":"p-1","title":"the seat that went quiet","status":"in_progress",
                 "metadata":{"agent_pane":"w:pQUIET"}},
                {"id":"p-2","title":"the seat that died","status":"in_progress",
                 "metadata":{"agent_pane":"w:pGONE"}}]"#,
        );
        let live = panes(r#"{"pane_id":"w:p1","cwd":"/home/user/proj","agent_status":"idle"}"#);
        let cfg = vec![project("proj", "/home/user/proj")];
        let missing = BTreeSet::from(["w:pQUIET".to_string()]);

        let joined = resolve(
            &[ProjectRows {
                project: "proj",
                rows: &beads,
            }],
            Listed {
                panes: &live,
                out_of_reach: &missing,
            },
            &Config::naming(cfg),
        );

        assert_eq!(joined.agents, BTreeMap::new(), "neither pane is live");
        assert_eq!(
            joined.out_of_reach,
            BTreeSet::from([key("proj", "p-1")]),
            "only the claim naming the pane this run is missing"
        );
        assert_eq!(joined.conflicts, vec![]);
    }

    /// A pane id names a pane only within its session, so a live pane
    /// carrying the id a bead named is not necessarily that bead's — the
    /// session that has gone quiet was holding one of the same name. The
    /// join awards neither, exactly as it awards neither when two sessions
    /// that both answered hold the id; it is not reported as a disagreement,
    /// because this run cannot establish that there are two panes, only that
    /// it cannot rule it out.
    #[test]
    fn a_live_pane_carrying_an_id_a_quiet_session_also_held_is_awarded_to_nobody() {
        let beads = rows(
            r#"[{"id":"p-1","title":"root","status":"in_progress",
                 "metadata":{"agent_pane":"w:p1"}}]"#,
        );
        let live = panes(r#"{"pane_id":"w:p1","cwd":"/home/user/proj","agent_status":"working"}"#);
        let cfg = vec![project("proj", "/home/user/proj")];
        let missing = BTreeSet::from(["w:p1".to_string()]);

        let joined = resolve(
            &[ProjectRows {
                project: "proj",
                rows: &beads,
            }],
            Listed {
                panes: &live,
                out_of_reach: &missing,
            },
            &Config::naming(cfg),
        );

        assert_eq!(
            joined.agents,
            BTreeMap::new(),
            "the live pane of that id may be the other session's"
        );
        assert_eq!(joined.out_of_reach, BTreeSet::from([key("proj", "p-1")]));
        assert_eq!(joined.conflicts, vec![]);
    }

    /// `display_agent` is free text. Most of it is not a bead id.
    #[test]
    fn a_pane_whose_display_agent_names_no_bead_joins_nothing() {
        let beads = rows(r#"[{"id":"p-1","title":"root","status":"open"}]"#);
        let live = panes(
            r#"{"pane_id":"w:p1","cwd":"/home/user/proj","agent_status":"working",
                "display_agent":"orch: some-effort"}"#,
        );
        let cfg = vec![project("proj", "/home/user/proj")];

        let joined = resolve(
            &[ProjectRows {
                project: "proj",
                rows: &beads,
            }],
            Listed::all(&live),
            &Config::naming(cfg),
        );

        assert_eq!(joined.agents, BTreeMap::new());
        assert_eq!(joined.conflicts, vec![]);
        assert_eq!(loose(&live, &joined), vec!["w:p1"]);
    }

    // ---- project scope -------------------------------------------------

    #[test]
    fn the_longest_configured_path_containing_a_pane_wins() {
        let cfg = vec![
            project("outer", "/home/user/dev"),
            project("inner", "/home/user/dev/inner"),
        ];

        assert_eq!(
            project_holding(Path::new("/home/user/dev/inner/src"), &cfg).map(|p| p.name.as_str()),
            Some("inner")
        );
        assert_eq!(
            project_holding(Path::new("/home/user/dev/other"), &cfg).map(|p| p.name.as_str()),
            Some("outer")
        );
        assert_eq!(project_holding(Path::new("/home/user"), &cfg), None);
    }

    /// One worktree per seat puts the panes in sibling worktrees, under
    /// neither each other nor the checkout bdi was run from.
    #[test]
    fn a_pane_in_another_worktree_of_the_repository_is_in_the_project() {
        let cfg = vec![project_working_in(
            "proj",
            "/home/user/proj",
            &["/home/user/proj", "/tmp/seat-a/wt"],
        )];

        assert_eq!(
            project_holding(Path::new("/tmp/seat-a/wt/src"), &cfg).map(|p| p.name.as_str()),
            Some("proj")
        );
        assert_eq!(project_holding(Path::new("/tmp/seat-b/wt"), &cfg), None);
    }

    /// A worktree is territory like any other, so the deepest directory
    /// containing the pane still wins the paths two projects share.
    #[test]
    fn a_worktree_deeper_than_another_projects_path_wins_the_pane() {
        let cfg = vec![
            project("outer", "/home/user/dev"),
            project_working_in("inner", "/srv/inner", &["/home/user/dev/wt"]),
        ];

        assert_eq!(
            project_holding(Path::new("/home/user/dev/wt/src"), &cfg).map(|p| p.name.as_str()),
            Some("inner")
        );
    }

    /// A pane in a linked worktree of a project that was never asked where
    /// it is worked — one the scope left out — is held by nothing as it
    /// stands, and is placed by where its directory sits in the main working
    /// tree instead.
    ///
    /// Only a pane built by hand can say so. Every pane read off the wire,
    /// which is every fixture under `tests/fixtures/`, has no main-tree
    /// place and is placed by its `cwd` alone, so a green run of those says
    /// nothing about this rule.
    #[test]
    fn a_pane_nothing_holds_is_placed_by_where_it_sits_in_the_main_working_tree() {
        let cfg = vec![project("proj", "/home/user/proj")];
        let mut live =
            panes(r#"{"pane_id":"w:p1","cwd":"/tmp/seat-a/wt/src","agent_status":"working"}"#);
        let pane = live
            .remove(0)
            .with_cwd_in_the_main_working_tree(Some(PathBuf::from("/home/user/proj/src")));

        assert_eq!(
            project_of(&pane, &cfg).map(|p| p.name.as_str()),
            Some("proj")
        );
    }

    /// The directory the pane is in wins over the one it corresponds to: a
    /// project configured at its place in a linked worktree holds a pane
    /// there, whatever the main working tree is under.
    #[test]
    fn a_pane_something_holds_is_placed_by_its_own_directory_first() {
        let cfg = vec![
            project("seat", "/tmp/seat-a/wt"),
            project("main", "/home/user/proj"),
        ];
        let mut live =
            panes(r#"{"pane_id":"w:p1","cwd":"/tmp/seat-a/wt/src","agent_status":"working"}"#);
        let pane = live
            .remove(0)
            .with_cwd_in_the_main_working_tree(Some(PathBuf::from("/home/user/proj/src")));

        assert_eq!(
            project_of(&pane, &cfg).map(|p| p.name.as_str()),
            Some("seat")
        );
    }

    #[test]
    fn a_pane_read_off_the_wire_is_placed_by_its_directory_alone() {
        let cfg = vec![project("proj", "/home/user/proj")];
        let live =
            panes(r#"{"pane_id":"w:p1","cwd":"/tmp/seat-a/wt/src","agent_status":"working"}"#);

        assert_eq!(live[0].cwd_in_the_main_working_tree(), None);
        assert_eq!(project_of(&live[0], &cfg), None);
    }

    /// The conflict a pane raises names the project it sits in, so the
    /// placement reaches it through the same rule.
    #[test]
    fn a_pane_placed_by_the_main_working_tree_is_reported_in_that_project() {
        let two = rows(
            r#"[{"id":"x-1","title":"in project two","status":"in_progress",
                 "metadata":{"agent_pane":"w:p1"}}]"#,
        );
        let mut live =
            panes(r#"{"pane_id":"w:p1","cwd":"/tmp/seat-a/wt/src","agent_status":"working"}"#);
        let pane = live
            .remove(0)
            .with_cwd_in_the_main_working_tree(Some(PathBuf::from("/home/user/one/src")));
        let cfg = vec![
            project("one", "/home/user/one"),
            project("two", "/home/user/two"),
        ];

        let joined = resolve(
            &[ProjectRows {
                project: "two",
                rows: &two,
            }],
            Listed::all(&[pane]),
            &Config::naming(cfg),
        );

        assert_eq!(
            joined.conflicts,
            vec![Conflict::PaneInAnotherProject {
                bead: key("two", "x-1"),
                pane: pane_key("w:p1"),
                pane_project: Some("one".to_string()),
            }]
        );
    }

    /// A sibling directory sharing a textual prefix is a different project.
    #[test]
    fn a_path_is_matched_by_whole_directories_rather_than_by_text() {
        let cfg = vec![project("bead", "/home/user/dev/bead")];

        assert_eq!(
            project_holding(Path::new("/home/user/dev/beady"), &cfg),
            None
        );
    }

    #[test]
    fn a_pane_in_no_configured_project_joins_nothing_and_is_unattributed() {
        let beads = rows(BEADS);
        let live = parse_agent_list(A_SESSION, PANES).expect("the fixture parses");
        let cfg = vec![project("orbital", FIXTURE_PROJECT_PATH)];

        let joined = resolve(
            &[ProjectRows {
                project: "orbital",
                rows: &beads,
            }],
            Listed::all(&live),
            &Config::naming(cfg),
        );

        // wF:pD names bead lnd-4t8.5 from an unconfigured lander, and is as
        // unattributed as the panes that named nothing at all.
        assert_eq!(
            loose(&live, &joined),
            vec!["wE:p1", "wE:p2", "wF:p1", "wF:pD", "wG:p1", "wG:p2", "wG:p4", "wG:p6", "wH:p1",]
        );
        assert_eq!(joined.conflicts, vec![]);
    }

    /// The reason the key is `(project, id)`: prefixes are per-tracker and
    /// nothing coordinates them.
    #[test]
    fn colliding_prefixes_do_not_cross_attach_an_inferred_agent() {
        let one = rows(r#"[{"id":"x-1","title":"in project one","status":"in_progress"}]"#);
        let two = rows(r#"[{"id":"x-1","title":"in project two","status":"in_progress"}]"#);
        let live = panes(
            r#"{"pane_id":"w:p1","cwd":"/home/user/one/src","agent_status":"working",
                "display_agent":"x-1"}"#,
        );
        let cfg = vec![
            project("one", "/home/user/one"),
            project("two", "/home/user/two"),
        ];

        let joined = resolve(
            &[
                ProjectRows {
                    project: "one",
                    rows: &one,
                },
                ProjectRows {
                    project: "two",
                    rows: &two,
                },
            ],
            Listed::all(&live),
            &Config::naming(cfg),
        );

        assert_eq!(pane_of(&joined, "one", "x-1").pane, pane_key("w:p1"));
        assert_eq!(
            joined.agents.len(),
            1,
            "project two's x-1 is a different bead"
        );
    }

    #[test]
    fn colliding_prefixes_do_not_cross_attach_an_exact_agent() {
        let one = rows(r#"[{"id":"x-1","title":"in project one","status":"open"}]"#);
        let two = rows(
            r#"[{"id":"x-1","title":"in project two","status":"in_progress",
                 "metadata":{"agent_pane":"w:p1"}}]"#,
        );
        let live =
            panes(r#"{"pane_id":"w:p1","cwd":"/home/user/one/src","agent_status":"working"}"#);
        let cfg = vec![
            project("one", "/home/user/one"),
            project("two", "/home/user/two"),
        ];

        let joined = resolve(
            &[
                ProjectRows {
                    project: "one",
                    rows: &one,
                },
                ProjectRows {
                    project: "two",
                    rows: &two,
                },
            ],
            Listed::all(&live),
            &Config::naming(cfg),
        );

        assert_eq!(
            joined.agents,
            BTreeMap::new(),
            "the pane is not project two's to claim"
        );
        assert_eq!(
            joined.conflicts,
            vec![Conflict::PaneInAnotherProject {
                bead: key("two", "x-1"),
                pane: pane_key("w:p1"),
                pane_project: Some("one".to_string()),
            }]
        );
        assert_eq!(
            joined.refused,
            BTreeMap::from([(
                key("two", "x-1"),
                Conflict::PaneInAnotherProject {
                    bead: key("two", "x-1"),
                    pane: pane_key("w:p1"),
                    pane_project: Some("one".to_string()),
                }
            )])
        );
    }

    /// A pane naming a bead that exists only in another project is the same
    /// finding from the other direction.
    #[test]
    fn a_pane_naming_another_projects_bead_does_not_join_and_is_reported() {
        let one = rows(r#"[{"id":"a-1","title":"only in one","status":"open"}]"#);
        let two = rows(r#"[{"id":"b-1","title":"only in two","status":"open"}]"#);
        let live = panes(
            r#"{"pane_id":"w:p1","cwd":"/home/user/two","agent_status":"working",
                "display_agent":"a-1"}"#,
        );
        let cfg = vec![
            project("one", "/home/user/one"),
            project("two", "/home/user/two"),
        ];

        let joined = resolve(
            &[
                ProjectRows {
                    project: "one",
                    rows: &one,
                },
                ProjectRows {
                    project: "two",
                    rows: &two,
                },
            ],
            Listed::all(&live),
            &Config::naming(cfg),
        );

        assert_eq!(joined.agents, BTreeMap::new());
        assert_eq!(
            joined.conflicts,
            vec![Conflict::PaneInAnotherProject {
                bead: key("one", "a-1"),
                pane: pane_key("w:p1"),
                pane_project: Some("two".to_string()),
            }]
        );
        assert_eq!(
            joined.refused,
            BTreeMap::new(),
            "project one's bead named no pane, so it is owed no reason it has none: \
             what a pane in another project calls itself is not its claim"
        );
    }

    /// A pane under a project the scope left out is neither drawn nor
    /// reported. Its own tracker was never read, so an id it names that a
    /// read project happens to hold says nothing about that project's bead —
    /// the pane is most likely on its own project's bead of that id.
    #[test]
    fn a_pane_in_a_project_the_scope_left_out_names_nothing_and_is_not_reported() {
        let one = rows(r#"[{"id":"x-1","title":"in project one","status":"in_progress"}]"#);
        let live = panes(
            r#"{"pane_id":"w:p1","cwd":"/home/user/two/src","agent_status":"working",
                "display_agent":"x-1"}"#,
        );
        let cfg = Config::naming(vec![
            project("one", "/home/user/one"),
            project("two", "/home/user/two"),
        ])
        .scoped_to(&["one".to_string()])
        .expect("one is configured");

        let joined = resolve(
            &[ProjectRows {
                project: "one",
                rows: &one,
            }],
            Listed::all(&live),
            &cfg,
        );

        assert_eq!(joined.agents, BTreeMap::new());
        assert_eq!(joined.conflicts, vec![]);
    }

    /// A read bead naming a pane that sits in a project the scope left out
    /// is reported as a pane in that project — which the config still names
    /// — rather than as one in a directory no project covers.
    #[test]
    fn a_bead_naming_a_pane_in_a_project_the_scope_left_out_is_told_which_project() {
        let beads = rows(
            r#"[{"id":"p-1","title":"root","status":"in_progress",
                 "metadata":{"agent_pane":"w:p1"}}]"#,
        );
        let live =
            panes(r#"{"pane_id":"w:p1","cwd":"/home/user/two/src","agent_status":"working"}"#);
        let cfg = Config::naming(vec![
            project("one", "/home/user/one"),
            project("two", "/home/user/two"),
        ])
        .scoped_to(&["one".to_string()])
        .expect("one is configured");

        let joined = resolve(
            &[ProjectRows {
                project: "one",
                rows: &beads,
            }],
            Listed::all(&live),
            &cfg,
        );

        assert_eq!(
            joined.conflicts,
            vec![Conflict::PaneInAnotherProject {
                bead: key("one", "p-1"),
                pane: pane_key("w:p1"),
                pane_project: Some("two".to_string()),
            }]
        );
    }

    #[test]
    fn a_bead_naming_a_pane_in_no_configured_project_does_not_join_and_is_reported() {
        let beads = rows(
            r#"[{"id":"p-1","title":"root","status":"in_progress",
                 "metadata":{"agent_pane":"w:p1"}}]"#,
        );
        let live = panes(r#"{"pane_id":"w:p1","cwd":"/tmp","agent_status":"working"}"#);
        let cfg = vec![project("proj", "/home/user/proj")];

        let joined = resolve(
            &[ProjectRows {
                project: "proj",
                rows: &beads,
            }],
            Listed::all(&live),
            &Config::naming(cfg),
        );

        assert_eq!(joined.agents, BTreeMap::new());
        let outside = Conflict::PaneInAnotherProject {
            bead: key("proj", "p-1"),
            pane: pane_key("w:p1"),
            pane_project: None,
        };
        assert_eq!(joined.conflicts, vec![outside.clone()]);
        assert_eq!(
            joined.refused,
            BTreeMap::from([(key("proj", "p-1"), outside)])
        );
    }

    // ---- the disagreements ---------------------------------------------

    #[test]
    fn the_two_directions_naming_different_panes_is_reported_and_the_beads_key_wins() {
        let beads = rows(
            r#"[{"id":"p-1","title":"root","status":"in_progress",
                 "metadata":{"agent_pane":"w:p1"}}]"#,
        );
        let live = panes(
            r#"{"pane_id":"w:p1","cwd":"/home/user/proj","agent_status":"working"},
               {"pane_id":"w:p2","cwd":"/home/user/proj","agent_status":"idle",
                "display_agent":"p-1"}"#,
        );
        let cfg = vec![project("proj", "/home/user/proj")];

        let joined = resolve(
            &[ProjectRows {
                project: "proj",
                rows: &beads,
            }],
            Listed::all(&live),
            &Config::naming(cfg),
        );

        let a = pane_of(&joined, "proj", "p-1");
        assert_eq!(
            a.pane,
            pane_key("w:p1"),
            "the bead's own key is exact and wins"
        );
        assert_eq!(a.source, JoinSource::AgentPane);
        assert_eq!(
            joined.conflicts,
            vec![Conflict::BeadAndPaneDisagree {
                bead: key("proj", "p-1"),
                named_by_bead: pane_key("w:p1"),
                named_by_pane: pane_key("w:p2"),
            }],
            "winning is not the same as agreeing"
        );
    }

    /// Discrimination for the test above: both directions pointing at one pane
    /// is agreement, and agreement is not a finding.
    #[test]
    fn the_two_directions_naming_one_pane_is_not_a_conflict() {
        let beads = rows(
            r#"[{"id":"p-1","title":"root","status":"in_progress",
                 "metadata":{"agent_pane":"w:p1"}}]"#,
        );
        let live = panes(
            r#"{"pane_id":"w:p1","cwd":"/home/user/proj","agent_status":"working",
                "display_agent":"p-1"}"#,
        );
        let cfg = vec![project("proj", "/home/user/proj")];

        let joined = resolve(
            &[ProjectRows {
                project: "proj",
                rows: &beads,
            }],
            Listed::all(&live),
            &Config::naming(cfg),
        );

        assert_eq!(
            pane_of(&joined, "proj", "p-1").source,
            JoinSource::AgentPane
        );
        assert_eq!(joined.conflicts, vec![]);
    }

    #[test]
    fn several_panes_naming_one_bead_leaves_it_unclaimed_and_reported() {
        let beads = rows(r#"[{"id":"p-1","title":"root","status":"in_progress"}]"#);
        let live = panes(
            r#"{"pane_id":"w:p2","cwd":"/home/user/proj","agent_status":"working",
                "display_agent":"p-1"},
               {"pane_id":"w:p1","cwd":"/home/user/proj","agent_status":"idle",
                "display_agent":"p-1"}"#,
        );
        let cfg = vec![project("proj", "/home/user/proj")];

        let joined = resolve(
            &[ProjectRows {
                project: "proj",
                rows: &beads,
            }],
            Listed::all(&live),
            &Config::naming(cfg),
        );

        assert_eq!(joined.agents, BTreeMap::new(), "neither pane wins");
        assert_eq!(
            joined.conflicts,
            vec![Conflict::SeveralPanesNameOneBead {
                bead: key("proj", "p-1"),
                panes: vec![pane_key("w:p1"), pane_key("w:p2")],
            }]
        );
        assert_eq!(loose(&live, &joined), vec!["w:p1", "w:p2"]);
    }

    #[test]
    fn several_beads_naming_one_pane_leaves_them_all_unclaimed_and_reported() {
        let beads = rows(
            r#"[
              {"id":"p-1","title":"root","status":"open"},
              {"id":"p-1.1","title":"one","status":"in_progress",
               "dependencies":[{"depends_on_id":"p-1","type":"parent-child"}],
               "metadata":{"agent_pane":"w:p1"}},
              {"id":"p-1.2","title":"two","status":"in_progress",
               "dependencies":[{"depends_on_id":"p-1","type":"parent-child"}],
               "metadata":{"agent_pane":"w:p1"}}
            ]"#,
        );
        let live = panes(r#"{"pane_id":"w:p1","cwd":"/home/user/proj","agent_status":"working"}"#);
        let cfg = vec![project("proj", "/home/user/proj")];

        let joined = resolve(
            &[ProjectRows {
                project: "proj",
                rows: &beads,
            }],
            Listed::all(&live),
            &Config::naming(cfg),
        );

        assert_eq!(joined.agents, BTreeMap::new(), "neither bead gets the pane");
        let contested = Conflict::SeveralBeadsNameOnePane {
            pane: pane_key("w:p1"),
            caption: None,
            beads: vec![key("proj", "p-1.1"), key("proj", "p-1.2")],
        };
        assert_eq!(joined.conflicts, vec![contested.clone()]);
        assert_eq!(
            joined.refused,
            BTreeMap::from([
                (key("proj", "p-1.1"), contested.clone()),
                (key("proj", "p-1.2"), contested),
            ]),
            "each of them claimed the pane, so each is owed the reason it has none"
        );
    }

    /// The reading this was measured from: one seat claimed three beads in
    /// turn and cleared its key on none of them, so three claims stood on the
    /// pane it was still sitting in. `bdi` awards it to none of them, and the
    /// only thing on the screen that tells the live claim from the two stale
    /// ones is the pane's own account of what it is working on — which the
    /// disagreement therefore carries. The session here is the one captured
    /// while it was happening.
    #[test]
    fn a_contested_pane_carries_its_own_account_of_what_it_is_working_on() {
        let beads = rows(
            r#"[
              {"id":"orb-9fw","title":"orbital v1","status":"open"},
              {"id":"orb-2bb.16","title":"a claim its seat moved on from",
               "status":"in_progress",
               "dependencies":[{"depends_on_id":"orb-9fw","type":"parent-child"}],
               "metadata":{"agent_pane":"wM:pG"}},
              {"id":"orb-9fw.12","title":"retiring the dep-tree row shape",
               "status":"in_progress",
               "dependencies":[{"depends_on_id":"orb-9fw","type":"parent-child"}],
               "metadata":{"agent_pane":"wM:pG"}},
              {"id":"orb-2bb.19","title":"the other claim it moved on from",
               "status":"in_progress",
               "dependencies":[{"depends_on_id":"orb-9fw","type":"parent-child"}],
               "metadata":{"agent_pane":"wM:pG"}}
            ]"#,
        );
        let live = parse_agent_list(A_SESSION, JOINED_PANES).expect("the fixture parses");
        let cfg = vec![project("orbital", FIXTURE_PROJECT_PATH)];

        let joined = resolve(
            &[ProjectRows {
                project: "orbital",
                rows: &beads,
            }],
            Listed::all(&live),
            &Config::naming(cfg),
        );

        assert_eq!(joined.agents, BTreeMap::new(), "none of the three gets it");
        assert_eq!(
            joined.conflicts,
            vec![Conflict::SeveralBeadsNameOnePane {
                pane: pane_key("wM:pG"),
                caption: Some(
                    "orb-9fw.12: retiring the dep-tree row shape from fixtures".to_string()
                ),
                beads: vec![
                    key("orbital", "orb-2bb.16"),
                    key("orbital", "orb-2bb.19"),
                    key("orbital", "orb-9fw.12"),
                ],
            }]
        );
    }

    /// A pane with nothing to say about itself still contests, and the
    /// disagreement says what it can rather than inventing the rest.
    #[test]
    fn a_contested_pane_that_says_nothing_about_itself_carries_nothing() {
        let beads = rows(
            r#"[
              {"id":"p-1","title":"root","status":"open"},
              {"id":"p-1.1","title":"one","status":"in_progress",
               "dependencies":[{"depends_on_id":"p-1","type":"parent-child"}],
               "metadata":{"agent_pane":"w:p1"}},
              {"id":"p-1.2","title":"two","status":"in_progress",
               "dependencies":[{"depends_on_id":"p-1","type":"parent-child"}],
               "metadata":{"agent_pane":"w:p1"}}
            ]"#,
        );
        let live = panes(r#"{"pane_id":"w:p1","cwd":"/home/user/proj","agent_status":"working"}"#);
        let cfg = vec![project("proj", "/home/user/proj")];

        let joined = resolve(
            &[ProjectRows {
                project: "proj",
                rows: &beads,
            }],
            Listed::all(&live),
            &Config::naming(cfg),
        );

        assert_eq!(
            joined.conflicts,
            vec![Conflict::SeveralBeadsNameOnePane {
                pane: pane_key("w:p1"),
                caption: None,
                beads: vec![key("proj", "p-1.1"), key("proj", "p-1.2")],
            }]
        );
    }

    /// A pane contested by two beads is still a pane one of them might be
    /// working. The inferred direction fills the gap the void left; both
    /// findings stand.
    #[test]
    fn a_bead_whose_exact_claim_was_voided_still_takes_an_uncontested_inferred_pane() {
        let beads = rows(
            r#"[
              {"id":"p-1","title":"root","status":"open"},
              {"id":"p-1.1","title":"one","status":"in_progress",
               "dependencies":[{"depends_on_id":"p-1","type":"parent-child"}],
               "metadata":{"agent_pane":"w:p1"}},
              {"id":"p-1.2","title":"two","status":"in_progress",
               "dependencies":[{"depends_on_id":"p-1","type":"parent-child"}],
               "metadata":{"agent_pane":"w:p1"}}
            ]"#,
        );
        let live = panes(
            r#"{"pane_id":"w:p1","cwd":"/home/user/proj","agent_status":"working"},
               {"pane_id":"w:p9","cwd":"/home/user/proj","agent_status":"working",
                "display_agent":"p-1.1"}"#,
        );
        let cfg = vec![project("proj", "/home/user/proj")];

        let joined = resolve(
            &[ProjectRows {
                project: "proj",
                rows: &beads,
            }],
            Listed::all(&live),
            &Config::naming(cfg),
        );

        let a = pane_of(&joined, "proj", "p-1.1");
        assert_eq!(a.pane, pane_key("w:p9"));
        assert_eq!(a.source, JoinSource::DisplayAgent);
        assert_eq!(
            joined.conflicts,
            vec![
                Conflict::BeadAndPaneDisagree {
                    bead: key("proj", "p-1.1"),
                    named_by_bead: pane_key("w:p1"),
                    named_by_pane: pane_key("w:p9"),
                },
                Conflict::SeveralBeadsNameOnePane {
                    pane: pane_key("w:p1"),
                    caption: None,
                    beads: vec![key("proj", "p-1.1"), key("proj", "p-1.2")],
                },
            ]
        );
    }

    // ---- the contract ----------------------------------------------------

    #[test]
    fn the_join_source_serialises_the_way_the_contract_spells_it() {
        assert_eq!(
            serde_json::to_string(&JoinSource::AgentPane).unwrap(),
            r#""agent_pane""#
        );
        assert_eq!(
            serde_json::to_string(&JoinSource::DisplayAgent).unwrap(),
            r#""display_agent""#
        );
    }

    #[test]
    fn a_conflict_serialises_under_its_own_name() {
        let out = serde_json::to_string(&Conflict::SeveralPanesNameOneBead {
            bead: key("proj", "p-1"),
            panes: vec![pane_key("w:p1")],
        })
        .unwrap();

        assert_eq!(
            out,
            r#"{"conflict":"several-panes-name-one-bead","bead":{"project":"proj","id":"p-1"},"panes":[{"session":"default","id":"w:p1"}]}"#
        );
    }
}