ikigai-cli 0.1.66

ikigai — a REPL client for resource-resolution kernels (native + WebAssembly).
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
//! `ikigai layers post|save`: post a brief, an issue or a file to a layer, and save a posted
//! file back to disk with its size and digest checked (ledger #1629).
//!
//! **Nothing here is a new way to reach a door.** A post is the `sink urn:layer:{layer}:say`
//! a `-c` batch sends, with the entry composed here instead of by hand, and a save is a
//! `source` of the entry. Both run on the engine the REPL and `-c` build, so the door is
//! reached however this process routes `urn:layer:` (an `--override`, a `--connect`, a config
//! home `mount` line):
//!
//! ```text
//! ikigai --override urn:layer:,urn:layers:,urn:collab:=quic://door:4434 --cert-dir D \
//!        layers post file talk ./trace.log
//! ```
//!
//! What it saves the caller is the composition: a Turtle literal escaped for every character
//! it may not hold raw, a file's SHA-256 and size, text or base64, and on the way back the
//! check the door cannot make (a door validates an entry's SHAPE, never that its digest is
//! true of its content).
//!
//! The behavior and the names follow the Python participant client (`ikigai-layers post
//! brief|issue|file`, `ikigai-layers save`, in ikigai-rs/ikigai-layers-client), so a
//! collaborator moving between the two finds the same commands, flags and refusals. Reading
//! needs nothing new: `source urn:layer:{layer}:entries type=<class IRI>` already reads one
//! kind at the door.
//!
//! The entry kinds and their properties are the layer's closed shapes (the edge's
//! `talk.shapes.ttl`): `ik:Brief` (`dcterms:title`, `ik:body`, `ik:repository`), `ik:Issue`
//! (the same and `ik:severity`), and `ik:File` (`ik:fileName`, `ik:mediaType`,
//! `ik:contentHash`, `ik:byteSize`, `ik:encoding`, `ik:content`), each optionally
//! `ik:inReplyTo` or `ik:supersedes` an entry. The host stamps who, when and the sequence.

use std::io::{IsTerminal, Read, Write};
use std::path::{Path, PathBuf};

use icu_properties::props::GeneralCategory;
use ikigai_engine::{Action, Engine};
use sha2::{Digest, Sha256};

const IK: &str = "https://ikigai-rs.dev/ns#";
const DCTERMS: &str = "http://purl.org/dc/terms/";
const RDF_TYPE: &str = "http://www.w3.org/1999/02/22-rdf-syntax-ns#type";
const SH: &str = "http://www.w3.org/ns/shacl#";

/// The severities an issue may name, as `urn:iki:severity:{name}`: ikigai's review triage
/// list less `praise`, which is not an issue. Triage for a reader, never a gate.
const SEVERITIES: [&str; 4] = ["critical", "major", "minor", "info"];

/// Exit status: the door answered with an error, or a file entry failed its own check.
pub(crate) const EXIT_DOOR: i32 = 1;
/// Exit status: a usage problem, or a problem on this side (a file that cannot be read).
pub(crate) const EXIT_USAGE: i32 = 2;

pub(crate) const USAGE: &str = "\
usage (connection flags such as --override, --connect and --cert-dir go BEFORE `layers`):
  ikigai layers post brief LAYER [FILE|-] [--title T] [--repo owner/name|https://…]
  ikigai layers post issue LAYER [FILE|-] [--title T] [--repo …] [--severity critical|major|minor|info]
  ikigai layers post file LAYER PATH [--file-name N] [--media-type type/subtype]
      every post also takes --in-reply-to ENTRY and --supersedes ENTRY, and prints the entry's IRI;
      a brief or issue body is FILE, or stdin with - or no FILE; its title defaults to the
      body's first `# ` heading
  ikigai layers save ENTRY [PATH] [--as NAME] [--force]
      write a file entry's bytes to PATH (a directory: its own name in it, or NAME with --as;
      none: your downloads directory, ~/Downloads) after checking its size and SHA-256; nothing
      is written when either does not match. A name an agent or a shell loads as configuration
      (CLAUDE.md, AGENTS.md, .mcp.json, .envrc, any .git name), any name beginning with a dot,
      any ':', and a Windows device name (CON, NUL, COM1) are refused: save it under another
      name with --as NAME. --force replaces an existing file at the target; its old content is
      lost
exit: 0 done, 1 the door answered an error (or a file failed its check), 2 usage or local";

/// What `ikigai layers …` was asked to do, as parsed (nothing read or dialed yet).
#[derive(Clone, Debug, PartialEq)]
pub(crate) enum Command {
    /// `layers -h|--help`: the usage, exit 0.
    Help,
    Post {
        layer: String,
        kind: PostKind,
        in_reply_to: Option<String>,
        supersedes: Option<String>,
    },
    Save {
        entry: String,
        path: Option<PathBuf>,
        /// `--as NAME`: save under NAME instead of the entry's own file name.
        as_name: Option<String>,
        force: bool,
    },
}

/// The kind of entry a post writes, with its own options.
#[derive(Clone, Debug, PartialEq)]
pub(crate) enum PostKind {
    Brief {
        source: String,
        title: Option<String>,
        repo: Option<String>,
    },
    Issue {
        source: String,
        title: Option<String>,
        repo: Option<String>,
        severity: Option<String>,
    },
    File {
        path: PathBuf,
        file_name: Option<String>,
        media_type: Option<String>,
    },
}

/// Parse what follows `layers` on the command line. Every argument after it belongs to it.
pub(crate) fn parse(argv: impl Iterator<Item = String>) -> Result<Command, String> {
    let mut argv = argv.peekable();
    let value = |argv: &mut std::iter::Peekable<_>, flag: &str| -> Result<String, String> {
        Iterator::next(argv).ok_or_else(|| format!("{flag} needs a value\n{USAGE}"))
    };
    match argv.next().as_deref() {
        Some("-h" | "--help") => Ok(Command::Help),
        Some("post") => {
            let kind = argv.next().ok_or_else(|| {
                format!("`layers post` needs a kind: brief, issue or file\n{USAGE}")
            })?;
            if !matches!(kind.as_str(), "brief" | "issue" | "file") {
                return Err(format!(
                    "`layers post {kind}`: the kinds are brief, issue and file\n{USAGE}"
                ));
            }
            let mut positional = Vec::new();
            let (mut title, mut repo, mut severity) = (None, None, None);
            let (mut file_name, mut media_type) = (None, None);
            let (mut in_reply_to, mut supersedes) = (None, None);
            while let Some(arg) = argv.next() {
                let text = kind.as_str() != "file";
                match arg.as_str() {
                    "--in-reply-to" => in_reply_to = Some(value(&mut argv, &arg)?),
                    "--supersedes" => supersedes = Some(value(&mut argv, &arg)?),
                    "--title" if text => title = Some(value(&mut argv, &arg)?),
                    "--repo" if text => repo = Some(value(&mut argv, &arg)?),
                    "--severity" if kind == "issue" => severity = Some(value(&mut argv, &arg)?),
                    "--file-name" if !text => file_name = Some(value(&mut argv, &arg)?),
                    "--media-type" if !text => media_type = Some(value(&mut argv, &arg)?),
                    "-" => positional.push(arg),
                    other if other.starts_with('-') => {
                        return Err(format!(
                            "unknown argument after `layers post {kind}`: {other} (connection \
                             flags go before `layers`)\n{USAGE}"
                        ))
                    }
                    _ => positional.push(arg),
                }
            }
            let mut positional = positional.into_iter();
            let layer = positional
                .next()
                .ok_or_else(|| format!("`layers post {kind}` needs a LAYER\n{USAGE}"))?;
            let second = positional.next();
            if let Some(extra) = positional.next() {
                return Err(format!(
                    "unexpected argument after `layers post {kind}`: {extra}\n{USAGE}"
                ));
            }
            let kind = match kind.as_str() {
                "brief" => PostKind::Brief {
                    source: second.unwrap_or_else(|| "-".to_string()),
                    title,
                    repo,
                },
                "issue" => PostKind::Issue {
                    source: second.unwrap_or_else(|| "-".to_string()),
                    title,
                    repo,
                    severity,
                },
                _ => PostKind::File {
                    path: PathBuf::from(second.ok_or_else(|| {
                        format!("`layers post file` needs the PATH of the file to attach\n{USAGE}")
                    })?),
                    file_name,
                    media_type,
                },
            };
            Ok(Command::Post {
                layer,
                kind,
                in_reply_to,
                supersedes,
            })
        }
        Some("save") => {
            let mut positional = Vec::new();
            let mut force = false;
            let mut as_name = None;
            while let Some(arg) = argv.next() {
                match arg.as_str() {
                    "--force" => force = true,
                    "--as" => as_name = Some(value(&mut argv, &arg)?),
                    other if other.starts_with('-') => {
                        return Err(format!(
                            "unknown argument after `layers save`: {other} (connection flags \
                             go before `layers`)\n{USAGE}"
                        ))
                    }
                    _ => positional.push(arg),
                }
            }
            let mut positional = positional.into_iter();
            let entry = positional
                .next()
                .ok_or_else(|| format!("`layers save` needs the file entry's IRI\n{USAGE}"))?;
            let path = positional.next().map(PathBuf::from);
            if let Some(extra) = positional.next() {
                return Err(format!(
                    "unexpected argument after `layers save`: {extra}\n{USAGE}"
                ));
            }
            Ok(Command::Save {
                entry,
                path,
                as_name,
                force,
            })
        }
        Some(other) => Err(format!("unknown `layers` command: {other}\n{USAGE}")),
        None => Err(USAGE.to_string()),
    }
}

