hird 0.3.0

Cross-harness agent work queue and shared assertion memory, backed by one local SQLite database
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
//! Core domain types: task status machine, tasks, events and assertions.
//!
//! The status machine in [`Status::apply`] is the single authority for which
//! task transitions exist; the repository layer refuses anything it rejects.

use std::fmt;
use std::str::FromStr;

use chrono::{DateTime, SecondsFormat, Utc};
use serde::{Deserialize, Serialize};

/// Format a timestamp the way every column in the database stores it.
///
/// Fixed-width RFC 3339 in UTC with millisecond precision, so lexicographic
/// ordering matches chronological ordering and `lease_expires_at < :now`
/// comparisons work as plain string comparisons in SQL.
pub fn fmt_ts(at: DateTime<Utc>) -> String {
    at.to_rfc3339_opts(SecondsFormat::Millis, true)
}

/// The current time, formatted for storage.
pub fn now_ts() -> String {
    fmt_ts(Utc::now())
}

/// Parse a timestamp previously written by [`fmt_ts`].
pub fn parse_ts(raw: &str) -> Option<DateTime<Utc>> {
    DateTime::parse_from_rfc3339(raw)
        .ok()
        .map(|dt| dt.with_timezone(&Utc))
}

/// Where a task sits in its lifecycle.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, PartialOrd, Ord, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum Status {
    Open,
    Claimed,
    InProgress,
    Done,
    Failed,
    Cancelled,
}

impl Status {
    /// Every status, for exhaustive iteration in tests and TUI filters.
    pub const ALL: [Status; 6] = [
        Status::Open,
        Status::Claimed,
        Status::InProgress,
        Status::Done,
        Status::Failed,
        Status::Cancelled,
    ];

    pub fn as_str(self) -> &'static str {
        match self {
            Status::Open => "open",
            Status::Claimed => "claimed",
            Status::InProgress => "in_progress",
            Status::Done => "done",
            Status::Failed => "failed",
            Status::Cancelled => "cancelled",
        }
    }

    /// Terminal statuses can only leave via `reopen` — a human's, or the one a
    /// review's `sent_back` verdict performs on the work it judged.
    pub fn is_terminal(self) -> bool {
        matches!(self, Status::Done | Status::Failed | Status::Cancelled)
    }

    /// Statuses that hold a lease.
    pub fn is_active(self) -> bool {
        matches!(self, Status::Claimed | Status::InProgress)
    }

    /// Apply a transition, returning the resulting status.
    ///
    /// `None` means the transition is not part of the status machine and must
    /// be rejected. This is the whole diagram, in one place:
    ///
    /// ```text
    /// open ──claim──► claimed ──start──► in_progress ──complete──► done
    ///   ▲                │                    │        └─fail────► failed
    ///   │                └── lease expiry ────┘
    ///   │                └──── release ───────┘
    ///   └── reopen (human, or a review's sent_back) ◄── done|failed|cancelled
    /// open ──cancel (human)──► cancelled
    /// ```
    ///
    /// `release` is the agent-side counterpart of a lease expiry: an agent
    /// that cannot finish a task hands it straight back rather than making
    /// everyone wait out the lease, and without the `failed` state a human
    /// would have to clear by hand.
    pub fn apply(self, transition: Transition) -> Option<Status> {
        use Status::*;
        use Transition::*;
        match (self, transition) {
            (Open, Claim) => Some(Claimed),
            // `start` is idempotent so an agent may repeat `task_update
            // status=in_progress` purely to renew its lease.
            (Claimed, Start) | (InProgress, Start) => Some(InProgress),
            (Claimed | InProgress, Complete) => Some(Done),
            (Claimed | InProgress, Fail) => Some(Failed),
            (Claimed | InProgress, LeaseExpiry | Release) => Some(Open),
            (Open | Claimed | InProgress, Cancel) => Some(Cancelled),
            (Done | Failed | Cancelled, Reopen) => Some(Open),
            _ => None,
        }
    }
}

impl fmt::Display for Status {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        // `pad` rather than `write_str` so `{status:<11}` lines columns up.
        f.pad(self.as_str())
    }
}

impl FromStr for Status {
    type Err = UnknownStatus;

    fn from_str(s: &str) -> Result<Self, Self::Err> {
        match s {
            "open" => Ok(Status::Open),
            "claimed" => Ok(Status::Claimed),
            "in_progress" | "in-progress" => Ok(Status::InProgress),
            "done" => Ok(Status::Done),
            "failed" => Ok(Status::Failed),
            "cancelled" | "canceled" => Ok(Status::Cancelled),
            other => Err(UnknownStatus(other.to_string())),
        }
    }
}

#[derive(Debug, Clone, thiserror::Error)]
#[error(
    "unknown status {0:?} (expected one of: open, claimed, in_progress, done, failed, cancelled)"
)]
pub struct UnknownStatus(pub String);

/// An edge in the status machine.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum Transition {
    /// Agent takes an `open` task.
    Claim,
    /// Agent reports it has started working.
    Start,
    /// Lease holder finishes successfully.
    Complete,
    /// Lease holder gives up.
    Fail,
    /// Lease ran out and the task returns to the pool.
    LeaseExpiry,
    /// Lease holder hands the task back unfinished.
    Release,
    /// Human abandons the task.
    Cancel,
    /// Human puts a terminal task back in the pool.
    Reopen,
}

impl Transition {
    /// Every transition, for exhaustive iteration in tests.
    pub const ALL: [Transition; 8] = [
        Transition::Claim,
        Transition::Start,
        Transition::Complete,
        Transition::Fail,
        Transition::LeaseExpiry,
        Transition::Release,
        Transition::Cancel,
        Transition::Reopen,
    ];

    pub fn as_str(self) -> &'static str {
        match self {
            Transition::Claim => "claim",
            Transition::Start => "start",
            Transition::Complete => "complete",
            Transition::Fail => "fail",
            Transition::LeaseExpiry => "lease_expiry",
            Transition::Release => "release",
            Transition::Cancel => "cancel",
            Transition::Reopen => "reopen",
        }
    }
}

impl fmt::Display for Transition {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.pad(self.as_str())
    }
}

