ikigai-script 0.3.0

Scripts (Lisp; SPARQL queries with typed parameters and authority derived from the graphs they name; or plans, validated before they are stored, with authority derived from their steps) as resources in any ikigai host: `urn:script:{name}` is an atom with content-addressed versions, a compiled form, a result you read and runs you record, run under the least of what the script declares, what its publisher held at publish time, the host's ceiling and the runner's own capability. A host library: no binary, no elevation.
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
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
//! The resources, as endpoints.
//!
//! Only two of them hold state — the script (its head and versions) and a run — and they
//! hold it through the host's [`Backend`]. Everything else is composed: `…:result` and
//! `…:runs` read `…:compiled` through the kernel and hand the program to the language's
//! evaluator as a sub-request; `urn:script:eval` saves a draft by sinking to the script
//! like any other writer.

use std::collections::{BTreeMap, BTreeSet, HashMap};
use std::sync::{Arc, Mutex};

use async_trait::async_trait;
use ikigai_core::{
    ActionSpec, ArgRef, ArgSpec, Description, Endpoint, EndpointSpace, Error, Exact, Invocation,
    Iri, ReprType, Representation, Request, Resolution, Result, Scope, Space, SpaceEntry, Topology,
    UriTemplate, Verb,
};
use serde::{Deserialize, Serialize};

use crate::authority::{
    self, cap, cap_run, principal_of, Act, Ceiling, CeilingPolicy, CAP_ANY, CAP_DELETE, CAP_LISP,
    CAP_READ, CAP_READ_PUBLIC, CAP_RUN, CAP_RUN_PUBLIC, CAP_WRITE,
};
use crate::backend::Backend;
use crate::model::{
    self, Event, Head, Language, Outcome, Run, State, Version, MAX_RECORDED_RESULT, SCHEMA,
};
use crate::name::{self, CATALOG_IRI, EVAL_IRI};
use crate::plan;
use crate::sparql::{self, Analysis, Form, Parameter, SparqlDoor, Value, CAP_READ_GRAPH};

const PLAIN: &str = "text/plain";
const JSON: &str = "application/json";
const XSD_STRING: &str = "http://www.w3.org/2001/XMLSchema#string";
const XSD_BOOLEAN: &str = "http://www.w3.org/2001/XMLSchema#boolean";
const XSD_INTEGER: &str = "http://www.w3.org/2001/XMLSchema#integer";
const TURTLE: &str = "text/turtle";
/// The faces of a read: the source (or a summary) for people, the record for machines.
const READ_FACES: [&str; 2] = [PLAIN, JSON];
/// The faces of the catalog and a run record: those, and the graph (see [`crate::graph`]).
const RECORD_FACES: [&str; 3] = [PLAIN, JSON, TURTLE];

/// What a host decides when it mounts scripts: where they live and the ceiling it allows each
/// one. Built with [`SpaceConfig::new`] and handed to [`space`].
///
/// Who a request comes from is not configured here: it is the principal the request's
/// capability names (`urn:cap:principal:<iri>`), which the host's door mints with
/// `Capability::with_principal` (see [`authority::principal_of`]).
///
/// There is no default backend and no default ceiling, on purpose: both are the host's
/// decision, and a library that guessed either would be guessing about authority.
#[derive(Clone)]
#[non_exhaustive]
pub struct SpaceConfig {
    backend: Arc<dyn Backend>,
    ceiling: CeilingPolicy,
    sparql: Option<SparqlDoor>,
    on_change: Option<ChangeHook>,
    /// Each published SPARQL script's own endpoints (its contract), built once per head and
    /// dropped by every publish or retire through this crate. `None`: the template's.
    described: Arc<Mutex<HashMap<String, Option<PerScript>>>>,
}

/// What a host runs when a script's head changes through this crate: see
/// [`SpaceConfig::on_change`].
pub type ChangeHook = Arc<dyn Fn(&str) + Send + Sync>;

impl SpaceConfig {
    /// Scripts kept in `backend`, each run under the ceiling `ceiling` answers for it.
    pub fn new(backend: Arc<dyn Backend>, ceiling: CeilingPolicy) -> SpaceConfig {
        SpaceConfig {
            backend,
            ceiling,
            sparql: None,
            on_change: None,
            described: Arc::new(Mutex::new(HashMap::new())),
        }
    }

    /// Accept SPARQL scripts, evaluated through `door` (the store's graph-scoped doors).
    /// Without it, publishing `language=sparql` is refused: which store a query runs
    /// against is the host's decision, as the evaluator a Lisp script reaches is.
    pub fn sparql(mut self, door: SparqlDoor) -> SpaceConfig {
        self.sparql = Some(door);
        self
    }

    /// Run `hook` with the script's name after every publish or retire through this crate.
    ///
    /// ★ **Wire it to `kernel.cut(ikigai_core::BINDINGS_THREAD)`.** A published SPARQL
    /// script is its own entry in the catalog and the action manifold (`…:result` and
    /// `…:runs` with ITS parameters as arguments), and the kernel caches every description
    /// and Meta answer under `urn:kernel:bindings`, which it cannot know a publish changed.
    /// The library holds no kernel and an endpoint may not cut that thread without
    /// `urn:cap:kernel:cut`, so the host, which holds both, makes the cut. Unwired, a new
    /// query is runnable at once but the catalog, MCP and the engine's argument routing
    /// describe the old set until something else cuts the thread.
    pub fn on_change(mut self, hook: ChangeHook) -> SpaceConfig {
        self.on_change = Some(hook);
        self
    }

    /// A script's head changed through this crate: forget its contract, tell the host.
    fn changed(&self, name: &str) {
        self.described
            .lock()
            .expect("the described-script memo")
            .remove(name);
        if let Some(hook) = &self.on_change {
            hook(name);
        }
    }
}

/// Bind every script resource.
///
/// âš  **Bind order is resolution order** (`EndpointSpace` takes the first grammar that
/// matches), and `urn:script:{name}`'s template captures the rest of an IRI, so the two
/// exact names go first and the bare script last. A name cannot contain `:`, so
/// `urn:script:x:compiled` can only be the compiled form of `x`.
pub fn space(config: SpaceConfig) -> ScriptSpace {
    let shared = Arc::new(config);
    let template = |t: &str| UriTemplate::parse(t).expect("a constant template");
    let inner = EndpointSpace::new()
        .bind(Exact::new(EVAL_IRI), EvalEndpoint)
        .bind(
            Exact::new(CATALOG_IRI),
            CatalogEndpoint {
                shared: Arc::clone(&shared),
            },
        )
        .bind(
            template("urn:script:{name}:version:{digest}"),
            VersionEndpoint {
                shared: Arc::clone(&shared),
            },
        )
        .bind(
            template("urn:script:{name}:run:{id}"),
            RunEndpoint {
                shared: Arc::clone(&shared),
            },
        )
        .bind(
            template("urn:script:{name}:compiled"),
            CompiledEndpoint {
                shared: Arc::clone(&shared),
            },
        )
        .bind(
            template("urn:script:{name}:result"),
            ResultEndpoint {
                shared: Arc::clone(&shared),
            },
        )
        .bind(
            template("urn:script:{name}:runs"),
            RunsEndpoint {
                shared: Arc::clone(&shared),
            },
        )
        .bind(
            template("urn:script:{name}"),
            ScriptEndpoint {
                shared: Arc::clone(&shared),
            },
        );
    ScriptSpace { inner, shared }
}

// ---------------------------------------------------------------------------------------
// Gates
// ---------------------------------------------------------------------------------------

fn holds(inv: &Invocation<'_>, scope: &str) -> bool {
    inv.capability.allows(scope)
}

/// Whether the caller may `act` on `name`: its exact grant or a namespace grant, and no
/// exclusion naming it ([`authority::holds`]).
fn may(inv: &Invocation<'_>, act: Act, name: &str) -> bool {
    authority::holds(inv.capability, act, name)
}

/// Whether the caller reaches a PUBLIC script through `public` (a `…:public` grant), unless
/// an exclusion it holds names the script.
fn may_public(inv: &Invocation<'_>, act: Act, public: &str, name: &str) -> bool {
    holds(inv, public) && !authority::excluded(inv.capability, act, name)
}

/// How a refusal says what a script grant can name.
const GRANT_FORMS: &str = "A script grant names one script (`urn:cap:script:{act}:{name}`) or a \
     namespace of them (`urn:cap:script:{act}:{namespace}-*`)";

/// Public and published: what the `…:public` grants reach.
fn is_public(head: &Head) -> bool {
    head.public && head.state == State::Published
}

/// The head of `name`, if the caller may read it.
///
/// ★ No existence oracle: a caller without the script's own read grant is told `Denied`
/// whether the script exists or not, unless it is public and they hold
/// [`CAP_READ_PUBLIC`]. Only a caller who may read it learns that it is absent.
fn readable_head(inv: &Invocation<'_>, shared: &SpaceConfig, name: &str) -> Result<Option<Head>> {
    gated_head(inv, shared, name, &[(Act::Read, CAP_READ_PUBLIC)])
}

/// The head of `name` if the caller may do one of `acts` to it ([`may`]), or the script is
/// public and they hold that act's public grant. Otherwise `Denied`, naming both.
fn gated_head(
    inv: &Invocation<'_>,
    shared: &SpaceConfig,
    name: &str,
    acts: &[(Act, &str)],
) -> Result<Option<Head>> {
    if acts.iter().any(|(act, _)| may(inv, *act, name)) {
        return shared.backend.head(name);
    }
    if let Ok(Some(head)) = shared.backend.head(name) {
        if is_public(&head)
            && acts
                .iter()
                .any(|(act, public)| may_public(inv, *act, public, name))
        {
            return Ok(Some(head));
        }
    }
    Err(Error::Denied(format!(
        "this capability holds none of {} — nor, for a public script, {}. {GRANT_FORMS}, and \
         an exclusion (`…:-{{name}}`) takes one back out",
        acts.iter()
            .map(|(act, _)| cap(*act, name))
            .collect::<Vec<_>>()
            .join(", "),
        acts.iter()
            .map(|(_, public)| *public)
            .collect::<Vec<_>>()
            .join(", ")
    )))
}

fn require(inv: &Invocation<'_>, act: Act, name: &str, doing: &str) -> Result<()> {
    if may(inv, act, name) {
        Ok(())
    } else {
        Err(Error::Denied(format!(
            "{doing} needs `{}`, which this capability does not hold. {GRANT_FORMS}, and an \
             exclusion (`…:-{{name}}`) takes one back out",
            cap(act, name)
        )))
    }
}

fn found(head: Option<Head>, name: &str) -> Result<Head> {
    head.ok_or_else(|| Error::NotFound(format!("no script is published at urn:script:{name}")))
}

// ---------------------------------------------------------------------------------------
// Drafts: a version never published is its author's alone
// ---------------------------------------------------------------------------------------

/// Who may see one version of a script, beyond the read (or run) grant that gates it.
///
/// ★ **A draft is visible only to its author until it is published.** The author is the
/// principal recorded when the draft was written, the principal the writer's CAPABILITY
/// named ([`authority::principal_of`]), and the reader is its author when their capability
/// `acts_as` that principal. Never an argument, so no caller can name itself the author.
/// This only ever NARROWS what a grant reaches: seeing a draft needs the grant AND
/// authorship, so it can give no one more than their capability.
///
/// Every answer is CACHEABLE, whatever the sight (ledger #1077). It is a function of the
/// capability and the script's state alone: the kernel keys its cache on the capability,
/// which carries the principal, so alice's cached draft is never bob's answer even when the
/// rest of their capabilities are the same; and every answer hangs from the script's
/// thread, which a publish or retire cuts. Before 0.2.0 the principal came from a host
/// stamper the cache key could not see, and these answers could not be cached.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
enum Sight {
    /// Published content, the caller's own draft, or a root caller.
    Visible,
    /// A version never published, and the caller is not its author: absent, told exactly
    /// as a name nobody wrote is (`NotFound`, `false`), so its existence does not leak.
    Hidden,
}

