draftwright 0.6.0-alpha.3

Dimensioned engineering drawings from STEP, with SVG, PDF and editable DXF output.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
//! A live drawing over the same Spec edits and recovered planner as the batch API.
use std::collections::BTreeMap;
#[cfg(not(target_arch = "wasm32"))]
use std::path::Path;

use serde::{Deserialize, Serialize};
use serde_json::Value;
use sha2::{Digest, Sha256};

#[cfg(not(target_arch = "wasm32"))]
use crate::api::Features;
use crate::api::{self, Error};
use crate::haecceity::Part;
use crate::items::{self, Op, State as ItemState};
use crate::model::{
    self, Analysis, AnnotationItem, GeometryLayer, Plan, Projections, RenderedItem, Spec,
};
use crate::output::Drawing;
use crate::placement_intent::{Along, Place, Slot};
use crate::text::Font;

const COLLECTIONS: [&str; 14] = [
    "sheets",
    "views",
    "captions",
    "dimensions",
    "omitted",
    "gdt",
    "requirements",
    "notes",
    "tables",
    "hole_tables",
    "balloons",
    "characteristics",
    "inspection_balloons",
    "problems",
];

#[derive(Clone, Debug, Serialize, Deserialize)]
#[serde(tag = "kind", content = "geometry", rename_all = "snake_case")]
pub enum Placement {
    Dimension(crate::dims::Placed),
    Annotation(crate::dims::gdt::GdtPlaced),
    View {
        at: [f64; 2],
        area: [f64; 4],
        scale: f64,
    },
    Box([f64; 4]),
}

/// A compact answer; numbers and page geometry are resolved outputs, never Spec inputs.
#[derive(Clone, Debug, Serialize, Deserialize)]
pub struct ItemAnswer {
    pub item: items::Item,
    pub value: Option<f64>,
    pub dimension_kind: Option<String>,
    pub placement: Option<Placement>,
    pub anchors: Vec<String>,
    pub features: Vec<String>,
    pub basis: String,
    pub operations: Vec<String>,
    #[serde(default)]
    pub operation_scopes: BTreeMap<String, crate::fixes::Scope>,
}

#[derive(Clone, Debug, Default, Serialize, Deserialize)]
#[serde(default, deny_unknown_fields)]
pub struct ItemFilter {
    #[serde(skip_serializing_if = "Option::is_none")]
    pub sheet: Option<u32>,
    pub view: Option<String>,
    pub kind: Option<String>,
    pub state: Option<ItemState>,
    pub feature: Option<String>,
    /// Page millimetres, [left, bottom, right, top].
    pub region: Option<[f64; 4]>,
    pub near: Option<String>,
    /// Page millimetres around the near item's box; default 10.
    pub radius: Option<f64>,
}

#[derive(Clone, Debug, Default, Serialize, Deserialize)]
#[serde(default, deny_unknown_fields)]
pub struct ProblemFilter {
    pub code: Option<String>,
    pub severity: Option<String>,
    pub subject: Option<String>,
    pub feature: Option<String>,
}

/// Where a callout's bend sits relative to its automatic leader tip, in paper mm (y up): the
/// origin of a pinned `place.offset` (#12). `offset` is `bend - tip`; pinning it reproduces
/// the bend. To pin a dragged bend `b`, set `place` to `{offset: b - tip, pinned: true}`.
#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
pub struct CalloutOffset {
    /// The view the automatic tip is in, `v:plan` etc.
    pub view: String,
    /// The automatic leader tip.
    pub tip: [f64; 2],
    /// The callout's bend as drawn now.
    pub bend: [f64; 2],
    pub offset: [f64; 2],
    /// Projected attachment sites used by the leader, in paper mm (y up).
    #[serde(default)]
    pub members: Vec<CalloutMember>,
}

/// A callout attachment site in the current page frame.
#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
pub struct CalloutMember {
    pub centre: [f64; 2],
    pub radius: f64,
    /// Present for an arc: counterclockwise angles in radians.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub span: Option<CalloutSpan>,
}

#[derive(Clone, Copy, Debug, PartialEq, Serialize, Deserialize)]
pub struct CalloutSpan {
    pub start: f64,
    pub sweep: f64,
}

/// A linear label and its canonical anchor in paper mm (y up), in the current view frame.
/// Pin a drop at `p` with `{offset: p - anchor, pinned: true}`.
#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
pub struct LabelOffset {
    pub view: String,
    pub anchor: [f64; 2],
    pub label: [f64; 2],
    pub offset: [f64; 2],
}

#[derive(Clone, Debug, Serialize, Deserialize)]
pub struct Options {
    pub id: String,
    pub operations: Vec<String>,
    #[serde(default)]
    pub operation_scopes: BTreeMap<String, crate::fixes::Scope>,
    /// Conservative, valid placements on the current carrier. Collisions remain problems.
    pub placements: Vec<Place>,
    pub view_anchors: Vec<String>,
    pub scales: Vec<String>,
    /// Escaped Spec paths and accepted values/forms; candidates require preview/apply.
    pub settable: BTreeMap<String, Value>,
}

/// An ordered collection's changed records, keyed by stable ID. An absent order keeps it.
#[derive(Clone, Debug, Default, Serialize, Deserialize)]
pub struct CollectionDelta {
    pub set: BTreeMap<String, Value>,
    pub removed: Vec<String>,
    pub order: Option<Vec<String>>,
}

/// A lossless patch of the Plan, including non-item furniture and collection order.
#[derive(Clone, Debug, Default, Serialize, Deserialize)]
pub struct PlanDelta {
    pub fields: BTreeMap<String, Value>,
    pub collections: BTreeMap<String, CollectionDelta>,
}

#[derive(Clone, Debug, Serialize, Deserialize)]
pub struct Delta {
    /// Complete recovered-planner invocations used for this edit, including a preview.
    pub plans: usize,
    pub changed: Vec<ItemAnswer>,
    pub added: Vec<ItemAnswer>,
    pub removed: Vec<String>,
    pub problems_added: Vec<model::Problem>,
    pub problems_resolved: Vec<model::Problem>,
    /// Findings about the edit itself, rather than the reproducible drawing state.
    #[serde(default, skip_serializing_if = "Vec::is_empty")]
    pub notices: Vec<model::Problem>,
    pub plan: PlanDelta,
    /// Only changed view geometry and annotation ink. Removed IDs use `removed`.
    pub geometry: GeometryLayer,
    pub annotations: BTreeMap<String, RenderedItem>,
    pub geometry_order: Option<Vec<String>>,
    pub problem_order: Option<Vec<String>>,
}

/// A saved Spec as [`Session::open_spec`] opened it.
#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
pub struct OpenedSpec {
    pub migrated: Vec<crate::stable_ids::Migrated>,
    pub ids: BTreeMap<String, String>,
    /// The Spec applied: the session's Spec after opening.
    pub spec: Value,
    /// The entries left out, in the order they were refused.
    pub set_aside: Vec<SetAside>,
}

/// A saved Spec's entry that [`Session::open_spec`] left out.
#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
pub struct SetAside {
    /// The entry's escaped Spec path, as an [`Op`] names it (`dimensions/d:f:hole:f6~1bore.diameter`).
    pub path: String,
    /// The entry, as the Spec read in its current form has it.
    pub entry: Value,
    /// Why, decided by what the part and the opened Spec have (never by the order the entries
    /// were refused in):
    /// - `no_feature`: the feature it names is not on the part: a `features` entry, a member of
    ///   an authored group (`features/<group>/pattern/members/<i>`, the member's ID as the
    ///   entry), a dimension or measurement on a feature that is neither recognised, authored
    ///   nor declared, nor anchored by a saved entry, or a detail view around such a feature;
    /// - `no_face`: a `faces` entry, or a detail view around a face, the part does not have;
    /// - `no_dimension`, `no_measurement`, `no_annotation`;
    /// - `no_view`: a view the part does not have, or a detail view whose source view it does
    ///   not have.
    pub reason_code: String,
}

/// An entry a refusal names, as [`Session::open_spec`] sets it aside: a keyed entry, or a member
/// of an authored group (`features/<group>/pattern/members/<i>`), read as its ID before any is
/// removed.
enum Target {
    Entry(String, String),
    Member(String, Value),
}

impl Target {
    fn read(value: &Value, path: &str) -> Option<Target> {
        let segments: Vec<String> = path
            .split('/')
            .map(|k| k.replace("~1", "/").replace("~0", "~"))
            .collect();
        match segments.as_slice() {
            [collection, key] if !key.is_empty() => {
                Some(Target::Entry(collection.clone(), key.clone()))
            }
            [features, group, pattern, members, i]
                if features == "features" && pattern == "pattern" && members == "members" =>
            {
                let member = value["features"][group]["pattern"]["members"]
                    .get(i.parse::<usize>().ok()?)?
                    .clone();
                Some(Target::Member(group.clone(), member))
            }
            _ => None,
        }
    }

    /// Take this entry out of *value*, recording it in *set_aside* (its reason is decided once
    /// the Spec is open); whether it was there to take.
    fn set_aside(
        self,
        analysis: &Analysis,
        saved: &Value,
        value: &mut Value,
        set_aside: &mut Vec<SetAside>,
    ) -> bool {
        let aside = |path: String, entry: Value| SetAside {
            path,
            entry,
            reason_code: String::new(),
        };
        match self {
            Target::Entry(collection, key) => {
                let Some(entry) = value
                    .get_mut(&collection)
                    .and_then(Value::as_object_mut)
                    .and_then(|entries| entries.remove(&key))
                else {
                    return false;
                };
                set_aside.push(aside(format!("{collection}/{}", items::key(&key)), entry));
                true
            }
            Target::Member(group, member) => {
                let Some(members) = value["features"][&group]["pattern"]["members"].as_array_mut()
                else {
                    return false;
                };
                let Some(i) = members.iter().position(|m| m == &member) else {
                    return false;
                };
                members.remove(i);
                // Named by its place in the saved group.
                let saved_group = &saved["features"][&group];
                let at = saved_group["pattern"]["members"]
                    .as_array()
                    .and_then(|m| m.iter().position(|m| m == &member))
                    .unwrap_or(i);
                let group_path = format!("features/{}", items::key(&group));
                set_aside.push(aside(format!("{group_path}/pattern/members/{at}"), member));
                let holds = serde_json::from_value::<Spec>(value.clone()).is_ok_and(|s| {
                    crate::recognition_corrections::group_holds(analysis, &s, &group)
                });
                if !holds {
                    // No longer a group: the whole group is set aside, as it was saved.
                    let prefix = format!("{group_path}/pattern/members/");
                    set_aside.retain(|s| !s.path.starts_with(&prefix));
                    let Some(entry) = value["features"]
                        .as_object_mut()
                        .and_then(|f| f.remove(&group))
                    else {
                        return true;
                    };
                    let entry = if saved_group.is_null() {
                        entry
                    } else {
                        saved_group.clone()
                    };
                    set_aside.push(aside(group_path, entry));
                }
                true
            }
        }
    }
}

/// Whether *analysis* has feature or pattern *id*.
fn has_feature(analysis: &Analysis, id: &str) -> bool {
    analysis.features.iter().any(|f| f.id == id)
        || analysis
            .recognition
            .iter()
            .flat_map(|r| &r.patterns)
            .any(|p| p.id == id)
}

