slipcase-open 0.1.5

Open the payload of a Slipcase container in its own application, and write edits back into the container
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
//! The instance that holds the sessions, and the loop that serves it.
//
// Author: David M. Anderson
// Built with AI assistance (Claude, Anthropic)
//
//! Concept 8. One process holds every open session; every other invocation
//! reaches it through the front door and exits.
//!
//! **Requests are served on the loop's own thread and the accepting is not.** A
//! blocking `accept` would starve the watchers, and the watchers are the whole
//! reason this process exists. So a thread does nothing but accept and hand
//! connections over, and the loop alternates between answering one, pumping the
//! sessions, and collecting whatever concept 9's channel has been told.
//!
//! ## Three lists rather than one
//!
//! A session is open, or it is closed and the application has not finished with
//! it (concept 6.2), or a question has been asked about it and nothing may
//! happen until somebody answers (concept 6.3). Concept 8 says the process
//! lives while any of the three exists and stops when none does, so they are
//! three lists and `is_idle` is all of them being empty.

use std::io;
use std::path::{Path, PathBuf};
use std::time::{Duration, Instant};

use crate::flow::{self, Lingering, Opened};
use crate::ipc::{Request, Response, Voice};
use crate::outside::Outside;
use crate::present::{Answer, Choice, Question, Report};
use crate::recover;
use crate::session::{self, Session};
use crate::table::Table;

/// How long the loop waits for a request before going round to pump.
const TICK: Duration = Duration::from_millis(250);

/// How long a closed session's payload directory must go untouched before the
/// application is taken to have finished with it (concept 8).
///
/// Short, because it is the second of two conditions rather than the whole
/// test: the siblings have to be gone as well, and an application that has
/// cleaned up after itself is not about to write again. What this guards is the
/// gap between the last write and the last unlink.
const SETTLED: Duration = Duration::from_secs(2);

/// How long the instance stays alive holding a question nobody has answered,
/// where there is nothing standing to keep showing it.
///
/// **A judgement, and worth naming as one.** Concept 8 says to bound the linger
/// and does not say where, because there is no measurement that settles it:
/// what it trades is a resident process against a question that can still be
/// acted on. Five minutes is long enough that somebody who saw the notification
/// and finished a sentence first still finds the buttons live, and short enough
/// that a process nobody is talking to does not sit there for the afternoon.
/// When it expires the question is taken back rather than left standing, and
/// what replaces it says how to reach the same decision from the command line —
/// a button that does nothing is worse than no button.
///
/// **It only applies where the question has nowhere to live**, which is what
/// [`Resident::let_questions_stand`] turns off. The whole of the reasoning above
/// is about a process staying alive *for* the question; where an icon is
/// already keeping it, the trade is not being made and the limit only throws
/// the question away.
const HELD: Duration = Duration::from_secs(300);

/// How long the icon shows that a save is going back into its container.
///
/// Long enough to be seen and short enough not to be a state. A repack of an
/// ordinary document is faster than this, so the wait is not the work — it is
/// the acknowledgement, and it exists because pressing Save and seeing nothing
/// change anywhere is what makes somebody doubt the tool is running.
const PULSE: Duration = Duration::from_millis(900);

/// A question the instance has asked and is holding open so that it can act on
/// the answer.
struct Pending {
    /// The session it is about, as `sessions` names it.
    about: String,
    /// What the answer acts on.
    session: Session,
    /// A container to open once this is settled, where the question was raised
    /// by an `open` that could not proceed until it was (concept 8).
    then_open: Option<PathBuf>,
    /// When it was last put in front of somebody. Reset by a `Reveal`, which is
    /// somebody saying *not yet* rather than declining to answer.
    asked: Instant,
}

/// The sessions this instance is holding.
pub struct Resident {
    root: PathBuf,
    sessions: Table<Opened>,
    lingering: Vec<Lingering>,
    pending: Vec<Pending>,
    /// [`SETTLED`] and [`HELD`], held rather than read, so that the rules they
    /// express can be tested instead of waited out. Nothing outside the tests
    /// changes them: a five-minute hold is not a setting concept 10 asks for,
    /// and making it one would put a second answer to *how long* in a file.
    settles_after: Duration,
    /// How long an unanswered question is held before being taken back, or
    /// `None` where something is standing that can keep showing it.
    holds_for: Option<Duration>,
    /// What the standing list is coloured for, until somebody puts it down.
    ///
    /// **Held here rather than only spoken**, because a notification is a
    /// moment and most of these are not. A container that would not open leaves
    /// no session behind to re-read the trouble from, so the toast saying so
    /// was the only record there was, and a toast that was missed is a trouble
    /// that never happened.
    troubles: Vec<crate::present::Trouble>,
    /// When a save last went back into a container, for the moment of colour
    /// that says so. `None` until one has.
    wrote_back: Option<Instant>,
}

impl Resident {
    /// An instance holding nothing, keeping its sessions under `root`.
    #[must_use]
    pub fn new(root: impl Into<PathBuf>) -> Self {
        Self {
            root: root.into(),
            sessions: Table::new(),
            lingering: Vec::new(),
            pending: Vec::new(),
            settles_after: SETTLED,
            holds_for: Some(HELD),
            troubles: Vec::new(),
            wrote_back: None,
        }
    }

    /// Set the two waits, for the tests of what happens on either side of
    /// them. A five-minute hold has no other way of being tested, and a
    /// two-second settle has no other way of being tested quickly.
    #[cfg(test)]
    fn waiting(mut self, settles_after: Duration, holds_for: Duration) -> Self {
        self.settles_after = settles_after;
        self.holds_for = Some(holds_for);
        self
    }

    /// Whether there is anything left to hold, which is concept 8's exit rule.
    #[must_use]
    pub fn is_idle(&self) -> bool {
        self.sessions.is_empty() && self.lingering.is_empty() && self.pending.is_empty()
    }

    /// Let questions stand for as long as the instance does.
    ///
    /// **For an instance with a standing surface, which is what [`HELD`] was
    /// standing in for.** That limit exists because a question is the only
    /// thing keeping the process alive and a process nobody is talking to must
    /// not sit there for the afternoon. Where an icon is up, the process is
    /// already staying for its own reasons, so the limit buys nothing and costs
    /// the question: taking it back and naming two command lines in its place
    /// is the dead end that made the tray look necessary in the first place.
    ///
    /// Not a setting. It follows from whether there is somewhere for the
    /// question to keep being visible, which is a fact about the platform and
    /// the invocation rather than a preference.
    pub fn let_questions_stand(&mut self) {
        self.holds_for = None;
    }

    /// Put back an edit that never reached its container.
    ///
    /// **Concept 6.3 as amended: this one is not a question.** `recover` only
    /// answers [`recover::Course::WriteBack`] where it knows the difference is
    /// this session's own edit and the container is still holding what the two
    /// last agreed on, so there is nothing for a person to decide and nobody
    /// else's work to lose. Asking anyway is this tool's failure handed back to
    /// them in vocabulary they never asked to learn.
    ///
    /// **Said, not silent.** The risk that survives the amendment is a save
    /// that was half-written when the process died, and the only defence left
    /// against it is somebody seeing what happened while the container is still
    /// in front of them. So the report is `ordinary` rather than `routine`: no
    /// setting drops it.
    fn put_it_back(&mut self, mut session: Session, outside: &Outside<'_>) {
        let name = slpc::display_name(&session.record().payload).into_owned();
        let into = slpc::display_path(&session.record().container);
        match crate::writeback::write_back(&mut session) {
            Ok(()) => {
                outside.report(
                    &Report::ordinary(format!("{name} was recovered and written back."))
                        .and(format!("Into {into}."))
                        .and("It had been edited after the session holding it stopped."),
                );
                let _ = session.remove();
            }
            Err(e) => {
                // The session stays, which is what makes a second attempt
                // possible, and the icon carries it: an edit that is nowhere
                // but a session directory is exactly what orange is for.
                outside.report(
                    &Report::interrupt(format!("{name} could not be written back."))
                        .and(e.to_string()),
                );
                self.note(
                    crate::present::Mood::AtRisk,
                    format!("recover:{}", id_of(&session)),
                    format!("{name} - an edit is not in its container"),
                );
            }
        }
    }

    /// Take on a trouble, for the icon to carry and the menu to explain.
    ///
    /// The same one twice is one trouble. A container that will not open is a
    /// container somebody is likely to double-click again, and three identical
    /// lines in a menu say nothing the first did not.
    fn note(&mut self, mood: crate::present::Mood, id: impl Into<String>, summary: String) {
        let id = id.into();
        if let Some(had) = self.troubles.iter_mut().find(|t| t.id == id) {
            had.mood = mood;
            had.summary = summary;
            return;
        }
        self.troubles
            .push(crate::present::Trouble { id, mood, summary });
    }

    /// Put down a trouble that has been read.
    fn dismiss(&mut self, id: &str) {
        self.troubles.retain(|t| t.id != id);
    }

    /// What the standing list is coloured for.
    #[must_use]
    pub fn troubles(&self) -> &[crate::present::Trouble] {
        &self.troubles
    }