/// Whether `digest` was ever published under this head: the head published at it, or any
/// change in its history did. Once published, always: history only grows.
fn ever_published(head: &Head, digest: &str) -> bool {
    (head.version == digest && head.state == State::Published)
        || head
            .history
            .iter()
            .any(|event| event.action == "publish" && event.version == digest)
}

/// What the caller may see of version `digest` of `head`'s script.
///
/// Root sees everything: it is the host's own authority, not a principal's, and holds the
/// backend the draft is stored in (`acts_as` is true for root, and root also sees a draft
/// whose author is nobody's identity).
fn sight(inv: &Invocation<'_>, head: &Head, digest: &str) -> Sight {
    if ever_published(head, digest) || inv.capability.is_root() {
        return Sight::Visible;
    }
    let wrote_it = |author: &str| authority::is_identity(author) && inv.capability.acts_as(author);
    let mine = (head.version == digest && wrote_it(&head.publisher))
        || head
            .history
            .iter()
            .any(|e| e.action == "draft" && e.version == digest && wrote_it(&e.principal));
    if mine {
        Sight::Visible
    } else {
        Sight::Hidden
    }
}

/// `found`'s NotFound for a name the caller may read but cannot see: the same words as for
/// a name nobody wrote, plus, for a capability naming no principal (which says nothing
/// about any one name), why that caller can see no draft at all.
fn absent(inv: &Invocation<'_>, name: &str) -> Error {
    let mut message = format!("no script is published at urn:script:{name}");
    if inv.capability.principal().is_none() && !inv.capability.is_root() {
        message.push_str(
            " (this request's capability names no principal (`urn:cap:principal:…`, minted \
             by the host's door), so it cannot be shown to be a draft's author: a draft is \
             visible to its author and root alone until it is published)",
        );
    }
    Error::NotFound(message)
}

/// The head of `name`, from a gate's answer, if the caller may also SEE the version it
/// points at.
fn seen_head(inv: &Invocation<'_>, name: &str, gated: Option<Head>) -> Result<Head> {
    let Some(head) = gated else {
        return Err(absent(inv, name));
    };
    match sight(inv, &head, &head.version) {
        Sight::Hidden => Err(absent(inv, name)),
        Sight::Visible => Ok(head),
    }
}

fn head_version(shared: &SpaceConfig, head: &Head) -> Result<Version> {
    shared
        .backend
        .version(&head.name, &head.version)?
        .ok_or_else(|| {
            Error::Endpoint(format!(
                "urn:script:{} points at version {}, which its backend does not hold",
                head.name, head.version
            ))
        })
}

// ---------------------------------------------------------------------------------------
// Arguments and faces
// ---------------------------------------------------------------------------------------

/// An optional inline argument; present-but-not-UTF-8 is an error, absent is `None`.
fn optional<'a>(inv: &'a Invocation<'_>, name: &str) -> Result<Option<&'a str>> {
    match inv.inline_str(name) {
        Ok(value) => Ok(Some(value)),
        Err(Error::MissingArgument(_)) => Ok(None),
        Err(other) => Err(other),
    }
}

fn parse_bool(name: &str, text: &str) -> Result<bool> {
    match text.trim() {
        "true" => Ok(true),
        "false" => Ok(false),
        other => Err(Error::InvalidArgument {
            name: name.to_string(),
            detail: format!("`{other}` is not `true` or `false`"),
        }),
    }
}

/// Which face was asked for: [`READ_FACES`]. One this resource cannot serve is refused,
/// never substituted.
fn wanted_face(inv: &Invocation<'_>) -> Result<&'static str> {
    face_of(inv, &READ_FACES)
}

/// Which of `faces` was asked for (the first when none was).
fn face_of(inv: &Invocation<'_>, faces: &[&'static str]) -> Result<&'static str> {
    match optional(inv, "as")? {
        None => Ok(faces[0]),
        Some(asked) => {
            let bare = asked.split(';').next().unwrap_or(asked).trim();
            faces
                .iter()
                .find(|face| **face == bare)
                .copied()
                .ok_or_else(|| Error::InvalidArgument {
                    name: "as".to_string(),
                    detail: format!(
                        "`{asked}` is not a face this resource serves; one of {}",
                        faces.join(", ")
                    ),
                })
        }
    }
}

fn plain(text: impl Into<String>) -> Representation {
    Representation::new(
        ReprType::new(PLAIN).with_param("charset", "utf-8"),
        text.into().into_bytes(),
    )
}

fn json<T: Serialize>(value: &T) -> Result<Representation> {
    let mut bytes = serde_json::to_vec_pretty(value)
        .map_err(|e| Error::Endpoint(format!("could not serialize a record: {e}")))?;
    bytes.push(b'\n');
    Ok(Representation::new(ReprType::new(JSON), bytes))
}

fn iri(text: &str) -> Result<Iri> {
    Iri::parse(text).map_err(|e| Error::Endpoint(format!("`{text}` is not an IRI: {e}")))
}

fn inline(text: &str) -> ArgRef {
    ArgRef::Inline(text.as_bytes().to_vec())
}

fn name_arg() -> ArgSpec {
    ArgSpec::new("name")
        .summary(
            "The script's name: one segment of lowercase letters, digits, `-` and `_` \
             (`eval`, `catalog`, `public`, `outcome` and `principal` are reserved).",
        )
        .class(XSD_STRING)
        .binding()
}

fn as_arg() -> ArgSpec {
    ArgSpec::new("as")
        .summary(
            "The face: `text/plain` (for people) or `application/json` (the versioned \
             record, `\"schema\": 1`). Any other is refused, never substituted.",
        )
        .class(XSD_STRING)
        .one_of(READ_FACES)
        .default_value(PLAIN)
        .optional()
}

/// `as` for a record with a graph face.
fn record_as_arg() -> ArgSpec {
    ArgSpec::new("as")
        .summary(
            "The face: `text/plain` (for people), `application/json` (the versioned record, \
             `\"schema\": 1`) or `text/turtle` (the graph: PROV-O and the shared \
             vocabulary, no blank nodes). Any other is refused, never substituted.",
        )
        .class(XSD_STRING)
        .one_of(RECORD_FACES)
        .default_value(PLAIN)
        .optional()
}

fn turtle(graph: crate::graph::Graph) -> Result<Representation> {
    let bytes = graph
        .turtle()
        .map_err(|e| Error::Endpoint(format!("could not write the graph face: {e}")))?;
    Ok(Representation::new(
        ReprType::new(TURTLE).with_param("charset", "utf-8"),
        bytes,
    ))
}

fn data_arg(arg: &str, summary: &str) -> ArgSpec {
    ArgSpec::new(arg)
        .summary(summary)
        .class(XSD_STRING)
        .optional()
}

// ---------------------------------------------------------------------------------------
// urn:script:{name} — the script (the head), and publishing
// ---------------------------------------------------------------------------------------

/// The script's JSON face: its head, flattened, with the version it points at.
#[derive(Serialize)]
struct ScriptDocument<'a> {
    iri: String,
    #[serde(flatten)]
    head: &'a Head,
    #[serde(rename = "versionIri")]
    version_iri: String,
    language: Language,
    requires: &'a BTreeSet<String>,
    source: &'a str,
}

struct ScriptEndpoint {
    shared: Arc<SpaceConfig>,
}

#[async_trait]
impl Endpoint for ScriptEndpoint {
    async fn invoke(&self, inv: &Invocation<'_>) -> Result<Representation> {
        let name = name::from_bindings(inv)?;
        let shared = &self.shared;
        match inv.request.verb {
            Verb::Exists => {
                let exists = match readable_head(inv, shared, &name)? {
                    None => false,
                    Some(head) => sight(inv, &head, &head.version) == Sight::Visible,
                };
                Ok(plain(if exists { "true\n" } else { "false\n" }).cacheable())
            }
            Verb::Source => {
                let want = wanted_face(inv)?;
                let head = seen_head(inv, &name, readable_head(inv, shared, &name)?)?;
                let version = head_version(shared, &head)?;
                let repr = if want == JSON {
                    json(&ScriptDocument {
                        iri: name::script_iri(&name),
                        head: &head,
                        version_iri: name::version_iri(&name, &head.version),
                        language: version.language,
                        requires: &version.requires,
                        source: &version.source,
                    })?
                } else {
                    plain(version.source)
                };
                Ok(repr.cacheable())
            }
            Verb::Sink => publish(inv, shared, &name).await,
            Verb::Delete => retire(inv, shared, &name),
            other => Err(unsupported("script", other)),
        }
    }

    fn name(&self) -> &str {
        "script"
    }

    fn describe(&self) -> Description {
        Description::new("script")
            .title("A script")
            .summary(
                "A script as a resource: fetch its source without running it, publish or \
                 replace it (every write is a new content-addressed version), or retire it.",
            )
            .verb(Verb::Meta)
            .action(
                ActionSpec::new(Verb::Source)
                    .summary(
                        "The script, WITHOUT running it: its source (`text/plain`), or its head \
                         record (`application/json`): state, public flag, version, what it \
                         declares and what its publisher held. Needs \
                         `urn:cap:script:read:{name}`, or `urn:cap:script:read:public` for a \
                         public published script.",
                    )
                    .input(name_arg())
                    .input(as_arg())
                    .output(PLAIN)
                    .output(JSON)
                    .requires(CAP_READ),
            )
            .action(
                ActionSpec::new(Verb::Exists)
                    .summary("Whether the script exists (to a caller who may read it).")
                    .input(name_arg())
                    .output(PLAIN)
                    .requires(CAP_READ),
            )
            .action(
                ActionSpec::new(Verb::Sink)
                    .summary(
                        "Publish or replace the script; answers the new version's IRI. The \
                         declared `requires` (plus the language's own capability) must all \
                         be held by the publisher, or the publish is refused: a script never \
                         runs with more than its publisher held. Needs \
                         `urn:cap:script:write:{name}`.",
                    )
                    .input(name_arg())
                    .input(
                        ArgSpec::new("content")
                            .summary("The source text.")
                            .class(XSD_STRING),
                    )
                    .input(
                        ArgSpec::new("language")
                            .summary(
                                "The script's language: `lisp`, `sparql` (a query or update, \
                                 when the host mounts a SPARQL door), or `plan` (an \
                                 ik:Process graph, when the host binds the plan doors).",
                            )
                            .class(XSD_STRING)
                            .one_of(Language::ALL.map(Language::as_str))
                            .default_value("lisp")
                            .optional(),
                    )
                    .input(
                        ArgSpec::new("requires")
                            .summary(
                                "Whitespace-separated `urn:cap:` grants the script needs to \
                                 run. Lisp: the language's capability is added; exact grants \
                                 only, no wildcards, no exclusions. SPARQL: DERIVED from the \
                                 graphs the text names; a plan: DERIVED from its steps' \
                                 contracts. For both, omit it: one that says anything else \
                                 is refused, naming the derived set.",
                            )
                            .class(XSD_STRING)
                            .optional(),
                    )
                    .input(
                        ArgSpec::new("public")
                            .summary(
                                "Whether holders of `urn:cap:script:run:public` (a host's \
                                 anonymous principal) may run it and holders of \
                                 `urn:cap:script:read:public` read it.",
                            )
                            .class(XSD_BOOLEAN)
                            .one_of(["true", "false"])
                            .default_value("false")
                            .optional(),
                    )
                    .input(
                        ArgSpec::new("state")
                            .summary(
                                "`published` (runnable) or `draft` (saved, not runnable, \
                                 and visible only to its author until published). To \
                                 retire, Delete.",
                            )
                            .class(XSD_STRING)
                            .one_of(["published", "draft"])
                            .default_value("published")
                            .optional(),
                    )
                    .input(
                        ArgSpec::new("if-version")
                            .summary(
                                "Write only if the head is still this version (`none`: only \
                                 if the script does not exist yet); otherwise `Conflict`.",
                            )
                            .class(XSD_STRING)
                            .optional(),
                    )
                    .output(PLAIN)
                    .requires(CAP_WRITE),
            )
            .action(
                ActionSpec::new(Verb::Delete)
                    .summary(
                        "Retire the script: it stays readable and its versions fetchable, but \
                         it never runs again until republished. Needs \
                         `urn:cap:script:delete:{name}`.",
                    )
                    .input(name_arg())
                    .output(PLAIN)
                    .requires(CAP_DELETE),
            )
    }
}

