mahbot 0.11.2

An autonomous agentic engineering system that manages software development through role separation, subagents, and deterministic diagnostics.
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
//! PhotoCraft (`photocraft-cli`, published as ArtCraft): the vendor's stateless headless image
//! editor, the product's managed copy of it, and the thin layer that drives it. One document is
//! opened, a chain of the vendor's own commands is applied in order, and the result is saved.
//!
//! Nothing is pinned — the copy in use is compared with the vendor's newest release at start-up and
//! installed on first use — so vendor fixes reach users without a product release. The vendor is
//! alpha and guarantees no compatibility for command ids, so a command it no longer knows surfaces
//! as a clear failure here, never as a silent no-op.
//!
//! The vendor's stream shape is what the answers are read from: stdout carries the result and stderr
//! follows it, so a document or a batch's summary comes first and the engine's warnings after it.
//!
//! Every invocation runs through [`run`]; [`drive`] is the one that answers with the vendor's own
//! output and turns a non-zero exit into the failure, while [`batch`] reads the outcome itself
//! because a report naming failed files is still its answer. The vendor runs under the product's
//! existing containment ([`crate::tools::shell::run_internal_program_outcome`]) and memory watchdog,
//! from the install root as its working directory — never the caller's — with no wall-clock ceiling
//! and no cap on canvas size, input size or run time. Every invocation that opens a document carries
//! the vendor's `--approve-high-memory`: its own soft gate on a large PSD/PSB is one cap the product
//! does not add, and the watchdog is the real bound. Paths are not confined: a caller's path is
//! passed on as it stands, and no invocation carries the vendor's in-place mode, so an existing file
//! is never modified in place.
//!
//! Every answer is capped at [`MAX_ANSWER_BYTES`] before the product's own tool-output cut, so what a
//! model receives is the product's own shortened answer rather than a raw omission marker; each
//! builder says what its own answer keeps and what it drops.

use std::path::{Path, PathBuf};

use crate::tools::shell::{ProgramOutcome, Stop, run_internal_program_outcome};

mod install;

/// The most of one vendor answer the product keeps — a registry listing, a batch's report or an
/// inspection: the product's own tool-output budget
/// ([`crate::util::TOOL_OUTPUT_BUDGET_BYTES`]), which is what a model receives. Every builder here
/// keeps its answer inside it, note included, so the product's own cut never re-cuts one.
pub(crate) const MAX_ANSWER_BYTES: usize = crate::util::TOOL_OUTPUT_BUDGET_BYTES;

/// The most a note saying what an answer left out may take: [`capped_lines`], [`capped_rows`] and
/// [`capped_batch_answer`] keep it back out of their budget. [`capped_document`] needs no reserve —
/// it measures the note with the rest of the text it emits.
const NOTE_ROOM: usize = 96;

/// The most of a failed run's output one answer quotes, so a batch's worth of lines cannot flood a
/// model's context. Cut as a head and a tail: a fatal error is the last thing the vendor says.
const MAX_FAILURE_BYTES: usize = 2_000;

/// One step of a chain: the vendor's own command id and its parameters.
pub(crate) struct Step {
    pub(crate) command: String,
    pub(crate) params: Option<serde_json::Value>,
}

/// A batch's answer and whether the vendor failed on any file. The report is the answer either way —
/// the files it wrote are the caller's work — and the flag is what a command-line caller exits
/// non-zero on.
#[derive(Debug)]
pub(crate) struct BatchReport {
    pub(crate) answer: String,
    pub(crate) partial: bool,
}

pub use install::install_on_start;

/// Open `input`, apply `chain` in order, save to `output`. An empty chain is the vendor's
/// `convert`, which is the same open-and-save with no commands. With no input at all, `canvas` is
/// the vendor's `--new` parameters and the document is created from nothing.
///
/// The vendor answers with one compact JSON line per step; the tool that delivers the result cuts
/// them to whole steps, with the room its own marker needs.
pub(crate) async fn run_chain(
    input: Option<&Path>,
    canvas: Option<&serde_json::Value>,
    chain: &[Step],
    output: &Path,
) -> anyhow::Result<String> {
    drive(&chain_args(input, canvas, chain, output)).await
}

/// The document's size, mode, depth and layers, as the vendor reports them, with the container
/// format its own name gives added as one field: the vendor's `info` carries no format field, so the
/// extension is what the product can know, and the answer says where it came from.
pub(crate) async fn inspect(input: &Path) -> anyhow::Result<String> {
    let answer = drive(&inspect_args(input)).await?;
    Ok(inspection_answer(&answer, input))
}

/// The container format a file's own name gives — the one fact the engine's `info` does not report.
fn format_field(input: &Path) -> String {
    match crate::util::extension_of(input) {
        Some(format) => format!("{format} (from the file name)"),
        None => "unknown (the file name has no extension)".to_string(),
    }
}

/// The inspection answer: the engine's document with [`format_field`] added as its `format` field, so
/// the answer is one JSON document, with whatever the engine wrote beside it still after it. An
/// answer carrying no JSON object keeps its text and gains the format after it.
///
/// A document longer than one answer keeps its own fields — the size, mode and depth the caller asked
/// for — and its arrays, its layer tree among them, keep the whole entries that fit with the count of
/// those left out: a layer is a record, so it is never half kept. A report the product's own capture
/// cut in the middle holds no whole document to recover, so the answer says so and keeps the format.
fn inspection_answer(answer: &str, input: &Path) -> String {
    let format = format_field(input);
    let (value, beside) = first_json(answer);
    let Some(serde_json::Value::Object(mut fields)) = value else {
        // The product's own capture cut the vendor's report in the middle, so no whole document can
        // be recovered: the answer says so and keeps the one fact the file name gives.
        if answer.contains(crate::util::TRUNCATED_MIDDLE) {
            return format!("… the inspection was too large to capture whole; format: {format}");
        }
        // No document to keep whole: the engine's own text is cut on whole lines, and the format the
        // product adds is kept whatever the text loses.
        let line = format!("format: {format}");
        let text = capped_lines(
            answer.trim_end(),
            MAX_ANSWER_BYTES.saturating_sub(line.len() + 1),
            "line",
            "",
        );
        return if text.is_empty() {
            line
        } else {
            format!("{text}\n{line}")
        };
    };
    fields.insert("format".to_string(), serde_json::Value::String(format));
    let beside = capped_lines(beside, MAX_ANSWER_BYTES / 4, "line", "");
    let room = MAX_ANSWER_BYTES.saturating_sub(beside.len() + 1);
    let document = capped_document(fields, room);
    if beside.is_empty() {
        document
    } else {
        format!("{document}\n{beside}")
    }
}

/// One inspection document cut to `room` bytes. A document that fits is answered exactly as the
/// engine wrote it; past that, its own fields are kept whole — they are what the caller asked for —
/// and an array keeps the whole entries that fit, with the count of those left out as its last
/// element, so a layer is never half kept. Every choice is made by measuring the text that will be
/// emitted, so no margin is predicted and none can drift from what is written.
///
/// The cheapest array is filled first and each one still to come keeps its floor — the count note
/// saying how much it holds — so a layer tree takes what the small ones (notes, snapshots, warnings)
/// leave instead of starving them. A document whose own fields alone are past one answer, which the
/// vendor's inspection does not produce, keeps the format the product adds and one note counting the
/// rest.
fn capped_document(fields: serde_json::Map<String, serde_json::Value>, room: usize) -> String {
    let total = fields.len();
    // The whole document first: the per-array measurement below keeps every later array at its floor,
    // which is a note larger than a short array's own content, so a document that fits is answered
    // exactly as the engine wrote it.
    if let Ok(text) = serde_json::to_string(&fields)
        && text.len() <= room
    {
        return text;
    }
    let mut facts = serde_json::Map::new();
    let mut arrays: Vec<(String, Vec<serde_json::Value>)> = Vec::new();
    for (name, value) in fields {
        match value {
            serde_json::Value::Array(entries) => arrays.push((name, entries)),
            fact => {
                facts.insert(name, fact);
            }
        }
    }
    if !fits(&facts, room) {
        return crowded_document(&facts, total);
    }
    arrays.sort_by_key(|(_, entries)| json_len(entries));
    for (index, (name, entries)) in arrays.iter().enumerate() {
        let kept = kept_entries(&facts, name, entries, &arrays[index + 1..], room);
        facts.insert(name.clone(), serde_json::Value::Array(kept));
    }
    match serde_json::to_string(&facts) {
        Ok(text) if text.len() <= room => text,
        // The arrays' floors alone do not fit beside the fields: the format and one counted note are
        // what is left to say.
        _ => crowded_document(&facts, total),
    }
}

/// The whole entries of `entries` that fit beside `facts` in `room` bytes, with the count of those
/// left out as the last element. `later` are the arrays still to be filled: each keeps its floor in
/// every measurement, so this array cannot take the room they need to say how much they hold.
fn kept_entries(
    facts: &serde_json::Map<String, serde_json::Value>,
    name: &str,
    entries: &[serde_json::Value],
    later: &[(String, Vec<serde_json::Value>)],
    room: usize,
) -> Vec<serde_json::Value> {
    // The answer only grows as more entries are kept — an entry costs at least three bytes with its
    // separator, while the count note loses at most one — so halving reaches the count that fits in a
    // handful of measurements rather than one per entry.
    let (mut fits_count, mut too_many) = (0, entries.len() + 1);
    while too_many - fits_count > 1 {
        let middle = fits_count + (too_many - fits_count) / 2;
        if measured(facts, name, entries, middle, later, room) {
            fits_count = middle;
        } else {
            too_many = middle;
        }
    }
    kept(entries, fits_count)
}

/// Whether the answer holds `count` of `entries` under `name`, with each array in `later` at its
/// floor, in `room` bytes — measured on the text that will be emitted.
fn measured(
    facts: &serde_json::Map<String, serde_json::Value>,
    name: &str,
    entries: &[serde_json::Value],
    count: usize,
    later: &[(String, Vec<serde_json::Value>)],
    room: usize,
) -> bool {
    let mut candidate = facts.clone();
    candidate.insert(
        name.to_string(),
        serde_json::Value::Array(kept(entries, count)),
    );
    for (name, entries) in later {
        candidate.insert(name.clone(), serde_json::Value::Array(kept(entries, 0)));
    }
    fits(&candidate, room)
}

/// The first `count` of `entries`, with the count of those left out as the last element when any are.
fn kept(entries: &[serde_json::Value], count: usize) -> Vec<serde_json::Value> {
    let mut shown = entries[..count].to_vec();
    if count < entries.len() {
        shown.push(serde_json::Value::String(count_note(
            entries.len() - count,
            entries.len(),
        )));
    }
    shown
}

/// Whether the document takes no more than `room` bytes as one JSON object.
fn fits(document: &serde_json::Map<String, serde_json::Value>, room: usize) -> bool {
    serde_json::to_string(document).is_ok_and(|text| text.len() <= room)
}

/// How long an array is as JSON. Only the order the arrays are filled in depends on it — the cheapest
/// first, so a layer tree cannot starve the small ones — never a budget decision.
fn json_len(entries: &[serde_json::Value]) -> usize {
    serde_json::to_string(entries).map_or(0, |json| json.len())
}