/// Why [`Session::open_spec`] set aside the entry at *path*, from what the part (*analysis*,
/// corrected) and the opened Spec have: *exists* answers whether a feature is there.
fn reason_code(
    analysis: &Analysis,
    path: &str,
    entry: &Value,
    exists: &dyn Fn(&str) -> bool,
) -> &'static str {
    let (collection, key) = path.split_once('/').unwrap_or((path, ""));
    let key = key.replace("~1", "/").replace("~0", "~");
    match collection {
        "dimensions" | "precision" => {
            match crate::recognition_corrections::measured_feature(&key) {
                Some(f) if !exists(f) => "no_feature",
                _ if collection == "precision" => "no_measurement",
                _ => "no_dimension",
            }
        }
        // Its own entry, or a member of an authored group.
        "features" => "no_feature",
        "faces" => "no_face",
        "annotations" => "no_annotation",
        "views" if crate::detail::NAMES.contains(&key.as_str()) => {
            // A detail view: around a feature or a face the part no longer has, else from a
            // source view it no longer has.
            let around: Vec<&str> = match &entry["detail"]["around"] {
                Value::String(id) => vec![id.as_str()],
                Value::Array(ids) => ids.iter().filter_map(Value::as_str).collect(),
                _ => Vec::new(),
            };
            if around.iter().any(|m| m.starts_with("f:") && !exists(m)) {
                "no_feature"
            } else if around
                .iter()
                .any(|m| !m.starts_with("f:") && crate::spec_gdt::named_faces(analysis, m).is_err())
            {
                "no_face"
            } else {
                "no_view"
            }
        }
        "views" => "no_view",
        _ => "no_entry",
    }
}

/// The client-replayable state. Its render reuses the supplied geometry and annotation ink.
#[derive(Clone, Debug, Serialize, Deserialize)]
pub struct State {
    pub plan: Plan,
    pub geometry: GeometryLayer,
    pub annotations: BTreeMap<String, RenderedItem>,
    pub problems: Vec<model::Problem>,
}
impl State {
    pub fn render(
        &self,
        part: &Part,
        analysis: &Analysis,
        font: &Font,
        cache: &mut Projections,
    ) -> Result<Drawing, Error> {
        self.render_sheet(part, analysis, font, cache, 1)
    }
    pub fn render_sheet(
        &self,
        part: &Part,
        analysis: &Analysis,
        font: &Font,
        cache: &mut Projections,
        sheet: u32,
    ) -> Result<Drawing, Error> {
        self.check_sheet(sheet)?;
        model::render_annotations_on_sheet(
            part,
            analysis,
            &self.plan,
            font,
            self.geometry.clone(),
            cache,
            &self.annotations,
            sheet,
        )
        .map_err(|e| Error::failed(format!("cannot render session: {e}")))
    }
    pub fn check_sheet(&self, sheet: u32) -> Result<(), Error> {
        if sheet == 1 || self.plan.sheets.iter().any(|s| s.number == sheet) {
            return Ok(());
        }
        Err(Error {
            choices: std::iter::once(&self.plan.sheet)
                .chain(&self.plan.sheets)
                .map(|s| s.number.to_string())
                .collect(),
            ..Error::invalid(format!("there is no sheet {sheet}"))
        })
    }
}

fn document(p: &Plan) -> Value {
    // Invariant: Document fields and map keys have JSON representations; serialization performs no external I/O.
    serde_json::to_value(p).expect("a plan serialises")
}
fn record_id(v: &Value) -> String {
    v["id"].as_str().map(String::from).unwrap_or_else(|| {
        format!(
            "sheet:{}",
            // Invariant: record_id sees only plan collections; records without id are PlannedSheet with a u32 number.
            v["number"].as_u64().expect("a sheet has a number")
        )
    })
}
fn records(v: &Value) -> BTreeMap<String, Value> {
    v.as_array()
        .into_iter()
        .flatten()
        .map(|v| (record_id(v), v.clone()))
        .collect()
}
fn order(v: &Value) -> Vec<String> {
    v.as_array().into_iter().flatten().map(record_id).collect()
}
fn patch(a: &Value, b: &Value) -> PlanDelta {
    let mut delta = PlanDelta::default();
    for name in COLLECTIONS {
        if a[name] == b[name] {
            continue;
        }
        let (old, new) = (records(&a[name]), records(&b[name]));
        delta.collections.insert(
            name.into(),
            CollectionDelta {
                set: new
                    .iter()
                    .filter(|(id, v)| old.get(*id) != Some(*v))
                    .map(|(id, v)| (id.clone(), v.clone()))
                    .collect(),
                removed: old
                    .keys()
                    .filter(|id| !new.contains_key(*id))
                    .cloned()
                    .collect(),
                order: (order(&a[name]) != order(&b[name])).then(|| order(&b[name])),
            },
        );
    }
    for name in a
        .as_object()
        // Invariant: document serializes a Plan struct as an object.
        .unwrap()
        .keys()
        // Invariant: document serializes a Plan struct as an object.
        .chain(b.as_object().unwrap().keys())
    {
        if !COLLECTIONS.contains(&name.as_str()) && a[name] != b[name] {
            delta.fields.insert(name.clone(), b[name].clone());
        }
    }
    delta
}
impl Delta {
    /// Applies a server delta without planning or formatting an annotation.
    pub fn apply_to(&self, state: &mut State) -> Result<(), Error> {
        let mut doc = document(&state.plan);
        for (name, value) in &self.plan.fields {
            if value.is_null() {
                // Invariant: document serializes a Plan struct as an object; field removals preserve that shape.
                doc.as_object_mut().unwrap().remove(name);
            } else {
                doc[name] = value.clone();
            }
        }
        for (name, delta) in &self.plan.collections {
            let mut entries = records(&doc[name]);
            let ids = delta.order.clone().unwrap_or_else(|| order(&doc[name]));
            for id in &delta.removed {
                entries.remove(id);
            }
            entries.extend(delta.set.clone());
            doc[name] = Value::Array(ids.iter().filter_map(|id| entries.remove(id)).collect());
            if !entries.is_empty() {
                return Err(Error::invalid("delta has records missing from its order"));
            }
        }
        let plan: Plan = serde_json::from_value(doc)
            .map_err(|e| Error::invalid(format!("invalid plan delta: {e}")))?;
        let mut geometry: BTreeMap<_, _> = state.geometry.views.iter().cloned().collect();
        let ids = self.geometry_order.clone().unwrap_or_else(|| {
            state
                .geometry
                .views
                .iter()
                .map(|(id, _)| id.clone())
                .collect()
        });
        for id in &self.removed {
            geometry.remove(id);
        }
        geometry.extend(self.geometry.views.iter().cloned());
        let views = ids
            .into_iter()
            .map(|id| {
                geometry
                    .remove(&id)
                    .map(|v| (id, v))
                    .ok_or_else(|| Error::invalid("delta is missing view geometry"))
            })
            .collect::<Result<_, _>>()?;
        let mut annotations = state.annotations.clone();
        // A shown-to-omitted transition keeps the item ID but removes its ink.
        for id in self.removed.iter().chain(
            self.changed
                .iter()
                .filter(|i| {
                    // Covered dimensions retain an empty fragment in State; a changed note
                    // owner must not discard it when its geometry is still unchanged.
                    !self.annotations.contains_key(&i.item.id)
                        && !plan.dimensions.iter().any(|d| d.id == i.item.id)
                        && !plan.gdt.iter().any(|g| g.id == i.item.id)
                })
                .map(|i| &i.item.id),
        ) {
            annotations.remove(id);
        }
        annotations.extend(self.annotations.clone());
        let mut problems = state.problems.clone();
        problems.retain(|p| !self.problems_resolved.contains(p));
        problems.extend(self.problems_added.clone());
        // A changed remedy is metadata for the same finding. Its complete update
        // travels in PlanDelta, without a false resolved/new warning pair.
        for problem in &mut problems {
            if let Some(updated) = plan.problems.iter().find(|p| same_finding(p, problem)) {
                *problem = updated.clone();
            }
        }
        // Plan problems keep planner order; render-only problems follow them.
        let plan: Plan = plan;
        problems.sort_by_key(|p| {
            if let Some(order) = &self.problem_order {
                return order
                    .iter()
                    .position(|id| id == &p.id)
                    .unwrap_or(usize::MAX);
            }
            plan.problems
                .iter()
                .position(|q| q == p)
                .unwrap_or(usize::MAX)
        });
        state.plan = plan;
        state.geometry.views = views;
        state.annotations = annotations;
        state.problems = problems;
        Ok(())
    }
}

#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Serialize)]
pub struct Stats {
    /// Complete recovered-planner invocations, excluding previews and queries.
    pub plans: usize,
    /// Actual recovered sheet-1 runs, summed across committed planner invocations.
    pub sheet_steps: usize,
    pub projections: usize,
    pub bounds: usize,
    pub annotations: usize,
}

#[derive(Clone, Default)]
struct Planner {
    calls: usize,
    sheet_steps: usize,
}
impl Planner {
    fn plan(&mut self, analysis: &Analysis, spec: &Spec) -> Result<Plan, Error> {
        self.calls += 1;
        Ok(crate::planning::with_retained(analysis, || {
            crate::sheets::count_runs(&mut self.sheet_steps, || {
                model::plan_recovered(analysis, spec)
            })
        })?)
    }
}
struct Evaluation {
    state: State,
    drawing: Drawing,
    projections: Projections,
    planner: Planner,
    rendered: usize,
}

