ljos 0.30.0

Memory, a work tracker and a shell-command gate for coding agents (Claude Code, Codex, Grok Build, agy, opencode) over MCP and hooks.
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
//! A panel closes itself. Once the substance has passed, a nit, a wording
//! note or a style note is written on the issue and never becomes a child.
//! A persona that wants a child marks the objection blocking and gives a
//! reason; the default, with no mark or a mark and no reason, is a nit.
//! The same objection is not filed twice. A dissent child does not file
//! a grandchild unless the new objection is blocking. At the round limit,
//! or when the budget is spent, the item closes as settled-with-notes and
//! lists what remains. Nothing here waits on a person.

use anyhow::{Context, Result};
use serde::Deserialize;
use serde_json::Value;
use std::collections::{BTreeMap, BTreeSet};
use std::path::PathBuf;

/// Severities that stay on the issue as notes once the substance has settled.
pub const BELOW_CHILD: &[&str] = &["nit", "wording", "style"];

/// How serious an objection is. A child is filed only for [`Severity::Blocking`]
/// with a non-empty reason, or for a held minority on an unsettled issue
/// that is not itself a dissent child.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum Severity {
    Nit,
    Wording,
    Style,
    Blocking,
}

impl Severity {
    #[must_use]
    pub fn parse(word: &str) -> Option<Self> {
        match word.trim().to_ascii_lowercase().as_str() {
            "nit" => Some(Self::Nit),
            "wording" => Some(Self::Wording),
            "style" => Some(Self::Style),
            "blocking" => Some(Self::Blocking),
            _ => None,
        }
    }

    #[must_use]
    pub fn word(self) -> &'static str {
        match self {
            Self::Nit => "nit",
            Self::Wording => "wording",
            Self::Style => "style",
            Self::Blocking => "blocking",
        }
    }

    /// Below the line that files a child.
    #[must_use]
    pub fn is_note(self) -> bool {
        !matches!(self, Self::Blocking)
    }
}

/// One objection a persona raised against a target (an issue, or a span on it).
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Objection {
    pub persona: String,
    pub severity: Severity,
    /// Required for a blocking mark. Empty means the mark did not take,
    /// and the objection is a nit.
    pub reason: String,
    pub text: String,
    pub target: String,
    /// A ballot that still holds a minority option on an issue whose
    /// substance has not settled. Filed once, at the top of a tree only.
    pub held: bool,
}

impl Objection {
    #[must_use]
    pub fn nit(persona: &str, text: &str, target: &str) -> Self {
        Self::noted(persona, Severity::Nit, "", text, target)
    }

    #[must_use]
    pub fn wording(persona: &str, text: &str, target: &str) -> Self {
        Self::noted(persona, Severity::Wording, "", text, target)
    }

    #[must_use]
    pub fn style(persona: &str, text: &str, target: &str) -> Self {
        Self::noted(persona, Severity::Style, "", text, target)
    }

    #[must_use]
    pub fn blocking(persona: &str, reason: &str, text: &str, target: &str) -> Self {
        Self::noted(persona, Severity::Blocking, reason, text, target)
    }

    fn noted(persona: &str, severity: Severity, reason: &str, text: &str, target: &str) -> Self {
        let reason = reason.trim().to_string();
        let severity = if severity == Severity::Blocking && reason.is_empty() {
            Severity::Nit
        } else {
            severity
        };
        Self {
            persona: persona.trim().to_string(),
            severity,
            reason,
            text: text.trim().to_string(),
            target: target.trim().to_string(),
            held: false,
        }
    }

    /// A blocking mark with a reason. Anything else stays a note.
    #[must_use]
    pub fn files_as_child(&self) -> bool {
        self.severity == Severity::Blocking && !self.reason.trim().is_empty()
    }

    /// Whether this objection may become a child from here.
    /// A dissent child (`depth` >= 1) files only a blocking objection.
    #[must_use]
    pub fn may_file(&self, passed: bool, depth: usize) -> bool {
        if self.files_as_child() {
            return true;
        }
        self.held && !passed && depth == 0
    }

    /// Normalized text plus normalized target. Two objections with this
    /// pair are the same objection.
    #[must_use]
    pub fn key(&self) -> (String, String) {
        (normalize(&self.text), normalize(&self.target))
    }
}

/// Collapse case, punctuation and whitespace so a rephrased comma is the
/// same objection as the last round's comma.
#[must_use]
pub fn normalize(text: &str) -> String {
    text.chars()
        .map(|c| {
            if c.is_ascii_alphanumeric() {
                c.to_ascii_lowercase()
            } else {
                ' '
            }
        })
        .collect::<String>()
        .split_whitespace()
        .collect::<Vec<_>>()
        .join(" ")
}

/// `finding: NAME: TEXT` is a nit. `finding: NAME: wording: TEXT` and
/// `finding: NAME: style: TEXT` stay notes. `finding: NAME: blocking: REASON :: TEXT`
/// files a child. A blocking mark with no reason is a nit. `@ target` at
/// the end names the target; otherwise `default_target` is the target.
#[must_use]
pub fn parse_objection(line: &str, default_target: &str) -> Option<Objection> {
    let line = line.trim().trim_start_matches('-').trim();
    let rest = line
        .strip_prefix("finding:")
        .or_else(|| line.strip_prefix("objection:"))
        .map(str::trim)
        .filter(|s| !s.is_empty())?;
    let (persona, rest) = rest.split_once(':')?;
    let persona = persona.trim();
    if persona.is_empty() {
        return None;
    }
    let rest = rest.trim();
    let (severity, rest) = match rest.split_once(':') {
        Some((word, after)) => match Severity::parse(word) {
            Some(severity) => (severity, after.trim()),
            None => (Severity::Nit, rest),
        },
        None => (Severity::Nit, rest),
    };
    let (reason, rest) = if severity == Severity::Blocking {
        match rest.split_once("::") {
            Some((reason, text)) => (reason.trim().to_string(), text.trim()),
            None => (String::new(), rest),
        }
    } else {
        (String::new(), rest)
    };
    let (text, target) = match rest.rsplit_once(" @ ") {
        Some((text, target)) if !target.trim().is_empty() => (text.trim(), target.trim()),
        _ => (rest, default_target),
    };
    if text.is_empty() {
        return None;
    }
    Some(Objection::noted(persona, severity, &reason, text, target))
}