/// What a cut array says after the entries it keeps: how many of them the answer does not show.
fn count_note(left_out: usize, total: usize) -> String {
    format!("… {left_out} of {total} further entries not shown")
}

/// The whole answer for a document that cannot be cut down to fit: the format the product adds and one
/// note counting the members the answer does not show, so a shortened answer is still one JSON
/// document rather than half a value. It answers a document whose own fields alone are past one
/// answer — a shape the vendor's inspection does not produce — and one whose arrays cannot even say
/// how much they hold.
fn crowded_document(facts: &serde_json::Map<String, serde_json::Value>, total: usize) -> String {
    let mut answer = serde_json::Map::new();
    let mut omitted = total;
    if let Some(format) = facts.get("format") {
        answer.insert("format".to_string(), format.clone());
        omitted -= 1;
    }
    answer.insert(
        "omitted".to_string(),
        serde_json::Value::String(format!("… {omitted} further member(s) not shown")),
    );
    serde_json::Value::Object(answer).to_string()
}

/// The first JSON value in an engine answer, and what the engine wrote after it: stderr is
/// concatenated after stdout, so the value comes first and a warning the engine printed follows it,
/// never the other way round.
fn first_json(text: &str) -> (Option<serde_json::Value>, &str) {
    let text = text.trim_start();
    let mut values = serde_json::Deserializer::from_str(text).into_iter::<serde_json::Value>();
    match values.next() {
        Some(Ok(value)) => (Some(value), text[values.byte_offset()..].trim()),
        _ => (None, text.trim()),
    }
}

/// The vendor's whole command registry, optionally narrowed by `filter`. A narrowed answer carries
/// each match's parameter documentation, so a caller gets from an id to a callable step in one
/// question; the unfiltered registry is an index of ids and labels.
pub(crate) async fn listing(filter: Option<&str>) -> anyhow::Result<String> {
    let output = drive(&listing_args(filter)).await?;
    if is_empty_listing(&output) {
        return Ok(no_commands(filter));
    }
    Ok(registry_answer(filter, &output))
}

/// The answer a registry response turns into: under the cap it is the vendor's own text, untouched
/// but for the blank lines it may open with; past it, a narrowed answer drops whole commands and the
/// unfiltered one is cut on a line boundary.
fn registry_answer(filter: Option<&str>, output: &str) -> String {
    let output = output.trim_start();
    // The product's own capture cut the vendor's answer in the middle — a filter matching most of the
    // registry is that large — so no whole command can be recovered from it: the answer says so
    // rather than handing back a JSON document cut in half.
    if output.contains(crate::util::TRUNCATED_MIDDLE) {
        return format!("… the registry answer was too large to capture whole; {NARROWER}");
    }
    if output.len() <= MAX_ANSWER_BYTES {
        return output.to_string();
    }
    match filter {
        Some(_) => capped_rows(output),
        None => capped_listing(output),
    }
}

/// Whether the vendor answered with nothing at all, or with the empty JSON array a filter that
/// matches no command produces.
fn is_empty_listing(output: &str) -> bool {
    let trimmed = output.trim();
    trimmed.is_empty()
        || (trimmed.len() >= 2
            && trimmed.starts_with('[')
            && trimmed.ends_with(']')
            && trimmed[1..trimmed.len() - 1].trim().is_empty())
}

/// What an empty registry answer says instead of nothing: a filter that matches no command is the
/// ordinary way to get one, and a blank tool result tells a model nothing.
fn no_commands(filter: Option<&str>) -> String {
    match filter {
        Some(filter) => format!("no command matches {filter:?}"),
        None => "the vendor's command registry is empty".to_string(),
    }
}

/// Apply `chain` to every file in the `input` folder, writing into the `output` folder.
///
/// The vendor reports a batch line by line: `ok    <input> -> <output>` on stdout per produced file
/// and `FAIL  <input>: <reason>` on stderr per file it could not do, with `<n> succeeded, <m> failed`
/// as the last line of its stdout when it ran the folder to the end. That summary is what tells a run
/// that ran from one that stopped, so it decides between an answer and a failure here.
pub(crate) async fn batch(
    chain: &[Step],
    input: &Path,
    output: &Path,
    format: Option<&str>,
    quality: Option<u32>,
    tiff_layers: bool,
) -> anyhow::Result<BatchReport> {
    let (_recipe_dir, recipe) = recipe_file(chain)?;
    let argv = batch_args(&recipe, input, output, format, quality, tiff_layers);
    let outcome = run(&argv).await?;
    let written = files_written(output).await;
    batch_outcome(&outcome, &written, output).map_err(anyhow::Error::msg)
}

/// A batch's run turned into its answer. The vendor prints its closing summary only when it ran the
/// folder to the end, so a summary present means the run got there and a non-zero exit only repeats
/// what the summary says. A run that succeeded without one is still an answer — nothing was left out,
/// so the report is whatever the vendor wrote. Without either, the run never got that far and its
/// failure is the answer: the tool then reports how many files it did write before stopping. A run
/// that failed on any file answers with the shortfall flag set, so a command-line caller exits
/// non-zero on the report it still receives.
fn batch_outcome(
    outcome: &ProgramOutcome,
    written: &FilesWritten,
    output_dir: &Path,
) -> Result<BatchReport, String> {
    if outcome.success || outcome.output.lines().any(is_summary) {
        Ok(BatchReport {
            answer: batch_answer(&outcome.output, written, output_dir),
            partial: failed_some(&outcome.output),
        })
    } else {
        Err(failure("batch", outcome))
    }
}

/// A batch's answer. It opens with the product's own sentence about what the batch wrote — the one
/// fact a caller must never lose — and then the vendor's report: whole per-file lines from the start
/// and from the end, the engine's warnings collapsed to one line per kind, the lines naming files it
/// failed on with their reasons, and its closing summary. The count and the summary are kept
/// whatever else goes; the failures are what a caller acts on, so they take their room first, then
/// the per-file lines, and the collapsed warnings get what is left. Whatever is dropped goes whole
/// and is counted, which the product's own tool-output cut could never do, since it takes a plain
/// middle out with no count.
fn batch_answer(output: &str, written: &FilesWritten, output_dir: &Path) -> String {
    let header = written.written_into(output_dir);
    let text = output.trim();
    if text.is_empty() {
        return header;
    }
    let lines: Vec<&str> = text.lines().collect();
    let summary: Vec<&str> = lines
        .iter()
        .copied()
        .filter(|line| is_summary(line))
        .collect();
    let failures: Vec<&str> = lines
        .iter()
        .copied()
        .filter(|line| is_failure(line))
        .collect();
    let warnings = collapsed_warnings(lines.iter().copied().filter(|line| is_warning(line)));
    let results: Vec<&str> = lines
        .iter()
        .copied()
        .filter(|line| !is_summary(line) && !is_failure(line) && !is_warning(line))
        .collect();

    // The ordinary answer, whole: every result line, one line per kind of warning, the failures and
    // the vendor's summary last.
    let mut answer = header.clone();
    for line in &results {
        crate::util::push_line(&mut answer, line);
    }
    for line in &warnings {
        crate::util::push_line(&mut answer, line);
    }
    for line in &failures {
        crate::util::push_line(&mut answer, line);
    }
    for line in &summary {
        crate::util::push_line(&mut answer, line);
    }
    if answer.len() <= MAX_ANSWER_BYTES {
        return answer;
    }
    // Too large for the cap: a note is room the lines give up, so the first layout reserves none and
    // a later one reserves what the first turned out to need.
    let mut notes = 0;
    loop {
        let (answer, used) =
            capped_batch_answer(&header, &results, &warnings, &failures, &summary, notes);
        if used <= notes {
            return answer;
        }
        notes = used;
    }
}

/// The answer for a batch too large to fit: whole result lines from both ends, the collapsed
/// warnings, the failures the vendor named and its summary, with room for `notes` notes kept out of
/// the lines' budget. Returns the answer and how many notes it carries.
fn capped_batch_answer(
    header: &str,
    results: &[&str],
    warnings: &[String],
    failures: &[&str],
    summary: &[&str],
    notes: usize,
) -> (String, usize) {
    let mut used = 0;
    let mut left = MAX_ANSWER_BYTES
        .saturating_sub(header.len() + 1 + lines_bytes(summary) + notes * NOTE_ROOM);
    // A file the vendor failed on is what a caller acts on, so the failures take their room first.
    let kept_failures = lines_within(failures.iter().copied(), left);
    if kept_failures < failures.len() {
        used += 1;
    }
    left = left.saturating_sub(lines_bytes(&failures[..kept_failures]));
    // The per-file report next: whole lines from the start and from the end, before any warning.
    let side = left / 2;
    let head = lines_within(results.iter().copied(), side);
    let tail = lines_within(results[head..].iter().rev().copied(), side);
    let omitted = results.len() - head - tail;
    if omitted > 0 {
        used += 1;
    }
    left = left.saturating_sub(
        lines_bytes(&results[..head]) + lines_bytes(&results[results.len() - tail..]),
    );
    // The collapsed warnings take only what is left.
    let kept_warnings = lines_within(warnings.iter().map(String::as_str), left);
    if kept_warnings < warnings.len() {
        used += 1;
    }

    let mut answer = header.to_string();
    for line in &results[..head] {
        crate::util::push_line(&mut answer, line);
    }
    if omitted > 0 {
        crate::util::push_line(
            &mut answer,
            &format!("… {omitted} further result lines not shown"),
        );
    }
    for line in &results[results.len() - tail..] {
        crate::util::push_line(&mut answer, line);
    }
    for line in &warnings[..kept_warnings] {
        crate::util::push_line(&mut answer, line);
    }
    if kept_warnings < warnings.len() {
        crate::util::push_line(
            &mut answer,
            &format!(
                "… {} further warning kinds not shown",
                warnings.len() - kept_warnings
            ),
        );
    }
    if kept_failures < failures.len() {
        crate::util::push_line(
            &mut answer,
            &format!(
                "… {} further failures not shown",
                failures.len() - kept_failures
            ),
        );
    }
    for line in &failures[..kept_failures] {
        crate::util::push_line(&mut answer, line);
    }
    for line in summary {
        crate::util::push_line(&mut answer, line);
    }
    (answer, used)
}

/// The vendor's per-file warnings, one line per kind: its own wording with the numbers that differ
/// between files shown as `N`, and how many warning lines of that kind it printed — one per file, so
/// the count reads as the number of files affected.
fn collapsed_warnings<'a>(warnings: impl Iterator<Item = &'a str>) -> Vec<String> {
    let mut kinds: Vec<WarningKind> = Vec::new();
    for line in warnings {
        let parts = warning_parts(line.trim_end().strip_prefix(WARNING_PREFIX).unwrap_or(line));
        match kinds.iter_mut().find(|kind| kind.same_as(&parts)) {
            Some(kind) => kind.merge(&parts),
            None => kinds.push(WarningKind::new(parts)),
        }
    }
    kinds
        .into_iter()
        .map(|kind| {
            let lines = if kind.count == 1 { "line" } else { "lines" };
            format!("warning ({} {lines}): {}", kind.count, kind.wording())
        })
        .collect()
}