/// A command with everything local done: a post's entry composed (its files and stdin read),
/// a save's arguments checked. What [`run`] does next is only the door's part.
pub(crate) enum Prepared {
    Help,
    Post {
        layer: String,
        turtle: String,
        /// For a file: what the size bound is checked against before sending.
        file: Option<FileCheck>,
    },
    Save {
        entry: String,
        target: SaveTarget,
        force: bool,
    },
}

/// Where `save` writes, settled before anything is dialed.
#[derive(Clone, Debug, PartialEq)]
pub(crate) enum SaveTarget {
    /// Into this directory (PATH named one, or the downloads directory when there was no
    /// PATH), under `as_name` or else the entry's own file name, which is checked when it
    /// arrives.
    Dir {
        dir: PathBuf,
        as_name: Option<String>,
    },
    /// To this file: PATH named it, and its name has been checked already, so the entry's
    /// own name does not matter.
    File(PathBuf),
}

/// What a file post checks against the layer's shapes before it is sent.
pub(crate) struct FileCheck {
    name: String,
    size: usize,
    encoding: &'static str,
    content: String,
}

/// Do everything that needs no door: refuse a bad name, entry, repository or severity, read
/// the body or file, and compose the entry. An error here is a usage or local problem.
pub(crate) fn prepare(command: Command) -> Result<Prepared, String> {
    match command {
        Command::Help => Ok(Prepared::Help),
        Command::Post {
            layer,
            kind,
            in_reply_to,
            supersedes,
        } => {
            layer_name(&layer)?;
            let thread = Thread {
                in_reply_to: in_reply_to.as_deref(),
                supersedes: supersedes.as_deref(),
            };
            let (turtle, file) = match kind {
                PostKind::Brief {
                    source,
                    title,
                    repo,
                } => {
                    let repo = repo.as_deref().map(repository).transpose()?;
                    let (title, body) = titled_body(&source, title, "brief")?;
                    (brief(&title, &body, repo.as_deref(), thread)?, None)
                }
                PostKind::Issue {
                    source,
                    title,
                    repo,
                    severity,
                } => {
                    let repo = repo.as_deref().map(repository).transpose()?;
                    let severity = severity.as_deref().map(severity_iri).transpose()?;
                    let (title, body) = titled_body(&source, title, "issue")?;
                    (
                        issue(&title, &body, repo.as_deref(), severity.as_deref(), thread)?,
                        None,
                    )
                }
                PostKind::File {
                    path,
                    file_name,
                    media_type,
                } => {
                    let data = std::fs::read(&path)
                        .map_err(|e| format!("cannot read {}: {e}", path.display()))?;
                    let name = match file_name {
                        Some(name) => name,
                        None => path
                            .file_name()
                            .and_then(|n| n.to_str())
                            .ok_or_else(|| {
                                format!(
                                    "{} has no file name to post it under: give --file-name",
                                    path.display()
                                )
                            })?
                            .to_string(),
                    };
                    safe_name(&name).map_err(|e| {
                        format!(
                            "{e}, and a reader cannot save it: post it under another name with \
                             --file-name NAME"
                        )
                    })?;
                    let entry = file_entry(&name, &data, media_type.as_deref(), thread)?;
                    (
                        entry.turtle,
                        Some(FileCheck {
                            name,
                            size: data.len(),
                            encoding: entry.encoding,
                            content: entry.content,
                        }),
                    )
                }
            };
            Ok(Prepared::Post {
                layer,
                turtle,
                file,
            })
        }
        Command::Save {
            entry,
            path,
            as_name,
            force,
        } => {
            entry_layer(&entry)?;
            if let Some(name) = &as_name {
                safe_name(name).map_err(|e| format!("not saved: {e}; choose another --as NAME"))?;
            }
            let target = save_target(path, as_name, downloads_dir)?;
            Ok(Prepared::Save {
                entry,
                target,
                force,
            })
        }
    }
}

/// Run a prepared command against `engine`; the process exit status.
pub(crate) fn run(engine: &Engine, prepared: Prepared) -> i32 {
    let outcome = match prepared {
        Prepared::Help => Ok(USAGE.to_string()),
        Prepared::Post {
            layer,
            turtle,
            file,
        } => post(engine, &layer, turtle, file),
        Prepared::Save {
            entry,
            target,
            force,
        } => save(engine, &entry, &target, force),
    };
    match outcome {
        Ok(line) => {
            println!("{line}");
            0
        }
        Err((code, message)) => {
            eprintln!("ikigai: {message}");
            code
        }
    }
}

type Outcome = Result<String, (i32, String)>;

/// One engine line's answer, or the door's error as exit 1.
fn ask(engine: &Engine, line: &str) -> Result<String, (i32, String)> {
    match engine.eval(line) {
        Action::Output(entry) => entry.result.map_err(|e| (EXIT_DOOR, e)),
        _ => Err((EXIT_DOOR, format!("`{line}` produced no answer"))),
    }
}

fn post(engine: &Engine, layer: &str, turtle: String, file: Option<FileCheck>) -> Outcome {
    if let Some(file) = file {
        let shapes = ask(engine, &format!("source urn:layer:{layer}:shapes"))?;
        let shapes = Shapes::parse(&shapes).map_err(|e| {
            (
                EXIT_DOOR,
                format!("the shapes of layer `{layer}` do not parse: {e}"),
            )
        })?;
        if !shapes.takes_files {
            return Err((
                EXIT_USAGE,
                format!("layer `{layer}` does not take files: its shapes admit no ik:File"),
            ));
        }
        if let Some(bound) = shapes.content_bound {
            if let Some(why) = too_large(&file, bound) {
                return Err((EXIT_USAGE, why));
            }
        }
    }
    engine.set_piped_input(turtle.into_bytes());
    let answer = ask(engine, &format!("sink urn:layer:{layer}:say"))?;
    Ok(answer.trim().to_string())
}

fn save(engine: &Engine, entry: &str, target: &SaveTarget, force: bool) -> Outcome {
    let turtle = ask(engine, &format!("source {entry} as=text/turtle"))?;
    let node = Node::of(&turtle, entry).map_err(|e| (EXIT_DOOR, format!("not saved: {e}")))?;
    if !node.types.iter().any(|t| t == &format!("{IK}File")) {
        let kind = node
            .types
            .iter()
            .map(|t| t.strip_prefix(IK).unwrap_or(t))
            .collect::<Vec<_>>()
            .join(",");
        return Err((
            EXIT_USAGE,
            format!(
                "{entry} is {}, not a file",
                if kind.is_empty() {
                    "an entry".to_string()
                } else {
                    format!("a {kind}")
                }
            ),
        ));
    }
    // The entry's own name matters only when it picks the file: not with --as, and not when
    // PATH names the file (both were checked before dialing).
    let target = match target {
        SaveTarget::Dir {
            dir,
            as_name: Some(name),
        } => dir.join(name),
        SaveTarget::Dir { dir, as_name: None } => {
            let name = node
                .one("fileName")
                .map_err(|e| (EXIT_DOOR, format!("not saved: {e}")))?;
            let name = safe_name(name).map_err(|e| {
                (
                    EXIT_DOOR,
                    format!("not saved: {e}; save it under another name with --as NAME"),
                )
            })?;
            dir.join(name)
        }
        SaveTarget::File(path) => path.clone(),
    };
    let data = node
        .bytes()
        .map_err(|e| (EXIT_DOOR, format!("not saved: {e}")))?;
    if target.exists() && !force {
        return Err((
            EXIT_USAGE,
            format!(
                "{} exists: give another PATH, --as NAME, or --force to replace it (its old \
                 content is lost)",
                target.display()
            ),
        ));
    }
    write_whole(&target, &data).map_err(|e| {
        (
            EXIT_USAGE,
            format!("cannot write {}: {e}", target.display()),
        )
    })?;
    Ok(format!(
        "saved {} ({} bytes, {})",
        target.display(),
        data.len(),
        digest(&data)
    ))
}