/// Rounds, children, and optional wall time or token estimate.
/// Unset wall time and tokens do not bind.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct ScopeBudget {
    pub max_rounds: usize,
    pub max_children: usize,
    pub max_descendants: usize,
    pub max_wall_secs: Option<u64>,
    pub max_tokens: Option<u64>,
}

impl ScopeBudget {
    /// A single panel: three rounds, a handful of children.
    #[must_use]
    pub fn panel() -> Self {
        Self {
            max_rounds: 3,
            max_children: 8,
            max_descendants: 12,
            max_wall_secs: None,
            max_tokens: None,
        }
    }

    /// A feature workflow, which files design and fix children of its own.
    #[must_use]
    pub fn workflow() -> Self {
        Self {
            max_rounds: 3,
            max_children: 24,
            max_descendants: 48,
            max_wall_secs: None,
            max_tokens: None,
        }
    }

    fn overlay(&mut self, file: &ScopeFile) {
        if let Some(n) = file.max_rounds {
            self.max_rounds = n;
        }
        if let Some(n) = file.max_children {
            self.max_children = n;
        }
        if let Some(n) = file.max_descendants {
            self.max_descendants = n;
        }
        if file.max_wall_secs.is_some() {
            self.max_wall_secs = file.max_wall_secs;
        }
        if file.max_tokens.is_some() {
            self.max_tokens = file.max_tokens;
        }
    }
}

/// Budgets for one panel and for a workflow parent. Defaults apply until
/// a toml file says otherwise.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct PanelBudget {
    pub panel: ScopeBudget,
    pub workflow: ScopeBudget,
}

impl Default for PanelBudget {
    fn default() -> Self {
        Self {
            panel: ScopeBudget::panel(),
            workflow: ScopeBudget::workflow(),
        }
    }
}

#[derive(Debug, Clone, Deserialize, Default)]
struct BudgetFile {
    #[serde(default)]
    panel: ScopeFile,
    #[serde(default)]
    workflow: ScopeFile,
}

#[derive(Debug, Clone, Deserialize, Default)]
struct ScopeFile {
    max_rounds: Option<usize>,
    max_children: Option<usize>,
    max_descendants: Option<usize>,
    max_wall_secs: Option<u64>,
    max_tokens: Option<u64>,
}

/// The budget a toml document names, over the defaults. A document with
/// neither table is the defaults. Unknown keys are ignored.
#[must_use]
pub fn budget_from_toml(text: &str) -> PanelBudget {
    let mut budget = PanelBudget::default();
    let Ok(file) = toml::from_str::<BudgetFile>(text) else {
        return budget;
    };
    budget.panel.overlay(&file.panel);
    budget.workflow.overlay(&file.workflow);
    budget
}

/// Config dir first, then `./vissue.toml` and `./ljos.toml`, then
/// `VISSUE_CONFIG` and `LJOS_CONFIG`. A later file overrides the keys it
/// sets. A missing file is skipped. A file that does not parse is skipped.
#[must_use]
pub fn load_budget() -> PanelBudget {
    let mut budget = PanelBudget::default();
    for path in budget_paths() {
        let Ok(text) = std::fs::read_to_string(&path) else {
            continue;
        };
        let Ok(file) = toml::from_str::<BudgetFile>(&text) else {
            continue;
        };
        budget.panel.overlay(&file.panel);
        budget.workflow.overlay(&file.workflow);
    }
    budget
}

fn budget_paths() -> Vec<PathBuf> {
    let mut paths = Vec::new();
    if let Some(base) = config_base() {
        paths.push(base.join("vissue").join("config.toml"));
        paths.push(base.join("ljos").join("ljos.toml"));
    }
    paths.push(PathBuf::from("vissue.toml"));
    paths.push(PathBuf::from("ljos.toml"));
    if let Some(path) = std::env::var_os("VISSUE_CONFIG").filter(|p| !p.is_empty()) {
        paths.push(PathBuf::from(path));
    }
    if let Some(path) = std::env::var_os("LJOS_CONFIG").filter(|p| !p.is_empty()) {
        paths.push(PathBuf::from(path));
    }
    paths
}

fn config_base() -> Option<PathBuf> {
    std::env::var_os("XDG_CONFIG_HOME")
        .map(PathBuf::from)
        .filter(|p| p.is_absolute())
        .or_else(|| {
            std::env::var_os("HOME")
                .map(|h| PathBuf::from(h).join(".config"))
                .filter(|p| p.is_absolute())
        })
}

/// Where a panel stands when the next round is asked for.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct PanelState {
    pub round: usize,
    pub passed: bool,
    /// 0 on the work. 1 on a dissent child, and so on.
    pub dissent_depth: usize,
    pub children_filed: usize,
    pub open_children: usize,
    /// The round has not been sat: ballots are missing. This is not a pause
    /// for a person to raise the round limit.
    pub awaiting_ballots: bool,
    pub seen: Vec<(String, String)>,
    pub wall_secs: Option<u64>,
    pub tokens: Option<u64>,
    pub objections: Vec<Objection>,
}

impl PanelState {
    #[must_use]
    pub fn at_round(round: usize) -> Self {
        Self {
            round,
            passed: false,
            dissent_depth: 0,
            children_filed: 0,
            open_children: 0,
            awaiting_ballots: false,
            seen: Vec::new(),
            wall_secs: None,
            tokens: None,
            objections: Vec::new(),
        }
    }
}

/// What to do with one objection.
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum Fate {
    Note(Objection),
    File(Objection),
    Duplicate(Objection),
    /// Would have been a child, and the budget closed the item instead.
    Listed(Objection),
}

/// What one call does with the objections it was shown.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Disposal {
    pub fates: Vec<Fate>,
    pub close: bool,
    pub summary: String,
}

/// The next step of a workflow panel. A close is the step, not an error
/// and not a wait.
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum RoundPlan {
    OpenRound { n: usize },
    FileBlocking { objections: Vec<Objection> },
    Settle { summary: String },
    Hold { why: String },
    Passed,
}

fn budget_why(state: &PanelState, limits: &ScopeBudget) -> Option<String> {
    if state.round > 0 && state.round >= limits.max_rounds {
        return Some(format!(
            "round {} reached the limit of {}",
            state.round, limits.max_rounds
        ));
    }
    if state.children_filed >= limits.max_children {
        return Some(format!(
            "{} children filed, at the limit of {}",
            state.children_filed, limits.max_children
        ));
    }
    if let (Some(used), Some(max)) = (state.wall_secs, limits.max_wall_secs) {
        if used >= max {
            return Some(format!("wall time {used}s reached the limit of {max}s"));
        }
    }
    if let (Some(used), Some(max)) = (state.tokens, limits.max_tokens) {
        if used >= max {
            return Some(format!("token estimate {used} reached the limit of {max}"));
        }
    }
    None
}