/// One kind of warning: its wording, each number kept until another file shows a different one, and
/// the number of warning lines collapsed into it.
struct WarningKind {
    parts: Vec<WarningPart>,
    count: usize,
}

/// A warning's message split at its numbers: the text is the warning's own wording, the numbers are
/// what the vendor varies between files.
enum WarningPart {
    Text(String),
    Number { value: String, varies: bool },
}

impl WarningKind {
    fn new(parts: Vec<WarningPart>) -> Self {
        Self { parts, count: 1 }
    }

    /// Whether `parts` is this kind's wording: the same text in the same order, whatever its numbers.
    fn same_as(&self, parts: &[WarningPart]) -> bool {
        self.parts.len() == parts.len()
            && self
                .parts
                .iter()
                .zip(parts)
                .all(|(known, part)| match (known, part) {
                    (WarningPart::Text(known), WarningPart::Text(text)) => known == text,
                    (WarningPart::Number { .. }, WarningPart::Number { .. }) => true,
                    _ => false,
                })
    }

    /// Fold another line of this kind in, noting a number that differs from the first one seen.
    fn merge(&mut self, parts: &[WarningPart]) {
        self.count += 1;
        for (known, part) in self.parts.iter_mut().zip(parts) {
            if let (WarningPart::Number { value, varies }, WarningPart::Number { value: seen, .. }) =
                (known, part)
                && !*varies
                && value != seen
            {
                *varies = true;
            }
        }
    }

    /// The kind's wording: the numbers every file agreed on, and `N` where they differed.
    fn wording(&self) -> String {
        let mut wording = String::new();
        for part in &self.parts {
            match part {
                WarningPart::Text(text) => wording.push_str(text),
                WarningPart::Number {
                    value,
                    varies: false,
                } => wording.push_str(value),
                WarningPart::Number { varies: true, .. } => wording.push('N'),
            }
        }
        wording
    }
}

/// A warning's message split where its numbers are, so one kind can hold the wording and compare the
/// numbers that vary between files.
fn warning_parts(message: &str) -> Vec<WarningPart> {
    let mut parts = Vec::new();
    let mut text = String::new();
    let mut number = String::new();
    for character in message.chars() {
        if character.is_ascii_digit() {
            if !text.is_empty() {
                parts.push(WarningPart::Text(std::mem::take(&mut text)));
            }
            number.push(character);
        } else {
            if !number.is_empty() {
                parts.push(WarningPart::Number {
                    value: std::mem::take(&mut number),
                    varies: false,
                });
            }
            text.push(character);
        }
    }
    if !number.is_empty() {
        parts.push(WarningPart::Number {
            value: number,
            varies: false,
        });
    }
    if !text.is_empty() {
        parts.push(WarningPart::Text(text));
    }
    parts
}

/// The vendor's closing line for a batch it ran to the end — `<n> succeeded, <m> failed`, and any
/// further clause it adds after that (`--stop-on-error` appends `, <k> skipped`) — as its two counts.
/// Read word by word, so punctuation and a trailing clause cannot hide it, while a per-file line or
/// the exit status's own `error:` line cannot be mistaken for it.
fn summary_counts(line: &str) -> Option<(u64, u64)> {
    let words: Vec<&str> = line
        .split(|c: char| !c.is_ascii_alphanumeric())
        .filter(|word| !word.is_empty())
        .collect();
    match words.as_slice() {
        [succeeded, "succeeded", failed, "failed", ..] => {
            Some((succeeded.parse().ok()?, failed.parse().ok()?))
        }
        _ => None,
    }
}

/// Whether a line is the vendor's closing summary: what tells a run that ran from one that stopped.
fn is_summary(line: &str) -> bool {
    summary_counts(line).is_some()
}

/// Whether the vendor failed on any file: a `FAIL` line names one, and the closing summary counts
/// them. Either is enough — a report that says a file failed never reads as a whole success.
fn failed_some(output: &str) -> bool {
    output
        .lines()
        .any(|line| is_failure(line) || summary_counts(line).is_some_and(|(_, failed)| failed > 0))
}

/// A file the vendor could not do, as it names one on stderr: `FAIL  <input>: <reason>`.
fn is_failure(line: &str) -> bool {
    line.starts_with("FAIL ")
}

/// The prefix every warning line the vendor prints carries. Its closing `error:` line is not one.
const WARNING_PREFIX: &str = "warning: ";

/// Whether a line is one of the vendor's per-file warnings, which it prints on stderr once per file
/// and per thing it had to change.
fn is_warning(line: &str) -> bool {
    line.starts_with(WARNING_PREFIX)
}

/// How many bytes `lines` take with their line breaks.
fn lines_bytes(lines: &[&str]) -> usize {
    lines.iter().map(|line| line.len() + 1).sum()
}

/// Count the files in a folder. A batch writes into a folder that is new or empty, so this is what
/// it produced — the one fact its answer opens with — and the tool counts a batch that stopped
/// partway the same way.
pub(crate) async fn files_written(dir: &Path) -> FilesWritten {
    let Ok(mut entries) = tokio::fs::read_dir(dir).await else {
        return FilesWritten {
            count: 0,
            whole: false,
        };
    };
    let mut written = FilesWritten {
        count: 0,
        whole: true,
    };
    loop {
        match entries.next_entry().await {
            // A folder or a link is not a produced file; one whose type cannot be read is a file
            // this count cannot vouch for.
            Ok(Some(entry)) => match entry.file_type().await {
                Ok(kind) if kind.is_file() => written.count += 1,
                Ok(_) => {}
                Err(_) => written.whole = false,
            },
            Ok(None) => return written,
            Err(_) => {
                written.whole = false;
                return written;
            }
        }
    }
}

/// A folder's file count, and whether it is the count or the floor a folder that could not be read
/// in full leaves: [`Self::written_into`] states both in one sentence, so a short read is never
/// passed off as the count.
pub(crate) struct FilesWritten {
    pub(crate) count: usize,
    pub(crate) whole: bool,
}

impl FilesWritten {
    /// What an answer says a batch wrote into `dir`: the count, the floor it could reach when the
    /// folder could not be read in full, or the folder's own unreadability when not one file could be
    /// counted. One sentence, so no caller has to splice a clause into another.
    pub(crate) fn written_into(&self, dir: &Path) -> String {
        let dir = dir.display();
        match (self.whole, self.count) {
            (true, 0) => format!("no file was written into {dir}"),
            (true, count) => format!("{count} file(s) written to {dir}"),
            (false, 0) => format!("the files in {dir} could not be counted"),
            (false, count) => format!(
                "at least {count} file(s) written to {dir} (the folder could not be read in full)"
            ),
        }
    }
}

/// How many whole lines of `lines` fit in `budget` bytes, counting from the start.
fn lines_within<'a>(lines: impl Iterator<Item = &'a str>, budget: usize) -> usize {
    let mut used = 0;
    let mut count = 0;
    for line in lines {
        used += line.len() + 1;
        if used > budget {
            break;
        }
        count += 1;
    }
    count
}

/// Run the vendor with `argv` and answer the whole outcome. Only a run the product itself ended is a
/// failure here: the program could not be started, or the memory watchdog stopped it. Whether the
/// program's own exit status is an error belongs to the entry point that asked for it.
async fn run(argv: &[String]) -> anyhow::Result<ProgramOutcome> {
    let what = argv.first().map_or("", String::as_str);
    let (_held, cwd, program) = install::hold_for_run().await.map_err(anyhow::Error::msg)?;
    let outcome = run_internal_program_outcome(&cwd, &program, argv, &[], None).await;
    if outcome.stopped() {
        return Err(anyhow::Error::msg(failure(what, &outcome)));
    }
    Ok(outcome)
}

/// Run the vendor with `argv` and answer its output. Every entry point that must succeed goes through
/// here, so the containment, the working directory and the shape of a failure are one decision. The
/// first element is the vendor's own subcommand, which is also what a failure names — every builder
/// returns the whole argv, so no call can lose it.
async fn drive(argv: &[String]) -> anyhow::Result<String> {
    let what = argv.first().map_or("", String::as_str);
    let outcome = run(argv).await?;
    if !outcome.success {
        return Err(anyhow::Error::msg(failure(what, &outcome)));
    }
    Ok(outcome.output)
}

/// Whether a format names TIFF: the one output format whose layers the vendor drops unless asked.
fn is_tiff(format: &str) -> bool {
    format.eq_ignore_ascii_case("tif") || format.eq_ignore_ascii_case("tiff")
}

/// The vendor's own soft gate on a large PSD/PSB import. Passing it is what keeps the product's
/// memory watchdog the only cap on how big an input may be; the vendor still holds its hard budget.
const APPROVE_HIGH_MEMORY: &str = "--approve-high-memory";

/// The argv of one chain over one document, or over a canvas created from nothing: the vendor's
/// `run`, the document — or `--new` and the canvas — one `--cmd` per step with its `--params` when it
/// has any, then the destination. With no steps and a document it is the vendor's `convert`, the same
/// open-and-save with nothing applied; a canvas has no file to open, so it always goes through `run`.
/// Every path is absolute: the vendor resolves a relative one against its working directory, which is
/// the install root.
///
/// A TIFF destination carries `--tiff-layers` on both paths: the vendor flattens a TIFF without it, so
/// leaving it out would silently drop the layers a `.tiff` destination is chosen to keep.
fn chain_args(
    input: Option<&Path>,
    canvas: Option<&serde_json::Value>,
    chain: &[Step],
    output: &Path,
) -> Vec<String> {
    let tiff = crate::util::extension_of(output).is_some_and(|extension| is_tiff(&extension));
    let output_arg = path_arg(output);
    if let (Some(input), None) = (input, canvas)
        && chain.is_empty()
    {
        let mut args = vec!["convert".to_string(), path_arg(input), output_arg];
        args.push(APPROVE_HIGH_MEMORY.to_string());
        if tiff {
            args.push("--tiff-layers".to_string());
        }
        return args;
    }
    let mut args = match (input, canvas) {
        (Some(input), None) => vec!["run".to_string(), path_arg(input)],
        (_, Some(canvas)) => vec!["run".to_string(), "--new".to_string(), canvas.to_string()],
        // The tool refuses a `run` with neither; the vendor's own usage error would answer it.
        (None, None) => vec!["run".to_string()],
    };
    for step in chain {
        args.push("--cmd".to_string());
        args.push(step.command.clone());
        if let Some(params) = &step.params {
            args.push("--params".to_string());
            args.push(params.to_string());
        }
    }
    args.push("--out".to_string());
    args.push(output_arg);
    args.push(APPROVE_HIGH_MEMORY.to_string());
    if tiff {
        args.push("--tiff-layers".to_string());
    }
    args
}

/// The argv of one `info`. `--compact` prints the document as one line instead of a pretty tree: a
/// layered document's tree runs past what one answer can hold, and the answer parses either way.
fn inspect_args(input: &Path) -> Vec<String> {
    vec![
        "info".to_string(),
        path_arg(input),
        "--compact".to_string(),
        APPROVE_HIGH_MEMORY.to_string(),
    ]
}