/// Where `save` writes, decided before dialing (the Python client's `_save_dir`): no PATH is
/// the downloads directory (`downloads`, refused when there is none, never the working
/// directory, which is often a repository an agent works in); a directory is that directory;
/// and a PATH naming the file has its own name held to [`safe_name`]. `--as` needs a directory.
fn save_target(
    path: Option<PathBuf>,
    as_name: Option<String>,
    downloads: impl FnOnce() -> Result<PathBuf, String>,
) -> Result<SaveTarget, String> {
    let Some(path) = path else {
        return Ok(SaveTarget::Dir {
            dir: downloads()?,
            as_name,
        });
    };
    if path.is_dir() {
        return Ok(SaveTarget::Dir { dir: path, as_name });
    }
    if as_name.is_some() {
        return Err(format!(
            "{} is not a directory: give --as NAME with a directory, or a PATH that names the file",
            path.display()
        ));
    }
    let name = match path.file_name() {
        None => String::new(),
        Some(name) => name.to_str().map(str::to_string).ok_or_else(|| {
            format!(
                "not saved: {} has a name that is not UTF-8; give PATH another name",
                path.display()
            )
        })?,
    };
    safe_name(&name).map_err(|e| format!("not saved: {e}; give PATH another name"))?;
    Ok(SaveTarget::File(path))
}

/// Write `data` to `target` whole or not at all: into a new file beside it, then renamed over
/// it, so a reader never sees half a file and a failed write leaves nothing behind.
fn write_whole(target: &Path, data: &[u8]) -> std::io::Result<()> {
    let dir = match target.parent() {
        Some(d) if !d.as_os_str().is_empty() => d.to_path_buf(),
        _ => PathBuf::from("."),
    };
    std::fs::create_dir_all(&dir)?;
    // A name no other file has: this process's id, then the first free number. `create_new`
    // refuses an existing file, so a leftover from a crashed save is stepped over, never reused.
    let mut n = 0u32;
    let (temp, mut file) = loop {
        let temp = dir.join(format!(".ikigai-layers-{}-{n}", std::process::id()));
        match std::fs::OpenOptions::new()
            .write(true)
            .create_new(true)
            .open(&temp)
        {
            Ok(file) => break (temp, file),
            Err(e) if e.kind() == std::io::ErrorKind::AlreadyExists && n < 1000 => n += 1,
            Err(e) => return Err(e),
        }
    };
    let written = (|| {
        file.write_all(data)?;
        file.sync_all()?;
        std::fs::rename(&temp, target)
    })();
    if written.is_err() {
        let _ = std::fs::remove_file(&temp);
    }
    written
}

// ---- composing an entry --------------------------------------------------------------------

/// The entry an entry answers or replaces, both optional.
#[derive(Clone, Copy, Default)]
struct Thread<'a> {
    in_reply_to: Option<&'a str>,
    supersedes: Option<&'a str>,
}

/// `name` if it is a layer name (lowercase `[a-z0-9-]`, 1 to 24, no leading or trailing `-`).
fn layer_name(name: &str) -> Result<&str, String> {
    let ok = (1..=24).contains(&name.len())
        && name
            .bytes()
            .all(|b| b.is_ascii_lowercase() || b.is_ascii_digit() || b == b'-')
        && !name.starts_with('-')
        && !name.ends_with('-');
    if ok {
        Ok(name)
    } else {
        Err(format!(
            "`{name}` is not a layer name (lowercase a-z, 0-9 and -, 1 to 24)"
        ))
    }
}

/// The layer an entry IRI (`urn:layer:{layer}:entry:{id}`) belongs to.
fn entry_layer(entry: &str) -> Result<&str, String> {
    let refuse = || format!("`{entry}` is not an entry IRI (urn:layer:LAYER:entry:ID)");
    let rest = entry.strip_prefix("urn:layer:").ok_or_else(refuse)?;
    let (layer, id) = rest.split_once(":entry:").ok_or_else(refuse)?;
    layer_name(layer).map_err(|_| refuse())?;
    if id.is_empty()
        || id
            .chars()
            .any(|c| c.is_whitespace() || matches!(c, '<' | '>' | '"'))
    {
        return Err(refuse());
    }
    Ok(layer)
}

/// `text` as a Turtle double-quoted string literal, every character it may not hold raw
/// escaped (Turtle 1.1 `STRING_LITERAL_QUOTE`).
fn literal(text: &str) -> String {
    let mut out = String::with_capacity(text.len() + 2);
    out.push('"');
    for c in text.chars() {
        match c {
            '\\' => out.push_str("\\\\"),
            '"' => out.push_str("\\\""),
            '\n' => out.push_str("\\n"),
            '\r' => out.push_str("\\r"),
            '\t' => out.push_str("\\t"),
            c if (c as u32) < 0x20 || c as u32 == 0x7F => {
                out.push_str(&format!("\\u{:04X}", c as u32))
            }
            c => out.push(c),
        }
    }
    out.push('"');
    out
}

/// `value` as a Turtle IRI reference `<…>`, refused when it cannot be one (Turtle 1.1 `IRIREF`
/// excludes these characters, and an IRI here is absolute).
fn iri(value: &str) -> Result<String, String> {
    let bad = value.is_empty()
        || !value.contains(':')
        || value
            .chars()
            .any(|c| (c as u32) <= 0x20 || "<>\"{}|^`\\".contains(c));
    if bad {
        Err(format!("{value:?} is not an absolute IRI"))
    } else {
        Ok(format!("<{value}>"))
    }
}

/// A repository's web IRI: `owner/name` is a GitHub repository, an `https://` IRI is taken as
/// given, and a bare name is refused (`ikigai-core` names a directory on one machine, and
/// means nothing to a reader on another).
fn repository(value: &str) -> Result<String, String> {
    if value.starts_with("https://") {
        iri(value)?;
        return Ok(value.to_string());
    }
    let part = |p: &str| {
        !p.is_empty()
            && p.bytes()
                .all(|b| b.is_ascii_alphanumeric() || matches!(b, b'_' | b'.' | b'-'))
    };
    match value.split_once('/') {
        Some((owner, name)) if part(owner) && part(name) && !value.contains("..") => {
            Ok(format!("https://github.com/{value}"))
        }
        _ => Err(format!(
            "`{value}` is not a repository: give owner/name (on GitHub) or its https:// IRI"
        )),
    }
}

/// The IRI of one of [`SEVERITIES`].
fn severity_iri(name: &str) -> Result<String, String> {
    if SEVERITIES.contains(&name) {
        Ok(format!("urn:iki:severity:{name}"))
    } else {
        Err(format!(
            "`{name}` is not a severity ({})",
            SEVERITIES.join(", ")
        ))
    }
}

/// The Turtle for one entry of class `ik:{kind}` making `statements` (each `predicate
/// object`, already serialized), about `<urn:layer:new>`, which the host renames.
fn entry(kind: &str, statements: Vec<String>, thread: Thread<'_>) -> Result<String, String> {
    let mut said = vec![format!("a ik:{kind}")];
    said.extend(statements);
    if let Some(target) = thread.in_reply_to {
        said.push(format!("ik:inReplyTo {}", iri(target)?));
    }
    if let Some(target) = thread.supersedes {
        said.push(format!("ik:supersedes {}", iri(target)?));
    }
    let mut prefixes = format!("@prefix ik: <{IK}> .\n");
    if said.iter().any(|s| s.starts_with("dcterms:")) {
        prefixes.push_str(&format!("@prefix dcterms: <{DCTERMS}> .\n"));
    }
    Ok(format!(
        "{prefixes}<urn:layer:new> {} .\n",
        said.join(" ; ")
    ))
}

fn brief(
    title: &str,
    body: &str,
    repo: Option<&str>,
    thread: Thread<'_>,
) -> Result<String, String> {
    let mut said = vec![
        format!("dcterms:title {}", literal(title)),
        format!("ik:body {}", literal(body)),
    ];
    if let Some(repo) = repo {
        said.push(format!("ik:repository {}", iri(repo)?));
    }
    entry("Brief", said, thread)
}

fn issue(
    title: &str,
    body: &str,
    repo: Option<&str>,
    severity: Option<&str>,
    thread: Thread<'_>,
) -> Result<String, String> {
    let mut said = vec![
        format!("dcterms:title {}", literal(title)),
        format!("ik:body {}", literal(body)),
    ];
    if let Some(repo) = repo {
        said.push(format!("ik:repository {}", iri(repo)?));
    }
    if let Some(severity) = severity {
        said.push(format!("ik:severity {}", iri(severity)?));
    }
    entry("Issue", said, thread)
}

/// The first `# ` heading of a Markdown body (a level-one ATX heading), its closing `#`s
/// dropped.
fn first_heading(body: &str) -> Option<String> {
    body.lines().find_map(|line| {
        let rest = line.strip_prefix('#')?;
        if !rest.starts_with([' ', '\t']) {
            return None;
        }
        let title = rest.trim().trim_end_matches('#').trim_end();
        (!title.is_empty()).then(|| title.to_string())
    })
}