pub struct Session {
    part: Part,
    analysis: Analysis,
    state: State,
    drawing: Drawing,
    font: Font,
    projections: Projections,
    undo: Vec<Spec>,
    redo: Vec<Spec>,
    stats: Stats,
    planner: Planner,
    render_memo: crate::planning::BoundedMemo<Drawing>,
}
impl Session {
    #[cfg(not(target_arch = "wasm32"))]
    pub fn open(step: &Path, features: Features<'_>, spec: Spec) -> Result<Self, Error> {
        Self::open_part(step, None, features, spec)
    }
    /// [`Session::open`] of part *part* of an assembly alone, when given
    /// ([`crate::model::analyse_step_part`]).
    #[cfg(not(target_arch = "wasm32"))]
    pub fn open_part(
        step: &Path,
        part: Option<usize>,
        features: Features<'_>,
        spec: Spec,
    ) -> Result<Self, Error> {
        let (part, analysis) =
            api::analysed(step, api::selected_part(step, part, &spec)?, features)?;
        Self::new(part, analysis, spec)
    }
    pub fn new(part: Part, analysis: Analysis, spec: Spec) -> Result<Self, Error> {
        Self::with_projections(part, analysis, spec, Projections::default())
    }
    /// Reconstruct a session from immutable documents and a persisted projection cache.
    pub fn with_projections(
        part: Part,
        analysis: Analysis,
        spec: Spec,
        mut projections: Projections,
    ) -> Result<Self, Error> {
        // Invariant: Font bytes are bundled at build time and parsed unchanged, independently of user input.
        let font = Font::new(api::MONO.to_vec()).expect("the bundled font parses");
        let mut planner = Planner::default();
        let plan = planner.plan(&analysis, &spec)?;
        let geometry = model::render_geometry(&part, &analysis, &plan, &mut projections)
            .map_err(|e| Error::failed(format!("cannot render session: {e}")))?;
        let annotations = ink(&plan, None);
        let mut state = State {
            problems: plan.problems.clone(),
            plan,
            geometry,
            annotations,
        };
        let drawing = state.render(&part, &analysis, &font, &mut projections)?;
        state.problems.extend(drawing.problems.clone());
        let stats = Stats {
            annotations: state.annotations.len(),
            ..Default::default()
        };
        Ok(Self {
            part,
            analysis,
            state,
            drawing,
            font,
            projections,
            undo: Vec::new(),
            redo: Vec::new(),
            stats,
            planner,
            render_memo: crate::planning::BoundedMemo::new(1),
        })
    }
    pub fn projections(&self) -> &Projections {
        &self.projections
    }
    pub fn part(&self) -> &Part {
        &self.part
    }
    pub fn analysis(&self) -> &Analysis {
        &self.analysis
    }
    pub fn spec(&self) -> &Spec {
        &self.state.plan.spec
    }
    pub fn plan(&self) -> &Plan {
        &self.state.plan
    }
    /// Exterior elbow rows for pinning a callout in each compatible principal view.
    /// Reads the current frames and canonical widths without planning or projecting.
    pub fn label_rows(
        &self,
        address: &str,
    ) -> Result<BTreeMap<String, crate::dims::LabelRows>, Error> {
        let item = self.item(address)?;
        if item.dimension_kind.as_deref() != Some("callout") {
            return Err(Error::invalid("label rows require a callout"));
        }
        let d = self
            .plan()
            .dimensions
            .iter()
            .find(|d| d.id == item.item.id)
            // Invariant: item derives dimension_kind from the dimension with this same id in this immutable plan.
            .unwrap();
        let ink = crate::dims::label_ink(&d.terms, crate::dims::font());
        let width = ink.max[0] - ink.min[0];
        let options = self.options(address)?;
        // Rows follow the spacing the drawing was planned with.
        let compact = self.plan().sheet.dimension_spacing == model::DimensionSpacing::Compact;
        Ok(crate::dims::with_compact(compact, || {
            self.plan()
                .views
                .iter()
                .filter(|v| {
                    options
                        .placements
                        .iter()
                        .any(|p| p.view.as_deref() == v.id.strip_prefix("v:"))
                })
                .map(|v| {
                    let width =
                        width.max(self.plan().canonical_callout_width(&v.id).unwrap_or(0.0));
                    (
                        v.id.clone(),
                        crate::dims::label_rows(&self.analysis, v, width),
                    )
                })
                .collect()
        }))
    }
    /// The callout's bend against its automatic leader tip, for pinning a drag with
    /// `place.offset`, with its projected attachment members. Does not plan or project model
    /// edges. Refuses a
    /// non-callout, or a callout with no automatic placement or not drawn in that view.
    pub fn callout_offset(&self, address: &str) -> Result<CalloutOffset, Error> {
        let item = self.item(address)?;
        if item.dimension_kind.as_deref() != Some("callout") {
            return Err(Error::invalid("a callout offset requires a callout"));
        }
        let id = item.item.id.as_str();
        let Some((view, tip)) = self.plan().automatic_callout_tip(id) else {
            return Err(Error::invalid(format!(
                "{id} has no automatic placement to measure an offset from"
            )));
        };
        let Some(bend) = self
            .plan()
            .dimensions
            .iter()
            .find(|d| d.id == id && d.view == view)
            .and_then(|d| Some(d.placed.as_ref()?.lines.first()?[1]))
        else {
            return Err(Error::invalid(format!("{id} is not drawn in {view}")));
        };
        Ok(CalloutOffset {
            view: view.into(),
            tip,
            bend,
            offset: [bend[0] - tip[0], bend[1] - tip[1]],
            members: crate::dims::callout_members(
                &self.analysis,
                self.spec(),
                self.plan(),
                id,
                view,
            ),
        })
    }
    /// The linear label's displacement from its canonical position, without replanning.
    /// Refuses non-linear items, missing automatic placements and other carrier views.
    pub fn label_offset(&self, address: &str) -> Result<LabelOffset, Error> {
        let item = self.item(address)?;
        if item.dimension_kind.as_deref() != Some("linear") {
            return Err(Error::invalid("a label offset requires a linear dimension"));
        }
        let id = item.item.id.as_str();
        let (view, anchor) = self.plan().automatic_dimension_label(id).ok_or_else(|| {
            Error::invalid(format!(
                "{id} has no automatic placement to measure an offset from"
            ))
        })?;
        let label = self
            .plan()
            .dimensions
            .iter()
            .find(|d| d.id == id && d.view == view)
            .and_then(|d| d.placed.as_ref().map(|p| p.label_at))
            .ok_or_else(|| Error::invalid(format!("{id} is not drawn in {view}")))?;
        Ok(LabelOffset {
            view: view.into(),
            anchor,
            label,
            offset: [label[0] - anchor[0], label[1] - anchor[1]],
        })
    }