async fn publish(inv: &Invocation<'_>, shared: &SpaceConfig, name: &str) -> Result<Representation> {
    require(inv, Act::Write, name, "publishing a script")?;
    let source = inv.inline_str("content")?;
    let language = optional(inv, "language")?
        .map(Language::parse)
        .transpose()?
        .unwrap_or(Language::Lisp);
    let requires = match language {
        Language::Lisp => {
            let mut requires = optional(inv, "requires")?
                .map(authority::parse_requires)
                .transpose()?
                .unwrap_or_default();
            requires.insert(CAP_LISP.to_string());
            requires
        }
        Language::Sparql => derived_requires(inv, shared, source)?,
        Language::Plan => {
            // Validated by the host's validator first, so a malformed plan is refused
            // naming the shape it broke; then read for what this crate decides itself;
            // then its authority derived from its steps' contracts.
            plan::check_bound(source)?;
            plan::validate(inv, source).await?;
            plan::analyze(source)?;
            plan::derive(inv, source, optional(inv, "requires")?).await?
        }
    };
    let public = optional(inv, "public")?
        .map(|v| parse_bool("public", v))
        .transpose()?
        .unwrap_or(false);
    let state = match optional(inv, "state")?.map(str::trim) {
        None | Some("published") => State::Published,
        Some("draft") => State::Draft,
        Some(other) => {
            return Err(Error::InvalidArgument {
                name: "state".to_string(),
                detail: format!(
                    "`{other}` is not `published` or `draft`; to retire a script, Delete it"
                ),
            })
        }
    };
    // ★ No elevation: refused here, before anything is written, if the publisher does not
    // hold every scope the script declares.
    let grant = authority::grant_at_publish(inv.capability, &requires)?;

    let version = Version {
        language,
        requires,
        source: source.to_string(),
    };
    let digest = version.digest();
    let current = shared.backend.head(name)?;
    if let Some(expected) = optional(inv, "if-version")? {
        let at = current.as_ref().map_or("none", |h| h.version.as_str());
        if at != expected.trim() {
            return Err(Error::Conflict(format!(
                "urn:script:{name} is at {at}, not {}; read it again and retry",
                expected.trim()
            )));
        }
    }
    shared.backend.put_version(name, &version)?;

    let principal = principal_of(inv.capability);
    let at = inv.now().map(|t| t.as_millis());
    let mut history = current
        .as_ref()
        .map(|h| h.history.clone())
        .unwrap_or_default();
    history.push(Event {
        action: if state == State::Draft {
            "draft"
        } else {
            "publish"
        }
        .to_string(),
        version: digest.clone(),
        state,
        public,
        principal: principal.clone(),
        at,
    });
    let head = Head {
        schema: SCHEMA,
        name: name.to_string(),
        version: digest.clone(),
        state,
        public,
        granted: grant.granted,
        exclusions: grant.exclusions,
        publisher: principal,
        updated: at,
        history,
    };
    shared.backend.swap_head(name, current.as_ref(), &head)?;
    shared.changed(name);
    Ok(plain(format!("{}\n", name::version_iri(name, &digest))))
}

/// A SPARQL script's authority: DERIVED from the graphs its text names, never declared.
/// A `requires=` is accepted only when it says exactly the same thing, and refused naming
/// the derived set when it does not, so an author can neither under- nor over-declare.
fn derived_requires(
    inv: &Invocation<'_>,
    shared: &SpaceConfig,
    source: &str,
) -> Result<BTreeSet<String>> {
    let door = door(shared).map_err(|_| Error::InvalidArgument {
        name: "language".to_string(),
        detail: "this host takes no SPARQL scripts: it mounts no SPARQL door".to_string(),
    })?;
    let derived = sparql::analyze(source, door)?.requires();
    if let Some(said) = optional(inv, "requires")? {
        let said: BTreeSet<String> = said.split_whitespace().map(str::to_string).collect();
        if said != derived {
            return Err(Error::InvalidArgument {
                name: "requires".to_string(),
                detail: format!(
                    "a SPARQL script's authority is derived from the graphs its text names, \
                     not declared, and this one derives {}; the `requires` given says {}. \
                     Omit `requires`",
                    listed(&derived),
                    listed(&said)
                ),
            });
        }
    }
    Ok(derived)
}

fn listed(scopes: &BTreeSet<String>) -> String {
    if scopes.is_empty() {
        "nothing".to_string()
    } else {
        scopes.iter().cloned().collect::<Vec<_>>().join(" ")
    }
}

fn door(shared: &SpaceConfig) -> Result<&SparqlDoor> {
    shared.sparql.as_ref().ok_or_else(|| {
        Error::Endpoint(
            "this host mounts no SPARQL door (`SpaceConfig::sparql`), so it cannot run a \
             SPARQL script"
                .to_string(),
        )
    })
}

fn retire(inv: &Invocation<'_>, shared: &SpaceConfig, name: &str) -> Result<Representation> {
    require(inv, Act::Delete, name, "retiring a script")?;
    let current = found(shared.backend.head(name)?, name)?;
    let iri = name::script_iri(name);
    if current.state == State::Retired {
        return Ok(plain(format!("{iri} was already retired\n")));
    }
    let at = inv.now().map(|t| t.as_millis());
    let mut head = current.clone();
    head.state = State::Retired;
    head.updated = at;
    head.history.push(Event {
        action: "retire".to_string(),
        version: current.version.clone(),
        state: State::Retired,
        public: current.public,
        principal: principal_of(inv.capability),
        at,
    });
    shared.backend.swap_head(name, Some(&current), &head)?;
    shared.changed(name);
    Ok(plain(format!("retired {iri}\n")))
}

// ---------------------------------------------------------------------------------------
// urn:script:{name}:version:{digest}
// ---------------------------------------------------------------------------------------

struct VersionEndpoint {
    shared: Arc<SpaceConfig>,
}

#[async_trait]
impl Endpoint for VersionEndpoint {
    async fn invoke(&self, inv: &Invocation<'_>) -> Result<Representation> {
        let name = name::from_bindings(inv)?;
        let digest = inv
            .bindings
            .get("digest")
            .ok_or_else(|| Error::Endpoint("no `digest` captured".to_string()))?
            .to_string();
        // ★ Hung from the SCRIPT's thread, which every publish and retire cuts, and named
        // FIRST, so every answer below carries it: a success (ledger #1076) and a NotFound
        // alike (ledger #1079). Everything this answer says is the script's state, not the
        // version's: whether that content has been written under the name (a version named
        // by its content can be asked for before anyone publishes it), whether the caller may
        // see it (a draft is its author's until it is published), and whether the caller may
        // read the name at all (a public script's version stops being readable through the
        // public grant when it is retired). The kernel hangs an answer from its OWN name,
        // which no write here ever targets, so without this a cached `false`, or a cached
        // fallback over a NotFound, outlived the publish that ended the absence. Caching the
        // negative is kept, not dropped: it is a backend read per ask, and an uncacheable
        // `Exists` would make every composite over it uncacheable. The kernel carries the
        // thread on a miss only (NotFound, Unresolved); a refusal stays never-cached.
        inv.depends_on(name::script_iri(&name));
        let head = readable_head(inv, &self.shared, &name)?;
        let version = self.shared.backend.version(&name, &digest);
        // A version that exists is seen as its head's history says: one never published is
        // its author's alone. One with no head at all (a write that lost its race) was never
        // published either.
        let seen = match (&version, &head) {
            (Ok(Some(_)), Some(head)) => sight(inv, head, &digest),
            (Ok(Some(_)), None) if !inv.capability.is_root() => Sight::Hidden,
            _ => Sight::Visible,
        };
        let no_such = || Error::NotFound(format!("urn:script:{name} has no version {digest}"));
        match inv.request.verb {
            Verb::Exists => {
                let exists = match version {
                    Ok(v) => v.is_some() && seen != Sight::Hidden,
                    Err(Error::InvalidArgument { .. }) => false,
                    Err(other) => return Err(other),
                };
                Ok(plain(if exists { "true\n" } else { "false\n" }).cacheable())
            }
            Verb::Source => {
                let want = wanted_face(inv)?;
                let version = version?.ok_or_else(no_such)?;
                if seen == Sight::Hidden {
                    return Err(no_such());
                }
                let repr = if want == JSON {
                    json(&version.document(&name))?
                } else {
                    plain(version.source)
                };
                Ok(repr.cacheable())
            }
            other => Err(unsupported("script-version", other)),
        }
    }

    fn name(&self) -> &str {
        "script-version"
    }

    fn describe(&self) -> Description {
        let digest = || {
            ArgSpec::new("digest")
                .summary("The version's digest: `sha256:` and 64 hex digits over its content.")
                .class(XSD_STRING)
                .binding()
        };
        Description::new("script-version")
            .title("One version of a script")
            .summary(
                "An immutable version, named by the sha256 of its content (language, declared \
                 capability, source). Republishing moves the head; every version stays \
                 fetchable here.",
            )
            .verb(Verb::Meta)
            .action(
                ActionSpec::new(Verb::Source)
                    .summary(
                        "The version's source (`text/plain`) or its record (`application/json`).",
                    )
                    .input(name_arg())
                    .input(digest())
                    .input(as_arg())
                    .output(PLAIN)
                    .output(JSON)
                    .requires(CAP_READ),
            )
            .action(
                ActionSpec::new(Verb::Exists)
                    .summary("Whether the script has this version.")
                    .input(name_arg())
                    .input(digest())
                    .output(PLAIN)
                    .requires(CAP_READ),
            )
    }
}

// ---------------------------------------------------------------------------------------
// urn:script:{name}:compiled — the prepared form
// ---------------------------------------------------------------------------------------