/// The body (`source`: a file, or stdin with `-`) and the title: `title`, else the body's
/// first `# ` heading.
fn titled_body(
    source: &str,
    title: Option<String>,
    what: &str,
) -> Result<(String, String), String> {
    let body = if source == "-" {
        let stdin = std::io::stdin();
        if stdin.is_terminal() {
            return Err(format!(
                "the {what}: give it as a FILE argument, or pipe it in"
            ));
        }
        let mut text = String::new();
        stdin
            .lock()
            .read_to_string(&mut text)
            .map_err(|e| format!("cannot read the {what} from stdin: {e}"))?;
        // A piped body keeps the newline its producer ended it with; it is not the body's.
        text.trim_end_matches('\n').to_string()
    } else {
        std::fs::read_to_string(source).map_err(|e| format!("cannot read {source}: {e}"))?
    };
    if body.trim().is_empty() {
        return Err(format!("the {what} is empty"));
    }
    let title = match title {
        Some(t) => t,
        None => first_heading(&body).ok_or_else(|| {
            format!("give the {what} a --title, or start it with a `# Title` line")
        })?,
    };
    let title = title.trim().to_string();
    if title.is_empty() {
        return Err("the title is empty".to_string());
    }
    Ok((title, body))
}

/// The digest an `ik:File` names: `sha256:` and the lowercase hex of the bytes' SHA-256.
fn digest(data: &[u8]) -> String {
    let hash = Sha256::digest(data);
    let mut out = String::with_capacity(7 + 64);
    out.push_str("sha256:");
    for byte in hash {
        out.push_str(&format!("{byte:02x}"));
    }
    out
}

/// `("text", the text)` when `data` is UTF-8 with no control character but tab, newline and
/// carriage return, else `("base64", its base64)`. Either way the digest and the size are of
/// the BYTES.
fn encode(data: &[u8]) -> (&'static str, String) {
    if let Ok(text) = std::str::from_utf8(data) {
        let plain = !text
            .chars()
            .any(|c| ((c as u32) < 0x20 && !matches!(c, '\t' | '\n' | '\r')) || c as u32 == 0x7F);
        if plain {
            return ("text", text.to_string());
        }
    }
    ("base64", ikigai_http::base64::encode(data))
}

/// Names an agent, a shell or a tool loads as configuration, or as credentials, from the
/// directory it runs in or from home, compared case-insensitively (each written here in its
/// folded form). Most begin with a dot, and the leading-dot rule refuses every one of those
/// anyway; they are listed so the reason a reader sees is the specific one. Every name
/// beginning `.git` is refused too (`.gitconfig`, `.gitattributes`, `.git`).
///
/// The Python participant client's `CONFIG_NAMES` (`files.py` in ikigai-layers-client), the
/// source of truth for this rule (ledger #1748): a name added there is added here.
const CONFIG_NAMES: [&str; 24] = [
    // agent instructions and tool servers
    "claude.md",
    "claude.local.md",
    "agents.md",
    "agent.md",
    "gemini.md",
    ".mcp.json",
    // shells and direnv
    ".envrc",
    ".env",
    ".bashrc",
    ".bash_profile",
    ".bash_login",
    ".profile",
    ".zshrc",
    ".zshenv",
    ".zprofile",
    ".zlogin",
    // credentials read by git, npm, pip and curl (`_netrc` is the Windows spelling)
    ".gitconfig",
    ".npmrc",
    ".pypirc",
    ".netrc",
    "_netrc",
    // Python code that pytest and the interpreter import on their own
    "conftest.py",
    "sitecustomize.py",
    "usercustomize.py",
];

/// Windows device names, reserved with or without an extension (`CON`, `con.txt`), as
/// Microsoft's naming rules list them, with the ports below. The Python client's
/// `WINDOWS_DEVICES`.
const WINDOWS_DEVICES: [&str; 6] = ["con", "prn", "aux", "nul", "conin$", "conout$"];
/// `COM0`-`COM9` and `LPT0`-`LPT9`, and the superscript-digit ports (`COM¹`, `LPT³`).
const WINDOWS_PORTS: [&str; 2] = ["com", "lpt"];
const WINDOWS_PORT_DIGITS: &str = "0123456789\u{b9}\u{b2}\u{b3}";
/// The characters Windows does not allow in a name (besides `:`, `/` and `\`, refused
/// before), in code point order so the one a refusal names is the Python client's.
const WINDOWS_FORBIDDEN: [char; 6] = ['"', '*', '<', '>', '?', '|'];

/// Full Unicode case folding, as Python's `str.casefold` does it.
fn fold(text: &str) -> String {
    icu_casemap::CaseMapper::new()
        .fold_string(text)
        .into_owned()
}

/// Whether `stem` (a name before its first dot, trailing spaces dropped) is a Windows device.
fn is_windows_device(stem: &str) -> bool {
    let stem = fold(stem);
    WINDOWS_DEVICES.contains(&stem.as_str()) || WINDOWS_PORTS.iter().any(|port| {
        let mut digit = stem.strip_prefix(port).unwrap_or("").chars();
        matches!((digit.next(), digit.next()), (Some(d), None) if WINDOWS_PORT_DIGITS.contains(d))
    })
}

/// `name` if it is a bare file name a reader can save without leaving the directory it chose
/// and without arming anything there, else why not.
///
/// Refused, in this order (the Python participant client's `safe_name`, ledger #1748): a name
/// with a directory in it, or a control character; a name over 255 characters; an invisible
/// character (general category Cf, Zl or Zp: HFS+ ignored some of these when comparing names,
/// git's CVE-2014-9390); a name an agent or a shell loads as configuration ([`CONFIG_NAMES`],
/// and every `.git` name), compared after NFKC, with trailing dots and spaces dropped, and
/// case folded, as a case-insensitive file system and Windows would compare it; any name
/// beginning with a dot; a Windows drive (`C:x`) and any other `:` (an alternate data stream,
/// `a.txt:x`); a character Windows does not allow; a trailing dot or space (Windows drops it, so
/// `CLAUDE.md.` would land as `CLAUDE.md`); and a Windows device name with or without an
/// extension (`CON`, `nul.txt`).
fn safe_name(name: &str) -> Result<&str, String> {
    let bad = name.is_empty()
        || name == "."
        || name == ".."
        || name.contains(['/', '\\'])
        || name.chars().any(|c| (c as u32) < 0x20 || c as u32 == 0x7F);
    if bad {
        return Err(format!("{name:?} is not a bare file name"));
    }
    if name.chars().count() > 255 {
        let head: String = name.chars().take(40).collect();
        return Err(format!("{head:?}… is over 255 characters"));
    }
    let categories = icu_properties::CodePointMapData::<GeneralCategory>::new();
    if let Some(hidden) = name.chars().find(|&c| {
        matches!(
            categories.get(c),
            GeneralCategory::Format
                | GeneralCategory::LineSeparator
                | GeneralCategory::ParagraphSeparator
        )
    }) {
        return Err(format!(
            "{name:?} holds an invisible character (U+{:04X})",
            hidden as u32
        ));
    }
    // As a case-insensitive file system, and Windows, would compare it.
    let normalized = icu_normalizer::ComposingNormalizer::new_nfkc().normalize(name);
    let key = fold(normalized.trim_end_matches(['.', ' ']));
    if CONFIG_NAMES.contains(&key.as_str()) || key.starts_with(".git") {
        return Err(format!(
            "{name:?} is a name an agent or a shell loads as configuration"
        ));
    }
    if name.starts_with('.') {
        return Err(format!(
            "{name:?} begins with a dot: a hidden file, the form configuration takes"
        ));
    }
    let mut chars = name.chars();
    if let (Some(drive), Some(':')) = (chars.next(), chars.next()) {
        if drive.is_ascii_alphabetic() {
            return Err(format!("{name:?} begins with a Windows drive ({drive}:)"));
        }
    }
    if name.contains(':') {
        return Err(format!(
            "{name:?} holds ':', which Windows reads as an alternate data stream"
        ));
    }
    if let Some(c) = WINDOWS_FORBIDDEN.iter().find(|&&c| name.contains(c)) {
        return Err(format!(
            "{name:?} holds {c:?}, which Windows does not allow in a name"
        ));
    }
    if name.ends_with(['.', ' ']) {
        return Err(format!(
            "{name:?} ends in a dot or a space, which Windows drops from a name"
        ));
    }
    let stem = name.split('.').next().unwrap_or(name);
    if is_windows_device(stem.trim_end_matches(' ')) {
        return Err(format!("{name:?} is a Windows device name ({stem})"));
    }
    Ok(name)
}

/// Where `save` writes when it is given no PATH: `~/Downloads` when it is a directory, else
/// the platform's own downloads directory (on Windows the Downloads known folder; elsewhere the
/// `XDG_DOWNLOAD_DIR` of `~/.config/user-dirs.dirs`), else a refusal asking for a PATH. Never
/// home itself: a downloads directory set to home is the XDG spelling of "none".
fn downloads_dir() -> Result<PathBuf, String> {
    // $HOME, read once here (the environment is process-global); on Windows a native shell
    // sets USERPROFILE instead.
    let mut home = std::env::var_os("HOME").unwrap_or_default();
    if cfg!(windows) && home.is_empty() {
        home = std::env::var_os("USERPROFILE").unwrap_or_default();
    }
    let home = PathBuf::from(home);
    #[cfg(windows)]
    let known_folder: Option<&dyn Fn() -> Option<PathBuf>> = Some(&dirs::download_dir);
    #[cfg(not(windows))]
    let known_folder: Option<&dyn Fn() -> Option<PathBuf>> = None;
    downloads_dir_in(&home, known_folder).ok_or_else(|| {
        if home.as_os_str().is_empty() {
            "no downloads directory found (HOME is not set): give a PATH to save into".to_string()
        } else {
            format!(
                "no downloads directory found (no {}): give a PATH to save into",
                home.join("Downloads").display()
            )
        }
    })
}