/// The argv of one `commands`: narrowed by the vendor's own `--filter` when the caller has one, and
/// asking for JSON only then, because that is what adds each match's parameter documentation — the
/// unfiltered registry's JSON is far past what one answer can hold.
fn listing_args(filter: Option<&str>) -> Vec<String> {
    let mut args = vec!["commands".to_string()];
    if let Some(filter) = filter {
        args.push("--filter".to_string());
        args.push(filter.to_string());
        args.push("--json".to_string());
    }
    args
}

/// The argv of one `batch`: the recipe, the two folders, the optional flags that were given, and
/// `--tiff-layers` whenever the output is TIFF — whether the caller set the flag or named the
/// format — because the vendor flattens a TIFF without it. The vendor's `--in-place` is never among
/// them.
fn batch_args(
    recipe: &Path,
    input: &Path,
    output: &Path,
    format: Option<&str>,
    quality: Option<u32>,
    tiff_layers: bool,
) -> Vec<String> {
    let mut args = vec![
        "batch".to_string(),
        "--actions".to_string(),
        path_arg(recipe),
        "--in".to_string(),
        path_arg(input),
        "--out".to_string(),
        path_arg(output),
    ];
    if let Some(format) = format {
        args.push("--format".to_string());
        args.push(format.to_string());
    }
    if let Some(quality) = quality {
        args.push("--quality".to_string());
        args.push(quality.to_string());
    }
    // A batch into TIFF keeps its layers whether the caller set the flag or named the format: the
    // vendor flattens a TIFF without it, and flattening is never what writing TIFF asks for.
    if tiff_layers || format.is_some_and(is_tiff) {
        args.push("--tiff-layers".to_string());
    }
    args.push(APPROVE_HIGH_MEMORY.to_string());
    args
}

/// A path as the vendor's argument parser takes it.
fn path_arg(path: &Path) -> String {
    path.to_string_lossy().into_owned()
}

/// The recipe `batch` reads: the chain as the vendor's step list, one object per step, with
/// `params` present only where the step has any. The file sits in the product's pinned temp root
/// and its directory is removed when the call ends, whether it succeeded or not.
fn recipe_file(chain: &[Step]) -> anyhow::Result<(tempfile::TempDir, PathBuf)> {
    use anyhow::Context as _;

    let dir = tempfile::Builder::new()
        .prefix("photocraft-")
        .tempdir_in(crate::temp::shell_tmpdir())
        .context("the batch recipe's temporary directory could not be created")?;
    let path = dir.path().join("recipe.json");
    std::fs::write(&path, recipe_json(chain)).context("the batch recipe could not be written")?;
    Ok((dir, path))
}

/// The recipe's JSON: an array of `{"command": "<id>", "params": {...}}`, `params` present only
/// where the step has any.
fn recipe_json(chain: &[Step]) -> String {
    let steps: Vec<serde_json::Value> = chain
        .iter()
        .map(|step| match &step.params {
            Some(params) => serde_json::json!({"command": step.command, "params": params}),
            None => serde_json::json!({"command": step.command}),
        })
        .collect();
    serde_json::Value::Array(steps).to_string()
}

/// What a capped listing says about reaching the rest.
const NARROWER: &str = "a narrower filter shows the rest";

/// A narrowed registry answer past [`MAX_ANSWER_BYTES`]: the vendor's JSON, one object per matching
/// command, cut at whole commands with a line saying how many were left out — never a document cut
/// in the middle, where a model cannot tell a truncated parameter from a complete one, and never a
/// single command cut in half. The JSON is found inside the answer, because a warning the engine
/// wrote beside it is not part of it. An answer carrying no JSON array is cut on line boundaries.
fn capped_rows(text: &str) -> String {
    let Some(rows) = registry_rows(text) else {
        return capped_listing(text);
    };
    // The note saying how many commands were left out is reserved up front, so the array and the
    // note together never run past one answer.
    let room = MAX_ANSWER_BYTES.saturating_sub(NOTE_ROOM);
    let total = rows.len();
    let mut kept: Vec<String> = Vec::new();
    let mut used = 2; // the enclosing brackets
    for row in &rows {
        let row = row.to_string();
        let separator = if kept.is_empty() { 0 } else { 2 }; // ",\n" between commands
        if used + separator + row.len() > room {
            break;
        }
        used += separator + row.len();
        kept.push(row);
    }
    if kept.is_empty() && !rows.is_empty() {
        // Not one command fits whole: the answer names the count rather than half a command, so a
        // model is never handed a truncated parameter it could mistake for a complete one.
        return format!(
            "[]\n… all {total} matching command(s) not shown: each one's own documentation is longer \
             than a whole answer"
        );
    }
    let answer = format!("[{}]", kept.join(",\n"));
    if kept.len() == total {
        // Every command is there but the vendor's pretty printing is past the cap: the compact
        // array says the same thing and fits.
        return answer;
    }
    let left_out = total - kept.len();
    format!("{answer}\n… {left_out} of {total} matching commands not shown; {NARROWER}")
}

/// The commands in a narrowed answer: the JSON array the vendor printed.
fn registry_rows(text: &str) -> Option<Vec<serde_json::Value>> {
    match first_json(text).0 {
        Some(serde_json::Value::Array(rows)) => Some(rows),
        _ => None,
    }
}

/// The unfiltered registry answer cut on a line boundary: the vendor prints one command per line, so
/// a whole line is a whole command.
fn capped_listing(text: &str) -> String {
    capped_lines(text, MAX_ANSWER_BYTES, "command", NARROWER)
}

/// An answer cut on whole lines: the lines from the start that fit `budget` less the room the note
/// needs, then one line saying what was withheld — `remedy`, when there is a way to reach the rest. A
/// line that does not fit on its own is dropped whole rather than cut inside, `noun` being what one
/// line is, and an answer never opens with the blank lines a program may print before its first line.
/// The cut is byte-bounded, so an answer built here, note included, stays under `budget` and the
/// product's own tool-output truncation never re-cuts it.
pub(crate) fn capped_lines(text: &str, budget: usize, noun: &str, remedy: &str) -> String {
    let text = text.trim_start();
    if text.is_empty() {
        return String::new();
    }
    // The note is reserved out of the budget up front, so an answer built here — kept lines and note
    // together — never runs past what its caller allowed.
    let room = budget.saturating_sub(NOTE_ROOM);
    let total = text.lines().count();
    let mut kept = String::new();
    let mut kept_lines = 0;
    for line in text.split_inclusive('\n') {
        if kept.len() + line.len() > room {
            break;
        }
        kept.push_str(line);
        kept_lines += 1;
    }
    let withheld = total - kept_lines;
    let note = if withheld == 0 {
        String::new()
    } else {
        let plural = if withheld == 1 { "" } else { "s" };
        if remedy.is_empty() {
            format!("… {withheld} further {noun}{plural} not shown")
        } else {
            format!("… {withheld} further {noun}{plural} not shown; {remedy}")
        }
    };
    match (kept_lines, note.is_empty()) {
        (0, _) => note,
        (_, true) => kept.trim_end().to_string(),
        (_, false) => format!("{}\n{note}", kept.trim_end()),
    }
}

/// What a failed run tells a model: what was attempted, how the run ended, and the vendor's own
/// words, bounded — its first lines and its last, because a fatal error is the last thing it says. A
/// run the product stopped is named as such — never as an io error.
fn failure(what: &str, outcome: &ProgramOutcome) -> String {
    let detail = match outcome.stop {
        Some(Stop::Spawn) => "the program could not be started on this machine".to_string(),
        Some(Stop::Memory) => format!(
            "the product's memory watchdog stopped it: {}",
            outcome.stop_reason()
        ),
        // No ceiling is set for this program, so a stop at one cannot arise.
        Some(Stop::Timeout) | None => outcome.detail.trim().to_string(),
    };
    let detail = if detail.is_empty() {
        "no reason was reported".to_string()
    } else {
        detail
    };
    let output =
        crate::util::truncate_sandwich(outcome.output.trim(), MAX_FAILURE_BYTES, "vendor output");
    if output.is_empty() {
        return format!("photocraft {what} failed ({detail})");
    }
    format!("photocraft {what} failed ({detail}): {output}")
        .trim_end()
        .to_string()
}

#[cfg(test)]
mod tests {
    use serde_json::json;

    use super::*;

    fn step(command: &str, params: Option<serde_json::Value>) -> Step {
        Step {
            command: command.to_string(),
            params,
        }
    }

    fn outcome(detail: &str, output: &str, stop: Option<Stop>) -> ProgramOutcome {
        ProgramOutcome {
            success: false,
            detail: detail.to_string(),
            output: output.to_string(),
            has_output: !output.is_empty(),
            stop,
        }
    }

    /// A count an answer states as exact, for the fixtures that never read a folder.
    fn counted(count: usize) -> FilesWritten {
        FilesWritten { count, whole: true }
    }

    #[test]
    fn a_chain_runs_its_steps_in_order_before_the_output() {
        let input = Some(Path::new("/work/plate.psd"));
        let output = Path::new("/work/out.png");
        // No steps is the vendor's `convert`, whose destination is the second positional argument.
        assert_eq!(
            chain_args(input, None, &[], output),
            [
                "convert",
                "/work/plate.psd",
                "/work/out.png",
                "--approve-high-memory"
            ]
        );
        // A step without parameters carries no `--params` at all.
        assert_eq!(
            chain_args(input, None, &[step("autocrop", None)], output),
            [
                "run",
                "/work/plate.psd",
                "--cmd",
                "autocrop",
                "--out",
                "/work/out.png",
                "--approve-high-memory"
            ]
        );
        assert_eq!(
            chain_args(
                input,
                None,
                &[
                    step("resize", Some(json!({"width": 800}))),
                    step("sharpen", None),
                ],
                output
            ),
            [
                "run",
                "/work/plate.psd",
                "--cmd",
                "resize",
                "--params",
                "{\"width\":800}",
                "--cmd",
                "sharpen",
                "--out",
                "/work/out.png",
                "--approve-high-memory"
            ]
        );
    }

    #[test]
    fn a_canvas_created_from_nothing_is_the_vendors_new_document() {
        let canvas = json!({"width": 800, "height": 600});
        let output = Path::new("/work/out.png");
        // No document to open: `convert` has nothing to read, so the canvas goes through `run`.
        assert_eq!(
            chain_args(None, Some(&canvas), &[], output),
            [
                "run",
                "--new",
                "{\"height\":600,\"width\":800}",
                "--out",
                "/work/out.png",
                "--approve-high-memory"
            ]
        );
        // A chain applies to the fresh document like any other.
        let args = chain_args(None, Some(&canvas), &[step("fill", None)], output);
        assert_eq!(
            args[..3],
            ["run", "--new", "{\"height\":600,\"width\":800}"]
        );
        assert_eq!(args[3], "--cmd");
        assert!(args.contains(&"--out".to_string()));
    }