fn fresh(state: &PanelState) -> Vec<Objection> {
    let mut batch = state.seen.iter().cloned().collect::<BTreeSet<_>>();
    let mut out = Vec::new();
    for objection in &state.objections {
        let key = objection.key();
        if batch.contains(&key) {
            continue;
        }
        batch.insert(key);
        out.push(objection.clone());
    }
    out
}

fn summary_of(why: &str, listed: &[Objection]) -> String {
    let mut out = format!("settled-with-notes: {why}");
    if listed.is_empty() {
        return out;
    }
    out.push_str(". remaining:");
    for (i, objection) in listed.iter().take(12).enumerate() {
        out.push_str(&format!(
            " ({}) {} {}: {}",
            i + 1,
            objection.persona,
            objection.severity.word(),
            clip(&objection.text, 80)
        ));
    }
    if listed.len() > 12 {
        out.push_str(&format!(" and {} more", listed.len() - 12));
    }
    out
}

fn clip(text: &str, n: usize) -> String {
    let mut out: String = text.chars().take(n).collect();
    if text.chars().count() > n {
        out.push('…');
    }
    out
}

/// Sort the objections on one issue: notes, one child each for what may
/// be filed, duplicates dropped, and a close when the budget is spent.
#[must_use]
pub fn dispose(state: &PanelState, limits: &ScopeBudget) -> Disposal {
    let mut fates = Vec::new();
    let mut batch = state.seen.iter().cloned().collect::<BTreeSet<_>>();
    let mut fileable = Vec::new();
    for objection in &state.objections {
        let key = objection.key();
        if batch.contains(&key) {
            fates.push(Fate::Duplicate(objection.clone()));
            continue;
        }
        batch.insert(key);
        if objection.may_file(state.passed, state.dissent_depth) {
            fileable.push(objection.clone());
        } else {
            fates.push(Fate::Note(objection.clone()));
        }
    }
    if let Some(why) = budget_why(state, limits) {
        for objection in &fileable {
            fates.push(Fate::Listed(objection.clone()));
        }
        return Disposal {
            fates,
            close: true,
            summary: summary_of(&why, &fileable),
        };
    }
    let room = limits.max_children.saturating_sub(state.children_filed);
    let mut listed = Vec::new();
    for (i, objection) in fileable.into_iter().enumerate() {
        if i < room {
            fates.push(Fate::File(objection));
        } else {
            listed.push(objection);
        }
    }
    let close = !listed.is_empty();
    let summary = if close {
        summary_of(
            &format!(
                "{} children filed, at the limit of {}",
                state.children_filed + room,
                limits.max_children
            ),
            &listed,
        )
    } else {
        String::new()
    };
    for objection in listed {
        fates.push(Fate::Listed(objection));
    }
    Disposal {
        fates,
        close,
        summary,
    }
}

/// The next workflow step. Open children and uncast ballots hold. A
/// blocking objection files one child, including after the substance has
/// passed, and the next round waits until that child is closed. A pass
/// with nothing blocking left is a pass. A round of nits closes. At the
/// round limit a blocking objection is listed, and a pass with nothing
/// left to file stays a pass.
#[must_use]
pub fn plan_round(state: &PanelState, limits: &ScopeBudget) -> RoundPlan {
    if state.open_children > 0 {
        return RoundPlan::Hold {
            why: format!(
                "{} {} still open",
                state.open_children,
                if state.open_children == 1 {
                    "child is"
                } else {
                    "children are"
                }
            ),
        };
    }
    if state.awaiting_ballots {
        return RoundPlan::Hold {
            why: "the panel has not all voted".into(),
        };
    }
    let mut fileable = Vec::new();
    let mut seen = state.seen.iter().cloned().collect::<BTreeSet<_>>();
    let mut addressed = 0;
    for objection in &state.objections {
        let key = objection.key();
        if seen.contains(&key) {
            if objection.files_as_child() {
                addressed += 1;
            }
            continue;
        }
        seen.insert(key);
        if objection.may_file(state.passed, state.dissent_depth) {
            fileable.push(objection.clone());
        }
    }
    if let Some(why) = budget_why(state, limits) {
        if state.passed && fileable.is_empty() {
            return RoundPlan::Passed;
        }
        return RoundPlan::Settle {
            summary: summary_of(&why, &fresh(state)),
        };
    }
    if !fileable.is_empty() {
        let room = limits.max_children.saturating_sub(state.children_filed);
        if room == 0 {
            return RoundPlan::Settle {
                summary: summary_of(
                    &format!(
                        "{} children filed, at the limit of {}",
                        state.children_filed, limits.max_children
                    ),
                    &fileable,
                ),
            };
        }
        fileable.truncate(room);
        return RoundPlan::FileBlocking {
            objections: fileable,
        };
    }
    if state.passed {
        return RoundPlan::Passed;
    }
    if state.round == 0 {
        return RoundPlan::OpenRound { n: 1 };
    }
    if addressed > 0 && state.round < limits.max_rounds {
        return RoundPlan::OpenRound { n: state.round + 1 };
    }
    let listed = fresh(state);
    let why = if listed.is_empty() {
        "no blocking objection remains".to_string()
    } else {
        "the objections are below blocking".to_string()
    };
    RoundPlan::Settle {
        summary: summary_of(&why, &listed),
    }
}

/// Whether `pass` is the unique strict majority, or, on any other ballot,
/// one option leads by a strict majority. A tie has not settled.
#[must_use]
pub fn substance_passed(choices: &[(String, String)]) -> bool {
    if choices.is_empty() {
        return false;
    }
    let norm = |s: &str| s.trim().to_ascii_lowercase();
    let pass = choices.iter().filter(|(_, c)| norm(c) == "pass").count();
    let revise = choices.iter().filter(|(_, c)| norm(c) == "revise").count();
    if pass + revise == choices.len() {
        return pass > revise && pass * 2 > choices.len();
    }
    let mut counts: BTreeMap<String, usize> = BTreeMap::new();
    for (_, choice) in choices {
        *counts.entry(normalize(choice)).or_default() += 1;
    }
    let mut ranked: Vec<(String, usize)> = counts.into_iter().collect();
    ranked.sort_by(|a, b| b.1.cmp(&a.1).then(a.0.cmp(&b.0)));
    ranked.len() == 1 || (ranked[0].1 > ranked[1].1 && ranked[0].1 * 2 > choices.len())
}