/// The compiled form: everything a run needs, in one cacheable resource hung from the
/// script's thread. For Lisp the "compilation" is preparation — the evaluator offers no
/// separate compile step to cache — so this is the program and the authority it runs
/// under, bound to the version they came from. For SPARQL it adds what the parsed text
/// says: its form, its parameters and the graphs it names ([`Analysis`]).
#[derive(Clone, Debug, Serialize, Deserialize)]
pub struct Prepared {
    /// [`SCHEMA`].
    pub schema: u32,
    /// The script.
    pub name: String,
    /// `urn:script:{name}:compiled`.
    pub iri: String,
    /// The version prepared.
    pub version: String,
    /// Its language.
    pub language: Language,
    /// The door a run issues to: `urn:lisp:eval`, or the host's SPARQL door for this form.
    pub evaluator: String,
    /// The program.
    pub program: String,
    /// What it declares (for SPARQL: what its text derives).
    pub requires: BTreeSet<String>,
    /// What its publisher held of that.
    pub granted: BTreeSet<String>,
    /// The publisher's exclusions.
    pub exclusions: BTreeSet<String>,
    /// Whether it is public.
    pub public: bool,
    /// Its state.
    pub state: State,
    /// For a SPARQL script: its form, parameters and graphs, read from the parsed text.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub sparql: Option<Analysis>,
    /// For a plan: its process, its steps' verbs (read or write) and its parameters.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub plan: Option<plan::Analysis>,
}

struct CompiledEndpoint {
    shared: Arc<SpaceConfig>,
}

#[async_trait]
impl Endpoint for CompiledEndpoint {
    async fn invoke(&self, inv: &Invocation<'_>) -> Result<Representation> {
        if inv.request.verb != Verb::Source {
            return Err(unsupported("script-compiled", inv.request.verb));
        }
        let name = name::from_bindings(inv)?;
        // ★ Hung from the SCRIPT's thread, which every publish and retire cuts (the kernel
        // cuts the thread named after a Sink's or Delete's target), and named FIRST so a
        // NotFound carries it as well as a success (ledger #1079): without it a cached
        // compiled form would outlive the version it was prepared from, and a cached fallback
        // over "nothing is published here" (or "this draft is not yours") would outlive the
        // publish that ended it. A run as a read reaches this form by sub-request, so its
        // answers, and its misses, inherit the thread.
        inv.depends_on(name::script_iri(&name));
        // Readable by a reader or a runner: a run reads its program here. A draft's program
        // is its author's alone, so a run of someone else's draft finds nothing to run.
        let gated = gated_head(
            inv,
            &self.shared,
            &name,
            &[(Act::Read, CAP_READ_PUBLIC), (Act::Run, CAP_RUN_PUBLIC)],
        )?;
        let head = seen_head(inv, &name, gated)?;
        let version = head_version(&self.shared, &head)?;
        let (evaluator, analysis, plan_analysis) = match version.language {
            Language::Lisp => (LISP_EVAL.to_string(), None, None),
            Language::Sparql => {
                let door = door(&self.shared)?;
                let analysis = sparql::analyze(&version.source, door)?;
                (door.query_iri(analysis.form), Some(analysis), None)
            }
            Language::Plan => (
                plan::EVAL.to_string(),
                None,
                Some(plan::analyze(&version.source)?),
            ),
        };
        let prepared = Prepared {
            schema: SCHEMA,
            iri: name::part_iri(&name, "compiled"),
            version: head.version.clone(),
            language: version.language,
            evaluator,
            program: version.source,
            requires: version.requires,
            granted: head.granted,
            exclusions: head.exclusions,
            public: head.public,
            state: head.state,
            sparql: analysis,
            plan: plan_analysis,
            name: name.clone(),
        };
        Ok(json(&prepared)?.cacheable())
    }

    fn name(&self) -> &str {
        "script-compiled"
    }

    fn describe(&self) -> Description {
        Description::new("script-compiled")
            .title("A script's compiled form")
            .summary(
                "The head version prepared for its evaluator, with the authority it runs \
                 under (and, for SPARQL, its form, parameters and graphs; for a plan, its \
                 steps' verbs and its parameters): cached, and cut \
                 whenever the script is republished or retired. Readable by a holder of the \
                 script's read OR run grant (a run reads its program here).",
            )
            .verb(Verb::Meta)
            .action(
                ActionSpec::new(Verb::Source)
                    .summary("The prepared form, as JSON.")
                    .input(name_arg())
                    .output(JSON)
                    .requires(CAP_ANY),
            )
    }
}

// ---------------------------------------------------------------------------------------
// Running: …:result (a read) and …:runs (a write)
// ---------------------------------------------------------------------------------------

const LISP_EVAL: &str = "urn:lisp:eval";

/// Read the compiled form through the kernel, check the run gate and the state, and
/// compute what the run may keep of the runner's capability (and under which ceiling).
async fn prepare_run(
    inv: &Invocation<'_>,
    shared: &SpaceConfig,
    name: &str,
) -> Result<(Prepared, BTreeSet<String>, Ceiling)> {
    // Composed, not reached into: the run is a reader of its own compiled form, so the
    // compiled form's thread (and the script's, through it) lands on the result.
    let compiled = inv
        .issue(Request::new(
            Verb::Source,
            iri(&name::part_iri(name, "compiled"))?,
        ))
        .await?;
    let prepared: Prepared = serde_json::from_slice(&compiled.bytes).map_err(|e| {
        Error::Endpoint(format!(
            "urn:script:{name}:compiled did not answer a prepared form: {e}"
        ))
    })?;
    if prepared.name != name {
        return Err(Error::Endpoint(format!(
            "urn:script:{name}:compiled answered for `{}`",
            prepared.name
        )));
    }
    let public = prepared.public && prepared.state == State::Published;
    let may_run =
        may(inv, Act::Run, name) || (public && may_public(inv, Act::Run, CAP_RUN_PUBLIC, name));
    if !may_run {
        return Err(Error::Denied(format!(
            "running urn:script:{name} needs `{}` or a namespace grant covering it{}. \
             {GRANT_FORMS}, and an exclusion (`…:-{{name}}`) takes one back out",
            cap_run(name),
            if prepared.public {
                format!(" (or `{CAP_RUN_PUBLIC}`, since it is public)")
            } else {
                " — it is not public".to_string()
            }
        )));
    }
    match prepared.state {
        State::Published => {}
        State::Draft => {
            return Err(Error::Conflict(format!(
                "urn:script:{name} is a draft; publish it (`state=published`) to run it, or \
                 try the code at urn:script:eval"
            )))
        }
        State::Retired => {
            return Err(Error::Conflict(format!(
                "urn:script:{name} is retired; publish it again to run it"
            )))
        }
    }
    let ceiling = (shared.ceiling)(name);
    let keep = authority::effective(
        &prepared.requires,
        &prepared.granted,
        &prepared.exclusions,
        &ceiling,
    );
    Ok((prepared, keep, ceiling))
}

/// The Lisp evaluator sub-request: `in` is the program, `data` what it reads with `(input)`.
fn lisp_request(program: &str, data: Option<&str>) -> Result<Request> {
    let mut request = Request::new(Verb::Source, iri(LISP_EVAL)?).with_arg("in", inline(program));
    if let Some(data) = data {
        request = request.with_arg("data", inline(data));
    }
    Ok(request)
}

/// `run_args`: the description of the run's input, shared by both run doors.
const DATA_SUMMARY: &str = "Optional data the script reads with `(input)` — data, never code.";

/// Which door a run came through.
#[derive(Clone, Copy, PartialEq, Eq)]
enum Door {
    /// `…:result`: a read, answered with the script's value.
    Result,
    /// `…:runs`: a write, recorded.
    Runs,
}

/// What a run issues, and exactly the scopes it issues it under (before the runner's own
/// capability is attenuated to them).
struct Planned {
    request: Request,
    keep: BTreeSet<String>,
}

/// The run's sub-request, for whichever language the script is in.
async fn prepare_request(
    inv: &Invocation<'_>,
    shared: &SpaceConfig,
    prepared: &Prepared,
    keep: BTreeSet<String>,
    ceiling: &Ceiling,
    through: Door,
) -> Result<Planned> {
    match prepared.language {
        Language::Lisp => {
            let data = optional(
                inv,
                match through {
                    Door::Result => "data",
                    Door::Runs => "content",
                },
            )?;
            Ok(Planned {
                request: lisp_request(&prepared.program, data)?,
                keep,
            })
        }
        Language::Sparql => plan_sparql(inv, shared, prepared, keep, ceiling, through).await,
        Language::Plan => plan_eval_request(inv, prepared, keep, ceiling, through),
    }
}

/// A plan run: refuse a write through the read door, take the plan's parameters, turn a
/// root publisher's derived families into exact grants, and address `urn:plan:eval`.
///
/// The plan's steps then run as `urn:plan:eval`'s sub-requests under exactly the narrowed
/// capability, and each meets its own target's floor there: a step the run may not take is
/// refused by the kernel at that step, typed, and arrives here unchanged.
fn plan_eval_request(
    inv: &Invocation<'_>,
    prepared: &Prepared,
    keep: BTreeSet<String>,
    ceiling: &Ceiling,
    through: Door,
) -> Result<Planned> {
    let name = &prepared.name;
    let analysis = prepared.plan.as_ref().ok_or_else(|| {
        Error::Endpoint(format!(
            "urn:script:{name}:compiled carries no plan analysis"
        ))
    })?;
    if through == Door::Result && analysis.mutates() {
        let writers: Vec<String> = analysis
            .steps
            .iter()
            .filter(|s| s.mutates())
            .map(|s| format!("<{}> ({} <{}>)", s.iri, s.verb, s.resolves))
            .collect();
        return Err(Error::InvalidArgument {
            name: "name".to_string(),
            detail: format!(
                "urn:script:{name} is a plan that writes ({}), so it is not a read: run it with \
                 a Sink to urn:script:{name}:runs",
                writers.join(", ")
            ),
        });
    }
    let declared: Vec<(&str, bool)> = analysis
        .parameters
        .iter()
        .map(|p| (p.name.as_str(), p.must_be_given()))
        .collect();
    let values = given_parameters(inv, name, &declared, through)?;
    let face = optional(inv, "as")?;
    Ok(Planned {
        request: plan::eval_request(&prepared.program, &values, face),
        keep: plan::expand_families(keep, inv.capability, ceiling),
    })
}