/// The kind of an entry in the append-only `task_events` trail.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum EventKind {
    Created,
    Claimed,
    Status,
    Note,
    LeaseRenewed,
    LeaseExpired,
    /// The holder handed the task back unfinished.
    Released,
    /// The holder released the task because it needs an answer from outside
    /// the agent queue before any agent can continue it.
    Asked,
    /// A human supplied the answer that makes an asking task claimable again.
    Answered,
    Completed,
    Failed,
    Cancelled,
    Reopened,
    /// A dependency on another task was added or removed.
    DepAdded,
    DepRemoved,
    /// The task's declared file scope changed.
    Scoped,
    /// The capabilities a claimant must advertise changed.
    Required,
    /// The task was recused from another one's worker, or the bar was lifted.
    Recused,
    /// A review of this task's work was filed, or this task is that review.
    Reviewed,
    /// The witness saw the working tree change under this task.
    Witnessed,
    /// A finished task this one builds on stopped being finished — sent back
    /// by a review, reopened, cancelled or failed — while this task was held.
    GroundShifted,
    /// A gate outside the queue — CI — reported on this task's work.
    Reported,
    /// The work is in: a merge was recorded, with what proves it.
    Merged,
    /// The task's branch, worktree or pull request link was set or cleared.
    Linked,
}

impl EventKind {
    pub fn as_str(self) -> &'static str {
        match self {
            EventKind::Created => "created",
            EventKind::Claimed => "claimed",
            EventKind::Status => "status",
            EventKind::Note => "note",
            EventKind::LeaseRenewed => "lease_renewed",
            EventKind::LeaseExpired => "lease_expired",
            EventKind::Released => "released",
            EventKind::Asked => "asked",
            EventKind::Answered => "answered",
            EventKind::Completed => "completed",
            EventKind::Failed => "failed",
            EventKind::Cancelled => "cancelled",
            EventKind::Reopened => "reopened",
            EventKind::DepAdded => "dep_added",
            EventKind::DepRemoved => "dep_removed",
            EventKind::Scoped => "scoped",
            EventKind::Required => "required",
            EventKind::Recused => "recused",
            EventKind::Reviewed => "reviewed",
            EventKind::Witnessed => "witnessed",
            EventKind::GroundShifted => "ground_shifted",
            EventKind::Reported => "reported",
            EventKind::Merged => "merged",
            EventKind::Linked => "linked",
        }
    }
}

impl fmt::Display for EventKind {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.pad(self.as_str())
    }
}

impl FromStr for EventKind {
    type Err = String;

    fn from_str(s: &str) -> Result<Self, Self::Err> {
        match s {
            "created" => Ok(EventKind::Created),
            "claimed" => Ok(EventKind::Claimed),
            "status" => Ok(EventKind::Status),
            "note" => Ok(EventKind::Note),
            "lease_renewed" => Ok(EventKind::LeaseRenewed),
            "lease_expired" => Ok(EventKind::LeaseExpired),
            "released" => Ok(EventKind::Released),
            "asked" => Ok(EventKind::Asked),
            "answered" => Ok(EventKind::Answered),
            "completed" => Ok(EventKind::Completed),
            "failed" => Ok(EventKind::Failed),
            "cancelled" => Ok(EventKind::Cancelled),
            "reopened" => Ok(EventKind::Reopened),
            "dep_added" => Ok(EventKind::DepAdded),
            "dep_removed" => Ok(EventKind::DepRemoved),
            "scoped" => Ok(EventKind::Scoped),
            "required" => Ok(EventKind::Required),
            "recused" => Ok(EventKind::Recused),
            "reviewed" => Ok(EventKind::Reviewed),
            "witnessed" => Ok(EventKind::Witnessed),
            "ground_shifted" => Ok(EventKind::GroundShifted),
            "reported" => Ok(EventKind::Reported),
            "merged" => Ok(EventKind::Merged),
            "linked" => Ok(EventKind::Linked),
            other => Err(format!("unknown event kind {other:?}")),
        }
    }
}

/// A unit of work, as stored.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct Task {
    pub id: String,
    pub seq: i64,
    pub project: String,
    pub title: String,
    pub body: String,
    pub status: Status,
    pub priority: i64,
    pub claimed_by: Option<String>,
    pub lease_expires_at: Option<String>,
    pub result: Option<String>,
    /// Whether finishing this task should put its work in front of another
    /// harness. See [`Recusal`] and DESIGN.md §15.
    pub review: bool,
    pub created_at: String,
    pub updated_at: String,
}

impl Task {
    /// Whole seconds left on the lease relative to `now`, if a lease is held.
    ///
    /// Negative once the lease is due for sweeping.
    pub fn lease_remaining_secs(&self, now: DateTime<Utc>) -> Option<i64> {
        let expires = parse_ts(self.lease_expires_at.as_deref()?)?;
        Some((expires - now).num_seconds())
    }
}

/// The projection used by list views, which never need the task body.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct TaskSummary {
    pub seq: i64,
    pub project: String,
    pub title: String,
    pub status: Status,
    pub priority: i64,
    pub claimed_by: Option<String>,
    pub lease_expires_at: Option<String>,
    pub updated_at: String,
    /// Capabilities a claimant must advertise before this task is workable.
    pub requirements: Vec<String>,
}

/// One entry in a task's audit trail.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct TaskEvent {
    pub id: String,
    pub task_id: String,
    pub at: String,
    pub actor: String,
    pub kind: EventKind,
    pub detail: String,
}

/// Where a task's work lives: the branch it is built on, the worktree it is
/// built in, the pull request that carries it. Each is absent until someone
/// sets it; the trail keeps every earlier value as a `linked` event.
#[derive(Debug, Clone, Default, PartialEq, Eq, Serialize, Deserialize)]
pub struct TaskLinks {
    #[serde(skip_serializing_if = "Option::is_none")]
    pub branch: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub worktree: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub pr_url: Option<String>,
    /// Who last changed them, and when.
    pub updated_by: String,
    pub at: String,
}

impl TaskLinks {
    /// Whether every link has been cleared.
    pub fn is_empty(&self) -> bool {
        self.branch.is_none() && self.worktree.is_none() && self.pr_url.is_none()
    }
}

/// One question a holder asked before releasing a task.
///
/// An unanswered row is a derived readiness gate: the task remains `open`,
/// preserving the status machine, but no agent can claim it until the human
/// path fills the answer in. Earlier answered rows remain as handoff context
/// for every later claim.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct Question {
    pub id: String,
    pub task_id: String,
    pub n: i64,
    pub asked_by: String,
    pub question: String,
    pub asked_at: String,
    pub answer: Option<String>,
    pub answered_by: Option<String>,
    pub answered_at: Option<String>,
}

impl Question {
    pub fn is_answered(&self) -> bool {
        self.answer.is_some()
    }
}