    /// What colour the icon is, which is the worst thing currently true.
    ///
    /// **Two sources, and they are different in kind.** A trouble is remembered
    /// until somebody puts it down, because it is a moment that would otherwise
    /// be gone. A session waiting on a decision is not remembered at all — it
    /// is read off the sessions every time, so answering the question is what
    /// clears the colour, with nothing to dismiss and nothing that can fall out
    /// of step with what is on disk.
    #[must_use]
    pub fn mood(&self) -> crate::present::Mood {
        use crate::present::Mood;
        let worst = self
            .troubles
            .iter()
            .map(|t| t.mood)
            .max()
            .unwrap_or(Mood::Settled);
        let waiting = if self.pending.is_empty() {
            Mood::Settled
        } else {
            Mood::Look
        };
        let saving = match self.wrote_back {
            Some(at) if at.elapsed() < PULSE => Mood::Working,
            _ => Mood::Settled,
        };
        worst.max(waiting).max(saving)
    }

    /// Answer one request.
    pub fn handle(&mut self, request: Request, outside: &Outside<'_>) -> Response {
        match request {
            Request::Ping => Response::Ok(Vec::new()),
            Request::List => self.list(),
            Request::Open { container, voice } => self.open(&container, voice, outside),
            Request::Close(id) => self.close(&id),
        }
    }

    /// Open a container, or bring forward the session that already has it.
    fn open(&mut self, container: &Path, voice: Voice, outside: &Outside<'_>) -> Response {
        // Concept 8: a container that already has a live session is not opened
        // twice. Two sessions would both repack it and the second write-back
        // would overwrite the first with nothing said. Re-launching is what a
        // second double-click on an open document does everywhere else.
        if let Some(open) = self.sessions.find_mut(container) {
            return match outside.launcher.launch(&open.payload_path()) {
                Ok(()) => say(
                    voice,
                    outside,
                    Report::ordinary(format!(
                        "{} is already open; brought forward.",
                        slpc::display_name(&open.session().record().payload)
                    )),
                ),
                Err(e) => refuse(voice, outside, format!("could not bring it forward: {e}")),
            };
        }

        // Concept 8: a pending recovery item is resolved first. A session left
        // by a crash is not in the live table, so nothing refuses it — but
        // opening a fresh one would extract the container's current payload and
        // leave the recovered edit with nowhere to go.
        match self.ask_about_what_was_left(container, outside) {
            Err(e) => return refuse(voice, outside, e),
            Ok(true) => {
                return refuse(
                    voice,
                    outside,
                    "a session on this container was left behind, and what to do with it \
                     comes first"
                        .to_string(),
                )
            }
            Ok(false) => {}
        }

        match flow::open(&self.root, container, outside) {
            Err(e) => {
                let named = container.file_name().map_or_else(
                    || container.display().to_string(),
                    |n| n.to_string_lossy().into_owned(),
                );
                // **The one refusal that is spoken twice.** Concept 5.1's check
                // fires close to never and means one thing when it does, and
                // the person is holding a file somebody sent them believing it
                // is a document. A notification they may not look at is not
                // enough for that, so it is insisted on as well as recorded —
                // and the icon goes red, which is the only thing red is for.
                if let flow::Error::Misrepresented(what) = &e {
                    outside.channel.insist(
                        &Report::interrupt(format!("{named} was not opened."))
                            .and(format!(
                                "Its payload is {}, not a document.",
                                what.describes()
                            ))
                            .and("That is the shape of a phishing attachment.")
                            .and("Nothing was extracted and nothing was run."),
                    );
                    self.note(
                        crate::present::Mood::Danger,
                        format!("content:{}", container.display()),
                        format!("{named} - is {}, not a document", what.describes()),
                    );
                    // `refuse` deliberately skipped: it would say "Not opened"
                    // through the same channel that has just been insisted at,
                    // which on Windows is a box and two toasts for one event.
                    // What was insisted on is the better sentence of the two,
                    // and the command line still gets this one back.
                    return Response::Err(e.to_string());
                }
                // Everything else: nothing was extracted and nothing is at
                // risk, so this is a look rather than a warning — but it is
                // still the case the standing list exists for. A double-click
                // that produces no document and no window has to say something
                // that outlasts a banner, or the tool appears not to work.
                self.note(
                    crate::present::Mood::Look,
                    format!("open:{}", container.display()),
                    format!("{named} - did not open: {e}"),
                );
                refuse(voice, outside, e.to_string())
            }
            Ok(opened) => {
                let name = slpc::display_name(&opened.session().record().payload).into_owned();
                let mut report = Report::routine(format!("{name} is open."))
                    .and(format!("Session {}", id_of(opened.session())));
                if opened.mark != slpc::provenance::Mark::Silent {
                    report = report.and("It came from somewhere else, and the copy says so.");
                }
                if let Err(e) = self.sessions.insert(container, opened) {
                    return refuse(
                        voice,
                        outside,
                        format!("the session could not be tracked: {e}"),
                    );
                }
                say(voice, outside, report)
            }
        }
    }

    /// Put concept 6.3's question about a session left behind on this
    /// container, where there is one worth asking about, and hold it.
    ///
    /// Answers `true` where the open must wait for it. Concept 8: *the recovery
    /// question comes first, and the new session follows the answer* — so the
    /// container is remembered against the question and opened once it is
    /// settled, rather than the person having to double-click a second time.
    fn ask_about_what_was_left(
        &mut self,
        container: &Path,
        outside: &Outside<'_>,
    ) -> Result<bool, String> {
        let want = crate::identity::of(container).map_err(|e| e.to_string())?;
        let sessions = session::scan(&self.root).map_err(|e| e.to_string())?;
        for left in sessions {
            // A container the record names that has since gone cannot be the
            // one being opened, so it is not this invocation's business.
            if !crate::identity::of(&left.record().container).is_ok_and(|is| is == want) {
                continue;
            }
            let state = recover::state(&left);
            match state.course() {
                // Nothing was lost; the sweep will take it.
                recover::Course::Sweep => continue,
                // The person's own edit, and the container has not moved. Put
                // it back and let the open carry on — being stopped to answer a
                // question about work they already saved is the thing this
                // replaces.
                recover::Course::WriteBack => {
                    self.put_it_back(left, outside);
                    continue;
                }
                recover::Course::Ask => {}
            }
            let about = id_of(&left);
            // Already asked, and still waiting. Asking again would put a second
            // copy of the same question in the message list, and answering
            // either would leave the other one behind.
            if self.pending.iter().any(|p| p.about == about) {
                return Ok(true);
            }
            outside.channel.ask(&Question {
                about: about.clone(),
                summary: format!(
                    "{} was left behind.",
                    slpc::display_name(&left.record().payload)
                ),
                // Short, because a notification body is one paragraph however
                // it is written. The container is named because that is what
                // the decision is about; the session directory is not, because
                // `Reveal` is the button that opens it.
                detail: vec![
                    format!("It is {state}."),
                    format!("From {}.", slpc::display_path(&left.record().container)),
                    "It will open once you have decided.".into(),
                ],
                choices: vec![Choice::WriteBack, Choice::Discard, Choice::Reveal],
            });
            self.pending.push(Pending {
                about,
                session: left,
                then_open: Some(container.to_path_buf()),
                asked: Instant::now(),
            });
            return Ok(true);
        }
        Ok(false)
    }

    /// Every session, with the id apart from the words.
    ///
    /// One source for both surfaces: `list` puts the id back in front and hands
    /// the lines to the command line, and concept 12's standing list takes the
    /// pieces, because a menu needs the id to act on and no room to show it.
    pub(crate) fn listed(&self) -> Vec<crate::present::Listed> {
        let mut out: Vec<crate::present::Listed> = self
            .sessions
            .iter()
            .map(|o| crate::present::Listed {
                id: id_of(o.session()),
                payload: slpc::display_name(&o.session().record().payload).into_owned(),
                label: format!(
                    "{}  open, {} write-back(s)",
                    slpc::display_name(&o.session().record().payload),
                    o.session().record().write_backs
                ),
                live: true,
                needs_a_person: false,
                write_backs: Some(o.session().record().write_backs),
            })
            .collect();
        out.extend(self.lingering.iter().map(|l| crate::present::Listed {
            id: id_of(l.session()),
            payload: slpc::display_name(&l.session().record().payload).into_owned(),
            label: format!(
                "{}  closed, waiting for the application to finish",
                slpc::display_name(&l.session().record().payload)
            ),
            live: true,
            needs_a_person: false,
            write_backs: Some(l.session().record().write_backs),
        }));
        out.extend(self.pending.iter().map(|p| crate::present::Listed {
            id: p.about.clone(),
            payload: slpc::display_name(&p.session.record().payload).into_owned(),
            label: format!(
                "{}  {}, waiting for you",
                slpc::display_name(&p.session.record().payload),
                recover::state(&p.session)
            ),
            live: false,
            needs_a_person: true,
            write_backs: None,
        }));

        // Everything above is also a directory under the root, so a scan that
        // did not know what was held would report each of them twice.
        let held = self.held_directories();
        if let Ok(left) = session::scan(&self.root) {
            for s in left
                .iter()
                .filter(|s| !held.contains(&s.dir().to_path_buf()))
            {
                out.push(crate::present::Listed {
                    id: id_of(s),
                    payload: slpc::display_name(&s.record().payload).into_owned(),
                    label: format!(
                        "{}  {}",
                        slpc::display_name(&s.record().payload),
                        recover::state(s)
                    ),
                    live: false,
                    // Concept 6.3: what needs nobody is swept and never spoken
                    // of. Listing it in a standing surface is furniture.
                    needs_a_person: recover::state(s).needs_a_person(),
                    write_backs: None,
                });
            }
        }
        out
    }