/// A SPARQL run: bind the parameters into the parsed query, work out its dataset and the
/// exact store grants it needs, and address the host's store door for its form.
///
/// ★ **The dataset is never wider than the authority.** The graphs the text fixes need
/// their grants in the run's capability (the store refuses the whole read, naming the
/// grant, when one is missing). A graph only the run decides (`GRAPH ?g`, or a parameter
/// naming a graph) is admitted only under the derived family, and only when the
/// publisher held it (or was root), the host's ceiling allows it AND the runner holds it:
/// that set is "the caller's readable union", computed here per run and never more.
async fn plan_sparql(
    inv: &Invocation<'_>,
    shared: &SpaceConfig,
    prepared: &Prepared,
    mut keep: BTreeSet<String>,
    ceiling: &Ceiling,
    through: Door,
) -> Result<Planned> {
    let name = &prepared.name;
    let door = door(shared)?;
    let analysis = prepared.sparql.as_ref().ok_or_else(|| {
        Error::Endpoint(format!(
            "urn:script:{name}:compiled carries no SPARQL analysis"
        ))
    })?;
    if through == Door::Result && !analysis.form.is_read() {
        return Err(Error::InvalidArgument {
            name: "name".to_string(),
            detail: format!(
                "urn:script:{name} is a SPARQL UPDATE, which is a write: run it with a Sink to \
                 urn:script:{name}:runs"
            ),
        });
    }
    let values = parameter_values(inv, name, &analysis.parameters, through)?;
    let face = sparql_face(inv, analysis.form)?;
    let bound = sparql::bind(&prepared.program, door, &values)?;

    let runner = inv.capability;
    // The root publisher's marker: expanded below into exact grants, never attenuated as
    // it stands (attenuation keeps grants by exact name).
    let root_marker = keep.remove(CAP_READ_GRAPH);
    let family = prepared.requires.contains(CAP_READ_GRAPH);
    let by_family = |scope: &str, keep: &BTreeSet<String>| {
        family
            && runner.allows(scope)
            && if root_marker {
                ceiling.allows(scope)
            } else {
                keep.contains(scope)
            }
    };

    if bound.form == Form::Update {
        // An update's one graph is fixed by its text (a parameter cannot name it), so its
        // grants are exactly the derived ones, already in `keep`.
        let graph = bound.graphs.iter().next().cloned().ok_or_else(|| {
            Error::Endpoint(format!("urn:script:{name} names no graph to update"))
        })?;
        for scope in &prepared.requires {
            may_use(name, &graph, scope, &keep, runner)?;
        }
        let request = Request::new(Verb::Sink, iri(&door.query_iri(Form::Update))?)
            .with_arg("content", inline(&bound.text))
            .with_arg("graph", inline(&graph));
        return Ok(Planned { request, keep });
    }

    let mut extra = BTreeSet::new();
    for graph in &bound.graphs {
        let scope = sparql::cap_read_graph(graph);
        if !keep.contains(&scope) && by_family(&scope, &keep) {
            extra.insert(scope);
        }
    }
    let mut dataset = bound.graphs.clone();
    if bound.any_graph {
        if !family {
            return Err(Error::Endpoint(format!(
                "urn:script:{name} reads a variable graph but its record derives no \
                 `{CAP_READ_GRAPH}`; republish it"
            )));
        }
        let under = |scopes: &mut dyn Iterator<Item = &String>| -> BTreeSet<String> {
            scopes
                .filter_map(|s| s.strip_prefix(sparql::READ_GRAPH_PREFIX))
                .filter(|g| !g.starts_with('-') && !g.ends_with('*'))
                .map(str::to_string)
                .collect()
        };
        let candidates = if !root_marker {
            under(&mut keep.iter())
        } else if let Some(held) = runner.scopes() {
            under(&mut held.iter())
        } else {
            // Root ran a script a root published: the readable union is every graph the
            // store holds, listed by the store itself under the broad read the runner holds.
            let listing = inv
                .issue_attenuated(
                    Request::new(Verb::Source, iri(&door.graphs_iri())?),
                    [sparql::CAP_STORE_READ.to_string()],
                )
                .await?;
            String::from_utf8_lossy(&listing.bytes)
                .lines()
                .map(str::trim)
                .filter(|l| !l.is_empty())
                .map(str::to_string)
                .collect()
        };
        for graph in candidates {
            let scope = sparql::cap_read_graph(&graph);
            if keep.contains(&scope) && runner.allows(&scope) || by_family(&scope, &keep) {
                extra.insert(scope);
                dataset.insert(graph);
            }
        }
    }
    if dataset.is_empty() {
        return Err(Error::Denied(format!(
            "urn:script:{name} reads the caller's readable graphs, and this run may read none: \
             it needs `{}` grants the publisher held, the host allows and the runner holds",
            sparql::READ_GRAPH_PREFIX
        )));
    }
    keep.extend(extra);
    for graph in &dataset {
        may_use(name, graph, &sparql::cap_read_graph(graph), &keep, runner)?;
    }
    let graphs = dataset.into_iter().collect::<Vec<_>>().join(" ");
    let request = Request::new(Verb::Source, iri(&door.query_iri(bound.form))?)
        .with_arg("query", inline(&bound.text))
        .with_arg("graph", inline(&graphs))
        .with_arg("as", inline(face));
    Ok(Planned { request, keep })
}

/// Refused HERE, naming the exact grant and who withheld it, rather than by the store's
/// door: the kernel's floor there sees only the family a run holds, so its refusal would
/// name `urn:cap:store:read:graph:*` and not the graph.
fn may_use(
    name: &str,
    graph: &str,
    scope: &str,
    keep: &BTreeSet<String>,
    runner: &ikigai_core::Capability,
) -> Result<()> {
    if !runner.allows(scope) {
        return Err(Error::Denied(format!(
            "running urn:script:{name} reads or writes <{graph}>, which needs `{scope}`, and the \
             runner does not hold it"
        )));
    }
    if !keep.contains(scope) {
        return Err(Error::Denied(format!(
            "running urn:script:{name} reads or writes <{graph}>, which needs `{scope}`, and \
             this script may not have it: its publisher did not hold it, or the host's ceiling \
             for it does not allow it"
        )));
    }
    Ok(())
}