/// What it takes for a dependency to clear its dependents.
///
/// `done` was the whole answer until v1.7 made `done` revocable: a review can
/// send finished work back, and a dependent claimed in the meantime is building
/// on ground that may be pulled out from under it. Whether that possibility
/// holds the dependent back is policy, not fact, so it is configuration
/// (`under_review`) rather than code.
#[derive(Debug, Clone, Copy, Default, PartialEq, Eq)]
pub enum Clearance {
    /// `done` clears a dependency, reviewed or not. The dependent is told the
    /// ground is provisional rather than kept waiting.
    #[default]
    Done,
    /// A `done` dependency with an unfinished review keeps holding its
    /// dependents until the review delivers its verdict — upheld releases
    /// them, sent back reopens the work and they wait on that instead.
    Reviewed,
}

/// A task that must finish before another one can start.
///
/// Carries enough context to explain a refusal without a second lookup, which
/// is what makes the error message actionable to a model.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct Blocker {
    pub seq: i64,
    pub title: String,
    pub status: Status,
    /// The unfinished review of this blocker's work, where one exists. Only
    /// meaningful on a `done` blocker: it is what makes that `done`
    /// provisional rather than the last word.
    pub pending_review: Option<i64>,
}

impl Blocker {
    /// A blocker is cleared only by reaching `done`; a `failed` or `cancelled`
    /// dependency keeps the dependent task off the ready list, because the work
    /// it was waiting for did not happen. Under [`Clearance::Reviewed`], `done`
    /// under an unfinished review is not enough either — the verdict is still
    /// out, and a sent-back would take the ground away again.
    pub fn is_cleared(&self, clearance: Clearance) -> bool {
        self.status == Status::Done
            && (clearance == Clearance::Done || self.pending_review.is_none())
    }
}

/// One glob a task has declared it will touch.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct PathClaim {
    pub task_id: String,
    pub pattern: String,
    pub declared_by: String,
    pub at: String,
}

/// A task together with the file scope it has declared.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct ScopedTask {
    pub seq: i64,
    pub title: String,
    pub status: Status,
    pub holder: Option<String>,
    pub patterns: Vec<String>,
}

/// Two tasks whose declared file scopes can name the same path.
///
/// Reported whenever one of the two is being actively worked, which is the
/// only time an overlap can turn into a lost edit.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct Conflict {
    /// The pattern declared by the task being checked.
    pub pattern: String,
    /// The task already in the way.
    pub other_seq: i64,
    pub other_title: String,
    pub other_pattern: String,
    pub other_status: Status,
    pub other_holder: Option<String>,
}

impl Conflict {
    /// One sentence, aimed at a model that has to relay it to a human.
    pub fn describe(&self) -> String {
        match &self.other_holder {
            Some(holder) => format!(
                "{} overlaps {} on task {} ({}), held by {holder}",
                self.pattern,
                self.other_pattern,
                self.other_seq,
                truncate_title(&self.other_title),
            ),
            None => format!(
                "{} overlaps {} on task {} ({}, {})",
                self.pattern,
                self.other_pattern,
                self.other_seq,
                truncate_title(&self.other_title),
                self.other_status,
            ),
        }
    }
}

fn truncate_title(title: &str) -> String {
    crate::fmt::truncate(title, 40)
}

/// One finished dependency, handed over at claim time: what the work it
/// produced says for itself, and how far that word can be trusted.
///
/// The dependency edge always was a context channel — "do the schema before
/// the API" means the API needs to know what the schema turned out to be — but
/// until v1.9 it was read only as a gate. The gate opens, the blocker's
/// `result` is dropped on the floor, and the dependent's agent starts blind
/// unless file overlap happens to carry something across. This is the row that
/// closes that gap: the claimant is handed each blocker's own summary without
/// knowing to ask for it.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct Ground {
    pub seq: i64,
    pub title: String,
    /// The summary its finisher wrote. `None` only for work finished outside
    /// the normal path — the column is what `task_complete` requires.
    pub result: Option<String>,
    pub standing: GroundStanding,
}

/// How much weight a finished dependency can bear.
///
/// Deliberately an echo of memory's `Standing` (§14): both answer "this was
/// true when it was written — is it still?", one for assertions, one for the
/// work a task builds on.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum GroundStanding {
    /// Finished, with nothing further on record.
    Done,
    /// Finished and seen to be finished: the newest verdict on it, from a
    /// harness that provably did not do the work, upheld it.
    Upheld,
    /// Finished, but its review has not delivered a verdict — a sent-back
    /// would reopen this work, so whatever builds on it is building on a
    /// provisional answer.
    UnderReview { review: i64 },
}

impl GroundStanding {
    /// `done`, `upheld`, `under review 15, provisional` — the words every
    /// front end uses for it.
    pub fn describe(self) -> String {
        match self {
            GroundStanding::Done => "done".to_string(),
            GroundStanding::Upheld => "upheld".to_string(),
            GroundStanding::UnderReview { review } => {
                format!("under review {review}, provisional")
            }
        }
    }

    pub fn is_provisional(self) -> bool {
        matches!(self, GroundStanding::UnderReview { .. })
    }
}

impl Ground {
    /// `#3 done`, `#3 upheld`, `#3 under review 15, provisional` — the label
    /// every front end uses.
    pub fn label(&self) -> String {
        format!("#{} {}", self.seq, self.standing.describe())
    }
}

/// A dependency that stopped being `done` while its dependent was being
/// worked.
///
/// Readiness is checked at the claim and never again, which was sound while
/// `done` was final. A verdict can now reopen finished work (§16), and a human
/// could always cancel or reopen it; either way, an agent mid-task is building
/// on ground that has moved, and it is the one participant with no way to
/// notice. This is what its next check-in tells it.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct Shifted {
    pub seq: i64,
    pub title: String,
    pub status: Status,
    /// The review whose sent-back reopened it, when that is what happened and
    /// nothing has happened since.
    pub sent_back_by: Option<i64>,
}

impl Shifted {
    /// One sentence, aimed at a model that has to decide what to do about it.
    pub fn describe(&self) -> String {
        let name = format!("task {} ({})", self.seq, truncate_title(&self.title));
        match (self.sent_back_by, self.status) {
            (Some(review), _) => format!(
                "{name}, which this task builds on, was sent back by review {review} and \
                 reopened; re-read it — the findings are in its brief — before building \
                 further on its work"
            ),
            (None, Status::Cancelled) => format!(
                "{name}, which this task builds on, has been cancelled since this task \
                 was claimed"
            ),
            (None, Status::Failed) => {
                format!("{name}, which this task builds on, has failed since this task was claimed")
            }
            (None, _) => format!(
                "{name}, which this task builds on, was reopened since this task was \
                 claimed and is no longer done"
            ),
        }
    }
}