/// True when two choices share the highest count. A single ballot, and a
/// strict lead, are not ties.
fn ballot_tie(choices: &[(String, String)]) -> bool {
    let mut counts: BTreeMap<String, usize> = BTreeMap::new();
    for (_, choice) in choices {
        *counts.entry(normalize(choice)).or_default() += 1;
    }
    let Some(max) = counts.values().copied().max() else {
        return false;
    };
    counts.values().filter(|n| **n == max).count() > 1
}

/// Findings on an issue JSON: logbook notes and body lines.
#[must_use]
pub fn objections_in(v: &Value, default_target: &str) -> Vec<Objection> {
    let mut out = Vec::new();
    let mut push = |text: &str| {
        if let Some(objection) = parse_objection(text, default_target) {
            out.push(objection);
        }
    };
    for entry in v["logbook"].as_array().into_iter().flatten() {
        if let Some(note) = entry["note"].as_str() {
            push(note);
        }
        if let Some(raw) = entry["raw"].as_str() {
            push(raw);
        }
    }
    if let Some(body) = v["body"].as_str() {
        for line in body.lines() {
            push(line);
        }
    }
    out
}

/// Depth of a dissent child: the `dissent-depth: N` note, else 1 when the
/// title is a dissent, else 0.
#[must_use]
pub fn dissent_depth(v: &Value) -> usize {
    for entry in v["logbook"].as_array().into_iter().flatten() {
        let Some(note) = entry["note"].as_str() else {
            continue;
        };
        if let Some(rest) = note.trim().strip_prefix("dissent-depth:") {
            if let Ok(n) = rest.trim().parse::<usize>() {
                return n;
            }
        }
    }
    let title = v["title"].as_str().unwrap_or("");
    if title.trim_start().starts_with("Dissent holds") {
        1
    } else {
        0
    }
}

/// The text a filed child carries, so a later round recognises it.
#[must_use]
pub fn filed_key(title: &str, target: &str) -> Option<(String, String)> {
    let title = title.trim();
    let text = title
        .strip_prefix("Dissent holds ")
        .or_else(|| title.strip_prefix("fix: "))
        .or_else(|| title.strip_prefix("fix:"))?;
    let text = text.trim();
    if text.is_empty() {
        return None;
    }
    Some((normalize(text), normalize(target)))
}

fn project_of(v: &Value, id: &str) -> String {
    v["project"]
        .as_str()
        .filter(|s| !s.trim().is_empty())
        .map(str::to_string)
        .or_else(|| id.rsplit_once('-').map(|(p, _)| p.to_string()))
        .unwrap_or_default()
}

/// Options a voter still holds that are not the unique lead. A tie, a
/// single option, and an empty ballot name nothing: there is no minority
/// to file.
fn minority_options(choices: &[(String, String)]) -> Vec<String> {
    let mut counts: BTreeMap<String, (String, usize)> = BTreeMap::new();
    for (_, choice) in choices {
        let shown = choice.trim();
        let key = normalize(shown);
        if key.is_empty() {
            continue;
        }
        let row = counts.entry(key).or_insert_with(|| (shown.to_string(), 0));
        row.1 += 1;
    }
    let Some(max) = counts.values().map(|(_, n)| *n).max() else {
        return Vec::new();
    };
    if counts.values().filter(|(_, n)| *n == max).count() != 1 {
        return Vec::new();
    }
    let mut out: Vec<String> = counts
        .into_values()
        .filter(|(_, n)| *n < max)
        .map(|(shown, _)| shown)
        .collect();
    out.sort();
    out
}

/// One `Dissent holds OPTION` child per minority on a settled majority.
/// A child already filed for that option is named and left alone.
fn file_settled_minority(
    issue: &str,
    project: &str,
    choices: &[(String, String)],
    children: &[String],
    depth: usize,
) -> Vec<String> {
    let mut lines = Vec::new();
    for option in minority_options(choices) {
        let title = format!("Dissent holds {option}");
        if children.iter().any(|t| normalize(t) == normalize(&title)) {
            lines.push(format!("{issue}: dissent {option} already filed"));
            continue;
        }
        match file_child(issue, project, &title, depth) {
            Ok(id) => lines.push(format!("filed {id}  TODO  [#C]  {title}")),
            Err(e) => lines.push(format!("dissent: file refused: {e:#}")),
        }
    }
    lines
}