/// The face a SPARQL run answers in: `as=`, one its form can serve, or the form's default.
/// Refused, never substituted.
fn sparql_face(inv: &Invocation<'_>, form: Form) -> Result<&'static str> {
    let faces = form.faces();
    match optional(inv, "as")? {
        None => Ok(faces[0]),
        Some(asked) => {
            let bare = asked.split(';').next().unwrap_or(asked).trim();
            faces
                .iter()
                .find(|f| **f == bare)
                .copied()
                .ok_or_else(|| Error::InvalidArgument {
                    name: "as".to_string(),
                    detail: format!(
                        "`{asked}` is not a face a SPARQL {} answers in; one of {}",
                        form.as_str().to_uppercase(),
                        faces.join(", ")
                    ),
                })
        }
    }
}

/// The run's parameter values, each checked against its declared type.
///
/// What was given is [`given_parameters`]'s; a parameter not given takes its default, and
/// one with neither stays unbound.
fn parameter_values(
    inv: &Invocation<'_>,
    name: &str,
    parameters: &[Parameter],
    through: Door,
) -> Result<BTreeMap<String, Value>> {
    let declared: Vec<(&str, bool)> = parameters
        .iter()
        .map(|p| (p.name.as_str(), p.required && p.default.is_none()))
        .collect();
    let mut given = given_parameters(inv, name, &declared, through)?;
    let mut values = BTreeMap::new();
    for parameter in parameters {
        if let Some(lexical) = given
            .remove(&parameter.name)
            .or_else(|| parameter.default.clone())
        {
            values.insert(parameter.name.clone(), parameter.value(&lexical)?);
        }
    }
    Ok(values)
}

/// The argument names a TRANSPORT owns, which a run never reads as a script's parameters
/// (ledger #1173).
///
/// `ikigai-web` stamps a write with its provenance, `received`, `client` and `principal`,
/// read off the connection, and with the body's `content-type`; `ikigai-quic` stamps
/// `principal` on every verb. Each is the door's statement about the request, never the
/// caller's, and none is an input to a script: who a request is from reaches a run in its
/// CAPABILITY (`Capability::with_principal`, read with [`authority::principal_of`]), which
/// is what every run and publish records.
///
/// So the doors of this crate ignore these names wherever a script's parameters are read
/// (`…:result` and `…:runs`, for a query and a plan), instead of refusing them as arguments
/// the script does not declare — which made every run for effects through the HTTP door a
/// `400` — and no script may declare a parameter by one of them (refused at publish; see
/// [`sparql::RESERVED_PARAMETERS`] and [`plan::RESERVED_PARAMETERS`]). A value a CALLER
/// supplies under one of them (where a door passes it through) is ignored the same way, so
/// it can neither reach a script nor stand in for the principal.
///
/// ```
/// use ikigai_script::TRANSPORT_ARGUMENTS;
/// for owned in TRANSPORT_ARGUMENTS {
///     assert!(ikigai_script::sparql::RESERVED_PARAMETERS.contains(&owned), "{owned}");
///     assert!(ikigai_script::plan::RESERVED_PARAMETERS.contains(&owned), "{owned}");
/// }
/// ```
pub const TRANSPORT_ARGUMENTS: [&str; 4] = ["received", "client", "principal", "content-type"];

/// The parameters a run was GIVEN, as text, for a script that declares `declared` (each
/// parameter's name, and whether a run must give it).
///
/// Named arguments, or (through `…:runs`) a JSON object piped as `content`. A missing
/// parameter a run must give is `MissingArgument`; a parameter given both ways and an
/// argument the script does not declare are `InvalidArgument`: a binding the script does not
/// mention is refused, never ignored, so a filter you thought was applied can never silently
/// not be. Refused here, before a run is recorded: nothing ran.
///
/// The one exception is [`TRANSPORT_ARGUMENTS`]: a door's own stamps, ignored, never bound.
/// `declared` never holds one: the parameters are read again from the source each time the
/// compiled form is prepared, and that read refuses a reserved name, so a script stored
/// before they were reserved that declares one fails there, naming it, and never runs with
/// the parameter silently unbound.
fn given_parameters(
    inv: &Invocation<'_>,
    name: &str,
    declared: &[(&str, bool)],
    through: Door,
) -> Result<BTreeMap<String, String>> {
    let listed = || {
        if declared.is_empty() {
            "it declares none".to_string()
        } else {
            format!(
                "its parameters are {}",
                declared
                    .iter()
                    .map(|(p, _)| *p)
                    .collect::<Vec<_>>()
                    .join(", ")
            )
        }
    };
    for arg in inv.request.args.keys() {
        let known = arg == "as"
            || TRANSPORT_ARGUMENTS.contains(&arg.as_str())
            || (through == Door::Runs && arg == "content")
            || declared.iter().any(|(p, _)| p == arg);
        if !known {
            return Err(Error::InvalidArgument {
                name: arg.clone(),
                detail: format!("is not a parameter of urn:script:{name}; {}", listed()),
            });
        }
    }
    let mut piped: BTreeMap<String, String> = BTreeMap::new();
    if through == Door::Runs {
        // An empty body is no parameters: a bare `sink …:runs` sends one.
        if let Some(content) = optional(inv, "content")?.filter(|c| !c.trim().is_empty()) {
            let object: serde_json::Map<String, serde_json::Value> = serde_json::from_str(content)
                .map_err(|e| Error::InvalidArgument {
                    name: "content".to_string(),
                    detail: format!(
                        "a script's piped content is its parameters as one JSON object ({e})"
                    ),
                })?;
            for (key, value) in object {
                let lexical = match value {
                    serde_json::Value::String(s) => s,
                    serde_json::Value::Number(n) => n.to_string(),
                    serde_json::Value::Bool(b) => b.to_string(),
                    other => {
                        return Err(Error::InvalidArgument {
                            name: "content".to_string(),
                            detail: format!(
                                "`{key}` is {other}; a parameter's value is a string, a number \
                                 or a boolean"
                            ),
                        })
                    }
                };
                piped.insert(key, lexical);
            }
        }
    }
    let mut given = BTreeMap::new();
    for (parameter, must) in declared {
        let named = optional(inv, parameter)?;
        let lexical = match (named, piped.remove(*parameter)) {
            (Some(_), Some(_)) => {
                return Err(Error::InvalidArgument {
                    name: parameter.to_string(),
                    detail: "is given both by name and in the piped content".to_string(),
                })
            }
            (Some(value), None) => Some(value.to_string()),
            (None, Some(value)) => Some(value),
            (None, None) => None,
        };
        match lexical {
            Some(lexical) => {
                given.insert(parameter.to_string(), lexical);
            }
            None if *must => return Err(Error::MissingArgument(parameter.to_string())),
            None => {}
        }
    }
    if let Some(stray) = piped.keys().next() {
        return Err(Error::InvalidArgument {
            name: "content".to_string(),
            detail: format!(
                "`{stray}` is not a parameter of urn:script:{name}; {}",
                listed()
            ),
        });
    }
    Ok(given)
}

struct ResultEndpoint {
    shared: Arc<SpaceConfig>,
}

#[async_trait]
impl Endpoint for ResultEndpoint {
    async fn invoke(&self, inv: &Invocation<'_>) -> Result<Representation> {
        if inv.request.verb != Verb::Source {
            return Err(unsupported("script-result", inv.request.verb));
        }
        let name = name::from_bindings(inv)?;
        let (prepared, keep, ceiling) = prepare_run(inv, &self.shared, &name).await?;
        let planned =
            prepare_request(inv, &self.shared, &prepared, keep, &ceiling, Door::Result).await?;
        // ★ The ONLY authority a run gets: the runner's own capability, narrowed. There is
        // no form that widens, so a script cannot reach past its runner whatever it says.
        let answer = inv.issue_attenuated(planned.request, planned.keep).await?;
        // Cacheable as far as this endpoint is concerned; the kernel folds in the
        // evaluator's expiry (a Lisp program is uncacheable unless it opts in with
        // `(cacheable …)`; a query is as cacheable as the store's answer, which hangs from
        // the store's write threads) and the compiled form's threads. The authority thread
        // is the host's to cut when it changes this script's ceiling.
        Ok(Representation::new(answer.repr_type, answer.bytes)
            .cacheable()
            .depends_on(name::authority_thread(&name)))
    }

    fn name(&self) -> &str {
        "script-result"
    }

    fn describe(&self) -> Description {
        Description::new("script-result")
            .title("A script's result")
            .summary(
                "Run the script as a READ and answer its value. It runs under the runner's \
                 capability narrowed to what the script declares, its publisher held and \
                 the host allows it — never more. Cacheable exactly as far as the script's \
                 own sub-requests are (a Lisp program opts in with `(cacheable …)`), and \
                 recomputed when the script is republished. No run record: a cached answer \
                 runs nothing; record a run with `…:runs`. A published SPARQL script is \
                 its own entry, with its parameters as arguments.",
            )
            .verb(Verb::Meta)
            .action(
                ActionSpec::new(Verb::Source)
                    .summary("The script's answer.")
                    .input(name_arg())
                    .input(data_arg("data", DATA_SUMMARY))
                    .output(PLAIN)
                    .requires(CAP_RUN)
                    .requires(CAP_LISP),
            )
    }
}

struct RunsEndpoint {
    shared: Arc<SpaceConfig>,
}

#[async_trait]
impl Endpoint for RunsEndpoint {
    async fn invoke(&self, inv: &Invocation<'_>) -> Result<Representation> {
        if inv.request.verb != Verb::Sink {
            return Err(unsupported("script-runs", inv.request.verb));
        }
        let name = name::from_bindings(inv)?;
        let shared = &self.shared;
        let (prepared, keep, ceiling) = prepare_run(inv, shared, &name).await?;
        // A caller's mistake (a missing or ill-typed parameter) is refused here, before a
        // run is recorded: nothing ran.
        let planned = prepare_request(inv, shared, &prepared, keep, &ceiling, Door::Runs).await?;
        let runs_as = inv.capability.attenuate(planned.keep.iter().cloned());
        let mut run = Run {
            schema: SCHEMA,
            name: name.clone(),
            id: 0,
            version: prepared.version.clone(),
            principal: principal_of(inv.capability),
            capability: runs_as.scopes().cloned().unwrap_or_default(),
            started: inv.now().map(|t| t.as_millis()),
            ended: None,
            outcome: Outcome::Running,
            result: None,
            truncated: false,
            trace_span: inv.trace_span(),
        };
        run.id = shared.backend.start_run(&name, &run)?;
        let answer = inv.issue_attenuated(planned.request, planned.keep).await;
        run.ended = inv.now().map(|t| t.as_millis());
        match answer {
            Ok(repr) => {
                let (text, truncated) = model::truncate(
                    String::from_utf8_lossy(&repr.bytes).into_owned(),
                    MAX_RECORDED_RESULT,
                );
                run.outcome = Outcome::Ok;
                run.result = Some(text);
                run.truncated = truncated;
                shared.backend.finish_run(&name, &run)?;
                Ok(plain(format!("{}\n", run.iri())))
            }
            Err(error) => {
                run.outcome = Outcome::Failed {
                    kind: kind(&error).to_string(),
                    message: error.to_string(),
                };
                shared.backend.finish_run(&name, &run)?;
                Err(annotate(error, &run.iri()))
            }
        }
    }

    fn name(&self) -> &str {
        "script-runs"
    }

    fn describe(&self) -> Description {
        Description::new("script-runs")
            .title("A script's runs")
            .summary(
                "Run the script for its EFFECTS and record the run: who (the principal \
                 the capability names), which version, under exactly what capability, when, and the \
                 outcome. Answers the run's IRI; a run that fails is recorded too, and its \
                 error names the record. A published SPARQL script is its own entry, with \
                 its parameters as arguments.",
            )
            .verb(Verb::Meta)
            .action(
                ActionSpec::new(Verb::Sink)
                    .summary("Run the script and record the run.")
                    .input(name_arg())
                    .input(data_arg("content", DATA_SUMMARY))
                    .output(PLAIN)
                    .requires(CAP_RUN)
                    .requires(CAP_LISP),
            )
    }
}

// ---------------------------------------------------------------------------------------
// A published SPARQL script's own contract, and the space that offers it
// ---------------------------------------------------------------------------------------

/// A SPARQL script's or a plan's own endpoints, `…:result` and `…:runs`, each the
/// template's endpoint behind the script's OWN description.
#[derive(Clone)]
struct PerScript {
    result: Arc<dyn Endpoint>,
    runs: Arc<dyn Endpoint>,
    /// Whether its `…:result` is a read it offers (a query, not an update).
    reads: bool,
    /// Whether it is published, and so listed in the catalog. A draft of content that was
    /// published before, or a retired script, still answers under its own contract (its run
    /// is refused for its state, not for a language capability it never needed), but is not
    /// offered. A draft never published has no contract of its own (see `build`).
    listed: bool,
}

/// The template's endpoint, behind one script's contract.
///
/// ★ **Why a script is its own entry.** A description is the contract the engine routes
/// arguments by, selection matches on, MCP projects and the Emacs aliases are generated
/// from, and the kernel answers it per ENDPOINT, never per IRI. A query's or a plan's
/// parameters are real arguments only if they are in a description, so each gets one. And
/// the template's `requires` is Lisp's (`urn:cap:lisp`): a query or a plan behind it would be
/// refused at the floor for a language grant it never needs, so a plan with no parameters
/// still gets its own.
/// It is built once per head (the kernel memoizes floors by this allocation) and dropped
/// by the next publish or retire.
struct Described {
    inner: Arc<dyn Endpoint>,
    id: String,
    description: Description,
}

#[async_trait]
impl Endpoint for Described {
    async fn invoke(&self, inv: &Invocation<'_>) -> Result<Representation> {
        self.inner.invoke(inv).await
    }

    fn name(&self) -> &str {
        &self.id
    }

    fn describe(&self) -> Description {
        self.description.clone()
    }
}

/// The script resources as a space: [`space`]'s bindings, plus each published SPARQL
/// script and plan as its own `…:result` and `…:runs` entry, carrying its parameters.
pub struct ScriptSpace {
    inner: EndpointSpace,
    shared: Arc<SpaceConfig>,
}

impl ScriptSpace {
    /// The script's own endpoints, when it is a SPARQL script this host can run or a plan.
    ///
    /// A Lisp script has none (the template's contract IS its contract), but finding that
    /// out costs one head and version read per name, remembered until the next publish.
    fn per_script(&self, name: &str) -> Option<PerScript> {
        let shared = &self.shared;
        if let Some(known) = shared
            .described
            .lock()
            .expect("the described-script memo")
            .get(name)
        {
            return known.clone();
        }
        let built = self.build(name);
        // âš  Remember only scripts that exist: a name nobody published is a miss every
        // time, never an entry, so resolving arbitrary names cannot grow this map.
        if built.is_some() || matches!(shared.backend.head(name), Ok(Some(_))) {
            shared
                .described
                .lock()
                .expect("the described-script memo")
                .insert(name.to_string(), built.clone());
        }
        built
    }

    fn build(&self, name: &str) -> Option<PerScript> {
        let shared = &self.shared;
        let head = shared.backend.head(name).ok()??;
        // ★ A draft never published wears the TEMPLATE's contract. Meta is answered from the
        // description to anyone who can reach the door (the kernel's, before any endpoint
        // runs, so it cannot ask who is calling), and a script's own contract is made of its
        // text: its parameters, its leading comment, the graphs it names. Its own entry would
        // publish all three, and that the draft exists. Its run is still refused (for its
        // state, to its author; as absent, to everyone else), now at the template's floor.
        if !ever_published(&head, &head.version) {
            return None;
        }
        let version = shared.backend.version(name, &head.version).ok()??;
        match version.language {
            Language::Lisp => None,
            Language::Sparql => self.build_sparql(name, &head, &version),
            Language::Plan => self.build_plan(name, &head, &version),
        }
    }

    /// A per-script endpoint: the template's, behind `description`.
    fn described(
        &self,
        inner: Arc<dyn Endpoint>,
        id: String,
        description: Description,
    ) -> Arc<dyn Endpoint> {
        Arc::new(Described {
            inner,
            id,
            description,
        }) as Arc<dyn Endpoint>
    }

    fn result_endpoint(&self) -> Arc<dyn Endpoint> {
        Arc::new(ResultEndpoint {
            shared: Arc::clone(&self.shared),
        })
    }

    fn runs_endpoint(&self) -> Arc<dyn Endpoint> {
        Arc::new(RunsEndpoint {
            shared: Arc::clone(&self.shared),
        })
    }

    fn build_sparql(&self, name: &str, head: &Head, version: &Version) -> Option<PerScript> {
        let door = self.shared.sparql.as_ref()?;
        let analysis = sparql::analyze(&version.source, door).ok()?;
        let about = about(&version.source);
        let inputs: Vec<ArgSpec> = analysis
            .parameters
            .iter()
            .map(|p| {
                let mut arg = ArgSpec::new(p.name.clone())
                    .summary(
                        p.summary
                            .clone()
                            .unwrap_or_else(|| format!("The value of ?{} in the query.", p.name)),
                    )
                    .class(p.kind.iri().to_string());
                if let Some(default) = &p.default {
                    arg = arg.default_value(default.clone()).optional();
                } else if !p.required {
                    arg = arg.optional();
                }
                arg
            })
            .collect();
        let params = |spec: ActionSpec| contracted(spec, &inputs, name, head, version);
        let form = analysis.form;
        let faces = form.faces();
        let as_arg = || {
            ArgSpec::new("as")
                .summary(format!(
                    "The face: one of {}. Any other is refused, never substituted.",
                    faces.join(", ")
                ))
                .class(XSD_STRING)
                .one_of(faces.iter().copied())
                .default_value(faces[0])
                .optional()
        };
        let id = format!("script-{name}-result");
        let description = if form.is_read() {
            let mut spec = params(ActionSpec::new(Verb::Source))
                .summary(format!("Run the query {name} and answer its result."))
                .input(as_arg());
            for face in faces {
                spec = spec.output(*face);
            }
            Description::new(id.clone())
                .title(format!("{name}: a SPARQL {}", form.as_str().to_uppercase()))
                .summary(format!(
                    "{about}Runs as a READ under the runner's capability narrowed to the \
                     graphs the query names; cached as the store's answer is, and \
                     recomputed after a write to the store or a republish."
                ))
                .verb(Verb::Meta)
                .action(spec)
        } else {
            // An update is never a read: no action is offered here, and a Source that
            // arrives anyway is refused by the run, naming `…:runs`.
            Description::new(id.clone())
                .title(format!("{name}: a SPARQL UPDATE (run it at …:runs)"))
                .summary(format!(
                    "{about}A write: run it with a Sink to urn:script:{name}:runs."
                ))
                .verb(Verb::Meta)
        };
        let result = self.described(self.result_endpoint(), id, description);
        let id = format!("script-{name}-runs");
        let mut spec = params(ActionSpec::new(Verb::Sink))
            .summary(format!(
                "Run {name} for its effects and record the run; answers the run's IRI."
            ))
            .input(piped_parameters_arg())
            .output(PLAIN);
        if form.is_read() {
            spec = spec.input(as_arg());
        }
        let runs = self.described(
            self.runs_endpoint(),
            id.clone(),
            Description::new(id)
                .title(format!("{name}: runs"))
                .summary(format!(
                    "{about}Each run is recorded: who, which version, under exactly what \
                     capability, when, and the outcome."
                ))
                .verb(Verb::Meta)
                .action(spec),
        );
        Some(PerScript {
            result,
            runs,
            reads: form.is_read(),
            listed: head.state == State::Published,
        })
    }

    /// A plan's own contract: its declared parameters as arguments, and the capability
    /// derived from its steps as what the floor checks.
    fn build_plan(&self, name: &str, head: &Head, version: &Version) -> Option<PerScript> {
        let analysis = plan::analyze(&version.source).ok()?;
        let about = about(&version.source);
        let inputs: Vec<ArgSpec> = analysis
            .parameters
            .iter()
            .map(|p| {
                let mut arg = ArgSpec::new(p.name.clone())
                    .summary(
                        p.summary
                            .clone()
                            .unwrap_or_else(|| format!("The plan's parameter `{}`.", p.name)),
                    )
                    .class(p.class.clone().unwrap_or_else(|| XSD_STRING.to_string()));
                if let Some(default) = &p.default {
                    arg = arg.default_value(default.clone()).optional();
                } else if !p.required {
                    arg = arg.optional();
                }
                arg
            })
            .collect();
        let params = |spec: ActionSpec| contracted(spec, &inputs, name, head, version);
        // A plan's answer is its result step's own representation, so no list of faces can
        // be declared in advance: `as` is passed to the evaluator, which transrepts or
        // refuses.
        let as_arg = || {
            ArgSpec::new("as")
                .summary(
                    "The face of the plan's result: when its result step served another \
                     media type it is transrepted, and refused when nothing converts it.",
                )
                .class(XSD_STRING)
                .optional()
        };
        let reads = !analysis.mutates();
        let id = format!("script-{name}-result");
        let description = if reads {
            Description::new(id.clone())
                .title(format!("{name}: a plan"))
                .summary(format!(
                    "{about}Runs as a READ (every step reads) under the runner's capability \
                     narrowed to what the plan's steps require; as cacheable as its least \
                     cacheable step, and recomputed after a republish."
                ))
                .verb(Verb::Meta)
                .action(
                    params(ActionSpec::new(Verb::Source))
                        .summary(format!("Run the plan {name} and answer its result."))
                        .input(as_arg())
                        .output(PLAIN),
                )
        } else {
            // A plan with a Sink or Delete step is never a read: no action is offered here,
            // and a Source that arrives anyway is refused by the run, naming `…:runs`.
            Description::new(id.clone())
                .title(format!("{name}: a plan that writes (run it at …:runs)"))
                .summary(format!(
                    "{about}A write: run it with a Sink to urn:script:{name}:runs."
                ))
                .verb(Verb::Meta)
        };
        let result = self.described(self.result_endpoint(), id, description);
        let id = format!("script-{name}-runs");
        let runs = self.described(
            self.runs_endpoint(),
            id.clone(),
            Description::new(id)
                .title(format!("{name}: runs"))
                .summary(format!(
                    "{about}Each run is recorded: who, which version, under exactly what \
                     capability, when, and the outcome."
                ))
                .verb(Verb::Meta)
                .action(
                    params(ActionSpec::new(Verb::Sink))
                        .summary(format!(
                            "Run the plan {name} and record the run; answers the run's IRI."
                        ))
                        .input(piped_parameters_arg())
                        .input(as_arg())
                        .output(PLAIN),
                ),
        );
        Some(PerScript {
            result,
            runs,
            reads,
            listed: head.state == State::Published,
        })
    }
}

/// `spec` with a script's own `inputs`, the run gate and every scope its version requires.
///
/// The run gate, as the kernel can check it before the endpoint is entered: the script's own
/// grant, or the family of its top-level namespace when it is in one, so a namespace grant
/// is not refused at the floor ([`authority::run_floor`]); for a public one, the family of
/// every run grant (its own grant OR the public one, which `requires` cannot say any other
/// way: it is all-of). The exact rule is checked inside, by [`prepare_run`].
fn contracted(
    mut spec: ActionSpec,
    inputs: &[ArgSpec],
    name: &str,
    head: &Head,
    version: &Version,
) -> ActionSpec {
    for input in inputs {
        spec = spec.input(input.clone());
    }
    spec = spec.requires(if head.public {
        CAP_RUN.to_string()
    } else {
        authority::run_floor(name)
    });
    for scope in &version.requires {
        spec = spec.requires(scope.clone());
    }
    spec
}

/// `…:runs`' piped body, for a script with parameters.
fn piped_parameters_arg() -> ArgSpec {
    ArgSpec::new("content")
        .summary(
            "The parameters as one JSON object, for a pipe (a parameter given both here and \
             by name is refused).",
        )
        .class(XSD_STRING)
        .optional()
}

/// A script's own words for the catalog: its leading comment lines that declare nothing.
fn about(source: &str) -> String {
    let mut lines = Vec::new();
    for line in source.lines() {
        let trimmed = line.trim();
        let Some(body) = trimmed.strip_prefix('#') else {
            if trimmed.is_empty() {
                continue;
            }
            break;
        };
        let body = body.trim();
        if !body.is_empty() && !body.starts_with("@param") {
            lines.push(body);
        }
    }
    if lines.is_empty() {
        String::new()
    } else {
        format!("{} ", lines.join(" "))
    }
}

impl Space for ScriptSpace {
    fn resolve(&self, request: &Request, scope: &Scope) -> Resolution {
        let resolution = self.inner.resolve(request, scope);
        let (name, part) = match &resolution {
            Resolution::Hit(hit) => {
                let part = match hit.endpoint.name() {
                    "script-result" => Door::Result,
                    "script-runs" => Door::Runs,
                    _ => return resolution,
                };
                match hit.bindings.get("name") {
                    Some(name) if name::validate(name).is_ok() => (name.to_string(), part),
                    _ => return resolution,
                }
            }
            Resolution::Miss => return resolution,
        };
        match self.per_script(&name) {
            Some(per) => resolution.map_endpoint(|_| match part {
                Door::Result => per.result,
                Door::Runs => per.runs,
            }),
            None => resolution,
        }
    }

    fn entries(&self) -> Option<Vec<SpaceEntry>> {
        let mut entries = self.inner.entries().unwrap_or_default();
        for name in self.shared.backend.names().unwrap_or_default() {
            let Some(per) = self.per_script(&name).filter(|per| per.listed) else {
                continue;
            };
            if per.reads {
                entries.push(SpaceEntry::new(
                    name::part_iri(&name, "result"),
                    per.result.name(),
                ));
            }
            entries.push(SpaceEntry::new(
                name::part_iri(&name, "runs"),
                per.runs.name(),
            ));
        }
        Some(entries)
    }

    fn id(&self) -> Option<Iri> {
        self.inner.id()
    }

    fn topology(&self) -> Topology {
        self.inner.topology()
    }
}

/// An error's kind, as a run record spells it.
fn kind(error: &Error) -> &'static str {
    match error {
        Error::Unresolved(_) => "unresolved",
        Error::MissingArgument(_) => "missing-argument",
        Error::InvalidArgument { .. } => "invalid-argument",
        Error::Endpoint(_) => "endpoint",
        Error::Denied(_) => "denied",
        Error::NotFound(_) => "not-found",
        Error::Conflict(_) => "conflict",
        Error::Timeout(_) => "timeout",
        Error::Unavailable(_) => "unavailable",
        _ => "other",
    }
}