/// One file the witness saw change while a task was held.
///
/// Deliberately not called "a file the task changed": a shared checkout cannot
/// tell you who typed. What it *can* tell you, exactly, is that the file is not
/// the file it was when the task started, and when that stopped being true.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct Observed {
    /// Project-relative path.
    pub path: String,
    /// How it differs from the tree as it stood when the task was claimed:
    /// `added`, `modified` or `deleted`.
    pub kind: String,
    /// Content hash as of `last_seen`; empty when the file is gone.
    pub hash: String,
    pub first_seen: String,
    pub last_seen: String,
}

impl Observed {
    /// `src/config.rs (modified)`, the way every front end prints it.
    pub fn describe(&self) -> String {
        format!("{} ({})", self.path, self.kind)
    }
}

/// One earlier holding of a task: who had it, how that ended, and what the
/// witness saw move while they did.
///
/// Archived at the moment a fresh claim would otherwise overwrite the
/// evidence, which is the only moment there is anything to archive. The
/// current holding is never in here — `task_witness` and `task_changes` are
/// its record — so `n` counts finished holdings, in order.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct Tenure {
    /// 1-based round number, per task.
    pub n: i64,
    /// Who held the lease. Empty when the baseline predates the column.
    pub holder: String,
    pub began_at: String,
    /// How the holding ended: `completed`, `failed`, `released`,
    /// `lease_expired` or `cancelled`. Empty when the trail could not say.
    pub ended: String,
    pub ended_at: String,
    /// What moved while it was held, frozen at the moment of archiving.
    pub changes: Vec<Observed>,
}

impl Tenure {
    /// `codex:9f2c` or, for a row old enough to predate the record, a phrase.
    fn who(&self) -> &str {
        if self.holder.is_empty() {
            "an earlier holder"
        } else {
            &self.holder
        }
    }

    /// How the holding ended, as the middle of a sentence.
    fn ending(&self) -> &'static str {
        match self.ended.as_str() {
            "completed" => "completed it",
            "failed" => "failed it",
            "released" => "handed it back unfinished",
            "lease_expired" => "went quiet until the lease expired",
            "cancelled" => "held it when it was cancelled",
            _ => "held it before this claim",
        }
    }

    /// The paths that moved, capped so a long list stays a sentence.
    fn moved(&self) -> String {
        const LISTED: usize = 6;
        let mut listed: Vec<String> = self
            .changes
            .iter()
            .take(LISTED)
            .map(Observed::describe)
            .collect();
        if self.changes.len() > LISTED {
            listed.push(format!("and {} more", self.changes.len() - LISTED));
        }
        listed.join(", ")
    }

    /// The sentence a successor is handed on claiming: what happened to this
    /// task before it was theirs, and where to read the details.
    ///
    /// The wording is careful about what the record can back. The changes are
    /// what moved *while the previous holding was live* — whatever state they
    /// were left in is part of the tree the new baseline was just read off,
    /// so the successor inherits it silently unless somebody says so. This is
    /// somebody saying so.
    pub fn describe(&self, seq: i64) -> String {
        if self.changes.is_empty() {
            return format!(
                "{} {} without the tree moving — there are no leftover edits to inherit",
                self.who(),
                self.ending()
            );
        }
        format!(
            "{} {}, and these files moved while they held it: {}. Whatever state they \
             left is part of the tree this claim starts from — `hird diff {seq} --tenure \
             {}` shows that round's changes before you build on or over them",
            self.who(),
            self.ending(),
            self.moved(),
            self.n
        )
    }

    /// The compact form `hird show` prints, one line per finished holding.
    pub fn label(&self) -> String {
        let ended = match self.ended.as_str() {
            "" => "ended".to_string(),
            kind => kind.replace('_', " "),
        };
        if self.changes.is_empty() {
            format!("round {}: {} — {ended}; read-only", self.n, self.who())
        } else {
            format!(
                "round {}: {} — {ended}; saw {}",
                self.n,
                self.who(),
                self.moved()
            )
        }
    }
}

/// Whether a task left a mark on the working tree, or only read it.
///
/// Every other witness type answers "what moved?", and answers it with a list
/// that comes back empty in two entirely different situations: a task that read
/// the code and wrote nothing, and a task hird was never watching. Those are
/// opposite claims — one is evidence, the other is the absence of any — and a
/// board that prints nothing in both cases invites a reader to take the second
/// for the first. This is the type that keeps them apart, and the only thing
/// that lets any front end say "read-only" out loud.
///
/// It is the same evidence [`Observed`] is, under the same limit: one checkout
/// has no keyboards, so `Modified` says the tree moved while the task was held
/// and not that this task's agent is who moved it. Where another task's
/// footprint holds one of the same files, `shared` says so rather than letting
/// a count speak with more confidence than it has earned.
#[derive(Debug, Clone, Copy, Default, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum Footprint {
    /// No baseline was ever taken: the task has not been claimed under a
    /// witness, or the project is not a git checkout. hird has no opinion
    /// here, and "read-only" would be inventing one.
    #[default]
    Unwatched,
    /// A baseline was taken and the tree still matches it. Nothing moved while
    /// the task was held — or whatever moved was put back the way it was.
    ReadOnly,
    /// Files differ from the baseline the task was claimed against.
    Modified {
        /// How many paths differ.
        paths: usize,
        /// Whether another task in the project has one of them in its own
        /// footprint, which is to say it was live in that file at the same
        /// time and the change belongs to both records.
        shared: bool,
    },
}

impl Footprint {
    /// Whether the witness watched this task at all. `false` is the one case
    /// where nothing may be said either way.
    pub fn is_watched(self) -> bool {
        !matches!(self, Footprint::Unwatched)
    }

    /// A badge for a list — `read-only`, `modified 3 files` — or nothing at
    /// all where there is nothing to stand on.
    ///
    /// `live` is whether the task is still being worked, which is the
    /// difference between a verdict and a running total: a task that has not
    /// written anything yet has not finished not writing anything.
    pub fn label(self, live: bool) -> Option<String> {
        match self {
            Footprint::Unwatched => None,
            Footprint::ReadOnly if live => Some("read-only so far".to_string()),
            Footprint::ReadOnly => Some("read-only".to_string()),
            Footprint::Modified { paths, .. } => Some(format!(
                "modified {paths} file{}",
                if paths == 1 { "" } else { "s" }
            )),
        }
    }

    /// The compact form, for a board card whose whole width is a couple of
    /// dozen columns: `read-only`, `modified 3`.
    ///
    /// It drops the hedge [`Footprint::label`] carries for a task still being
    /// worked. Everything on a live board is a running total — the status, the
    /// lease, the count of open tasks — and a card with room for one badge
    /// should spend it on the fact rather than on saying so twice.
    pub fn badge(self) -> Option<String> {
        match self {
            Footprint::Unwatched => None,
            Footprint::ReadOnly => Some("read-only".to_string()),
            Footprint::Modified { paths, .. } => Some(format!("modified {paths}")),
        }
    }