    #[test]
    fn a_tiff_destination_keeps_its_layers_and_every_other_format_is_left_alone() {
        let input = Some(Path::new("/work/plate.psd"));
        let chain = [step("filter.sharpen.smartSharpen", None)];
        // Without the flag the vendor flattens a TIFF, so a `.tiff` destination must carry it —
        // on the open-and-save path as well as the chain path.
        for output in ["/work/out.tiff", "/work/out.TIF"] {
            let args = chain_args(input, None, &chain, Path::new(output));
            assert!(
                args.contains(&"--tiff-layers".to_string()),
                "{output}: {args:?}"
            );
            let converted = chain_args(input, None, &[], Path::new(output));
            assert!(
                converted.contains(&"--tiff-layers".to_string()),
                "{output}: {converted:?}"
            );
        }
        // A PNG, a PSD and a name with no extension are not TIFF and carry nothing extra.
        for output in ["/work/out.png", "/work/out.psd", "/work/out"] {
            assert!(
                !chain_args(input, None, &chain, Path::new(output))
                    .contains(&"--tiff-layers".to_string()),
                "{output}"
            );
        }
    }

    #[test]
    fn inspection_and_the_registry_ask_for_what_the_caller_needs() {
        // `--compact` keeps a layered document's report to one line; `--approve-high-memory` keeps
        // the vendor's own soft gate from refusing a large PSD the product was asked to open.
        assert_eq!(
            inspect_args(Path::new("/work/plate.pcraft")),
            [
                "info",
                "/work/plate.pcraft",
                "--compact",
                "--approve-high-memory"
            ]
        );
        assert_eq!(listing_args(None), ["commands"]);
        // A narrowed answer asks for the JSON, which is what carries each match's parameter docs.
        assert_eq!(
            listing_args(Some("blur")),
            ["commands", "--filter", "blur", "--json"]
        );
    }

    #[test]
    fn batch_carries_every_flag_it_was_given_and_none_it_was_not() {
        let recipe = Path::new("/tmp/recipe.json");
        let input = Path::new("/work/in");
        let output = Path::new("/work/out");
        assert_eq!(
            batch_args(recipe, input, output, None, None, false),
            [
                "batch",
                "--actions",
                "/tmp/recipe.json",
                "--in",
                "/work/in",
                "--out",
                "/work/out",
                "--approve-high-memory"
            ]
        );
        assert_eq!(
            batch_args(recipe, input, output, Some("png"), Some(90), true),
            [
                "batch",
                "--actions",
                "/tmp/recipe.json",
                "--in",
                "/work/in",
                "--out",
                "/work/out",
                "--format",
                "png",
                "--quality",
                "90",
                "--tiff-layers",
                "--approve-high-memory"
            ]
        );
        // A TIFF output keeps its layers whether the flag was set or the format names it.
        for format in ["tiff", "TIF"] {
            let args = batch_args(recipe, input, output, Some(format), None, false);
            assert!(
                args.contains(&"--tiff-layers".to_string()),
                "{format}: {args:?}"
            );
        }
    }

    #[test]
    fn a_long_batch_answer_keeps_its_first_and_last_lines_and_says_how_many_it_left_out() {
        // The vendor's shape: one line per produced file, then its own summary.
        let mut spelled: Vec<String> = (0..400)
            .map(|index| format!("ok    /work/in/photo{index}.heic -> /work/out/photo{index}.png"))
            .collect();
        spelled.push("400 succeeded, 0 failed".to_string());
        let results: Vec<&str> = spelled[..400].iter().map(String::as_str).collect();
        let output = spelled.join("\n");
        assert!(output.len() > MAX_ANSWER_BYTES, "the fixture must not fit");
        let capped = batch_answer(&output, &counted(400), Path::new("/work/out"));
        let kept: Vec<&str> = capped.lines().collect();
        // The product's own count opens the answer: the one fact a caller must never lose.
        assert_eq!(kept[0], "400 file(s) written to /work/out");
        let note = kept[1..]
            .iter()
            .position(|line| line.starts_with('…'))
            .map(|at| at + 1)
            .expect("the cap says what it left out");
        // Whole result lines from the start, whole result lines from the end, and the vendor's own
        // summary after them.
        assert_eq!(kept[1..note], results[..note - 1]);
        let tail = &kept[note + 1..kept.len() - 1];
        assert_eq!(tail, &results[results.len() - tail.len()..]);
        assert!(
            kept[note].contains(&format!(
                "{} further result lines not shown",
                results.len() - (note - 1) - tail.len()
            )),
            "{}",
            kept[note]
        );
        // The vendor's own summary is kept whole, after the report.
        assert_eq!(capped.lines().last(), Some("400 succeeded, 0 failed"));
        // The cap binds before the product's own tool-output cut, which would take the same middle
        // out with no count, and the note is inside it.
        assert!(capped.len() <= MAX_ANSWER_BYTES, "{}", capped.len());
        assert_eq!(crate::util::truncate_tool_output(&capped), capped);
        // An answer within the cap is the vendor's own text under the count.
        let short = "ok    /work/in/a.heic -> /work/out/a.png\n1 succeeded, 0 failed";
        assert_eq!(
            batch_answer(short, &counted(1), Path::new("/work/out")),
            format!("1 file(s) written to /work/out\n{short}")
        );
    }

    #[test]
    fn a_batch_answer_keeps_every_failure_and_the_summary_whatever_it_drops() {
        // The vendor's own report: successes and the summary on stdout, then the failures and the
        // engine's per-file warnings on stderr, which arrives appended. The summary is therefore
        // nowhere near the tail, and the failure sits behind hundreds of per-file lines.
        let mut spelled: Vec<String> = (0..300)
            .map(|index| format!("ok    /work/in/photo{index}.jpg -> /work/out/photo{index}.jpg"))
            .collect();
        spelled.push("300 succeeded, 1 failed".to_string());
        spelled.push("FAIL  /work/in/photo150.jpg: unsupported 12-bit depth".to_string());
        // A wall of distinct warnings: the palette message names each file's own colour count.
        spelled
            .extend((0..300).map(|index| {
                format!("warning: written as an 8-bit palette BMP ({index} colours)")
            }));
        let output = spelled.join("\n");
        assert!(output.len() > MAX_ANSWER_BYTES, "the fixture must not fit");
        let capped = batch_answer(&output, &counted(300), Path::new("/work/out"));
        let kept: Vec<&str> = capped.lines().collect();
        assert_eq!(kept[0], "300 file(s) written to /work/out");
        // Both facts survive: which file failed and why, and the vendor's own count.
        assert!(
            capped.contains("FAIL  /work/in/photo150.jpg: unsupported 12-bit depth"),
            "{capped}"
        );
        assert_eq!(capped.lines().last(), Some("300 succeeded, 1 failed"));
        // The 300 warnings are one kind, and what did not fit is counted as a kind.
        assert!(
            capped.contains("… 1 further warning kinds not shown"),
            "{capped}"
        );
        // Whole lines only, and the cap binds before the product's own cut.
        assert!(
            kept[1..]
                .iter()
                .all(|line| spelled.contains(&line.to_string()) || line.starts_with('…')),
            "{capped}"
        );
        assert_eq!(crate::util::truncate_tool_output(&capped), capped);
    }

    #[test]
    fn a_capped_batch_still_shows_a_kind_whose_per_file_values_all_differ() {
        // 100 per-file palette warnings are one kind, and the result lines leave it the room.
        let mut spelled: Vec<String> = (0..100)
            .map(|index| format!("ok    /work/in/photo{index}.png -> /work/out/photo{index}.bmp"))
            .collect();
        spelled.push("100 succeeded, 0 failed".to_string());
        spelled
            .extend((0..100).map(|index| {
                format!("warning: written as an 8-bit palette BMP ({index} colours)")
            }));
        let output = spelled.join("\n");
        assert!(output.len() > MAX_ANSWER_BYTES, "the fixture must not fit");
        let capped = batch_answer(&output, &counted(100), Path::new("/work/out"));
        assert!(
            capped.contains("warning (100 lines): written as an 8-bit palette BMP (N colours)"),
            "{capped}"
        );
        assert_eq!(capped.lines().last(), Some("100 succeeded, 0 failed"));
        assert!(capped.len() <= MAX_ANSWER_BYTES, "{}", capped.len());
        assert_eq!(crate::util::truncate_tool_output(&capped), capped);
    }

    #[test]
    fn a_batch_that_needs_every_note_still_fits_and_keeps_its_head_and_summary() {
        let mut spelled: Vec<String> = (0..200)
            .map(|index| format!("ok    /work/in/photo{index}.jpg -> /work/out/photo{index}.jpg"))
            .collect();
        spelled.push("200 succeeded, 200 failed".to_string());
        spelled.extend(
            (0..200).map(|index| format!("FAIL  /work/in/broken{index}.jpg: unreadable header")),
        );
        spelled.extend(
            (0..200)
                .map(|index| format!("warning: an encoder complaint number {index} about a file")),
        );
        let output = spelled.join("\n");
        let capped = batch_answer(&output, &counted(200), Path::new("/work/out"));
        assert_eq!(
            capped.lines().next(),
            Some("200 file(s) written to /work/out")
        );
        assert_eq!(capped.lines().last(), Some("200 succeeded, 200 failed"));
        // All three cuts say what they dropped, and the whole answer stays inside the cap.
        assert!(
            capped.contains("further result lines not shown"),
            "{capped}"
        );
        assert!(capped.contains("further failures not shown"), "{capped}");
        assert!(
            capped.contains("further warning kinds not shown"),
            "{capped}"
        );
        assert!(capped.len() <= MAX_ANSWER_BYTES, "{}", capped.len());
        assert_eq!(crate::util::truncate_tool_output(&capped), capped);
    }

    #[test]
    fn a_batch_answer_collapses_the_engines_warnings_to_one_line_per_kind() {
        // The live shape: 25 PNGs converted to JPG at quality 80, four warnings per file, all of them
        // on stderr after the vendor's summary.
        let mut spelled: Vec<String> = (0..25)
            .map(|index| format!("ok    /work/in/photo{index}.png -> /work/out/photo{index}.jpg"))
            .collect();
        spelled.push("25 succeeded, 0 failed".to_string());
        for _ in 0..25 {
            spelled.push("warning: transparency composited over white for Jpeg".to_string());
            spelled.push("warning: 16-bit will be reduced to 8-bit".to_string());
            spelled.push("warning: lossy compression".to_string());
            spelled.push("warning: text metadata dropped".to_string());
        }
        let output = spelled.join("\n");
        let answer = batch_answer(&output, &counted(25), Path::new("/work/out"));
        let kept: Vec<&str> = answer.lines().collect();
        // The product's count opens the answer and the vendor's summary closes it, untouched.
        assert_eq!(kept[0], "25 file(s) written to /work/out");
        assert_eq!(kept.last(), Some(&"25 succeeded, 0 failed"));
        // Every per-file result survives.
        assert_eq!(
            kept.iter().filter(|line| line.starts_with("ok")).count(),
            25,
            "{answer}"
        );
        // One line per kind, each carrying the 25 warning lines it collapsed.
        let warnings: Vec<&&str> = kept
            .iter()
            .filter(|line| line.starts_with("warning"))
            .collect();
        assert_eq!(warnings.len(), 4, "{answer}");
        for line in warnings {
            assert!(line.contains("(25 lines)"), "{line}");
        }
        assert!(
            answer.contains("warning (25 lines): lossy compression"),
            "{answer}"
        );
        assert!(
            answer.contains("warning (25 lines): 16-bit will be reduced to 8-bit"),
            "{answer}"
        );
        // The answer is a fraction of the wall of warnings it replaced, and fits before the product's
        // own cut.
        assert!(answer.len() < 3_000, "{}", answer.len());
        assert_eq!(crate::util::truncate_tool_output(&answer), answer);
    }