/// File dissent children and note the rest for `issue`. A settled majority
/// still files one `Dissent holds OPTION` child for each option a voter
/// holds that is not the lead. A tie files none. A tracker that does not
/// answer leaves the reading as it was: filing is a side effect.
#[must_use]
pub fn record_dissents(issue: &str) -> Vec<String> {
    let Ok(v) = crate::issue_record(issue) else {
        return Vec::new();
    };
    let choices: Vec<(String, String)> = crate::ballot_rows(issue)
        .into_iter()
        .filter_map(|row| {
            let agent = row.get("agent")?.as_str()?.to_string();
            let choice = row.get("choice")?.as_str()?.to_string();
            Some((agent, choice))
        })
        .collect();
    let passed = substance_passed(&choices);
    // A tie has no held side to file. Both options lead, so a dissent child
    // for each would be the settle saying it could not choose.
    let tied = ballot_tie(&choices);
    let mut objections = objections_in(&v, issue);
    let noted: BTreeSet<(String, String)> = objections.iter().map(Objection::key).collect();
    if !passed && !tied {
        for (persona, choice) in &choices {
            if normalize(choice) == "pass" {
                continue;
            }
            let mut held = Objection::nit(persona, &format!("holds {choice}"), issue);
            held.held = true;
            if !noted.contains(&held.key())
                && !objections
                    .iter()
                    .any(|o| o.persona == *persona && o.files_as_child())
            {
                objections.push(held);
            }
        }
    }
    let children = child_titles(issue);
    let mut seen = Vec::new();
    let mut children_filed = 0;
    for title in &children {
        if let Some(key) = filed_key(title, issue) {
            seen.push(key);
            children_filed += 1;
        }
    }
    let state = PanelState {
        round: crate::panel_round_number(v["title"].as_str().unwrap_or("")).unwrap_or(0),
        passed,
        dissent_depth: dissent_depth(&v),
        children_filed,
        open_children: 0,
        awaiting_ballots: false,
        seen,
        wall_secs: None,
        tokens: None,
        objections,
    };
    let limits = load_budget().panel;
    let disposal = dispose(&state, &limits);
    let mut lines = Vec::new();
    let project = project_of(&v, issue);
    let depth = state.dissent_depth;
    for fate in disposal.fates {
        match fate {
            Fate::Duplicate(_) => {}
            Fate::Note(objection) if noted.contains(&objection.key()) => {}
            Fate::Note(objection) => match note_issue(issue, &format_note(&objection)) {
                Ok(()) => lines.push(format!(
                    "noted {} from {} on {}: {}",
                    objection.severity.word(),
                    objection.persona,
                    objection.target,
                    objection.text
                )),
                Err(e) => lines.push(format!("dissent: note refused: {e:#}")),
            },
            Fate::File(objection) => {
                let title = dissent_title(&objection);
                match file_child(issue, &project, &title, depth + 1) {
                    Ok(id) => lines.push(format!("filed {id}  TODO  [#C]  {title}")),
                    Err(e) => lines.push(format!("dissent: file refused: {e:#}")),
                }
            }
            Fate::Listed(objection) => lines.push(format!(
                "listed {} from {}: {}",
                objection.severity.word(),
                objection.persona,
                objection.text
            )),
        }
    }
    if disposal.close {
        match close_settled(issue, &disposal.summary) {
            Ok(said) => lines.push(said.trim().to_string()),
            Err(e) => lines.push(format!("dissent: close refused: {e:#}")),
        }
    }
    // A strict majority has settled the substance. The option a voter
    // still holds, and that is not the lead, is still a dissent child.
    // The tie never reaches here with a minority to name.
    if passed {
        lines.extend(file_settled_minority(
            issue,
            &project,
            &choices,
            &children,
            depth + 1,
        ));
    }
    lines
}

fn format_note(objection: &Objection) -> String {
    if objection.files_as_child() {
        format!(
            "finding: {}: blocking: {} :: {} @ {}",
            objection.persona, objection.reason, objection.text, objection.target
        )
    } else {
        format!(
            "finding: {}: {}: {} @ {}",
            objection.persona,
            objection.severity.word(),
            objection.text,
            objection.target
        )
    }
}

fn dissent_title(objection: &Objection) -> String {
    let text = clip(&objection.text, 120);
    format!("Dissent holds {text}")
}

fn child_titles(issue: &str) -> Vec<String> {
    let Ok(said) = crate::run_captured("vissue", &["children", issue, "--json"]) else {
        return Vec::new();
    };
    let Ok(rows) = serde_json::from_str::<Vec<Value>>(&said.stdout) else {
        return Vec::new();
    };
    rows.into_iter()
        .filter_map(|row| {
            let title = row["title"].as_str().map(str::to_string);
            if title.is_some() {
                return title;
            }
            let id = row["id"].as_str()?;
            crate::issue_record(id)
                .ok()
                .and_then(|v| v["title"].as_str().map(str::to_string))
        })
        .collect()
}

fn file_child(parent: &str, project: &str, title: &str, depth: usize) -> Result<String> {
    let mut args = vec!["q".to_string()];
    if !project.is_empty() {
        args.push("-p".into());
        args.push(project.to_string());
    }
    args.extend([
        "--parent".into(),
        parent.to_string(),
        "-t".into(),
        "task".into(),
        title.to_string(),
    ]);
    let said = crate::run_captured("vissue", &args)?;
    let id = said.stdout.trim().to_string();
    if id.is_empty() {
        anyhow::bail!("vissue q printed no id");
    }
    let _ = crate::run_captured("vissue", &["note", &id, &format!("dissent-depth: {depth}")]);
    Ok(id)
}

fn note_issue(id: &str, text: &str) -> Result<()> {
    crate::run_captured("vissue", &["note", id, text])?;
    Ok(())
}

/// Note the summary and mark the issue done. The close is the whole step.
pub fn close_settled(id: &str, summary: &str) -> Result<String> {
    note_issue(id, summary).with_context(|| format!("note {id}"))?;
    crate::run_captured("vissue", &["update", id, "-s", "DONE"])
        .with_context(|| format!("close {id}"))?;
    let mut out = format!("{id} closed as {summary}\n");
    out.push_str(&crate::persist_tracker(id, "settled with notes"));
    Ok(out)
}

/// File one `fix:` child for a blocking objection. The review of it is the
/// next panel round, not a second child.
pub fn file_fix(parent: &str, project: &str, objection: &Objection) -> Result<String> {
    let title = format!("fix: {}", clip(&objection.text, 120));
    let id = crate::workflow_child(parent, project, "task", crate::Stage::Implement, &title)?;
    let _ = note_issue(
        &id,
        &format!(
            "finding: {}: blocking: {} :: {} @ {}",
            objection.persona, objection.reason, objection.text, parent
        ),
    );
    Ok(id)
}

/// One issue in an export, enough to see a tree running away.
#[derive(Debug, Clone, PartialEq, Eq)]
struct Node {
    id: String,
    parent: Option<String>,
    title: String,
    open: bool,
    blocking: usize,
}

/// Lines for trees that have run away: more descendants than the budget,
/// or panel rounds that grew without a new blocking objection. The top of
/// the tree is named, not every child. `apply` closes each flagged issue
/// as settled-with-notes.
#[must_use]
pub fn reap_runaways(apply: bool) -> String {
    let Ok(said) = crate::run_captured("vissue", &["export"]) else {
        return String::new();
    };
    let budget = load_budget();
    let mut out = String::new();
    for (id, why) in flag_runaways(&said.stdout, &budget) {
        out.push_str(&format!("runaway: {id} {why}\n"));
        if !apply {
            continue;
        }
        let summary = format!("settled-with-notes: runaway, {why}");
        match close_settled(&id, &summary) {
            Ok(said) => out.push_str(&said),
            Err(e) => out.push_str(&format!("reap: {id}: close refused: {e:#}\n")),
        }
    }
    out
}