/// [`downloads_dir`] for a given `home` (an empty one is no home). `known_folder` is the
/// Windows lookup, and its presence is the Windows branch, so a test takes either branch on
/// any platform (the Python client's `registry` parameter).
fn downloads_dir_in(
    home: &Path,
    known_folder: Option<&dyn Fn() -> Option<PathBuf>>,
) -> Option<PathBuf> {
    let has_home = !home.as_os_str().is_empty();
    let home_resolved = std::fs::canonicalize(home).ok();
    let usable = |candidate: PathBuf| -> Option<PathBuf> {
        if !candidate.is_dir() {
            return None;
        }
        let resolved = std::fs::canonicalize(&candidate).ok()?;
        (home_resolved.as_ref() != Some(&resolved)).then_some(candidate)
    };
    if has_home {
        if let Some(found) = usable(home.join("Downloads")) {
            return Some(found);
        }
    }
    if let Some(lookup) = known_folder {
        return lookup().and_then(usable);
    }
    if !has_home {
        return None;
    }
    let dirs = std::fs::read_to_string(home.join(".config").join("user-dirs.dirs")).ok()?;
    for line in dirs.lines() {
        let Some(value) = xdg_download_dir(line) else {
            continue;
        };
        if let Some(rest) = value.strip_prefix("$HOME/") {
            return usable(home.join(rest));
        }
        if value.starts_with('/') {
            return usable(PathBuf::from(value));
        }
        return None;
    }
    None
}

/// The value of a `XDG_DOWNLOAD_DIR="…"` line of `user-dirs.dirs`, `None` for any other line.
fn xdg_download_dir(line: &str) -> Option<&str> {
    let rest = line.trim_start().strip_prefix("XDG_DOWNLOAD_DIR")?;
    let rest = rest.trim_start().strip_prefix('=')?;
    let rest = rest.trim_start().strip_prefix('"')?;
    let value = rest.trim_end().strip_suffix('"')?;
    (!value.contains('"')).then_some(value)
}

/// Whether a layer's shapes take `value` as a media type: lowercase `type/subtype`, no
/// parameters, at most 127 bytes (the edge's `talk.shapes.ttl` pattern).
fn is_media_type(value: &str) -> bool {
    let token = |t: &str| {
        let mut bytes = t.bytes();
        matches!(bytes.next(), Some(b) if b.is_ascii_lowercase() || b.is_ascii_digit())
            && bytes
                .all(|b| b.is_ascii_lowercase() || b.is_ascii_digit() || b"!$&^_.+-".contains(&b))
    };
    value.len() <= 127 && matches!(value.split_once('/'), Some((t, s)) if token(t) && token(s))
}

/// The media type of `name` by its extension; when that says nothing a layer takes, `text/plain`
/// for text and `application/octet-stream` for anything else.
fn guess_media_type(name: &str, encoding: &str) -> String {
    let lower = name.to_ascii_lowercase();
    let guessed = if lower.ends_with(".md") || lower.ends_with(".markdown") {
        // Not in every table.
        Some("text/markdown".to_string())
    } else {
        mime_guess::from_path(name)
            .first_raw()
            .map(str::to_ascii_lowercase)
    };
    match guessed {
        Some(m) if is_media_type(&m) => m,
        _ if encoding == "text" => "text/plain".to_string(),
        _ => "application/octet-stream".to_string(),
    }
}

/// One `ik:File` entry, and what its size bound is checked against.
struct FileEntry {
    turtle: String,
    encoding: &'static str,
    content: String,
}

fn file_entry(
    name: &str,
    data: &[u8],
    media: Option<&str>,
    thread: Thread<'_>,
) -> Result<FileEntry, String> {
    safe_name(name)?;
    let (encoding, content) = encode(data);
    let media = match media {
        Some(m) => {
            let m = m.to_ascii_lowercase();
            if !is_media_type(&m) {
                return Err(format!(
                    "`{m}` is not a media type a layer takes (lowercase type/subtype)"
                ));
            }
            m
        }
        None => guess_media_type(name, encoding),
    };
    let said = vec![
        format!("ik:fileName {}", literal(name)),
        format!("ik:mediaType {}", literal(&media)),
        format!("ik:contentHash {}", literal(&digest(data))),
        format!("ik:byteSize {}", data.len()),
        format!("ik:encoding {}", literal(encoding)),
        format!("ik:content {}", literal(&content)),
    ];
    Ok(FileEntry {
        turtle: entry("File", said, thread)?,
        encoding,
        content,
    })
}

/// Why the encoded content is over `bound` (the shapes' `sh:maxLength` on `ik:content`),
/// naming the largest file the layer takes; `None` when it fits. Measured in UTF-8 bytes of
/// the escaped literal, never fewer than the characters `sh:maxLength` counts, so a file that
/// fits here fits the door's entry bound too.
fn too_large(file: &FileCheck, bound: u64) -> Option<String> {
    let encoded = (literal(&file.content).len() - 2) as u64;
    if encoded <= bound {
        return None;
    }
    let binary = bound / 4 * 3;
    Some(format!(
        "{} is {} bytes, {encoded} bytes as {}; this layer takes at most {bound} bytes of \
         content, which is a file of about {binary} bytes of binary or {bound} bytes of plain \
         text. Share a link to it instead.",
        file.name, file.size, file.encoding
    ))
}

// ---- reading what the door answers ----------------------------------------------------------

/// What `post file` reads of a layer's shapes (`urn:layer:{layer}:shapes`, Turtle).
#[derive(Debug, PartialEq)]
struct Shapes {
    /// Some shape targets `ik:File`.
    takes_files: bool,
    /// The least `sh:maxLength` any property shape on `ik:content` sets.
    content_bound: Option<u64>,
}

impl Shapes {
    fn parse(turtle: &str) -> Result<Shapes, String> {
        let triples = parse_turtle(turtle)?;
        let file = format!("{IK}File");
        let content = format!("{IK}content");
        let (target, path, max) = (
            format!("{SH}targetClass"),
            format!("{SH}path"),
            format!("{SH}maxLength"),
        );
        let takes_files = triples
            .iter()
            .any(|t| t.predicate.as_str() == target && iri_of(&t.object) == Some(file.as_str()));
        let on_content: Vec<&oxrdf::NamedOrBlankNode> = triples
            .iter()
            .filter(|t| t.predicate.as_str() == path && iri_of(&t.object) == Some(content.as_str()))
            .map(|t| &t.subject)
            .collect();
        let content_bound = triples
            .iter()
            .filter(|t| t.predicate.as_str() == max && on_content.contains(&&t.subject))
            .filter_map(|t| literal_of(&t.object)?.parse::<u64>().ok())
            .min();
        Ok(Shapes {
            takes_files,
            content_bound,
        })
    }
}

fn parse_turtle(text: &str) -> Result<Vec<oxrdf::Triple>, String> {
    oxttl::TurtleParser::new()
        .for_slice(text.as_bytes())
        .collect::<Result<Vec<_>, _>>()
        .map_err(|e| e.to_string())
}

fn iri_of(term: &oxrdf::Term) -> Option<&str> {
    match term {
        oxrdf::Term::NamedNode(n) => Some(n.as_str()),
        _ => None,
    }
}

fn literal_of(term: &oxrdf::Term) -> Option<&str> {
    match term {
        oxrdf::Term::Literal(l) => Some(l.value()),
        _ => None,
    }
}

/// One entry, as the statements about it in the door's Turtle.
struct Node {
    iri: String,
    types: Vec<String>,
    values: Vec<(String, String)>,
}

impl Node {
    fn of(turtle: &str, entry: &str) -> Result<Node, String> {
        let triples = parse_turtle(turtle)
            .map_err(|e| format!("the door's answer for {entry} does not parse: {e}"))?;
        let mut node = Node {
            iri: entry.to_string(),
            types: Vec::new(),
            values: Vec::new(),
        };
        let mut found = false;
        for t in triples {
            let about =
                matches!(&t.subject, oxrdf::NamedOrBlankNode::NamedNode(n) if n.as_str() == entry);
            if !about {
                continue;
            }
            found = true;
            if t.predicate.as_str() == RDF_TYPE {
                if let Some(class) = iri_of(&t.object) {
                    node.types.push(class.to_string());
                }
            } else if let Some(value) = literal_of(&t.object) {
                node.values
                    .push((t.predicate.as_str().to_string(), value.to_string()));
            }
        }
        if found {
            Ok(node)
        } else {
            Err(format!("the door's answer for {entry} holds no such entry"))
        }
    }