    pub fn state(&self) -> &State {
        &self.state
    }
    pub fn drawing(&self) -> &Drawing {
        &self.drawing
    }
    pub fn stats(&self) -> Stats {
        let p = self.projections.stats();
        Stats {
            plans: self.planner.calls,
            sheet_steps: self.planner.sheet_steps,
            projections: p.projections,
            bounds: p.bounds,
            ..self.stats
        }
    }
    /// Validation, one complete plan, then changed annotation ink; commit only after success.
    pub fn apply(&mut self, ops: &[Op], commit: bool) -> Result<Delta, Error> {
        let spec = model::edit(
            self.spec(),
            // Invariant: Document fields and map keys have JSON representations; serialization performs no external I/O.
            &serde_json::to_value(ops).expect("ops serialise"),
        )?;
        let evaluated = self.evaluate(&spec)?;
        if spec.sheet.keep_layout == Some(true) {
            crate::local_layout::verify(self.plan(), &evaluated.state.plan)?;
        }
        let delta = difference(
            &self.state,
            &evaluated.state,
            &self.analysis,
            evaluated.planner.calls - self.planner.calls,
        );
        if commit {
            self.undo.push(self.spec().clone());
            self.redo.clear();
            self.install(evaluated);
        }
        Ok(delta)
    }
    /// Replace the Spec with *spec* (read in its current form, [`model::migrate`]), as one
    /// committed edit of the top-level fields that differ; refused as [`Session::apply`] refuses.
    pub fn set_spec(&mut self, spec: &Spec) -> Result<(), Error> {
        // Unlike open_spec, a spec/3 is not scanned for rank keys: it has already been opened.
        let spec = model::migrate(spec, self.analysis())?;
        // Invariant: Document fields and map keys have JSON representations; serialization performs no external I/O.
        let old = serde_json::to_value(self.spec()).expect("spec serialises");
        // Invariant: Document fields and map keys have JSON representations; serialization performs no external I/O.
        let new = serde_json::to_value(spec).expect("spec serialises");
        let mut ops = Vec::new();
        // Invariant: Spec derives serialization as an object.
        for (key, value) in new.as_object().expect("spec is an object") {
            if old.get(key) != Some(value) {
                ops.push(Op::set(key, value.clone()));
            }
        }
        // Invariant: Spec derives serialization as an object.
        for key in old.as_object().expect("spec is an object").keys() {
            if new.get(key).is_none() {
                ops.push(Op::remove(key));
            }
        }
        if !ops.is_empty() {
            self.apply(&ops, true)?;
        }
        Ok(())
    }
    /// Open a saved Spec (JSON), which may have been made for an earlier draftwright or
    /// recognition: it is read in its current form (`sheet/preferred_printer` as
    /// `sheet/preferred_max_size`, a `draftwright.spec/1` as `/2`). Stale edits that can be kept
    /// remain in the Spec with warnings. Entries that validation still refuses are set aside,
    /// with the entry and why, and the rest applied as [`Session::set_spec`] applies it. A
    /// member an authored group names that the part no longer has is taken out of the group
    /// alone, unless the group is then no longer one, when the whole group is set aside. Any
    /// other refusal refuses the whole Spec, and the session is unchanged.
    ///
    /// Each refusal names every entry its check found stale (`model::Invalid::also`), so a Spec
    /// with many stale entries costs about one plan per kind of check refusing, not one per entry.
    pub fn open_spec(&mut self, json: &str) -> Result<OpenedSpec, Error> {
        let invalid = |e: serde_json::Error| Error::invalid(format!("invalid spec: {e}"));
        let mut value: Value = serde_json::from_str(json).map_err(invalid)?;
        // draftwright alpha.11 refuses `preferred_printer`; its A4 or A3 is the same
        // preferred_max_size.
        if let Some(sheet) = value.get_mut("sheet").and_then(Value::as_object_mut)
            && let Some(printer) = sheet.remove("preferred_printer")
        {
            sheet.entry("preferred_max_size").or_insert(printer);
        }
        let spec: Spec = serde_json::from_value(value).map_err(invalid)?;
        // Invariant: Document fields and map keys have JSON representations; serialization performs no external I/O.
        let migration = crate::stable_ids::migrate(&spec, self.analysis())?;
        let mut value = serde_json::to_value(&migration.spec).expect("spec serialises");
        let saved = value.clone();
        let mut set_aside = Vec::new();
        loop {
            let spec: Spec = serde_json::from_value(value.clone()).map_err(invalid)?;
            let refused = match self.set_spec(&spec) {
                Ok(()) => break,
                Err(e) => e,
            };
            // Each pass removes at least one entry, so this ends.
            let targets: Vec<Target> = refused
                .path
                .iter()
                .chain(&refused.also)
                .filter_map(|path| Target::read(&value, path))
                .collect();
            let mut removed = false;
            for target in targets {
                removed |= target.set_aside(&self.analysis, &saved, &mut value, &mut set_aside);
            }
            if !removed {
                return Err(refused);
            }
        }
        // Why, decided by what the part and the opened Spec have, not by the order of refusals.
        let corrected = crate::recognition_corrections::effective(&self.analysis, self.spec()).ok();
        let analysis = corrected.as_deref().unwrap_or(&self.analysis);
        let spec = self.spec();
        let exists = |id: &str| {
            has_feature(analysis, id)
                || spec
                    .features
                    .get(id)
                    .is_some_and(|f| f.anchor.is_some() || f.pattern.is_some())
                || spec.feature_declarations.iter().any(|d| d.id == id)
        };
        for s in &mut set_aside {
            s.reason_code = reason_code(analysis, &s.path, &s.entry, &exists).into();
        }
        migration.report(&mut self.state.plan);
        self.state.problems = self.state.plan.problems.clone();
        Ok(OpenedSpec {
            migrated: migration.migrated,
            ids: self.state.plan.ids.clone(),
            // Invariant: Document fields and map keys have JSON representations; serialization performs no external I/O.
            spec: serde_json::to_value(self.spec()).expect("spec serialises"),
            set_aside,
        })
    }
    /// Replace PMI against the original geometry, transactionally. Geometry caches survive.
    pub fn set_pmi(&mut self, bytes: &[u8]) -> Result<Delta, Error> {
        self.set_pmi_ops(bytes, &[])
    }
    /// Install PMI and its accompanying spec edits as one transaction and one plan.
    pub fn set_pmi_ops(&mut self, bytes: &[u8], ops: &[Op]) -> Result<Delta, Error> {
        let spec = model::edit(
            self.spec(),
            // Invariant: Document fields and map keys have JSON representations; serialization performs no external I/O.
            &serde_json::to_value(ops).expect("ops serialise"),
        )?;
        // A part of an assembly read alone takes its own PMI only.
        let reading = crate::pmi::read_part(bytes, &self.part, self.analysis.binding.part_index)
            .map_err(Error::invalid)?;
        let previous = self
            .analysis
            .pmi
            .replace(crate::pmi::Reading::Read(reading));
        let evaluated = match self.evaluate(&spec) {
            Ok(value) => value,
            Err(error) => {
                self.analysis.pmi = previous;
                return Err(error);
            }
        };
        let delta = difference(&self.state, &evaluated.state, &self.analysis, 1);
        self.install(evaluated);
        self.undo.clear();
        self.redo.clear();
        Ok(delta)
    }
    /// SVG fragments in sheet coordinates, sharing the full renderer's styles and text.
    pub fn svg_fragments(&self, delta: Option<&Delta>) -> BTreeMap<String, String> {
        self.svg_fragments_on_sheet(delta, 1)
            // Invariant: Every successfully constructed session has sheet 1.
            .expect("sheet 1 exists")
    }
    pub fn drawing_on_sheet(&self, sheet: u32) -> Result<Drawing, Error> {
        if sheet == 1 {
            return Ok(self.drawing.clone());
        }
        self.state.render_sheet(
            &self.part,
            &self.analysis,
            &self.font,
            &mut self.projections.clone(),
            sheet,
        )
    }
    pub fn svg_fragments_on_sheet(
        &self,
        delta: Option<&Delta>,
        sheet: u32,
    ) -> Result<BTreeMap<String, String>, Error> {
        self.state.check_sheet(sheet)?;
        let selected = (sheet != 1)
            .then(|| self.drawing_on_sheet(sheet))
            .transpose()?;
        let drawing = selected.as_ref().unwrap_or(&self.drawing);
        let mut out = BTreeMap::new();
        let on_sheet = |id: &str| {
            self.plan()
                .views
                .iter()
                .any(|v| v.id == id && v.sheet == sheet)
        };
        let owners = items::items(self.plan());
        let annotation_on_sheet = |id: &str| owners.iter().any(|i| i.id == id && i.sheet == sheet);
        let blank = || Drawing {
            page: drawing.page,
            layers: drawing
                .layers
                .iter()
                .filter(|l| !l.name.starts_with("shaded-"))
                .map(|l| crate::output::Layer {
                    name: l.name.clone(),
                    style: l.style.clone(),
                    items: Vec::new(),
                })
                .collect(),
            link: None,
            text: Vec::new(),
            annotations: Vec::new(),
            sheet: crate::output::Sheet::default(),
            problems: Vec::new(),
            spec: None,
        };
        // A thread's or knurl's thin lines move with their view, so they travel in its fragment.
        let thin = drawing.layers.iter().position(|l| l.name == "thin");
        let manufacturing = thin.map(|_| crate::pmi_lowering::manufacturing(self.analysis()));
        let thin_ink = |id: &str| -> Vec<crate::output::Item> {
            let (Some(m), Some(v)) = (
                &manufacturing,
                self.plan().views.iter().find(|v| v.id == id),
            ) else {
                return Vec::new();
            };
            if crate::shaded::enabled(self.spec(), id) {
                return Vec::new();
            }
            crate::cosmetic::ink(self.analysis(), m, v)
                .into_iter()
                .map(crate::output::Item::Edge)
                .collect()
        };
        for (id, geometry) in &self.state.geometry.views {
            if !on_sheet(id)
                || delta.is_some_and(|d| !d.geometry.views.iter().any(|(v, _)| v == id))
            {
                continue;
            }
            let mut d = blank();
            d.layers[0].items = geometry.visible.clone();
            d.layers[1].items = geometry.hidden.clone();
            if let Some(k) = thin {
                d.layers[k].items = thin_ink(id);
            }
            d.layers.extend(crate::shaded::layers(id, &geometry.shaded));
            out.insert(id.clone(), d.svg());
        }
        for (id, item) in &self.state.annotations {
            if !annotation_on_sheet(id) || delta.is_some_and(|d| !d.annotations.contains_key(id)) {
                continue;
            }
            let mut d = blank();
            d.layers[2].items = item.items.clone();
            d.text = item.text.clone();
            out.insert(id.clone(), d.svg());
        }
        // Planned captions participate in the furniture collection diff, including removal.
        let furniture_changed = delta.is_none_or(|d| {
            d.plan.fields.keys().any(|k| k != "spec")
                || d.plan.collections.contains_key("views")
                || d.plan.collections.keys().any(|k| {
                    !matches!(
                        k.as_str(),
                        "dimensions" | "gdt" | "omitted" | "views" | "problems"
                    )
                })
        });
        if furniture_changed {
            let mut d = drawing.clone();
            let visible: usize = self
                .state
                .geometry
                .views
                .iter()
                .filter(|(id, _)| on_sheet(id))
                .map(|(_, g)| g.visible.len())
                .sum();
            let hidden: usize = self
                .state
                .geometry
                .views
                .iter()
                .filter(|(id, _)| on_sheet(id))
                .map(|(_, g)| g.hidden.len())
                .sum();
            let ink: usize = self
                .state
                .annotations
                .iter()
                .filter(|(id, _)| annotation_on_sheet(id))
                .map(|(_, a)| a.items.len())
                .sum();
            let text: usize = self
                .state
                .annotations
                .iter()
                .filter(|(id, _)| annotation_on_sheet(id))
                .map(|(_, a)| a.text.len())
                .sum();
            d.layers.retain(|l| !l.name.starts_with("shaded-"));
            d.layers[0].items.drain(..visible);
            d.layers[1].items.drain(..hidden);
            d.layers[2].items.drain(..ink);
            if let Some(k) = thin {
                d.layers[k].items.clear();
            }
            d.text.drain(..text);
            out.insert("furniture".into(), d.svg());
        }
        Ok(out)
    }
    pub fn pmi_report(&self) -> Value {
        let mut lowered = Vec::new();
        let mut unresolved = Vec::new();
        // Include Spec-authored datums and controls, exactly as the planner reads them.
        // A session already validated this Spec against its Analysis.
        let effective = crate::spec_gdt::effective(self.analysis(), self.spec())
            .expect("the session has a validated Spec");
        if let Some(crate::pmi::Reading::Read(report)) = &effective.pmi {
            let identities = report.identities();
            for record in &report.records {
                // A Specify general-tolerance dimension is drawn by the planners' item that
                // conveys it, or as draftwright's own planned dimension, neither of which
                // claims it (#265): either lowers it.
                let general = crate::pmi::specify_general(report, record);
                // The items that state it: one claiming it, or for a Specify general dimension
                // the planned item made for it or one that conveys it, neither of which claims it
                // (#265). One that is drawn lowers it; one not drawn because another item states
                // what it would (a hole callout the hole table replaces) lowers it to that item.
                let ids: Vec<_> = self
                    .plan()
                    .dimensions
                    .iter()
                    .filter(|d| {
                        identities.id(&record.source_id).is_ok_and(|id| {
                            d.measurements.iter().any(|m| {
                                m.feature == id
                                    || general
                                        && m.feature
                                            == crate::dims::pmi_dimensions::general_feature(id)
                            }) || general
                                && crate::dims::pmi_dimensions::conveys(&d.basis, &record.source_id)
                        })
                    })
                    .filter_map(|d| {
                        let kind = d
                            .measurements
                            .iter()
                            .find(|m| matches!(m.parameter.as_str(), "thread" | "knurl"))
                            .map(|m| m.parameter.clone())
                            .unwrap_or_else(|| d.kind.clone());
                        match (&d.placed, &d.state) {
                            (Some(_), _) => Some((d.id.clone(), d.label.clone(), kind)),
                            (None, crate::items::State::Covered { by }) => {
                                Some((by.clone(), d.label.clone(), "covered".to_string()))
                            }
                            _ => None,
                        }
                    })
                    // A planners' item that conveys it but is itself left out because other
                    // stated dimensions determine it still states that size on the sheet.
                    .chain(
                        self.plan()
                            .omitted
                            .iter()
                            .filter(|o| {
                                general
                                    && matches!(o.state, crate::items::State::Covered { .. })
                                    && crate::dims::pmi_dimensions::conveys(
                                        &o.basis,
                                        &record.source_id,
                                    )
                            })
                            .map(|o| (o.id.clone(), o.label.clone(), "covered".to_string())),
                    )
                    .chain(
                        self.plan()
                            .gdt
                            .iter()
                            .filter(|g| g.source == record.source_id && g.placed.is_some())
                            .map(|g| (g.id.clone(), g.label.clone(), g.kind.clone())),
                    )
                    .chain(
                        self.plan()
                            .requirements
                            .iter()
                            .filter(|r| {
                                r.source == record.source_id
                                    && matches!(r.state, crate::items::State::Shown)
                            })
                            .map(|r| (r.id.clone(), r.text.clone(), r.kind.clone())),
                    )
                    .collect();
                if ids.is_empty() {
                    let related = identities.id(&record.source_id).ok();
                    let reasons: Vec<_> = self
                        .state
                        .problems
                        .iter()
                        .filter(|p| {
                            (p.code == "pmi_ignored"
                                && !record.source_id.starts_with(crate::spec_gdt::SOURCE_PREFIX))
                                || p.refs.contains(&record.source_id)
                                || related.is_some_and(|id| p.subjects.iter().any(|s| s == id))
                        })
                        .map(|p| p.message.as_str())
                        .collect();
                    let reason = if let Some(reason) = crate::spec_gdt::datum_omission(
                        &self.plan().spec,
                        report,
                        record,
                        &crate::dims::pmi_policy::origin_datums(&self.analysis, &self.plan().spec),
                    ) {
                        reason.into()
                    } else if !reasons.is_empty() {
                        reasons.join("; ")
                    } else if !record.blockers.is_empty() {
                        record.blockers.join("; ")
                    } else if let Some(g) = &record.geometry
                        && !g.reasons.is_empty()
                    {
                        g.reasons.join("; ")
                    } else if let Some(item) = self
                        .plan()
                        .gdt
                        .iter()
                        .find(|g| g.source == record.source_id)
                    {
                        format!("Annotation is not drawn ({:?}): {}", item.state, item.basis)
                    } else if crate::pmi_lowering::no_part_requirement(record) {
                        "No drawing requirement is stated by this record".into()
                    } else {
                        format!(
                            "{} has no supported drawing representation ({})",
                            record.kind, record.source_id
                        )
                    };
                    let reason = if reason.trim().is_empty() {
                        format!(
                            "{} ({}) has no supported drawing representation",
                            record.kind, record.source_id
                        )
                    } else {
                        reason
                    };
                    unresolved.push(serde_json::json!({"source_id":record.source_id,"label":record.label,"kind":record.kind,"reason":reason}));
                }
                for (feature_id, label, kind) in ids {
                    lowered.push(serde_json::json!({"source_id":record.source_id,"feature_id":feature_id,"label":label,"kind":kind,"fit":record.fit}));
                }
            }
            if let Some(material) = report.title_material()
                && self
                    .plan()
                    .sheet
                    .title
                    .get("material")
                    .is_some_and(|m| m == material.designation.trim())
            {
                lowered.push(serde_json::json!({"source_id":material.census_id(),"feature_id":"part","label":material.designation.trim(),"kind":"material"}));
            }
            for source in &report.sources {
                if source.outcome == crate::pmi::Outcome::NotExtracted
                    && !lowered.iter().any(|r| r["source_id"] == source.id)
                {
                    unresolved.push(serde_json::json!({"source_id":source.id,"label":source.entity,"kind":source.category,"reason":if source.reason.trim().is_empty() { "STEP source could not be extracted into a supported annotation" } else { &source.reason }}));
                }
            }
            serde_json::json!({"extraction":report,"lowered":lowered,"unresolved":unresolved})
        } else {
            serde_json::json!({"lowered":[],"unresolved":[]})
        }
    }
    pub fn undo(&mut self) -> Result<Delta, Error> {
        self.history(false)
    }
    pub fn redo(&mut self) -> Result<Delta, Error> {
        self.history(true)
    }
    fn history(&mut self, redo: bool) -> Result<Delta, Error> {
        let spec = if redo {
            self.redo.last()
        } else {
            self.undo.last()
        }
        .ok_or_else(|| {
            Error::invalid(if redo {
                "nothing to redo"
            } else {
                "nothing to undo"
            })
        })?
        .clone();
        let evaluated = self.evaluate(&spec)?;
        let delta = difference(
            &self.state,
            &evaluated.state,
            &self.analysis,
            evaluated.planner.calls - self.planner.calls,
        );
        let current = self.spec().clone();
        if redo {
            self.redo.pop();
            self.undo.push(current);
        } else {
            self.undo.pop();
            self.redo.push(current);
        }
        self.install(evaluated);
        Ok(delta)
    }
    fn evaluate(&self, spec: &Spec) -> Result<Evaluation, Error> {
        let mut planner = self.planner.clone();
        let plan = planner.plan(&self.analysis, spec)?;
        let mut cache = self.projections.clone();
        let geometry = model::render_geometry(&self.part, &self.analysis, &plan, &mut cache)
            .map_err(|e| Error::failed(format!("cannot render session: {e}")))?;
        let annotations = ink(&plan, Some(&self.state));
        let rendered = annotations
            .keys()
            .filter(|id| annotation_changed(&self.state.plan, &plan, id))
            .count();
        let mut state = State {
            problems: plan.problems.clone(),
            plan,
            geometry,
            annotations,
        };
        // Part, font and recognized geometry are fixed for this Session; PMI and
        // Spec are its mutable rendering inputs. Reuse an exact preview's assembled
        // drawing, still planning, placing geometry and computing ink/deltas each time.
        let key: [u8; 32] = Sha256::digest(
            // Invariant: Document fields and map keys have JSON representations; serialization performs no external I/O.
            serde_json::to_vec(&(spec, &self.analysis.pmi)).expect("render inputs serialize"),
        )
        .into();
        let drawing = if let Some(drawing) = self.render_memo.get(&key) {
            drawing
        } else {
            let drawing = state.render(&self.part, &self.analysis, &self.font, &mut cache)?;
            self.render_memo.insert(key, drawing.clone());
            drawing
        };
        state.problems.extend(drawing.problems.clone());
        Ok(Evaluation {
            state,
            drawing,
            projections: cache,
            planner,
            rendered,
        })
    }
    fn install(&mut self, evaluated: Evaluation) {
        self.state = evaluated.state;
        self.drawing = evaluated.drawing;
        self.projections = evaluated.projections;
        self.planner = evaluated.planner;
        self.stats.annotations += evaluated.rendered;
    }
    pub fn item(&self, address: &str) -> Result<ItemAnswer, Error> {
        let (sheet, id) = item_address(address)?;
        let item = self.bare_item(id)?;
        if sheet.is_some_and(|n| n != item.item.sheet) {
            return Err(Error {
                choices: vec![format!("sheet:{}/{}", item.item.sheet, id)],
                ..Error::invalid(format!("{id} is on sheet {}", item.item.sheet))
            });
        }
        Ok(item)
    }
    fn bare_item(&self, id: &str) -> Result<ItemAnswer, Error> {
        answers(self.plan(), &self.analysis)
            .into_iter()
            .find(|i| i.item.id == id)
            .ok_or_else(|| {
                let mut choices: Vec<_> = items::items(self.plan())
                    .into_iter()
                    .map(|i| i.id)
                    .collect();
                choices
                    .sort_by_key(|choice| (crate::dims::levenshtein(id, choice), choice.clone()));
                choices.truncate(5);
                Error {
                    choices,
                    ..Error::invalid(format!("unknown item {id:?}"))
                }
            })
    }
    /// File-owned inspection numbers and their resolved sheet and item.
    pub fn numbered_characteristics(&self) -> &[crate::characteristics::Characteristic] {
        crate::characteristics::list(self.plan())
    }
    pub fn items(&self, filter: &ItemFilter) -> Vec<ItemAnswer> {
        let near = filter
            .near
            .as_ref()
            .and_then(|id| self.item(id).ok())
            .and_then(|i| item_bounds(&i).map(|bounds| (i.item.sheet, bounds)));
        let region = filter.region.or_else(|| {
            near.map(|(_, [a, b, c, d])| {
                let r = filter.radius.unwrap_or(10.0).max(0.0);
                [a - r, b - r, c + r, d + r]
            })
        });
        answers(self.plan(), &self.analysis)
            .into_iter()
            .filter(|i| {
                filter.sheet.is_none_or(|n| i.item.sheet == n)
                    && near.is_none_or(|(n, _)| i.item.sheet == n)
                    && filter.view.as_ref().is_none_or(|v| {
                        i.item.view.as_ref().is_some_and(|w| {
                            w.trim_start_matches("v:") == v.trim_start_matches("v:")
                        })
                    })
                    && filter
                        .kind
                        .as_ref()
                        .is_none_or(|k| &i.item.kind == k || i.dimension_kind.as_ref() == Some(k))
                    && filter.state.as_ref().is_none_or(|s| &i.item.state == s)
                    && filter
                        .feature
                        .as_ref()
                        .is_none_or(|f| i.features.contains(f))
                    && (filter.near.is_none() || near.is_some())
                    && region.is_none_or(|r| {
                        item_bounds(i).is_some_and(|b| {
                            b[0] <= r[2] && b[2] >= r[0] && b[1] <= r[3] && b[3] >= r[1]
                        })
                    })
            })
            .collect()
    }
    pub fn problems(&self, filter: &ProblemFilter) -> Vec<model::Problem> {
        let subjects: Vec<_> = filter
            .feature
            .as_ref()
            .map(|feature| {
                self.items(&ItemFilter {
                    feature: Some(feature.clone()),
                    ..ItemFilter::default()
                })
                .into_iter()
                .map(|i| i.item.id)
                .chain(std::iter::once(feature.clone()))
                .collect()
            })
            .unwrap_or_default();
        self.state
            .problems
            .iter()
            .filter(|p| {
                filter.code.as_ref().is_none_or(|c| &p.code == c)
                    && filter.severity.as_ref().is_none_or(|s| &p.severity == s)
                    && filter
                        .subject
                        .as_ref()
                        .is_none_or(|s| p.subjects.contains(s))
                    && (filter.feature.is_none() || p.subjects.iter().any(|s| subjects.contains(s)))
            })
            .cloned()
            .collect()
    }
    pub fn options(&self, id: &str) -> Result<Options, Error> {
        if let Some(face) = id.strip_prefix("face:") {
            let count = self.analysis.face_attachments.len();
            let index = face.parse::<usize>().ok().filter(|&i| i < count)
                .ok_or_else(|| Error::invalid(format!("{id:?} is not a face in this Analysis; use face:<index> with 0 <= index < {count}, or re-analyse if face evidence is absent.")))?;
            let fingerprint =
                crate::recognition_corrections::face_fingerprint(&self.analysis, index)?;
            return Ok(Options {
                id: id.into(),
                operations: vec!["add".into()],
                operation_scopes: BTreeMap::from([(
                    "add".into(),
                    crate::fixes::path_scope("feature_declarations"),
                )]),
                placements: vec![],
                view_anchors: vec![],
                scales: vec![],
                settable: scoped_fields(BTreeMap::from([(
                    "feature_declarations".into(),
                    serde_json::json!({"fields":[fingerprint],"forms":["add {id: f:declared:<name>, kind: hole|boss|turned_step|slot|pocket, faces: [fingerprint,…]}"]}),
                )])),
            });
        }
        if id.starts_with("f:") {
            let feature = self.analysis.features.iter().find(|f| f.id == id);
            let pattern = self
                .analysis
                .recognition
                .iter()
                .flat_map(|r| &r.patterns)
                .any(|p| p.id == id);
            if feature.is_none() && !pattern {
                return Err(Error::invalid(format!(
                    "There is no recognised feature or pattern {id:?}; use analyse to read the current feature and pattern IDs."
                )));
            }
            let mut fields = BTreeMap::new();
            let base = format!("features/{}", items::key(id));
            if let Some(f) = feature {
                fields.insert(
                    format!("{base}/suppress"),
                    serde_json::json!({"choices":[false,true]}),
                );
                let kinds = match f.kind.as_str() {
                    "boss" | "turned_step" => vec!["boss", "turned_step"],
                    "slot" | "section_recess" => vec!["slot", "pocket"],
                    _ => vec![],
                };
                if !kinds.is_empty() {
                    fields.insert(
                        format!("{base}/reclassify"),
                        serde_json::json!({"choices":kinds}),
                    );
                }
            }
            if pattern
                || feature.is_some_and(|f| matches!(f.kind.as_str(), "slot_pattern" | "slot"))
            {
                fields.insert(
                    format!("{base}/split"),
                    serde_json::json!({"choices":[false,true]}),
                );
            }
            return Ok(Options {
                id: id.into(),
                operations: vec!["set".into(), "remove".into()],
                operation_scopes: ["set", "remove"]
                    .map(|op| (op.into(), crate::fixes::path_scope("features")))
                    .into(),
                placements: vec![],
                view_anchors: vec![],
                scales: vec![],
                settable: scoped_fields(fields),
            });
        }
        Ok(self.options_for(self.item(id)?, &self.placement_choices()))
    }
    /// All item options, computing the shared geometry choices once and without planning.
    pub fn all_options(&self) -> BTreeMap<String, Options> {
        let choices = self.placement_choices();
        answers(self.plan(), &self.analysis)
            .into_iter()
            .map(|item| (item.item.id.clone(), self.options_for(item, &choices)))
            .collect()
    }
    fn placement_choices(&self) -> BTreeMap<String, Value> {
        let mut choices = crate::dims::placement_options_with_spec(
            &self.analysis,
            &self.plan().views,
            self.spec(),
        );
        // The Spec's datums and controls are placed as the file's PMI is ([`crate::spec_gdt`]).
        let merged = crate::spec_gdt::effective(&self.analysis, &self.plan().spec);
        choices.extend(crate::dims::gdt::placement_options(
            merged.as_deref().unwrap_or(&self.analysis),
            &self.plan().views,
        ));
        for n in &self.plan().notes {
            if n.id.starts_with("n:f:blend:") {
                choices.insert(n.id.clone(), serde_json::json!({"views":self.plan().views.iter().filter(|v| ["v:front","v:plan","v:side"].contains(&v.id.as_str())).map(|v| serde_json::json!({"view":v.id.trim_start_matches("v:"),"sides":["above","below","left","right"]})).collect::<Vec<_>>() }));
            }
        }
        choices
    }
    fn options_for(&self, item: ItemAnswer, choices: &BTreeMap<String, Value>) -> Options {
        let id = item.item.id.as_str();
        let angular = item.dimension_kind.as_deref() == Some("angular");
        let placements: Vec<Place> = choices
            .get(id)
            .into_iter()
            .flat_map(|c| {
                c["views"].as_array().into_iter().flatten().flat_map(|v| {
                    v["sides"]
                        .as_array()
                        .into_iter()
                        .flatten()
                        .map(Some)
                        .chain(angular.then_some(None))
                        .filter_map(move |side| {
                            Some(Place {
                                view: Some(v["view"].as_str()?.into()),
                                side: side
                                    .and_then(|side| serde_json::from_value(side.clone()).ok()),
                                lane: None,
                                along: Some(Along::Slot(Slot::Middle)),
                                offset: None,
                                pinned: false,
                            })
                        })
                })
            })
            .collect();
        let view_anchors: Vec<String> = if item.item.kind == "view" {
            self.plan()
                .views
                .iter()
                .filter(|v| v.id != id && !depends_on(self.spec(), &v.id, id))
                .map(|v| v.id.clone())
                .collect()
        } else {
            Vec::new()
        };
        Options {
            settable: settable(self, &item, &placements, &view_anchors),
            id: id.into(),
            operations: item.operations,
            operation_scopes: item.operation_scopes,
            placements,
            view_anchors,
            scales: if item.item.kind == "sheet" {
                self.spec()
                    .scales()
                    .iter()
                    .copied()
                    .map(model::scale_name)
                    .collect()
            } else {
                Vec::new()
            },
        }
    }
}
fn settable(
    session: &Session,
    item: &ItemAnswer,
    placements: &[Place],
    view_anchors: &[String],
) -> BTreeMap<String, Value> {
    use serde_json::json;
    let mut fields = BTreeMap::new();
    let merged = crate::recognition_corrections::effective(session.analysis(), session.spec()).ok();
    let analysis = merged.as_deref().unwrap_or(session.analysis());
    let id = &item.item.id;
    let choices = |values: Value| json!({"choices":values});
    if let Some(b) = session
        .plan()
        .inspection_balloons
        .iter()
        .find(|b| &b.id == id)
    {
        let base = format!("balloons/items/{}", items::key(&b.source));
        fields.insert(format!("{base}/suppress"), choices(json!([false, true])));
        fields.insert(format!("{base}/position"),json!({"forms":["[dx,dy]"],"consequences":["Paper mm from the drawn carrier's ink centre; an obstructed pin is reported and left undrawn."]}));
        return fields;
    }
    let base = match item.item.kind.as_str() {
        "dimension" => format!("dimensions/{}", items::key(id)),
        "view" => format!("views/{}", items::key(id.trim_start_matches("v:"))),
        "control_frame" | "datum_feature" | "label" | "surface_finish" | "note"
            if item.item.kind != "note" || id.starts_with("n:f:blend:") =>
        {
            format!("annotations/{}", items::key(id))
        }
        _ => String::new(),
    };

    fields.insert(
        "hole_table".into(),
        choices(json!(["auto", "always", "never"])),
    );
    if id == "sheet" {
        fields.insert("dimension_schemes/<name>".into(), json!({"forms":["{scheme:chain|baseline|ordinate,axis:x|y|z,view?:front|plan|side,targets?:[ID|fingerprint],from?:min|max|reference}"],"consequences":["Requires targets or view; rejects overlapping owners. Remove for auto."]}));
        fields.insert("dimensions/d:authored:<name>".into(), json!({"forms":["{between:[reference,reference],along:x|y|z|true,kind:linear}","{kind:diameter|radius,between:[face reference]}","{kind:angle,between:[reference,reference]}"],"consequences":["References are stable keys from Analysis.edge_ink.references; duplicate quantities are refused with their existing owner ID"]}));
    }
    if item.item.kind == "note" && id.starts_with("pmi:") {
        fields.insert(
            format!("annotations/{}/omit", items::key(id)),
            choices(json!([false, true])),
        );
    }
    if !base.is_empty() {
        fields.insert(format!("{base}/omit"), choices(json!([false, true])));
        if item.item.kind == "view" {
            for field in crate::linework::FIELDS {
                fields.insert(format!("{base}/{field}"), choices(json!([false, true])));
            }
            if matches!(id.as_str(), "v:iso" | "v:iso_below") {
                fields.insert(
                    format!("{base}/style"),
                    choices(json!(["wireframe", "shaded"])),
                );
                fields.insert(
                    format!("{base}/colour"),
                    json!({"forms":["[red,green,blue] (integers 0–255)"]}),
                );
                fields.insert(
                    format!("{base}/direction"),
                    json!({"forms":["[x,y,z] towards viewer; Z up"]}),
                );
            }
            // A profile view of a turned part may be drawn as a half section ([`crate::half_section`]).
            if matches!(id.as_str(), "v:front" | "v:plan" | "v:side") {
                fields.insert(
                    format!("{base}/half_section"),
                    choices(json!([false, true])),
                );
            }
            // Preserve default single-sheet answers until the author enables sheet controls.
            if session.spec().sheets.is_some() {
                let name = id.trim_start_matches("v:");
                let max = session
                    .spec()
                    .sheets
                    .as_ref()
                    .and_then(|s| s.max)
                    .unwrap_or(1);
                let mut consequences = Vec::new();
                for group in session
                    .spec()
                    .sheets
                    .iter()
                    .flat_map(|s| &s.keep_together)
                    .filter(|g| g.iter().any(|v| v == name))
                {
                    consequences.push(format!(
                        "Must share a sheet with keep_together group: {}",
                        group.join(", ")
                    ));
                }
                for (view, intent) in &session.spec().views {
                    if let Some(place) = &intent.place
                        && (view == name || place.relative_to == name)
                    {
                        consequences.push(format!(
                        "{view} must share a sheet with {}; remove views/{}/place to separate them",
                        place.relative_to,
                        items::key(view)
                    ));
                    }
                }
                if ["front", "plan", "side"].contains(&name) {
                    consequences.push("Principal views must stay on sheet 1".into());
                }
                fields.insert(format!("{base}/sheet"), json!({
                "choices":(1..=if ["front", "plan", "side"].contains(&name) {1} else {max}).collect::<Vec<_>>(),
                "consequences":consequences,
            }));
            }
            fields.insert(
                format!("{base}/place/relative_to"),
                choices(json!(
                    view_anchors
                        .iter()
                        .map(|v| v.trim_start_matches("v:"))
                        .collect::<Vec<_>>()
                )),
            );
            fields.insert(
                format!("{base}/place/side"),
                choices(json!(["above", "below", "left", "right"])),
            );
            fields.insert(format!("{base}/place/gap"), json!({"range":[0,1000],"consequences":["Clear paper distance in mm between the views’ annotation bands, not their part outlines (default 10 mm)."]}));
            let name = id.trim_start_matches("v:");
            let anchor = session
                .spec()
                .views
                .get(name)
                .and_then(|v| v.place.as_ref())
                .map(|p| p.relative_to.as_str())
                .or_else(|| {
                    view_anchors
                        .iter()
                        .map(|v| v.trim_start_matches("v:"))
                        .find(|anchor| crate::placement_intent::projection_related(name, anchor))
                })
                .unwrap_or("");
            fields.insert(
                format!("{base}/place/align"),
                choices(json!(crate::placement_intent::align_choices(name, anchor))),
            );
        } else {
            let mut views: Vec<_> = placements.iter().filter_map(|p| p.view.clone()).collect();
            views.sort();
            views.dedup();
            let mut sides: Vec<_> = placements
                .iter()
                .filter_map(|p| p.side.map(|s| s.name()))
                .collect();
            sides.sort();
            sides.dedup();
            fields.insert(format!("{base}/place/view"), choices(json!(views)));
            fields.insert(format!("{base}/place/side"), choices(json!(sides)));
            fields.insert(
                format!("{base}/place/lane"),
                json!({"range":[0,1000],"integer":true}),
            );
            fields.insert(
                format!("{base}/place/along"),
                json!({"choices":["start","middle","end","outside"],"range":[0,1]}),
            );
            // Free placement: callout bend from tip, or linear label from canonical label.
            if matches!(item.dimension_kind.as_deref(), Some("callout" | "linear")) {
                fields.insert(
                    format!("{base}/place/offset"),
                    json!({"range":[-1000,1000]}),
                );
            }
        }
        fields.insert(
            format!("{base}/place/pinned"),
            choices(json!([false, true])),
        );
        if item.item.kind == "dimension"
            || item.item.kind == "label"
            || id.starts_with("n:f:blend:")
        {
            fields.insert(
                format!("{base}/text"),
                json!({"fields":["prefix","suffix","template"],"forms":["{value} AFTER PLATING"]}),
            );
        }
        if item.item.kind == "dimension" {
            fields.insert(
                format!("{base}/style"),
                choices(json!(["reference", "basic"])),
            );
            fields.insert(
                format!("{base}/view"),
                fields[&format!("{base}/place/view")].clone(),
            );
            fields.insert(format!("{base}/tolerance"), json!({"forms":["±T","+U -L"]}));
            if session
                .spec()
                .dimensions
                .get(id)
                .is_some_and(|d| d.between.is_some())
            {
                fields.insert(
                    format!("{base}/kind"),
                    choices(json!(["linear", "diameter", "radius", "angle"])),
                );
                fields.insert(format!("{base}/between"), json!({"forms":["[reference, reference] (linear/angle)","[face reference] (diameter/radius)"],"consequences":["Copy stable keys from Analysis.edge_ink.references; axis:<face key> selects a cylinder axis"]}));
                fields.insert(
                    format!("{base}/along"),
                    choices(json!(["x", "y", "z", "true"])),
                );
            }
        }
    }
    // Recognition edits travel through the same transactional preview/apply and history.
    if id == "sheet" {
        fields.insert("features/f:hole_pattern:<name>/pattern".into(), json!({"fields":["arrangement", "members"], "forms":["{arrangement: linear|grid|bolt_circle, members: [hole id,…]}"]}));
        fields.insert("feature_declarations".into(), json!({
            "forms":["add {id: f:declared:<name>, kind: hole|boss|turned_step|slot|pocket, faces: [fingerprint,…]}",
                format!("Get a face fingerprint with item_options face:<index>, 0 <= index < {}", analysis.face_attachments.len())]
        }));
    }
    // A fit qualifies a feature, including each member of a consolidated hole mark.
    let mut fits: BTreeMap<Vec<String>, Vec<String>> = BTreeMap::new();
    for feature in &item.features {
        if let Some(f) = analysis.features.iter().find(|f| &f.id == feature) {
            let classes = crate::dims::fit_choices(
                &f.kind,
                f.parameters
                    .get("diameter")
                    .and_then(Value::as_f64)
                    .unwrap_or(0.0),
            );
            if !classes.is_empty() {
                fits.entry(classes)
                    .or_default()
                    .push(format!("features/{}/fit", items::key(feature)));
            }
        }
    }
    let callout_paths: Vec<_> =
        item.features
            .iter()
            .filter(|id| {
                analysis.features.iter().any(|f| {
                    &f.id == *id && matches!(f.kind.as_str(), "hole" | "turned_step" | "boss")
                })
            })
            .map(|id| format!("features/{}/callouts/style", items::key(id)))
            .collect();
    if let Some(path) = callout_paths.first() {
        let mut descriptor = choices(json!(["auto", "compact", "full", "note"]));
        if callout_paths.len() > 1 {
            descriptor["paths"] = json!(callout_paths);
        }
        fields.insert(path.clone(), descriptor);
    }
    for (classes, paths) in fits {
        let mut descriptor = choices(json!(classes));
        if paths.len() > 1 {
            descriptor["paths"] = json!(paths);
        }
        fields.insert(paths[0].clone(), descriptor);
    }
    if item.item.kind == "sheet" {
        fields.insert("units".into(), choices(json!(["mm", "in"])));
        fields.insert(
            "sheet/paper".into(),
            choices(json!(["iso", "ansi", "arch"])),
        );
        fields.insert(
            "sheet/callouts/style".into(),
            choices(json!(["auto", "compact", "full", "note"])),
        );
        for field in crate::linework::FIELDS {
            fields.insert(format!("sheet/{field}"), choices(json!([false, true])));
        }
    }
    // A datum letter and feature control frames qualify a feature or hole pattern
    // ([`crate::spec_gdt`]); a face takes them at faces/<side or face:n>/….
    let patterns = analysis
        .recognition
        .as_ref()
        .map_or(&[][..], |r| r.patterns.as_slice());
    // The feature the item's ID names (a hole's callout its hole, a hole pattern's its pattern),
    // once: offering every measured feature would outgrow the reply's wire budget.
    let named = item
        .item
        .id
        .strip_prefix("d:")
        .and_then(|s| s.rsplit_once('/'))
        .map(|(f, _)| f.to_string());
    if let Some(feature) = &named {
        let known = analysis.features.iter().any(|f| &f.id == feature)
            || patterns.iter().any(|p| &p.id == feature);
        if known {
            let base = format!("features/{}", items::key(feature));
            fields.insert(
                format!("{base}/callouts/style"),
                choices(json!(["auto", "compact", "full", "note"])),
            );
            fields.insert(format!("{base}/datum"), json!({"forms":["A"]}));
            fields.insert(
                format!("{base}/controls"),
                json!({
                    "fields": ["characteristic", "tolerance", "datums", "diameter", "modifier"],
                }),
            );
            // A finish and a note on any feature; a thread on a hole, hole pattern or turned
            // diameter; a knurl on a turned diameter ([`crate::spec_notes`]).
            fields.insert(format!("{base}/finish"), json!({"fields":["ra","removal"]}));
            fields.insert(format!("{base}/note"), json!({"forms":["text"]}));
            let kind = analysis
                .features
                .iter()
                .find(|f| &f.id == feature)
                .map_or("hole_pattern", |f| f.kind.as_str());
            if kind == "modelled_thread" {
                fields.insert(
                    format!("{base}/modelled_thread/depiction"),
                    choices(json!([false, true])),
                );
                fields.insert(
                    format!("{base}/modelled_thread/designation"),
                    json!({"forms":["text"],"description":"Replace the entire modelled-thread callout verbatim; no generated length suffix."}),
                );
            }
            if matches!(kind, "hole" | "hole_pattern" | "turned_step" | "boss") {
                fields.insert(
                    format!("{base}/thread"),
                    json!({"fields":["designation","length"]}),
                );
            }
            if matches!(kind, "turned_step" | "boss") {
                fields.insert(
                    format!("{base}/knurl"),
                    json!({"fields":["pattern","pitch"],"choices":["straight","diamond"]}),
                );
            }
        }
    }
    if item.item.kind == "sheet" {
        fields.insert("sheet/keep_layout".into(), json!({"choices":[true,false],"forms":["true with sheet/size, sheet/scale and sheets/max=1", "false/remove refits whole sheet and hand placements", "explicit paper/scale/count edits release the guard unless keep_layout is set in that batch"], "consequences":["Pins size/scale/count; reports moved views."]}));
        fields.insert(
            "sheet/size".into(),
            choices(json!((*session.spec().pages()).map(|p| p.0))),
        );
        fields.insert("sheet/preferred_max_sheets".into(), json!({"forms":["integer >= 1 (default 2)"], "consequences":["Soft preference; sheets/max remains hard."]}));
        fields.insert(
            "sheet/preferred_max_size".into(),
            choices(json!((*session.spec().pages()).map(|p| p.0))),
        );
        fields.insert(
            "sheet/scale".into(),
            choices(json!(
                session
                    .spec()
                    .scales()
                    .iter()
                    .copied()
                    .map(model::scale_name)
                    .collect::<Vec<_>>()
            )),
        );
        fields.insert(
            "sheet/frame_gutter".into(),
            json!({"forms":["0-20 mm, default 3"]}),
        );
        fields.insert("sheet/dimension_spacing".into(), json!({"choices":["normal","compact"], "consequences":["Unset: 10/7 mm lanes, then 7/5.5 mm on this sheet before a smaller scale or larger paper; normal forbids that; compact uses it."]}));
        fields.insert(
            "sheet/title".into(),
            json!({"fields":crate::title_block::KEYS.map(|k|k.0),"forms":["text"]}),
        );
        fields.insert(
            "sheet/view_gutter".into(),
            json!({"forms":["paper mm >= 0, default 25; 0 disables"]}),
        );
        fields.insert("sheet/uncited_datums".into(), choices(json!([true, false])));
        fields.insert("sheet/zones".into(), json!({"choices":[true,false], "consequences":["Zones/centring on by default; false hides uncited."]}));
        fields.insert("sheet/table_columns".into(), json!({"forms":["{id:1-4}"]}));
        fields.insert("sheet/references".into(), choices(json!([false, true])));
        fields.insert("sheet/fit_limits".into(), choices(json!([false, true])));
        fields.insert("sheet/fit_deviations".into(), choices(json!([false, true])));
        fields.insert(
            "sheet/projection".into(),
            choices(json!(model::PROJECTIONS)),
        );
        fields.insert(
            "sheet/default_finish".into(),
            json!({"fields":["ra","removal"]}),
        );
    }
    scoped_fields(fields)
}