    pub(crate) fn list(&self) -> Response {
        let mut lines: Vec<String> = self
            .listed()
            .into_iter()
            .map(|e| format!("{}  {}", e.id, e.label))
            .collect();
        if lines.is_empty() {
            lines.push("No sessions.".into());
        }
        Response::Ok(lines)
    }

    /// Every session directory this instance is holding, in any of its three
    /// lists.
    fn held_directories(&self) -> Vec<PathBuf> {
        self.sessions
            .iter()
            .map(|o| o.session().dir().to_path_buf())
            .chain(
                self.lingering
                    .iter()
                    .map(|l| l.session().dir().to_path_buf()),
            )
            .chain(self.pending.iter().map(|p| p.session.dir().to_path_buf()))
            .collect()
    }

    fn close(&mut self, id: &str) -> Response {
        let Some(container) = self
            .sessions
            .iter()
            .find(|o| id_of(o.session()) == id)
            .map(|o| o.session().record().container.clone())
        else {
            return Response::Err(format!("no open session {id}"));
        };
        let Some(opened) = self.sessions.remove(&container) else {
            return Response::Err(format!("no open session {id}"));
        };
        match opened.close() {
            Ok(flow::Closed::Cleared) => Response::Ok(vec!["Session closed.".into()]),
            // Concept 6.2 and 8. The close is honoured; the directory is not
            // removed, and the watch stays on it so that the application's last
            // save is noticed when it happens rather than at the next launch.
            Ok(flow::Closed::LeftForRecovery(lingering)) => {
                self.lingering.push(*lingering);
                Response::Ok(vec![
                    "Session closed, and the application still has the payload open.".into(),
                    "It is being watched until the application finishes.".into(),
                ])
            }
            Err(e) => Response::Err(e.to_string()),
        }
    }

    /// One turn: pump the open sessions, move on whatever has settled, and act
    /// on whatever has been answered.
    pub fn turn(&mut self, outside: &Outside<'_>) {
        self.pump_all(outside);
        self.ask_about_what_has_settled(outside);
        self.act_on_answers(outside);
        self.let_go_of_the_unanswered(outside);
    }

    /// Give every open session's watch a turn, and report what came of it.
    fn pump_all(&mut self, outside: &Outside<'_>) {
        let mut wrote_back = Vec::new();
        let mut landed = Vec::new();
        let mut failed = Vec::new();
        for open in self.sessions.iter_mut() {
            match open.pump() {
                Ok(true) => {
                    let s = open.session();
                    // **The first of a session and not each one.** A write-back
                    // fires on every save, so an hour's editing is dozens of
                    // notifications for the same fact. The first is worth
                    // saying, because it is how somebody learns the loop works
                    // at all; the rest are the tool congratulating itself.
                    // Concept 6.2 wants a session visible and wants somewhere
                    // to look when an edit is expected to have landed, and
                    // `sessions` answers that better than a stream of banners.
                    if s.record().write_backs <= 1 {
                        outside.report(
                            &Report::routine(format!(
                                "{} written back.",
                                slpc::display_name(&s.record().payload)
                            ))
                            .and("Saves from here on are written back quietly."),
                        );
                    }
                    landed.push(id_of(s));
                    wrote_back.push(s.record().container.clone());
                }
                Ok(false) => {}
                // One failing container is not a reason to stop watching the
                // rest, and it is a reason to say so: concept 6.2 puts the
                // close at the user's hand, and a save that did not land is the
                // thing they most need to know did not.
                Err(e) => {
                    let name = slpc::display_name(&open.session().record().payload).into_owned();
                    outside.report(
                        &Report::interrupt(format!("{name} could not be written back."))
                            .and(e.to_string()),
                    );
                    failed.push((id_of(open.session()), name));
                }
            }
        }
        // A write-back renamed a new file over the container, so the identity
        // recorded when the session opened is stale. See `table::refresh`.
        if !wrote_back.is_empty() {
            // The moment of colour that says a save went home. Set from a
            // write-back having happened rather than from an event having
            // arrived, which is the same rule `pump` itself decides by.
            self.wrote_back = Some(Instant::now());
        }
        for container in wrote_back {
            self.sessions.refresh(&container);
        }
        // The promise this tool makes is that a save reaches the container, and
        // this is that promise outstanding. It stays on the icon until somebody
        // puts it down, because the alternative is a banner that flashed past
        // while they were typing into the very document that did not save.
        for (id, name) in failed {
            self.note(
                crate::present::Mood::AtRisk,
                format!("writeback:{id}"),
                format!("{name} - a save did not reach its container"),
            );
        }
        // And it goes when a later save from the same session lands, without
        // anybody dismissing anything. The colour is a claim about right now,
        // so a claim the next save disproves has to answer to it.
        for id in landed {
            self.dismiss(&format!("writeback:{id}"));
        }
    }

    /// Ask about every lingering session the application has finished with.
    fn ask_about_what_has_settled(&mut self, outside: &Outside<'_>) {
        let mut still_waiting = Vec::new();
        for mut lingering in std::mem::take(&mut self.lingering) {
            if !lingering.has_settled(self.settles_after) {
                still_waiting.push(lingering);
                continue;
            }
            let about = id_of(lingering.session());
            let session = lingering.into_session();
            let state = recover::state(&session);
            let name = slpc::display_name(&session.record().payload).into_owned();
            // Concept 6.3: equal to what the container holds means nothing was
            // lost, so clean up and say nothing. This is that case arriving
            // while the process is still alive to see it, which is what concept
            // 8 keeps it alive for.
            //
            // And the amended case beside it: a save that landed after the
            // close is the most ordinary reason to be here at all — somebody
            // pressed Save on the way out — so it goes back rather than being
            // put to them as a choice.
            match state.course() {
                recover::Course::Sweep => {
                    let _ = session.remove();
                    continue;
                }
                recover::Course::WriteBack => {
                    self.put_it_back(session, outside);
                    continue;
                }
                recover::Course::Ask => {}
            }
            outside.channel.ask(&Question {
                about: about.clone(),
                summary: format!("{name} was saved after you closed the session."),
                detail: vec![
                    format!("It is {state}."),
                    format!("Into {}.", slpc::display_path(&session.record().container)),
                ],
                choices: vec![Choice::WriteBack, Choice::Discard, Choice::Reveal],
            });
            self.pending.push(Pending {
                about,
                session,
                then_open: None,
                asked: Instant::now(),
            });
        }
        self.lingering = still_waiting;
    }

    /// Do what somebody chose.
    fn act_on_answers(&mut self, outside: &Outside<'_>) {
        for answer in outside.channel.answers() {
            self.settle(&answer, outside);
        }
    }

    fn settle(&mut self, answer: &Answer, outside: &Outside<'_>) {
        let Some(at) = self.pending.iter().position(|p| p.about == answer.about) else {
            // A button from a question this instance is no longer holding: the
            // same decision taken at the command line in the meantime, or a
            // notification that outlived the process that asked. Saying so
            // beats a click that appears to do nothing.
            outside.report(&Report::ordinary(format!(
                "{} has already been dealt with.",
                answer.about
            )));
            return;
        };

        // Reveal is *not yet* rather than an answer, so the question stays and
        // is put again. A service that closes a notification when one of its
        // actions is invoked — which GNOME Shell does — would otherwise leave
        // somebody looking at the folder with no way back to the decision.
        if answer.choice == Choice::Reveal {
            let pending = &mut self.pending[at];
            pending.asked = Instant::now();
            let dir = pending.session.payload_dir();
            let question = Question {
                about: pending.about.clone(),
                summary: format!(
                    "{} is still waiting.",
                    slpc::display_name(&pending.session.record().payload)
                ),
                detail: vec![format!("The payload is in {}", dir.display())],
                choices: vec![Choice::WriteBack, Choice::Discard, Choice::Reveal],
            };
            if let Err(e) = outside.launcher.launch(&dir) {
                outside.report(&Report::ordinary(format!(
                    "{} could not be shown: {e}",
                    dir.display()
                )));
            }
            outside.channel.ask(&question);
            return;
        }

        let mut pending = self.pending.remove(at);
        outside.channel.withdraw(&pending.about);
        // Owned, because what follows moves the session out from under it.
        let name = slpc::display_name(&pending.session.record().payload).into_owned();
        match answer.choice {
            Choice::WriteBack => match crate::writeback::write_back(&mut pending.session) {
                Ok(()) => {
                    outside.report(&Report::ordinary(format!(
                        "{name} written back to {}.",
                        slpc::display_path(&pending.session.record().container)
                    )));
                    let _ = pending.session.remove();
                }
                Err(e) => {
                    // Nothing is removed. The session stays where it
                    // was, which is what makes a second attempt possible, and
                    // the question goes back so there is something to make it
                    // with.
                    outside.report(
                        &Report::interrupt(format!("{name} could not be written back."))
                            .and(e.to_string()),
                    );
                    pending.asked = Instant::now();
                    self.pending.push(pending);
                    return;
                }
            },
            Choice::Discard => {
                let _ = pending.session.remove();
                outside.report(&Report::ordinary(format!("{name} discarded.")));
            }
            Choice::Reveal => unreachable!("answered above"),
        }

        // Concept 8: the new session follows the answer.
        if let Some(container) = pending.then_open {
            if let Response::Err(why) = self.open(&container, Voice::Instance, outside) {
                outside.report(&Report::interrupt(format!("{name} did not open: {why}")));
            }
        }
    }