/// Runaway trees in a `vissue export` (one JSON issue a line).
#[must_use]
pub fn flag_runaways(export: &str, budget: &PanelBudget) -> Vec<(String, String)> {
    let nodes = nodes_of(export);
    let children = children_of(&nodes);
    let mut desc_memo: BTreeMap<String, usize> = BTreeMap::new();
    let mut round_memo: BTreeMap<String, usize> = BTreeMap::new();
    let mut block_memo: BTreeMap<String, usize> = BTreeMap::new();
    let mut flags = Vec::new();
    for node in &nodes {
        if !node.open {
            continue;
        }
        let limits = if is_workflow(&nodes, &node.id) {
            &budget.workflow
        } else {
            &budget.panel
        };
        let desc = descendants(&node.id, &children, &mut desc_memo, &mut BTreeSet::new());
        let parent_over = node.parent.as_ref().is_some_and(|p| {
            let parent_limits = if is_workflow(&nodes, p) {
                &budget.workflow
            } else {
                &budget.panel
            };
            descendants(p, &children, &mut desc_memo, &mut BTreeSet::new())
                > parent_limits.max_descendants
        });
        if desc > limits.max_descendants && !parent_over {
            flags.push((
                node.id.clone(),
                format!(
                    "has {desc} descendants, over the limit of {}",
                    limits.max_descendants
                ),
            ));
        }
        let round = max_round(
            &node.id,
            &nodes,
            &children,
            &mut round_memo,
            &mut BTreeSet::new(),
        );
        let blocking = blocking_under(
            &node.id,
            &nodes,
            &children,
            &mut block_memo,
            &mut BTreeSet::new(),
        );
        let grew = round >= 2 && round > blocking + 1;
        let parent_grew = node.parent.as_ref().is_some_and(|p| {
            let parent_round =
                max_round(p, &nodes, &children, &mut round_memo, &mut BTreeSet::new());
            let parent_blocking =
                blocking_under(p, &nodes, &children, &mut block_memo, &mut BTreeSet::new());
            parent_round >= round && parent_round >= 2 && parent_round > parent_blocking + 1
        });
        if grew && !parent_grew {
            flags.push((
                node.id.clone(),
                format!(
                    "reached round {round} with {blocking} blocking {}; rounds grew without a new blocking objection",
                    if blocking == 1 { "objection" } else { "objections" }
                ),
            ));
        }
    }
    flags
}

fn is_workflow(nodes: &[Node], id: &str) -> bool {
    nodes.iter().any(|n| {
        n.id == id
            && (n.title.contains("workflow")
                || n.title.starts_with("design:")
                || n.title.starts_with("panel r"))
    }) || nodes
        .iter()
        .any(|n| n.parent.as_deref() == Some(id) && crate::panel_round_number(&n.title).is_some())
}

fn nodes_of(export: &str) -> Vec<Node> {
    let mut nodes = Vec::new();
    for line in export.lines() {
        let Ok(v) = serde_json::from_str::<Value>(line) else {
            continue;
        };
        let Some(id) = v["id"].as_str().filter(|s| !s.is_empty()) else {
            continue;
        };
        let state = v["state"].as_str().unwrap_or("");
        let open = !matches!(state, "DONE" | "CANCELLED");
        let blocking = objections_in(&v, id)
            .iter()
            .filter(|o| o.files_as_child())
            .count();
        nodes.push(Node {
            id: id.to_string(),
            parent: v["parent"]
                .as_str()
                .filter(|s| !s.is_empty())
                .map(str::to_string),
            title: v["title"].as_str().unwrap_or("").to_string(),
            open,
            blocking,
        });
    }
    nodes
}

fn children_of(nodes: &[Node]) -> BTreeMap<String, Vec<String>> {
    let mut map: BTreeMap<String, Vec<String>> = BTreeMap::new();
    for node in nodes {
        if let Some(parent) = &node.parent {
            map.entry(parent.clone()).or_default().push(node.id.clone());
        }
    }
    map
}

fn descendants(
    id: &str,
    children: &BTreeMap<String, Vec<String>>,
    memo: &mut BTreeMap<String, usize>,
    stack: &mut BTreeSet<String>,
) -> usize {
    if let Some(n) = memo.get(id) {
        return *n;
    }
    if !stack.insert(id.to_string()) {
        return 0;
    }
    let n = children.get(id).map_or(0, |kids| {
        kids.iter()
            .map(|kid| 1 + descendants(kid, children, memo, stack))
            .sum()
    });
    stack.remove(id);
    memo.insert(id.to_string(), n);
    n
}

fn max_round(
    id: &str,
    nodes: &[Node],
    children: &BTreeMap<String, Vec<String>>,
    memo: &mut BTreeMap<String, usize>,
    stack: &mut BTreeSet<String>,
) -> usize {
    if let Some(n) = memo.get(id) {
        return *n;
    }
    if !stack.insert(id.to_string()) {
        return 0;
    }
    let own = nodes
        .iter()
        .find(|n| n.id == id)
        .map(|n| crate::panel_round_number(&n.title).unwrap_or(0))
        .unwrap_or(0);
    let below = children.get(id).map_or(0, |kids| {
        kids.iter()
            .map(|kid| max_round(kid, nodes, children, memo, stack))
            .max()
            .unwrap_or(0)
    });
    stack.remove(id);
    let n = own.max(below);
    memo.insert(id.to_string(), n);
    n
}