    /// The same thing as a sentence, carrying the caveat where there is one.
    pub fn describe(self, live: bool) -> Option<String> {
        let label = self.label(live)?;
        Some(match self {
            Footprint::ReadOnly if live => {
                format!("{label} — nothing in the working tree has moved since it was claimed")
            }
            Footprint::ReadOnly => {
                format!("{label} — nothing in the working tree moved while it was held")
            }
            Footprint::Modified { shared: true, .. } => {
                format!("{label}, though another agent was live in some of them")
            }
            _ => label,
        })
    }
}

/// A task together with what the witness saw happen under it.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct WitnessedTask {
    pub seq: i64,
    pub title: String,
    pub status: Status,
    pub holder: Option<String>,
    pub changes: Vec<Observed>,
}

/// One file that moved while two tasks were both being worked.
///
/// This is the observed counterpart of [`Conflict`], and it is a stronger
/// statement: a conflict says two agents *might* end up in the same file, while
/// a contention says the file on disk changed during a window in which two
/// agents were both live in it. Whoever read it first is holding a copy that no
/// longer matches, and a write from that copy silently discards the other's
/// work — which is the failure the whole file-scope apparatus exists to catch,
/// finally visible rather than predicted.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct Contention {
    pub path: String,
    /// Content hash and time this task's side was last confirmed at.
    pub hash: String,
    pub last_seen: String,
    pub other_seq: i64,
    pub other_title: String,
    pub other_status: Status,
    pub other_holder: Option<String>,
    pub other_hash: String,
    pub other_last_seen: String,
}

impl Contention {
    /// Do the two sides disagree about what the file says?
    ///
    /// Each records the version it was last shown, so disagreement means one of
    /// them is behind. Agreement means nobody is, which is why a contention
    /// where this is false is never reported: two agents up to date on the same
    /// file have nothing to act on, and a warning nobody needs to act on is a
    /// warning that gets ignored the once it matters. What is left is the
    /// declared overlap, which the board already shows.
    pub fn is_stale(&self) -> bool {
        self.hash != self.other_hash
    }

    /// One sentence, aimed at a model that has to relay it to a human.
    ///
    /// It says what is certainly true and what to do about it, and stops there:
    /// hird watched a file change, it did not watch anybody type.
    pub fn describe(&self) -> String {
        let who = match &self.other_holder {
            Some(holder) => format!(
                "task {} ({}), held by {holder}",
                self.other_seq,
                truncate_title(&self.other_title)
            ),
            None => format!(
                "task {} ({}, {})",
                self.other_seq,
                truncate_title(&self.other_title),
                self.other_status
            ),
        };
        // Only a strictly later confirmation on this side lets us say the
        // other agent is the one holding the old copy. Anything else — theirs
        // later, or the two too close together to order — is answered with the
        // warning, because being told to re-read a file you already have is
        // cheap and losing an edit is not.
        if self.last_seen > self.other_last_seen {
            format!(
                "{} changed under {who}, and again on your side afterwards — check you did \
                 not write over their edit",
                self.path
            )
        } else {
            format!(
                "{} changed under {who} at {}, at or after the version hird last confirmed \
                 for you — re-read it before you write, or your edit will discard theirs",
                self.path,
                short_time(&self.other_last_seen),
            )
        }
    }
}

/// `2026-07-25T14:32:07.001Z` → `14:32`, for a sentence rather than a log line.
fn short_time(ts: &str) -> String {
    match parse_ts(ts) {
        Some(dt) => dt.format("%H:%M UTC").to_string(),
        None => ts.to_string(),
    }
}

/// One rejected attempt at a task: whose work, who rejected it, and what
/// they said. Handed to the next claimant so it starts where the last one was
/// stopped, not where the last one started.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct Attempt {
    pub n: i64,
    pub holder: String,
    #[serde(skip_serializing_if = "String::is_empty", default)]
    pub model: String,
    /// `sent_back`, `ci_failed`, `failed` or `human_rejected`.
    pub outcome: String,
    pub source: String,
    pub actor: String,
    pub output: String,
    pub at: String,
}

/// A task whose worker is barred from working another one.
///
/// Carries who that is, so a refusal can name them without a second lookup —
/// the same reason [`Blocker`] carries a title.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct Recusal {
    pub from_seq: i64,
    pub from_title: String,
    /// Why the bar exists, in the words of whoever filed it.
    pub reason: String,
    /// Whoever last held the task being recused from, if anybody has. `None`
    /// means the work has not been done yet, so the recusal bars nobody — a
    /// constraint waiting for something to constrain.
    pub worker: Option<String>,
    /// Everybody else who ever held the task being recused from, oldest
    /// first. A lease that lapsed or work that was released and finished by
    /// somebody else still leaves its first author barred: having written
    /// some of the work is enough to be the wrong reader of it.
    #[serde(default, skip_serializing_if = "Vec::is_empty")]
    pub earlier: Vec<String>,
}

impl Recusal {
    /// Every hand that held the work, latest first.
    pub fn holders(&self) -> impl Iterator<Item = &str> {
        self.worker
            .as_deref()
            .into_iter()
            .chain(self.earlier.iter().map(String::as_str))
    }

    /// One sentence, aimed at a model that has to relay it to a human.
    pub fn describe(&self) -> String {
        let what = format!(
            "task {} ({})",
            self.from_seq,
            crate::fmt::truncate(&self.from_title, 40)
        );
        let why = if self.reason.is_empty() {
            String::new()
        } else {
            format!(" — {}", self.reason)
        };
        match &self.worker {
            Some(worker) => format!(
                "not whoever worked {what}: that was {worker}, so this needs another harness{why}"
            ),
            None => format!("not whoever works {what}; nobody has yet{why}"),
        }
    }
}

/// A project's queue stood down by the human.
///
/// While this stands, nothing is handed out — named claims and `task_next`
/// alike are refused, and the dispatch hook stays quiet. Work already claimed
/// runs to its own end: a recess stops the hand-out, not the work.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct Recess {
    pub project: String,
    /// Why, in the human's words. May be empty.
    pub reason: String,
    /// The surface that called it — `cli` or `tui`.
    pub actor: String,
    /// When it was called. Updating the reason does not move this: the recess
    /// began when it began.
    pub at: String,
}