    #[test]
    fn a_warning_naming_a_per_file_value_is_one_kind_however_the_value_differs() {
        // The vendor's palette warning names each file's own colour count, so exact text would give
        // one line per file. The count varies, the wording does not: one kind, with `N` where it
        // differed and the numbers every file agreed on kept.
        let output = [
            "ok    /work/in/a.png -> /work/out/a.bmp",
            "warning: written as an 8-bit palette BMP (16 colours)",
            "ok    /work/in/b.png -> /work/out/b.bmp",
            "warning: written as an 8-bit palette BMP (2 colours)",
            "ok    /work/in/c.png -> /work/out/c.bmp",
            "warning: written as an 8-bit palette BMP (200 colours)",
            "3 succeeded, 0 failed",
        ]
        .join("\n");
        let answer = batch_answer(&output, &counted(3), Path::new("/work/out"));
        let warnings: Vec<&str> = answer
            .lines()
            .filter(|line| line.starts_with("warning"))
            .collect();
        assert_eq!(
            warnings,
            ["warning (3 lines): written as an 8-bit palette BMP (N colours)"]
        );
        assert_eq!(answer.lines().last(), Some("3 succeeded, 0 failed"));
    }

    #[test]
    fn the_engines_own_error_line_is_a_result_and_not_swallowed_as_a_warning() {
        // `error: <n> file(s) failed` shares stderr with the warnings but is the vendor's own result
        // line: it stays one, and a single warning still carries its count.
        let output = [
            "ok    /work/in/a.png -> /work/out/a.jpg",
            "warning: lossy compression",
            "error: 1 file(s) failed",
            "FAIL  /work/in/b.png: unknown file format: png8",
            "1 succeeded, 1 failed",
        ]
        .join("\n");
        let answer = batch_answer(&output, &counted(1), Path::new("/work/out"));
        assert!(answer.contains("error: 1 file(s) failed"), "{answer}");
        assert!(
            answer.contains("FAIL  /work/in/b.png: unknown file format: png8"),
            "{answer}"
        );
        assert!(
            answer.contains("warning (1 line): lossy compression"),
            "{answer}"
        );
        assert_eq!(answer.lines().last(), Some("1 succeeded, 1 failed"));
    }

    #[test]
    fn a_batch_too_large_for_its_results_gives_them_the_room_before_any_warning() {
        let mut spelled: Vec<String> = (0..400)
            .map(|index| format!("ok    /work/in/photo{index}.jpg -> /work/out/photo{index}.jpg"))
            .collect();
        spelled.push("400 succeeded, 0 failed".to_string());
        let warning = format!("warning: {}", "x".repeat(300));
        spelled.extend((0..400).map(|_| warning.clone()));
        let output = spelled.join("\n");
        let answer = batch_answer(&output, &counted(400), Path::new("/work/out"));
        // The results fill the answer; the warning is too long for the room they left, so it is
        // counted rather than taking a result line's place.
        assert!(
            answer.contains("further result lines not shown"),
            "{answer}"
        );
        assert!(
            answer.contains("… 1 further warning kinds not shown"),
            "{answer}"
        );
        assert!(!answer.contains("warning (400 lines)"), "{answer}");
        assert_eq!(answer.lines().last(), Some("400 succeeded, 0 failed"));
        assert!(answer.len() <= MAX_ANSWER_BYTES, "{}", answer.len());
        assert_eq!(crate::util::truncate_tool_output(&answer), answer);
    }

    #[test]
    fn a_batch_with_more_failures_than_fit_keeps_the_summary_and_counts_the_rest() {
        let mut spelled: Vec<String> = (0..400)
            .map(|index| format!("FAIL  /work/in/photo{index}.heic: unreadable header"))
            .collect();
        spelled.push("0 succeeded, 400 failed".to_string());
        let output = spelled.join("\n");
        let capped = batch_answer(&output, &counted(0), Path::new("/work/out"));
        assert_eq!(
            capped.lines().next(),
            Some("no file was written into /work/out"),
            "the count is the head"
        );
        // Every kept failure line is whole, and the ones that did not fit are counted.
        let note = capped
            .lines()
            .find(|line| line.starts_with("… ") && line.ends_with("further failures not shown"))
            .expect("the cap counts the failures it dropped");
        assert!(capped.contains("FAIL  /work/in/photo0.heic: unreadable header"));
        assert_eq!(capped.lines().last(), Some("0 succeeded, 400 failed"));
        assert!(note.contains("further failures not shown"), "{note}");
        assert!(capped.len() <= MAX_ANSWER_BYTES, "{}", capped.len());
        assert_eq!(crate::util::truncate_tool_output(&capped), capped);
        // A batch of nothing but failures fills the answer: the room the failures can use is theirs,
        // not half of it.
        assert!(
            capped.len() + 200 >= MAX_ANSWER_BYTES,
            "{} of {MAX_ANSWER_BYTES}",
            capped.len()
        );
    }

    #[test]
    fn a_batch_answer_never_opens_with_the_blank_line_the_engine_printed_first() {
        let output = "\n\nok    /work/in/a.heic -> /work/out/a.png\n1 succeeded, 0 failed\n";
        let answer = batch_answer(output, &counted(1), Path::new("/work/out"));
        assert_eq!(
            answer.lines().next(),
            Some("1 file(s) written to /work/out")
        );
        assert!(!answer.starts_with('\n'), "{answer:?}");
    }

    #[test]
    fn the_recipe_is_the_vendors_step_list_with_params_only_where_there_are_some() {
        let plain = recipe_json(&[step("autocrop", None), step("sharpen", None)]);
        assert_eq!(
            serde_json::from_str::<serde_json::Value>(&plain).expect("the recipe parses"),
            json!([{"command": "autocrop"}, {"command": "sharpen"}])
        );
        // The vendor's own shape: a step without parameters has no `params` key.
        assert!(!plain.contains("params"), "{plain}");
        let with_params = recipe_json(&[step("resize", Some(json!({"width": 800})))]);
        assert_eq!(
            serde_json::from_str::<serde_json::Value>(&with_params).expect("the recipe parses"),
            json!([{"command": "resize", "params": {"width": 800}}])
        );
    }

    #[test]
    fn a_listing_within_the_cap_comes_back_as_the_vendor_wrote_it() {
        let registry = "blur: gaussian blur\ncrop: cut the canvas\n";
        assert_eq!(registry_answer(None, registry), registry);
        // A narrowed answer under the cap is the vendor's own text too, never re-serialized.
        let narrowed = "[\n  {\n    \"id\": \"image.crop\"\n  }\n]";
        assert_eq!(registry_answer(Some("crop"), narrowed), narrowed);
    }