fn scoped_fields(mut fields: BTreeMap<String, Value>) -> BTreeMap<String, Value> {
    for (path, descriptor) in &mut fields {
        let scope = descriptor["paths"]
            .as_array()
            .map(|paths| {
                let ops: Vec<_> = paths
                    .iter()
                    .filter_map(Value::as_str)
                    .map(|path| Op::set(path, Value::Null))
                    .collect();
                crate::fixes::scope(&ops)
            })
            .unwrap_or_else(|| crate::fixes::path_scope(path));
        // Invariant: Document fields and map keys have JSON representations; serialization performs no external I/O.
        descriptor["scope"] = serde_json::to_value(scope).unwrap();
    }
    fields
}

/// IDs remain unique; a qualifier checks ownership rather than changing identity.
pub(crate) fn item_address(address: &str) -> Result<(Option<u32>, &str), Error> {
    if let Some((prefix, id)) = address.split_once('/')
        && let Some(n) = prefix.strip_prefix("sheet:")
    {
        let sheet = n
            .parse::<u32>()
            .ok()
            .filter(|n| (1..=9).contains(n))
            .ok_or_else(|| Error::invalid("item addresses use sheet:<1..9>/<id>"))?;
        return Ok((Some(sheet), id));
    }
    Ok((None, address))
}