/// The same error, of the same type, naming the run record it was written to.
fn annotate(error: Error, run: &str) -> Error {
    let note = |message: String| format!("{message} (run recorded at {run})");
    match error {
        Error::Endpoint(m) => Error::Endpoint(note(m)),
        Error::Denied(m) => Error::Denied(note(m)),
        Error::NotFound(m) => Error::NotFound(note(m)),
        Error::Conflict(m) => Error::Conflict(note(m)),
        Error::Timeout(m) => Error::Timeout(note(m)),
        Error::Unavailable(m) => Error::Unavailable(note(m)),
        Error::InvalidArgument { name, detail } => Error::InvalidArgument {
            name,
            detail: note(detail),
        },
        other => other,
    }
}

// ---------------------------------------------------------------------------------------
// urn:script:{name}:run:{id}
// ---------------------------------------------------------------------------------------

struct RunEndpoint {
    shared: Arc<SpaceConfig>,
}

#[async_trait]
impl Endpoint for RunEndpoint {
    async fn invoke(&self, inv: &Invocation<'_>) -> Result<Representation> {
        let name = name::from_bindings(inv)?;
        // ★ Hung from the script's RUNS thread, which every run cuts (the kernel cuts the
        // thread named after a Sink's target, `urn:script:{name}:runs`), and named FIRST so a
        // NotFound carries it as well as a success (ledger #1079's shape): whether run `{id}`
        // exists is the state a run writes, never anything written through this record's own
        // name, so without it a cached fallback over "no run {id}" outlived the run that
        // recorded it. A FAILED run is recorded too, and cuts it as well: the kernel cuts a
        // Sink's target whenever the endpoint ran (ikigai-core 0.1.95; pinned in
        // `tests/lifecycle.rs`).
        inv.depends_on(name::part_iri(&name, "runs"));
        // A run record says who ran what: readable by the script's readers and its named
        // runners, never through the public grants.
        if !may(inv, Act::Read, &name) && !may(inv, Act::Run, &name) {
            return Err(Error::Denied(format!(
                "a run record of urn:script:{name} needs `{}` or `{}`. {GRANT_FORMS}",
                cap(Act::Read, &name),
                cap(Act::Run, &name)
            )));
        }
        let id_text = inv
            .bindings
            .get("id")
            .ok_or_else(|| Error::Endpoint("no `id` captured".to_string()))?;
        let id = id_text.parse::<u64>().map_err(|_| Error::InvalidArgument {
            name: "id".to_string(),
            detail: format!("`{id_text}` is not a run number"),
        });
        let run = match id {
            Ok(id) => self.shared.backend.run(&name, id)?,
            Err(_) if inv.request.verb == Verb::Exists => None,
            Err(e) => return Err(e),
        };
        match inv.request.verb {
            Verb::Exists => Ok(plain(if run.is_some() { "true\n" } else { "false\n" })),
            Verb::Source => {
                let want = face_of(inv, &RECORD_FACES)?;
                let run = run.ok_or_else(|| {
                    Error::NotFound(format!("urn:script:{name} has no run {id_text}"))
                })?;
                let finished = run.outcome != Outcome::Running;
                let repr = match want {
                    JSON => json(&run)?,
                    TURTLE => {
                        let mut graph = crate::graph::Graph::default();
                        graph.record(&run);
                        turtle(graph)?
                    }
                    _ => plain(run.render()),
                };
                // A finished run never changes again; one still running will.
                Ok(if finished { repr.cacheable() } else { repr })
            }
            other => Err(unsupported("script-run", other)),
        }
    }