impl Recess {
    /// One sentence, aimed at a model that has to relay it to a human.
    pub fn describe(&self) -> String {
        format!(
            "the human stood this queue down{} — nothing is handed out until \
             `hird resume` lifts it, though work already claimed continues",
            self.quoted_reason()
        )
    }

    /// ` ("rebasing main")`, or nothing when no reason was given.
    pub fn quoted_reason(&self) -> String {
        if self.reason.is_empty() {
            String::new()
        } else {
            format!(" ({:?})", self.reason)
        }
    }
}

/// What a review concluded about the work it reviewed.
///
/// A review's `result` is prose; the verdict is the one bit of it the queue
/// can act on. `Upheld` means the work stands — done, and seen to be done by a
/// harness that did not do it. `SentBack` means it does not, and names its own
/// consequence: the work returns to the pool carrying the reviewer's findings.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum Verdict {
    Upheld,
    SentBack,
}

impl Verdict {
    pub fn as_str(self) -> &'static str {
        match self {
            Verdict::Upheld => "upheld",
            Verdict::SentBack => "sent_back",
        }
    }
}

impl fmt::Display for Verdict {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.pad(self.as_str())
    }
}

impl FromStr for Verdict {
    type Err = UnknownVerdict;

    fn from_str(s: &str) -> Result<Self, Self::Err> {
        match s.trim() {
            "upheld" | "uphold" | "approve" | "approved" => Ok(Verdict::Upheld),
            "sent_back" | "sent-back" | "send_back" | "send-back" | "needs_work" | "needs-work" => {
                Ok(Verdict::SentBack)
            }
            other => Err(UnknownVerdict(other.to_string())),
        }
    }
}

#[derive(Debug, Clone, thiserror::Error)]
#[error(
    "unknown verdict {0:?}: say \"upheld\" if the work stands, or \"sent_back\" to return it \
     to the pool carrying your findings"
)]
pub struct UnknownVerdict(pub String);

/// One verdict, as delivered: which review said it, about whose work.
///
/// Append-only, like the event trail — a work task sent back and redone
/// accumulates one of these per round, which is what makes the record honest
/// about how many rounds there were.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct VerdictRecord {
    pub review_seq: i64,
    pub task_seq: i64,
    pub verdict: Verdict,
    /// Whoever's work was judged: the holder of record when the verdict
    /// landed. Empty when nobody had worked the task.
    pub worker: String,
    /// Whoever delivered it.
    pub reviewer: String,
    pub at: String,
}

impl VerdictRecord {
    /// One sentence, the way the board and `task_get` say it.
    pub fn describe(&self) -> String {
        match self.verdict {
            Verdict::Upheld => format!(
                "upheld by {} (review {})",
                crate::identity::actor_harness(&self.reviewer),
                self.review_seq
            ),
            Verdict::SentBack => format!(
                "sent back by {} (review {})",
                crate::identity::actor_harness(&self.reviewer),
                self.review_seq
            ),
        }
    }
}

/// One harness's — or one person's — standing in the verdict record, both as
/// worker and reviewer.
///
/// Derived entirely from delivered verdicts, so it measures the one thing the
/// queue can measure: whose work survives a reading by a different model.
/// Which sense of *whose* is [`crate::identity::Axis`], chosen when the record
/// is read.
#[derive(Debug, Clone, Default, PartialEq, Eq, Serialize, Deserialize)]
pub struct RecordRow {
    /// The harness or the person this row aggregates, per the axis it was read
    /// along.
    pub who: String,
    /// Verdicts received on this row's work, across every round.
    pub judged: i64,
    pub upheld: i64,
    pub sent_back: i64,
    /// Distinct tasks judged, and how many of them were upheld on the first
    /// verdict — before any round of rework.
    pub tasks_judged: i64,
    pub first_pass: i64,
    /// Verdicts this row has delivered as a reviewer.
    pub upheld_given: i64,
    pub sent_back_given: i64,
}

/// One file an assertion was learned against, and what that file said then.
///
/// An assertion is a statement *about code*, and code moves. The anchor is the
/// receipt: this is the file the claim was read off, and this is the version of
/// it that was open at the time.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct Anchor {
    /// Project-relative path.
    pub path: String,
    /// Content hash when the anchor was taken; empty if the file was already
    /// absent, which is a fact worth keeping as much as any other.
    pub hash: String,
    pub at: String,
}

/// A file an assertion stands on that is no longer what it was.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct Shift {
    pub path: String,
    /// The file is not merely different, it is not there.
    pub gone: bool,
}

/// How much the ground under an assertion has moved since it was recorded.
///
/// This is the one question every agent-memory store fails to answer and every
/// one of them needs to: a fact recorded six weeks ago about a file that has
/// been rewritten twice since is not a fact any more, and nothing about the
/// sentence itself gives that away. hird can answer it because the witness
/// already fingerprints files, so an assertion can be stored alongside the
/// version of the code it was read off.
///
/// Note what it deliberately is not: a verdict. A file changing does not make
/// an assertion false — a rename, a formatting pass and a rewrite all look
/// identical from here. It makes it *unverified*, which is a different and much
/// more useful thing to be told, because it is exactly the set of facts worth
/// spending a re-read on.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "snake_case", tag = "standing")]
pub enum Standing {
    /// No files were ever recorded, so there is nothing to check it against.
    Unanchored,
    /// Every file it was recorded against still says what it said.
    Firm { paths: Vec<String> },
    /// At least one of them does not.
    Shaky {
        moved: Vec<Shift>,
        firm: Vec<String>,
    },
    /// All of them are gone.
    Orphaned { paths: Vec<String> },
}