    /// Take back the questions nobody has answered inside [`HELD`], and say
    /// where the same decision still lives.
    fn let_go_of_the_unanswered(&mut self, outside: &Outside<'_>) {
        let (gone, kept) = std::mem::take(&mut self.pending)
            .into_iter()
            .partition::<Vec<_>, _>(|p| {
                self.holds_for.is_some_and(|held| p.asked.elapsed() >= held)
            });
        self.pending = kept;
        for p in &gone {
            Self::stop_asking(p, outside);
        }
    }

    /// Withdraw one question and leave the command line in its place.
    fn stop_asking(pending: &Pending, outside: &Outside<'_>) {
        outside.channel.withdraw(&pending.about);
        outside.report(
            &Report::ordinary(format!(
                "{} is still undecided.",
                slpc::display_name(&pending.session.record().payload)
            ))
            .and(format!(
                "slipcase-open recover {} --write-back",
                pending.about
            ))
            .and(format!("slipcase-open recover {} --discard", pending.about)),
        );
    }

    /// Close every open session, and put down every question, for a shutdown
    /// that is not a crash.
    ///
    /// A question left standing when this process goes is a button with nobody
    /// behind it, so each is withdrawn and replaced by the two commands that
    /// reach the same decision. The session directories stay: concept 6.3
    /// carries them to the next launch, which is where they were always going
    /// to be answered if nobody answered here.
    pub fn stand_down(&mut self, outside: &Outside<'_>) {
        for open in self.sessions.drain().collect::<Vec<_>>() {
            match open.close() {
                Ok(flow::Closed::Cleared) => {}
                Ok(flow::Closed::LeftForRecovery(lingering)) => self.lingering.push(*lingering),
                Err(e) => {
                    outside.report(&Report::interrupt(format!("a session did not close: {e}")));
                }
            }
        }
        for lingering in std::mem::take(&mut self.lingering) {
            let session = lingering.into_session();
            if recover::state(&session).is_quiet() {
                let _ = session.remove();
            } else {
                outside.report(
                    &Report::ordinary(format!(
                        "{} was closed while its application was still working.",
                        slpc::display_name(&session.record().payload)
                    ))
                    .and("It is left for recovery: run `slipcase-open sessions`."),
                );
            }
        }
        for pending in &std::mem::take(&mut self.pending) {
            Self::stop_asking(pending, outside);
        }
    }
}

/// The name `list` prints and `close` takes back.
fn id_of(s: &Session) -> String {
    s.dir()
        .file_name()
        .map_or_else(|| "?".to_string(), |n| n.to_string_lossy().into_owned())
}

/// A report said once: through the channel where the client has nowhere to show
/// it, and back to the client where it has.
fn say(voice: Voice, outside: &Outside<'_>, report: Report) -> Response {
    if voice == Voice::Instance {
        outside.report(&report);
    }
    Response::Ok(
        std::iter::once(report.summary)
            .chain(report.detail.into_iter().map(|d| format!("  {d}")))
            .collect(),
    )
}

/// A refusal said once, on the same rule.
///
/// Weighted as an interrupt through the channel. A refusal answers something
/// somebody just double-clicked, and concept 5.1's extensionless case is one of
/// the things it can be: a quiet refusal there reads as the document simply not
/// opening.
fn refuse(voice: Voice, outside: &Outside<'_>, why: String) -> Response {
    if voice == Voice::Instance {
        outside.report(&Report::interrupt("Not opened.").and(why.clone()));
    }
    Response::Err(why)
}

/// Remove the sessions left behind that have nothing to say.
///
/// Concept 6.3: a recovered payload matching its container means nothing was
/// lost, so clean up and say nothing.
///
/// **This could not be done before Phase 2 and that is why it was not.** A
/// session that is open and not yet edited reads as unchanged, and no process
/// could tell a live session from a dead one — a sweep run from a second
/// terminal would have deleted a directory out from under a running editor.
/// `live` is what the resident instance knows and nothing else did.
///
/// # Errors
///
/// Where the session root cannot be read. A session that will not go is left
/// rather than reported: it is debris, the next sweep will try again, and
/// failing a launch over it would be the tail wagging the dog.
pub fn sweep(root: &Path, live: &[PathBuf]) -> io::Result<usize> {
    let mut removed = 0;
    for s in session::scan(root)? {
        if live.iter().any(|d| d == s.dir()) {
            continue;
        }
        // `is_quiet` and not `!needs_a_person`: an edit that never landed now
        // takes neither course, and the negation would have swept it.
        if !recover::state(&s).is_quiet() {
            continue;
        }
        if s.remove().is_ok() {
            removed += 1;
        }
    }
    Ok(removed)
}

/// Hold the sessions and serve the front door until nothing is left.
///
/// # Errors
///
/// Where the endpoint cannot be served.
pub fn run(
    listener: crate::endpoint::Listener,
    resident: &mut Resident,
    outside: &Outside<'_>,
    standing: &dyn crate::present::Standing,
) -> io::Result<()> {
    // A question with somewhere to keep being seen is not on a clock. See
    // `HELD`, whose reasoning is entirely about a process staying alive for a
    // question that has nowhere else to live.
    if standing.holding() {
        resident.let_questions_stand();
    }
    let mut shown: Vec<crate::present::Listed> = Vec::new();
    let mut carried: Vec<crate::present::Trouble> = Vec::new();
    let mut wearing = crate::present::Mood::Settled;
    let (tx, rx) = std::sync::mpsc::channel();
    std::thread::spawn(move || {
        // A caller that went away between connecting and being read is not an
        // event: `flatten` drops it and takes the next.
        for stream in listener.incoming().flatten() {
            if tx.send(stream).is_err() {
                return;
            }
        }
    });

    loop {
        match rx.recv_timeout(TICK) {
            Ok(mut stream) => {
                let response = match crate::ipc::take(&mut stream) {
                    Ok(request) => resident.handle(request, outside),
                    // A request this build cannot read is answered rather than
                    // dropped, so a client waiting on the front door is not
                    // left waiting on it.
                    Err(e) => Response::Err(e.to_string()),
                };
                let _ = crate::ipc::answer(&mut stream, &response);
            }
            Err(std::sync::mpsc::RecvTimeoutError::Timeout) => {}
            // The accepting thread has gone, which means the listener has.
            Err(std::sync::mpsc::RecvTimeoutError::Disconnected) => break,
        }

        resident.turn(outside);

        // Concept 12's standing list, told what to say and asked what was
        // said back. Only when it changed: this is every 250 milliseconds, and
        // all three are the same on almost all of them.
        //
        // The mood is in that comparison and it is the one that moves on its
        // own — [`PULSE`] expires with nothing else happening — which is what
        // takes the icon back to blue after a save without needing a timer of
        // its own anywhere.
        let listed = resident.listed();
        let troubles = resident.troubles().to_vec();
        let mood = resident.mood();
        if (&listed, &troubles, mood) != (&shown, &carried, wearing) {
            standing.show(&listed, &troubles, mood);
            shown = listed;
            carried = troubles;
            wearing = mood;
        }
        let mut leaving = false;
        for chosen in standing.taken() {
            match chosen {
                // Somebody has read it. Nothing else happens: the trouble was
                // the record that it happened at all, and putting it down is
                // the person saying they have the record now.
                crate::present::Chosen::Dismiss(id) => resident.dismiss(&id),
                // The same ending as interrupting the command line, and the
                // menu item says so: every session stays where it is and stays
                // recoverable.
                crate::present::Chosen::Quit => leaving = true,
            }
        }
        if leaving {
            break;
        }

        // Concept 8's exit rule: nothing open, nothing lingering, nothing
        // waiting on somebody. Staying resident does nothing for the crash
        // case, where this process is dead by definition.
        //
        // **Where there is a standing list the rule does not apply**, and that
        // is a change rather than an exception — see
        // [`crate::present::Standing::holding`]. The rule was written for a
        // process with no face: nothing for it to be, so no reason for it to
        // be. An icon gives warnings somewhere to live, and a warning raised by
        // a process on its way out has nowhere to go.
        if resident.is_idle() && !standing.holding() {
            break;
        }
    }
    Ok(())
}

#[cfg(test)]
mod tests {
    use super::{sweep, Resident};
    use crate::ipc::{Request, Response, Voice};
    use crate::outside::Outside;
    use crate::platform::testing::Recording;
    use crate::policy::{Origin, Read, Source};
    use crate::present::testing::Recording as Told;
    use crate::present::Choice;
    use crate::{extract, recover, session};
    use std::fs;
    use std::path::{Path, PathBuf};
    use std::time::Duration;

    struct Default_;
    impl Source for Default_ {
        fn layer(&self, _o: Origin) -> Read {
            Ok(None)
        }
    }