fn depends_on(spec: &Spec, view: &str, id: &str) -> bool {
    let mut at = view.trim_start_matches("v:");
    for _ in 0..spec.views.len() {
        let Some(p) = spec.views.get(at).and_then(|v| v.place.as_ref()) else {
            return false;
        };
        if p.relative_to == id.trim_start_matches("v:") {
            return true;
        }
        at = &p.relative_to;
    }
    false
}
fn annotation_changed(old: &Plan, new: &Plan, id: &str) -> bool {
    let carrier_sheet = |p: &Plan| {
        p.dimensions
            .iter()
            .find(|d| d.id == id)
            .map(|d| &d.view)
            .or_else(|| p.gdt.iter().find(|g| g.id == id).map(|g| &g.view))
            .and_then(|id| p.views.iter().find(|v| &v.id == id))
            .map(|v| v.sheet)
    };
    carrier_sheet(old) != carrier_sheet(new)
        || old
            .dimensions
            .iter()
            .find(|d| d.id == id)
            .map(|d| (&d.view, &d.kind, &d.terms, &d.measurements, &d.placed))
            != new
                .dimensions
                .iter()
                .find(|d| d.id == id)
                .map(|d| (&d.view, &d.kind, &d.terms, &d.measurements, &d.placed))
        || old
            .gdt
            .iter()
            .find(|d| d.id == id)
            .map(|d| (&d.view, &d.cells, &d.placed))
            != new
                .gdt
                .iter()
                .find(|d| d.id == id)
                .map(|d| (&d.view, &d.cells, &d.placed))
}
fn ink(plan: &Plan, old: Option<&State>) -> BTreeMap<String, RenderedItem> {
    let owners = if plan.sheets.is_empty() {
        Vec::new()
    } else {
        items::items(plan)
    };
    plan.dimensions
        .iter()
        .map(|d| (&d.id, AnnotationItem::Dimension(d)))
        .chain(plan.gdt.iter().map(|g| (&g.id, AnnotationItem::Gdt(g))))
        .map(|(id, item)| {
            let kept = old
                .filter(|s| !annotation_changed(&s.plan, plan, id))
                .and_then(|s| s.annotations.get(id))
                .cloned();
            let mut rendered = kept.unwrap_or_else(|| model::render_item(item));
            rendered.provenance = owners
                .iter()
                .find(|i| &i.id == id)
                .cloned()
                .into_iter()
                .collect();
            (id.clone(), rendered)
        })
        .collect()
}
fn answers(plan: &Plan, analysis: &Analysis) -> Vec<ItemAnswer> {
    items::items(plan)
        .into_iter()
        .map(|item| {
            let id = &item.id;
            let mut a = ItemAnswer {
                value: None,
                dimension_kind: None,
                placement: None,
                anchors: Vec::new(),
                features: Vec::new(),
                basis: String::new(),
                operations: Vec::new(),
                operation_scopes: BTreeMap::new(),
                item: item.clone(),
            };
            if item.kind == "sheet" {
                a.basis = std::iter::once(&plan.sheet)
                    .chain(&plan.sheets)
                    .find(|s| s.number == item.sheet)
                    // Invariant: Sheet items are generated directly from this plan's sheet collection.
                    .expect("a sheet item has a planned sheet")
                    .basis
                    .clone();
                a.operations = ["size", "scale", "title"].map(String::from).into();
            }
            if let Some(v) = plan.views.iter().find(|v| &v.id == id) {
                a.placement = Some(Placement::View {
                    at: v.at,
                    area: v.area,
                    scale: v.scale,
                });
                a.basis = v.basis.clone();
                a.operations = ["omit", "place", "pin"].map(String::from).into();
                a.operations
                    .extend(crate::linework::FIELDS.map(String::from));
                if plan.spec.sheets.is_some() {
                    a.operations.push("move_to_sheet".into());
                }
                a.anchors = plan
                    .spec
                    .views
                    .get(id.trim_start_matches("v:"))
                    .and_then(|v| v.place.as_ref())
                    .map(|p| vec![format!("v:{}", p.relative_to)])
                    .unwrap_or_default();
            }
            if let Some(d) = plan.dimensions.iter().find(|d| &d.id == id) {
                a.value = Some(d.value);
                a.dimension_kind = Some(d.kind.clone());
                a.placement = d.placed.clone().map(Placement::Dimension);
                a.basis = d.basis.clone();
                a.features = d.measurements.iter().map(|m| m.feature.clone()).collect();
                a.features.sort();
                a.features.dedup();
                a.anchors = vec![d.view.clone()];
                a.anchors.extend(a.features.clone());
                for m in &d.measurements {
                    if let Some(refs) = m.feature.strip_prefix("geometry:") {
                        a.anchors.extend(refs.split('|').map(String::from));
                    }
                }
                a.operations = ["omit", "tolerance", "text", "style", "place", "pin"]
                    .map(String::from)
                    .into();
                if d.from_spec.iter().any(|field| field == "between") {
                    a.operations
                        .extend(["between", "kind", "along"].map(String::from));
                }
            }
            if let Some(o) = plan.omitted.iter().find(|o| &o.id == id) {
                a.basis = o.basis.clone();
                if let Some(t) = &plan.radius_table {
                    a.features.extend(
                        t.rows
                            .iter()
                            .filter(|r| r.dimension == *id)
                            .map(|r| r.feature.clone()),
                    );
                }
                if let Some((feature, _)) = id.strip_prefix("d:").and_then(|s| s.rsplit_once('/'))
                    && !a.features.iter().any(|f| f == feature)
                {
                    a.features.push(feature.into());
                }
                a.operations = vec!["omit".into()];
                if let ItemState::Covered { by } = &o.state {
                    a.anchors.push(by.clone());
                }
            }
            if let Some(g) = plan.gdt.iter().find(|g| &g.id == id) {
                a.basis = g.basis.clone();
                a.placement = g.placed.clone().map(Placement::Annotation);
                a.anchors = vec![g.view.clone()];
                a.anchors
                    .extend(g.faces.iter().map(|face| format!("face:{face}")));
                a.features = analysis
                    .features
                    .iter()
                    .filter(|f| f.faces.iter().any(|face| g.faces.contains(face)))
                    .map(|f| f.id.clone())
                    .collect();
                a.operations = ["omit", "place", "pin"].map(String::from).into();
                if g.kind == "label" {
                    a.operations.push("text".into());
                }
            }
            if let Some(c) = plan.captions.iter().find(|c| &c.id == id) {
                a.basis = c.basis.clone();
                a.placement = Some(Placement::Box(c.ink));
                a.anchors = vec![c.view.clone()];
            }
            if let Some(n) = plan.notes.iter().find(|n| &n.id == id) {
                a.basis = n.basis.clone();
                a.placement = n.placed.as_ref().map(|p| Placement::Box(p.cells));
                a.features = n.measurements.iter().map(|m| m.feature.clone()).collect();
                if n.id.starts_with("n:f:blend:") {
                    a.operations = ["omit", "text", "place"].map(String::from).into();
                } else if n.id.starts_with("pmi:") {
                    a.operations = vec!["omit".into()];
                }
            }
            if let Some(r) = plan.requirements.iter().find(|r| &r.id == id) {
                a.basis = r.basis.clone();
                a.anchors = vec!["sheet".into()];
                if let Some(p) = &r.placed {
                    a.placement = Some(Placement::Box(p.glyph));
                }
            }
            if let Some(t) = plan.tables.iter().find(|t| &t.id == id) {
                a.basis = t.basis.clone();
                a.placement = t.placed.as_ref().map(|p| Placement::Box(p.cells));
            }
            if let Some(t) = plan.radius_table.as_ref().filter(|t| &t.table.id == id) {
                a.basis = t.table.basis.clone();
                a.placement = t.table.placed.as_ref().map(|p| Placement::Box(p.cells));
                a.features = t.rows.iter().map(|r| r.feature.clone()).collect();
            }
            if let Some(t) = plan
                .hole_table
                .iter()
                .chain(&plan.hole_tables)
                .find(|t| &t.id == id)
            {
                a.basis = t.basis.clone();
                a.features = t.rows.iter().map(|r| r.feature.clone()).collect();
                a.anchors = vec![t.view.clone().unwrap_or_else(|| "v:plan".into())];
            }
            if let Some(b) = plan.balloons.iter().find(|b| &b.id == id) {
                a.features = b.features.clone();
                a.anchors = vec![b.view.clone()];
                a.placement = Some(Placement::Box([
                    b.at[0] - b.radius,
                    b.at[1] - b.radius,
                    b.at[0] + b.radius,
                    b.at[1] + b.radius,
                ]));
            }
            if let Some(b) = plan.inspection_balloons.iter().find(|b| &b.id == id) {
                a.anchors = vec![b.item.clone()];
                a.placement = Some(Placement::Box(b.bounds()));
                a.basis = format!(
                    "File-owned inspection characteristic {} ({})",
                    b.number, b.source
                );
                a.operations = vec!["suppress".into(), "position".into()];
            }
            a.operation_scopes = a
                .operations
                .iter()
                .map(|operation| {
                    let base =
                        if let Some(b) = plan.inspection_balloons.iter().find(|b| &b.id == id) {
                            format!("balloons/items/{}", items::key(&b.source))
                        } else if item.kind == "sheet" {
                            "sheet".into()
                        } else if item.kind == "view" {
                            format!("views/{}", items::key(id.trim_start_matches("v:")))
                        } else if item.kind == "dimension" {
                            format!("dimensions/{}", items::key(id))
                        } else {
                            format!("annotations/{}", items::key(id))
                        };
                    let field = match operation.as_str() {
                        "pin" => "place/pinned",
                        "move_to_sheet" => "sheet",
                        other => other,
                    };
                    (
                        operation.clone(),
                        crate::fixes::path_scope(&format!("{base}/{field}")),
                    )
                })
                .collect();
            a
        })
        .collect()
}
fn expand_box<'a>(b: [f64; 4], points: impl Iterator<Item = &'a [f64; 2]>) -> [f64; 4] {
    points.fold(b, |b, p| {
        [
            b[0].min(p[0]),
            b[1].min(p[1]),
            b[2].max(p[0]),
            b[3].max(p[1]),
        ]
    })
}
pub fn item_bounds(i: &ItemAnswer) -> Option<[f64; 4]> {
    match i.placement.as_ref()? {
        Placement::Dimension(p) => Some(expand_box(
            p.label_box,
            p.lines.iter().chain(&p.arrows).flatten(),
        )),
        Placement::Annotation(p) => Some(expand_box(
            p.label_box,
            p.lines.iter().chain(&p.arrows).flatten(),
        )),
        Placement::View { area, .. } | Placement::Box(area) => Some(*area),
    }
}
fn item_records(doc: &Value) -> BTreeMap<String, Vec<Value>> {
    let mut out = BTreeMap::<String, Vec<Value>>::new();
    out.insert("sheet".into(), vec![doc["sheet"].clone()]);
    for name in COLLECTIONS.into_iter().filter(|name| *name != "problems") {
        for (id, record) in records(&doc[name]) {
            out.entry(id).or_default().push(record);
        }
    }
    if let Some(id) = doc["radius_table"]["table"]["id"].as_str() {
        out.entry(id.into())
            .or_default()
            .push(doc["radius_table"].clone());
    }
    if let Some(id) = doc["hole_table"]["id"].as_str() {
        out.entry(id.into())
            .or_default()
            .push(doc["hole_table"].clone());
    }
    out
}
fn difference(old: &State, new: &State, analysis: &Analysis, plans: usize) -> Delta {
    let before: BTreeMap<_, _> = answers(&old.plan, analysis)
        .into_iter()
        .map(|i| (i.item.id.clone(), i))
        .collect();
    let after = answers(&new.plan, analysis);
    // Item comparison and the lossless Plan patch read the same documents. Serialize
    // each once, including every remedy; retained-frame metadata must not multiply
    // that work. The records and patch below retain their complete previous inputs.
    let old_document = document(&old.plan);
    let new_document = document(&new.plan);
    let old_records = item_records(&old_document);
    let new_records = item_records(&new_document);
    let new_ids: Vec<_> = after.iter().map(|i| i.item.id.clone()).collect();
    let changed: Vec<_> = after
        .iter()
        .filter(|i| {
            before.get(&i.item.id).is_some_and(|b| {
                old_records.get(&i.item.id) != new_records.get(&i.item.id)
                    // Invariant: Document fields and map keys have JSON representations; serialization performs no external I/O.
                    || serde_json::to_value(b).unwrap() != serde_json::to_value(i).unwrap()
            })
        })
        .cloned()
        .collect();
    let geometry = GeometryLayer {
        views: new
            .geometry
            .views
            .iter()
            .filter(|(id, _)| {
                let a = old.plan.views.iter().find(|v| &v.id == id);
                let b = new
                    .plan
                    .views
                    .iter()
                    .find(|v| &v.id == id)
                    // Invariant: render_geometry emits entries only for views in this same new plan.
                    .expect("geometry has a planned view");
                a.is_none_or(|a| {
                    a.sheet != b.sheet
                        || a.at != b.at
                        || a.scale != b.scale
                        || a.direction != b.direction
                        || a.up != b.up
                        || a.cut != b.cut
                        || a.line_style != b.line_style
                        || old.plan.spec.views.get(id.trim_start_matches("v:"))
                            != new.plan.spec.views.get(id.trim_start_matches("v:"))
                })
            })
            .cloned()
            .collect(),
    };
    let view_ids = |s: &State| {
        s.geometry
            .views
            .iter()
            .map(|(id, _)| id.clone())
            .collect::<Vec<_>>()
    };
    Delta {
        plans,
        notices: crate::local_layout::notice(&old.plan, &new.plan)
            .into_iter()
            .collect(),
        changed,
        added: after
            .into_iter()
            .filter(|i| !before.contains_key(&i.item.id))
            .collect(),
        removed: before
            .keys()
            .filter(|id| !new_ids.contains(id))
            .cloned()
            .collect(),
        problems_added: new
            .problems
            .iter()
            .filter(|p| {
                !old.problems
                    .iter()
                    .any(|q| unchanged_problem(q, p, old, new))
            })
            .cloned()
            .collect(),
        problems_resolved: old
            .problems
            .iter()
            .filter(|p| {
                !new.problems
                    .iter()
                    .any(|q| unchanged_problem(p, q, old, new))
            })
            .cloned()
            .collect(),
        plan: patch(&old_document, &new_document),
        geometry,
        annotations: new
            .annotations
            .iter()
            .filter(|(id, v)| {
                annotation_changed(&old.plan, &new.plan, id)
                    || old.annotations.get(*id).map(|a| &a.provenance) != Some(&v.provenance)
            })
            .map(|(id, v)| (id.clone(), v.clone()))
            .collect(),
        geometry_order: (view_ids(old) != view_ids(new)).then(|| view_ids(new)),
        problem_order: (old.problems.iter().map(|p| &p.id).collect::<Vec<_>>()
            != new.problems.iter().map(|p| &p.id).collect::<Vec<_>>())
        .then(|| new.problems.iter().map(|p| p.id.clone()).collect()),
    }
}

// Finding identity includes its engineering/layout evidence, but not its remedies.
// Only settled Plan problems have a separate lossless metadata patch; render-only
// problems retain complete equality in the added/resolved stream.
fn same_finding(a: &model::Problem, b: &model::Problem) -> bool {
    a.id == b.id
        && a.code == b.code
        && a.severity == b.severity
        && a.message == b.message
        && a.subjects == b.subjects
        && a.refs == b.refs
}

fn unchanged_problem(a: &model::Problem, b: &model::Problem, old: &State, new: &State) -> bool {
    a == b || (same_finding(a, b) && old.plan.problems.contains(a) && new.plan.problems.contains(b))
}