impl Standing {
    pub fn as_str(&self) -> &'static str {
        match self {
            Standing::Unanchored => "unanchored",
            Standing::Firm { .. } => "firm",
            Standing::Shaky { .. } => "shaky",
            Standing::Orphaned { .. } => "orphaned",
        }
    }

    /// Every file this standing was decided from, in order.
    pub fn paths(&self) -> Vec<&str> {
        match self {
            Standing::Unanchored => Vec::new(),
            Standing::Firm { paths } | Standing::Orphaned { paths } => {
                paths.iter().map(String::as_str).collect()
            }
            Standing::Shaky { moved, firm } => moved
                .iter()
                .map(|s| s.path.as_str())
                .chain(firm.iter().map(String::as_str))
                .collect(),
        }
    }

    /// Is this worth re-reading the code over?
    pub fn needs_checking(&self) -> bool {
        matches!(self, Standing::Shaky { .. } | Standing::Orphaned { .. })
    }

    /// How much to trust it, lowest first. Used to order recall, where the
    /// budget is small and a verified fact should outrank a suspect one.
    pub fn rank(&self) -> u8 {
        match self {
            Standing::Firm { .. } => 0,
            Standing::Unanchored => 1,
            Standing::Shaky { .. } => 2,
            Standing::Orphaned { .. } => 3,
        }
    }

    /// One sentence, aimed at a model deciding whether to trust the assertion.
    ///
    /// `None` for an unanchored assertion: hird has nothing to say about it,
    /// and saying so on every row would be noise wearing the clothes of a
    /// warning.
    pub fn describe(&self) -> Option<String> {
        match self {
            Standing::Unanchored => None,
            Standing::Firm { paths } => Some(match paths.as_slice() {
                [one] => format!("{one} is unchanged since this was recorded"),
                many => format!(
                    "all {} files this was recorded against are unchanged",
                    many.len()
                ),
            }),
            Standing::Shaky { moved, .. } => Some(format!(
                "{} since this was recorded — re-read before relying on it",
                describe_shifts(moved)
            )),
            Standing::Orphaned { paths } => Some(match paths.as_slice() {
                [one] => {
                    format!("{one} no longer exists — this was recorded about a file that has gone")
                }
                many => format!(
                    "all {} files this was recorded against have been deleted",
                    many.len()
                ),
            }),
        }
    }
}

/// `src/config.rs has changed`, `src/a.rs is gone and 2 others have changed`.
fn describe_shifts(moved: &[Shift]) -> String {
    let verb = |s: &Shift| if s.gone { "is gone" } else { "has changed" };
    match moved {
        [] => "nothing has changed".to_string(),
        [one] => format!("{} {}", one.path, verb(one)),
        [first, rest @ ..] => {
            let others = rest.len();
            let tail = match (others == 1, rest.iter().all(|s| s.gone)) {
                (true, true) => "is gone",
                (true, false) => "has changed",
                (false, true) => "are gone",
                (false, false) => "have changed",
            };
            format!(
                "{} {} and {others} other{} {tail}",
                first.path,
                verb(first),
                if others == 1 { "" } else { "s" },
            )
        }
    }
}

/// Everyone who has stated an assertion, counting whoever recorded it first.
///
/// Two agents in one harness saying the same thing is repetition; two agents in
/// *different* harnesses saying it independently is corroboration, and hird is
/// the only thing in the room that can tell the difference, because it is the
/// only thing both of them talk to.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct Voices {
    /// Actors, the agent that first recorded the assertion first.
    pub actors: Vec<String>,
}

impl Voices {
    /// The distinct harnesses among them, in first-seen order.
    pub fn harnesses(&self) -> Vec<&str> {
        let mut out: Vec<&str> = Vec::new();
        for actor in &self.actors {
            let harness = crate::identity::actor_harness(actor);
            if !out.contains(&harness) {
                out.push(harness);
            }
        }
        out
    }

    /// One sentence, or `None` when only one voice has ever said it — which is
    /// the ordinary case and not worth a line.
    ///
    /// Names them rather than counting them, because who confirmed a fact is
    /// most of what the count was standing in for, and a reader can weigh
    /// `codex:9f2c` against `cli` for themselves. Long lists are cut off with a
    /// tally rather than truncated silently.
    pub fn describe(&self) -> Option<String> {
        let [_first, others @ ..] = self.actors.as_slice() else {
            return None;
        };
        if others.is_empty() {
            return None;
        }
        const NAMED: usize = 3;
        let shown: Vec<&str> = others.iter().take(NAMED).map(String::as_str).collect();
        let mut sentence = format!("also stated by {}", shown.join(", "));
        if others.len() > shown.len() {
            sentence.push_str(&format!(" and {} more", others.len() - shown.len()));
        }
        // The claim worth making loudest: not that several sessions agree, but
        // that sessions which cannot see each other agree.
        let harnesses = self.harnesses().len();
        if harnesses > 1 {
            sentence.push_str(&format!(", independently across {harnesses} harnesses"));
        }
        Some(sentence)
    }
}

/// One durable factual claim recorded by an agent or a human.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct Assertion {
    pub id: String,
    pub project: String,
    pub content: String,
    pub tags: String,
    pub actor: String,
    pub task_id: Option<String>,
    /// Set to the id of the assertion that replaced this one; `None` = current.
    pub superseded_by: Option<String>,
    pub created_at: String,
}

impl Assertion {
    /// Tags split into trimmed, non-empty pieces.
    pub fn tag_list(&self) -> Vec<&str> {
        self.tags
            .split(',')
            .map(str::trim)
            .filter(|t| !t.is_empty())
            .collect()
    }
}