    /// The three things the engine works against, kept together so a test can
    /// hand them over as one and then ask each of them what it saw.
    struct World {
        policy: Default_,
        launcher: Recording,
        channel: Told,
    }

    impl World {
        fn new() -> Self {
            Self {
                policy: Default_,
                launcher: Recording::default(),
                channel: Told::default(),
            }
        }

        fn outside(&self) -> Outside<'_> {
            Outside::new(&self.policy, &self.launcher, &self.channel)
        }

        /// The same, with the routine reports let through.
        fn loud(&self) -> Outside<'_> {
            self.outside().saying(crate::policy::Notify::Everything)
        }
    }

    /// An `open` from somewhere with a terminal, which is what most of these
    /// are: the response is the assertion.
    fn opening(container: PathBuf) -> Request {
        Request::Open {
            container,
            voice: Voice::Client,
        }
    }

    /// An `open` from a double-click, where the instance has to speak.
    fn announcing(container: PathBuf) -> Request {
        Request::Open {
            container,
            voice: Voice::Instance,
        }
    }

    fn container(at: &Path, name: &str, payload: &[u8]) -> PathBuf {
        let doc: slpc::toml_edit::DocumentMut =
            format!("slipcase_version = \"1.0\"\n\n[payload]\nfile = \"{name}\"\n")
                .parse()
                .unwrap();
        let path = at.join(format!("{name}.slpc"));
        slpc::pack_reader(name, payload, doc, fs::File::create(&path).unwrap()).unwrap();
        path
    }

    fn ok(r: Response) -> Vec<String> {
        match r {
            Response::Ok(lines) => lines,
            Response::Err(e) => panic!("{e}"),
        }
    }

    fn err(r: Response) -> String {
        match r {
            Response::Err(e) => e,
            Response::Ok(lines) => panic!("expected a refusal, got {lines:?}"),
        }
    }

    /// The session name the `open` response gives back.
    fn session_named_in(lines: &[String]) -> String {
        lines
            .iter()
            .find_map(|l| l.strip_prefix("  Session "))
            .unwrap_or_else(|| panic!("no session named in {lines:?}"))
            .to_string()
    }

    /// A session left behind by a process that died with an edit in it.
    ///
    /// Its container has not moved, so concept 6.3 as amended puts the edit
    /// back without asking. Use [`a_diverged_session`] for the tests that are
    /// about the question.
    fn a_crashed_session(root: &Path, c: &Path, name: &str, edit: &[u8]) -> session::Session {
        let mut left = session::create(root, c, name).unwrap();
        extract::extract(&mut slpc::Container::open(c).unwrap(), &mut left).unwrap();
        fs::write(left.payload_path(), edit).unwrap();
        left
    }

    /// What `a_diverged_session` leaves in the container, for the tests that
    /// have to say what the container holds afterwards.
    const SOMEBODY_ELSE: &[u8] = b"what somebody else put there in the meantime";

    /// The same, and then somebody else repacks the container while the session
    /// is not running.
    ///
    /// **The only shape that still raises concept 6.3's question**, because it
    /// is the only one where both sides hold work the other does not and no
    /// answer is obviously right.
    fn a_diverged_session(root: &Path, c: &Path, name: &str, edit: &[u8]) -> session::Session {
        let left = a_crashed_session(root, c, name, edit);
        container(c.parent().unwrap(), name, SOMEBODY_ELSE);
        left
    }

    #[test]
    fn opening_a_container_twice_brings_the_session_forward() {
        // Concept 8. Two sessions would both repack it and the second
        // write-back would overwrite the first with nothing said.
        let tmp = tempfile::tempdir().unwrap();
        let root = tmp.path().join("sessions");
        let c = container(tmp.path(), "report.pdf", b"first");
        let w = World::new();
        let mut r = Resident::new(&root);

        ok(r.handle(opening(c.clone()), &w.outside()));
        let again = ok(r.handle(opening(c.clone()), &w.outside()));

        assert!(again[0].contains("already open"), "{again:?}");
        assert_eq!(session::scan(&root).unwrap().len(), 1);
        // Brought forward means launched again, which is what a second
        // double-click does everywhere else.
        assert_eq!(w.launcher.launched().len(), 2);
    }

    #[cfg(unix)]
    #[test]
    fn the_same_container_under_another_hard_link_is_the_same_session() {
        let tmp = tempfile::tempdir().unwrap();
        let root = tmp.path().join("sessions");
        let c = container(tmp.path(), "report.pdf", b"first");
        let link = tmp.path().join("other-name.slpc");
        fs::hard_link(&c, &link).unwrap();
        let w = World::new();
        let mut r = Resident::new(&root);

        ok(r.handle(opening(c), &w.outside()));
        let again = ok(r.handle(opening(link), &w.outside()));
        assert!(again[0].contains("already open"), "{again:?}");
        assert_eq!(session::scan(&root).unwrap().len(), 1);
    }

    #[test]
    fn two_different_containers_get_two_sessions() {
        let tmp = tempfile::tempdir().unwrap();
        let root = tmp.path().join("sessions");
        let a = container(tmp.path(), "report.pdf", b"a");
        let b = container(tmp.path(), "notes.txt", b"b");
        let w = World::new();
        let mut r = Resident::new(&root);

        ok(r.handle(opening(a), &w.outside()));
        ok(r.handle(opening(b), &w.outside()));
        assert_eq!(session::scan(&root).unwrap().len(), 2);
        assert!(!r.is_idle());
    }

    #[test]
    fn a_session_survives_a_write_back_still_being_the_same_container() {
        // A session is still the same session after it has saved, even though
        // the write-back renamed a new file over the container and so gave it a
        // new inode. This passes on the path arm alone — checked by reverting
        // `refresh` and watching it stay green — so what it pins is that a save
        // does not lose a session, not that `refresh` works. The identity arm
        // is covered where it can be seen: `table::refreshing_keeps_the_
        // identity_arm_working_after_a_save`, which reaches the container
        // through a hard link made after the save and does fail without it.
        let tmp = tempfile::tempdir().unwrap();
        let root = tmp.path().join("sessions");
        let c = container(tmp.path(), "report.pdf", b"first");
        let w = World::new();
        let mut r = Resident::new(&root);

        ok(r.handle(opening(c.clone()), &w.outside()));
        let payload = session::scan(&root).unwrap()[0].payload_path();
        fs::write(&payload, b"edited").unwrap();

        // Watched through the record rather than through what was said. The
        // write-back count is what `pump` guarantees; a notification is a
        // presentation choice that a threshold may now drop.
        let deadline = std::time::Instant::now() + Duration::from_secs(10);
        let saved = || session::scan(&root).unwrap()[0].record().write_backs;
        while std::time::Instant::now() < deadline && saved() == 0 {
            r.turn(&w.outside());
        }
        assert!(saved() >= 1, "nothing was written back");

        let again = ok(r.handle(opening(c), &w.outside()));
        assert!(again[0].contains("already open"), "{again:?}");
        assert_eq!(session::scan(&root).unwrap().len(), 1);
    }

    #[test]
    fn a_recovery_item_on_the_same_container_is_asked_about_first() {
        // Concept 8. Opening a fresh session would extract the container's
        // current payload and leave the recovered edit with nowhere to go.
        let tmp = tempfile::tempdir().unwrap();
        let root = tmp.path().join("sessions");
        let c = container(tmp.path(), "report.pdf", b"first");
        let left = a_diverged_session(&root, &c, "report.pdf", b"edited then the process died");

        let w = World::new();
        let mut r = Resident::new(&root);
        let refused = err(r.handle(opening(c), &w.outside()));

        assert!(refused.contains("left behind"), "{refused}");
        assert!(
            w.launcher.launched().is_empty(),
            "nothing should have opened"
        );
        assert_eq!(session::scan(&root).unwrap().len(), 1);

        // Concept 9 turns the Phase 2 refusal into a question, and it has to
        // carry all three of concept 6.3's answers.
        let asked = w.channel.questions();
        assert_eq!(asked.len(), 1);
        assert!(asked[0]
            .about
            .starts_with(left.dir().file_name().unwrap().to_str().unwrap()));
        assert_eq!(
            asked[0].choices,
            vec![Choice::WriteBack, Choice::Discard, Choice::Reveal]
        );
        // And the instance is holding it, which is what makes an answer
        // actionable rather than a button with nobody behind it.
        assert!(!r.is_idle());
    }

    #[test]
    fn one_question_is_asked_however_many_times_the_container_is_double_clicked() {
        let tmp = tempfile::tempdir().unwrap();
        let root = tmp.path().join("sessions");
        let c = container(tmp.path(), "report.pdf", b"first");
        a_diverged_session(&root, &c, "report.pdf", b"edited");

        let w = World::new();
        let mut r = Resident::new(&root);
        err(r.handle(opening(c.clone()), &w.outside()));
        err(r.handle(opening(c), &w.outside()));
        assert_eq!(w.channel.questions().len(), 1);
    }

    #[test]
    fn writing_back_a_recovered_session_opens_the_one_that_was_waiting() {
        // Concept 8: the new session follows the answer, so nobody has to
        // double-click the container a second time.
        let tmp = tempfile::tempdir().unwrap();
        let root = tmp.path().join("sessions");
        let c = container(tmp.path(), "report.pdf", b"first");
        a_diverged_session(&root, &c, "report.pdf", b"the edit that never landed");

        let w = World::new();
        let mut r = Resident::new(&root);
        err(r.handle(opening(c.clone()), &w.outside()));
        let about = w.channel.questions()[0].about.clone();

        w.channel.answer(&about, Choice::WriteBack);
        r.turn(&w.outside());

        // The edit is in the container, the question has been taken back, and
        // the session that was waiting on the answer is open.
        let mut held = slpc::Container::open(&c).unwrap();
        let mut bytes = Vec::new();
        std::io::Read::read_to_end(&mut held.payload().unwrap(), &mut bytes).unwrap();
        assert_eq!(bytes, b"the edit that never landed");
        assert_eq!(w.channel.withdrawn(), vec![about]);
        assert_eq!(w.launcher.launched().len(), 1);
        assert_eq!(session::scan(&root).unwrap().len(), 1);
    }

    #[test]
    fn discarding_a_recovered_session_opens_the_one_that_was_waiting() {
        let tmp = tempfile::tempdir().unwrap();
        let root = tmp.path().join("sessions");
        let c = container(tmp.path(), "report.pdf", b"first");
        a_diverged_session(&root, &c, "report.pdf", b"edited");

        let w = World::new();
        let mut r = Resident::new(&root);
        err(r.handle(opening(c.clone()), &w.outside()));
        let about = w.channel.questions()[0].about.clone();

        w.channel.answer(&about, Choice::Discard);
        r.turn(&w.outside());

        // Not asserted by the directory being gone: a session is named for the
        // second it started in, so the one that follows the answer takes the
        // same name back. What says the edit was discarded is that the session
        // now on disk holds the container's payload rather than the edit.
        let now = session::scan(&root).unwrap();
        assert_eq!(now.len(), 1);
        assert_eq!(fs::read(now[0].payload_path()).unwrap(), SOMEBODY_ELSE);
        let mut held = slpc::Container::open(&c).unwrap();
        let mut bytes = Vec::new();
        std::io::Read::read_to_end(&mut held.payload().unwrap(), &mut bytes).unwrap();
        assert_eq!(bytes, SOMEBODY_ELSE, "discard must not touch the container");
        assert_eq!(w.launcher.launched().len(), 1);
    }

    #[test]
    fn revealing_shows_the_folder_and_puts_the_question_again() {
        // Reveal is *not yet* rather than an answer. A service that closes a
        // notification when one of its actions is invoked would otherwise leave
        // somebody looking at a folder with no way back to the decision.
        let tmp = tempfile::tempdir().unwrap();
        let root = tmp.path().join("sessions");
        let c = container(tmp.path(), "report.pdf", b"first");
        let left = a_diverged_session(&root, &c, "report.pdf", b"edited");

        let w = World::new();
        let mut r = Resident::new(&root);
        err(r.handle(opening(c), &w.outside()));
        let about = w.channel.questions()[0].about.clone();

        w.channel.answer(&about, Choice::Reveal);
        r.turn(&w.outside());

        assert_eq!(w.launcher.launched(), vec![left.payload_dir()]);
        assert_eq!(w.channel.questions().len(), 2);
        assert!(w.channel.withdrawn().is_empty());
        assert!(left.dir().exists());
        assert!(!r.is_idle());
    }

    #[test]
    fn an_answer_about_a_session_nobody_is_holding_says_so() {
        let tmp = tempfile::tempdir().unwrap();
        let w = World::new();
        let mut r = Resident::new(tmp.path().join("sessions"));
        w.channel.answer("gone-0", Choice::WriteBack);
        r.turn(&w.outside());
        assert!(
            w.channel.said().contains("already been dealt with"),
            "{}",
            w.channel.said()
        );
    }

    #[test]
    fn a_question_nobody_answers_is_taken_back_and_replaced_by_the_commands() {
        // Concept 8 says to bound the linger. What it costs is that the buttons
        // stop working, so they are removed rather than left to do nothing, and
        // what replaces them reaches the same decision.
        let tmp = tempfile::tempdir().unwrap();
        let root = tmp.path().join("sessions");
        let c = container(tmp.path(), "report.pdf", b"first");
        let left = a_diverged_session(&root, &c, "report.pdf", b"edited");

        let w = World::new();
        let mut r = Resident::new(&root).waiting(Duration::ZERO, Duration::ZERO);
        err(r.handle(opening(c), &w.outside()));
        let about = w.channel.questions()[0].about.clone();

        r.turn(&w.outside());

        assert_eq!(w.channel.withdrawn(), vec![about.clone()]);
        assert!(w
            .channel
            .said()
            .contains(&format!("recover {about} --write-back")));
        assert!(w
            .channel
            .said()
            .contains(&format!("recover {about} --discard")));
        // The session itself stays. Concept 6.3 carries it to the next launch.
        assert!(left.dir().exists());
        assert!(r.is_idle());
    }

    #[test]
    fn a_question_with_somewhere_to_live_is_not_taken_back() {
        // The same setup as above, and the opposite outcome, because something
        // is standing that can keep showing it. `HELD` exists to stop a process
        // sitting there for a question nobody can see; where an icon is up the
        // process is staying anyway, so the limit would only throw the question
        // away — and naming two command lines in its place is the dead end that
        // made the icon look necessary to begin with.
        let tmp = tempfile::tempdir().unwrap();
        let root = tmp.path().join("sessions");
        let c = container(tmp.path(), "report.pdf", b"first");
        let left = a_diverged_session(&root, &c, "report.pdf", b"edited");

        let w = World::new();
        let mut r = Resident::new(&root).waiting(Duration::ZERO, Duration::ZERO);
        err(r.handle(opening(c), &w.outside()));
        let about = w.channel.questions()[0].about.clone();

        r.let_questions_stand();
        for _ in 0..5 {
            r.turn(&w.outside());
        }

        assert!(
            w.channel.withdrawn().is_empty(),
            "the question was taken back: {:?}",
            w.channel.withdrawn()
        );
        assert!(
            !w.channel.said().contains(&format!("recover {about}")),
            "it fell back to the command line while a surface was showing it"
        );
        assert!(left.dir().exists());
        // And it is still the instance's to act on, which is the point: the
        // buttons in front of somebody still reach a question that is here.
        assert!(!r.is_idle());
        assert_eq!(r.mood(), crate::present::Mood::Look);
    }

    #[test]
    fn a_leftover_edit_goes_back_and_the_open_carries_on() {
        // The complaint this whole amendment came from: an edit was saved, the
        // process that was watching stopped, and reopening the container put a
        // question in the way instead of the document. The container has not
        // moved, so the edit is the person's own — it goes back, and the open
        // proceeds in the same breath rather than waiting on an answer.
        let tmp = tempfile::tempdir().unwrap();
        let root = tmp.path().join("sessions");
        let c = container(tmp.path(), "report.pdf", b"first");
        a_crashed_session(&root, &c, "report.pdf", b"the edit that never landed");

        let w = World::new();
        let mut r = Resident::new(&root);
        let opened = ok(r.handle(opening(c.clone()), &w.loud()));

        assert!(
            w.channel.questions().is_empty(),
            "the person was asked about their own save: {:?}",
            w.channel.questions()
        );
        assert!(
            opened.iter().any(|l| l.contains("is open")),
            "the open did not carry on: {opened:?}"
        );
        assert_eq!(w.launcher.launched().len(), 1);

        // The edit reached the container, and the session that carried it is
        // gone rather than left to be listed as needing a decision.
        let mut held = slpc::Container::open(&c).unwrap();
        let mut bytes = Vec::new();
        std::io::Read::read_to_end(&mut held.payload().unwrap(), &mut bytes).unwrap();
        assert_eq!(bytes, b"the edit that never landed");
        assert!(w.channel.said().contains("recovered and written back"));
        // One session on disk: the new one, holding what the container now has.
        let now = session::scan(&root).unwrap();
        assert_eq!(now.len(), 1, "{now:?}");
        assert_eq!(
            fs::read(now[0].payload_path()).unwrap(),
            b"the edit that never landed"
        );
    }

    #[test]
    fn a_write_back_that_fails_on_recovery_keeps_the_session_and_colours_the_icon() {
        // The edit is the one thing that must not be dropped on the floor. A
        // failed recovery write-back leaves the session where it was, so a
        // second attempt is possible, and puts the icon in the state that means
        // *work is not in its container*.
        let tmp = tempfile::tempdir().unwrap();
        let root = tmp.path().join("sessions");
        let c = container(tmp.path(), "report.pdf", b"first");
        let left = a_crashed_session(&root, &c, "report.pdf", b"an edit worth keeping");

        // The container has to stay *readable*, or `recover` answers
        // `Unreadable` and asks rather than reaching the write-back at all.
        // Both of these, because the two platforms refuse in different places:
        // Windows will not rename over a read-only file, and Unix will not
        // create the temporary beside it in a directory it cannot write.
        let held = tmp.path().join("held");
        fs::create_dir(&held).unwrap();
        let c = {
            let moved = held.join("report.pdf.slpc");
            fs::rename(&c, &moved).unwrap();
            moved
        };
        // The record still names the old path, so put the session back onto
        // this one by rebuilding it there.
        fs::remove_dir_all(left.dir()).unwrap();
        let left = a_crashed_session(&root, &c, "report.pdf", b"an edit worth keeping");
        readonly(&c, true);
        readonly(&held, true);

        let w = World::new();
        let mut r = Resident::new(&root);
        let _ = r.handle(opening(c.clone()), &w.outside());

        readonly(&held, false);
        readonly(&c, false);

        assert!(left.dir().exists(), "the edit was thrown away");
        assert_eq!(
            fs::read(left.payload_path()).unwrap(),
            b"an edit worth keeping"
        );
        assert_eq!(
            r.mood(),
            crate::present::Mood::AtRisk,
            "an edit that is nowhere but a session directory is what orange is for"
        );
    }

    /// Make a path refuse writes, and let it accept them again.
    fn readonly(at: &Path, yes: bool) {
        let mut perms = fs::metadata(at).unwrap().permissions();
        #[cfg(unix)]
        {
            use std::os::unix::fs::PermissionsExt as _;
            perms.set_mode(if yes { 0o500 } else { 0o700 });
        }
        #[cfg(not(unix))]
        perms.set_readonly(yes);
        fs::set_permissions(at, perms).unwrap();
    }

    #[test]
    fn a_quiet_leftover_does_not_stand_in_the_way() {
        // Only a recovery item worth asking about blocks. One that matches its
        // container has nothing to lose and should not stop somebody working.
        let tmp = tempfile::tempdir().unwrap();
        let root = tmp.path().join("sessions");
        let c = container(tmp.path(), "report.pdf", b"first");
        let mut left = session::create(&root, &c, "report.pdf").unwrap();
        extract::extract(&mut slpc::Container::open(&c).unwrap(), &mut left).unwrap();
        assert!(matches!(recover::state(&left), recover::State::Unchanged));

        let w = World::new();
        let mut r = Resident::new(&root);
        ok(r.handle(opening(c), &w.outside()));
        assert_eq!(w.launcher.launched().len(), 1);
        assert!(w.channel.questions().is_empty());
    }

    #[test]
    fn closing_by_name_closes_that_session() {
        let tmp = tempfile::tempdir().unwrap();
        let root = tmp.path().join("sessions");
        let c = container(tmp.path(), "report.pdf", b"first");
        let w = World::new();
        let mut r = Resident::new(&root);

        let opened = ok(r.handle(opening(c), &w.outside()));
        ok(r.handle(Request::Close(session_named_in(&opened)), &w.outside()));
        assert!(r.is_idle());
        assert!(session::scan(&root).unwrap().is_empty());
    }

    #[test]
    fn closing_while_the_application_is_working_keeps_the_watch_on_it() {
        // Concept 6.2 and 8. The close is honoured, the directory stays, and
        // the process keeps watching so the application's last save is noticed
        // when it happens rather than at the next launch.
        let tmp = tempfile::tempdir().unwrap();
        let root = tmp.path().join("sessions");
        let c = container(tmp.path(), "report.pdf", b"first");
        let w = World::new();
        let mut r = Resident::new(&root);

        let opened = ok(r.handle(opening(c), &w.outside()));
        let dir = session::scan(&root).unwrap()[0].payload_dir();
        fs::write(dir.join(".~lock.report.pdf#"), b"still working").unwrap();

        let closed = ok(r.handle(Request::Close(session_named_in(&opened)), &w.outside()));
        assert!(
            closed
                .iter()
                .any(|l| l.contains("still has the payload open")),
            "{closed:?}"
        );
        assert!(
            !r.is_idle(),
            "the process has to stay for the watch to be worth anything"
        );
        assert_eq!(session::scan(&root).unwrap().len(), 1);
    }

    #[test]
    fn a_lingering_session_saved_after_the_close_is_written_back_not_asked_about() {
        // The most ordinary reason to be here at all: somebody pressed Save on
        // the way out. Concept 6.3 as amended — the container has not moved, so
        // the save is theirs and it goes home. Putting it to them as *write
        // back, discard, or reveal the folder* would be this tool's own timing
        // handed back to them as a decision.
        let tmp = tempfile::tempdir().unwrap();
        let root = tmp.path().join("sessions");
        let c = container(tmp.path(), "report.pdf", b"first");
        let w = World::new();
        let mut r = Resident::new(&root).waiting(Duration::ZERO, Duration::from_secs(300));

        let opened = ok(r.handle(opening(c.clone()), &w.outside()));
        let dir = session::scan(&root).unwrap()[0].payload_dir();
        let sibling = dir.join(".~lock.report.pdf#");
        fs::write(&sibling, b"still working").unwrap();
        ok(r.handle(Request::Close(session_named_in(&opened)), &w.outside()));

        // The application's last save, and then it tidies up after itself.
        fs::write(dir.join("report.pdf"), b"the last save").unwrap();
        fs::remove_file(&sibling).unwrap();
        r.turn(&w.loud());

        assert!(
            w.channel.questions().is_empty(),
            "{:?}",
            w.channel.questions()
        );
        // It reached the container, which is the whole point.
        let mut held = slpc::Container::open(&c).unwrap();
        let mut bytes = Vec::new();
        std::io::Read::read_to_end(&mut held.payload().unwrap(), &mut bytes).unwrap();
        assert_eq!(bytes, b"the last save");
        // Said rather than done silently: the risk this accepts is a save that
        // was half-written, and being able to see what happened is the only
        // defence left against it.
        assert!(
            w.channel.said().contains("recovered and written back"),
            "{}",
            w.channel.said()
        );
        assert!(session::scan(&root).unwrap().is_empty());
        assert!(r.is_idle());
    }

    #[test]
    fn a_lingering_session_that_matches_its_container_goes_quietly() {
        // Concept 6.3: equal means nothing was lost, so clean up and say
        // nothing. This is that case arriving while the process is still alive
        // to see it, rather than at the next launch.
        let tmp = tempfile::tempdir().unwrap();
        let root = tmp.path().join("sessions");
        let c = container(tmp.path(), "report.pdf", b"first");
        let w = World::new();
        let mut r = Resident::new(&root).waiting(Duration::ZERO, Duration::from_secs(300));

        let opened = ok(r.handle(opening(c), &w.outside()));
        let dir = session::scan(&root).unwrap()[0].payload_dir();
        let sibling = dir.join(".~lock.report.pdf#");
        fs::write(&sibling, b"still working").unwrap();
        ok(r.handle(Request::Close(session_named_in(&opened)), &w.outside()));

        fs::remove_file(&sibling).unwrap();
        r.turn(&w.outside());

        assert!(
            w.channel.questions().is_empty(),
            "{:?}",
            w.channel.questions()
        );
        assert!(session::scan(&root).unwrap().is_empty());
        assert!(r.is_idle());
    }

    #[test]
    fn closing_a_session_that_is_not_open_says_so() {
        let tmp = tempfile::tempdir().unwrap();
        let w = World::new();
        let mut r = Resident::new(tmp.path().join("sessions"));
        let refused = err(r.handle(Request::Close("nothing-0".into()), &w.outside()));
        assert!(refused.contains("no open session"), "{refused}");
    }

    #[test]
    fn a_double_click_is_spoken_for_and_a_terminal_is_not() {
        // Concept 9. An invocation with nowhere to print is why the instance
        // has a channel at all, and one with a terminal of its own would
        // otherwise hear everything twice.
        let tmp = tempfile::tempdir().unwrap();
        let root = tmp.path().join("sessions");
        let quiet = container(tmp.path(), "quiet.pdf", b"a");
        let loud = container(tmp.path(), "loud.pdf", b"b");
        let w = World::new();
        let mut r = Resident::new(&root);

        ok(r.handle(opening(quiet), &w.loud()));
        assert!(w.channel.reports().is_empty(), "{:?}", w.channel.reports());

        ok(r.handle(announcing(loud), &w.loud()));
        assert!(
            w.channel.said().contains("loud.pdf is open"),
            "{}",
            w.channel.said()
        );
    }

    #[test]
    fn listing_shows_what_is_open_and_what_was_left() {
        let tmp = tempfile::tempdir().unwrap();
        let root = tmp.path().join("sessions");
        let open_one = container(tmp.path(), "report.pdf", b"a");
        let crashed = container(tmp.path(), "notes.txt", b"b");
        a_crashed_session(&root, &crashed, "notes.txt", b"edited");

        let w = World::new();
        let mut r = Resident::new(&root);
        ok(r.handle(opening(open_one), &w.outside()));

        let lines = ok(r.handle(Request::List, &w.outside()));
        assert!(lines
            .iter()
            .any(|l| l.contains("report.pdf") && l.contains("open")));
        assert!(lines
            .iter()
            .any(|l| l.contains("notes.txt") && l.contains("edited")));
        // And each of them once. A session the instance is holding is also on
        // disk, so a list that read both without checking would double it.
        assert_eq!(lines.len(), 2, "{lines:?}");
    }

    #[test]
    fn the_sweep_takes_the_quiet_ones_and_leaves_the_rest() {
        let tmp = tempfile::tempdir().unwrap();
        let root = tmp.path().join("sessions");
        let a = container(tmp.path(), "quiet.pdf", b"a");
        let b = container(tmp.path(), "edited.pdf", b"b");

        let mut quiet = session::create(&root, &a, "quiet.pdf").unwrap();
        extract::extract(&mut slpc::Container::open(&a).unwrap(), &mut quiet).unwrap();

        let edited = a_crashed_session(&root, &b, "edited.pdf", b"an edit that never landed");
        let half_made = session::create(&root, &a, "quiet.pdf").unwrap();

        assert_eq!(sweep(&root, &[]).unwrap(), 2);
        let left = session::scan(&root).unwrap();
        assert_eq!(left.len(), 1);
        assert_eq!(left[0].dir(), edited.dir());
        assert!(!half_made.dir().exists());
    }

    #[test]
    fn the_sweep_will_not_touch_a_live_session() {
        // The reason this could not be written before Phase 2. A session that
        // is open and not yet edited reads as unchanged, and deleting it would
        // take the directory out from under a running editor.
        let tmp = tempfile::tempdir().unwrap();
        let root = tmp.path().join("sessions");
        let c = container(tmp.path(), "report.pdf", b"first");
        let w = World::new();
        let mut r = Resident::new(&root);
        ok(r.handle(opening(c), &w.outside()));

        let live: Vec<_> = session::scan(&root)
            .unwrap()
            .iter()
            .map(|s| s.dir().to_path_buf())
            .collect();
        assert!(matches!(
            recover::state(&session::scan(&root).unwrap()[0]),
            recover::State::Unchanged
        ));

        assert_eq!(sweep(&root, &live).unwrap(), 0);
        assert_eq!(session::scan(&root).unwrap().len(), 1);
        // And it would have gone, had the sweep not been told.
        assert_eq!(sweep(&root, &[]).unwrap(), 1);
    }

    #[test]
    fn standing_down_takes_back_every_question_it_was_holding() {
        let tmp = tempfile::tempdir().unwrap();
        let root = tmp.path().join("sessions");
        let c = container(tmp.path(), "report.pdf", b"first");
        let left = a_diverged_session(&root, &c, "report.pdf", b"edited");

        let w = World::new();
        let mut r = Resident::new(&root);
        err(r.handle(opening(c), &w.outside()));
        let about = w.channel.questions()[0].about.clone();

        r.stand_down(&w.outside());

        assert_eq!(w.channel.withdrawn(), vec![about.clone()]);
        assert!(w
            .channel
            .said()
            .contains(&format!("recover {about} --write-back")));
        assert!(
            left.dir().exists(),
            "the session carries the question to the next launch"
        );
        assert!(r.is_idle());
    }

    #[test]
    fn a_ping_is_answered_and_changes_nothing() {
        let tmp = tempfile::tempdir().unwrap();
        let w = World::new();
        let mut r = Resident::new(tmp.path().join("sessions"));
        assert_eq!(
            r.handle(Request::Ping, &w.outside()),
            Response::Ok(Vec::new())
        );
        assert!(r.is_idle());
    }

    #[test]
    fn nothing_wrong_is_the_ordinary_colour() {
        let tmp = tempfile::tempdir().unwrap();
        let r = Resident::new(tmp.path().join("sessions"));
        assert_eq!(r.mood(), crate::present::Mood::Settled);
        assert!(r.troubles().is_empty());
    }

    #[test]
    fn the_icon_wears_the_worst_thing_currently_true() {
        use crate::present::Mood;
        let tmp = tempfile::tempdir().unwrap();
        let mut r = Resident::new(tmp.path().join("sessions"));

        r.note(Mood::Look, "a", "one did not open".into());
        assert_eq!(r.mood(), Mood::Look);
        // Worse arrives and wins, whatever order they were taken on in.
        r.note(Mood::Danger, "b", "one is a program".into());
        assert_eq!(r.mood(), Mood::Danger);
        r.note(Mood::AtRisk, "c", "one did not save".into());
        assert_eq!(
            r.mood(),
            Mood::Danger,
            "a lesser trouble does not talk the icon down"
        );

        // And it comes back down as they are put down, rather than sticking at
        // the worst thing that ever happened.
        r.dismiss("b");
        assert_eq!(r.mood(), Mood::AtRisk);
        r.dismiss("c");
        assert_eq!(r.mood(), Mood::Look);
        r.dismiss("a");
        assert_eq!(r.mood(), Mood::Settled);
    }

    #[test]
    fn the_same_trouble_twice_is_one_trouble() {
        use crate::present::Mood;
        let tmp = tempfile::tempdir().unwrap();
        let mut r = Resident::new(tmp.path().join("sessions"));
        r.note(
            Mood::Look,
            "open:report",
            "report.slpc - did not open".into(),
        );
        r.note(
            Mood::Look,
            "open:report",
            "report.slpc - did not open".into(),
        );
        r.note(Mood::Look, "open:report", "report.slpc - still not".into());
        assert_eq!(r.troubles().len(), 1, "{:?}", r.troubles());
        assert_eq!(r.troubles()[0].summary, "report.slpc - still not");
    }

    #[test]
    fn a_container_that_will_not_open_leaves_something_behind() {
        // The case the standing list exists for: a double-click that produces
        // no document and no window. The notification saying so is a moment,
        // and a moment that was missed is indistinguishable from the tool being
        // broken.
        let tmp = tempfile::tempdir().unwrap();
        let w = World::new();
        let mut r = Resident::new(tmp.path().join("sessions"));
        let nowhere = tmp.path().join("not-a-container.slpc");
        fs::write(&nowhere, b"this is not a container").unwrap();

        let _ = err(r.handle(opening(nowhere), &w.outside()));
        assert_eq!(r.mood(), crate::present::Mood::Look);
        let said = &r.troubles()[0].summary;
        assert!(
            said.starts_with("not-a-container.slpc"),
            "it names the file the person clicked, not the session: {said}"
        );
    }

    #[test]
    fn a_payload_that_is_a_program_is_the_one_thing_red_is_for() {
        // Concept 5.1's check, which fires close to never and means one thing
        // when it does. Nothing else in this file may reach `Danger`.
        let tmp = tempfile::tempdir().unwrap();
        let w = World::new();
        let mut r = Resident::new(tmp.path().join("sessions"));
        let c = container(tmp.path(), "invoice.txt", b"MZ\x90\x00 not a document");

        let why = err(r.handle(opening(c), &w.outside()));
        assert!(why.contains("was not opened"), "{why}");
        assert!(r.is_idle(), "nothing opened, so nothing is being held");
        assert!(
            w.launcher.launched().is_empty(),
            "nothing was handed to the desktop"
        );
        assert!(
            session::scan(&tmp.path().join("sessions"))
                .unwrap_or_default()
                .is_empty(),
            "the refusal is before the session, so nothing reached the disk"
        );

        // Insisted on rather than reported. A notification can be missed, and
        // this is the one refusal where being missed matters: the person is
        // holding a file somebody sent them believing it is a document.
        let insisted = w.channel.insisted();
        assert_eq!(insisted.len(), 1, "{insisted:?}");
        assert!(insisted[0].summary.contains("invoice.txt"), "{insisted:?}");

        assert_eq!(r.mood(), crate::present::Mood::Danger);
        let said = &r.troubles()[0].summary;
        assert!(
            said.contains("invoice.txt") && said.contains("Windows executable"),
            "{said}"
        );
    }

    #[test]
    fn a_trouble_stays_until_it_is_put_down() {
        // The property that makes the colour worth reading: it is not a banner
        // that expires while somebody is looking the other way. Turning the
        // loop over changes nothing about it.
        let tmp = tempfile::tempdir().unwrap();
        let w = World::new();
        let mut r = Resident::new(tmp.path().join("sessions"));
        let c = container(tmp.path(), "invoice.txt", b"MZ\x90\x00 not a document");
        let _ = err(r.handle(opening(c), &w.outside()));

        for _ in 0..5 {
            r.turn(&w.outside());
        }
        assert_eq!(r.mood(), crate::present::Mood::Danger);

        let id = r.troubles()[0].id.clone();
        r.dismiss(&id);
        assert!(r.troubles().is_empty());
        assert_eq!(r.mood(), crate::present::Mood::Settled);
    }

    #[test]
    fn a_question_waiting_colours_the_icon_and_answering_clears_it() {
        // Read off the sessions rather than remembered, so there is nothing to
        // dismiss and nothing that can disagree with what is on disk.
        let tmp = tempfile::tempdir().unwrap();
        let root = tmp.path().join("sessions");
        let w = World::new();
        let c = container(tmp.path(), "report.txt", b"a report\n");

        a_diverged_session(&root, &c, "report.txt", b"an edit nobody wrote back\n");

        let mut r = Resident::new(&root);
        assert_eq!(r.mood(), crate::present::Mood::Settled);
        // Opening the same container is what raises concept 6.3's question.
        let _ = r.handle(announcing(c), &w.loud());
        assert_eq!(
            r.mood(),
            crate::present::Mood::Look,
            "a decision waiting is worth a look and nothing more"
        );
        assert!(
            r.troubles().is_empty(),
            "and it is not a trouble: it is on the sessions, so answering ends it"
        );

        let about = w.channel.questions()[0].about.clone();
        w.channel.answer(&about, Choice::Discard);
        r.turn(&w.outside());
        assert_eq!(r.mood(), crate::present::Mood::Settled);
    }
}