    /// The one literal value of `ik:{local}`; refused when there is none or several.
    fn one(&self, local: &str) -> Result<&str, String> {
        let predicate = format!("{IK}{local}");
        let mut values = self.values.iter().filter(|(p, _)| *p == predicate);
        match (values.next(), values.next()) {
            (Some((_, v)), None) => Ok(v),
            (None, _) => Err(format!("{} carries no ik:{local}", self.iri)),
            _ => Err(format!("{} carries more than one ik:{local}", self.iri)),
        }
    }

    /// A file entry's bytes, after checking its size and its digest against them: the check
    /// the door cannot make, so the one that matters. Its NAME is not checked here: whether it
    /// matters depends on where it is saved ([`SaveTarget`]), and `save` holds it to
    /// [`safe_name`] when it does.
    fn bytes(&self) -> Result<Vec<u8>, String> {
        let content = self.one("content")?;
        let data = match self.one("encoding")? {
            "text" => content.as_bytes().to_vec(),
            "base64" => ikigai_http::base64::decode(content)
                .map_err(|e| format!("{}: its base64 does not decode: {e}", self.iri))?,
            other => {
                return Err(format!(
                    "{} has encoding {other:?}, which is neither text nor base64",
                    self.iri
                ))
            }
        };
        let size = self.one("byteSize")?;
        if size.parse::<usize>().ok() != Some(data.len()) {
            return Err(format!(
                "{}: its content is {} bytes, and it says {size}",
                self.iri,
                data.len()
            ));
        }
        let expected = self.one("contentHash")?;
        let actual = digest(&data);
        if actual != expected {
            return Err(format!(
                "{}: its content hashes to {actual}, and it says {expected}",
                self.iri
            ));
        }
        Ok(data)
    }
}

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

    fn args(line: &[&str]) -> Result<Command, String> {
        parse(line.iter().map(|s| s.to_string()))
    }

    #[test]
    fn a_literal_escapes_every_character_turtle_may_not_hold_raw() {
        assert_eq!(literal("say \"hi\"\n"), r#""say \"hi\"\n""#);
        assert_eq!(literal("a\\b\tc\rd"), r#""a\\b\tc\rd""#);
        assert_eq!(literal("bell\u{7}del\u{7f}"), r#""bell\u0007del\u007F""#);
        assert_eq!(literal("ümlaut ✓"), "\"ümlaut ✓\"");
        // And what it escapes parses back to the same text.
        let text = "line one\n\"quoted\" \\ back\u{1}slash";
        let turtle = format!("<urn:s> <urn:p> {} .", literal(text));
        let triples = parse_turtle(&turtle).unwrap();
        assert_eq!(literal_of(&triples[0].object), Some(text));
    }

    #[test]
    fn a_brief_is_the_turtle_the_shapes_name() {
        let turtle = brief(
            "Add a door check",
            "## Why",
            Some("https://github.com/ikigai-rs/ikigai-devtools"),
            Thread::default(),
        )
        .unwrap();
        assert_eq!(
            turtle,
            "@prefix ik: <https://ikigai-rs.dev/ns#> .\n\
             @prefix dcterms: <http://purl.org/dc/terms/> .\n\
             <urn:layer:new> a ik:Brief ; dcterms:title \"Add a door check\" ; ik:body \"## Why\" ; \
             ik:repository <https://github.com/ikigai-rs/ikigai-devtools> .\n"
        );
    }

    #[test]
    fn an_issue_names_its_severity_and_its_thread() {
        let turtle = issue(
            "Typo",
            "In the README.",
            None,
            Some("urn:iki:severity:minor"),
            Thread {
                in_reply_to: Some("urn:layer:talk:entry:01a"),
                supersedes: Some("urn:layer:talk:entry:01b"),
            },
        )
        .unwrap();
        assert!(
            turtle.contains("ik:severity <urn:iki:severity:minor>"),
            "{turtle}"
        );
        assert!(turtle.contains("ik:inReplyTo <urn:layer:talk:entry:01a>"));
        assert!(turtle.contains("ik:supersedes <urn:layer:talk:entry:01b>"));
        assert!(parse_turtle(&turtle).is_ok(), "it parses: {turtle}");
        // A thread target that cannot be an IRI is refused, never written into the Turtle.
        let bad = Thread {
            in_reply_to: Some("urn:x> . <urn:evil"),
            supersedes: None,
        };
        assert!(issue("t", "b", None, None, bad).is_err());
    }

    #[test]
    fn a_repository_is_owner_slash_name_or_https_and_never_a_bare_name() {
        assert_eq!(
            repository("ikigai-rs/ikigai-core").unwrap(),
            "https://github.com/ikigai-rs/ikigai-core"
        );
        assert_eq!(
            repository("https://example.org/r").unwrap(),
            "https://example.org/r"
        );
        for bad in [
            "ikigai-core",
            "a/b/c",
            "../x",
            "a/..",
            "http://x/y",
            "a/b c",
        ] {
            assert!(repository(bad).is_err(), "{bad}");
        }
    }

    #[test]
    fn a_severity_is_one_of_four() {
        assert_eq!(severity_iri("major").unwrap(), "urn:iki:severity:major");
        assert!(severity_iri("praise").is_err());
    }

    #[test]
    fn the_title_defaults_to_the_first_level_one_heading() {
        assert_eq!(
            first_heading("intro\n## Not this\n# Crash on start ##\nbody"),
            Some("Crash on start".to_string())
        );
        assert_eq!(first_heading("#hashtag\n## two"), None);
    }

    #[test]
    fn a_digest_is_sha256_hex() {
        assert_eq!(
            digest(b""),
            "sha256:e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855"
        );
    }

    #[test]
    fn text_travels_as_itself_and_anything_else_as_base64() {
        assert_eq!(
            encode(b"two\nlines\r\n\t"),
            ("text", "two\nlines\r\n\t".to_string())
        );
        assert_eq!(encode(b"\x89PNG\r\n"), ("base64", "iVBORw0K".to_string()));
        // UTF-8 with a control character is not text.
        assert_eq!(encode(b"esc\x1b[0m").0, "base64");
    }

    // ---- what `save` will write, and where (ledger #1748) ------------------------------------
    // Mirrors the Python participant client's tests/test_save_names.py, case for case.

    /// `name` is refused, and the reason says `because`.
    fn refused(name: &str, because: &str) {
        let err = safe_name(name).expect_err(name);
        assert!(err.contains(because), "{name:?}: {err}");
    }

    #[test]
    fn a_name_an_agent_or_shell_loads_as_configuration_is_refused() {
        for name in [
            "CLAUDE.md",
            "CLAUDE.local.md",
            "AGENTS.md",
            ".mcp.json",
            ".envrc",
            ".env",
            ".bashrc",
            ".bash_profile",
            ".profile",
            ".zshrc",
            ".zshenv",
            ".zprofile",
            ".gitconfig",
            ".npmrc",
            ".pypirc",
            ".netrc",
            "_netrc",
            "conftest.py",
        ] {
            assert!(CONFIG_NAMES.contains(&fold(name).as_str()), "{name}");
            refused(name, "loads as configuration");
        }
        // The list is written folded, so a lookup by a folded key can find every entry.
        for name in CONFIG_NAMES {
            assert_eq!(fold(name), name);
            refused(name, "loads as configuration");
        }
    }

    #[test]
    fn configuration_names_match_case_insensitively() {
        for name in [
            "claude.md",
            "Claude.MD",
            "agents.MD",
            ".MCP.JSON",
            ".EnvRC",
            "CONFTEST.PY",
        ] {
            refused(name, "loads as configuration");
        }
    }

    #[test]
    fn every_git_name_is_refused() {
        for name in [
            ".git",
            ".gitattributes",
            ".gitignore",
            ".git-credentials",
            ".GITMODULES",
        ] {
            refused(name, "loads as configuration");
        }
    }

    #[test]
    fn a_trailing_dot_or_space_or_a_lookalike_does_not_slip_past_the_list() {
        // Windows drops a trailing dot or space, so CLAUDE.md. lands as CLAUDE.md there; and
        // NFKC makes the fullwidth spelling the ASCII one.
        for name in [
            "CLAUDE.md.",
            "AGENTS.md ",
            "CLAUDE.md. .",
            "\u{FF23}\u{FF2C}\u{FF21}\u{FF35}\u{FF24}\u{FF25}.md",
        ] {
            refused(name, "loads as configuration");
        }
    }

    #[test]
    fn the_comparison_folds_case_as_python_casefold_does() {
        // Full case folding, not lowercasing: these differ only where a fold expands.
        assert_eq!(fold("Stra\u{DF}e"), "strasse");
        assert_eq!(fold("\u{1E9E}"), "ss");
        assert_eq!(fold("CLAUDE.MD"), "claude.md");
    }

    #[test]
    fn an_invisible_character_is_refused() {
        // HFS+ ignored some of these when comparing names (git's CVE-2014-9390).
        for name in [
            "CLAUDE\u{200C}.md",
            "notes\u{200B}.md",
            "a\u{2028}b",
            "\u{FEFF}x.txt",
        ] {
            refused(name, "invisible character");
        }
        refused("a\u{2029}b", "(U+2029)");
    }

    #[test]
    fn any_leading_dot_is_refused() {
        for name in [".hidden", ".claude", "..notes", "._x", ".cursorrules"] {
            refused(name, "begins with a dot");
        }
    }

    #[test]
    fn windows_device_names_are_refused_with_or_without_an_extension() {
        for name in [
            "CON", "con", "PRN", "AUX", "Nul.txt", "COM1", "com9.log", "LPT1", "lpt0",
        ] {
            refused(name, "Windows device name");
        }
        for name in [
            "COM\u{B9}",
            "LPT\u{B3}.txt",
            "CONIN$",
            "aux .txt",
            "nul.tar.gz",
        ] {
            refused(name, "Windows device name");
        }
        refused("Nul.txt", "(Nul)");
    }

    #[test]
    fn a_drive_letter_is_refused() {
        for name in ["C:x", "c:evil", "Z:"] {
            refused(name, "Windows drive");
        }
    }

    #[test]
    fn any_colon_is_refused_as_an_alternate_data_stream() {
        for name in [
            "a.txt:ads",
            "notes:Zone.Identifier",
            "10:30.txt",
            "a.txt::$DATA",
        ] {
            refused(name, "alternate data stream");
        }
    }

    #[test]
    fn a_character_windows_does_not_allow_is_refused() {
        for name in ["a?b", "a*b", "a\"b", "a|b", "a<b", "a>b"] {
            refused(name, "Windows does not allow");
        }
        // The one named is the first in code point order, as the Python client names it.
        refused("a|b?c", "holds '?'");
    }

    #[test]
    fn a_trailing_dot_or_space_is_refused() {
        for name in ["notes.", "notes ", "a.txt.."] {
            refused(name, "ends in a dot or a space");
        }
    }

    #[test]
    fn a_directory_or_control_character_is_still_refused() {
        for name in ["", ".", "..", "a/b", "a\\b", "x\ny", "\0"] {
            refused(name, "not a bare file name");
        }
        refused(&"x".repeat(256), "over 255 characters");
    }

    #[test]
    fn an_ordinary_name_is_allowed() {
        for name in [
            "notes.md",
            "trace.log",
            "README.md",
            "report 2026-10-16.pdf",
            "claude-notes.md",
            "my.CLAUDE.md",
            "agents.md.txt",
            "console.txt",
            "COM10",
            "conx.txt",
            "donn\u{E9}es.csv",
            "a.b.c",
            &"x".repeat(255),
            // 255 characters, 510 bytes: the bound counts characters, as the Python client does.
            &"\u{E9}".repeat(255),
        ] {
            assert_eq!(safe_name(name), Ok(name), "{name:?}");
        }
    }

    /// A scratch directory unique to this case and process, removed when dropped.
    struct Scratch(PathBuf);

    impl Scratch {
        fn new(case: &str) -> Scratch {
            let dir = std::env::temp_dir().join(format!("iki-names-{}-{case}", std::process::id()));
            let _ = std::fs::remove_dir_all(&dir);
            std::fs::create_dir_all(&dir).unwrap();
            Scratch(dir)
        }
    }

    impl Drop for Scratch {
        fn drop(&mut self) {
            let _ = std::fs::remove_dir_all(&self.0);
        }
    }

    #[test]
    fn downloads_under_home_when_it_exists() {
        let home = Scratch::new("dl-home");
        let downloads = home.0.join("Downloads");
        std::fs::create_dir(&downloads).unwrap();
        assert_eq!(downloads_dir_in(&home.0, None), Some(downloads.clone()));
        // On Windows too: ~\Downloads before the known folder.
        let none = || None;
        assert_eq!(downloads_dir_in(&home.0, Some(&none)), Some(downloads));
    }

    #[test]
    fn the_xdg_download_dir_otherwise() {
        let home = Scratch::new("dl-xdg");
        std::fs::create_dir_all(home.0.join(".config")).unwrap();
        std::fs::create_dir(home.0.join("T\u{E9}l\u{E9}chargements")).unwrap();
        std::fs::write(
            home.0.join(".config").join("user-dirs.dirs"),
            "# written by xdg-user-dirs-update\nXDG_DESKTOP_DIR=\"$HOME/Desktop\"\n\
             XDG_DOWNLOAD_DIR=\"$HOME/T\u{E9}l\u{E9}chargements\"\n",
        )
        .unwrap();
        assert_eq!(
            downloads_dir_in(&home.0, None),
            Some(home.0.join("T\u{E9}l\u{E9}chargements"))
        );
    }

    #[test]
    fn an_xdg_download_dir_that_is_home_means_none() {
        let home = Scratch::new("dl-xdg-home");
        std::fs::create_dir_all(home.0.join(".config")).unwrap();
        std::fs::write(
            home.0.join(".config").join("user-dirs.dirs"),
            "XDG_DOWNLOAD_DIR=\"$HOME/\"\n",
        )
        .unwrap();
        assert_eq!(downloads_dir_in(&home.0, None), None);
    }

    #[test]
    fn the_windows_known_folder_otherwise() {
        let home = Scratch::new("dl-win");
        let elsewhere = home.0.join("D").join("Downloads2");
        std::fs::create_dir_all(&elsewhere).unwrap();
        let found = || Some(elsewhere.clone());
        assert_eq!(
            downloads_dir_in(&home.0, Some(&found)),
            Some(elsewhere.clone())
        );
        let none = || None;
        assert_eq!(downloads_dir_in(&home.0, Some(&none)), None);
        // And the known folder is never home itself.
        let home_itself = || Some(home.0.clone());
        assert_eq!(downloads_dir_in(&home.0, Some(&home_itself)), None);
    }

    #[test]
    fn none_when_there_is_no_downloads_directory() {
        let home = Scratch::new("dl-none");
        assert_eq!(downloads_dir_in(&home.0, None), None);
        // No home at all is not the working directory's `Downloads`.
        assert_eq!(downloads_dir_in(Path::new(""), None), None);
    }

    #[test]
    fn with_no_path_it_saves_to_downloads_and_with_none_it_asks_for_a_path() {
        let downloads = PathBuf::from("/somewhere/Downloads");
        assert_eq!(
            save_target(None, None, || Ok(downloads.clone())),
            Ok(SaveTarget::Dir {
                dir: downloads.clone(),
                as_name: None
            })
        );
        let err = save_target(None, None, || Err("no downloads directory found".into()));
        assert!(err.unwrap_err().contains("no downloads directory found"));
    }

    #[test]
    fn a_path_naming_a_file_is_held_to_the_same_rules_before_dialing() {
        let dir = Scratch::new("target");
        let never = || -> Result<PathBuf, String> { panic!("downloads consulted") };
        for name in ["CLAUDE.md", ".zshenv", "AUX.txt"] {
            let err = save_target(Some(dir.0.join(name)), None, never).unwrap_err();
            assert!(err.contains("give PATH another name"), "{name}: {err}");
        }
        assert_eq!(
            save_target(Some(dir.0.join("envrc.txt")), None, never),
            Ok(SaveTarget::File(dir.0.join("envrc.txt")))
        );
        assert_eq!(
            save_target(Some(dir.0.clone()), Some("mcp.json".into()), never),
            Ok(SaveTarget::Dir {
                dir: dir.0.clone(),
                as_name: Some("mcp.json".into())
            })
        );
        // --as names the file, so PATH must be a directory.
        let err = save_target(Some(dir.0.join("x.txt")), Some("y".into()), never).unwrap_err();
        assert!(err.contains("not a directory"), "{err}");
    }

    #[test]
    fn as_is_held_to_the_same_rules_before_dialing() {
        let dir = Scratch::new("as");
        for name in [".envrc", "CLAUDE.md", "nul", "x:y"] {
            let command = Command::Save {
                entry: "urn:layer:work:entry:1".into(),
                path: Some(dir.0.clone()),
                as_name: Some(name.into()),
                force: false,
            };
            let err = prepare(command).err().expect(name);
            assert!(err.contains("choose another --as NAME"), "{name}: {err}");
        }
        assert_eq!(
            args(&["save", "urn:layer:talk:entry:1", "--as", "mcp.json"]).unwrap(),
            Command::Save {
                entry: "urn:layer:talk:entry:1".into(),
                path: None,
                as_name: Some("mcp.json".into()),
                force: false
            }
        );
        assert!(args(&["save", "urn:layer:talk:entry:1", "--as"]).is_err());
    }

    #[test]
    fn a_file_a_reader_could_not_save_is_not_posted_and_says_how() {
        let dir = Scratch::new("post");
        let dotfile = dir.0.join(".gitignore");
        std::fs::write(&dotfile, "target/\n").unwrap();
        let post = |file_name: Option<&str>| {
            prepare(Command::Post {
                layer: "work".into(),
                kind: PostKind::File {
                    path: dotfile.clone(),
                    file_name: file_name.map(str::to_string),
                    media_type: None,
                },
                in_reply_to: None,
                supersedes: None,
            })
        };
        let err = post(None).err().expect("refused");
        assert!(
            err.contains("loads as configuration") && err.contains("--file-name NAME"),
            "{err}"
        );
        assert!(post(Some("gitignore.txt")).is_ok());
        let err = post(Some("CON")).err().expect("refused");
        assert!(err.contains("Windows device name"), "{err}");
    }

    #[test]
    fn a_media_type_is_guessed_or_checked() {
        assert_eq!(guess_media_type("brief.md", "text"), "text/markdown");
        assert_eq!(guess_media_type("a.png", "base64"), "image/png");
        assert_eq!(guess_media_type("noext", "text"), "text/plain");
        assert_eq!(
            guess_media_type("noext", "base64"),
            "application/octet-stream"
        );
        assert!(is_media_type("application/vnd.ikigai+json"));
        assert!(!is_media_type("text/plain; charset=utf-8"));
        assert!(!is_media_type("Text/Plain"));
    }

    /// The file entry round-trips through what `save` reads: the Turtle the door would hold,
    /// parsed, checked and decoded back to the same bytes, text and binary alike.
    #[test]
    fn a_file_entry_saves_back_to_its_own_bytes() {
        for (name, data) in [
            ("trace.log", b"line \"one\"\nline two\n".to_vec()),
            ("blob.bin", (0u8..=255).collect::<Vec<u8>>()),
        ] {
            let entry = file_entry(name, &data, None, Thread::default()).unwrap();
            let stored = entry
                .turtle
                .replace("<urn:layer:new>", "<urn:layer:talk:entry:01x>");
            let node = Node::of(&stored, "urn:layer:talk:entry:01x").unwrap();
            assert!(node.types.contains(&format!("{IK}File")));
            assert_eq!(node.one("fileName").unwrap(), name);
            assert_eq!(node.bytes().unwrap(), data);
        }
    }

    /// A file whose content does not match its own digest or size is refused, naming which.
    #[test]
    fn a_file_entry_that_lies_about_its_bytes_is_refused() {
        let good = file_entry("a.txt", b"hello\n", None, Thread::default())
            .unwrap()
            .turtle
            .replace("<urn:layer:new>", "<urn:layer:t:entry:1>");
        let wrong_hash = good.replace(&digest(b"hello\n"), &digest(b"other"));
        let err = Node::of(&wrong_hash, "urn:layer:t:entry:1")
            .unwrap()
            .bytes()
            .unwrap_err();
        assert!(err.contains("hashes to"), "{err}");
        let wrong_size = good.replace("ik:byteSize 6", "ik:byteSize 7");
        let err = Node::of(&wrong_size, "urn:layer:t:entry:1")
            .unwrap()
            .bytes()
            .unwrap_err();
        assert!(err.contains("it says 7"), "{err}");
    }

    /// A file's base64 decodes by the ecosystem's one profile (`ikigai_http::base64`, ledger
    /// #1857 and #1866): canonical RFC 4648 §4, so nonzero pad bits are refused, and the
    /// refusal names the rule without echoing a byte or an offset of the content.
    #[test]
    fn a_file_entry_whose_base64_is_not_canonical_is_refused_by_the_shared_profile() {
        let data = [0x89u8, 0, 0, 1];
        let (encoding, b64) = encode(&data);
        assert_eq!((encoding, b64.as_str()), ("base64", "iQAAAQ=="));
        let good = file_entry("a.bin", &data, None, Thread::default())
            .unwrap()
            .turtle
            .replace("<urn:layer:new>", "<urn:layer:t:entry:1>");
        // `R` differs from `Q` only in the low bits that padding leaves unused.
        let bad = good.replace("\"iQAAAQ==\"", "\"iQAAAR==\"");
        assert_ne!(bad, good, "the fixture names the content literal");
        let err = Node::of(&bad, "urn:layer:t:entry:1")
            .unwrap()
            .bytes()
            .unwrap_err();
        assert!(err.contains("not canonical base64"), "{err}");
        assert!(!err.contains("offset"), "{err}");
    }

    #[test]
    fn the_shapes_say_whether_a_layer_takes_files_and_how_large() {
        // As a shapes FILE writes it (blank nodes) and as the door serves it (named nodes).
        let file = "@prefix sh: <http://www.w3.org/ns/shacl#> . @prefix ik: <https://ikigai-rs.dev/ns#> .\n\
                    <urn:s:file> sh:targetClass ik:File ; sh:property \
                    [ sh:path ik:content ; sh:maxLength 520192 ] , [ sh:path ik:body ; sh:maxLength 9 ] .";
        assert_eq!(
            Shapes::parse(file).unwrap(),
            Shapes {
                takes_files: true,
                content_bound: Some(520192)
            }
        );
        let served = "@prefix sh: <http://www.w3.org/ns/shacl#> .\n\
                      <urn:layer:t:shapes:n45> sh:path <https://ikigai-rs.dev/ns#content> .\n\
                      <urn:layer:t:shapes:n45> sh:maxLength \"100\"^^<http://www.w3.org/2001/XMLSchema#integer> .";
        assert_eq!(
            Shapes::parse(served).unwrap(),
            Shapes {
                takes_files: false,
                content_bound: Some(100)
            }
        );
    }

    #[test]
    fn a_file_over_the_bound_is_refused_naming_what_fits() {
        let check = |content: &str| FileCheck {
            name: "x.txt".into(),
            size: content.len(),
            encoding: "text",
            content: content.into(),
        };
        assert!(too_large(&check("12345"), 5).is_none());
        // An escaped quote is two bytes in the literal.
        let why = too_large(&check("1234\""), 5).unwrap();
        assert!(
            why.contains("6 bytes as text") && why.contains("about 3 bytes of binary"),
            "{why}"
        );
    }

    #[test]
    fn names_and_entries_are_checked_before_anything_is_dialed() {
        assert!(layer_name("talk-chris").is_ok());
        for bad in ["", "Talk", "-x", "x-", "a_b", &"a".repeat(25)] {
            assert!(layer_name(bad).is_err(), "{bad:?}");
        }
        assert_eq!(entry_layer("urn:layer:talk:entry:01m4").unwrap(), "talk");
        for bad in [
            "urn:layer:talk:01m4",
            "urn:layer:Talk:entry:1",
            "urn:layer:talk:entry:",
            "urn:layer:talk:entry:a b",
            "urn:layer:talk:entry:a> <b",
        ] {
            assert!(entry_layer(bad).is_err(), "{bad:?}");
        }
    }

    #[test]
    fn the_command_line_parses_as_the_python_client_spells_it() {
        assert_eq!(
            args(&["post", "brief", "talk", "brief.md", "--repo", "o/n"]).unwrap(),
            Command::Post {
                layer: "talk".into(),
                kind: PostKind::Brief {
                    source: "brief.md".into(),
                    title: None,
                    repo: Some("o/n".into())
                },
                in_reply_to: None,
                supersedes: None,
            }
        );
        assert_eq!(
            args(&[
                "post",
                "issue",
                "talk",
                "--title",
                "T",
                "--severity",
                "major"
            ])
            .unwrap(),
            Command::Post {
                layer: "talk".into(),
                kind: PostKind::Issue {
                    source: "-".into(),
                    title: Some("T".into()),
                    repo: None,
                    severity: Some("major".into())
                },
                in_reply_to: None,
                supersedes: None,
            }
        );
        assert_eq!(
            args(&[
                "post",
                "file",
                "talk",
                "./a.png",
                "--in-reply-to",
                "urn:layer:talk:entry:1"
            ])
            .unwrap(),
            Command::Post {
                layer: "talk".into(),
                kind: PostKind::File {
                    path: "./a.png".into(),
                    file_name: None,
                    media_type: None
                },
                in_reply_to: Some("urn:layer:talk:entry:1".into()),
                supersedes: None,
            }
        );
        assert_eq!(
            args(&["save", "urn:layer:talk:entry:1", "out/", "--force"]).unwrap(),
            Command::Save {
                entry: "urn:layer:talk:entry:1".into(),
                path: Some("out/".into()),
                as_name: None,
                force: true
            }
        );
        // A flag that belongs to another kind, or to the connection, is refused by name.
        let err = args(&["post", "file", "talk", "a", "--severity", "major"]).unwrap_err();
        assert!(err.contains("--severity"), "{err}");
        let err = args(&["post", "brief", "talk", "--override", "x=y"]).unwrap_err();
        assert!(err.contains("connection flags go before `layers`"), "{err}");
        assert!(args(&["post", "file", "talk"]).is_err());
        assert!(args(&["post", "note", "talk"]).is_err());
        assert!(args(&[]).is_err());
    }
}