/// Normalize freeform tag input into the comma-separated storage form.
pub fn normalize_tags(raw: &str) -> String {
    raw.split(',')
        .map(str::trim)
        .filter(|t| !t.is_empty())
        .collect::<Vec<_>>()
        .join(",")
}

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

    /// The reachable set from `open` must be exactly the six statuses, and no
    /// transition may produce a status outside the diagram in §5 of DESIGN.md.
    #[test]
    fn status_machine_has_no_edges_outside_the_diagram() {
        // The complete edge list, transcribed independently from the diagram.
        let allowed: &[(Status, Transition, Status)] = &[
            (Status::Open, Transition::Claim, Status::Claimed),
            (Status::Claimed, Transition::Start, Status::InProgress),
            (Status::InProgress, Transition::Start, Status::InProgress),
            (Status::Claimed, Transition::Complete, Status::Done),
            (Status::InProgress, Transition::Complete, Status::Done),
            (Status::Claimed, Transition::Fail, Status::Failed),
            (Status::InProgress, Transition::Fail, Status::Failed),
            (Status::Claimed, Transition::LeaseExpiry, Status::Open),
            (Status::InProgress, Transition::LeaseExpiry, Status::Open),
            (Status::Claimed, Transition::Release, Status::Open),
            (Status::InProgress, Transition::Release, Status::Open),
            (Status::Open, Transition::Cancel, Status::Cancelled),
            (Status::Claimed, Transition::Cancel, Status::Cancelled),
            (Status::InProgress, Transition::Cancel, Status::Cancelled),
            (Status::Done, Transition::Reopen, Status::Open),
            (Status::Failed, Transition::Reopen, Status::Open),
            (Status::Cancelled, Transition::Reopen, Status::Open),
        ];

        for from in Status::ALL {
            for transition in Transition::ALL {
                let expected = allowed
                    .iter()
                    .find(|(f, t, _)| *f == from && *t == transition)
                    .map(|(_, _, to)| *to);
                assert_eq!(
                    from.apply(transition),
                    expected,
                    "edge {from} --{transition}--> mismatch"
                );
            }
        }
    }

    #[test]
    fn every_status_is_reachable_from_open() {
        let mut seen = std::collections::BTreeSet::from([Status::Open]);
        loop {
            let next: Vec<Status> = seen
                .iter()
                .flat_map(|s| Transition::ALL.iter().filter_map(|t| s.apply(*t)))
                .collect();
            let before = seen.len();
            seen.extend(next);
            if seen.len() == before {
                break;
            }
        }
        assert_eq!(seen, Status::ALL.into_iter().collect());
    }

    #[test]
    fn terminal_statuses_only_leave_via_reopen() {
        for status in Status::ALL.into_iter().filter(|s| s.is_terminal()) {
            for transition in Transition::ALL {
                let reached = status.apply(transition);
                if transition == Transition::Reopen {
                    assert_eq!(reached, Some(Status::Open));
                } else {
                    assert_eq!(reached, None, "{status} escaped via {transition}");
                }
            }
        }
    }

    #[test]
    fn status_round_trips_through_its_string_form() {
        for status in Status::ALL {
            assert_eq!(status.as_str().parse::<Status>().unwrap(), status);
        }
    }

    #[test]
    fn timestamps_sort_lexicographically() {
        let early = fmt_ts(DateTime::from_timestamp(1_700_000_000, 0).unwrap());
        let late = fmt_ts(DateTime::from_timestamp(1_800_000_000, 0).unwrap());
        assert!(early < late);
        assert_eq!(early.len(), late.len());
        assert_eq!(parse_ts(&early).unwrap().timestamp(), 1_700_000_000);
    }

    /// The sentence a shaky assertion carries is the whole product: it has to
    /// name the file, say what happened to it, and say what to do — without
    /// ever claiming the assertion is false, which hird cannot know.
    #[test]
    fn a_shaky_standing_names_the_file_and_stops_short_of_a_verdict() {
        let one = Standing::Shaky {
            moved: vec![Shift {
                path: "src/config.rs".into(),
                gone: false,
            }],
            firm: vec![],
        };
        assert_eq!(
            one.describe().unwrap(),
            "src/config.rs has changed since this was recorded — re-read before relying on it"
        );
        for word in ["wrong", "false", "stale", "invalid"] {
            assert!(!one.describe().unwrap().contains(word), "{word}");
        }

        let several = Standing::Shaky {
            moved: vec![
                Shift {
                    path: "src/a.rs".into(),
                    gone: true,
                },
                Shift {
                    path: "src/b.rs".into(),
                    gone: false,
                },
            ],
            firm: vec!["src/c.rs".into()],
        };
        assert!(
            several
                .describe()
                .unwrap()
                .starts_with("src/a.rs is gone and 1 other has changed"),
            "{:?}",
            several.describe()
        );
    }

    #[test]
    fn standings_report_every_path_they_were_decided_from() {
        let shaky = Standing::Shaky {
            moved: vec![Shift {
                path: "a".into(),
                gone: false,
            }],
            firm: vec!["b".into()],
        };
        assert_eq!(shaky.paths(), vec!["a", "b"]);
        assert!(Standing::Unanchored.paths().is_empty());
        assert_eq!(
            Standing::Firm {
                paths: vec!["a".into()]
            }
            .paths(),
            vec!["a"]
        );
    }

    /// One agent saying something is provenance. Two agents in two harnesses
    /// saying it independently is the strongest signal hird can produce, and it
    /// is the one nothing else in the room is positioned to see.
    #[test]
    fn corroboration_is_only_worth_a_sentence_when_it_crosses_agents() {
        let alone = Voices {
            actors: vec!["codex:9f2c".into()],
        };
        assert!(alone.describe().is_none());

        let same_harness = Voices {
            actors: vec!["codex:9f2c".into(), "codex:1a2b".into()],
        };
        assert_eq!(
            same_harness.describe().unwrap(),
            "also stated by codex:1a2b",
            "two sessions of one harness are not independent confirmation"
        );
        assert_eq!(same_harness.harnesses(), vec!["codex"]);

        let across = Voices {
            actors: vec![
                "codex:9f2c".into(),
                "claude-code:af31".into(),
                "copilot:77".into(),
            ],
        };
        assert_eq!(
            across.describe().unwrap(),
            "also stated by claude-code:af31, copilot:77, independently across 3 harnesses"
        );

        // A long list is cut off with a tally, never silently truncated.
        let many = Voices {
            actors: (0..6).map(|i| format!("codex:{i}")).collect(),
        };
        let sentence = many.describe().unwrap();
        assert!(sentence.contains("and 2 more"), "{sentence}");
    }

    #[test]
    fn tags_are_normalized_to_a_compact_comma_list() {
        assert_eq!(normalize_tags(" a , ,b,, c "), "a,b,c");
        assert_eq!(normalize_tags(""), "");
    }

    /// The distinction the type exists for: an unwatched task says nothing,
    /// and it must not be possible to mistake that for "nothing moved".
    #[test]
    fn an_unwatched_task_makes_no_claim_either_way() {
        assert_eq!(Footprint::Unwatched.label(false), None);
        assert_eq!(Footprint::Unwatched.describe(false), None);
        assert_eq!(Footprint::default(), Footprint::Unwatched);
        assert!(!Footprint::Unwatched.is_watched());
        assert!(Footprint::ReadOnly.is_watched());
    }

    /// A task still being worked has not finished not writing anything, and
    /// the wording has to leave room for its next edit.
    #[test]
    fn read_only_is_a_verdict_when_finished_and_a_running_total_while_held() {
        assert_eq!(Footprint::ReadOnly.label(false).unwrap(), "read-only");
        assert_eq!(Footprint::ReadOnly.label(true).unwrap(), "read-only so far");
        assert!(Footprint::ReadOnly
            .describe(false)
            .unwrap()
            .contains("while it was held"));
        assert!(Footprint::ReadOnly
            .describe(true)
            .unwrap()
            .contains("since it was claimed"));
    }

    #[test]
    fn a_shared_file_stops_a_count_from_speaking_for_one_agent() {
        let alone = Footprint::Modified {
            paths: 1,
            shared: false,
        };
        assert_eq!(alone.label(false).unwrap(), "modified 1 file");
        assert_eq!(alone.describe(false).unwrap(), "modified 1 file");

        let shared = Footprint::Modified {
            paths: 3,
            shared: true,
        };
        assert_eq!(shared.label(true).unwrap(), "modified 3 files");
        let sentence = shared.describe(true).unwrap();
        assert!(sentence.starts_with("modified 3 files,"), "{sentence}");
        assert!(sentence.contains("another agent"), "{sentence}");
    }
}