    #[test]
    fn a_filter_that_matches_nothing_says_so_instead_of_answering_blank() {
        // The vendor answers a filter that matches nothing with an empty array, or with nothing at
        // all; a real match is never mistaken for one.
        for empty in ["[]", " [ ]\n", "", "  \n"] {
            assert!(is_empty_listing(empty), "{empty:?}");
        }
        assert!(!is_empty_listing(r#"[{"id":"image.crop"}]"#));
        let answer = no_commands(Some("nosuchthing"));
        assert!(answer.contains("no command matches"), "{answer}");
        assert!(answer.contains("nosuchthing"), "{answer}");
        assert!(no_commands(None).contains("registry is empty"));
    }

    #[test]
    fn a_listing_past_the_cap_is_cut_on_a_line_boundary_and_says_how_much_it_withheld() {
        let line = "blur: gaussian blur; params: radius (number)\n";
        // The lines that fit one answer less the room its note needs.
        let fits = (MAX_ANSWER_BYTES - NOTE_ROOM) / line.len();
        let withheld = 25;
        let registry = line.repeat(fits + withheld);
        let capped = capped_listing(&registry);
        let mut lines = capped.lines().collect::<Vec<_>>();
        let note = lines.pop().expect("the cap says what it withheld");
        // Every kept line is a whole line of the vendor's own text.
        assert!(lines.iter().all(|kept| *kept == line.trim_end()));
        assert_eq!(lines.len(), fits);
        assert!(
            note.contains(&format!("{withheld} further commands")),
            "{note}"
        );
        assert!(note.contains("a narrower filter shows the rest"), "{note}");
        // The cap binds before the product's own tool-output cut, so a model sees this note
        // rather than that cut's raw omission marker — and the note itself is inside the cap.
        assert!(capped.len() <= MAX_ANSWER_BYTES, "{}", capped.len());
        assert_eq!(crate::util::truncate_tool_output(&capped), capped);
    }

    #[test]
    fn a_registry_answer_the_capture_cut_in_the_middle_says_so_instead_of_half_a_document() {
        // A filter matching most of the registry produces more than one capture holds, so the vendor's
        // own JSON arrives cut in the middle: no whole command can be recovered, and half a document
        // would make a model guess at parameter names.
        let text = format!(
            "[{{\"id\":\"image.crop\"}},\n{}\n[{{\"id\":\"image.trim\"}}]",
            crate::util::TRUNCATED_MIDDLE
        );
        let answer = registry_answer(Some("image"), &text);
        assert_eq!(
            answer,
            format!("… the registry answer was too large to capture whole; {NARROWER}")
        );
        assert!(
            !answer.contains('{'),
            "no fragment of the document: {answer}"
        );
    }

    #[test]
    fn an_inspection_the_capture_cut_in_the_middle_says_so_and_keeps_the_format() {
        // A layered document's report can outgrow the product's own capture, which splices its
        // marker into the middle of the vendor's JSON: no whole document is recoverable, so the
        // answer says so rather than handing back half a value — and keeps the one fact the file
        // name gives.
        let text = format!(
            "{{\"width\":4096,\"mode\":\"Rgb\",\"layers\":[{{\"name\":\"Layer 0\"}},\n{}\n{{\"name\":\"Layer 900\"}}]}}",
            crate::util::TRUNCATED_MIDDLE
        );
        let answer = inspection_answer(&text, Path::new("/work/plate.psd"));
        assert_eq!(
            answer,
            "… the inspection was too large to capture whole; format: psd (from the file name)"
        );
        assert!(!answer.contains('{'), "no fragment: {answer}");
        assert!(!answer.starts_with('\n'), "no blank first line");
        assert_eq!(crate::util::truncate_tool_output(&answer), answer);
    }

    #[test]
    fn a_line_too_long_to_fit_is_dropped_whole_rather_than_cut_inside() {
        // One line can be longer than the cap on its own — an answer carrying no JSON array to cut
        // at whole commands. It is a record, so it goes whole and the answer names the count; what
        // arrives is never half a line a caller could mistake for a complete one.
        let line = format!(
            "[{}]",
            "{\"id\":\"image.crop\",\"params\":\"кадр\"},".repeat(2000)
        );
        let capped = capped_listing(&line);
        assert_eq!(capped, format!("… 1 further command not shown; {NARROWER}"));
        assert!(!capped.starts_with('\n'), "no blank first line");
        assert_eq!(crate::util::truncate_tool_output(&capped), capped);
        // Blank lines the engine printed first are not part of the answer either.
        let capped = capped_listing("\n\nblur: gaussian blur\n");
        assert_eq!(capped, "blur: gaussian blur");
    }

    #[test]
    fn the_inspection_answer_names_the_format_its_file_name_gives_as_one_more_field() {
        let answer = inspection_answer(
            r#"{"file":"/work/plate.PSD","mode":"Rgb","depth":8}"#,
            Path::new("/work/plate.PSD"),
        );
        let document: serde_json::Value =
            serde_json::from_str(&answer).expect("the answer is still one JSON document");
        assert_eq!(document["format"], "psd (from the file name)");
        assert_eq!(document["mode"], "Rgb", "the engine's own fields stand");
        // No extension, or an empty one: the answer says it does not know, rather than guessing.
        for name in ["/work/scan", "/work/scan."] {
            let answer = inspection_answer(r#"{"file":"x"}"#, Path::new(name));
            let document: serde_json::Value =
                serde_json::from_str(&answer).expect("still one JSON document");
            assert_eq!(
                document["format"],
                "unknown (the file name has no extension)"
            );
        }
        // An engine answer that is not a JSON object keeps its text and gains the format after it.
        let answer = inspection_answer("mode: Rgb\ndepth: 8\n", Path::new("/work/plate.pcraft"));
        assert_eq!(
            answer,
            "mode: Rgb\ndepth: 8\nformat: pcraft (from the file name)"
        );
        // The engine's stderr is concatenated after its stdout: the document is still one JSON
        // object, and what the engine wrote beside it stays after it.
        let answer = inspection_answer(
            "{\"mode\":\"Rgb\"}\nwarning: a profile could not be embedded",
            Path::new("/work/plate.psd"),
        );
        let (document, beside) = answer
            .split_once('\n')
            .expect("the warning stays after the document");
        assert_eq!(beside, "warning: a profile could not be embedded");
        let document: serde_json::Value =
            serde_json::from_str(document).expect("still one JSON document");
        assert_eq!(document["format"], "psd (from the file name)");
        assert_eq!(document["mode"], "Rgb");
    }

    #[test]
    fn a_layered_inspection_keeps_its_facts_and_counts_the_layers_that_do_not_fit() {
        // The engine prints small arrays beside the layer tree — notes, the selected layers, the
        // snapshots, the warnings — and a real document's report lands just past one answer at some
        // sizes and not others. Wherever the cut falls, the document's own facts and those arrays
        // must survive and the tree must end in a count of what did not fit: never half a layer, and
        // never the format alone.
        for count in 1..=40 {
            for padding in [0, 7, 13, 29, 41] {
                let layers: Vec<serde_json::Value> = (0..count)
                    .map(|index| {
                        // A real layer entry carries its bounds, mask and effects: some 320 bytes.
                        json!({
                            "name": format!("Layer {index}{}", "x".repeat(padding)),
                            "kind": "pixel",
                            "visible": true,
                            "opacity": 100,
                            "blend": "normal",
                            "bounds": {"x": 0, "y": 0, "width": 3000, "height": 2000},
                            "source": format!("/work/plate.psd#layer{index}"),
                            "mask": {"path": format!("/work/mask-{index}.png"), "enabled": true, "density": 100},
                            "effects": {"dropShadow": false, "stroke": false, "bevel": false},
                        })
                    })
                    .collect();
                let document = json!({
                    "file": "/work/plate.psd",
                    "width": 3000,
                    "height": 2000,
                    "mode": "Rgb",
                    "depth": 8,
                    "layers": layers,
                    "notes": [],
                    "selectedLayers": [],
                    "snapshots": [],
                    "warnings": [],
                })
                .to_string();
                if document.len() <= MAX_ANSWER_BYTES {
                    continue;
                }
                let where_ = format!("{count} layers padded by {padding}");
                let answer = inspection_answer(&document, Path::new("/work/plate.psd"));
                assert!(
                    answer.len() <= MAX_ANSWER_BYTES,
                    "{where_}: {}",
                    answer.len()
                );
                assert_eq!(
                    crate::util::truncate_tool_output(&answer),
                    answer,
                    "{where_}"
                );
                let document: serde_json::Value = serde_json::from_str(&answer)
                    .unwrap_or_else(|_| panic!("{where_}: not one JSON document: {answer}"));
                for fact in ["format", "width", "height", "mode", "depth"] {
                    assert!(
                        !document[fact].is_null(),
                        "{where_}: {fact} was lost: {answer}"
                    );
                }
                assert_eq!(document["notes"], json!([]), "{where_}: {answer}");
                let kept = document["layers"]
                    .as_array()
                    .unwrap_or_else(|| panic!("{where_}: {}", document["layers"]));
                // The last entry is the count of what did not fit, and every entry before it is whole.
                let (note, entries) = kept.split_last().expect("{where_}: the tree is not empty");
                assert_eq!(
                    note.as_str(),
                    Some(
                        format!(
                            "… {} of {count} further entries not shown",
                            count - entries.len()
                        )
                        .as_str()
                    ),
                    "{where_}"
                );
                assert_eq!(
                    entries[0]["name"],
                    format!("Layer 0{}", "x".repeat(padding)),
                    "{where_}"
                );
            }
        }
    }

    #[test]
    fn a_layered_inspection_leaves_a_later_array_its_own_count() {
        // Two arrays past one answer: the cheaper one is filled first, and it must leave the other the
        // room to say how much it holds — one array never starves the rest of the answer.
        let warnings: Vec<serde_json::Value> = (0..300)
            .map(|index| json!(format!("layer {index} has no colour profile")))
            .collect();
        let layers: Vec<serde_json::Value> = (0..400)
            .map(|index| {
                json!({
                    "name": format!("Layer {index}"),
                    "kind": "pixel",
                    "visible": true,
                    "bounds": {"x": 0, "y": 0, "width": 3000, "height": 2000},
                })
            })
            .collect();
        let document = json!({
            "file": "/work/plate.psd",
            "width": 3000,
            "height": 2000,
            "mode": "Rgb",
            "depth": 8,
            "layers": layers,
            "warnings": warnings,
        })
        .to_string();
        let answer = inspection_answer(&document, Path::new("/work/plate.psd"));
        assert!(answer.len() <= MAX_ANSWER_BYTES, "{}", answer.len());
        let document: serde_json::Value =
            serde_json::from_str(&answer).expect("still one whole JSON document");
        assert_eq!(document["mode"], "Rgb", "{answer}");
        for name in ["layers", "warnings"] {
            let kept = document[name]
                .as_array()
                .unwrap_or_else(|| panic!("{name}: {}", document[name]));
            assert!(
                kept.last().is_some_and(serde_json::Value::is_string),
                "{name} keeps its count of the rest: {answer}"
            );
        }
    }

    #[test]
    fn a_narrowed_answer_drops_whole_commands_and_says_how_many_it_left_out() {
        let rows: Vec<serde_json::Value> = (0..500)
            .map(|index| json!({"id": format!("image.command{index}"), "params": "x".repeat(40)}))
            .collect();
        let text = serde_json::to_string_pretty(&rows).expect("the answer serializes");
        assert!(text.len() > MAX_ANSWER_BYTES, "the fixture must not fit");
        let capped = capped_rows(&text);
        // The kept part is whole commands — a JSON array that parses, never half a row.
        let body = capped
            .lines()
            .take_while(|line| !line.starts_with('…'))
            .collect::<Vec<_>>()
            .join("\n");
        let parsed: Vec<serde_json::Value> =
            serde_json::from_str(&body).expect("the kept commands are a JSON array");
        assert!(!parsed.is_empty(), "the answer carries what fits");
        assert!(parsed.len() < rows.len());
        assert_eq!(
            parsed,
            rows[..parsed.len()].to_vec(),
            "leading commands only"
        );
        let note = capped
            .lines()
            .last()
            .expect("the cap says what it left out");
        assert!(
            note.contains(&format!(
                "{} of 500 matching commands not shown",
                rows.len() - parsed.len()
            )),
            "{note}"
        );
        assert!(note.contains("a narrower filter shows the rest"), "{note}");
        // The cap binds before the product's own tool-output cut, note included.
        assert!(capped.len() <= MAX_ANSWER_BYTES, "{}", capped.len());
        assert_eq!(crate::util::truncate_tool_output(&capped), capped);
    }

    #[test]
    fn a_narrowed_answer_that_fits_comes_back_as_the_vendor_wrote_it() {
        // Many small commands: pretty-printed past the cap, every one of them fitting compactly.
        let rows: Vec<serde_json::Value> = (0..180)
            .map(|index| json!({"id": format!("image.command{index}")}))
            .collect();
        let pretty = serde_json::to_string_pretty(&rows).expect("the answer serializes");
        let compact = serde_json::to_string(&rows).expect("the answer serializes");
        assert!(
            pretty.len() > MAX_ANSWER_BYTES,
            "the fixture must not fit as printed"
        );
        assert!(
            compact.len() <= MAX_ANSWER_BYTES,
            "the fixture fits compactly"
        );
        let capped = registry_answer(Some("command"), &pretty);
        assert!(!capped.contains('…'), "nothing was left out: {capped}");
        assert_eq!(
            serde_json::from_str::<Vec<serde_json::Value>>(&capped).expect("still a JSON array"),
            rows
        );
    }

    #[test]
    fn a_narrowed_answer_that_is_not_a_json_array_is_still_cut_on_whole_lines() {
        let text = "image.crop: Crop\n".repeat(2000);
        let capped = capped_rows(&text);
        assert!(
            capped
                .lines()
                .all(|line| line == "image.crop: Crop" || line.starts_with('…')),
            "whole lines only"
        );
        assert_eq!(crate::util::truncate_tool_output(&capped), capped);
    }

    #[test]
    fn a_narrowed_answer_finds_its_commands_beside_a_warning_the_engine_wrote() {
        // The engine's stderr is concatenated after its stdout, so a warning must not cost the
        // answer its whole-command cut.
        let rows: Vec<serde_json::Value> = (0..300)
            .map(|index| json!({"id": format!("image.command{index}"), "params": "x".repeat(40)}))
            .collect();
        let text = format!(
            "{}\nwarning: some profile could not be embedded",
            serde_json::to_string_pretty(&rows).expect("the answer serializes")
        );
        let capped = capped_rows(&text);
        let body = capped
            .lines()
            .take_while(|line| !line.starts_with('…'))
            .collect::<Vec<_>>()
            .join("\n");
        let parsed: Vec<serde_json::Value> =
            serde_json::from_str(&body).expect("the kept commands are a JSON array");
        assert!(!parsed.is_empty());
        assert!(parsed.len() < rows.len());
        assert_eq!(parsed, rows[..parsed.len()].to_vec());
    }

    #[test]
    fn a_command_too_long_to_fit_whole_is_dropped_with_a_count_rather_than_cut_mid_entry() {
        // A registry answer is never cut mid-entry: half a command's parameter documentation reads
        // as a complete one. Not one command fits here, so the answer is an empty array and the
        // count — never a fragment.
        let rows = vec![json!({"id": "image.crop", "params": "x".repeat(MAX_ANSWER_BYTES)})];
        let text = serde_json::to_string_pretty(&rows).expect("the answer serializes");
        let capped = capped_rows(&text);
        let mut lines = capped.lines();
        assert_eq!(lines.next(), Some("[]"), "{capped}");
        let note = lines.next().expect("the cap says what it left out");
        assert!(
            note.contains("all 1 matching command(s) not shown"),
            "{note}"
        );
        assert!(note.contains("longer than a whole answer"), "{note}");
        assert_eq!(lines.next(), None, "nothing else is left: {capped}");
        assert_eq!(crate::util::truncate_tool_output(&capped), capped);
    }

    #[test]
    fn a_failed_run_carries_the_exit_detail_and_the_vendors_own_words() {
        let message = failure(
            "run",
            &outcome("exit status 2", "unknown flag --cmdd\n", None),
        );
        assert_eq!(
            message,
            "photocraft run failed (exit status 2): unknown flag --cmdd"
        );
    }

    #[test]
    fn a_failure_with_no_reason_says_so_instead_of_leaving_an_empty_parenthesis() {
        assert_eq!(
            failure("run", &outcome("", "", None)),
            "photocraft run failed (no reason was reported)"
        );
    }

    #[test]
    fn a_run_the_product_never_started_says_so_instead_of_quoting_an_io_error() {
        let message = failure(
            "run",
            &outcome("io error: No such file or directory", "", Some(Stop::Spawn)),
        );
        assert!(message.contains("could not be started"), "{message}");
        assert!(!message.contains("No such file"), "{message}");
    }

    #[test]
    fn the_memory_watchdog_is_named_as_the_product_stopping_the_run() {
        let message = failure(
            "batch",
            &outcome(
                "terminated: exceeded memory limit (used ~15.4 GiB of 18.0 GiB)",
                "",
                Some(Stop::Memory),
            ),
        );
        assert!(message.contains("memory watchdog"), "{message}");
        assert!(message.contains("exceeded memory limit"), "{message}");
    }

    #[test]
    fn the_summary_and_the_failures_are_the_vendors_own_lines_and_nothing_else_is() {
        // The vendor's closing line, with and without its --stop-on-error clause.
        for summary in [
            "0 succeeded, 0 failed",
            "300 succeeded, 1 failed",
            "4 succeeded, 2 failed, 7 skipped",
        ] {
            assert!(is_summary(summary), "{summary}");
        }
        // A per-file line, a failure, the exit status's own line and a warning are none of them a
        // summary — the decision between an answer and a failure rests on this.
        for other in [
            "ok    /in/a.png -> /out/a.png",
            "FAIL  /in/a.png: unreadable header",
            "error: 1 file(s) failed",
            "300 files succeeded, 1 failed",
            "warning: photo1.jpg: no ICC profile",
        ] {
            assert!(!is_summary(other), "{other}");
        }
        assert!(is_failure("FAIL  /in/a.png: unreadable header"));
        assert!(!is_failure("error: 1 file(s) failed"));
        assert!(!is_failure("ok    /in/a.png -> /out/a.png"));
    }

    #[test]
    fn a_batch_that_ran_to_its_summary_is_an_answer_and_one_that_stopped_is_a_failure() {
        // A batch that produced a file, failed on another and exited non-zero: the vendor reached its
        // summary, so the answer is the report — the exit status only repeats what it says.
        let ran = ProgramOutcome {
            success: false,
            detail: "exit status 1".to_string(),
            output: "ok    /in/a.png -> /out/a.png\n1 succeeded, 1 failed\nFAIL  /in/b.png: unreadable header".to_string(),
            has_output: true,
            stop: None,
        };
        let report = batch_outcome(&ran, &counted(1), Path::new("/out"))
            .expect("the run reached its summary");
        let answer = &report.answer;
        assert_eq!(answer.lines().next(), Some("1 file(s) written to /out"));
        assert!(
            answer.contains("FAIL  /in/b.png: unreadable header"),
            "{answer}"
        );
        // Nothing was left out, so the vendor's own report arrives as it wrote it.
        assert!(
            answer.lines().any(|line| line == "1 succeeded, 1 failed"),
            "{answer}"
        );
        // A file failed, so the call is a partial one: the command line exits non-zero on the answer
        // it still receives, and the agent reads the report.
        assert!(report.partial, "{answer}");

        // A run that stopped before its summary — a crash, a stop, a usage error — is the failure,
        // and the caller's own count of what it wrote before stopping is the tool's to add.
        let stopped = ProgramOutcome {
            success: false,
            detail: "exit status 1".to_string(),
            output: "ok    /in/a.png -> /out/a.png\nerror: the disk is full".to_string(),
            has_output: true,
            stop: None,
        };
        let error = batch_outcome(&stopped, &counted(1), Path::new("/out"))
            .expect_err("no summary, no report");
        assert!(
            error.starts_with("photocraft batch failed (exit status 1)"),
            "{error}"
        );
        assert!(error.contains("the disk is full"), "{error}");

        // A run that succeeded is an answer even with no summary line: nothing was left out, and
        // reporting a clean exit as a failure would be a lie about the machine.
        let clean = ProgramOutcome {
            success: true,
            detail: "exit status 0".to_string(),
            output: "ok    /in/a.png -> /out/a.png".to_string(),
            has_output: true,
            stop: None,
        };
        let report = batch_outcome(&clean, &counted(1), Path::new("/out"))
            .expect("a clean run is an answer");
        assert_eq!(
            report.answer.lines().next(),
            Some("1 file(s) written to /out")
        );
        assert!(!report.partial, "a clean run is no shortfall");
    }

    #[test]
    fn a_batch_that_produced_nothing_is_a_partial_answer_not_a_success() {
        // The vendor ran the folder to its summary and failed on every file: the report is the
        // answer — it names each file and the reason — and the call is a partial one, so a
        // command-line caller exits non-zero instead of reading a whole success.
        let report = batch_outcome(
            &outcome("exit status 1", "0 succeeded, 2 failed\nFAIL  /in/a.heic: unreadable header\nFAIL  /in/b.heic: unreadable header", None),
            &counted(0),
            Path::new("/out"),
        )
        .expect("the run reached its summary");
        assert_eq!(
            report.answer.lines().next(),
            Some("no file was written into /out")
        );
        assert!(report.partial, "{}", report.answer);
        // The summary's own count is enough on its own: a failure line the capture lost still
        // cannot make a failed batch read as a success.
        let summary_only = outcome("exit status 1", "3 succeeded, 4 failed", None);
        assert!(failed_some(&summary_only.output));
        assert!(!failed_some(
            "ok    /in/a.png -> /out/a.png\n1 succeeded, 0 failed"
        ));
    }

    #[test]
    fn an_inspection_whose_own_fields_alone_do_not_fit_keeps_the_format_and_counts_the_rest() {
        // The vendor's inspection does not produce a document whose own fields alone are past one
        // answer; when one arrives, the answer keeps the format the product adds and one note counting
        // what it could not show — still one JSON document rather than half a value.
        let mut document = serde_json::Map::new();
        document.insert("mode".to_string(), json!("Rgb"));
        document.insert("depth".to_string(), json!(8));
        for index in 0..8 {
            document.insert(format!("field{index}"), json!("y".repeat(800)));
        }
        let answer = inspection_answer(
            &serde_json::Value::Object(document).to_string(),
            Path::new("/work/plate.psd"),
        );
        assert!(answer.len() <= MAX_ANSWER_BYTES, "{}", answer.len());
        let document: serde_json::Value =
            serde_json::from_str(&answer).expect("still one whole JSON document");
        assert_eq!(document["format"], "psd (from the file name)");
        assert_eq!(document["omitted"], "… 10 further member(s) not shown");
        assert_eq!(crate::util::truncate_tool_output(&answer), answer);
    }

    #[test]
    fn a_count_the_folder_could_not_be_read_for_is_stated_as_the_floor_it_is() {
        // A folder that cannot be read in full leaves the count a lower bound: the answer says so
        // beside the count rather than passing a short read off as the one fact a batch answer must
        // never lose — and the sentence stays one the reader can parse, whatever the count is.
        let short = FilesWritten {
            count: 12,
            whole: false,
        };
        let report = "ok    /work/in/a.png -> /work/out/a.png\n1 succeeded, 0 failed";
        let answer = batch_answer(report, &short, Path::new("/work/out"));
        assert_eq!(
            answer.lines().next(),
            Some("at least 12 file(s) written to /work/out (the folder could not be read in full)")
        );
        // A folder that could not be read at all says that, rather than counting a floor of nothing.
        let unread = batch_answer(
            report,
            &FilesWritten {
                count: 0,
                whole: false,
            },
            Path::new("/work/out"),
        );
        assert_eq!(
            unread.lines().next(),
            Some("the files in /work/out could not be counted")
        );
        assert_eq!(
            counted(12).written_into(Path::new("/work/out")),
            "12 file(s) written to /work/out"
        );
        assert_eq!(
            counted(0).written_into(Path::new("/work/out")),
            "no file was written into /work/out"
        );
    }

    #[test]
    fn a_failure_longer_than_the_whole_per_file_report_is_still_named() {
        // One FAIL line can be longer than all 400 per-file lines together — a deep path and a long
        // reason. It still gets the room it needs, because which file failed is what a caller acts on.
        let mut spelled: Vec<String> = (0..400)
            .map(|index| format!("ok    /work/in/photo{index}.jpg -> /work/out/photo{index}.jpg"))
            .collect();
        let reason = "x".repeat(2_600);
        spelled.push(format!("FAIL  /work/in/deep/photo.jpg: {reason}"));
        spelled.push("400 succeeded, 1 failed".to_string());
        let output = spelled.join("\n");
        let capped = batch_answer(&output, &counted(400), Path::new("/work/out"));
        assert!(
            capped.contains(&format!("FAIL  /work/in/deep/photo.jpg: {reason}")),
            "the failing file is named"
        );
        assert_eq!(capped.lines().last(), Some("400 succeeded, 1 failed"));
        assert!(capped.len() <= MAX_ANSWER_BYTES, "{}", capped.len());
        assert_eq!(crate::util::truncate_tool_output(&capped), capped);
    }

    #[test]
    fn a_wall_of_output_is_bounded_before_it_reaches_a_model() {
        // A fatal error is the last thing the vendor says, so the bound keeps a head AND a tail
        // rather than the first two kilobytes alone.
        let message = failure(
            "batch",
            &outcome(
                "exit status 1",
                &format!("{}broken.png: unreadable\n", "noise line\n".repeat(1000)),
                None,
            ),
        );
        assert!(
            message.chars().count() <= MAX_FAILURE_BYTES + 256,
            "{}",
            message.chars().count()
        );
        assert!(message.ends_with("broken.png: unreadable"), "{message}");
        assert!(
            message.contains("bytes omitted"),
            "the cut says so: {message}"
        );
    }
}