fn blocking_under(
    id: &str,
    nodes: &[Node],
    children: &BTreeMap<String, Vec<String>>,
    memo: &mut BTreeMap<String, usize>,
    stack: &mut BTreeSet<String>,
) -> usize {
    if let Some(n) = memo.get(id) {
        return *n;
    }
    if !stack.insert(id.to_string()) {
        return 0;
    }
    let own = nodes
        .iter()
        .find(|n| n.id == id)
        .map(|n| n.blocking)
        .unwrap_or(0);
    let below: usize = children.get(id).map_or(0, |kids| {
        kids.iter()
            .map(|kid| blocking_under(kid, nodes, children, memo, stack))
            .sum()
    });
    stack.remove(id);
    let n = own + below;
    memo.insert(id.to_string(), n);
    n
}

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

    fn nit(n: usize) -> Objection {
        Objection::nit("ceo", &format!("comma in note {n}"), "anneal-z64x")
    }

    #[test]
    fn a_blocking_mark_without_a_reason_is_a_nit() {
        let line = "finding: tester: blocking: the comma is wrong";
        let got = parse_objection(line, "anneal-z64x").unwrap();
        assert_eq!(got.severity, Severity::Nit);
        assert!(!got.files_as_child());
        let marked = parse_objection(
            "finding: tester: blocking: the exit status is dropped :: the gate ignores a failure @ anneal-z64x",
            "other",
        )
        .unwrap();
        assert!(marked.files_as_child());
        assert_eq!(marked.reason, "the exit status is dropped");
        assert_eq!(marked.text, "the gate ignores a failure");
        assert_eq!(marked.target, "anneal-z64x");
        let plain = parse_objection("finding: cto: heading tone", "anneal-z64x").unwrap();
        assert_eq!(plain.severity, Severity::Nit);
        assert_eq!(plain.text, "heading tone");
        let wording =
            parse_objection("finding: cto: wording: heading tone", "anneal-z64x").unwrap();
        assert_eq!(wording.severity, Severity::Wording);
        assert!(parse_objection("not a finding", "x").is_none());
    }

    #[test]
    fn the_same_text_and_target_are_one_objection() {
        let a = Objection::nit("ceo", "Heading, tone!", "anneal-z64x");
        let b = Objection::wording("cto", "heading tone", "Anneal-z64x");
        assert_eq!(a.key(), b.key());
        let other = Objection::nit("ceo", "heading tone", "anneal-other");
        assert_ne!(a.key(), other.key());
    }

    #[test]
    fn an_endless_stream_of_nits_after_a_pass_files_nothing() {
        let limits = ScopeBudget::panel();
        let mut state = PanelState::at_round(1);
        state.passed = true;
        let mut filed = 0;
        let mut opened = 0;
        for n in 0..10 {
            state.objections.push(nit(n));
            state
                .objections
                .push(Objection::wording("cto", "heading tone", "anneal-z64x"));
            state.objections.push(Objection::style(
                "junior-dev",
                "whitespace in the comment",
                "anneal-z64x",
            ));
            match plan_round(&state, &limits) {
                RoundPlan::FileBlocking { objections } => filed += objections.len(),
                RoundPlan::OpenRound { n } => {
                    opened += 1;
                    state.round = n;
                }
                RoundPlan::Settle { summary } => {
                    panic!("a pass does not need a close to stop: {summary}");
                }
                RoundPlan::Passed => {}
                RoundPlan::Hold { why } => panic!("a pass does not hold: {why}"),
            }
        }
        assert_eq!(filed, 0, "nits after a pass are notes");
        assert_eq!(opened, 0, "a pass does not open another round");
        assert!(state.round <= limits.max_rounds);

        // The anneal as it actually ran: revise votes on wording, so the
        // round does not read as passed, and the agent asks for another.
        let mut state = PanelState::at_round(1);
        let mut filed = 0;
        let mut opened = 0;
        let mut settled = 0;
        let mut last = String::new();
        for n in 0..10 {
            state.objections.push(nit(n));
            state
                .objections
                .push(Objection::wording("cto", "heading tone", "anneal-z64x"));
            match plan_round(&state, &limits) {
                RoundPlan::FileBlocking { objections } => filed += objections.len(),
                RoundPlan::OpenRound { n } => {
                    opened += 1;
                    state.round = n;
                }
                RoundPlan::Settle { summary } => {
                    settled += 1;
                    last = summary;
                }
                RoundPlan::Passed | RoundPlan::Hold { .. } => {}
            }
        }
        assert_eq!(filed, 0);
        assert_eq!(opened, 0);
        assert!(settled >= 1);
        assert!(state.round <= limits.max_rounds);
        assert!(last.contains("settled-with-notes"), "{last}");
        assert!(last.contains("comma in note 0"), "{last}");
        assert!(last.contains("heading tone"), "{last}");
        // The repeated wording is one objection, not one per round.
        assert_eq!(last.matches("heading tone").count(), 1, "{last}");
    }

    #[test]
    fn a_blocking_dissent_files_a_child_and_a_nit_does_not_file_a_grandchild() {
        let limits = ScopeBudget::panel();
        let mut state = PanelState::at_round(1);
        state.passed = true;
        state.objections.push(Objection::blocking(
            "tester",
            "the gate drops a real failure",
            "the exit status is ignored",
            "anneal-z64x",
        ));
        state
            .objections
            .push(Objection::nit("ceo", "comma in note 0", "anneal-z64x"));
        let plan = plan_round(&state, &limits);
        let RoundPlan::FileBlocking { objections } = plan else {
            panic!("a blocking dissent is a child: {plan:?}");
        };
        assert_eq!(objections.len(), 1);
        assert!(objections[0].text.contains("exit status"));

        let mut child = PanelState::at_round(1);
        child.dissent_depth = 1;
        child.passed = true;
        child
            .objections
            .push(Objection::nit("ceo", "rename the local", "anneal-child"));
        child
            .objections
            .push(Objection::wording("cto", "heading tone", "anneal-child"));
        match plan_round(&child, &limits) {
            RoundPlan::Passed | RoundPlan::Settle { .. } => {}
            other => panic!("a dissent child does not file a nit grandchild: {other:?}"),
        }
        let mut deeper = child.clone();
        deeper.passed = true;
        deeper.objections.push(Objection::blocking(
            "tester",
            "the child still drops the status",
            "the wait is unchecked",
            "anneal-child",
        ));
        match plan_round(&deeper, &limits) {
            RoundPlan::FileBlocking { objections } => {
                assert_eq!(objections.len(), 1);
                assert!(objections[0].text.contains("unchecked"));
            }
            other => panic!("a blocking objection on a dissent child is still a child: {other:?}"),
        }
    }

    #[test]
    fn new_blocking_objections_stop_at_the_round_limit() {
        let limits = ScopeBudget::panel();
        let mut state = PanelState::at_round(1);
        let mut filed = Vec::new();
        for n in 0..6 {
            state.objections.push(Objection::blocking(
                "tester",
                &format!("failure {n} escapes"),
                &format!("bug {n} is still open"),
                "anneal-z64x",
            ));
            state.open_children = 0;
            match plan_round(&state, &limits) {
                RoundPlan::FileBlocking { objections } => {
                    assert_eq!(objections.len(), 1, "{objections:?}");
                    let objection = &objections[0];
                    filed.push(objection.text.clone());
                    state.seen.push(objection.key());
                    state.children_filed += 1;
                    match plan_round(&state, &limits) {
                        RoundPlan::OpenRound { n } => state.round = n,
                        RoundPlan::Settle { summary } => {
                            assert!(summary.contains("settled-with-notes"), "{summary}");
                            break;
                        }
                        other => panic!("after the fix closed: {other:?}"),
                    }
                }
                RoundPlan::Settle { summary } => {
                    assert!(summary.contains("settled-with-notes"), "{summary}");
                    assert!(summary.contains("bug"), "{summary}");
                    break;
                }
                other => panic!("step {n}: {other:?}"),
            }
        }
        assert!(state.round <= limits.max_rounds, "round {}", state.round);
        assert!(filed.len() < 6, "filed without a cap: {filed:?}");
        assert!(filed.iter().any(|text| text.contains("bug 0")), "{filed:?}");
        assert!(
            !filed.iter().any(|text| text.contains("bug 5")),
            "the later bugs are listed, not filed: {filed:?}"
        );
    }

    #[test]
    fn a_wall_budget_closes_without_filing() {
        let mut limits = ScopeBudget::panel();
        limits.max_wall_secs = Some(60);
        let mut state = PanelState::at_round(1);
        state.wall_secs = Some(120);
        state.objections.push(Objection::blocking(
            "tester",
            "a real failure",
            "the exit status is ignored",
            "anneal-z64x",
        ));
        match plan_round(&state, &limits) {
            RoundPlan::Settle { summary } => {
                assert!(summary.contains("wall time"), "{summary}");
                assert!(summary.contains("exit status"), "{summary}");
            }
            other => panic!("over the wall budget the objection is listed: {other:?}"),
        }
    }

    #[test]
    fn a_settled_majority_names_only_the_minority() {
        let majority = vec![
            ("alice".to_string(), "drop".to_string()),
            ("bob".to_string(), "drop".to_string()),
            ("keeper".to_string(), "keep".to_string()),
        ];
        assert_eq!(minority_options(&majority), vec!["keep".to_string()]);
        let tie = vec![
            ("alice".to_string(), "yes".to_string()),
            ("bob".to_string(), "no".to_string()),
        ];
        assert!(minority_options(&tie).is_empty());
        let unanimous = vec![
            ("alice".to_string(), "ship".to_string()),
            ("bob".to_string(), "ship".to_string()),
        ];
        assert!(minority_options(&unanimous).is_empty());
    }

    #[test]
    fn a_duplicate_is_not_filed_again() {
        let limits = ScopeBudget::panel();
        let mut state = PanelState::at_round(1);
        let objection = Objection::blocking(
            "tester",
            "the gate drops a real failure",
            "the exit status is ignored",
            "anneal-z64x",
        );
        state.seen.push(objection.key());
        state.objections.push(objection);
        state.objections.push(Objection::blocking(
            "cto",
            "the gate drops a real failure",
            "The exit status is ignored.",
            "anneal-z64x",
        ));
        let disposal = dispose(&state, &limits);
        assert!(disposal
            .fates
            .iter()
            .all(|f| matches!(f, Fate::Duplicate(_))));
        assert!(!disposal.close);
    }

    #[test]
    fn toml_overlays_the_defaults_and_leaves_the_other_scope() {
        let budget = budget_from_toml(
            "[panel]\nmax_rounds = 2\nmax_tokens = 1000\n[workflow]\nmax_children = 4\n",
        );
        assert_eq!(budget.panel.max_rounds, 2);
        assert_eq!(budget.panel.max_children, ScopeBudget::panel().max_children);
        assert_eq!(budget.panel.max_tokens, Some(1000));
        assert_eq!(budget.workflow.max_children, 4);
        assert_eq!(budget.workflow.max_rounds, 3);
        assert_eq!(budget_from_toml("not toml"), PanelBudget::default());
    }

    #[test]
    fn reap_flags_a_wide_tree_and_rounds_with_no_new_blocking_objection() {
        let budget = PanelBudget::default();
        let mut lines = Vec::new();
        lines.push(
            r#"{"id":"anneal-z64x","title":"anneal the notes","state":"STARTED","parent":""}"#
                .to_string(),
        );
        for n in 1..=10 {
            lines.push(format!(
                r#"{{"id":"anneal-r{n}","title":"panel r{n}: anneal","state":"DONE","parent":"anneal-z64x"}}"#
            ));
        }
        for n in 0..60 {
            lines.push(format!(
                r#"{{"id":"anneal-c{n}","title":"fix: comma {n}","state":"TODO","parent":"anneal-z64x"}}"#
            ));
        }
        // 70 descendants and round 10, with no blocking note.
        let flags = flag_runaways(&lines.join("\n"), &budget);
        let ids: Vec<&str> = flags.iter().map(|(id, _)| id.as_str()).collect();
        assert!(ids.contains(&"anneal-z64x"), "{flags:?}");
        let why = flags
            .iter()
            .filter(|(id, _)| id == "anneal-z64x")
            .map(|(_, why)| why.as_str())
            .collect::<Vec<_>>()
            .join(" ");
        assert!(why.contains("descendants"), "{why}");
        assert!(why.contains("round 10"), "{why}");
        assert!(why.contains("without a new blocking"), "{why}");

        // One blocking objection justifies a second round. A third does not
        // when nothing new is blocking.
        let small = [
            r#"{"id":"p","title":"widget","state":"STARTED"}"#,
            r#"{"id":"p-r1","title":"panel r1: widget","state":"DONE","parent":"p","logbook":[{"note":"finding: tester: blocking: drops a failure :: exit status @ p"}]}"#,
            r#"{"id":"p-r2","title":"panel r2: widget","state":"DONE","parent":"p"}"#,
        ]
        .join("\n");
        let flags = flag_runaways(&small, &budget);
        assert!(
            flags.iter().all(|(id, _)| id != "p"),
            "round 2 with one blocking objection is the fix, not a runaway: {flags:?}"
        );
        let wide_round = [
            r#"{"id":"p","title":"widget","state":"STARTED"}"#,
            r#"{"id":"p-r1","title":"panel r1: widget","state":"DONE","parent":"p"}"#,
            r#"{"id":"p-r2","title":"panel r2: widget","state":"DONE","parent":"p"}"#,
            r#"{"id":"p-r3","title":"panel r3: widget","state":"DONE","parent":"p"}"#,
        ]
        .join("\n");
        let flags = flag_runaways(&wide_round, &budget);
        assert!(
            flags
                .iter()
                .any(|(id, why)| id == "p" && why.contains("round 3")),
            "{flags:?}"
        );
    }
}