    fn name(&self) -> &str {
        "script-run"
    }

    fn describe(&self) -> Description {
        let id = || {
            ArgSpec::new("id")
                .summary("The run's number, unique per script.")
                .class(XSD_INTEGER)
                .binding()
        };
        Description::new("script-run")
            .title("One run of a script")
            .summary(
                "A recorded run: principal, version, capability, start and end, outcome, the \
                 result (up to 64 KiB) and the host's trace span when it traced. Needs the \
                 script's read or run grant.",
            )
            .verb(Verb::Meta)
            .action(
                ActionSpec::new(Verb::Source)
                    .summary("The run record, as text, JSON, or a PROV-O graph (Turtle).")
                    .input(name_arg())
                    .input(id())
                    .input(record_as_arg())
                    .output(PLAIN)
                    .output(JSON)
                    .output(TURTLE)
                    .requires(CAP_ANY),
            )
            .action(
                ActionSpec::new(Verb::Exists)
                    .summary("Whether the run exists.")
                    .input(name_arg())
                    .input(id())
                    .output(PLAIN)
                    .requires(CAP_ANY),
            )
    }
}

// ---------------------------------------------------------------------------------------
// urn:script:eval — the paste box
// ---------------------------------------------------------------------------------------

struct EvalEndpoint;

#[async_trait]
impl Endpoint for EvalEndpoint {
    async fn invoke(&self, inv: &Invocation<'_>) -> Result<Representation> {
        if inv.request.verb != Verb::Sink {
            return Err(unsupported("script-eval", inv.request.verb));
        }
        let code = inv.inline_str("content")?;
        let language_text = optional(inv, "language")?;
        let language = language_text
            .map(Language::parse)
            .transpose()?
            .unwrap_or(Language::Lisp);
        if language == Language::Sparql {
            return Err(Error::InvalidArgument {
                name: "language".to_string(),
                detail: "SPARQL through urn:script:eval is the SPARQL Protocol face (ledger \
                         #955), not in this version: publish the query as a script and read \
                         its `…:result`"
                    .to_string(),
            });
        }
        if language == Language::Plan {
            // Not a second door onto the same thing: the host's evaluator already runs a
            // supplied plan under the caller's own capability, which is all this door adds.
            return Err(Error::InvalidArgument {
                name: "language".to_string(),
                detail: format!(
                    "a plan supplied ad hoc runs at {} (`in` = the plan), under the caller's \
                     own capability; to keep it, publish it as a script",
                    plan::EVAL
                ),
            });
        }
        if let Some(save) = optional(inv, "save")? {
            let save = save.trim();
            name::validate(save)?;
            // Saved the way any writer saves: a Sink to the script, under the caller's own
            // capability (so it needs `urn:cap:script:write:{save}`), which cuts the script's
            // thread like every other write.
            let mut request = Request::new(Verb::Sink, iri(&name::script_iri(save))?)
                .with_arg("content", inline(code))
                .with_arg("language", inline(language.as_str()))
                .with_arg("state", inline("draft"));
            if let Some(requires) = optional(inv, "requires")? {
                request = request.with_arg("requires", inline(requires));
            }
            inv.issue(request).await?;
        }
        // ★ The caller's OWN capability, unchanged: `issue`, not `issue_attenuated`, and
        // nothing that could add to it exists.
        let answer = inv
            .issue(lisp_request(code, optional(inv, "data")?)?)
            .await?;
        Ok(Representation::new(answer.repr_type, answer.bytes))
    }

    fn name(&self) -> &str {
        "script-eval"
    }

    fn describe(&self) -> Description {
        Description::new("script-eval")
            .title("Run code you supply")
            .summary(
                "Run typed or pasted code as an anonymous script under the caller's OWN \
                 capability and nothing more; answers its value. `save=` keeps it as a draft \
                 of that script (which needs that script's write grant).",
            )
            .verb(Verb::Meta)
            .action(
                ActionSpec::new(Verb::Sink)
                    .summary("Run the code.")
                    .input(
                        ArgSpec::new("content")
                            .summary("The code (piped).")
                            .class(XSD_STRING),
                    )
                    .input(
                        ArgSpec::new("language")
                            .summary(
                                "The code's language: `lisp`, the one this door runs, so the \
                                 one its `requires` (`urn:cap:lisp`) is for. A plan runs at \
                                 urn:plan:eval, which declares no language capability; SPARQL \
                                 here is the Protocol face, not in this version.",
                            )
                            .class(XSD_STRING)
                            // ★ Only what this door runs (ledger #1174): offering `sparql` and
                            // `plan` here advertised values it always refuses, under a floor
                            // (`urn:cap:lisp`) that only Lisp needs.
                            .one_of([Language::Lisp.as_str()])
                            .default_value("lisp")
                            .optional(),
                    )
                    .input(data_arg("data", DATA_SUMMARY))
                    .input(
                        ArgSpec::new("save")
                            .summary(
                                "A script name: also keep the code as a draft at \
                                 `urn:script:{save}`. Needs `urn:cap:script:write:{save}`.",
                            )
                            .class(XSD_STRING)
                            .optional(),
                    )
                    .input(
                        ArgSpec::new("requires")
                            .summary("With `save=`: the draft's declared capability.")
                            .class(XSD_STRING)
                            .optional(),
                    )
                    .output(PLAIN)
                    .requires(CAP_LISP),
            )
    }
}

// ---------------------------------------------------------------------------------------
// urn:script:catalog
// ---------------------------------------------------------------------------------------

/// One row of the catalog.
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
pub struct CatalogEntry {
    /// The script.
    pub name: String,
    /// `urn:script:{name}`.
    pub iri: String,
    /// Its state, when its head could be read.
    pub state: Option<State>,
    /// Whether it is public.
    pub public: Option<bool>,
    /// Its head version.
    pub version: Option<String>,
    /// Its latest run.
    #[serde(rename = "lastRun")]
    pub last_run: Option<LastRun>,
    /// Why its head could not be read, when it could not.
    pub error: Option<String>,
}

/// A catalog row's latest run.
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
pub struct LastRun {
    /// `urn:script:{name}:run:{id}`.
    pub iri: String,
    /// `running`, `ok` or `failed`.
    pub status: String,
    /// When it ended.
    pub ended: Option<u64>,
}

/// The catalog's JSON face.
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
pub struct Catalog {
    /// [`SCHEMA`].
    pub schema: u32,
    /// Every script the caller may read, by name.
    pub scripts: Vec<CatalogEntry>,
}

struct CatalogEndpoint {
    shared: Arc<SpaceConfig>,
}

#[async_trait]
impl Endpoint for CatalogEndpoint {
    async fn invoke(&self, inv: &Invocation<'_>) -> Result<Representation> {
        if inv.request.verb != Verb::Source {
            return Err(unsupported("script-catalog", inv.request.verb));
        }
        let want = face_of(inv, &RECORD_FACES)?;
        let backend = &self.shared.backend;
        let mut scripts = Vec::new();
        // Each row's last run in full, for the graph face (a catalog row keeps less).
        let mut last_runs = Vec::new();
        for name in backend.names()? {
            let own = may(inv, Act::Read, &name);
            let mut entry = CatalogEntry {
                iri: name::script_iri(&name),
                name: name.clone(),
                state: None,
                public: None,
                version: None,
                last_run: None,
                error: None,
            };
            let mut last = None;
            match backend.head(&name) {
                // A broken head is shown to whoever may read the script, so the dashboard
                // says what is wrong instead of losing the row (or the whole list).
                Err(e) if own => entry.error = Some(e.to_string()),
                // Someone else's draft is not listed: to this caller it does not exist.
                Ok(Some(head)) if sight(inv, &head, &head.version) == Sight::Hidden => continue,
                Ok(Some(head))
                    if own
                        || (is_public(&head)
                            && may_public(inv, Act::Read, CAP_READ_PUBLIC, &name)) =>
                {
                    entry.state = Some(head.state);
                    entry.public = Some(head.public);
                    entry.version = Some(head.version);
                    last = backend.last_run(&name).ok().flatten();
                    entry.last_run = last.as_ref().map(|run| LastRun {
                        iri: run.iri(),
                        status: match run.outcome {
                            Outcome::Running => "running",
                            Outcome::Ok => "ok",
                            Outcome::Failed { .. } => "failed",
                        }
                        .to_string(),
                        ended: run.ended,
                    });
                }
                _ => continue,
            }
            last_runs.push(last);
            scripts.push(entry);
        }
        if want == TURTLE {
            let mut graph = crate::graph::Graph::default();
            for (entry, last) in scripts.iter().zip(&last_runs) {
                graph.script(&entry.name, entry.version.as_deref(), last.as_ref());
            }
            return turtle(graph);
        }
        if want == JSON {
            // Live: a run that fails is recorded without a write through any name the
            // catalog could hang from, so this list is never cached.
            return json(&Catalog {
                schema: SCHEMA,
                scripts,
            });
        }
        let mut text = String::new();
        for entry in &scripts {
            match (&entry.error, entry.state) {
                (Some(error), _) => text.push_str(&format!("{}  BROKEN: {error}\n", entry.iri)),
                (None, Some(state)) => {
                    let version = entry.version.as_deref().unwrap_or("");
                    text.push_str(&format!(
                        "{}  {}{}  {}",
                        entry.iri,
                        state.as_str(),
                        if entry.public == Some(true) {
                            " public"
                        } else {
                            ""
                        },
                        &version[..version.len().min(19)],
                    ));
                    if let Some(run) = &entry.last_run {
                        text.push_str(&format!(
                            "  last run {} {} {}",
                            run.iri,
                            run.status,
                            model::when(run.ended)
                        ));
                    }
                    text.push('\n');
                }
                _ => {}
            }
        }
        if scripts.is_empty() {
            text.push_str("no scripts this capability may read\n");
        }
        Ok(plain(text))
    }

    fn name(&self) -> &str {
        "script-catalog"
    }

    fn describe(&self) -> Description {
        Description::new("script-catalog")
            .title("Scripts")
            .summary(
                "Every script the caller may read: state, public flag, head version and last \
                 run. Live (never cached). A host that wants `urn:host:scripts` aliases it \
                 here.",
            )
            .verb(Verb::Meta)
            .action(
                ActionSpec::new(Verb::Source)
                    .summary("The catalog, as text, JSON, or a graph (Turtle).")
                    .input(record_as_arg())
                    .output(PLAIN)
                    .output(JSON)
                    .output(TURTLE)
                    .requires(CAP_READ),
            )
    }
}

fn unsupported(id: &str, verb: Verb) -> Error {
    Error::Endpoint(format!("{id} does not answer {verb:?}"))
}